blob: 54890516db689a92b0c30be688c492a8f68a8f52 [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/**
35 * @brief Try to find object with MEMBER string matching the IDENT in the given ARRAY.
36 * Macro logs an error message and returns LY_EVALID in case of existence of a matching object.
37 *
38 * @param[in] CTX yang parser context for logging.
39 * @param[in] ARRAY [sized array](@ref sizedarrays) of a generic objects with member named MEMBER to search.
40 * @param[in] MEMBER Name of the member of the objects in the ARRAY to compare.
41 * @param[in] STMT Name of the compared YANG statements for logging.
42 * @param[in] IDENT String trying to find in the ARRAY's objects inside the MEMBER member.
43 */
Radek Krejcifaa1eac2018-10-30 14:34:55 +010044#define CHECK_UNIQUENESS(CTX, ARRAY, MEMBER, STMT, IDENT) \
45 if (ARRAY) { \
46 for (unsigned int u = 0; u < LY_ARRAY_SIZE(ARRAY) - 1; ++u) { \
47 if (!strcmp((ARRAY)[u].MEMBER, IDENT)) { \
David Sedlákb3077192019-06-19 10:55:37 +020048 LOGVAL_PARSER(CTX, LY_VCODE_DUPIDENT, IDENT, STMT); \
Radek Krejcifaa1eac2018-10-30 14:34:55 +010049 return LY_EVALID; \
50 } \
51 } \
52 }
53
Radek Krejciceaf2122019-01-02 15:03:26 +010054/**
55 * @brief Insert WORD into the libyang context's dictionary and store as TARGET.
56 * @param[in] CTX yang parser context to access libyang context.
57 * @param[in] BUF buffer in case the word is not a constant and can be inserted directly (zero-copy)
58 * @param[out] TARGET variable where to store the pointer to the inserted value.
59 * @param[in] WORD string to store.
60 * @param[in] LEN length of the string in WORD to store.
61 */
Radek Krejci9fcacc12018-10-11 15:59:11 +020062#define INSERT_WORD(CTX, BUF, TARGET, WORD, LEN) \
63 if (BUF) {(TARGET) = lydict_insert_zc((CTX)->ctx, WORD);}\
Radek Krejcif09e4e82019-06-14 15:08:11 +020064 else {(TARGET) = lydict_insert((CTX)->ctx, LEN ? WORD : "", LEN);}
Radek Krejci44ceedc2018-10-02 15:54:31 +020065
Radek Krejciceaf2122019-01-02 15:03:26 +010066/**
67 * @brief Move the DATA pointer by COUNT items. Also updates the indent value in yang parser context
68 * @param[in] CTX yang parser context to update its indent value.
69 * @param[in,out] DATA pointer to move
70 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
71 */
Radek Krejci2b610482019-01-02 13:36:09 +010072#define MOVE_INPUT(CTX, DATA, COUNT) (*(DATA))+=COUNT;(CTX)->indent+=COUNT
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020073
Michal Vaskoea5abea2018-09-18 13:10:54 +020074/**
75 * @brief Loop through all substatements providing, return if there are none.
76 *
Radek Krejci44ceedc2018-10-02 15:54:31 +020077 * @param[in] CTX yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +020078 * @param[in] DATA Raw data to read from.
79 * @param[out] KW YANG keyword read.
80 * @param[out] WORD Pointer to the keyword itself.
81 * @param[out] WORD_LEN Length of the keyword.
82 * @param[out] ERR Variable for error storing.
83 *
84 * @return In case there are no substatements or a fatal error encountered.
85 */
Radek Krejci6d6556c2018-11-08 09:37:45 +010086#define YANG_READ_SUBSTMT_FOR(CTX, DATA, KW, WORD, WORD_LEN, ERR, CHECKGOTO) \
Radek Krejci6d9b9b52018-11-02 12:43:39 +010087 LY_CHECK_RET(get_keyword(CTX, DATA, &KW, &WORD, &WORD_LEN)); \
Michal Vasko7fbc8162018-09-17 10:35:16 +020088 if (KW == YANG_SEMICOLON) { \
Radek Krejci6d6556c2018-11-08 09:37:45 +010089 CHECKGOTO; \
Radek Krejci6d9b9b52018-11-02 12:43:39 +010090 return LY_SUCCESS; \
Michal Vasko7fbc8162018-09-17 10:35:16 +020091 } \
92 if (KW != YANG_LEFT_BRACE) { \
David Sedlákb3077192019-06-19 10:55:37 +020093 LOGVAL_PARSER(CTX, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\", expected \";\" or \"{\".", ly_stmt2str(KW)); \
Michal Vasko7fbc8162018-09-17 10:35:16 +020094 return LY_EVALID; \
95 } \
96 for (ERR = get_keyword(CTX, DATA, &KW, &WORD, &WORD_LEN); \
97 !ERR && (KW != YANG_RIGHT_BRACE); \
98 ERR = get_keyword(CTX, DATA, &KW, &WORD, &WORD_LEN))
99
Radek Krejciceaf2122019-01-02 15:03:26 +0100100/**
101 * @brief Check module version is at least 2 (YANG 1.1) because of the keyword presence.
102 * Logs error message and returns LY_EVALID in case of module in YANG version 1.0.
103 * @param[in] CTX yang parser context to get current module and for logging.
104 * @param[in] KW keyword allowed only in YANG version 1.1 (or later) - for logging.
105 * @param[in] PARENT parent statement where the KW is present - for logging.
106 */
107#define YANG_CHECK_STMTVER2_RET(CTX, KW, PARENT) \
David Sedlákb3077192019-06-19 10:55:37 +0200108 if ((CTX)->mod_version < 2) {LOGVAL_PARSER((CTX), LY_VCODE_INCHILDSTMT2, KW, PARENT); return LY_EVALID;}
Radek Krejci10113652018-11-14 16:56:50 +0100109
Radek Krejcif09e4e82019-06-14 15:08:11 +0200110#define YANG_CHECK_NONEMPTY(CTX, OBJECT, VALUE_LEN, STMT) \
111 if (!VALUE_LEN) { \
112 LOGWRN((CTX)->ctx, "Empty argument of %s statement does not make sense.", STMT); \
113 }
114
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200115LY_ERR parse_container(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
116LY_ERR parse_uses(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
117LY_ERR parse_choice(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
118LY_ERR parse_case(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
119LY_ERR parse_list(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
120LY_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 +0200121
Radek Krejci7fc68292019-06-12 13:51:09 +0200122static LY_ERR parse_finalize_reallocated(struct lys_parser_ctx *ctx, struct lysp_grp *groupings, struct lysp_augment *augments,
123 struct lysp_action *actions, struct lysp_notif *notifs);
124
Michal Vaskoea5abea2018-09-18 13:10:54 +0200125/**
126 * @brief Add another character to dynamic buffer, a low-level function.
127 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200128 * Enlarge if needed. Updates \p input as well as \p buf_used.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200129 *
Radek Krejci404251e2018-10-09 12:06:44 +0200130 * @param[in] ctx libyang context for logging.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200131 * @param[in, out] input Input string to process.
132 * @param[in] len Number of bytes to get from the input string and copy into the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200133 * @param[in,out] buf Buffer to use, can be moved by realloc().
134 * @param[in,out] buf_len Current size of the buffer.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200135 * @param[in,out] buf_used Currently used characters of the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200136 *
137 * @return LY_ERR values.
138 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200139LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +0200140buf_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 +0200141{
Radek Krejci44ceedc2018-10-02 15:54:31 +0200142 if (*buf_len <= (*buf_used) + len) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200143 *buf_len += 16;
144 *buf = ly_realloc(*buf, *buf_len);
145 LY_CHECK_ERR_RET(!*buf, LOGMEM(ctx), LY_EMEM);
146 }
Radek Krejcic0917392019-04-10 13:04:04 +0200147 if (*buf_used) {
148 memcpy(&(*buf)[*buf_used], *input, len);
149 } else {
150 memcpy(*buf, *input, len);
151 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200152
Radek Krejci44ceedc2018-10-02 15:54:31 +0200153 (*buf_used) += len;
154 (*input) += len;
155
Michal Vasko7fbc8162018-09-17 10:35:16 +0200156 return LY_SUCCESS;
157}
158
Michal Vaskoea5abea2018-09-18 13:10:54 +0200159/**
Radek Krejci44ceedc2018-10-02 15:54:31 +0200160 * @brief Store a single UTF8 character. It depends whether in a dynamically-allocated buffer or just as a pointer to the data.
161 *
162 * @param[in] ctx yang parser context for logging.
163 * @param[in,out] input Pointer to the string where to get the character to store. Automatically moved to the next character
164 * when function returns.
165 * @param[in] arg Type of the input string to select method of checking character validity.
166 * @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 +0200167 * 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 +0200168 * @param[in,out] word_len Current length of the word pointed to by \p word_p.
169 * @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 +0200170 * @param[in,out] buf_len Current length of \p word_b.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200171 * @param[in] need_buf Flag if the dynamically allocated buffer is required.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200172 *
173 * @return LY_ERR values.
174 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200175LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200176buf_store_char(struct lys_parser_ctx *ctx, const char **input, enum yang_arg arg,
Radek Krejci44ceedc2018-10-02 15:54:31 +0200177 char **word_p, size_t *word_len, char **word_b, size_t *buf_len, int need_buf)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200178{
Radek Krejci404251e2018-10-09 12:06:44 +0200179 int prefix = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200180 unsigned int c;
181 size_t len;
182
Radek Krejcif29b7c32019-04-09 16:17:49 +0200183 /* check valid combination of input paremeters - if need_buf specified, word_b must be provided */
184 assert(!need_buf || (need_buf && word_b));
185
Radek Krejci44ceedc2018-10-02 15:54:31 +0200186 /* get UTF8 code point (and number of bytes coding the character) */
187 LY_CHECK_ERR_RET(ly_getutf8(input, &c, &len),
David Sedlákb3077192019-06-19 10:55:37 +0200188 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, (*input)[-len]), LY_EVALID);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200189 (*input) -= len;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200190 if (c == '\n') {
191 ctx->indent = 0;
192 } else {
193 /* note - even the multibyte character is count as 1 */
194 ++ctx->indent;
195 }
196
Radek Krejci44ceedc2018-10-02 15:54:31 +0200197 /* check character validity */
198 switch (arg) {
199 case Y_IDENTIF_ARG:
David Sedlák4a650532019-07-10 11:55:18 +0200200 LY_CHECK_RET(lysp_check_identifierchar(ctx, c, !(*word_len), NULL));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200201 break;
202 case Y_PREF_IDENTIF_ARG:
David Sedlák4a650532019-07-10 11:55:18 +0200203 LY_CHECK_RET(lysp_check_identifierchar(ctx, c, !(*word_len), &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200204 break;
205 case Y_STR_ARG:
206 case Y_MAYBE_STR_ARG:
David Sedlák4a650532019-07-10 11:55:18 +0200207 LY_CHECK_RET(lysp_check_stringchar(ctx, c));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200208 break;
209 }
210
Michal Vasko7fbc8162018-09-17 10:35:16 +0200211 if (word_b && *word_b) {
212 /* add another character into buffer */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200213 if (buf_add_char(ctx->ctx, input, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200214 return LY_EMEM;
215 }
216
217 /* in case of realloc */
218 *word_p = *word_b;
Radek Krejcif29b7c32019-04-09 16:17:49 +0200219 } else if (word_b && need_buf) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200220 /* first time we need a buffer, copy everything read up to now */
221 if (*word_len) {
222 *word_b = malloc(*word_len);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200223 LY_CHECK_ERR_RET(!*word_b, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200224 *buf_len = *word_len;
225 memcpy(*word_b, *word_p, *word_len);
226 }
227
228 /* add this new character into buffer */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200229 if (buf_add_char(ctx->ctx, input, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200230 return LY_EMEM;
231 }
232
233 /* in case of realloc */
234 *word_p = *word_b;
235 } else {
236 /* just remember the first character pointer */
237 if (!*word_p) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200238 *word_p = (char *)(*input);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200239 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200240 /* ... and update the word's length */
241 (*word_len) += len;
242 (*input) += len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200243 }
244
245 return LY_SUCCESS;
246}
247
Michal Vaskoea5abea2018-09-18 13:10:54 +0200248/**
249 * @brief Skip YANG comment in data.
250 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200251 * @param[in] ctx yang parser context for logging.
252 * @param[in,out] data Data to read from, automatically moved after the comment.
253 * @param[in] comment Type of the comment to process:
254 * 1 for a one-line comment,
255 * 2 for a block comment.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200256 *
257 * @return LY_ERR values.
258 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200259LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200260skip_comment(struct lys_parser_ctx *ctx, const char **data, int comment)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200261{
Radek Krejci80dd33e2018-09-26 15:57:18 +0200262 /* internal statuses: 0 - comment ended,
263 * 1 - in line comment,
264 * 2 - in block comment,
265 * 3 - in block comment with last read character '*'
266 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200267 while (**data && comment) {
268 switch (comment) {
269 case 1:
270 if (**data == '\n') {
271 comment = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200272 ++ctx->line;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200273 }
274 break;
275 case 2:
276 if (**data == '*') {
Radek Krejci15c80ca2018-10-09 11:01:31 +0200277 comment = 3;
278 } else if (**data == '\n') {
279 ++ctx->line;
280 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200281 break;
282 case 3:
283 if (**data == '/') {
284 comment = 0;
Radek Krejci5b930492019-06-11 14:54:08 +0200285 } else if (**data != '*') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200286 if (**data == '\n') {
287 ++ctx->line;
288 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200289 comment = 2;
290 }
291 break;
292 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200293 LOGINT_RET(ctx->ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200294 }
295
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200296 if (**data == '\n') {
297 ctx->indent = 0;
298 } else {
299 ++ctx->indent;
300 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200301 ++(*data);
302 }
303
304 if (!**data && (comment > 1)) {
David Sedlákb3077192019-06-19 10:55:37 +0200305 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Unexpected end-of-input, non-terminated comment.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200306 return LY_EVALID;
307 }
308
309 return LY_SUCCESS;
310}
311
Michal Vaskoea5abea2018-09-18 13:10:54 +0200312/**
313 * @brief Read a quoted string from data.
314 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200315 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200316 * @param[in,out] data Data to read from, always moved to currently handled character.
317 * @param[in] arg Type of YANG keyword argument expected.
318 * @param[out] word_p Pointer to the read quoted string.
319 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read quoted string. If not needed,
320 * set to NULL. Otherwise equal to \p word_p.
321 * @param[out] word_len Length of the read quoted string.
322 * @param[out] buf_len Length of the dynamically-allocated buffer \p word_b.
323 * @param[in] indent Current indent (number of YANG spaces). Needed for correct multi-line string
324 * indenation in the final quoted string.
325 *
326 * @return LY_ERR values.
327 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200328static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200329read_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 +0200330 size_t *buf_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200331{
332 /* string: 0 - string ended, 1 - string with ', 2 - string with ", 3 - string with " with last character \,
333 * 4 - string finished, now skipping whitespaces looking for +,
334 * 5 - string continues after +, skipping whitespaces */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200335 unsigned int string, block_indent = 0, current_indent = 0, need_buf = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200336 const char *c;
337
338 if (**data == '\"') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200339 string = 2;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200340 current_indent = block_indent = ctx->indent + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200341 } else {
342 assert(**data == '\'');
343 string = 1;
344 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200345 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200346
347 while (**data && string) {
348 switch (string) {
349 case 1:
350 switch (**data) {
351 case '\'':
352 /* string may be finished, but check for + */
353 string = 4;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200354 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200355 break;
356 default:
357 /* check and store character */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200358 LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, buf_len, need_buf));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200359 break;
360 }
361 break;
362 case 2:
363 switch (**data) {
364 case '\"':
365 /* string may be finished, but check for + */
366 string = 4;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200367 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200368 break;
369 case '\\':
370 /* special character following */
371 string = 3;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200372 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200373 break;
374 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200375 if (current_indent < block_indent) {
376 ++current_indent;
377 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200378 } else {
379 /* check and store character */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200380 LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, buf_len, need_buf));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200381 }
382 break;
383 case '\t':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200384 if (current_indent < block_indent) {
385 assert(need_buf);
386 current_indent += 8;
387 ctx->indent += 8;
388 for (; current_indent > block_indent; --current_indent, --ctx->indent) {
389 /* store leftover spaces from the tab */
390 c = " ";
391 LY_CHECK_RET(buf_store_char(ctx, &c, arg, word_p, word_len, word_b, buf_len, need_buf));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200392 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200393 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200394 } else {
395 /* check and store character */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200396 LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, buf_len, need_buf));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200397 /* additional characters for indentation - only 1 was count in buf_store_char */
398 ctx->indent += 7;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200399 }
400 break;
401 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200402 if (block_indent) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200403 /* we will be removing the indents so we need our own buffer */
404 need_buf = 1;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200405
406 /* remove trailing tabs and spaces */
407 while ((*word_len) && ((*word_p)[(*word_len) - 1] == '\t' || (*word_p)[(*word_len) - 1] == ' ')) {
408 --(*word_len);
409 }
410
411 /* start indentation */
412 current_indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200413 }
414
415 /* check and store character */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200416 LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, buf_len, need_buf));
417
418 /* maintain line number */
419 ++ctx->line;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200420
421 /* reset context indentation counter for possible string after this one */
422 ctx->indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200423 break;
424 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200425 /* first non-whitespace character, stop eating indentation */
426 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200427
428 /* check and store character */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200429 LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, buf_len, need_buf));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200430 break;
431 }
432 break;
433 case 3:
434 /* string encoded characters */
435 switch (**data) {
436 case 'n':
437 c = "\n";
438 break;
439 case 't':
440 c = "\t";
441 break;
442 case '\"':
443 c = *data;
444 break;
445 case '\\':
446 c = *data;
447 break;
448 default:
David Sedlákb3077192019-06-19 10:55:37 +0200449 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Double-quoted string unknown special character '\\%c'.", **data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200450 return LY_EVALID;
451 }
452
453 /* check and store character */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200454 LY_CHECK_RET(buf_store_char(ctx, &c, arg, word_p, word_len, word_b, buf_len, need_buf));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200455
456 string = 2;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200457 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200458 break;
459 case 4:
460 switch (**data) {
461 case '+':
462 /* string continues */
463 string = 5;
Radek Krejciefd22f62018-09-27 11:47:58 +0200464 need_buf = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200465 break;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200466 case '\n':
467 ++ctx->line;
468 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200469 case ' ':
470 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200471 /* just skip */
472 break;
473 default:
474 /* string is finished */
475 goto string_end;
476 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200477 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200478 break;
479 case 5:
480 switch (**data) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200481 case '\n':
482 ++ctx->line;
483 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200484 case ' ':
485 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200486 /* skip */
487 break;
488 case '\'':
489 string = 1;
490 break;
491 case '\"':
492 string = 2;
493 break;
494 default:
495 /* it must be quoted again */
David Sedlákb3077192019-06-19 10:55:37 +0200496 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Both string parts divided by '+' must be quoted.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200497 return LY_EVALID;
498 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200499 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200500 break;
501 default:
502 return LY_EINT;
503 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200504 }
505
506string_end:
Radek Krejci4e199f52019-05-28 09:09:28 +0200507 if (arg <= Y_PREF_IDENTIF_ARG && !(*word_len)) {
508 /* empty identifier */
David Sedlákb3077192019-06-19 10:55:37 +0200509 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Statement argument is required.");
Radek Krejci4e199f52019-05-28 09:09:28 +0200510 return LY_EVALID;
511 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200512 return LY_SUCCESS;
513}
514
Michal Vaskoea5abea2018-09-18 13:10:54 +0200515/**
516 * @brief Get another YANG string from the raw data.
517 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200518 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200519 * @param[in,out] data Data to read from, always moved to currently handled character.
520 * @param[in] arg Type of YANG keyword argument expected.
Radek Krejcid3ca0632019-04-16 16:54:54 +0200521 * @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 +0200522 * @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 +0200523 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read string. If not needed,
524 * set to NULL. Otherwise equal to \p word_p.
525 * @param[out] word_len Length of the read string.
526 *
527 * @return LY_ERR values.
Michal Vasko7fbc8162018-09-17 10:35:16 +0200528 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200529LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200530get_argument(struct lys_parser_ctx *ctx, const char **data, enum yang_arg arg,
Radek Krejcid3ca0632019-04-16 16:54:54 +0200531 uint16_t *flags, char **word_p, char **word_b, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200532{
Radek Krejci44ceedc2018-10-02 15:54:31 +0200533 size_t buf_len = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200534
535 /* word buffer - dynamically allocated */
536 *word_b = NULL;
537
538 /* word pointer - just a pointer to data */
539 *word_p = NULL;
540
541 *word_len = 0;
542 while (**data) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200543 switch (**data) {
544 case '\'':
545 case '\"':
546 if (*word_len) {
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200547 /* invalid - quotes cannot be in unquoted string and only optsep, ; or { can follow it */
David Sedlákb3077192019-06-19 10:55:37 +0200548 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, *data,
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200549 "unquoted string character, optsep, semicolon or opening brace");
550 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200551 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200552 if (flags) {
553 (*flags) |= (**data) == '\'' ? LYS_SINGLEQUOTED : LYS_DOUBLEQUOTED;
554 }
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100555 LY_CHECK_RET(read_qstring(ctx, data, arg, word_p, word_b, word_len, &buf_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200556 goto str_end;
557 case '/':
Radek Krejci44ceedc2018-10-02 15:54:31 +0200558 if ((*data)[1] == '/') {
559 /* one-line comment */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200560 MOVE_INPUT(ctx, data, 2);
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100561 LY_CHECK_RET(skip_comment(ctx, data, 1));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200562 } else if ((*data)[1] == '*') {
563 /* block comment */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200564 MOVE_INPUT(ctx, data, 2);
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100565 LY_CHECK_RET(skip_comment(ctx, data, 2));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200566 } else {
567 /* not a comment after all */
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100568 LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, &buf_len, 0));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200569 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200570 break;
571 case ' ':
572 if (*word_len) {
573 /* word is finished */
574 goto str_end;
575 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200576 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200577 break;
578 case '\t':
579 if (*word_len) {
580 /* word is finished */
581 goto str_end;
582 }
583 /* tabs count for 8 spaces */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200584 ctx->indent += 8;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200585
586 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200587 break;
588 case '\n':
589 if (*word_len) {
590 /* word is finished */
591 goto str_end;
592 }
593 /* reset indent */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200594 ctx->indent = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200595
596 /* track line numbers */
597 ++ctx->line;
598
599 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200600 break;
601 case ';':
602 case '{':
603 if (*word_len || (arg == Y_MAYBE_STR_ARG)) {
604 /* word is finished */
605 goto str_end;
606 }
607
David Sedlákb3077192019-06-19 10:55:37 +0200608 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, *data, "an argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200609 return LY_EVALID;
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200610 case '}':
611 /* invalid - braces cannot be in unquoted string (opening braces terminates the string and can follow it) */
David Sedlákb3077192019-06-19 10:55:37 +0200612 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, *data,
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200613 "unquoted string character, optsep, semicolon or opening brace");
614 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200615 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200616 LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, &buf_len, 0));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200617 break;
618 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200619 }
620
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200621 /* unexpected end of loop */
David Sedlákb3077192019-06-19 10:55:37 +0200622 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200623 return LY_EVALID;
624
Michal Vasko7fbc8162018-09-17 10:35:16 +0200625str_end:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200626 /* terminating NULL byte for buf */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200627 if (*word_b) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200628 (*word_b) = ly_realloc(*word_b, (*word_len) + 1);
629 LY_CHECK_ERR_RET(!(*word_b), LOGMEM(ctx->ctx), LY_EMEM);
630 (*word_b)[*word_len] = '\0';
Michal Vasko7fbc8162018-09-17 10:35:16 +0200631 *word_p = *word_b;
632 }
633
634 return LY_SUCCESS;
635}
636
Michal Vaskoea5abea2018-09-18 13:10:54 +0200637/**
638 * @brief Get another YANG keyword from the raw data.
639 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200640 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200641 * @param[in,out] data Data to read from, always moved to currently handled character.
642 * @param[out] kw YANG keyword read.
643 * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances.
644 * @param[out] word_len Length of the keyword in the data. Useful for extension instances.
645 *
646 * @return LY_ERR values.
647 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200648LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200649get_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 +0200650{
Michal Vasko7fbc8162018-09-17 10:35:16 +0200651 int prefix;
652 const char *word_start;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200653 unsigned int c;
654 size_t len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200655
656 if (word_p) {
657 *word_p = NULL;
658 *word_len = 0;
659 }
660
661 /* first skip "optsep", comments */
662 while (**data) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200663 switch (**data) {
664 case '/':
665 if ((*data)[1] == '/') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200666 /* one-line comment */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200667 MOVE_INPUT(ctx, data, 2);
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100668 LY_CHECK_RET(skip_comment(ctx, data, 1));
Radek Krejcidcc7b322018-10-11 14:24:02 +0200669 } else if ((*data)[1] == '*') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200670 /* block comment */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200671 MOVE_INPUT(ctx, data, 2);
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100672 LY_CHECK_RET(skip_comment(ctx, data, 2));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200673 } else {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200674 /* error - not a comment after all, keyword cannot start with slash */
David Sedlákb3077192019-06-19 10:55:37 +0200675 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '/'.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200676 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200677 }
Radek Krejci13028282019-06-11 14:56:48 +0200678 continue;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200679 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200680 /* skip whitespaces (optsep) */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200681 ++ctx->line;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200682 ctx->indent = 0;
683 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200684 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200685 /* skip whitespaces (optsep) */
686 ++ctx->indent;
687 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200688 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200689 /* skip whitespaces (optsep) */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200690 ctx->indent += 8;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200691 break;
692 default:
693 /* either a keyword start or an invalid character */
694 goto keyword_start;
695 }
696
697 ++(*data);
698 }
699
700keyword_start:
701 word_start = *data;
David Sedlák5f8f0332019-06-18 16:34:30 +0200702 *kw = lysp_match_kw(ctx, data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200703
David Sedlák1bccdfa2019-06-17 15:55:27 +0200704 if (*kw == YANG_SEMICOLON || *kw == YANG_LEFT_BRACE || *kw == YANG_RIGHT_BRACE) {
Radek Krejci626df482018-10-11 15:06:31 +0200705 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200706 }
707
708 if (*kw != YANG_NONE) {
709 /* make sure we have the whole keyword */
710 switch (**data) {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200711 case '\n':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200712 case '\t':
Radek Krejciabdd8062019-06-11 16:44:19 +0200713 case ' ':
714 /* mandatory "sep" is just checked, not eaten so nothing in the context is updated */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200715 break;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200716 case ':':
717 /* keyword is not actually a keyword, but prefix of an extension.
718 * To avoid repeated check of the prefix syntax, move to the point where the colon was read
719 * and we will be checking the keyword (extension instance) itself */
720 prefix = 1;
721 MOVE_INPUT(ctx, data, 1);
722 goto extension;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200723 case '{':
724 /* allowed only for input and output statements which can be without arguments */
725 if (*kw == YANG_INPUT || *kw == YANG_OUTPUT) {
726 break;
727 }
728 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200729 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200730 MOVE_INPUT(ctx, data, 1);
David Sedlákb3077192019-06-19 10:55:37 +0200731 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(*data - word_start), word_start,
Radek Krejci44ceedc2018-10-02 15:54:31 +0200732 "a keyword followed by a separator");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200733 return LY_EVALID;
734 }
735 } else {
736 /* still can be an extension */
737 prefix = 0;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200738extension:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200739 while (**data && (**data != ' ') && (**data != '\t') && (**data != '\n') && (**data != '{') && (**data != ';')) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200740 LY_CHECK_ERR_RET(ly_getutf8(data, &c, &len),
David Sedlákb3077192019-06-19 10:55:37 +0200741 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, (*data)[-len]), LY_EVALID);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200742 ++ctx->indent;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200743 /* check character validity */
David Sedlák4a650532019-07-10 11:55:18 +0200744 LY_CHECK_RET(lysp_check_identifierchar(ctx, c, *data - len == word_start ? 1 : 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200745 }
746 if (!**data) {
David Sedlákb3077192019-06-19 10:55:37 +0200747 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200748 return LY_EVALID;
749 }
750
751 /* prefix is mandatory for extension instances */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200752 if (prefix != 2) {
David Sedlákb3077192019-06-19 10:55:37 +0200753 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(*data - word_start), word_start, "a keyword");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200754 return LY_EVALID;
755 }
756
757 *kw = YANG_CUSTOM;
758 }
Radek Krejci626df482018-10-11 15:06:31 +0200759success:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200760 if (word_p) {
761 *word_p = (char *)word_start;
762 *word_len = *data - word_start;
763 }
764
765 return LY_SUCCESS;
766}
767
Michal Vaskoea5abea2018-09-18 13:10:54 +0200768/**
769 * @brief Parse extension instance substatements.
770 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200771 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200772 * @param[in,out] data Data to read from, always moved to currently handled character.
773 * @param[in] word Extension instance substatement name (keyword).
774 * @param[in] word_len Extension instance substatement name length.
775 * @param[in,out] child Children of this extension instance to add to.
776 *
777 * @return LY_ERR values.
778 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200779static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200780parse_ext_substmt(struct lys_parser_ctx *ctx, const char **data, char *word, size_t word_len,
Michal Vasko7fbc8162018-09-17 10:35:16 +0200781 struct lysp_stmt **child)
782{
783 char *buf;
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100784 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200785 enum yang_keyword kw;
786 struct lysp_stmt *stmt, *par_child;
787
788 stmt = calloc(1, sizeof *stmt);
789 LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM);
790
Radek Krejcibb9b1982019-04-08 14:24:59 +0200791 /* insert into parent statements */
792 if (!*child) {
793 *child = stmt;
794 } else {
795 for (par_child = *child; par_child->next; par_child = par_child->next);
796 par_child->next = stmt;
797 }
798
Radek Krejci44ceedc2018-10-02 15:54:31 +0200799 stmt->stmt = lydict_insert(ctx->ctx, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200800
801 /* get optional argument */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200802 LY_CHECK_RET(get_argument(ctx, data, Y_MAYBE_STR_ARG, &stmt->flags, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200803
Radek Krejci0ae092d2018-09-20 16:43:19 +0200804 if (word) {
805 if (buf) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200806 stmt->arg = lydict_insert_zc(ctx->ctx, word);
Radek Krejci0ae092d2018-09-20 16:43:19 +0200807 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200808 stmt->arg = lydict_insert(ctx->ctx, word, word_len);
Radek Krejci0ae092d2018-09-20 16:43:19 +0200809 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200810 }
811
Radek Krejci6d6556c2018-11-08 09:37:45 +0100812 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, ) {
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100813 LY_CHECK_RET(parse_ext_substmt(ctx, data, word, word_len, &stmt->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200814 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200815 return ret;
816}
817
Michal Vaskoea5abea2018-09-18 13:10:54 +0200818/**
819 * @brief Parse extension instance.
820 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200821 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200822 * @param[in,out] data Data to read from, always moved to currently handled character.
823 * @param[in] ext_name Extension instance substatement name (keyword).
824 * @param[in] ext_name_len Extension instance substatement name length.
825 * @param[in] insubstmt Type of the keyword this extension instance is a substatement of.
826 * @param[in] insubstmt_index Index of the keyword instance this extension instance is a substatement of.
827 * @param[in,out] exts Extension instances to add to.
828 *
829 * @return LY_ERR values.
830 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200831static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200832parse_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 +0200833 uint32_t insubstmt_index, struct lysp_ext_instance **exts)
834{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100835 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200836 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200837 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200838 struct lysp_ext_instance *e;
839 enum yang_keyword kw;
840
Radek Krejci2c4e7172018-10-19 15:56:26 +0200841 LY_ARRAY_NEW_RET(ctx->ctx, *exts, e, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200842
843 /* store name and insubstmt info */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200844 e->name = lydict_insert(ctx->ctx, ext_name, ext_name_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200845 e->insubstmt = insubstmt;
846 e->insubstmt_index = insubstmt_index;
847
848 /* get optional argument */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200849 LY_CHECK_RET(get_argument(ctx, data, Y_MAYBE_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200850
Radek Krejci0ae092d2018-09-20 16:43:19 +0200851 if (word) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200852 INSERT_WORD(ctx, buf, e->argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200853 }
854
Radek Krejci6d6556c2018-11-08 09:37:45 +0100855 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100856 LY_CHECK_RET(parse_ext_substmt(ctx, data, word, word_len, &e->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200857 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200858 return ret;
859}
860
Michal Vaskoea5abea2018-09-18 13:10:54 +0200861/**
862 * @brief Parse a generic text field without specific constraints. Those are contact, organization,
863 * description, etc...
864 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200865 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200866 * @param[in,out] data Data to read from, always moved to currently handled character.
867 * @param[in] substmt Type of this substatement.
868 * @param[in] substmt_index Index of this substatement.
869 * @param[in,out] value Place to store the parsed value.
870 * @param[in] arg Type of the YANG keyword argument (of the value).
871 * @param[in,out] exts Extension instances to add to.
872 *
873 * @return LY_ERR values.
874 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200875static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200876parse_text_field(struct lys_parser_ctx *ctx, const char **data, LYEXT_SUBSTMT substmt, uint32_t substmt_index,
Michal Vasko7fbc8162018-09-17 10:35:16 +0200877 const char **value, enum yang_arg arg, struct lysp_ext_instance **exts)
878{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100879 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200880 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200881 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200882 enum yang_keyword kw;
883
884 if (*value) {
David Sedlákb3077192019-06-19 10:55:37 +0200885 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200886 return LY_EVALID;
887 }
888
889 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200890 LY_CHECK_RET(get_argument(ctx, data, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200891
892 /* store value and spend buf if allocated */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200893 INSERT_WORD(ctx, buf, *value, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200894
Radek Krejci6d6556c2018-11-08 09:37:45 +0100895 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200896 switch (kw) {
897 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100898 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, substmt, substmt_index, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200899 break;
900 default:
David Sedlákb3077192019-06-19 10:55:37 +0200901 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200902 return LY_EVALID;
903 }
904 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200905 return ret;
906}
907
Michal Vaskoea5abea2018-09-18 13:10:54 +0200908/**
909 * @brief Parse the yang-version statement.
910 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200911 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200912 * @param[in,out] data Data to read from, always moved to currently handled character.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100913 * @param[out] version Storage for the parsed information.
914 * @param[in, out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200915 *
916 * @return LY_ERR values.
917 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200918static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200919parse_yangversion(struct lys_parser_ctx *ctx, const char **data, uint8_t *version, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200920{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100921 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200922 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200923 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200924 enum yang_keyword kw;
925
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100926 if (*version) {
David Sedlákb3077192019-06-19 10:55:37 +0200927 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200928 return LY_EVALID;
929 }
930
931 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200932 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200933
934 if ((word_len == 3) && !strncmp(word, "1.0", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100935 *version = LYS_VERSION_1_0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200936 } else if ((word_len == 3) && !strncmp(word, "1.1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100937 *version = LYS_VERSION_1_1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200938 } else {
David Sedlákb3077192019-06-19 10:55:37 +0200939 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200940 free(buf);
941 return LY_EVALID;
942 }
943 free(buf);
944
Radek Krejci6d6556c2018-11-08 09:37:45 +0100945 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200946 switch (kw) {
947 case YANG_CUSTOM:
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100948 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_VERSION, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200949 break;
950 default:
David Sedlákb3077192019-06-19 10:55:37 +0200951 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200952 return LY_EVALID;
953 }
954 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200955 return ret;
956}
957
Michal Vaskoea5abea2018-09-18 13:10:54 +0200958/**
959 * @brief Parse the belongs-to statement.
960 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200961 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200962 * @param[in,out] data Data to read from, always moved to currently handled character.
963 * @param[in,out] belongsto Place to store the parsed value.
964 * @param[in,out] prefix Place to store the parsed belongs-to prefix value.
965 * @param[in,out] exts Extension instances to add to.
966 *
967 * @return LY_ERR values.
968 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200969static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200970parse_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 +0200971{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100972 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200973 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200974 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200975 enum yang_keyword kw;
976
977 if (*belongsto) {
David Sedlákb3077192019-06-19 10:55:37 +0200978 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200979 return LY_EVALID;
980 }
981
982 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200983 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200984
Radek Krejci44ceedc2018-10-02 15:54:31 +0200985 INSERT_WORD(ctx, buf, *belongsto, word, word_len);
Radek Krejcif09e4e82019-06-14 15:08:11 +0200986
Radek Krejci6d6556c2018-11-08 09:37:45 +0100987 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200988 switch (kw) {
989 case YANG_PREFIX:
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100990 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_PREFIX, 0, prefix, Y_IDENTIF_ARG, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200991 break;
992 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100993 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_BELONGSTO, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200994 break;
995 default:
David Sedlákb3077192019-06-19 10:55:37 +0200996 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200997 return LY_EVALID;
998 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200999 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001000 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001001checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001002 /* mandatory substatements */
1003 if (!*prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001004 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001005 return LY_EVALID;
1006 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001007 return ret;
1008}
1009
Michal Vaskoea5abea2018-09-18 13:10:54 +02001010/**
1011 * @brief Parse the revision-date statement.
1012 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001013 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001014 * @param[in,out] data Data to read from, always moved to currently handled character.
1015 * @param[in,out] rev Array to store the parsed value in.
1016 * @param[in,out] exts Extension instances to add to.
1017 *
1018 * @return LY_ERR values.
1019 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001020static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001021parse_revisiondate(struct lys_parser_ctx *ctx, const char **data, char *rev, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001022{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001023 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001024 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001025 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001026 enum yang_keyword kw;
1027
1028 if (rev[0]) {
David Sedlákb3077192019-06-19 10:55:37 +02001029 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001030 return LY_EVALID;
1031 }
1032
1033 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001034 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001035
1036 /* check value */
Radek Krejcibbe09a92018-11-08 09:36:54 +01001037 if (lysp_check_date(ctx, word, word_len, "revision-date")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001038 free(buf);
1039 return LY_EVALID;
1040 }
1041
1042 /* store value and spend buf if allocated */
1043 strncpy(rev, word, word_len);
1044 free(buf);
1045
Radek Krejci6d6556c2018-11-08 09:37:45 +01001046 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001047 switch (kw) {
1048 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001049 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_REVISIONDATE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001050 break;
1051 default:
David Sedlákb3077192019-06-19 10:55:37 +02001052 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001053 return LY_EVALID;
1054 }
1055 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001056 return ret;
1057}
1058
Michal Vaskoea5abea2018-09-18 13:10:54 +02001059/**
1060 * @brief Parse the include statement.
1061 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001062 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001063 * @param[in] module_name Name of the module to check name collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001064 * @param[in,out] data Data to read from, always moved to currently handled character.
1065 * @param[in,out] includes Parsed includes to add to.
1066 *
1067 * @return LY_ERR values.
1068 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001069static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001070parse_include(struct lys_parser_ctx *ctx, const char *module_name, const char **data, struct lysp_include **includes)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001071{
Radek Krejcid33273d2018-10-25 14:55:52 +02001072 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001073 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001074 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001075 enum yang_keyword kw;
1076 struct lysp_include *inc;
1077
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001078 LY_ARRAY_NEW_RET(ctx->ctx, *includes, inc, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001079
1080 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001081 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001082
Radek Krejci086c7132018-10-26 15:29:04 +02001083 INSERT_WORD(ctx, buf, inc->name, word, word_len);
1084
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001085 /* submodules share the namespace with the module names, so there must not be
1086 * a module of the same name in the context, no need for revision matching */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001087 if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(ctx->ctx, inc->name)) {
David Sedlákb3077192019-06-19 10:55:37 +02001088 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Name collision between module and submodule of name \"%s\".", inc->name);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001089 return LY_EVALID;
1090 }
1091
Radek Krejci6d6556c2018-11-08 09:37:45 +01001092 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001093 switch (kw) {
1094 case YANG_DESCRIPTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01001095 YANG_CHECK_STMTVER2_RET(ctx, "description", "include");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001096 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 +02001097 break;
1098 case YANG_REFERENCE:
Radek Krejciceaf2122019-01-02 15:03:26 +01001099 YANG_CHECK_STMTVER2_RET(ctx, "reference", "include");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001100 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 +02001101 break;
1102 case YANG_REVISION_DATE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001103 LY_CHECK_RET(parse_revisiondate(ctx, data, inc->rev, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001104 break;
1105 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001106 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001107 break;
1108 default:
David Sedlákb3077192019-06-19 10:55:37 +02001109 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001110 return LY_EVALID;
1111 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001112 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001113 return ret;
1114}
1115
Michal Vaskoea5abea2018-09-18 13:10:54 +02001116/**
1117 * @brief Parse the import statement.
1118 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001119 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001120 * @param[in] module_prefix Prefix of the module to check prefix collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001121 * @param[in,out] data Data to read from, always moved to currently handled character.
1122 * @param[in,out] imports Parsed imports to add to.
1123 *
1124 * @return LY_ERR values.
1125 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001126static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001127parse_import(struct lys_parser_ctx *ctx, const char *module_prefix, const char **data, struct lysp_import **imports)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001128{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001129 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001130 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001131 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001132 enum yang_keyword kw;
1133 struct lysp_import *imp;
1134
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001135 LY_ARRAY_NEW_RET(ctx->ctx, *imports, imp, LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001136
1137 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001138 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02001139 INSERT_WORD(ctx, buf, imp->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001140
Radek Krejci6d6556c2018-11-08 09:37:45 +01001141 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001142 switch (kw) {
1143 case YANG_PREFIX:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001144 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 +01001145 LY_CHECK_RET(lysp_check_prefix(ctx, *imports, module_prefix, &imp->prefix), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001146 break;
1147 case YANG_DESCRIPTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01001148 YANG_CHECK_STMTVER2_RET(ctx, "description", "import");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001149 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 +02001150 break;
1151 case YANG_REFERENCE:
Radek Krejciceaf2122019-01-02 15:03:26 +01001152 YANG_CHECK_STMTVER2_RET(ctx, "reference", "import");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001153 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 +02001154 break;
1155 case YANG_REVISION_DATE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001156 LY_CHECK_RET(parse_revisiondate(ctx, data, imp->rev, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001157 break;
1158 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001159 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001160 break;
1161 default:
David Sedlákb3077192019-06-19 10:55:37 +02001162 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001163 return LY_EVALID;
1164 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001165 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001166 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001167checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001168 /* mandatory substatements */
David Sedlákb3077192019-06-19 10:55:37 +02001169 LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001170
1171 return ret;
1172}
1173
Michal Vaskoea5abea2018-09-18 13:10:54 +02001174/**
1175 * @brief Parse the revision statement.
1176 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001177 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001178 * @param[in,out] data Data to read from, always moved to currently handled character.
1179 * @param[in,out] revs Parsed revisions to add to.
1180 *
1181 * @return LY_ERR values.
1182 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001183static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001184parse_revision(struct lys_parser_ctx *ctx, const char **data, struct lysp_revision **revs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001185{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001186 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001187 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001188 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001189 enum yang_keyword kw;
1190 struct lysp_revision *rev;
1191
Radek Krejci2c4e7172018-10-19 15:56:26 +02001192 LY_ARRAY_NEW_RET(ctx->ctx, *revs, rev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001193
1194 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001195 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001196
1197 /* check value */
Radek Krejcibbe09a92018-11-08 09:36:54 +01001198 if (lysp_check_date(ctx, word, word_len, "revision")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001199 return LY_EVALID;
1200 }
1201
Radek Krejcib7db73a2018-10-24 14:18:40 +02001202 strncpy(rev->date, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001203 free(buf);
1204
Radek Krejci6d6556c2018-11-08 09:37:45 +01001205 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001206 switch (kw) {
1207 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001208 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 +02001209 break;
1210 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001211 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 +02001212 break;
1213 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001214 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001215 break;
1216 default:
David Sedlákb3077192019-06-19 10:55:37 +02001217 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001218 return LY_EVALID;
1219 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001220 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001221 return ret;
1222}
1223
Michal Vaskoea5abea2018-09-18 13:10:54 +02001224/**
1225 * @brief Parse a generic text field that can have more instances such as base.
1226 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001227 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001228 * @param[in,out] data Data to read from, always moved to currently handled character.
1229 * @param[in] substmt Type of this substatement.
1230 * @param[in,out] texts Parsed values to add to.
1231 * @param[in] arg Type of the expected argument.
1232 * @param[in,out] exts Extension instances to add to.
1233 *
1234 * @return LY_ERR values.
1235 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001236static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001237parse_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 +02001238 struct lysp_ext_instance **exts)
1239{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001240 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001241 char *buf, *word;
Radek Krejci151a5b72018-10-19 14:21:44 +02001242 const char **item;
1243 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001244 enum yang_keyword kw;
1245
1246 /* allocate new pointer */
Radek Krejci2c4e7172018-10-19 15:56:26 +02001247 LY_ARRAY_NEW_RET(ctx->ctx, *texts, item, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001248
1249 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001250 LY_CHECK_RET(get_argument(ctx, data, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001251
Radek Krejci151a5b72018-10-19 14:21:44 +02001252 INSERT_WORD(ctx, buf, *item, word, word_len);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001253 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001254 switch (kw) {
1255 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001256 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, substmt, LY_ARRAY_SIZE(*texts) - 1, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001257 break;
1258 default:
David Sedlákb3077192019-06-19 10:55:37 +02001259 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001260 return LY_EVALID;
1261 }
1262 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001263 return ret;
1264}
1265
Michal Vaskoea5abea2018-09-18 13:10:54 +02001266/**
1267 * @brief Parse the config statement.
1268 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001269 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001270 * @param[in,out] data Data to read from, always moved to currently handled character.
1271 * @param[in,out] flags Flags to add to.
1272 * @param[in,out] exts Extension instances to add to.
1273 *
1274 * @return LY_ERR values.
1275 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001276static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001277parse_config(struct lys_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001278{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001279 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001280 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001281 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001282 enum yang_keyword kw;
1283
1284 if (*flags & LYS_CONFIG_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001285 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001286 return LY_EVALID;
1287 }
1288
1289 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001290 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001291
1292 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1293 *flags |= LYS_CONFIG_W;
1294 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1295 *flags |= LYS_CONFIG_R;
1296 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001297 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001298 free(buf);
1299 return LY_EVALID;
1300 }
1301 free(buf);
1302
Radek Krejci6d6556c2018-11-08 09:37:45 +01001303 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001304 switch (kw) {
1305 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001306 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_CONFIG, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001307 break;
1308 default:
David Sedlákb3077192019-06-19 10:55:37 +02001309 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001310 return LY_EVALID;
1311 }
1312 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001313 return ret;
1314}
1315
Michal Vaskoea5abea2018-09-18 13:10:54 +02001316/**
1317 * @brief Parse the mandatory statement.
1318 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001319 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001320 * @param[in,out] data Data to read from, always moved to currently handled character.
1321 * @param[in,out] flags Flags to add to.
1322 * @param[in,out] exts Extension instances to add to.
1323 *
1324 * @return LY_ERR values.
1325 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001326static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001327parse_mandatory(struct lys_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001328{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001329 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001330 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001331 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001332 enum yang_keyword kw;
1333
1334 if (*flags & LYS_MAND_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001335 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001336 return LY_EVALID;
1337 }
1338
1339 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001340 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001341
1342 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1343 *flags |= LYS_MAND_TRUE;
1344 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1345 *flags |= LYS_MAND_FALSE;
1346 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001347 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001348 free(buf);
1349 return LY_EVALID;
1350 }
1351 free(buf);
1352
Radek Krejci6d6556c2018-11-08 09:37:45 +01001353 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001354 switch (kw) {
1355 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001356 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MANDATORY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001357 break;
1358 default:
David Sedlákb3077192019-06-19 10:55:37 +02001359 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001360 return LY_EVALID;
1361 }
1362 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001363 return ret;
1364}
1365
Michal Vaskoea5abea2018-09-18 13:10:54 +02001366/**
1367 * @brief Parse a restriction such as range or length.
1368 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001369 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001370 * @param[in,out] data Data to read from, always moved to currently handled character.
1371 * @param[in] restr_kw Type of this particular restriction.
1372 * @param[in,out] exts Extension instances to add to.
1373 *
1374 * @return LY_ERR values.
1375 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001376static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001377parse_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 +02001378{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001379 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001380 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001381 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001382 enum yang_keyword kw;
1383
1384 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001385 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001386
Radek Krejcif09e4e82019-06-14 15:08:11 +02001387 YANG_CHECK_NONEMPTY(ctx, NULL, word_len, ly_stmt2str(restr_kw));
Radek Krejci44ceedc2018-10-02 15:54:31 +02001388 INSERT_WORD(ctx, buf, restr->arg, word, word_len);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001389 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001390 switch (kw) {
1391 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001392 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 +02001393 break;
1394 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001395 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 +02001396 break;
1397 case YANG_ERROR_APP_TAG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001398 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 +02001399 break;
1400 case YANG_ERROR_MESSAGE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001401 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 +02001402 break;
1403 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001404 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001405 break;
1406 default:
David Sedlákb3077192019-06-19 10:55:37 +02001407 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001408 return LY_EVALID;
1409 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001410 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001411 return ret;
1412}
1413
Michal Vaskoea5abea2018-09-18 13:10:54 +02001414/**
1415 * @brief Parse a restriction that can have more instances such as must.
1416 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001417 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001418 * @param[in,out] data Data to read from, always moved to currently handled character.
1419 * @param[in] restr_kw Type of this particular restriction.
1420 * @param[in,out] restrs Restrictions to add to.
1421 *
1422 * @return LY_ERR values.
1423 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001424static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001425parse_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 +02001426{
1427 struct lysp_restr *restr;
1428
Radek Krejci2c4e7172018-10-19 15:56:26 +02001429 LY_ARRAY_NEW_RET(ctx->ctx, *restrs, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001430 return parse_restr(ctx, data, restr_kw, restr);
1431}
1432
Michal Vaskoea5abea2018-09-18 13:10:54 +02001433/**
1434 * @brief Parse the status statement.
1435 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001436 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001437 * @param[in,out] data Data to read from, always moved to currently handled character.
1438 * @param[in,out] flags Flags to add to.
1439 * @param[in,out] exts Extension instances to add to.
1440 *
1441 * @return LY_ERR values.
1442 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001443static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001444parse_status(struct lys_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001445{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001446 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001447 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001448 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001449 enum yang_keyword kw;
1450
1451 if (*flags & LYS_STATUS_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001452 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001453 return LY_EVALID;
1454 }
1455
1456 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001457 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001458
1459 if ((word_len == 7) && !strncmp(word, "current", word_len)) {
1460 *flags |= LYS_STATUS_CURR;
1461 } else if ((word_len == 10) && !strncmp(word, "deprecated", word_len)) {
1462 *flags |= LYS_STATUS_DEPRC;
1463 } else if ((word_len == 8) && !strncmp(word, "obsolete", word_len)) {
1464 *flags |= LYS_STATUS_OBSLT;
1465 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001466 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001467 free(buf);
1468 return LY_EVALID;
1469 }
1470 free(buf);
1471
Radek Krejci6d6556c2018-11-08 09:37:45 +01001472 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001473 switch (kw) {
1474 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001475 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_STATUS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001476 break;
1477 default:
David Sedlákb3077192019-06-19 10:55:37 +02001478 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001479 return LY_EVALID;
1480 }
1481 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001482 return ret;
1483}
1484
Michal Vaskoea5abea2018-09-18 13:10:54 +02001485/**
1486 * @brief Parse the when statement.
1487 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001488 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001489 * @param[in,out] data Data to read from, always moved to currently handled character.
1490 * @param[in,out] when_p When pointer to parse to.
1491 *
1492 * @return LY_ERR values.
1493 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001494LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001495parse_when(struct lys_parser_ctx *ctx, const char **data, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001496{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001497 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001498 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001499 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001500 enum yang_keyword kw;
1501 struct lysp_when *when;
1502
1503 if (*when_p) {
David Sedlákb3077192019-06-19 10:55:37 +02001504 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001505 return LY_EVALID;
1506 }
1507
1508 when = calloc(1, sizeof *when);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001509 LY_CHECK_ERR_RET(!when, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001510
1511 /* get value */
Radek Krejci2f54df52019-06-21 10:59:19 +02001512 LY_CHECK_ERR_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len), free(when), LY_EMEM);
Radek Krejcif09e4e82019-06-14 15:08:11 +02001513 YANG_CHECK_NONEMPTY(ctx, when, word_len, "when");
Radek Krejci44ceedc2018-10-02 15:54:31 +02001514 INSERT_WORD(ctx, buf, when->cond, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001515
Radek Krejcif09e4e82019-06-14 15:08:11 +02001516 *when_p = when;
1517
Radek Krejci6d6556c2018-11-08 09:37:45 +01001518 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001519 switch (kw) {
1520 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001521 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 +02001522 break;
1523 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001524 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 +02001525 break;
1526 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001527 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001528 break;
1529 default:
David Sedlákb3077192019-06-19 10:55:37 +02001530 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001531 return LY_EVALID;
1532 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001533 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001534 return ret;
1535}
1536
Michal Vaskoea5abea2018-09-18 13:10:54 +02001537/**
1538 * @brief Parse the anydata or anyxml statement.
1539 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001540 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001541 * @param[in,out] data Data to read from, always moved to currently handled character.
1542 * @param[in] kw Type of this particular keyword.
1543 * @param[in,out] siblings Siblings to add to.
1544 *
1545 * @return LY_ERR values.
1546 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02001547LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001548parse_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 +02001549{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001550 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001551 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001552 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001553 struct lysp_node *iter;
1554 struct lysp_node_anydata *any;
1555
1556 /* create structure */
1557 any = calloc(1, sizeof *any);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001558 LY_CHECK_ERR_RET(!any, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001559 any->nodetype = kw == YANG_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001560 any->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001561
1562 /* insert into siblings */
1563 if (!*siblings) {
1564 *siblings = (struct lysp_node *)any;
1565 } else {
1566 for (iter = *siblings; iter->next; iter = iter->next);
1567 iter->next = (struct lysp_node *)any;
1568 }
1569
1570 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001571 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02001572 INSERT_WORD(ctx, buf, any->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001573
1574 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01001575 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001576 switch (kw) {
1577 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001578 LY_CHECK_RET(parse_config(ctx, data, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001579 break;
1580 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001581 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 +02001582 break;
1583 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001584 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 +02001585 break;
1586 case YANG_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001587 LY_CHECK_RET(parse_mandatory(ctx, data, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001588 break;
1589 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001590 LY_CHECK_RET(parse_restrs(ctx, data, kw, &any->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001591 break;
1592 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001593 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 +02001594 break;
1595 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001596 LY_CHECK_RET(parse_status(ctx, data, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001597 break;
1598 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001599 LY_CHECK_RET(parse_when(ctx, data, &any->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001600 break;
1601 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001602 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001603 break;
1604 default:
David Sedlákb3077192019-06-19 10:55:37 +02001605 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
Radek Krejcic59bc972018-09-17 16:13:06 +02001606 (any->nodetype & LYS_ANYDATA) == LYS_ANYDATA ? ly_stmt2str(YANG_ANYDATA) : ly_stmt2str(YANG_ANYXML));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001607 return LY_EVALID;
1608 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001609 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001610 return ret;
1611}
1612
Michal Vaskoea5abea2018-09-18 13:10:54 +02001613/**
1614 * @brief Parse the value or position statement. Substatement of type enum statement.
1615 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001616 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001617 * @param[in,out] data Data to read from, always moved to currently handled character.
1618 * @param[in] val_kw Type of this particular keyword.
1619 * @param[in,out] value Value to write to.
1620 * @param[in,out] flags Flags to write to.
1621 * @param[in,out] exts Extension instances to add to.
1622 *
1623 * @return LY_ERR values.
1624 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001625static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001626parse_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 +02001627 struct lysp_ext_instance **exts)
1628{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001629 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001630 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001631 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001632 long int num;
1633 unsigned long int unum;
1634 enum yang_keyword kw;
1635
1636 if (*flags & LYS_SET_VALUE) {
David Sedlákb3077192019-06-19 10:55:37 +02001637 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001638 return LY_EVALID;
1639 }
1640 *flags |= LYS_SET_VALUE;
1641
1642 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001643 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001644
1645 if (!word_len || (word[0] == '+') || ((word[0] == '0') && (word_len > 1)) || ((val_kw == YANG_VALUE) && !strncmp(word, "-0", 2))) {
David Sedlákb3077192019-06-19 10:55:37 +02001646 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001647 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001648 }
1649
1650 errno = 0;
1651 if (val_kw == YANG_VALUE) {
1652 num = strtol(word, &ptr, 10);
Radek Krejci8b764662018-11-14 14:15:13 +01001653 if (num < INT64_C(-2147483648) || num > INT64_C(2147483647)) {
David Sedlákb3077192019-06-19 10:55:37 +02001654 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001655 goto error;
1656 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001657 } else {
1658 unum = strtoul(word, &ptr, 10);
Radek Krejci8b764662018-11-14 14:15:13 +01001659 if (unum > UINT64_C(4294967295)) {
David Sedlákb3077192019-06-19 10:55:37 +02001660 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001661 goto error;
1662 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001663 }
1664 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001665 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001666 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001667 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001668 }
1669 if (errno == ERANGE) {
David Sedlákb3077192019-06-19 10:55:37 +02001670 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001671 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001672 }
1673 if (val_kw == YANG_VALUE) {
1674 *value = num;
1675 } else {
1676 *value = unum;
1677 }
1678 free(buf);
1679
Radek Krejci6d6556c2018-11-08 09:37:45 +01001680 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001681 switch (kw) {
1682 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001683 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 +02001684 break;
1685 default:
David Sedlákb3077192019-06-19 10:55:37 +02001686 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001687 return LY_EVALID;
1688 }
1689 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001690 return ret;
Radek Krejci8b764662018-11-14 14:15:13 +01001691
1692error:
1693 free(buf);
1694 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001695}
1696
Michal Vaskoea5abea2018-09-18 13:10:54 +02001697/**
1698 * @brief Parse the enum or bit statement. Substatement of type statement.
1699 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001700 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001701 * @param[in,out] data Data to read from, always moved to currently handled character.
1702 * @param[in] enum_kw Type of this particular keyword.
1703 * @param[in,out] enums Enums or bits to add to.
1704 *
1705 * @return LY_ERR values.
1706 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001707static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001708parse_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 +02001709{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001710 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001711 char *buf, *word;
Radek Krejci8b764662018-11-14 14:15:13 +01001712 size_t word_len, u;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001713 enum yang_keyword kw;
1714 struct lysp_type_enum *enm;
1715
Radek Krejci2c4e7172018-10-19 15:56:26 +02001716 LY_ARRAY_NEW_RET(ctx->ctx, *enums, enm, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001717
1718 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001719 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 +01001720 if (enum_kw == YANG_ENUM) {
1721 if (!word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001722 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not be zero-length.");
Radek Krejci8b764662018-11-14 14:15:13 +01001723 free(buf);
1724 return LY_EVALID;
1725 } else if (isspace(word[0]) || isspace(word[word_len - 1])) {
David Sedlákb3077192019-06-19 10:55:37 +02001726 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not have any leading or trailing whitespaces (\"%.*s\").",
Radek Krejci8b764662018-11-14 14:15:13 +01001727 word_len, word);
1728 free(buf);
1729 return LY_EVALID;
1730 } else {
1731 for (u = 0; u < word_len; ++u) {
1732 if (iscntrl(word[u])) {
1733 LOGWRN(ctx->ctx, "Control characters in enum name should be avoided (\"%.*s\", character number %d).",
1734 word_len, word, u + 1);
1735 break;
1736 }
1737 }
1738 }
1739 } else { /* YANG_BIT */
1740
1741 }
Radek Krejcif09e4e82019-06-14 15:08:11 +02001742 if (enum_kw == YANG_ENUM) {
1743 YANG_CHECK_NONEMPTY(ctx, NULL, word_len, "enum");
1744 }
Radek Krejci44ceedc2018-10-02 15:54:31 +02001745 INSERT_WORD(ctx, buf, enm->name, word, word_len);
Radek Krejci8b764662018-11-14 14:15:13 +01001746 CHECK_UNIQUENESS(ctx, *enums, name, ly_stmt2str(enum_kw), enm->name);
1747
Radek Krejci6d6556c2018-11-08 09:37:45 +01001748 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001749 switch (kw) {
1750 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001751 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 +02001752 break;
1753 case YANG_IF_FEATURE:
Radek Krejciceaf2122019-01-02 15:03:26 +01001754 YANG_CHECK_STMTVER2_RET(ctx, "if-feature", ly_stmt2str(enum_kw));
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001755 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 +02001756 break;
1757 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001758 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 +02001759 break;
1760 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001761 LY_CHECK_RET(parse_status(ctx, data, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001762 break;
1763 case YANG_VALUE:
1764 case YANG_POSITION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001765 LY_CHECK_RET(parse_type_enum_value_pos(ctx, data, kw, &enm->value, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001766 break;
1767 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001768 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001769 break;
1770 default:
David Sedlákb3077192019-06-19 10:55:37 +02001771 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001772 return LY_EVALID;
1773 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001774 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001775 return ret;
1776}
1777
Michal Vaskoea5abea2018-09-18 13:10:54 +02001778/**
1779 * @brief Parse the fraction-digits statement. Substatement of type statement.
1780 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001781 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001782 * @param[in,out] data Data to read from, always moved to currently handled character.
1783 * @param[in,out] fracdig Value to write to.
1784 * @param[in,out] exts Extension instances to add to.
1785 *
1786 * @return LY_ERR values.
1787 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001788static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001789parse_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 +02001790{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001791 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001792 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001793 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001794 unsigned long int num;
1795 enum yang_keyword kw;
1796
1797 if (*fracdig) {
David Sedlákb3077192019-06-19 10:55:37 +02001798 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001799 return LY_EVALID;
1800 }
1801
1802 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001803 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001804
1805 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
David Sedlákb3077192019-06-19 10:55:37 +02001806 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001807 free(buf);
1808 return LY_EVALID;
1809 }
1810
1811 errno = 0;
1812 num = strtoul(word, &ptr, 10);
1813 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001814 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001815 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001816 free(buf);
1817 return LY_EVALID;
1818 }
1819 if ((errno == ERANGE) || (num > 18)) {
David Sedlákb3077192019-06-19 10:55:37 +02001820 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001821 free(buf);
1822 return LY_EVALID;
1823 }
1824 *fracdig = num;
1825 free(buf);
1826
Radek Krejci6d6556c2018-11-08 09:37:45 +01001827 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001828 switch (kw) {
1829 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001830 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_FRACDIGITS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001831 break;
1832 default:
David Sedlákb3077192019-06-19 10:55:37 +02001833 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001834 return LY_EVALID;
1835 }
1836 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001837 return ret;
1838}
1839
Michal Vaskoea5abea2018-09-18 13:10:54 +02001840/**
1841 * @brief Parse the require-instance statement. Substatement of type statement.
1842 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001843 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001844 * @param[in,out] data Data to read from, always moved to currently handled character.
1845 * @param[in,out] reqinst Value to write to.
1846 * @param[in,out] flags Flags to write to.
1847 * @param[in,out] exts Extension instances to add to.
1848 *
1849 * @return LY_ERR values.
1850 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001851static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001852parse_type_reqinstance(struct lys_parser_ctx *ctx, const char **data, uint8_t *reqinst, uint16_t *flags,
Michal Vasko7fbc8162018-09-17 10:35:16 +02001853 struct lysp_ext_instance **exts)
1854{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001855 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001856 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001857 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001858 enum yang_keyword kw;
1859
1860 if (*flags & LYS_SET_REQINST) {
David Sedlákb3077192019-06-19 10:55:37 +02001861 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001862 return LY_EVALID;
1863 }
1864 *flags |= LYS_SET_REQINST;
1865
1866 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001867 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001868
1869 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1870 *reqinst = 1;
1871 } else if ((word_len != 5) || strncmp(word, "false", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001872 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001873 free(buf);
1874 return LY_EVALID;
1875 }
1876 free(buf);
1877
Radek Krejci6d6556c2018-11-08 09:37:45 +01001878 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001879 switch (kw) {
1880 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001881 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_REQINSTANCE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001882 break;
1883 default:
David Sedlákb3077192019-06-19 10:55:37 +02001884 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001885 return LY_EVALID;
1886 }
1887 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001888 return ret;
1889}
1890
Michal Vaskoea5abea2018-09-18 13:10:54 +02001891/**
1892 * @brief Parse the modifier statement. Substatement of type pattern statement.
1893 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001894 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001895 * @param[in,out] data Data to read from, always moved to currently handled character.
1896 * @param[in,out] pat Value to write to.
1897 * @param[in,out] exts Extension instances to add to.
1898 *
1899 * @return LY_ERR values.
1900 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001901static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001902parse_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 +02001903{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001904 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001905 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001906 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001907 enum yang_keyword kw;
1908
1909 if ((*pat)[0] == 0x15) {
David Sedlákb3077192019-06-19 10:55:37 +02001910 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001911 return LY_EVALID;
1912 }
1913
1914 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001915 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001916
1917 if ((word_len != 12) || strncmp(word, "invert-match", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001918 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001919 free(buf);
1920 return LY_EVALID;
1921 }
1922 free(buf);
1923
1924 /* replace the value in the dictionary */
1925 buf = malloc(strlen(*pat) + 1);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001926 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001927 strcpy(buf, *pat);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001928 lydict_remove(ctx->ctx, *pat);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001929
1930 assert(buf[0] == 0x06);
1931 buf[0] = 0x15;
Radek Krejci44ceedc2018-10-02 15:54:31 +02001932 *pat = lydict_insert_zc(ctx->ctx, buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001933
Radek Krejci6d6556c2018-11-08 09:37:45 +01001934 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001935 switch (kw) {
1936 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001937 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MODIFIER, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001938 break;
1939 default:
David Sedlákb3077192019-06-19 10:55:37 +02001940 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001941 return LY_EVALID;
1942 }
1943 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001944 return ret;
1945}
1946
Michal Vaskoea5abea2018-09-18 13:10:54 +02001947/**
1948 * @brief Parse the pattern statement. Substatement of type statement.
1949 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001950 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001951 * @param[in,out] data Data to read from, always moved to currently handled character.
1952 * @param[in,out] patterns Restrictions to add to.
1953 *
1954 * @return LY_ERR values.
1955 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001956static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001957parse_type_pattern(struct lys_parser_ctx *ctx, const char **data, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001958{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001959 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001960 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001961 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001962 enum yang_keyword kw;
1963 struct lysp_restr *restr;
1964
Radek Krejci2c4e7172018-10-19 15:56:26 +02001965 LY_ARRAY_NEW_RET(ctx->ctx, *patterns, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001966
1967 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001968 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001969
1970 /* add special meaning first byte */
1971 if (buf) {
1972 buf = realloc(buf, word_len + 2);
1973 word = buf;
1974 } else {
1975 buf = malloc(word_len + 2);
1976 }
Radek Krejci44ceedc2018-10-02 15:54:31 +02001977 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejci86d106e2018-10-18 09:53:19 +02001978 memmove(buf + 1, word, word_len);
1979 buf[0] = 0x06; /* pattern's default regular-match flag */
1980 buf[word_len + 1] = '\0'; /* terminating NULL byte */
1981 restr->arg = lydict_insert_zc(ctx->ctx, buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001982
Radek Krejci6d6556c2018-11-08 09:37:45 +01001983 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001984 switch (kw) {
1985 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001986 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 +02001987 break;
1988 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001989 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 +02001990 break;
1991 case YANG_ERROR_APP_TAG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001992 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 +02001993 break;
1994 case YANG_ERROR_MESSAGE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001995 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 +02001996 break;
1997 case YANG_MODIFIER:
Radek Krejciceaf2122019-01-02 15:03:26 +01001998 YANG_CHECK_STMTVER2_RET(ctx, "modifier", "pattern");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001999 LY_CHECK_RET(parse_type_pattern_modifier(ctx, data, &restr->arg, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002000 break;
2001 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002002 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002003 break;
2004 default:
David Sedlákb3077192019-06-19 10:55:37 +02002005 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002006 return LY_EVALID;
2007 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002008 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002009 return ret;
2010}
2011
Michal Vaskoea5abea2018-09-18 13:10:54 +02002012/**
2013 * @brief Parse the type statement.
2014 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002015 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002016 * @param[in,out] data Data to read from, always moved to currently handled character.
2017 * @param[in,out] type Type to wrote to.
2018 *
2019 * @return LY_ERR values.
2020 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002021static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002022parse_type(struct lys_parser_ctx *ctx, const char **data, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002023{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002024 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002025 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002026 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002027 enum yang_keyword kw;
2028 struct lysp_type *nest_type;
2029
2030 if (type->name) {
David Sedlákb3077192019-06-19 10:55:37 +02002031 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002032 return LY_EVALID;
2033 }
2034
2035 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002036 LY_CHECK_RET(get_argument(ctx, data, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002037 INSERT_WORD(ctx, buf, type->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002038
Radek Krejci6d6556c2018-11-08 09:37:45 +01002039 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002040 switch (kw) {
2041 case YANG_BASE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002042 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 +01002043 type->flags |= LYS_SET_BASE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002044 break;
2045 case YANG_BIT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002046 LY_CHECK_RET(parse_type_enum(ctx, data, kw, &type->bits));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002047 type->flags |= LYS_SET_BIT;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002048 break;
2049 case YANG_ENUM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002050 LY_CHECK_RET(parse_type_enum(ctx, data, kw, &type->enums));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002051 type->flags |= LYS_SET_ENUM;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002052 break;
2053 case YANG_FRACTION_DIGITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002054 LY_CHECK_RET(parse_type_fracdigits(ctx, data, &type->fraction_digits, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002055 type->flags |= LYS_SET_FRDIGITS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002056 break;
2057 case YANG_LENGTH:
2058 if (type->length) {
David Sedlákb3077192019-06-19 10:55:37 +02002059 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002060 return LY_EVALID;
2061 }
2062 type->length = calloc(1, sizeof *type->length);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002063 LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002064
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002065 LY_CHECK_RET(parse_restr(ctx, data, kw, type->length));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002066 type->flags |= LYS_SET_LENGTH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002067 break;
2068 case YANG_PATH:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002069 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 +01002070 type->flags |= LYS_SET_PATH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002071 break;
2072 case YANG_PATTERN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002073 LY_CHECK_RET(parse_type_pattern(ctx, data, &type->patterns));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002074 type->flags |= LYS_SET_PATTERN;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002075 break;
2076 case YANG_RANGE:
2077 if (type->range) {
David Sedlákb3077192019-06-19 10:55:37 +02002078 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002079 return LY_EVALID;
2080 }
2081 type->range = calloc(1, sizeof *type->range);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002082 LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx->ctx), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002083
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002084 LY_CHECK_RET(parse_restr(ctx, data, kw, type->range));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002085 type->flags |= LYS_SET_RANGE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002086 break;
2087 case YANG_REQUIRE_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002088 LY_CHECK_RET(parse_type_reqinstance(ctx, data, &type->require_instance, &type->flags, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002089 /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002090 break;
2091 case YANG_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002092 LY_ARRAY_NEW_RET(ctx->ctx, type->types, nest_type, LY_EMEM);
2093 LY_CHECK_RET(parse_type(ctx, data, nest_type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002094 type->flags |= LYS_SET_TYPE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002095 break;
2096 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002097 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &type->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002098 break;
2099 default:
David Sedlákb3077192019-06-19 10:55:37 +02002100 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002101 return LY_EVALID;
2102 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002103 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002104 return ret;
2105}
2106
Michal Vaskoea5abea2018-09-18 13:10:54 +02002107/**
2108 * @brief Parse the leaf statement.
2109 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002110 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002111 * @param[in,out] data Data to read from, always moved to currently handled character.
2112 * @param[in,out] siblings Siblings to add to.
2113 *
2114 * @return LY_ERR values.
2115 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002116LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002117parse_leaf(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002118{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002119 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002120 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002121 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002122 enum yang_keyword kw;
2123 struct lysp_node *iter;
2124 struct lysp_node_leaf *leaf;
2125
2126 /* create structure */
2127 leaf = calloc(1, sizeof *leaf);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002128 LY_CHECK_ERR_RET(!leaf, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002129 leaf->nodetype = LYS_LEAF;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002130 leaf->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002131
2132 /* insert into siblings */
2133 if (!*siblings) {
2134 *siblings = (struct lysp_node *)leaf;
2135 } else {
2136 for (iter = *siblings; iter->next; iter = iter->next);
2137 iter->next = (struct lysp_node *)leaf;
2138 }
2139
2140 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002141 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002142 INSERT_WORD(ctx, buf, leaf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002143
2144 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01002145 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002146 switch (kw) {
2147 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002148 LY_CHECK_RET(parse_config(ctx, data, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002149 break;
2150 case YANG_DEFAULT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002151 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 +02002152 break;
2153 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002154 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 +02002155 break;
2156 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002157 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 +02002158 break;
2159 case YANG_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002160 LY_CHECK_RET(parse_mandatory(ctx, data, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002161 break;
2162 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002163 LY_CHECK_RET(parse_restrs(ctx, data, kw, &leaf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002164 break;
2165 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002166 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 +02002167 break;
2168 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002169 LY_CHECK_RET(parse_status(ctx, data, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002170 break;
2171 case YANG_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002172 LY_CHECK_RET(parse_type(ctx, data, &leaf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002173 break;
2174 case YANG_UNITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002175 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 +02002176 break;
2177 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002178 LY_CHECK_RET(parse_when(ctx, data, &leaf->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002179 break;
2180 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002181 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002182 break;
2183 default:
David Sedlákb3077192019-06-19 10:55:37 +02002184 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002185 return LY_EVALID;
2186 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002187 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002188 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002189checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002190 /* mandatory substatements */
2191 if (!leaf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002192 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002193 return LY_EVALID;
2194 }
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002195 if ((leaf->flags & LYS_MAND_TRUE) && (leaf->dflt)) {
David Sedlákb3077192019-06-19 10:55:37 +02002196 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMSCOMB, "mandatory", "default", "leaf");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002197 return LY_EVALID;
2198 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002199
2200 return ret;
2201}
2202
Michal Vaskoea5abea2018-09-18 13:10:54 +02002203/**
2204 * @brief Parse the max-elements statement.
2205 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002206 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002207 * @param[in,out] data Data to read from, always moved to currently handled character.
2208 * @param[in,out] max Value to write to.
2209 * @param[in,out] flags Flags to write to.
2210 * @param[in,out] exts Extension instances to add to.
2211 *
2212 * @return LY_ERR values.
2213 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002214LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002215parse_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 +02002216{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002217 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002218 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002219 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002220 unsigned long int num;
2221 enum yang_keyword kw;
2222
2223 if (*flags & LYS_SET_MAX) {
David Sedlákb3077192019-06-19 10:55:37 +02002224 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002225 return LY_EVALID;
2226 }
2227 *flags |= LYS_SET_MAX;
2228
2229 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002230 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002231
2232 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
David Sedlákb3077192019-06-19 10:55:37 +02002233 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002234 free(buf);
2235 return LY_EVALID;
2236 }
2237
2238 if (strncmp(word, "unbounded", word_len)) {
2239 errno = 0;
2240 num = strtoul(word, &ptr, 10);
2241 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002242 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002243 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002244 free(buf);
2245 return LY_EVALID;
2246 }
2247 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002248 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002249 free(buf);
2250 return LY_EVALID;
2251 }
2252
2253 *max = num;
2254 }
2255 free(buf);
2256
Radek Krejci6d6556c2018-11-08 09:37:45 +01002257 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002258 switch (kw) {
2259 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002260 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MAX, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002261 break;
2262 default:
David Sedlákb3077192019-06-19 10:55:37 +02002263 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002264 return LY_EVALID;
2265 }
2266 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002267 return ret;
2268}
2269
Michal Vaskoea5abea2018-09-18 13:10:54 +02002270/**
2271 * @brief Parse the min-elements statement.
2272 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002273 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002274 * @param[in,out] data Data to read from, always moved to currently handled character.
2275 * @param[in,out] min Value to write to.
2276 * @param[in,out] flags Flags to write to.
2277 * @param[in,out] exts Extension instances to add to.
2278 *
2279 * @return LY_ERR values.
2280 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002281LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002282parse_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 +02002283{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002284 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002285 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002286 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002287 unsigned long int num;
2288 enum yang_keyword kw;
2289
2290 if (*flags & LYS_SET_MIN) {
David Sedlákb3077192019-06-19 10:55:37 +02002291 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002292 return LY_EVALID;
2293 }
2294 *flags |= LYS_SET_MIN;
2295
2296 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002297 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002298
2299 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
David Sedlákb3077192019-06-19 10:55:37 +02002300 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002301 free(buf);
2302 return LY_EVALID;
2303 }
2304
2305 errno = 0;
2306 num = strtoul(word, &ptr, 10);
2307 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002308 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002309 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002310 free(buf);
2311 return LY_EVALID;
2312 }
2313 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002314 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002315 free(buf);
2316 return LY_EVALID;
2317 }
2318 *min = num;
2319 free(buf);
2320
Radek Krejci6d6556c2018-11-08 09:37:45 +01002321 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002322 switch (kw) {
2323 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002324 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MIN, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002325 break;
2326 default:
David Sedlákb3077192019-06-19 10:55:37 +02002327 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002328 return LY_EVALID;
2329 }
2330 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002331 return ret;
2332}
2333
Michal Vaskoea5abea2018-09-18 13:10:54 +02002334/**
2335 * @brief Parse the ordered-by statement.
2336 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002337 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002338 * @param[in,out] data Data to read from, always moved to currently handled character.
2339 * @param[in,out] flags Flags to write to.
2340 * @param[in,out] exts Extension instances to add to.
2341 *
2342 * @return LY_ERR values.
2343 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002344static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002345parse_orderedby(struct lys_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002346{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002347 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002348 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002349 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002350 enum yang_keyword kw;
2351
2352 if (*flags & LYS_ORDBY_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02002353 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002354 return LY_EVALID;
2355 }
2356
2357 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002358 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002359
2360 if ((word_len == 6) && !strncmp(word, "system", word_len)) {
2361 *flags |= LYS_ORDBY_SYSTEM;
2362 } else if ((word_len == 4) && !strncmp(word, "user", word_len)) {
2363 *flags |= LYS_ORDBY_USER;
2364 } else {
David Sedlákb3077192019-06-19 10:55:37 +02002365 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002366 free(buf);
2367 return LY_EVALID;
2368 }
2369 free(buf);
2370
Radek Krejci6d6556c2018-11-08 09:37:45 +01002371 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002372 switch (kw) {
2373 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002374 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_ORDEREDBY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002375 break;
2376 default:
David Sedlákb3077192019-06-19 10:55:37 +02002377 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002378 return LY_EVALID;
2379 }
2380 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002381 return ret;
2382}
2383
Michal Vaskoea5abea2018-09-18 13:10:54 +02002384/**
2385 * @brief Parse the leaf-list statement.
2386 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002387 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002388 * @param[in,out] data Data to read from, always moved to currently handled character.
2389 * @param[in,out] siblings Siblings to add to.
2390 *
2391 * @return LY_ERR values.
2392 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002393LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002394parse_leaflist(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002395{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002396 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002397 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002398 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002399 enum yang_keyword kw;
2400 struct lysp_node *iter;
2401 struct lysp_node_leaflist *llist;
2402
2403 /* create structure */
2404 llist = calloc(1, sizeof *llist);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002405 LY_CHECK_ERR_RET(!llist, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002406 llist->nodetype = LYS_LEAFLIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002407 llist->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002408
2409 /* insert into siblings */
2410 if (!*siblings) {
2411 *siblings = (struct lysp_node *)llist;
2412 } else {
2413 for (iter = *siblings; iter->next; iter = iter->next);
2414 iter->next = (struct lysp_node *)llist;
2415 }
2416
2417 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002418 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002419 INSERT_WORD(ctx, buf, llist->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002420
2421 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01002422 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002423 switch (kw) {
2424 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002425 LY_CHECK_RET(parse_config(ctx, data, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002426 break;
2427 case YANG_DEFAULT:
Radek Krejciceaf2122019-01-02 15:03:26 +01002428 YANG_CHECK_STMTVER2_RET(ctx, "default", "leaf-list");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002429 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 +02002430 break;
2431 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002432 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 +02002433 break;
2434 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002435 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 +02002436 break;
2437 case YANG_MAX_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002438 LY_CHECK_RET(parse_maxelements(ctx, data, &llist->max, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002439 break;
2440 case YANG_MIN_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002441 LY_CHECK_RET(parse_minelements(ctx, data, &llist->min, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002442 break;
2443 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002444 LY_CHECK_RET(parse_restrs(ctx, data, kw, &llist->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002445 break;
2446 case YANG_ORDERED_BY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002447 LY_CHECK_RET(parse_orderedby(ctx, data, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002448 break;
2449 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002450 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 +02002451 break;
2452 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002453 LY_CHECK_RET(parse_status(ctx, data, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002454 break;
2455 case YANG_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002456 LY_CHECK_RET(parse_type(ctx, data, &llist->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002457 break;
2458 case YANG_UNITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002459 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 +02002460 break;
2461 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002462 LY_CHECK_RET(parse_when(ctx, data, &llist->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002463 break;
2464 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002465 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002466 break;
2467 default:
David Sedlákb3077192019-06-19 10:55:37 +02002468 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002469 return LY_EVALID;
2470 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002471 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002472 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002473checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002474 /* mandatory substatements */
2475 if (!llist->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002476 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002477 return LY_EVALID;
2478 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01002479 if ((llist->min) && (llist->dflts)) {
David Sedlákb3077192019-06-19 10:55:37 +02002480 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMSCOMB, "min-elements", "default", "leaf-list");
Radek Krejci0e5d8382018-11-28 16:37:53 +01002481 return LY_EVALID;
2482 }
Radek Krejcidf6cad12018-11-28 17:10:55 +01002483 if (llist->max && llist->min > llist->max) {
David Sedlákb3077192019-06-19 10:55:37 +02002484 LOGVAL_PARSER(ctx, LYVE_SEMANTICS,
Radek Krejcidf6cad12018-11-28 17:10:55 +01002485 "Invalid combination of min-elements and max-elements: min value %u is bigger than the max value %u.",
2486 llist->min, llist->max);
2487 return LY_EVALID;
2488 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002489
2490 return ret;
2491}
2492
Michal Vaskoea5abea2018-09-18 13:10:54 +02002493/**
2494 * @brief Parse the refine statement.
2495 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002496 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002497 * @param[in,out] data Data to read from, always moved to currently handled character.
2498 * @param[in,out] refines Refines to add to.
2499 *
2500 * @return LY_ERR values.
2501 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002502static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002503parse_refine(struct lys_parser_ctx *ctx, const char **data, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002504{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002505 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002506 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002507 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002508 enum yang_keyword kw;
2509 struct lysp_refine *rf;
2510
Radek Krejci2c4e7172018-10-19 15:56:26 +02002511 LY_ARRAY_NEW_RET(ctx->ctx, *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002512
2513 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002514 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Radek Krejcif09e4e82019-06-14 15:08:11 +02002515 YANG_CHECK_NONEMPTY(ctx, NULL, word_len, "refine");
Radek Krejci44ceedc2018-10-02 15:54:31 +02002516 INSERT_WORD(ctx, buf, rf->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002517
Radek Krejci6d6556c2018-11-08 09:37:45 +01002518 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002519 switch (kw) {
2520 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002521 LY_CHECK_RET(parse_config(ctx, data, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002522 break;
2523 case YANG_DEFAULT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002524 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 +02002525 break;
2526 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002527 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 +02002528 break;
2529 case YANG_IF_FEATURE:
Radek Krejciceaf2122019-01-02 15:03:26 +01002530 YANG_CHECK_STMTVER2_RET(ctx, "if-feature", "refine");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002531 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 +02002532 break;
2533 case YANG_MAX_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002534 LY_CHECK_RET(parse_maxelements(ctx, data, &rf->max, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002535 break;
2536 case YANG_MIN_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002537 LY_CHECK_RET(parse_minelements(ctx, data, &rf->min, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002538 break;
2539 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002540 LY_CHECK_RET(parse_restrs(ctx, data, kw, &rf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002541 break;
2542 case YANG_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002543 LY_CHECK_RET(parse_mandatory(ctx, data, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002544 break;
2545 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002546 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 +02002547 break;
2548 case YANG_PRESENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002549 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 +02002550 break;
2551 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002552 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002553 break;
2554 default:
David Sedlákb3077192019-06-19 10:55:37 +02002555 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002556 return LY_EVALID;
2557 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002558 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002559 return ret;
2560}
2561
Michal Vaskoea5abea2018-09-18 13:10:54 +02002562/**
2563 * @brief Parse the typedef statement.
2564 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002565 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002566 * @param[in,out] data Data to read from, always moved to currently handled character.
2567 * @param[in,out] typedefs Typedefs to add to.
2568 *
2569 * @return LY_ERR values.
2570 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002571static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002572parse_typedef(struct lys_parser_ctx *ctx, struct lysp_node *parent, const char **data, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002573{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002574 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002575 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002576 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002577 enum yang_keyword kw;
2578 struct lysp_tpdf *tpdf;
2579
Radek Krejci2c4e7172018-10-19 15:56:26 +02002580 LY_ARRAY_NEW_RET(ctx->ctx, *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002581
2582 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002583 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002584 INSERT_WORD(ctx, buf, tpdf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002585
2586 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01002587 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002588 switch (kw) {
2589 case YANG_DEFAULT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002590 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 +02002591 break;
2592 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002593 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 +02002594 break;
2595 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002596 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 +02002597 break;
2598 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002599 LY_CHECK_RET(parse_status(ctx, data, &tpdf->flags, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002600 break;
2601 case YANG_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002602 LY_CHECK_RET(parse_type(ctx, data, &tpdf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002603 break;
2604 case YANG_UNITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002605 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 +02002606 break;
2607 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002608 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002609 break;
2610 default:
David Sedlákb3077192019-06-19 10:55:37 +02002611 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002612 return LY_EVALID;
2613 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002614 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002615 LY_CHECK_RET(ret);
Radek Krejcibbe09a92018-11-08 09:36:54 +01002616checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002617 /* mandatory substatements */
2618 if (!tpdf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002619 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002620 return LY_EVALID;
2621 }
2622
Radek Krejcibbe09a92018-11-08 09:36:54 +01002623 /* store data for collision check */
Radek Krejci7fc68292019-06-12 13:51:09 +02002624 if (parent && !(parent->nodetype & (LYS_GROUPING | LYS_ACTION | LYS_INOUT | LYS_NOTIF))) {
Radek Krejcibbe09a92018-11-08 09:36:54 +01002625 ly_set_add(&ctx->tpdfs_nodes, parent, 0);
2626 }
2627
Michal Vasko7fbc8162018-09-17 10:35:16 +02002628 return ret;
2629}
2630
Michal Vaskoea5abea2018-09-18 13:10:54 +02002631/**
2632 * @brief Parse the input or output statement.
2633 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002634 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002635 * @param[in,out] data Data to read from, always moved to currently handled character.
2636 * @param[in] kw Type of this particular keyword
2637 * @param[in,out] inout_p Input/output pointer to write to.
2638 *
2639 * @return LY_ERR values.
2640 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002641static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002642parse_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 +02002643{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002644 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002645 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002646 size_t word_len;
Radek Krejci10113652018-11-14 16:56:50 +01002647 enum yang_keyword kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002648
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002649 if (inout_p->nodetype) {
David Sedlákb3077192019-06-19 10:55:37 +02002650 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002651 return LY_EVALID;
2652 }
2653
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002654 /* initiate structure */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002655 inout_p->nodetype = &((struct lysp_action*)parent)->input == inout_p ? LYS_INPUT : LYS_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002656 inout_p->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002657
2658 /* parse substatements */
Radek Krejci7fc68292019-06-12 13:51:09 +02002659 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002660 switch (kw) {
2661 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01002662 YANG_CHECK_STMTVER2_RET(ctx, "anydata", ly_stmt2str(inout_kw));
Radek Krejci10113652018-11-14 16:56:50 +01002663 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002664 case YANG_ANYXML:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002665 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002666 break;
2667 case YANG_CHOICE:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002668 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002669 break;
2670 case YANG_CONTAINER:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002671 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002672 break;
2673 case YANG_LEAF:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002674 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002675 break;
2676 case YANG_LEAF_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002677 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002678 break;
2679 case YANG_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002680 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002681 break;
2682 case YANG_USES:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002683 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002684 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002685 case YANG_TYPEDEF:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002686 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)inout_p, data, &inout_p->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002687 break;
2688 case YANG_MUST:
Radek Krejciceaf2122019-01-02 15:03:26 +01002689 YANG_CHECK_STMTVER2_RET(ctx, "must", ly_stmt2str(inout_kw));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002690 LY_CHECK_RET(parse_restrs(ctx, data, kw, &inout_p->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002691 break;
2692 case YANG_GROUPING:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002693 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)inout_p, &inout_p->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002694 break;
2695 case YANG_CUSTOM:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002696 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inout_p->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002697 break;
2698 default:
David Sedlákb3077192019-06-19 10:55:37 +02002699 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002700 return LY_EVALID;
2701 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002702 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002703 LY_CHECK_RET(ret);
2704checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002705 /* finalize parent pointers to the reallocated items */
Radek Krejci7fc68292019-06-12 13:51:09 +02002706 LY_CHECK_RET(parse_finalize_reallocated(ctx, inout_p->groupings, NULL, NULL, NULL));
2707
Michal Vasko7fbc8162018-09-17 10:35:16 +02002708 return ret;
2709}
2710
Michal Vaskoea5abea2018-09-18 13:10:54 +02002711/**
2712 * @brief Parse the action statement.
2713 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002714 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002715 * @param[in,out] data Data to read from, always moved to currently handled character.
2716 * @param[in,out] actions Actions to add to.
2717 *
2718 * @return LY_ERR values.
2719 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002720LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002721parse_action(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002722{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002723 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002724 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002725 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002726 enum yang_keyword kw;
2727 struct lysp_action *act;
2728
Radek Krejci2c4e7172018-10-19 15:56:26 +02002729 LY_ARRAY_NEW_RET(ctx->ctx, *actions, act, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002730
2731 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002732 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002733 INSERT_WORD(ctx, buf, act->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002734 act->nodetype = LYS_ACTION;
2735 act->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002736
Radek Krejci7fc68292019-06-12 13:51:09 +02002737 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002738 switch (kw) {
2739 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002740 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 +02002741 break;
2742 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002743 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 +02002744 break;
2745 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002746 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 +02002747 break;
2748 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002749 LY_CHECK_RET(parse_status(ctx, data, &act->flags, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002750 break;
2751
2752 case YANG_INPUT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002753 LY_CHECK_RET(parse_inout(ctx, data, kw, (struct lysp_node*)act, &act->input));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002754 break;
2755 case YANG_OUTPUT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002756 LY_CHECK_RET(parse_inout(ctx, data, kw, (struct lysp_node*)act, &act->output));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002757 break;
2758
2759 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01002760 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)act, data, &act->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002761 break;
2762 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002763 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)act, &act->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002764 break;
2765 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002766 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002767 break;
2768 default:
David Sedlákb3077192019-06-19 10:55:37 +02002769 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), parent ? "action" : "rpc");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002770 return LY_EVALID;
2771 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002772 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002773 LY_CHECK_RET(ret);
2774checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002775 /* finalize parent pointers to the reallocated items */
Radek Krejci7fc68292019-06-12 13:51:09 +02002776 LY_CHECK_RET(parse_finalize_reallocated(ctx, act->groupings, NULL, NULL, NULL));
2777
Michal Vasko7fbc8162018-09-17 10:35:16 +02002778 return ret;
2779}
2780
Michal Vaskoea5abea2018-09-18 13:10:54 +02002781/**
2782 * @brief Parse the notification statement.
2783 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002784 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002785 * @param[in,out] data Data to read from, always moved to currently handled character.
2786 * @param[in,out] notifs Notifications to add to.
2787 *
2788 * @return LY_ERR values.
2789 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002790LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002791parse_notif(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002792{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002793 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002794 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002795 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002796 enum yang_keyword kw;
2797 struct lysp_notif *notif;
2798
Radek Krejci2c4e7172018-10-19 15:56:26 +02002799 LY_ARRAY_NEW_RET(ctx->ctx, *notifs, notif, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002800
2801 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002802 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002803 INSERT_WORD(ctx, buf, notif->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002804 notif->nodetype = LYS_NOTIF;
2805 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002806
Radek Krejci7fc68292019-06-12 13:51:09 +02002807 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002808 switch (kw) {
2809 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002810 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 +02002811 break;
2812 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002813 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 +02002814 break;
2815 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002816 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 +02002817 break;
2818 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002819 LY_CHECK_RET(parse_status(ctx, data, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002820 break;
2821
2822 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01002823 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "notification");
Radek Krejci10113652018-11-14 16:56:50 +01002824 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002825 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002826 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002827 break;
2828 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002829 LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002830 break;
2831 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002832 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002833 break;
2834 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002835 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002836 break;
2837 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002838 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002839 break;
2840 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002841 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002842 break;
2843 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002844 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002845 break;
2846
2847 case YANG_MUST:
Radek Krejciceaf2122019-01-02 15:03:26 +01002848 YANG_CHECK_STMTVER2_RET(ctx, "must", "notification");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002849 LY_CHECK_RET(parse_restrs(ctx, data, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002850 break;
2851 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01002852 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)notif, data, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002853 break;
2854 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002855 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002856 break;
2857 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002858 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002859 break;
2860 default:
David Sedlákb3077192019-06-19 10:55:37 +02002861 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002862 return LY_EVALID;
2863 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002864 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002865 LY_CHECK_RET(ret);
2866checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002867 /* finalize parent pointers to the reallocated items */
Radek Krejci7fc68292019-06-12 13:51:09 +02002868 LY_CHECK_RET(parse_finalize_reallocated(ctx, notif->groupings, NULL, NULL, NULL));
2869
Michal Vasko7fbc8162018-09-17 10:35:16 +02002870 return ret;
2871}
2872
Michal Vaskoea5abea2018-09-18 13:10:54 +02002873/**
2874 * @brief Parse the grouping statement.
2875 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002876 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002877 * @param[in,out] data Data to read from, always moved to currently handled character.
2878 * @param[in,out] groupings Groupings to add to.
2879 *
2880 * @return LY_ERR values.
2881 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002882LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002883parse_grouping(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002884{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002885 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002886 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002887 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002888 enum yang_keyword kw;
2889 struct lysp_grp *grp;
2890
Radek Krejci2c4e7172018-10-19 15:56:26 +02002891 LY_ARRAY_NEW_RET(ctx->ctx, *groupings, grp, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002892
2893 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002894 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002895 INSERT_WORD(ctx, buf, grp->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002896 grp->nodetype = LYS_GROUPING;
2897 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002898
Radek Krejci7fc68292019-06-12 13:51:09 +02002899 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002900 switch (kw) {
2901 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002902 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 +02002903 break;
2904 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002905 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 +02002906 break;
2907 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002908 LY_CHECK_RET(parse_status(ctx, data, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002909 break;
2910
2911 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01002912 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "grouping");
Radek Krejci10113652018-11-14 16:56:50 +01002913 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002914 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002915 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002916 break;
2917 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002918 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002919 break;
2920 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002921 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002922 break;
2923 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002924 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002925 break;
2926 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002927 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002928 break;
2929 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002930 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002931 break;
2932 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002933 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002934 break;
2935
2936 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01002937 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)grp, data, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002938 break;
2939 case YANG_ACTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01002940 YANG_CHECK_STMTVER2_RET(ctx, "action", "grouping");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002941 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)grp, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002942 break;
2943 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002944 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)grp, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002945 break;
2946 case YANG_NOTIFICATION:
Radek Krejciceaf2122019-01-02 15:03:26 +01002947 YANG_CHECK_STMTVER2_RET(ctx, "notification", "grouping");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002948 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)grp, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002949 break;
2950 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002951 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002952 break;
2953 default:
David Sedlákb3077192019-06-19 10:55:37 +02002954 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002955 return LY_EVALID;
2956 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002957 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002958 LY_CHECK_RET(ret);
2959checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002960 /* finalize parent pointers to the reallocated items */
Radek Krejci7fc68292019-06-12 13:51:09 +02002961 LY_CHECK_RET(parse_finalize_reallocated(ctx, grp->groupings, NULL, grp->actions, grp->notifs));
2962
Michal Vasko7fbc8162018-09-17 10:35:16 +02002963 return ret;
2964}
2965
Michal Vaskoea5abea2018-09-18 13:10:54 +02002966/**
2967 * @brief Parse the refine statement.
2968 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002969 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002970 * @param[in,out] data Data to read from, always moved to currently handled character.
2971 * @param[in,out] augments Augments to add to.
2972 *
2973 * @return LY_ERR values.
2974 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002975LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002976parse_augment(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002977{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002978 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002979 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002980 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002981 enum yang_keyword kw;
2982 struct lysp_augment *aug;
2983
Radek Krejci2c4e7172018-10-19 15:56:26 +02002984 LY_ARRAY_NEW_RET(ctx->ctx, *augments, aug, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002985
2986 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002987 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Radek Krejcif09e4e82019-06-14 15:08:11 +02002988 YANG_CHECK_NONEMPTY(ctx, NULL, word_len, "augment");
Radek Krejci44ceedc2018-10-02 15:54:31 +02002989 INSERT_WORD(ctx, buf, aug->nodeid, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002990 aug->nodetype = LYS_AUGMENT;
2991 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002992
Radek Krejci7fc68292019-06-12 13:51:09 +02002993 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002994 switch (kw) {
2995 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002996 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 +02002997 break;
2998 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002999 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 +02003000 break;
3001 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003002 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 +02003003 break;
3004 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003005 LY_CHECK_RET(parse_status(ctx, data, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003006 break;
3007 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003008 LY_CHECK_RET(parse_when(ctx, data, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003009 break;
3010
3011 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01003012 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "augment");
Radek Krejci10113652018-11-14 16:56:50 +01003013 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003014 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003015 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003016 break;
3017 case YANG_CASE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003018 LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003019 break;
3020 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003021 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003022 break;
3023 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003024 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003025 break;
3026 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003027 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003028 break;
3029 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003030 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003031 break;
3032 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003033 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003034 break;
3035 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003036 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003037 break;
3038
3039 case YANG_ACTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003040 YANG_CHECK_STMTVER2_RET(ctx, "action", "augment");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003041 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003042 break;
3043 case YANG_NOTIFICATION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003044 YANG_CHECK_STMTVER2_RET(ctx, "notification", "augment");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003045 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003046 break;
3047 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003048 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003049 break;
3050 default:
David Sedlákb3077192019-06-19 10:55:37 +02003051 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003052 return LY_EVALID;
3053 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003054 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003055 LY_CHECK_RET(ret);
3056checks:
3057 /* finalize parent pointers to the reallocated items */
3058 LY_CHECK_RET(parse_finalize_reallocated(ctx, NULL, NULL, aug->actions, aug->notifs));
3059
Michal Vasko7fbc8162018-09-17 10:35:16 +02003060 return ret;
3061}
3062
Michal Vaskoea5abea2018-09-18 13:10:54 +02003063/**
Radek Krejci7fc68292019-06-12 13:51:09 +02003064 * @brief Finalize some of the structures in case they are stored in sized array,
3065 * which can be possibly reallocated and some other data may point to them.
3066 *
3067 * Update parent pointers in the nodes inside grouping/augment/RPC/Notification, which could be reallocated.
3068 *
3069 * @param[in] mod Parsed module to be updated.
3070 * @return LY_ERR value (currently only LY_SUCCESS, but it can change in future).
3071 */
3072static LY_ERR
3073parse_finalize_reallocated(struct lys_parser_ctx *ctx, struct lysp_grp *groupings, struct lysp_augment *augments,
3074 struct lysp_action *actions, struct lysp_notif *notifs)
3075{
3076 unsigned int u, v;
3077 struct lysp_node *child;
3078
3079 /* finalize parent pointers to the reallocated items */
3080
3081 /* gropings */
3082 LY_ARRAY_FOR(groupings, u) {
3083 LY_LIST_FOR(groupings[u].data, child) {
3084 child->parent = (struct lysp_node*)&groupings[u];
3085 }
3086 LY_ARRAY_FOR(groupings[u].actions, v) {
3087 groupings[u].actions[v].parent = (struct lysp_node*)&groupings[u];
3088 }
3089 LY_ARRAY_FOR(groupings[u].notifs, v) {
3090 groupings[u].notifs[v].parent = (struct lysp_node*)&groupings[u];
3091 }
3092 LY_ARRAY_FOR(groupings[u].groupings, v) {
3093 groupings[u].groupings[v].parent = (struct lysp_node*)&groupings[u];
3094 }
3095 if (groupings[u].typedefs) {
3096 ly_set_add(&ctx->tpdfs_nodes, &groupings[u], 0);
3097 }
3098 }
3099
3100 /* augments */
3101 LY_ARRAY_FOR(augments, u) {
3102 LY_LIST_FOR(augments[u].child, child) {
3103 child->parent = (struct lysp_node*)&augments[u];
3104 }
3105 LY_ARRAY_FOR(augments[u].actions, v) {
3106 augments[u].actions[v].parent = (struct lysp_node*)&augments[u];
3107 }
3108 LY_ARRAY_FOR(augments[u].notifs, v) {
3109 augments[u].notifs[v].parent = (struct lysp_node*)&augments[u];
3110 }
3111 }
3112
3113 /* actions */
3114 LY_ARRAY_FOR(actions, u) {
3115 if (actions[u].input.parent) {
3116 actions[u].input.parent = (struct lysp_node*)&actions[u];
3117 LY_LIST_FOR(actions[u].input.data, child) {
3118 child->parent = (struct lysp_node*)&actions[u].input;
3119 }
3120 LY_ARRAY_FOR(actions[u].input.groupings, v) {
3121 actions[u].input.groupings[v].parent = (struct lysp_node*)&actions[u].input;
3122 }
3123 if (actions[u].input.typedefs) {
3124 ly_set_add(&ctx->tpdfs_nodes, &actions[u].input, 0);
3125 }
3126 }
3127 if (actions[u].output.parent) {
3128 actions[u].output.parent = (struct lysp_node*)&actions[u];
3129 LY_LIST_FOR(actions[u].output.data, child) {
3130 child->parent = (struct lysp_node*)&actions[u].output;
3131 }
3132 LY_ARRAY_FOR(actions[u].output.groupings, v) {
3133 actions[u].output.groupings[v].parent = (struct lysp_node*)&actions[u].output;
3134 }
3135 if (actions[u].output.typedefs) {
3136 ly_set_add(&ctx->tpdfs_nodes, &actions[u].output, 0);
3137 }
3138 }
3139 LY_ARRAY_FOR(actions[u].groupings, v) {
3140 actions[u].groupings[v].parent = (struct lysp_node*)&actions[u];
3141 }
3142 if (actions[u].typedefs) {
3143 ly_set_add(&ctx->tpdfs_nodes, &actions[u], 0);
3144 }
3145 }
3146
3147 /* notifications */
3148 LY_ARRAY_FOR(notifs, u) {
3149 LY_LIST_FOR(notifs[u].data, child) {
3150 child->parent = (struct lysp_node*)&notifs[u];
3151 }
3152 LY_ARRAY_FOR(notifs[u].groupings, v) {
3153 notifs[u].groupings[v].parent = (struct lysp_node*)&notifs[u];
3154 }
3155 if (notifs[u].typedefs) {
3156 ly_set_add(&ctx->tpdfs_nodes, &notifs[u], 0);
3157 }
3158 }
3159
3160 return LY_SUCCESS;
3161}
3162
3163/**
Michal Vaskoea5abea2018-09-18 13:10:54 +02003164 * @brief Parse the uses statement.
3165 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003166 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003167 * @param[in,out] data Data to read from, always moved to currently handled character.
3168 * @param[in,out] siblings Siblings to add to.
3169 *
3170 * @return LY_ERR values.
3171 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003172LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003173parse_uses(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003174{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003175 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003176 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003177 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003178 enum yang_keyword kw;
3179 struct lysp_node *iter;
3180 struct lysp_node_uses *uses;
3181
3182 /* create structure */
3183 uses = calloc(1, sizeof *uses);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003184 LY_CHECK_ERR_RET(!uses, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003185 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003186 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003187
3188 /* insert into siblings */
3189 if (!*siblings) {
3190 *siblings = (struct lysp_node *)uses;
3191 } else {
3192 for (iter = *siblings; iter->next; iter = iter->next);
3193 iter->next = (struct lysp_node *)uses;
3194 }
3195
3196 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003197 LY_CHECK_RET(get_argument(ctx, data, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003198 INSERT_WORD(ctx, buf, uses->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003199
3200 /* parse substatements */
Radek Krejci7fc68292019-06-12 13:51:09 +02003201 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003202 switch (kw) {
3203 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003204 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 +02003205 break;
3206 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003207 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 +02003208 break;
3209 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003210 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 +02003211 break;
3212 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003213 LY_CHECK_RET(parse_status(ctx, data, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003214 break;
3215 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003216 LY_CHECK_RET(parse_when(ctx, data, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003217 break;
3218
3219 case YANG_REFINE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003220 LY_CHECK_RET(parse_refine(ctx, data, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003221 break;
3222 case YANG_AUGMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003223 LY_CHECK_RET(parse_augment(ctx, data, (struct lysp_node*)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003224 break;
3225 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003226 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003227 break;
3228 default:
David Sedlákb3077192019-06-19 10:55:37 +02003229 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003230 return LY_EVALID;
3231 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003232 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003233checks:
3234 /* finalize parent pointers to the reallocated items */
3235 LY_CHECK_RET(parse_finalize_reallocated(ctx, NULL, uses->augments, NULL, NULL));
3236
Michal Vasko7fbc8162018-09-17 10:35:16 +02003237 return ret;
3238}
3239
Michal Vaskoea5abea2018-09-18 13:10:54 +02003240/**
3241 * @brief Parse the case statement.
3242 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003243 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003244 * @param[in,out] data Data to read from, always moved to currently handled character.
3245 * @param[in,out] siblings Siblings to add to.
3246 *
3247 * @return LY_ERR values.
3248 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003249LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003250parse_case(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003251{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003252 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003253 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003254 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003255 enum yang_keyword kw;
3256 struct lysp_node *iter;
3257 struct lysp_node_case *cas;
3258
3259 /* create structure */
3260 cas = calloc(1, sizeof *cas);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003261 LY_CHECK_ERR_RET(!cas, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003262 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003263 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003264
3265 /* insert into siblings */
3266 if (!*siblings) {
3267 *siblings = (struct lysp_node *)cas;
3268 } else {
3269 for (iter = *siblings; iter->next; iter = iter->next);
3270 iter->next = (struct lysp_node *)cas;
3271 }
3272
3273 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003274 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003275 INSERT_WORD(ctx, buf, cas->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003276
3277 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01003278 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003279 switch (kw) {
3280 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003281 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 +02003282 break;
3283 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003284 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 +02003285 break;
3286 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003287 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 +02003288 break;
3289 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003290 LY_CHECK_RET(parse_status(ctx, data, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003291 break;
3292 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003293 LY_CHECK_RET(parse_when(ctx, data, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003294 break;
3295
3296 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01003297 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "case");
Radek Krejci10113652018-11-14 16:56:50 +01003298 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003299 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003300 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003301 break;
3302 case YANG_CHOICE:
Radek Krejcia9026eb2018-12-12 16:04:47 +01003303 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003304 break;
3305 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003306 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003307 break;
3308 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003309 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003310 break;
3311 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003312 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003313 break;
3314 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003315 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003316 break;
3317 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003318 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003319 break;
3320 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003321 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003322 break;
3323 default:
David Sedlákb3077192019-06-19 10:55:37 +02003324 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003325 return LY_EVALID;
3326 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003327 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003328 return ret;
3329}
3330
Michal Vaskoea5abea2018-09-18 13:10:54 +02003331/**
3332 * @brief Parse the choice statement.
3333 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003334 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003335 * @param[in,out] data Data to read from, always moved to currently handled character.
3336 * @param[in,out] siblings Siblings to add to.
3337 *
3338 * @return LY_ERR values.
3339 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003340LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003341parse_choice(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003342{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003343 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003344 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003345 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003346 enum yang_keyword kw;
3347 struct lysp_node *iter;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003348 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003349
3350 /* create structure */
Radek Krejci44ceedc2018-10-02 15:54:31 +02003351 choice = calloc(1, sizeof *choice);
3352 LY_CHECK_ERR_RET(!choice, LOGMEM(ctx->ctx), LY_EMEM);
3353 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003354 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003355
3356 /* insert into siblings */
3357 if (!*siblings) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02003358 *siblings = (struct lysp_node *)choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003359 } else {
3360 for (iter = *siblings; iter->next; iter = iter->next);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003361 iter->next = (struct lysp_node *)choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003362 }
3363
3364 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003365 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003366 INSERT_WORD(ctx, buf, choice->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003367
3368 /* parse substatements */
Radek Krejcia9026eb2018-12-12 16:04:47 +01003369 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003370 switch (kw) {
3371 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003372 LY_CHECK_RET(parse_config(ctx, data, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003373 break;
3374 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003375 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 +02003376 break;
3377 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003378 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 +02003379 break;
3380 case YANG_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003381 LY_CHECK_RET(parse_mandatory(ctx, data, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003382 break;
3383 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003384 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 +02003385 break;
3386 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003387 LY_CHECK_RET(parse_status(ctx, data, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003388 break;
3389 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003390 LY_CHECK_RET(parse_when(ctx, data, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003391 break;
3392 case YANG_DEFAULT:
Radek Krejcia9026eb2018-12-12 16:04:47 +01003393 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 +02003394 break;
3395
3396 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01003397 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "choice");
Radek Krejci10113652018-11-14 16:56:50 +01003398 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003399 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003400 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003401 break;
3402 case YANG_CASE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003403 LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003404 break;
3405 case YANG_CHOICE:
Radek Krejciceaf2122019-01-02 15:03:26 +01003406 YANG_CHECK_STMTVER2_RET(ctx, "choice", "choice");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003407 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003408 break;
3409 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003410 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003411 break;
3412 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003413 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003414 break;
3415 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003416 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003417 break;
3418 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003419 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003420 break;
3421 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003422 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003423 break;
3424 default:
David Sedlákb3077192019-06-19 10:55:37 +02003425 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003426 return LY_EVALID;
3427 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003428 }
Radek Krejcia9026eb2018-12-12 16:04:47 +01003429 LY_CHECK_RET(ret);
3430checks:
3431 if ((choice->flags & LYS_MAND_TRUE) && choice->dflt) {
David Sedlákb3077192019-06-19 10:55:37 +02003432 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMSCOMB, "mandatory", "default", "choice");
Radek Krejcia9026eb2018-12-12 16:04:47 +01003433 return LY_EVALID;
3434 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003435 return ret;
3436}
3437
Michal Vaskoea5abea2018-09-18 13:10:54 +02003438/**
3439 * @brief Parse the container statement.
3440 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003441 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003442 * @param[in,out] data Data to read from, always moved to currently handled character.
3443 * @param[in,out] siblings Siblings to add to.
3444 *
3445 * @return LY_ERR values.
3446 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003447LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003448parse_container(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003449{
3450 LY_ERR ret = 0;
3451 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003452 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003453 enum yang_keyword kw;
3454 struct lysp_node *iter;
3455 struct lysp_node_container *cont;
3456
3457 /* create structure */
3458 cont = calloc(1, sizeof *cont);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003459 LY_CHECK_ERR_RET(!cont, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003460 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003461 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003462
3463 /* insert into siblings */
3464 if (!*siblings) {
3465 *siblings = (struct lysp_node *)cont;
3466 } else {
3467 for (iter = *siblings; iter->next; iter = iter->next);
3468 iter->next = (struct lysp_node *)cont;
3469 }
3470
3471 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003472 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003473 INSERT_WORD(ctx, buf, cont->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003474
3475 /* parse substatements */
Radek Krejci7fc68292019-06-12 13:51:09 +02003476 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003477 switch (kw) {
3478 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003479 LY_CHECK_RET(parse_config(ctx, data, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003480 break;
3481 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003482 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 +02003483 break;
3484 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003485 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 +02003486 break;
3487 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003488 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 +02003489 break;
3490 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003491 LY_CHECK_RET(parse_status(ctx, data, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003492 break;
3493 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003494 LY_CHECK_RET(parse_when(ctx, data, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003495 break;
3496 case YANG_PRESENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003497 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 +02003498 break;
3499
3500 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01003501 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "container");
Radek Krejci10113652018-11-14 16:56:50 +01003502 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003503 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003504 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003505 break;
3506 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003507 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003508 break;
3509 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003510 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003511 break;
3512 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003513 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003514 break;
3515 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003516 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003517 break;
3518 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003519 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003520 break;
3521 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003522 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003523 break;
3524
3525 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01003526 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)cont, data, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003527 break;
3528 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003529 LY_CHECK_RET(parse_restrs(ctx, data, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003530 break;
3531 case YANG_ACTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003532 YANG_CHECK_STMTVER2_RET(ctx, "action", "container");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003533 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003534 break;
3535 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003536 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003537 break;
3538 case YANG_NOTIFICATION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003539 YANG_CHECK_STMTVER2_RET(ctx, "notification", "container");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003540 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003541 break;
3542 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003543 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003544 break;
3545 default:
David Sedlákb3077192019-06-19 10:55:37 +02003546 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003547 return LY_EVALID;
3548 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003549 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003550checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01003551 /* finalize parent pointers to the reallocated items */
Radek Krejci7fc68292019-06-12 13:51:09 +02003552 LY_CHECK_RET(parse_finalize_reallocated(ctx, cont->groupings, NULL, cont->actions, cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003553 return ret;
3554}
3555
Michal Vaskoea5abea2018-09-18 13:10:54 +02003556/**
3557 * @brief Parse the list statement.
3558 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003559 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003560 * @param[in,out] data Data to read from, always moved to currently handled character.
3561 * @param[in,out] siblings Siblings to add to.
3562 *
3563 * @return LY_ERR values.
3564 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003565LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003566parse_list(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003567{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003568 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003569 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003570 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003571 enum yang_keyword kw;
3572 struct lysp_node *iter;
3573 struct lysp_node_list *list;
3574
3575 /* create structure */
3576 list = calloc(1, sizeof *list);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003577 LY_CHECK_ERR_RET(!list, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003578 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003579 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003580
3581 /* insert into siblings */
3582 if (!*siblings) {
3583 *siblings = (struct lysp_node *)list;
3584 } else {
3585 for (iter = *siblings; iter->next; iter = iter->next);
3586 iter->next = (struct lysp_node *)list;
3587 }
3588
3589 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003590 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003591 INSERT_WORD(ctx, buf, list->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003592
3593 /* parse substatements */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003594 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003595 switch (kw) {
3596 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003597 LY_CHECK_RET(parse_config(ctx, data, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003598 break;
3599 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003600 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 +02003601 break;
3602 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003603 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 +02003604 break;
3605 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003606 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 +02003607 break;
3608 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003609 LY_CHECK_RET(parse_status(ctx, data, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003610 break;
3611 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003612 LY_CHECK_RET(parse_when(ctx, data, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003613 break;
3614 case YANG_KEY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003615 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 +02003616 break;
3617 case YANG_MAX_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003618 LY_CHECK_RET(parse_maxelements(ctx, data, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003619 break;
3620 case YANG_MIN_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003621 LY_CHECK_RET(parse_minelements(ctx, data, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003622 break;
3623 case YANG_ORDERED_BY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003624 LY_CHECK_RET(parse_orderedby(ctx, data, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003625 break;
3626 case YANG_UNIQUE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003627 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 +02003628 break;
3629
3630 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01003631 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "list");
Radek Krejci10113652018-11-14 16:56:50 +01003632 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003633 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003634 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003635 break;
3636 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003637 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003638 break;
3639 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003640 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003641 break;
3642 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003643 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003644 break;
3645 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003646 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003647 break;
3648 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003649 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003650 break;
3651 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003652 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003653 break;
3654
3655 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01003656 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)list, data, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003657 break;
3658 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003659 LY_CHECK_RET(parse_restrs(ctx, data, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003660 break;
3661 case YANG_ACTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003662 YANG_CHECK_STMTVER2_RET(ctx, "action", "list");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003663 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003664 break;
3665 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003666 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003667 break;
3668 case YANG_NOTIFICATION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003669 YANG_CHECK_STMTVER2_RET(ctx, "notification", "list");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003670 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003671 break;
3672 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003673 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003674 break;
3675 default:
David Sedlákb3077192019-06-19 10:55:37 +02003676 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003677 return LY_EVALID;
3678 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003679 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003680 LY_CHECK_RET(ret);
3681checks:
Radek Krejci7fc68292019-06-12 13:51:09 +02003682 /* finalize parent pointers to the reallocated items */
3683 LY_CHECK_RET(parse_finalize_reallocated(ctx, list->groupings, NULL, list->actions, list->notifs));
3684
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003685 if (list->max && list->min > list->max) {
David Sedlákb3077192019-06-19 10:55:37 +02003686 LOGVAL_PARSER(ctx, LYVE_SEMANTICS,
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003687 "Invalid combination of min-elements and max-elements: min value %u is bigger than the max value %u.",
3688 list->min, list->max);
3689 return LY_EVALID;
3690 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003691
3692 return ret;
3693}
3694
Michal Vaskoea5abea2018-09-18 13:10:54 +02003695/**
3696 * @brief Parse the yin-element statement.
3697 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003698 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003699 * @param[in,out] data Data to read from, always moved to currently handled character.
3700 * @param[in,out] flags Flags to write to.
3701 * @param[in,out] exts Extension instances to add to.
3702 *
3703 * @return LY_ERR values.
3704 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003705static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003706parse_yinelement(struct lys_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003707{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003708 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003709 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003710 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003711 enum yang_keyword kw;
3712
3713 if (*flags & LYS_YINELEM_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02003714 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003715 return LY_EVALID;
3716 }
3717
3718 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003719 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003720
3721 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
3722 *flags |= LYS_YINELEM_TRUE;
3723 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
3724 *flags |= LYS_YINELEM_FALSE;
3725 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003726 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003727 free(buf);
3728 return LY_EVALID;
3729 }
3730 free(buf);
3731
Radek Krejci6d6556c2018-11-08 09:37:45 +01003732 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003733 switch (kw) {
3734 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003735 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_YINELEM, 0, exts));
3736 LY_CHECK_RET(ret); break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003737 default:
David Sedlákb3077192019-06-19 10:55:37 +02003738 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003739 return LY_EVALID;
3740 }
3741 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003742 return ret;
3743}
3744
Michal Vaskoea5abea2018-09-18 13:10:54 +02003745/**
David Sedlák2444f8f2019-07-09 11:02:47 +02003746 * @brief Parse the argument statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003747 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003748 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003749 * @param[in,out] data Data to read from, always moved to currently handled character.
3750 * @param[in,out] argument Value to write to.
3751 * @param[in,out] flags Flags to write to.
3752 * @param[in,out] exts Extension instances to add to.
3753 *
3754 * @return LY_ERR values.
3755 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003756static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003757parse_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 +02003758{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003759 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003760 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003761 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003762 enum yang_keyword kw;
3763
3764 if (*argument) {
David Sedlákb3077192019-06-19 10:55:37 +02003765 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003766 return LY_EVALID;
3767 }
3768
3769 /* get value */
David Sedlák2444f8f2019-07-09 11:02:47 +02003770 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003771 INSERT_WORD(ctx, buf, *argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003772
Radek Krejci6d6556c2018-11-08 09:37:45 +01003773 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003774 switch (kw) {
3775 case YANG_YIN_ELEMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003776 LY_CHECK_RET(parse_yinelement(ctx, data, flags, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003777 break;
3778 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003779 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_ARGUMENT, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003780 break;
3781 default:
David Sedlákb3077192019-06-19 10:55:37 +02003782 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003783 return LY_EVALID;
3784 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003785 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003786 return ret;
3787}
3788
Michal Vaskoea5abea2018-09-18 13:10:54 +02003789/**
3790 * @brief Parse the extension statement.
3791 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003792 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003793 * @param[in,out] data Data to read from, always moved to currently handled character.
3794 * @param[in,out] extensions Extensions to add to.
3795 *
3796 * @return LY_ERR values.
3797 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003798static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003799parse_extension(struct lys_parser_ctx *ctx, const char **data, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003800{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003801 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003802 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003803 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003804 enum yang_keyword kw;
3805 struct lysp_ext *ex;
3806
Radek Krejci2c4e7172018-10-19 15:56:26 +02003807 LY_ARRAY_NEW_RET(ctx->ctx, *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003808
3809 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003810 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003811 INSERT_WORD(ctx, buf, ex->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003812
Radek Krejci6d6556c2018-11-08 09:37:45 +01003813 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003814 switch (kw) {
3815 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003816 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 +02003817 break;
3818 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003819 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 +02003820 break;
3821 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003822 LY_CHECK_RET(parse_status(ctx, data, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003823 break;
3824 case YANG_ARGUMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003825 LY_CHECK_RET(parse_argument(ctx, data, &ex->argument, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003826 break;
3827 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003828 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003829 break;
3830 default:
David Sedlákb3077192019-06-19 10:55:37 +02003831 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003832 return LY_EVALID;
3833 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003834 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003835 return ret;
3836}
3837
Michal Vaskoea5abea2018-09-18 13:10:54 +02003838/**
3839 * @brief Parse the deviate statement.
3840 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003841 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003842 * @param[in,out] data Data to read from, always moved to currently handled character.
3843 * @param[in,out] deviates Deviates to add to.
3844 *
3845 * @return LY_ERR values.
3846 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003847LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003848parse_deviate(struct lys_parser_ctx *ctx, const char **data, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003849{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003850 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003851 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003852 size_t word_len, dev_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003853 enum yang_keyword kw;
3854 struct lysp_deviate *iter, *d;
3855 struct lysp_deviate_add *d_add = NULL;
3856 struct lysp_deviate_rpl *d_rpl = NULL;
3857 struct lysp_deviate_del *d_del = NULL;
3858 const char **d_units, ***d_uniques, ***d_dflts;
3859 struct lysp_restr **d_musts;
3860 uint16_t *d_flags;
3861 uint32_t *d_min, *d_max;
3862
3863 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003864 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003865
3866 if ((word_len == 13) && !strncmp(word, "not-supported", word_len)) {
3867 dev_mod = LYS_DEV_NOT_SUPPORTED;
3868 } else if ((word_len == 3) && !strncmp(word, "add", word_len)) {
3869 dev_mod = LYS_DEV_ADD;
3870 } else if ((word_len == 7) && !strncmp(word, "replace", word_len)) {
3871 dev_mod = LYS_DEV_REPLACE;
3872 } else if ((word_len == 6) && !strncmp(word, "delete", word_len)) {
3873 dev_mod = LYS_DEV_DELETE;
3874 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003875 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003876 free(buf);
3877 return LY_EVALID;
3878 }
3879 free(buf);
3880
3881 /* create structure */
3882 switch (dev_mod) {
3883 case LYS_DEV_NOT_SUPPORTED:
3884 d = calloc(1, sizeof *d);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003885 LY_CHECK_ERR_RET(!d, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003886 break;
3887 case LYS_DEV_ADD:
3888 d_add = calloc(1, sizeof *d_add);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003889 LY_CHECK_ERR_RET(!d_add, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003890 d = (struct lysp_deviate *)d_add;
3891 d_units = &d_add->units;
3892 d_uniques = &d_add->uniques;
3893 d_dflts = &d_add->dflts;
3894 d_musts = &d_add->musts;
3895 d_flags = &d_add->flags;
3896 d_min = &d_add->min;
3897 d_max = &d_add->max;
3898 break;
3899 case LYS_DEV_REPLACE:
3900 d_rpl = calloc(1, sizeof *d_rpl);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003901 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003902 d = (struct lysp_deviate *)d_rpl;
3903 d_units = &d_rpl->units;
3904 d_flags = &d_rpl->flags;
3905 d_min = &d_rpl->min;
3906 d_max = &d_rpl->max;
3907 break;
3908 case LYS_DEV_DELETE:
3909 d_del = calloc(1, sizeof *d_del);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003910 LY_CHECK_ERR_RET(!d_del, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003911 d = (struct lysp_deviate *)d_del;
3912 d_units = &d_del->units;
3913 d_uniques = &d_del->uniques;
3914 d_dflts = &d_del->dflts;
3915 d_musts = &d_del->musts;
3916 d_flags = &d_del->flags;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003917 break;
3918 default:
3919 assert(0);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003920 LOGINT_RET(ctx->ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003921 }
3922 d->mod = dev_mod;
3923
3924 /* insert into siblings */
3925 if (!*deviates) {
3926 *deviates = d;
3927 } else {
3928 for (iter = *deviates; iter->next; iter = iter->next);
3929 iter->next = d;
3930 }
3931
Radek Krejci6d6556c2018-11-08 09:37:45 +01003932 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003933 switch (kw) {
3934 case YANG_CONFIG:
3935 switch (dev_mod) {
3936 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003937 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003938 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003939 return LY_EVALID;
3940 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003941 LY_CHECK_RET(parse_config(ctx, data, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003942 break;
3943 }
3944 break;
3945 case YANG_DEFAULT:
3946 switch (dev_mod) {
3947 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003948 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003949 return LY_EVALID;
3950 case LYS_DEV_REPLACE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003951 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 +02003952 break;
3953 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003954 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 +02003955 break;
3956 }
3957 break;
3958 case YANG_MANDATORY:
3959 switch (dev_mod) {
3960 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003961 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003962 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003963 return LY_EVALID;
3964 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003965 LY_CHECK_RET(parse_mandatory(ctx, data, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003966 break;
3967 }
3968 break;
3969 case YANG_MAX_ELEMENTS:
3970 switch (dev_mod) {
3971 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003972 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003973 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003974 return LY_EVALID;
3975 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003976 LY_CHECK_RET(parse_maxelements(ctx, data, d_max, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003977 break;
3978 }
3979 break;
3980 case YANG_MIN_ELEMENTS:
3981 switch (dev_mod) {
3982 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003983 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003984 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003985 return LY_EVALID;
3986 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003987 LY_CHECK_RET(parse_minelements(ctx, data, d_min, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003988 break;
3989 }
3990 break;
3991 case YANG_MUST:
3992 switch (dev_mod) {
3993 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003994 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003995 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003996 return LY_EVALID;
3997 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003998 LY_CHECK_RET(parse_restrs(ctx, data, kw, d_musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003999 break;
4000 }
4001 break;
4002 case YANG_TYPE:
4003 switch (dev_mod) {
4004 case LYS_DEV_NOT_SUPPORTED:
4005 case LYS_DEV_ADD:
4006 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02004007 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004008 return LY_EVALID;
4009 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004010 if (d_rpl->type) {
David Sedlákb3077192019-06-19 10:55:37 +02004011 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004012 return LY_EVALID;
4013 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004014 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004015 LY_CHECK_ERR_RET(!d_rpl->type, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004016 LY_CHECK_RET(parse_type(ctx, data, d_rpl->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004017 break;
4018 }
4019 break;
4020 case YANG_UNIQUE:
4021 switch (dev_mod) {
4022 case LYS_DEV_NOT_SUPPORTED:
4023 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02004024 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004025 return LY_EVALID;
4026 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004027 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 +02004028 break;
4029 }
4030 break;
4031 case YANG_UNITS:
4032 switch (dev_mod) {
4033 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02004034 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004035 return LY_EVALID;
4036 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004037 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 +02004038 break;
4039 }
4040 break;
4041 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004042 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004043 break;
4044 default:
David Sedlákb3077192019-06-19 10:55:37 +02004045 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004046 return LY_EVALID;
4047 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004048 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004049 return ret;
4050}
4051
Michal Vaskoea5abea2018-09-18 13:10:54 +02004052/**
4053 * @brief Parse the deviation statement.
4054 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004055 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004056 * @param[in,out] data Data to read from, always moved to currently handled character.
4057 * @param[in,out] deviations Deviations to add to.
4058 *
4059 * @return LY_ERR values.
4060 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004061LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02004062parse_deviation(struct lys_parser_ctx *ctx, const char **data, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004063{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004064 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004065 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004066 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004067 enum yang_keyword kw;
4068 struct lysp_deviation *dev;
4069
Radek Krejci2c4e7172018-10-19 15:56:26 +02004070 LY_ARRAY_NEW_RET(ctx->ctx, *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004071
4072 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02004073 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Radek Krejcif09e4e82019-06-14 15:08:11 +02004074 YANG_CHECK_NONEMPTY(ctx, NULL, word_len, "deviation");
Radek Krejci44ceedc2018-10-02 15:54:31 +02004075 INSERT_WORD(ctx, buf, dev->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004076
Radek Krejci6d6556c2018-11-08 09:37:45 +01004077 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004078 switch (kw) {
4079 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004080 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 +02004081 break;
4082 case YANG_DEVIATE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004083 LY_CHECK_RET(parse_deviate(ctx, data, &dev->deviates));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004084 break;
4085 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004086 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 +02004087 break;
4088 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004089 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004090 break;
4091 default:
David Sedlákb3077192019-06-19 10:55:37 +02004092 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004093 return LY_EVALID;
4094 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004095 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004096 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01004097checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004098 /* mandatory substatements */
4099 if (!dev->deviates) {
David Sedlákb3077192019-06-19 10:55:37 +02004100 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004101 return LY_EVALID;
4102 }
4103
4104 return ret;
4105}
4106
Michal Vaskoea5abea2018-09-18 13:10:54 +02004107/**
4108 * @brief Parse the feature statement.
4109 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004110 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004111 * @param[in,out] data Data to read from, always moved to currently handled character.
4112 * @param[in,out] features Features to add to.
4113 *
4114 * @return LY_ERR values.
4115 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004116LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02004117parse_feature(struct lys_parser_ctx *ctx, const char **data, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004118{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004119 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004120 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004121 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004122 enum yang_keyword kw;
4123 struct lysp_feature *feat;
4124
Radek Krejci2c4e7172018-10-19 15:56:26 +02004125 LY_ARRAY_NEW_RET(ctx->ctx, *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004126
4127 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02004128 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02004129 INSERT_WORD(ctx, buf, feat->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004130
Radek Krejci6d6556c2018-11-08 09:37:45 +01004131 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004132 switch (kw) {
4133 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004134 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 +02004135 break;
4136 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004137 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 +02004138 break;
4139 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004140 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 +02004141 break;
4142 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004143 LY_CHECK_RET(parse_status(ctx, data, &feat->flags, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004144 break;
4145 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004146 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004147 break;
4148 default:
David Sedlákb3077192019-06-19 10:55:37 +02004149 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004150 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004151 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004152 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004153 return ret;
4154}
4155
Michal Vaskoea5abea2018-09-18 13:10:54 +02004156/**
4157 * @brief Parse the identity statement.
4158 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004159 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004160 * @param[in,out] data Data to read from, always moved to currently handled character.
4161 * @param[in,out] identities Identities to add to.
4162 *
4163 * @return LY_ERR values.
4164 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004165LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02004166parse_identity(struct lys_parser_ctx *ctx, const char **data, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004167{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004168 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004169 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004170 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004171 enum yang_keyword kw;
4172 struct lysp_ident *ident;
4173
Radek Krejci2c4e7172018-10-19 15:56:26 +02004174 LY_ARRAY_NEW_RET(ctx->ctx, *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004175
4176 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02004177 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02004178 INSERT_WORD(ctx, buf, ident->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004179
Radek Krejci6d6556c2018-11-08 09:37:45 +01004180 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004181 switch (kw) {
4182 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004183 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 +02004184 break;
4185 case YANG_IF_FEATURE:
Radek Krejciceaf2122019-01-02 15:03:26 +01004186 YANG_CHECK_STMTVER2_RET(ctx, "if-feature", "identity");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004187 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 +02004188 break;
4189 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004190 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 +02004191 break;
4192 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004193 LY_CHECK_RET(parse_status(ctx, data, &ident->flags, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004194 break;
4195 case YANG_BASE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004196 if (ident->bases && ctx->mod_version < 2) {
David Sedlákb3077192019-06-19 10:55:37 +02004197 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 +01004198 return LY_EVALID;
4199 }
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004200 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 +02004201 break;
4202 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004203 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004204 break;
4205 default:
David Sedlákb3077192019-06-19 10:55:37 +02004206 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004207 return LY_EVALID;
4208 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004209 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004210 return ret;
4211}
4212
Michal Vaskoea5abea2018-09-18 13:10:54 +02004213/**
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004214 * @brief Parse module substatements.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004215 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004216 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004217 * @param[in,out] data Data to read from, always moved to currently handled character.
4218 * @param[in,out] mod Module to write to.
4219 *
4220 * @return LY_ERR values.
4221 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004222LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02004223parse_module(struct lys_parser_ctx *ctx, const char **data, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004224{
4225 LY_ERR ret = 0;
4226 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004227 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004228 enum yang_keyword kw, prev_kw = 0;
4229 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004230 struct lysp_submodule *dup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004231
4232 /* (sub)module name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02004233 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004234 INSERT_WORD(ctx, buf, mod->mod->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004235
Radek Krejci6d6556c2018-11-08 09:37:45 +01004236 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004237
Radek Krejcie3846472018-10-15 15:24:51 +02004238#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004239 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 +02004240
Michal Vasko7fbc8162018-09-17 10:35:16 +02004241 switch (kw) {
4242 /* module header */
4243 case YANG_NAMESPACE:
4244 case YANG_PREFIX:
Radek Krejcie3846472018-10-15 15:24:51 +02004245 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4246 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004247 case YANG_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004248 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004249 break;
4250 /* linkage */
4251 case YANG_INCLUDE:
4252 case YANG_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004253 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004254 break;
4255 /* meta */
4256 case YANG_ORGANIZATION:
4257 case YANG_CONTACT:
4258 case YANG_DESCRIPTION:
4259 case YANG_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004260 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004261 break;
4262
4263 /* revision */
4264 case YANG_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004265 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004266 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004267 /* body */
4268 case YANG_ANYDATA:
4269 case YANG_ANYXML:
4270 case YANG_AUGMENT:
4271 case YANG_CHOICE:
4272 case YANG_CONTAINER:
4273 case YANG_DEVIATION:
4274 case YANG_EXTENSION:
4275 case YANG_FEATURE:
4276 case YANG_GROUPING:
4277 case YANG_IDENTITY:
4278 case YANG_LEAF:
4279 case YANG_LEAF_LIST:
4280 case YANG_LIST:
4281 case YANG_NOTIFICATION:
4282 case YANG_RPC:
4283 case YANG_TYPEDEF:
4284 case YANG_USES:
4285 case YANG_CUSTOM:
4286 mod_stmt = Y_MOD_BODY;
4287 break;
4288 default:
4289 /* error handled in the next switch */
4290 break;
4291 }
Radek Krejcie3846472018-10-15 15:24:51 +02004292#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004293
Radek Krejcie3846472018-10-15 15:24:51 +02004294 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004295 switch (kw) {
4296 /* module header */
4297 case YANG_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004298 LY_CHECK_RET(parse_yangversion(ctx, data, &mod->mod->version, &mod->exts));
4299 ctx->mod_version = mod->mod->version;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004300 break;
4301 case YANG_NAMESPACE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004302 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 +02004303 break;
4304 case YANG_PREFIX:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004305 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 +02004306 break;
4307
4308 /* linkage */
4309 case YANG_INCLUDE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004310 LY_CHECK_RET(parse_include(ctx, mod->mod->name, data, &mod->includes));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004311 break;
4312 case YANG_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004313 LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, data, &mod->imports));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004314 break;
4315
4316 /* meta */
4317 case YANG_ORGANIZATION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004318 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 +02004319 break;
4320 case YANG_CONTACT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004321 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 +02004322 break;
4323 case YANG_DESCRIPTION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004324 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 +02004325 break;
4326 case YANG_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004327 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 +02004328 break;
4329
4330 /* revision */
4331 case YANG_REVISION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004332 LY_CHECK_RET(parse_revision(ctx, data, &mod->revs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004333 break;
4334
4335 /* body */
4336 case YANG_ANYDATA:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004337 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "module");
Radek Krejci10113652018-11-14 16:56:50 +01004338 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004339 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004340 LY_CHECK_RET(parse_any(ctx, data, kw, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004341 break;
4342 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004343 LY_CHECK_RET(parse_choice(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004344 break;
4345 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004346 LY_CHECK_RET(parse_container(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004347 break;
4348 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004349 LY_CHECK_RET(parse_leaf(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004350 break;
4351 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004352 LY_CHECK_RET(parse_leaflist(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004353 break;
4354 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004355 LY_CHECK_RET(parse_list(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004356 break;
4357 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004358 LY_CHECK_RET(parse_uses(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004359 break;
4360
4361 case YANG_AUGMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004362 LY_CHECK_RET(parse_augment(ctx, data, NULL, &mod->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004363 break;
4364 case YANG_DEVIATION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004365 LY_CHECK_RET(parse_deviation(ctx, data, &mod->deviations));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004366 break;
4367 case YANG_EXTENSION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004368 LY_CHECK_RET(parse_extension(ctx, data, &mod->extensions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004369 break;
4370 case YANG_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004371 LY_CHECK_RET(parse_feature(ctx, data, &mod->features));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004372 break;
4373 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004374 LY_CHECK_RET(parse_grouping(ctx, data, NULL, &mod->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004375 break;
4376 case YANG_IDENTITY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004377 LY_CHECK_RET(parse_identity(ctx, data, &mod->identities));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004378 break;
4379 case YANG_NOTIFICATION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004380 LY_CHECK_RET(parse_notif(ctx, data, NULL, &mod->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004381 break;
4382 case YANG_RPC:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004383 LY_CHECK_RET(parse_action(ctx, data, NULL, &mod->rpcs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004384 break;
4385 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01004386 LY_CHECK_RET(parse_typedef(ctx, NULL, data, &mod->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004387 break;
4388 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004389 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004390 break;
4391
4392 default:
David Sedlákb3077192019-06-19 10:55:37 +02004393 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004394 return LY_EVALID;
4395 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004396 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004397 LY_CHECK_RET(ret);
Radek Krejcie86bf772018-12-14 11:39:53 +01004398
Radek Krejci6d6556c2018-11-08 09:37:45 +01004399checks:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004400 /* finalize parent pointers to the reallocated items */
Radek Krejci7fc68292019-06-12 13:51:09 +02004401 LY_CHECK_RET(parse_finalize_reallocated(ctx, mod->groupings, mod->augments, mod->rpcs, mod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004402
Michal Vasko7fbc8162018-09-17 10:35:16 +02004403 /* mandatory substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004404 if (!mod->mod->ns) {
David Sedlákb3077192019-06-19 10:55:37 +02004405 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004406 return LY_EVALID;
4407 } else if (!mod->mod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004408 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004409 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004410 }
4411
Radek Krejcie9e987e2018-10-31 12:50:27 +01004412 /* submodules share the namespace with the module names, so there must not be
4413 * a submodule of the same name in the context, no need for revision matching */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004414 dup = ly_ctx_get_submodule(ctx->ctx, NULL, mod->mod->name, NULL);
4415 if (dup) {
David Sedlákb3077192019-06-19 10:55:37 +02004416 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 +01004417 return LY_EVALID;
4418 }
4419
4420 return ret;
4421}
4422
4423/**
4424 * @brief Parse submodule substatements.
4425 *
4426 * @param[in] ctx yang parser context for logging.
4427 * @param[in,out] data Data to read from, always moved to currently handled character.
4428 * @param[out] submod Parsed submodule structure.
4429 *
4430 * @return LY_ERR values.
4431 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004432LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02004433parse_submodule(struct lys_parser_ctx *ctx, const char **data, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004434{
4435 LY_ERR ret = 0;
4436 char *buf, *word;
4437 size_t word_len;
4438 enum yang_keyword kw, prev_kw = 0;
4439 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
4440 struct lysp_submodule *dup;
4441
4442 /* submodule name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02004443 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004444 INSERT_WORD(ctx, buf, submod->name, word, word_len);
4445
4446 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
4447
4448#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004449 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 +01004450
4451 switch (kw) {
4452 /* module header */
4453 case YANG_BELONGS_TO:
4454 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4455 break;
4456 case YANG_YANG_VERSION:
4457 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4458 break;
4459 /* linkage */
4460 case YANG_INCLUDE:
4461 case YANG_IMPORT:
4462 CHECK_ORDER(Y_MOD_LINKAGE);
4463 break;
4464 /* meta */
4465 case YANG_ORGANIZATION:
4466 case YANG_CONTACT:
4467 case YANG_DESCRIPTION:
4468 case YANG_REFERENCE:
4469 CHECK_ORDER(Y_MOD_META);
4470 break;
4471
4472 /* revision */
4473 case YANG_REVISION:
4474 CHECK_ORDER(Y_MOD_REVISION);
4475 break;
4476 /* body */
4477 case YANG_ANYDATA:
4478 case YANG_ANYXML:
4479 case YANG_AUGMENT:
4480 case YANG_CHOICE:
4481 case YANG_CONTAINER:
4482 case YANG_DEVIATION:
4483 case YANG_EXTENSION:
4484 case YANG_FEATURE:
4485 case YANG_GROUPING:
4486 case YANG_IDENTITY:
4487 case YANG_LEAF:
4488 case YANG_LEAF_LIST:
4489 case YANG_LIST:
4490 case YANG_NOTIFICATION:
4491 case YANG_RPC:
4492 case YANG_TYPEDEF:
4493 case YANG_USES:
4494 case YANG_CUSTOM:
4495 mod_stmt = Y_MOD_BODY;
4496 break;
4497 default:
4498 /* error handled in the next switch */
4499 break;
4500 }
4501#undef CHECK_ORDER
4502
4503 prev_kw = kw;
4504 switch (kw) {
4505 /* module header */
4506 case YANG_YANG_VERSION:
4507 LY_CHECK_RET(parse_yangversion(ctx, data, &submod->version, &submod->exts));
4508 ctx->mod_version = submod->version;
4509 break;
4510 case YANG_BELONGS_TO:
4511 LY_CHECK_RET(parse_belongsto(ctx, data, &submod->belongsto, &submod->prefix, &submod->exts));
4512 break;
4513
4514 /* linkage */
4515 case YANG_INCLUDE:
4516 LY_CHECK_RET(parse_include(ctx, submod->name, data, &submod->includes));
4517 break;
4518 case YANG_IMPORT:
4519 LY_CHECK_RET(parse_import(ctx, submod->prefix, data, &submod->imports));
4520 break;
4521
4522 /* meta */
4523 case YANG_ORGANIZATION:
4524 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_ORGANIZATION, 0, &submod->org, Y_STR_ARG, &submod->exts));
4525 break;
4526 case YANG_CONTACT:
4527 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_CONTACT, 0, &submod->contact, Y_STR_ARG, &submod->exts));
4528 break;
4529 case YANG_DESCRIPTION:
4530 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &submod->dsc, Y_STR_ARG, &submod->exts));
4531 break;
4532 case YANG_REFERENCE:
4533 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &submod->ref, Y_STR_ARG, &submod->exts));
4534 break;
4535
4536 /* revision */
4537 case YANG_REVISION:
4538 LY_CHECK_RET(parse_revision(ctx, data, &submod->revs));
4539 break;
4540
4541 /* body */
4542 case YANG_ANYDATA:
4543 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "submodule");
4544 /* fall through */
4545 case YANG_ANYXML:
4546 LY_CHECK_RET(parse_any(ctx, data, kw, NULL, &submod->data));
4547 break;
4548 case YANG_CHOICE:
4549 LY_CHECK_RET(parse_choice(ctx, data, NULL, &submod->data));
4550 break;
4551 case YANG_CONTAINER:
4552 LY_CHECK_RET(parse_container(ctx, data, NULL, &submod->data));
4553 break;
4554 case YANG_LEAF:
4555 LY_CHECK_RET(parse_leaf(ctx, data, NULL, &submod->data));
4556 break;
4557 case YANG_LEAF_LIST:
4558 LY_CHECK_RET(parse_leaflist(ctx, data, NULL, &submod->data));
4559 break;
4560 case YANG_LIST:
4561 LY_CHECK_RET(parse_list(ctx, data, NULL, &submod->data));
4562 break;
4563 case YANG_USES:
4564 LY_CHECK_RET(parse_uses(ctx, data, NULL, &submod->data));
4565 break;
4566
4567 case YANG_AUGMENT:
4568 LY_CHECK_RET(parse_augment(ctx, data, NULL, &submod->augments));
4569 break;
4570 case YANG_DEVIATION:
4571 LY_CHECK_RET(parse_deviation(ctx, data, &submod->deviations));
4572 break;
4573 case YANG_EXTENSION:
4574 LY_CHECK_RET(parse_extension(ctx, data, &submod->extensions));
4575 break;
4576 case YANG_FEATURE:
4577 LY_CHECK_RET(parse_feature(ctx, data, &submod->features));
4578 break;
4579 case YANG_GROUPING:
4580 LY_CHECK_RET(parse_grouping(ctx, data, NULL, &submod->groupings));
4581 break;
4582 case YANG_IDENTITY:
4583 LY_CHECK_RET(parse_identity(ctx, data, &submod->identities));
4584 break;
4585 case YANG_NOTIFICATION:
4586 LY_CHECK_RET(parse_notif(ctx, data, NULL, &submod->notifs));
4587 break;
4588 case YANG_RPC:
4589 LY_CHECK_RET(parse_action(ctx, data, NULL, &submod->rpcs));
4590 break;
4591 case YANG_TYPEDEF:
4592 LY_CHECK_RET(parse_typedef(ctx, NULL, data, &submod->typedefs));
4593 break;
4594 case YANG_CUSTOM:
4595 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &submod->exts));
4596 break;
4597
4598 default:
David Sedlákb3077192019-06-19 10:55:37 +02004599 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004600 return LY_EVALID;
4601 }
4602 }
4603 LY_CHECK_RET(ret);
4604
4605checks:
4606 /* finalize parent pointers to the reallocated items */
Radek Krejci7fc68292019-06-12 13:51:09 +02004607 LY_CHECK_RET(parse_finalize_reallocated(ctx, submod->groupings, submod->augments, submod->rpcs, submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004608
4609 /* mandatory substatements */
4610 if (!submod->belongsto) {
David Sedlákb3077192019-06-19 10:55:37 +02004611 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004612 return LY_EVALID;
4613 }
4614
4615 /* submodules share the namespace with the module names, so there must not be
4616 * a submodule of the same name in the context, no need for revision matching */
4617 dup = ly_ctx_get_submodule(ctx->ctx, NULL, submod->name, NULL);
4618 if (dup && strcmp(dup->belongsto, submod->belongsto)) {
David Sedlákb3077192019-06-19 10:55:37 +02004619 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Name collision between submodules of name \"%s\".", dup->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +01004620 return LY_EVALID;
4621 }
4622
Michal Vasko7fbc8162018-09-17 10:35:16 +02004623 return ret;
4624}
4625
Radek Krejcid4557c62018-09-17 11:42:09 +02004626LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02004627yang_parse_submodule(struct lys_parser_ctx *context, const char *data, struct lysp_submodule **submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004628{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004629 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004630 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004631 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004632 enum yang_keyword kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004633 struct lysp_submodule *mod_p = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004634
4635 /* "module"/"submodule" */
Radek Krejcibbe09a92018-11-08 09:36:54 +01004636 ret = get_keyword(context, &data, &kw, &word, &word_len);
4637 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004638
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004639 if (kw == YANG_MODULE) {
4640 LOGERR(context->ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
4641 ret = LY_EINVAL;
4642 goto cleanup;
4643 } else if (kw != YANG_SUBMODULE) {
David Sedlákb3077192019-06-19 10:55:37 +02004644 LOGVAL_PARSER(context, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".",
Radek Krejcic59bc972018-09-17 16:13:06 +02004645 ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004646 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004647 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004648 }
4649
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004650 mod_p = calloc(1, sizeof *mod_p);
4651 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(context->ctx), cleanup);
4652 mod_p->parsing = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004653
4654 /* substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004655 ret = parse_submodule(context, &data, mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004656 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004657
4658 /* read some trailing spaces or new lines */
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004659 while(*data && isspace(*data)) {
4660 data++;
4661 }
4662 if (*data) {
David Sedlákb3077192019-06-19 10:55:37 +02004663 LOGVAL_PARSER(context, LYVE_SYNTAX, "Trailing garbage \"%.*s%s\" after submodule, expected end-of-input.",
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004664 15, data, strlen(data) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004665 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004666 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004667 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004668
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004669 mod_p->parsing = 0;
4670 *submod = mod_p;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004671
Radek Krejcibbe09a92018-11-08 09:36:54 +01004672cleanup:
4673 if (ret) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004674 lysp_submodule_free(context->ctx, mod_p);
4675 }
4676
4677 return ret;
4678}
4679
4680LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02004681yang_parse_module(struct lys_parser_ctx *context, const char *data, struct lys_module *mod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004682{
4683 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004684 char *word;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004685 size_t word_len;
4686 enum yang_keyword kw;
4687 struct lysp_module *mod_p = NULL;
4688
4689 /* "module"/"submodule" */
4690 ret = get_keyword(context, &data, &kw, &word, &word_len);
4691 LY_CHECK_GOTO(ret, cleanup);
4692
4693 if (kw == YANG_SUBMODULE) {
4694 LOGERR(context->ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module.");
4695 ret = LY_EINVAL;
4696 goto cleanup;
4697 } else if (kw != YANG_MODULE) {
David Sedlákb3077192019-06-19 10:55:37 +02004698 LOGVAL_PARSER(context, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004699 ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004700 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004701 goto cleanup;
4702 }
4703
4704 mod_p = calloc(1, sizeof *mod_p);
4705 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(context->ctx), cleanup);
4706 mod_p->mod = mod;
4707 mod_p->parsing = 1;
4708
4709 /* substatements */
4710 ret = parse_module(context, &data, mod_p);
4711 LY_CHECK_GOTO(ret, cleanup);
4712
4713 /* read some trailing spaces or new lines */
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004714 while(*data && isspace(*data)) {
4715 data++;
4716 }
4717 if (*data) {
David Sedlákb3077192019-06-19 10:55:37 +02004718 LOGVAL_PARSER(context, LYVE_SYNTAX, "Trailing garbage \"%.*s%s\" after module, expected end-of-input.",
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004719 15, data, strlen(data) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004720 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004721 goto cleanup;
4722 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004723
4724 mod_p->parsing = 0;
4725 mod->parsed = mod_p;
4726
4727cleanup:
4728 if (ret) {
4729 lysp_module_free(mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004730 }
4731
Michal Vasko7fbc8162018-09-17 10:35:16 +02004732 return ret;
4733}