blob: 93231a8becf5d7ceb08941c434b55ab27b219d0c [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 Vasko7fbc8162018-09-17 10:35:16 +020014
Radek Krejcie7b95092019-05-15 11:03:07 +020015#include <assert.h>
16#include <ctype.h>
17#include <errno.h>
18#include <stdint.h>
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22
Radek Krejci535ea9f2020-05-29 16:01:05 +020023#include "common.h"
Michal Vasko7fbc8162018-09-17 10:35:16 +020024#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020025#include "dict.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020026#include "log.h"
Michal Vasko004d3152020-06-11 19:59:22 +020027#include "path.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020028#include "parser_schema.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020029#include "set.h"
30#include "tree.h"
31#include "tree_schema.h"
Radek Krejci70853c52018-10-15 14:46:16 +020032#include "tree_schema_internal.h"
Radek Krejci44ceedc2018-10-02 15:54:31 +020033
Radek Krejciceaf2122019-01-02 15:03:26 +010034/**
35 * @brief Insert WORD into the libyang context's dictionary and store as TARGET.
36 * @param[in] CTX yang parser context to access libyang context.
37 * @param[in] BUF buffer in case the word is not a constant and can be inserted directly (zero-copy)
38 * @param[out] TARGET variable where to store the pointer to the inserted value.
39 * @param[in] WORD string to store.
40 * @param[in] LEN length of the string in WORD to store.
41 */
Radek Krejci9fcacc12018-10-11 15:59:11 +020042#define INSERT_WORD(CTX, BUF, TARGET, WORD, LEN) \
43 if (BUF) {(TARGET) = lydict_insert_zc((CTX)->ctx, WORD);}\
Radek Krejcif09e4e82019-06-14 15:08:11 +020044 else {(TARGET) = lydict_insert((CTX)->ctx, LEN ? WORD : "", LEN);}
Radek Krejci44ceedc2018-10-02 15:54:31 +020045
Radek Krejciceaf2122019-01-02 15:03:26 +010046/**
47 * @brief Move the DATA pointer by COUNT items. Also updates the indent value in yang parser context
48 * @param[in] CTX yang parser context to update its indent value.
49 * @param[in,out] DATA pointer to move
50 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
51 */
Radek Krejci2b610482019-01-02 13:36:09 +010052#define MOVE_INPUT(CTX, DATA, COUNT) (*(DATA))+=COUNT;(CTX)->indent+=COUNT
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020053
Michal Vaskoea5abea2018-09-18 13:10:54 +020054/**
55 * @brief Loop through all substatements providing, return if there are none.
56 *
Radek Krejci44ceedc2018-10-02 15:54:31 +020057 * @param[in] CTX yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +020058 * @param[in] DATA Raw data to read from.
59 * @param[out] KW YANG keyword read.
60 * @param[out] WORD Pointer to the keyword itself.
61 * @param[out] WORD_LEN Length of the keyword.
62 * @param[out] ERR Variable for error storing.
63 *
64 * @return In case there are no substatements or a fatal error encountered.
65 */
Radek Krejci6d6556c2018-11-08 09:37:45 +010066#define YANG_READ_SUBSTMT_FOR(CTX, DATA, KW, WORD, WORD_LEN, ERR, CHECKGOTO) \
Radek Krejci6d9b9b52018-11-02 12:43:39 +010067 LY_CHECK_RET(get_keyword(CTX, DATA, &KW, &WORD, &WORD_LEN)); \
Radek Krejcid6b76452019-09-03 17:03:03 +020068 if (KW == LY_STMT_SYNTAX_SEMICOLON) { \
Radek Krejci6d6556c2018-11-08 09:37:45 +010069 CHECKGOTO; \
Radek Krejci6d9b9b52018-11-02 12:43:39 +010070 return LY_SUCCESS; \
Michal Vasko7fbc8162018-09-17 10:35:16 +020071 } \
Radek Krejcid6b76452019-09-03 17:03:03 +020072 if (KW != LY_STMT_SYNTAX_LEFT_BRACE) { \
David Sedlákb3077192019-06-19 10:55:37 +020073 LOGVAL_PARSER(CTX, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\", expected \";\" or \"{\".", ly_stmt2str(KW)); \
Michal Vasko7fbc8162018-09-17 10:35:16 +020074 return LY_EVALID; \
75 } \
76 for (ERR = get_keyword(CTX, DATA, &KW, &WORD, &WORD_LEN); \
Radek Krejcid6b76452019-09-03 17:03:03 +020077 !ERR && (KW != LY_STMT_SYNTAX_RIGHT_BRACE); \
Michal Vasko7fbc8162018-09-17 10:35:16 +020078 ERR = get_keyword(CTX, DATA, &KW, &WORD, &WORD_LEN))
79
Michal Vaskob36053d2020-03-26 15:49:30 +010080LY_ERR parse_container(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
81LY_ERR parse_uses(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
82LY_ERR parse_choice(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
83LY_ERR parse_case(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
84LY_ERR parse_list(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
85LY_ERR parse_grouping(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_grp **groupings);
Michal Vasko7fbc8162018-09-17 10:35:16 +020086
Michal Vaskoea5abea2018-09-18 13:10:54 +020087/**
88 * @brief Add another character to dynamic buffer, a low-level function.
89 *
Radek Krejci44ceedc2018-10-02 15:54:31 +020090 * Enlarge if needed. Updates \p input as well as \p buf_used.
Michal Vaskoea5abea2018-09-18 13:10:54 +020091 *
Radek Krejci404251e2018-10-09 12:06:44 +020092 * @param[in] ctx libyang context for logging.
Radek Krejci44ceedc2018-10-02 15:54:31 +020093 * @param[in, out] input Input string to process.
94 * @param[in] len Number of bytes to get from the input string and copy into the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +020095 * @param[in,out] buf Buffer to use, can be moved by realloc().
96 * @param[in,out] buf_len Current size of the buffer.
Radek Krejci44ceedc2018-10-02 15:54:31 +020097 * @param[in,out] buf_used Currently used characters of the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +020098 *
99 * @return LY_ERR values.
100 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200101LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +0200102buf_add_char(struct ly_ctx *ctx, const char **input, size_t len, char **buf, size_t *buf_len, size_t *buf_used)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200103{
Radek Krejci44ceedc2018-10-02 15:54:31 +0200104 if (*buf_len <= (*buf_used) + len) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200105 *buf_len += 16;
106 *buf = ly_realloc(*buf, *buf_len);
107 LY_CHECK_ERR_RET(!*buf, LOGMEM(ctx), LY_EMEM);
108 }
Radek Krejcic0917392019-04-10 13:04:04 +0200109 if (*buf_used) {
110 memcpy(&(*buf)[*buf_used], *input, len);
111 } else {
112 memcpy(*buf, *input, len);
113 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200114
Radek Krejci44ceedc2018-10-02 15:54:31 +0200115 (*buf_used) += len;
116 (*input) += len;
117
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.
125 * @param[in,out] input Pointer to the string where to get the character to store. Automatically moved to the next character
126 * when function returns.
127 * @param[in] arg Type of the input string to select method of checking character validity.
128 * @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 +0200129 * 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 +0200130 * @param[in,out] word_len Current length of the word pointed to by \p word_p.
131 * @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 +0200132 * @param[in,out] buf_len Current length of \p word_b.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200133 * @param[in] need_buf Flag if the dynamically allocated buffer is required.
David Sedlák40bb13b2019-07-10 14:34:18 +0200134 * @param[in,out] prefix Storage for internally used flag in case of possible prefixed identifiers:
135 * 0 - colon not yet found (no prefix)
136 * 1 - \p c is the colon character
137 * 2 - prefix already processed, now processing the identifier
Michal Vaskoea5abea2018-09-18 13:10:54 +0200138 *
139 * @return LY_ERR values.
140 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200141LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100142buf_store_char(struct lys_yang_parser_ctx *ctx, const char **input, enum yang_arg arg, char **word_p,
David Sedlák40bb13b2019-07-10 14:34:18 +0200143 size_t *word_len, char **word_b, size_t *buf_len, int need_buf, int *prefix)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200144{
Radek Krejci44ceedc2018-10-02 15:54:31 +0200145 unsigned int c;
146 size_t len;
147
Radek Krejcif29b7c32019-04-09 16:17:49 +0200148 /* check valid combination of input paremeters - if need_buf specified, word_b must be provided */
149 assert(!need_buf || (need_buf && word_b));
150
Radek Krejci44ceedc2018-10-02 15:54:31 +0200151 /* get UTF8 code point (and number of bytes coding the character) */
152 LY_CHECK_ERR_RET(ly_getutf8(input, &c, &len),
David Sedlákb3077192019-06-19 10:55:37 +0200153 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, (*input)[-len]), LY_EVALID);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200154 (*input) -= len;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200155 if (c == '\n') {
156 ctx->indent = 0;
157 } else {
158 /* note - even the multibyte character is count as 1 */
159 ++ctx->indent;
160 }
161
Radek Krejci44ceedc2018-10-02 15:54:31 +0200162 /* check character validity */
163 switch (arg) {
164 case Y_IDENTIF_ARG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100165 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !(*word_len), NULL));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200166 break;
167 case Y_PREF_IDENTIF_ARG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100168 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !(*word_len), prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200169 break;
170 case Y_STR_ARG:
171 case Y_MAYBE_STR_ARG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100172 LY_CHECK_RET(lysp_check_stringchar((struct lys_parser_ctx *)ctx, c));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200173 break;
174 }
175
Michal Vasko7fbc8162018-09-17 10:35:16 +0200176 if (word_b && *word_b) {
177 /* add another character into buffer */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200178 if (buf_add_char(ctx->ctx, input, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200179 return LY_EMEM;
180 }
181
182 /* in case of realloc */
183 *word_p = *word_b;
Radek Krejcif29b7c32019-04-09 16:17:49 +0200184 } else if (word_b && need_buf) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200185 /* first time we need a buffer, copy everything read up to now */
186 if (*word_len) {
187 *word_b = malloc(*word_len);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200188 LY_CHECK_ERR_RET(!*word_b, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200189 *buf_len = *word_len;
190 memcpy(*word_b, *word_p, *word_len);
191 }
192
193 /* add this new character into buffer */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200194 if (buf_add_char(ctx->ctx, input, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200195 return LY_EMEM;
196 }
197
198 /* in case of realloc */
199 *word_p = *word_b;
200 } else {
201 /* just remember the first character pointer */
202 if (!*word_p) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200203 *word_p = (char *)(*input);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200204 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200205 /* ... and update the word's length */
206 (*word_len) += len;
207 (*input) += len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200208 }
209
210 return LY_SUCCESS;
211}
212
Michal Vaskoea5abea2018-09-18 13:10:54 +0200213/**
214 * @brief Skip YANG comment in data.
215 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200216 * @param[in] ctx yang parser context for logging.
217 * @param[in,out] data Data to read from, automatically moved after the comment.
218 * @param[in] comment Type of the comment to process:
219 * 1 for a one-line comment,
220 * 2 for a block comment.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200221 *
222 * @return LY_ERR values.
223 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200224LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100225skip_comment(struct lys_yang_parser_ctx *ctx, const char **data, int comment)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200226{
Radek Krejci80dd33e2018-09-26 15:57:18 +0200227 /* internal statuses: 0 - comment ended,
228 * 1 - in line comment,
229 * 2 - in block comment,
230 * 3 - in block comment with last read character '*'
231 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200232 while (**data && comment) {
233 switch (comment) {
234 case 1:
235 if (**data == '\n') {
236 comment = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200237 ++ctx->line;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200238 }
239 break;
240 case 2:
241 if (**data == '*') {
Radek Krejci15c80ca2018-10-09 11:01:31 +0200242 comment = 3;
243 } else if (**data == '\n') {
244 ++ctx->line;
245 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200246 break;
247 case 3:
248 if (**data == '/') {
249 comment = 0;
Radek Krejci5b930492019-06-11 14:54:08 +0200250 } else if (**data != '*') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200251 if (**data == '\n') {
252 ++ctx->line;
253 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200254 comment = 2;
255 }
256 break;
257 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200258 LOGINT_RET(ctx->ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200259 }
260
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200261 if (**data == '\n') {
262 ctx->indent = 0;
263 } else {
264 ++ctx->indent;
265 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200266 ++(*data);
267 }
268
269 if (!**data && (comment > 1)) {
David Sedlákb3077192019-06-19 10:55:37 +0200270 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Unexpected end-of-input, non-terminated comment.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200271 return LY_EVALID;
272 }
273
274 return LY_SUCCESS;
275}
276
Michal Vaskoea5abea2018-09-18 13:10:54 +0200277/**
278 * @brief Read a quoted string from data.
279 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200280 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200281 * @param[in,out] data Data to read from, always moved to currently handled character.
282 * @param[in] arg Type of YANG keyword argument expected.
283 * @param[out] word_p Pointer to the read quoted string.
284 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read quoted string. If not needed,
285 * set to NULL. Otherwise equal to \p word_p.
286 * @param[out] word_len Length of the read quoted string.
287 * @param[out] buf_len Length of the dynamically-allocated buffer \p word_b.
288 * @param[in] indent Current indent (number of YANG spaces). Needed for correct multi-line string
289 * indenation in the final quoted string.
290 *
291 * @return LY_ERR values.
292 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200293static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100294read_qstring(struct lys_yang_parser_ctx *ctx, const char **data, enum yang_arg arg, char **word_p, char **word_b,
295 size_t *word_len, size_t *buf_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200296{
297 /* string: 0 - string ended, 1 - string with ', 2 - string with ", 3 - string with " with last character \,
298 * 4 - string finished, now skipping whitespaces looking for +,
299 * 5 - string continues after +, skipping whitespaces */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200300 unsigned int string, block_indent = 0, current_indent = 0, need_buf = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200301 const char *c;
David Sedlák40bb13b2019-07-10 14:34:18 +0200302 int prefix = 0;
Michal Vasko90edde42019-11-25 15:25:07 +0100303 unsigned int trailing_ws = 0; /* current number of stored trailing whitespace characters */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200304
305 if (**data == '\"') {
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 {
309 assert(**data == '\'');
310 string = 1;
311 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200312 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200313
314 while (**data && string) {
315 switch (string) {
316 case 1:
317 switch (**data) {
318 case '\'':
319 /* string may be finished, but check for + */
320 string = 4;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200321 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200322 break;
323 default:
324 /* check and store character */
David Sedlák40bb13b2019-07-10 14:34:18 +0200325 LY_CHECK_RET(buf_store_char(ctx, data, 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:
330 switch (**data) {
331 case '\"':
332 /* string may be finished, but check for + */
333 string = 4;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200334 MOVE_INPUT(ctx, data, 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 */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200345 ++(*data);
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;
357 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200358 } else {
Michal Vasko90edde42019-11-25 15:25:07 +0100359 /* check and store whitespace character */
David Sedlák40bb13b2019-07-10 14:34:18 +0200360 LY_CHECK_RET(buf_store_char(ctx, data, 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;
369 for (; current_indent > block_indent; --current_indent, --ctx->indent) {
370 /* store leftover spaces from the tab */
371 c = " ";
David Sedlák40bb13b2019-07-10 14:34:18 +0200372 LY_CHECK_RET(buf_store_char(ctx, &c, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko90edde42019-11-25 15:25:07 +0100373 trailing_ws++;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200374 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200375 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200376 } else {
Michal Vasko90edde42019-11-25 15:25:07 +0100377 /* check and store whitespace character */
David Sedlák40bb13b2019-07-10 14:34:18 +0200378 LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko90edde42019-11-25 15:25:07 +0100379 trailing_ws++;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200380 /* additional characters for indentation - only 1 was count in buf_store_char */
381 ctx->indent += 7;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200382 }
383 break;
384 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200385 if (block_indent) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200386 /* we will be removing the indents so we need our own buffer */
387 need_buf = 1;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200388
389 /* remove trailing tabs and spaces */
Radek Krejciff13cd12019-10-25 15:34:24 +0200390 (*word_len) = *word_len - trailing_ws;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200391
Radek Krejciff13cd12019-10-25 15:34:24 +0200392 /* restart indentation */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200393 current_indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200394 }
395
396 /* check and store character */
David Sedlák40bb13b2019-07-10 14:34:18 +0200397 LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200398
399 /* maintain line number */
400 ++ctx->line;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200401
402 /* reset context indentation counter for possible string after this one */
403 ctx->indent = 0;
Radek Krejciff13cd12019-10-25 15:34:24 +0200404 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200405 break;
406 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200407 /* first non-whitespace character, stop eating indentation */
408 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200409
410 /* check and store character */
David Sedlák40bb13b2019-07-10 14:34:18 +0200411 LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Radek Krejciff13cd12019-10-25 15:34:24 +0200412 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200413 break;
414 }
415 break;
416 case 3:
417 /* string encoded characters */
418 switch (**data) {
419 case 'n':
420 c = "\n";
421 break;
422 case 't':
423 c = "\t";
424 break;
425 case '\"':
426 c = *data;
427 break;
428 case '\\':
429 c = *data;
430 break;
431 default:
David Sedlákb3077192019-06-19 10:55:37 +0200432 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Double-quoted string unknown special character '\\%c'.", **data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200433 return LY_EVALID;
434 }
435
436 /* check and store character */
David Sedlák40bb13b2019-07-10 14:34:18 +0200437 LY_CHECK_RET(buf_store_char(ctx, &c, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200438
439 string = 2;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200440 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200441 break;
442 case 4:
443 switch (**data) {
444 case '+':
445 /* string continues */
446 string = 5;
Radek Krejciefd22f62018-09-27 11:47:58 +0200447 need_buf = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200448 break;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200449 case '\n':
450 ++ctx->line;
451 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200452 case ' ':
453 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200454 /* just skip */
455 break;
456 default:
457 /* string is finished */
458 goto string_end;
459 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200460 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200461 break;
462 case 5:
463 switch (**data) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200464 case '\n':
465 ++ctx->line;
466 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200467 case ' ':
468 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200469 /* skip */
470 break;
471 case '\'':
472 string = 1;
473 break;
474 case '\"':
475 string = 2;
476 break;
477 default:
478 /* it must be quoted again */
David Sedlákb3077192019-06-19 10:55:37 +0200479 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Both string parts divided by '+' must be quoted.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200480 return LY_EVALID;
481 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200482 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200483 break;
484 default:
485 return LY_EINT;
486 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200487 }
488
489string_end:
Radek Krejci4e199f52019-05-28 09:09:28 +0200490 if (arg <= Y_PREF_IDENTIF_ARG && !(*word_len)) {
491 /* empty identifier */
David Sedlákb3077192019-06-19 10:55:37 +0200492 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Statement argument is required.");
Radek Krejci4e199f52019-05-28 09:09:28 +0200493 return LY_EVALID;
494 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200495 return LY_SUCCESS;
496}
497
Michal Vaskoea5abea2018-09-18 13:10:54 +0200498/**
499 * @brief Get another YANG string from the raw data.
500 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200501 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200502 * @param[in,out] data Data to read from, always moved to currently handled character.
503 * @param[in] arg Type of YANG keyword argument expected.
Radek Krejcid3ca0632019-04-16 16:54:54 +0200504 * @param[out] flags optional output argument to get flag of the argument's quoting (LYS_*QOUTED - see [schema node flags](@ref snodeflags))
Michal Vasko2ca70f52018-09-27 11:04:51 +0200505 * @param[out] word_p Pointer to the read string. Can return NULL if \p arg is #Y_MAYBE_STR_ARG.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200506 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read string. If not needed,
507 * set to NULL. Otherwise equal to \p word_p.
508 * @param[out] word_len Length of the read string.
509 *
510 * @return LY_ERR values.
Michal Vasko7fbc8162018-09-17 10:35:16 +0200511 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200512LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100513get_argument(struct lys_yang_parser_ctx *ctx, const char **data, enum yang_arg arg,
Radek Krejcid3ca0632019-04-16 16:54:54 +0200514 uint16_t *flags, char **word_p, char **word_b, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200515{
Radek Krejci44ceedc2018-10-02 15:54:31 +0200516 size_t buf_len = 0;
David Sedlák40bb13b2019-07-10 14:34:18 +0200517 int prefix = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200518 /* word buffer - dynamically allocated */
519 *word_b = NULL;
520
521 /* word pointer - just a pointer to data */
522 *word_p = NULL;
523
524 *word_len = 0;
525 while (**data) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200526 switch (**data) {
527 case '\'':
528 case '\"':
529 if (*word_len) {
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200530 /* invalid - quotes cannot be in unquoted string and only optsep, ; or { can follow it */
David Sedlákb3077192019-06-19 10:55:37 +0200531 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, *data,
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200532 "unquoted string character, optsep, semicolon or opening brace");
533 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200534 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200535 if (flags) {
536 (*flags) |= (**data) == '\'' ? LYS_SINGLEQUOTED : LYS_DOUBLEQUOTED;
537 }
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100538 LY_CHECK_RET(read_qstring(ctx, data, arg, word_p, word_b, word_len, &buf_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200539 goto str_end;
540 case '/':
Radek Krejci44ceedc2018-10-02 15:54:31 +0200541 if ((*data)[1] == '/') {
542 /* one-line comment */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200543 MOVE_INPUT(ctx, data, 2);
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100544 LY_CHECK_RET(skip_comment(ctx, data, 1));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200545 } else if ((*data)[1] == '*') {
546 /* block comment */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200547 MOVE_INPUT(ctx, data, 2);
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100548 LY_CHECK_RET(skip_comment(ctx, data, 2));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200549 } else {
550 /* not a comment after all */
David Sedlák40bb13b2019-07-10 14:34:18 +0200551 LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, &buf_len, 0, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200552 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200553 break;
554 case ' ':
555 if (*word_len) {
556 /* word is finished */
557 goto str_end;
558 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200559 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200560 break;
561 case '\t':
562 if (*word_len) {
563 /* word is finished */
564 goto str_end;
565 }
566 /* tabs count for 8 spaces */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200567 ctx->indent += 8;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200568
569 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200570 break;
571 case '\n':
572 if (*word_len) {
573 /* word is finished */
574 goto str_end;
575 }
576 /* reset indent */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200577 ctx->indent = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200578
579 /* track line numbers */
580 ++ctx->line;
581
582 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200583 break;
584 case ';':
585 case '{':
586 if (*word_len || (arg == Y_MAYBE_STR_ARG)) {
587 /* word is finished */
588 goto str_end;
589 }
590
David Sedlákb3077192019-06-19 10:55:37 +0200591 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, *data, "an argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200592 return LY_EVALID;
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200593 case '}':
594 /* invalid - braces cannot be in unquoted string (opening braces terminates the string and can follow it) */
David Sedlákb3077192019-06-19 10:55:37 +0200595 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, *data,
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200596 "unquoted string character, optsep, semicolon or opening brace");
597 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200598 default:
David Sedlák40bb13b2019-07-10 14:34:18 +0200599 LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, &buf_len, 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200600 break;
601 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200602 }
603
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200604 /* unexpected end of loop */
David Sedlákb3077192019-06-19 10:55:37 +0200605 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200606 return LY_EVALID;
607
Michal Vasko7fbc8162018-09-17 10:35:16 +0200608str_end:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200609 /* terminating NULL byte for buf */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200610 if (*word_b) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200611 (*word_b) = ly_realloc(*word_b, (*word_len) + 1);
612 LY_CHECK_ERR_RET(!(*word_b), LOGMEM(ctx->ctx), LY_EMEM);
613 (*word_b)[*word_len] = '\0';
Michal Vasko7fbc8162018-09-17 10:35:16 +0200614 *word_p = *word_b;
615 }
616
617 return LY_SUCCESS;
618}
619
Michal Vaskoea5abea2018-09-18 13:10:54 +0200620/**
621 * @brief Get another YANG keyword from the raw data.
622 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200623 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200624 * @param[in,out] data Data to read from, always moved to currently handled character.
625 * @param[out] kw YANG keyword read.
626 * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances.
627 * @param[out] word_len Length of the keyword in the data. Useful for extension instances.
628 *
629 * @return LY_ERR values.
630 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200631LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100632get_keyword(struct lys_yang_parser_ctx *ctx, const char **data, enum ly_stmt *kw, char **word_p, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200633{
Michal Vasko7fbc8162018-09-17 10:35:16 +0200634 int prefix;
635 const char *word_start;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200636 unsigned int c;
637 size_t len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200638
639 if (word_p) {
640 *word_p = NULL;
641 *word_len = 0;
642 }
643
644 /* first skip "optsep", comments */
645 while (**data) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200646 switch (**data) {
647 case '/':
648 if ((*data)[1] == '/') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200649 /* one-line comment */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200650 MOVE_INPUT(ctx, data, 2);
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100651 LY_CHECK_RET(skip_comment(ctx, data, 1));
Radek Krejcidcc7b322018-10-11 14:24:02 +0200652 } else if ((*data)[1] == '*') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200653 /* block comment */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200654 MOVE_INPUT(ctx, data, 2);
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100655 LY_CHECK_RET(skip_comment(ctx, data, 2));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200656 } else {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200657 /* error - not a comment after all, keyword cannot start with slash */
David Sedlákb3077192019-06-19 10:55:37 +0200658 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '/'.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200659 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200660 }
Radek Krejci13028282019-06-11 14:56:48 +0200661 continue;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200662 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200663 /* skip whitespaces (optsep) */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200664 ++ctx->line;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200665 ctx->indent = 0;
666 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200667 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200668 /* skip whitespaces (optsep) */
669 ++ctx->indent;
670 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200671 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200672 /* skip whitespaces (optsep) */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200673 ctx->indent += 8;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200674 break;
675 default:
676 /* either a keyword start or an invalid character */
677 goto keyword_start;
678 }
679
680 ++(*data);
681 }
682
683keyword_start:
684 word_start = *data;
David Sedlák5f8f0332019-06-18 16:34:30 +0200685 *kw = lysp_match_kw(ctx, data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200686
Radek Krejcid6b76452019-09-03 17:03:03 +0200687 if (*kw == LY_STMT_SYNTAX_SEMICOLON || *kw == LY_STMT_SYNTAX_LEFT_BRACE || *kw == LY_STMT_SYNTAX_RIGHT_BRACE) {
Radek Krejci626df482018-10-11 15:06:31 +0200688 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200689 }
690
Radek Krejcid6b76452019-09-03 17:03:03 +0200691 if (*kw != LY_STMT_NONE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200692 /* make sure we have the whole keyword */
693 switch (**data) {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200694 case '\n':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200695 case '\t':
Radek Krejciabdd8062019-06-11 16:44:19 +0200696 case ' ':
697 /* mandatory "sep" is just checked, not eaten so nothing in the context is updated */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200698 break;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200699 case ':':
700 /* keyword is not actually a keyword, but prefix of an extension.
701 * To avoid repeated check of the prefix syntax, move to the point where the colon was read
702 * and we will be checking the keyword (extension instance) itself */
703 prefix = 1;
704 MOVE_INPUT(ctx, data, 1);
705 goto extension;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200706 case '{':
707 /* allowed only for input and output statements which can be without arguments */
Radek Krejcid6b76452019-09-03 17:03:03 +0200708 if (*kw == LY_STMT_INPUT || *kw == LY_STMT_OUTPUT) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200709 break;
710 }
711 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200712 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200713 MOVE_INPUT(ctx, data, 1);
David Sedlákb3077192019-06-19 10:55:37 +0200714 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(*data - word_start), word_start,
Radek Krejci44ceedc2018-10-02 15:54:31 +0200715 "a keyword followed by a separator");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200716 return LY_EVALID;
717 }
718 } else {
719 /* still can be an extension */
720 prefix = 0;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200721extension:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200722 while (**data && (**data != ' ') && (**data != '\t') && (**data != '\n') && (**data != '{') && (**data != ';')) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200723 LY_CHECK_ERR_RET(ly_getutf8(data, &c, &len),
David Sedlákb3077192019-06-19 10:55:37 +0200724 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, (*data)[-len]), LY_EVALID);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200725 ++ctx->indent;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200726 /* check character validity */
Michal Vaskob36053d2020-03-26 15:49:30 +0100727 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, *data - len == word_start ? 1 : 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200728 }
729 if (!**data) {
David Sedlákb3077192019-06-19 10:55:37 +0200730 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200731 return LY_EVALID;
732 }
733
734 /* prefix is mandatory for extension instances */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200735 if (prefix != 2) {
David Sedlákb3077192019-06-19 10:55:37 +0200736 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(*data - word_start), word_start, "a keyword");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200737 return LY_EVALID;
738 }
739
Radek Krejcid6b76452019-09-03 17:03:03 +0200740 *kw = LY_STMT_EXTENSION_INSTANCE;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200741 }
Radek Krejci626df482018-10-11 15:06:31 +0200742success:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200743 if (word_p) {
744 *word_p = (char *)word_start;
745 *word_len = *data - word_start;
746 }
747
748 return LY_SUCCESS;
749}
750
Michal Vaskoea5abea2018-09-18 13:10:54 +0200751/**
752 * @brief Parse extension instance substatements.
753 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200754 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200755 * @param[in,out] data Data to read from, always moved to currently handled character.
Radek Krejci335332a2019-09-05 13:03:35 +0200756 * @param[in] kw Statement keyword value matching @p word value.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200757 * @param[in] word Extension instance substatement name (keyword).
758 * @param[in] word_len Extension instance substatement name length.
759 * @param[in,out] child Children of this extension instance to add to.
760 *
761 * @return LY_ERR values.
762 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200763static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100764parse_ext_substmt(struct lys_yang_parser_ctx *ctx, const char **data, enum ly_stmt kw, char *word, size_t word_len,
Michal Vasko7fbc8162018-09-17 10:35:16 +0200765 struct lysp_stmt **child)
766{
767 char *buf;
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100768 LY_ERR ret = LY_SUCCESS;
Radek Krejci335332a2019-09-05 13:03:35 +0200769 enum ly_stmt child_kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200770 struct lysp_stmt *stmt, *par_child;
771
772 stmt = calloc(1, sizeof *stmt);
773 LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM);
774
Radek Krejcibb9b1982019-04-08 14:24:59 +0200775 /* insert into parent statements */
776 if (!*child) {
777 *child = stmt;
778 } else {
779 for (par_child = *child; par_child->next; par_child = par_child->next);
780 par_child->next = stmt;
781 }
782
Radek Krejci44ceedc2018-10-02 15:54:31 +0200783 stmt->stmt = lydict_insert(ctx->ctx, word, word_len);
Radek Krejci335332a2019-09-05 13:03:35 +0200784 stmt->kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200785
786 /* get optional argument */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200787 LY_CHECK_RET(get_argument(ctx, data, Y_MAYBE_STR_ARG, &stmt->flags, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200788
Radek Krejci0ae092d2018-09-20 16:43:19 +0200789 if (word) {
790 if (buf) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200791 stmt->arg = lydict_insert_zc(ctx->ctx, word);
Radek Krejci0ae092d2018-09-20 16:43:19 +0200792 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200793 stmt->arg = lydict_insert(ctx->ctx, word, word_len);
Radek Krejci0ae092d2018-09-20 16:43:19 +0200794 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200795 }
796
Radek Krejci335332a2019-09-05 13:03:35 +0200797 YANG_READ_SUBSTMT_FOR(ctx, data, child_kw, word, word_len, ret, ) {
798 LY_CHECK_RET(parse_ext_substmt(ctx, data, child_kw, word, word_len, &stmt->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200799 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200800 return ret;
801}
802
Michal Vaskoea5abea2018-09-18 13:10:54 +0200803/**
804 * @brief Parse extension instance.
805 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200806 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200807 * @param[in,out] data Data to read from, always moved to currently handled character.
808 * @param[in] ext_name Extension instance substatement name (keyword).
809 * @param[in] ext_name_len Extension instance substatement name length.
810 * @param[in] insubstmt Type of the keyword this extension instance is a substatement of.
811 * @param[in] insubstmt_index Index of the keyword instance this extension instance is a substatement of.
812 * @param[in,out] exts Extension instances to add to.
813 *
814 * @return LY_ERR values.
815 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200816static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100817parse_ext(struct lys_yang_parser_ctx *ctx, const char **data, const char *ext_name, int ext_name_len, LYEXT_SUBSTMT insubstmt,
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200818 LY_ARRAY_SIZE_TYPE insubstmt_index, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200819{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100820 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200821 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200822 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200823 struct lysp_ext_instance *e;
Radek Krejcid6b76452019-09-03 17:03:03 +0200824 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200825
Radek Krejci2c4e7172018-10-19 15:56:26 +0200826 LY_ARRAY_NEW_RET(ctx->ctx, *exts, e, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200827
828 /* store name and insubstmt info */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200829 e->name = lydict_insert(ctx->ctx, ext_name, ext_name_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200830 e->insubstmt = insubstmt;
831 e->insubstmt_index = insubstmt_index;
832
833 /* get optional argument */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200834 LY_CHECK_RET(get_argument(ctx, data, Y_MAYBE_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200835
Radek Krejci0ae092d2018-09-20 16:43:19 +0200836 if (word) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200837 INSERT_WORD(ctx, buf, e->argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200838 }
839
Radek Krejci6d6556c2018-11-08 09:37:45 +0100840 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Radek Krejci335332a2019-09-05 13:03:35 +0200841 LY_CHECK_RET(parse_ext_substmt(ctx, data, kw, word, word_len, &e->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200842 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200843 return ret;
844}
845
Michal Vaskoea5abea2018-09-18 13:10:54 +0200846/**
847 * @brief Parse a generic text field without specific constraints. Those are contact, organization,
848 * description, etc...
849 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200850 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200851 * @param[in,out] data Data to read from, always moved to currently handled character.
852 * @param[in] substmt Type of this substatement.
853 * @param[in] substmt_index Index of this substatement.
854 * @param[in,out] value Place to store the parsed value.
855 * @param[in] arg Type of the YANG keyword argument (of the value).
856 * @param[in,out] exts Extension instances to add to.
857 *
858 * @return LY_ERR values.
859 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200860static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100861parse_text_field(struct lys_yang_parser_ctx *ctx, const char **data, LYEXT_SUBSTMT substmt, uint32_t substmt_index,
Michal Vasko7fbc8162018-09-17 10:35:16 +0200862 const char **value, enum yang_arg arg, struct lysp_ext_instance **exts)
863{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100864 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200865 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200866 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200867 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200868
869 if (*value) {
David Sedlákb3077192019-06-19 10:55:37 +0200870 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200871 return LY_EVALID;
872 }
873
874 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200875 LY_CHECK_RET(get_argument(ctx, data, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200876
877 /* store value and spend buf if allocated */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200878 INSERT_WORD(ctx, buf, *value, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200879
Radek Krejci6d6556c2018-11-08 09:37:45 +0100880 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200881 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200882 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100883 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, substmt, substmt_index, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200884 break;
885 default:
David Sedlákb3077192019-06-19 10:55:37 +0200886 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200887 return LY_EVALID;
888 }
889 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200890 return ret;
891}
892
Michal Vaskoea5abea2018-09-18 13:10:54 +0200893/**
894 * @brief Parse the yang-version statement.
895 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200896 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200897 * @param[in,out] data Data to read from, always moved to currently handled character.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100898 * @param[out] version Storage for the parsed information.
899 * @param[in, out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200900 *
901 * @return LY_ERR values.
902 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200903static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100904parse_yangversion(struct lys_yang_parser_ctx *ctx, const char **data, uint8_t *version, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200905{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100906 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200907 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200908 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200909 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200910
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100911 if (*version) {
David Sedlákb3077192019-06-19 10:55:37 +0200912 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200913 return LY_EVALID;
914 }
915
916 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200917 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200918
919 if ((word_len == 3) && !strncmp(word, "1.0", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100920 *version = LYS_VERSION_1_0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200921 } else if ((word_len == 3) && !strncmp(word, "1.1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100922 *version = LYS_VERSION_1_1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200923 } else {
David Sedlákb3077192019-06-19 10:55:37 +0200924 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200925 free(buf);
926 return LY_EVALID;
927 }
928 free(buf);
929
Radek Krejci6d6556c2018-11-08 09:37:45 +0100930 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200931 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200932 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100933 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_VERSION, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200934 break;
935 default:
David Sedlákb3077192019-06-19 10:55:37 +0200936 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200937 return LY_EVALID;
938 }
939 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200940 return ret;
941}
942
Michal Vaskoea5abea2018-09-18 13:10:54 +0200943/**
944 * @brief Parse the belongs-to statement.
945 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200946 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200947 * @param[in,out] data Data to read from, always moved to currently handled character.
948 * @param[in,out] belongsto Place to store the parsed value.
949 * @param[in,out] prefix Place to store the parsed belongs-to prefix value.
950 * @param[in,out] exts Extension instances to add to.
951 *
952 * @return LY_ERR values.
953 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200954static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100955parse_belongsto(struct lys_yang_parser_ctx *ctx, const char **data, const char **belongsto, const char **prefix, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200956{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100957 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200958 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200959 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200960 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200961
962 if (*belongsto) {
David Sedlákb3077192019-06-19 10:55:37 +0200963 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200964 return LY_EVALID;
965 }
966
967 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200968 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200969
Radek Krejci44ceedc2018-10-02 15:54:31 +0200970 INSERT_WORD(ctx, buf, *belongsto, word, word_len);
Radek Krejcif09e4e82019-06-14 15:08:11 +0200971
Radek Krejci6d6556c2018-11-08 09:37:45 +0100972 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200973 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200974 case LY_STMT_PREFIX:
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100975 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_PREFIX, 0, prefix, Y_IDENTIF_ARG, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200976 break;
Radek Krejcid6b76452019-09-03 17:03:03 +0200977 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100978 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_BELONGSTO, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200979 break;
980 default:
David Sedlákb3077192019-06-19 10:55:37 +0200981 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200982 return LY_EVALID;
983 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200984 }
Radek Krejcic59bc972018-09-17 16:13:06 +0200985 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +0100986checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200987 /* mandatory substatements */
988 if (!*prefix) {
David Sedlákb3077192019-06-19 10:55:37 +0200989 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200990 return LY_EVALID;
991 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200992 return ret;
993}
994
Michal Vaskoea5abea2018-09-18 13:10:54 +0200995/**
996 * @brief Parse the revision-date statement.
997 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200998 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200999 * @param[in,out] data Data to read from, always moved to currently handled character.
1000 * @param[in,out] rev Array to store the parsed value in.
1001 * @param[in,out] exts Extension instances to add to.
1002 *
1003 * @return LY_ERR values.
1004 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001005static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001006parse_revisiondate(struct lys_yang_parser_ctx *ctx, const char **data, char *rev, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001007{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001008 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001009 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001010 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001011 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001012
1013 if (rev[0]) {
David Sedlákb3077192019-06-19 10:55:37 +02001014 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001015 return LY_EVALID;
1016 }
1017
1018 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001019 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001020
1021 /* check value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001022 if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision-date")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001023 free(buf);
1024 return LY_EVALID;
1025 }
1026
1027 /* store value and spend buf if allocated */
1028 strncpy(rev, word, word_len);
1029 free(buf);
1030
Radek Krejci6d6556c2018-11-08 09:37:45 +01001031 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001032 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001033 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001034 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_REVISIONDATE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001035 break;
1036 default:
David Sedlákb3077192019-06-19 10:55:37 +02001037 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001038 return LY_EVALID;
1039 }
1040 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001041 return ret;
1042}
1043
Michal Vaskoea5abea2018-09-18 13:10:54 +02001044/**
1045 * @brief Parse the include statement.
1046 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001047 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001048 * @param[in] module_name Name of the module to check name collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001049 * @param[in,out] data Data to read from, always moved to currently handled character.
1050 * @param[in,out] includes Parsed includes to add to.
1051 *
1052 * @return LY_ERR values.
1053 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001054static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001055parse_include(struct lys_yang_parser_ctx *ctx, const char *module_name, const char **data, struct lysp_include **includes)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001056{
Radek Krejcid33273d2018-10-25 14:55:52 +02001057 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001058 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001059 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001060 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001061 struct lysp_include *inc;
1062
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001063 LY_ARRAY_NEW_RET(ctx->ctx, *includes, inc, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001064
1065 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001066 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001067
Radek Krejci086c7132018-10-26 15:29:04 +02001068 INSERT_WORD(ctx, buf, inc->name, word, word_len);
1069
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001070 /* submodules share the namespace with the module names, so there must not be
1071 * a module of the same name in the context, no need for revision matching */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001072 if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(ctx->ctx, inc->name)) {
David Sedlákb3077192019-06-19 10:55:37 +02001073 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Name collision between module and submodule of name \"%s\".", inc->name);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001074 return LY_EVALID;
1075 }
1076
Radek Krejci6d6556c2018-11-08 09:37:45 +01001077 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001078 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001079 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001080 PARSER_CHECK_STMTVER2_RET(ctx, "description", "include");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001081 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &inc->dsc, Y_STR_ARG, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001082 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001083 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001084 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "include");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001085 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &inc->ref, Y_STR_ARG, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001086 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001087 case LY_STMT_REVISION_DATE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001088 LY_CHECK_RET(parse_revisiondate(ctx, data, inc->rev, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001089 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001090 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001091 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001092 break;
1093 default:
David Sedlákb3077192019-06-19 10:55:37 +02001094 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001095 return LY_EVALID;
1096 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001097 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001098 return ret;
1099}
1100
Michal Vaskoea5abea2018-09-18 13:10:54 +02001101/**
1102 * @brief Parse the import statement.
1103 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001104 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001105 * @param[in] module_prefix Prefix of the module to check prefix collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001106 * @param[in,out] data Data to read from, always moved to currently handled character.
1107 * @param[in,out] imports Parsed imports to add to.
1108 *
1109 * @return LY_ERR values.
1110 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001111static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001112parse_import(struct lys_yang_parser_ctx *ctx, const char *module_prefix, const char **data, struct lysp_import **imports)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001113{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001114 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001115 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001116 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001117 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001118 struct lysp_import *imp;
1119
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001120 LY_ARRAY_NEW_RET(ctx->ctx, *imports, imp, LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001121
1122 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001123 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02001124 INSERT_WORD(ctx, buf, imp->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001125
Radek Krejci6d6556c2018-11-08 09:37:45 +01001126 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001127 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001128 case LY_STMT_PREFIX:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001129 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_PREFIX, 0, &imp->prefix, Y_IDENTIF_ARG, &imp->exts));
Michal Vaskob36053d2020-03-26 15:49:30 +01001130 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 +02001131 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001132 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001133 PARSER_CHECK_STMTVER2_RET(ctx, "description", "import");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001134 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &imp->dsc, Y_STR_ARG, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001135 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001136 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001137 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "import");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001138 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &imp->ref, Y_STR_ARG, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001139 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001140 case LY_STMT_REVISION_DATE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001141 LY_CHECK_RET(parse_revisiondate(ctx, data, imp->rev, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001142 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001143 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001144 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001145 break;
1146 default:
David Sedlákb3077192019-06-19 10:55:37 +02001147 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001148 return LY_EVALID;
1149 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001150 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001151 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001152checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001153 /* mandatory substatements */
David Sedlákb3077192019-06-19 10:55:37 +02001154 LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001155
1156 return ret;
1157}
1158
Michal Vaskoea5abea2018-09-18 13:10:54 +02001159/**
1160 * @brief Parse the revision statement.
1161 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001162 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001163 * @param[in,out] data Data to read from, always moved to currently handled character.
1164 * @param[in,out] revs Parsed revisions to add to.
1165 *
1166 * @return LY_ERR values.
1167 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001168static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001169parse_revision(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_revision **revs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001170{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001171 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001172 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001173 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001174 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001175 struct lysp_revision *rev;
1176
Radek Krejci2c4e7172018-10-19 15:56:26 +02001177 LY_ARRAY_NEW_RET(ctx->ctx, *revs, rev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001178
1179 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001180 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001181
1182 /* check value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001183 if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision")) {
David Sedlák68ef3dc2019-07-22 13:40:19 +02001184 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001185 return LY_EVALID;
1186 }
1187
Radek Krejcib7db73a2018-10-24 14:18:40 +02001188 strncpy(rev->date, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001189 free(buf);
1190
Radek Krejci6d6556c2018-11-08 09:37:45 +01001191 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001192 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001193 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001194 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &rev->dsc, Y_STR_ARG, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001195 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001196 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001197 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &rev->ref, Y_STR_ARG, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001198 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001199 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001200 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001201 break;
1202 default:
David Sedlákb3077192019-06-19 10:55:37 +02001203 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001204 return LY_EVALID;
1205 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001206 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001207 return ret;
1208}
1209
Michal Vaskoea5abea2018-09-18 13:10:54 +02001210/**
1211 * @brief Parse a generic text field that can have more instances such as base.
1212 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001213 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001214 * @param[in,out] data Data to read from, always moved to currently handled character.
1215 * @param[in] substmt Type of this substatement.
1216 * @param[in,out] texts Parsed values to add to.
1217 * @param[in] arg Type of the expected argument.
1218 * @param[in,out] exts Extension instances to add to.
1219 *
1220 * @return LY_ERR values.
1221 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001222static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001223parse_text_fields(struct lys_yang_parser_ctx *ctx, const char **data, LYEXT_SUBSTMT substmt, const char ***texts, enum yang_arg arg,
Michal Vasko7fbc8162018-09-17 10:35:16 +02001224 struct lysp_ext_instance **exts)
1225{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001226 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001227 char *buf, *word;
Radek Krejci151a5b72018-10-19 14:21:44 +02001228 const char **item;
1229 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001230 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001231
1232 /* allocate new pointer */
Radek Krejci2c4e7172018-10-19 15:56:26 +02001233 LY_ARRAY_NEW_RET(ctx->ctx, *texts, item, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001234
1235 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001236 LY_CHECK_RET(get_argument(ctx, data, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001237
Radek Krejci151a5b72018-10-19 14:21:44 +02001238 INSERT_WORD(ctx, buf, *item, word, word_len);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001239 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001240 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001241 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001242 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, substmt, LY_ARRAY_SIZE(*texts) - 1, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001243 break;
1244 default:
David Sedlákb3077192019-06-19 10:55:37 +02001245 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001246 return LY_EVALID;
1247 }
1248 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001249 return ret;
1250}
1251
Michal Vaskoea5abea2018-09-18 13:10:54 +02001252/**
1253 * @brief Parse the config statement.
1254 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001255 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001256 * @param[in,out] data Data to read from, always moved to currently handled character.
1257 * @param[in,out] flags Flags to add to.
1258 * @param[in,out] exts Extension instances to add to.
1259 *
1260 * @return LY_ERR values.
1261 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001262static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001263parse_config(struct lys_yang_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001264{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001265 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001266 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001267 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001268 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001269
1270 if (*flags & LYS_CONFIG_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001271 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001272 return LY_EVALID;
1273 }
1274
1275 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001276 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001277
1278 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1279 *flags |= LYS_CONFIG_W;
1280 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1281 *flags |= LYS_CONFIG_R;
1282 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001283 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001284 free(buf);
1285 return LY_EVALID;
1286 }
1287 free(buf);
1288
Radek Krejci6d6556c2018-11-08 09:37:45 +01001289 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001290 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001291 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001292 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_CONFIG, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001293 break;
1294 default:
David Sedlákb3077192019-06-19 10:55:37 +02001295 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001296 return LY_EVALID;
1297 }
1298 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001299 return ret;
1300}
1301
Michal Vaskoea5abea2018-09-18 13:10:54 +02001302/**
1303 * @brief Parse the mandatory statement.
1304 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001305 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001306 * @param[in,out] data Data to read from, always moved to currently handled character.
1307 * @param[in,out] flags Flags to add to.
1308 * @param[in,out] exts Extension instances to add to.
1309 *
1310 * @return LY_ERR values.
1311 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001312static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001313parse_mandatory(struct lys_yang_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001314{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001315 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001316 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001317 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001318 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001319
1320 if (*flags & LYS_MAND_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001321 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001322 return LY_EVALID;
1323 }
1324
1325 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001326 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001327
1328 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1329 *flags |= LYS_MAND_TRUE;
1330 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1331 *flags |= LYS_MAND_FALSE;
1332 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001333 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001334 free(buf);
1335 return LY_EVALID;
1336 }
1337 free(buf);
1338
Radek Krejci6d6556c2018-11-08 09:37:45 +01001339 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001340 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001341 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001342 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MANDATORY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001343 break;
1344 default:
David Sedlákb3077192019-06-19 10:55:37 +02001345 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001346 return LY_EVALID;
1347 }
1348 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001349 return ret;
1350}
1351
Michal Vaskoea5abea2018-09-18 13:10:54 +02001352/**
1353 * @brief Parse a restriction such as range or length.
1354 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001355 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001356 * @param[in,out] data Data to read from, always moved to currently handled character.
1357 * @param[in] restr_kw Type of this particular restriction.
1358 * @param[in,out] exts Extension instances to add to.
1359 *
1360 * @return LY_ERR values.
1361 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001362static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001363parse_restr(struct lys_yang_parser_ctx *ctx, const char **data, enum ly_stmt restr_kw, struct lysp_restr *restr)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001364{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001365 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001366 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001367 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001368 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001369
1370 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001371 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001372
Michal Vaskob36053d2020-03-26 15:49:30 +01001373 CHECK_NONEMPTY(ctx, word_len, ly_stmt2str(restr_kw));
Radek Krejci44ceedc2018-10-02 15:54:31 +02001374 INSERT_WORD(ctx, buf, restr->arg, word, word_len);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001375 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001376 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001377 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001378 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001379 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001380 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001381 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001382 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001383 case LY_STMT_ERROR_APP_TAG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001384 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRTAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001385 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001386 case LY_STMT_ERROR_MESSAGE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001387 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRMSG, 0, &restr->emsg, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001388 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001389 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001390 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001391 break;
1392 default:
David Sedlákb3077192019-06-19 10:55:37 +02001393 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001394 return LY_EVALID;
1395 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001396 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001397 return ret;
1398}
1399
Michal Vaskoea5abea2018-09-18 13:10:54 +02001400/**
1401 * @brief Parse a restriction that can have more instances such as must.
1402 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001403 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001404 * @param[in,out] data Data to read from, always moved to currently handled character.
1405 * @param[in] restr_kw Type of this particular restriction.
1406 * @param[in,out] restrs Restrictions to add to.
1407 *
1408 * @return LY_ERR values.
1409 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001410static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001411parse_restrs(struct lys_yang_parser_ctx *ctx, const char **data, enum ly_stmt restr_kw, struct lysp_restr **restrs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001412{
1413 struct lysp_restr *restr;
1414
Radek Krejci2c4e7172018-10-19 15:56:26 +02001415 LY_ARRAY_NEW_RET(ctx->ctx, *restrs, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001416 return parse_restr(ctx, data, restr_kw, restr);
1417}
1418
Michal Vaskoea5abea2018-09-18 13:10:54 +02001419/**
1420 * @brief Parse the status statement.
1421 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001422 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001423 * @param[in,out] data Data to read from, always moved to currently handled character.
1424 * @param[in,out] flags Flags to add to.
1425 * @param[in,out] exts Extension instances to add to.
1426 *
1427 * @return LY_ERR values.
1428 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001429static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001430parse_status(struct lys_yang_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001431{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001432 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001433 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001434 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001435 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001436
1437 if (*flags & LYS_STATUS_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001438 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001439 return LY_EVALID;
1440 }
1441
1442 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001443 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001444
1445 if ((word_len == 7) && !strncmp(word, "current", word_len)) {
1446 *flags |= LYS_STATUS_CURR;
1447 } else if ((word_len == 10) && !strncmp(word, "deprecated", word_len)) {
1448 *flags |= LYS_STATUS_DEPRC;
1449 } else if ((word_len == 8) && !strncmp(word, "obsolete", word_len)) {
1450 *flags |= LYS_STATUS_OBSLT;
1451 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001452 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001453 free(buf);
1454 return LY_EVALID;
1455 }
1456 free(buf);
1457
Radek Krejci6d6556c2018-11-08 09:37:45 +01001458 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001459 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001460 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001461 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_STATUS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001462 break;
1463 default:
David Sedlákb3077192019-06-19 10:55:37 +02001464 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001465 return LY_EVALID;
1466 }
1467 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001468 return ret;
1469}
1470
Michal Vaskoea5abea2018-09-18 13:10:54 +02001471/**
1472 * @brief Parse the when statement.
1473 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001474 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001475 * @param[in,out] data Data to read from, always moved to currently handled character.
1476 * @param[in,out] when_p When pointer to parse to.
1477 *
1478 * @return LY_ERR values.
1479 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001480LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001481parse_when(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001482{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001483 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001484 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001485 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001486 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001487 struct lysp_when *when;
1488
1489 if (*when_p) {
David Sedlákb3077192019-06-19 10:55:37 +02001490 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001491 return LY_EVALID;
1492 }
1493
1494 when = calloc(1, sizeof *when);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001495 LY_CHECK_ERR_RET(!when, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001496
1497 /* get value */
Radek Krejci2f54df52019-06-21 10:59:19 +02001498 LY_CHECK_ERR_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len), free(when), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01001499 CHECK_NONEMPTY(ctx, word_len, "when");
Radek Krejci44ceedc2018-10-02 15:54:31 +02001500 INSERT_WORD(ctx, buf, when->cond, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001501
Radek Krejcif09e4e82019-06-14 15:08:11 +02001502 *when_p = when;
1503
Radek Krejci6d6556c2018-11-08 09:37:45 +01001504 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001505 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001506 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001507 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &when->dsc, Y_STR_ARG, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001508 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001509 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001510 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &when->ref, Y_STR_ARG, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001511 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001512 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001513 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001514 break;
1515 default:
David Sedlákb3077192019-06-19 10:55:37 +02001516 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001517 return LY_EVALID;
1518 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001519 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001520 return ret;
1521}
1522
Michal Vaskoea5abea2018-09-18 13:10:54 +02001523/**
1524 * @brief Parse the anydata or anyxml statement.
1525 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001526 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001527 * @param[in,out] data Data to read from, always moved to currently handled character.
1528 * @param[in] kw Type of this particular keyword.
1529 * @param[in,out] siblings Siblings to add to.
1530 *
1531 * @return LY_ERR values.
1532 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02001533LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001534parse_any(struct lys_yang_parser_ctx *ctx, const char **data, enum ly_stmt kw, struct lysp_node *parent, struct lysp_node **siblings)
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;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001539 struct lysp_node_anydata *any;
1540
David Sedlák60adc092019-08-06 15:57:02 +02001541 /* create new structure and insert into siblings */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02001542 LY_LIST_NEW_RET(ctx->ctx, siblings, any, next, LY_EMEM);
David Sedlák60adc092019-08-06 15:57:02 +02001543
Radek Krejcid6b76452019-09-03 17:03:03 +02001544 any->nodetype = kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001545 any->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001546
Michal Vasko7fbc8162018-09-17 10:35:16 +02001547 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001548 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02001549 INSERT_WORD(ctx, buf, any->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001550
1551 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01001552 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001553 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001554 case LY_STMT_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001555 LY_CHECK_RET(parse_config(ctx, data, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001556 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001557 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001558 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &any->dsc, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001559 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001560 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001561 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &any->iffeatures, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001562 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001563 case LY_STMT_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001564 LY_CHECK_RET(parse_mandatory(ctx, data, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001565 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001566 case LY_STMT_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001567 LY_CHECK_RET(parse_restrs(ctx, data, kw, &any->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001568 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001569 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001570 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &any->ref, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001571 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001572 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001573 LY_CHECK_RET(parse_status(ctx, data, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001574 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001575 case LY_STMT_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001576 LY_CHECK_RET(parse_when(ctx, data, &any->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001577 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001578 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001579 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001580 break;
1581 default:
David Sedlákb3077192019-06-19 10:55:37 +02001582 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
Radek Krejcid6b76452019-09-03 17:03:03 +02001583 (any->nodetype & LYS_ANYDATA) == LYS_ANYDATA ? ly_stmt2str(LY_STMT_ANYDATA) : ly_stmt2str(LY_STMT_ANYXML));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001584 return LY_EVALID;
1585 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001586 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001587 return ret;
1588}
1589
Michal Vaskoea5abea2018-09-18 13:10:54 +02001590/**
1591 * @brief Parse the value or position statement. Substatement of type enum statement.
1592 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001593 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001594 * @param[in,out] data Data to read from, always moved to currently handled character.
1595 * @param[in] val_kw Type of this particular keyword.
1596 * @param[in,out] value Value to write to.
1597 * @param[in,out] flags Flags to write to.
1598 * @param[in,out] exts Extension instances to add to.
1599 *
1600 * @return LY_ERR values.
1601 */
David Sedlákd6ce6d72019-07-16 17:30:18 +02001602LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001603parse_type_enum_value_pos(struct lys_yang_parser_ctx *ctx, const char **data, enum ly_stmt val_kw, int64_t *value, uint16_t *flags,
Michal Vasko7fbc8162018-09-17 10:35:16 +02001604 struct lysp_ext_instance **exts)
1605{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001606 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001607 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001608 size_t word_len;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02001609 long int num = 0;
1610 unsigned long int unum = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001611 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001612
1613 if (*flags & LYS_SET_VALUE) {
David Sedlákb3077192019-06-19 10:55:37 +02001614 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001615 return LY_EVALID;
1616 }
1617 *flags |= LYS_SET_VALUE;
1618
1619 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001620 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001621
Radek Krejcid6b76452019-09-03 17:03:03 +02001622 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 +02001623 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001624 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001625 }
1626
1627 errno = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001628 if (val_kw == LY_STMT_VALUE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001629 num = strtol(word, &ptr, 10);
Radek Krejci8b764662018-11-14 14:15:13 +01001630 if (num < INT64_C(-2147483648) || num > INT64_C(2147483647)) {
David Sedlákb3077192019-06-19 10:55:37 +02001631 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001632 goto error;
1633 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001634 } else {
1635 unum = strtoul(word, &ptr, 10);
Radek Krejci8b764662018-11-14 14:15:13 +01001636 if (unum > UINT64_C(4294967295)) {
David Sedlákb3077192019-06-19 10:55:37 +02001637 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001638 goto error;
1639 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001640 }
1641 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001642 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001643 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001644 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001645 }
1646 if (errno == ERANGE) {
David Sedlákb3077192019-06-19 10:55:37 +02001647 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001648 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001649 }
Radek Krejcid6b76452019-09-03 17:03:03 +02001650 if (val_kw == LY_STMT_VALUE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001651 *value = num;
1652 } else {
1653 *value = unum;
1654 }
1655 free(buf);
1656
Radek Krejci6d6556c2018-11-08 09:37:45 +01001657 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001658 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001659 case LY_STMT_EXTENSION_INSTANCE:
1660 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, val_kw == LY_STMT_VALUE ? LYEXT_SUBSTMT_VALUE : LYEXT_SUBSTMT_POSITION, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001661 break;
1662 default:
David Sedlákb3077192019-06-19 10:55:37 +02001663 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001664 return LY_EVALID;
1665 }
1666 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001667 return ret;
Radek Krejci8b764662018-11-14 14:15:13 +01001668
1669error:
1670 free(buf);
1671 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001672}
1673
Michal Vaskoea5abea2018-09-18 13:10:54 +02001674/**
1675 * @brief Parse the enum or bit statement. Substatement of type statement.
1676 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001677 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001678 * @param[in,out] data Data to read from, always moved to currently handled character.
1679 * @param[in] enum_kw Type of this particular keyword.
1680 * @param[in,out] enums Enums or bits to add to.
1681 *
1682 * @return LY_ERR values.
1683 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001684static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001685parse_type_enum(struct lys_yang_parser_ctx *ctx, const char **data, enum ly_stmt enum_kw, struct lysp_type_enum **enums)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001686{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001687 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001688 char *buf, *word;
David Sedlák6544c182019-07-12 13:17:33 +02001689 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001690 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001691 struct lysp_type_enum *enm;
1692
Radek Krejci2c4e7172018-10-19 15:56:26 +02001693 LY_ARRAY_NEW_RET(ctx->ctx, *enums, enm, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001694
1695 /* get value */
Radek Krejcid6b76452019-09-03 17:03:03 +02001696 LY_CHECK_RET(get_argument(ctx, data, enum_kw == LY_STMT_ENUM ? Y_STR_ARG : Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
1697 if (enum_kw == LY_STMT_ENUM) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001698 ret = lysp_check_enum_name((struct lys_parser_ctx *)ctx, (const char *)word, word_len);
David Sedlák6544c182019-07-12 13:17:33 +02001699 LY_CHECK_ERR_RET(ret, free(buf), ret);
Radek Krejci335332a2019-09-05 13:03:35 +02001700 } /* else nothing specific for YANG_BIT */
Radek Krejci8b764662018-11-14 14:15:13 +01001701
Radek Krejci44ceedc2018-10-02 15:54:31 +02001702 INSERT_WORD(ctx, buf, enm->name, word, word_len);
Radek Krejci8b764662018-11-14 14:15:13 +01001703 CHECK_UNIQUENESS(ctx, *enums, name, ly_stmt2str(enum_kw), enm->name);
1704
Radek Krejci6d6556c2018-11-08 09:37:45 +01001705 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001706 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001707 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001708 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &enm->dsc, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001709 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001710 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02001711 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", ly_stmt2str(enum_kw));
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001712 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &enm->iffeatures, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001713 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001714 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001715 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &enm->ref, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001716 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001717 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001718 LY_CHECK_RET(parse_status(ctx, data, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001719 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001720 case LY_STMT_VALUE:
1721 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 +02001722 ly_stmt2str(enum_kw)), LY_EVALID);
1723 LY_CHECK_RET(parse_type_enum_value_pos(ctx, data, kw, &enm->value, &enm->flags, &enm->exts));
1724 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001725 case LY_STMT_POSITION:
1726 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 +02001727 ly_stmt2str(enum_kw)), LY_EVALID);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001728 LY_CHECK_RET(parse_type_enum_value_pos(ctx, data, kw, &enm->value, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001729 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001730 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001731 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001732 break;
1733 default:
David Sedlákb3077192019-06-19 10:55:37 +02001734 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001735 return LY_EVALID;
1736 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001737 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001738 return ret;
1739}
1740
Michal Vaskoea5abea2018-09-18 13:10:54 +02001741/**
1742 * @brief Parse the fraction-digits statement. Substatement of type statement.
1743 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001744 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001745 * @param[in,out] data Data to read from, always moved to currently handled character.
1746 * @param[in,out] fracdig Value to write to.
1747 * @param[in,out] exts Extension instances to add to.
1748 *
1749 * @return LY_ERR values.
1750 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001751static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001752parse_type_fracdigits(struct lys_yang_parser_ctx *ctx, const char **data, uint8_t *fracdig, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001753{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001754 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001755 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001756 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001757 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02001758 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001759
1760 if (*fracdig) {
David Sedlákb3077192019-06-19 10:55:37 +02001761 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001762 return LY_EVALID;
1763 }
1764
1765 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001766 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001767
1768 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
David Sedlákb3077192019-06-19 10:55:37 +02001769 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001770 free(buf);
1771 return LY_EVALID;
1772 }
1773
1774 errno = 0;
1775 num = strtoul(word, &ptr, 10);
1776 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001777 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001778 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001779 free(buf);
1780 return LY_EVALID;
1781 }
1782 if ((errno == ERANGE) || (num > 18)) {
David Sedlákb3077192019-06-19 10:55:37 +02001783 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001784 free(buf);
1785 return LY_EVALID;
1786 }
1787 *fracdig = num;
1788 free(buf);
1789
Radek Krejci6d6556c2018-11-08 09:37:45 +01001790 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001791 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001792 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001793 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_FRACDIGITS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001794 break;
1795 default:
David Sedlákb3077192019-06-19 10:55:37 +02001796 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001797 return LY_EVALID;
1798 }
1799 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001800 return ret;
1801}
1802
Michal Vaskoea5abea2018-09-18 13:10:54 +02001803/**
1804 * @brief Parse the require-instance statement. Substatement of type statement.
1805 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001806 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001807 * @param[in,out] data Data to read from, always moved to currently handled character.
1808 * @param[in,out] reqinst Value to write to.
1809 * @param[in,out] flags Flags to write to.
1810 * @param[in,out] exts Extension instances to add to.
1811 *
1812 * @return LY_ERR values.
1813 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001814static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001815parse_type_reqinstance(struct lys_yang_parser_ctx *ctx, const char **data, uint8_t *reqinst, uint16_t *flags,
Michal Vasko7fbc8162018-09-17 10:35:16 +02001816 struct lysp_ext_instance **exts)
1817{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001818 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001819 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001820 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001821 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001822
1823 if (*flags & LYS_SET_REQINST) {
David Sedlákb3077192019-06-19 10:55:37 +02001824 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001825 return LY_EVALID;
1826 }
1827 *flags |= LYS_SET_REQINST;
1828
1829 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001830 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001831
1832 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1833 *reqinst = 1;
1834 } else if ((word_len != 5) || strncmp(word, "false", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001835 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001836 free(buf);
1837 return LY_EVALID;
1838 }
1839 free(buf);
1840
Radek Krejci6d6556c2018-11-08 09:37:45 +01001841 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001842 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001843 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001844 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_REQINSTANCE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001845 break;
1846 default:
David Sedlákb3077192019-06-19 10:55:37 +02001847 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001848 return LY_EVALID;
1849 }
1850 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001851 return ret;
1852}
1853
Michal Vaskoea5abea2018-09-18 13:10:54 +02001854/**
1855 * @brief Parse the modifier statement. Substatement of type pattern statement.
1856 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001857 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001858 * @param[in,out] data Data to read from, always moved to currently handled character.
1859 * @param[in,out] pat Value to write to.
1860 * @param[in,out] exts Extension instances to add to.
1861 *
1862 * @return LY_ERR values.
1863 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001864static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001865parse_type_pattern_modifier(struct lys_yang_parser_ctx *ctx, const char **data, const char **pat, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001866{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001867 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001868 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001869 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001870 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001871
1872 if ((*pat)[0] == 0x15) {
David Sedlákb3077192019-06-19 10:55:37 +02001873 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001874 return LY_EVALID;
1875 }
1876
1877 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001878 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001879
1880 if ((word_len != 12) || strncmp(word, "invert-match", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001881 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001882 free(buf);
1883 return LY_EVALID;
1884 }
1885 free(buf);
1886
1887 /* replace the value in the dictionary */
1888 buf = malloc(strlen(*pat) + 1);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001889 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001890 strcpy(buf, *pat);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001891 lydict_remove(ctx->ctx, *pat);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001892
1893 assert(buf[0] == 0x06);
1894 buf[0] = 0x15;
Radek Krejci44ceedc2018-10-02 15:54:31 +02001895 *pat = lydict_insert_zc(ctx->ctx, buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001896
Radek Krejci6d6556c2018-11-08 09:37:45 +01001897 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001898 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001899 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001900 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MODIFIER, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001901 break;
1902 default:
David Sedlákb3077192019-06-19 10:55:37 +02001903 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001904 return LY_EVALID;
1905 }
1906 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001907 return ret;
1908}
1909
Michal Vaskoea5abea2018-09-18 13:10:54 +02001910/**
1911 * @brief Parse the pattern statement. Substatement of type statement.
1912 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001913 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001914 * @param[in,out] data Data to read from, always moved to currently handled character.
1915 * @param[in,out] patterns Restrictions to add to.
1916 *
1917 * @return LY_ERR values.
1918 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001919static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001920parse_type_pattern(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001921{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001922 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001923 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001924 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001925 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001926 struct lysp_restr *restr;
1927
Radek Krejci2c4e7172018-10-19 15:56:26 +02001928 LY_ARRAY_NEW_RET(ctx->ctx, *patterns, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001929
1930 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001931 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001932
1933 /* add special meaning first byte */
1934 if (buf) {
1935 buf = realloc(buf, word_len + 2);
1936 word = buf;
1937 } else {
1938 buf = malloc(word_len + 2);
1939 }
Radek Krejci44ceedc2018-10-02 15:54:31 +02001940 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejci86d106e2018-10-18 09:53:19 +02001941 memmove(buf + 1, word, word_len);
1942 buf[0] = 0x06; /* pattern's default regular-match flag */
1943 buf[word_len + 1] = '\0'; /* terminating NULL byte */
1944 restr->arg = lydict_insert_zc(ctx->ctx, buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001945
Radek Krejci6d6556c2018-11-08 09:37:45 +01001946 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001947 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001948 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001949 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001950 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001951 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001952 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001953 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001954 case LY_STMT_ERROR_APP_TAG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001955 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRTAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001956 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001957 case LY_STMT_ERROR_MESSAGE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001958 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRMSG, 0, &restr->emsg, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001959 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001960 case LY_STMT_MODIFIER:
Radek Krejci335332a2019-09-05 13:03:35 +02001961 PARSER_CHECK_STMTVER2_RET(ctx, "modifier", "pattern");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001962 LY_CHECK_RET(parse_type_pattern_modifier(ctx, data, &restr->arg, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001963 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001964 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001965 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001966 break;
1967 default:
David Sedlákb3077192019-06-19 10:55:37 +02001968 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001969 return LY_EVALID;
1970 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001971 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001972 return ret;
1973}
1974
Michal Vaskoea5abea2018-09-18 13:10:54 +02001975/**
1976 * @brief Parse the type statement.
1977 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001978 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001979 * @param[in,out] data Data to read from, always moved to currently handled character.
1980 * @param[in,out] type Type to wrote to.
1981 *
1982 * @return LY_ERR values.
1983 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001984static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001985parse_type(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001986{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001987 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001988 char *buf, *word;
Michal Vasko004d3152020-06-11 19:59:22 +02001989 const char *str_path = NULL;
Radek Krejciefd22f62018-09-27 11:47:58 +02001990 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001991 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001992 struct lysp_type *nest_type;
1993
1994 if (type->name) {
David Sedlákb3077192019-06-19 10:55:37 +02001995 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001996 return LY_EVALID;
1997 }
1998
1999 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002000 LY_CHECK_RET(get_argument(ctx, data, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002001 INSERT_WORD(ctx, buf, type->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002002
Radek Krejci6d6556c2018-11-08 09:37:45 +01002003 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002004 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002005 case LY_STMT_BASE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002006 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_BASE, &type->bases, Y_PREF_IDENTIF_ARG, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002007 type->flags |= LYS_SET_BASE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002008 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002009 case LY_STMT_BIT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002010 LY_CHECK_RET(parse_type_enum(ctx, data, kw, &type->bits));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002011 type->flags |= LYS_SET_BIT;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002012 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002013 case LY_STMT_ENUM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002014 LY_CHECK_RET(parse_type_enum(ctx, data, kw, &type->enums));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002015 type->flags |= LYS_SET_ENUM;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002016 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002017 case LY_STMT_FRACTION_DIGITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002018 LY_CHECK_RET(parse_type_fracdigits(ctx, data, &type->fraction_digits, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002019 type->flags |= LYS_SET_FRDIGITS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002020 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002021 case LY_STMT_LENGTH:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002022 if (type->length) {
David Sedlákb3077192019-06-19 10:55:37 +02002023 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002024 return LY_EVALID;
2025 }
2026 type->length = calloc(1, sizeof *type->length);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002027 LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002028
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002029 LY_CHECK_RET(parse_restr(ctx, data, kw, type->length));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002030 type->flags |= LYS_SET_LENGTH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002031 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002032 case LY_STMT_PATH:
Michal Vasko004d3152020-06-11 19:59:22 +02002033 if (type->path) {
2034 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyext_substmt2str(LYEXT_SUBSTMT_PATH));
2035 return LY_EVALID;
2036 }
2037
2038 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_PATH, 0, &str_path, Y_STR_ARG, &type->exts));
2039 ret = ly_path_parse(ctx->ctx, str_path, 0, LY_PATH_BEGIN_EITHER, LY_PATH_LREF_TRUE,
2040 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path);
2041 lydict_remove(ctx->ctx, str_path);
2042 LY_CHECK_RET(ret);
Radek Krejcid505e3d2018-11-13 09:04:17 +01002043 type->flags |= LYS_SET_PATH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002044 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002045 case LY_STMT_PATTERN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002046 LY_CHECK_RET(parse_type_pattern(ctx, data, &type->patterns));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002047 type->flags |= LYS_SET_PATTERN;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002048 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002049 case LY_STMT_RANGE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002050 if (type->range) {
David Sedlákb3077192019-06-19 10:55:37 +02002051 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002052 return LY_EVALID;
2053 }
2054 type->range = calloc(1, sizeof *type->range);
David Sedlák7a8b2472019-07-11 15:08:34 +02002055 LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002056
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002057 LY_CHECK_RET(parse_restr(ctx, data, kw, type->range));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002058 type->flags |= LYS_SET_RANGE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002059 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002060 case LY_STMT_REQUIRE_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002061 LY_CHECK_RET(parse_type_reqinstance(ctx, data, &type->require_instance, &type->flags, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002062 /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002063 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002064 case LY_STMT_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002065 LY_ARRAY_NEW_RET(ctx->ctx, type->types, nest_type, LY_EMEM);
2066 LY_CHECK_RET(parse_type(ctx, data, nest_type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002067 type->flags |= LYS_SET_TYPE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002068 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002069 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002070 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &type->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002071 break;
2072 default:
David Sedlákb3077192019-06-19 10:55:37 +02002073 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002074 return LY_EVALID;
2075 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002076 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002077 return ret;
2078}
2079
Michal Vaskoea5abea2018-09-18 13:10:54 +02002080/**
2081 * @brief Parse the leaf statement.
2082 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002083 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002084 * @param[in,out] data Data to read from, always moved to currently handled character.
2085 * @param[in,out] siblings Siblings to add to.
2086 *
2087 * @return LY_ERR values.
2088 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002089LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002090parse_leaf(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002091{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002092 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002093 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002094 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002095 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002096 struct lysp_node_leaf *leaf;
2097
David Sedlák60adc092019-08-06 15:57:02 +02002098 /* create new leaf structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02002099 LY_LIST_NEW_RET(ctx->ctx, siblings, leaf, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002100 leaf->nodetype = LYS_LEAF;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002101 leaf->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002102
Michal Vasko7fbc8162018-09-17 10:35:16 +02002103 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002104 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002105 INSERT_WORD(ctx, buf, leaf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002106
2107 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01002108 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002109 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002110 case LY_STMT_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002111 LY_CHECK_RET(parse_config(ctx, data, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002112 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002113 case LY_STMT_DEFAULT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002114 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &leaf->dflt, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002115 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002116 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002117 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &leaf->dsc, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002118 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002119 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002120 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &leaf->iffeatures, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002121 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002122 case LY_STMT_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002123 LY_CHECK_RET(parse_mandatory(ctx, data, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002124 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002125 case LY_STMT_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002126 LY_CHECK_RET(parse_restrs(ctx, data, kw, &leaf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002127 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002128 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002129 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &leaf->ref, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002130 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002131 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002132 LY_CHECK_RET(parse_status(ctx, data, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002133 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002134 case LY_STMT_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002135 LY_CHECK_RET(parse_type(ctx, data, &leaf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002136 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002137 case LY_STMT_UNITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002138 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, &leaf->units, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002139 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002140 case LY_STMT_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002141 LY_CHECK_RET(parse_when(ctx, data, &leaf->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002142 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002143 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002144 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002145 break;
2146 default:
David Sedlákb3077192019-06-19 10:55:37 +02002147 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002148 return LY_EVALID;
2149 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002150 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002151 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002152checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002153 /* mandatory substatements */
2154 if (!leaf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002155 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002156 return LY_EVALID;
2157 }
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002158 if ((leaf->flags & LYS_MAND_TRUE) && (leaf->dflt)) {
David Sedlákb3077192019-06-19 10:55:37 +02002159 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMSCOMB, "mandatory", "default", "leaf");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002160 return LY_EVALID;
2161 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002162
2163 return ret;
2164}
2165
Michal Vaskoea5abea2018-09-18 13:10:54 +02002166/**
2167 * @brief Parse the max-elements statement.
2168 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002169 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002170 * @param[in,out] data Data to read from, always moved to currently handled character.
2171 * @param[in,out] max Value to write to.
2172 * @param[in,out] flags Flags to write to.
2173 * @param[in,out] exts Extension instances to add to.
2174 *
2175 * @return LY_ERR values.
2176 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002177LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002178parse_maxelements(struct lys_yang_parser_ctx *ctx, const char **data, uint32_t *max, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002179{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002180 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002181 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002182 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002183 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002184 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002185
2186 if (*flags & LYS_SET_MAX) {
David Sedlákb3077192019-06-19 10:55:37 +02002187 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002188 return LY_EVALID;
2189 }
2190 *flags |= LYS_SET_MAX;
2191
2192 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002193 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002194
2195 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
David Sedlákb3077192019-06-19 10:55:37 +02002196 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002197 free(buf);
2198 return LY_EVALID;
2199 }
2200
Radek Krejci7f9b6512019-09-18 13:11:09 +02002201 if (ly_strncmp("unbounded", word, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002202 errno = 0;
2203 num = strtoul(word, &ptr, 10);
2204 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002205 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002206 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002207 free(buf);
2208 return LY_EVALID;
2209 }
2210 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002211 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002212 free(buf);
2213 return LY_EVALID;
2214 }
2215
2216 *max = num;
2217 }
2218 free(buf);
2219
Radek Krejci6d6556c2018-11-08 09:37:45 +01002220 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002221 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002222 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002223 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MAX, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002224 break;
2225 default:
David Sedlákb3077192019-06-19 10:55:37 +02002226 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002227 return LY_EVALID;
2228 }
2229 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002230 return ret;
2231}
2232
Michal Vaskoea5abea2018-09-18 13:10:54 +02002233/**
2234 * @brief Parse the min-elements statement.
2235 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002236 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002237 * @param[in,out] data Data to read from, always moved to currently handled character.
2238 * @param[in,out] min Value to write to.
2239 * @param[in,out] flags Flags to write to.
2240 * @param[in,out] exts Extension instances to add to.
2241 *
2242 * @return LY_ERR values.
2243 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002244LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002245parse_minelements(struct lys_yang_parser_ctx *ctx, const char **data, uint32_t *min, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002246{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002247 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002248 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002249 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002250 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002251 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002252
2253 if (*flags & LYS_SET_MIN) {
David Sedlákb3077192019-06-19 10:55:37 +02002254 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002255 return LY_EVALID;
2256 }
2257 *flags |= LYS_SET_MIN;
2258
2259 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002260 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002261
2262 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
David Sedlákb3077192019-06-19 10:55:37 +02002263 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002264 free(buf);
2265 return LY_EVALID;
2266 }
2267
2268 errno = 0;
2269 num = strtoul(word, &ptr, 10);
2270 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002271 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002272 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002273 free(buf);
2274 return LY_EVALID;
2275 }
2276 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002277 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002278 free(buf);
2279 return LY_EVALID;
2280 }
2281 *min = num;
2282 free(buf);
2283
Radek Krejci6d6556c2018-11-08 09:37:45 +01002284 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002285 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002286 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002287 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MIN, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002288 break;
2289 default:
David Sedlákb3077192019-06-19 10:55:37 +02002290 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002291 return LY_EVALID;
2292 }
2293 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002294 return ret;
2295}
2296
Michal Vaskoea5abea2018-09-18 13:10:54 +02002297/**
2298 * @brief Parse the ordered-by statement.
2299 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002300 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002301 * @param[in,out] data Data to read from, always moved to currently handled character.
2302 * @param[in,out] flags Flags to write to.
2303 * @param[in,out] exts Extension instances to add to.
2304 *
2305 * @return LY_ERR values.
2306 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002307static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002308parse_orderedby(struct lys_yang_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002309{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002310 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002311 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002312 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002313 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002314
2315 if (*flags & LYS_ORDBY_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02002316 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002317 return LY_EVALID;
2318 }
2319
2320 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002321 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002322
2323 if ((word_len == 6) && !strncmp(word, "system", word_len)) {
2324 *flags |= LYS_ORDBY_SYSTEM;
2325 } else if ((word_len == 4) && !strncmp(word, "user", word_len)) {
2326 *flags |= LYS_ORDBY_USER;
2327 } else {
David Sedlákb3077192019-06-19 10:55:37 +02002328 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002329 free(buf);
2330 return LY_EVALID;
2331 }
2332 free(buf);
2333
Radek Krejci6d6556c2018-11-08 09:37:45 +01002334 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002335 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002336 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002337 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_ORDEREDBY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002338 break;
2339 default:
David Sedlákb3077192019-06-19 10:55:37 +02002340 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002341 return LY_EVALID;
2342 }
2343 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002344 return ret;
2345}
2346
Michal Vaskoea5abea2018-09-18 13:10:54 +02002347/**
2348 * @brief Parse the leaf-list statement.
2349 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002350 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002351 * @param[in,out] data Data to read from, always moved to currently handled character.
2352 * @param[in,out] siblings Siblings to add to.
2353 *
2354 * @return LY_ERR values.
2355 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002356LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002357parse_leaflist(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002358{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002359 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002360 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002361 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002362 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002363 struct lysp_node_leaflist *llist;
2364
David Sedlák60adc092019-08-06 15:57:02 +02002365 /* create new leaf-list structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02002366 LY_LIST_NEW_RET(ctx->ctx, siblings, llist, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002367 llist->nodetype = LYS_LEAFLIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002368 llist->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002369
Michal Vasko7fbc8162018-09-17 10:35:16 +02002370 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002371 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002372 INSERT_WORD(ctx, buf, llist->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002373
2374 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01002375 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002376 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002377 case LY_STMT_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002378 LY_CHECK_RET(parse_config(ctx, data, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002379 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002380 case LY_STMT_DEFAULT:
Radek Krejci335332a2019-09-05 13:03:35 +02002381 PARSER_CHECK_STMTVER2_RET(ctx, "default", "leaf-list");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002382 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_DEFAULT, &llist->dflts, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002383 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002384 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002385 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &llist->dsc, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002386 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002387 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002388 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &llist->iffeatures, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002389 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002390 case LY_STMT_MAX_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002391 LY_CHECK_RET(parse_maxelements(ctx, data, &llist->max, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002392 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002393 case LY_STMT_MIN_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002394 LY_CHECK_RET(parse_minelements(ctx, data, &llist->min, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002395 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002396 case LY_STMT_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002397 LY_CHECK_RET(parse_restrs(ctx, data, kw, &llist->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002398 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002399 case LY_STMT_ORDERED_BY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002400 LY_CHECK_RET(parse_orderedby(ctx, data, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002401 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002402 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002403 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &llist->ref, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002404 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002405 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002406 LY_CHECK_RET(parse_status(ctx, data, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002407 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002408 case LY_STMT_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002409 LY_CHECK_RET(parse_type(ctx, data, &llist->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002410 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002411 case LY_STMT_UNITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002412 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, &llist->units, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002413 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002414 case LY_STMT_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002415 LY_CHECK_RET(parse_when(ctx, data, &llist->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002416 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002417 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002418 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002419 break;
2420 default:
David Sedlákb3077192019-06-19 10:55:37 +02002421 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002422 return LY_EVALID;
2423 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002424 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002425 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002426checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002427 /* mandatory substatements */
2428 if (!llist->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002429 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002430 return LY_EVALID;
2431 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01002432 if ((llist->min) && (llist->dflts)) {
David Sedlákb3077192019-06-19 10:55:37 +02002433 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMSCOMB, "min-elements", "default", "leaf-list");
Radek Krejci0e5d8382018-11-28 16:37:53 +01002434 return LY_EVALID;
2435 }
Radek Krejcidf6cad12018-11-28 17:10:55 +01002436 if (llist->max && llist->min > llist->max) {
David Sedlákb3077192019-06-19 10:55:37 +02002437 LOGVAL_PARSER(ctx, LYVE_SEMANTICS,
Radek Krejcidf6cad12018-11-28 17:10:55 +01002438 "Invalid combination of min-elements and max-elements: min value %u is bigger than the max value %u.",
2439 llist->min, llist->max);
2440 return LY_EVALID;
2441 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002442
2443 return ret;
2444}
2445
Michal Vaskoea5abea2018-09-18 13:10:54 +02002446/**
2447 * @brief Parse the refine statement.
2448 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002449 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002450 * @param[in,out] data Data to read from, always moved to currently handled character.
2451 * @param[in,out] refines Refines to add to.
2452 *
2453 * @return LY_ERR values.
2454 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002455static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002456parse_refine(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002457{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002458 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002459 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002460 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002461 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002462 struct lysp_refine *rf;
2463
Radek Krejci2c4e7172018-10-19 15:56:26 +02002464 LY_ARRAY_NEW_RET(ctx->ctx, *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002465
2466 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002467 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01002468 CHECK_NONEMPTY(ctx, word_len, "refine");
Radek Krejci44ceedc2018-10-02 15:54:31 +02002469 INSERT_WORD(ctx, buf, rf->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002470
Radek Krejci6d6556c2018-11-08 09:37:45 +01002471 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002472 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002473 case LY_STMT_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002474 LY_CHECK_RET(parse_config(ctx, data, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002475 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002476 case LY_STMT_DEFAULT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002477 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_DEFAULT, &rf->dflts, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002478 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002479 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002480 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &rf->dsc, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002481 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002482 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02002483 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "refine");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002484 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &rf->iffeatures, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002485 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002486 case LY_STMT_MAX_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002487 LY_CHECK_RET(parse_maxelements(ctx, data, &rf->max, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002488 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002489 case LY_STMT_MIN_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002490 LY_CHECK_RET(parse_minelements(ctx, data, &rf->min, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002491 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002492 case LY_STMT_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002493 LY_CHECK_RET(parse_restrs(ctx, data, kw, &rf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002494 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002495 case LY_STMT_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002496 LY_CHECK_RET(parse_mandatory(ctx, data, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002497 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002498 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002499 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &rf->ref, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002500 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002501 case LY_STMT_PRESENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002502 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_PRESENCE, 0, &rf->presence, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002503 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002504 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002505 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002506 break;
2507 default:
David Sedlákb3077192019-06-19 10:55:37 +02002508 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002509 return LY_EVALID;
2510 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002511 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002512 return ret;
2513}
2514
Michal Vaskoea5abea2018-09-18 13:10:54 +02002515/**
2516 * @brief Parse the typedef statement.
2517 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002518 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002519 * @param[in,out] data Data to read from, always moved to currently handled character.
2520 * @param[in,out] typedefs Typedefs to add to.
2521 *
2522 * @return LY_ERR values.
2523 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002524static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002525parse_typedef(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, const char **data, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002526{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002527 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002528 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002529 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002530 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002531 struct lysp_tpdf *tpdf;
2532
Radek Krejci2c4e7172018-10-19 15:56:26 +02002533 LY_ARRAY_NEW_RET(ctx->ctx, *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002534
2535 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002536 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002537 INSERT_WORD(ctx, buf, tpdf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002538
2539 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01002540 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002541 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002542 case LY_STMT_DEFAULT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002543 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &tpdf->dflt, Y_STR_ARG, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002544 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002545 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002546 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &tpdf->dsc, Y_STR_ARG, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002547 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002548 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002549 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &tpdf->ref, Y_STR_ARG, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002550 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002551 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002552 LY_CHECK_RET(parse_status(ctx, data, &tpdf->flags, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002553 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002554 case LY_STMT_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002555 LY_CHECK_RET(parse_type(ctx, data, &tpdf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002556 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002557 case LY_STMT_UNITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002558 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, &tpdf->units, Y_STR_ARG, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002559 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002560 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002561 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002562 break;
2563 default:
David Sedlákb3077192019-06-19 10:55:37 +02002564 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002565 return LY_EVALID;
2566 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002567 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002568 LY_CHECK_RET(ret);
Radek Krejcibbe09a92018-11-08 09:36:54 +01002569checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002570 /* mandatory substatements */
2571 if (!tpdf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002572 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002573 return LY_EVALID;
2574 }
2575
Radek Krejcibbe09a92018-11-08 09:36:54 +01002576 /* store data for collision check */
Michal Vasko1bf09392020-03-27 12:38:10 +01002577 if (parent && !(parent->nodetype & (LYS_GROUPING | LYS_RPC | LYS_ACTION | LYS_INOUT | LYS_NOTIF))) {
David Sedláke8b74df2019-08-14 14:18:22 +02002578 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, parent, 0) == -1, LY_EMEM);
Radek Krejcibbe09a92018-11-08 09:36:54 +01002579 }
2580
Michal Vasko7fbc8162018-09-17 10:35:16 +02002581 return ret;
2582}
2583
Michal Vaskoea5abea2018-09-18 13:10:54 +02002584/**
2585 * @brief Parse the input or output statement.
2586 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002587 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002588 * @param[in,out] data Data to read from, always moved to currently handled character.
2589 * @param[in] kw Type of this particular keyword
2590 * @param[in,out] inout_p Input/output pointer to write to.
2591 *
2592 * @return LY_ERR values.
2593 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002594static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002595parse_inout(struct lys_yang_parser_ctx *ctx, const char **data, enum ly_stmt inout_kw, struct lysp_node *parent, struct lysp_action_inout *inout_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002596{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002597 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002598 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002599 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002600 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002601
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002602 if (inout_p->nodetype) {
David Sedlákb3077192019-06-19 10:55:37 +02002603 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002604 return LY_EVALID;
2605 }
2606
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002607 /* initiate structure */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002608 inout_p->nodetype = &((struct lysp_action*)parent)->input == inout_p ? LYS_INPUT : LYS_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002609 inout_p->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002610
2611 /* parse substatements */
Radek Krejci7fc68292019-06-12 13:51:09 +02002612 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002613 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002614 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002615 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", ly_stmt2str(inout_kw));
Radek Krejci10113652018-11-14 16:56:50 +01002616 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002617 case LY_STMT_ANYXML:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002618 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002619 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002620 case LY_STMT_CHOICE:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002621 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002622 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002623 case LY_STMT_CONTAINER:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002624 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002625 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002626 case LY_STMT_LEAF:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002627 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002628 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002629 case LY_STMT_LEAF_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002630 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002631 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002632 case LY_STMT_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002633 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002634 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002635 case LY_STMT_USES:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002636 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002637 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002638 case LY_STMT_TYPEDEF:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002639 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)inout_p, data, &inout_p->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002640 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002641 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002642 PARSER_CHECK_STMTVER2_RET(ctx, "must", ly_stmt2str(inout_kw));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002643 LY_CHECK_RET(parse_restrs(ctx, data, kw, &inout_p->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002644 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002645 case LY_STMT_GROUPING:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002646 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)inout_p, &inout_p->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002647 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002648 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002649 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inout_p->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002650 break;
2651 default:
David Sedlákb3077192019-06-19 10:55:37 +02002652 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002653 return LY_EVALID;
2654 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002655 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002656 LY_CHECK_RET(ret);
Michal Vaskob83af8a2020-01-06 09:49:22 +01002657
Radek Krejci7fc68292019-06-12 13:51:09 +02002658checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002659 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01002660 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 +02002661
Michal Vaskob83af8a2020-01-06 09:49:22 +01002662 if (!inout_p->data) {
2663 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "data-def-stmt", ly_stmt2str(inout_kw));
2664 return LY_EVALID;
2665 }
2666
Michal Vasko7fbc8162018-09-17 10:35:16 +02002667 return ret;
2668}
2669
Michal Vaskoea5abea2018-09-18 13:10:54 +02002670/**
2671 * @brief Parse the action statement.
2672 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002673 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002674 * @param[in,out] data Data to read from, always moved to currently handled character.
2675 * @param[in,out] actions Actions to add to.
2676 *
2677 * @return LY_ERR values.
2678 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002679LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002680parse_action(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002681{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002682 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002683 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002684 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002685 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002686 struct lysp_action *act;
2687
Radek Krejci2c4e7172018-10-19 15:56:26 +02002688 LY_ARRAY_NEW_RET(ctx->ctx, *actions, act, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002689
2690 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002691 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002692 INSERT_WORD(ctx, buf, act->name, word, word_len);
Michal Vasko1bf09392020-03-27 12:38:10 +01002693 act->nodetype = parent ? LYS_ACTION : LYS_RPC;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002694 act->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002695
Radek Krejci7fc68292019-06-12 13:51:09 +02002696 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002697 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002698 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002699 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &act->dsc, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002700 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002701 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002702 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &act->iffeatures, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002703 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002704 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002705 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &act->ref, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002706 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002707 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002708 LY_CHECK_RET(parse_status(ctx, data, &act->flags, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002709 break;
2710
Radek Krejcid6b76452019-09-03 17:03:03 +02002711 case LY_STMT_INPUT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002712 LY_CHECK_RET(parse_inout(ctx, data, kw, (struct lysp_node*)act, &act->input));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002713 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002714 case LY_STMT_OUTPUT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002715 LY_CHECK_RET(parse_inout(ctx, data, kw, (struct lysp_node*)act, &act->output));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002716 break;
2717
Radek Krejcid6b76452019-09-03 17:03:03 +02002718 case LY_STMT_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01002719 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)act, data, &act->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002720 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002721 case LY_STMT_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002722 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)act, &act->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002723 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002724 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002725 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002726 break;
2727 default:
David Sedlákb3077192019-06-19 10:55:37 +02002728 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), parent ? "action" : "rpc");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002729 return LY_EVALID;
2730 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002731 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002732 LY_CHECK_RET(ret);
2733checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002734 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01002735 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, act->groupings, NULL, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02002736
Michal Vasko7fbc8162018-09-17 10:35:16 +02002737 return ret;
2738}
2739
Michal Vaskoea5abea2018-09-18 13:10:54 +02002740/**
2741 * @brief Parse the notification statement.
2742 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002743 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002744 * @param[in,out] data Data to read from, always moved to currently handled character.
2745 * @param[in,out] notifs Notifications to add to.
2746 *
2747 * @return LY_ERR values.
2748 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002749LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002750parse_notif(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002751{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002752 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002753 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002754 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002755 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002756 struct lysp_notif *notif;
2757
Radek Krejci2c4e7172018-10-19 15:56:26 +02002758 LY_ARRAY_NEW_RET(ctx->ctx, *notifs, notif, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002759
2760 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002761 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002762 INSERT_WORD(ctx, buf, notif->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002763 notif->nodetype = LYS_NOTIF;
2764 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002765
Radek Krejci7fc68292019-06-12 13:51:09 +02002766 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002767 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002768 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002769 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &notif->dsc, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002770 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002771 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002772 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &notif->iffeatures, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002773 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002774 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002775 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &notif->ref, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002776 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002777 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002778 LY_CHECK_RET(parse_status(ctx, data, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002779 break;
2780
Radek Krejcid6b76452019-09-03 17:03:03 +02002781 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002782 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "notification");
Radek Krejci10113652018-11-14 16:56:50 +01002783 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002784 case LY_STMT_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002785 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002786 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002787 case LY_STMT_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002788 LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002789 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002790 case LY_STMT_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002791 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002792 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002793 case LY_STMT_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002794 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002795 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002796 case LY_STMT_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002797 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002798 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002799 case LY_STMT_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002800 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002801 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002802 case LY_STMT_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002803 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002804 break;
2805
Radek Krejcid6b76452019-09-03 17:03:03 +02002806 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002807 PARSER_CHECK_STMTVER2_RET(ctx, "must", "notification");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002808 LY_CHECK_RET(parse_restrs(ctx, data, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002809 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002810 case LY_STMT_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01002811 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)notif, data, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002812 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002813 case LY_STMT_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002814 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002815 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002816 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002817 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002818 break;
2819 default:
David Sedlákb3077192019-06-19 10:55:37 +02002820 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002821 return LY_EVALID;
2822 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002823 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002824 LY_CHECK_RET(ret);
2825checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002826 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01002827 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, notif->groupings, NULL, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02002828
Michal Vasko7fbc8162018-09-17 10:35:16 +02002829 return ret;
2830}
2831
Michal Vaskoea5abea2018-09-18 13:10:54 +02002832/**
2833 * @brief Parse the grouping statement.
2834 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002835 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002836 * @param[in,out] data Data to read from, always moved to currently handled character.
2837 * @param[in,out] groupings Groupings to add to.
2838 *
2839 * @return LY_ERR values.
2840 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002841LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002842parse_grouping(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002843{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002844 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002845 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002846 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002847 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002848 struct lysp_grp *grp;
2849
Radek Krejci2c4e7172018-10-19 15:56:26 +02002850 LY_ARRAY_NEW_RET(ctx->ctx, *groupings, grp, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002851
2852 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002853 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002854 INSERT_WORD(ctx, buf, grp->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002855 grp->nodetype = LYS_GROUPING;
2856 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002857
Radek Krejci7fc68292019-06-12 13:51:09 +02002858 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002859 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002860 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002861 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &grp->dsc, Y_STR_ARG, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002862 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002863 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002864 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &grp->ref, Y_STR_ARG, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002865 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002866 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002867 LY_CHECK_RET(parse_status(ctx, data, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002868 break;
2869
Radek Krejcid6b76452019-09-03 17:03:03 +02002870 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002871 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "grouping");
Radek Krejci10113652018-11-14 16:56:50 +01002872 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002873 case LY_STMT_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002874 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002875 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002876 case LY_STMT_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002877 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002878 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002879 case LY_STMT_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002880 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002881 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002882 case LY_STMT_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002883 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002884 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002885 case LY_STMT_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002886 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002887 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002888 case LY_STMT_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002889 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002890 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002891 case LY_STMT_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002892 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002893 break;
2894
Radek Krejcid6b76452019-09-03 17:03:03 +02002895 case LY_STMT_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01002896 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)grp, data, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002897 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002898 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02002899 PARSER_CHECK_STMTVER2_RET(ctx, "action", "grouping");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002900 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)grp, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002901 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002902 case LY_STMT_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002903 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)grp, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002904 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002905 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02002906 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "grouping");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002907 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)grp, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002908 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002909 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002910 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002911 break;
2912 default:
David Sedlákb3077192019-06-19 10:55:37 +02002913 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002914 return LY_EVALID;
2915 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002916 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002917 LY_CHECK_RET(ret);
2918checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002919 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01002920 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 +02002921
Michal Vasko7fbc8162018-09-17 10:35:16 +02002922 return ret;
2923}
2924
Michal Vaskoea5abea2018-09-18 13:10:54 +02002925/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02002926 * @brief Parse the augment statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002927 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002928 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002929 * @param[in,out] data Data to read from, always moved to currently handled character.
2930 * @param[in,out] augments Augments to add to.
2931 *
2932 * @return LY_ERR values.
2933 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002934LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002935parse_augment(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002936{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002937 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002938 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002939 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002940 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002941 struct lysp_augment *aug;
2942
Radek Krejci2c4e7172018-10-19 15:56:26 +02002943 LY_ARRAY_NEW_RET(ctx->ctx, *augments, aug, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002944
2945 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002946 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01002947 CHECK_NONEMPTY(ctx, word_len, "augment");
Radek Krejci44ceedc2018-10-02 15:54:31 +02002948 INSERT_WORD(ctx, buf, aug->nodeid, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002949 aug->nodetype = LYS_AUGMENT;
2950 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002951
Radek Krejci7fc68292019-06-12 13:51:09 +02002952 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002953 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002954 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002955 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &aug->dsc, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002956 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002957 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002958 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &aug->iffeatures, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002959 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002960 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002961 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &aug->ref, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002962 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002963 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002964 LY_CHECK_RET(parse_status(ctx, data, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002965 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002966 case LY_STMT_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002967 LY_CHECK_RET(parse_when(ctx, data, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002968 break;
2969
Radek Krejcid6b76452019-09-03 17:03:03 +02002970 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002971 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "augment");
Radek Krejci10113652018-11-14 16:56:50 +01002972 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002973 case LY_STMT_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002974 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002975 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002976 case LY_STMT_CASE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002977 LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002978 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002979 case LY_STMT_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002980 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002981 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002982 case LY_STMT_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002983 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002984 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002985 case LY_STMT_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002986 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002987 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002988 case LY_STMT_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002989 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002990 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002991 case LY_STMT_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002992 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002993 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002994 case LY_STMT_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002995 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002996 break;
2997
Radek Krejcid6b76452019-09-03 17:03:03 +02002998 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02002999 PARSER_CHECK_STMTVER2_RET(ctx, "action", "augment");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003000 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003001 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003002 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003003 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "augment");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003004 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003005 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003006 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003007 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003008 break;
3009 default:
David Sedlákb3077192019-06-19 10:55:37 +02003010 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003011 return LY_EVALID;
3012 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003013 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003014 LY_CHECK_RET(ret);
3015checks:
3016 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01003017 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 +02003018
Michal Vasko7fbc8162018-09-17 10:35:16 +02003019 return ret;
3020}
3021
Michal Vaskoea5abea2018-09-18 13:10:54 +02003022/**
3023 * @brief Parse the uses statement.
3024 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003025 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003026 * @param[in,out] data Data to read from, always moved to currently handled character.
3027 * @param[in,out] siblings Siblings to add to.
3028 *
3029 * @return LY_ERR values.
3030 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003031LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01003032parse_uses(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003033{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003034 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003035 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003036 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003037 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003038 struct lysp_node_uses *uses;
3039
David Sedlák60adc092019-08-06 15:57:02 +02003040 /* create uses structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003041 LY_LIST_NEW_RET(ctx->ctx, siblings, uses, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003042 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003043 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003044
Michal Vasko7fbc8162018-09-17 10:35:16 +02003045 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003046 LY_CHECK_RET(get_argument(ctx, data, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003047 INSERT_WORD(ctx, buf, uses->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003048
3049 /* parse substatements */
Radek Krejci7fc68292019-06-12 13:51:09 +02003050 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003051 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003052 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003053 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &uses->dsc, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003054 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003055 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003056 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &uses->iffeatures, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003057 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003058 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003059 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &uses->ref, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003060 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003061 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003062 LY_CHECK_RET(parse_status(ctx, data, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003063 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003064 case LY_STMT_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003065 LY_CHECK_RET(parse_when(ctx, data, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003066 break;
3067
Radek Krejcid6b76452019-09-03 17:03:03 +02003068 case LY_STMT_REFINE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003069 LY_CHECK_RET(parse_refine(ctx, data, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003070 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003071 case LY_STMT_AUGMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003072 LY_CHECK_RET(parse_augment(ctx, data, (struct lysp_node*)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003073 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003074 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003075 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003076 break;
3077 default:
David Sedlákb3077192019-06-19 10:55:37 +02003078 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003079 return LY_EVALID;
3080 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003081 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003082checks:
3083 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01003084 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, NULL, uses->augments, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02003085
Michal Vasko7fbc8162018-09-17 10:35:16 +02003086 return ret;
3087}
3088
Michal Vaskoea5abea2018-09-18 13:10:54 +02003089/**
3090 * @brief Parse the case statement.
3091 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003092 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003093 * @param[in,out] data Data to read from, always moved to currently handled character.
3094 * @param[in,out] siblings Siblings to add to.
3095 *
3096 * @return LY_ERR values.
3097 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003098LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01003099parse_case(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003100{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003101 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003102 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003103 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003104 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003105 struct lysp_node_case *cas;
3106
David Sedlák60adc092019-08-06 15:57:02 +02003107 /* create new case structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003108 LY_LIST_NEW_RET(ctx->ctx, siblings, cas, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003109 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003110 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003111
Michal Vasko7fbc8162018-09-17 10:35:16 +02003112 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003113 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003114 INSERT_WORD(ctx, buf, cas->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003115
3116 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01003117 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003118 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003119 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003120 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &cas->dsc, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003121 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003122 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003123 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &cas->iffeatures, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003124 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003125 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003126 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &cas->ref, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003127 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003128 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003129 LY_CHECK_RET(parse_status(ctx, data, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003130 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003131 case LY_STMT_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003132 LY_CHECK_RET(parse_when(ctx, data, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003133 break;
3134
Radek Krejcid6b76452019-09-03 17:03:03 +02003135 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003136 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "case");
Radek Krejci10113652018-11-14 16:56:50 +01003137 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003138 case LY_STMT_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003139 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003140 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003141 case LY_STMT_CHOICE:
Radek Krejcia9026eb2018-12-12 16:04:47 +01003142 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003143 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003144 case LY_STMT_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003145 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003146 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003147 case LY_STMT_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003148 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003149 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003150 case LY_STMT_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003151 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003152 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003153 case LY_STMT_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003154 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003155 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003156 case LY_STMT_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003157 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003158 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003159 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003160 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003161 break;
3162 default:
David Sedlákb3077192019-06-19 10:55:37 +02003163 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003164 return LY_EVALID;
3165 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003166 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003167 return ret;
3168}
3169
Michal Vaskoea5abea2018-09-18 13:10:54 +02003170/**
3171 * @brief Parse the choice statement.
3172 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003173 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003174 * @param[in,out] data Data to read from, always moved to currently handled character.
3175 * @param[in,out] siblings Siblings to add to.
3176 *
3177 * @return LY_ERR values.
3178 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003179LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01003180parse_choice(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003181{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003182 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003183 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003184 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003185 enum ly_stmt kw;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003186 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003187
David Sedlák60adc092019-08-06 15:57:02 +02003188 /* create new choice structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003189 LY_LIST_NEW_RET(ctx->ctx, siblings, choice, next, LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003190 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003191 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003192
Michal Vasko7fbc8162018-09-17 10:35:16 +02003193 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003194 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003195 INSERT_WORD(ctx, buf, choice->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003196
3197 /* parse substatements */
Radek Krejcia9026eb2018-12-12 16:04:47 +01003198 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003199 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003200 case LY_STMT_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003201 LY_CHECK_RET(parse_config(ctx, data, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003202 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003203 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003204 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &choice->dsc, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003205 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003206 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003207 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &choice->iffeatures, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003208 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003209 case LY_STMT_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003210 LY_CHECK_RET(parse_mandatory(ctx, data, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003211 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003212 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003213 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &choice->ref, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003214 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003215 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003216 LY_CHECK_RET(parse_status(ctx, data, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003217 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003218 case LY_STMT_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003219 LY_CHECK_RET(parse_when(ctx, data, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003220 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003221 case LY_STMT_DEFAULT:
Radek Krejcia9026eb2018-12-12 16:04:47 +01003222 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &choice->dflt, Y_PREF_IDENTIF_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003223 break;
3224
Radek Krejcid6b76452019-09-03 17:03:03 +02003225 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003226 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "choice");
Radek Krejci10113652018-11-14 16:56:50 +01003227 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003228 case LY_STMT_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003229 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003230 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003231 case LY_STMT_CASE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003232 LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003233 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003234 case LY_STMT_CHOICE:
Radek Krejci335332a2019-09-05 13:03:35 +02003235 PARSER_CHECK_STMTVER2_RET(ctx, "choice", "choice");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003236 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003237 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003238 case LY_STMT_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003239 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003240 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003241 case LY_STMT_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003242 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003243 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003244 case LY_STMT_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003245 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003246 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003247 case LY_STMT_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003248 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003249 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003250 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003251 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003252 break;
3253 default:
David Sedlákb3077192019-06-19 10:55:37 +02003254 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003255 return LY_EVALID;
3256 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003257 }
Radek Krejcia9026eb2018-12-12 16:04:47 +01003258 LY_CHECK_RET(ret);
3259checks:
3260 if ((choice->flags & LYS_MAND_TRUE) && choice->dflt) {
David Sedlákb3077192019-06-19 10:55:37 +02003261 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMSCOMB, "mandatory", "default", "choice");
Radek Krejcia9026eb2018-12-12 16:04:47 +01003262 return LY_EVALID;
3263 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003264 return ret;
3265}
3266
Michal Vaskoea5abea2018-09-18 13:10:54 +02003267/**
3268 * @brief Parse the container statement.
3269 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003270 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003271 * @param[in,out] data Data to read from, always moved to currently handled character.
3272 * @param[in,out] siblings Siblings to add to.
3273 *
3274 * @return LY_ERR values.
3275 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003276LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01003277parse_container(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003278{
3279 LY_ERR ret = 0;
3280 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003281 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003282 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003283 struct lysp_node_container *cont;
3284
David Sedlák60adc092019-08-06 15:57:02 +02003285 /* create new container structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003286 LY_LIST_NEW_RET(ctx->ctx, siblings, cont, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003287 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003288 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003289
Michal Vasko7fbc8162018-09-17 10:35:16 +02003290 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003291 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003292 INSERT_WORD(ctx, buf, cont->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003293
3294 /* parse substatements */
Radek Krejci7fc68292019-06-12 13:51:09 +02003295 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003296 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003297 case LY_STMT_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003298 LY_CHECK_RET(parse_config(ctx, data, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003299 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003300 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003301 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &cont->dsc, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003302 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003303 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003304 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &cont->iffeatures, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003305 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003306 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003307 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &cont->ref, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003308 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003309 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003310 LY_CHECK_RET(parse_status(ctx, data, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003311 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003312 case LY_STMT_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003313 LY_CHECK_RET(parse_when(ctx, data, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003314 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003315 case LY_STMT_PRESENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003316 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_PRESENCE, 0, &cont->presence, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003317 break;
3318
Radek Krejcid6b76452019-09-03 17:03:03 +02003319 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003320 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "container");
Radek Krejci10113652018-11-14 16:56:50 +01003321 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003322 case LY_STMT_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003323 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003324 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003325 case LY_STMT_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003326 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003327 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003328 case LY_STMT_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003329 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003330 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003331 case LY_STMT_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003332 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003333 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003334 case LY_STMT_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003335 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003336 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003337 case LY_STMT_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003338 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003339 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003340 case LY_STMT_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003341 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003342 break;
3343
Radek Krejcid6b76452019-09-03 17:03:03 +02003344 case LY_STMT_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01003345 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)cont, data, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003346 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003347 case LY_STMT_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003348 LY_CHECK_RET(parse_restrs(ctx, data, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003349 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003350 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003351 PARSER_CHECK_STMTVER2_RET(ctx, "action", "container");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003352 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003353 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003354 case LY_STMT_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003355 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003356 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003357 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003358 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "container");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003359 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003360 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003361 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003362 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003363 break;
3364 default:
David Sedlákb3077192019-06-19 10:55:37 +02003365 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003366 return LY_EVALID;
3367 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003368 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003369checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01003370 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01003371 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 +02003372 return ret;
3373}
3374
Michal Vaskoea5abea2018-09-18 13:10:54 +02003375/**
3376 * @brief Parse the list statement.
3377 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003378 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003379 * @param[in,out] data Data to read from, always moved to currently handled character.
3380 * @param[in,out] siblings Siblings to add to.
3381 *
3382 * @return LY_ERR values.
3383 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003384LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01003385parse_list(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003386{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003387 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003388 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003389 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003390 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003391 struct lysp_node_list *list;
3392
David Sedlák60adc092019-08-06 15:57:02 +02003393 /* create new list structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003394 LY_LIST_NEW_RET(ctx->ctx, siblings, list, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003395 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003396 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003397
Michal Vasko7fbc8162018-09-17 10:35:16 +02003398 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003399 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003400 INSERT_WORD(ctx, buf, list->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003401
3402 /* parse substatements */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003403 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003404 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003405 case LY_STMT_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003406 LY_CHECK_RET(parse_config(ctx, data, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003407 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003408 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003409 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &list->dsc, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003410 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003411 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003412 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &list->iffeatures, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003413 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003414 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003415 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &list->ref, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003416 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003417 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003418 LY_CHECK_RET(parse_status(ctx, data, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003419 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003420 case LY_STMT_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003421 LY_CHECK_RET(parse_when(ctx, data, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003422 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003423 case LY_STMT_KEY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003424 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_KEY, 0, &list->key, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003425 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003426 case LY_STMT_MAX_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003427 LY_CHECK_RET(parse_maxelements(ctx, data, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003428 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003429 case LY_STMT_MIN_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003430 LY_CHECK_RET(parse_minelements(ctx, data, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003431 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003432 case LY_STMT_ORDERED_BY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003433 LY_CHECK_RET(parse_orderedby(ctx, data, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003434 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003435 case LY_STMT_UNIQUE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003436 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_UNIQUE, &list->uniques, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003437 break;
3438
Radek Krejcid6b76452019-09-03 17:03:03 +02003439 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003440 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "list");
Radek Krejci10113652018-11-14 16:56:50 +01003441 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003442 case LY_STMT_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003443 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003444 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003445 case LY_STMT_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003446 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003447 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003448 case LY_STMT_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003449 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003450 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003451 case LY_STMT_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003452 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003453 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003454 case LY_STMT_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003455 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003456 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003457 case LY_STMT_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003458 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003459 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003460 case LY_STMT_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003461 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003462 break;
3463
Radek Krejcid6b76452019-09-03 17:03:03 +02003464 case LY_STMT_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01003465 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)list, data, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003466 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003467 case LY_STMT_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003468 LY_CHECK_RET(parse_restrs(ctx, data, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003469 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003470 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003471 PARSER_CHECK_STMTVER2_RET(ctx, "action", "list");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003472 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003473 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003474 case LY_STMT_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003475 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003476 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003477 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003478 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "list");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003479 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003480 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003481 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003482 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003483 break;
3484 default:
David Sedlákb3077192019-06-19 10:55:37 +02003485 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003486 return LY_EVALID;
3487 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003488 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003489 LY_CHECK_RET(ret);
3490checks:
Radek Krejci7fc68292019-06-12 13:51:09 +02003491 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01003492 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 +02003493
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003494 if (list->max && list->min > list->max) {
David Sedlákb3077192019-06-19 10:55:37 +02003495 LOGVAL_PARSER(ctx, LYVE_SEMANTICS,
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003496 "Invalid combination of min-elements and max-elements: min value %u is bigger than the max value %u.",
3497 list->min, list->max);
3498 return LY_EVALID;
3499 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003500
3501 return ret;
3502}
3503
Michal Vaskoea5abea2018-09-18 13:10:54 +02003504/**
3505 * @brief Parse the yin-element statement.
3506 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003507 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003508 * @param[in,out] data Data to read from, always moved to currently handled character.
3509 * @param[in,out] flags Flags to write to.
3510 * @param[in,out] exts Extension instances to add to.
3511 *
3512 * @return LY_ERR values.
3513 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003514static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01003515parse_yinelement(struct lys_yang_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003516{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003517 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003518 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003519 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003520 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003521
3522 if (*flags & LYS_YINELEM_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02003523 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003524 return LY_EVALID;
3525 }
3526
3527 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003528 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003529
3530 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
3531 *flags |= LYS_YINELEM_TRUE;
3532 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
3533 *flags |= LYS_YINELEM_FALSE;
3534 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003535 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003536 free(buf);
3537 return LY_EVALID;
3538 }
3539 free(buf);
3540
Radek Krejci6d6556c2018-11-08 09:37:45 +01003541 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003542 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003543 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003544 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_YINELEM, 0, exts));
3545 LY_CHECK_RET(ret); break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003546 default:
David Sedlákb3077192019-06-19 10:55:37 +02003547 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003548 return LY_EVALID;
3549 }
3550 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003551 return ret;
3552}
3553
Michal Vaskoea5abea2018-09-18 13:10:54 +02003554/**
David Sedlák2444f8f2019-07-09 11:02:47 +02003555 * @brief Parse the argument statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003556 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003557 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003558 * @param[in,out] data Data to read from, always moved to currently handled character.
3559 * @param[in,out] argument Value to write to.
3560 * @param[in,out] flags Flags to write to.
3561 * @param[in,out] exts Extension instances to add to.
3562 *
3563 * @return LY_ERR values.
3564 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003565static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01003566parse_argument(struct lys_yang_parser_ctx *ctx, const char **data, const char **argument, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003567{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003568 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003569 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003570 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003571 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003572
3573 if (*argument) {
David Sedlákb3077192019-06-19 10:55:37 +02003574 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003575 return LY_EVALID;
3576 }
3577
3578 /* get value */
David Sedlák2444f8f2019-07-09 11:02:47 +02003579 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003580 INSERT_WORD(ctx, buf, *argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003581
Radek Krejci6d6556c2018-11-08 09:37:45 +01003582 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003583 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003584 case LY_STMT_YIN_ELEMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003585 LY_CHECK_RET(parse_yinelement(ctx, data, flags, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003586 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003587 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003588 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_ARGUMENT, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003589 break;
3590 default:
David Sedlákb3077192019-06-19 10:55:37 +02003591 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003592 return LY_EVALID;
3593 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003594 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003595 return ret;
3596}
3597
Michal Vaskoea5abea2018-09-18 13:10:54 +02003598/**
3599 * @brief Parse the extension statement.
3600 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003601 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003602 * @param[in,out] data Data to read from, always moved to currently handled character.
3603 * @param[in,out] extensions Extensions to add to.
3604 *
3605 * @return LY_ERR values.
3606 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003607static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01003608parse_extension(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003609{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003610 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003611 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003612 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003613 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003614 struct lysp_ext *ex;
3615
Radek Krejci2c4e7172018-10-19 15:56:26 +02003616 LY_ARRAY_NEW_RET(ctx->ctx, *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003617
3618 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003619 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003620 INSERT_WORD(ctx, buf, ex->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003621
Radek Krejci6d6556c2018-11-08 09:37:45 +01003622 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003623 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003624 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003625 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &ex->dsc, Y_STR_ARG, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003626 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003627 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003628 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &ex->ref, Y_STR_ARG, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003629 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003630 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003631 LY_CHECK_RET(parse_status(ctx, data, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003632 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003633 case LY_STMT_ARGUMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003634 LY_CHECK_RET(parse_argument(ctx, data, &ex->argument, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003635 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003636 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003637 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ex->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), "extension");
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 deviate statement.
3649 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003650 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003651 * @param[in,out] data Data to read from, always moved to currently handled character.
3652 * @param[in,out] deviates Deviates to add to.
3653 *
3654 * @return LY_ERR values.
3655 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003656LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01003657parse_deviate(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_deviate **deviates)
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, dev_mod;
Radek Krejcid6b76452019-09-03 17:03:03 +02003662 enum ly_stmt kw;
David Sedlák60adc092019-08-06 15:57:02 +02003663 struct lysp_deviate *d;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003664 struct lysp_deviate_add *d_add = NULL;
3665 struct lysp_deviate_rpl *d_rpl = NULL;
3666 struct lysp_deviate_del *d_del = NULL;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02003667 const char **d_units = NULL, ***d_uniques = NULL, ***d_dflts = NULL;
3668 struct lysp_restr **d_musts = NULL;
3669 uint16_t *d_flags = 0;
3670 uint32_t *d_min = 0, *d_max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003671
3672 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003673 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003674
3675 if ((word_len == 13) && !strncmp(word, "not-supported", word_len)) {
3676 dev_mod = LYS_DEV_NOT_SUPPORTED;
3677 } else if ((word_len == 3) && !strncmp(word, "add", word_len)) {
3678 dev_mod = LYS_DEV_ADD;
3679 } else if ((word_len == 7) && !strncmp(word, "replace", word_len)) {
3680 dev_mod = LYS_DEV_REPLACE;
3681 } else if ((word_len == 6) && !strncmp(word, "delete", word_len)) {
3682 dev_mod = LYS_DEV_DELETE;
3683 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003684 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003685 free(buf);
3686 return LY_EVALID;
3687 }
3688 free(buf);
3689
3690 /* create structure */
3691 switch (dev_mod) {
3692 case LYS_DEV_NOT_SUPPORTED:
3693 d = calloc(1, sizeof *d);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003694 LY_CHECK_ERR_RET(!d, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003695 break;
3696 case LYS_DEV_ADD:
3697 d_add = calloc(1, sizeof *d_add);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003698 LY_CHECK_ERR_RET(!d_add, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003699 d = (struct lysp_deviate *)d_add;
3700 d_units = &d_add->units;
3701 d_uniques = &d_add->uniques;
3702 d_dflts = &d_add->dflts;
3703 d_musts = &d_add->musts;
3704 d_flags = &d_add->flags;
3705 d_min = &d_add->min;
3706 d_max = &d_add->max;
3707 break;
3708 case LYS_DEV_REPLACE:
3709 d_rpl = calloc(1, sizeof *d_rpl);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003710 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003711 d = (struct lysp_deviate *)d_rpl;
3712 d_units = &d_rpl->units;
3713 d_flags = &d_rpl->flags;
3714 d_min = &d_rpl->min;
3715 d_max = &d_rpl->max;
3716 break;
3717 case LYS_DEV_DELETE:
3718 d_del = calloc(1, sizeof *d_del);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003719 LY_CHECK_ERR_RET(!d_del, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003720 d = (struct lysp_deviate *)d_del;
3721 d_units = &d_del->units;
3722 d_uniques = &d_del->uniques;
3723 d_dflts = &d_del->dflts;
3724 d_musts = &d_del->musts;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003725 break;
3726 default:
3727 assert(0);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003728 LOGINT_RET(ctx->ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003729 }
3730 d->mod = dev_mod;
3731
3732 /* insert into siblings */
David Sedlák60adc092019-08-06 15:57:02 +02003733 LY_LIST_INSERT(deviates, d, next);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003734
Radek Krejci6d6556c2018-11-08 09:37:45 +01003735 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003736 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003737 case LY_STMT_CONFIG:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003738 switch (dev_mod) {
3739 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003740 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003741 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003742 return LY_EVALID;
3743 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003744 LY_CHECK_RET(parse_config(ctx, data, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003745 break;
3746 }
3747 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003748 case LY_STMT_DEFAULT:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003749 switch (dev_mod) {
3750 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003751 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003752 return LY_EVALID;
3753 case LYS_DEV_REPLACE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003754 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &d_rpl->dflt, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003755 break;
3756 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003757 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_DEFAULT, d_dflts, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003758 break;
3759 }
3760 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003761 case LY_STMT_MANDATORY:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003762 switch (dev_mod) {
3763 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003764 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003765 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003766 return LY_EVALID;
3767 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003768 LY_CHECK_RET(parse_mandatory(ctx, data, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003769 break;
3770 }
3771 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003772 case LY_STMT_MAX_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003773 switch (dev_mod) {
3774 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003775 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003776 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003777 return LY_EVALID;
3778 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003779 LY_CHECK_RET(parse_maxelements(ctx, data, d_max, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003780 break;
3781 }
3782 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003783 case LY_STMT_MIN_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003784 switch (dev_mod) {
3785 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003786 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003787 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003788 return LY_EVALID;
3789 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003790 LY_CHECK_RET(parse_minelements(ctx, data, d_min, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003791 break;
3792 }
3793 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003794 case LY_STMT_MUST:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003795 switch (dev_mod) {
3796 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003797 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003798 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003799 return LY_EVALID;
3800 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003801 LY_CHECK_RET(parse_restrs(ctx, data, kw, d_musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003802 break;
3803 }
3804 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003805 case LY_STMT_TYPE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003806 switch (dev_mod) {
3807 case LYS_DEV_NOT_SUPPORTED:
3808 case LYS_DEV_ADD:
3809 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003810 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003811 return LY_EVALID;
3812 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003813 if (d_rpl->type) {
David Sedlákb3077192019-06-19 10:55:37 +02003814 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003815 return LY_EVALID;
3816 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003817 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003818 LY_CHECK_ERR_RET(!d_rpl->type, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003819 LY_CHECK_RET(parse_type(ctx, data, d_rpl->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003820 break;
3821 }
3822 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003823 case LY_STMT_UNIQUE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003824 switch (dev_mod) {
3825 case LYS_DEV_NOT_SUPPORTED:
3826 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003827 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003828 return LY_EVALID;
3829 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003830 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_UNIQUE, d_uniques, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003831 break;
3832 }
3833 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003834 case LY_STMT_UNITS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003835 switch (dev_mod) {
3836 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003837 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003838 return LY_EVALID;
3839 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003840 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, d_units, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003841 break;
3842 }
3843 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003844 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003845 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003846 break;
3847 default:
David Sedlákb3077192019-06-19 10:55:37 +02003848 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003849 return LY_EVALID;
3850 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003851 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003852 return ret;
3853}
3854
Michal Vaskoea5abea2018-09-18 13:10:54 +02003855/**
3856 * @brief Parse the deviation statement.
3857 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003858 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003859 * @param[in,out] data Data to read from, always moved to currently handled character.
3860 * @param[in,out] deviations Deviations to add to.
3861 *
3862 * @return LY_ERR values.
3863 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003864LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01003865parse_deviation(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003866{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003867 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003868 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003869 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003870 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003871 struct lysp_deviation *dev;
3872
Radek Krejci2c4e7172018-10-19 15:56:26 +02003873 LY_ARRAY_NEW_RET(ctx->ctx, *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003874
3875 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003876 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01003877 CHECK_NONEMPTY(ctx, word_len, "deviation");
Radek Krejci44ceedc2018-10-02 15:54:31 +02003878 INSERT_WORD(ctx, buf, dev->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003879
Radek Krejci6d6556c2018-11-08 09:37:45 +01003880 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003881 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003882 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003883 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &dev->dsc, Y_STR_ARG, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003884 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003885 case LY_STMT_DEVIATE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003886 LY_CHECK_RET(parse_deviate(ctx, data, &dev->deviates));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003887 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003888 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003889 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &dev->ref, Y_STR_ARG, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003890 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003891 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003892 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003893 break;
3894 default:
David Sedlákb3077192019-06-19 10:55:37 +02003895 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003896 return LY_EVALID;
3897 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003898 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003899 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01003900checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003901 /* mandatory substatements */
3902 if (!dev->deviates) {
David Sedlákb3077192019-06-19 10:55:37 +02003903 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003904 return LY_EVALID;
3905 }
3906
3907 return ret;
3908}
3909
Michal Vaskoea5abea2018-09-18 13:10:54 +02003910/**
3911 * @brief Parse the feature statement.
3912 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003913 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003914 * @param[in,out] data Data to read from, always moved to currently handled character.
3915 * @param[in,out] features Features to add to.
3916 *
3917 * @return LY_ERR values.
3918 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003919LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01003920parse_feature(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003921{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003922 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003923 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003924 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003925 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003926 struct lysp_feature *feat;
3927
Radek Krejci2c4e7172018-10-19 15:56:26 +02003928 LY_ARRAY_NEW_RET(ctx->ctx, *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003929
3930 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003931 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003932 INSERT_WORD(ctx, buf, feat->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01003933
Radek Krejci6d6556c2018-11-08 09:37:45 +01003934 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003935 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003936 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003937 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &feat->dsc, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003938 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003939 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003940 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &feat->iffeatures, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003941 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003942 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003943 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &feat->ref, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003944 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003945 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003946 LY_CHECK_RET(parse_status(ctx, data, &feat->flags, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003947 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003948 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003949 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003950 break;
3951 default:
David Sedlákb3077192019-06-19 10:55:37 +02003952 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003953 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003954 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003955 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003956 return ret;
3957}
3958
Michal Vaskoea5abea2018-09-18 13:10:54 +02003959/**
3960 * @brief Parse the identity statement.
3961 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003962 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003963 * @param[in,out] data Data to read from, always moved to currently handled character.
3964 * @param[in,out] identities Identities to add to.
3965 *
3966 * @return LY_ERR values.
3967 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003968LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01003969parse_identity(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_ident **identities)
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_ident *ident;
3976
Radek Krejci2c4e7172018-10-19 15:56:26 +02003977 LY_ARRAY_NEW_RET(ctx->ctx, *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003978
3979 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003980 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003981 INSERT_WORD(ctx, buf, ident->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01003982
Radek Krejci6d6556c2018-11-08 09:37:45 +01003983 YANG_READ_SUBSTMT_FOR(ctx, data, 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:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003986 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &ident->dsc, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003987 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003988 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02003989 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "identity");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003990 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &ident->iffeatures, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003991 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003992 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003993 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &ident->ref, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003994 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003995 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003996 LY_CHECK_RET(parse_status(ctx, data, &ident->flags, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003997 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003998 case LY_STMT_BASE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003999 if (ident->bases && ctx->mod_version < 2) {
David Sedlákb3077192019-06-19 10:55:37 +02004000 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 +01004001 return LY_EVALID;
4002 }
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004003 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_BASE, &ident->bases, Y_PREF_IDENTIF_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004004 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004005 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004006 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004007 break;
4008 default:
David Sedlákb3077192019-06-19 10:55:37 +02004009 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004010 return LY_EVALID;
4011 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004012 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004013 return ret;
4014}
4015
Michal Vaskoea5abea2018-09-18 13:10:54 +02004016/**
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004017 * @brief Parse module substatements.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004018 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004019 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004020 * @param[in,out] data Data to read from, always moved to currently handled character.
4021 * @param[in,out] mod Module to write to.
4022 *
4023 * @return LY_ERR values.
4024 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004025LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01004026parse_module(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004027{
4028 LY_ERR ret = 0;
4029 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004030 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004031 enum ly_stmt kw, prev_kw = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004032 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004033 struct lysp_submodule *dup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004034
4035 /* (sub)module name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02004036 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004037 INSERT_WORD(ctx, buf, mod->mod->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004038
Radek Krejci6d6556c2018-11-08 09:37:45 +01004039 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004040
Radek Krejcie3846472018-10-15 15:24:51 +02004041#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004042 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 +02004043
Michal Vasko7fbc8162018-09-17 10:35:16 +02004044 switch (kw) {
4045 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004046 case LY_STMT_NAMESPACE:
4047 case LY_STMT_PREFIX:
Radek Krejcie3846472018-10-15 15:24:51 +02004048 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4049 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004050 case LY_STMT_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004051 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004052 break;
4053 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004054 case LY_STMT_INCLUDE:
4055 case LY_STMT_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004056 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004057 break;
4058 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004059 case LY_STMT_ORGANIZATION:
4060 case LY_STMT_CONTACT:
4061 case LY_STMT_DESCRIPTION:
4062 case LY_STMT_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004063 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004064 break;
4065
4066 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004067 case LY_STMT_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004068 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004069 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004070 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004071 case LY_STMT_ANYDATA:
4072 case LY_STMT_ANYXML:
4073 case LY_STMT_AUGMENT:
4074 case LY_STMT_CHOICE:
4075 case LY_STMT_CONTAINER:
4076 case LY_STMT_DEVIATION:
4077 case LY_STMT_EXTENSION:
4078 case LY_STMT_FEATURE:
4079 case LY_STMT_GROUPING:
4080 case LY_STMT_IDENTITY:
4081 case LY_STMT_LEAF:
4082 case LY_STMT_LEAF_LIST:
4083 case LY_STMT_LIST:
4084 case LY_STMT_NOTIFICATION:
4085 case LY_STMT_RPC:
4086 case LY_STMT_TYPEDEF:
4087 case LY_STMT_USES:
4088 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004089 mod_stmt = Y_MOD_BODY;
4090 break;
4091 default:
4092 /* error handled in the next switch */
4093 break;
4094 }
Radek Krejcie3846472018-10-15 15:24:51 +02004095#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004096
Radek Krejcie3846472018-10-15 15:24:51 +02004097 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004098 switch (kw) {
4099 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004100 case LY_STMT_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004101 LY_CHECK_RET(parse_yangversion(ctx, data, &mod->mod->version, &mod->exts));
4102 ctx->mod_version = mod->mod->version;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004103 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004104 case LY_STMT_NAMESPACE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004105 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_NAMESPACE, 0, &mod->mod->ns, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004106 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004107 case LY_STMT_PREFIX:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004108 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_PREFIX, 0, &mod->mod->prefix, Y_IDENTIF_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004109 break;
4110
4111 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004112 case LY_STMT_INCLUDE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004113 LY_CHECK_RET(parse_include(ctx, mod->mod->name, data, &mod->includes));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004114 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004115 case LY_STMT_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004116 LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, data, &mod->imports));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004117 break;
4118
4119 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004120 case LY_STMT_ORGANIZATION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004121 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_ORGANIZATION, 0, &mod->mod->org, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004122 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004123 case LY_STMT_CONTACT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004124 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_CONTACT, 0, &mod->mod->contact, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004125 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004126 case LY_STMT_DESCRIPTION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004127 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &mod->mod->dsc, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004128 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004129 case LY_STMT_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004130 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &mod->mod->ref, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004131 break;
4132
4133 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004134 case LY_STMT_REVISION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004135 LY_CHECK_RET(parse_revision(ctx, data, &mod->revs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004136 break;
4137
4138 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004139 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004140 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "module");
Radek Krejci10113652018-11-14 16:56:50 +01004141 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004142 case LY_STMT_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004143 LY_CHECK_RET(parse_any(ctx, data, kw, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004144 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004145 case LY_STMT_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004146 LY_CHECK_RET(parse_choice(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004147 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004148 case LY_STMT_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004149 LY_CHECK_RET(parse_container(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004150 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004151 case LY_STMT_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004152 LY_CHECK_RET(parse_leaf(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004153 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004154 case LY_STMT_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004155 LY_CHECK_RET(parse_leaflist(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004156 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004157 case LY_STMT_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004158 LY_CHECK_RET(parse_list(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004159 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004160 case LY_STMT_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004161 LY_CHECK_RET(parse_uses(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004162 break;
4163
Radek Krejcid6b76452019-09-03 17:03:03 +02004164 case LY_STMT_AUGMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004165 LY_CHECK_RET(parse_augment(ctx, data, NULL, &mod->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004166 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004167 case LY_STMT_DEVIATION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004168 LY_CHECK_RET(parse_deviation(ctx, data, &mod->deviations));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004169 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004170 case LY_STMT_EXTENSION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004171 LY_CHECK_RET(parse_extension(ctx, data, &mod->extensions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004172 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004173 case LY_STMT_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004174 LY_CHECK_RET(parse_feature(ctx, data, &mod->features));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004175 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004176 case LY_STMT_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004177 LY_CHECK_RET(parse_grouping(ctx, data, NULL, &mod->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004178 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004179 case LY_STMT_IDENTITY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004180 LY_CHECK_RET(parse_identity(ctx, data, &mod->identities));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004181 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004182 case LY_STMT_NOTIFICATION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004183 LY_CHECK_RET(parse_notif(ctx, data, NULL, &mod->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004184 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004185 case LY_STMT_RPC:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004186 LY_CHECK_RET(parse_action(ctx, data, NULL, &mod->rpcs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004187 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004188 case LY_STMT_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01004189 LY_CHECK_RET(parse_typedef(ctx, NULL, data, &mod->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004190 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004191 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004192 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004193 break;
4194
4195 default:
David Sedlákb3077192019-06-19 10:55:37 +02004196 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004197 return LY_EVALID;
4198 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004199 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004200 LY_CHECK_RET(ret);
Radek Krejcie86bf772018-12-14 11:39:53 +01004201
Radek Krejci6d6556c2018-11-08 09:37:45 +01004202checks:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004203 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01004204 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 +01004205
Michal Vasko7fbc8162018-09-17 10:35:16 +02004206 /* mandatory substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004207 if (!mod->mod->ns) {
David Sedlákb3077192019-06-19 10:55:37 +02004208 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004209 return LY_EVALID;
4210 } else if (!mod->mod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004211 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004212 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004213 }
4214
Radek Krejcie9e987e2018-10-31 12:50:27 +01004215 /* submodules share the namespace with the module names, so there must not be
4216 * a submodule of the same name in the context, no need for revision matching */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004217 dup = ly_ctx_get_submodule(ctx->ctx, NULL, mod->mod->name, NULL);
4218 if (dup) {
David Sedlákb3077192019-06-19 10:55:37 +02004219 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 +01004220 return LY_EVALID;
4221 }
4222
4223 return ret;
4224}
4225
4226/**
4227 * @brief Parse submodule substatements.
4228 *
4229 * @param[in] ctx yang parser context for logging.
4230 * @param[in,out] data Data to read from, always moved to currently handled character.
4231 * @param[out] submod Parsed submodule structure.
4232 *
4233 * @return LY_ERR values.
4234 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004235LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01004236parse_submodule(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004237{
4238 LY_ERR ret = 0;
4239 char *buf, *word;
4240 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004241 enum ly_stmt kw, prev_kw = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004242 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
4243 struct lysp_submodule *dup;
4244
4245 /* submodule name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02004246 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004247 INSERT_WORD(ctx, buf, submod->name, word, word_len);
4248
4249 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
4250
4251#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004252 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 +01004253
4254 switch (kw) {
4255 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004256 case LY_STMT_BELONGS_TO:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004257 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4258 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004259 case LY_STMT_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004260 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4261 break;
4262 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004263 case LY_STMT_INCLUDE:
4264 case LY_STMT_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004265 CHECK_ORDER(Y_MOD_LINKAGE);
4266 break;
4267 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004268 case LY_STMT_ORGANIZATION:
4269 case LY_STMT_CONTACT:
4270 case LY_STMT_DESCRIPTION:
4271 case LY_STMT_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004272 CHECK_ORDER(Y_MOD_META);
4273 break;
4274
4275 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004276 case LY_STMT_REVISION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004277 CHECK_ORDER(Y_MOD_REVISION);
4278 break;
4279 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004280 case LY_STMT_ANYDATA:
4281 case LY_STMT_ANYXML:
4282 case LY_STMT_AUGMENT:
4283 case LY_STMT_CHOICE:
4284 case LY_STMT_CONTAINER:
4285 case LY_STMT_DEVIATION:
4286 case LY_STMT_EXTENSION:
4287 case LY_STMT_FEATURE:
4288 case LY_STMT_GROUPING:
4289 case LY_STMT_IDENTITY:
4290 case LY_STMT_LEAF:
4291 case LY_STMT_LEAF_LIST:
4292 case LY_STMT_LIST:
4293 case LY_STMT_NOTIFICATION:
4294 case LY_STMT_RPC:
4295 case LY_STMT_TYPEDEF:
4296 case LY_STMT_USES:
4297 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004298 mod_stmt = Y_MOD_BODY;
4299 break;
4300 default:
4301 /* error handled in the next switch */
4302 break;
4303 }
4304#undef CHECK_ORDER
4305
4306 prev_kw = kw;
4307 switch (kw) {
4308 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004309 case LY_STMT_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004310 LY_CHECK_RET(parse_yangversion(ctx, data, &submod->version, &submod->exts));
4311 ctx->mod_version = submod->version;
4312 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004313 case LY_STMT_BELONGS_TO:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004314 LY_CHECK_RET(parse_belongsto(ctx, data, &submod->belongsto, &submod->prefix, &submod->exts));
4315 break;
4316
4317 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004318 case LY_STMT_INCLUDE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004319 LY_CHECK_RET(parse_include(ctx, submod->name, data, &submod->includes));
4320 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004321 case LY_STMT_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004322 LY_CHECK_RET(parse_import(ctx, submod->prefix, data, &submod->imports));
4323 break;
4324
4325 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004326 case LY_STMT_ORGANIZATION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004327 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_ORGANIZATION, 0, &submod->org, Y_STR_ARG, &submod->exts));
4328 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004329 case LY_STMT_CONTACT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004330 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_CONTACT, 0, &submod->contact, Y_STR_ARG, &submod->exts));
4331 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004332 case LY_STMT_DESCRIPTION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004333 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &submod->dsc, Y_STR_ARG, &submod->exts));
4334 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004335 case LY_STMT_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004336 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &submod->ref, Y_STR_ARG, &submod->exts));
4337 break;
4338
4339 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004340 case LY_STMT_REVISION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004341 LY_CHECK_RET(parse_revision(ctx, data, &submod->revs));
4342 break;
4343
4344 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004345 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004346 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004347 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004348 case LY_STMT_ANYXML:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004349 LY_CHECK_RET(parse_any(ctx, data, kw, NULL, &submod->data));
4350 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004351 case LY_STMT_CHOICE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004352 LY_CHECK_RET(parse_choice(ctx, data, NULL, &submod->data));
4353 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004354 case LY_STMT_CONTAINER:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004355 LY_CHECK_RET(parse_container(ctx, data, NULL, &submod->data));
4356 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004357 case LY_STMT_LEAF:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004358 LY_CHECK_RET(parse_leaf(ctx, data, NULL, &submod->data));
4359 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004360 case LY_STMT_LEAF_LIST:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004361 LY_CHECK_RET(parse_leaflist(ctx, data, NULL, &submod->data));
4362 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004363 case LY_STMT_LIST:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004364 LY_CHECK_RET(parse_list(ctx, data, NULL, &submod->data));
4365 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004366 case LY_STMT_USES:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004367 LY_CHECK_RET(parse_uses(ctx, data, NULL, &submod->data));
4368 break;
4369
Radek Krejcid6b76452019-09-03 17:03:03 +02004370 case LY_STMT_AUGMENT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004371 LY_CHECK_RET(parse_augment(ctx, data, NULL, &submod->augments));
4372 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004373 case LY_STMT_DEVIATION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004374 LY_CHECK_RET(parse_deviation(ctx, data, &submod->deviations));
4375 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004376 case LY_STMT_EXTENSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004377 LY_CHECK_RET(parse_extension(ctx, data, &submod->extensions));
4378 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004379 case LY_STMT_FEATURE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004380 LY_CHECK_RET(parse_feature(ctx, data, &submod->features));
4381 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004382 case LY_STMT_GROUPING:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004383 LY_CHECK_RET(parse_grouping(ctx, data, NULL, &submod->groupings));
4384 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004385 case LY_STMT_IDENTITY:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004386 LY_CHECK_RET(parse_identity(ctx, data, &submod->identities));
4387 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004388 case LY_STMT_NOTIFICATION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004389 LY_CHECK_RET(parse_notif(ctx, data, NULL, &submod->notifs));
4390 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004391 case LY_STMT_RPC:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004392 LY_CHECK_RET(parse_action(ctx, data, NULL, &submod->rpcs));
4393 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004394 case LY_STMT_TYPEDEF:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004395 LY_CHECK_RET(parse_typedef(ctx, NULL, data, &submod->typedefs));
4396 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004397 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004398 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &submod->exts));
4399 break;
4400
4401 default:
David Sedlákb3077192019-06-19 10:55:37 +02004402 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004403 return LY_EVALID;
4404 }
4405 }
4406 LY_CHECK_RET(ret);
4407
4408checks:
4409 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01004410 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, submod->groupings, submod->augments,
4411 submod->rpcs, submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004412
4413 /* mandatory substatements */
4414 if (!submod->belongsto) {
David Sedlákb3077192019-06-19 10:55:37 +02004415 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004416 return LY_EVALID;
4417 }
4418
4419 /* submodules share the namespace with the module names, so there must not be
4420 * a submodule of the same name in the context, no need for revision matching */
4421 dup = ly_ctx_get_submodule(ctx->ctx, NULL, submod->name, NULL);
4422 if (dup && strcmp(dup->belongsto, submod->belongsto)) {
David Sedlákb3077192019-06-19 10:55:37 +02004423 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Name collision between submodules of name \"%s\".", dup->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +01004424 return LY_EVALID;
4425 }
4426
Michal Vasko7fbc8162018-09-17 10:35:16 +02004427 return ret;
4428}
4429
Radek Krejcid4557c62018-09-17 11:42:09 +02004430LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01004431yang_parse_submodule(struct lys_yang_parser_ctx **context, struct ly_ctx *ly_ctx, struct lys_parser_ctx *main_ctx,
4432 const char *data, struct lysp_submodule **submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004433{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004434 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004435 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004436 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004437 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004438 struct lysp_submodule *mod_p = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004439
David Sedlák1b623122019-08-05 15:27:49 +02004440 /* create context */
4441 *context = calloc(1, sizeof **context);
4442 LY_CHECK_ERR_RET(!(*context), LOGMEM(ly_ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004443 (*context)->format = LYS_IN_YANG;
David Sedlák1b623122019-08-05 15:27:49 +02004444 (*context)->ctx = ly_ctx;
Radek Krejci99435242019-09-05 16:19:15 +02004445 (*context)->pos_type = LY_VLOG_LINE;
David Sedlák1b623122019-08-05 15:27:49 +02004446 (*context)->line = 1;
4447
4448 /* map the typedefs and groupings list from main context to the submodule's context */
4449 memcpy(&(*context)->tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
4450 memcpy(&(*context)->grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes);
4451
Michal Vasko7fbc8162018-09-17 10:35:16 +02004452 /* "module"/"submodule" */
David Sedlák1b623122019-08-05 15:27:49 +02004453 ret = get_keyword(*context, &data, &kw, &word, &word_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004454 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004455
Radek Krejcid6b76452019-09-03 17:03:03 +02004456 if (kw == LY_STMT_MODULE) {
David Sedlák1b623122019-08-05 15:27:49 +02004457 LOGERR((*context)->ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004458 ret = LY_EINVAL;
4459 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004460 } else if (kw != LY_STMT_SUBMODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004461 LOGVAL_PARSER(*context, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004462 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004463 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004464 }
4465
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004466 mod_p = calloc(1, sizeof *mod_p);
David Sedlák1b623122019-08-05 15:27:49 +02004467 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM((*context)->ctx), cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004468 mod_p->parsing = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004469
4470 /* substatements */
David Sedlák1b623122019-08-05 15:27:49 +02004471 ret = parse_submodule(*context, &data, mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004472 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004473
4474 /* read some trailing spaces or new lines */
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004475 while(*data && isspace(*data)) {
4476 data++;
4477 }
4478 if (*data) {
David Sedlák1538a842019-08-08 15:38:51 +02004479 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_SUBMOD, 15, data, strlen(data) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004480 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004481 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004482 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004483
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004484 mod_p->parsing = 0;
4485 *submod = mod_p;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004486
Radek Krejcibbe09a92018-11-08 09:36:54 +01004487cleanup:
4488 if (ret) {
David Sedlák1b623122019-08-05 15:27:49 +02004489 lysp_submodule_free((*context)->ctx, mod_p);
Michal Vaskob36053d2020-03-26 15:49:30 +01004490 yang_parser_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004491 *context = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004492 }
4493
4494 return ret;
4495}
4496
4497LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01004498yang_parse_module(struct lys_yang_parser_ctx **context, const char *data, struct lys_module *mod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004499{
4500 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004501 char *word;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004502 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004503 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004504 struct lysp_module *mod_p = NULL;
4505
David Sedlák1b623122019-08-05 15:27:49 +02004506 /* create context */
4507 *context = calloc(1, sizeof **context);
4508 LY_CHECK_ERR_RET(!(*context), LOGMEM(mod->ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004509 (*context)->format = LYS_IN_YANG;
David Sedlák1b623122019-08-05 15:27:49 +02004510 (*context)->ctx = mod->ctx;
Radek Krejci335332a2019-09-05 13:03:35 +02004511 (*context)->pos_type = LY_VLOG_LINE;
David Sedlák1b623122019-08-05 15:27:49 +02004512 (*context)->line = 1;
4513
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004514 /* "module"/"submodule" */
David Sedlák1b623122019-08-05 15:27:49 +02004515 ret = get_keyword(*context, &data, &kw, &word, &word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004516 LY_CHECK_GOTO(ret, cleanup);
4517
Radek Krejcid6b76452019-09-03 17:03:03 +02004518 if (kw == LY_STMT_SUBMODULE) {
David Sedlák1b623122019-08-05 15:27:49 +02004519 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 +01004520 ret = LY_EINVAL;
4521 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004522 } else if (kw != LY_STMT_MODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004523 LOGVAL_PARSER((*context), LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004524 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004525 goto cleanup;
4526 }
4527
4528 mod_p = calloc(1, sizeof *mod_p);
David Sedlák1b623122019-08-05 15:27:49 +02004529 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM((*context)->ctx), cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004530 mod_p->mod = mod;
4531 mod_p->parsing = 1;
4532
4533 /* substatements */
David Sedlák1b623122019-08-05 15:27:49 +02004534 ret = parse_module(*context, &data, mod_p);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004535 LY_CHECK_GOTO(ret, cleanup);
4536
4537 /* read some trailing spaces or new lines */
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004538 while(*data && isspace(*data)) {
4539 data++;
4540 }
4541 if (*data) {
David Sedlák1538a842019-08-08 15:38:51 +02004542 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_MOD, 15, data, strlen(data) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004543 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004544 goto cleanup;
4545 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004546
4547 mod_p->parsing = 0;
4548 mod->parsed = mod_p;
4549
4550cleanup:
4551 if (ret) {
4552 lysp_module_free(mod_p);
Michal Vaskob36053d2020-03-26 15:49:30 +01004553 yang_parser_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004554 *context = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004555 }
4556
Michal Vasko7fbc8162018-09-17 10:35:16 +02004557 return ret;
4558}