blob: f3ca6a4dbdfee3fd3d67975596aded897734ead0 [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;
Radek Krejciff13cd12019-10-25 15:34:24 +0200303 unsigned int trailing_ws = 0; /* current number of 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 {
359 /* check and store 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 Vasko7fbc8162018-09-17 10:35:16 +0200361 }
Radek Krejciff13cd12019-10-25 15:34:24 +0200362 trailing_ws++;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200363 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 Vasko7fbc8162018-09-17 10:35:16 +0200373 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200374 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200375 } else {
376 /* check and store character */
David Sedlák40bb13b2019-07-10 14:34:18 +0200377 LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200378 /* additional characters for indentation - only 1 was count in buf_store_char */
379 ctx->indent += 7;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200380 }
Radek Krejciff13cd12019-10-25 15:34:24 +0200381 trailing_ws++;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200382 break;
383 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200384 if (block_indent) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200385 /* we will be removing the indents so we need our own buffer */
386 need_buf = 1;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200387
388 /* remove trailing tabs and spaces */
Radek Krejciff13cd12019-10-25 15:34:24 +0200389 (*word_len) = *word_len - trailing_ws;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200390
Radek Krejciff13cd12019-10-25 15:34:24 +0200391 /* restart indentation */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200392 current_indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200393 }
394
395 /* check and store character */
David Sedlák40bb13b2019-07-10 14:34:18 +0200396 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 +0200397
398 /* maintain line number */
399 ++ctx->line;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200400
401 /* reset context indentation counter for possible string after this one */
402 ctx->indent = 0;
Radek Krejciff13cd12019-10-25 15:34:24 +0200403 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200404 break;
405 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200406 /* first non-whitespace character, stop eating indentation */
407 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200408
409 /* check and store character */
David Sedlák40bb13b2019-07-10 14:34:18 +0200410 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 +0200411 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200412 break;
413 }
414 break;
415 case 3:
416 /* string encoded characters */
417 switch (**data) {
418 case 'n':
419 c = "\n";
420 break;
421 case 't':
422 c = "\t";
423 break;
424 case '\"':
425 c = *data;
426 break;
427 case '\\':
428 c = *data;
429 break;
430 default:
David Sedlákb3077192019-06-19 10:55:37 +0200431 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Double-quoted string unknown special character '\\%c'.", **data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200432 return LY_EVALID;
433 }
434
435 /* check and store character */
David Sedlák40bb13b2019-07-10 14:34:18 +0200436 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 +0200437
438 string = 2;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200439 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200440 break;
441 case 4:
442 switch (**data) {
443 case '+':
444 /* string continues */
445 string = 5;
Radek Krejciefd22f62018-09-27 11:47:58 +0200446 need_buf = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200447 break;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200448 case '\n':
449 ++ctx->line;
450 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200451 case ' ':
452 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200453 /* just skip */
454 break;
455 default:
456 /* string is finished */
457 goto string_end;
458 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200459 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200460 break;
461 case 5:
462 switch (**data) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200463 case '\n':
464 ++ctx->line;
465 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200466 case ' ':
467 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200468 /* skip */
469 break;
470 case '\'':
471 string = 1;
472 break;
473 case '\"':
474 string = 2;
475 break;
476 default:
477 /* it must be quoted again */
David Sedlákb3077192019-06-19 10:55:37 +0200478 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Both string parts divided by '+' must be quoted.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200479 return LY_EVALID;
480 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200481 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200482 break;
483 default:
484 return LY_EINT;
485 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200486 }
487
488string_end:
Radek Krejci4e199f52019-05-28 09:09:28 +0200489 if (arg <= Y_PREF_IDENTIF_ARG && !(*word_len)) {
490 /* empty identifier */
David Sedlákb3077192019-06-19 10:55:37 +0200491 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Statement argument is required.");
Radek Krejci4e199f52019-05-28 09:09:28 +0200492 return LY_EVALID;
493 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200494 return LY_SUCCESS;
495}
496
Michal Vaskoea5abea2018-09-18 13:10:54 +0200497/**
498 * @brief Get another YANG string from the raw data.
499 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200500 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200501 * @param[in,out] data Data to read from, always moved to currently handled character.
502 * @param[in] arg Type of YANG keyword argument expected.
Radek Krejcid3ca0632019-04-16 16:54:54 +0200503 * @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 +0200504 * @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 +0200505 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read string. If not needed,
506 * set to NULL. Otherwise equal to \p word_p.
507 * @param[out] word_len Length of the read string.
508 *
509 * @return LY_ERR values.
Michal Vasko7fbc8162018-09-17 10:35:16 +0200510 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200511LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200512get_argument(struct lys_parser_ctx *ctx, const char **data, enum yang_arg arg,
Radek Krejcid3ca0632019-04-16 16:54:54 +0200513 uint16_t *flags, char **word_p, char **word_b, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200514{
Radek Krejci44ceedc2018-10-02 15:54:31 +0200515 size_t buf_len = 0;
David Sedlák40bb13b2019-07-10 14:34:18 +0200516 int prefix = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200517 /* word buffer - dynamically allocated */
518 *word_b = NULL;
519
520 /* word pointer - just a pointer to data */
521 *word_p = NULL;
522
523 *word_len = 0;
524 while (**data) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200525 switch (**data) {
526 case '\'':
527 case '\"':
528 if (*word_len) {
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200529 /* invalid - quotes cannot be in unquoted string and only optsep, ; or { can follow it */
David Sedlákb3077192019-06-19 10:55:37 +0200530 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, *data,
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200531 "unquoted string character, optsep, semicolon or opening brace");
532 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200533 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200534 if (flags) {
535 (*flags) |= (**data) == '\'' ? LYS_SINGLEQUOTED : LYS_DOUBLEQUOTED;
536 }
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100537 LY_CHECK_RET(read_qstring(ctx, data, arg, word_p, word_b, word_len, &buf_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200538 goto str_end;
539 case '/':
Radek Krejci44ceedc2018-10-02 15:54:31 +0200540 if ((*data)[1] == '/') {
541 /* one-line comment */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200542 MOVE_INPUT(ctx, data, 2);
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100543 LY_CHECK_RET(skip_comment(ctx, data, 1));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200544 } else if ((*data)[1] == '*') {
545 /* block comment */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200546 MOVE_INPUT(ctx, data, 2);
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100547 LY_CHECK_RET(skip_comment(ctx, data, 2));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200548 } else {
549 /* not a comment after all */
David Sedlák40bb13b2019-07-10 14:34:18 +0200550 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 +0200551 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200552 break;
553 case ' ':
554 if (*word_len) {
555 /* word is finished */
556 goto str_end;
557 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200558 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200559 break;
560 case '\t':
561 if (*word_len) {
562 /* word is finished */
563 goto str_end;
564 }
565 /* tabs count for 8 spaces */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200566 ctx->indent += 8;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200567
568 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200569 break;
570 case '\n':
571 if (*word_len) {
572 /* word is finished */
573 goto str_end;
574 }
575 /* reset indent */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200576 ctx->indent = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200577
578 /* track line numbers */
579 ++ctx->line;
580
581 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200582 break;
583 case ';':
584 case '{':
585 if (*word_len || (arg == Y_MAYBE_STR_ARG)) {
586 /* word is finished */
587 goto str_end;
588 }
589
David Sedlákb3077192019-06-19 10:55:37 +0200590 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, *data, "an argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200591 return LY_EVALID;
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200592 case '}':
593 /* invalid - braces cannot be in unquoted string (opening braces terminates the string and can follow it) */
David Sedlákb3077192019-06-19 10:55:37 +0200594 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, *data,
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200595 "unquoted string character, optsep, semicolon or opening brace");
596 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200597 default:
David Sedlák40bb13b2019-07-10 14:34:18 +0200598 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 +0200599 break;
600 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200601 }
602
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200603 /* unexpected end of loop */
David Sedlákb3077192019-06-19 10:55:37 +0200604 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200605 return LY_EVALID;
606
Michal Vasko7fbc8162018-09-17 10:35:16 +0200607str_end:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200608 /* terminating NULL byte for buf */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200609 if (*word_b) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200610 (*word_b) = ly_realloc(*word_b, (*word_len) + 1);
611 LY_CHECK_ERR_RET(!(*word_b), LOGMEM(ctx->ctx), LY_EMEM);
612 (*word_b)[*word_len] = '\0';
Michal Vasko7fbc8162018-09-17 10:35:16 +0200613 *word_p = *word_b;
614 }
615
616 return LY_SUCCESS;
617}
618
Michal Vaskoea5abea2018-09-18 13:10:54 +0200619/**
620 * @brief Get another YANG keyword from the raw data.
621 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200622 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200623 * @param[in,out] data Data to read from, always moved to currently handled character.
624 * @param[out] kw YANG keyword read.
625 * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances.
626 * @param[out] word_len Length of the keyword in the data. Useful for extension instances.
627 *
628 * @return LY_ERR values.
629 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200630LY_ERR
Radek Krejcid6b76452019-09-03 17:03:03 +0200631get_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 +0200632{
Michal Vasko7fbc8162018-09-17 10:35:16 +0200633 int prefix;
634 const char *word_start;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200635 unsigned int c;
636 size_t len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200637
638 if (word_p) {
639 *word_p = NULL;
640 *word_len = 0;
641 }
642
643 /* first skip "optsep", comments */
644 while (**data) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200645 switch (**data) {
646 case '/':
647 if ((*data)[1] == '/') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200648 /* one-line comment */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200649 MOVE_INPUT(ctx, data, 2);
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100650 LY_CHECK_RET(skip_comment(ctx, data, 1));
Radek Krejcidcc7b322018-10-11 14:24:02 +0200651 } else if ((*data)[1] == '*') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200652 /* block comment */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200653 MOVE_INPUT(ctx, data, 2);
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100654 LY_CHECK_RET(skip_comment(ctx, data, 2));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200655 } else {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200656 /* error - not a comment after all, keyword cannot start with slash */
David Sedlákb3077192019-06-19 10:55:37 +0200657 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '/'.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200658 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200659 }
Radek Krejci13028282019-06-11 14:56:48 +0200660 continue;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200661 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200662 /* skip whitespaces (optsep) */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200663 ++ctx->line;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200664 ctx->indent = 0;
665 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200666 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200667 /* skip whitespaces (optsep) */
668 ++ctx->indent;
669 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200670 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200671 /* skip whitespaces (optsep) */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200672 ctx->indent += 8;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200673 break;
674 default:
675 /* either a keyword start or an invalid character */
676 goto keyword_start;
677 }
678
679 ++(*data);
680 }
681
682keyword_start:
683 word_start = *data;
David Sedlák5f8f0332019-06-18 16:34:30 +0200684 *kw = lysp_match_kw(ctx, data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200685
Radek Krejcid6b76452019-09-03 17:03:03 +0200686 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 +0200687 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200688 }
689
Radek Krejcid6b76452019-09-03 17:03:03 +0200690 if (*kw != LY_STMT_NONE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200691 /* make sure we have the whole keyword */
692 switch (**data) {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200693 case '\n':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200694 case '\t':
Radek Krejciabdd8062019-06-11 16:44:19 +0200695 case ' ':
696 /* mandatory "sep" is just checked, not eaten so nothing in the context is updated */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200697 break;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200698 case ':':
699 /* keyword is not actually a keyword, but prefix of an extension.
700 * To avoid repeated check of the prefix syntax, move to the point where the colon was read
701 * and we will be checking the keyword (extension instance) itself */
702 prefix = 1;
703 MOVE_INPUT(ctx, data, 1);
704 goto extension;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200705 case '{':
706 /* allowed only for input and output statements which can be without arguments */
Radek Krejcid6b76452019-09-03 17:03:03 +0200707 if (*kw == LY_STMT_INPUT || *kw == LY_STMT_OUTPUT) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200708 break;
709 }
710 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200711 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200712 MOVE_INPUT(ctx, data, 1);
David Sedlákb3077192019-06-19 10:55:37 +0200713 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(*data - word_start), word_start,
Radek Krejci44ceedc2018-10-02 15:54:31 +0200714 "a keyword followed by a separator");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200715 return LY_EVALID;
716 }
717 } else {
718 /* still can be an extension */
719 prefix = 0;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200720extension:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200721 while (**data && (**data != ' ') && (**data != '\t') && (**data != '\n') && (**data != '{') && (**data != ';')) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200722 LY_CHECK_ERR_RET(ly_getutf8(data, &c, &len),
David Sedlákb3077192019-06-19 10:55:37 +0200723 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, (*data)[-len]), LY_EVALID);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200724 ++ctx->indent;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200725 /* check character validity */
David Sedlák4a650532019-07-10 11:55:18 +0200726 LY_CHECK_RET(lysp_check_identifierchar(ctx, c, *data - len == word_start ? 1 : 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200727 }
728 if (!**data) {
David Sedlákb3077192019-06-19 10:55:37 +0200729 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200730 return LY_EVALID;
731 }
732
733 /* prefix is mandatory for extension instances */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200734 if (prefix != 2) {
David Sedlákb3077192019-06-19 10:55:37 +0200735 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(*data - word_start), word_start, "a keyword");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200736 return LY_EVALID;
737 }
738
Radek Krejcid6b76452019-09-03 17:03:03 +0200739 *kw = LY_STMT_EXTENSION_INSTANCE;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200740 }
Radek Krejci626df482018-10-11 15:06:31 +0200741success:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200742 if (word_p) {
743 *word_p = (char *)word_start;
744 *word_len = *data - word_start;
745 }
746
747 return LY_SUCCESS;
748}
749
Michal Vaskoea5abea2018-09-18 13:10:54 +0200750/**
751 * @brief Parse extension instance substatements.
752 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200753 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200754 * @param[in,out] data Data to read from, always moved to currently handled character.
Radek Krejci335332a2019-09-05 13:03:35 +0200755 * @param[in] kw Statement keyword value matching @p word value.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200756 * @param[in] word Extension instance substatement name (keyword).
757 * @param[in] word_len Extension instance substatement name length.
758 * @param[in,out] child Children of this extension instance to add to.
759 *
760 * @return LY_ERR values.
761 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200762static LY_ERR
Radek Krejci335332a2019-09-05 13:03:35 +0200763parse_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 +0200764 struct lysp_stmt **child)
765{
766 char *buf;
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100767 LY_ERR ret = LY_SUCCESS;
Radek Krejci335332a2019-09-05 13:03:35 +0200768 enum ly_stmt child_kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200769 struct lysp_stmt *stmt, *par_child;
770
771 stmt = calloc(1, sizeof *stmt);
772 LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM);
773
Radek Krejcibb9b1982019-04-08 14:24:59 +0200774 /* insert into parent statements */
775 if (!*child) {
776 *child = stmt;
777 } else {
778 for (par_child = *child; par_child->next; par_child = par_child->next);
779 par_child->next = stmt;
780 }
781
Radek Krejci44ceedc2018-10-02 15:54:31 +0200782 stmt->stmt = lydict_insert(ctx->ctx, word, word_len);
Radek Krejci335332a2019-09-05 13:03:35 +0200783 stmt->kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200784
785 /* get optional argument */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200786 LY_CHECK_RET(get_argument(ctx, data, Y_MAYBE_STR_ARG, &stmt->flags, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200787
Radek Krejci0ae092d2018-09-20 16:43:19 +0200788 if (word) {
789 if (buf) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200790 stmt->arg = lydict_insert_zc(ctx->ctx, word);
Radek Krejci0ae092d2018-09-20 16:43:19 +0200791 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200792 stmt->arg = lydict_insert(ctx->ctx, word, word_len);
Radek Krejci0ae092d2018-09-20 16:43:19 +0200793 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200794 }
795
Radek Krejci335332a2019-09-05 13:03:35 +0200796 YANG_READ_SUBSTMT_FOR(ctx, data, child_kw, word, word_len, ret, ) {
797 LY_CHECK_RET(parse_ext_substmt(ctx, data, child_kw, word, word_len, &stmt->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200798 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200799 return ret;
800}
801
Michal Vaskoea5abea2018-09-18 13:10:54 +0200802/**
803 * @brief Parse extension instance.
804 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200805 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200806 * @param[in,out] data Data to read from, always moved to currently handled character.
807 * @param[in] ext_name Extension instance substatement name (keyword).
808 * @param[in] ext_name_len Extension instance substatement name length.
809 * @param[in] insubstmt Type of the keyword this extension instance is a substatement of.
810 * @param[in] insubstmt_index Index of the keyword instance this extension instance is a substatement of.
811 * @param[in,out] exts Extension instances to add to.
812 *
813 * @return LY_ERR values.
814 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200815static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200816parse_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 +0200817 uint32_t insubstmt_index, struct lysp_ext_instance **exts)
818{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100819 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200820 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200821 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200822 struct lysp_ext_instance *e;
Radek Krejcid6b76452019-09-03 17:03:03 +0200823 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200824
Radek Krejci2c4e7172018-10-19 15:56:26 +0200825 LY_ARRAY_NEW_RET(ctx->ctx, *exts, e, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200826
827 /* store name and insubstmt info */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200828 e->name = lydict_insert(ctx->ctx, ext_name, ext_name_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200829 e->insubstmt = insubstmt;
830 e->insubstmt_index = insubstmt_index;
831
832 /* get optional argument */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200833 LY_CHECK_RET(get_argument(ctx, data, Y_MAYBE_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200834
Radek Krejci0ae092d2018-09-20 16:43:19 +0200835 if (word) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200836 INSERT_WORD(ctx, buf, e->argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200837 }
838
Radek Krejci6d6556c2018-11-08 09:37:45 +0100839 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Radek Krejci335332a2019-09-05 13:03:35 +0200840 LY_CHECK_RET(parse_ext_substmt(ctx, data, kw, word, word_len, &e->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200841 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200842 return ret;
843}
844
Michal Vaskoea5abea2018-09-18 13:10:54 +0200845/**
846 * @brief Parse a generic text field without specific constraints. Those are contact, organization,
847 * description, etc...
848 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200849 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200850 * @param[in,out] data Data to read from, always moved to currently handled character.
851 * @param[in] substmt Type of this substatement.
852 * @param[in] substmt_index Index of this substatement.
853 * @param[in,out] value Place to store the parsed value.
854 * @param[in] arg Type of the YANG keyword argument (of the value).
855 * @param[in,out] exts Extension instances to add to.
856 *
857 * @return LY_ERR values.
858 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200859static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200860parse_text_field(struct lys_parser_ctx *ctx, const char **data, LYEXT_SUBSTMT substmt, uint32_t substmt_index,
Michal Vasko7fbc8162018-09-17 10:35:16 +0200861 const char **value, enum yang_arg arg, struct lysp_ext_instance **exts)
862{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100863 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200864 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200865 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200866 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200867
868 if (*value) {
David Sedlákb3077192019-06-19 10:55:37 +0200869 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200870 return LY_EVALID;
871 }
872
873 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200874 LY_CHECK_RET(get_argument(ctx, data, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200875
876 /* store value and spend buf if allocated */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200877 INSERT_WORD(ctx, buf, *value, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200878
Radek Krejci6d6556c2018-11-08 09:37:45 +0100879 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200880 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200881 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100882 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, substmt, substmt_index, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200883 break;
884 default:
David Sedlákb3077192019-06-19 10:55:37 +0200885 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200886 return LY_EVALID;
887 }
888 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200889 return ret;
890}
891
Michal Vaskoea5abea2018-09-18 13:10:54 +0200892/**
893 * @brief Parse the yang-version statement.
894 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200895 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200896 * @param[in,out] data Data to read from, always moved to currently handled character.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100897 * @param[out] version Storage for the parsed information.
898 * @param[in, out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200899 *
900 * @return LY_ERR values.
901 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200902static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200903parse_yangversion(struct lys_parser_ctx *ctx, const char **data, uint8_t *version, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200904{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100905 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200906 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200907 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200908 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200909
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100910 if (*version) {
David Sedlákb3077192019-06-19 10:55:37 +0200911 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200912 return LY_EVALID;
913 }
914
915 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200916 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200917
918 if ((word_len == 3) && !strncmp(word, "1.0", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100919 *version = LYS_VERSION_1_0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200920 } else if ((word_len == 3) && !strncmp(word, "1.1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100921 *version = LYS_VERSION_1_1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200922 } else {
David Sedlákb3077192019-06-19 10:55:37 +0200923 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200924 free(buf);
925 return LY_EVALID;
926 }
927 free(buf);
928
Radek Krejci6d6556c2018-11-08 09:37:45 +0100929 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200930 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200931 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100932 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_VERSION, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200933 break;
934 default:
David Sedlákb3077192019-06-19 10:55:37 +0200935 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200936 return LY_EVALID;
937 }
938 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200939 return ret;
940}
941
Michal Vaskoea5abea2018-09-18 13:10:54 +0200942/**
943 * @brief Parse the belongs-to statement.
944 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200945 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200946 * @param[in,out] data Data to read from, always moved to currently handled character.
947 * @param[in,out] belongsto Place to store the parsed value.
948 * @param[in,out] prefix Place to store the parsed belongs-to prefix value.
949 * @param[in,out] exts Extension instances to add to.
950 *
951 * @return LY_ERR values.
952 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200953static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200954parse_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 +0200955{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100956 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200957 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200958 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200959 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200960
961 if (*belongsto) {
David Sedlákb3077192019-06-19 10:55:37 +0200962 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200963 return LY_EVALID;
964 }
965
966 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200967 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200968
Radek Krejci44ceedc2018-10-02 15:54:31 +0200969 INSERT_WORD(ctx, buf, *belongsto, word, word_len);
Radek Krejcif09e4e82019-06-14 15:08:11 +0200970
Radek Krejci6d6556c2018-11-08 09:37:45 +0100971 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200972 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200973 case LY_STMT_PREFIX:
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100974 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_PREFIX, 0, prefix, Y_IDENTIF_ARG, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200975 break;
Radek Krejcid6b76452019-09-03 17:03:03 +0200976 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100977 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_BELONGSTO, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200978 break;
979 default:
David Sedlákb3077192019-06-19 10:55:37 +0200980 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200981 return LY_EVALID;
982 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200983 }
Radek Krejcic59bc972018-09-17 16:13:06 +0200984 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +0100985checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200986 /* mandatory substatements */
987 if (!*prefix) {
David Sedlákb3077192019-06-19 10:55:37 +0200988 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200989 return LY_EVALID;
990 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200991 return ret;
992}
993
Michal Vaskoea5abea2018-09-18 13:10:54 +0200994/**
995 * @brief Parse the revision-date statement.
996 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200997 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200998 * @param[in,out] data Data to read from, always moved to currently handled character.
999 * @param[in,out] rev Array to store the parsed value in.
1000 * @param[in,out] exts Extension instances to add to.
1001 *
1002 * @return LY_ERR values.
1003 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001004static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001005parse_revisiondate(struct lys_parser_ctx *ctx, const char **data, char *rev, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001006{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001007 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001008 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001009 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001010 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001011
1012 if (rev[0]) {
David Sedlákb3077192019-06-19 10:55:37 +02001013 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001014 return LY_EVALID;
1015 }
1016
1017 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001018 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001019
1020 /* check value */
Radek Krejcibbe09a92018-11-08 09:36:54 +01001021 if (lysp_check_date(ctx, word, word_len, "revision-date")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001022 free(buf);
1023 return LY_EVALID;
1024 }
1025
1026 /* store value and spend buf if allocated */
1027 strncpy(rev, word, word_len);
1028 free(buf);
1029
Radek Krejci6d6556c2018-11-08 09:37:45 +01001030 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001031 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001032 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001033 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_REVISIONDATE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001034 break;
1035 default:
David Sedlákb3077192019-06-19 10:55:37 +02001036 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001037 return LY_EVALID;
1038 }
1039 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001040 return ret;
1041}
1042
Michal Vaskoea5abea2018-09-18 13:10:54 +02001043/**
1044 * @brief Parse the include statement.
1045 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001046 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001047 * @param[in] module_name Name of the module to check name collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001048 * @param[in,out] data Data to read from, always moved to currently handled character.
1049 * @param[in,out] includes Parsed includes to add to.
1050 *
1051 * @return LY_ERR values.
1052 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001053static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001054parse_include(struct lys_parser_ctx *ctx, const char *module_name, const char **data, struct lysp_include **includes)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001055{
Radek Krejcid33273d2018-10-25 14:55:52 +02001056 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001057 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001058 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001059 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001060 struct lysp_include *inc;
1061
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001062 LY_ARRAY_NEW_RET(ctx->ctx, *includes, inc, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001063
1064 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001065 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001066
Radek Krejci086c7132018-10-26 15:29:04 +02001067 INSERT_WORD(ctx, buf, inc->name, word, word_len);
1068
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001069 /* submodules share the namespace with the module names, so there must not be
1070 * a module of the same name in the context, no need for revision matching */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001071 if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(ctx->ctx, inc->name)) {
David Sedlákb3077192019-06-19 10:55:37 +02001072 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Name collision between module and submodule of name \"%s\".", inc->name);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001073 return LY_EVALID;
1074 }
1075
Radek Krejci6d6556c2018-11-08 09:37:45 +01001076 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001077 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001078 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001079 PARSER_CHECK_STMTVER2_RET(ctx, "description", "include");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001080 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 +02001081 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001082 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001083 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "include");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001084 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 +02001085 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001086 case LY_STMT_REVISION_DATE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001087 LY_CHECK_RET(parse_revisiondate(ctx, data, inc->rev, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001088 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001089 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001090 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001091 break;
1092 default:
David Sedlákb3077192019-06-19 10:55:37 +02001093 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001094 return LY_EVALID;
1095 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001096 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001097 return ret;
1098}
1099
Michal Vaskoea5abea2018-09-18 13:10:54 +02001100/**
1101 * @brief Parse the import statement.
1102 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001103 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001104 * @param[in] module_prefix Prefix of the module to check prefix collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001105 * @param[in,out] data Data to read from, always moved to currently handled character.
1106 * @param[in,out] imports Parsed imports to add to.
1107 *
1108 * @return LY_ERR values.
1109 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001110static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001111parse_import(struct lys_parser_ctx *ctx, const char *module_prefix, const char **data, struct lysp_import **imports)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001112{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001113 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001114 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001115 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001116 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001117 struct lysp_import *imp;
1118
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001119 LY_ARRAY_NEW_RET(ctx->ctx, *imports, imp, LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001120
1121 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001122 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02001123 INSERT_WORD(ctx, buf, imp->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001124
Radek Krejci6d6556c2018-11-08 09:37:45 +01001125 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001126 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001127 case LY_STMT_PREFIX:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001128 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 +01001129 LY_CHECK_RET(lysp_check_prefix(ctx, *imports, module_prefix, &imp->prefix), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001130 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001131 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001132 PARSER_CHECK_STMTVER2_RET(ctx, "description", "import");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001133 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 +02001134 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001135 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001136 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "import");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001137 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 +02001138 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001139 case LY_STMT_REVISION_DATE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001140 LY_CHECK_RET(parse_revisiondate(ctx, data, imp->rev, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001141 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001142 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001143 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001144 break;
1145 default:
David Sedlákb3077192019-06-19 10:55:37 +02001146 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001147 return LY_EVALID;
1148 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001149 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001150 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001151checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001152 /* mandatory substatements */
David Sedlákb3077192019-06-19 10:55:37 +02001153 LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001154
1155 return ret;
1156}
1157
Michal Vaskoea5abea2018-09-18 13:10:54 +02001158/**
1159 * @brief Parse the revision statement.
1160 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001161 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001162 * @param[in,out] data Data to read from, always moved to currently handled character.
1163 * @param[in,out] revs Parsed revisions to add to.
1164 *
1165 * @return LY_ERR values.
1166 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001167static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001168parse_revision(struct lys_parser_ctx *ctx, const char **data, struct lysp_revision **revs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001169{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001170 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001171 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001172 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001173 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001174 struct lysp_revision *rev;
1175
Radek Krejci2c4e7172018-10-19 15:56:26 +02001176 LY_ARRAY_NEW_RET(ctx->ctx, *revs, rev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001177
1178 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001179 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001180
1181 /* check value */
Radek Krejcibbe09a92018-11-08 09:36:54 +01001182 if (lysp_check_date(ctx, word, word_len, "revision")) {
David Sedlák68ef3dc2019-07-22 13:40:19 +02001183 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001184 return LY_EVALID;
1185 }
1186
Radek Krejcib7db73a2018-10-24 14:18:40 +02001187 strncpy(rev->date, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001188 free(buf);
1189
Radek Krejci6d6556c2018-11-08 09:37:45 +01001190 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001191 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001192 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001193 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 +02001194 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001195 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001196 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 +02001197 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001198 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001199 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001200 break;
1201 default:
David Sedlákb3077192019-06-19 10:55:37 +02001202 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001203 return LY_EVALID;
1204 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001205 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001206 return ret;
1207}
1208
Michal Vaskoea5abea2018-09-18 13:10:54 +02001209/**
1210 * @brief Parse a generic text field that can have more instances such as base.
1211 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001212 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001213 * @param[in,out] data Data to read from, always moved to currently handled character.
1214 * @param[in] substmt Type of this substatement.
1215 * @param[in,out] texts Parsed values to add to.
1216 * @param[in] arg Type of the expected argument.
1217 * @param[in,out] exts Extension instances to add to.
1218 *
1219 * @return LY_ERR values.
1220 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001221static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001222parse_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 +02001223 struct lysp_ext_instance **exts)
1224{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001225 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001226 char *buf, *word;
Radek Krejci151a5b72018-10-19 14:21:44 +02001227 const char **item;
1228 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001229 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001230
1231 /* allocate new pointer */
Radek Krejci2c4e7172018-10-19 15:56:26 +02001232 LY_ARRAY_NEW_RET(ctx->ctx, *texts, item, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001233
1234 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001235 LY_CHECK_RET(get_argument(ctx, data, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001236
Radek Krejci151a5b72018-10-19 14:21:44 +02001237 INSERT_WORD(ctx, buf, *item, word, word_len);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001238 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001239 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001240 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001241 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, substmt, LY_ARRAY_SIZE(*texts) - 1, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001242 break;
1243 default:
David Sedlákb3077192019-06-19 10:55:37 +02001244 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001245 return LY_EVALID;
1246 }
1247 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001248 return ret;
1249}
1250
Michal Vaskoea5abea2018-09-18 13:10:54 +02001251/**
1252 * @brief Parse the config statement.
1253 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001254 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001255 * @param[in,out] data Data to read from, always moved to currently handled character.
1256 * @param[in,out] flags Flags to add to.
1257 * @param[in,out] exts Extension instances to add to.
1258 *
1259 * @return LY_ERR values.
1260 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001261static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001262parse_config(struct lys_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001263{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001264 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001265 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001266 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001267 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001268
1269 if (*flags & LYS_CONFIG_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001270 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001271 return LY_EVALID;
1272 }
1273
1274 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001275 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001276
1277 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1278 *flags |= LYS_CONFIG_W;
1279 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1280 *flags |= LYS_CONFIG_R;
1281 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001282 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001283 free(buf);
1284 return LY_EVALID;
1285 }
1286 free(buf);
1287
Radek Krejci6d6556c2018-11-08 09:37:45 +01001288 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001289 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001290 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001291 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_CONFIG, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001292 break;
1293 default:
David Sedlákb3077192019-06-19 10:55:37 +02001294 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001295 return LY_EVALID;
1296 }
1297 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001298 return ret;
1299}
1300
Michal Vaskoea5abea2018-09-18 13:10:54 +02001301/**
1302 * @brief Parse the mandatory statement.
1303 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001304 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001305 * @param[in,out] data Data to read from, always moved to currently handled character.
1306 * @param[in,out] flags Flags to add to.
1307 * @param[in,out] exts Extension instances to add to.
1308 *
1309 * @return LY_ERR values.
1310 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001311static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001312parse_mandatory(struct lys_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001313{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001314 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001315 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001316 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001317 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001318
1319 if (*flags & LYS_MAND_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001320 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001321 return LY_EVALID;
1322 }
1323
1324 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001325 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001326
1327 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1328 *flags |= LYS_MAND_TRUE;
1329 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1330 *flags |= LYS_MAND_FALSE;
1331 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001332 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001333 free(buf);
1334 return LY_EVALID;
1335 }
1336 free(buf);
1337
Radek Krejci6d6556c2018-11-08 09:37:45 +01001338 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001339 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001340 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001341 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MANDATORY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001342 break;
1343 default:
David Sedlákb3077192019-06-19 10:55:37 +02001344 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001345 return LY_EVALID;
1346 }
1347 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001348 return ret;
1349}
1350
Michal Vaskoea5abea2018-09-18 13:10:54 +02001351/**
1352 * @brief Parse a restriction such as range or length.
1353 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001354 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001355 * @param[in,out] data Data to read from, always moved to currently handled character.
1356 * @param[in] restr_kw Type of this particular restriction.
1357 * @param[in,out] exts Extension instances to add to.
1358 *
1359 * @return LY_ERR values.
1360 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001361static LY_ERR
Radek Krejcid6b76452019-09-03 17:03:03 +02001362parse_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 +02001363{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001364 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001365 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001366 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001367 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001368
1369 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001370 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001371
David Sedlákb9b892c2019-07-12 14:44:02 +02001372 YANG_CHECK_NONEMPTY(ctx, word_len, ly_stmt2str(restr_kw));
Radek Krejci44ceedc2018-10-02 15:54:31 +02001373 INSERT_WORD(ctx, buf, restr->arg, word, word_len);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001374 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001375 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001376 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001377 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 +02001378 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001379 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001380 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 +02001381 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001382 case LY_STMT_ERROR_APP_TAG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001383 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 +02001384 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001385 case LY_STMT_ERROR_MESSAGE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001386 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 +02001387 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001388 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001389 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001390 break;
1391 default:
David Sedlákb3077192019-06-19 10:55:37 +02001392 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001393 return LY_EVALID;
1394 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001395 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001396 return ret;
1397}
1398
Michal Vaskoea5abea2018-09-18 13:10:54 +02001399/**
1400 * @brief Parse a restriction that can have more instances such as must.
1401 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001402 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001403 * @param[in,out] data Data to read from, always moved to currently handled character.
1404 * @param[in] restr_kw Type of this particular restriction.
1405 * @param[in,out] restrs Restrictions to add to.
1406 *
1407 * @return LY_ERR values.
1408 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001409static LY_ERR
Radek Krejcid6b76452019-09-03 17:03:03 +02001410parse_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 +02001411{
1412 struct lysp_restr *restr;
1413
Radek Krejci2c4e7172018-10-19 15:56:26 +02001414 LY_ARRAY_NEW_RET(ctx->ctx, *restrs, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001415 return parse_restr(ctx, data, restr_kw, restr);
1416}
1417
Michal Vaskoea5abea2018-09-18 13:10:54 +02001418/**
1419 * @brief Parse the status statement.
1420 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001421 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001422 * @param[in,out] data Data to read from, always moved to currently handled character.
1423 * @param[in,out] flags Flags to add to.
1424 * @param[in,out] exts Extension instances to add to.
1425 *
1426 * @return LY_ERR values.
1427 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001428static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001429parse_status(struct lys_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001430{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001431 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001432 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001433 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001434 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001435
1436 if (*flags & LYS_STATUS_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001437 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001438 return LY_EVALID;
1439 }
1440
1441 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001442 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001443
1444 if ((word_len == 7) && !strncmp(word, "current", word_len)) {
1445 *flags |= LYS_STATUS_CURR;
1446 } else if ((word_len == 10) && !strncmp(word, "deprecated", word_len)) {
1447 *flags |= LYS_STATUS_DEPRC;
1448 } else if ((word_len == 8) && !strncmp(word, "obsolete", word_len)) {
1449 *flags |= LYS_STATUS_OBSLT;
1450 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001451 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001452 free(buf);
1453 return LY_EVALID;
1454 }
1455 free(buf);
1456
Radek Krejci6d6556c2018-11-08 09:37:45 +01001457 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001458 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001459 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001460 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_STATUS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001461 break;
1462 default:
David Sedlákb3077192019-06-19 10:55:37 +02001463 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001464 return LY_EVALID;
1465 }
1466 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001467 return ret;
1468}
1469
Michal Vaskoea5abea2018-09-18 13:10:54 +02001470/**
1471 * @brief Parse the when statement.
1472 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001473 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001474 * @param[in,out] data Data to read from, always moved to currently handled character.
1475 * @param[in,out] when_p When pointer to parse to.
1476 *
1477 * @return LY_ERR values.
1478 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001479LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001480parse_when(struct lys_parser_ctx *ctx, const char **data, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001481{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001482 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001483 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001484 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001485 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001486 struct lysp_when *when;
1487
1488 if (*when_p) {
David Sedlákb3077192019-06-19 10:55:37 +02001489 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001490 return LY_EVALID;
1491 }
1492
1493 when = calloc(1, sizeof *when);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001494 LY_CHECK_ERR_RET(!when, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001495
1496 /* get value */
Radek Krejci2f54df52019-06-21 10:59:19 +02001497 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 +02001498 YANG_CHECK_NONEMPTY(ctx, word_len, "when");
Radek Krejci44ceedc2018-10-02 15:54:31 +02001499 INSERT_WORD(ctx, buf, when->cond, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001500
Radek Krejcif09e4e82019-06-14 15:08:11 +02001501 *when_p = when;
1502
Radek Krejci6d6556c2018-11-08 09:37:45 +01001503 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001504 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001505 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001506 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 +02001507 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001508 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001509 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 +02001510 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001511 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001512 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001513 break;
1514 default:
David Sedlákb3077192019-06-19 10:55:37 +02001515 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001516 return LY_EVALID;
1517 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001518 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001519 return ret;
1520}
1521
Michal Vaskoea5abea2018-09-18 13:10:54 +02001522/**
1523 * @brief Parse the anydata or anyxml statement.
1524 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001525 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001526 * @param[in,out] data Data to read from, always moved to currently handled character.
1527 * @param[in] kw Type of this particular keyword.
1528 * @param[in,out] siblings Siblings to add to.
1529 *
1530 * @return LY_ERR values.
1531 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02001532LY_ERR
Radek Krejcid6b76452019-09-03 17:03:03 +02001533parse_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 +02001534{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001535 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001536 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001537 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001538 struct lysp_node_anydata *any;
1539
David Sedlák60adc092019-08-06 15:57:02 +02001540 /* create new structure and insert into siblings */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02001541 LY_LIST_NEW_RET(ctx->ctx, siblings, any, next, LY_EMEM);
David Sedlák60adc092019-08-06 15:57:02 +02001542
Radek Krejcid6b76452019-09-03 17:03:03 +02001543 any->nodetype = kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001544 any->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001545
Michal Vasko7fbc8162018-09-17 10:35:16 +02001546 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001547 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02001548 INSERT_WORD(ctx, buf, any->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001549
1550 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01001551 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001552 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001553 case LY_STMT_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001554 LY_CHECK_RET(parse_config(ctx, data, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001555 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001556 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001557 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 +02001558 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001559 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001560 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 +02001561 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001562 case LY_STMT_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001563 LY_CHECK_RET(parse_mandatory(ctx, data, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001564 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001565 case LY_STMT_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001566 LY_CHECK_RET(parse_restrs(ctx, data, kw, &any->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001567 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001568 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001569 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 +02001570 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001571 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001572 LY_CHECK_RET(parse_status(ctx, data, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001573 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001574 case LY_STMT_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001575 LY_CHECK_RET(parse_when(ctx, data, &any->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001576 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001577 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001578 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001579 break;
1580 default:
David Sedlákb3077192019-06-19 10:55:37 +02001581 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
Radek Krejcid6b76452019-09-03 17:03:03 +02001582 (any->nodetype & LYS_ANYDATA) == LYS_ANYDATA ? ly_stmt2str(LY_STMT_ANYDATA) : ly_stmt2str(LY_STMT_ANYXML));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001583 return LY_EVALID;
1584 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001585 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001586 return ret;
1587}
1588
Michal Vaskoea5abea2018-09-18 13:10:54 +02001589/**
1590 * @brief Parse the value or position statement. Substatement of type enum statement.
1591 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001592 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001593 * @param[in,out] data Data to read from, always moved to currently handled character.
1594 * @param[in] val_kw Type of this particular keyword.
1595 * @param[in,out] value Value to write to.
1596 * @param[in,out] flags Flags to write to.
1597 * @param[in,out] exts Extension instances to add to.
1598 *
1599 * @return LY_ERR values.
1600 */
David Sedlákd6ce6d72019-07-16 17:30:18 +02001601LY_ERR
Radek Krejcid6b76452019-09-03 17:03:03 +02001602parse_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 +02001603 struct lysp_ext_instance **exts)
1604{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001605 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001606 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001607 size_t word_len;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02001608 long int num = 0;
1609 unsigned long int unum = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001610 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001611
1612 if (*flags & LYS_SET_VALUE) {
David Sedlákb3077192019-06-19 10:55:37 +02001613 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001614 return LY_EVALID;
1615 }
1616 *flags |= LYS_SET_VALUE;
1617
1618 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001619 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001620
Radek Krejcid6b76452019-09-03 17:03:03 +02001621 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 +02001622 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001623 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001624 }
1625
1626 errno = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001627 if (val_kw == LY_STMT_VALUE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001628 num = strtol(word, &ptr, 10);
Radek Krejci8b764662018-11-14 14:15:13 +01001629 if (num < INT64_C(-2147483648) || num > INT64_C(2147483647)) {
David Sedlákb3077192019-06-19 10:55:37 +02001630 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001631 goto error;
1632 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001633 } else {
1634 unum = strtoul(word, &ptr, 10);
Radek Krejci8b764662018-11-14 14:15:13 +01001635 if (unum > UINT64_C(4294967295)) {
David Sedlákb3077192019-06-19 10:55:37 +02001636 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001637 goto error;
1638 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001639 }
1640 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001641 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001642 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001643 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001644 }
1645 if (errno == ERANGE) {
David Sedlákb3077192019-06-19 10:55:37 +02001646 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001647 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001648 }
Radek Krejcid6b76452019-09-03 17:03:03 +02001649 if (val_kw == LY_STMT_VALUE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001650 *value = num;
1651 } else {
1652 *value = unum;
1653 }
1654 free(buf);
1655
Radek Krejci6d6556c2018-11-08 09:37:45 +01001656 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001657 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001658 case LY_STMT_EXTENSION_INSTANCE:
1659 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 +02001660 break;
1661 default:
David Sedlákb3077192019-06-19 10:55:37 +02001662 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001663 return LY_EVALID;
1664 }
1665 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001666 return ret;
Radek Krejci8b764662018-11-14 14:15:13 +01001667
1668error:
1669 free(buf);
1670 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001671}
1672
Michal Vaskoea5abea2018-09-18 13:10:54 +02001673/**
1674 * @brief Parse the enum or bit statement. Substatement of type statement.
1675 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001676 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001677 * @param[in,out] data Data to read from, always moved to currently handled character.
1678 * @param[in] enum_kw Type of this particular keyword.
1679 * @param[in,out] enums Enums or bits to add to.
1680 *
1681 * @return LY_ERR values.
1682 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001683static LY_ERR
Radek Krejcid6b76452019-09-03 17:03:03 +02001684parse_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 +02001685{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001686 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001687 char *buf, *word;
David Sedlák6544c182019-07-12 13:17:33 +02001688 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001689 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001690 struct lysp_type_enum *enm;
1691
Radek Krejci2c4e7172018-10-19 15:56:26 +02001692 LY_ARRAY_NEW_RET(ctx->ctx, *enums, enm, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001693
1694 /* get value */
Radek Krejcid6b76452019-09-03 17:03:03 +02001695 LY_CHECK_RET(get_argument(ctx, data, enum_kw == LY_STMT_ENUM ? Y_STR_ARG : Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
1696 if (enum_kw == LY_STMT_ENUM) {
David Sedlák07869a52019-07-12 14:28:19 +02001697 ret = lysp_check_enum_name(ctx, (const char *)word, word_len);
David Sedlák6544c182019-07-12 13:17:33 +02001698 LY_CHECK_ERR_RET(ret, free(buf), ret);
Radek Krejci335332a2019-09-05 13:03:35 +02001699 } /* else nothing specific for YANG_BIT */
Radek Krejci8b764662018-11-14 14:15:13 +01001700
Radek Krejci44ceedc2018-10-02 15:54:31 +02001701 INSERT_WORD(ctx, buf, enm->name, word, word_len);
Radek Krejci8b764662018-11-14 14:15:13 +01001702 CHECK_UNIQUENESS(ctx, *enums, name, ly_stmt2str(enum_kw), enm->name);
1703
Radek Krejci6d6556c2018-11-08 09:37:45 +01001704 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001705 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001706 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001707 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 +02001708 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001709 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02001710 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", ly_stmt2str(enum_kw));
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001711 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 +02001712 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001713 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001714 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 +02001715 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001716 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001717 LY_CHECK_RET(parse_status(ctx, data, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001718 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001719 case LY_STMT_VALUE:
1720 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 +02001721 ly_stmt2str(enum_kw)), LY_EVALID);
1722 LY_CHECK_RET(parse_type_enum_value_pos(ctx, data, kw, &enm->value, &enm->flags, &enm->exts));
1723 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001724 case LY_STMT_POSITION:
1725 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 +02001726 ly_stmt2str(enum_kw)), LY_EVALID);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001727 LY_CHECK_RET(parse_type_enum_value_pos(ctx, data, kw, &enm->value, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001728 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001729 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001730 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001731 break;
1732 default:
David Sedlákb3077192019-06-19 10:55:37 +02001733 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001734 return LY_EVALID;
1735 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001736 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001737 return ret;
1738}
1739
Michal Vaskoea5abea2018-09-18 13:10:54 +02001740/**
1741 * @brief Parse the fraction-digits statement. Substatement of type statement.
1742 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001743 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001744 * @param[in,out] data Data to read from, always moved to currently handled character.
1745 * @param[in,out] fracdig Value to write to.
1746 * @param[in,out] exts Extension instances to add to.
1747 *
1748 * @return LY_ERR values.
1749 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001750static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001751parse_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 +02001752{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001753 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001754 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001755 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001756 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02001757 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001758
1759 if (*fracdig) {
David Sedlákb3077192019-06-19 10:55:37 +02001760 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001761 return LY_EVALID;
1762 }
1763
1764 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001765 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001766
1767 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
David Sedlákb3077192019-06-19 10:55:37 +02001768 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001769 free(buf);
1770 return LY_EVALID;
1771 }
1772
1773 errno = 0;
1774 num = strtoul(word, &ptr, 10);
1775 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001776 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001777 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001778 free(buf);
1779 return LY_EVALID;
1780 }
1781 if ((errno == ERANGE) || (num > 18)) {
David Sedlákb3077192019-06-19 10:55:37 +02001782 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001783 free(buf);
1784 return LY_EVALID;
1785 }
1786 *fracdig = num;
1787 free(buf);
1788
Radek Krejci6d6556c2018-11-08 09:37:45 +01001789 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001790 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001791 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001792 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_FRACDIGITS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001793 break;
1794 default:
David Sedlákb3077192019-06-19 10:55:37 +02001795 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001796 return LY_EVALID;
1797 }
1798 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001799 return ret;
1800}
1801
Michal Vaskoea5abea2018-09-18 13:10:54 +02001802/**
1803 * @brief Parse the require-instance statement. Substatement of type statement.
1804 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001805 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001806 * @param[in,out] data Data to read from, always moved to currently handled character.
1807 * @param[in,out] reqinst Value to write to.
1808 * @param[in,out] flags Flags to write to.
1809 * @param[in,out] exts Extension instances to add to.
1810 *
1811 * @return LY_ERR values.
1812 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001813static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001814parse_type_reqinstance(struct lys_parser_ctx *ctx, const char **data, uint8_t *reqinst, uint16_t *flags,
Michal Vasko7fbc8162018-09-17 10:35:16 +02001815 struct lysp_ext_instance **exts)
1816{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001817 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001818 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001819 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001820 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001821
1822 if (*flags & LYS_SET_REQINST) {
David Sedlákb3077192019-06-19 10:55:37 +02001823 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001824 return LY_EVALID;
1825 }
1826 *flags |= LYS_SET_REQINST;
1827
1828 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001829 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001830
1831 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1832 *reqinst = 1;
1833 } else if ((word_len != 5) || strncmp(word, "false", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001834 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001835 free(buf);
1836 return LY_EVALID;
1837 }
1838 free(buf);
1839
Radek Krejci6d6556c2018-11-08 09:37:45 +01001840 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001841 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001842 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001843 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_REQINSTANCE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001844 break;
1845 default:
David Sedlákb3077192019-06-19 10:55:37 +02001846 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001847 return LY_EVALID;
1848 }
1849 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001850 return ret;
1851}
1852
Michal Vaskoea5abea2018-09-18 13:10:54 +02001853/**
1854 * @brief Parse the modifier statement. Substatement of type pattern statement.
1855 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001856 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001857 * @param[in,out] data Data to read from, always moved to currently handled character.
1858 * @param[in,out] pat Value to write to.
1859 * @param[in,out] exts Extension instances to add to.
1860 *
1861 * @return LY_ERR values.
1862 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001863static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001864parse_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 +02001865{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001866 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001867 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001868 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001869 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001870
1871 if ((*pat)[0] == 0x15) {
David Sedlákb3077192019-06-19 10:55:37 +02001872 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001873 return LY_EVALID;
1874 }
1875
1876 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001877 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001878
1879 if ((word_len != 12) || strncmp(word, "invert-match", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001880 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001881 free(buf);
1882 return LY_EVALID;
1883 }
1884 free(buf);
1885
1886 /* replace the value in the dictionary */
1887 buf = malloc(strlen(*pat) + 1);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001888 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001889 strcpy(buf, *pat);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001890 lydict_remove(ctx->ctx, *pat);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001891
1892 assert(buf[0] == 0x06);
1893 buf[0] = 0x15;
Radek Krejci44ceedc2018-10-02 15:54:31 +02001894 *pat = lydict_insert_zc(ctx->ctx, buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001895
Radek Krejci6d6556c2018-11-08 09:37:45 +01001896 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001897 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001898 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001899 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MODIFIER, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001900 break;
1901 default:
David Sedlákb3077192019-06-19 10:55:37 +02001902 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001903 return LY_EVALID;
1904 }
1905 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001906 return ret;
1907}
1908
Michal Vaskoea5abea2018-09-18 13:10:54 +02001909/**
1910 * @brief Parse the pattern statement. Substatement of type statement.
1911 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001912 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001913 * @param[in,out] data Data to read from, always moved to currently handled character.
1914 * @param[in,out] patterns Restrictions to add to.
1915 *
1916 * @return LY_ERR values.
1917 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001918static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001919parse_type_pattern(struct lys_parser_ctx *ctx, const char **data, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001920{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001921 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001922 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001923 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001924 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001925 struct lysp_restr *restr;
1926
Radek Krejci2c4e7172018-10-19 15:56:26 +02001927 LY_ARRAY_NEW_RET(ctx->ctx, *patterns, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001928
1929 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001930 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001931
1932 /* add special meaning first byte */
1933 if (buf) {
1934 buf = realloc(buf, word_len + 2);
1935 word = buf;
1936 } else {
1937 buf = malloc(word_len + 2);
1938 }
Radek Krejci44ceedc2018-10-02 15:54:31 +02001939 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejci86d106e2018-10-18 09:53:19 +02001940 memmove(buf + 1, word, word_len);
1941 buf[0] = 0x06; /* pattern's default regular-match flag */
1942 buf[word_len + 1] = '\0'; /* terminating NULL byte */
1943 restr->arg = lydict_insert_zc(ctx->ctx, buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001944
Radek Krejci6d6556c2018-11-08 09:37:45 +01001945 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001946 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001947 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001948 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 +02001949 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001950 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001951 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 +02001952 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001953 case LY_STMT_ERROR_APP_TAG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001954 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 +02001955 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001956 case LY_STMT_ERROR_MESSAGE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001957 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 +02001958 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001959 case LY_STMT_MODIFIER:
Radek Krejci335332a2019-09-05 13:03:35 +02001960 PARSER_CHECK_STMTVER2_RET(ctx, "modifier", "pattern");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001961 LY_CHECK_RET(parse_type_pattern_modifier(ctx, data, &restr->arg, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001962 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001963 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001964 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001965 break;
1966 default:
David Sedlákb3077192019-06-19 10:55:37 +02001967 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001968 return LY_EVALID;
1969 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001970 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001971 return ret;
1972}
1973
Michal Vaskoea5abea2018-09-18 13:10:54 +02001974/**
1975 * @brief Parse the type statement.
1976 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001977 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001978 * @param[in,out] data Data to read from, always moved to currently handled character.
1979 * @param[in,out] type Type to wrote to.
1980 *
1981 * @return LY_ERR values.
1982 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001983static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001984parse_type(struct lys_parser_ctx *ctx, const char **data, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001985{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001986 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001987 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001988 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001989 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001990 struct lysp_type *nest_type;
1991
1992 if (type->name) {
David Sedlákb3077192019-06-19 10:55:37 +02001993 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001994 return LY_EVALID;
1995 }
1996
1997 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001998 LY_CHECK_RET(get_argument(ctx, data, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02001999 INSERT_WORD(ctx, buf, type->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002000
Radek Krejci6d6556c2018-11-08 09:37:45 +01002001 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002002 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002003 case LY_STMT_BASE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002004 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 +01002005 type->flags |= LYS_SET_BASE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002006 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002007 case LY_STMT_BIT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002008 LY_CHECK_RET(parse_type_enum(ctx, data, kw, &type->bits));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002009 type->flags |= LYS_SET_BIT;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002010 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002011 case LY_STMT_ENUM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002012 LY_CHECK_RET(parse_type_enum(ctx, data, kw, &type->enums));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002013 type->flags |= LYS_SET_ENUM;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002014 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002015 case LY_STMT_FRACTION_DIGITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002016 LY_CHECK_RET(parse_type_fracdigits(ctx, data, &type->fraction_digits, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002017 type->flags |= LYS_SET_FRDIGITS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002018 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002019 case LY_STMT_LENGTH:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002020 if (type->length) {
David Sedlákb3077192019-06-19 10:55:37 +02002021 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002022 return LY_EVALID;
2023 }
2024 type->length = calloc(1, sizeof *type->length);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002025 LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002026
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002027 LY_CHECK_RET(parse_restr(ctx, data, kw, type->length));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002028 type->flags |= LYS_SET_LENGTH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002029 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002030 case LY_STMT_PATH:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002031 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 +01002032 type->flags |= LYS_SET_PATH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002033 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002034 case LY_STMT_PATTERN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002035 LY_CHECK_RET(parse_type_pattern(ctx, data, &type->patterns));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002036 type->flags |= LYS_SET_PATTERN;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002037 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002038 case LY_STMT_RANGE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002039 if (type->range) {
David Sedlákb3077192019-06-19 10:55:37 +02002040 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002041 return LY_EVALID;
2042 }
2043 type->range = calloc(1, sizeof *type->range);
David Sedlák7a8b2472019-07-11 15:08:34 +02002044 LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002045
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002046 LY_CHECK_RET(parse_restr(ctx, data, kw, type->range));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002047 type->flags |= LYS_SET_RANGE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002048 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002049 case LY_STMT_REQUIRE_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002050 LY_CHECK_RET(parse_type_reqinstance(ctx, data, &type->require_instance, &type->flags, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002051 /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002052 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002053 case LY_STMT_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002054 LY_ARRAY_NEW_RET(ctx->ctx, type->types, nest_type, LY_EMEM);
2055 LY_CHECK_RET(parse_type(ctx, data, nest_type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002056 type->flags |= LYS_SET_TYPE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002057 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002058 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002059 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &type->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002060 break;
2061 default:
David Sedlákb3077192019-06-19 10:55:37 +02002062 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002063 return LY_EVALID;
2064 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002065 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002066 return ret;
2067}
2068
Michal Vaskoea5abea2018-09-18 13:10:54 +02002069/**
2070 * @brief Parse the leaf statement.
2071 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002072 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002073 * @param[in,out] data Data to read from, always moved to currently handled character.
2074 * @param[in,out] siblings Siblings to add to.
2075 *
2076 * @return LY_ERR values.
2077 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002078LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002079parse_leaf(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002080{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002081 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002082 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002083 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002084 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002085 struct lysp_node_leaf *leaf;
2086
David Sedlák60adc092019-08-06 15:57:02 +02002087 /* create new leaf structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02002088 LY_LIST_NEW_RET(ctx->ctx, siblings, leaf, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002089 leaf->nodetype = LYS_LEAF;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002090 leaf->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002091
Michal Vasko7fbc8162018-09-17 10:35:16 +02002092 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002093 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002094 INSERT_WORD(ctx, buf, leaf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002095
2096 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01002097 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002098 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002099 case LY_STMT_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002100 LY_CHECK_RET(parse_config(ctx, data, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002101 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002102 case LY_STMT_DEFAULT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002103 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 +02002104 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002105 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002106 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 +02002107 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002108 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002109 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 +02002110 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002111 case LY_STMT_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002112 LY_CHECK_RET(parse_mandatory(ctx, data, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002113 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002114 case LY_STMT_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002115 LY_CHECK_RET(parse_restrs(ctx, data, kw, &leaf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002116 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002117 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002118 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 +02002119 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002120 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002121 LY_CHECK_RET(parse_status(ctx, data, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002122 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002123 case LY_STMT_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002124 LY_CHECK_RET(parse_type(ctx, data, &leaf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002125 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002126 case LY_STMT_UNITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002127 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 +02002128 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002129 case LY_STMT_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002130 LY_CHECK_RET(parse_when(ctx, data, &leaf->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002131 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002132 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002133 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002134 break;
2135 default:
David Sedlákb3077192019-06-19 10:55:37 +02002136 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002137 return LY_EVALID;
2138 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002139 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002140 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002141checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002142 /* mandatory substatements */
2143 if (!leaf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002144 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002145 return LY_EVALID;
2146 }
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002147 if ((leaf->flags & LYS_MAND_TRUE) && (leaf->dflt)) {
David Sedlákb3077192019-06-19 10:55:37 +02002148 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMSCOMB, "mandatory", "default", "leaf");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002149 return LY_EVALID;
2150 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002151
2152 return ret;
2153}
2154
Michal Vaskoea5abea2018-09-18 13:10:54 +02002155/**
2156 * @brief Parse the max-elements statement.
2157 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002158 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002159 * @param[in,out] data Data to read from, always moved to currently handled character.
2160 * @param[in,out] max Value to write to.
2161 * @param[in,out] flags Flags to write to.
2162 * @param[in,out] exts Extension instances to add to.
2163 *
2164 * @return LY_ERR values.
2165 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002166LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002167parse_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 +02002168{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002169 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002170 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002171 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002172 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002173 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002174
2175 if (*flags & LYS_SET_MAX) {
David Sedlákb3077192019-06-19 10:55:37 +02002176 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002177 return LY_EVALID;
2178 }
2179 *flags |= LYS_SET_MAX;
2180
2181 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002182 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002183
2184 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
David Sedlákb3077192019-06-19 10:55:37 +02002185 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002186 free(buf);
2187 return LY_EVALID;
2188 }
2189
Radek Krejci7f9b6512019-09-18 13:11:09 +02002190 if (ly_strncmp("unbounded", word, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002191 errno = 0;
2192 num = strtoul(word, &ptr, 10);
2193 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002194 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002195 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002196 free(buf);
2197 return LY_EVALID;
2198 }
2199 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002200 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002201 free(buf);
2202 return LY_EVALID;
2203 }
2204
2205 *max = num;
2206 }
2207 free(buf);
2208
Radek Krejci6d6556c2018-11-08 09:37:45 +01002209 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002210 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002211 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002212 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MAX, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002213 break;
2214 default:
David Sedlákb3077192019-06-19 10:55:37 +02002215 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002216 return LY_EVALID;
2217 }
2218 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002219 return ret;
2220}
2221
Michal Vaskoea5abea2018-09-18 13:10:54 +02002222/**
2223 * @brief Parse the min-elements statement.
2224 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002225 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002226 * @param[in,out] data Data to read from, always moved to currently handled character.
2227 * @param[in,out] min Value to write to.
2228 * @param[in,out] flags Flags to write to.
2229 * @param[in,out] exts Extension instances to add to.
2230 *
2231 * @return LY_ERR values.
2232 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002233LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002234parse_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 +02002235{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002236 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002237 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002238 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002239 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002240 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002241
2242 if (*flags & LYS_SET_MIN) {
David Sedlákb3077192019-06-19 10:55:37 +02002243 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002244 return LY_EVALID;
2245 }
2246 *flags |= LYS_SET_MIN;
2247
2248 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002249 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002250
2251 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
David Sedlákb3077192019-06-19 10:55:37 +02002252 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002253 free(buf);
2254 return LY_EVALID;
2255 }
2256
2257 errno = 0;
2258 num = strtoul(word, &ptr, 10);
2259 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002260 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002261 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002262 free(buf);
2263 return LY_EVALID;
2264 }
2265 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002266 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002267 free(buf);
2268 return LY_EVALID;
2269 }
2270 *min = num;
2271 free(buf);
2272
Radek Krejci6d6556c2018-11-08 09:37:45 +01002273 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002274 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002275 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002276 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MIN, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002277 break;
2278 default:
David Sedlákb3077192019-06-19 10:55:37 +02002279 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002280 return LY_EVALID;
2281 }
2282 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002283 return ret;
2284}
2285
Michal Vaskoea5abea2018-09-18 13:10:54 +02002286/**
2287 * @brief Parse the ordered-by statement.
2288 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002289 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002290 * @param[in,out] data Data to read from, always moved to currently handled character.
2291 * @param[in,out] flags Flags to write to.
2292 * @param[in,out] exts Extension instances to add to.
2293 *
2294 * @return LY_ERR values.
2295 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002296static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002297parse_orderedby(struct lys_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002298{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002299 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002300 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002301 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002302 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002303
2304 if (*flags & LYS_ORDBY_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02002305 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002306 return LY_EVALID;
2307 }
2308
2309 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002310 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002311
2312 if ((word_len == 6) && !strncmp(word, "system", word_len)) {
2313 *flags |= LYS_ORDBY_SYSTEM;
2314 } else if ((word_len == 4) && !strncmp(word, "user", word_len)) {
2315 *flags |= LYS_ORDBY_USER;
2316 } else {
David Sedlákb3077192019-06-19 10:55:37 +02002317 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002318 free(buf);
2319 return LY_EVALID;
2320 }
2321 free(buf);
2322
Radek Krejci6d6556c2018-11-08 09:37:45 +01002323 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002324 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002325 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002326 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_ORDEREDBY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002327 break;
2328 default:
David Sedlákb3077192019-06-19 10:55:37 +02002329 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002330 return LY_EVALID;
2331 }
2332 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002333 return ret;
2334}
2335
Michal Vaskoea5abea2018-09-18 13:10:54 +02002336/**
2337 * @brief Parse the leaf-list statement.
2338 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002339 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002340 * @param[in,out] data Data to read from, always moved to currently handled character.
2341 * @param[in,out] siblings Siblings to add to.
2342 *
2343 * @return LY_ERR values.
2344 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002345LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002346parse_leaflist(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002347{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002348 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002349 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002350 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002351 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002352 struct lysp_node_leaflist *llist;
2353
David Sedlák60adc092019-08-06 15:57:02 +02002354 /* create new leaf-list structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02002355 LY_LIST_NEW_RET(ctx->ctx, siblings, llist, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002356 llist->nodetype = LYS_LEAFLIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002357 llist->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002358
Michal Vasko7fbc8162018-09-17 10:35:16 +02002359 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002360 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002361 INSERT_WORD(ctx, buf, llist->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002362
2363 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01002364 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002365 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002366 case LY_STMT_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002367 LY_CHECK_RET(parse_config(ctx, data, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002368 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002369 case LY_STMT_DEFAULT:
Radek Krejci335332a2019-09-05 13:03:35 +02002370 PARSER_CHECK_STMTVER2_RET(ctx, "default", "leaf-list");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002371 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 +02002372 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002373 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002374 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 +02002375 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002376 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002377 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 +02002378 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002379 case LY_STMT_MAX_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002380 LY_CHECK_RET(parse_maxelements(ctx, data, &llist->max, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002381 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002382 case LY_STMT_MIN_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002383 LY_CHECK_RET(parse_minelements(ctx, data, &llist->min, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002384 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002385 case LY_STMT_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002386 LY_CHECK_RET(parse_restrs(ctx, data, kw, &llist->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002387 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002388 case LY_STMT_ORDERED_BY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002389 LY_CHECK_RET(parse_orderedby(ctx, data, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002390 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002391 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002392 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 +02002393 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002394 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002395 LY_CHECK_RET(parse_status(ctx, data, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002396 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002397 case LY_STMT_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002398 LY_CHECK_RET(parse_type(ctx, data, &llist->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002399 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002400 case LY_STMT_UNITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002401 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 +02002402 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002403 case LY_STMT_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002404 LY_CHECK_RET(parse_when(ctx, data, &llist->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002405 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002406 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002407 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002408 break;
2409 default:
David Sedlákb3077192019-06-19 10:55:37 +02002410 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002411 return LY_EVALID;
2412 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002413 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002414 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002415checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002416 /* mandatory substatements */
2417 if (!llist->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002418 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002419 return LY_EVALID;
2420 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01002421 if ((llist->min) && (llist->dflts)) {
David Sedlákb3077192019-06-19 10:55:37 +02002422 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMSCOMB, "min-elements", "default", "leaf-list");
Radek Krejci0e5d8382018-11-28 16:37:53 +01002423 return LY_EVALID;
2424 }
Radek Krejcidf6cad12018-11-28 17:10:55 +01002425 if (llist->max && llist->min > llist->max) {
David Sedlákb3077192019-06-19 10:55:37 +02002426 LOGVAL_PARSER(ctx, LYVE_SEMANTICS,
Radek Krejcidf6cad12018-11-28 17:10:55 +01002427 "Invalid combination of min-elements and max-elements: min value %u is bigger than the max value %u.",
2428 llist->min, llist->max);
2429 return LY_EVALID;
2430 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002431
2432 return ret;
2433}
2434
Michal Vaskoea5abea2018-09-18 13:10:54 +02002435/**
2436 * @brief Parse the refine statement.
2437 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002438 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002439 * @param[in,out] data Data to read from, always moved to currently handled character.
2440 * @param[in,out] refines Refines to add to.
2441 *
2442 * @return LY_ERR values.
2443 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002444static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002445parse_refine(struct lys_parser_ctx *ctx, const char **data, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002446{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002447 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002448 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002449 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002450 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002451 struct lysp_refine *rf;
2452
Radek Krejci2c4e7172018-10-19 15:56:26 +02002453 LY_ARRAY_NEW_RET(ctx->ctx, *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002454
2455 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002456 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
David Sedlákb9b892c2019-07-12 14:44:02 +02002457 YANG_CHECK_NONEMPTY(ctx, word_len, "refine");
Radek Krejci44ceedc2018-10-02 15:54:31 +02002458 INSERT_WORD(ctx, buf, rf->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002459
Radek Krejci6d6556c2018-11-08 09:37:45 +01002460 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002461 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002462 case LY_STMT_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002463 LY_CHECK_RET(parse_config(ctx, data, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002464 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002465 case LY_STMT_DEFAULT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002466 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 +02002467 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002468 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002469 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 +02002470 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002471 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02002472 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "refine");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002473 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 +02002474 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002475 case LY_STMT_MAX_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002476 LY_CHECK_RET(parse_maxelements(ctx, data, &rf->max, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002477 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002478 case LY_STMT_MIN_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002479 LY_CHECK_RET(parse_minelements(ctx, data, &rf->min, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002480 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002481 case LY_STMT_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002482 LY_CHECK_RET(parse_restrs(ctx, data, kw, &rf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002483 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002484 case LY_STMT_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002485 LY_CHECK_RET(parse_mandatory(ctx, data, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002486 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002487 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002488 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 +02002489 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002490 case LY_STMT_PRESENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002491 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 +02002492 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002493 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002494 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002495 break;
2496 default:
David Sedlákb3077192019-06-19 10:55:37 +02002497 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002498 return LY_EVALID;
2499 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002500 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002501 return ret;
2502}
2503
Michal Vaskoea5abea2018-09-18 13:10:54 +02002504/**
2505 * @brief Parse the typedef statement.
2506 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002507 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002508 * @param[in,out] data Data to read from, always moved to currently handled character.
2509 * @param[in,out] typedefs Typedefs to add to.
2510 *
2511 * @return LY_ERR values.
2512 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002513static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002514parse_typedef(struct lys_parser_ctx *ctx, struct lysp_node *parent, const char **data, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002515{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002516 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002517 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002518 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002519 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002520 struct lysp_tpdf *tpdf;
2521
Radek Krejci2c4e7172018-10-19 15:56:26 +02002522 LY_ARRAY_NEW_RET(ctx->ctx, *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002523
2524 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002525 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002526 INSERT_WORD(ctx, buf, tpdf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002527
2528 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01002529 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002530 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002531 case LY_STMT_DEFAULT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002532 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 +02002533 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002534 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002535 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 +02002536 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002537 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002538 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 +02002539 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002540 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002541 LY_CHECK_RET(parse_status(ctx, data, &tpdf->flags, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002542 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002543 case LY_STMT_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002544 LY_CHECK_RET(parse_type(ctx, data, &tpdf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002545 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002546 case LY_STMT_UNITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002547 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 +02002548 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002549 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002550 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002551 break;
2552 default:
David Sedlákb3077192019-06-19 10:55:37 +02002553 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002554 return LY_EVALID;
2555 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002556 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002557 LY_CHECK_RET(ret);
Radek Krejcibbe09a92018-11-08 09:36:54 +01002558checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002559 /* mandatory substatements */
2560 if (!tpdf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002561 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002562 return LY_EVALID;
2563 }
2564
Radek Krejcibbe09a92018-11-08 09:36:54 +01002565 /* store data for collision check */
Radek Krejci7fc68292019-06-12 13:51:09 +02002566 if (parent && !(parent->nodetype & (LYS_GROUPING | LYS_ACTION | LYS_INOUT | LYS_NOTIF))) {
David Sedláke8b74df2019-08-14 14:18:22 +02002567 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, parent, 0) == -1, LY_EMEM);
Radek Krejcibbe09a92018-11-08 09:36:54 +01002568 }
2569
Michal Vasko7fbc8162018-09-17 10:35:16 +02002570 return ret;
2571}
2572
Michal Vaskoea5abea2018-09-18 13:10:54 +02002573/**
2574 * @brief Parse the input or output statement.
2575 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002576 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002577 * @param[in,out] data Data to read from, always moved to currently handled character.
2578 * @param[in] kw Type of this particular keyword
2579 * @param[in,out] inout_p Input/output pointer to write to.
2580 *
2581 * @return LY_ERR values.
2582 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002583static LY_ERR
Radek Krejcid6b76452019-09-03 17:03:03 +02002584parse_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 +02002585{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002586 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002587 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002588 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002589 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002590
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002591 if (inout_p->nodetype) {
David Sedlákb3077192019-06-19 10:55:37 +02002592 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002593 return LY_EVALID;
2594 }
2595
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002596 /* initiate structure */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002597 inout_p->nodetype = &((struct lysp_action*)parent)->input == inout_p ? LYS_INPUT : LYS_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002598 inout_p->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002599
2600 /* parse substatements */
Radek Krejci7fc68292019-06-12 13:51:09 +02002601 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002602 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002603 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002604 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", ly_stmt2str(inout_kw));
Radek Krejci10113652018-11-14 16:56:50 +01002605 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002606 case LY_STMT_ANYXML:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002607 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002608 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002609 case LY_STMT_CHOICE:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002610 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002611 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002612 case LY_STMT_CONTAINER:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002613 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002614 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002615 case LY_STMT_LEAF:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002616 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002617 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002618 case LY_STMT_LEAF_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002619 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002620 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002621 case LY_STMT_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002622 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002623 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002624 case LY_STMT_USES:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002625 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002626 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002627 case LY_STMT_TYPEDEF:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002628 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)inout_p, data, &inout_p->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002629 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002630 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002631 PARSER_CHECK_STMTVER2_RET(ctx, "must", ly_stmt2str(inout_kw));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002632 LY_CHECK_RET(parse_restrs(ctx, data, kw, &inout_p->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002633 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002634 case LY_STMT_GROUPING:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002635 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)inout_p, &inout_p->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002636 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002637 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002638 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inout_p->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002639 break;
2640 default:
David Sedlákb3077192019-06-19 10:55:37 +02002641 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002642 return LY_EVALID;
2643 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002644 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002645 LY_CHECK_RET(ret);
2646checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002647 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02002648 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, inout_p->groupings, NULL, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02002649
Michal Vasko7fbc8162018-09-17 10:35:16 +02002650 return ret;
2651}
2652
Michal Vaskoea5abea2018-09-18 13:10:54 +02002653/**
2654 * @brief Parse the action statement.
2655 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002656 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002657 * @param[in,out] data Data to read from, always moved to currently handled character.
2658 * @param[in,out] actions Actions to add to.
2659 *
2660 * @return LY_ERR values.
2661 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002662LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002663parse_action(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002664{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002665 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002666 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002667 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002668 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002669 struct lysp_action *act;
2670
Radek Krejci2c4e7172018-10-19 15:56:26 +02002671 LY_ARRAY_NEW_RET(ctx->ctx, *actions, act, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002672
2673 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002674 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002675 INSERT_WORD(ctx, buf, act->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002676 act->nodetype = LYS_ACTION;
2677 act->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002678
Radek Krejci7fc68292019-06-12 13:51:09 +02002679 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002680 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002681 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002682 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 +02002683 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002684 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002685 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 +02002686 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002687 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002688 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 +02002689 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002690 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002691 LY_CHECK_RET(parse_status(ctx, data, &act->flags, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002692 break;
2693
Radek Krejcid6b76452019-09-03 17:03:03 +02002694 case LY_STMT_INPUT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002695 LY_CHECK_RET(parse_inout(ctx, data, kw, (struct lysp_node*)act, &act->input));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002696 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002697 case LY_STMT_OUTPUT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002698 LY_CHECK_RET(parse_inout(ctx, data, kw, (struct lysp_node*)act, &act->output));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002699 break;
2700
Radek Krejcid6b76452019-09-03 17:03:03 +02002701 case LY_STMT_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01002702 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)act, data, &act->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002703 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002704 case LY_STMT_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002705 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)act, &act->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002706 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002707 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002708 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002709 break;
2710 default:
David Sedlákb3077192019-06-19 10:55:37 +02002711 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), parent ? "action" : "rpc");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002712 return LY_EVALID;
2713 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002714 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002715 LY_CHECK_RET(ret);
2716checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002717 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02002718 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, act->groupings, NULL, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02002719
Michal Vasko7fbc8162018-09-17 10:35:16 +02002720 return ret;
2721}
2722
Michal Vaskoea5abea2018-09-18 13:10:54 +02002723/**
2724 * @brief Parse the notification statement.
2725 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002726 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002727 * @param[in,out] data Data to read from, always moved to currently handled character.
2728 * @param[in,out] notifs Notifications to add to.
2729 *
2730 * @return LY_ERR values.
2731 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002732LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002733parse_notif(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002734{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002735 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002736 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002737 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002738 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002739 struct lysp_notif *notif;
2740
Radek Krejci2c4e7172018-10-19 15:56:26 +02002741 LY_ARRAY_NEW_RET(ctx->ctx, *notifs, notif, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002742
2743 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002744 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002745 INSERT_WORD(ctx, buf, notif->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002746 notif->nodetype = LYS_NOTIF;
2747 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002748
Radek Krejci7fc68292019-06-12 13:51:09 +02002749 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002750 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002751 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002752 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 +02002753 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002754 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002755 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 +02002756 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002757 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002758 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 +02002759 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002760 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002761 LY_CHECK_RET(parse_status(ctx, data, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002762 break;
2763
Radek Krejcid6b76452019-09-03 17:03:03 +02002764 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002765 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "notification");
Radek Krejci10113652018-11-14 16:56:50 +01002766 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002767 case LY_STMT_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002768 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002769 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002770 case LY_STMT_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002771 LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002772 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002773 case LY_STMT_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002774 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002775 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002776 case LY_STMT_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002777 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002778 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002779 case LY_STMT_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002780 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002781 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002782 case LY_STMT_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002783 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002784 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002785 case LY_STMT_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002786 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002787 break;
2788
Radek Krejcid6b76452019-09-03 17:03:03 +02002789 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002790 PARSER_CHECK_STMTVER2_RET(ctx, "must", "notification");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002791 LY_CHECK_RET(parse_restrs(ctx, data, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002792 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002793 case LY_STMT_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01002794 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)notif, data, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002795 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002796 case LY_STMT_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002797 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002798 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002799 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002800 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002801 break;
2802 default:
David Sedlákb3077192019-06-19 10:55:37 +02002803 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002804 return LY_EVALID;
2805 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002806 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002807 LY_CHECK_RET(ret);
2808checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002809 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02002810 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, notif->groupings, NULL, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02002811
Michal Vasko7fbc8162018-09-17 10:35:16 +02002812 return ret;
2813}
2814
Michal Vaskoea5abea2018-09-18 13:10:54 +02002815/**
2816 * @brief Parse the grouping statement.
2817 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002818 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002819 * @param[in,out] data Data to read from, always moved to currently handled character.
2820 * @param[in,out] groupings Groupings to add to.
2821 *
2822 * @return LY_ERR values.
2823 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002824LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002825parse_grouping(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002826{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002827 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002828 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002829 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002830 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002831 struct lysp_grp *grp;
2832
Radek Krejci2c4e7172018-10-19 15:56:26 +02002833 LY_ARRAY_NEW_RET(ctx->ctx, *groupings, grp, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002834
2835 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002836 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002837 INSERT_WORD(ctx, buf, grp->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002838 grp->nodetype = LYS_GROUPING;
2839 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002840
Radek Krejci7fc68292019-06-12 13:51:09 +02002841 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002842 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002843 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002844 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 +02002845 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002846 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002847 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 +02002848 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002849 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002850 LY_CHECK_RET(parse_status(ctx, data, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002851 break;
2852
Radek Krejcid6b76452019-09-03 17:03:03 +02002853 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002854 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "grouping");
Radek Krejci10113652018-11-14 16:56:50 +01002855 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002856 case LY_STMT_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002857 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002858 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002859 case LY_STMT_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002860 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002861 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002862 case LY_STMT_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002863 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002864 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002865 case LY_STMT_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002866 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002867 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002868 case LY_STMT_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002869 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002870 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002871 case LY_STMT_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002872 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002873 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002874 case LY_STMT_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002875 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002876 break;
2877
Radek Krejcid6b76452019-09-03 17:03:03 +02002878 case LY_STMT_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01002879 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)grp, data, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002880 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002881 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02002882 PARSER_CHECK_STMTVER2_RET(ctx, "action", "grouping");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002883 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)grp, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002884 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002885 case LY_STMT_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002886 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)grp, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002887 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002888 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02002889 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "grouping");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002890 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)grp, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002891 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002892 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002893 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002894 break;
2895 default:
David Sedlákb3077192019-06-19 10:55:37 +02002896 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002897 return LY_EVALID;
2898 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002899 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002900 LY_CHECK_RET(ret);
2901checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002902 /* finalize parent pointers to the reallocated items */
David Sedlák0d6de5a2019-07-22 13:25:44 +02002903 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, grp->groupings, NULL, grp->actions, grp->notifs));
Radek Krejci7fc68292019-06-12 13:51:09 +02002904
Michal Vasko7fbc8162018-09-17 10:35:16 +02002905 return ret;
2906}
2907
Michal Vaskoea5abea2018-09-18 13:10:54 +02002908/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02002909 * @brief Parse the augment statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002910 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002911 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002912 * @param[in,out] data Data to read from, always moved to currently handled character.
2913 * @param[in,out] augments Augments to add to.
2914 *
2915 * @return LY_ERR values.
2916 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002917LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002918parse_augment(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002919{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002920 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002921 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002922 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002923 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002924 struct lysp_augment *aug;
2925
Radek Krejci2c4e7172018-10-19 15:56:26 +02002926 LY_ARRAY_NEW_RET(ctx->ctx, *augments, aug, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002927
2928 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002929 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
David Sedlákb9b892c2019-07-12 14:44:02 +02002930 YANG_CHECK_NONEMPTY(ctx, word_len, "augment");
Radek Krejci44ceedc2018-10-02 15:54:31 +02002931 INSERT_WORD(ctx, buf, aug->nodeid, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002932 aug->nodetype = LYS_AUGMENT;
2933 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002934
Radek Krejci7fc68292019-06-12 13:51:09 +02002935 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002936 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002937 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002938 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 +02002939 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002940 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002941 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 +02002942 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002943 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002944 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 +02002945 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002946 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002947 LY_CHECK_RET(parse_status(ctx, data, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002948 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002949 case LY_STMT_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002950 LY_CHECK_RET(parse_when(ctx, data, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002951 break;
2952
Radek Krejcid6b76452019-09-03 17:03:03 +02002953 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002954 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "augment");
Radek Krejci10113652018-11-14 16:56:50 +01002955 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002956 case LY_STMT_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002957 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002958 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002959 case LY_STMT_CASE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002960 LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002961 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002962 case LY_STMT_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002963 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002964 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002965 case LY_STMT_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002966 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002967 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002968 case LY_STMT_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002969 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002970 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002971 case LY_STMT_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002972 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002973 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002974 case LY_STMT_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002975 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002976 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002977 case LY_STMT_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002978 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002979 break;
2980
Radek Krejcid6b76452019-09-03 17:03:03 +02002981 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02002982 PARSER_CHECK_STMTVER2_RET(ctx, "action", "augment");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002983 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002984 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002985 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02002986 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "augment");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002987 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002988 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002989 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002990 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002991 break;
2992 default:
David Sedlákb3077192019-06-19 10:55:37 +02002993 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002994 return LY_EVALID;
2995 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002996 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002997 LY_CHECK_RET(ret);
2998checks:
2999 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02003000 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, NULL, NULL, aug->actions, aug->notifs));
Radek Krejci7fc68292019-06-12 13:51:09 +02003001
Michal Vasko7fbc8162018-09-17 10:35:16 +02003002 return ret;
3003}
3004
Michal Vaskoea5abea2018-09-18 13:10:54 +02003005/**
3006 * @brief Parse the uses statement.
3007 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003008 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003009 * @param[in,out] data Data to read from, always moved to currently handled character.
3010 * @param[in,out] siblings Siblings to add to.
3011 *
3012 * @return LY_ERR values.
3013 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003014LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003015parse_uses(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003016{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003017 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003018 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003019 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003020 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003021 struct lysp_node_uses *uses;
3022
David Sedlák60adc092019-08-06 15:57:02 +02003023 /* create uses structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003024 LY_LIST_NEW_RET(ctx->ctx, siblings, uses, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003025 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003026 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003027
Michal Vasko7fbc8162018-09-17 10:35:16 +02003028 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003029 LY_CHECK_RET(get_argument(ctx, data, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003030 INSERT_WORD(ctx, buf, uses->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003031
3032 /* parse substatements */
Radek Krejci7fc68292019-06-12 13:51:09 +02003033 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003034 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003035 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003036 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 +02003037 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003038 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003039 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 +02003040 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003041 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003042 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 +02003043 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003044 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003045 LY_CHECK_RET(parse_status(ctx, data, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003046 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003047 case LY_STMT_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003048 LY_CHECK_RET(parse_when(ctx, data, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003049 break;
3050
Radek Krejcid6b76452019-09-03 17:03:03 +02003051 case LY_STMT_REFINE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003052 LY_CHECK_RET(parse_refine(ctx, data, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003053 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003054 case LY_STMT_AUGMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003055 LY_CHECK_RET(parse_augment(ctx, data, (struct lysp_node*)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003056 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003057 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003058 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003059 break;
3060 default:
David Sedlákb3077192019-06-19 10:55:37 +02003061 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003062 return LY_EVALID;
3063 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003064 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003065checks:
3066 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02003067 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, NULL, uses->augments, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02003068
Michal Vasko7fbc8162018-09-17 10:35:16 +02003069 return ret;
3070}
3071
Michal Vaskoea5abea2018-09-18 13:10:54 +02003072/**
3073 * @brief Parse the case statement.
3074 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003075 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003076 * @param[in,out] data Data to read from, always moved to currently handled character.
3077 * @param[in,out] siblings Siblings to add to.
3078 *
3079 * @return LY_ERR values.
3080 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003081LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003082parse_case(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003083{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003084 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003085 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003086 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003087 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003088 struct lysp_node_case *cas;
3089
David Sedlák60adc092019-08-06 15:57:02 +02003090 /* create new case structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003091 LY_LIST_NEW_RET(ctx->ctx, siblings, cas, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003092 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003093 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003094
Michal Vasko7fbc8162018-09-17 10:35:16 +02003095 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003096 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003097 INSERT_WORD(ctx, buf, cas->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003098
3099 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01003100 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003101 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003102 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003103 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 +02003104 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003105 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003106 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 +02003107 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003108 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003109 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 +02003110 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003111 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003112 LY_CHECK_RET(parse_status(ctx, data, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003113 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003114 case LY_STMT_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003115 LY_CHECK_RET(parse_when(ctx, data, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003116 break;
3117
Radek Krejcid6b76452019-09-03 17:03:03 +02003118 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003119 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "case");
Radek Krejci10113652018-11-14 16:56:50 +01003120 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003121 case LY_STMT_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003122 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003123 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003124 case LY_STMT_CHOICE:
Radek Krejcia9026eb2018-12-12 16:04:47 +01003125 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003126 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003127 case LY_STMT_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003128 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003129 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003130 case LY_STMT_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003131 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003132 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003133 case LY_STMT_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003134 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003135 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003136 case LY_STMT_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003137 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003138 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003139 case LY_STMT_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003140 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003141 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003142 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003143 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003144 break;
3145 default:
David Sedlákb3077192019-06-19 10:55:37 +02003146 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003147 return LY_EVALID;
3148 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003149 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003150 return ret;
3151}
3152
Michal Vaskoea5abea2018-09-18 13:10:54 +02003153/**
3154 * @brief Parse the choice statement.
3155 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003156 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003157 * @param[in,out] data Data to read from, always moved to currently handled character.
3158 * @param[in,out] siblings Siblings to add to.
3159 *
3160 * @return LY_ERR values.
3161 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003162LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003163parse_choice(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003164{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003165 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003166 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003167 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003168 enum ly_stmt kw;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003169 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003170
David Sedlák60adc092019-08-06 15:57:02 +02003171 /* create new choice structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003172 LY_LIST_NEW_RET(ctx->ctx, siblings, choice, next, LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003173 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003174 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003175
Michal Vasko7fbc8162018-09-17 10:35:16 +02003176 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003177 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003178 INSERT_WORD(ctx, buf, choice->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003179
3180 /* parse substatements */
Radek Krejcia9026eb2018-12-12 16:04:47 +01003181 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003182 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003183 case LY_STMT_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003184 LY_CHECK_RET(parse_config(ctx, data, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003185 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003186 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003187 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 +02003188 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003189 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003190 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 +02003191 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003192 case LY_STMT_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003193 LY_CHECK_RET(parse_mandatory(ctx, data, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003194 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003195 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003196 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 +02003197 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003198 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003199 LY_CHECK_RET(parse_status(ctx, data, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003200 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003201 case LY_STMT_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003202 LY_CHECK_RET(parse_when(ctx, data, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003203 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003204 case LY_STMT_DEFAULT:
Radek Krejcia9026eb2018-12-12 16:04:47 +01003205 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 +02003206 break;
3207
Radek Krejcid6b76452019-09-03 17:03:03 +02003208 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003209 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "choice");
Radek Krejci10113652018-11-14 16:56:50 +01003210 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003211 case LY_STMT_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003212 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003213 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003214 case LY_STMT_CASE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003215 LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003216 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003217 case LY_STMT_CHOICE:
Radek Krejci335332a2019-09-05 13:03:35 +02003218 PARSER_CHECK_STMTVER2_RET(ctx, "choice", "choice");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003219 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003220 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003221 case LY_STMT_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003222 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003223 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003224 case LY_STMT_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003225 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003226 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003227 case LY_STMT_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003228 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003229 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003230 case LY_STMT_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003231 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003232 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003233 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003234 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003235 break;
3236 default:
David Sedlákb3077192019-06-19 10:55:37 +02003237 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003238 return LY_EVALID;
3239 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003240 }
Radek Krejcia9026eb2018-12-12 16:04:47 +01003241 LY_CHECK_RET(ret);
3242checks:
3243 if ((choice->flags & LYS_MAND_TRUE) && choice->dflt) {
David Sedlákb3077192019-06-19 10:55:37 +02003244 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMSCOMB, "mandatory", "default", "choice");
Radek Krejcia9026eb2018-12-12 16:04:47 +01003245 return LY_EVALID;
3246 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003247 return ret;
3248}
3249
Michal Vaskoea5abea2018-09-18 13:10:54 +02003250/**
3251 * @brief Parse the container statement.
3252 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003253 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003254 * @param[in,out] data Data to read from, always moved to currently handled character.
3255 * @param[in,out] siblings Siblings to add to.
3256 *
3257 * @return LY_ERR values.
3258 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003259LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003260parse_container(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003261{
3262 LY_ERR ret = 0;
3263 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003264 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003265 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003266 struct lysp_node_container *cont;
3267
David Sedlák60adc092019-08-06 15:57:02 +02003268 /* create new container structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003269 LY_LIST_NEW_RET(ctx->ctx, siblings, cont, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003270 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003271 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003272
Michal Vasko7fbc8162018-09-17 10:35:16 +02003273 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003274 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003275 INSERT_WORD(ctx, buf, cont->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003276
3277 /* parse substatements */
Radek Krejci7fc68292019-06-12 13:51:09 +02003278 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003279 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003280 case LY_STMT_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003281 LY_CHECK_RET(parse_config(ctx, data, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003282 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003283 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003284 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 +02003285 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003286 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003287 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 +02003288 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003289 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003290 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 +02003291 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003292 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003293 LY_CHECK_RET(parse_status(ctx, data, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003294 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003295 case LY_STMT_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003296 LY_CHECK_RET(parse_when(ctx, data, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003297 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003298 case LY_STMT_PRESENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003299 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 +02003300 break;
3301
Radek Krejcid6b76452019-09-03 17:03:03 +02003302 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003303 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "container");
Radek Krejci10113652018-11-14 16:56:50 +01003304 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003305 case LY_STMT_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003306 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003307 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003308 case LY_STMT_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003309 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003310 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003311 case LY_STMT_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003312 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003313 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003314 case LY_STMT_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003315 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003316 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003317 case LY_STMT_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003318 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003319 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003320 case LY_STMT_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003321 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003322 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003323 case LY_STMT_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003324 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003325 break;
3326
Radek Krejcid6b76452019-09-03 17:03:03 +02003327 case LY_STMT_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01003328 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)cont, data, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003329 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003330 case LY_STMT_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003331 LY_CHECK_RET(parse_restrs(ctx, data, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003332 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003333 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003334 PARSER_CHECK_STMTVER2_RET(ctx, "action", "container");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003335 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003336 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003337 case LY_STMT_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003338 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003339 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003340 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003341 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "container");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003342 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003343 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003344 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003345 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003346 break;
3347 default:
David Sedlákb3077192019-06-19 10:55:37 +02003348 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003349 return LY_EVALID;
3350 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003351 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003352checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01003353 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02003354 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, cont->groupings, NULL, cont->actions, cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003355 return ret;
3356}
3357
Michal Vaskoea5abea2018-09-18 13:10:54 +02003358/**
3359 * @brief Parse the list statement.
3360 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003361 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003362 * @param[in,out] data Data to read from, always moved to currently handled character.
3363 * @param[in,out] siblings Siblings to add to.
3364 *
3365 * @return LY_ERR values.
3366 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003367LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003368parse_list(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003369{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003370 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003371 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003372 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003373 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003374 struct lysp_node_list *list;
3375
David Sedlák60adc092019-08-06 15:57:02 +02003376 /* create new list structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003377 LY_LIST_NEW_RET(ctx->ctx, siblings, list, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003378 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003379 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003380
Michal Vasko7fbc8162018-09-17 10:35:16 +02003381 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003382 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003383 INSERT_WORD(ctx, buf, list->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003384
3385 /* parse substatements */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003386 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003387 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003388 case LY_STMT_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003389 LY_CHECK_RET(parse_config(ctx, data, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003390 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003391 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003392 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 +02003393 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003394 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003395 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 +02003396 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003397 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003398 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 +02003399 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003400 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003401 LY_CHECK_RET(parse_status(ctx, data, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003402 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003403 case LY_STMT_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003404 LY_CHECK_RET(parse_when(ctx, data, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003405 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003406 case LY_STMT_KEY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003407 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 +02003408 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003409 case LY_STMT_MAX_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003410 LY_CHECK_RET(parse_maxelements(ctx, data, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003411 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003412 case LY_STMT_MIN_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003413 LY_CHECK_RET(parse_minelements(ctx, data, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003414 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003415 case LY_STMT_ORDERED_BY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003416 LY_CHECK_RET(parse_orderedby(ctx, data, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003417 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003418 case LY_STMT_UNIQUE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003419 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 +02003420 break;
3421
Radek Krejcid6b76452019-09-03 17:03:03 +02003422 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003423 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "list");
Radek Krejci10113652018-11-14 16:56:50 +01003424 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003425 case LY_STMT_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003426 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003427 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003428 case LY_STMT_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003429 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003430 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003431 case LY_STMT_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003432 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003433 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003434 case LY_STMT_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003435 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003436 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003437 case LY_STMT_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003438 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003439 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003440 case LY_STMT_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003441 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003442 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003443 case LY_STMT_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003444 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003445 break;
3446
Radek Krejcid6b76452019-09-03 17:03:03 +02003447 case LY_STMT_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01003448 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)list, data, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003449 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003450 case LY_STMT_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003451 LY_CHECK_RET(parse_restrs(ctx, data, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003452 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003453 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003454 PARSER_CHECK_STMTVER2_RET(ctx, "action", "list");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003455 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003456 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003457 case LY_STMT_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003458 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003459 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003460 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003461 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "list");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003462 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003463 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003464 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003465 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003466 break;
3467 default:
David Sedlákb3077192019-06-19 10:55:37 +02003468 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003469 return LY_EVALID;
3470 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003471 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003472 LY_CHECK_RET(ret);
3473checks:
Radek Krejci7fc68292019-06-12 13:51:09 +02003474 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02003475 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, list->groupings, NULL, list->actions, list->notifs));
Radek Krejci7fc68292019-06-12 13:51:09 +02003476
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003477 if (list->max && list->min > list->max) {
David Sedlákb3077192019-06-19 10:55:37 +02003478 LOGVAL_PARSER(ctx, LYVE_SEMANTICS,
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003479 "Invalid combination of min-elements and max-elements: min value %u is bigger than the max value %u.",
3480 list->min, list->max);
3481 return LY_EVALID;
3482 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003483
3484 return ret;
3485}
3486
Michal Vaskoea5abea2018-09-18 13:10:54 +02003487/**
3488 * @brief Parse the yin-element statement.
3489 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003490 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003491 * @param[in,out] data Data to read from, always moved to currently handled character.
3492 * @param[in,out] flags Flags to write to.
3493 * @param[in,out] exts Extension instances to add to.
3494 *
3495 * @return LY_ERR values.
3496 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003497static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003498parse_yinelement(struct lys_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003499{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003500 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003501 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003502 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003503 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003504
3505 if (*flags & LYS_YINELEM_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02003506 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003507 return LY_EVALID;
3508 }
3509
3510 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003511 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003512
3513 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
3514 *flags |= LYS_YINELEM_TRUE;
3515 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
3516 *flags |= LYS_YINELEM_FALSE;
3517 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003518 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003519 free(buf);
3520 return LY_EVALID;
3521 }
3522 free(buf);
3523
Radek Krejci6d6556c2018-11-08 09:37:45 +01003524 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003525 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003526 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003527 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_YINELEM, 0, exts));
3528 LY_CHECK_RET(ret); break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003529 default:
David Sedlákb3077192019-06-19 10:55:37 +02003530 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003531 return LY_EVALID;
3532 }
3533 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003534 return ret;
3535}
3536
Michal Vaskoea5abea2018-09-18 13:10:54 +02003537/**
David Sedlák2444f8f2019-07-09 11:02:47 +02003538 * @brief Parse the argument statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003539 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003540 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003541 * @param[in,out] data Data to read from, always moved to currently handled character.
3542 * @param[in,out] argument Value to write to.
3543 * @param[in,out] flags Flags to write to.
3544 * @param[in,out] exts Extension instances to add to.
3545 *
3546 * @return LY_ERR values.
3547 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003548static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003549parse_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 +02003550{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003551 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003552 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003553 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003554 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003555
3556 if (*argument) {
David Sedlákb3077192019-06-19 10:55:37 +02003557 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003558 return LY_EVALID;
3559 }
3560
3561 /* get value */
David Sedlák2444f8f2019-07-09 11:02:47 +02003562 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003563 INSERT_WORD(ctx, buf, *argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003564
Radek Krejci6d6556c2018-11-08 09:37:45 +01003565 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003566 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003567 case LY_STMT_YIN_ELEMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003568 LY_CHECK_RET(parse_yinelement(ctx, data, flags, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003569 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003570 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003571 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_ARGUMENT, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003572 break;
3573 default:
David Sedlákb3077192019-06-19 10:55:37 +02003574 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003575 return LY_EVALID;
3576 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003577 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003578 return ret;
3579}
3580
Michal Vaskoea5abea2018-09-18 13:10:54 +02003581/**
3582 * @brief Parse the extension statement.
3583 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003584 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003585 * @param[in,out] data Data to read from, always moved to currently handled character.
3586 * @param[in,out] extensions Extensions to add to.
3587 *
3588 * @return LY_ERR values.
3589 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003590static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003591parse_extension(struct lys_parser_ctx *ctx, const char **data, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003592{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003593 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003594 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003595 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003596 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003597 struct lysp_ext *ex;
3598
Radek Krejci2c4e7172018-10-19 15:56:26 +02003599 LY_ARRAY_NEW_RET(ctx->ctx, *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003600
3601 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003602 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003603 INSERT_WORD(ctx, buf, ex->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003604
Radek Krejci6d6556c2018-11-08 09:37:45 +01003605 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003606 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003607 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003608 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 +02003609 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003610 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003611 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 +02003612 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003613 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003614 LY_CHECK_RET(parse_status(ctx, data, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003615 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003616 case LY_STMT_ARGUMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003617 LY_CHECK_RET(parse_argument(ctx, data, &ex->argument, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003618 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003619 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003620 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003621 break;
3622 default:
David Sedlákb3077192019-06-19 10:55:37 +02003623 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003624 return LY_EVALID;
3625 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003626 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003627 return ret;
3628}
3629
Michal Vaskoea5abea2018-09-18 13:10:54 +02003630/**
3631 * @brief Parse the deviate statement.
3632 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003633 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003634 * @param[in,out] data Data to read from, always moved to currently handled character.
3635 * @param[in,out] deviates Deviates to add to.
3636 *
3637 * @return LY_ERR values.
3638 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003639LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003640parse_deviate(struct lys_parser_ctx *ctx, const char **data, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003641{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003642 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003643 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003644 size_t word_len, dev_mod;
Radek Krejcid6b76452019-09-03 17:03:03 +02003645 enum ly_stmt kw;
David Sedlák60adc092019-08-06 15:57:02 +02003646 struct lysp_deviate *d;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003647 struct lysp_deviate_add *d_add = NULL;
3648 struct lysp_deviate_rpl *d_rpl = NULL;
3649 struct lysp_deviate_del *d_del = NULL;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02003650 const char **d_units = NULL, ***d_uniques = NULL, ***d_dflts = NULL;
3651 struct lysp_restr **d_musts = NULL;
3652 uint16_t *d_flags = 0;
3653 uint32_t *d_min = 0, *d_max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003654
3655 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003656 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003657
3658 if ((word_len == 13) && !strncmp(word, "not-supported", word_len)) {
3659 dev_mod = LYS_DEV_NOT_SUPPORTED;
3660 } else if ((word_len == 3) && !strncmp(word, "add", word_len)) {
3661 dev_mod = LYS_DEV_ADD;
3662 } else if ((word_len == 7) && !strncmp(word, "replace", word_len)) {
3663 dev_mod = LYS_DEV_REPLACE;
3664 } else if ((word_len == 6) && !strncmp(word, "delete", word_len)) {
3665 dev_mod = LYS_DEV_DELETE;
3666 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003667 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003668 free(buf);
3669 return LY_EVALID;
3670 }
3671 free(buf);
3672
3673 /* create structure */
3674 switch (dev_mod) {
3675 case LYS_DEV_NOT_SUPPORTED:
3676 d = calloc(1, sizeof *d);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003677 LY_CHECK_ERR_RET(!d, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003678 break;
3679 case LYS_DEV_ADD:
3680 d_add = calloc(1, sizeof *d_add);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003681 LY_CHECK_ERR_RET(!d_add, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003682 d = (struct lysp_deviate *)d_add;
3683 d_units = &d_add->units;
3684 d_uniques = &d_add->uniques;
3685 d_dflts = &d_add->dflts;
3686 d_musts = &d_add->musts;
3687 d_flags = &d_add->flags;
3688 d_min = &d_add->min;
3689 d_max = &d_add->max;
3690 break;
3691 case LYS_DEV_REPLACE:
3692 d_rpl = calloc(1, sizeof *d_rpl);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003693 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003694 d = (struct lysp_deviate *)d_rpl;
3695 d_units = &d_rpl->units;
3696 d_flags = &d_rpl->flags;
3697 d_min = &d_rpl->min;
3698 d_max = &d_rpl->max;
3699 break;
3700 case LYS_DEV_DELETE:
3701 d_del = calloc(1, sizeof *d_del);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003702 LY_CHECK_ERR_RET(!d_del, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003703 d = (struct lysp_deviate *)d_del;
3704 d_units = &d_del->units;
3705 d_uniques = &d_del->uniques;
3706 d_dflts = &d_del->dflts;
3707 d_musts = &d_del->musts;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003708 break;
3709 default:
3710 assert(0);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003711 LOGINT_RET(ctx->ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003712 }
3713 d->mod = dev_mod;
3714
3715 /* insert into siblings */
David Sedlák60adc092019-08-06 15:57:02 +02003716 LY_LIST_INSERT(deviates, d, next);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003717
Radek Krejci6d6556c2018-11-08 09:37:45 +01003718 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003719 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003720 case LY_STMT_CONFIG:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003721 switch (dev_mod) {
3722 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003723 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003724 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003725 return LY_EVALID;
3726 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003727 LY_CHECK_RET(parse_config(ctx, data, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003728 break;
3729 }
3730 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003731 case LY_STMT_DEFAULT:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003732 switch (dev_mod) {
3733 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003734 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003735 return LY_EVALID;
3736 case LYS_DEV_REPLACE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003737 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 +02003738 break;
3739 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003740 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 +02003741 break;
3742 }
3743 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003744 case LY_STMT_MANDATORY:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003745 switch (dev_mod) {
3746 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003747 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003748 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003749 return LY_EVALID;
3750 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003751 LY_CHECK_RET(parse_mandatory(ctx, data, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003752 break;
3753 }
3754 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003755 case LY_STMT_MAX_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003756 switch (dev_mod) {
3757 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003758 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003759 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003760 return LY_EVALID;
3761 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003762 LY_CHECK_RET(parse_maxelements(ctx, data, d_max, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003763 break;
3764 }
3765 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003766 case LY_STMT_MIN_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003767 switch (dev_mod) {
3768 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003769 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003770 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003771 return LY_EVALID;
3772 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003773 LY_CHECK_RET(parse_minelements(ctx, data, d_min, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003774 break;
3775 }
3776 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003777 case LY_STMT_MUST:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003778 switch (dev_mod) {
3779 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003780 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003781 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003782 return LY_EVALID;
3783 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003784 LY_CHECK_RET(parse_restrs(ctx, data, kw, d_musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003785 break;
3786 }
3787 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003788 case LY_STMT_TYPE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003789 switch (dev_mod) {
3790 case LYS_DEV_NOT_SUPPORTED:
3791 case LYS_DEV_ADD:
3792 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003793 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003794 return LY_EVALID;
3795 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003796 if (d_rpl->type) {
David Sedlákb3077192019-06-19 10:55:37 +02003797 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003798 return LY_EVALID;
3799 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003800 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003801 LY_CHECK_ERR_RET(!d_rpl->type, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003802 LY_CHECK_RET(parse_type(ctx, data, d_rpl->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003803 break;
3804 }
3805 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003806 case LY_STMT_UNIQUE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003807 switch (dev_mod) {
3808 case LYS_DEV_NOT_SUPPORTED:
3809 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003810 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003811 return LY_EVALID;
3812 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003813 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 +02003814 break;
3815 }
3816 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003817 case LY_STMT_UNITS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003818 switch (dev_mod) {
3819 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003820 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003821 return LY_EVALID;
3822 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003823 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 +02003824 break;
3825 }
3826 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003827 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003828 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003829 break;
3830 default:
David Sedlákb3077192019-06-19 10:55:37 +02003831 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003832 return LY_EVALID;
3833 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003834 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003835 return ret;
3836}
3837
Michal Vaskoea5abea2018-09-18 13:10:54 +02003838/**
3839 * @brief Parse the deviation statement.
3840 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003841 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003842 * @param[in,out] data Data to read from, always moved to currently handled character.
3843 * @param[in,out] deviations Deviations to add to.
3844 *
3845 * @return LY_ERR values.
3846 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003847LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003848parse_deviation(struct lys_parser_ctx *ctx, const char **data, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003849{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003850 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003851 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003852 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003853 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003854 struct lysp_deviation *dev;
3855
Radek Krejci2c4e7172018-10-19 15:56:26 +02003856 LY_ARRAY_NEW_RET(ctx->ctx, *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003857
3858 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003859 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
David Sedlákb9b892c2019-07-12 14:44:02 +02003860 YANG_CHECK_NONEMPTY(ctx, word_len, "deviation");
Radek Krejci44ceedc2018-10-02 15:54:31 +02003861 INSERT_WORD(ctx, buf, dev->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003862
Radek Krejci6d6556c2018-11-08 09:37:45 +01003863 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003864 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003865 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003866 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 +02003867 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003868 case LY_STMT_DEVIATE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003869 LY_CHECK_RET(parse_deviate(ctx, data, &dev->deviates));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003870 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003871 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003872 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 +02003873 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003874 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003875 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003876 break;
3877 default:
David Sedlákb3077192019-06-19 10:55:37 +02003878 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003879 return LY_EVALID;
3880 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003881 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003882 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01003883checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003884 /* mandatory substatements */
3885 if (!dev->deviates) {
David Sedlákb3077192019-06-19 10:55:37 +02003886 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003887 return LY_EVALID;
3888 }
3889
3890 return ret;
3891}
3892
Michal Vaskoea5abea2018-09-18 13:10:54 +02003893/**
3894 * @brief Parse the feature statement.
3895 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003896 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003897 * @param[in,out] data Data to read from, always moved to currently handled character.
3898 * @param[in,out] features Features to add to.
3899 *
3900 * @return LY_ERR values.
3901 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003902LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003903parse_feature(struct lys_parser_ctx *ctx, const char **data, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003904{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003905 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003906 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003907 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003908 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003909 struct lysp_feature *feat;
3910
Radek Krejci2c4e7172018-10-19 15:56:26 +02003911 LY_ARRAY_NEW_RET(ctx->ctx, *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003912
3913 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003914 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003915 INSERT_WORD(ctx, buf, feat->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01003916
Radek Krejci6d6556c2018-11-08 09:37:45 +01003917 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003918 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003919 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003920 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 +02003921 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003922 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003923 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 +02003924 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003925 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003926 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 +02003927 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003928 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003929 LY_CHECK_RET(parse_status(ctx, data, &feat->flags, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003930 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003931 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003932 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003933 break;
3934 default:
David Sedlákb3077192019-06-19 10:55:37 +02003935 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003936 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003937 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003938 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003939 return ret;
3940}
3941
Michal Vaskoea5abea2018-09-18 13:10:54 +02003942/**
3943 * @brief Parse the identity statement.
3944 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003945 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003946 * @param[in,out] data Data to read from, always moved to currently handled character.
3947 * @param[in,out] identities Identities to add to.
3948 *
3949 * @return LY_ERR values.
3950 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003951LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003952parse_identity(struct lys_parser_ctx *ctx, const char **data, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003953{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003954 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003955 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003956 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003957 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003958 struct lysp_ident *ident;
3959
Radek Krejci2c4e7172018-10-19 15:56:26 +02003960 LY_ARRAY_NEW_RET(ctx->ctx, *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003961
3962 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003963 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003964 INSERT_WORD(ctx, buf, ident->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01003965
Radek Krejci6d6556c2018-11-08 09:37:45 +01003966 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003967 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003968 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003969 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 +02003970 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003971 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02003972 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "identity");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003973 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 +02003974 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003975 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003976 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 +02003977 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003978 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003979 LY_CHECK_RET(parse_status(ctx, data, &ident->flags, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003980 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003981 case LY_STMT_BASE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003982 if (ident->bases && ctx->mod_version < 2) {
David Sedlákb3077192019-06-19 10:55:37 +02003983 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 +01003984 return LY_EVALID;
3985 }
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003986 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 +02003987 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003988 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003989 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003990 break;
3991 default:
David Sedlákb3077192019-06-19 10:55:37 +02003992 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003993 return LY_EVALID;
3994 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003995 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003996 return ret;
3997}
3998
Michal Vaskoea5abea2018-09-18 13:10:54 +02003999/**
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004000 * @brief Parse module substatements.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004001 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004002 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004003 * @param[in,out] data Data to read from, always moved to currently handled character.
4004 * @param[in,out] mod Module to write to.
4005 *
4006 * @return LY_ERR values.
4007 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004008LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02004009parse_module(struct lys_parser_ctx *ctx, const char **data, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004010{
4011 LY_ERR ret = 0;
4012 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004013 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004014 enum ly_stmt kw, prev_kw = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004015 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004016 struct lysp_submodule *dup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004017
4018 /* (sub)module name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02004019 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004020 INSERT_WORD(ctx, buf, mod->mod->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004021
Radek Krejci6d6556c2018-11-08 09:37:45 +01004022 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004023
Radek Krejcie3846472018-10-15 15:24:51 +02004024#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004025 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 +02004026
Michal Vasko7fbc8162018-09-17 10:35:16 +02004027 switch (kw) {
4028 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004029 case LY_STMT_NAMESPACE:
4030 case LY_STMT_PREFIX:
Radek Krejcie3846472018-10-15 15:24:51 +02004031 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4032 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004033 case LY_STMT_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004034 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004035 break;
4036 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004037 case LY_STMT_INCLUDE:
4038 case LY_STMT_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004039 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004040 break;
4041 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004042 case LY_STMT_ORGANIZATION:
4043 case LY_STMT_CONTACT:
4044 case LY_STMT_DESCRIPTION:
4045 case LY_STMT_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004046 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004047 break;
4048
4049 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004050 case LY_STMT_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004051 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004052 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004053 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004054 case LY_STMT_ANYDATA:
4055 case LY_STMT_ANYXML:
4056 case LY_STMT_AUGMENT:
4057 case LY_STMT_CHOICE:
4058 case LY_STMT_CONTAINER:
4059 case LY_STMT_DEVIATION:
4060 case LY_STMT_EXTENSION:
4061 case LY_STMT_FEATURE:
4062 case LY_STMT_GROUPING:
4063 case LY_STMT_IDENTITY:
4064 case LY_STMT_LEAF:
4065 case LY_STMT_LEAF_LIST:
4066 case LY_STMT_LIST:
4067 case LY_STMT_NOTIFICATION:
4068 case LY_STMT_RPC:
4069 case LY_STMT_TYPEDEF:
4070 case LY_STMT_USES:
4071 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004072 mod_stmt = Y_MOD_BODY;
4073 break;
4074 default:
4075 /* error handled in the next switch */
4076 break;
4077 }
Radek Krejcie3846472018-10-15 15:24:51 +02004078#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004079
Radek Krejcie3846472018-10-15 15:24:51 +02004080 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004081 switch (kw) {
4082 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004083 case LY_STMT_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004084 LY_CHECK_RET(parse_yangversion(ctx, data, &mod->mod->version, &mod->exts));
4085 ctx->mod_version = mod->mod->version;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004086 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004087 case LY_STMT_NAMESPACE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004088 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 +02004089 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004090 case LY_STMT_PREFIX:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004091 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 +02004092 break;
4093
4094 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004095 case LY_STMT_INCLUDE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004096 LY_CHECK_RET(parse_include(ctx, mod->mod->name, data, &mod->includes));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004097 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004098 case LY_STMT_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004099 LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, data, &mod->imports));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004100 break;
4101
4102 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004103 case LY_STMT_ORGANIZATION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004104 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 +02004105 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004106 case LY_STMT_CONTACT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004107 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 +02004108 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004109 case LY_STMT_DESCRIPTION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004110 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 +02004111 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004112 case LY_STMT_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004113 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 +02004114 break;
4115
4116 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004117 case LY_STMT_REVISION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004118 LY_CHECK_RET(parse_revision(ctx, data, &mod->revs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004119 break;
4120
4121 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004122 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004123 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "module");
Radek Krejci10113652018-11-14 16:56:50 +01004124 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004125 case LY_STMT_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004126 LY_CHECK_RET(parse_any(ctx, data, kw, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004127 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004128 case LY_STMT_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004129 LY_CHECK_RET(parse_choice(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004130 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004131 case LY_STMT_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004132 LY_CHECK_RET(parse_container(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004133 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004134 case LY_STMT_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004135 LY_CHECK_RET(parse_leaf(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004136 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004137 case LY_STMT_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004138 LY_CHECK_RET(parse_leaflist(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004139 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004140 case LY_STMT_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004141 LY_CHECK_RET(parse_list(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004142 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004143 case LY_STMT_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004144 LY_CHECK_RET(parse_uses(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004145 break;
4146
Radek Krejcid6b76452019-09-03 17:03:03 +02004147 case LY_STMT_AUGMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004148 LY_CHECK_RET(parse_augment(ctx, data, NULL, &mod->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004149 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004150 case LY_STMT_DEVIATION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004151 LY_CHECK_RET(parse_deviation(ctx, data, &mod->deviations));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004152 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004153 case LY_STMT_EXTENSION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004154 LY_CHECK_RET(parse_extension(ctx, data, &mod->extensions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004155 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004156 case LY_STMT_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004157 LY_CHECK_RET(parse_feature(ctx, data, &mod->features));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004158 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004159 case LY_STMT_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004160 LY_CHECK_RET(parse_grouping(ctx, data, NULL, &mod->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004161 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004162 case LY_STMT_IDENTITY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004163 LY_CHECK_RET(parse_identity(ctx, data, &mod->identities));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004164 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004165 case LY_STMT_NOTIFICATION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004166 LY_CHECK_RET(parse_notif(ctx, data, NULL, &mod->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004167 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004168 case LY_STMT_RPC:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004169 LY_CHECK_RET(parse_action(ctx, data, NULL, &mod->rpcs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004170 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004171 case LY_STMT_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01004172 LY_CHECK_RET(parse_typedef(ctx, NULL, data, &mod->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004173 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004174 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004175 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004176 break;
4177
4178 default:
David Sedlákb3077192019-06-19 10:55:37 +02004179 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004180 return LY_EVALID;
4181 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004182 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004183 LY_CHECK_RET(ret);
Radek Krejcie86bf772018-12-14 11:39:53 +01004184
Radek Krejci6d6556c2018-11-08 09:37:45 +01004185checks:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004186 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02004187 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, mod->groupings, mod->augments, mod->rpcs, mod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004188
Michal Vasko7fbc8162018-09-17 10:35:16 +02004189 /* mandatory substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004190 if (!mod->mod->ns) {
David Sedlákb3077192019-06-19 10:55:37 +02004191 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004192 return LY_EVALID;
4193 } else if (!mod->mod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004194 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004195 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004196 }
4197
Radek Krejcie9e987e2018-10-31 12:50:27 +01004198 /* submodules share the namespace with the module names, so there must not be
4199 * a submodule of the same name in the context, no need for revision matching */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004200 dup = ly_ctx_get_submodule(ctx->ctx, NULL, mod->mod->name, NULL);
4201 if (dup) {
David Sedlákb3077192019-06-19 10:55:37 +02004202 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 +01004203 return LY_EVALID;
4204 }
4205
4206 return ret;
4207}
4208
4209/**
4210 * @brief Parse submodule substatements.
4211 *
4212 * @param[in] ctx yang parser context for logging.
4213 * @param[in,out] data Data to read from, always moved to currently handled character.
4214 * @param[out] submod Parsed submodule structure.
4215 *
4216 * @return LY_ERR values.
4217 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004218LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02004219parse_submodule(struct lys_parser_ctx *ctx, const char **data, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004220{
4221 LY_ERR ret = 0;
4222 char *buf, *word;
4223 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004224 enum ly_stmt kw, prev_kw = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004225 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
4226 struct lysp_submodule *dup;
4227
4228 /* submodule name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02004229 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004230 INSERT_WORD(ctx, buf, submod->name, word, word_len);
4231
4232 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
4233
4234#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004235 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 +01004236
4237 switch (kw) {
4238 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004239 case LY_STMT_BELONGS_TO:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004240 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4241 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004242 case LY_STMT_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004243 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4244 break;
4245 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004246 case LY_STMT_INCLUDE:
4247 case LY_STMT_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004248 CHECK_ORDER(Y_MOD_LINKAGE);
4249 break;
4250 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004251 case LY_STMT_ORGANIZATION:
4252 case LY_STMT_CONTACT:
4253 case LY_STMT_DESCRIPTION:
4254 case LY_STMT_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004255 CHECK_ORDER(Y_MOD_META);
4256 break;
4257
4258 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004259 case LY_STMT_REVISION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004260 CHECK_ORDER(Y_MOD_REVISION);
4261 break;
4262 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004263 case LY_STMT_ANYDATA:
4264 case LY_STMT_ANYXML:
4265 case LY_STMT_AUGMENT:
4266 case LY_STMT_CHOICE:
4267 case LY_STMT_CONTAINER:
4268 case LY_STMT_DEVIATION:
4269 case LY_STMT_EXTENSION:
4270 case LY_STMT_FEATURE:
4271 case LY_STMT_GROUPING:
4272 case LY_STMT_IDENTITY:
4273 case LY_STMT_LEAF:
4274 case LY_STMT_LEAF_LIST:
4275 case LY_STMT_LIST:
4276 case LY_STMT_NOTIFICATION:
4277 case LY_STMT_RPC:
4278 case LY_STMT_TYPEDEF:
4279 case LY_STMT_USES:
4280 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004281 mod_stmt = Y_MOD_BODY;
4282 break;
4283 default:
4284 /* error handled in the next switch */
4285 break;
4286 }
4287#undef CHECK_ORDER
4288
4289 prev_kw = kw;
4290 switch (kw) {
4291 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004292 case LY_STMT_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004293 LY_CHECK_RET(parse_yangversion(ctx, data, &submod->version, &submod->exts));
4294 ctx->mod_version = submod->version;
4295 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004296 case LY_STMT_BELONGS_TO:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004297 LY_CHECK_RET(parse_belongsto(ctx, data, &submod->belongsto, &submod->prefix, &submod->exts));
4298 break;
4299
4300 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004301 case LY_STMT_INCLUDE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004302 LY_CHECK_RET(parse_include(ctx, submod->name, data, &submod->includes));
4303 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004304 case LY_STMT_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004305 LY_CHECK_RET(parse_import(ctx, submod->prefix, data, &submod->imports));
4306 break;
4307
4308 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004309 case LY_STMT_ORGANIZATION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004310 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_ORGANIZATION, 0, &submod->org, Y_STR_ARG, &submod->exts));
4311 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004312 case LY_STMT_CONTACT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004313 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_CONTACT, 0, &submod->contact, Y_STR_ARG, &submod->exts));
4314 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004315 case LY_STMT_DESCRIPTION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004316 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &submod->dsc, Y_STR_ARG, &submod->exts));
4317 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004318 case LY_STMT_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004319 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &submod->ref, Y_STR_ARG, &submod->exts));
4320 break;
4321
4322 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004323 case LY_STMT_REVISION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004324 LY_CHECK_RET(parse_revision(ctx, data, &submod->revs));
4325 break;
4326
4327 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004328 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004329 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004330 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004331 case LY_STMT_ANYXML:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004332 LY_CHECK_RET(parse_any(ctx, data, kw, NULL, &submod->data));
4333 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004334 case LY_STMT_CHOICE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004335 LY_CHECK_RET(parse_choice(ctx, data, NULL, &submod->data));
4336 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004337 case LY_STMT_CONTAINER:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004338 LY_CHECK_RET(parse_container(ctx, data, NULL, &submod->data));
4339 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004340 case LY_STMT_LEAF:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004341 LY_CHECK_RET(parse_leaf(ctx, data, NULL, &submod->data));
4342 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004343 case LY_STMT_LEAF_LIST:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004344 LY_CHECK_RET(parse_leaflist(ctx, data, NULL, &submod->data));
4345 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004346 case LY_STMT_LIST:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004347 LY_CHECK_RET(parse_list(ctx, data, NULL, &submod->data));
4348 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004349 case LY_STMT_USES:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004350 LY_CHECK_RET(parse_uses(ctx, data, NULL, &submod->data));
4351 break;
4352
Radek Krejcid6b76452019-09-03 17:03:03 +02004353 case LY_STMT_AUGMENT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004354 LY_CHECK_RET(parse_augment(ctx, data, NULL, &submod->augments));
4355 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004356 case LY_STMT_DEVIATION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004357 LY_CHECK_RET(parse_deviation(ctx, data, &submod->deviations));
4358 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004359 case LY_STMT_EXTENSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004360 LY_CHECK_RET(parse_extension(ctx, data, &submod->extensions));
4361 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004362 case LY_STMT_FEATURE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004363 LY_CHECK_RET(parse_feature(ctx, data, &submod->features));
4364 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004365 case LY_STMT_GROUPING:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004366 LY_CHECK_RET(parse_grouping(ctx, data, NULL, &submod->groupings));
4367 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004368 case LY_STMT_IDENTITY:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004369 LY_CHECK_RET(parse_identity(ctx, data, &submod->identities));
4370 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004371 case LY_STMT_NOTIFICATION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004372 LY_CHECK_RET(parse_notif(ctx, data, NULL, &submod->notifs));
4373 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004374 case LY_STMT_RPC:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004375 LY_CHECK_RET(parse_action(ctx, data, NULL, &submod->rpcs));
4376 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004377 case LY_STMT_TYPEDEF:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004378 LY_CHECK_RET(parse_typedef(ctx, NULL, data, &submod->typedefs));
4379 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004380 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004381 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &submod->exts));
4382 break;
4383
4384 default:
David Sedlákb3077192019-06-19 10:55:37 +02004385 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004386 return LY_EVALID;
4387 }
4388 }
4389 LY_CHECK_RET(ret);
4390
4391checks:
4392 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02004393 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, submod->groupings, submod->augments, submod->rpcs, submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004394
4395 /* mandatory substatements */
4396 if (!submod->belongsto) {
David Sedlákb3077192019-06-19 10:55:37 +02004397 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004398 return LY_EVALID;
4399 }
4400
4401 /* submodules share the namespace with the module names, so there must not be
4402 * a submodule of the same name in the context, no need for revision matching */
4403 dup = ly_ctx_get_submodule(ctx->ctx, NULL, submod->name, NULL);
4404 if (dup && strcmp(dup->belongsto, submod->belongsto)) {
David Sedlákb3077192019-06-19 10:55:37 +02004405 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Name collision between submodules of name \"%s\".", dup->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +01004406 return LY_EVALID;
4407 }
4408
Michal Vasko7fbc8162018-09-17 10:35:16 +02004409 return ret;
4410}
4411
Radek Krejcid4557c62018-09-17 11:42:09 +02004412LY_ERR
David Sedlák1b623122019-08-05 15:27:49 +02004413yang_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 +02004414{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004415 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004416 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004417 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004418 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004419 struct lysp_submodule *mod_p = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004420
David Sedlák1b623122019-08-05 15:27:49 +02004421 /* create context */
4422 *context = calloc(1, sizeof **context);
4423 LY_CHECK_ERR_RET(!(*context), LOGMEM(ly_ctx), LY_EMEM);
4424 (*context)->ctx = ly_ctx;
Radek Krejci99435242019-09-05 16:19:15 +02004425 (*context)->pos_type = LY_VLOG_LINE;
David Sedlák1b623122019-08-05 15:27:49 +02004426 (*context)->line = 1;
4427
4428 /* map the typedefs and groupings list from main context to the submodule's context */
4429 memcpy(&(*context)->tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
4430 memcpy(&(*context)->grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes);
4431
Michal Vasko7fbc8162018-09-17 10:35:16 +02004432 /* "module"/"submodule" */
David Sedlák1b623122019-08-05 15:27:49 +02004433 ret = get_keyword(*context, &data, &kw, &word, &word_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004434 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004435
Radek Krejcid6b76452019-09-03 17:03:03 +02004436 if (kw == LY_STMT_MODULE) {
David Sedlák1b623122019-08-05 15:27:49 +02004437 LOGERR((*context)->ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004438 ret = LY_EINVAL;
4439 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004440 } else if (kw != LY_STMT_SUBMODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004441 LOGVAL_PARSER(*context, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004442 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004443 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004444 }
4445
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004446 mod_p = calloc(1, sizeof *mod_p);
David Sedlák1b623122019-08-05 15:27:49 +02004447 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM((*context)->ctx), cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004448 mod_p->parsing = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004449
4450 /* substatements */
David Sedlák1b623122019-08-05 15:27:49 +02004451 ret = parse_submodule(*context, &data, mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004452 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004453
4454 /* read some trailing spaces or new lines */
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004455 while(*data && isspace(*data)) {
4456 data++;
4457 }
4458 if (*data) {
David Sedlák1538a842019-08-08 15:38:51 +02004459 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_SUBMOD, 15, data, strlen(data) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004460 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004461 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004462 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004463
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004464 mod_p->parsing = 0;
4465 *submod = mod_p;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004466
Radek Krejcibbe09a92018-11-08 09:36:54 +01004467cleanup:
4468 if (ret) {
David Sedlák1b623122019-08-05 15:27:49 +02004469 lysp_submodule_free((*context)->ctx, mod_p);
4470 lys_parser_ctx_free(*context);
4471 *context = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004472 }
4473
4474 return ret;
4475}
4476
4477LY_ERR
David Sedlák1b623122019-08-05 15:27:49 +02004478yang_parse_module(struct lys_parser_ctx **context, const char *data, struct lys_module *mod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004479{
4480 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004481 char *word;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004482 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004483 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004484 struct lysp_module *mod_p = NULL;
4485
David Sedlák1b623122019-08-05 15:27:49 +02004486 /* create context */
4487 *context = calloc(1, sizeof **context);
4488 LY_CHECK_ERR_RET(!(*context), LOGMEM(mod->ctx), LY_EMEM);
4489 (*context)->ctx = mod->ctx;
Radek Krejci335332a2019-09-05 13:03:35 +02004490 (*context)->pos_type = LY_VLOG_LINE;
David Sedlák1b623122019-08-05 15:27:49 +02004491 (*context)->line = 1;
4492
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004493 /* "module"/"submodule" */
David Sedlák1b623122019-08-05 15:27:49 +02004494 ret = get_keyword(*context, &data, &kw, &word, &word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004495 LY_CHECK_GOTO(ret, cleanup);
4496
Radek Krejcid6b76452019-09-03 17:03:03 +02004497 if (kw == LY_STMT_SUBMODULE) {
David Sedlák1b623122019-08-05 15:27:49 +02004498 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 +01004499 ret = LY_EINVAL;
4500 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004501 } else if (kw != LY_STMT_MODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004502 LOGVAL_PARSER((*context), LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004503 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004504 goto cleanup;
4505 }
4506
4507 mod_p = calloc(1, sizeof *mod_p);
David Sedlák1b623122019-08-05 15:27:49 +02004508 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM((*context)->ctx), cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004509 mod_p->mod = mod;
4510 mod_p->parsing = 1;
4511
4512 /* substatements */
David Sedlák1b623122019-08-05 15:27:49 +02004513 ret = parse_module(*context, &data, mod_p);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004514 LY_CHECK_GOTO(ret, cleanup);
4515
4516 /* read some trailing spaces or new lines */
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004517 while(*data && isspace(*data)) {
4518 data++;
4519 }
4520 if (*data) {
David Sedlák1538a842019-08-08 15:38:51 +02004521 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_MOD, 15, data, strlen(data) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004522 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004523 goto cleanup;
4524 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004525
4526 mod_p->parsing = 0;
4527 mod->parsed = mod_p;
4528
4529cleanup:
4530 if (ret) {
4531 lysp_module_free(mod_p);
David Sedlák1b623122019-08-05 15:27:49 +02004532 lys_parser_ctx_free(*context);
4533 *context = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004534 }
4535
Michal Vasko7fbc8162018-09-17 10:35:16 +02004536 return ret;
4537}