blob: 05977f1025859df86c67d00eaf9e7c66e6b39dfc [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
15#include "common.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020016
17#include <assert.h>
18#include <ctype.h>
19#include <errno.h>
20#include <stdint.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24
Michal Vasko7fbc8162018-09-17 10:35:16 +020025#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020026#include "dict.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020027#include "log.h"
Radek Krejci0935f412019-08-20 16:15:18 +020028#include "plugins_exts.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
Radek Krejci2d7a47b2019-05-16 13:34:10 +020080LY_ERR parse_container(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
81LY_ERR parse_uses(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
82LY_ERR parse_choice(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
83LY_ERR parse_case(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
84LY_ERR parse_list(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
85LY_ERR parse_grouping(struct lys_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
David Sedlák40bb13b2019-07-10 14:34:18 +0200142buf_store_char(struct lys_parser_ctx *ctx, const char **input, enum yang_arg arg, char **word_p,
143 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:
David Sedlák4a650532019-07-10 11:55:18 +0200165 LY_CHECK_RET(lysp_check_identifierchar(ctx, c, !(*word_len), NULL));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200166 break;
167 case Y_PREF_IDENTIF_ARG:
David Sedlák40bb13b2019-07-10 14:34:18 +0200168 LY_CHECK_RET(lysp_check_identifierchar(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:
David Sedlák4a650532019-07-10 11:55:18 +0200172 LY_CHECK_RET(lysp_check_stringchar(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
Radek Krejcie7b95092019-05-15 11:03:07 +0200225skip_comment(struct lys_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
Radek Krejcie7b95092019-05-15 11:03:07 +0200294read_qstring(struct lys_parser_ctx *ctx, const char **data, enum yang_arg arg, char **word_p, char **word_b, size_t *word_len,
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200295 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
Radek Krejcie7b95092019-05-15 11:03:07 +0200513get_argument(struct lys_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
Radek Krejcid6b76452019-09-03 17:03:03 +0200632get_keyword(struct lys_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 */
David Sedlák4a650532019-07-10 11:55:18 +0200727 LY_CHECK_RET(lysp_check_identifierchar(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
Radek Krejci335332a2019-09-05 13:03:35 +0200764parse_ext_substmt(struct lys_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
Radek Krejcie7b95092019-05-15 11:03:07 +0200817parse_ext(struct lys_parser_ctx *ctx, const char **data, const char *ext_name, int ext_name_len, LYEXT_SUBSTMT insubstmt,
Michal Vasko7fbc8162018-09-17 10:35:16 +0200818 uint32_t insubstmt_index, struct lysp_ext_instance **exts)
819{
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
Radek Krejcie7b95092019-05-15 11:03:07 +0200861parse_text_field(struct lys_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
Radek Krejcie7b95092019-05-15 11:03:07 +0200904parse_yangversion(struct lys_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
Radek Krejcie7b95092019-05-15 11:03:07 +0200955parse_belongsto(struct lys_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
Radek Krejcie7b95092019-05-15 11:03:07 +02001006parse_revisiondate(struct lys_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 */
Radek Krejcibbe09a92018-11-08 09:36:54 +01001022 if (lysp_check_date(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
Radek Krejcie7b95092019-05-15 11:03:07 +02001055parse_include(struct lys_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
Radek Krejcie7b95092019-05-15 11:03:07 +02001112parse_import(struct lys_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));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001130 LY_CHECK_RET(lysp_check_prefix(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
Radek Krejcie7b95092019-05-15 11:03:07 +02001169parse_revision(struct lys_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 */
Radek Krejcibbe09a92018-11-08 09:36:54 +01001183 if (lysp_check_date(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
Radek Krejcie7b95092019-05-15 11:03:07 +02001223parse_text_fields(struct lys_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
Radek Krejcie7b95092019-05-15 11:03:07 +02001263parse_config(struct lys_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
Radek Krejcie7b95092019-05-15 11:03:07 +02001313parse_mandatory(struct lys_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
Radek Krejcid6b76452019-09-03 17:03:03 +02001363parse_restr(struct lys_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
David Sedlákb9b892c2019-07-12 14:44:02 +02001373 YANG_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
Radek Krejcid6b76452019-09-03 17:03:03 +02001411parse_restrs(struct lys_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
Radek Krejcie7b95092019-05-15 11:03:07 +02001430parse_status(struct lys_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
Radek Krejcie7b95092019-05-15 11:03:07 +02001481parse_when(struct lys_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);
David Sedlákb9b892c2019-07-12 14:44:02 +02001499 YANG_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
Radek Krejcid6b76452019-09-03 17:03:03 +02001534parse_any(struct lys_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
Radek Krejcid6b76452019-09-03 17:03:03 +02001603parse_type_enum_value_pos(struct lys_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
Radek Krejcid6b76452019-09-03 17:03:03 +02001685parse_type_enum(struct lys_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) {
David Sedlák07869a52019-07-12 14:28:19 +02001698 ret = lysp_check_enum_name(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
Radek Krejcie7b95092019-05-15 11:03:07 +02001752parse_type_fracdigits(struct lys_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
Radek Krejcie7b95092019-05-15 11:03:07 +02001815parse_type_reqinstance(struct lys_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
Radek Krejcie7b95092019-05-15 11:03:07 +02001865parse_type_pattern_modifier(struct lys_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
Radek Krejcie7b95092019-05-15 11:03:07 +02001920parse_type_pattern(struct lys_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
Radek Krejcie7b95092019-05-15 11:03:07 +02001985parse_type(struct lys_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;
Radek Krejciefd22f62018-09-27 11:47:58 +02001989 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001990 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001991 struct lysp_type *nest_type;
1992
1993 if (type->name) {
David Sedlákb3077192019-06-19 10:55:37 +02001994 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001995 return LY_EVALID;
1996 }
1997
1998 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001999 LY_CHECK_RET(get_argument(ctx, data, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002000 INSERT_WORD(ctx, buf, type->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002001
Radek Krejci6d6556c2018-11-08 09:37:45 +01002002 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002003 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002004 case LY_STMT_BASE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002005 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 +01002006 type->flags |= LYS_SET_BASE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002007 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002008 case LY_STMT_BIT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002009 LY_CHECK_RET(parse_type_enum(ctx, data, kw, &type->bits));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002010 type->flags |= LYS_SET_BIT;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002011 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002012 case LY_STMT_ENUM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002013 LY_CHECK_RET(parse_type_enum(ctx, data, kw, &type->enums));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002014 type->flags |= LYS_SET_ENUM;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002015 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002016 case LY_STMT_FRACTION_DIGITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002017 LY_CHECK_RET(parse_type_fracdigits(ctx, data, &type->fraction_digits, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002018 type->flags |= LYS_SET_FRDIGITS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002019 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002020 case LY_STMT_LENGTH:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002021 if (type->length) {
David Sedlákb3077192019-06-19 10:55:37 +02002022 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002023 return LY_EVALID;
2024 }
2025 type->length = calloc(1, sizeof *type->length);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002026 LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002027
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002028 LY_CHECK_RET(parse_restr(ctx, data, kw, type->length));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002029 type->flags |= LYS_SET_LENGTH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002030 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002031 case LY_STMT_PATH:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002032 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_PATH, 0, &type->path, Y_STR_ARG, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002033 type->flags |= LYS_SET_PATH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002034 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002035 case LY_STMT_PATTERN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002036 LY_CHECK_RET(parse_type_pattern(ctx, data, &type->patterns));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002037 type->flags |= LYS_SET_PATTERN;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002038 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002039 case LY_STMT_RANGE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002040 if (type->range) {
David Sedlákb3077192019-06-19 10:55:37 +02002041 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002042 return LY_EVALID;
2043 }
2044 type->range = calloc(1, sizeof *type->range);
David Sedlák7a8b2472019-07-11 15:08:34 +02002045 LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002046
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002047 LY_CHECK_RET(parse_restr(ctx, data, kw, type->range));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002048 type->flags |= LYS_SET_RANGE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002049 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002050 case LY_STMT_REQUIRE_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002051 LY_CHECK_RET(parse_type_reqinstance(ctx, data, &type->require_instance, &type->flags, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002052 /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002053 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002054 case LY_STMT_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002055 LY_ARRAY_NEW_RET(ctx->ctx, type->types, nest_type, LY_EMEM);
2056 LY_CHECK_RET(parse_type(ctx, data, nest_type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002057 type->flags |= LYS_SET_TYPE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002058 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002059 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002060 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &type->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002061 break;
2062 default:
David Sedlákb3077192019-06-19 10:55:37 +02002063 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002064 return LY_EVALID;
2065 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002066 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002067 return ret;
2068}
2069
Michal Vaskoea5abea2018-09-18 13:10:54 +02002070/**
2071 * @brief Parse the leaf statement.
2072 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002073 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002074 * @param[in,out] data Data to read from, always moved to currently handled character.
2075 * @param[in,out] siblings Siblings to add to.
2076 *
2077 * @return LY_ERR values.
2078 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002079LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002080parse_leaf(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002081{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002082 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002083 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002084 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002085 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002086 struct lysp_node_leaf *leaf;
2087
David Sedlák60adc092019-08-06 15:57:02 +02002088 /* create new leaf structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02002089 LY_LIST_NEW_RET(ctx->ctx, siblings, leaf, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002090 leaf->nodetype = LYS_LEAF;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002091 leaf->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002092
Michal Vasko7fbc8162018-09-17 10:35:16 +02002093 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002094 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002095 INSERT_WORD(ctx, buf, leaf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002096
2097 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01002098 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002099 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002100 case LY_STMT_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002101 LY_CHECK_RET(parse_config(ctx, data, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002102 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002103 case LY_STMT_DEFAULT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002104 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 +02002105 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002106 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002107 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 +02002108 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002109 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002110 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 +02002111 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002112 case LY_STMT_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002113 LY_CHECK_RET(parse_mandatory(ctx, data, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002114 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002115 case LY_STMT_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002116 LY_CHECK_RET(parse_restrs(ctx, data, kw, &leaf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002117 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002118 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002119 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 +02002120 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002121 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002122 LY_CHECK_RET(parse_status(ctx, data, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002123 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002124 case LY_STMT_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002125 LY_CHECK_RET(parse_type(ctx, data, &leaf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002126 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002127 case LY_STMT_UNITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002128 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 +02002129 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002130 case LY_STMT_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002131 LY_CHECK_RET(parse_when(ctx, data, &leaf->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002132 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002133 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002134 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002135 break;
2136 default:
David Sedlákb3077192019-06-19 10:55:37 +02002137 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002138 return LY_EVALID;
2139 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002140 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002141 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002142checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002143 /* mandatory substatements */
2144 if (!leaf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002145 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002146 return LY_EVALID;
2147 }
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002148 if ((leaf->flags & LYS_MAND_TRUE) && (leaf->dflt)) {
David Sedlákb3077192019-06-19 10:55:37 +02002149 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMSCOMB, "mandatory", "default", "leaf");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002150 return LY_EVALID;
2151 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002152
2153 return ret;
2154}
2155
Michal Vaskoea5abea2018-09-18 13:10:54 +02002156/**
2157 * @brief Parse the max-elements statement.
2158 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002159 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002160 * @param[in,out] data Data to read from, always moved to currently handled character.
2161 * @param[in,out] max Value to write to.
2162 * @param[in,out] flags Flags to write to.
2163 * @param[in,out] exts Extension instances to add to.
2164 *
2165 * @return LY_ERR values.
2166 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002167LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002168parse_maxelements(struct lys_parser_ctx *ctx, const char **data, uint32_t *max, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002169{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002170 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002171 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002172 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002173 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002174 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002175
2176 if (*flags & LYS_SET_MAX) {
David Sedlákb3077192019-06-19 10:55:37 +02002177 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002178 return LY_EVALID;
2179 }
2180 *flags |= LYS_SET_MAX;
2181
2182 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002183 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002184
2185 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
David Sedlákb3077192019-06-19 10:55:37 +02002186 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002187 free(buf);
2188 return LY_EVALID;
2189 }
2190
Radek Krejci7f9b6512019-09-18 13:11:09 +02002191 if (ly_strncmp("unbounded", word, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002192 errno = 0;
2193 num = strtoul(word, &ptr, 10);
2194 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002195 if ((size_t)(ptr - word) != word_len) {
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 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002201 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002202 free(buf);
2203 return LY_EVALID;
2204 }
2205
2206 *max = num;
2207 }
2208 free(buf);
2209
Radek Krejci6d6556c2018-11-08 09:37:45 +01002210 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002211 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002212 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002213 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MAX, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002214 break;
2215 default:
David Sedlákb3077192019-06-19 10:55:37 +02002216 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002217 return LY_EVALID;
2218 }
2219 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002220 return ret;
2221}
2222
Michal Vaskoea5abea2018-09-18 13:10:54 +02002223/**
2224 * @brief Parse the min-elements statement.
2225 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002226 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002227 * @param[in,out] data Data to read from, always moved to currently handled character.
2228 * @param[in,out] min Value to write to.
2229 * @param[in,out] flags Flags to write to.
2230 * @param[in,out] exts Extension instances to add to.
2231 *
2232 * @return LY_ERR values.
2233 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002234LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002235parse_minelements(struct lys_parser_ctx *ctx, const char **data, uint32_t *min, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002236{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002237 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002238 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002239 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002240 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002241 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002242
2243 if (*flags & LYS_SET_MIN) {
David Sedlákb3077192019-06-19 10:55:37 +02002244 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002245 return LY_EVALID;
2246 }
2247 *flags |= LYS_SET_MIN;
2248
2249 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002250 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002251
2252 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
David Sedlákb3077192019-06-19 10:55:37 +02002253 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002254 free(buf);
2255 return LY_EVALID;
2256 }
2257
2258 errno = 0;
2259 num = strtoul(word, &ptr, 10);
2260 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002261 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002262 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002263 free(buf);
2264 return LY_EVALID;
2265 }
2266 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002267 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002268 free(buf);
2269 return LY_EVALID;
2270 }
2271 *min = num;
2272 free(buf);
2273
Radek Krejci6d6556c2018-11-08 09:37:45 +01002274 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002275 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002276 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002277 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MIN, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002278 break;
2279 default:
David Sedlákb3077192019-06-19 10:55:37 +02002280 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002281 return LY_EVALID;
2282 }
2283 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002284 return ret;
2285}
2286
Michal Vaskoea5abea2018-09-18 13:10:54 +02002287/**
2288 * @brief Parse the ordered-by statement.
2289 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002290 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002291 * @param[in,out] data Data to read from, always moved to currently handled character.
2292 * @param[in,out] flags Flags to write to.
2293 * @param[in,out] exts Extension instances to add to.
2294 *
2295 * @return LY_ERR values.
2296 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002297static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002298parse_orderedby(struct lys_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002299{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002300 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002301 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002302 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002303 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002304
2305 if (*flags & LYS_ORDBY_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02002306 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002307 return LY_EVALID;
2308 }
2309
2310 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002311 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002312
2313 if ((word_len == 6) && !strncmp(word, "system", word_len)) {
2314 *flags |= LYS_ORDBY_SYSTEM;
2315 } else if ((word_len == 4) && !strncmp(word, "user", word_len)) {
2316 *flags |= LYS_ORDBY_USER;
2317 } else {
David Sedlákb3077192019-06-19 10:55:37 +02002318 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002319 free(buf);
2320 return LY_EVALID;
2321 }
2322 free(buf);
2323
Radek Krejci6d6556c2018-11-08 09:37:45 +01002324 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002325 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002326 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002327 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_ORDEREDBY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002328 break;
2329 default:
David Sedlákb3077192019-06-19 10:55:37 +02002330 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002331 return LY_EVALID;
2332 }
2333 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002334 return ret;
2335}
2336
Michal Vaskoea5abea2018-09-18 13:10:54 +02002337/**
2338 * @brief Parse the leaf-list statement.
2339 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002340 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002341 * @param[in,out] data Data to read from, always moved to currently handled character.
2342 * @param[in,out] siblings Siblings to add to.
2343 *
2344 * @return LY_ERR values.
2345 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002346LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002347parse_leaflist(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002348{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002349 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002350 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002351 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002352 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002353 struct lysp_node_leaflist *llist;
2354
David Sedlák60adc092019-08-06 15:57:02 +02002355 /* create new leaf-list structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02002356 LY_LIST_NEW_RET(ctx->ctx, siblings, llist, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002357 llist->nodetype = LYS_LEAFLIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002358 llist->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002359
Michal Vasko7fbc8162018-09-17 10:35:16 +02002360 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002361 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002362 INSERT_WORD(ctx, buf, llist->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002363
2364 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01002365 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002366 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002367 case LY_STMT_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002368 LY_CHECK_RET(parse_config(ctx, data, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002369 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002370 case LY_STMT_DEFAULT:
Radek Krejci335332a2019-09-05 13:03:35 +02002371 PARSER_CHECK_STMTVER2_RET(ctx, "default", "leaf-list");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002372 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 +02002373 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002374 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002375 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 +02002376 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002377 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002378 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 +02002379 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002380 case LY_STMT_MAX_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002381 LY_CHECK_RET(parse_maxelements(ctx, data, &llist->max, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002382 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002383 case LY_STMT_MIN_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002384 LY_CHECK_RET(parse_minelements(ctx, data, &llist->min, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002385 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002386 case LY_STMT_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002387 LY_CHECK_RET(parse_restrs(ctx, data, kw, &llist->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002388 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002389 case LY_STMT_ORDERED_BY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002390 LY_CHECK_RET(parse_orderedby(ctx, data, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002391 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002392 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002393 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 +02002394 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002395 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002396 LY_CHECK_RET(parse_status(ctx, data, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002397 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002398 case LY_STMT_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002399 LY_CHECK_RET(parse_type(ctx, data, &llist->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002400 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002401 case LY_STMT_UNITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002402 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 +02002403 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002404 case LY_STMT_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002405 LY_CHECK_RET(parse_when(ctx, data, &llist->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002406 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002407 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002408 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002409 break;
2410 default:
David Sedlákb3077192019-06-19 10:55:37 +02002411 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002412 return LY_EVALID;
2413 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002414 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002415 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002416checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002417 /* mandatory substatements */
2418 if (!llist->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002419 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002420 return LY_EVALID;
2421 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01002422 if ((llist->min) && (llist->dflts)) {
David Sedlákb3077192019-06-19 10:55:37 +02002423 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMSCOMB, "min-elements", "default", "leaf-list");
Radek Krejci0e5d8382018-11-28 16:37:53 +01002424 return LY_EVALID;
2425 }
Radek Krejcidf6cad12018-11-28 17:10:55 +01002426 if (llist->max && llist->min > llist->max) {
David Sedlákb3077192019-06-19 10:55:37 +02002427 LOGVAL_PARSER(ctx, LYVE_SEMANTICS,
Radek Krejcidf6cad12018-11-28 17:10:55 +01002428 "Invalid combination of min-elements and max-elements: min value %u is bigger than the max value %u.",
2429 llist->min, llist->max);
2430 return LY_EVALID;
2431 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002432
2433 return ret;
2434}
2435
Michal Vaskoea5abea2018-09-18 13:10:54 +02002436/**
2437 * @brief Parse the refine statement.
2438 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002439 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002440 * @param[in,out] data Data to read from, always moved to currently handled character.
2441 * @param[in,out] refines Refines to add to.
2442 *
2443 * @return LY_ERR values.
2444 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002445static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002446parse_refine(struct lys_parser_ctx *ctx, const char **data, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002447{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002448 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002449 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002450 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002451 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002452 struct lysp_refine *rf;
2453
Radek Krejci2c4e7172018-10-19 15:56:26 +02002454 LY_ARRAY_NEW_RET(ctx->ctx, *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002455
2456 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002457 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
David Sedlákb9b892c2019-07-12 14:44:02 +02002458 YANG_CHECK_NONEMPTY(ctx, word_len, "refine");
Radek Krejci44ceedc2018-10-02 15:54:31 +02002459 INSERT_WORD(ctx, buf, rf->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002460
Radek Krejci6d6556c2018-11-08 09:37:45 +01002461 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002462 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002463 case LY_STMT_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002464 LY_CHECK_RET(parse_config(ctx, data, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002465 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002466 case LY_STMT_DEFAULT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002467 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 +02002468 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002469 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002470 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 +02002471 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002472 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02002473 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "refine");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002474 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 +02002475 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002476 case LY_STMT_MAX_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002477 LY_CHECK_RET(parse_maxelements(ctx, data, &rf->max, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002478 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002479 case LY_STMT_MIN_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002480 LY_CHECK_RET(parse_minelements(ctx, data, &rf->min, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002481 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002482 case LY_STMT_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002483 LY_CHECK_RET(parse_restrs(ctx, data, kw, &rf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002484 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002485 case LY_STMT_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002486 LY_CHECK_RET(parse_mandatory(ctx, data, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002487 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002488 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002489 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 +02002490 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002491 case LY_STMT_PRESENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002492 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 +02002493 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002494 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002495 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002496 break;
2497 default:
David Sedlákb3077192019-06-19 10:55:37 +02002498 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002499 return LY_EVALID;
2500 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002501 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002502 return ret;
2503}
2504
Michal Vaskoea5abea2018-09-18 13:10:54 +02002505/**
2506 * @brief Parse the typedef statement.
2507 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002508 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002509 * @param[in,out] data Data to read from, always moved to currently handled character.
2510 * @param[in,out] typedefs Typedefs to add to.
2511 *
2512 * @return LY_ERR values.
2513 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002514static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002515parse_typedef(struct lys_parser_ctx *ctx, struct lysp_node *parent, const char **data, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002516{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002517 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002518 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002519 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002520 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002521 struct lysp_tpdf *tpdf;
2522
Radek Krejci2c4e7172018-10-19 15:56:26 +02002523 LY_ARRAY_NEW_RET(ctx->ctx, *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002524
2525 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002526 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002527 INSERT_WORD(ctx, buf, tpdf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002528
2529 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01002530 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002531 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002532 case LY_STMT_DEFAULT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002533 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 +02002534 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002535 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002536 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 +02002537 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002538 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002539 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 +02002540 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002541 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002542 LY_CHECK_RET(parse_status(ctx, data, &tpdf->flags, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002543 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002544 case LY_STMT_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002545 LY_CHECK_RET(parse_type(ctx, data, &tpdf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002546 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002547 case LY_STMT_UNITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002548 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 +02002549 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002550 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002551 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002552 break;
2553 default:
David Sedlákb3077192019-06-19 10:55:37 +02002554 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002555 return LY_EVALID;
2556 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002557 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002558 LY_CHECK_RET(ret);
Radek Krejcibbe09a92018-11-08 09:36:54 +01002559checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002560 /* mandatory substatements */
2561 if (!tpdf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002562 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002563 return LY_EVALID;
2564 }
2565
Radek Krejcibbe09a92018-11-08 09:36:54 +01002566 /* store data for collision check */
Radek Krejci7fc68292019-06-12 13:51:09 +02002567 if (parent && !(parent->nodetype & (LYS_GROUPING | LYS_ACTION | LYS_INOUT | LYS_NOTIF))) {
David Sedláke8b74df2019-08-14 14:18:22 +02002568 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, parent, 0) == -1, LY_EMEM);
Radek Krejcibbe09a92018-11-08 09:36:54 +01002569 }
2570
Michal Vasko7fbc8162018-09-17 10:35:16 +02002571 return ret;
2572}
2573
Michal Vaskoea5abea2018-09-18 13:10:54 +02002574/**
2575 * @brief Parse the input or output statement.
2576 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002577 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002578 * @param[in,out] data Data to read from, always moved to currently handled character.
2579 * @param[in] kw Type of this particular keyword
2580 * @param[in,out] inout_p Input/output pointer to write to.
2581 *
2582 * @return LY_ERR values.
2583 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002584static LY_ERR
Radek Krejcid6b76452019-09-03 17:03:03 +02002585parse_inout(struct lys_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 +02002586{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002587 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002588 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002589 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002590 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002591
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002592 if (inout_p->nodetype) {
David Sedlákb3077192019-06-19 10:55:37 +02002593 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002594 return LY_EVALID;
2595 }
2596
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002597 /* initiate structure */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002598 inout_p->nodetype = &((struct lysp_action*)parent)->input == inout_p ? LYS_INPUT : LYS_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002599 inout_p->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002600
2601 /* parse substatements */
Radek Krejci7fc68292019-06-12 13:51:09 +02002602 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002603 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002604 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002605 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", ly_stmt2str(inout_kw));
Radek Krejci10113652018-11-14 16:56:50 +01002606 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002607 case LY_STMT_ANYXML:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002608 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002609 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002610 case LY_STMT_CHOICE:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002611 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002612 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002613 case LY_STMT_CONTAINER:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002614 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002615 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002616 case LY_STMT_LEAF:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002617 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002618 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002619 case LY_STMT_LEAF_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002620 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002621 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002622 case LY_STMT_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002623 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002624 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002625 case LY_STMT_USES:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002626 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002627 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002628 case LY_STMT_TYPEDEF:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002629 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)inout_p, data, &inout_p->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002630 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002631 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002632 PARSER_CHECK_STMTVER2_RET(ctx, "must", ly_stmt2str(inout_kw));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002633 LY_CHECK_RET(parse_restrs(ctx, data, kw, &inout_p->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002634 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002635 case LY_STMT_GROUPING:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002636 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)inout_p, &inout_p->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002637 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002638 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002639 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inout_p->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002640 break;
2641 default:
David Sedlákb3077192019-06-19 10:55:37 +02002642 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002643 return LY_EVALID;
2644 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002645 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002646 LY_CHECK_RET(ret);
2647checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002648 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02002649 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, inout_p->groupings, NULL, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02002650
Michal Vasko7fbc8162018-09-17 10:35:16 +02002651 return ret;
2652}
2653
Michal Vaskoea5abea2018-09-18 13:10:54 +02002654/**
2655 * @brief Parse the action statement.
2656 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002657 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002658 * @param[in,out] data Data to read from, always moved to currently handled character.
2659 * @param[in,out] actions Actions to add to.
2660 *
2661 * @return LY_ERR values.
2662 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002663LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002664parse_action(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002665{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002666 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002667 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002668 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002669 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002670 struct lysp_action *act;
2671
Radek Krejci2c4e7172018-10-19 15:56:26 +02002672 LY_ARRAY_NEW_RET(ctx->ctx, *actions, act, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002673
2674 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002675 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002676 INSERT_WORD(ctx, buf, act->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002677 act->nodetype = LYS_ACTION;
2678 act->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002679
Radek Krejci7fc68292019-06-12 13:51:09 +02002680 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002681 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002682 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002683 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 +02002684 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002685 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002686 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 +02002687 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002688 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002689 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 +02002690 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002691 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002692 LY_CHECK_RET(parse_status(ctx, data, &act->flags, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002693 break;
2694
Radek Krejcid6b76452019-09-03 17:03:03 +02002695 case LY_STMT_INPUT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002696 LY_CHECK_RET(parse_inout(ctx, data, kw, (struct lysp_node*)act, &act->input));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002697 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002698 case LY_STMT_OUTPUT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002699 LY_CHECK_RET(parse_inout(ctx, data, kw, (struct lysp_node*)act, &act->output));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002700 break;
2701
Radek Krejcid6b76452019-09-03 17:03:03 +02002702 case LY_STMT_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01002703 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)act, data, &act->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002704 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002705 case LY_STMT_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002706 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)act, &act->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002707 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002708 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002709 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002710 break;
2711 default:
David Sedlákb3077192019-06-19 10:55:37 +02002712 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), parent ? "action" : "rpc");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002713 return LY_EVALID;
2714 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002715 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002716 LY_CHECK_RET(ret);
2717checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002718 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02002719 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, act->groupings, NULL, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02002720
Michal Vasko7fbc8162018-09-17 10:35:16 +02002721 return ret;
2722}
2723
Michal Vaskoea5abea2018-09-18 13:10:54 +02002724/**
2725 * @brief Parse the notification statement.
2726 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002727 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002728 * @param[in,out] data Data to read from, always moved to currently handled character.
2729 * @param[in,out] notifs Notifications to add to.
2730 *
2731 * @return LY_ERR values.
2732 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002733LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002734parse_notif(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002735{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002736 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002737 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002738 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002739 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002740 struct lysp_notif *notif;
2741
Radek Krejci2c4e7172018-10-19 15:56:26 +02002742 LY_ARRAY_NEW_RET(ctx->ctx, *notifs, notif, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002743
2744 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002745 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002746 INSERT_WORD(ctx, buf, notif->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002747 notif->nodetype = LYS_NOTIF;
2748 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002749
Radek Krejci7fc68292019-06-12 13:51:09 +02002750 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002751 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002752 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002753 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 +02002754 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002755 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002756 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 +02002757 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002758 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002759 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 +02002760 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002761 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002762 LY_CHECK_RET(parse_status(ctx, data, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002763 break;
2764
Radek Krejcid6b76452019-09-03 17:03:03 +02002765 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002766 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "notification");
Radek Krejci10113652018-11-14 16:56:50 +01002767 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002768 case LY_STMT_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002769 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002770 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002771 case LY_STMT_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002772 LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002773 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002774 case LY_STMT_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002775 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002776 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002777 case LY_STMT_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002778 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002779 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002780 case LY_STMT_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002781 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002782 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002783 case LY_STMT_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002784 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002785 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002786 case LY_STMT_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002787 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002788 break;
2789
Radek Krejcid6b76452019-09-03 17:03:03 +02002790 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002791 PARSER_CHECK_STMTVER2_RET(ctx, "must", "notification");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002792 LY_CHECK_RET(parse_restrs(ctx, data, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002793 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002794 case LY_STMT_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01002795 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)notif, data, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002796 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002797 case LY_STMT_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002798 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002799 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002800 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002801 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002802 break;
2803 default:
David Sedlákb3077192019-06-19 10:55:37 +02002804 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002805 return LY_EVALID;
2806 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002807 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002808 LY_CHECK_RET(ret);
2809checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002810 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02002811 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, notif->groupings, NULL, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02002812
Michal Vasko7fbc8162018-09-17 10:35:16 +02002813 return ret;
2814}
2815
Michal Vaskoea5abea2018-09-18 13:10:54 +02002816/**
2817 * @brief Parse the grouping statement.
2818 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002819 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002820 * @param[in,out] data Data to read from, always moved to currently handled character.
2821 * @param[in,out] groupings Groupings to add to.
2822 *
2823 * @return LY_ERR values.
2824 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002825LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002826parse_grouping(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002827{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002828 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002829 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002830 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002831 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002832 struct lysp_grp *grp;
2833
Radek Krejci2c4e7172018-10-19 15:56:26 +02002834 LY_ARRAY_NEW_RET(ctx->ctx, *groupings, grp, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002835
2836 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002837 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002838 INSERT_WORD(ctx, buf, grp->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002839 grp->nodetype = LYS_GROUPING;
2840 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002841
Radek Krejci7fc68292019-06-12 13:51:09 +02002842 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002843 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002844 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002845 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 +02002846 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002847 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002848 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 +02002849 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002850 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002851 LY_CHECK_RET(parse_status(ctx, data, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002852 break;
2853
Radek Krejcid6b76452019-09-03 17:03:03 +02002854 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002855 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "grouping");
Radek Krejci10113652018-11-14 16:56:50 +01002856 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002857 case LY_STMT_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002858 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002859 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002860 case LY_STMT_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002861 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002862 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002863 case LY_STMT_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002864 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002865 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002866 case LY_STMT_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002867 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002868 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002869 case LY_STMT_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002870 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002871 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002872 case LY_STMT_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002873 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002874 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002875 case LY_STMT_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002876 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002877 break;
2878
Radek Krejcid6b76452019-09-03 17:03:03 +02002879 case LY_STMT_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01002880 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)grp, data, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002881 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002882 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02002883 PARSER_CHECK_STMTVER2_RET(ctx, "action", "grouping");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002884 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)grp, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002885 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002886 case LY_STMT_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002887 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)grp, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002888 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002889 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02002890 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "grouping");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002891 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)grp, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002892 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002893 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002894 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002895 break;
2896 default:
David Sedlákb3077192019-06-19 10:55:37 +02002897 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002898 return LY_EVALID;
2899 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002900 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002901 LY_CHECK_RET(ret);
2902checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002903 /* finalize parent pointers to the reallocated items */
David Sedlák0d6de5a2019-07-22 13:25:44 +02002904 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, grp->groupings, NULL, grp->actions, grp->notifs));
Radek Krejci7fc68292019-06-12 13:51:09 +02002905
Michal Vasko7fbc8162018-09-17 10:35:16 +02002906 return ret;
2907}
2908
Michal Vaskoea5abea2018-09-18 13:10:54 +02002909/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02002910 * @brief Parse the augment statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002911 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002912 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002913 * @param[in,out] data Data to read from, always moved to currently handled character.
2914 * @param[in,out] augments Augments to add to.
2915 *
2916 * @return LY_ERR values.
2917 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002918LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002919parse_augment(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002920{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002921 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002922 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002923 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002924 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002925 struct lysp_augment *aug;
2926
Radek Krejci2c4e7172018-10-19 15:56:26 +02002927 LY_ARRAY_NEW_RET(ctx->ctx, *augments, aug, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002928
2929 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002930 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
David Sedlákb9b892c2019-07-12 14:44:02 +02002931 YANG_CHECK_NONEMPTY(ctx, word_len, "augment");
Radek Krejci44ceedc2018-10-02 15:54:31 +02002932 INSERT_WORD(ctx, buf, aug->nodeid, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002933 aug->nodetype = LYS_AUGMENT;
2934 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002935
Radek Krejci7fc68292019-06-12 13:51:09 +02002936 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002937 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002938 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002939 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 +02002940 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002941 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002942 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 +02002943 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002944 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002945 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 +02002946 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002947 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002948 LY_CHECK_RET(parse_status(ctx, data, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002949 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002950 case LY_STMT_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002951 LY_CHECK_RET(parse_when(ctx, data, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002952 break;
2953
Radek Krejcid6b76452019-09-03 17:03:03 +02002954 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002955 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "augment");
Radek Krejci10113652018-11-14 16:56:50 +01002956 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002957 case LY_STMT_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002958 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002959 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002960 case LY_STMT_CASE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002961 LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002962 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002963 case LY_STMT_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002964 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002965 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002966 case LY_STMT_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002967 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002968 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002969 case LY_STMT_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002970 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002971 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002972 case LY_STMT_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002973 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002974 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002975 case LY_STMT_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002976 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002977 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002978 case LY_STMT_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002979 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002980 break;
2981
Radek Krejcid6b76452019-09-03 17:03:03 +02002982 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02002983 PARSER_CHECK_STMTVER2_RET(ctx, "action", "augment");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002984 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002985 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002986 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02002987 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "augment");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002988 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002989 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002990 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002991 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002992 break;
2993 default:
David Sedlákb3077192019-06-19 10:55:37 +02002994 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002995 return LY_EVALID;
2996 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002997 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002998 LY_CHECK_RET(ret);
2999checks:
3000 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02003001 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, NULL, NULL, aug->actions, aug->notifs));
Radek Krejci7fc68292019-06-12 13:51:09 +02003002
Michal Vasko7fbc8162018-09-17 10:35:16 +02003003 return ret;
3004}
3005
Michal Vaskoea5abea2018-09-18 13:10:54 +02003006/**
3007 * @brief Parse the uses statement.
3008 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003009 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003010 * @param[in,out] data Data to read from, always moved to currently handled character.
3011 * @param[in,out] siblings Siblings to add to.
3012 *
3013 * @return LY_ERR values.
3014 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003015LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003016parse_uses(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003017{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003018 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003019 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003020 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003021 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003022 struct lysp_node_uses *uses;
3023
David Sedlák60adc092019-08-06 15:57:02 +02003024 /* create uses structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003025 LY_LIST_NEW_RET(ctx->ctx, siblings, uses, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003026 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003027 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003028
Michal Vasko7fbc8162018-09-17 10:35:16 +02003029 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003030 LY_CHECK_RET(get_argument(ctx, data, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003031 INSERT_WORD(ctx, buf, uses->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003032
3033 /* parse substatements */
Radek Krejci7fc68292019-06-12 13:51:09 +02003034 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003035 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003036 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003037 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 +02003038 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003039 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003040 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 +02003041 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003042 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003043 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 +02003044 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003045 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003046 LY_CHECK_RET(parse_status(ctx, data, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003047 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003048 case LY_STMT_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003049 LY_CHECK_RET(parse_when(ctx, data, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003050 break;
3051
Radek Krejcid6b76452019-09-03 17:03:03 +02003052 case LY_STMT_REFINE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003053 LY_CHECK_RET(parse_refine(ctx, data, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003054 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003055 case LY_STMT_AUGMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003056 LY_CHECK_RET(parse_augment(ctx, data, (struct lysp_node*)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003057 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003058 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003059 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003060 break;
3061 default:
David Sedlákb3077192019-06-19 10:55:37 +02003062 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003063 return LY_EVALID;
3064 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003065 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003066checks:
3067 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02003068 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, NULL, uses->augments, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02003069
Michal Vasko7fbc8162018-09-17 10:35:16 +02003070 return ret;
3071}
3072
Michal Vaskoea5abea2018-09-18 13:10:54 +02003073/**
3074 * @brief Parse the case statement.
3075 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003076 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003077 * @param[in,out] data Data to read from, always moved to currently handled character.
3078 * @param[in,out] siblings Siblings to add to.
3079 *
3080 * @return LY_ERR values.
3081 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003082LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003083parse_case(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003084{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003085 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003086 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003087 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003088 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003089 struct lysp_node_case *cas;
3090
David Sedlák60adc092019-08-06 15:57:02 +02003091 /* create new case structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003092 LY_LIST_NEW_RET(ctx->ctx, siblings, cas, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003093 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003094 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003095
Michal Vasko7fbc8162018-09-17 10:35:16 +02003096 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003097 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003098 INSERT_WORD(ctx, buf, cas->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003099
3100 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01003101 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003102 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003103 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003104 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 +02003105 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003106 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003107 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 +02003108 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003109 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003110 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 +02003111 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003112 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003113 LY_CHECK_RET(parse_status(ctx, data, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003114 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003115 case LY_STMT_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003116 LY_CHECK_RET(parse_when(ctx, data, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003117 break;
3118
Radek Krejcid6b76452019-09-03 17:03:03 +02003119 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003120 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "case");
Radek Krejci10113652018-11-14 16:56:50 +01003121 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003122 case LY_STMT_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003123 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003124 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003125 case LY_STMT_CHOICE:
Radek Krejcia9026eb2018-12-12 16:04:47 +01003126 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003127 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003128 case LY_STMT_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003129 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003130 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003131 case LY_STMT_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003132 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003133 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003134 case LY_STMT_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003135 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003136 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003137 case LY_STMT_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003138 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003139 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003140 case LY_STMT_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003141 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003142 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003143 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003144 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003145 break;
3146 default:
David Sedlákb3077192019-06-19 10:55:37 +02003147 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003148 return LY_EVALID;
3149 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003150 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003151 return ret;
3152}
3153
Michal Vaskoea5abea2018-09-18 13:10:54 +02003154/**
3155 * @brief Parse the choice statement.
3156 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003157 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003158 * @param[in,out] data Data to read from, always moved to currently handled character.
3159 * @param[in,out] siblings Siblings to add to.
3160 *
3161 * @return LY_ERR values.
3162 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003163LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003164parse_choice(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003165{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003166 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003167 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003168 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003169 enum ly_stmt kw;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003170 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003171
David Sedlák60adc092019-08-06 15:57:02 +02003172 /* create new choice structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003173 LY_LIST_NEW_RET(ctx->ctx, siblings, choice, next, LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003174 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003175 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003176
Michal Vasko7fbc8162018-09-17 10:35:16 +02003177 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003178 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003179 INSERT_WORD(ctx, buf, choice->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003180
3181 /* parse substatements */
Radek Krejcia9026eb2018-12-12 16:04:47 +01003182 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003183 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003184 case LY_STMT_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003185 LY_CHECK_RET(parse_config(ctx, data, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003186 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003187 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003188 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 +02003189 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003190 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003191 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 +02003192 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003193 case LY_STMT_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003194 LY_CHECK_RET(parse_mandatory(ctx, data, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003195 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003196 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003197 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 +02003198 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003199 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003200 LY_CHECK_RET(parse_status(ctx, data, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003201 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003202 case LY_STMT_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003203 LY_CHECK_RET(parse_when(ctx, data, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003204 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003205 case LY_STMT_DEFAULT:
Radek Krejcia9026eb2018-12-12 16:04:47 +01003206 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 +02003207 break;
3208
Radek Krejcid6b76452019-09-03 17:03:03 +02003209 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003210 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "choice");
Radek Krejci10113652018-11-14 16:56:50 +01003211 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003212 case LY_STMT_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003213 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003214 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003215 case LY_STMT_CASE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003216 LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003217 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003218 case LY_STMT_CHOICE:
Radek Krejci335332a2019-09-05 13:03:35 +02003219 PARSER_CHECK_STMTVER2_RET(ctx, "choice", "choice");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003220 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003221 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003222 case LY_STMT_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003223 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003224 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003225 case LY_STMT_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003226 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003227 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003228 case LY_STMT_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003229 LY_CHECK_RET(parse_leaflist(ctx, data, (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_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003232 LY_CHECK_RET(parse_list(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_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003235 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003236 break;
3237 default:
David Sedlákb3077192019-06-19 10:55:37 +02003238 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003239 return LY_EVALID;
3240 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003241 }
Radek Krejcia9026eb2018-12-12 16:04:47 +01003242 LY_CHECK_RET(ret);
3243checks:
3244 if ((choice->flags & LYS_MAND_TRUE) && choice->dflt) {
David Sedlákb3077192019-06-19 10:55:37 +02003245 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMSCOMB, "mandatory", "default", "choice");
Radek Krejcia9026eb2018-12-12 16:04:47 +01003246 return LY_EVALID;
3247 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003248 return ret;
3249}
3250
Michal Vaskoea5abea2018-09-18 13:10:54 +02003251/**
3252 * @brief Parse the container statement.
3253 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003254 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003255 * @param[in,out] data Data to read from, always moved to currently handled character.
3256 * @param[in,out] siblings Siblings to add to.
3257 *
3258 * @return LY_ERR values.
3259 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003260LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003261parse_container(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003262{
3263 LY_ERR ret = 0;
3264 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003265 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003266 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003267 struct lysp_node_container *cont;
3268
David Sedlák60adc092019-08-06 15:57:02 +02003269 /* create new container structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003270 LY_LIST_NEW_RET(ctx->ctx, siblings, cont, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003271 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003272 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003273
Michal Vasko7fbc8162018-09-17 10:35:16 +02003274 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003275 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003276 INSERT_WORD(ctx, buf, cont->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003277
3278 /* parse substatements */
Radek Krejci7fc68292019-06-12 13:51:09 +02003279 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003280 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003281 case LY_STMT_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003282 LY_CHECK_RET(parse_config(ctx, data, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003283 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003284 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003285 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 +02003286 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003287 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003288 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 +02003289 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003290 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003291 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 +02003292 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003293 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003294 LY_CHECK_RET(parse_status(ctx, data, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003295 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003296 case LY_STMT_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003297 LY_CHECK_RET(parse_when(ctx, data, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003298 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003299 case LY_STMT_PRESENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003300 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 +02003301 break;
3302
Radek Krejcid6b76452019-09-03 17:03:03 +02003303 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003304 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "container");
Radek Krejci10113652018-11-14 16:56:50 +01003305 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003306 case LY_STMT_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003307 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003308 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003309 case LY_STMT_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003310 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003311 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003312 case LY_STMT_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003313 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003314 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003315 case LY_STMT_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003316 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003317 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003318 case LY_STMT_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003319 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003320 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003321 case LY_STMT_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003322 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003323 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003324 case LY_STMT_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003325 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003326 break;
3327
Radek Krejcid6b76452019-09-03 17:03:03 +02003328 case LY_STMT_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01003329 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)cont, data, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003330 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003331 case LY_STMT_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003332 LY_CHECK_RET(parse_restrs(ctx, data, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003333 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003334 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003335 PARSER_CHECK_STMTVER2_RET(ctx, "action", "container");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003336 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003337 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003338 case LY_STMT_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003339 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003340 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003341 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003342 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "container");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003343 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003344 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003345 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003346 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003347 break;
3348 default:
David Sedlákb3077192019-06-19 10:55:37 +02003349 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003350 return LY_EVALID;
3351 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003352 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003353checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01003354 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02003355 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, cont->groupings, NULL, cont->actions, cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003356 return ret;
3357}
3358
Michal Vaskoea5abea2018-09-18 13:10:54 +02003359/**
3360 * @brief Parse the list statement.
3361 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003362 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003363 * @param[in,out] data Data to read from, always moved to currently handled character.
3364 * @param[in,out] siblings Siblings to add to.
3365 *
3366 * @return LY_ERR values.
3367 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003368LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003369parse_list(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003370{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003371 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003372 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003373 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003374 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003375 struct lysp_node_list *list;
3376
David Sedlák60adc092019-08-06 15:57:02 +02003377 /* create new list structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003378 LY_LIST_NEW_RET(ctx->ctx, siblings, list, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003379 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003380 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003381
Michal Vasko7fbc8162018-09-17 10:35:16 +02003382 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003383 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003384 INSERT_WORD(ctx, buf, list->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003385
3386 /* parse substatements */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003387 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003388 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003389 case LY_STMT_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003390 LY_CHECK_RET(parse_config(ctx, data, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003391 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003392 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003393 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 +02003394 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003395 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003396 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 +02003397 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003398 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003399 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 +02003400 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003401 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003402 LY_CHECK_RET(parse_status(ctx, data, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003403 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003404 case LY_STMT_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003405 LY_CHECK_RET(parse_when(ctx, data, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003406 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003407 case LY_STMT_KEY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003408 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 +02003409 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003410 case LY_STMT_MAX_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003411 LY_CHECK_RET(parse_maxelements(ctx, data, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003412 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003413 case LY_STMT_MIN_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003414 LY_CHECK_RET(parse_minelements(ctx, data, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003415 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003416 case LY_STMT_ORDERED_BY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003417 LY_CHECK_RET(parse_orderedby(ctx, data, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003418 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003419 case LY_STMT_UNIQUE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003420 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 +02003421 break;
3422
Radek Krejcid6b76452019-09-03 17:03:03 +02003423 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003424 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "list");
Radek Krejci10113652018-11-14 16:56:50 +01003425 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003426 case LY_STMT_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003427 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003428 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003429 case LY_STMT_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003430 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003431 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003432 case LY_STMT_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003433 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003434 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003435 case LY_STMT_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003436 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003437 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003438 case LY_STMT_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003439 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003440 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003441 case LY_STMT_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003442 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003443 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003444 case LY_STMT_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003445 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003446 break;
3447
Radek Krejcid6b76452019-09-03 17:03:03 +02003448 case LY_STMT_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01003449 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)list, data, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003450 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003451 case LY_STMT_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003452 LY_CHECK_RET(parse_restrs(ctx, data, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003453 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003454 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003455 PARSER_CHECK_STMTVER2_RET(ctx, "action", "list");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003456 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003457 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003458 case LY_STMT_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003459 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003460 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003461 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003462 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "list");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003463 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003464 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003465 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003466 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003467 break;
3468 default:
David Sedlákb3077192019-06-19 10:55:37 +02003469 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003470 return LY_EVALID;
3471 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003472 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003473 LY_CHECK_RET(ret);
3474checks:
Radek Krejci7fc68292019-06-12 13:51:09 +02003475 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02003476 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, list->groupings, NULL, list->actions, list->notifs));
Radek Krejci7fc68292019-06-12 13:51:09 +02003477
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003478 if (list->max && list->min > list->max) {
David Sedlákb3077192019-06-19 10:55:37 +02003479 LOGVAL_PARSER(ctx, LYVE_SEMANTICS,
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003480 "Invalid combination of min-elements and max-elements: min value %u is bigger than the max value %u.",
3481 list->min, list->max);
3482 return LY_EVALID;
3483 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003484
3485 return ret;
3486}
3487
Michal Vaskoea5abea2018-09-18 13:10:54 +02003488/**
3489 * @brief Parse the yin-element statement.
3490 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003491 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003492 * @param[in,out] data Data to read from, always moved to currently handled character.
3493 * @param[in,out] flags Flags to write to.
3494 * @param[in,out] exts Extension instances to add to.
3495 *
3496 * @return LY_ERR values.
3497 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003498static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003499parse_yinelement(struct lys_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003500{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003501 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003502 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003503 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003504 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003505
3506 if (*flags & LYS_YINELEM_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02003507 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003508 return LY_EVALID;
3509 }
3510
3511 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003512 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003513
3514 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
3515 *flags |= LYS_YINELEM_TRUE;
3516 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
3517 *flags |= LYS_YINELEM_FALSE;
3518 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003519 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003520 free(buf);
3521 return LY_EVALID;
3522 }
3523 free(buf);
3524
Radek Krejci6d6556c2018-11-08 09:37:45 +01003525 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003526 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003527 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003528 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_YINELEM, 0, exts));
3529 LY_CHECK_RET(ret); break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003530 default:
David Sedlákb3077192019-06-19 10:55:37 +02003531 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003532 return LY_EVALID;
3533 }
3534 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003535 return ret;
3536}
3537
Michal Vaskoea5abea2018-09-18 13:10:54 +02003538/**
David Sedlák2444f8f2019-07-09 11:02:47 +02003539 * @brief Parse the argument statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003540 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003541 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003542 * @param[in,out] data Data to read from, always moved to currently handled character.
3543 * @param[in,out] argument Value to write to.
3544 * @param[in,out] flags Flags to write to.
3545 * @param[in,out] exts Extension instances to add to.
3546 *
3547 * @return LY_ERR values.
3548 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003549static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003550parse_argument(struct lys_parser_ctx *ctx, const char **data, const char **argument, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003551{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003552 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003553 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003554 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003555 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003556
3557 if (*argument) {
David Sedlákb3077192019-06-19 10:55:37 +02003558 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003559 return LY_EVALID;
3560 }
3561
3562 /* get value */
David Sedlák2444f8f2019-07-09 11:02:47 +02003563 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003564 INSERT_WORD(ctx, buf, *argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003565
Radek Krejci6d6556c2018-11-08 09:37:45 +01003566 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003567 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003568 case LY_STMT_YIN_ELEMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003569 LY_CHECK_RET(parse_yinelement(ctx, data, flags, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003570 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003571 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003572 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_ARGUMENT, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003573 break;
3574 default:
David Sedlákb3077192019-06-19 10:55:37 +02003575 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003576 return LY_EVALID;
3577 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003578 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003579 return ret;
3580}
3581
Michal Vaskoea5abea2018-09-18 13:10:54 +02003582/**
3583 * @brief Parse the extension statement.
3584 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003585 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003586 * @param[in,out] data Data to read from, always moved to currently handled character.
3587 * @param[in,out] extensions Extensions to add to.
3588 *
3589 * @return LY_ERR values.
3590 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003591static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003592parse_extension(struct lys_parser_ctx *ctx, const char **data, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003593{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003594 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003595 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003596 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003597 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003598 struct lysp_ext *ex;
3599
Radek Krejci2c4e7172018-10-19 15:56:26 +02003600 LY_ARRAY_NEW_RET(ctx->ctx, *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003601
3602 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003603 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003604 INSERT_WORD(ctx, buf, ex->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003605
Radek Krejci6d6556c2018-11-08 09:37:45 +01003606 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003607 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003608 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003609 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 +02003610 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003611 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003612 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 +02003613 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003614 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003615 LY_CHECK_RET(parse_status(ctx, data, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003616 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003617 case LY_STMT_ARGUMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003618 LY_CHECK_RET(parse_argument(ctx, data, &ex->argument, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003619 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003620 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003621 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003622 break;
3623 default:
David Sedlákb3077192019-06-19 10:55:37 +02003624 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003625 return LY_EVALID;
3626 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003627 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003628 return ret;
3629}
3630
Michal Vaskoea5abea2018-09-18 13:10:54 +02003631/**
3632 * @brief Parse the deviate statement.
3633 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003634 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003635 * @param[in,out] data Data to read from, always moved to currently handled character.
3636 * @param[in,out] deviates Deviates to add to.
3637 *
3638 * @return LY_ERR values.
3639 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003640LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003641parse_deviate(struct lys_parser_ctx *ctx, const char **data, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003642{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003643 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003644 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003645 size_t word_len, dev_mod;
Radek Krejcid6b76452019-09-03 17:03:03 +02003646 enum ly_stmt kw;
David Sedlák60adc092019-08-06 15:57:02 +02003647 struct lysp_deviate *d;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003648 struct lysp_deviate_add *d_add = NULL;
3649 struct lysp_deviate_rpl *d_rpl = NULL;
3650 struct lysp_deviate_del *d_del = NULL;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02003651 const char **d_units = NULL, ***d_uniques = NULL, ***d_dflts = NULL;
3652 struct lysp_restr **d_musts = NULL;
3653 uint16_t *d_flags = 0;
3654 uint32_t *d_min = 0, *d_max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003655
3656 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003657 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003658
3659 if ((word_len == 13) && !strncmp(word, "not-supported", word_len)) {
3660 dev_mod = LYS_DEV_NOT_SUPPORTED;
3661 } else if ((word_len == 3) && !strncmp(word, "add", word_len)) {
3662 dev_mod = LYS_DEV_ADD;
3663 } else if ((word_len == 7) && !strncmp(word, "replace", word_len)) {
3664 dev_mod = LYS_DEV_REPLACE;
3665 } else if ((word_len == 6) && !strncmp(word, "delete", word_len)) {
3666 dev_mod = LYS_DEV_DELETE;
3667 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003668 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003669 free(buf);
3670 return LY_EVALID;
3671 }
3672 free(buf);
3673
3674 /* create structure */
3675 switch (dev_mod) {
3676 case LYS_DEV_NOT_SUPPORTED:
3677 d = calloc(1, sizeof *d);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003678 LY_CHECK_ERR_RET(!d, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003679 break;
3680 case LYS_DEV_ADD:
3681 d_add = calloc(1, sizeof *d_add);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003682 LY_CHECK_ERR_RET(!d_add, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003683 d = (struct lysp_deviate *)d_add;
3684 d_units = &d_add->units;
3685 d_uniques = &d_add->uniques;
3686 d_dflts = &d_add->dflts;
3687 d_musts = &d_add->musts;
3688 d_flags = &d_add->flags;
3689 d_min = &d_add->min;
3690 d_max = &d_add->max;
3691 break;
3692 case LYS_DEV_REPLACE:
3693 d_rpl = calloc(1, sizeof *d_rpl);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003694 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003695 d = (struct lysp_deviate *)d_rpl;
3696 d_units = &d_rpl->units;
3697 d_flags = &d_rpl->flags;
3698 d_min = &d_rpl->min;
3699 d_max = &d_rpl->max;
3700 break;
3701 case LYS_DEV_DELETE:
3702 d_del = calloc(1, sizeof *d_del);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003703 LY_CHECK_ERR_RET(!d_del, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003704 d = (struct lysp_deviate *)d_del;
3705 d_units = &d_del->units;
3706 d_uniques = &d_del->uniques;
3707 d_dflts = &d_del->dflts;
3708 d_musts = &d_del->musts;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003709 break;
3710 default:
3711 assert(0);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003712 LOGINT_RET(ctx->ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003713 }
3714 d->mod = dev_mod;
3715
3716 /* insert into siblings */
David Sedlák60adc092019-08-06 15:57:02 +02003717 LY_LIST_INSERT(deviates, d, next);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003718
Radek Krejci6d6556c2018-11-08 09:37:45 +01003719 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003720 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003721 case LY_STMT_CONFIG:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003722 switch (dev_mod) {
3723 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003724 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003725 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003726 return LY_EVALID;
3727 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003728 LY_CHECK_RET(parse_config(ctx, data, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003729 break;
3730 }
3731 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003732 case LY_STMT_DEFAULT:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003733 switch (dev_mod) {
3734 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003735 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003736 return LY_EVALID;
3737 case LYS_DEV_REPLACE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003738 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 +02003739 break;
3740 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003741 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 +02003742 break;
3743 }
3744 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003745 case LY_STMT_MANDATORY:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003746 switch (dev_mod) {
3747 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003748 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003749 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003750 return LY_EVALID;
3751 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003752 LY_CHECK_RET(parse_mandatory(ctx, data, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003753 break;
3754 }
3755 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003756 case LY_STMT_MAX_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003757 switch (dev_mod) {
3758 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003759 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003760 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003761 return LY_EVALID;
3762 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003763 LY_CHECK_RET(parse_maxelements(ctx, data, d_max, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003764 break;
3765 }
3766 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003767 case LY_STMT_MIN_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003768 switch (dev_mod) {
3769 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003770 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003771 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003772 return LY_EVALID;
3773 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003774 LY_CHECK_RET(parse_minelements(ctx, data, d_min, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003775 break;
3776 }
3777 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003778 case LY_STMT_MUST:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003779 switch (dev_mod) {
3780 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003781 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003782 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003783 return LY_EVALID;
3784 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003785 LY_CHECK_RET(parse_restrs(ctx, data, kw, d_musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003786 break;
3787 }
3788 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003789 case LY_STMT_TYPE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003790 switch (dev_mod) {
3791 case LYS_DEV_NOT_SUPPORTED:
3792 case LYS_DEV_ADD:
3793 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003794 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003795 return LY_EVALID;
3796 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003797 if (d_rpl->type) {
David Sedlákb3077192019-06-19 10:55:37 +02003798 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003799 return LY_EVALID;
3800 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003801 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003802 LY_CHECK_ERR_RET(!d_rpl->type, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003803 LY_CHECK_RET(parse_type(ctx, data, d_rpl->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003804 break;
3805 }
3806 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003807 case LY_STMT_UNIQUE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003808 switch (dev_mod) {
3809 case LYS_DEV_NOT_SUPPORTED:
3810 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003811 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003812 return LY_EVALID;
3813 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003814 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 +02003815 break;
3816 }
3817 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003818 case LY_STMT_UNITS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003819 switch (dev_mod) {
3820 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003821 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003822 return LY_EVALID;
3823 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003824 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 +02003825 break;
3826 }
3827 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003828 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003829 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003830 break;
3831 default:
David Sedlákb3077192019-06-19 10:55:37 +02003832 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003833 return LY_EVALID;
3834 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003835 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003836 return ret;
3837}
3838
Michal Vaskoea5abea2018-09-18 13:10:54 +02003839/**
3840 * @brief Parse the deviation statement.
3841 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003842 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003843 * @param[in,out] data Data to read from, always moved to currently handled character.
3844 * @param[in,out] deviations Deviations to add to.
3845 *
3846 * @return LY_ERR values.
3847 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003848LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003849parse_deviation(struct lys_parser_ctx *ctx, const char **data, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003850{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003851 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003852 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003853 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003854 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003855 struct lysp_deviation *dev;
3856
Radek Krejci2c4e7172018-10-19 15:56:26 +02003857 LY_ARRAY_NEW_RET(ctx->ctx, *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003858
3859 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003860 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
David Sedlákb9b892c2019-07-12 14:44:02 +02003861 YANG_CHECK_NONEMPTY(ctx, word_len, "deviation");
Radek Krejci44ceedc2018-10-02 15:54:31 +02003862 INSERT_WORD(ctx, buf, dev->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003863
Radek Krejci6d6556c2018-11-08 09:37:45 +01003864 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003865 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003866 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003867 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 +02003868 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003869 case LY_STMT_DEVIATE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003870 LY_CHECK_RET(parse_deviate(ctx, data, &dev->deviates));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003871 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003872 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003873 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 +02003874 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003875 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003876 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003877 break;
3878 default:
David Sedlákb3077192019-06-19 10:55:37 +02003879 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003880 return LY_EVALID;
3881 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003882 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003883 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01003884checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003885 /* mandatory substatements */
3886 if (!dev->deviates) {
David Sedlákb3077192019-06-19 10:55:37 +02003887 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003888 return LY_EVALID;
3889 }
3890
3891 return ret;
3892}
3893
Michal Vaskoea5abea2018-09-18 13:10:54 +02003894/**
3895 * @brief Parse the feature statement.
3896 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003897 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003898 * @param[in,out] data Data to read from, always moved to currently handled character.
3899 * @param[in,out] features Features to add to.
3900 *
3901 * @return LY_ERR values.
3902 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003903LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003904parse_feature(struct lys_parser_ctx *ctx, const char **data, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003905{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003906 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003907 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003908 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003909 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003910 struct lysp_feature *feat;
3911
Radek Krejci2c4e7172018-10-19 15:56:26 +02003912 LY_ARRAY_NEW_RET(ctx->ctx, *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003913
3914 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003915 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003916 INSERT_WORD(ctx, buf, feat->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01003917
Radek Krejci6d6556c2018-11-08 09:37:45 +01003918 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003919 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003920 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003921 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 +02003922 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003923 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003924 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 +02003925 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003926 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003927 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 +02003928 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003929 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003930 LY_CHECK_RET(parse_status(ctx, data, &feat->flags, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003931 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003932 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003933 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003934 break;
3935 default:
David Sedlákb3077192019-06-19 10:55:37 +02003936 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003937 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003938 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003939 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003940 return ret;
3941}
3942
Michal Vaskoea5abea2018-09-18 13:10:54 +02003943/**
3944 * @brief Parse the identity statement.
3945 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003946 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003947 * @param[in,out] data Data to read from, always moved to currently handled character.
3948 * @param[in,out] identities Identities to add to.
3949 *
3950 * @return LY_ERR values.
3951 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003952LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003953parse_identity(struct lys_parser_ctx *ctx, const char **data, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003954{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003955 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003956 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003957 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003958 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003959 struct lysp_ident *ident;
3960
Radek Krejci2c4e7172018-10-19 15:56:26 +02003961 LY_ARRAY_NEW_RET(ctx->ctx, *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003962
3963 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003964 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003965 INSERT_WORD(ctx, buf, ident->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01003966
Radek Krejci6d6556c2018-11-08 09:37:45 +01003967 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003968 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003969 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003970 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 +02003971 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003972 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02003973 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "identity");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003974 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 +02003975 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003976 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003977 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 +02003978 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003979 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003980 LY_CHECK_RET(parse_status(ctx, data, &ident->flags, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003981 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003982 case LY_STMT_BASE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003983 if (ident->bases && ctx->mod_version < 2) {
David Sedlákb3077192019-06-19 10:55:37 +02003984 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 +01003985 return LY_EVALID;
3986 }
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003987 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 +02003988 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003989 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003990 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003991 break;
3992 default:
David Sedlákb3077192019-06-19 10:55:37 +02003993 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003994 return LY_EVALID;
3995 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003996 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003997 return ret;
3998}
3999
Michal Vaskoea5abea2018-09-18 13:10:54 +02004000/**
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004001 * @brief Parse module substatements.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004002 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004003 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004004 * @param[in,out] data Data to read from, always moved to currently handled character.
4005 * @param[in,out] mod Module to write to.
4006 *
4007 * @return LY_ERR values.
4008 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004009LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02004010parse_module(struct lys_parser_ctx *ctx, const char **data, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004011{
4012 LY_ERR ret = 0;
4013 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004014 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004015 enum ly_stmt kw, prev_kw = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004016 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004017 struct lysp_submodule *dup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004018
4019 /* (sub)module name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02004020 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004021 INSERT_WORD(ctx, buf, mod->mod->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004022
Radek Krejci6d6556c2018-11-08 09:37:45 +01004023 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004024
Radek Krejcie3846472018-10-15 15:24:51 +02004025#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004026 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 +02004027
Michal Vasko7fbc8162018-09-17 10:35:16 +02004028 switch (kw) {
4029 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004030 case LY_STMT_NAMESPACE:
4031 case LY_STMT_PREFIX:
Radek Krejcie3846472018-10-15 15:24:51 +02004032 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4033 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004034 case LY_STMT_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004035 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004036 break;
4037 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004038 case LY_STMT_INCLUDE:
4039 case LY_STMT_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004040 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004041 break;
4042 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004043 case LY_STMT_ORGANIZATION:
4044 case LY_STMT_CONTACT:
4045 case LY_STMT_DESCRIPTION:
4046 case LY_STMT_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004047 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004048 break;
4049
4050 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004051 case LY_STMT_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004052 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004053 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004054 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004055 case LY_STMT_ANYDATA:
4056 case LY_STMT_ANYXML:
4057 case LY_STMT_AUGMENT:
4058 case LY_STMT_CHOICE:
4059 case LY_STMT_CONTAINER:
4060 case LY_STMT_DEVIATION:
4061 case LY_STMT_EXTENSION:
4062 case LY_STMT_FEATURE:
4063 case LY_STMT_GROUPING:
4064 case LY_STMT_IDENTITY:
4065 case LY_STMT_LEAF:
4066 case LY_STMT_LEAF_LIST:
4067 case LY_STMT_LIST:
4068 case LY_STMT_NOTIFICATION:
4069 case LY_STMT_RPC:
4070 case LY_STMT_TYPEDEF:
4071 case LY_STMT_USES:
4072 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004073 mod_stmt = Y_MOD_BODY;
4074 break;
4075 default:
4076 /* error handled in the next switch */
4077 break;
4078 }
Radek Krejcie3846472018-10-15 15:24:51 +02004079#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004080
Radek Krejcie3846472018-10-15 15:24:51 +02004081 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004082 switch (kw) {
4083 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004084 case LY_STMT_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004085 LY_CHECK_RET(parse_yangversion(ctx, data, &mod->mod->version, &mod->exts));
4086 ctx->mod_version = mod->mod->version;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004087 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004088 case LY_STMT_NAMESPACE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004089 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 +02004090 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004091 case LY_STMT_PREFIX:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004092 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 +02004093 break;
4094
4095 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004096 case LY_STMT_INCLUDE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004097 LY_CHECK_RET(parse_include(ctx, mod->mod->name, data, &mod->includes));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004098 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004099 case LY_STMT_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004100 LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, data, &mod->imports));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004101 break;
4102
4103 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004104 case LY_STMT_ORGANIZATION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004105 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 +02004106 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004107 case LY_STMT_CONTACT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004108 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 +02004109 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004110 case LY_STMT_DESCRIPTION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004111 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 +02004112 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004113 case LY_STMT_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004114 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 +02004115 break;
4116
4117 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004118 case LY_STMT_REVISION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004119 LY_CHECK_RET(parse_revision(ctx, data, &mod->revs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004120 break;
4121
4122 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004123 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004124 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "module");
Radek Krejci10113652018-11-14 16:56:50 +01004125 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004126 case LY_STMT_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004127 LY_CHECK_RET(parse_any(ctx, data, kw, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004128 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004129 case LY_STMT_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004130 LY_CHECK_RET(parse_choice(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004131 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004132 case LY_STMT_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004133 LY_CHECK_RET(parse_container(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004134 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004135 case LY_STMT_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004136 LY_CHECK_RET(parse_leaf(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004137 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004138 case LY_STMT_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004139 LY_CHECK_RET(parse_leaflist(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004140 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004141 case LY_STMT_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004142 LY_CHECK_RET(parse_list(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004143 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004144 case LY_STMT_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004145 LY_CHECK_RET(parse_uses(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004146 break;
4147
Radek Krejcid6b76452019-09-03 17:03:03 +02004148 case LY_STMT_AUGMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004149 LY_CHECK_RET(parse_augment(ctx, data, NULL, &mod->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004150 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004151 case LY_STMT_DEVIATION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004152 LY_CHECK_RET(parse_deviation(ctx, data, &mod->deviations));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004153 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004154 case LY_STMT_EXTENSION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004155 LY_CHECK_RET(parse_extension(ctx, data, &mod->extensions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004156 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004157 case LY_STMT_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004158 LY_CHECK_RET(parse_feature(ctx, data, &mod->features));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004159 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004160 case LY_STMT_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004161 LY_CHECK_RET(parse_grouping(ctx, data, NULL, &mod->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004162 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004163 case LY_STMT_IDENTITY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004164 LY_CHECK_RET(parse_identity(ctx, data, &mod->identities));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004165 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004166 case LY_STMT_NOTIFICATION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004167 LY_CHECK_RET(parse_notif(ctx, data, NULL, &mod->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004168 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004169 case LY_STMT_RPC:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004170 LY_CHECK_RET(parse_action(ctx, data, NULL, &mod->rpcs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004171 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004172 case LY_STMT_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01004173 LY_CHECK_RET(parse_typedef(ctx, NULL, data, &mod->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004174 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004175 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004176 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004177 break;
4178
4179 default:
David Sedlákb3077192019-06-19 10:55:37 +02004180 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004181 return LY_EVALID;
4182 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004183 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004184 LY_CHECK_RET(ret);
Radek Krejcie86bf772018-12-14 11:39:53 +01004185
Radek Krejci6d6556c2018-11-08 09:37:45 +01004186checks:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004187 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02004188 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, mod->groupings, mod->augments, mod->rpcs, mod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004189
Michal Vasko7fbc8162018-09-17 10:35:16 +02004190 /* mandatory substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004191 if (!mod->mod->ns) {
David Sedlákb3077192019-06-19 10:55:37 +02004192 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004193 return LY_EVALID;
4194 } else if (!mod->mod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004195 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004196 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004197 }
4198
Radek Krejcie9e987e2018-10-31 12:50:27 +01004199 /* submodules share the namespace with the module names, so there must not be
4200 * a submodule of the same name in the context, no need for revision matching */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004201 dup = ly_ctx_get_submodule(ctx->ctx, NULL, mod->mod->name, NULL);
4202 if (dup) {
David Sedlákb3077192019-06-19 10:55:37 +02004203 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 +01004204 return LY_EVALID;
4205 }
4206
4207 return ret;
4208}
4209
4210/**
4211 * @brief Parse submodule substatements.
4212 *
4213 * @param[in] ctx yang parser context for logging.
4214 * @param[in,out] data Data to read from, always moved to currently handled character.
4215 * @param[out] submod Parsed submodule structure.
4216 *
4217 * @return LY_ERR values.
4218 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004219LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02004220parse_submodule(struct lys_parser_ctx *ctx, const char **data, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004221{
4222 LY_ERR ret = 0;
4223 char *buf, *word;
4224 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004225 enum ly_stmt kw, prev_kw = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004226 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
4227 struct lysp_submodule *dup;
4228
4229 /* submodule name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02004230 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004231 INSERT_WORD(ctx, buf, submod->name, word, word_len);
4232
4233 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
4234
4235#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004236 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 +01004237
4238 switch (kw) {
4239 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004240 case LY_STMT_BELONGS_TO:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004241 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4242 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004243 case LY_STMT_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004244 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4245 break;
4246 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004247 case LY_STMT_INCLUDE:
4248 case LY_STMT_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004249 CHECK_ORDER(Y_MOD_LINKAGE);
4250 break;
4251 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004252 case LY_STMT_ORGANIZATION:
4253 case LY_STMT_CONTACT:
4254 case LY_STMT_DESCRIPTION:
4255 case LY_STMT_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004256 CHECK_ORDER(Y_MOD_META);
4257 break;
4258
4259 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004260 case LY_STMT_REVISION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004261 CHECK_ORDER(Y_MOD_REVISION);
4262 break;
4263 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004264 case LY_STMT_ANYDATA:
4265 case LY_STMT_ANYXML:
4266 case LY_STMT_AUGMENT:
4267 case LY_STMT_CHOICE:
4268 case LY_STMT_CONTAINER:
4269 case LY_STMT_DEVIATION:
4270 case LY_STMT_EXTENSION:
4271 case LY_STMT_FEATURE:
4272 case LY_STMT_GROUPING:
4273 case LY_STMT_IDENTITY:
4274 case LY_STMT_LEAF:
4275 case LY_STMT_LEAF_LIST:
4276 case LY_STMT_LIST:
4277 case LY_STMT_NOTIFICATION:
4278 case LY_STMT_RPC:
4279 case LY_STMT_TYPEDEF:
4280 case LY_STMT_USES:
4281 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004282 mod_stmt = Y_MOD_BODY;
4283 break;
4284 default:
4285 /* error handled in the next switch */
4286 break;
4287 }
4288#undef CHECK_ORDER
4289
4290 prev_kw = kw;
4291 switch (kw) {
4292 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004293 case LY_STMT_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004294 LY_CHECK_RET(parse_yangversion(ctx, data, &submod->version, &submod->exts));
4295 ctx->mod_version = submod->version;
4296 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004297 case LY_STMT_BELONGS_TO:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004298 LY_CHECK_RET(parse_belongsto(ctx, data, &submod->belongsto, &submod->prefix, &submod->exts));
4299 break;
4300
4301 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004302 case LY_STMT_INCLUDE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004303 LY_CHECK_RET(parse_include(ctx, submod->name, data, &submod->includes));
4304 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004305 case LY_STMT_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004306 LY_CHECK_RET(parse_import(ctx, submod->prefix, data, &submod->imports));
4307 break;
4308
4309 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004310 case LY_STMT_ORGANIZATION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004311 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_ORGANIZATION, 0, &submod->org, Y_STR_ARG, &submod->exts));
4312 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004313 case LY_STMT_CONTACT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004314 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_CONTACT, 0, &submod->contact, Y_STR_ARG, &submod->exts));
4315 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004316 case LY_STMT_DESCRIPTION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004317 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &submod->dsc, Y_STR_ARG, &submod->exts));
4318 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004319 case LY_STMT_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004320 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &submod->ref, Y_STR_ARG, &submod->exts));
4321 break;
4322
4323 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004324 case LY_STMT_REVISION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004325 LY_CHECK_RET(parse_revision(ctx, data, &submod->revs));
4326 break;
4327
4328 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004329 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004330 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004331 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004332 case LY_STMT_ANYXML:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004333 LY_CHECK_RET(parse_any(ctx, data, kw, NULL, &submod->data));
4334 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004335 case LY_STMT_CHOICE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004336 LY_CHECK_RET(parse_choice(ctx, data, NULL, &submod->data));
4337 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004338 case LY_STMT_CONTAINER:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004339 LY_CHECK_RET(parse_container(ctx, data, NULL, &submod->data));
4340 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004341 case LY_STMT_LEAF:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004342 LY_CHECK_RET(parse_leaf(ctx, data, NULL, &submod->data));
4343 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004344 case LY_STMT_LEAF_LIST:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004345 LY_CHECK_RET(parse_leaflist(ctx, data, NULL, &submod->data));
4346 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004347 case LY_STMT_LIST:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004348 LY_CHECK_RET(parse_list(ctx, data, NULL, &submod->data));
4349 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004350 case LY_STMT_USES:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004351 LY_CHECK_RET(parse_uses(ctx, data, NULL, &submod->data));
4352 break;
4353
Radek Krejcid6b76452019-09-03 17:03:03 +02004354 case LY_STMT_AUGMENT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004355 LY_CHECK_RET(parse_augment(ctx, data, NULL, &submod->augments));
4356 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004357 case LY_STMT_DEVIATION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004358 LY_CHECK_RET(parse_deviation(ctx, data, &submod->deviations));
4359 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004360 case LY_STMT_EXTENSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004361 LY_CHECK_RET(parse_extension(ctx, data, &submod->extensions));
4362 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004363 case LY_STMT_FEATURE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004364 LY_CHECK_RET(parse_feature(ctx, data, &submod->features));
4365 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004366 case LY_STMT_GROUPING:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004367 LY_CHECK_RET(parse_grouping(ctx, data, NULL, &submod->groupings));
4368 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004369 case LY_STMT_IDENTITY:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004370 LY_CHECK_RET(parse_identity(ctx, data, &submod->identities));
4371 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004372 case LY_STMT_NOTIFICATION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004373 LY_CHECK_RET(parse_notif(ctx, data, NULL, &submod->notifs));
4374 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004375 case LY_STMT_RPC:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004376 LY_CHECK_RET(parse_action(ctx, data, NULL, &submod->rpcs));
4377 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004378 case LY_STMT_TYPEDEF:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004379 LY_CHECK_RET(parse_typedef(ctx, NULL, data, &submod->typedefs));
4380 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004381 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004382 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &submod->exts));
4383 break;
4384
4385 default:
David Sedlákb3077192019-06-19 10:55:37 +02004386 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004387 return LY_EVALID;
4388 }
4389 }
4390 LY_CHECK_RET(ret);
4391
4392checks:
4393 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02004394 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, submod->groupings, submod->augments, submod->rpcs, submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004395
4396 /* mandatory substatements */
4397 if (!submod->belongsto) {
David Sedlákb3077192019-06-19 10:55:37 +02004398 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004399 return LY_EVALID;
4400 }
4401
4402 /* submodules share the namespace with the module names, so there must not be
4403 * a submodule of the same name in the context, no need for revision matching */
4404 dup = ly_ctx_get_submodule(ctx->ctx, NULL, submod->name, NULL);
4405 if (dup && strcmp(dup->belongsto, submod->belongsto)) {
David Sedlákb3077192019-06-19 10:55:37 +02004406 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Name collision between submodules of name \"%s\".", dup->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +01004407 return LY_EVALID;
4408 }
4409
Michal Vasko7fbc8162018-09-17 10:35:16 +02004410 return ret;
4411}
4412
Radek Krejcid4557c62018-09-17 11:42:09 +02004413LY_ERR
David Sedlák1b623122019-08-05 15:27:49 +02004414yang_parse_submodule(struct lys_parser_ctx **context, struct ly_ctx *ly_ctx, struct lys_parser_ctx *main_ctx, const char *data, struct lysp_submodule **submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004415{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004416 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004417 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004418 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004419 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004420 struct lysp_submodule *mod_p = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004421
David Sedlák1b623122019-08-05 15:27:49 +02004422 /* create context */
4423 *context = calloc(1, sizeof **context);
4424 LY_CHECK_ERR_RET(!(*context), LOGMEM(ly_ctx), LY_EMEM);
4425 (*context)->ctx = ly_ctx;
Radek Krejci99435242019-09-05 16:19:15 +02004426 (*context)->pos_type = LY_VLOG_LINE;
David Sedlák1b623122019-08-05 15:27:49 +02004427 (*context)->line = 1;
4428
4429 /* map the typedefs and groupings list from main context to the submodule's context */
4430 memcpy(&(*context)->tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
4431 memcpy(&(*context)->grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes);
4432
Michal Vasko7fbc8162018-09-17 10:35:16 +02004433 /* "module"/"submodule" */
David Sedlák1b623122019-08-05 15:27:49 +02004434 ret = get_keyword(*context, &data, &kw, &word, &word_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004435 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004436
Radek Krejcid6b76452019-09-03 17:03:03 +02004437 if (kw == LY_STMT_MODULE) {
David Sedlák1b623122019-08-05 15:27:49 +02004438 LOGERR((*context)->ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004439 ret = LY_EINVAL;
4440 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004441 } else if (kw != LY_STMT_SUBMODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004442 LOGVAL_PARSER(*context, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004443 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004444 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004445 }
4446
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004447 mod_p = calloc(1, sizeof *mod_p);
David Sedlák1b623122019-08-05 15:27:49 +02004448 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM((*context)->ctx), cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004449 mod_p->parsing = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004450
4451 /* substatements */
David Sedlák1b623122019-08-05 15:27:49 +02004452 ret = parse_submodule(*context, &data, mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004453 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004454
4455 /* read some trailing spaces or new lines */
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004456 while(*data && isspace(*data)) {
4457 data++;
4458 }
4459 if (*data) {
David Sedlák1538a842019-08-08 15:38:51 +02004460 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_SUBMOD, 15, data, strlen(data) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004461 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004462 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004463 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004464
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004465 mod_p->parsing = 0;
4466 *submod = mod_p;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004467
Radek Krejcibbe09a92018-11-08 09:36:54 +01004468cleanup:
4469 if (ret) {
David Sedlák1b623122019-08-05 15:27:49 +02004470 lysp_submodule_free((*context)->ctx, mod_p);
4471 lys_parser_ctx_free(*context);
4472 *context = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004473 }
4474
4475 return ret;
4476}
4477
4478LY_ERR
David Sedlák1b623122019-08-05 15:27:49 +02004479yang_parse_module(struct lys_parser_ctx **context, const char *data, struct lys_module *mod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004480{
4481 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004482 char *word;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004483 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004484 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004485 struct lysp_module *mod_p = NULL;
4486
David Sedlák1b623122019-08-05 15:27:49 +02004487 /* create context */
4488 *context = calloc(1, sizeof **context);
4489 LY_CHECK_ERR_RET(!(*context), LOGMEM(mod->ctx), LY_EMEM);
4490 (*context)->ctx = mod->ctx;
Radek Krejci335332a2019-09-05 13:03:35 +02004491 (*context)->pos_type = LY_VLOG_LINE;
David Sedlák1b623122019-08-05 15:27:49 +02004492 (*context)->line = 1;
4493
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004494 /* "module"/"submodule" */
David Sedlák1b623122019-08-05 15:27:49 +02004495 ret = get_keyword(*context, &data, &kw, &word, &word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004496 LY_CHECK_GOTO(ret, cleanup);
4497
Radek Krejcid6b76452019-09-03 17:03:03 +02004498 if (kw == LY_STMT_SUBMODULE) {
David Sedlák1b623122019-08-05 15:27:49 +02004499 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 +01004500 ret = LY_EINVAL;
4501 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004502 } else if (kw != LY_STMT_MODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004503 LOGVAL_PARSER((*context), LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004504 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004505 goto cleanup;
4506 }
4507
4508 mod_p = calloc(1, sizeof *mod_p);
David Sedlák1b623122019-08-05 15:27:49 +02004509 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM((*context)->ctx), cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004510 mod_p->mod = mod;
4511 mod_p->parsing = 1;
4512
4513 /* substatements */
David Sedlák1b623122019-08-05 15:27:49 +02004514 ret = parse_module(*context, &data, mod_p);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004515 LY_CHECK_GOTO(ret, cleanup);
4516
4517 /* read some trailing spaces or new lines */
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004518 while(*data && isspace(*data)) {
4519 data++;
4520 }
4521 if (*data) {
David Sedlák1538a842019-08-08 15:38:51 +02004522 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_MOD, 15, data, strlen(data) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004523 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004524 goto cleanup;
4525 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004526
4527 mod_p->parsing = 0;
4528 mod->parsed = mod_p;
4529
4530cleanup:
4531 if (ret) {
4532 lysp_module_free(mod_p);
David Sedlák1b623122019-08-05 15:27:49 +02004533 lys_parser_ctx_free(*context);
4534 *context = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004535 }
4536
Michal Vasko7fbc8162018-09-17 10:35:16 +02004537 return ret;
4538}