blob: f72dd2ffa941f2adcd862423bf09f8eb7ce624cc [file] [log] [blame]
Michal Vasko7fbc8162018-09-17 10:35:16 +02001/**
2 * @file parser_yang.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief YANG parser
5 *
6 * Copyright (c) 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Michal Vasko7fbc8162018-09-17 10:35:16 +020014
15#include "common.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020016
17#include <assert.h>
18#include <ctype.h>
19#include <errno.h>
20#include <stdint.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24
Michal Vasko7fbc8162018-09-17 10:35:16 +020025#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020026#include "dict.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020027#include "log.h"
Radek Krejci0935f412019-08-20 16:15:18 +020028#include "plugins_exts.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020029#include "set.h"
30#include "tree.h"
31#include "tree_schema.h"
Radek Krejci70853c52018-10-15 14:46:16 +020032#include "tree_schema_internal.h"
Radek Krejci44ceedc2018-10-02 15:54:31 +020033
Radek Krejciceaf2122019-01-02 15:03:26 +010034/**
35 * @brief Insert WORD into the libyang context's dictionary and store as TARGET.
36 * @param[in] CTX yang parser context to access libyang context.
37 * @param[in] BUF buffer in case the word is not a constant and can be inserted directly (zero-copy)
38 * @param[out] TARGET variable where to store the pointer to the inserted value.
39 * @param[in] WORD string to store.
40 * @param[in] LEN length of the string in WORD to store.
41 */
Radek Krejci9fcacc12018-10-11 15:59:11 +020042#define INSERT_WORD(CTX, BUF, TARGET, WORD, LEN) \
43 if (BUF) {(TARGET) = lydict_insert_zc((CTX)->ctx, WORD);}\
Radek Krejcif09e4e82019-06-14 15:08:11 +020044 else {(TARGET) = lydict_insert((CTX)->ctx, LEN ? WORD : "", LEN);}
Radek Krejci44ceedc2018-10-02 15:54:31 +020045
Radek Krejciceaf2122019-01-02 15:03:26 +010046/**
47 * @brief Move the DATA pointer by COUNT items. Also updates the indent value in yang parser context
48 * @param[in] CTX yang parser context to update its indent value.
49 * @param[in,out] DATA pointer to move
50 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
51 */
Radek Krejci2b610482019-01-02 13:36:09 +010052#define MOVE_INPUT(CTX, DATA, COUNT) (*(DATA))+=COUNT;(CTX)->indent+=COUNT
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020053
Michal Vaskoea5abea2018-09-18 13:10:54 +020054/**
55 * @brief Loop through all substatements providing, return if there are none.
56 *
Radek Krejci44ceedc2018-10-02 15:54:31 +020057 * @param[in] CTX yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +020058 * @param[in] DATA Raw data to read from.
59 * @param[out] KW YANG keyword read.
60 * @param[out] WORD Pointer to the keyword itself.
61 * @param[out] WORD_LEN Length of the keyword.
62 * @param[out] ERR Variable for error storing.
63 *
64 * @return In case there are no substatements or a fatal error encountered.
65 */
Radek Krejci6d6556c2018-11-08 09:37:45 +010066#define YANG_READ_SUBSTMT_FOR(CTX, DATA, KW, WORD, WORD_LEN, ERR, CHECKGOTO) \
Radek Krejci6d9b9b52018-11-02 12:43:39 +010067 LY_CHECK_RET(get_keyword(CTX, DATA, &KW, &WORD, &WORD_LEN)); \
Radek Krejcid6b76452019-09-03 17:03:03 +020068 if (KW == LY_STMT_SYNTAX_SEMICOLON) { \
Radek Krejci6d6556c2018-11-08 09:37:45 +010069 CHECKGOTO; \
Radek Krejci6d9b9b52018-11-02 12:43:39 +010070 return LY_SUCCESS; \
Michal Vasko7fbc8162018-09-17 10:35:16 +020071 } \
Radek Krejcid6b76452019-09-03 17:03:03 +020072 if (KW != LY_STMT_SYNTAX_LEFT_BRACE) { \
David Sedlákb3077192019-06-19 10:55:37 +020073 LOGVAL_PARSER(CTX, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\", expected \";\" or \"{\".", ly_stmt2str(KW)); \
Michal Vasko7fbc8162018-09-17 10:35:16 +020074 return LY_EVALID; \
75 } \
76 for (ERR = get_keyword(CTX, DATA, &KW, &WORD, &WORD_LEN); \
Radek Krejcid6b76452019-09-03 17:03:03 +020077 !ERR && (KW != LY_STMT_SYNTAX_RIGHT_BRACE); \
Michal Vasko7fbc8162018-09-17 10:35:16 +020078 ERR = get_keyword(CTX, DATA, &KW, &WORD, &WORD_LEN))
79
Radek Krejci2d7a47b2019-05-16 13:34:10 +020080LY_ERR parse_container(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
81LY_ERR parse_uses(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
82LY_ERR parse_choice(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
83LY_ERR parse_case(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
84LY_ERR parse_list(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
85LY_ERR parse_grouping(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_grp **groupings);
Michal Vasko7fbc8162018-09-17 10:35:16 +020086
Michal Vaskoea5abea2018-09-18 13:10:54 +020087/**
88 * @brief Add another character to dynamic buffer, a low-level function.
89 *
Radek Krejci44ceedc2018-10-02 15:54:31 +020090 * Enlarge if needed. Updates \p input as well as \p buf_used.
Michal Vaskoea5abea2018-09-18 13:10:54 +020091 *
Radek Krejci404251e2018-10-09 12:06:44 +020092 * @param[in] ctx libyang context for logging.
Radek Krejci44ceedc2018-10-02 15:54:31 +020093 * @param[in, out] input Input string to process.
94 * @param[in] len Number of bytes to get from the input string and copy into the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +020095 * @param[in,out] buf Buffer to use, can be moved by realloc().
96 * @param[in,out] buf_len Current size of the buffer.
Radek Krejci44ceedc2018-10-02 15:54:31 +020097 * @param[in,out] buf_used Currently used characters of the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +020098 *
99 * @return LY_ERR values.
100 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200101LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +0200102buf_add_char(struct ly_ctx *ctx, const char **input, size_t len, char **buf, size_t *buf_len, size_t *buf_used)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200103{
Radek Krejci44ceedc2018-10-02 15:54:31 +0200104 if (*buf_len <= (*buf_used) + len) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200105 *buf_len += 16;
106 *buf = ly_realloc(*buf, *buf_len);
107 LY_CHECK_ERR_RET(!*buf, LOGMEM(ctx), LY_EMEM);
108 }
Radek Krejcic0917392019-04-10 13:04:04 +0200109 if (*buf_used) {
110 memcpy(&(*buf)[*buf_used], *input, len);
111 } else {
112 memcpy(*buf, *input, len);
113 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200114
Radek Krejci44ceedc2018-10-02 15:54:31 +0200115 (*buf_used) += len;
116 (*input) += len;
117
Michal Vasko7fbc8162018-09-17 10:35:16 +0200118 return LY_SUCCESS;
119}
120
Michal Vaskoea5abea2018-09-18 13:10:54 +0200121/**
Radek Krejci44ceedc2018-10-02 15:54:31 +0200122 * @brief Store a single UTF8 character. It depends whether in a dynamically-allocated buffer or just as a pointer to the data.
123 *
124 * @param[in] ctx yang parser context for logging.
125 * @param[in,out] input Pointer to the string where to get the character to store. Automatically moved to the next character
126 * when function returns.
127 * @param[in] arg Type of the input string to select method of checking character validity.
128 * @param[in,out] word_p Word pointer. If buffer (\p word_b) was not yet needed, it is just a pointer to the first
Michal Vaskoea5abea2018-09-18 13:10:54 +0200129 * stored character. If buffer was needed (\p word_b is non-NULL or \p need_buf is set), it is pointing to the buffer.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200130 * @param[in,out] word_len Current length of the word pointed to by \p word_p.
131 * @param[in,out] word_b Word buffer. Is kept NULL as long as it is not requested (word is a substring of the data).
Michal Vaskoea5abea2018-09-18 13:10:54 +0200132 * @param[in,out] buf_len Current length of \p word_b.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200133 * @param[in] need_buf Flag if the dynamically allocated buffer is required.
David Sedlák40bb13b2019-07-10 14:34:18 +0200134 * @param[in,out] prefix Storage for internally used flag in case of possible prefixed identifiers:
135 * 0 - colon not yet found (no prefix)
136 * 1 - \p c is the colon character
137 * 2 - prefix already processed, now processing the identifier
Michal Vaskoea5abea2018-09-18 13:10:54 +0200138 *
139 * @return LY_ERR values.
140 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200141LY_ERR
David Sedlák40bb13b2019-07-10 14:34:18 +0200142buf_store_char(struct lys_parser_ctx *ctx, const char **input, enum yang_arg arg, char **word_p,
143 size_t *word_len, char **word_b, size_t *buf_len, int need_buf, int *prefix)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200144{
Radek Krejci44ceedc2018-10-02 15:54:31 +0200145 unsigned int c;
146 size_t len;
147
Radek Krejcif29b7c32019-04-09 16:17:49 +0200148 /* check valid combination of input paremeters - if need_buf specified, word_b must be provided */
149 assert(!need_buf || (need_buf && word_b));
150
Radek Krejci44ceedc2018-10-02 15:54:31 +0200151 /* get UTF8 code point (and number of bytes coding the character) */
152 LY_CHECK_ERR_RET(ly_getutf8(input, &c, &len),
David Sedlákb3077192019-06-19 10:55:37 +0200153 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, (*input)[-len]), LY_EVALID);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200154 (*input) -= len;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200155 if (c == '\n') {
156 ctx->indent = 0;
157 } else {
158 /* note - even the multibyte character is count as 1 */
159 ++ctx->indent;
160 }
161
Radek Krejci44ceedc2018-10-02 15:54:31 +0200162 /* check character validity */
163 switch (arg) {
164 case Y_IDENTIF_ARG:
David Sedlák4a650532019-07-10 11:55:18 +0200165 LY_CHECK_RET(lysp_check_identifierchar(ctx, c, !(*word_len), NULL));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200166 break;
167 case Y_PREF_IDENTIF_ARG:
David Sedlák40bb13b2019-07-10 14:34:18 +0200168 LY_CHECK_RET(lysp_check_identifierchar(ctx, c, !(*word_len), prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200169 break;
170 case Y_STR_ARG:
171 case Y_MAYBE_STR_ARG:
David Sedlák4a650532019-07-10 11:55:18 +0200172 LY_CHECK_RET(lysp_check_stringchar(ctx, c));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200173 break;
174 }
175
Michal Vasko7fbc8162018-09-17 10:35:16 +0200176 if (word_b && *word_b) {
177 /* add another character into buffer */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200178 if (buf_add_char(ctx->ctx, input, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200179 return LY_EMEM;
180 }
181
182 /* in case of realloc */
183 *word_p = *word_b;
Radek Krejcif29b7c32019-04-09 16:17:49 +0200184 } else if (word_b && need_buf) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200185 /* first time we need a buffer, copy everything read up to now */
186 if (*word_len) {
187 *word_b = malloc(*word_len);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200188 LY_CHECK_ERR_RET(!*word_b, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200189 *buf_len = *word_len;
190 memcpy(*word_b, *word_p, *word_len);
191 }
192
193 /* add this new character into buffer */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200194 if (buf_add_char(ctx->ctx, input, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200195 return LY_EMEM;
196 }
197
198 /* in case of realloc */
199 *word_p = *word_b;
200 } else {
201 /* just remember the first character pointer */
202 if (!*word_p) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200203 *word_p = (char *)(*input);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200204 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200205 /* ... and update the word's length */
206 (*word_len) += len;
207 (*input) += len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200208 }
209
210 return LY_SUCCESS;
211}
212
Michal Vaskoea5abea2018-09-18 13:10:54 +0200213/**
214 * @brief Skip YANG comment in data.
215 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200216 * @param[in] ctx yang parser context for logging.
217 * @param[in,out] data Data to read from, automatically moved after the comment.
218 * @param[in] comment Type of the comment to process:
219 * 1 for a one-line comment,
220 * 2 for a block comment.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200221 *
222 * @return LY_ERR values.
223 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200224LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200225skip_comment(struct lys_parser_ctx *ctx, const char **data, int comment)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200226{
Radek Krejci80dd33e2018-09-26 15:57:18 +0200227 /* internal statuses: 0 - comment ended,
228 * 1 - in line comment,
229 * 2 - in block comment,
230 * 3 - in block comment with last read character '*'
231 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200232 while (**data && comment) {
233 switch (comment) {
234 case 1:
235 if (**data == '\n') {
236 comment = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200237 ++ctx->line;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200238 }
239 break;
240 case 2:
241 if (**data == '*') {
Radek Krejci15c80ca2018-10-09 11:01:31 +0200242 comment = 3;
243 } else if (**data == '\n') {
244 ++ctx->line;
245 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200246 break;
247 case 3:
248 if (**data == '/') {
249 comment = 0;
Radek Krejci5b930492019-06-11 14:54:08 +0200250 } else if (**data != '*') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200251 if (**data == '\n') {
252 ++ctx->line;
253 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200254 comment = 2;
255 }
256 break;
257 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200258 LOGINT_RET(ctx->ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200259 }
260
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200261 if (**data == '\n') {
262 ctx->indent = 0;
263 } else {
264 ++ctx->indent;
265 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200266 ++(*data);
267 }
268
269 if (!**data && (comment > 1)) {
David Sedlákb3077192019-06-19 10:55:37 +0200270 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Unexpected end-of-input, non-terminated comment.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200271 return LY_EVALID;
272 }
273
274 return LY_SUCCESS;
275}
276
Michal Vaskoea5abea2018-09-18 13:10:54 +0200277/**
278 * @brief Read a quoted string from data.
279 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200280 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200281 * @param[in,out] data Data to read from, always moved to currently handled character.
282 * @param[in] arg Type of YANG keyword argument expected.
283 * @param[out] word_p Pointer to the read quoted string.
284 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read quoted string. If not needed,
285 * set to NULL. Otherwise equal to \p word_p.
286 * @param[out] word_len Length of the read quoted string.
287 * @param[out] buf_len Length of the dynamically-allocated buffer \p word_b.
288 * @param[in] indent Current indent (number of YANG spaces). Needed for correct multi-line string
289 * indenation in the final quoted string.
290 *
291 * @return LY_ERR values.
292 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200293static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200294read_qstring(struct lys_parser_ctx *ctx, const char **data, enum yang_arg arg, char **word_p, char **word_b, size_t *word_len,
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200295 size_t *buf_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200296{
297 /* string: 0 - string ended, 1 - string with ', 2 - string with ", 3 - string with " with last character \,
298 * 4 - string finished, now skipping whitespaces looking for +,
299 * 5 - string continues after +, skipping whitespaces */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200300 unsigned int string, block_indent = 0, current_indent = 0, need_buf = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200301 const char *c;
David Sedlák40bb13b2019-07-10 14:34:18 +0200302 int prefix = 0;
Michal Vasko90edde42019-11-25 15:25:07 +0100303 unsigned int trailing_ws = 0; /* current number of stored trailing whitespace characters */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200304
305 if (**data == '\"') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200306 string = 2;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200307 current_indent = block_indent = ctx->indent + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200308 } else {
309 assert(**data == '\'');
310 string = 1;
311 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200312 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200313
314 while (**data && string) {
315 switch (string) {
316 case 1:
317 switch (**data) {
318 case '\'':
319 /* string may be finished, but check for + */
320 string = 4;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200321 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200322 break;
323 default:
324 /* check and store character */
David Sedlák40bb13b2019-07-10 14:34:18 +0200325 LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200326 break;
327 }
328 break;
329 case 2:
330 switch (**data) {
331 case '\"':
332 /* string may be finished, but check for + */
333 string = 4;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200334 MOVE_INPUT(ctx, data, 1);
Radek Krejciff13cd12019-10-25 15:34:24 +0200335 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200336 break;
337 case '\\':
338 /* special character following */
339 string = 3;
Radek Krejciff13cd12019-10-25 15:34:24 +0200340
341 /* the backslash sequence is substituted, so we will need a buffer to store the result */
342 need_buf = 1;
343
344 /* move forward to the escaped character */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200345 ++(*data);
Radek Krejciff13cd12019-10-25 15:34:24 +0200346
347 /* note that the trailing whitespaces are supposed to be trimmed before substitution of
348 * backslash-escaped characters (RFC 7950, 6.1.3), so we have to zero the trailing whitespaces counter */
349 trailing_ws = 0;
350
351 /* since the backslash-escaped character is handled as first non-whitespace character, stop eating indentation */
352 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200353 break;
354 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200355 if (current_indent < block_indent) {
356 ++current_indent;
357 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200358 } else {
Michal Vasko90edde42019-11-25 15:25:07 +0100359 /* check and store whitespace character */
David Sedlák40bb13b2019-07-10 14:34:18 +0200360 LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko90edde42019-11-25 15:25:07 +0100361 trailing_ws++;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200362 }
363 break;
364 case '\t':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200365 if (current_indent < block_indent) {
366 assert(need_buf);
367 current_indent += 8;
368 ctx->indent += 8;
369 for (; current_indent > block_indent; --current_indent, --ctx->indent) {
370 /* store leftover spaces from the tab */
371 c = " ";
David Sedlák40bb13b2019-07-10 14:34:18 +0200372 LY_CHECK_RET(buf_store_char(ctx, &c, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko90edde42019-11-25 15:25:07 +0100373 trailing_ws++;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200374 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200375 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200376 } else {
Michal Vasko90edde42019-11-25 15:25:07 +0100377 /* check and store whitespace character */
David Sedlák40bb13b2019-07-10 14:34:18 +0200378 LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko90edde42019-11-25 15:25:07 +0100379 trailing_ws++;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200380 /* additional characters for indentation - only 1 was count in buf_store_char */
381 ctx->indent += 7;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200382 }
383 break;
384 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200385 if (block_indent) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200386 /* we will be removing the indents so we need our own buffer */
387 need_buf = 1;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200388
389 /* remove trailing tabs and spaces */
Radek Krejciff13cd12019-10-25 15:34:24 +0200390 (*word_len) = *word_len - trailing_ws;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200391
Radek Krejciff13cd12019-10-25 15:34:24 +0200392 /* restart indentation */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200393 current_indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200394 }
395
396 /* check and store character */
David Sedlák40bb13b2019-07-10 14:34:18 +0200397 LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200398
399 /* maintain line number */
400 ++ctx->line;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200401
402 /* reset context indentation counter for possible string after this one */
403 ctx->indent = 0;
Radek Krejciff13cd12019-10-25 15:34:24 +0200404 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200405 break;
406 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200407 /* first non-whitespace character, stop eating indentation */
408 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200409
410 /* check and store character */
David Sedlák40bb13b2019-07-10 14:34:18 +0200411 LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Radek Krejciff13cd12019-10-25 15:34:24 +0200412 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200413 break;
414 }
415 break;
416 case 3:
417 /* string encoded characters */
418 switch (**data) {
419 case 'n':
420 c = "\n";
421 break;
422 case 't':
423 c = "\t";
424 break;
425 case '\"':
426 c = *data;
427 break;
428 case '\\':
429 c = *data;
430 break;
431 default:
David Sedlákb3077192019-06-19 10:55:37 +0200432 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Double-quoted string unknown special character '\\%c'.", **data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200433 return LY_EVALID;
434 }
435
436 /* check and store character */
David Sedlák40bb13b2019-07-10 14:34:18 +0200437 LY_CHECK_RET(buf_store_char(ctx, &c, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200438
439 string = 2;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200440 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200441 break;
442 case 4:
443 switch (**data) {
444 case '+':
445 /* string continues */
446 string = 5;
Radek Krejciefd22f62018-09-27 11:47:58 +0200447 need_buf = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200448 break;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200449 case '\n':
450 ++ctx->line;
451 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200452 case ' ':
453 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200454 /* just skip */
455 break;
456 default:
457 /* string is finished */
458 goto string_end;
459 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200460 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200461 break;
462 case 5:
463 switch (**data) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200464 case '\n':
465 ++ctx->line;
466 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200467 case ' ':
468 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200469 /* skip */
470 break;
471 case '\'':
472 string = 1;
473 break;
474 case '\"':
475 string = 2;
476 break;
477 default:
478 /* it must be quoted again */
David Sedlákb3077192019-06-19 10:55:37 +0200479 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Both string parts divided by '+' must be quoted.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200480 return LY_EVALID;
481 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200482 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200483 break;
484 default:
485 return LY_EINT;
486 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200487 }
488
489string_end:
Radek Krejci4e199f52019-05-28 09:09:28 +0200490 if (arg <= Y_PREF_IDENTIF_ARG && !(*word_len)) {
491 /* empty identifier */
David Sedlákb3077192019-06-19 10:55:37 +0200492 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Statement argument is required.");
Radek Krejci4e199f52019-05-28 09:09:28 +0200493 return LY_EVALID;
494 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200495 return LY_SUCCESS;
496}
497
Michal Vaskoea5abea2018-09-18 13:10:54 +0200498/**
499 * @brief Get another YANG string from the raw data.
500 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200501 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200502 * @param[in,out] data Data to read from, always moved to currently handled character.
503 * @param[in] arg Type of YANG keyword argument expected.
Radek Krejcid3ca0632019-04-16 16:54:54 +0200504 * @param[out] flags optional output argument to get flag of the argument's quoting (LYS_*QOUTED - see [schema node flags](@ref snodeflags))
Michal Vasko2ca70f52018-09-27 11:04:51 +0200505 * @param[out] word_p Pointer to the read string. Can return NULL if \p arg is #Y_MAYBE_STR_ARG.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200506 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read string. If not needed,
507 * set to NULL. Otherwise equal to \p word_p.
508 * @param[out] word_len Length of the read string.
509 *
510 * @return LY_ERR values.
Michal Vasko7fbc8162018-09-17 10:35:16 +0200511 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200512LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200513get_argument(struct lys_parser_ctx *ctx, const char **data, enum yang_arg arg,
Radek Krejcid3ca0632019-04-16 16:54:54 +0200514 uint16_t *flags, char **word_p, char **word_b, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200515{
Radek Krejci44ceedc2018-10-02 15:54:31 +0200516 size_t buf_len = 0;
David Sedlák40bb13b2019-07-10 14:34:18 +0200517 int prefix = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200518 /* word buffer - dynamically allocated */
519 *word_b = NULL;
520
521 /* word pointer - just a pointer to data */
522 *word_p = NULL;
523
524 *word_len = 0;
525 while (**data) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200526 switch (**data) {
527 case '\'':
528 case '\"':
529 if (*word_len) {
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200530 /* invalid - quotes cannot be in unquoted string and only optsep, ; or { can follow it */
David Sedlákb3077192019-06-19 10:55:37 +0200531 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, *data,
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200532 "unquoted string character, optsep, semicolon or opening brace");
533 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200534 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200535 if (flags) {
536 (*flags) |= (**data) == '\'' ? LYS_SINGLEQUOTED : LYS_DOUBLEQUOTED;
537 }
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100538 LY_CHECK_RET(read_qstring(ctx, data, arg, word_p, word_b, word_len, &buf_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200539 goto str_end;
540 case '/':
Radek Krejci44ceedc2018-10-02 15:54:31 +0200541 if ((*data)[1] == '/') {
542 /* one-line comment */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200543 MOVE_INPUT(ctx, data, 2);
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100544 LY_CHECK_RET(skip_comment(ctx, data, 1));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200545 } else if ((*data)[1] == '*') {
546 /* block comment */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200547 MOVE_INPUT(ctx, data, 2);
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100548 LY_CHECK_RET(skip_comment(ctx, data, 2));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200549 } else {
550 /* not a comment after all */
David Sedlák40bb13b2019-07-10 14:34:18 +0200551 LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, &buf_len, 0, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200552 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200553 break;
554 case ' ':
555 if (*word_len) {
556 /* word is finished */
557 goto str_end;
558 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200559 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200560 break;
561 case '\t':
562 if (*word_len) {
563 /* word is finished */
564 goto str_end;
565 }
566 /* tabs count for 8 spaces */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200567 ctx->indent += 8;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200568
569 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200570 break;
571 case '\n':
572 if (*word_len) {
573 /* word is finished */
574 goto str_end;
575 }
576 /* reset indent */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200577 ctx->indent = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200578
579 /* track line numbers */
580 ++ctx->line;
581
582 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200583 break;
584 case ';':
585 case '{':
586 if (*word_len || (arg == Y_MAYBE_STR_ARG)) {
587 /* word is finished */
588 goto str_end;
589 }
590
David Sedlákb3077192019-06-19 10:55:37 +0200591 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, *data, "an argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200592 return LY_EVALID;
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200593 case '}':
594 /* invalid - braces cannot be in unquoted string (opening braces terminates the string and can follow it) */
David Sedlákb3077192019-06-19 10:55:37 +0200595 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, *data,
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200596 "unquoted string character, optsep, semicolon or opening brace");
597 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200598 default:
David Sedlák40bb13b2019-07-10 14:34:18 +0200599 LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, &buf_len, 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200600 break;
601 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200602 }
603
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200604 /* unexpected end of loop */
David Sedlákb3077192019-06-19 10:55:37 +0200605 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200606 return LY_EVALID;
607
Michal Vasko7fbc8162018-09-17 10:35:16 +0200608str_end:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200609 /* terminating NULL byte for buf */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200610 if (*word_b) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200611 (*word_b) = ly_realloc(*word_b, (*word_len) + 1);
612 LY_CHECK_ERR_RET(!(*word_b), LOGMEM(ctx->ctx), LY_EMEM);
613 (*word_b)[*word_len] = '\0';
Michal Vasko7fbc8162018-09-17 10:35:16 +0200614 *word_p = *word_b;
615 }
616
617 return LY_SUCCESS;
618}
619
Michal Vaskoea5abea2018-09-18 13:10:54 +0200620/**
621 * @brief Get another YANG keyword from the raw data.
622 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200623 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200624 * @param[in,out] data Data to read from, always moved to currently handled character.
625 * @param[out] kw YANG keyword read.
626 * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances.
627 * @param[out] word_len Length of the keyword in the data. Useful for extension instances.
628 *
629 * @return LY_ERR values.
630 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200631LY_ERR
Radek Krejcid6b76452019-09-03 17:03:03 +0200632get_keyword(struct lys_parser_ctx *ctx, const char **data, enum ly_stmt *kw, char **word_p, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200633{
Michal Vasko7fbc8162018-09-17 10:35:16 +0200634 int prefix;
635 const char *word_start;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200636 unsigned int c;
637 size_t len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200638
639 if (word_p) {
640 *word_p = NULL;
641 *word_len = 0;
642 }
643
644 /* first skip "optsep", comments */
645 while (**data) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200646 switch (**data) {
647 case '/':
648 if ((*data)[1] == '/') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200649 /* one-line comment */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200650 MOVE_INPUT(ctx, data, 2);
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100651 LY_CHECK_RET(skip_comment(ctx, data, 1));
Radek Krejcidcc7b322018-10-11 14:24:02 +0200652 } else if ((*data)[1] == '*') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200653 /* block comment */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200654 MOVE_INPUT(ctx, data, 2);
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100655 LY_CHECK_RET(skip_comment(ctx, data, 2));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200656 } else {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200657 /* error - not a comment after all, keyword cannot start with slash */
David Sedlákb3077192019-06-19 10:55:37 +0200658 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '/'.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200659 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200660 }
Radek Krejci13028282019-06-11 14:56:48 +0200661 continue;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200662 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200663 /* skip whitespaces (optsep) */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200664 ++ctx->line;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200665 ctx->indent = 0;
666 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200667 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200668 /* skip whitespaces (optsep) */
669 ++ctx->indent;
670 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200671 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200672 /* skip whitespaces (optsep) */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200673 ctx->indent += 8;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200674 break;
675 default:
676 /* either a keyword start or an invalid character */
677 goto keyword_start;
678 }
679
680 ++(*data);
681 }
682
683keyword_start:
684 word_start = *data;
David Sedlák5f8f0332019-06-18 16:34:30 +0200685 *kw = lysp_match_kw(ctx, data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200686
Radek Krejcid6b76452019-09-03 17:03:03 +0200687 if (*kw == LY_STMT_SYNTAX_SEMICOLON || *kw == LY_STMT_SYNTAX_LEFT_BRACE || *kw == LY_STMT_SYNTAX_RIGHT_BRACE) {
Radek Krejci626df482018-10-11 15:06:31 +0200688 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200689 }
690
Radek Krejcid6b76452019-09-03 17:03:03 +0200691 if (*kw != LY_STMT_NONE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200692 /* make sure we have the whole keyword */
693 switch (**data) {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200694 case '\n':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200695 case '\t':
Radek Krejciabdd8062019-06-11 16:44:19 +0200696 case ' ':
697 /* mandatory "sep" is just checked, not eaten so nothing in the context is updated */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200698 break;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200699 case ':':
700 /* keyword is not actually a keyword, but prefix of an extension.
701 * To avoid repeated check of the prefix syntax, move to the point where the colon was read
702 * and we will be checking the keyword (extension instance) itself */
703 prefix = 1;
704 MOVE_INPUT(ctx, data, 1);
705 goto extension;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200706 case '{':
707 /* allowed only for input and output statements which can be without arguments */
Radek Krejcid6b76452019-09-03 17:03:03 +0200708 if (*kw == LY_STMT_INPUT || *kw == LY_STMT_OUTPUT) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200709 break;
710 }
711 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200712 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200713 MOVE_INPUT(ctx, data, 1);
David Sedlákb3077192019-06-19 10:55:37 +0200714 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(*data - word_start), word_start,
Radek Krejci44ceedc2018-10-02 15:54:31 +0200715 "a keyword followed by a separator");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200716 return LY_EVALID;
717 }
718 } else {
719 /* still can be an extension */
720 prefix = 0;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200721extension:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200722 while (**data && (**data != ' ') && (**data != '\t') && (**data != '\n') && (**data != '{') && (**data != ';')) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200723 LY_CHECK_ERR_RET(ly_getutf8(data, &c, &len),
David Sedlákb3077192019-06-19 10:55:37 +0200724 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, (*data)[-len]), LY_EVALID);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200725 ++ctx->indent;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200726 /* check character validity */
David Sedlák4a650532019-07-10 11:55:18 +0200727 LY_CHECK_RET(lysp_check_identifierchar(ctx, c, *data - len == word_start ? 1 : 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200728 }
729 if (!**data) {
David Sedlákb3077192019-06-19 10:55:37 +0200730 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200731 return LY_EVALID;
732 }
733
734 /* prefix is mandatory for extension instances */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200735 if (prefix != 2) {
David Sedlákb3077192019-06-19 10:55:37 +0200736 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(*data - word_start), word_start, "a keyword");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200737 return LY_EVALID;
738 }
739
Radek Krejcid6b76452019-09-03 17:03:03 +0200740 *kw = LY_STMT_EXTENSION_INSTANCE;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200741 }
Radek Krejci626df482018-10-11 15:06:31 +0200742success:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200743 if (word_p) {
744 *word_p = (char *)word_start;
745 *word_len = *data - word_start;
746 }
747
748 return LY_SUCCESS;
749}
750
Michal Vaskoea5abea2018-09-18 13:10:54 +0200751/**
752 * @brief Parse extension instance substatements.
753 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200754 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200755 * @param[in,out] data Data to read from, always moved to currently handled character.
Radek Krejci335332a2019-09-05 13:03:35 +0200756 * @param[in] kw Statement keyword value matching @p word value.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200757 * @param[in] word Extension instance substatement name (keyword).
758 * @param[in] word_len Extension instance substatement name length.
759 * @param[in,out] child Children of this extension instance to add to.
760 *
761 * @return LY_ERR values.
762 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200763static LY_ERR
Radek Krejci335332a2019-09-05 13:03:35 +0200764parse_ext_substmt(struct lys_parser_ctx *ctx, const char **data, enum ly_stmt kw, char *word, size_t word_len,
Michal Vasko7fbc8162018-09-17 10:35:16 +0200765 struct lysp_stmt **child)
766{
767 char *buf;
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100768 LY_ERR ret = LY_SUCCESS;
Radek Krejci335332a2019-09-05 13:03:35 +0200769 enum ly_stmt child_kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200770 struct lysp_stmt *stmt, *par_child;
771
772 stmt = calloc(1, sizeof *stmt);
773 LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM);
774
Radek Krejcibb9b1982019-04-08 14:24:59 +0200775 /* insert into parent statements */
776 if (!*child) {
777 *child = stmt;
778 } else {
779 for (par_child = *child; par_child->next; par_child = par_child->next);
780 par_child->next = stmt;
781 }
782
Radek Krejci44ceedc2018-10-02 15:54:31 +0200783 stmt->stmt = lydict_insert(ctx->ctx, word, word_len);
Radek Krejci335332a2019-09-05 13:03:35 +0200784 stmt->kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200785
786 /* get optional argument */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200787 LY_CHECK_RET(get_argument(ctx, data, Y_MAYBE_STR_ARG, &stmt->flags, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200788
Radek Krejci0ae092d2018-09-20 16:43:19 +0200789 if (word) {
790 if (buf) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200791 stmt->arg = lydict_insert_zc(ctx->ctx, word);
Radek Krejci0ae092d2018-09-20 16:43:19 +0200792 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200793 stmt->arg = lydict_insert(ctx->ctx, word, word_len);
Radek Krejci0ae092d2018-09-20 16:43:19 +0200794 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200795 }
796
Radek Krejci335332a2019-09-05 13:03:35 +0200797 YANG_READ_SUBSTMT_FOR(ctx, data, child_kw, word, word_len, ret, ) {
798 LY_CHECK_RET(parse_ext_substmt(ctx, data, child_kw, word, word_len, &stmt->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200799 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200800 return ret;
801}
802
Michal Vaskoea5abea2018-09-18 13:10:54 +0200803/**
804 * @brief Parse extension instance.
805 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200806 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200807 * @param[in,out] data Data to read from, always moved to currently handled character.
808 * @param[in] ext_name Extension instance substatement name (keyword).
809 * @param[in] ext_name_len Extension instance substatement name length.
810 * @param[in] insubstmt Type of the keyword this extension instance is a substatement of.
811 * @param[in] insubstmt_index Index of the keyword instance this extension instance is a substatement of.
812 * @param[in,out] exts Extension instances to add to.
813 *
814 * @return LY_ERR values.
815 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200816static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200817parse_ext(struct lys_parser_ctx *ctx, const char **data, const char *ext_name, int ext_name_len, LYEXT_SUBSTMT insubstmt,
Michal Vasko7fbc8162018-09-17 10:35:16 +0200818 uint32_t insubstmt_index, struct lysp_ext_instance **exts)
819{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100820 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200821 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200822 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200823 struct lysp_ext_instance *e;
Radek Krejcid6b76452019-09-03 17:03:03 +0200824 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200825
Radek Krejci2c4e7172018-10-19 15:56:26 +0200826 LY_ARRAY_NEW_RET(ctx->ctx, *exts, e, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200827
828 /* store name and insubstmt info */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200829 e->name = lydict_insert(ctx->ctx, ext_name, ext_name_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200830 e->insubstmt = insubstmt;
831 e->insubstmt_index = insubstmt_index;
832
833 /* get optional argument */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200834 LY_CHECK_RET(get_argument(ctx, data, Y_MAYBE_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200835
Radek Krejci0ae092d2018-09-20 16:43:19 +0200836 if (word) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200837 INSERT_WORD(ctx, buf, e->argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200838 }
839
Radek Krejci6d6556c2018-11-08 09:37:45 +0100840 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Radek Krejci335332a2019-09-05 13:03:35 +0200841 LY_CHECK_RET(parse_ext_substmt(ctx, data, kw, word, word_len, &e->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200842 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200843 return ret;
844}
845
Michal Vaskoea5abea2018-09-18 13:10:54 +0200846/**
847 * @brief Parse a generic text field without specific constraints. Those are contact, organization,
848 * description, etc...
849 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200850 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200851 * @param[in,out] data Data to read from, always moved to currently handled character.
852 * @param[in] substmt Type of this substatement.
853 * @param[in] substmt_index Index of this substatement.
854 * @param[in,out] value Place to store the parsed value.
855 * @param[in] arg Type of the YANG keyword argument (of the value).
856 * @param[in,out] exts Extension instances to add to.
857 *
858 * @return LY_ERR values.
859 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200860static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200861parse_text_field(struct lys_parser_ctx *ctx, const char **data, LYEXT_SUBSTMT substmt, uint32_t substmt_index,
Michal Vasko7fbc8162018-09-17 10:35:16 +0200862 const char **value, enum yang_arg arg, struct lysp_ext_instance **exts)
863{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100864 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200865 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200866 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200867 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200868
869 if (*value) {
David Sedlákb3077192019-06-19 10:55:37 +0200870 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200871 return LY_EVALID;
872 }
873
874 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200875 LY_CHECK_RET(get_argument(ctx, data, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200876
877 /* store value and spend buf if allocated */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200878 INSERT_WORD(ctx, buf, *value, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200879
Radek Krejci6d6556c2018-11-08 09:37:45 +0100880 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200881 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200882 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100883 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, substmt, substmt_index, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200884 break;
885 default:
David Sedlákb3077192019-06-19 10:55:37 +0200886 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200887 return LY_EVALID;
888 }
889 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200890 return ret;
891}
892
Michal Vaskoea5abea2018-09-18 13:10:54 +0200893/**
894 * @brief Parse the yang-version statement.
895 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200896 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200897 * @param[in,out] data Data to read from, always moved to currently handled character.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100898 * @param[out] version Storage for the parsed information.
899 * @param[in, out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200900 *
901 * @return LY_ERR values.
902 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200903static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200904parse_yangversion(struct lys_parser_ctx *ctx, const char **data, uint8_t *version, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200905{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100906 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200907 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200908 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200909 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200910
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100911 if (*version) {
David Sedlákb3077192019-06-19 10:55:37 +0200912 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200913 return LY_EVALID;
914 }
915
916 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200917 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200918
919 if ((word_len == 3) && !strncmp(word, "1.0", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100920 *version = LYS_VERSION_1_0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200921 } else if ((word_len == 3) && !strncmp(word, "1.1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100922 *version = LYS_VERSION_1_1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200923 } else {
David Sedlákb3077192019-06-19 10:55:37 +0200924 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200925 free(buf);
926 return LY_EVALID;
927 }
928 free(buf);
929
Radek Krejci6d6556c2018-11-08 09:37:45 +0100930 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200931 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200932 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100933 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_VERSION, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200934 break;
935 default:
David Sedlákb3077192019-06-19 10:55:37 +0200936 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200937 return LY_EVALID;
938 }
939 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200940 return ret;
941}
942
Michal Vaskoea5abea2018-09-18 13:10:54 +0200943/**
944 * @brief Parse the belongs-to statement.
945 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200946 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200947 * @param[in,out] data Data to read from, always moved to currently handled character.
948 * @param[in,out] belongsto Place to store the parsed value.
949 * @param[in,out] prefix Place to store the parsed belongs-to prefix value.
950 * @param[in,out] exts Extension instances to add to.
951 *
952 * @return LY_ERR values.
953 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200954static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200955parse_belongsto(struct lys_parser_ctx *ctx, const char **data, const char **belongsto, const char **prefix, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200956{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100957 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200958 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200959 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200960 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200961
962 if (*belongsto) {
David Sedlákb3077192019-06-19 10:55:37 +0200963 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200964 return LY_EVALID;
965 }
966
967 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200968 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200969
Radek Krejci44ceedc2018-10-02 15:54:31 +0200970 INSERT_WORD(ctx, buf, *belongsto, word, word_len);
Radek Krejcif09e4e82019-06-14 15:08:11 +0200971
Radek Krejci6d6556c2018-11-08 09:37:45 +0100972 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200973 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200974 case LY_STMT_PREFIX:
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100975 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_PREFIX, 0, prefix, Y_IDENTIF_ARG, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200976 break;
Radek Krejcid6b76452019-09-03 17:03:03 +0200977 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100978 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_BELONGSTO, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200979 break;
980 default:
David Sedlákb3077192019-06-19 10:55:37 +0200981 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200982 return LY_EVALID;
983 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200984 }
Radek Krejcic59bc972018-09-17 16:13:06 +0200985 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +0100986checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200987 /* mandatory substatements */
988 if (!*prefix) {
David Sedlákb3077192019-06-19 10:55:37 +0200989 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200990 return LY_EVALID;
991 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200992 return ret;
993}
994
Michal Vaskoea5abea2018-09-18 13:10:54 +0200995/**
996 * @brief Parse the revision-date statement.
997 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200998 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200999 * @param[in,out] data Data to read from, always moved to currently handled character.
1000 * @param[in,out] rev Array to store the parsed value in.
1001 * @param[in,out] exts Extension instances to add to.
1002 *
1003 * @return LY_ERR values.
1004 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001005static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001006parse_revisiondate(struct lys_parser_ctx *ctx, const char **data, char *rev, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001007{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001008 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001009 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001010 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001011 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001012
1013 if (rev[0]) {
David Sedlákb3077192019-06-19 10:55:37 +02001014 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001015 return LY_EVALID;
1016 }
1017
1018 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001019 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001020
1021 /* check value */
Radek Krejcibbe09a92018-11-08 09:36:54 +01001022 if (lysp_check_date(ctx, word, word_len, "revision-date")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001023 free(buf);
1024 return LY_EVALID;
1025 }
1026
1027 /* store value and spend buf if allocated */
1028 strncpy(rev, word, word_len);
1029 free(buf);
1030
Radek Krejci6d6556c2018-11-08 09:37:45 +01001031 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001032 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001033 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001034 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_REVISIONDATE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001035 break;
1036 default:
David Sedlákb3077192019-06-19 10:55:37 +02001037 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001038 return LY_EVALID;
1039 }
1040 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001041 return ret;
1042}
1043
Michal Vaskoea5abea2018-09-18 13:10:54 +02001044/**
1045 * @brief Parse the include statement.
1046 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001047 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001048 * @param[in] module_name Name of the module to check name collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001049 * @param[in,out] data Data to read from, always moved to currently handled character.
1050 * @param[in,out] includes Parsed includes to add to.
1051 *
1052 * @return LY_ERR values.
1053 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001054static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001055parse_include(struct lys_parser_ctx *ctx, const char *module_name, const char **data, struct lysp_include **includes)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001056{
Radek Krejcid33273d2018-10-25 14:55:52 +02001057 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001058 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001059 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001060 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001061 struct lysp_include *inc;
1062
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001063 LY_ARRAY_NEW_RET(ctx->ctx, *includes, inc, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001064
1065 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001066 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001067
Radek Krejci086c7132018-10-26 15:29:04 +02001068 INSERT_WORD(ctx, buf, inc->name, word, word_len);
1069
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001070 /* submodules share the namespace with the module names, so there must not be
1071 * a module of the same name in the context, no need for revision matching */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001072 if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(ctx->ctx, inc->name)) {
David Sedlákb3077192019-06-19 10:55:37 +02001073 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Name collision between module and submodule of name \"%s\".", inc->name);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001074 return LY_EVALID;
1075 }
1076
Radek Krejci6d6556c2018-11-08 09:37:45 +01001077 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001078 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001079 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001080 PARSER_CHECK_STMTVER2_RET(ctx, "description", "include");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001081 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &inc->dsc, Y_STR_ARG, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001082 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001083 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001084 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "include");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001085 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &inc->ref, Y_STR_ARG, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001086 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001087 case LY_STMT_REVISION_DATE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001088 LY_CHECK_RET(parse_revisiondate(ctx, data, inc->rev, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001089 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001090 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001091 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001092 break;
1093 default:
David Sedlákb3077192019-06-19 10:55:37 +02001094 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001095 return LY_EVALID;
1096 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001097 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001098 return ret;
1099}
1100
Michal Vaskoea5abea2018-09-18 13:10:54 +02001101/**
1102 * @brief Parse the import statement.
1103 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001104 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001105 * @param[in] module_prefix Prefix of the module to check prefix collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001106 * @param[in,out] data Data to read from, always moved to currently handled character.
1107 * @param[in,out] imports Parsed imports to add to.
1108 *
1109 * @return LY_ERR values.
1110 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001111static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001112parse_import(struct lys_parser_ctx *ctx, const char *module_prefix, const char **data, struct lysp_import **imports)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001113{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001114 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001115 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001116 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001117 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001118 struct lysp_import *imp;
1119
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001120 LY_ARRAY_NEW_RET(ctx->ctx, *imports, imp, LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001121
1122 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001123 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02001124 INSERT_WORD(ctx, buf, imp->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001125
Radek Krejci6d6556c2018-11-08 09:37:45 +01001126 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001127 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001128 case LY_STMT_PREFIX:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001129 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_PREFIX, 0, &imp->prefix, Y_IDENTIF_ARG, &imp->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001130 LY_CHECK_RET(lysp_check_prefix(ctx, *imports, module_prefix, &imp->prefix), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001131 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001132 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001133 PARSER_CHECK_STMTVER2_RET(ctx, "description", "import");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001134 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &imp->dsc, Y_STR_ARG, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001135 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001136 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001137 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "import");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001138 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &imp->ref, Y_STR_ARG, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001139 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001140 case LY_STMT_REVISION_DATE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001141 LY_CHECK_RET(parse_revisiondate(ctx, data, imp->rev, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001142 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001143 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001144 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001145 break;
1146 default:
David Sedlákb3077192019-06-19 10:55:37 +02001147 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001148 return LY_EVALID;
1149 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001150 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001151 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001152checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001153 /* mandatory substatements */
David Sedlákb3077192019-06-19 10:55:37 +02001154 LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001155
1156 return ret;
1157}
1158
Michal Vaskoea5abea2018-09-18 13:10:54 +02001159/**
1160 * @brief Parse the revision statement.
1161 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001162 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001163 * @param[in,out] data Data to read from, always moved to currently handled character.
1164 * @param[in,out] revs Parsed revisions to add to.
1165 *
1166 * @return LY_ERR values.
1167 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001168static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001169parse_revision(struct lys_parser_ctx *ctx, const char **data, struct lysp_revision **revs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001170{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001171 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001172 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001173 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001174 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001175 struct lysp_revision *rev;
1176
Radek Krejci2c4e7172018-10-19 15:56:26 +02001177 LY_ARRAY_NEW_RET(ctx->ctx, *revs, rev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001178
1179 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001180 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001181
1182 /* check value */
Radek Krejcibbe09a92018-11-08 09:36:54 +01001183 if (lysp_check_date(ctx, word, word_len, "revision")) {
David Sedlák68ef3dc2019-07-22 13:40:19 +02001184 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001185 return LY_EVALID;
1186 }
1187
Radek Krejcib7db73a2018-10-24 14:18:40 +02001188 strncpy(rev->date, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001189 free(buf);
1190
Radek Krejci6d6556c2018-11-08 09:37:45 +01001191 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001192 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001193 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001194 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &rev->dsc, Y_STR_ARG, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001195 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001196 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001197 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &rev->ref, Y_STR_ARG, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001198 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001199 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001200 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001201 break;
1202 default:
David Sedlákb3077192019-06-19 10:55:37 +02001203 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001204 return LY_EVALID;
1205 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001206 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001207 return ret;
1208}
1209
Michal Vaskoea5abea2018-09-18 13:10:54 +02001210/**
1211 * @brief Parse a generic text field that can have more instances such as base.
1212 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001213 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001214 * @param[in,out] data Data to read from, always moved to currently handled character.
1215 * @param[in] substmt Type of this substatement.
1216 * @param[in,out] texts Parsed values to add to.
1217 * @param[in] arg Type of the expected argument.
1218 * @param[in,out] exts Extension instances to add to.
1219 *
1220 * @return LY_ERR values.
1221 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001222static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001223parse_text_fields(struct lys_parser_ctx *ctx, const char **data, LYEXT_SUBSTMT substmt, const char ***texts, enum yang_arg arg,
Michal Vasko7fbc8162018-09-17 10:35:16 +02001224 struct lysp_ext_instance **exts)
1225{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001226 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001227 char *buf, *word;
Radek Krejci151a5b72018-10-19 14:21:44 +02001228 const char **item;
1229 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001230 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001231
1232 /* allocate new pointer */
Radek Krejci2c4e7172018-10-19 15:56:26 +02001233 LY_ARRAY_NEW_RET(ctx->ctx, *texts, item, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001234
1235 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001236 LY_CHECK_RET(get_argument(ctx, data, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001237
Radek Krejci151a5b72018-10-19 14:21:44 +02001238 INSERT_WORD(ctx, buf, *item, word, word_len);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001239 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001240 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001241 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001242 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, substmt, LY_ARRAY_SIZE(*texts) - 1, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001243 break;
1244 default:
David Sedlákb3077192019-06-19 10:55:37 +02001245 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001246 return LY_EVALID;
1247 }
1248 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001249 return ret;
1250}
1251
Michal Vaskoea5abea2018-09-18 13:10:54 +02001252/**
1253 * @brief Parse the config statement.
1254 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001255 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001256 * @param[in,out] data Data to read from, always moved to currently handled character.
1257 * @param[in,out] flags Flags to add to.
1258 * @param[in,out] exts Extension instances to add to.
1259 *
1260 * @return LY_ERR values.
1261 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001262static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001263parse_config(struct lys_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001264{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001265 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001266 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001267 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001268 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001269
1270 if (*flags & LYS_CONFIG_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001271 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001272 return LY_EVALID;
1273 }
1274
1275 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001276 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001277
1278 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1279 *flags |= LYS_CONFIG_W;
1280 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1281 *flags |= LYS_CONFIG_R;
1282 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001283 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001284 free(buf);
1285 return LY_EVALID;
1286 }
1287 free(buf);
1288
Radek Krejci6d6556c2018-11-08 09:37:45 +01001289 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001290 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001291 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001292 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_CONFIG, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001293 break;
1294 default:
David Sedlákb3077192019-06-19 10:55:37 +02001295 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001296 return LY_EVALID;
1297 }
1298 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001299 return ret;
1300}
1301
Michal Vaskoea5abea2018-09-18 13:10:54 +02001302/**
1303 * @brief Parse the mandatory statement.
1304 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001305 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001306 * @param[in,out] data Data to read from, always moved to currently handled character.
1307 * @param[in,out] flags Flags to add to.
1308 * @param[in,out] exts Extension instances to add to.
1309 *
1310 * @return LY_ERR values.
1311 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001312static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001313parse_mandatory(struct lys_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001314{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001315 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001316 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001317 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001318 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001319
1320 if (*flags & LYS_MAND_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001321 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001322 return LY_EVALID;
1323 }
1324
1325 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001326 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001327
1328 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1329 *flags |= LYS_MAND_TRUE;
1330 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1331 *flags |= LYS_MAND_FALSE;
1332 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001333 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001334 free(buf);
1335 return LY_EVALID;
1336 }
1337 free(buf);
1338
Radek Krejci6d6556c2018-11-08 09:37:45 +01001339 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001340 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001341 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001342 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MANDATORY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001343 break;
1344 default:
David Sedlákb3077192019-06-19 10:55:37 +02001345 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001346 return LY_EVALID;
1347 }
1348 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001349 return ret;
1350}
1351
Michal Vaskoea5abea2018-09-18 13:10:54 +02001352/**
1353 * @brief Parse a restriction such as range or length.
1354 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001355 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001356 * @param[in,out] data Data to read from, always moved to currently handled character.
1357 * @param[in] restr_kw Type of this particular restriction.
1358 * @param[in,out] exts Extension instances to add to.
1359 *
1360 * @return LY_ERR values.
1361 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001362static LY_ERR
Radek Krejcid6b76452019-09-03 17:03:03 +02001363parse_restr(struct lys_parser_ctx *ctx, const char **data, enum ly_stmt restr_kw, struct lysp_restr *restr)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001364{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001365 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001366 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001367 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001368 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001369
1370 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001371 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001372
David Sedlákb9b892c2019-07-12 14:44:02 +02001373 YANG_CHECK_NONEMPTY(ctx, word_len, ly_stmt2str(restr_kw));
Radek Krejci44ceedc2018-10-02 15:54:31 +02001374 INSERT_WORD(ctx, buf, restr->arg, word, word_len);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001375 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001376 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001377 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001378 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001379 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001380 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001381 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001382 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001383 case LY_STMT_ERROR_APP_TAG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001384 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRTAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001385 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001386 case LY_STMT_ERROR_MESSAGE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001387 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRMSG, 0, &restr->emsg, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001388 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001389 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001390 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001391 break;
1392 default:
David Sedlákb3077192019-06-19 10:55:37 +02001393 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001394 return LY_EVALID;
1395 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001396 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001397 return ret;
1398}
1399
Michal Vaskoea5abea2018-09-18 13:10:54 +02001400/**
1401 * @brief Parse a restriction that can have more instances such as must.
1402 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001403 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001404 * @param[in,out] data Data to read from, always moved to currently handled character.
1405 * @param[in] restr_kw Type of this particular restriction.
1406 * @param[in,out] restrs Restrictions to add to.
1407 *
1408 * @return LY_ERR values.
1409 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001410static LY_ERR
Radek Krejcid6b76452019-09-03 17:03:03 +02001411parse_restrs(struct lys_parser_ctx *ctx, const char **data, enum ly_stmt restr_kw, struct lysp_restr **restrs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001412{
1413 struct lysp_restr *restr;
1414
Radek Krejci2c4e7172018-10-19 15:56:26 +02001415 LY_ARRAY_NEW_RET(ctx->ctx, *restrs, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001416 return parse_restr(ctx, data, restr_kw, restr);
1417}
1418
Michal Vaskoea5abea2018-09-18 13:10:54 +02001419/**
1420 * @brief Parse the status statement.
1421 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001422 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001423 * @param[in,out] data Data to read from, always moved to currently handled character.
1424 * @param[in,out] flags Flags to add to.
1425 * @param[in,out] exts Extension instances to add to.
1426 *
1427 * @return LY_ERR values.
1428 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001429static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001430parse_status(struct lys_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001431{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001432 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001433 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001434 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001435 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001436
1437 if (*flags & LYS_STATUS_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001438 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001439 return LY_EVALID;
1440 }
1441
1442 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001443 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001444
1445 if ((word_len == 7) && !strncmp(word, "current", word_len)) {
1446 *flags |= LYS_STATUS_CURR;
1447 } else if ((word_len == 10) && !strncmp(word, "deprecated", word_len)) {
1448 *flags |= LYS_STATUS_DEPRC;
1449 } else if ((word_len == 8) && !strncmp(word, "obsolete", word_len)) {
1450 *flags |= LYS_STATUS_OBSLT;
1451 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001452 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001453 free(buf);
1454 return LY_EVALID;
1455 }
1456 free(buf);
1457
Radek Krejci6d6556c2018-11-08 09:37:45 +01001458 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001459 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001460 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001461 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_STATUS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001462 break;
1463 default:
David Sedlákb3077192019-06-19 10:55:37 +02001464 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001465 return LY_EVALID;
1466 }
1467 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001468 return ret;
1469}
1470
Michal Vaskoea5abea2018-09-18 13:10:54 +02001471/**
1472 * @brief Parse the when statement.
1473 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001474 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001475 * @param[in,out] data Data to read from, always moved to currently handled character.
1476 * @param[in,out] when_p When pointer to parse to.
1477 *
1478 * @return LY_ERR values.
1479 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001480LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001481parse_when(struct lys_parser_ctx *ctx, const char **data, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001482{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001483 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001484 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001485 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001486 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001487 struct lysp_when *when;
1488
1489 if (*when_p) {
David Sedlákb3077192019-06-19 10:55:37 +02001490 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001491 return LY_EVALID;
1492 }
1493
1494 when = calloc(1, sizeof *when);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001495 LY_CHECK_ERR_RET(!when, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001496
1497 /* get value */
Radek Krejci2f54df52019-06-21 10:59:19 +02001498 LY_CHECK_ERR_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len), free(when), LY_EMEM);
David Sedlákb9b892c2019-07-12 14:44:02 +02001499 YANG_CHECK_NONEMPTY(ctx, word_len, "when");
Radek Krejci44ceedc2018-10-02 15:54:31 +02001500 INSERT_WORD(ctx, buf, when->cond, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001501
Radek Krejcif09e4e82019-06-14 15:08:11 +02001502 *when_p = when;
1503
Radek Krejci6d6556c2018-11-08 09:37:45 +01001504 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001505 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001506 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001507 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &when->dsc, Y_STR_ARG, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001508 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001509 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001510 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &when->ref, Y_STR_ARG, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001511 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001512 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001513 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001514 break;
1515 default:
David Sedlákb3077192019-06-19 10:55:37 +02001516 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001517 return LY_EVALID;
1518 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001519 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001520 return ret;
1521}
1522
Michal Vaskoea5abea2018-09-18 13:10:54 +02001523/**
1524 * @brief Parse the anydata or anyxml statement.
1525 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001526 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001527 * @param[in,out] data Data to read from, always moved to currently handled character.
1528 * @param[in] kw Type of this particular keyword.
1529 * @param[in,out] siblings Siblings to add to.
1530 *
1531 * @return LY_ERR values.
1532 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02001533LY_ERR
Radek Krejcid6b76452019-09-03 17:03:03 +02001534parse_any(struct lys_parser_ctx *ctx, const char **data, enum ly_stmt kw, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001535{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001536 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001537 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001538 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001539 struct lysp_node_anydata *any;
1540
David Sedlák60adc092019-08-06 15:57:02 +02001541 /* create new structure and insert into siblings */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02001542 LY_LIST_NEW_RET(ctx->ctx, siblings, any, next, LY_EMEM);
David Sedlák60adc092019-08-06 15:57:02 +02001543
Radek Krejcid6b76452019-09-03 17:03:03 +02001544 any->nodetype = kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001545 any->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001546
Michal Vasko7fbc8162018-09-17 10:35:16 +02001547 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001548 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02001549 INSERT_WORD(ctx, buf, any->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001550
1551 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01001552 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001553 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001554 case LY_STMT_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001555 LY_CHECK_RET(parse_config(ctx, data, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001556 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001557 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001558 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &any->dsc, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001559 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001560 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001561 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &any->iffeatures, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001562 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001563 case LY_STMT_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001564 LY_CHECK_RET(parse_mandatory(ctx, data, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001565 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001566 case LY_STMT_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001567 LY_CHECK_RET(parse_restrs(ctx, data, kw, &any->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001568 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001569 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001570 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &any->ref, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001571 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001572 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001573 LY_CHECK_RET(parse_status(ctx, data, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001574 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001575 case LY_STMT_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001576 LY_CHECK_RET(parse_when(ctx, data, &any->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001577 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001578 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001579 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001580 break;
1581 default:
David Sedlákb3077192019-06-19 10:55:37 +02001582 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
Radek Krejcid6b76452019-09-03 17:03:03 +02001583 (any->nodetype & LYS_ANYDATA) == LYS_ANYDATA ? ly_stmt2str(LY_STMT_ANYDATA) : ly_stmt2str(LY_STMT_ANYXML));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001584 return LY_EVALID;
1585 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001586 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001587 return ret;
1588}
1589
Michal Vaskoea5abea2018-09-18 13:10:54 +02001590/**
1591 * @brief Parse the value or position statement. Substatement of type enum statement.
1592 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001593 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001594 * @param[in,out] data Data to read from, always moved to currently handled character.
1595 * @param[in] val_kw Type of this particular keyword.
1596 * @param[in,out] value Value to write to.
1597 * @param[in,out] flags Flags to write to.
1598 * @param[in,out] exts Extension instances to add to.
1599 *
1600 * @return LY_ERR values.
1601 */
David Sedlákd6ce6d72019-07-16 17:30:18 +02001602LY_ERR
Radek Krejcid6b76452019-09-03 17:03:03 +02001603parse_type_enum_value_pos(struct lys_parser_ctx *ctx, const char **data, enum ly_stmt val_kw, int64_t *value, uint16_t *flags,
Michal Vasko7fbc8162018-09-17 10:35:16 +02001604 struct lysp_ext_instance **exts)
1605{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001606 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001607 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001608 size_t word_len;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02001609 long int num = 0;
1610 unsigned long int unum = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001611 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001612
1613 if (*flags & LYS_SET_VALUE) {
David Sedlákb3077192019-06-19 10:55:37 +02001614 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001615 return LY_EVALID;
1616 }
1617 *flags |= LYS_SET_VALUE;
1618
1619 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001620 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001621
Radek Krejcid6b76452019-09-03 17:03:03 +02001622 if (!word_len || (word[0] == '+') || ((word[0] == '0') && (word_len > 1)) || ((val_kw == LY_STMT_POSITION) && !strncmp(word, "-0", 2))) {
David Sedlákb3077192019-06-19 10:55:37 +02001623 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001624 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001625 }
1626
1627 errno = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001628 if (val_kw == LY_STMT_VALUE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001629 num = strtol(word, &ptr, 10);
Radek Krejci8b764662018-11-14 14:15:13 +01001630 if (num < INT64_C(-2147483648) || num > INT64_C(2147483647)) {
David Sedlákb3077192019-06-19 10:55:37 +02001631 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001632 goto error;
1633 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001634 } else {
1635 unum = strtoul(word, &ptr, 10);
Radek Krejci8b764662018-11-14 14:15:13 +01001636 if (unum > UINT64_C(4294967295)) {
David Sedlákb3077192019-06-19 10:55:37 +02001637 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001638 goto error;
1639 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001640 }
1641 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001642 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001643 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001644 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001645 }
1646 if (errno == ERANGE) {
David Sedlákb3077192019-06-19 10:55:37 +02001647 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001648 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001649 }
Radek Krejcid6b76452019-09-03 17:03:03 +02001650 if (val_kw == LY_STMT_VALUE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001651 *value = num;
1652 } else {
1653 *value = unum;
1654 }
1655 free(buf);
1656
Radek Krejci6d6556c2018-11-08 09:37:45 +01001657 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001658 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001659 case LY_STMT_EXTENSION_INSTANCE:
1660 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, val_kw == LY_STMT_VALUE ? LYEXT_SUBSTMT_VALUE : LYEXT_SUBSTMT_POSITION, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001661 break;
1662 default:
David Sedlákb3077192019-06-19 10:55:37 +02001663 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001664 return LY_EVALID;
1665 }
1666 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001667 return ret;
Radek Krejci8b764662018-11-14 14:15:13 +01001668
1669error:
1670 free(buf);
1671 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001672}
1673
Michal Vaskoea5abea2018-09-18 13:10:54 +02001674/**
1675 * @brief Parse the enum or bit statement. Substatement of type statement.
1676 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001677 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001678 * @param[in,out] data Data to read from, always moved to currently handled character.
1679 * @param[in] enum_kw Type of this particular keyword.
1680 * @param[in,out] enums Enums or bits to add to.
1681 *
1682 * @return LY_ERR values.
1683 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001684static LY_ERR
Radek Krejcid6b76452019-09-03 17:03:03 +02001685parse_type_enum(struct lys_parser_ctx *ctx, const char **data, enum ly_stmt enum_kw, struct lysp_type_enum **enums)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001686{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001687 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001688 char *buf, *word;
David Sedlák6544c182019-07-12 13:17:33 +02001689 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001690 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001691 struct lysp_type_enum *enm;
1692
Radek Krejci2c4e7172018-10-19 15:56:26 +02001693 LY_ARRAY_NEW_RET(ctx->ctx, *enums, enm, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001694
1695 /* get value */
Radek Krejcid6b76452019-09-03 17:03:03 +02001696 LY_CHECK_RET(get_argument(ctx, data, enum_kw == LY_STMT_ENUM ? Y_STR_ARG : Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
1697 if (enum_kw == LY_STMT_ENUM) {
David Sedlák07869a52019-07-12 14:28:19 +02001698 ret = lysp_check_enum_name(ctx, (const char *)word, word_len);
David Sedlák6544c182019-07-12 13:17:33 +02001699 LY_CHECK_ERR_RET(ret, free(buf), ret);
Radek Krejci335332a2019-09-05 13:03:35 +02001700 } /* else nothing specific for YANG_BIT */
Radek Krejci8b764662018-11-14 14:15:13 +01001701
Radek Krejci44ceedc2018-10-02 15:54:31 +02001702 INSERT_WORD(ctx, buf, enm->name, word, word_len);
Radek Krejci8b764662018-11-14 14:15:13 +01001703 CHECK_UNIQUENESS(ctx, *enums, name, ly_stmt2str(enum_kw), enm->name);
1704
Radek Krejci6d6556c2018-11-08 09:37:45 +01001705 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001706 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001707 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001708 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &enm->dsc, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001709 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001710 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02001711 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", ly_stmt2str(enum_kw));
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001712 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &enm->iffeatures, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001713 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001714 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001715 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &enm->ref, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001716 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001717 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001718 LY_CHECK_RET(parse_status(ctx, data, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001719 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001720 case LY_STMT_VALUE:
1721 LY_CHECK_ERR_RET(enum_kw == LY_STMT_BIT, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
David Sedlák9fb515f2019-07-11 10:33:58 +02001722 ly_stmt2str(enum_kw)), LY_EVALID);
1723 LY_CHECK_RET(parse_type_enum_value_pos(ctx, data, kw, &enm->value, &enm->flags, &enm->exts));
1724 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001725 case LY_STMT_POSITION:
1726 LY_CHECK_ERR_RET(enum_kw == LY_STMT_ENUM, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
David Sedlák9fb515f2019-07-11 10:33:58 +02001727 ly_stmt2str(enum_kw)), LY_EVALID);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001728 LY_CHECK_RET(parse_type_enum_value_pos(ctx, data, kw, &enm->value, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001729 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001730 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001731 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001732 break;
1733 default:
David Sedlákb3077192019-06-19 10:55:37 +02001734 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001735 return LY_EVALID;
1736 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001737 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001738 return ret;
1739}
1740
Michal Vaskoea5abea2018-09-18 13:10:54 +02001741/**
1742 * @brief Parse the fraction-digits statement. Substatement of type statement.
1743 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001744 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001745 * @param[in,out] data Data to read from, always moved to currently handled character.
1746 * @param[in,out] fracdig Value to write to.
1747 * @param[in,out] exts Extension instances to add to.
1748 *
1749 * @return LY_ERR values.
1750 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001751static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001752parse_type_fracdigits(struct lys_parser_ctx *ctx, const char **data, uint8_t *fracdig, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001753{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001754 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001755 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001756 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001757 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02001758 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001759
1760 if (*fracdig) {
David Sedlákb3077192019-06-19 10:55:37 +02001761 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001762 return LY_EVALID;
1763 }
1764
1765 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001766 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001767
1768 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
David Sedlákb3077192019-06-19 10:55:37 +02001769 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001770 free(buf);
1771 return LY_EVALID;
1772 }
1773
1774 errno = 0;
1775 num = strtoul(word, &ptr, 10);
1776 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001777 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001778 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001779 free(buf);
1780 return LY_EVALID;
1781 }
1782 if ((errno == ERANGE) || (num > 18)) {
David Sedlákb3077192019-06-19 10:55:37 +02001783 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001784 free(buf);
1785 return LY_EVALID;
1786 }
1787 *fracdig = num;
1788 free(buf);
1789
Radek Krejci6d6556c2018-11-08 09:37:45 +01001790 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001791 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001792 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001793 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_FRACDIGITS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001794 break;
1795 default:
David Sedlákb3077192019-06-19 10:55:37 +02001796 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001797 return LY_EVALID;
1798 }
1799 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001800 return ret;
1801}
1802
Michal Vaskoea5abea2018-09-18 13:10:54 +02001803/**
1804 * @brief Parse the require-instance statement. Substatement of type statement.
1805 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001806 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001807 * @param[in,out] data Data to read from, always moved to currently handled character.
1808 * @param[in,out] reqinst Value to write to.
1809 * @param[in,out] flags Flags to write to.
1810 * @param[in,out] exts Extension instances to add to.
1811 *
1812 * @return LY_ERR values.
1813 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001814static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001815parse_type_reqinstance(struct lys_parser_ctx *ctx, const char **data, uint8_t *reqinst, uint16_t *flags,
Michal Vasko7fbc8162018-09-17 10:35:16 +02001816 struct lysp_ext_instance **exts)
1817{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001818 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001819 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001820 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001821 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001822
1823 if (*flags & LYS_SET_REQINST) {
David Sedlákb3077192019-06-19 10:55:37 +02001824 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001825 return LY_EVALID;
1826 }
1827 *flags |= LYS_SET_REQINST;
1828
1829 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001830 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001831
1832 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1833 *reqinst = 1;
1834 } else if ((word_len != 5) || strncmp(word, "false", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001835 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001836 free(buf);
1837 return LY_EVALID;
1838 }
1839 free(buf);
1840
Radek Krejci6d6556c2018-11-08 09:37:45 +01001841 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001842 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001843 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001844 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_REQINSTANCE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001845 break;
1846 default:
David Sedlákb3077192019-06-19 10:55:37 +02001847 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001848 return LY_EVALID;
1849 }
1850 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001851 return ret;
1852}
1853
Michal Vaskoea5abea2018-09-18 13:10:54 +02001854/**
1855 * @brief Parse the modifier statement. Substatement of type pattern statement.
1856 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001857 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001858 * @param[in,out] data Data to read from, always moved to currently handled character.
1859 * @param[in,out] pat Value to write to.
1860 * @param[in,out] exts Extension instances to add to.
1861 *
1862 * @return LY_ERR values.
1863 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001864static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001865parse_type_pattern_modifier(struct lys_parser_ctx *ctx, const char **data, const char **pat, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001866{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001867 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001868 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001869 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001870 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001871
1872 if ((*pat)[0] == 0x15) {
David Sedlákb3077192019-06-19 10:55:37 +02001873 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001874 return LY_EVALID;
1875 }
1876
1877 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001878 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001879
1880 if ((word_len != 12) || strncmp(word, "invert-match", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001881 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001882 free(buf);
1883 return LY_EVALID;
1884 }
1885 free(buf);
1886
1887 /* replace the value in the dictionary */
1888 buf = malloc(strlen(*pat) + 1);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001889 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001890 strcpy(buf, *pat);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001891 lydict_remove(ctx->ctx, *pat);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001892
1893 assert(buf[0] == 0x06);
1894 buf[0] = 0x15;
Radek Krejci44ceedc2018-10-02 15:54:31 +02001895 *pat = lydict_insert_zc(ctx->ctx, buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001896
Radek Krejci6d6556c2018-11-08 09:37:45 +01001897 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001898 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001899 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001900 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MODIFIER, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001901 break;
1902 default:
David Sedlákb3077192019-06-19 10:55:37 +02001903 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001904 return LY_EVALID;
1905 }
1906 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001907 return ret;
1908}
1909
Michal Vaskoea5abea2018-09-18 13:10:54 +02001910/**
1911 * @brief Parse the pattern statement. Substatement of type statement.
1912 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001913 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001914 * @param[in,out] data Data to read from, always moved to currently handled character.
1915 * @param[in,out] patterns Restrictions to add to.
1916 *
1917 * @return LY_ERR values.
1918 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001919static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001920parse_type_pattern(struct lys_parser_ctx *ctx, const char **data, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001921{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001922 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001923 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001924 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001925 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001926 struct lysp_restr *restr;
1927
Radek Krejci2c4e7172018-10-19 15:56:26 +02001928 LY_ARRAY_NEW_RET(ctx->ctx, *patterns, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001929
1930 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001931 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001932
1933 /* add special meaning first byte */
1934 if (buf) {
1935 buf = realloc(buf, word_len + 2);
1936 word = buf;
1937 } else {
1938 buf = malloc(word_len + 2);
1939 }
Radek Krejci44ceedc2018-10-02 15:54:31 +02001940 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejci86d106e2018-10-18 09:53:19 +02001941 memmove(buf + 1, word, word_len);
1942 buf[0] = 0x06; /* pattern's default regular-match flag */
1943 buf[word_len + 1] = '\0'; /* terminating NULL byte */
1944 restr->arg = lydict_insert_zc(ctx->ctx, buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001945
Radek Krejci6d6556c2018-11-08 09:37:45 +01001946 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001947 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001948 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001949 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001950 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001951 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001952 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001953 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001954 case LY_STMT_ERROR_APP_TAG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001955 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRTAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001956 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001957 case LY_STMT_ERROR_MESSAGE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001958 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRMSG, 0, &restr->emsg, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001959 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001960 case LY_STMT_MODIFIER:
Radek Krejci335332a2019-09-05 13:03:35 +02001961 PARSER_CHECK_STMTVER2_RET(ctx, "modifier", "pattern");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001962 LY_CHECK_RET(parse_type_pattern_modifier(ctx, data, &restr->arg, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001963 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001964 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001965 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001966 break;
1967 default:
David Sedlákb3077192019-06-19 10:55:37 +02001968 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001969 return LY_EVALID;
1970 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001971 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001972 return ret;
1973}
1974
Michal Vaskoea5abea2018-09-18 13:10:54 +02001975/**
1976 * @brief Parse the type statement.
1977 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001978 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001979 * @param[in,out] data Data to read from, always moved to currently handled character.
1980 * @param[in,out] type Type to wrote to.
1981 *
1982 * @return LY_ERR values.
1983 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001984static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001985parse_type(struct lys_parser_ctx *ctx, const char **data, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001986{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001987 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001988 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001989 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001990 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001991 struct lysp_type *nest_type;
1992
1993 if (type->name) {
David Sedlákb3077192019-06-19 10:55:37 +02001994 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001995 return LY_EVALID;
1996 }
1997
1998 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001999 LY_CHECK_RET(get_argument(ctx, data, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002000 INSERT_WORD(ctx, buf, type->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002001
Radek Krejci6d6556c2018-11-08 09:37:45 +01002002 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002003 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002004 case LY_STMT_BASE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002005 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_BASE, &type->bases, Y_PREF_IDENTIF_ARG, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002006 type->flags |= LYS_SET_BASE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002007 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002008 case LY_STMT_BIT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002009 LY_CHECK_RET(parse_type_enum(ctx, data, kw, &type->bits));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002010 type->flags |= LYS_SET_BIT;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002011 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002012 case LY_STMT_ENUM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002013 LY_CHECK_RET(parse_type_enum(ctx, data, kw, &type->enums));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002014 type->flags |= LYS_SET_ENUM;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002015 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002016 case LY_STMT_FRACTION_DIGITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002017 LY_CHECK_RET(parse_type_fracdigits(ctx, data, &type->fraction_digits, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002018 type->flags |= LYS_SET_FRDIGITS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002019 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002020 case LY_STMT_LENGTH:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002021 if (type->length) {
David Sedlákb3077192019-06-19 10:55:37 +02002022 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002023 return LY_EVALID;
2024 }
2025 type->length = calloc(1, sizeof *type->length);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002026 LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002027
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002028 LY_CHECK_RET(parse_restr(ctx, data, kw, type->length));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002029 type->flags |= LYS_SET_LENGTH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002030 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002031 case LY_STMT_PATH:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002032 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_PATH, 0, &type->path, Y_STR_ARG, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002033 type->flags |= LYS_SET_PATH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002034 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002035 case LY_STMT_PATTERN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002036 LY_CHECK_RET(parse_type_pattern(ctx, data, &type->patterns));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002037 type->flags |= LYS_SET_PATTERN;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002038 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002039 case LY_STMT_RANGE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002040 if (type->range) {
David Sedlákb3077192019-06-19 10:55:37 +02002041 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002042 return LY_EVALID;
2043 }
2044 type->range = calloc(1, sizeof *type->range);
David Sedlák7a8b2472019-07-11 15:08:34 +02002045 LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002046
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002047 LY_CHECK_RET(parse_restr(ctx, data, kw, type->range));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002048 type->flags |= LYS_SET_RANGE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002049 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002050 case LY_STMT_REQUIRE_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002051 LY_CHECK_RET(parse_type_reqinstance(ctx, data, &type->require_instance, &type->flags, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002052 /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002053 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002054 case LY_STMT_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002055 LY_ARRAY_NEW_RET(ctx->ctx, type->types, nest_type, LY_EMEM);
2056 LY_CHECK_RET(parse_type(ctx, data, nest_type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002057 type->flags |= LYS_SET_TYPE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002058 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002059 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002060 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &type->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002061 break;
2062 default:
David Sedlákb3077192019-06-19 10:55:37 +02002063 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002064 return LY_EVALID;
2065 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002066 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002067 return ret;
2068}
2069
Michal Vaskoea5abea2018-09-18 13:10:54 +02002070/**
2071 * @brief Parse the leaf statement.
2072 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002073 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002074 * @param[in,out] data Data to read from, always moved to currently handled character.
2075 * @param[in,out] siblings Siblings to add to.
2076 *
2077 * @return LY_ERR values.
2078 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002079LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002080parse_leaf(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002081{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002082 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002083 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002084 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002085 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002086 struct lysp_node_leaf *leaf;
2087
David Sedlák60adc092019-08-06 15:57:02 +02002088 /* create new leaf structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02002089 LY_LIST_NEW_RET(ctx->ctx, siblings, leaf, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002090 leaf->nodetype = LYS_LEAF;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002091 leaf->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002092
Michal Vasko7fbc8162018-09-17 10:35:16 +02002093 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002094 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002095 INSERT_WORD(ctx, buf, leaf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002096
2097 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01002098 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002099 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002100 case LY_STMT_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002101 LY_CHECK_RET(parse_config(ctx, data, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002102 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002103 case LY_STMT_DEFAULT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002104 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &leaf->dflt, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002105 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002106 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002107 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &leaf->dsc, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002108 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002109 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002110 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &leaf->iffeatures, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002111 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002112 case LY_STMT_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002113 LY_CHECK_RET(parse_mandatory(ctx, data, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002114 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002115 case LY_STMT_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002116 LY_CHECK_RET(parse_restrs(ctx, data, kw, &leaf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002117 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002118 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002119 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &leaf->ref, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002120 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002121 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002122 LY_CHECK_RET(parse_status(ctx, data, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002123 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002124 case LY_STMT_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002125 LY_CHECK_RET(parse_type(ctx, data, &leaf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002126 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002127 case LY_STMT_UNITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002128 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, &leaf->units, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002129 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002130 case LY_STMT_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002131 LY_CHECK_RET(parse_when(ctx, data, &leaf->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002132 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002133 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002134 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002135 break;
2136 default:
David Sedlákb3077192019-06-19 10:55:37 +02002137 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002138 return LY_EVALID;
2139 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002140 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002141 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002142checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002143 /* mandatory substatements */
2144 if (!leaf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002145 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002146 return LY_EVALID;
2147 }
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002148 if ((leaf->flags & LYS_MAND_TRUE) && (leaf->dflt)) {
David Sedlákb3077192019-06-19 10:55:37 +02002149 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMSCOMB, "mandatory", "default", "leaf");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002150 return LY_EVALID;
2151 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002152
2153 return ret;
2154}
2155
Michal Vaskoea5abea2018-09-18 13:10:54 +02002156/**
2157 * @brief Parse the max-elements statement.
2158 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002159 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002160 * @param[in,out] data Data to read from, always moved to currently handled character.
2161 * @param[in,out] max Value to write to.
2162 * @param[in,out] flags Flags to write to.
2163 * @param[in,out] exts Extension instances to add to.
2164 *
2165 * @return LY_ERR values.
2166 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002167LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002168parse_maxelements(struct lys_parser_ctx *ctx, const char **data, uint32_t *max, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002169{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002170 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002171 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002172 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002173 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002174 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002175
2176 if (*flags & LYS_SET_MAX) {
David Sedlákb3077192019-06-19 10:55:37 +02002177 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002178 return LY_EVALID;
2179 }
2180 *flags |= LYS_SET_MAX;
2181
2182 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002183 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002184
2185 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
David Sedlákb3077192019-06-19 10:55:37 +02002186 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002187 free(buf);
2188 return LY_EVALID;
2189 }
2190
Radek Krejci7f9b6512019-09-18 13:11:09 +02002191 if (ly_strncmp("unbounded", word, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002192 errno = 0;
2193 num = strtoul(word, &ptr, 10);
2194 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002195 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002196 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002197 free(buf);
2198 return LY_EVALID;
2199 }
2200 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002201 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002202 free(buf);
2203 return LY_EVALID;
2204 }
2205
2206 *max = num;
2207 }
2208 free(buf);
2209
Radek Krejci6d6556c2018-11-08 09:37:45 +01002210 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002211 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002212 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002213 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MAX, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002214 break;
2215 default:
David Sedlákb3077192019-06-19 10:55:37 +02002216 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002217 return LY_EVALID;
2218 }
2219 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002220 return ret;
2221}
2222
Michal Vaskoea5abea2018-09-18 13:10:54 +02002223/**
2224 * @brief Parse the min-elements statement.
2225 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002226 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002227 * @param[in,out] data Data to read from, always moved to currently handled character.
2228 * @param[in,out] min Value to write to.
2229 * @param[in,out] flags Flags to write to.
2230 * @param[in,out] exts Extension instances to add to.
2231 *
2232 * @return LY_ERR values.
2233 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002234LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002235parse_minelements(struct lys_parser_ctx *ctx, const char **data, uint32_t *min, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002236{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002237 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002238 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002239 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002240 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002241 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002242
2243 if (*flags & LYS_SET_MIN) {
David Sedlákb3077192019-06-19 10:55:37 +02002244 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002245 return LY_EVALID;
2246 }
2247 *flags |= LYS_SET_MIN;
2248
2249 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002250 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002251
2252 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
David Sedlákb3077192019-06-19 10:55:37 +02002253 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002254 free(buf);
2255 return LY_EVALID;
2256 }
2257
2258 errno = 0;
2259 num = strtoul(word, &ptr, 10);
2260 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002261 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002262 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002263 free(buf);
2264 return LY_EVALID;
2265 }
2266 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002267 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002268 free(buf);
2269 return LY_EVALID;
2270 }
2271 *min = num;
2272 free(buf);
2273
Radek Krejci6d6556c2018-11-08 09:37:45 +01002274 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002275 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002276 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002277 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MIN, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002278 break;
2279 default:
David Sedlákb3077192019-06-19 10:55:37 +02002280 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002281 return LY_EVALID;
2282 }
2283 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002284 return ret;
2285}
2286
Michal Vaskoea5abea2018-09-18 13:10:54 +02002287/**
2288 * @brief Parse the ordered-by statement.
2289 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002290 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002291 * @param[in,out] data Data to read from, always moved to currently handled character.
2292 * @param[in,out] flags Flags to write to.
2293 * @param[in,out] exts Extension instances to add to.
2294 *
2295 * @return LY_ERR values.
2296 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002297static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002298parse_orderedby(struct lys_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002299{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002300 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002301 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002302 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002303 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002304
2305 if (*flags & LYS_ORDBY_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02002306 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002307 return LY_EVALID;
2308 }
2309
2310 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002311 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002312
2313 if ((word_len == 6) && !strncmp(word, "system", word_len)) {
2314 *flags |= LYS_ORDBY_SYSTEM;
2315 } else if ((word_len == 4) && !strncmp(word, "user", word_len)) {
2316 *flags |= LYS_ORDBY_USER;
2317 } else {
David Sedlákb3077192019-06-19 10:55:37 +02002318 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002319 free(buf);
2320 return LY_EVALID;
2321 }
2322 free(buf);
2323
Radek Krejci6d6556c2018-11-08 09:37:45 +01002324 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002325 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002326 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002327 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_ORDEREDBY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002328 break;
2329 default:
David Sedlákb3077192019-06-19 10:55:37 +02002330 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002331 return LY_EVALID;
2332 }
2333 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002334 return ret;
2335}
2336
Michal Vaskoea5abea2018-09-18 13:10:54 +02002337/**
2338 * @brief Parse the leaf-list statement.
2339 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002340 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002341 * @param[in,out] data Data to read from, always moved to currently handled character.
2342 * @param[in,out] siblings Siblings to add to.
2343 *
2344 * @return LY_ERR values.
2345 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002346LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002347parse_leaflist(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002348{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002349 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002350 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002351 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002352 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002353 struct lysp_node_leaflist *llist;
2354
David Sedlák60adc092019-08-06 15:57:02 +02002355 /* create new leaf-list structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02002356 LY_LIST_NEW_RET(ctx->ctx, siblings, llist, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002357 llist->nodetype = LYS_LEAFLIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002358 llist->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002359
Michal Vasko7fbc8162018-09-17 10:35:16 +02002360 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002361 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002362 INSERT_WORD(ctx, buf, llist->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002363
2364 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01002365 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002366 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002367 case LY_STMT_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002368 LY_CHECK_RET(parse_config(ctx, data, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002369 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002370 case LY_STMT_DEFAULT:
Radek Krejci335332a2019-09-05 13:03:35 +02002371 PARSER_CHECK_STMTVER2_RET(ctx, "default", "leaf-list");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002372 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_DEFAULT, &llist->dflts, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002373 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002374 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002375 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &llist->dsc, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002376 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002377 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002378 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &llist->iffeatures, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002379 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002380 case LY_STMT_MAX_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002381 LY_CHECK_RET(parse_maxelements(ctx, data, &llist->max, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002382 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002383 case LY_STMT_MIN_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002384 LY_CHECK_RET(parse_minelements(ctx, data, &llist->min, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002385 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002386 case LY_STMT_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002387 LY_CHECK_RET(parse_restrs(ctx, data, kw, &llist->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002388 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002389 case LY_STMT_ORDERED_BY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002390 LY_CHECK_RET(parse_orderedby(ctx, data, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002391 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002392 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002393 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &llist->ref, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002394 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002395 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002396 LY_CHECK_RET(parse_status(ctx, data, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002397 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002398 case LY_STMT_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002399 LY_CHECK_RET(parse_type(ctx, data, &llist->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002400 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002401 case LY_STMT_UNITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002402 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, &llist->units, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002403 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002404 case LY_STMT_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002405 LY_CHECK_RET(parse_when(ctx, data, &llist->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002406 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002407 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002408 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002409 break;
2410 default:
David Sedlákb3077192019-06-19 10:55:37 +02002411 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002412 return LY_EVALID;
2413 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002414 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002415 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002416checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002417 /* mandatory substatements */
2418 if (!llist->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002419 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002420 return LY_EVALID;
2421 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01002422 if ((llist->min) && (llist->dflts)) {
David Sedlákb3077192019-06-19 10:55:37 +02002423 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMSCOMB, "min-elements", "default", "leaf-list");
Radek Krejci0e5d8382018-11-28 16:37:53 +01002424 return LY_EVALID;
2425 }
Radek Krejcidf6cad12018-11-28 17:10:55 +01002426 if (llist->max && llist->min > llist->max) {
David Sedlákb3077192019-06-19 10:55:37 +02002427 LOGVAL_PARSER(ctx, LYVE_SEMANTICS,
Radek Krejcidf6cad12018-11-28 17:10:55 +01002428 "Invalid combination of min-elements and max-elements: min value %u is bigger than the max value %u.",
2429 llist->min, llist->max);
2430 return LY_EVALID;
2431 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002432
2433 return ret;
2434}
2435
Michal Vaskoea5abea2018-09-18 13:10:54 +02002436/**
2437 * @brief Parse the refine statement.
2438 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002439 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002440 * @param[in,out] data Data to read from, always moved to currently handled character.
2441 * @param[in,out] refines Refines to add to.
2442 *
2443 * @return LY_ERR values.
2444 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002445static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002446parse_refine(struct lys_parser_ctx *ctx, const char **data, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002447{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002448 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002449 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002450 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002451 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002452 struct lysp_refine *rf;
2453
Radek Krejci2c4e7172018-10-19 15:56:26 +02002454 LY_ARRAY_NEW_RET(ctx->ctx, *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002455
2456 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002457 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
David Sedlákb9b892c2019-07-12 14:44:02 +02002458 YANG_CHECK_NONEMPTY(ctx, word_len, "refine");
Radek Krejci44ceedc2018-10-02 15:54:31 +02002459 INSERT_WORD(ctx, buf, rf->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002460
Radek Krejci6d6556c2018-11-08 09:37:45 +01002461 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002462 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002463 case LY_STMT_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002464 LY_CHECK_RET(parse_config(ctx, data, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002465 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002466 case LY_STMT_DEFAULT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002467 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_DEFAULT, &rf->dflts, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002468 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002469 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002470 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &rf->dsc, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002471 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002472 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02002473 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "refine");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002474 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &rf->iffeatures, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002475 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002476 case LY_STMT_MAX_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002477 LY_CHECK_RET(parse_maxelements(ctx, data, &rf->max, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002478 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002479 case LY_STMT_MIN_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002480 LY_CHECK_RET(parse_minelements(ctx, data, &rf->min, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002481 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002482 case LY_STMT_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002483 LY_CHECK_RET(parse_restrs(ctx, data, kw, &rf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002484 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002485 case LY_STMT_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002486 LY_CHECK_RET(parse_mandatory(ctx, data, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002487 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002488 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002489 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &rf->ref, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002490 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002491 case LY_STMT_PRESENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002492 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_PRESENCE, 0, &rf->presence, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002493 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002494 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002495 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002496 break;
2497 default:
David Sedlákb3077192019-06-19 10:55:37 +02002498 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002499 return LY_EVALID;
2500 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002501 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002502 return ret;
2503}
2504
Michal Vaskoea5abea2018-09-18 13:10:54 +02002505/**
2506 * @brief Parse the typedef statement.
2507 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002508 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002509 * @param[in,out] data Data to read from, always moved to currently handled character.
2510 * @param[in,out] typedefs Typedefs to add to.
2511 *
2512 * @return LY_ERR values.
2513 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002514static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002515parse_typedef(struct lys_parser_ctx *ctx, struct lysp_node *parent, const char **data, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002516{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002517 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002518 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002519 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002520 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002521 struct lysp_tpdf *tpdf;
2522
Radek Krejci2c4e7172018-10-19 15:56:26 +02002523 LY_ARRAY_NEW_RET(ctx->ctx, *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002524
2525 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002526 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002527 INSERT_WORD(ctx, buf, tpdf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002528
2529 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01002530 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002531 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002532 case LY_STMT_DEFAULT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002533 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &tpdf->dflt, Y_STR_ARG, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002534 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002535 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002536 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &tpdf->dsc, Y_STR_ARG, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002537 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002538 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002539 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &tpdf->ref, Y_STR_ARG, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002540 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002541 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002542 LY_CHECK_RET(parse_status(ctx, data, &tpdf->flags, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002543 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002544 case LY_STMT_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002545 LY_CHECK_RET(parse_type(ctx, data, &tpdf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002546 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002547 case LY_STMT_UNITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002548 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, &tpdf->units, Y_STR_ARG, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002549 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002550 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002551 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002552 break;
2553 default:
David Sedlákb3077192019-06-19 10:55:37 +02002554 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002555 return LY_EVALID;
2556 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002557 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002558 LY_CHECK_RET(ret);
Radek Krejcibbe09a92018-11-08 09:36:54 +01002559checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002560 /* mandatory substatements */
2561 if (!tpdf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002562 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002563 return LY_EVALID;
2564 }
2565
Radek Krejcibbe09a92018-11-08 09:36:54 +01002566 /* store data for collision check */
Radek Krejci7fc68292019-06-12 13:51:09 +02002567 if (parent && !(parent->nodetype & (LYS_GROUPING | LYS_ACTION | LYS_INOUT | LYS_NOTIF))) {
David Sedláke8b74df2019-08-14 14:18:22 +02002568 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, parent, 0) == -1, LY_EMEM);
Radek Krejcibbe09a92018-11-08 09:36:54 +01002569 }
2570
Michal Vasko7fbc8162018-09-17 10:35:16 +02002571 return ret;
2572}
2573
Michal Vaskoea5abea2018-09-18 13:10:54 +02002574/**
2575 * @brief Parse the input or output statement.
2576 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002577 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002578 * @param[in,out] data Data to read from, always moved to currently handled character.
2579 * @param[in] kw Type of this particular keyword
2580 * @param[in,out] inout_p Input/output pointer to write to.
2581 *
2582 * @return LY_ERR values.
2583 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002584static LY_ERR
Radek Krejcid6b76452019-09-03 17:03:03 +02002585parse_inout(struct lys_parser_ctx *ctx, const char **data, enum ly_stmt inout_kw, struct lysp_node *parent, struct lysp_action_inout *inout_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002586{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002587 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002588 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002589 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002590 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002591
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002592 if (inout_p->nodetype) {
David Sedlákb3077192019-06-19 10:55:37 +02002593 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002594 return LY_EVALID;
2595 }
2596
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002597 /* initiate structure */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002598 inout_p->nodetype = &((struct lysp_action*)parent)->input == inout_p ? LYS_INPUT : LYS_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002599 inout_p->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002600
2601 /* parse substatements */
Radek Krejci7fc68292019-06-12 13:51:09 +02002602 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002603 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002604 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002605 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", ly_stmt2str(inout_kw));
Radek Krejci10113652018-11-14 16:56:50 +01002606 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002607 case LY_STMT_ANYXML:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002608 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002609 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002610 case LY_STMT_CHOICE:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002611 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002612 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002613 case LY_STMT_CONTAINER:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002614 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002615 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002616 case LY_STMT_LEAF:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002617 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002618 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002619 case LY_STMT_LEAF_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002620 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002621 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002622 case LY_STMT_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002623 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002624 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002625 case LY_STMT_USES:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002626 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002627 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002628 case LY_STMT_TYPEDEF:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002629 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)inout_p, data, &inout_p->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002630 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002631 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002632 PARSER_CHECK_STMTVER2_RET(ctx, "must", ly_stmt2str(inout_kw));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002633 LY_CHECK_RET(parse_restrs(ctx, data, kw, &inout_p->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002634 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002635 case LY_STMT_GROUPING:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002636 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)inout_p, &inout_p->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002637 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002638 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002639 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inout_p->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002640 break;
2641 default:
David Sedlákb3077192019-06-19 10:55:37 +02002642 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002643 return LY_EVALID;
2644 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002645 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002646 LY_CHECK_RET(ret);
Michal Vaskob83af8a2020-01-06 09:49:22 +01002647
Radek Krejci7fc68292019-06-12 13:51:09 +02002648checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002649 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02002650 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, inout_p->groupings, NULL, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02002651
Michal Vaskob83af8a2020-01-06 09:49:22 +01002652 if (!inout_p->data) {
2653 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "data-def-stmt", ly_stmt2str(inout_kw));
2654 return LY_EVALID;
2655 }
2656
Michal Vasko7fbc8162018-09-17 10:35:16 +02002657 return ret;
2658}
2659
Michal Vaskoea5abea2018-09-18 13:10:54 +02002660/**
2661 * @brief Parse the action statement.
2662 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002663 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002664 * @param[in,out] data Data to read from, always moved to currently handled character.
2665 * @param[in,out] actions Actions to add to.
2666 *
2667 * @return LY_ERR values.
2668 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002669LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002670parse_action(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002671{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002672 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002673 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002674 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002675 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002676 struct lysp_action *act;
2677
Radek Krejci2c4e7172018-10-19 15:56:26 +02002678 LY_ARRAY_NEW_RET(ctx->ctx, *actions, act, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002679
2680 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002681 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002682 INSERT_WORD(ctx, buf, act->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002683 act->nodetype = LYS_ACTION;
2684 act->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002685
Radek Krejci7fc68292019-06-12 13:51:09 +02002686 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002687 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002688 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002689 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 +02002690 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002691 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002692 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 +02002693 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002694 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002695 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 +02002696 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002697 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002698 LY_CHECK_RET(parse_status(ctx, data, &act->flags, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002699 break;
2700
Radek Krejcid6b76452019-09-03 17:03:03 +02002701 case LY_STMT_INPUT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002702 LY_CHECK_RET(parse_inout(ctx, data, kw, (struct lysp_node*)act, &act->input));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002703 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002704 case LY_STMT_OUTPUT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002705 LY_CHECK_RET(parse_inout(ctx, data, kw, (struct lysp_node*)act, &act->output));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002706 break;
2707
Radek Krejcid6b76452019-09-03 17:03:03 +02002708 case LY_STMT_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01002709 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)act, data, &act->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002710 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002711 case LY_STMT_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002712 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)act, &act->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002713 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002714 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002715 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002716 break;
2717 default:
David Sedlákb3077192019-06-19 10:55:37 +02002718 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), parent ? "action" : "rpc");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002719 return LY_EVALID;
2720 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002721 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002722 LY_CHECK_RET(ret);
2723checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002724 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02002725 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, act->groupings, NULL, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02002726
Michal Vasko7fbc8162018-09-17 10:35:16 +02002727 return ret;
2728}
2729
Michal Vaskoea5abea2018-09-18 13:10:54 +02002730/**
2731 * @brief Parse the notification statement.
2732 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002733 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002734 * @param[in,out] data Data to read from, always moved to currently handled character.
2735 * @param[in,out] notifs Notifications to add to.
2736 *
2737 * @return LY_ERR values.
2738 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002739LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002740parse_notif(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002741{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002742 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002743 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002744 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002745 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002746 struct lysp_notif *notif;
2747
Radek Krejci2c4e7172018-10-19 15:56:26 +02002748 LY_ARRAY_NEW_RET(ctx->ctx, *notifs, notif, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002749
2750 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002751 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002752 INSERT_WORD(ctx, buf, notif->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002753 notif->nodetype = LYS_NOTIF;
2754 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002755
Radek Krejci7fc68292019-06-12 13:51:09 +02002756 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002757 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002758 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002759 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 +02002760 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002761 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002762 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 +02002763 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002764 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002765 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 +02002766 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002767 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002768 LY_CHECK_RET(parse_status(ctx, data, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002769 break;
2770
Radek Krejcid6b76452019-09-03 17:03:03 +02002771 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002772 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "notification");
Radek Krejci10113652018-11-14 16:56:50 +01002773 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002774 case LY_STMT_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002775 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002776 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002777 case LY_STMT_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002778 LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002779 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002780 case LY_STMT_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002781 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002782 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002783 case LY_STMT_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002784 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002785 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002786 case LY_STMT_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002787 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002788 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002789 case LY_STMT_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002790 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002791 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002792 case LY_STMT_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002793 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002794 break;
2795
Radek Krejcid6b76452019-09-03 17:03:03 +02002796 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002797 PARSER_CHECK_STMTVER2_RET(ctx, "must", "notification");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002798 LY_CHECK_RET(parse_restrs(ctx, data, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002799 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002800 case LY_STMT_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01002801 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)notif, data, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002802 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002803 case LY_STMT_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002804 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002805 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002806 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002807 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002808 break;
2809 default:
David Sedlákb3077192019-06-19 10:55:37 +02002810 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002811 return LY_EVALID;
2812 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002813 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002814 LY_CHECK_RET(ret);
2815checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002816 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02002817 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, notif->groupings, NULL, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02002818
Michal Vasko7fbc8162018-09-17 10:35:16 +02002819 return ret;
2820}
2821
Michal Vaskoea5abea2018-09-18 13:10:54 +02002822/**
2823 * @brief Parse the grouping statement.
2824 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002825 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002826 * @param[in,out] data Data to read from, always moved to currently handled character.
2827 * @param[in,out] groupings Groupings to add to.
2828 *
2829 * @return LY_ERR values.
2830 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002831LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002832parse_grouping(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002833{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002834 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002835 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002836 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002837 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002838 struct lysp_grp *grp;
2839
Radek Krejci2c4e7172018-10-19 15:56:26 +02002840 LY_ARRAY_NEW_RET(ctx->ctx, *groupings, grp, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002841
2842 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002843 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002844 INSERT_WORD(ctx, buf, grp->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002845 grp->nodetype = LYS_GROUPING;
2846 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002847
Radek Krejci7fc68292019-06-12 13:51:09 +02002848 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002849 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002850 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002851 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 +02002852 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002853 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002854 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 +02002855 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002856 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002857 LY_CHECK_RET(parse_status(ctx, data, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002858 break;
2859
Radek Krejcid6b76452019-09-03 17:03:03 +02002860 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002861 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "grouping");
Radek Krejci10113652018-11-14 16:56:50 +01002862 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002863 case LY_STMT_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002864 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002865 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002866 case LY_STMT_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002867 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002868 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002869 case LY_STMT_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002870 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002871 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002872 case LY_STMT_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002873 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002874 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002875 case LY_STMT_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002876 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002877 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002878 case LY_STMT_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002879 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002880 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002881 case LY_STMT_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002882 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002883 break;
2884
Radek Krejcid6b76452019-09-03 17:03:03 +02002885 case LY_STMT_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01002886 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)grp, data, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002887 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002888 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02002889 PARSER_CHECK_STMTVER2_RET(ctx, "action", "grouping");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002890 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)grp, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002891 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002892 case LY_STMT_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002893 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)grp, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002894 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002895 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02002896 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "grouping");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002897 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)grp, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002898 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002899 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002900 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002901 break;
2902 default:
David Sedlákb3077192019-06-19 10:55:37 +02002903 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002904 return LY_EVALID;
2905 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002906 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002907 LY_CHECK_RET(ret);
2908checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002909 /* finalize parent pointers to the reallocated items */
David Sedlák0d6de5a2019-07-22 13:25:44 +02002910 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, grp->groupings, NULL, grp->actions, grp->notifs));
Radek Krejci7fc68292019-06-12 13:51:09 +02002911
Michal Vasko7fbc8162018-09-17 10:35:16 +02002912 return ret;
2913}
2914
Michal Vaskoea5abea2018-09-18 13:10:54 +02002915/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02002916 * @brief Parse the augment statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002917 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002918 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002919 * @param[in,out] data Data to read from, always moved to currently handled character.
2920 * @param[in,out] augments Augments to add to.
2921 *
2922 * @return LY_ERR values.
2923 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002924LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002925parse_augment(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002926{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002927 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002928 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002929 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002930 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002931 struct lysp_augment *aug;
2932
Radek Krejci2c4e7172018-10-19 15:56:26 +02002933 LY_ARRAY_NEW_RET(ctx->ctx, *augments, aug, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002934
2935 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002936 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
David Sedlákb9b892c2019-07-12 14:44:02 +02002937 YANG_CHECK_NONEMPTY(ctx, word_len, "augment");
Radek Krejci44ceedc2018-10-02 15:54:31 +02002938 INSERT_WORD(ctx, buf, aug->nodeid, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002939 aug->nodetype = LYS_AUGMENT;
2940 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002941
Radek Krejci7fc68292019-06-12 13:51:09 +02002942 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002943 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002944 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002945 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 +02002946 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002947 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002948 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 +02002949 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002950 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002951 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 +02002952 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002953 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002954 LY_CHECK_RET(parse_status(ctx, data, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002955 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002956 case LY_STMT_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002957 LY_CHECK_RET(parse_when(ctx, data, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002958 break;
2959
Radek Krejcid6b76452019-09-03 17:03:03 +02002960 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002961 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "augment");
Radek Krejci10113652018-11-14 16:56:50 +01002962 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002963 case LY_STMT_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002964 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002965 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002966 case LY_STMT_CASE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002967 LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002968 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002969 case LY_STMT_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002970 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002971 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002972 case LY_STMT_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002973 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002974 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002975 case LY_STMT_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002976 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002977 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002978 case LY_STMT_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002979 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002980 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002981 case LY_STMT_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002982 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002983 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002984 case LY_STMT_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002985 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002986 break;
2987
Radek Krejcid6b76452019-09-03 17:03:03 +02002988 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02002989 PARSER_CHECK_STMTVER2_RET(ctx, "action", "augment");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002990 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002991 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002992 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02002993 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "augment");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002994 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002995 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002996 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002997 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002998 break;
2999 default:
David Sedlákb3077192019-06-19 10:55:37 +02003000 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003001 return LY_EVALID;
3002 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003003 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003004 LY_CHECK_RET(ret);
3005checks:
3006 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02003007 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, NULL, NULL, aug->actions, aug->notifs));
Radek Krejci7fc68292019-06-12 13:51:09 +02003008
Michal Vasko7fbc8162018-09-17 10:35:16 +02003009 return ret;
3010}
3011
Michal Vaskoea5abea2018-09-18 13:10:54 +02003012/**
3013 * @brief Parse the uses statement.
3014 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003015 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003016 * @param[in,out] data Data to read from, always moved to currently handled character.
3017 * @param[in,out] siblings Siblings to add to.
3018 *
3019 * @return LY_ERR values.
3020 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003021LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003022parse_uses(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003023{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003024 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003025 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003026 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003027 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003028 struct lysp_node_uses *uses;
3029
David Sedlák60adc092019-08-06 15:57:02 +02003030 /* create uses structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003031 LY_LIST_NEW_RET(ctx->ctx, siblings, uses, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003032 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003033 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003034
Michal Vasko7fbc8162018-09-17 10:35:16 +02003035 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003036 LY_CHECK_RET(get_argument(ctx, data, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003037 INSERT_WORD(ctx, buf, uses->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003038
3039 /* parse substatements */
Radek Krejci7fc68292019-06-12 13:51:09 +02003040 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003041 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003042 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003043 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 +02003044 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003045 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003046 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 +02003047 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003048 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003049 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 +02003050 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003051 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003052 LY_CHECK_RET(parse_status(ctx, data, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003053 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003054 case LY_STMT_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003055 LY_CHECK_RET(parse_when(ctx, data, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003056 break;
3057
Radek Krejcid6b76452019-09-03 17:03:03 +02003058 case LY_STMT_REFINE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003059 LY_CHECK_RET(parse_refine(ctx, data, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003060 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003061 case LY_STMT_AUGMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003062 LY_CHECK_RET(parse_augment(ctx, data, (struct lysp_node*)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003063 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003064 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003065 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003066 break;
3067 default:
David Sedlákb3077192019-06-19 10:55:37 +02003068 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003069 return LY_EVALID;
3070 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003071 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003072checks:
3073 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02003074 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, NULL, uses->augments, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02003075
Michal Vasko7fbc8162018-09-17 10:35:16 +02003076 return ret;
3077}
3078
Michal Vaskoea5abea2018-09-18 13:10:54 +02003079/**
3080 * @brief Parse the case statement.
3081 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003082 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003083 * @param[in,out] data Data to read from, always moved to currently handled character.
3084 * @param[in,out] siblings Siblings to add to.
3085 *
3086 * @return LY_ERR values.
3087 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003088LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003089parse_case(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003090{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003091 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003092 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003093 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003094 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003095 struct lysp_node_case *cas;
3096
David Sedlák60adc092019-08-06 15:57:02 +02003097 /* create new case structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003098 LY_LIST_NEW_RET(ctx->ctx, siblings, cas, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003099 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003100 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003101
Michal Vasko7fbc8162018-09-17 10:35:16 +02003102 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003103 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003104 INSERT_WORD(ctx, buf, cas->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003105
3106 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01003107 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003108 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003109 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003110 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 +02003111 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003112 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003113 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 +02003114 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003115 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003116 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 +02003117 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003118 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003119 LY_CHECK_RET(parse_status(ctx, data, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003120 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003121 case LY_STMT_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003122 LY_CHECK_RET(parse_when(ctx, data, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003123 break;
3124
Radek Krejcid6b76452019-09-03 17:03:03 +02003125 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003126 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "case");
Radek Krejci10113652018-11-14 16:56:50 +01003127 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003128 case LY_STMT_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003129 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003130 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003131 case LY_STMT_CHOICE:
Radek Krejcia9026eb2018-12-12 16:04:47 +01003132 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003133 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003134 case LY_STMT_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003135 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003136 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003137 case LY_STMT_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003138 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003139 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003140 case LY_STMT_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003141 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003142 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003143 case LY_STMT_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003144 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003145 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003146 case LY_STMT_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003147 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003148 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003149 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003150 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003151 break;
3152 default:
David Sedlákb3077192019-06-19 10:55:37 +02003153 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003154 return LY_EVALID;
3155 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003156 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003157 return ret;
3158}
3159
Michal Vaskoea5abea2018-09-18 13:10:54 +02003160/**
3161 * @brief Parse the choice statement.
3162 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003163 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003164 * @param[in,out] data Data to read from, always moved to currently handled character.
3165 * @param[in,out] siblings Siblings to add to.
3166 *
3167 * @return LY_ERR values.
3168 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003169LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003170parse_choice(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003171{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003172 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003173 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003174 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003175 enum ly_stmt kw;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003176 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003177
David Sedlák60adc092019-08-06 15:57:02 +02003178 /* create new choice structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003179 LY_LIST_NEW_RET(ctx->ctx, siblings, choice, next, LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003180 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003181 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003182
Michal Vasko7fbc8162018-09-17 10:35:16 +02003183 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003184 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003185 INSERT_WORD(ctx, buf, choice->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003186
3187 /* parse substatements */
Radek Krejcia9026eb2018-12-12 16:04:47 +01003188 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003189 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003190 case LY_STMT_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003191 LY_CHECK_RET(parse_config(ctx, data, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003192 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003193 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003194 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 +02003195 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003196 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003197 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 +02003198 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003199 case LY_STMT_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003200 LY_CHECK_RET(parse_mandatory(ctx, data, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003201 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003202 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003203 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 +02003204 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003205 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003206 LY_CHECK_RET(parse_status(ctx, data, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003207 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003208 case LY_STMT_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003209 LY_CHECK_RET(parse_when(ctx, data, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003210 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003211 case LY_STMT_DEFAULT:
Radek Krejcia9026eb2018-12-12 16:04:47 +01003212 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 +02003213 break;
3214
Radek Krejcid6b76452019-09-03 17:03:03 +02003215 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003216 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "choice");
Radek Krejci10113652018-11-14 16:56:50 +01003217 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003218 case LY_STMT_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003219 LY_CHECK_RET(parse_any(ctx, data, kw, (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_CASE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003222 LY_CHECK_RET(parse_case(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_CHOICE:
Radek Krejci335332a2019-09-05 13:03:35 +02003225 PARSER_CHECK_STMTVER2_RET(ctx, "choice", "choice");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003226 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003227 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003228 case LY_STMT_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003229 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003230 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003231 case LY_STMT_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003232 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003233 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003234 case LY_STMT_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003235 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003236 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003237 case LY_STMT_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003238 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003239 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003240 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003241 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003242 break;
3243 default:
David Sedlákb3077192019-06-19 10:55:37 +02003244 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003245 return LY_EVALID;
3246 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003247 }
Radek Krejcia9026eb2018-12-12 16:04:47 +01003248 LY_CHECK_RET(ret);
3249checks:
3250 if ((choice->flags & LYS_MAND_TRUE) && choice->dflt) {
David Sedlákb3077192019-06-19 10:55:37 +02003251 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMSCOMB, "mandatory", "default", "choice");
Radek Krejcia9026eb2018-12-12 16:04:47 +01003252 return LY_EVALID;
3253 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003254 return ret;
3255}
3256
Michal Vaskoea5abea2018-09-18 13:10:54 +02003257/**
3258 * @brief Parse the container statement.
3259 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003260 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003261 * @param[in,out] data Data to read from, always moved to currently handled character.
3262 * @param[in,out] siblings Siblings to add to.
3263 *
3264 * @return LY_ERR values.
3265 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003266LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003267parse_container(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003268{
3269 LY_ERR ret = 0;
3270 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003271 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003272 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003273 struct lysp_node_container *cont;
3274
David Sedlák60adc092019-08-06 15:57:02 +02003275 /* create new container structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003276 LY_LIST_NEW_RET(ctx->ctx, siblings, cont, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003277 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003278 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003279
Michal Vasko7fbc8162018-09-17 10:35:16 +02003280 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003281 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003282 INSERT_WORD(ctx, buf, cont->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003283
3284 /* parse substatements */
Radek Krejci7fc68292019-06-12 13:51:09 +02003285 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003286 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003287 case LY_STMT_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003288 LY_CHECK_RET(parse_config(ctx, data, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003289 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003290 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003291 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 +02003292 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003293 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003294 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 +02003295 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003296 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003297 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 +02003298 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003299 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003300 LY_CHECK_RET(parse_status(ctx, data, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003301 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003302 case LY_STMT_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003303 LY_CHECK_RET(parse_when(ctx, data, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003304 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003305 case LY_STMT_PRESENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003306 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 +02003307 break;
3308
Radek Krejcid6b76452019-09-03 17:03:03 +02003309 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003310 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "container");
Radek Krejci10113652018-11-14 16:56:50 +01003311 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003312 case LY_STMT_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003313 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003314 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003315 case LY_STMT_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003316 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003317 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003318 case LY_STMT_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003319 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003320 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003321 case LY_STMT_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003322 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003323 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003324 case LY_STMT_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003325 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003326 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003327 case LY_STMT_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003328 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003329 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003330 case LY_STMT_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003331 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003332 break;
3333
Radek Krejcid6b76452019-09-03 17:03:03 +02003334 case LY_STMT_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01003335 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)cont, data, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003336 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003337 case LY_STMT_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003338 LY_CHECK_RET(parse_restrs(ctx, data, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003339 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003340 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003341 PARSER_CHECK_STMTVER2_RET(ctx, "action", "container");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003342 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003343 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003344 case LY_STMT_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003345 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003346 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003347 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003348 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "container");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003349 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003350 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003351 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003352 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003353 break;
3354 default:
David Sedlákb3077192019-06-19 10:55:37 +02003355 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003356 return LY_EVALID;
3357 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003358 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003359checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01003360 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02003361 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, cont->groupings, NULL, cont->actions, cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003362 return ret;
3363}
3364
Michal Vaskoea5abea2018-09-18 13:10:54 +02003365/**
3366 * @brief Parse the list statement.
3367 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003368 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003369 * @param[in,out] data Data to read from, always moved to currently handled character.
3370 * @param[in,out] siblings Siblings to add to.
3371 *
3372 * @return LY_ERR values.
3373 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003374LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003375parse_list(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003376{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003377 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003378 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003379 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003380 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003381 struct lysp_node_list *list;
3382
David Sedlák60adc092019-08-06 15:57:02 +02003383 /* create new list structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003384 LY_LIST_NEW_RET(ctx->ctx, siblings, list, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003385 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003386 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003387
Michal Vasko7fbc8162018-09-17 10:35:16 +02003388 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003389 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003390 INSERT_WORD(ctx, buf, list->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003391
3392 /* parse substatements */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003393 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003394 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003395 case LY_STMT_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003396 LY_CHECK_RET(parse_config(ctx, data, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003397 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003398 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003399 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 +02003400 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003401 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003402 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 +02003403 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003404 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003405 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 +02003406 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003407 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003408 LY_CHECK_RET(parse_status(ctx, data, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003409 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003410 case LY_STMT_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003411 LY_CHECK_RET(parse_when(ctx, data, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003412 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003413 case LY_STMT_KEY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003414 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 +02003415 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003416 case LY_STMT_MAX_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003417 LY_CHECK_RET(parse_maxelements(ctx, data, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003418 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003419 case LY_STMT_MIN_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003420 LY_CHECK_RET(parse_minelements(ctx, data, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003421 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003422 case LY_STMT_ORDERED_BY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003423 LY_CHECK_RET(parse_orderedby(ctx, data, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003424 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003425 case LY_STMT_UNIQUE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003426 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 +02003427 break;
3428
Radek Krejcid6b76452019-09-03 17:03:03 +02003429 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003430 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "list");
Radek Krejci10113652018-11-14 16:56:50 +01003431 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003432 case LY_STMT_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003433 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003434 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003435 case LY_STMT_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003436 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003437 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003438 case LY_STMT_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003439 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003440 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003441 case LY_STMT_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003442 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003443 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003444 case LY_STMT_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003445 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003446 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003447 case LY_STMT_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003448 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003449 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003450 case LY_STMT_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003451 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003452 break;
3453
Radek Krejcid6b76452019-09-03 17:03:03 +02003454 case LY_STMT_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01003455 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)list, data, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003456 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003457 case LY_STMT_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003458 LY_CHECK_RET(parse_restrs(ctx, data, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003459 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003460 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003461 PARSER_CHECK_STMTVER2_RET(ctx, "action", "list");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003462 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003463 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003464 case LY_STMT_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003465 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003466 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003467 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003468 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "list");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003469 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003470 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003471 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003472 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003473 break;
3474 default:
David Sedlákb3077192019-06-19 10:55:37 +02003475 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003476 return LY_EVALID;
3477 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003478 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003479 LY_CHECK_RET(ret);
3480checks:
Radek Krejci7fc68292019-06-12 13:51:09 +02003481 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02003482 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, list->groupings, NULL, list->actions, list->notifs));
Radek Krejci7fc68292019-06-12 13:51:09 +02003483
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003484 if (list->max && list->min > list->max) {
David Sedlákb3077192019-06-19 10:55:37 +02003485 LOGVAL_PARSER(ctx, LYVE_SEMANTICS,
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003486 "Invalid combination of min-elements and max-elements: min value %u is bigger than the max value %u.",
3487 list->min, list->max);
3488 return LY_EVALID;
3489 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003490
3491 return ret;
3492}
3493
Michal Vaskoea5abea2018-09-18 13:10:54 +02003494/**
3495 * @brief Parse the yin-element statement.
3496 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003497 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003498 * @param[in,out] data Data to read from, always moved to currently handled character.
3499 * @param[in,out] flags Flags to write to.
3500 * @param[in,out] exts Extension instances to add to.
3501 *
3502 * @return LY_ERR values.
3503 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003504static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003505parse_yinelement(struct lys_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003506{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003507 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003508 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003509 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003510 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003511
3512 if (*flags & LYS_YINELEM_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02003513 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003514 return LY_EVALID;
3515 }
3516
3517 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003518 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003519
3520 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
3521 *flags |= LYS_YINELEM_TRUE;
3522 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
3523 *flags |= LYS_YINELEM_FALSE;
3524 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003525 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003526 free(buf);
3527 return LY_EVALID;
3528 }
3529 free(buf);
3530
Radek Krejci6d6556c2018-11-08 09:37:45 +01003531 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003532 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003533 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003534 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_YINELEM, 0, exts));
3535 LY_CHECK_RET(ret); break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003536 default:
David Sedlákb3077192019-06-19 10:55:37 +02003537 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003538 return LY_EVALID;
3539 }
3540 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003541 return ret;
3542}
3543
Michal Vaskoea5abea2018-09-18 13:10:54 +02003544/**
David Sedlák2444f8f2019-07-09 11:02:47 +02003545 * @brief Parse the argument statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003546 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003547 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003548 * @param[in,out] data Data to read from, always moved to currently handled character.
3549 * @param[in,out] argument Value to write to.
3550 * @param[in,out] flags Flags to write to.
3551 * @param[in,out] exts Extension instances to add to.
3552 *
3553 * @return LY_ERR values.
3554 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003555static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003556parse_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 +02003557{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003558 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003559 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003560 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003561 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003562
3563 if (*argument) {
David Sedlákb3077192019-06-19 10:55:37 +02003564 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003565 return LY_EVALID;
3566 }
3567
3568 /* get value */
David Sedlák2444f8f2019-07-09 11:02:47 +02003569 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003570 INSERT_WORD(ctx, buf, *argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003571
Radek Krejci6d6556c2018-11-08 09:37:45 +01003572 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003573 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003574 case LY_STMT_YIN_ELEMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003575 LY_CHECK_RET(parse_yinelement(ctx, data, flags, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003576 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003577 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003578 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_ARGUMENT, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003579 break;
3580 default:
David Sedlákb3077192019-06-19 10:55:37 +02003581 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003582 return LY_EVALID;
3583 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003584 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003585 return ret;
3586}
3587
Michal Vaskoea5abea2018-09-18 13:10:54 +02003588/**
3589 * @brief Parse the extension statement.
3590 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003591 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003592 * @param[in,out] data Data to read from, always moved to currently handled character.
3593 * @param[in,out] extensions Extensions to add to.
3594 *
3595 * @return LY_ERR values.
3596 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003597static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003598parse_extension(struct lys_parser_ctx *ctx, const char **data, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003599{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003600 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003601 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003602 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003603 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003604 struct lysp_ext *ex;
3605
Radek Krejci2c4e7172018-10-19 15:56:26 +02003606 LY_ARRAY_NEW_RET(ctx->ctx, *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003607
3608 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003609 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003610 INSERT_WORD(ctx, buf, ex->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003611
Radek Krejci6d6556c2018-11-08 09:37:45 +01003612 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003613 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003614 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003615 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 +02003616 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003617 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003618 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 +02003619 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003620 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003621 LY_CHECK_RET(parse_status(ctx, data, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003622 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003623 case LY_STMT_ARGUMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003624 LY_CHECK_RET(parse_argument(ctx, data, &ex->argument, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003625 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003626 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003627 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003628 break;
3629 default:
David Sedlákb3077192019-06-19 10:55:37 +02003630 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003631 return LY_EVALID;
3632 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003633 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003634 return ret;
3635}
3636
Michal Vaskoea5abea2018-09-18 13:10:54 +02003637/**
3638 * @brief Parse the deviate statement.
3639 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003640 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003641 * @param[in,out] data Data to read from, always moved to currently handled character.
3642 * @param[in,out] deviates Deviates to add to.
3643 *
3644 * @return LY_ERR values.
3645 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003646LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003647parse_deviate(struct lys_parser_ctx *ctx, const char **data, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003648{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003649 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003650 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003651 size_t word_len, dev_mod;
Radek Krejcid6b76452019-09-03 17:03:03 +02003652 enum ly_stmt kw;
David Sedlák60adc092019-08-06 15:57:02 +02003653 struct lysp_deviate *d;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003654 struct lysp_deviate_add *d_add = NULL;
3655 struct lysp_deviate_rpl *d_rpl = NULL;
3656 struct lysp_deviate_del *d_del = NULL;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02003657 const char **d_units = NULL, ***d_uniques = NULL, ***d_dflts = NULL;
3658 struct lysp_restr **d_musts = NULL;
3659 uint16_t *d_flags = 0;
3660 uint32_t *d_min = 0, *d_max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003661
3662 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003663 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003664
3665 if ((word_len == 13) && !strncmp(word, "not-supported", word_len)) {
3666 dev_mod = LYS_DEV_NOT_SUPPORTED;
3667 } else if ((word_len == 3) && !strncmp(word, "add", word_len)) {
3668 dev_mod = LYS_DEV_ADD;
3669 } else if ((word_len == 7) && !strncmp(word, "replace", word_len)) {
3670 dev_mod = LYS_DEV_REPLACE;
3671 } else if ((word_len == 6) && !strncmp(word, "delete", word_len)) {
3672 dev_mod = LYS_DEV_DELETE;
3673 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003674 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003675 free(buf);
3676 return LY_EVALID;
3677 }
3678 free(buf);
3679
3680 /* create structure */
3681 switch (dev_mod) {
3682 case LYS_DEV_NOT_SUPPORTED:
3683 d = calloc(1, sizeof *d);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003684 LY_CHECK_ERR_RET(!d, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003685 break;
3686 case LYS_DEV_ADD:
3687 d_add = calloc(1, sizeof *d_add);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003688 LY_CHECK_ERR_RET(!d_add, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003689 d = (struct lysp_deviate *)d_add;
3690 d_units = &d_add->units;
3691 d_uniques = &d_add->uniques;
3692 d_dflts = &d_add->dflts;
3693 d_musts = &d_add->musts;
3694 d_flags = &d_add->flags;
3695 d_min = &d_add->min;
3696 d_max = &d_add->max;
3697 break;
3698 case LYS_DEV_REPLACE:
3699 d_rpl = calloc(1, sizeof *d_rpl);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003700 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003701 d = (struct lysp_deviate *)d_rpl;
3702 d_units = &d_rpl->units;
3703 d_flags = &d_rpl->flags;
3704 d_min = &d_rpl->min;
3705 d_max = &d_rpl->max;
3706 break;
3707 case LYS_DEV_DELETE:
3708 d_del = calloc(1, sizeof *d_del);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003709 LY_CHECK_ERR_RET(!d_del, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003710 d = (struct lysp_deviate *)d_del;
3711 d_units = &d_del->units;
3712 d_uniques = &d_del->uniques;
3713 d_dflts = &d_del->dflts;
3714 d_musts = &d_del->musts;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003715 break;
3716 default:
3717 assert(0);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003718 LOGINT_RET(ctx->ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003719 }
3720 d->mod = dev_mod;
3721
3722 /* insert into siblings */
David Sedlák60adc092019-08-06 15:57:02 +02003723 LY_LIST_INSERT(deviates, d, next);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003724
Radek Krejci6d6556c2018-11-08 09:37:45 +01003725 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003726 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003727 case LY_STMT_CONFIG:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003728 switch (dev_mod) {
3729 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003730 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003731 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003732 return LY_EVALID;
3733 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003734 LY_CHECK_RET(parse_config(ctx, data, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003735 break;
3736 }
3737 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003738 case LY_STMT_DEFAULT:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003739 switch (dev_mod) {
3740 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003741 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003742 return LY_EVALID;
3743 case LYS_DEV_REPLACE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003744 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 +02003745 break;
3746 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003747 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 +02003748 break;
3749 }
3750 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003751 case LY_STMT_MANDATORY:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003752 switch (dev_mod) {
3753 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003754 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003755 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003756 return LY_EVALID;
3757 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003758 LY_CHECK_RET(parse_mandatory(ctx, data, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003759 break;
3760 }
3761 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003762 case LY_STMT_MAX_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003763 switch (dev_mod) {
3764 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003765 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003766 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003767 return LY_EVALID;
3768 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003769 LY_CHECK_RET(parse_maxelements(ctx, data, d_max, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003770 break;
3771 }
3772 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003773 case LY_STMT_MIN_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003774 switch (dev_mod) {
3775 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003776 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003777 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003778 return LY_EVALID;
3779 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003780 LY_CHECK_RET(parse_minelements(ctx, data, d_min, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003781 break;
3782 }
3783 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003784 case LY_STMT_MUST:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003785 switch (dev_mod) {
3786 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003787 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003788 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003789 return LY_EVALID;
3790 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003791 LY_CHECK_RET(parse_restrs(ctx, data, kw, d_musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003792 break;
3793 }
3794 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003795 case LY_STMT_TYPE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003796 switch (dev_mod) {
3797 case LYS_DEV_NOT_SUPPORTED:
3798 case LYS_DEV_ADD:
3799 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003800 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003801 return LY_EVALID;
3802 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003803 if (d_rpl->type) {
David Sedlákb3077192019-06-19 10:55:37 +02003804 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003805 return LY_EVALID;
3806 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003807 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003808 LY_CHECK_ERR_RET(!d_rpl->type, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003809 LY_CHECK_RET(parse_type(ctx, data, d_rpl->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003810 break;
3811 }
3812 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003813 case LY_STMT_UNIQUE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003814 switch (dev_mod) {
3815 case LYS_DEV_NOT_SUPPORTED:
3816 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003817 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003818 return LY_EVALID;
3819 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003820 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 +02003821 break;
3822 }
3823 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003824 case LY_STMT_UNITS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003825 switch (dev_mod) {
3826 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003827 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003828 return LY_EVALID;
3829 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003830 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, d_units, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003831 break;
3832 }
3833 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003834 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003835 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003836 break;
3837 default:
David Sedlákb3077192019-06-19 10:55:37 +02003838 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003839 return LY_EVALID;
3840 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003841 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003842 return ret;
3843}
3844
Michal Vaskoea5abea2018-09-18 13:10:54 +02003845/**
3846 * @brief Parse the deviation statement.
3847 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003848 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003849 * @param[in,out] data Data to read from, always moved to currently handled character.
3850 * @param[in,out] deviations Deviations to add to.
3851 *
3852 * @return LY_ERR values.
3853 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003854LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003855parse_deviation(struct lys_parser_ctx *ctx, const char **data, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003856{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003857 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003858 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003859 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003860 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003861 struct lysp_deviation *dev;
3862
Radek Krejci2c4e7172018-10-19 15:56:26 +02003863 LY_ARRAY_NEW_RET(ctx->ctx, *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003864
3865 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003866 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
David Sedlákb9b892c2019-07-12 14:44:02 +02003867 YANG_CHECK_NONEMPTY(ctx, word_len, "deviation");
Radek Krejci44ceedc2018-10-02 15:54:31 +02003868 INSERT_WORD(ctx, buf, dev->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003869
Radek Krejci6d6556c2018-11-08 09:37:45 +01003870 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003871 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003872 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003873 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 +02003874 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003875 case LY_STMT_DEVIATE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003876 LY_CHECK_RET(parse_deviate(ctx, data, &dev->deviates));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003877 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003878 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003879 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 +02003880 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003881 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003882 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003883 break;
3884 default:
David Sedlákb3077192019-06-19 10:55:37 +02003885 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003886 return LY_EVALID;
3887 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003888 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003889 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01003890checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003891 /* mandatory substatements */
3892 if (!dev->deviates) {
David Sedlákb3077192019-06-19 10:55:37 +02003893 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003894 return LY_EVALID;
3895 }
3896
3897 return ret;
3898}
3899
Michal Vaskoea5abea2018-09-18 13:10:54 +02003900/**
3901 * @brief Parse the feature statement.
3902 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003903 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003904 * @param[in,out] data Data to read from, always moved to currently handled character.
3905 * @param[in,out] features Features to add to.
3906 *
3907 * @return LY_ERR values.
3908 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003909LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003910parse_feature(struct lys_parser_ctx *ctx, const char **data, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003911{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003912 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003913 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003914 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003915 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003916 struct lysp_feature *feat;
3917
Radek Krejci2c4e7172018-10-19 15:56:26 +02003918 LY_ARRAY_NEW_RET(ctx->ctx, *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003919
3920 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003921 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003922 INSERT_WORD(ctx, buf, feat->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01003923
Radek Krejci6d6556c2018-11-08 09:37:45 +01003924 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003925 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003926 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003927 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 +02003928 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003929 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003930 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 +02003931 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003932 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003933 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 +02003934 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003935 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003936 LY_CHECK_RET(parse_status(ctx, data, &feat->flags, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003937 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003938 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003939 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003940 break;
3941 default:
David Sedlákb3077192019-06-19 10:55:37 +02003942 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003943 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003944 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003945 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003946 return ret;
3947}
3948
Michal Vaskoea5abea2018-09-18 13:10:54 +02003949/**
3950 * @brief Parse the identity statement.
3951 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003952 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003953 * @param[in,out] data Data to read from, always moved to currently handled character.
3954 * @param[in,out] identities Identities to add to.
3955 *
3956 * @return LY_ERR values.
3957 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003958LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003959parse_identity(struct lys_parser_ctx *ctx, const char **data, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003960{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003961 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003962 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003963 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003964 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003965 struct lysp_ident *ident;
3966
Radek Krejci2c4e7172018-10-19 15:56:26 +02003967 LY_ARRAY_NEW_RET(ctx->ctx, *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003968
3969 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003970 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003971 INSERT_WORD(ctx, buf, ident->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01003972
Radek Krejci6d6556c2018-11-08 09:37:45 +01003973 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003974 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003975 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003976 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 +02003977 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003978 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02003979 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "identity");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003980 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 +02003981 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003982 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003983 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 +02003984 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003985 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003986 LY_CHECK_RET(parse_status(ctx, data, &ident->flags, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003987 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003988 case LY_STMT_BASE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003989 if (ident->bases && ctx->mod_version < 2) {
David Sedlákb3077192019-06-19 10:55:37 +02003990 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 +01003991 return LY_EVALID;
3992 }
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003993 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 +02003994 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003995 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003996 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003997 break;
3998 default:
David Sedlákb3077192019-06-19 10:55:37 +02003999 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004000 return LY_EVALID;
4001 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004002 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004003 return ret;
4004}
4005
Michal Vaskoea5abea2018-09-18 13:10:54 +02004006/**
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004007 * @brief Parse module substatements.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004008 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004009 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004010 * @param[in,out] data Data to read from, always moved to currently handled character.
4011 * @param[in,out] mod Module to write to.
4012 *
4013 * @return LY_ERR values.
4014 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004015LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02004016parse_module(struct lys_parser_ctx *ctx, const char **data, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004017{
4018 LY_ERR ret = 0;
4019 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004020 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004021 enum ly_stmt kw, prev_kw = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004022 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004023 struct lysp_submodule *dup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004024
4025 /* (sub)module name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02004026 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004027 INSERT_WORD(ctx, buf, mod->mod->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004028
Radek Krejci6d6556c2018-11-08 09:37:45 +01004029 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004030
Radek Krejcie3846472018-10-15 15:24:51 +02004031#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004032 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 +02004033
Michal Vasko7fbc8162018-09-17 10:35:16 +02004034 switch (kw) {
4035 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004036 case LY_STMT_NAMESPACE:
4037 case LY_STMT_PREFIX:
Radek Krejcie3846472018-10-15 15:24:51 +02004038 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4039 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004040 case LY_STMT_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004041 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004042 break;
4043 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004044 case LY_STMT_INCLUDE:
4045 case LY_STMT_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004046 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004047 break;
4048 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004049 case LY_STMT_ORGANIZATION:
4050 case LY_STMT_CONTACT:
4051 case LY_STMT_DESCRIPTION:
4052 case LY_STMT_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004053 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004054 break;
4055
4056 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004057 case LY_STMT_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004058 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004059 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004060 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004061 case LY_STMT_ANYDATA:
4062 case LY_STMT_ANYXML:
4063 case LY_STMT_AUGMENT:
4064 case LY_STMT_CHOICE:
4065 case LY_STMT_CONTAINER:
4066 case LY_STMT_DEVIATION:
4067 case LY_STMT_EXTENSION:
4068 case LY_STMT_FEATURE:
4069 case LY_STMT_GROUPING:
4070 case LY_STMT_IDENTITY:
4071 case LY_STMT_LEAF:
4072 case LY_STMT_LEAF_LIST:
4073 case LY_STMT_LIST:
4074 case LY_STMT_NOTIFICATION:
4075 case LY_STMT_RPC:
4076 case LY_STMT_TYPEDEF:
4077 case LY_STMT_USES:
4078 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004079 mod_stmt = Y_MOD_BODY;
4080 break;
4081 default:
4082 /* error handled in the next switch */
4083 break;
4084 }
Radek Krejcie3846472018-10-15 15:24:51 +02004085#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004086
Radek Krejcie3846472018-10-15 15:24:51 +02004087 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004088 switch (kw) {
4089 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004090 case LY_STMT_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004091 LY_CHECK_RET(parse_yangversion(ctx, data, &mod->mod->version, &mod->exts));
4092 ctx->mod_version = mod->mod->version;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004093 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004094 case LY_STMT_NAMESPACE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004095 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 +02004096 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004097 case LY_STMT_PREFIX:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004098 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 +02004099 break;
4100
4101 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004102 case LY_STMT_INCLUDE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004103 LY_CHECK_RET(parse_include(ctx, mod->mod->name, data, &mod->includes));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004104 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004105 case LY_STMT_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004106 LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, data, &mod->imports));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004107 break;
4108
4109 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004110 case LY_STMT_ORGANIZATION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004111 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 +02004112 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004113 case LY_STMT_CONTACT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004114 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 +02004115 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004116 case LY_STMT_DESCRIPTION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004117 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 +02004118 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004119 case LY_STMT_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004120 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 +02004121 break;
4122
4123 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004124 case LY_STMT_REVISION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004125 LY_CHECK_RET(parse_revision(ctx, data, &mod->revs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004126 break;
4127
4128 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004129 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004130 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "module");
Radek Krejci10113652018-11-14 16:56:50 +01004131 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004132 case LY_STMT_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004133 LY_CHECK_RET(parse_any(ctx, data, kw, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004134 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004135 case LY_STMT_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004136 LY_CHECK_RET(parse_choice(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004137 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004138 case LY_STMT_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004139 LY_CHECK_RET(parse_container(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004140 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004141 case LY_STMT_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004142 LY_CHECK_RET(parse_leaf(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004143 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004144 case LY_STMT_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004145 LY_CHECK_RET(parse_leaflist(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004146 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004147 case LY_STMT_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004148 LY_CHECK_RET(parse_list(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004149 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004150 case LY_STMT_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004151 LY_CHECK_RET(parse_uses(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004152 break;
4153
Radek Krejcid6b76452019-09-03 17:03:03 +02004154 case LY_STMT_AUGMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004155 LY_CHECK_RET(parse_augment(ctx, data, NULL, &mod->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004156 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004157 case LY_STMT_DEVIATION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004158 LY_CHECK_RET(parse_deviation(ctx, data, &mod->deviations));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004159 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004160 case LY_STMT_EXTENSION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004161 LY_CHECK_RET(parse_extension(ctx, data, &mod->extensions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004162 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004163 case LY_STMT_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004164 LY_CHECK_RET(parse_feature(ctx, data, &mod->features));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004165 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004166 case LY_STMT_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004167 LY_CHECK_RET(parse_grouping(ctx, data, NULL, &mod->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004168 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004169 case LY_STMT_IDENTITY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004170 LY_CHECK_RET(parse_identity(ctx, data, &mod->identities));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004171 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004172 case LY_STMT_NOTIFICATION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004173 LY_CHECK_RET(parse_notif(ctx, data, NULL, &mod->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004174 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004175 case LY_STMT_RPC:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004176 LY_CHECK_RET(parse_action(ctx, data, NULL, &mod->rpcs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004177 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004178 case LY_STMT_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01004179 LY_CHECK_RET(parse_typedef(ctx, NULL, data, &mod->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004180 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004181 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004182 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004183 break;
4184
4185 default:
David Sedlákb3077192019-06-19 10:55:37 +02004186 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004187 return LY_EVALID;
4188 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004189 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004190 LY_CHECK_RET(ret);
Radek Krejcie86bf772018-12-14 11:39:53 +01004191
Radek Krejci6d6556c2018-11-08 09:37:45 +01004192checks:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004193 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02004194 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, mod->groupings, mod->augments, mod->rpcs, mod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004195
Michal Vasko7fbc8162018-09-17 10:35:16 +02004196 /* mandatory substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004197 if (!mod->mod->ns) {
David Sedlákb3077192019-06-19 10:55:37 +02004198 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004199 return LY_EVALID;
4200 } else if (!mod->mod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004201 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004202 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004203 }
4204
Radek Krejcie9e987e2018-10-31 12:50:27 +01004205 /* submodules share the namespace with the module names, so there must not be
4206 * a submodule of the same name in the context, no need for revision matching */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004207 dup = ly_ctx_get_submodule(ctx->ctx, NULL, mod->mod->name, NULL);
4208 if (dup) {
David Sedlákb3077192019-06-19 10:55:37 +02004209 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 +01004210 return LY_EVALID;
4211 }
4212
4213 return ret;
4214}
4215
4216/**
4217 * @brief Parse submodule substatements.
4218 *
4219 * @param[in] ctx yang parser context for logging.
4220 * @param[in,out] data Data to read from, always moved to currently handled character.
4221 * @param[out] submod Parsed submodule structure.
4222 *
4223 * @return LY_ERR values.
4224 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004225LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02004226parse_submodule(struct lys_parser_ctx *ctx, const char **data, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004227{
4228 LY_ERR ret = 0;
4229 char *buf, *word;
4230 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004231 enum ly_stmt kw, prev_kw = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004232 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
4233 struct lysp_submodule *dup;
4234
4235 /* submodule name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02004236 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004237 INSERT_WORD(ctx, buf, submod->name, word, word_len);
4238
4239 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
4240
4241#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004242 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 +01004243
4244 switch (kw) {
4245 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004246 case LY_STMT_BELONGS_TO:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004247 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4248 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004249 case LY_STMT_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004250 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4251 break;
4252 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004253 case LY_STMT_INCLUDE:
4254 case LY_STMT_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004255 CHECK_ORDER(Y_MOD_LINKAGE);
4256 break;
4257 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004258 case LY_STMT_ORGANIZATION:
4259 case LY_STMT_CONTACT:
4260 case LY_STMT_DESCRIPTION:
4261 case LY_STMT_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004262 CHECK_ORDER(Y_MOD_META);
4263 break;
4264
4265 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004266 case LY_STMT_REVISION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004267 CHECK_ORDER(Y_MOD_REVISION);
4268 break;
4269 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004270 case LY_STMT_ANYDATA:
4271 case LY_STMT_ANYXML:
4272 case LY_STMT_AUGMENT:
4273 case LY_STMT_CHOICE:
4274 case LY_STMT_CONTAINER:
4275 case LY_STMT_DEVIATION:
4276 case LY_STMT_EXTENSION:
4277 case LY_STMT_FEATURE:
4278 case LY_STMT_GROUPING:
4279 case LY_STMT_IDENTITY:
4280 case LY_STMT_LEAF:
4281 case LY_STMT_LEAF_LIST:
4282 case LY_STMT_LIST:
4283 case LY_STMT_NOTIFICATION:
4284 case LY_STMT_RPC:
4285 case LY_STMT_TYPEDEF:
4286 case LY_STMT_USES:
4287 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004288 mod_stmt = Y_MOD_BODY;
4289 break;
4290 default:
4291 /* error handled in the next switch */
4292 break;
4293 }
4294#undef CHECK_ORDER
4295
4296 prev_kw = kw;
4297 switch (kw) {
4298 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004299 case LY_STMT_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004300 LY_CHECK_RET(parse_yangversion(ctx, data, &submod->version, &submod->exts));
4301 ctx->mod_version = submod->version;
4302 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004303 case LY_STMT_BELONGS_TO:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004304 LY_CHECK_RET(parse_belongsto(ctx, data, &submod->belongsto, &submod->prefix, &submod->exts));
4305 break;
4306
4307 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004308 case LY_STMT_INCLUDE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004309 LY_CHECK_RET(parse_include(ctx, submod->name, data, &submod->includes));
4310 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004311 case LY_STMT_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004312 LY_CHECK_RET(parse_import(ctx, submod->prefix, data, &submod->imports));
4313 break;
4314
4315 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004316 case LY_STMT_ORGANIZATION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004317 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_ORGANIZATION, 0, &submod->org, Y_STR_ARG, &submod->exts));
4318 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004319 case LY_STMT_CONTACT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004320 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_CONTACT, 0, &submod->contact, Y_STR_ARG, &submod->exts));
4321 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004322 case LY_STMT_DESCRIPTION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004323 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &submod->dsc, Y_STR_ARG, &submod->exts));
4324 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004325 case LY_STMT_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004326 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &submod->ref, Y_STR_ARG, &submod->exts));
4327 break;
4328
4329 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004330 case LY_STMT_REVISION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004331 LY_CHECK_RET(parse_revision(ctx, data, &submod->revs));
4332 break;
4333
4334 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004335 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004336 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004337 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004338 case LY_STMT_ANYXML:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004339 LY_CHECK_RET(parse_any(ctx, data, kw, NULL, &submod->data));
4340 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004341 case LY_STMT_CHOICE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004342 LY_CHECK_RET(parse_choice(ctx, data, NULL, &submod->data));
4343 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004344 case LY_STMT_CONTAINER:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004345 LY_CHECK_RET(parse_container(ctx, data, NULL, &submod->data));
4346 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004347 case LY_STMT_LEAF:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004348 LY_CHECK_RET(parse_leaf(ctx, data, NULL, &submod->data));
4349 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004350 case LY_STMT_LEAF_LIST:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004351 LY_CHECK_RET(parse_leaflist(ctx, data, NULL, &submod->data));
4352 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004353 case LY_STMT_LIST:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004354 LY_CHECK_RET(parse_list(ctx, data, NULL, &submod->data));
4355 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004356 case LY_STMT_USES:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004357 LY_CHECK_RET(parse_uses(ctx, data, NULL, &submod->data));
4358 break;
4359
Radek Krejcid6b76452019-09-03 17:03:03 +02004360 case LY_STMT_AUGMENT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004361 LY_CHECK_RET(parse_augment(ctx, data, NULL, &submod->augments));
4362 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004363 case LY_STMT_DEVIATION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004364 LY_CHECK_RET(parse_deviation(ctx, data, &submod->deviations));
4365 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004366 case LY_STMT_EXTENSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004367 LY_CHECK_RET(parse_extension(ctx, data, &submod->extensions));
4368 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004369 case LY_STMT_FEATURE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004370 LY_CHECK_RET(parse_feature(ctx, data, &submod->features));
4371 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004372 case LY_STMT_GROUPING:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004373 LY_CHECK_RET(parse_grouping(ctx, data, NULL, &submod->groupings));
4374 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004375 case LY_STMT_IDENTITY:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004376 LY_CHECK_RET(parse_identity(ctx, data, &submod->identities));
4377 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004378 case LY_STMT_NOTIFICATION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004379 LY_CHECK_RET(parse_notif(ctx, data, NULL, &submod->notifs));
4380 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004381 case LY_STMT_RPC:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004382 LY_CHECK_RET(parse_action(ctx, data, NULL, &submod->rpcs));
4383 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004384 case LY_STMT_TYPEDEF:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004385 LY_CHECK_RET(parse_typedef(ctx, NULL, data, &submod->typedefs));
4386 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004387 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004388 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &submod->exts));
4389 break;
4390
4391 default:
David Sedlákb3077192019-06-19 10:55:37 +02004392 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004393 return LY_EVALID;
4394 }
4395 }
4396 LY_CHECK_RET(ret);
4397
4398checks:
4399 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02004400 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, submod->groupings, submod->augments, submod->rpcs, submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004401
4402 /* mandatory substatements */
4403 if (!submod->belongsto) {
David Sedlákb3077192019-06-19 10:55:37 +02004404 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004405 return LY_EVALID;
4406 }
4407
4408 /* submodules share the namespace with the module names, so there must not be
4409 * a submodule of the same name in the context, no need for revision matching */
4410 dup = ly_ctx_get_submodule(ctx->ctx, NULL, submod->name, NULL);
4411 if (dup && strcmp(dup->belongsto, submod->belongsto)) {
David Sedlákb3077192019-06-19 10:55:37 +02004412 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Name collision between submodules of name \"%s\".", dup->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +01004413 return LY_EVALID;
4414 }
4415
Michal Vasko7fbc8162018-09-17 10:35:16 +02004416 return ret;
4417}
4418
Radek Krejcid4557c62018-09-17 11:42:09 +02004419LY_ERR
David Sedlák1b623122019-08-05 15:27:49 +02004420yang_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 +02004421{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004422 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004423 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004424 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004425 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004426 struct lysp_submodule *mod_p = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004427
David Sedlák1b623122019-08-05 15:27:49 +02004428 /* create context */
4429 *context = calloc(1, sizeof **context);
4430 LY_CHECK_ERR_RET(!(*context), LOGMEM(ly_ctx), LY_EMEM);
4431 (*context)->ctx = ly_ctx;
Radek Krejci99435242019-09-05 16:19:15 +02004432 (*context)->pos_type = LY_VLOG_LINE;
David Sedlák1b623122019-08-05 15:27:49 +02004433 (*context)->line = 1;
4434
4435 /* map the typedefs and groupings list from main context to the submodule's context */
4436 memcpy(&(*context)->tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
4437 memcpy(&(*context)->grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes);
4438
Michal Vasko7fbc8162018-09-17 10:35:16 +02004439 /* "module"/"submodule" */
David Sedlák1b623122019-08-05 15:27:49 +02004440 ret = get_keyword(*context, &data, &kw, &word, &word_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004441 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004442
Radek Krejcid6b76452019-09-03 17:03:03 +02004443 if (kw == LY_STMT_MODULE) {
David Sedlák1b623122019-08-05 15:27:49 +02004444 LOGERR((*context)->ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004445 ret = LY_EINVAL;
4446 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004447 } else if (kw != LY_STMT_SUBMODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004448 LOGVAL_PARSER(*context, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004449 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004450 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004451 }
4452
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004453 mod_p = calloc(1, sizeof *mod_p);
David Sedlák1b623122019-08-05 15:27:49 +02004454 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM((*context)->ctx), cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004455 mod_p->parsing = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004456
4457 /* substatements */
David Sedlák1b623122019-08-05 15:27:49 +02004458 ret = parse_submodule(*context, &data, mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004459 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004460
4461 /* read some trailing spaces or new lines */
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004462 while(*data && isspace(*data)) {
4463 data++;
4464 }
4465 if (*data) {
David Sedlák1538a842019-08-08 15:38:51 +02004466 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_SUBMOD, 15, data, strlen(data) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004467 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004468 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004469 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004470
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004471 mod_p->parsing = 0;
4472 *submod = mod_p;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004473
Radek Krejcibbe09a92018-11-08 09:36:54 +01004474cleanup:
4475 if (ret) {
David Sedlák1b623122019-08-05 15:27:49 +02004476 lysp_submodule_free((*context)->ctx, mod_p);
4477 lys_parser_ctx_free(*context);
4478 *context = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004479 }
4480
4481 return ret;
4482}
4483
4484LY_ERR
David Sedlák1b623122019-08-05 15:27:49 +02004485yang_parse_module(struct lys_parser_ctx **context, const char *data, struct lys_module *mod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004486{
4487 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004488 char *word;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004489 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004490 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004491 struct lysp_module *mod_p = NULL;
4492
David Sedlák1b623122019-08-05 15:27:49 +02004493 /* create context */
4494 *context = calloc(1, sizeof **context);
4495 LY_CHECK_ERR_RET(!(*context), LOGMEM(mod->ctx), LY_EMEM);
4496 (*context)->ctx = mod->ctx;
Radek Krejci335332a2019-09-05 13:03:35 +02004497 (*context)->pos_type = LY_VLOG_LINE;
David Sedlák1b623122019-08-05 15:27:49 +02004498 (*context)->line = 1;
4499
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004500 /* "module"/"submodule" */
David Sedlák1b623122019-08-05 15:27:49 +02004501 ret = get_keyword(*context, &data, &kw, &word, &word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004502 LY_CHECK_GOTO(ret, cleanup);
4503
Radek Krejcid6b76452019-09-03 17:03:03 +02004504 if (kw == LY_STMT_SUBMODULE) {
David Sedlák1b623122019-08-05 15:27:49 +02004505 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 +01004506 ret = LY_EINVAL;
4507 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004508 } else if (kw != LY_STMT_MODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004509 LOGVAL_PARSER((*context), LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004510 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004511 goto cleanup;
4512 }
4513
4514 mod_p = calloc(1, sizeof *mod_p);
David Sedlák1b623122019-08-05 15:27:49 +02004515 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM((*context)->ctx), cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004516 mod_p->mod = mod;
4517 mod_p->parsing = 1;
4518
4519 /* substatements */
David Sedlák1b623122019-08-05 15:27:49 +02004520 ret = parse_module(*context, &data, mod_p);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004521 LY_CHECK_GOTO(ret, cleanup);
4522
4523 /* read some trailing spaces or new lines */
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004524 while(*data && isspace(*data)) {
4525 data++;
4526 }
4527 if (*data) {
David Sedlák1538a842019-08-08 15:38:51 +02004528 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_MOD, 15, data, strlen(data) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004529 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004530 goto cleanup;
4531 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004532
4533 mod_p->parsing = 0;
4534 mod->parsed = mod_p;
4535
4536cleanup:
4537 if (ret) {
4538 lysp_module_free(mod_p);
David Sedlák1b623122019-08-05 15:27:49 +02004539 lys_parser_ctx_free(*context);
4540 *context = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004541 }
4542
Michal Vasko7fbc8162018-09-17 10:35:16 +02004543 return ret;
4544}