blob: e5dde2f6d0bff672b069670661510e8187462440 [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"
27#include "extensions.h"
28#include "log.h"
29#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/**
Radek Krejciceaf2122019-01-02 15:03:26 +010035 * @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)); \
Michal Vasko7fbc8162018-09-17 10:35:16 +020068 if (KW == YANG_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 } \
72 if (KW != YANG_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); \
77 !ERR && (KW != YANG_RIGHT_BRACE); \
78 ERR = get_keyword(CTX, DATA, &KW, &WORD, &WORD_LEN))
79
Radek Krejciceaf2122019-01-02 15:03:26 +010080/**
81 * @brief Check module version is at least 2 (YANG 1.1) because of the keyword presence.
82 * Logs error message and returns LY_EVALID in case of module in YANG version 1.0.
83 * @param[in] CTX yang parser context to get current module and for logging.
84 * @param[in] KW keyword allowed only in YANG version 1.1 (or later) - for logging.
85 * @param[in] PARENT parent statement where the KW is present - for logging.
86 */
87#define YANG_CHECK_STMTVER2_RET(CTX, KW, PARENT) \
David Sedlákb3077192019-06-19 10:55:37 +020088 if ((CTX)->mod_version < 2) {LOGVAL_PARSER((CTX), LY_VCODE_INCHILDSTMT2, KW, PARENT); return LY_EVALID;}
Radek Krejci10113652018-11-14 16:56:50 +010089
Radek Krejci2d7a47b2019-05-16 13:34:10 +020090LY_ERR parse_container(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
91LY_ERR parse_uses(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
92LY_ERR parse_choice(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
93LY_ERR parse_case(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
94LY_ERR parse_list(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
95LY_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 +020096
Michal Vaskoea5abea2018-09-18 13:10:54 +020097/**
98 * @brief Add another character to dynamic buffer, a low-level function.
99 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200100 * Enlarge if needed. Updates \p input as well as \p buf_used.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200101 *
Radek Krejci404251e2018-10-09 12:06:44 +0200102 * @param[in] ctx libyang context for logging.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200103 * @param[in, out] input Input string to process.
104 * @param[in] len Number of bytes to get from the input string and copy into the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200105 * @param[in,out] buf Buffer to use, can be moved by realloc().
106 * @param[in,out] buf_len Current size of the buffer.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200107 * @param[in,out] buf_used Currently used characters of the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200108 *
109 * @return LY_ERR values.
110 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200111LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +0200112buf_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 +0200113{
Radek Krejci44ceedc2018-10-02 15:54:31 +0200114 if (*buf_len <= (*buf_used) + len) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200115 *buf_len += 16;
116 *buf = ly_realloc(*buf, *buf_len);
117 LY_CHECK_ERR_RET(!*buf, LOGMEM(ctx), LY_EMEM);
118 }
Radek Krejcic0917392019-04-10 13:04:04 +0200119 if (*buf_used) {
120 memcpy(&(*buf)[*buf_used], *input, len);
121 } else {
122 memcpy(*buf, *input, len);
123 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200124
Radek Krejci44ceedc2018-10-02 15:54:31 +0200125 (*buf_used) += len;
126 (*input) += len;
127
Michal Vasko7fbc8162018-09-17 10:35:16 +0200128 return LY_SUCCESS;
129}
130
Michal Vaskoea5abea2018-09-18 13:10:54 +0200131/**
Radek Krejci44ceedc2018-10-02 15:54:31 +0200132 * @brief Store a single UTF8 character. It depends whether in a dynamically-allocated buffer or just as a pointer to the data.
133 *
134 * @param[in] ctx yang parser context for logging.
135 * @param[in,out] input Pointer to the string where to get the character to store. Automatically moved to the next character
136 * when function returns.
137 * @param[in] arg Type of the input string to select method of checking character validity.
138 * @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 +0200139 * 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 +0200140 * @param[in,out] word_len Current length of the word pointed to by \p word_p.
141 * @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 +0200142 * @param[in,out] buf_len Current length of \p word_b.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200143 * @param[in] need_buf Flag if the dynamically allocated buffer is required.
David Sedlák40bb13b2019-07-10 14:34:18 +0200144 * @param[in,out] prefix Storage for internally used flag in case of possible prefixed identifiers:
145 * 0 - colon not yet found (no prefix)
146 * 1 - \p c is the colon character
147 * 2 - prefix already processed, now processing the identifier
Michal Vaskoea5abea2018-09-18 13:10:54 +0200148 *
149 * @return LY_ERR values.
150 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200151LY_ERR
David Sedlák40bb13b2019-07-10 14:34:18 +0200152buf_store_char(struct lys_parser_ctx *ctx, const char **input, enum yang_arg arg, char **word_p,
153 size_t *word_len, char **word_b, size_t *buf_len, int need_buf, int *prefix)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200154{
Radek Krejci44ceedc2018-10-02 15:54:31 +0200155 unsigned int c;
156 size_t len;
157
Radek Krejcif29b7c32019-04-09 16:17:49 +0200158 /* check valid combination of input paremeters - if need_buf specified, word_b must be provided */
159 assert(!need_buf || (need_buf && word_b));
160
Radek Krejci44ceedc2018-10-02 15:54:31 +0200161 /* get UTF8 code point (and number of bytes coding the character) */
162 LY_CHECK_ERR_RET(ly_getutf8(input, &c, &len),
David Sedlákb3077192019-06-19 10:55:37 +0200163 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, (*input)[-len]), LY_EVALID);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200164 (*input) -= len;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200165 if (c == '\n') {
166 ctx->indent = 0;
167 } else {
168 /* note - even the multibyte character is count as 1 */
169 ++ctx->indent;
170 }
171
Radek Krejci44ceedc2018-10-02 15:54:31 +0200172 /* check character validity */
173 switch (arg) {
174 case Y_IDENTIF_ARG:
David Sedlák4a650532019-07-10 11:55:18 +0200175 LY_CHECK_RET(lysp_check_identifierchar(ctx, c, !(*word_len), NULL));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200176 break;
177 case Y_PREF_IDENTIF_ARG:
David Sedlák40bb13b2019-07-10 14:34:18 +0200178 LY_CHECK_RET(lysp_check_identifierchar(ctx, c, !(*word_len), prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200179 break;
180 case Y_STR_ARG:
181 case Y_MAYBE_STR_ARG:
David Sedlák4a650532019-07-10 11:55:18 +0200182 LY_CHECK_RET(lysp_check_stringchar(ctx, c));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200183 break;
184 }
185
Michal Vasko7fbc8162018-09-17 10:35:16 +0200186 if (word_b && *word_b) {
187 /* add another character into buffer */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200188 if (buf_add_char(ctx->ctx, input, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200189 return LY_EMEM;
190 }
191
192 /* in case of realloc */
193 *word_p = *word_b;
Radek Krejcif29b7c32019-04-09 16:17:49 +0200194 } else if (word_b && need_buf) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200195 /* first time we need a buffer, copy everything read up to now */
196 if (*word_len) {
197 *word_b = malloc(*word_len);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200198 LY_CHECK_ERR_RET(!*word_b, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200199 *buf_len = *word_len;
200 memcpy(*word_b, *word_p, *word_len);
201 }
202
203 /* add this new character into buffer */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200204 if (buf_add_char(ctx->ctx, input, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200205 return LY_EMEM;
206 }
207
208 /* in case of realloc */
209 *word_p = *word_b;
210 } else {
211 /* just remember the first character pointer */
212 if (!*word_p) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200213 *word_p = (char *)(*input);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200214 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200215 /* ... and update the word's length */
216 (*word_len) += len;
217 (*input) += len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200218 }
219
220 return LY_SUCCESS;
221}
222
Michal Vaskoea5abea2018-09-18 13:10:54 +0200223/**
224 * @brief Skip YANG comment in data.
225 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200226 * @param[in] ctx yang parser context for logging.
227 * @param[in,out] data Data to read from, automatically moved after the comment.
228 * @param[in] comment Type of the comment to process:
229 * 1 for a one-line comment,
230 * 2 for a block comment.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200231 *
232 * @return LY_ERR values.
233 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200234LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200235skip_comment(struct lys_parser_ctx *ctx, const char **data, int comment)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200236{
Radek Krejci80dd33e2018-09-26 15:57:18 +0200237 /* internal statuses: 0 - comment ended,
238 * 1 - in line comment,
239 * 2 - in block comment,
240 * 3 - in block comment with last read character '*'
241 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200242 while (**data && comment) {
243 switch (comment) {
244 case 1:
245 if (**data == '\n') {
246 comment = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200247 ++ctx->line;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200248 }
249 break;
250 case 2:
251 if (**data == '*') {
Radek Krejci15c80ca2018-10-09 11:01:31 +0200252 comment = 3;
253 } else if (**data == '\n') {
254 ++ctx->line;
255 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200256 break;
257 case 3:
258 if (**data == '/') {
259 comment = 0;
Radek Krejci5b930492019-06-11 14:54:08 +0200260 } else if (**data != '*') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200261 if (**data == '\n') {
262 ++ctx->line;
263 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200264 comment = 2;
265 }
266 break;
267 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200268 LOGINT_RET(ctx->ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200269 }
270
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200271 if (**data == '\n') {
272 ctx->indent = 0;
273 } else {
274 ++ctx->indent;
275 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200276 ++(*data);
277 }
278
279 if (!**data && (comment > 1)) {
David Sedlákb3077192019-06-19 10:55:37 +0200280 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Unexpected end-of-input, non-terminated comment.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200281 return LY_EVALID;
282 }
283
284 return LY_SUCCESS;
285}
286
Michal Vaskoea5abea2018-09-18 13:10:54 +0200287/**
288 * @brief Read a quoted string from data.
289 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200290 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200291 * @param[in,out] data Data to read from, always moved to currently handled character.
292 * @param[in] arg Type of YANG keyword argument expected.
293 * @param[out] word_p Pointer to the read quoted string.
294 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read quoted string. If not needed,
295 * set to NULL. Otherwise equal to \p word_p.
296 * @param[out] word_len Length of the read quoted string.
297 * @param[out] buf_len Length of the dynamically-allocated buffer \p word_b.
298 * @param[in] indent Current indent (number of YANG spaces). Needed for correct multi-line string
299 * indenation in the final quoted string.
300 *
301 * @return LY_ERR values.
302 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200303static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200304read_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 +0200305 size_t *buf_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200306{
307 /* string: 0 - string ended, 1 - string with ', 2 - string with ", 3 - string with " with last character \,
308 * 4 - string finished, now skipping whitespaces looking for +,
309 * 5 - string continues after +, skipping whitespaces */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200310 unsigned int string, block_indent = 0, current_indent = 0, need_buf = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200311 const char *c;
David Sedlák40bb13b2019-07-10 14:34:18 +0200312 int prefix = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200313
314 if (**data == '\"') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200315 string = 2;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200316 current_indent = block_indent = ctx->indent + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200317 } else {
318 assert(**data == '\'');
319 string = 1;
320 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200321 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200322
323 while (**data && string) {
324 switch (string) {
325 case 1:
326 switch (**data) {
327 case '\'':
328 /* string may be finished, but check for + */
329 string = 4;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200330 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200331 break;
332 default:
333 /* check and store character */
David Sedlák40bb13b2019-07-10 14:34:18 +0200334 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 +0200335 break;
336 }
337 break;
338 case 2:
339 switch (**data) {
340 case '\"':
341 /* string may be finished, but check for + */
342 string = 4;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200343 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200344 break;
345 case '\\':
346 /* special character following */
347 string = 3;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200348 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200349 break;
350 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200351 if (current_indent < block_indent) {
352 ++current_indent;
353 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200354 } else {
355 /* check and store character */
David Sedlák40bb13b2019-07-10 14:34:18 +0200356 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 +0200357 }
358 break;
359 case '\t':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200360 if (current_indent < block_indent) {
361 assert(need_buf);
362 current_indent += 8;
363 ctx->indent += 8;
364 for (; current_indent > block_indent; --current_indent, --ctx->indent) {
365 /* store leftover spaces from the tab */
366 c = " ";
David Sedlák40bb13b2019-07-10 14:34:18 +0200367 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 +0200368 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200369 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200370 } else {
371 /* check and store character */
David Sedlák40bb13b2019-07-10 14:34:18 +0200372 LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200373 /* additional characters for indentation - only 1 was count in buf_store_char */
374 ctx->indent += 7;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200375 }
376 break;
377 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200378 if (block_indent) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200379 /* we will be removing the indents so we need our own buffer */
380 need_buf = 1;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200381
382 /* remove trailing tabs and spaces */
383 while ((*word_len) && ((*word_p)[(*word_len) - 1] == '\t' || (*word_p)[(*word_len) - 1] == ' ')) {
384 --(*word_len);
385 }
386
387 /* start indentation */
388 current_indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200389 }
390
391 /* check and store character */
David Sedlák40bb13b2019-07-10 14:34:18 +0200392 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 +0200393
394 /* maintain line number */
395 ++ctx->line;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200396
397 /* reset context indentation counter for possible string after this one */
398 ctx->indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200399 break;
400 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200401 /* first non-whitespace character, stop eating indentation */
402 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200403
404 /* check and store character */
David Sedlák40bb13b2019-07-10 14:34:18 +0200405 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 +0200406 break;
407 }
408 break;
409 case 3:
410 /* string encoded characters */
411 switch (**data) {
412 case 'n':
413 c = "\n";
414 break;
415 case 't':
416 c = "\t";
417 break;
418 case '\"':
419 c = *data;
420 break;
421 case '\\':
422 c = *data;
423 break;
424 default:
David Sedlákb3077192019-06-19 10:55:37 +0200425 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Double-quoted string unknown special character '\\%c'.", **data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200426 return LY_EVALID;
427 }
428
429 /* check and store character */
David Sedlák40bb13b2019-07-10 14:34:18 +0200430 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 +0200431
432 string = 2;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200433 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200434 break;
435 case 4:
436 switch (**data) {
437 case '+':
438 /* string continues */
439 string = 5;
Radek Krejciefd22f62018-09-27 11:47:58 +0200440 need_buf = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200441 break;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200442 case '\n':
443 ++ctx->line;
444 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200445 case ' ':
446 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200447 /* just skip */
448 break;
449 default:
450 /* string is finished */
451 goto string_end;
452 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200453 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200454 break;
455 case 5:
456 switch (**data) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200457 case '\n':
458 ++ctx->line;
459 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200460 case ' ':
461 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200462 /* skip */
463 break;
464 case '\'':
465 string = 1;
466 break;
467 case '\"':
468 string = 2;
469 break;
470 default:
471 /* it must be quoted again */
David Sedlákb3077192019-06-19 10:55:37 +0200472 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Both string parts divided by '+' must be quoted.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200473 return LY_EVALID;
474 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200475 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200476 break;
477 default:
478 return LY_EINT;
479 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200480 }
481
482string_end:
Radek Krejci4e199f52019-05-28 09:09:28 +0200483 if (arg <= Y_PREF_IDENTIF_ARG && !(*word_len)) {
484 /* empty identifier */
David Sedlákb3077192019-06-19 10:55:37 +0200485 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Statement argument is required.");
Radek Krejci4e199f52019-05-28 09:09:28 +0200486 return LY_EVALID;
487 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200488 return LY_SUCCESS;
489}
490
Michal Vaskoea5abea2018-09-18 13:10:54 +0200491/**
492 * @brief Get another YANG string from the raw data.
493 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200494 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200495 * @param[in,out] data Data to read from, always moved to currently handled character.
496 * @param[in] arg Type of YANG keyword argument expected.
Radek Krejcid3ca0632019-04-16 16:54:54 +0200497 * @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 +0200498 * @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 +0200499 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read string. If not needed,
500 * set to NULL. Otherwise equal to \p word_p.
501 * @param[out] word_len Length of the read string.
502 *
503 * @return LY_ERR values.
Michal Vasko7fbc8162018-09-17 10:35:16 +0200504 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200505LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200506get_argument(struct lys_parser_ctx *ctx, const char **data, enum yang_arg arg,
Radek Krejcid3ca0632019-04-16 16:54:54 +0200507 uint16_t *flags, char **word_p, char **word_b, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200508{
Radek Krejci44ceedc2018-10-02 15:54:31 +0200509 size_t buf_len = 0;
David Sedlák40bb13b2019-07-10 14:34:18 +0200510 int prefix = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200511 /* word buffer - dynamically allocated */
512 *word_b = NULL;
513
514 /* word pointer - just a pointer to data */
515 *word_p = NULL;
516
517 *word_len = 0;
518 while (**data) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200519 switch (**data) {
520 case '\'':
521 case '\"':
522 if (*word_len) {
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200523 /* invalid - quotes cannot be in unquoted string and only optsep, ; or { can follow it */
David Sedlákb3077192019-06-19 10:55:37 +0200524 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, *data,
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200525 "unquoted string character, optsep, semicolon or opening brace");
526 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200527 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200528 if (flags) {
529 (*flags) |= (**data) == '\'' ? LYS_SINGLEQUOTED : LYS_DOUBLEQUOTED;
530 }
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100531 LY_CHECK_RET(read_qstring(ctx, data, arg, word_p, word_b, word_len, &buf_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200532 goto str_end;
533 case '/':
Radek Krejci44ceedc2018-10-02 15:54:31 +0200534 if ((*data)[1] == '/') {
535 /* one-line comment */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200536 MOVE_INPUT(ctx, data, 2);
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100537 LY_CHECK_RET(skip_comment(ctx, data, 1));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200538 } else if ((*data)[1] == '*') {
539 /* block comment */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200540 MOVE_INPUT(ctx, data, 2);
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100541 LY_CHECK_RET(skip_comment(ctx, data, 2));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200542 } else {
543 /* not a comment after all */
David Sedlák40bb13b2019-07-10 14:34:18 +0200544 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 +0200545 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200546 break;
547 case ' ':
548 if (*word_len) {
549 /* word is finished */
550 goto str_end;
551 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200552 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200553 break;
554 case '\t':
555 if (*word_len) {
556 /* word is finished */
557 goto str_end;
558 }
559 /* tabs count for 8 spaces */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200560 ctx->indent += 8;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200561
562 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200563 break;
564 case '\n':
565 if (*word_len) {
566 /* word is finished */
567 goto str_end;
568 }
569 /* reset indent */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200570 ctx->indent = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200571
572 /* track line numbers */
573 ++ctx->line;
574
575 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200576 break;
577 case ';':
578 case '{':
579 if (*word_len || (arg == Y_MAYBE_STR_ARG)) {
580 /* word is finished */
581 goto str_end;
582 }
583
David Sedlákb3077192019-06-19 10:55:37 +0200584 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, *data, "an argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200585 return LY_EVALID;
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200586 case '}':
587 /* invalid - braces cannot be in unquoted string (opening braces terminates the string and can follow it) */
David Sedlákb3077192019-06-19 10:55:37 +0200588 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, *data,
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200589 "unquoted string character, optsep, semicolon or opening brace");
590 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200591 default:
David Sedlák40bb13b2019-07-10 14:34:18 +0200592 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 +0200593 break;
594 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200595 }
596
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200597 /* unexpected end of loop */
David Sedlákb3077192019-06-19 10:55:37 +0200598 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200599 return LY_EVALID;
600
Michal Vasko7fbc8162018-09-17 10:35:16 +0200601str_end:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200602 /* terminating NULL byte for buf */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200603 if (*word_b) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200604 (*word_b) = ly_realloc(*word_b, (*word_len) + 1);
605 LY_CHECK_ERR_RET(!(*word_b), LOGMEM(ctx->ctx), LY_EMEM);
606 (*word_b)[*word_len] = '\0';
Michal Vasko7fbc8162018-09-17 10:35:16 +0200607 *word_p = *word_b;
608 }
609
610 return LY_SUCCESS;
611}
612
Michal Vaskoea5abea2018-09-18 13:10:54 +0200613/**
614 * @brief Get another YANG keyword from the raw data.
615 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200616 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200617 * @param[in,out] data Data to read from, always moved to currently handled character.
618 * @param[out] kw YANG keyword read.
619 * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances.
620 * @param[out] word_len Length of the keyword in the data. Useful for extension instances.
621 *
622 * @return LY_ERR values.
623 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200624LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200625get_keyword(struct lys_parser_ctx *ctx, const char **data, enum yang_keyword *kw, char **word_p, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200626{
Michal Vasko7fbc8162018-09-17 10:35:16 +0200627 int prefix;
628 const char *word_start;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200629 unsigned int c;
630 size_t len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200631
632 if (word_p) {
633 *word_p = NULL;
634 *word_len = 0;
635 }
636
637 /* first skip "optsep", comments */
638 while (**data) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200639 switch (**data) {
640 case '/':
641 if ((*data)[1] == '/') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200642 /* one-line comment */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200643 MOVE_INPUT(ctx, data, 2);
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100644 LY_CHECK_RET(skip_comment(ctx, data, 1));
Radek Krejcidcc7b322018-10-11 14:24:02 +0200645 } else if ((*data)[1] == '*') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200646 /* block comment */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200647 MOVE_INPUT(ctx, data, 2);
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100648 LY_CHECK_RET(skip_comment(ctx, data, 2));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200649 } else {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200650 /* error - not a comment after all, keyword cannot start with slash */
David Sedlákb3077192019-06-19 10:55:37 +0200651 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '/'.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200652 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200653 }
Radek Krejci13028282019-06-11 14:56:48 +0200654 continue;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200655 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200656 /* skip whitespaces (optsep) */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200657 ++ctx->line;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200658 ctx->indent = 0;
659 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200660 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200661 /* skip whitespaces (optsep) */
662 ++ctx->indent;
663 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200664 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200665 /* skip whitespaces (optsep) */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200666 ctx->indent += 8;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200667 break;
668 default:
669 /* either a keyword start or an invalid character */
670 goto keyword_start;
671 }
672
673 ++(*data);
674 }
675
676keyword_start:
677 word_start = *data;
David Sedlák5f8f0332019-06-18 16:34:30 +0200678 *kw = lysp_match_kw(ctx, data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200679
David Sedlák1bccdfa2019-06-17 15:55:27 +0200680 if (*kw == YANG_SEMICOLON || *kw == YANG_LEFT_BRACE || *kw == YANG_RIGHT_BRACE) {
Radek Krejci626df482018-10-11 15:06:31 +0200681 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200682 }
683
684 if (*kw != YANG_NONE) {
685 /* make sure we have the whole keyword */
686 switch (**data) {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200687 case '\n':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200688 case '\t':
Radek Krejciabdd8062019-06-11 16:44:19 +0200689 case ' ':
690 /* mandatory "sep" is just checked, not eaten so nothing in the context is updated */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200691 break;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200692 case ':':
693 /* keyword is not actually a keyword, but prefix of an extension.
694 * To avoid repeated check of the prefix syntax, move to the point where the colon was read
695 * and we will be checking the keyword (extension instance) itself */
696 prefix = 1;
697 MOVE_INPUT(ctx, data, 1);
698 goto extension;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200699 case '{':
700 /* allowed only for input and output statements which can be without arguments */
701 if (*kw == YANG_INPUT || *kw == YANG_OUTPUT) {
702 break;
703 }
704 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200705 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200706 MOVE_INPUT(ctx, data, 1);
David Sedlákb3077192019-06-19 10:55:37 +0200707 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(*data - word_start), word_start,
Radek Krejci44ceedc2018-10-02 15:54:31 +0200708 "a keyword followed by a separator");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200709 return LY_EVALID;
710 }
711 } else {
712 /* still can be an extension */
713 prefix = 0;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200714extension:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200715 while (**data && (**data != ' ') && (**data != '\t') && (**data != '\n') && (**data != '{') && (**data != ';')) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200716 LY_CHECK_ERR_RET(ly_getutf8(data, &c, &len),
David Sedlákb3077192019-06-19 10:55:37 +0200717 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, (*data)[-len]), LY_EVALID);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200718 ++ctx->indent;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200719 /* check character validity */
David Sedlák4a650532019-07-10 11:55:18 +0200720 LY_CHECK_RET(lysp_check_identifierchar(ctx, c, *data - len == word_start ? 1 : 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200721 }
722 if (!**data) {
David Sedlákb3077192019-06-19 10:55:37 +0200723 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200724 return LY_EVALID;
725 }
726
727 /* prefix is mandatory for extension instances */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200728 if (prefix != 2) {
David Sedlákb3077192019-06-19 10:55:37 +0200729 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(*data - word_start), word_start, "a keyword");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200730 return LY_EVALID;
731 }
732
733 *kw = YANG_CUSTOM;
734 }
Radek Krejci626df482018-10-11 15:06:31 +0200735success:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200736 if (word_p) {
737 *word_p = (char *)word_start;
738 *word_len = *data - word_start;
739 }
740
741 return LY_SUCCESS;
742}
743
Michal Vaskoea5abea2018-09-18 13:10:54 +0200744/**
745 * @brief Parse extension instance substatements.
746 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200747 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200748 * @param[in,out] data Data to read from, always moved to currently handled character.
749 * @param[in] word Extension instance substatement name (keyword).
750 * @param[in] word_len Extension instance substatement name length.
751 * @param[in,out] child Children of this extension instance to add to.
752 *
753 * @return LY_ERR values.
754 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200755static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200756parse_ext_substmt(struct lys_parser_ctx *ctx, const char **data, char *word, size_t word_len,
Michal Vasko7fbc8162018-09-17 10:35:16 +0200757 struct lysp_stmt **child)
758{
759 char *buf;
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100760 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200761 enum yang_keyword kw;
762 struct lysp_stmt *stmt, *par_child;
763
764 stmt = calloc(1, sizeof *stmt);
765 LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM);
766
Radek Krejcibb9b1982019-04-08 14:24:59 +0200767 /* insert into parent statements */
768 if (!*child) {
769 *child = stmt;
770 } else {
771 for (par_child = *child; par_child->next; par_child = par_child->next);
772 par_child->next = stmt;
773 }
774
Radek Krejci44ceedc2018-10-02 15:54:31 +0200775 stmt->stmt = lydict_insert(ctx->ctx, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200776
777 /* get optional argument */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200778 LY_CHECK_RET(get_argument(ctx, data, Y_MAYBE_STR_ARG, &stmt->flags, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200779
Radek Krejci0ae092d2018-09-20 16:43:19 +0200780 if (word) {
781 if (buf) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200782 stmt->arg = lydict_insert_zc(ctx->ctx, word);
Radek Krejci0ae092d2018-09-20 16:43:19 +0200783 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200784 stmt->arg = lydict_insert(ctx->ctx, word, word_len);
Radek Krejci0ae092d2018-09-20 16:43:19 +0200785 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200786 }
787
Radek Krejci6d6556c2018-11-08 09:37:45 +0100788 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, ) {
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100789 LY_CHECK_RET(parse_ext_substmt(ctx, data, word, word_len, &stmt->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200790 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200791 return ret;
792}
793
Michal Vaskoea5abea2018-09-18 13:10:54 +0200794/**
795 * @brief Parse extension instance.
796 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200797 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200798 * @param[in,out] data Data to read from, always moved to currently handled character.
799 * @param[in] ext_name Extension instance substatement name (keyword).
800 * @param[in] ext_name_len Extension instance substatement name length.
801 * @param[in] insubstmt Type of the keyword this extension instance is a substatement of.
802 * @param[in] insubstmt_index Index of the keyword instance this extension instance is a substatement of.
803 * @param[in,out] exts Extension instances to add to.
804 *
805 * @return LY_ERR values.
806 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200807static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200808parse_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 +0200809 uint32_t insubstmt_index, struct lysp_ext_instance **exts)
810{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100811 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200812 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200813 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200814 struct lysp_ext_instance *e;
815 enum yang_keyword kw;
816
Radek Krejci2c4e7172018-10-19 15:56:26 +0200817 LY_ARRAY_NEW_RET(ctx->ctx, *exts, e, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200818
819 /* store name and insubstmt info */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200820 e->name = lydict_insert(ctx->ctx, ext_name, ext_name_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200821 e->insubstmt = insubstmt;
822 e->insubstmt_index = insubstmt_index;
823
824 /* get optional argument */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200825 LY_CHECK_RET(get_argument(ctx, data, Y_MAYBE_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200826
Radek Krejci0ae092d2018-09-20 16:43:19 +0200827 if (word) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200828 INSERT_WORD(ctx, buf, e->argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200829 }
830
Radek Krejci6d6556c2018-11-08 09:37:45 +0100831 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100832 LY_CHECK_RET(parse_ext_substmt(ctx, data, word, word_len, &e->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200833 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200834 return ret;
835}
836
Michal Vaskoea5abea2018-09-18 13:10:54 +0200837/**
838 * @brief Parse a generic text field without specific constraints. Those are contact, organization,
839 * description, etc...
840 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200841 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200842 * @param[in,out] data Data to read from, always moved to currently handled character.
843 * @param[in] substmt Type of this substatement.
844 * @param[in] substmt_index Index of this substatement.
845 * @param[in,out] value Place to store the parsed value.
846 * @param[in] arg Type of the YANG keyword argument (of the value).
847 * @param[in,out] exts Extension instances to add to.
848 *
849 * @return LY_ERR values.
850 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200851static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200852parse_text_field(struct lys_parser_ctx *ctx, const char **data, LYEXT_SUBSTMT substmt, uint32_t substmt_index,
Michal Vasko7fbc8162018-09-17 10:35:16 +0200853 const char **value, enum yang_arg arg, struct lysp_ext_instance **exts)
854{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100855 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200856 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200857 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200858 enum yang_keyword kw;
859
860 if (*value) {
David Sedlákb3077192019-06-19 10:55:37 +0200861 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200862 return LY_EVALID;
863 }
864
865 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200866 LY_CHECK_RET(get_argument(ctx, data, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200867
868 /* store value and spend buf if allocated */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200869 INSERT_WORD(ctx, buf, *value, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200870
Radek Krejci6d6556c2018-11-08 09:37:45 +0100871 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200872 switch (kw) {
873 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100874 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, substmt, substmt_index, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200875 break;
876 default:
David Sedlákb3077192019-06-19 10:55:37 +0200877 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200878 return LY_EVALID;
879 }
880 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200881 return ret;
882}
883
Michal Vaskoea5abea2018-09-18 13:10:54 +0200884/**
885 * @brief Parse the yang-version statement.
886 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200887 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200888 * @param[in,out] data Data to read from, always moved to currently handled character.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100889 * @param[out] version Storage for the parsed information.
890 * @param[in, out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200891 *
892 * @return LY_ERR values.
893 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200894static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200895parse_yangversion(struct lys_parser_ctx *ctx, const char **data, uint8_t *version, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200896{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100897 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200898 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200899 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200900 enum yang_keyword kw;
901
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100902 if (*version) {
David Sedlákb3077192019-06-19 10:55:37 +0200903 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200904 return LY_EVALID;
905 }
906
907 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200908 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200909
910 if ((word_len == 3) && !strncmp(word, "1.0", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100911 *version = LYS_VERSION_1_0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200912 } else if ((word_len == 3) && !strncmp(word, "1.1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100913 *version = LYS_VERSION_1_1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200914 } else {
David Sedlákb3077192019-06-19 10:55:37 +0200915 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200916 free(buf);
917 return LY_EVALID;
918 }
919 free(buf);
920
Radek Krejci6d6556c2018-11-08 09:37:45 +0100921 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200922 switch (kw) {
923 case YANG_CUSTOM:
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100924 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_VERSION, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200925 break;
926 default:
David Sedlákb3077192019-06-19 10:55:37 +0200927 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200928 return LY_EVALID;
929 }
930 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200931 return ret;
932}
933
Michal Vaskoea5abea2018-09-18 13:10:54 +0200934/**
935 * @brief Parse the belongs-to statement.
936 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200937 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200938 * @param[in,out] data Data to read from, always moved to currently handled character.
939 * @param[in,out] belongsto Place to store the parsed value.
940 * @param[in,out] prefix Place to store the parsed belongs-to prefix value.
941 * @param[in,out] exts Extension instances to add to.
942 *
943 * @return LY_ERR values.
944 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200945static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200946parse_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 +0200947{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100948 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200949 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200950 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200951 enum yang_keyword kw;
952
953 if (*belongsto) {
David Sedlákb3077192019-06-19 10:55:37 +0200954 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200955 return LY_EVALID;
956 }
957
958 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200959 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200960
Radek Krejci44ceedc2018-10-02 15:54:31 +0200961 INSERT_WORD(ctx, buf, *belongsto, word, word_len);
Radek Krejcif09e4e82019-06-14 15:08:11 +0200962
Radek Krejci6d6556c2018-11-08 09:37:45 +0100963 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200964 switch (kw) {
965 case YANG_PREFIX:
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100966 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_PREFIX, 0, prefix, Y_IDENTIF_ARG, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200967 break;
968 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100969 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_BELONGSTO, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200970 break;
971 default:
David Sedlákb3077192019-06-19 10:55:37 +0200972 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200973 return LY_EVALID;
974 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200975 }
Radek Krejcic59bc972018-09-17 16:13:06 +0200976 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +0100977checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200978 /* mandatory substatements */
979 if (!*prefix) {
David Sedlákb3077192019-06-19 10:55:37 +0200980 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200981 return LY_EVALID;
982 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200983 return ret;
984}
985
Michal Vaskoea5abea2018-09-18 13:10:54 +0200986/**
987 * @brief Parse the revision-date statement.
988 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200989 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200990 * @param[in,out] data Data to read from, always moved to currently handled character.
991 * @param[in,out] rev Array to store the parsed value in.
992 * @param[in,out] exts Extension instances to add to.
993 *
994 * @return LY_ERR values.
995 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200996static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200997parse_revisiondate(struct lys_parser_ctx *ctx, const char **data, char *rev, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200998{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100999 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001000 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001001 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001002 enum yang_keyword kw;
1003
1004 if (rev[0]) {
David Sedlákb3077192019-06-19 10:55:37 +02001005 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001006 return LY_EVALID;
1007 }
1008
1009 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001010 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001011
1012 /* check value */
Radek Krejcibbe09a92018-11-08 09:36:54 +01001013 if (lysp_check_date(ctx, word, word_len, "revision-date")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001014 free(buf);
1015 return LY_EVALID;
1016 }
1017
1018 /* store value and spend buf if allocated */
1019 strncpy(rev, word, word_len);
1020 free(buf);
1021
Radek Krejci6d6556c2018-11-08 09:37:45 +01001022 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001023 switch (kw) {
1024 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001025 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_REVISIONDATE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001026 break;
1027 default:
David Sedlákb3077192019-06-19 10:55:37 +02001028 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001029 return LY_EVALID;
1030 }
1031 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001032 return ret;
1033}
1034
Michal Vaskoea5abea2018-09-18 13:10:54 +02001035/**
1036 * @brief Parse the include statement.
1037 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001038 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001039 * @param[in] module_name Name of the module to check name collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001040 * @param[in,out] data Data to read from, always moved to currently handled character.
1041 * @param[in,out] includes Parsed includes to add to.
1042 *
1043 * @return LY_ERR values.
1044 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001045static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001046parse_include(struct lys_parser_ctx *ctx, const char *module_name, const char **data, struct lysp_include **includes)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001047{
Radek Krejcid33273d2018-10-25 14:55:52 +02001048 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001049 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001050 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001051 enum yang_keyword kw;
1052 struct lysp_include *inc;
1053
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001054 LY_ARRAY_NEW_RET(ctx->ctx, *includes, inc, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001055
1056 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001057 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001058
Radek Krejci086c7132018-10-26 15:29:04 +02001059 INSERT_WORD(ctx, buf, inc->name, word, word_len);
1060
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001061 /* submodules share the namespace with the module names, so there must not be
1062 * a module of the same name in the context, no need for revision matching */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001063 if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(ctx->ctx, inc->name)) {
David Sedlákb3077192019-06-19 10:55:37 +02001064 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Name collision between module and submodule of name \"%s\".", inc->name);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001065 return LY_EVALID;
1066 }
1067
Radek Krejci6d6556c2018-11-08 09:37:45 +01001068 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001069 switch (kw) {
1070 case YANG_DESCRIPTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01001071 YANG_CHECK_STMTVER2_RET(ctx, "description", "include");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001072 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 +02001073 break;
1074 case YANG_REFERENCE:
Radek Krejciceaf2122019-01-02 15:03:26 +01001075 YANG_CHECK_STMTVER2_RET(ctx, "reference", "include");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001076 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 +02001077 break;
1078 case YANG_REVISION_DATE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001079 LY_CHECK_RET(parse_revisiondate(ctx, data, inc->rev, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001080 break;
1081 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001082 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001083 break;
1084 default:
David Sedlákb3077192019-06-19 10:55:37 +02001085 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001086 return LY_EVALID;
1087 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001088 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001089 return ret;
1090}
1091
Michal Vaskoea5abea2018-09-18 13:10:54 +02001092/**
1093 * @brief Parse the import statement.
1094 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001095 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001096 * @param[in] module_prefix Prefix of the module to check prefix collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001097 * @param[in,out] data Data to read from, always moved to currently handled character.
1098 * @param[in,out] imports Parsed imports to add to.
1099 *
1100 * @return LY_ERR values.
1101 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001102static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001103parse_import(struct lys_parser_ctx *ctx, const char *module_prefix, const char **data, struct lysp_import **imports)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001104{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001105 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001106 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001107 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001108 enum yang_keyword kw;
1109 struct lysp_import *imp;
1110
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001111 LY_ARRAY_NEW_RET(ctx->ctx, *imports, imp, LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001112
1113 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001114 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02001115 INSERT_WORD(ctx, buf, imp->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001116
Radek Krejci6d6556c2018-11-08 09:37:45 +01001117 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001118 switch (kw) {
1119 case YANG_PREFIX:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001120 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 +01001121 LY_CHECK_RET(lysp_check_prefix(ctx, *imports, module_prefix, &imp->prefix), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001122 break;
1123 case YANG_DESCRIPTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01001124 YANG_CHECK_STMTVER2_RET(ctx, "description", "import");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001125 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 +02001126 break;
1127 case YANG_REFERENCE:
Radek Krejciceaf2122019-01-02 15:03:26 +01001128 YANG_CHECK_STMTVER2_RET(ctx, "reference", "import");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001129 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 +02001130 break;
1131 case YANG_REVISION_DATE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001132 LY_CHECK_RET(parse_revisiondate(ctx, data, imp->rev, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001133 break;
1134 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001135 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001136 break;
1137 default:
David Sedlákb3077192019-06-19 10:55:37 +02001138 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001139 return LY_EVALID;
1140 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001141 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001142 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001143checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001144 /* mandatory substatements */
David Sedlákb3077192019-06-19 10:55:37 +02001145 LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001146
1147 return ret;
1148}
1149
Michal Vaskoea5abea2018-09-18 13:10:54 +02001150/**
1151 * @brief Parse the revision statement.
1152 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001153 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001154 * @param[in,out] data Data to read from, always moved to currently handled character.
1155 * @param[in,out] revs Parsed revisions to add to.
1156 *
1157 * @return LY_ERR values.
1158 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001159static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001160parse_revision(struct lys_parser_ctx *ctx, const char **data, struct lysp_revision **revs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001161{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001162 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001163 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001164 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001165 enum yang_keyword kw;
1166 struct lysp_revision *rev;
1167
Radek Krejci2c4e7172018-10-19 15:56:26 +02001168 LY_ARRAY_NEW_RET(ctx->ctx, *revs, rev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001169
1170 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001171 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001172
1173 /* check value */
Radek Krejcibbe09a92018-11-08 09:36:54 +01001174 if (lysp_check_date(ctx, word, word_len, "revision")) {
David Sedlák68ef3dc2019-07-22 13:40:19 +02001175 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001176 return LY_EVALID;
1177 }
1178
Radek Krejcib7db73a2018-10-24 14:18:40 +02001179 strncpy(rev->date, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001180 free(buf);
1181
Radek Krejci6d6556c2018-11-08 09:37:45 +01001182 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001183 switch (kw) {
1184 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001185 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 +02001186 break;
1187 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001188 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 +02001189 break;
1190 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001191 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001192 break;
1193 default:
David Sedlákb3077192019-06-19 10:55:37 +02001194 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001195 return LY_EVALID;
1196 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001197 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001198 return ret;
1199}
1200
Michal Vaskoea5abea2018-09-18 13:10:54 +02001201/**
1202 * @brief Parse a generic text field that can have more instances such as base.
1203 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001204 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001205 * @param[in,out] data Data to read from, always moved to currently handled character.
1206 * @param[in] substmt Type of this substatement.
1207 * @param[in,out] texts Parsed values to add to.
1208 * @param[in] arg Type of the expected argument.
1209 * @param[in,out] exts Extension instances to add to.
1210 *
1211 * @return LY_ERR values.
1212 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001213static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001214parse_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 +02001215 struct lysp_ext_instance **exts)
1216{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001217 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001218 char *buf, *word;
Radek Krejci151a5b72018-10-19 14:21:44 +02001219 const char **item;
1220 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001221 enum yang_keyword kw;
1222
1223 /* allocate new pointer */
Radek Krejci2c4e7172018-10-19 15:56:26 +02001224 LY_ARRAY_NEW_RET(ctx->ctx, *texts, item, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001225
1226 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001227 LY_CHECK_RET(get_argument(ctx, data, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001228
Radek Krejci151a5b72018-10-19 14:21:44 +02001229 INSERT_WORD(ctx, buf, *item, word, word_len);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001230 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001231 switch (kw) {
1232 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001233 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, substmt, LY_ARRAY_SIZE(*texts) - 1, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001234 break;
1235 default:
David Sedlákb3077192019-06-19 10:55:37 +02001236 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001237 return LY_EVALID;
1238 }
1239 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001240 return ret;
1241}
1242
Michal Vaskoea5abea2018-09-18 13:10:54 +02001243/**
1244 * @brief Parse the config statement.
1245 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001246 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001247 * @param[in,out] data Data to read from, always moved to currently handled character.
1248 * @param[in,out] flags Flags to add to.
1249 * @param[in,out] exts Extension instances to add to.
1250 *
1251 * @return LY_ERR values.
1252 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001253static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001254parse_config(struct lys_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001255{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001256 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001257 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001258 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001259 enum yang_keyword kw;
1260
1261 if (*flags & LYS_CONFIG_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001262 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001263 return LY_EVALID;
1264 }
1265
1266 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001267 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001268
1269 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1270 *flags |= LYS_CONFIG_W;
1271 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1272 *flags |= LYS_CONFIG_R;
1273 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001274 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001275 free(buf);
1276 return LY_EVALID;
1277 }
1278 free(buf);
1279
Radek Krejci6d6556c2018-11-08 09:37:45 +01001280 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001281 switch (kw) {
1282 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001283 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_CONFIG, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001284 break;
1285 default:
David Sedlákb3077192019-06-19 10:55:37 +02001286 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001287 return LY_EVALID;
1288 }
1289 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001290 return ret;
1291}
1292
Michal Vaskoea5abea2018-09-18 13:10:54 +02001293/**
1294 * @brief Parse the mandatory statement.
1295 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001296 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001297 * @param[in,out] data Data to read from, always moved to currently handled character.
1298 * @param[in,out] flags Flags to add to.
1299 * @param[in,out] exts Extension instances to add to.
1300 *
1301 * @return LY_ERR values.
1302 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001303static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001304parse_mandatory(struct lys_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001305{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001306 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001307 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001308 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001309 enum yang_keyword kw;
1310
1311 if (*flags & LYS_MAND_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001312 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001313 return LY_EVALID;
1314 }
1315
1316 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001317 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001318
1319 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1320 *flags |= LYS_MAND_TRUE;
1321 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1322 *flags |= LYS_MAND_FALSE;
1323 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001324 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001325 free(buf);
1326 return LY_EVALID;
1327 }
1328 free(buf);
1329
Radek Krejci6d6556c2018-11-08 09:37:45 +01001330 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001331 switch (kw) {
1332 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001333 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MANDATORY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001334 break;
1335 default:
David Sedlákb3077192019-06-19 10:55:37 +02001336 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001337 return LY_EVALID;
1338 }
1339 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001340 return ret;
1341}
1342
Michal Vaskoea5abea2018-09-18 13:10:54 +02001343/**
1344 * @brief Parse a restriction such as range or length.
1345 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001346 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001347 * @param[in,out] data Data to read from, always moved to currently handled character.
1348 * @param[in] restr_kw Type of this particular restriction.
1349 * @param[in,out] exts Extension instances to add to.
1350 *
1351 * @return LY_ERR values.
1352 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001353static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001354parse_restr(struct lys_parser_ctx *ctx, const char **data, enum yang_keyword restr_kw, struct lysp_restr *restr)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001355{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001356 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001357 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001358 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001359 enum yang_keyword kw;
1360
1361 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001362 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001363
David Sedlákb9b892c2019-07-12 14:44:02 +02001364 YANG_CHECK_NONEMPTY(ctx, word_len, ly_stmt2str(restr_kw));
Radek Krejci44ceedc2018-10-02 15:54:31 +02001365 INSERT_WORD(ctx, buf, restr->arg, word, word_len);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001366 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001367 switch (kw) {
1368 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001369 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 +02001370 break;
1371 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001372 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 +02001373 break;
1374 case YANG_ERROR_APP_TAG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001375 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 +02001376 break;
1377 case YANG_ERROR_MESSAGE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001378 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 +02001379 break;
1380 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001381 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001382 break;
1383 default:
David Sedlákb3077192019-06-19 10:55:37 +02001384 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001385 return LY_EVALID;
1386 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001387 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001388 return ret;
1389}
1390
Michal Vaskoea5abea2018-09-18 13:10:54 +02001391/**
1392 * @brief Parse a restriction that can have more instances such as must.
1393 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001394 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001395 * @param[in,out] data Data to read from, always moved to currently handled character.
1396 * @param[in] restr_kw Type of this particular restriction.
1397 * @param[in,out] restrs Restrictions to add to.
1398 *
1399 * @return LY_ERR values.
1400 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001401static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001402parse_restrs(struct lys_parser_ctx *ctx, const char **data, enum yang_keyword restr_kw, struct lysp_restr **restrs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001403{
1404 struct lysp_restr *restr;
1405
Radek Krejci2c4e7172018-10-19 15:56:26 +02001406 LY_ARRAY_NEW_RET(ctx->ctx, *restrs, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001407 return parse_restr(ctx, data, restr_kw, restr);
1408}
1409
Michal Vaskoea5abea2018-09-18 13:10:54 +02001410/**
1411 * @brief Parse the status statement.
1412 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001413 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001414 * @param[in,out] data Data to read from, always moved to currently handled character.
1415 * @param[in,out] flags Flags to add to.
1416 * @param[in,out] exts Extension instances to add to.
1417 *
1418 * @return LY_ERR values.
1419 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001420static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001421parse_status(struct lys_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001422{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001423 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001424 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001425 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001426 enum yang_keyword kw;
1427
1428 if (*flags & LYS_STATUS_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001429 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001430 return LY_EVALID;
1431 }
1432
1433 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001434 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001435
1436 if ((word_len == 7) && !strncmp(word, "current", word_len)) {
1437 *flags |= LYS_STATUS_CURR;
1438 } else if ((word_len == 10) && !strncmp(word, "deprecated", word_len)) {
1439 *flags |= LYS_STATUS_DEPRC;
1440 } else if ((word_len == 8) && !strncmp(word, "obsolete", word_len)) {
1441 *flags |= LYS_STATUS_OBSLT;
1442 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001443 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001444 free(buf);
1445 return LY_EVALID;
1446 }
1447 free(buf);
1448
Radek Krejci6d6556c2018-11-08 09:37:45 +01001449 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001450 switch (kw) {
1451 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001452 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_STATUS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001453 break;
1454 default:
David Sedlákb3077192019-06-19 10:55:37 +02001455 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001456 return LY_EVALID;
1457 }
1458 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001459 return ret;
1460}
1461
Michal Vaskoea5abea2018-09-18 13:10:54 +02001462/**
1463 * @brief Parse the when statement.
1464 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001465 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001466 * @param[in,out] data Data to read from, always moved to currently handled character.
1467 * @param[in,out] when_p When pointer to parse to.
1468 *
1469 * @return LY_ERR values.
1470 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001471LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001472parse_when(struct lys_parser_ctx *ctx, const char **data, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001473{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001474 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001475 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001476 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001477 enum yang_keyword kw;
1478 struct lysp_when *when;
1479
1480 if (*when_p) {
David Sedlákb3077192019-06-19 10:55:37 +02001481 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001482 return LY_EVALID;
1483 }
1484
1485 when = calloc(1, sizeof *when);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001486 LY_CHECK_ERR_RET(!when, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001487
1488 /* get value */
Radek Krejci2f54df52019-06-21 10:59:19 +02001489 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 +02001490 YANG_CHECK_NONEMPTY(ctx, word_len, "when");
Radek Krejci44ceedc2018-10-02 15:54:31 +02001491 INSERT_WORD(ctx, buf, when->cond, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001492
Radek Krejcif09e4e82019-06-14 15:08:11 +02001493 *when_p = when;
1494
Radek Krejci6d6556c2018-11-08 09:37:45 +01001495 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001496 switch (kw) {
1497 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001498 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 +02001499 break;
1500 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001501 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 +02001502 break;
1503 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001504 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001505 break;
1506 default:
David Sedlákb3077192019-06-19 10:55:37 +02001507 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001508 return LY_EVALID;
1509 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001510 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001511 return ret;
1512}
1513
Michal Vaskoea5abea2018-09-18 13:10:54 +02001514/**
1515 * @brief Parse the anydata or anyxml statement.
1516 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001517 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001518 * @param[in,out] data Data to read from, always moved to currently handled character.
1519 * @param[in] kw Type of this particular keyword.
1520 * @param[in,out] siblings Siblings to add to.
1521 *
1522 * @return LY_ERR values.
1523 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02001524LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001525parse_any(struct lys_parser_ctx *ctx, const char **data, enum yang_keyword kw, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001526{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001527 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001528 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001529 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001530 struct lysp_node_anydata *any;
1531
David Sedlák60adc092019-08-06 15:57:02 +02001532 /* create new structure and insert into siblings */
1533 LY_LIST_NEW_RET(ctx->ctx, siblings, any, next);
1534
Michal Vasko7fbc8162018-09-17 10:35:16 +02001535 any->nodetype = kw == YANG_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001536 any->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001537
Michal Vasko7fbc8162018-09-17 10:35:16 +02001538 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001539 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02001540 INSERT_WORD(ctx, buf, any->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001541
1542 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01001543 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001544 switch (kw) {
1545 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001546 LY_CHECK_RET(parse_config(ctx, data, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001547 break;
1548 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001549 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 +02001550 break;
1551 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001552 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 +02001553 break;
1554 case YANG_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001555 LY_CHECK_RET(parse_mandatory(ctx, data, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001556 break;
1557 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001558 LY_CHECK_RET(parse_restrs(ctx, data, kw, &any->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001559 break;
1560 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001561 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 +02001562 break;
1563 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001564 LY_CHECK_RET(parse_status(ctx, data, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001565 break;
1566 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001567 LY_CHECK_RET(parse_when(ctx, data, &any->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001568 break;
1569 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001570 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001571 break;
1572 default:
David Sedlákb3077192019-06-19 10:55:37 +02001573 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
Radek Krejcic59bc972018-09-17 16:13:06 +02001574 (any->nodetype & LYS_ANYDATA) == LYS_ANYDATA ? ly_stmt2str(YANG_ANYDATA) : ly_stmt2str(YANG_ANYXML));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001575 return LY_EVALID;
1576 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001577 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001578 return ret;
1579}
1580
Michal Vaskoea5abea2018-09-18 13:10:54 +02001581/**
1582 * @brief Parse the value or position statement. Substatement of type enum statement.
1583 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001584 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001585 * @param[in,out] data Data to read from, always moved to currently handled character.
1586 * @param[in] val_kw Type of this particular keyword.
1587 * @param[in,out] value Value to write to.
1588 * @param[in,out] flags Flags to write to.
1589 * @param[in,out] exts Extension instances to add to.
1590 *
1591 * @return LY_ERR values.
1592 */
David Sedlákd6ce6d72019-07-16 17:30:18 +02001593LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001594parse_type_enum_value_pos(struct lys_parser_ctx *ctx, const char **data, enum yang_keyword val_kw, int64_t *value, uint16_t *flags,
Michal Vasko7fbc8162018-09-17 10:35:16 +02001595 struct lysp_ext_instance **exts)
1596{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001597 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001598 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001599 size_t word_len;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02001600 long int num = 0;
1601 unsigned long int unum = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001602 enum yang_keyword kw;
1603
1604 if (*flags & LYS_SET_VALUE) {
David Sedlákb3077192019-06-19 10:55:37 +02001605 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001606 return LY_EVALID;
1607 }
1608 *flags |= LYS_SET_VALUE;
1609
1610 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001611 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001612
David Sedlákd6ce6d72019-07-16 17:30:18 +02001613 if (!word_len || (word[0] == '+') || ((word[0] == '0') && (word_len > 1)) || ((val_kw == YANG_POSITION) && !strncmp(word, "-0", 2))) {
David Sedlákb3077192019-06-19 10:55:37 +02001614 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001615 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001616 }
1617
1618 errno = 0;
1619 if (val_kw == YANG_VALUE) {
1620 num = strtol(word, &ptr, 10);
Radek Krejci8b764662018-11-14 14:15:13 +01001621 if (num < INT64_C(-2147483648) || num > INT64_C(2147483647)) {
David Sedlákb3077192019-06-19 10:55:37 +02001622 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001623 goto error;
1624 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001625 } else {
1626 unum = strtoul(word, &ptr, 10);
Radek Krejci8b764662018-11-14 14:15:13 +01001627 if (unum > UINT64_C(4294967295)) {
David Sedlákb3077192019-06-19 10:55:37 +02001628 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001629 goto error;
1630 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001631 }
1632 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001633 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001634 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001635 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001636 }
1637 if (errno == ERANGE) {
David Sedlákb3077192019-06-19 10:55:37 +02001638 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001639 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001640 }
1641 if (val_kw == YANG_VALUE) {
1642 *value = num;
1643 } else {
1644 *value = unum;
1645 }
1646 free(buf);
1647
Radek Krejci6d6556c2018-11-08 09:37:45 +01001648 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001649 switch (kw) {
1650 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001651 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, val_kw == YANG_VALUE ? LYEXT_SUBSTMT_VALUE : LYEXT_SUBSTMT_POSITION, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001652 break;
1653 default:
David Sedlákb3077192019-06-19 10:55:37 +02001654 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001655 return LY_EVALID;
1656 }
1657 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001658 return ret;
Radek Krejci8b764662018-11-14 14:15:13 +01001659
1660error:
1661 free(buf);
1662 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001663}
1664
Michal Vaskoea5abea2018-09-18 13:10:54 +02001665/**
1666 * @brief Parse the enum or bit statement. Substatement of type statement.
1667 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001668 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001669 * @param[in,out] data Data to read from, always moved to currently handled character.
1670 * @param[in] enum_kw Type of this particular keyword.
1671 * @param[in,out] enums Enums or bits to add to.
1672 *
1673 * @return LY_ERR values.
1674 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001675static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001676parse_type_enum(struct lys_parser_ctx *ctx, const char **data, enum yang_keyword enum_kw, struct lysp_type_enum **enums)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001677{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001678 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001679 char *buf, *word;
David Sedlák6544c182019-07-12 13:17:33 +02001680 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001681 enum yang_keyword kw;
1682 struct lysp_type_enum *enm;
1683
Radek Krejci2c4e7172018-10-19 15:56:26 +02001684 LY_ARRAY_NEW_RET(ctx->ctx, *enums, enm, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001685
1686 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001687 LY_CHECK_RET(get_argument(ctx, data, enum_kw == YANG_ENUM ? Y_STR_ARG : Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci8b764662018-11-14 14:15:13 +01001688 if (enum_kw == YANG_ENUM) {
David Sedlák07869a52019-07-12 14:28:19 +02001689 ret = lysp_check_enum_name(ctx, (const char *)word, word_len);
David Sedlák6544c182019-07-12 13:17:33 +02001690 LY_CHECK_ERR_RET(ret, free(buf), ret);
Radek Krejci8b764662018-11-14 14:15:13 +01001691 } else { /* YANG_BIT */
1692
1693 }
Radek Krejcif09e4e82019-06-14 15:08:11 +02001694 if (enum_kw == YANG_ENUM) {
David Sedlákb9b892c2019-07-12 14:44:02 +02001695 YANG_CHECK_NONEMPTY(ctx, word_len, "enum");
Radek Krejcif09e4e82019-06-14 15:08:11 +02001696 }
Radek Krejci44ceedc2018-10-02 15:54:31 +02001697 INSERT_WORD(ctx, buf, enm->name, word, word_len);
Radek Krejci8b764662018-11-14 14:15:13 +01001698 CHECK_UNIQUENESS(ctx, *enums, name, ly_stmt2str(enum_kw), enm->name);
1699
Radek Krejci6d6556c2018-11-08 09:37:45 +01001700 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001701 switch (kw) {
1702 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001703 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 +02001704 break;
1705 case YANG_IF_FEATURE:
Radek Krejciceaf2122019-01-02 15:03:26 +01001706 YANG_CHECK_STMTVER2_RET(ctx, "if-feature", ly_stmt2str(enum_kw));
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001707 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 +02001708 break;
1709 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001710 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 +02001711 break;
1712 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001713 LY_CHECK_RET(parse_status(ctx, data, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001714 break;
1715 case YANG_VALUE:
David Sedlák9fb515f2019-07-11 10:33:58 +02001716 LY_CHECK_ERR_RET(enum_kw == YANG_BIT, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
1717 ly_stmt2str(enum_kw)), LY_EVALID);
1718 LY_CHECK_RET(parse_type_enum_value_pos(ctx, data, kw, &enm->value, &enm->flags, &enm->exts));
1719 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001720 case YANG_POSITION:
David Sedlák9fb515f2019-07-11 10:33:58 +02001721 LY_CHECK_ERR_RET(enum_kw == YANG_ENUM, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
1722 ly_stmt2str(enum_kw)), LY_EVALID);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001723 LY_CHECK_RET(parse_type_enum_value_pos(ctx, data, kw, &enm->value, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001724 break;
1725 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001726 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001727 break;
1728 default:
David Sedlákb3077192019-06-19 10:55:37 +02001729 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001730 return LY_EVALID;
1731 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001732 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001733 return ret;
1734}
1735
Michal Vaskoea5abea2018-09-18 13:10:54 +02001736/**
1737 * @brief Parse the fraction-digits statement. Substatement of type statement.
1738 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001739 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001740 * @param[in,out] data Data to read from, always moved to currently handled character.
1741 * @param[in,out] fracdig Value to write to.
1742 * @param[in,out] exts Extension instances to add to.
1743 *
1744 * @return LY_ERR values.
1745 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001746static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001747parse_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 +02001748{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001749 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001750 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001751 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001752 unsigned long int num;
1753 enum yang_keyword kw;
1754
1755 if (*fracdig) {
David Sedlákb3077192019-06-19 10:55:37 +02001756 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001757 return LY_EVALID;
1758 }
1759
1760 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001761 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001762
1763 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
David Sedlákb3077192019-06-19 10:55:37 +02001764 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001765 free(buf);
1766 return LY_EVALID;
1767 }
1768
1769 errno = 0;
1770 num = strtoul(word, &ptr, 10);
1771 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001772 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001773 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001774 free(buf);
1775 return LY_EVALID;
1776 }
1777 if ((errno == ERANGE) || (num > 18)) {
David Sedlákb3077192019-06-19 10:55:37 +02001778 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001779 free(buf);
1780 return LY_EVALID;
1781 }
1782 *fracdig = num;
1783 free(buf);
1784
Radek Krejci6d6556c2018-11-08 09:37:45 +01001785 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001786 switch (kw) {
1787 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001788 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_FRACDIGITS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001789 break;
1790 default:
David Sedlákb3077192019-06-19 10:55:37 +02001791 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001792 return LY_EVALID;
1793 }
1794 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001795 return ret;
1796}
1797
Michal Vaskoea5abea2018-09-18 13:10:54 +02001798/**
1799 * @brief Parse the require-instance statement. Substatement of type statement.
1800 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001801 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001802 * @param[in,out] data Data to read from, always moved to currently handled character.
1803 * @param[in,out] reqinst Value to write to.
1804 * @param[in,out] flags Flags to write to.
1805 * @param[in,out] exts Extension instances to add to.
1806 *
1807 * @return LY_ERR values.
1808 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001809static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001810parse_type_reqinstance(struct lys_parser_ctx *ctx, const char **data, uint8_t *reqinst, uint16_t *flags,
Michal Vasko7fbc8162018-09-17 10:35:16 +02001811 struct lysp_ext_instance **exts)
1812{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001813 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001814 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001815 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001816 enum yang_keyword kw;
1817
1818 if (*flags & LYS_SET_REQINST) {
David Sedlákb3077192019-06-19 10:55:37 +02001819 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001820 return LY_EVALID;
1821 }
1822 *flags |= LYS_SET_REQINST;
1823
1824 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001825 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001826
1827 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1828 *reqinst = 1;
1829 } else if ((word_len != 5) || strncmp(word, "false", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001830 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001831 free(buf);
1832 return LY_EVALID;
1833 }
1834 free(buf);
1835
Radek Krejci6d6556c2018-11-08 09:37:45 +01001836 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001837 switch (kw) {
1838 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001839 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_REQINSTANCE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001840 break;
1841 default:
David Sedlákb3077192019-06-19 10:55:37 +02001842 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001843 return LY_EVALID;
1844 }
1845 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001846 return ret;
1847}
1848
Michal Vaskoea5abea2018-09-18 13:10:54 +02001849/**
1850 * @brief Parse the modifier statement. Substatement of type pattern statement.
1851 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001852 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001853 * @param[in,out] data Data to read from, always moved to currently handled character.
1854 * @param[in,out] pat Value to write to.
1855 * @param[in,out] exts Extension instances to add to.
1856 *
1857 * @return LY_ERR values.
1858 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001859static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001860parse_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 +02001861{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001862 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001863 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001864 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001865 enum yang_keyword kw;
1866
1867 if ((*pat)[0] == 0x15) {
David Sedlákb3077192019-06-19 10:55:37 +02001868 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001869 return LY_EVALID;
1870 }
1871
1872 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001873 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001874
1875 if ((word_len != 12) || strncmp(word, "invert-match", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001876 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001877 free(buf);
1878 return LY_EVALID;
1879 }
1880 free(buf);
1881
1882 /* replace the value in the dictionary */
1883 buf = malloc(strlen(*pat) + 1);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001884 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001885 strcpy(buf, *pat);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001886 lydict_remove(ctx->ctx, *pat);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001887
1888 assert(buf[0] == 0x06);
1889 buf[0] = 0x15;
Radek Krejci44ceedc2018-10-02 15:54:31 +02001890 *pat = lydict_insert_zc(ctx->ctx, buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001891
Radek Krejci6d6556c2018-11-08 09:37:45 +01001892 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001893 switch (kw) {
1894 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001895 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MODIFIER, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001896 break;
1897 default:
David Sedlákb3077192019-06-19 10:55:37 +02001898 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001899 return LY_EVALID;
1900 }
1901 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001902 return ret;
1903}
1904
Michal Vaskoea5abea2018-09-18 13:10:54 +02001905/**
1906 * @brief Parse the pattern statement. Substatement of type statement.
1907 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001908 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001909 * @param[in,out] data Data to read from, always moved to currently handled character.
1910 * @param[in,out] patterns Restrictions to add to.
1911 *
1912 * @return LY_ERR values.
1913 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001914static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001915parse_type_pattern(struct lys_parser_ctx *ctx, const char **data, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001916{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001917 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001918 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001919 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001920 enum yang_keyword kw;
1921 struct lysp_restr *restr;
1922
Radek Krejci2c4e7172018-10-19 15:56:26 +02001923 LY_ARRAY_NEW_RET(ctx->ctx, *patterns, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001924
1925 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001926 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001927
1928 /* add special meaning first byte */
1929 if (buf) {
1930 buf = realloc(buf, word_len + 2);
1931 word = buf;
1932 } else {
1933 buf = malloc(word_len + 2);
1934 }
Radek Krejci44ceedc2018-10-02 15:54:31 +02001935 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejci86d106e2018-10-18 09:53:19 +02001936 memmove(buf + 1, word, word_len);
1937 buf[0] = 0x06; /* pattern's default regular-match flag */
1938 buf[word_len + 1] = '\0'; /* terminating NULL byte */
1939 restr->arg = lydict_insert_zc(ctx->ctx, buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001940
Radek Krejci6d6556c2018-11-08 09:37:45 +01001941 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001942 switch (kw) {
1943 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001944 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 +02001945 break;
1946 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001947 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 +02001948 break;
1949 case YANG_ERROR_APP_TAG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001950 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 +02001951 break;
1952 case YANG_ERROR_MESSAGE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001953 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 +02001954 break;
1955 case YANG_MODIFIER:
Radek Krejciceaf2122019-01-02 15:03:26 +01001956 YANG_CHECK_STMTVER2_RET(ctx, "modifier", "pattern");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001957 LY_CHECK_RET(parse_type_pattern_modifier(ctx, data, &restr->arg, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001958 break;
1959 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001960 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001961 break;
1962 default:
David Sedlákb3077192019-06-19 10:55:37 +02001963 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001964 return LY_EVALID;
1965 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001966 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001967 return ret;
1968}
1969
Michal Vaskoea5abea2018-09-18 13:10:54 +02001970/**
1971 * @brief Parse the type statement.
1972 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001973 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001974 * @param[in,out] data Data to read from, always moved to currently handled character.
1975 * @param[in,out] type Type to wrote to.
1976 *
1977 * @return LY_ERR values.
1978 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001979static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001980parse_type(struct lys_parser_ctx *ctx, const char **data, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001981{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001982 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001983 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001984 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001985 enum yang_keyword kw;
1986 struct lysp_type *nest_type;
1987
1988 if (type->name) {
David Sedlákb3077192019-06-19 10:55:37 +02001989 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001990 return LY_EVALID;
1991 }
1992
1993 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001994 LY_CHECK_RET(get_argument(ctx, data, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02001995 INSERT_WORD(ctx, buf, type->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001996
Radek Krejci6d6556c2018-11-08 09:37:45 +01001997 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001998 switch (kw) {
1999 case YANG_BASE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002000 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 +01002001 type->flags |= LYS_SET_BASE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002002 break;
2003 case YANG_BIT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002004 LY_CHECK_RET(parse_type_enum(ctx, data, kw, &type->bits));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002005 type->flags |= LYS_SET_BIT;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002006 break;
2007 case YANG_ENUM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002008 LY_CHECK_RET(parse_type_enum(ctx, data, kw, &type->enums));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002009 type->flags |= LYS_SET_ENUM;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002010 break;
2011 case YANG_FRACTION_DIGITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002012 LY_CHECK_RET(parse_type_fracdigits(ctx, data, &type->fraction_digits, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002013 type->flags |= LYS_SET_FRDIGITS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002014 break;
2015 case YANG_LENGTH:
2016 if (type->length) {
David Sedlákb3077192019-06-19 10:55:37 +02002017 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002018 return LY_EVALID;
2019 }
2020 type->length = calloc(1, sizeof *type->length);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002021 LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002022
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002023 LY_CHECK_RET(parse_restr(ctx, data, kw, type->length));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002024 type->flags |= LYS_SET_LENGTH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002025 break;
2026 case YANG_PATH:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002027 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 +01002028 type->flags |= LYS_SET_PATH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002029 break;
2030 case YANG_PATTERN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002031 LY_CHECK_RET(parse_type_pattern(ctx, data, &type->patterns));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002032 type->flags |= LYS_SET_PATTERN;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002033 break;
2034 case YANG_RANGE:
2035 if (type->range) {
David Sedlákb3077192019-06-19 10:55:37 +02002036 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002037 return LY_EVALID;
2038 }
2039 type->range = calloc(1, sizeof *type->range);
David Sedlák7a8b2472019-07-11 15:08:34 +02002040 LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002041
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002042 LY_CHECK_RET(parse_restr(ctx, data, kw, type->range));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002043 type->flags |= LYS_SET_RANGE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002044 break;
2045 case YANG_REQUIRE_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002046 LY_CHECK_RET(parse_type_reqinstance(ctx, data, &type->require_instance, &type->flags, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002047 /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002048 break;
2049 case YANG_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002050 LY_ARRAY_NEW_RET(ctx->ctx, type->types, nest_type, LY_EMEM);
2051 LY_CHECK_RET(parse_type(ctx, data, nest_type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002052 type->flags |= LYS_SET_TYPE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002053 break;
2054 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002055 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &type->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002056 break;
2057 default:
David Sedlákb3077192019-06-19 10:55:37 +02002058 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002059 return LY_EVALID;
2060 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002061 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002062 return ret;
2063}
2064
Michal Vaskoea5abea2018-09-18 13:10:54 +02002065/**
2066 * @brief Parse the leaf statement.
2067 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002068 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002069 * @param[in,out] data Data to read from, always moved to currently handled character.
2070 * @param[in,out] siblings Siblings to add to.
2071 *
2072 * @return LY_ERR values.
2073 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002074LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002075parse_leaf(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002076{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002077 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002078 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002079 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002080 enum yang_keyword kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002081 struct lysp_node_leaf *leaf;
2082
David Sedlák60adc092019-08-06 15:57:02 +02002083 /* create new leaf structure */
2084 LY_LIST_NEW_RET(ctx->ctx, siblings, leaf, next);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002085 leaf->nodetype = LYS_LEAF;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002086 leaf->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002087
Michal Vasko7fbc8162018-09-17 10:35:16 +02002088 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002089 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002090 INSERT_WORD(ctx, buf, leaf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002091
2092 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01002093 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002094 switch (kw) {
2095 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002096 LY_CHECK_RET(parse_config(ctx, data, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002097 break;
2098 case YANG_DEFAULT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002099 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 +02002100 break;
2101 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002102 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 +02002103 break;
2104 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002105 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 +02002106 break;
2107 case YANG_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002108 LY_CHECK_RET(parse_mandatory(ctx, data, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002109 break;
2110 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002111 LY_CHECK_RET(parse_restrs(ctx, data, kw, &leaf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002112 break;
2113 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002114 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 +02002115 break;
2116 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002117 LY_CHECK_RET(parse_status(ctx, data, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002118 break;
2119 case YANG_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002120 LY_CHECK_RET(parse_type(ctx, data, &leaf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002121 break;
2122 case YANG_UNITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002123 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 +02002124 break;
2125 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002126 LY_CHECK_RET(parse_when(ctx, data, &leaf->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002127 break;
2128 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002129 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002130 break;
2131 default:
David Sedlákb3077192019-06-19 10:55:37 +02002132 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002133 return LY_EVALID;
2134 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002135 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002136 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002137checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002138 /* mandatory substatements */
2139 if (!leaf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002140 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002141 return LY_EVALID;
2142 }
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002143 if ((leaf->flags & LYS_MAND_TRUE) && (leaf->dflt)) {
David Sedlákb3077192019-06-19 10:55:37 +02002144 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMSCOMB, "mandatory", "default", "leaf");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002145 return LY_EVALID;
2146 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002147
2148 return ret;
2149}
2150
Michal Vaskoea5abea2018-09-18 13:10:54 +02002151/**
2152 * @brief Parse the max-elements statement.
2153 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002154 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002155 * @param[in,out] data Data to read from, always moved to currently handled character.
2156 * @param[in,out] max Value to write to.
2157 * @param[in,out] flags Flags to write to.
2158 * @param[in,out] exts Extension instances to add to.
2159 *
2160 * @return LY_ERR values.
2161 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002162LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002163parse_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 +02002164{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002165 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002166 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002167 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002168 unsigned long int num;
2169 enum yang_keyword kw;
2170
2171 if (*flags & LYS_SET_MAX) {
David Sedlákb3077192019-06-19 10:55:37 +02002172 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002173 return LY_EVALID;
2174 }
2175 *flags |= LYS_SET_MAX;
2176
2177 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002178 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002179
2180 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
David Sedlákb3077192019-06-19 10:55:37 +02002181 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002182 free(buf);
2183 return LY_EVALID;
2184 }
2185
2186 if (strncmp(word, "unbounded", word_len)) {
2187 errno = 0;
2188 num = strtoul(word, &ptr, 10);
2189 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002190 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002191 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002192 free(buf);
2193 return LY_EVALID;
2194 }
2195 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002196 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002197 free(buf);
2198 return LY_EVALID;
2199 }
2200
2201 *max = num;
2202 }
2203 free(buf);
2204
Radek Krejci6d6556c2018-11-08 09:37:45 +01002205 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002206 switch (kw) {
2207 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002208 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MAX, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002209 break;
2210 default:
David Sedlákb3077192019-06-19 10:55:37 +02002211 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002212 return LY_EVALID;
2213 }
2214 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002215 return ret;
2216}
2217
Michal Vaskoea5abea2018-09-18 13:10:54 +02002218/**
2219 * @brief Parse the min-elements statement.
2220 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002221 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002222 * @param[in,out] data Data to read from, always moved to currently handled character.
2223 * @param[in,out] min Value to write to.
2224 * @param[in,out] flags Flags to write to.
2225 * @param[in,out] exts Extension instances to add to.
2226 *
2227 * @return LY_ERR values.
2228 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002229LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002230parse_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 +02002231{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002232 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002233 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002234 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002235 unsigned long int num;
2236 enum yang_keyword kw;
2237
2238 if (*flags & LYS_SET_MIN) {
David Sedlákb3077192019-06-19 10:55:37 +02002239 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002240 return LY_EVALID;
2241 }
2242 *flags |= LYS_SET_MIN;
2243
2244 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002245 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002246
2247 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
David Sedlákb3077192019-06-19 10:55:37 +02002248 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002249 free(buf);
2250 return LY_EVALID;
2251 }
2252
2253 errno = 0;
2254 num = strtoul(word, &ptr, 10);
2255 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002256 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002257 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002258 free(buf);
2259 return LY_EVALID;
2260 }
2261 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002262 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002263 free(buf);
2264 return LY_EVALID;
2265 }
2266 *min = num;
2267 free(buf);
2268
Radek Krejci6d6556c2018-11-08 09:37:45 +01002269 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002270 switch (kw) {
2271 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002272 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MIN, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002273 break;
2274 default:
David Sedlákb3077192019-06-19 10:55:37 +02002275 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002276 return LY_EVALID;
2277 }
2278 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002279 return ret;
2280}
2281
Michal Vaskoea5abea2018-09-18 13:10:54 +02002282/**
2283 * @brief Parse the ordered-by statement.
2284 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002285 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002286 * @param[in,out] data Data to read from, always moved to currently handled character.
2287 * @param[in,out] flags Flags to write to.
2288 * @param[in,out] exts Extension instances to add to.
2289 *
2290 * @return LY_ERR values.
2291 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002292static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002293parse_orderedby(struct lys_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002294{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002295 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002296 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002297 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002298 enum yang_keyword kw;
2299
2300 if (*flags & LYS_ORDBY_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02002301 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002302 return LY_EVALID;
2303 }
2304
2305 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002306 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002307
2308 if ((word_len == 6) && !strncmp(word, "system", word_len)) {
2309 *flags |= LYS_ORDBY_SYSTEM;
2310 } else if ((word_len == 4) && !strncmp(word, "user", word_len)) {
2311 *flags |= LYS_ORDBY_USER;
2312 } else {
David Sedlákb3077192019-06-19 10:55:37 +02002313 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002314 free(buf);
2315 return LY_EVALID;
2316 }
2317 free(buf);
2318
Radek Krejci6d6556c2018-11-08 09:37:45 +01002319 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002320 switch (kw) {
2321 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002322 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_ORDEREDBY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002323 break;
2324 default:
David Sedlákb3077192019-06-19 10:55:37 +02002325 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002326 return LY_EVALID;
2327 }
2328 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002329 return ret;
2330}
2331
Michal Vaskoea5abea2018-09-18 13:10:54 +02002332/**
2333 * @brief Parse the leaf-list statement.
2334 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002335 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002336 * @param[in,out] data Data to read from, always moved to currently handled character.
2337 * @param[in,out] siblings Siblings to add to.
2338 *
2339 * @return LY_ERR values.
2340 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002341LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002342parse_leaflist(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002343{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002344 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002345 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002346 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002347 enum yang_keyword kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002348 struct lysp_node_leaflist *llist;
2349
David Sedlák60adc092019-08-06 15:57:02 +02002350 /* create new leaf-list structure */
2351 LY_LIST_NEW_RET(ctx->ctx, siblings, llist, next);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002352 llist->nodetype = LYS_LEAFLIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002353 llist->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002354
Michal Vasko7fbc8162018-09-17 10:35:16 +02002355 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002356 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002357 INSERT_WORD(ctx, buf, llist->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002358
2359 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01002360 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002361 switch (kw) {
2362 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002363 LY_CHECK_RET(parse_config(ctx, data, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002364 break;
2365 case YANG_DEFAULT:
Radek Krejciceaf2122019-01-02 15:03:26 +01002366 YANG_CHECK_STMTVER2_RET(ctx, "default", "leaf-list");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002367 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 +02002368 break;
2369 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002370 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 +02002371 break;
2372 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002373 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 +02002374 break;
2375 case YANG_MAX_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002376 LY_CHECK_RET(parse_maxelements(ctx, data, &llist->max, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002377 break;
2378 case YANG_MIN_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002379 LY_CHECK_RET(parse_minelements(ctx, data, &llist->min, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002380 break;
2381 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002382 LY_CHECK_RET(parse_restrs(ctx, data, kw, &llist->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002383 break;
2384 case YANG_ORDERED_BY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002385 LY_CHECK_RET(parse_orderedby(ctx, data, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002386 break;
2387 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002388 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 +02002389 break;
2390 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002391 LY_CHECK_RET(parse_status(ctx, data, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002392 break;
2393 case YANG_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002394 LY_CHECK_RET(parse_type(ctx, data, &llist->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002395 break;
2396 case YANG_UNITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002397 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 +02002398 break;
2399 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002400 LY_CHECK_RET(parse_when(ctx, data, &llist->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002401 break;
2402 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002403 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002404 break;
2405 default:
David Sedlákb3077192019-06-19 10:55:37 +02002406 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002407 return LY_EVALID;
2408 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002409 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002410 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002411checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002412 /* mandatory substatements */
2413 if (!llist->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002414 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002415 return LY_EVALID;
2416 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01002417 if ((llist->min) && (llist->dflts)) {
David Sedlákb3077192019-06-19 10:55:37 +02002418 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMSCOMB, "min-elements", "default", "leaf-list");
Radek Krejci0e5d8382018-11-28 16:37:53 +01002419 return LY_EVALID;
2420 }
Radek Krejcidf6cad12018-11-28 17:10:55 +01002421 if (llist->max && llist->min > llist->max) {
David Sedlákb3077192019-06-19 10:55:37 +02002422 LOGVAL_PARSER(ctx, LYVE_SEMANTICS,
Radek Krejcidf6cad12018-11-28 17:10:55 +01002423 "Invalid combination of min-elements and max-elements: min value %u is bigger than the max value %u.",
2424 llist->min, llist->max);
2425 return LY_EVALID;
2426 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002427
2428 return ret;
2429}
2430
Michal Vaskoea5abea2018-09-18 13:10:54 +02002431/**
2432 * @brief Parse the refine statement.
2433 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002434 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002435 * @param[in,out] data Data to read from, always moved to currently handled character.
2436 * @param[in,out] refines Refines to add to.
2437 *
2438 * @return LY_ERR values.
2439 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002440static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002441parse_refine(struct lys_parser_ctx *ctx, const char **data, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002442{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002443 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002444 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002445 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002446 enum yang_keyword kw;
2447 struct lysp_refine *rf;
2448
Radek Krejci2c4e7172018-10-19 15:56:26 +02002449 LY_ARRAY_NEW_RET(ctx->ctx, *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002450
2451 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002452 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
David Sedlákb9b892c2019-07-12 14:44:02 +02002453 YANG_CHECK_NONEMPTY(ctx, word_len, "refine");
Radek Krejci44ceedc2018-10-02 15:54:31 +02002454 INSERT_WORD(ctx, buf, rf->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002455
Radek Krejci6d6556c2018-11-08 09:37:45 +01002456 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002457 switch (kw) {
2458 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002459 LY_CHECK_RET(parse_config(ctx, data, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002460 break;
2461 case YANG_DEFAULT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002462 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 +02002463 break;
2464 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002465 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 +02002466 break;
2467 case YANG_IF_FEATURE:
Radek Krejciceaf2122019-01-02 15:03:26 +01002468 YANG_CHECK_STMTVER2_RET(ctx, "if-feature", "refine");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002469 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 +02002470 break;
2471 case YANG_MAX_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002472 LY_CHECK_RET(parse_maxelements(ctx, data, &rf->max, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002473 break;
2474 case YANG_MIN_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002475 LY_CHECK_RET(parse_minelements(ctx, data, &rf->min, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002476 break;
2477 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002478 LY_CHECK_RET(parse_restrs(ctx, data, kw, &rf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002479 break;
2480 case YANG_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002481 LY_CHECK_RET(parse_mandatory(ctx, data, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002482 break;
2483 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002484 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 +02002485 break;
2486 case YANG_PRESENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002487 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 +02002488 break;
2489 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002490 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002491 break;
2492 default:
David Sedlákb3077192019-06-19 10:55:37 +02002493 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002494 return LY_EVALID;
2495 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002496 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002497 return ret;
2498}
2499
Michal Vaskoea5abea2018-09-18 13:10:54 +02002500/**
2501 * @brief Parse the typedef statement.
2502 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002503 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002504 * @param[in,out] data Data to read from, always moved to currently handled character.
2505 * @param[in,out] typedefs Typedefs to add to.
2506 *
2507 * @return LY_ERR values.
2508 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002509static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002510parse_typedef(struct lys_parser_ctx *ctx, struct lysp_node *parent, const char **data, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002511{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002512 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002513 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002514 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002515 enum yang_keyword kw;
2516 struct lysp_tpdf *tpdf;
2517
Radek Krejci2c4e7172018-10-19 15:56:26 +02002518 LY_ARRAY_NEW_RET(ctx->ctx, *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002519
2520 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002521 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002522 INSERT_WORD(ctx, buf, tpdf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002523
2524 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01002525 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002526 switch (kw) {
2527 case YANG_DEFAULT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002528 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 +02002529 break;
2530 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002531 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 +02002532 break;
2533 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002534 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 +02002535 break;
2536 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002537 LY_CHECK_RET(parse_status(ctx, data, &tpdf->flags, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002538 break;
2539 case YANG_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002540 LY_CHECK_RET(parse_type(ctx, data, &tpdf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002541 break;
2542 case YANG_UNITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002543 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 +02002544 break;
2545 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002546 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002547 break;
2548 default:
David Sedlákb3077192019-06-19 10:55:37 +02002549 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002550 return LY_EVALID;
2551 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002552 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002553 LY_CHECK_RET(ret);
Radek Krejcibbe09a92018-11-08 09:36:54 +01002554checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002555 /* mandatory substatements */
2556 if (!tpdf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002557 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002558 return LY_EVALID;
2559 }
2560
Radek Krejcibbe09a92018-11-08 09:36:54 +01002561 /* store data for collision check */
Radek Krejci7fc68292019-06-12 13:51:09 +02002562 if (parent && !(parent->nodetype & (LYS_GROUPING | LYS_ACTION | LYS_INOUT | LYS_NOTIF))) {
David Sedláke8b74df2019-08-14 14:18:22 +02002563 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, parent, 0) == -1, LY_EMEM);
Radek Krejcibbe09a92018-11-08 09:36:54 +01002564 }
2565
Michal Vasko7fbc8162018-09-17 10:35:16 +02002566 return ret;
2567}
2568
Michal Vaskoea5abea2018-09-18 13:10:54 +02002569/**
2570 * @brief Parse the input or output statement.
2571 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002572 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002573 * @param[in,out] data Data to read from, always moved to currently handled character.
2574 * @param[in] kw Type of this particular keyword
2575 * @param[in,out] inout_p Input/output pointer to write to.
2576 *
2577 * @return LY_ERR values.
2578 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002579static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002580parse_inout(struct lys_parser_ctx *ctx, const char **data, enum yang_keyword inout_kw, struct lysp_node *parent, struct lysp_action_inout *inout_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002581{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002582 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002583 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002584 size_t word_len;
Radek Krejci10113652018-11-14 16:56:50 +01002585 enum yang_keyword kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002586
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002587 if (inout_p->nodetype) {
David Sedlákb3077192019-06-19 10:55:37 +02002588 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002589 return LY_EVALID;
2590 }
2591
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002592 /* initiate structure */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002593 inout_p->nodetype = &((struct lysp_action*)parent)->input == inout_p ? LYS_INPUT : LYS_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002594 inout_p->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002595
2596 /* parse substatements */
Radek Krejci7fc68292019-06-12 13:51:09 +02002597 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002598 switch (kw) {
2599 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01002600 YANG_CHECK_STMTVER2_RET(ctx, "anydata", ly_stmt2str(inout_kw));
Radek Krejci10113652018-11-14 16:56:50 +01002601 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002602 case YANG_ANYXML:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002603 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002604 break;
2605 case YANG_CHOICE:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002606 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002607 break;
2608 case YANG_CONTAINER:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002609 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002610 break;
2611 case YANG_LEAF:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002612 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002613 break;
2614 case YANG_LEAF_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002615 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002616 break;
2617 case YANG_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002618 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002619 break;
2620 case YANG_USES:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002621 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002622 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002623 case YANG_TYPEDEF:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002624 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)inout_p, data, &inout_p->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002625 break;
2626 case YANG_MUST:
Radek Krejciceaf2122019-01-02 15:03:26 +01002627 YANG_CHECK_STMTVER2_RET(ctx, "must", ly_stmt2str(inout_kw));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002628 LY_CHECK_RET(parse_restrs(ctx, data, kw, &inout_p->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002629 break;
2630 case YANG_GROUPING:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002631 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)inout_p, &inout_p->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002632 break;
2633 case YANG_CUSTOM:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002634 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inout_p->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002635 break;
2636 default:
David Sedlákb3077192019-06-19 10:55:37 +02002637 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002638 return LY_EVALID;
2639 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002640 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002641 LY_CHECK_RET(ret);
2642checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002643 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02002644 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, inout_p->groupings, NULL, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02002645
Michal Vasko7fbc8162018-09-17 10:35:16 +02002646 return ret;
2647}
2648
Michal Vaskoea5abea2018-09-18 13:10:54 +02002649/**
2650 * @brief Parse the action statement.
2651 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002652 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002653 * @param[in,out] data Data to read from, always moved to currently handled character.
2654 * @param[in,out] actions Actions to add to.
2655 *
2656 * @return LY_ERR values.
2657 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002658LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002659parse_action(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002660{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002661 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002662 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002663 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002664 enum yang_keyword kw;
2665 struct lysp_action *act;
2666
Radek Krejci2c4e7172018-10-19 15:56:26 +02002667 LY_ARRAY_NEW_RET(ctx->ctx, *actions, act, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002668
2669 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002670 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002671 INSERT_WORD(ctx, buf, act->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002672 act->nodetype = LYS_ACTION;
2673 act->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002674
Radek Krejci7fc68292019-06-12 13:51:09 +02002675 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002676 switch (kw) {
2677 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002678 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 +02002679 break;
2680 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002681 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 +02002682 break;
2683 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002684 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 +02002685 break;
2686 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002687 LY_CHECK_RET(parse_status(ctx, data, &act->flags, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002688 break;
2689
2690 case YANG_INPUT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002691 LY_CHECK_RET(parse_inout(ctx, data, kw, (struct lysp_node*)act, &act->input));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002692 break;
2693 case YANG_OUTPUT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002694 LY_CHECK_RET(parse_inout(ctx, data, kw, (struct lysp_node*)act, &act->output));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002695 break;
2696
2697 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01002698 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)act, data, &act->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002699 break;
2700 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002701 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)act, &act->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002702 break;
2703 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002704 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002705 break;
2706 default:
David Sedlákb3077192019-06-19 10:55:37 +02002707 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), parent ? "action" : "rpc");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002708 return LY_EVALID;
2709 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002710 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002711 LY_CHECK_RET(ret);
2712checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002713 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02002714 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, act->groupings, NULL, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02002715
Michal Vasko7fbc8162018-09-17 10:35:16 +02002716 return ret;
2717}
2718
Michal Vaskoea5abea2018-09-18 13:10:54 +02002719/**
2720 * @brief Parse the notification statement.
2721 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002722 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002723 * @param[in,out] data Data to read from, always moved to currently handled character.
2724 * @param[in,out] notifs Notifications to add to.
2725 *
2726 * @return LY_ERR values.
2727 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002728LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002729parse_notif(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002730{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002731 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002732 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002733 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002734 enum yang_keyword kw;
2735 struct lysp_notif *notif;
2736
Radek Krejci2c4e7172018-10-19 15:56:26 +02002737 LY_ARRAY_NEW_RET(ctx->ctx, *notifs, notif, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002738
2739 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002740 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002741 INSERT_WORD(ctx, buf, notif->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002742 notif->nodetype = LYS_NOTIF;
2743 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002744
Radek Krejci7fc68292019-06-12 13:51:09 +02002745 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002746 switch (kw) {
2747 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002748 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 +02002749 break;
2750 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002751 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 +02002752 break;
2753 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002754 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 +02002755 break;
2756 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002757 LY_CHECK_RET(parse_status(ctx, data, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002758 break;
2759
2760 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01002761 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "notification");
Radek Krejci10113652018-11-14 16:56:50 +01002762 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002763 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002764 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002765 break;
2766 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002767 LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002768 break;
2769 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002770 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002771 break;
2772 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002773 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002774 break;
2775 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002776 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002777 break;
2778 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002779 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002780 break;
2781 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002782 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002783 break;
2784
2785 case YANG_MUST:
Radek Krejciceaf2122019-01-02 15:03:26 +01002786 YANG_CHECK_STMTVER2_RET(ctx, "must", "notification");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002787 LY_CHECK_RET(parse_restrs(ctx, data, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002788 break;
2789 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01002790 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)notif, data, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002791 break;
2792 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002793 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002794 break;
2795 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002796 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002797 break;
2798 default:
David Sedlákb3077192019-06-19 10:55:37 +02002799 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002800 return LY_EVALID;
2801 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002802 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002803 LY_CHECK_RET(ret);
2804checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002805 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02002806 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, notif->groupings, NULL, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02002807
Michal Vasko7fbc8162018-09-17 10:35:16 +02002808 return ret;
2809}
2810
Michal Vaskoea5abea2018-09-18 13:10:54 +02002811/**
2812 * @brief Parse the grouping statement.
2813 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002814 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002815 * @param[in,out] data Data to read from, always moved to currently handled character.
2816 * @param[in,out] groupings Groupings to add to.
2817 *
2818 * @return LY_ERR values.
2819 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002820LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002821parse_grouping(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002822{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002823 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002824 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002825 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002826 enum yang_keyword kw;
2827 struct lysp_grp *grp;
2828
Radek Krejci2c4e7172018-10-19 15:56:26 +02002829 LY_ARRAY_NEW_RET(ctx->ctx, *groupings, grp, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002830
2831 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002832 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002833 INSERT_WORD(ctx, buf, grp->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002834 grp->nodetype = LYS_GROUPING;
2835 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002836
Radek Krejci7fc68292019-06-12 13:51:09 +02002837 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002838 switch (kw) {
2839 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002840 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 +02002841 break;
2842 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002843 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 +02002844 break;
2845 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002846 LY_CHECK_RET(parse_status(ctx, data, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002847 break;
2848
2849 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01002850 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "grouping");
Radek Krejci10113652018-11-14 16:56:50 +01002851 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002852 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002853 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002854 break;
2855 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002856 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002857 break;
2858 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002859 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002860 break;
2861 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002862 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002863 break;
2864 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002865 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002866 break;
2867 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002868 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002869 break;
2870 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002871 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002872 break;
2873
2874 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01002875 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)grp, data, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002876 break;
2877 case YANG_ACTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01002878 YANG_CHECK_STMTVER2_RET(ctx, "action", "grouping");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002879 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)grp, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002880 break;
2881 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002882 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)grp, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002883 break;
2884 case YANG_NOTIFICATION:
Radek Krejciceaf2122019-01-02 15:03:26 +01002885 YANG_CHECK_STMTVER2_RET(ctx, "notification", "grouping");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002886 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)grp, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002887 break;
2888 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002889 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002890 break;
2891 default:
David Sedlákb3077192019-06-19 10:55:37 +02002892 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002893 return LY_EVALID;
2894 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002895 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002896 LY_CHECK_RET(ret);
2897checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002898 /* finalize parent pointers to the reallocated items */
David Sedlák0d6de5a2019-07-22 13:25:44 +02002899 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, grp->groupings, NULL, grp->actions, grp->notifs));
Radek Krejci7fc68292019-06-12 13:51:09 +02002900
Michal Vasko7fbc8162018-09-17 10:35:16 +02002901 return ret;
2902}
2903
Michal Vaskoea5abea2018-09-18 13:10:54 +02002904/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02002905 * @brief Parse the augment statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002906 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002907 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002908 * @param[in,out] data Data to read from, always moved to currently handled character.
2909 * @param[in,out] augments Augments to add to.
2910 *
2911 * @return LY_ERR values.
2912 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002913LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002914parse_augment(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002915{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002916 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002917 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002918 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002919 enum yang_keyword kw;
2920 struct lysp_augment *aug;
2921
Radek Krejci2c4e7172018-10-19 15:56:26 +02002922 LY_ARRAY_NEW_RET(ctx->ctx, *augments, aug, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002923
2924 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002925 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
David Sedlákb9b892c2019-07-12 14:44:02 +02002926 YANG_CHECK_NONEMPTY(ctx, word_len, "augment");
Radek Krejci44ceedc2018-10-02 15:54:31 +02002927 INSERT_WORD(ctx, buf, aug->nodeid, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002928 aug->nodetype = LYS_AUGMENT;
2929 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002930
Radek Krejci7fc68292019-06-12 13:51:09 +02002931 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002932 switch (kw) {
2933 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002934 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 +02002935 break;
2936 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002937 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 +02002938 break;
2939 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002940 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 +02002941 break;
2942 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002943 LY_CHECK_RET(parse_status(ctx, data, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002944 break;
2945 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002946 LY_CHECK_RET(parse_when(ctx, data, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002947 break;
2948
2949 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01002950 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "augment");
Radek Krejci10113652018-11-14 16:56:50 +01002951 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002952 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002953 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002954 break;
2955 case YANG_CASE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002956 LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002957 break;
2958 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002959 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002960 break;
2961 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002962 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002963 break;
2964 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002965 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002966 break;
2967 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002968 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002969 break;
2970 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002971 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002972 break;
2973 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002974 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002975 break;
2976
2977 case YANG_ACTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01002978 YANG_CHECK_STMTVER2_RET(ctx, "action", "augment");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002979 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002980 break;
2981 case YANG_NOTIFICATION:
Radek Krejciceaf2122019-01-02 15:03:26 +01002982 YANG_CHECK_STMTVER2_RET(ctx, "notification", "augment");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002983 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002984 break;
2985 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002986 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002987 break;
2988 default:
David Sedlákb3077192019-06-19 10:55:37 +02002989 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002990 return LY_EVALID;
2991 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002992 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002993 LY_CHECK_RET(ret);
2994checks:
2995 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02002996 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, NULL, NULL, aug->actions, aug->notifs));
Radek Krejci7fc68292019-06-12 13:51:09 +02002997
Michal Vasko7fbc8162018-09-17 10:35:16 +02002998 return ret;
2999}
3000
Michal Vaskoea5abea2018-09-18 13:10:54 +02003001/**
3002 * @brief Parse the uses statement.
3003 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003004 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003005 * @param[in,out] data Data to read from, always moved to currently handled character.
3006 * @param[in,out] siblings Siblings to add to.
3007 *
3008 * @return LY_ERR values.
3009 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003010LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003011parse_uses(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003012{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003013 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003014 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003015 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003016 enum yang_keyword kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003017 struct lysp_node_uses *uses;
3018
David Sedlák60adc092019-08-06 15:57:02 +02003019 /* create uses structure */
3020 LY_LIST_NEW_RET(ctx->ctx, siblings, uses, next);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003021 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003022 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003023
Michal Vasko7fbc8162018-09-17 10:35:16 +02003024 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003025 LY_CHECK_RET(get_argument(ctx, data, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003026 INSERT_WORD(ctx, buf, uses->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003027
3028 /* parse substatements */
Radek Krejci7fc68292019-06-12 13:51:09 +02003029 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003030 switch (kw) {
3031 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003032 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 +02003033 break;
3034 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003035 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 +02003036 break;
3037 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003038 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 +02003039 break;
3040 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003041 LY_CHECK_RET(parse_status(ctx, data, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003042 break;
3043 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003044 LY_CHECK_RET(parse_when(ctx, data, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003045 break;
3046
3047 case YANG_REFINE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003048 LY_CHECK_RET(parse_refine(ctx, data, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003049 break;
3050 case YANG_AUGMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003051 LY_CHECK_RET(parse_augment(ctx, data, (struct lysp_node*)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003052 break;
3053 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003054 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003055 break;
3056 default:
David Sedlákb3077192019-06-19 10:55:37 +02003057 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003058 return LY_EVALID;
3059 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003060 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003061checks:
3062 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02003063 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, NULL, uses->augments, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02003064
Michal Vasko7fbc8162018-09-17 10:35:16 +02003065 return ret;
3066}
3067
Michal Vaskoea5abea2018-09-18 13:10:54 +02003068/**
3069 * @brief Parse the case statement.
3070 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003071 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003072 * @param[in,out] data Data to read from, always moved to currently handled character.
3073 * @param[in,out] siblings Siblings to add to.
3074 *
3075 * @return LY_ERR values.
3076 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003077LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003078parse_case(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003079{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003080 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003081 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003082 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003083 enum yang_keyword kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003084 struct lysp_node_case *cas;
3085
David Sedlák60adc092019-08-06 15:57:02 +02003086 /* create new case structure */
3087 LY_LIST_NEW_RET(ctx->ctx, siblings, cas, next);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003088 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003089 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003090
Michal Vasko7fbc8162018-09-17 10:35:16 +02003091 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003092 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003093 INSERT_WORD(ctx, buf, cas->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003094
3095 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01003096 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003097 switch (kw) {
3098 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003099 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 +02003100 break;
3101 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003102 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 +02003103 break;
3104 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003105 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 +02003106 break;
3107 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003108 LY_CHECK_RET(parse_status(ctx, data, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003109 break;
3110 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003111 LY_CHECK_RET(parse_when(ctx, data, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003112 break;
3113
3114 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01003115 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "case");
Radek Krejci10113652018-11-14 16:56:50 +01003116 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003117 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003118 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003119 break;
3120 case YANG_CHOICE:
Radek Krejcia9026eb2018-12-12 16:04:47 +01003121 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003122 break;
3123 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003124 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003125 break;
3126 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003127 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003128 break;
3129 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003130 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003131 break;
3132 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003133 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003134 break;
3135 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003136 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003137 break;
3138 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003139 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003140 break;
3141 default:
David Sedlákb3077192019-06-19 10:55:37 +02003142 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003143 return LY_EVALID;
3144 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003145 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003146 return ret;
3147}
3148
Michal Vaskoea5abea2018-09-18 13:10:54 +02003149/**
3150 * @brief Parse the choice statement.
3151 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003152 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003153 * @param[in,out] data Data to read from, always moved to currently handled character.
3154 * @param[in,out] siblings Siblings to add to.
3155 *
3156 * @return LY_ERR values.
3157 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003158LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003159parse_choice(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003160{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003161 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003162 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003163 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003164 enum yang_keyword kw;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003165 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003166
David Sedlák60adc092019-08-06 15:57:02 +02003167 /* create new choice structure */
3168 LY_LIST_NEW_RET(ctx->ctx, siblings, choice, next);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003169 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003170 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003171
Michal Vasko7fbc8162018-09-17 10:35:16 +02003172 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003173 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003174 INSERT_WORD(ctx, buf, choice->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003175
3176 /* parse substatements */
Radek Krejcia9026eb2018-12-12 16:04:47 +01003177 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003178 switch (kw) {
3179 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003180 LY_CHECK_RET(parse_config(ctx, data, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003181 break;
3182 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003183 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 +02003184 break;
3185 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003186 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 +02003187 break;
3188 case YANG_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003189 LY_CHECK_RET(parse_mandatory(ctx, data, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003190 break;
3191 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003192 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 +02003193 break;
3194 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003195 LY_CHECK_RET(parse_status(ctx, data, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003196 break;
3197 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003198 LY_CHECK_RET(parse_when(ctx, data, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003199 break;
3200 case YANG_DEFAULT:
Radek Krejcia9026eb2018-12-12 16:04:47 +01003201 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 +02003202 break;
3203
3204 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01003205 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "choice");
Radek Krejci10113652018-11-14 16:56:50 +01003206 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003207 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003208 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003209 break;
3210 case YANG_CASE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003211 LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003212 break;
3213 case YANG_CHOICE:
Radek Krejciceaf2122019-01-02 15:03:26 +01003214 YANG_CHECK_STMTVER2_RET(ctx, "choice", "choice");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003215 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003216 break;
3217 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003218 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003219 break;
3220 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003221 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003222 break;
3223 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003224 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003225 break;
3226 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003227 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003228 break;
3229 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003230 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003231 break;
3232 default:
David Sedlákb3077192019-06-19 10:55:37 +02003233 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003234 return LY_EVALID;
3235 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003236 }
Radek Krejcia9026eb2018-12-12 16:04:47 +01003237 LY_CHECK_RET(ret);
3238checks:
3239 if ((choice->flags & LYS_MAND_TRUE) && choice->dflt) {
David Sedlákb3077192019-06-19 10:55:37 +02003240 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMSCOMB, "mandatory", "default", "choice");
Radek Krejcia9026eb2018-12-12 16:04:47 +01003241 return LY_EVALID;
3242 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003243 return ret;
3244}
3245
Michal Vaskoea5abea2018-09-18 13:10:54 +02003246/**
3247 * @brief Parse the container statement.
3248 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003249 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003250 * @param[in,out] data Data to read from, always moved to currently handled character.
3251 * @param[in,out] siblings Siblings to add to.
3252 *
3253 * @return LY_ERR values.
3254 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003255LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003256parse_container(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003257{
3258 LY_ERR ret = 0;
3259 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003260 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003261 enum yang_keyword kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003262 struct lysp_node_container *cont;
3263
David Sedlák60adc092019-08-06 15:57:02 +02003264 /* create new container structure */
3265 LY_LIST_NEW_RET(ctx->ctx, siblings, cont, next);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003266 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003267 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003268
Michal Vasko7fbc8162018-09-17 10:35:16 +02003269 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003270 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003271 INSERT_WORD(ctx, buf, cont->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003272
3273 /* parse substatements */
Radek Krejci7fc68292019-06-12 13:51:09 +02003274 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003275 switch (kw) {
3276 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003277 LY_CHECK_RET(parse_config(ctx, data, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003278 break;
3279 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003280 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 +02003281 break;
3282 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003283 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 +02003284 break;
3285 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003286 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 +02003287 break;
3288 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003289 LY_CHECK_RET(parse_status(ctx, data, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003290 break;
3291 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003292 LY_CHECK_RET(parse_when(ctx, data, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003293 break;
3294 case YANG_PRESENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003295 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 +02003296 break;
3297
3298 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01003299 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "container");
Radek Krejci10113652018-11-14 16:56:50 +01003300 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003301 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003302 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003303 break;
3304 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003305 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003306 break;
3307 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003308 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003309 break;
3310 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003311 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003312 break;
3313 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003314 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003315 break;
3316 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003317 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003318 break;
3319 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003320 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003321 break;
3322
3323 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01003324 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)cont, data, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003325 break;
3326 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003327 LY_CHECK_RET(parse_restrs(ctx, data, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003328 break;
3329 case YANG_ACTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003330 YANG_CHECK_STMTVER2_RET(ctx, "action", "container");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003331 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003332 break;
3333 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003334 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003335 break;
3336 case YANG_NOTIFICATION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003337 YANG_CHECK_STMTVER2_RET(ctx, "notification", "container");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003338 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003339 break;
3340 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003341 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003342 break;
3343 default:
David Sedlákb3077192019-06-19 10:55:37 +02003344 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003345 return LY_EVALID;
3346 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003347 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003348checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01003349 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02003350 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, cont->groupings, NULL, cont->actions, cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003351 return ret;
3352}
3353
Michal Vaskoea5abea2018-09-18 13:10:54 +02003354/**
3355 * @brief Parse the list statement.
3356 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003357 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003358 * @param[in,out] data Data to read from, always moved to currently handled character.
3359 * @param[in,out] siblings Siblings to add to.
3360 *
3361 * @return LY_ERR values.
3362 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003363LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003364parse_list(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003365{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003366 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003367 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003368 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003369 enum yang_keyword kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003370 struct lysp_node_list *list;
3371
David Sedlák60adc092019-08-06 15:57:02 +02003372 /* create new list structure */
3373 LY_LIST_NEW_RET(ctx->ctx, siblings, list, next);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003374 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003375 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003376
Michal Vasko7fbc8162018-09-17 10:35:16 +02003377 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003378 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003379 INSERT_WORD(ctx, buf, list->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003380
3381 /* parse substatements */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003382 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003383 switch (kw) {
3384 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003385 LY_CHECK_RET(parse_config(ctx, data, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003386 break;
3387 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003388 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 +02003389 break;
3390 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003391 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 +02003392 break;
3393 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003394 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 +02003395 break;
3396 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003397 LY_CHECK_RET(parse_status(ctx, data, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003398 break;
3399 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003400 LY_CHECK_RET(parse_when(ctx, data, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003401 break;
3402 case YANG_KEY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003403 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 +02003404 break;
3405 case YANG_MAX_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003406 LY_CHECK_RET(parse_maxelements(ctx, data, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003407 break;
3408 case YANG_MIN_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003409 LY_CHECK_RET(parse_minelements(ctx, data, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003410 break;
3411 case YANG_ORDERED_BY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003412 LY_CHECK_RET(parse_orderedby(ctx, data, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003413 break;
3414 case YANG_UNIQUE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003415 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 +02003416 break;
3417
3418 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01003419 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "list");
Radek Krejci10113652018-11-14 16:56:50 +01003420 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003421 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003422 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003423 break;
3424 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003425 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003426 break;
3427 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003428 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003429 break;
3430 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003431 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003432 break;
3433 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003434 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003435 break;
3436 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003437 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003438 break;
3439 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003440 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003441 break;
3442
3443 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01003444 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)list, data, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003445 break;
3446 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003447 LY_CHECK_RET(parse_restrs(ctx, data, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003448 break;
3449 case YANG_ACTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003450 YANG_CHECK_STMTVER2_RET(ctx, "action", "list");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003451 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003452 break;
3453 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003454 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003455 break;
3456 case YANG_NOTIFICATION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003457 YANG_CHECK_STMTVER2_RET(ctx, "notification", "list");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003458 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003459 break;
3460 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003461 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003462 break;
3463 default:
David Sedlákb3077192019-06-19 10:55:37 +02003464 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003465 return LY_EVALID;
3466 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003467 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003468 LY_CHECK_RET(ret);
3469checks:
Radek Krejci7fc68292019-06-12 13:51:09 +02003470 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02003471 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, list->groupings, NULL, list->actions, list->notifs));
Radek Krejci7fc68292019-06-12 13:51:09 +02003472
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003473 if (list->max && list->min > list->max) {
David Sedlákb3077192019-06-19 10:55:37 +02003474 LOGVAL_PARSER(ctx, LYVE_SEMANTICS,
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003475 "Invalid combination of min-elements and max-elements: min value %u is bigger than the max value %u.",
3476 list->min, list->max);
3477 return LY_EVALID;
3478 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003479
3480 return ret;
3481}
3482
Michal Vaskoea5abea2018-09-18 13:10:54 +02003483/**
3484 * @brief Parse the yin-element statement.
3485 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003486 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003487 * @param[in,out] data Data to read from, always moved to currently handled character.
3488 * @param[in,out] flags Flags to write to.
3489 * @param[in,out] exts Extension instances to add to.
3490 *
3491 * @return LY_ERR values.
3492 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003493static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003494parse_yinelement(struct lys_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003495{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003496 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003497 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003498 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003499 enum yang_keyword kw;
3500
3501 if (*flags & LYS_YINELEM_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02003502 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003503 return LY_EVALID;
3504 }
3505
3506 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003507 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003508
3509 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
3510 *flags |= LYS_YINELEM_TRUE;
3511 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
3512 *flags |= LYS_YINELEM_FALSE;
3513 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003514 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003515 free(buf);
3516 return LY_EVALID;
3517 }
3518 free(buf);
3519
Radek Krejci6d6556c2018-11-08 09:37:45 +01003520 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003521 switch (kw) {
3522 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003523 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_YINELEM, 0, exts));
3524 LY_CHECK_RET(ret); break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003525 default:
David Sedlákb3077192019-06-19 10:55:37 +02003526 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003527 return LY_EVALID;
3528 }
3529 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003530 return ret;
3531}
3532
Michal Vaskoea5abea2018-09-18 13:10:54 +02003533/**
David Sedlák2444f8f2019-07-09 11:02:47 +02003534 * @brief Parse the argument statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003535 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003536 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003537 * @param[in,out] data Data to read from, always moved to currently handled character.
3538 * @param[in,out] argument Value to write to.
3539 * @param[in,out] flags Flags to write to.
3540 * @param[in,out] exts Extension instances to add to.
3541 *
3542 * @return LY_ERR values.
3543 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003544static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003545parse_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 +02003546{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003547 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003548 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003549 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003550 enum yang_keyword kw;
3551
3552 if (*argument) {
David Sedlákb3077192019-06-19 10:55:37 +02003553 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003554 return LY_EVALID;
3555 }
3556
3557 /* get value */
David Sedlák2444f8f2019-07-09 11:02:47 +02003558 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003559 INSERT_WORD(ctx, buf, *argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003560
Radek Krejci6d6556c2018-11-08 09:37:45 +01003561 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003562 switch (kw) {
3563 case YANG_YIN_ELEMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003564 LY_CHECK_RET(parse_yinelement(ctx, data, flags, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003565 break;
3566 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003567 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_ARGUMENT, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003568 break;
3569 default:
David Sedlákb3077192019-06-19 10:55:37 +02003570 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003571 return LY_EVALID;
3572 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003573 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003574 return ret;
3575}
3576
Michal Vaskoea5abea2018-09-18 13:10:54 +02003577/**
3578 * @brief Parse the extension statement.
3579 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003580 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003581 * @param[in,out] data Data to read from, always moved to currently handled character.
3582 * @param[in,out] extensions Extensions to add to.
3583 *
3584 * @return LY_ERR values.
3585 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003586static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003587parse_extension(struct lys_parser_ctx *ctx, const char **data, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003588{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003589 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003590 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003591 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003592 enum yang_keyword kw;
3593 struct lysp_ext *ex;
3594
Radek Krejci2c4e7172018-10-19 15:56:26 +02003595 LY_ARRAY_NEW_RET(ctx->ctx, *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003596
3597 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003598 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003599 INSERT_WORD(ctx, buf, ex->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003600
Radek Krejci6d6556c2018-11-08 09:37:45 +01003601 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003602 switch (kw) {
3603 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003604 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 +02003605 break;
3606 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003607 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 +02003608 break;
3609 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003610 LY_CHECK_RET(parse_status(ctx, data, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003611 break;
3612 case YANG_ARGUMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003613 LY_CHECK_RET(parse_argument(ctx, data, &ex->argument, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003614 break;
3615 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003616 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003617 break;
3618 default:
David Sedlákb3077192019-06-19 10:55:37 +02003619 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003620 return LY_EVALID;
3621 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003622 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003623 return ret;
3624}
3625
Michal Vaskoea5abea2018-09-18 13:10:54 +02003626/**
3627 * @brief Parse the deviate statement.
3628 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003629 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003630 * @param[in,out] data Data to read from, always moved to currently handled character.
3631 * @param[in,out] deviates Deviates to add to.
3632 *
3633 * @return LY_ERR values.
3634 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003635LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003636parse_deviate(struct lys_parser_ctx *ctx, const char **data, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003637{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003638 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003639 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003640 size_t word_len, dev_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003641 enum yang_keyword kw;
David Sedlák60adc092019-08-06 15:57:02 +02003642 struct lysp_deviate *d;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003643 struct lysp_deviate_add *d_add = NULL;
3644 struct lysp_deviate_rpl *d_rpl = NULL;
3645 struct lysp_deviate_del *d_del = NULL;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02003646 const char **d_units = NULL, ***d_uniques = NULL, ***d_dflts = NULL;
3647 struct lysp_restr **d_musts = NULL;
3648 uint16_t *d_flags = 0;
3649 uint32_t *d_min = 0, *d_max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003650
3651 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003652 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003653
3654 if ((word_len == 13) && !strncmp(word, "not-supported", word_len)) {
3655 dev_mod = LYS_DEV_NOT_SUPPORTED;
3656 } else if ((word_len == 3) && !strncmp(word, "add", word_len)) {
3657 dev_mod = LYS_DEV_ADD;
3658 } else if ((word_len == 7) && !strncmp(word, "replace", word_len)) {
3659 dev_mod = LYS_DEV_REPLACE;
3660 } else if ((word_len == 6) && !strncmp(word, "delete", word_len)) {
3661 dev_mod = LYS_DEV_DELETE;
3662 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003663 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003664 free(buf);
3665 return LY_EVALID;
3666 }
3667 free(buf);
3668
3669 /* create structure */
3670 switch (dev_mod) {
3671 case LYS_DEV_NOT_SUPPORTED:
3672 d = calloc(1, sizeof *d);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003673 LY_CHECK_ERR_RET(!d, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003674 break;
3675 case LYS_DEV_ADD:
3676 d_add = calloc(1, sizeof *d_add);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003677 LY_CHECK_ERR_RET(!d_add, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003678 d = (struct lysp_deviate *)d_add;
3679 d_units = &d_add->units;
3680 d_uniques = &d_add->uniques;
3681 d_dflts = &d_add->dflts;
3682 d_musts = &d_add->musts;
3683 d_flags = &d_add->flags;
3684 d_min = &d_add->min;
3685 d_max = &d_add->max;
3686 break;
3687 case LYS_DEV_REPLACE:
3688 d_rpl = calloc(1, sizeof *d_rpl);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003689 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003690 d = (struct lysp_deviate *)d_rpl;
3691 d_units = &d_rpl->units;
3692 d_flags = &d_rpl->flags;
3693 d_min = &d_rpl->min;
3694 d_max = &d_rpl->max;
3695 break;
3696 case LYS_DEV_DELETE:
3697 d_del = calloc(1, sizeof *d_del);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003698 LY_CHECK_ERR_RET(!d_del, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003699 d = (struct lysp_deviate *)d_del;
3700 d_units = &d_del->units;
3701 d_uniques = &d_del->uniques;
3702 d_dflts = &d_del->dflts;
3703 d_musts = &d_del->musts;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003704 break;
3705 default:
3706 assert(0);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003707 LOGINT_RET(ctx->ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003708 }
3709 d->mod = dev_mod;
3710
3711 /* insert into siblings */
David Sedlák60adc092019-08-06 15:57:02 +02003712 LY_LIST_INSERT(deviates, d, next);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003713
Radek Krejci6d6556c2018-11-08 09:37:45 +01003714 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003715 switch (kw) {
3716 case YANG_CONFIG:
3717 switch (dev_mod) {
3718 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003719 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003720 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003721 return LY_EVALID;
3722 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003723 LY_CHECK_RET(parse_config(ctx, data, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003724 break;
3725 }
3726 break;
3727 case YANG_DEFAULT:
3728 switch (dev_mod) {
3729 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003730 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003731 return LY_EVALID;
3732 case LYS_DEV_REPLACE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003733 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 +02003734 break;
3735 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003736 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 +02003737 break;
3738 }
3739 break;
3740 case YANG_MANDATORY:
3741 switch (dev_mod) {
3742 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003743 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003744 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003745 return LY_EVALID;
3746 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003747 LY_CHECK_RET(parse_mandatory(ctx, data, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003748 break;
3749 }
3750 break;
3751 case YANG_MAX_ELEMENTS:
3752 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_maxelements(ctx, data, d_max, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003759 break;
3760 }
3761 break;
3762 case YANG_MIN_ELEMENTS:
3763 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_minelements(ctx, data, d_min, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003770 break;
3771 }
3772 break;
3773 case YANG_MUST:
3774 switch (dev_mod) {
3775 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003776 case LYS_DEV_REPLACE:
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_restrs(ctx, data, kw, d_musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003781 break;
3782 }
3783 break;
3784 case YANG_TYPE:
3785 switch (dev_mod) {
3786 case LYS_DEV_NOT_SUPPORTED:
3787 case LYS_DEV_ADD:
3788 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003789 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003790 return LY_EVALID;
3791 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003792 if (d_rpl->type) {
David Sedlákb3077192019-06-19 10:55:37 +02003793 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003794 return LY_EVALID;
3795 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003796 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003797 LY_CHECK_ERR_RET(!d_rpl->type, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003798 LY_CHECK_RET(parse_type(ctx, data, d_rpl->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003799 break;
3800 }
3801 break;
3802 case YANG_UNIQUE:
3803 switch (dev_mod) {
3804 case LYS_DEV_NOT_SUPPORTED:
3805 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003806 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003807 return LY_EVALID;
3808 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003809 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 +02003810 break;
3811 }
3812 break;
3813 case YANG_UNITS:
3814 switch (dev_mod) {
3815 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003816 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003817 return LY_EVALID;
3818 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003819 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 +02003820 break;
3821 }
3822 break;
3823 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003824 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003825 break;
3826 default:
David Sedlákb3077192019-06-19 10:55:37 +02003827 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003828 return LY_EVALID;
3829 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003830 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003831 return ret;
3832}
3833
Michal Vaskoea5abea2018-09-18 13:10:54 +02003834/**
3835 * @brief Parse the deviation statement.
3836 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003837 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003838 * @param[in,out] data Data to read from, always moved to currently handled character.
3839 * @param[in,out] deviations Deviations to add to.
3840 *
3841 * @return LY_ERR values.
3842 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003843LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003844parse_deviation(struct lys_parser_ctx *ctx, const char **data, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003845{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003846 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003847 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003848 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003849 enum yang_keyword kw;
3850 struct lysp_deviation *dev;
3851
Radek Krejci2c4e7172018-10-19 15:56:26 +02003852 LY_ARRAY_NEW_RET(ctx->ctx, *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003853
3854 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003855 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
David Sedlákb9b892c2019-07-12 14:44:02 +02003856 YANG_CHECK_NONEMPTY(ctx, word_len, "deviation");
Radek Krejci44ceedc2018-10-02 15:54:31 +02003857 INSERT_WORD(ctx, buf, dev->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003858
Radek Krejci6d6556c2018-11-08 09:37:45 +01003859 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003860 switch (kw) {
3861 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003862 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 +02003863 break;
3864 case YANG_DEVIATE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003865 LY_CHECK_RET(parse_deviate(ctx, data, &dev->deviates));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003866 break;
3867 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003868 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 +02003869 break;
3870 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003871 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003872 break;
3873 default:
David Sedlákb3077192019-06-19 10:55:37 +02003874 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003875 return LY_EVALID;
3876 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003877 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003878 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01003879checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003880 /* mandatory substatements */
3881 if (!dev->deviates) {
David Sedlákb3077192019-06-19 10:55:37 +02003882 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003883 return LY_EVALID;
3884 }
3885
3886 return ret;
3887}
3888
Michal Vaskoea5abea2018-09-18 13:10:54 +02003889/**
3890 * @brief Parse the feature statement.
3891 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003892 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003893 * @param[in,out] data Data to read from, always moved to currently handled character.
3894 * @param[in,out] features Features to add to.
3895 *
3896 * @return LY_ERR values.
3897 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003898LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003899parse_feature(struct lys_parser_ctx *ctx, const char **data, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003900{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003901 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003902 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003903 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003904 enum yang_keyword kw;
3905 struct lysp_feature *feat;
3906
Radek Krejci2c4e7172018-10-19 15:56:26 +02003907 LY_ARRAY_NEW_RET(ctx->ctx, *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003908
3909 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003910 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003911 INSERT_WORD(ctx, buf, feat->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01003912
Radek Krejci6d6556c2018-11-08 09:37:45 +01003913 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003914 switch (kw) {
3915 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003916 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 +02003917 break;
3918 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003919 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 +02003920 break;
3921 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003922 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 +02003923 break;
3924 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003925 LY_CHECK_RET(parse_status(ctx, data, &feat->flags, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003926 break;
3927 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003928 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003929 break;
3930 default:
David Sedlákb3077192019-06-19 10:55:37 +02003931 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003932 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003933 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003934 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003935 return ret;
3936}
3937
Michal Vaskoea5abea2018-09-18 13:10:54 +02003938/**
3939 * @brief Parse the identity statement.
3940 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003941 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003942 * @param[in,out] data Data to read from, always moved to currently handled character.
3943 * @param[in,out] identities Identities to add to.
3944 *
3945 * @return LY_ERR values.
3946 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003947LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003948parse_identity(struct lys_parser_ctx *ctx, const char **data, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003949{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003950 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003951 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003952 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003953 enum yang_keyword kw;
3954 struct lysp_ident *ident;
3955
Radek Krejci2c4e7172018-10-19 15:56:26 +02003956 LY_ARRAY_NEW_RET(ctx->ctx, *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003957
3958 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003959 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003960 INSERT_WORD(ctx, buf, ident->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01003961
Radek Krejci6d6556c2018-11-08 09:37:45 +01003962 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003963 switch (kw) {
3964 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003965 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 +02003966 break;
3967 case YANG_IF_FEATURE:
Radek Krejciceaf2122019-01-02 15:03:26 +01003968 YANG_CHECK_STMTVER2_RET(ctx, "if-feature", "identity");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003969 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 +02003970 break;
3971 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003972 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 +02003973 break;
3974 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003975 LY_CHECK_RET(parse_status(ctx, data, &ident->flags, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003976 break;
3977 case YANG_BASE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003978 if (ident->bases && ctx->mod_version < 2) {
David Sedlákb3077192019-06-19 10:55:37 +02003979 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 +01003980 return LY_EVALID;
3981 }
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003982 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 +02003983 break;
3984 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003985 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003986 break;
3987 default:
David Sedlákb3077192019-06-19 10:55:37 +02003988 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003989 return LY_EVALID;
3990 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003991 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003992 return ret;
3993}
3994
Michal Vaskoea5abea2018-09-18 13:10:54 +02003995/**
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003996 * @brief Parse module substatements.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003997 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003998 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003999 * @param[in,out] data Data to read from, always moved to currently handled character.
4000 * @param[in,out] mod Module to write to.
4001 *
4002 * @return LY_ERR values.
4003 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004004LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02004005parse_module(struct lys_parser_ctx *ctx, const char **data, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004006{
4007 LY_ERR ret = 0;
4008 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004009 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004010 enum yang_keyword kw, prev_kw = 0;
4011 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004012 struct lysp_submodule *dup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004013
4014 /* (sub)module name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02004015 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004016 INSERT_WORD(ctx, buf, mod->mod->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004017
Radek Krejci6d6556c2018-11-08 09:37:45 +01004018 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004019
Radek Krejcie3846472018-10-15 15:24:51 +02004020#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004021 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 +02004022
Michal Vasko7fbc8162018-09-17 10:35:16 +02004023 switch (kw) {
4024 /* module header */
4025 case YANG_NAMESPACE:
4026 case YANG_PREFIX:
Radek Krejcie3846472018-10-15 15:24:51 +02004027 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4028 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004029 case YANG_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004030 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004031 break;
4032 /* linkage */
4033 case YANG_INCLUDE:
4034 case YANG_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004035 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004036 break;
4037 /* meta */
4038 case YANG_ORGANIZATION:
4039 case YANG_CONTACT:
4040 case YANG_DESCRIPTION:
4041 case YANG_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004042 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004043 break;
4044
4045 /* revision */
4046 case YANG_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004047 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004048 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004049 /* body */
4050 case YANG_ANYDATA:
4051 case YANG_ANYXML:
4052 case YANG_AUGMENT:
4053 case YANG_CHOICE:
4054 case YANG_CONTAINER:
4055 case YANG_DEVIATION:
4056 case YANG_EXTENSION:
4057 case YANG_FEATURE:
4058 case YANG_GROUPING:
4059 case YANG_IDENTITY:
4060 case YANG_LEAF:
4061 case YANG_LEAF_LIST:
4062 case YANG_LIST:
4063 case YANG_NOTIFICATION:
4064 case YANG_RPC:
4065 case YANG_TYPEDEF:
4066 case YANG_USES:
4067 case YANG_CUSTOM:
4068 mod_stmt = Y_MOD_BODY;
4069 break;
4070 default:
4071 /* error handled in the next switch */
4072 break;
4073 }
Radek Krejcie3846472018-10-15 15:24:51 +02004074#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004075
Radek Krejcie3846472018-10-15 15:24:51 +02004076 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004077 switch (kw) {
4078 /* module header */
4079 case YANG_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004080 LY_CHECK_RET(parse_yangversion(ctx, data, &mod->mod->version, &mod->exts));
4081 ctx->mod_version = mod->mod->version;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004082 break;
4083 case YANG_NAMESPACE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004084 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 +02004085 break;
4086 case YANG_PREFIX:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004087 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 +02004088 break;
4089
4090 /* linkage */
4091 case YANG_INCLUDE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004092 LY_CHECK_RET(parse_include(ctx, mod->mod->name, data, &mod->includes));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004093 break;
4094 case YANG_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004095 LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, data, &mod->imports));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004096 break;
4097
4098 /* meta */
4099 case YANG_ORGANIZATION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004100 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 +02004101 break;
4102 case YANG_CONTACT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004103 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 +02004104 break;
4105 case YANG_DESCRIPTION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004106 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 +02004107 break;
4108 case YANG_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004109 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 +02004110 break;
4111
4112 /* revision */
4113 case YANG_REVISION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004114 LY_CHECK_RET(parse_revision(ctx, data, &mod->revs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004115 break;
4116
4117 /* body */
4118 case YANG_ANYDATA:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004119 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "module");
Radek Krejci10113652018-11-14 16:56:50 +01004120 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004121 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004122 LY_CHECK_RET(parse_any(ctx, data, kw, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004123 break;
4124 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004125 LY_CHECK_RET(parse_choice(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004126 break;
4127 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004128 LY_CHECK_RET(parse_container(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004129 break;
4130 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004131 LY_CHECK_RET(parse_leaf(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004132 break;
4133 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004134 LY_CHECK_RET(parse_leaflist(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004135 break;
4136 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004137 LY_CHECK_RET(parse_list(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004138 break;
4139 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004140 LY_CHECK_RET(parse_uses(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004141 break;
4142
4143 case YANG_AUGMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004144 LY_CHECK_RET(parse_augment(ctx, data, NULL, &mod->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004145 break;
4146 case YANG_DEVIATION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004147 LY_CHECK_RET(parse_deviation(ctx, data, &mod->deviations));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004148 break;
4149 case YANG_EXTENSION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004150 LY_CHECK_RET(parse_extension(ctx, data, &mod->extensions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004151 break;
4152 case YANG_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004153 LY_CHECK_RET(parse_feature(ctx, data, &mod->features));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004154 break;
4155 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004156 LY_CHECK_RET(parse_grouping(ctx, data, NULL, &mod->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004157 break;
4158 case YANG_IDENTITY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004159 LY_CHECK_RET(parse_identity(ctx, data, &mod->identities));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004160 break;
4161 case YANG_NOTIFICATION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004162 LY_CHECK_RET(parse_notif(ctx, data, NULL, &mod->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004163 break;
4164 case YANG_RPC:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004165 LY_CHECK_RET(parse_action(ctx, data, NULL, &mod->rpcs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004166 break;
4167 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01004168 LY_CHECK_RET(parse_typedef(ctx, NULL, data, &mod->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004169 break;
4170 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004171 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004172 break;
4173
4174 default:
David Sedlákb3077192019-06-19 10:55:37 +02004175 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004176 return LY_EVALID;
4177 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004178 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004179 LY_CHECK_RET(ret);
Radek Krejcie86bf772018-12-14 11:39:53 +01004180
Radek Krejci6d6556c2018-11-08 09:37:45 +01004181checks:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004182 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02004183 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, mod->groupings, mod->augments, mod->rpcs, mod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004184
Michal Vasko7fbc8162018-09-17 10:35:16 +02004185 /* mandatory substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004186 if (!mod->mod->ns) {
David Sedlákb3077192019-06-19 10:55:37 +02004187 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004188 return LY_EVALID;
4189 } else if (!mod->mod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004190 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004191 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004192 }
4193
Radek Krejcie9e987e2018-10-31 12:50:27 +01004194 /* submodules share the namespace with the module names, so there must not be
4195 * a submodule of the same name in the context, no need for revision matching */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004196 dup = ly_ctx_get_submodule(ctx->ctx, NULL, mod->mod->name, NULL);
4197 if (dup) {
David Sedlákb3077192019-06-19 10:55:37 +02004198 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 +01004199 return LY_EVALID;
4200 }
4201
4202 return ret;
4203}
4204
4205/**
4206 * @brief Parse submodule substatements.
4207 *
4208 * @param[in] ctx yang parser context for logging.
4209 * @param[in,out] data Data to read from, always moved to currently handled character.
4210 * @param[out] submod Parsed submodule structure.
4211 *
4212 * @return LY_ERR values.
4213 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004214LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02004215parse_submodule(struct lys_parser_ctx *ctx, const char **data, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004216{
4217 LY_ERR ret = 0;
4218 char *buf, *word;
4219 size_t word_len;
4220 enum yang_keyword kw, prev_kw = 0;
4221 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
4222 struct lysp_submodule *dup;
4223
4224 /* submodule name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02004225 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004226 INSERT_WORD(ctx, buf, submod->name, word, word_len);
4227
4228 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
4229
4230#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004231 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 +01004232
4233 switch (kw) {
4234 /* module header */
4235 case YANG_BELONGS_TO:
4236 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4237 break;
4238 case YANG_YANG_VERSION:
4239 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4240 break;
4241 /* linkage */
4242 case YANG_INCLUDE:
4243 case YANG_IMPORT:
4244 CHECK_ORDER(Y_MOD_LINKAGE);
4245 break;
4246 /* meta */
4247 case YANG_ORGANIZATION:
4248 case YANG_CONTACT:
4249 case YANG_DESCRIPTION:
4250 case YANG_REFERENCE:
4251 CHECK_ORDER(Y_MOD_META);
4252 break;
4253
4254 /* revision */
4255 case YANG_REVISION:
4256 CHECK_ORDER(Y_MOD_REVISION);
4257 break;
4258 /* body */
4259 case YANG_ANYDATA:
4260 case YANG_ANYXML:
4261 case YANG_AUGMENT:
4262 case YANG_CHOICE:
4263 case YANG_CONTAINER:
4264 case YANG_DEVIATION:
4265 case YANG_EXTENSION:
4266 case YANG_FEATURE:
4267 case YANG_GROUPING:
4268 case YANG_IDENTITY:
4269 case YANG_LEAF:
4270 case YANG_LEAF_LIST:
4271 case YANG_LIST:
4272 case YANG_NOTIFICATION:
4273 case YANG_RPC:
4274 case YANG_TYPEDEF:
4275 case YANG_USES:
4276 case YANG_CUSTOM:
4277 mod_stmt = Y_MOD_BODY;
4278 break;
4279 default:
4280 /* error handled in the next switch */
4281 break;
4282 }
4283#undef CHECK_ORDER
4284
4285 prev_kw = kw;
4286 switch (kw) {
4287 /* module header */
4288 case YANG_YANG_VERSION:
4289 LY_CHECK_RET(parse_yangversion(ctx, data, &submod->version, &submod->exts));
4290 ctx->mod_version = submod->version;
4291 break;
4292 case YANG_BELONGS_TO:
4293 LY_CHECK_RET(parse_belongsto(ctx, data, &submod->belongsto, &submod->prefix, &submod->exts));
4294 break;
4295
4296 /* linkage */
4297 case YANG_INCLUDE:
4298 LY_CHECK_RET(parse_include(ctx, submod->name, data, &submod->includes));
4299 break;
4300 case YANG_IMPORT:
4301 LY_CHECK_RET(parse_import(ctx, submod->prefix, data, &submod->imports));
4302 break;
4303
4304 /* meta */
4305 case YANG_ORGANIZATION:
4306 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_ORGANIZATION, 0, &submod->org, Y_STR_ARG, &submod->exts));
4307 break;
4308 case YANG_CONTACT:
4309 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_CONTACT, 0, &submod->contact, Y_STR_ARG, &submod->exts));
4310 break;
4311 case YANG_DESCRIPTION:
4312 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &submod->dsc, Y_STR_ARG, &submod->exts));
4313 break;
4314 case YANG_REFERENCE:
4315 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &submod->ref, Y_STR_ARG, &submod->exts));
4316 break;
4317
4318 /* revision */
4319 case YANG_REVISION:
4320 LY_CHECK_RET(parse_revision(ctx, data, &submod->revs));
4321 break;
4322
4323 /* body */
4324 case YANG_ANYDATA:
4325 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "submodule");
4326 /* fall through */
4327 case YANG_ANYXML:
4328 LY_CHECK_RET(parse_any(ctx, data, kw, NULL, &submod->data));
4329 break;
4330 case YANG_CHOICE:
4331 LY_CHECK_RET(parse_choice(ctx, data, NULL, &submod->data));
4332 break;
4333 case YANG_CONTAINER:
4334 LY_CHECK_RET(parse_container(ctx, data, NULL, &submod->data));
4335 break;
4336 case YANG_LEAF:
4337 LY_CHECK_RET(parse_leaf(ctx, data, NULL, &submod->data));
4338 break;
4339 case YANG_LEAF_LIST:
4340 LY_CHECK_RET(parse_leaflist(ctx, data, NULL, &submod->data));
4341 break;
4342 case YANG_LIST:
4343 LY_CHECK_RET(parse_list(ctx, data, NULL, &submod->data));
4344 break;
4345 case YANG_USES:
4346 LY_CHECK_RET(parse_uses(ctx, data, NULL, &submod->data));
4347 break;
4348
4349 case YANG_AUGMENT:
4350 LY_CHECK_RET(parse_augment(ctx, data, NULL, &submod->augments));
4351 break;
4352 case YANG_DEVIATION:
4353 LY_CHECK_RET(parse_deviation(ctx, data, &submod->deviations));
4354 break;
4355 case YANG_EXTENSION:
4356 LY_CHECK_RET(parse_extension(ctx, data, &submod->extensions));
4357 break;
4358 case YANG_FEATURE:
4359 LY_CHECK_RET(parse_feature(ctx, data, &submod->features));
4360 break;
4361 case YANG_GROUPING:
4362 LY_CHECK_RET(parse_grouping(ctx, data, NULL, &submod->groupings));
4363 break;
4364 case YANG_IDENTITY:
4365 LY_CHECK_RET(parse_identity(ctx, data, &submod->identities));
4366 break;
4367 case YANG_NOTIFICATION:
4368 LY_CHECK_RET(parse_notif(ctx, data, NULL, &submod->notifs));
4369 break;
4370 case YANG_RPC:
4371 LY_CHECK_RET(parse_action(ctx, data, NULL, &submod->rpcs));
4372 break;
4373 case YANG_TYPEDEF:
4374 LY_CHECK_RET(parse_typedef(ctx, NULL, data, &submod->typedefs));
4375 break;
4376 case YANG_CUSTOM:
4377 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &submod->exts));
4378 break;
4379
4380 default:
David Sedlákb3077192019-06-19 10:55:37 +02004381 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004382 return LY_EVALID;
4383 }
4384 }
4385 LY_CHECK_RET(ret);
4386
4387checks:
4388 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02004389 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, submod->groupings, submod->augments, submod->rpcs, submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004390
4391 /* mandatory substatements */
4392 if (!submod->belongsto) {
David Sedlákb3077192019-06-19 10:55:37 +02004393 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004394 return LY_EVALID;
4395 }
4396
4397 /* submodules share the namespace with the module names, so there must not be
4398 * a submodule of the same name in the context, no need for revision matching */
4399 dup = ly_ctx_get_submodule(ctx->ctx, NULL, submod->name, NULL);
4400 if (dup && strcmp(dup->belongsto, submod->belongsto)) {
David Sedlákb3077192019-06-19 10:55:37 +02004401 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Name collision between submodules of name \"%s\".", dup->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +01004402 return LY_EVALID;
4403 }
4404
Michal Vasko7fbc8162018-09-17 10:35:16 +02004405 return ret;
4406}
4407
Radek Krejcid4557c62018-09-17 11:42:09 +02004408LY_ERR
David Sedlák1b623122019-08-05 15:27:49 +02004409yang_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 +02004410{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004411 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004412 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004413 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004414 enum yang_keyword kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004415 struct lysp_submodule *mod_p = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004416
David Sedlák1b623122019-08-05 15:27:49 +02004417 /* create context */
4418 *context = calloc(1, sizeof **context);
4419 LY_CHECK_ERR_RET(!(*context), LOGMEM(ly_ctx), LY_EMEM);
4420 (*context)->ctx = ly_ctx;
4421 (*context)->line = 1;
4422
4423 /* map the typedefs and groupings list from main context to the submodule's context */
4424 memcpy(&(*context)->tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
4425 memcpy(&(*context)->grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes);
4426
Michal Vasko7fbc8162018-09-17 10:35:16 +02004427 /* "module"/"submodule" */
David Sedlák1b623122019-08-05 15:27:49 +02004428 ret = get_keyword(*context, &data, &kw, &word, &word_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004429 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004430
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004431 if (kw == YANG_MODULE) {
David Sedlák1b623122019-08-05 15:27:49 +02004432 LOGERR((*context)->ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004433 ret = LY_EINVAL;
4434 goto cleanup;
4435 } else if (kw != YANG_SUBMODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004436 LOGVAL_PARSER(*context, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004437 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004438 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004439 }
4440
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004441 mod_p = calloc(1, sizeof *mod_p);
David Sedlák1b623122019-08-05 15:27:49 +02004442 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM((*context)->ctx), cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004443 mod_p->parsing = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004444
4445 /* substatements */
David Sedlák1b623122019-08-05 15:27:49 +02004446 ret = parse_submodule(*context, &data, mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004447 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004448
4449 /* read some trailing spaces or new lines */
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004450 while(*data && isspace(*data)) {
4451 data++;
4452 }
4453 if (*data) {
David Sedlák1538a842019-08-08 15:38:51 +02004454 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_SUBMOD, 15, data, strlen(data) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004455 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004456 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004457 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004458
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004459 mod_p->parsing = 0;
4460 *submod = mod_p;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004461
Radek Krejcibbe09a92018-11-08 09:36:54 +01004462cleanup:
4463 if (ret) {
David Sedlák1b623122019-08-05 15:27:49 +02004464 lysp_submodule_free((*context)->ctx, mod_p);
4465 lys_parser_ctx_free(*context);
4466 *context = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004467 }
4468
4469 return ret;
4470}
4471
4472LY_ERR
David Sedlák1b623122019-08-05 15:27:49 +02004473yang_parse_module(struct lys_parser_ctx **context, const char *data, struct lys_module *mod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004474{
4475 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004476 char *word;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004477 size_t word_len;
4478 enum yang_keyword kw;
4479 struct lysp_module *mod_p = NULL;
4480
David Sedlák1b623122019-08-05 15:27:49 +02004481 /* create context */
4482 *context = calloc(1, sizeof **context);
4483 LY_CHECK_ERR_RET(!(*context), LOGMEM(mod->ctx), LY_EMEM);
4484 (*context)->ctx = mod->ctx;
4485 (*context)->line = 1;
4486
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004487 /* "module"/"submodule" */
David Sedlák1b623122019-08-05 15:27:49 +02004488 ret = get_keyword(*context, &data, &kw, &word, &word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004489 LY_CHECK_GOTO(ret, cleanup);
4490
4491 if (kw == YANG_SUBMODULE) {
David Sedlák1b623122019-08-05 15:27:49 +02004492 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 +01004493 ret = LY_EINVAL;
4494 goto cleanup;
4495 } else if (kw != YANG_MODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004496 LOGVAL_PARSER((*context), LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004497 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004498 goto cleanup;
4499 }
4500
4501 mod_p = calloc(1, sizeof *mod_p);
David Sedlák1b623122019-08-05 15:27:49 +02004502 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM((*context)->ctx), cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004503 mod_p->mod = mod;
4504 mod_p->parsing = 1;
4505
4506 /* substatements */
David Sedlák1b623122019-08-05 15:27:49 +02004507 ret = parse_module(*context, &data, mod_p);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004508 LY_CHECK_GOTO(ret, cleanup);
4509
4510 /* read some trailing spaces or new lines */
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004511 while(*data && isspace(*data)) {
4512 data++;
4513 }
4514 if (*data) {
David Sedlák1538a842019-08-08 15:38:51 +02004515 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_MOD, 15, data, strlen(data) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004516 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004517 goto cleanup;
4518 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004519
4520 mod_p->parsing = 0;
4521 mod->parsed = mod_p;
4522
4523cleanup:
4524 if (ret) {
4525 lysp_module_free(mod_p);
David Sedlák1b623122019-08-05 15:27:49 +02004526 lys_parser_ctx_free(*context);
4527 *context = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004528 }
4529
Michal Vasko7fbc8162018-09-17 10:35:16 +02004530 return ret;
4531}