blob: 7de7c19a66ae935be317c05d626db0eba633137d [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
34/* Macro to check YANG's yang-char grammar rule */
35#define is_yangutf8char(c) ((c >= 0x20 && c <= 0xd77) || c == 0x09 || c == 0x0a || c == 0x0d || \
36 (c >= 0xe000 && c <= 0xfdcf) || (c >= 0xfdf0 && c <= 0xfffd) || \
37 (c >= 0x10000 && c <= 0x1fffd) || (c >= 0x20000 && c <= 0x2fffd) || \
38 (c >= 0x30000 && c <= 0x3fffd) || (c >= 0x40000 && c <= 0x2fffd) || \
39 (c >= 0x50000 && c <= 0x5fffd) || (c >= 0x60000 && c <= 0x6fffd) || \
40 (c >= 0x70000 && c <= 0x7fffd) || (c >= 0x80000 && c <= 0x8fffd) || \
41 (c >= 0x90000 && c <= 0x9fffd) || (c >= 0xa0000 && c <= 0xafffd) || \
42 (c >= 0xb0000 && c <= 0xbfffd) || (c >= 0xc0000 && c <= 0xcfffd) || \
43 (c >= 0xd0000 && c <= 0xdfffd) || (c >= 0xe0000 && c <= 0xefffd) || \
44 (c >= 0xf0000 && c <= 0xffffd) || (c >= 0x100000 && c <= 0x10fffd))
45
Radek Krejciceaf2122019-01-02 15:03:26 +010046/**
47 * @brief Try to find object with MEMBER string matching the IDENT in the given ARRAY.
48 * Macro logs an error message and returns LY_EVALID in case of existence of a matching object.
49 *
50 * @param[in] CTX yang parser context for logging.
51 * @param[in] ARRAY [sized array](@ref sizedarrays) of a generic objects with member named MEMBER to search.
52 * @param[in] MEMBER Name of the member of the objects in the ARRAY to compare.
53 * @param[in] STMT Name of the compared YANG statements for logging.
54 * @param[in] IDENT String trying to find in the ARRAY's objects inside the MEMBER member.
55 */
Radek Krejcifaa1eac2018-10-30 14:34:55 +010056#define CHECK_UNIQUENESS(CTX, ARRAY, MEMBER, STMT, IDENT) \
57 if (ARRAY) { \
58 for (unsigned int u = 0; u < LY_ARRAY_SIZE(ARRAY) - 1; ++u) { \
59 if (!strcmp((ARRAY)[u].MEMBER, IDENT)) { \
David Sedlákb3077192019-06-19 10:55:37 +020060 LOGVAL_PARSER(CTX, LY_VCODE_DUPIDENT, IDENT, STMT); \
Radek Krejcifaa1eac2018-10-30 14:34:55 +010061 return LY_EVALID; \
62 } \
63 } \
64 }
65
Radek Krejciceaf2122019-01-02 15:03:26 +010066/**
67 * @brief Insert WORD into the libyang context's dictionary and store as TARGET.
68 * @param[in] CTX yang parser context to access libyang context.
69 * @param[in] BUF buffer in case the word is not a constant and can be inserted directly (zero-copy)
70 * @param[out] TARGET variable where to store the pointer to the inserted value.
71 * @param[in] WORD string to store.
72 * @param[in] LEN length of the string in WORD to store.
73 */
Radek Krejci9fcacc12018-10-11 15:59:11 +020074#define INSERT_WORD(CTX, BUF, TARGET, WORD, LEN) \
75 if (BUF) {(TARGET) = lydict_insert_zc((CTX)->ctx, WORD);}\
Radek Krejcif09e4e82019-06-14 15:08:11 +020076 else {(TARGET) = lydict_insert((CTX)->ctx, LEN ? WORD : "", LEN);}
Radek Krejci44ceedc2018-10-02 15:54:31 +020077
Radek Krejciceaf2122019-01-02 15:03:26 +010078/**
79 * @brief Move the DATA pointer by COUNT items. Also updates the indent value in yang parser context
80 * @param[in] CTX yang parser context to update its indent value.
81 * @param[in,out] DATA pointer to move
82 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
83 */
Radek Krejci2b610482019-01-02 13:36:09 +010084#define MOVE_INPUT(CTX, DATA, COUNT) (*(DATA))+=COUNT;(CTX)->indent+=COUNT
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020085
Michal Vaskoea5abea2018-09-18 13:10:54 +020086/**
87 * @brief Loop through all substatements providing, return if there are none.
88 *
Radek Krejci44ceedc2018-10-02 15:54:31 +020089 * @param[in] CTX yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +020090 * @param[in] DATA Raw data to read from.
91 * @param[out] KW YANG keyword read.
92 * @param[out] WORD Pointer to the keyword itself.
93 * @param[out] WORD_LEN Length of the keyword.
94 * @param[out] ERR Variable for error storing.
95 *
96 * @return In case there are no substatements or a fatal error encountered.
97 */
Radek Krejci6d6556c2018-11-08 09:37:45 +010098#define YANG_READ_SUBSTMT_FOR(CTX, DATA, KW, WORD, WORD_LEN, ERR, CHECKGOTO) \
Radek Krejci6d9b9b52018-11-02 12:43:39 +010099 LY_CHECK_RET(get_keyword(CTX, DATA, &KW, &WORD, &WORD_LEN)); \
Michal Vasko7fbc8162018-09-17 10:35:16 +0200100 if (KW == YANG_SEMICOLON) { \
Radek Krejci6d6556c2018-11-08 09:37:45 +0100101 CHECKGOTO; \
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100102 return LY_SUCCESS; \
Michal Vasko7fbc8162018-09-17 10:35:16 +0200103 } \
104 if (KW != YANG_LEFT_BRACE) { \
David Sedlákb3077192019-06-19 10:55:37 +0200105 LOGVAL_PARSER(CTX, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\", expected \";\" or \"{\".", ly_stmt2str(KW)); \
Michal Vasko7fbc8162018-09-17 10:35:16 +0200106 return LY_EVALID; \
107 } \
108 for (ERR = get_keyword(CTX, DATA, &KW, &WORD, &WORD_LEN); \
109 !ERR && (KW != YANG_RIGHT_BRACE); \
110 ERR = get_keyword(CTX, DATA, &KW, &WORD, &WORD_LEN))
111
Radek Krejciceaf2122019-01-02 15:03:26 +0100112/**
113 * @brief Check module version is at least 2 (YANG 1.1) because of the keyword presence.
114 * Logs error message and returns LY_EVALID in case of module in YANG version 1.0.
115 * @param[in] CTX yang parser context to get current module and for logging.
116 * @param[in] KW keyword allowed only in YANG version 1.1 (or later) - for logging.
117 * @param[in] PARENT parent statement where the KW is present - for logging.
118 */
119#define YANG_CHECK_STMTVER2_RET(CTX, KW, PARENT) \
David Sedlákb3077192019-06-19 10:55:37 +0200120 if ((CTX)->mod_version < 2) {LOGVAL_PARSER((CTX), LY_VCODE_INCHILDSTMT2, KW, PARENT); return LY_EVALID;}
Radek Krejci10113652018-11-14 16:56:50 +0100121
Radek Krejcif09e4e82019-06-14 15:08:11 +0200122#define YANG_CHECK_NONEMPTY(CTX, OBJECT, VALUE_LEN, STMT) \
123 if (!VALUE_LEN) { \
124 LOGWRN((CTX)->ctx, "Empty argument of %s statement does not make sense.", STMT); \
125 }
126
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200127LY_ERR parse_container(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
128LY_ERR parse_uses(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
129LY_ERR parse_choice(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
130LY_ERR parse_case(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
131LY_ERR parse_list(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
132LY_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 +0200133
Radek Krejci7fc68292019-06-12 13:51:09 +0200134static LY_ERR parse_finalize_reallocated(struct lys_parser_ctx *ctx, struct lysp_grp *groupings, struct lysp_augment *augments,
135 struct lysp_action *actions, struct lysp_notif *notifs);
136
Michal Vaskoea5abea2018-09-18 13:10:54 +0200137/**
138 * @brief Add another character to dynamic buffer, a low-level function.
139 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200140 * Enlarge if needed. Updates \p input as well as \p buf_used.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200141 *
Radek Krejci404251e2018-10-09 12:06:44 +0200142 * @param[in] ctx libyang context for logging.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200143 * @param[in, out] input Input string to process.
144 * @param[in] len Number of bytes to get from the input string and copy into the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200145 * @param[in,out] buf Buffer to use, can be moved by realloc().
146 * @param[in,out] buf_len Current size of the buffer.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200147 * @param[in,out] buf_used Currently used characters of the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200148 *
149 * @return LY_ERR values.
150 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200151LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +0200152buf_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 +0200153{
Radek Krejci44ceedc2018-10-02 15:54:31 +0200154 if (*buf_len <= (*buf_used) + len) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200155 *buf_len += 16;
156 *buf = ly_realloc(*buf, *buf_len);
157 LY_CHECK_ERR_RET(!*buf, LOGMEM(ctx), LY_EMEM);
158 }
Radek Krejcic0917392019-04-10 13:04:04 +0200159 if (*buf_used) {
160 memcpy(&(*buf)[*buf_used], *input, len);
161 } else {
162 memcpy(*buf, *input, len);
163 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200164
Radek Krejci44ceedc2018-10-02 15:54:31 +0200165 (*buf_used) += len;
166 (*input) += len;
167
Michal Vasko7fbc8162018-09-17 10:35:16 +0200168 return LY_SUCCESS;
169}
170
Michal Vaskoea5abea2018-09-18 13:10:54 +0200171/**
Radek Krejci44ceedc2018-10-02 15:54:31 +0200172 * @brief Check that \p c is valid UTF8 code point for YANG string.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200173 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200174 * @param[in] ctx yang parser context for logging.
175 * @param[in] c UTF8 code point of a character to check.
176 * @return LY_ERR values.
177 */
178static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200179check_stringchar(struct lys_parser_ctx *ctx, unsigned int c)
Radek Krejci44ceedc2018-10-02 15:54:31 +0200180{
181 if (!is_yangutf8char(c)) {
David Sedlákb3077192019-06-19 10:55:37 +0200182 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, c);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200183 return LY_EVALID;
184 }
185 return LY_SUCCESS;
186}
187
188/**
189 * @brief Check that \p c is valid UTF8 code point for YANG identifier.
190 *
191 * @param[in] ctx yang parser context for logging.
192 * @param[in] c UTF8 code point of a character to check.
193 * @param[in] first Flag to check the first character of an identifier, which is more restricted.
Radek Krejcidcc7b322018-10-11 14:24:02 +0200194 * @param[in,out] prefix Storage for internally used flag in case of possible prefixed identifiers:
195 * 0 - colon not yet found (no prefix)
196 * 1 - \p c is the colon character
197 * 2 - prefix already processed, now processing the identifier
198 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200199 * If the identifier cannot be prefixed, NULL is expected.
200 * @return LY_ERR values.
201 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200202LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200203check_identifierchar(struct lys_parser_ctx *ctx, unsigned int c, int first, int *prefix)
Radek Krejci44ceedc2018-10-02 15:54:31 +0200204{
205 if (first || (prefix && (*prefix) == 1)) {
206 if (!is_yangidentstartchar(c)) {
David Sedlákb3077192019-06-19 10:55:37 +0200207 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '%c'.", c);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200208 return LY_EVALID;
209 }
Radek Krejci9fcacc12018-10-11 15:59:11 +0200210 if (prefix) {
211 if (first) {
212 (*prefix) = 0;
213 } else {
214 (*prefix) = 2;
215 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200216 }
217 } else if (c == ':' && prefix && (*prefix) == 0) {
218 (*prefix) = 1;
219 } else if (!is_yangidentchar(c)) {
David Sedlákb3077192019-06-19 10:55:37 +0200220 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier character '%c'.", c);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200221 return LY_EVALID;
222 }
223
224 return LY_SUCCESS;
225}
226
227/**
228 * @brief Store a single UTF8 character. It depends whether in a dynamically-allocated buffer or just as a pointer to the data.
229 *
230 * @param[in] ctx yang parser context for logging.
231 * @param[in,out] input Pointer to the string where to get the character to store. Automatically moved to the next character
232 * when function returns.
233 * @param[in] arg Type of the input string to select method of checking character validity.
234 * @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 +0200235 * 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 +0200236 * @param[in,out] word_len Current length of the word pointed to by \p word_p.
237 * @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 +0200238 * @param[in,out] buf_len Current length of \p word_b.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200239 * @param[in] need_buf Flag if the dynamically allocated buffer is required.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200240 *
241 * @return LY_ERR values.
242 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200243LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200244buf_store_char(struct lys_parser_ctx *ctx, const char **input, enum yang_arg arg,
Radek Krejci44ceedc2018-10-02 15:54:31 +0200245 char **word_p, size_t *word_len, char **word_b, size_t *buf_len, int need_buf)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200246{
Radek Krejci404251e2018-10-09 12:06:44 +0200247 int prefix = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200248 unsigned int c;
249 size_t len;
250
Radek Krejcif29b7c32019-04-09 16:17:49 +0200251 /* check valid combination of input paremeters - if need_buf specified, word_b must be provided */
252 assert(!need_buf || (need_buf && word_b));
253
Radek Krejci44ceedc2018-10-02 15:54:31 +0200254 /* get UTF8 code point (and number of bytes coding the character) */
255 LY_CHECK_ERR_RET(ly_getutf8(input, &c, &len),
David Sedlákb3077192019-06-19 10:55:37 +0200256 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, (*input)[-len]), LY_EVALID);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200257 (*input) -= len;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200258 if (c == '\n') {
259 ctx->indent = 0;
260 } else {
261 /* note - even the multibyte character is count as 1 */
262 ++ctx->indent;
263 }
264
Radek Krejci44ceedc2018-10-02 15:54:31 +0200265 /* check character validity */
266 switch (arg) {
267 case Y_IDENTIF_ARG:
268 LY_CHECK_RET(check_identifierchar(ctx, c, !(*word_len), NULL));
269 break;
270 case Y_PREF_IDENTIF_ARG:
271 LY_CHECK_RET(check_identifierchar(ctx, c, !(*word_len), &prefix));
272 break;
273 case Y_STR_ARG:
274 case Y_MAYBE_STR_ARG:
275 LY_CHECK_RET(check_stringchar(ctx, c));
276 break;
277 }
278
Michal Vasko7fbc8162018-09-17 10:35:16 +0200279 if (word_b && *word_b) {
280 /* add another character into buffer */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200281 if (buf_add_char(ctx->ctx, input, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200282 return LY_EMEM;
283 }
284
285 /* in case of realloc */
286 *word_p = *word_b;
Radek Krejcif29b7c32019-04-09 16:17:49 +0200287 } else if (word_b && need_buf) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200288 /* first time we need a buffer, copy everything read up to now */
289 if (*word_len) {
290 *word_b = malloc(*word_len);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200291 LY_CHECK_ERR_RET(!*word_b, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200292 *buf_len = *word_len;
293 memcpy(*word_b, *word_p, *word_len);
294 }
295
296 /* add this new character into buffer */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200297 if (buf_add_char(ctx->ctx, input, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200298 return LY_EMEM;
299 }
300
301 /* in case of realloc */
302 *word_p = *word_b;
303 } else {
304 /* just remember the first character pointer */
305 if (!*word_p) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200306 *word_p = (char *)(*input);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200307 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200308 /* ... and update the word's length */
309 (*word_len) += len;
310 (*input) += len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200311 }
312
313 return LY_SUCCESS;
314}
315
Michal Vaskoea5abea2018-09-18 13:10:54 +0200316/**
317 * @brief Skip YANG comment in data.
318 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200319 * @param[in] ctx yang parser context for logging.
320 * @param[in,out] data Data to read from, automatically moved after the comment.
321 * @param[in] comment Type of the comment to process:
322 * 1 for a one-line comment,
323 * 2 for a block comment.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200324 *
325 * @return LY_ERR values.
326 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200327LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200328skip_comment(struct lys_parser_ctx *ctx, const char **data, int comment)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200329{
Radek Krejci80dd33e2018-09-26 15:57:18 +0200330 /* internal statuses: 0 - comment ended,
331 * 1 - in line comment,
332 * 2 - in block comment,
333 * 3 - in block comment with last read character '*'
334 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200335 while (**data && comment) {
336 switch (comment) {
337 case 1:
338 if (**data == '\n') {
339 comment = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200340 ++ctx->line;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200341 }
342 break;
343 case 2:
344 if (**data == '*') {
Radek Krejci15c80ca2018-10-09 11:01:31 +0200345 comment = 3;
346 } else if (**data == '\n') {
347 ++ctx->line;
348 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200349 break;
350 case 3:
351 if (**data == '/') {
352 comment = 0;
Radek Krejci5b930492019-06-11 14:54:08 +0200353 } else if (**data != '*') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200354 if (**data == '\n') {
355 ++ctx->line;
356 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200357 comment = 2;
358 }
359 break;
360 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200361 LOGINT_RET(ctx->ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200362 }
363
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200364 if (**data == '\n') {
365 ctx->indent = 0;
366 } else {
367 ++ctx->indent;
368 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200369 ++(*data);
370 }
371
372 if (!**data && (comment > 1)) {
David Sedlákb3077192019-06-19 10:55:37 +0200373 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Unexpected end-of-input, non-terminated comment.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200374 return LY_EVALID;
375 }
376
377 return LY_SUCCESS;
378}
379
Michal Vaskoea5abea2018-09-18 13:10:54 +0200380/**
381 * @brief Read a quoted string from data.
382 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200383 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200384 * @param[in,out] data Data to read from, always moved to currently handled character.
385 * @param[in] arg Type of YANG keyword argument expected.
386 * @param[out] word_p Pointer to the read quoted string.
387 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read quoted string. If not needed,
388 * set to NULL. Otherwise equal to \p word_p.
389 * @param[out] word_len Length of the read quoted string.
390 * @param[out] buf_len Length of the dynamically-allocated buffer \p word_b.
391 * @param[in] indent Current indent (number of YANG spaces). Needed for correct multi-line string
392 * indenation in the final quoted string.
393 *
394 * @return LY_ERR values.
395 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200396static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200397read_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 +0200398 size_t *buf_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200399{
400 /* string: 0 - string ended, 1 - string with ', 2 - string with ", 3 - string with " with last character \,
401 * 4 - string finished, now skipping whitespaces looking for +,
402 * 5 - string continues after +, skipping whitespaces */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200403 unsigned int string, block_indent = 0, current_indent = 0, need_buf = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200404 const char *c;
405
406 if (**data == '\"') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200407 string = 2;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200408 current_indent = block_indent = ctx->indent + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200409 } else {
410 assert(**data == '\'');
411 string = 1;
412 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200413 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200414
415 while (**data && string) {
416 switch (string) {
417 case 1:
418 switch (**data) {
419 case '\'':
420 /* string may be finished, but check for + */
421 string = 4;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200422 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200423 break;
424 default:
425 /* check and store character */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200426 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 +0200427 break;
428 }
429 break;
430 case 2:
431 switch (**data) {
432 case '\"':
433 /* string may be finished, but check for + */
434 string = 4;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200435 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200436 break;
437 case '\\':
438 /* special character following */
439 string = 3;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200440 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200441 break;
442 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200443 if (current_indent < block_indent) {
444 ++current_indent;
445 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200446 } else {
447 /* check and store character */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200448 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 +0200449 }
450 break;
451 case '\t':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200452 if (current_indent < block_indent) {
453 assert(need_buf);
454 current_indent += 8;
455 ctx->indent += 8;
456 for (; current_indent > block_indent; --current_indent, --ctx->indent) {
457 /* store leftover spaces from the tab */
458 c = " ";
459 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 +0200460 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200461 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200462 } else {
463 /* check and store character */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200464 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 +0200465 /* additional characters for indentation - only 1 was count in buf_store_char */
466 ctx->indent += 7;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200467 }
468 break;
469 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200470 if (block_indent) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200471 /* we will be removing the indents so we need our own buffer */
472 need_buf = 1;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200473
474 /* remove trailing tabs and spaces */
475 while ((*word_len) && ((*word_p)[(*word_len) - 1] == '\t' || (*word_p)[(*word_len) - 1] == ' ')) {
476 --(*word_len);
477 }
478
479 /* start indentation */
480 current_indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200481 }
482
483 /* check and store character */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200484 LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, buf_len, need_buf));
485
486 /* maintain line number */
487 ++ctx->line;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200488
489 /* reset context indentation counter for possible string after this one */
490 ctx->indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200491 break;
492 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200493 /* first non-whitespace character, stop eating indentation */
494 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200495
496 /* check and store character */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200497 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 +0200498 break;
499 }
500 break;
501 case 3:
502 /* string encoded characters */
503 switch (**data) {
504 case 'n':
505 c = "\n";
506 break;
507 case 't':
508 c = "\t";
509 break;
510 case '\"':
511 c = *data;
512 break;
513 case '\\':
514 c = *data;
515 break;
516 default:
David Sedlákb3077192019-06-19 10:55:37 +0200517 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Double-quoted string unknown special character '\\%c'.", **data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200518 return LY_EVALID;
519 }
520
521 /* check and store character */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200522 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 +0200523
524 string = 2;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200525 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200526 break;
527 case 4:
528 switch (**data) {
529 case '+':
530 /* string continues */
531 string = 5;
Radek Krejciefd22f62018-09-27 11:47:58 +0200532 need_buf = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200533 break;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200534 case '\n':
535 ++ctx->line;
536 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200537 case ' ':
538 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200539 /* just skip */
540 break;
541 default:
542 /* string is finished */
543 goto string_end;
544 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200545 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200546 break;
547 case 5:
548 switch (**data) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200549 case '\n':
550 ++ctx->line;
551 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200552 case ' ':
553 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200554 /* skip */
555 break;
556 case '\'':
557 string = 1;
558 break;
559 case '\"':
560 string = 2;
561 break;
562 default:
563 /* it must be quoted again */
David Sedlákb3077192019-06-19 10:55:37 +0200564 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Both string parts divided by '+' must be quoted.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200565 return LY_EVALID;
566 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200567 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200568 break;
569 default:
570 return LY_EINT;
571 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200572 }
573
574string_end:
Radek Krejci4e199f52019-05-28 09:09:28 +0200575 if (arg <= Y_PREF_IDENTIF_ARG && !(*word_len)) {
576 /* empty identifier */
David Sedlákb3077192019-06-19 10:55:37 +0200577 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Statement argument is required.");
Radek Krejci4e199f52019-05-28 09:09:28 +0200578 return LY_EVALID;
579 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200580 return LY_SUCCESS;
581}
582
Michal Vaskoea5abea2018-09-18 13:10:54 +0200583/**
584 * @brief Get another YANG string from the raw data.
585 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200586 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200587 * @param[in,out] data Data to read from, always moved to currently handled character.
588 * @param[in] arg Type of YANG keyword argument expected.
Radek Krejcid3ca0632019-04-16 16:54:54 +0200589 * @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 +0200590 * @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 +0200591 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read string. If not needed,
592 * set to NULL. Otherwise equal to \p word_p.
593 * @param[out] word_len Length of the read string.
594 *
595 * @return LY_ERR values.
Michal Vasko7fbc8162018-09-17 10:35:16 +0200596 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200597LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200598get_argument(struct lys_parser_ctx *ctx, const char **data, enum yang_arg arg,
Radek Krejcid3ca0632019-04-16 16:54:54 +0200599 uint16_t *flags, char **word_p, char **word_b, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200600{
Radek Krejci44ceedc2018-10-02 15:54:31 +0200601 size_t buf_len = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200602
603 /* word buffer - dynamically allocated */
604 *word_b = NULL;
605
606 /* word pointer - just a pointer to data */
607 *word_p = NULL;
608
609 *word_len = 0;
610 while (**data) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200611 switch (**data) {
612 case '\'':
613 case '\"':
614 if (*word_len) {
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200615 /* invalid - quotes cannot be in unquoted string and only optsep, ; or { can follow it */
David Sedlákb3077192019-06-19 10:55:37 +0200616 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, *data,
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200617 "unquoted string character, optsep, semicolon or opening brace");
618 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200619 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200620 if (flags) {
621 (*flags) |= (**data) == '\'' ? LYS_SINGLEQUOTED : LYS_DOUBLEQUOTED;
622 }
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100623 LY_CHECK_RET(read_qstring(ctx, data, arg, word_p, word_b, word_len, &buf_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200624 goto str_end;
625 case '/':
Radek Krejci44ceedc2018-10-02 15:54:31 +0200626 if ((*data)[1] == '/') {
627 /* one-line comment */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200628 MOVE_INPUT(ctx, data, 2);
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100629 LY_CHECK_RET(skip_comment(ctx, data, 1));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200630 } else if ((*data)[1] == '*') {
631 /* block comment */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200632 MOVE_INPUT(ctx, data, 2);
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100633 LY_CHECK_RET(skip_comment(ctx, data, 2));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200634 } else {
635 /* not a comment after all */
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100636 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 +0200637 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200638 break;
639 case ' ':
640 if (*word_len) {
641 /* word is finished */
642 goto str_end;
643 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200644 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200645 break;
646 case '\t':
647 if (*word_len) {
648 /* word is finished */
649 goto str_end;
650 }
651 /* tabs count for 8 spaces */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200652 ctx->indent += 8;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200653
654 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200655 break;
656 case '\n':
657 if (*word_len) {
658 /* word is finished */
659 goto str_end;
660 }
661 /* reset indent */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200662 ctx->indent = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200663
664 /* track line numbers */
665 ++ctx->line;
666
667 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200668 break;
669 case ';':
670 case '{':
671 if (*word_len || (arg == Y_MAYBE_STR_ARG)) {
672 /* word is finished */
673 goto str_end;
674 }
675
David Sedlákb3077192019-06-19 10:55:37 +0200676 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, *data, "an argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200677 return LY_EVALID;
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200678 case '}':
679 /* invalid - braces cannot be in unquoted string (opening braces terminates the string and can follow it) */
David Sedlákb3077192019-06-19 10:55:37 +0200680 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, *data,
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200681 "unquoted string character, optsep, semicolon or opening brace");
682 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200683 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200684 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 +0200685 break;
686 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200687 }
688
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200689 /* unexpected end of loop */
David Sedlákb3077192019-06-19 10:55:37 +0200690 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200691 return LY_EVALID;
692
Michal Vasko7fbc8162018-09-17 10:35:16 +0200693str_end:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200694 /* terminating NULL byte for buf */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200695 if (*word_b) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200696 (*word_b) = ly_realloc(*word_b, (*word_len) + 1);
697 LY_CHECK_ERR_RET(!(*word_b), LOGMEM(ctx->ctx), LY_EMEM);
698 (*word_b)[*word_len] = '\0';
Michal Vasko7fbc8162018-09-17 10:35:16 +0200699 *word_p = *word_b;
700 }
701
702 return LY_SUCCESS;
703}
704
Michal Vaskoea5abea2018-09-18 13:10:54 +0200705/**
706 * @brief Get another YANG keyword from the raw data.
707 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200708 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200709 * @param[in,out] data Data to read from, always moved to currently handled character.
710 * @param[out] kw YANG keyword read.
711 * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances.
712 * @param[out] word_len Length of the keyword in the data. Useful for extension instances.
713 *
714 * @return LY_ERR values.
715 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200716LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200717get_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 +0200718{
Michal Vasko7fbc8162018-09-17 10:35:16 +0200719 int prefix;
720 const char *word_start;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200721 unsigned int c;
722 size_t len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200723
724 if (word_p) {
725 *word_p = NULL;
726 *word_len = 0;
727 }
728
729 /* first skip "optsep", comments */
730 while (**data) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200731 switch (**data) {
732 case '/':
733 if ((*data)[1] == '/') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200734 /* one-line comment */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200735 MOVE_INPUT(ctx, data, 2);
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100736 LY_CHECK_RET(skip_comment(ctx, data, 1));
Radek Krejcidcc7b322018-10-11 14:24:02 +0200737 } else if ((*data)[1] == '*') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200738 /* block comment */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200739 MOVE_INPUT(ctx, data, 2);
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100740 LY_CHECK_RET(skip_comment(ctx, data, 2));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200741 } else {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200742 /* error - not a comment after all, keyword cannot start with slash */
David Sedlákb3077192019-06-19 10:55:37 +0200743 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '/'.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200744 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200745 }
Radek Krejci13028282019-06-11 14:56:48 +0200746 continue;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200747 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200748 /* skip whitespaces (optsep) */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200749 ++ctx->line;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200750 ctx->indent = 0;
751 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200752 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200753 /* skip whitespaces (optsep) */
754 ++ctx->indent;
755 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200756 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200757 /* skip whitespaces (optsep) */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200758 ctx->indent += 8;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200759 break;
760 default:
761 /* either a keyword start or an invalid character */
762 goto keyword_start;
763 }
764
765 ++(*data);
766 }
767
768keyword_start:
769 word_start = *data;
David Sedlák5f8f0332019-06-18 16:34:30 +0200770 *kw = lysp_match_kw(ctx, data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200771
David Sedlák1bccdfa2019-06-17 15:55:27 +0200772 if (*kw == YANG_SEMICOLON || *kw == YANG_LEFT_BRACE || *kw == YANG_RIGHT_BRACE) {
Radek Krejci626df482018-10-11 15:06:31 +0200773 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200774 }
775
776 if (*kw != YANG_NONE) {
777 /* make sure we have the whole keyword */
778 switch (**data) {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200779 case '\n':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200780 case '\t':
Radek Krejciabdd8062019-06-11 16:44:19 +0200781 case ' ':
782 /* mandatory "sep" is just checked, not eaten so nothing in the context is updated */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200783 break;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200784 case ':':
785 /* keyword is not actually a keyword, but prefix of an extension.
786 * To avoid repeated check of the prefix syntax, move to the point where the colon was read
787 * and we will be checking the keyword (extension instance) itself */
788 prefix = 1;
789 MOVE_INPUT(ctx, data, 1);
790 goto extension;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200791 case '{':
792 /* allowed only for input and output statements which can be without arguments */
793 if (*kw == YANG_INPUT || *kw == YANG_OUTPUT) {
794 break;
795 }
796 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200797 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200798 MOVE_INPUT(ctx, data, 1);
David Sedlákb3077192019-06-19 10:55:37 +0200799 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(*data - word_start), word_start,
Radek Krejci44ceedc2018-10-02 15:54:31 +0200800 "a keyword followed by a separator");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200801 return LY_EVALID;
802 }
803 } else {
804 /* still can be an extension */
805 prefix = 0;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200806extension:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200807 while (**data && (**data != ' ') && (**data != '\t') && (**data != '\n') && (**data != '{') && (**data != ';')) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200808 LY_CHECK_ERR_RET(ly_getutf8(data, &c, &len),
David Sedlákb3077192019-06-19 10:55:37 +0200809 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, (*data)[-len]), LY_EVALID);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200810 ++ctx->indent;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200811 /* check character validity */
812 LY_CHECK_RET(check_identifierchar(ctx, c, *data - len == word_start ? 1 : 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200813 }
814 if (!**data) {
David Sedlákb3077192019-06-19 10:55:37 +0200815 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200816 return LY_EVALID;
817 }
818
819 /* prefix is mandatory for extension instances */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200820 if (prefix != 2) {
David Sedlákb3077192019-06-19 10:55:37 +0200821 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(*data - word_start), word_start, "a keyword");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200822 return LY_EVALID;
823 }
824
825 *kw = YANG_CUSTOM;
826 }
Radek Krejci626df482018-10-11 15:06:31 +0200827success:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200828 if (word_p) {
829 *word_p = (char *)word_start;
830 *word_len = *data - word_start;
831 }
832
833 return LY_SUCCESS;
834}
835
Michal Vaskoea5abea2018-09-18 13:10:54 +0200836/**
837 * @brief Parse extension instance substatements.
838 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200839 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200840 * @param[in,out] data Data to read from, always moved to currently handled character.
841 * @param[in] word Extension instance substatement name (keyword).
842 * @param[in] word_len Extension instance substatement name length.
843 * @param[in,out] child Children of this extension instance to add to.
844 *
845 * @return LY_ERR values.
846 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200847static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200848parse_ext_substmt(struct lys_parser_ctx *ctx, const char **data, char *word, size_t word_len,
Michal Vasko7fbc8162018-09-17 10:35:16 +0200849 struct lysp_stmt **child)
850{
851 char *buf;
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100852 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200853 enum yang_keyword kw;
854 struct lysp_stmt *stmt, *par_child;
855
856 stmt = calloc(1, sizeof *stmt);
857 LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM);
858
Radek Krejcibb9b1982019-04-08 14:24:59 +0200859 /* insert into parent statements */
860 if (!*child) {
861 *child = stmt;
862 } else {
863 for (par_child = *child; par_child->next; par_child = par_child->next);
864 par_child->next = stmt;
865 }
866
Radek Krejci44ceedc2018-10-02 15:54:31 +0200867 stmt->stmt = lydict_insert(ctx->ctx, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200868
869 /* get optional argument */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200870 LY_CHECK_RET(get_argument(ctx, data, Y_MAYBE_STR_ARG, &stmt->flags, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200871
Radek Krejci0ae092d2018-09-20 16:43:19 +0200872 if (word) {
873 if (buf) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200874 stmt->arg = lydict_insert_zc(ctx->ctx, word);
Radek Krejci0ae092d2018-09-20 16:43:19 +0200875 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200876 stmt->arg = lydict_insert(ctx->ctx, word, word_len);
Radek Krejci0ae092d2018-09-20 16:43:19 +0200877 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200878 }
879
Radek Krejci6d6556c2018-11-08 09:37:45 +0100880 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, ) {
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100881 LY_CHECK_RET(parse_ext_substmt(ctx, data, word, word_len, &stmt->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200882 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200883 return ret;
884}
885
Michal Vaskoea5abea2018-09-18 13:10:54 +0200886/**
887 * @brief Parse extension instance.
888 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200889 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200890 * @param[in,out] data Data to read from, always moved to currently handled character.
891 * @param[in] ext_name Extension instance substatement name (keyword).
892 * @param[in] ext_name_len Extension instance substatement name length.
893 * @param[in] insubstmt Type of the keyword this extension instance is a substatement of.
894 * @param[in] insubstmt_index Index of the keyword instance this extension instance is a substatement of.
895 * @param[in,out] exts Extension instances to add to.
896 *
897 * @return LY_ERR values.
898 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200899static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200900parse_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 +0200901 uint32_t insubstmt_index, struct lysp_ext_instance **exts)
902{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100903 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200904 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200905 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200906 struct lysp_ext_instance *e;
907 enum yang_keyword kw;
908
Radek Krejci2c4e7172018-10-19 15:56:26 +0200909 LY_ARRAY_NEW_RET(ctx->ctx, *exts, e, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200910
911 /* store name and insubstmt info */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200912 e->name = lydict_insert(ctx->ctx, ext_name, ext_name_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200913 e->insubstmt = insubstmt;
914 e->insubstmt_index = insubstmt_index;
915
916 /* get optional argument */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200917 LY_CHECK_RET(get_argument(ctx, data, Y_MAYBE_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200918
Radek Krejci0ae092d2018-09-20 16:43:19 +0200919 if (word) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200920 INSERT_WORD(ctx, buf, e->argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200921 }
922
Radek Krejci6d6556c2018-11-08 09:37:45 +0100923 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100924 LY_CHECK_RET(parse_ext_substmt(ctx, data, word, word_len, &e->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200925 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200926 return ret;
927}
928
Michal Vaskoea5abea2018-09-18 13:10:54 +0200929/**
930 * @brief Parse a generic text field without specific constraints. Those are contact, organization,
931 * description, etc...
932 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200933 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200934 * @param[in,out] data Data to read from, always moved to currently handled character.
935 * @param[in] substmt Type of this substatement.
936 * @param[in] substmt_index Index of this substatement.
937 * @param[in,out] value Place to store the parsed value.
938 * @param[in] arg Type of the YANG keyword argument (of the value).
939 * @param[in,out] exts Extension instances to add to.
940 *
941 * @return LY_ERR values.
942 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200943static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200944parse_text_field(struct lys_parser_ctx *ctx, const char **data, LYEXT_SUBSTMT substmt, uint32_t substmt_index,
Michal Vasko7fbc8162018-09-17 10:35:16 +0200945 const char **value, enum yang_arg arg, struct lysp_ext_instance **exts)
946{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100947 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200948 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200949 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200950 enum yang_keyword kw;
951
952 if (*value) {
David Sedlákb3077192019-06-19 10:55:37 +0200953 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200954 return LY_EVALID;
955 }
956
957 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200958 LY_CHECK_RET(get_argument(ctx, data, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200959
960 /* store value and spend buf if allocated */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200961 INSERT_WORD(ctx, buf, *value, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200962
Radek Krejci6d6556c2018-11-08 09:37:45 +0100963 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200964 switch (kw) {
965 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100966 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, substmt, substmt_index, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200967 break;
968 default:
David Sedlákb3077192019-06-19 10:55:37 +0200969 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200970 return LY_EVALID;
971 }
972 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200973 return ret;
974}
975
Michal Vaskoea5abea2018-09-18 13:10:54 +0200976/**
977 * @brief Parse the yang-version statement.
978 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200979 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200980 * @param[in,out] data Data to read from, always moved to currently handled character.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100981 * @param[out] version Storage for the parsed information.
982 * @param[in, out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200983 *
984 * @return LY_ERR values.
985 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200986static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200987parse_yangversion(struct lys_parser_ctx *ctx, const char **data, uint8_t *version, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200988{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100989 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200990 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200991 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200992 enum yang_keyword kw;
993
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100994 if (*version) {
David Sedlákb3077192019-06-19 10:55:37 +0200995 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200996 return LY_EVALID;
997 }
998
999 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001000 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001001
1002 if ((word_len == 3) && !strncmp(word, "1.0", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001003 *version = LYS_VERSION_1_0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001004 } else if ((word_len == 3) && !strncmp(word, "1.1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001005 *version = LYS_VERSION_1_1;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001006 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001007 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001008 free(buf);
1009 return LY_EVALID;
1010 }
1011 free(buf);
1012
Radek Krejci6d6556c2018-11-08 09:37:45 +01001013 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001014 switch (kw) {
1015 case YANG_CUSTOM:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001016 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_VERSION, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001017 break;
1018 default:
David Sedlákb3077192019-06-19 10:55:37 +02001019 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001020 return LY_EVALID;
1021 }
1022 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001023 return ret;
1024}
1025
Michal Vaskoea5abea2018-09-18 13:10:54 +02001026/**
1027 * @brief Parse the belongs-to statement.
1028 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001029 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001030 * @param[in,out] data Data to read from, always moved to currently handled character.
1031 * @param[in,out] belongsto Place to store the parsed value.
1032 * @param[in,out] prefix Place to store the parsed belongs-to prefix value.
1033 * @param[in,out] exts Extension instances to add to.
1034 *
1035 * @return LY_ERR values.
1036 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001037static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001038parse_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 +02001039{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001040 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001041 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001042 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001043 enum yang_keyword kw;
1044
1045 if (*belongsto) {
David Sedlákb3077192019-06-19 10:55:37 +02001046 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001047 return LY_EVALID;
1048 }
1049
1050 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001051 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001052
Radek Krejci44ceedc2018-10-02 15:54:31 +02001053 INSERT_WORD(ctx, buf, *belongsto, word, word_len);
Radek Krejcif09e4e82019-06-14 15:08:11 +02001054
Radek Krejci6d6556c2018-11-08 09:37:45 +01001055 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001056 switch (kw) {
1057 case YANG_PREFIX:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001058 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_PREFIX, 0, prefix, Y_IDENTIF_ARG, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001059 break;
1060 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001061 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_BELONGSTO, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001062 break;
1063 default:
David Sedlákb3077192019-06-19 10:55:37 +02001064 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001065 return LY_EVALID;
1066 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001067 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001068 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001069checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001070 /* mandatory substatements */
1071 if (!*prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001072 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001073 return LY_EVALID;
1074 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001075 return ret;
1076}
1077
Michal Vaskoea5abea2018-09-18 13:10:54 +02001078/**
1079 * @brief Parse the revision-date statement.
1080 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001081 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001082 * @param[in,out] data Data to read from, always moved to currently handled character.
1083 * @param[in,out] rev Array to store the parsed value in.
1084 * @param[in,out] exts Extension instances to add to.
1085 *
1086 * @return LY_ERR values.
1087 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001088static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001089parse_revisiondate(struct lys_parser_ctx *ctx, const char **data, char *rev, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001090{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001091 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001092 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001093 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001094 enum yang_keyword kw;
1095
1096 if (rev[0]) {
David Sedlákb3077192019-06-19 10:55:37 +02001097 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001098 return LY_EVALID;
1099 }
1100
1101 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001102 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001103
1104 /* check value */
Radek Krejcibbe09a92018-11-08 09:36:54 +01001105 if (lysp_check_date(ctx, word, word_len, "revision-date")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001106 free(buf);
1107 return LY_EVALID;
1108 }
1109
1110 /* store value and spend buf if allocated */
1111 strncpy(rev, word, word_len);
1112 free(buf);
1113
Radek Krejci6d6556c2018-11-08 09:37:45 +01001114 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001115 switch (kw) {
1116 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001117 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_REVISIONDATE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001118 break;
1119 default:
David Sedlákb3077192019-06-19 10:55:37 +02001120 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001121 return LY_EVALID;
1122 }
1123 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001124 return ret;
1125}
1126
Michal Vaskoea5abea2018-09-18 13:10:54 +02001127/**
1128 * @brief Parse the include statement.
1129 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001130 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001131 * @param[in] module_name Name of the module to check name collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001132 * @param[in,out] data Data to read from, always moved to currently handled character.
1133 * @param[in,out] includes Parsed includes to add to.
1134 *
1135 * @return LY_ERR values.
1136 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001137static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001138parse_include(struct lys_parser_ctx *ctx, const char *module_name, const char **data, struct lysp_include **includes)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001139{
Radek Krejcid33273d2018-10-25 14:55:52 +02001140 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001141 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001142 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001143 enum yang_keyword kw;
1144 struct lysp_include *inc;
1145
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001146 LY_ARRAY_NEW_RET(ctx->ctx, *includes, inc, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001147
1148 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001149 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001150
Radek Krejci086c7132018-10-26 15:29:04 +02001151 INSERT_WORD(ctx, buf, inc->name, word, word_len);
1152
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001153 /* submodules share the namespace with the module names, so there must not be
1154 * a module of the same name in the context, no need for revision matching */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001155 if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(ctx->ctx, inc->name)) {
David Sedlákb3077192019-06-19 10:55:37 +02001156 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Name collision between module and submodule of name \"%s\".", inc->name);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001157 return LY_EVALID;
1158 }
1159
Radek Krejci6d6556c2018-11-08 09:37:45 +01001160 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001161 switch (kw) {
1162 case YANG_DESCRIPTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01001163 YANG_CHECK_STMTVER2_RET(ctx, "description", "include");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001164 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 +02001165 break;
1166 case YANG_REFERENCE:
Radek Krejciceaf2122019-01-02 15:03:26 +01001167 YANG_CHECK_STMTVER2_RET(ctx, "reference", "include");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001168 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 +02001169 break;
1170 case YANG_REVISION_DATE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001171 LY_CHECK_RET(parse_revisiondate(ctx, data, inc->rev, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001172 break;
1173 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001174 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001175 break;
1176 default:
David Sedlákb3077192019-06-19 10:55:37 +02001177 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001178 return LY_EVALID;
1179 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001180 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001181 return ret;
1182}
1183
Michal Vaskoea5abea2018-09-18 13:10:54 +02001184/**
1185 * @brief Parse the import statement.
1186 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001187 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001188 * @param[in] module_prefix Prefix of the module to check prefix collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001189 * @param[in,out] data Data to read from, always moved to currently handled character.
1190 * @param[in,out] imports Parsed imports to add to.
1191 *
1192 * @return LY_ERR values.
1193 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001194static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001195parse_import(struct lys_parser_ctx *ctx, const char *module_prefix, const char **data, struct lysp_import **imports)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001196{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001197 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001198 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001199 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001200 enum yang_keyword kw;
1201 struct lysp_import *imp;
1202
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001203 LY_ARRAY_NEW_RET(ctx->ctx, *imports, imp, LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001204
1205 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001206 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02001207 INSERT_WORD(ctx, buf, imp->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001208
Radek Krejci6d6556c2018-11-08 09:37:45 +01001209 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001210 switch (kw) {
1211 case YANG_PREFIX:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001212 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 +01001213 LY_CHECK_RET(lysp_check_prefix(ctx, *imports, module_prefix, &imp->prefix), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001214 break;
1215 case YANG_DESCRIPTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01001216 YANG_CHECK_STMTVER2_RET(ctx, "description", "import");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001217 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 +02001218 break;
1219 case YANG_REFERENCE:
Radek Krejciceaf2122019-01-02 15:03:26 +01001220 YANG_CHECK_STMTVER2_RET(ctx, "reference", "import");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001221 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 +02001222 break;
1223 case YANG_REVISION_DATE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001224 LY_CHECK_RET(parse_revisiondate(ctx, data, imp->rev, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001225 break;
1226 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001227 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001228 break;
1229 default:
David Sedlákb3077192019-06-19 10:55:37 +02001230 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001231 return LY_EVALID;
1232 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001233 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001234 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001235checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001236 /* mandatory substatements */
David Sedlákb3077192019-06-19 10:55:37 +02001237 LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001238
1239 return ret;
1240}
1241
Michal Vaskoea5abea2018-09-18 13:10:54 +02001242/**
1243 * @brief Parse the revision statement.
1244 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001245 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001246 * @param[in,out] data Data to read from, always moved to currently handled character.
1247 * @param[in,out] revs Parsed revisions to add to.
1248 *
1249 * @return LY_ERR values.
1250 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001251static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001252parse_revision(struct lys_parser_ctx *ctx, const char **data, struct lysp_revision **revs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001253{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001254 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001255 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001256 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001257 enum yang_keyword kw;
1258 struct lysp_revision *rev;
1259
Radek Krejci2c4e7172018-10-19 15:56:26 +02001260 LY_ARRAY_NEW_RET(ctx->ctx, *revs, rev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001261
1262 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001263 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001264
1265 /* check value */
Radek Krejcibbe09a92018-11-08 09:36:54 +01001266 if (lysp_check_date(ctx, word, word_len, "revision")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001267 return LY_EVALID;
1268 }
1269
Radek Krejcib7db73a2018-10-24 14:18:40 +02001270 strncpy(rev->date, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001271 free(buf);
1272
Radek Krejci6d6556c2018-11-08 09:37:45 +01001273 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001274 switch (kw) {
1275 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001276 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 +02001277 break;
1278 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001279 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 +02001280 break;
1281 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001282 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001283 break;
1284 default:
David Sedlákb3077192019-06-19 10:55:37 +02001285 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001286 return LY_EVALID;
1287 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001288 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001289 return ret;
1290}
1291
Michal Vaskoea5abea2018-09-18 13:10:54 +02001292/**
1293 * @brief Parse a generic text field that can have more instances such as base.
1294 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001295 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001296 * @param[in,out] data Data to read from, always moved to currently handled character.
1297 * @param[in] substmt Type of this substatement.
1298 * @param[in,out] texts Parsed values to add to.
1299 * @param[in] arg Type of the expected argument.
1300 * @param[in,out] exts Extension instances to add to.
1301 *
1302 * @return LY_ERR values.
1303 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001304static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001305parse_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 +02001306 struct lysp_ext_instance **exts)
1307{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001308 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001309 char *buf, *word;
Radek Krejci151a5b72018-10-19 14:21:44 +02001310 const char **item;
1311 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001312 enum yang_keyword kw;
1313
1314 /* allocate new pointer */
Radek Krejci2c4e7172018-10-19 15:56:26 +02001315 LY_ARRAY_NEW_RET(ctx->ctx, *texts, item, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001316
1317 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001318 LY_CHECK_RET(get_argument(ctx, data, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001319
Radek Krejci151a5b72018-10-19 14:21:44 +02001320 INSERT_WORD(ctx, buf, *item, word, word_len);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001321 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001322 switch (kw) {
1323 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001324 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, substmt, LY_ARRAY_SIZE(*texts) - 1, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001325 break;
1326 default:
David Sedlákb3077192019-06-19 10:55:37 +02001327 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001328 return LY_EVALID;
1329 }
1330 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001331 return ret;
1332}
1333
Michal Vaskoea5abea2018-09-18 13:10:54 +02001334/**
1335 * @brief Parse the config statement.
1336 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001337 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001338 * @param[in,out] data Data to read from, always moved to currently handled character.
1339 * @param[in,out] flags Flags to add to.
1340 * @param[in,out] exts Extension instances to add to.
1341 *
1342 * @return LY_ERR values.
1343 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001344static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001345parse_config(struct lys_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001346{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001347 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001348 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001349 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001350 enum yang_keyword kw;
1351
1352 if (*flags & LYS_CONFIG_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001353 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001354 return LY_EVALID;
1355 }
1356
1357 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001358 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001359
1360 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1361 *flags |= LYS_CONFIG_W;
1362 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1363 *flags |= LYS_CONFIG_R;
1364 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001365 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001366 free(buf);
1367 return LY_EVALID;
1368 }
1369 free(buf);
1370
Radek Krejci6d6556c2018-11-08 09:37:45 +01001371 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001372 switch (kw) {
1373 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001374 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_CONFIG, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001375 break;
1376 default:
David Sedlákb3077192019-06-19 10:55:37 +02001377 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001378 return LY_EVALID;
1379 }
1380 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001381 return ret;
1382}
1383
Michal Vaskoea5abea2018-09-18 13:10:54 +02001384/**
1385 * @brief Parse the mandatory statement.
1386 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001387 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001388 * @param[in,out] data Data to read from, always moved to currently handled character.
1389 * @param[in,out] flags Flags to add to.
1390 * @param[in,out] exts Extension instances to add to.
1391 *
1392 * @return LY_ERR values.
1393 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001394static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001395parse_mandatory(struct lys_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001396{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001397 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001398 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001399 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001400 enum yang_keyword kw;
1401
1402 if (*flags & LYS_MAND_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001403 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001404 return LY_EVALID;
1405 }
1406
1407 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001408 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001409
1410 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1411 *flags |= LYS_MAND_TRUE;
1412 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1413 *flags |= LYS_MAND_FALSE;
1414 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001415 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001416 free(buf);
1417 return LY_EVALID;
1418 }
1419 free(buf);
1420
Radek Krejci6d6556c2018-11-08 09:37:45 +01001421 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001422 switch (kw) {
1423 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001424 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MANDATORY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001425 break;
1426 default:
David Sedlákb3077192019-06-19 10:55:37 +02001427 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001428 return LY_EVALID;
1429 }
1430 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001431 return ret;
1432}
1433
Michal Vaskoea5abea2018-09-18 13:10:54 +02001434/**
1435 * @brief Parse a restriction such as range or length.
1436 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001437 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001438 * @param[in,out] data Data to read from, always moved to currently handled character.
1439 * @param[in] restr_kw Type of this particular restriction.
1440 * @param[in,out] exts Extension instances to add to.
1441 *
1442 * @return LY_ERR values.
1443 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001444static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001445parse_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 +02001446{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001447 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001448 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001449 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001450 enum yang_keyword kw;
1451
1452 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001453 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001454
Radek Krejcif09e4e82019-06-14 15:08:11 +02001455 YANG_CHECK_NONEMPTY(ctx, NULL, word_len, ly_stmt2str(restr_kw));
Radek Krejci44ceedc2018-10-02 15:54:31 +02001456 INSERT_WORD(ctx, buf, restr->arg, word, word_len);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001457 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001458 switch (kw) {
1459 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001460 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 +02001461 break;
1462 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001463 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 +02001464 break;
1465 case YANG_ERROR_APP_TAG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001466 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 +02001467 break;
1468 case YANG_ERROR_MESSAGE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001469 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 +02001470 break;
1471 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001472 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001473 break;
1474 default:
David Sedlákb3077192019-06-19 10:55:37 +02001475 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001476 return LY_EVALID;
1477 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001478 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001479 return ret;
1480}
1481
Michal Vaskoea5abea2018-09-18 13:10:54 +02001482/**
1483 * @brief Parse a restriction that can have more instances such as must.
1484 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001485 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001486 * @param[in,out] data Data to read from, always moved to currently handled character.
1487 * @param[in] restr_kw Type of this particular restriction.
1488 * @param[in,out] restrs Restrictions to add to.
1489 *
1490 * @return LY_ERR values.
1491 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001492static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001493parse_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 +02001494{
1495 struct lysp_restr *restr;
1496
Radek Krejci2c4e7172018-10-19 15:56:26 +02001497 LY_ARRAY_NEW_RET(ctx->ctx, *restrs, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001498 return parse_restr(ctx, data, restr_kw, restr);
1499}
1500
Michal Vaskoea5abea2018-09-18 13:10:54 +02001501/**
1502 * @brief Parse the status statement.
1503 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001504 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001505 * @param[in,out] data Data to read from, always moved to currently handled character.
1506 * @param[in,out] flags Flags to add to.
1507 * @param[in,out] exts Extension instances to add to.
1508 *
1509 * @return LY_ERR values.
1510 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001511static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001512parse_status(struct lys_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001513{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001514 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001515 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001516 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001517 enum yang_keyword kw;
1518
1519 if (*flags & LYS_STATUS_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001520 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001521 return LY_EVALID;
1522 }
1523
1524 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001525 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001526
1527 if ((word_len == 7) && !strncmp(word, "current", word_len)) {
1528 *flags |= LYS_STATUS_CURR;
1529 } else if ((word_len == 10) && !strncmp(word, "deprecated", word_len)) {
1530 *flags |= LYS_STATUS_DEPRC;
1531 } else if ((word_len == 8) && !strncmp(word, "obsolete", word_len)) {
1532 *flags |= LYS_STATUS_OBSLT;
1533 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001534 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001535 free(buf);
1536 return LY_EVALID;
1537 }
1538 free(buf);
1539
Radek Krejci6d6556c2018-11-08 09:37:45 +01001540 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001541 switch (kw) {
1542 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001543 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_STATUS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001544 break;
1545 default:
David Sedlákb3077192019-06-19 10:55:37 +02001546 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001547 return LY_EVALID;
1548 }
1549 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001550 return ret;
1551}
1552
Michal Vaskoea5abea2018-09-18 13:10:54 +02001553/**
1554 * @brief Parse the when statement.
1555 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001556 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001557 * @param[in,out] data Data to read from, always moved to currently handled character.
1558 * @param[in,out] when_p When pointer to parse to.
1559 *
1560 * @return LY_ERR values.
1561 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001562LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001563parse_when(struct lys_parser_ctx *ctx, const char **data, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001564{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001565 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001566 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001567 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001568 enum yang_keyword kw;
1569 struct lysp_when *when;
1570
1571 if (*when_p) {
David Sedlákb3077192019-06-19 10:55:37 +02001572 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001573 return LY_EVALID;
1574 }
1575
1576 when = calloc(1, sizeof *when);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001577 LY_CHECK_ERR_RET(!when, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001578
1579 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001580 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Radek Krejcif09e4e82019-06-14 15:08:11 +02001581 YANG_CHECK_NONEMPTY(ctx, when, word_len, "when");
Radek Krejci44ceedc2018-10-02 15:54:31 +02001582 INSERT_WORD(ctx, buf, when->cond, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001583
Radek Krejcif09e4e82019-06-14 15:08:11 +02001584 *when_p = when;
1585
Radek Krejci6d6556c2018-11-08 09:37:45 +01001586 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001587 switch (kw) {
1588 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001589 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 +02001590 break;
1591 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001592 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 +02001593 break;
1594 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001595 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001596 break;
1597 default:
David Sedlákb3077192019-06-19 10:55:37 +02001598 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001599 return LY_EVALID;
1600 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001601 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001602 return ret;
1603}
1604
Michal Vaskoea5abea2018-09-18 13:10:54 +02001605/**
1606 * @brief Parse the anydata or anyxml statement.
1607 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001608 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001609 * @param[in,out] data Data to read from, always moved to currently handled character.
1610 * @param[in] kw Type of this particular keyword.
1611 * @param[in,out] siblings Siblings to add to.
1612 *
1613 * @return LY_ERR values.
1614 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02001615LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001616parse_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 +02001617{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001618 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001619 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001620 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001621 struct lysp_node *iter;
1622 struct lysp_node_anydata *any;
1623
1624 /* create structure */
1625 any = calloc(1, sizeof *any);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001626 LY_CHECK_ERR_RET(!any, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001627 any->nodetype = kw == YANG_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001628 any->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001629
1630 /* insert into siblings */
1631 if (!*siblings) {
1632 *siblings = (struct lysp_node *)any;
1633 } else {
1634 for (iter = *siblings; iter->next; iter = iter->next);
1635 iter->next = (struct lysp_node *)any;
1636 }
1637
1638 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001639 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02001640 INSERT_WORD(ctx, buf, any->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001641
1642 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01001643 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001644 switch (kw) {
1645 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001646 LY_CHECK_RET(parse_config(ctx, data, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001647 break;
1648 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001649 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 +02001650 break;
1651 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001652 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 +02001653 break;
1654 case YANG_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001655 LY_CHECK_RET(parse_mandatory(ctx, data, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001656 break;
1657 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001658 LY_CHECK_RET(parse_restrs(ctx, data, kw, &any->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001659 break;
1660 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001661 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 +02001662 break;
1663 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001664 LY_CHECK_RET(parse_status(ctx, data, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001665 break;
1666 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001667 LY_CHECK_RET(parse_when(ctx, data, &any->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001668 break;
1669 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001670 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001671 break;
1672 default:
David Sedlákb3077192019-06-19 10:55:37 +02001673 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
Radek Krejcic59bc972018-09-17 16:13:06 +02001674 (any->nodetype & LYS_ANYDATA) == LYS_ANYDATA ? ly_stmt2str(YANG_ANYDATA) : ly_stmt2str(YANG_ANYXML));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001675 return LY_EVALID;
1676 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001677 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001678 return ret;
1679}
1680
Michal Vaskoea5abea2018-09-18 13:10:54 +02001681/**
1682 * @brief Parse the value or position statement. Substatement of type enum statement.
1683 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001684 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001685 * @param[in,out] data Data to read from, always moved to currently handled character.
1686 * @param[in] val_kw Type of this particular keyword.
1687 * @param[in,out] value Value to write to.
1688 * @param[in,out] flags Flags to write to.
1689 * @param[in,out] exts Extension instances to add to.
1690 *
1691 * @return LY_ERR values.
1692 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001693static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001694parse_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 +02001695 struct lysp_ext_instance **exts)
1696{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001697 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001698 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001699 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001700 long int num;
1701 unsigned long int unum;
1702 enum yang_keyword kw;
1703
1704 if (*flags & LYS_SET_VALUE) {
David Sedlákb3077192019-06-19 10:55:37 +02001705 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001706 return LY_EVALID;
1707 }
1708 *flags |= LYS_SET_VALUE;
1709
1710 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001711 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001712
1713 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 +02001714 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001715 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001716 }
1717
1718 errno = 0;
1719 if (val_kw == YANG_VALUE) {
1720 num = strtol(word, &ptr, 10);
Radek Krejci8b764662018-11-14 14:15:13 +01001721 if (num < INT64_C(-2147483648) || num > INT64_C(2147483647)) {
David Sedlákb3077192019-06-19 10:55:37 +02001722 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001723 goto error;
1724 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001725 } else {
1726 unum = strtoul(word, &ptr, 10);
Radek Krejci8b764662018-11-14 14:15:13 +01001727 if (unum > UINT64_C(4294967295)) {
David Sedlákb3077192019-06-19 10:55:37 +02001728 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001729 goto error;
1730 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001731 }
1732 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001733 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001734 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001735 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001736 }
1737 if (errno == ERANGE) {
David Sedlákb3077192019-06-19 10:55:37 +02001738 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001739 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001740 }
1741 if (val_kw == YANG_VALUE) {
1742 *value = num;
1743 } else {
1744 *value = unum;
1745 }
1746 free(buf);
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_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001751 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 +02001752 break;
1753 default:
David Sedlákb3077192019-06-19 10:55:37 +02001754 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001755 return LY_EVALID;
1756 }
1757 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001758 return ret;
Radek Krejci8b764662018-11-14 14:15:13 +01001759
1760error:
1761 free(buf);
1762 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001763}
1764
Michal Vaskoea5abea2018-09-18 13:10:54 +02001765/**
1766 * @brief Parse the enum or bit statement. Substatement of type statement.
1767 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001768 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001769 * @param[in,out] data Data to read from, always moved to currently handled character.
1770 * @param[in] enum_kw Type of this particular keyword.
1771 * @param[in,out] enums Enums or bits to add to.
1772 *
1773 * @return LY_ERR values.
1774 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001775static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001776parse_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 +02001777{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001778 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001779 char *buf, *word;
Radek Krejci8b764662018-11-14 14:15:13 +01001780 size_t word_len, u;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001781 enum yang_keyword kw;
1782 struct lysp_type_enum *enm;
1783
Radek Krejci2c4e7172018-10-19 15:56:26 +02001784 LY_ARRAY_NEW_RET(ctx->ctx, *enums, enm, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001785
1786 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001787 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 +01001788 if (enum_kw == YANG_ENUM) {
1789 if (!word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001790 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not be zero-length.");
Radek Krejci8b764662018-11-14 14:15:13 +01001791 free(buf);
1792 return LY_EVALID;
1793 } else if (isspace(word[0]) || isspace(word[word_len - 1])) {
David Sedlákb3077192019-06-19 10:55:37 +02001794 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not have any leading or trailing whitespaces (\"%.*s\").",
Radek Krejci8b764662018-11-14 14:15:13 +01001795 word_len, word);
1796 free(buf);
1797 return LY_EVALID;
1798 } else {
1799 for (u = 0; u < word_len; ++u) {
1800 if (iscntrl(word[u])) {
1801 LOGWRN(ctx->ctx, "Control characters in enum name should be avoided (\"%.*s\", character number %d).",
1802 word_len, word, u + 1);
1803 break;
1804 }
1805 }
1806 }
1807 } else { /* YANG_BIT */
1808
1809 }
Radek Krejcif09e4e82019-06-14 15:08:11 +02001810 if (enum_kw == YANG_ENUM) {
1811 YANG_CHECK_NONEMPTY(ctx, NULL, word_len, "enum");
1812 }
Radek Krejci44ceedc2018-10-02 15:54:31 +02001813 INSERT_WORD(ctx, buf, enm->name, word, word_len);
Radek Krejci8b764662018-11-14 14:15:13 +01001814 CHECK_UNIQUENESS(ctx, *enums, name, ly_stmt2str(enum_kw), enm->name);
1815
Radek Krejci6d6556c2018-11-08 09:37:45 +01001816 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001817 switch (kw) {
1818 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001819 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 +02001820 break;
1821 case YANG_IF_FEATURE:
Radek Krejciceaf2122019-01-02 15:03:26 +01001822 YANG_CHECK_STMTVER2_RET(ctx, "if-feature", ly_stmt2str(enum_kw));
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001823 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 +02001824 break;
1825 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001826 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 +02001827 break;
1828 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001829 LY_CHECK_RET(parse_status(ctx, data, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001830 break;
1831 case YANG_VALUE:
1832 case YANG_POSITION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001833 LY_CHECK_RET(parse_type_enum_value_pos(ctx, data, kw, &enm->value, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001834 break;
1835 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001836 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001837 break;
1838 default:
David Sedlákb3077192019-06-19 10:55:37 +02001839 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001840 return LY_EVALID;
1841 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001842 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001843 return ret;
1844}
1845
Michal Vaskoea5abea2018-09-18 13:10:54 +02001846/**
1847 * @brief Parse the fraction-digits statement. Substatement of type statement.
1848 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001849 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001850 * @param[in,out] data Data to read from, always moved to currently handled character.
1851 * @param[in,out] fracdig Value to write to.
1852 * @param[in,out] exts Extension instances to add to.
1853 *
1854 * @return LY_ERR values.
1855 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001856static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001857parse_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 +02001858{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001859 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001860 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001861 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001862 unsigned long int num;
1863 enum yang_keyword kw;
1864
1865 if (*fracdig) {
David Sedlákb3077192019-06-19 10:55:37 +02001866 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001867 return LY_EVALID;
1868 }
1869
1870 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001871 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001872
1873 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
David Sedlákb3077192019-06-19 10:55:37 +02001874 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001875 free(buf);
1876 return LY_EVALID;
1877 }
1878
1879 errno = 0;
1880 num = strtoul(word, &ptr, 10);
1881 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001882 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001883 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001884 free(buf);
1885 return LY_EVALID;
1886 }
1887 if ((errno == ERANGE) || (num > 18)) {
David Sedlákb3077192019-06-19 10:55:37 +02001888 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001889 free(buf);
1890 return LY_EVALID;
1891 }
1892 *fracdig = num;
1893 free(buf);
1894
Radek Krejci6d6556c2018-11-08 09:37:45 +01001895 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001896 switch (kw) {
1897 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001898 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_FRACDIGITS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001899 break;
1900 default:
David Sedlákb3077192019-06-19 10:55:37 +02001901 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001902 return LY_EVALID;
1903 }
1904 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001905 return ret;
1906}
1907
Michal Vaskoea5abea2018-09-18 13:10:54 +02001908/**
1909 * @brief Parse the require-instance statement. Substatement of type statement.
1910 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001911 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001912 * @param[in,out] data Data to read from, always moved to currently handled character.
1913 * @param[in,out] reqinst Value to write to.
1914 * @param[in,out] flags Flags to write to.
1915 * @param[in,out] exts Extension instances to add to.
1916 *
1917 * @return LY_ERR values.
1918 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001919static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001920parse_type_reqinstance(struct lys_parser_ctx *ctx, const char **data, uint8_t *reqinst, uint16_t *flags,
Michal Vasko7fbc8162018-09-17 10:35:16 +02001921 struct lysp_ext_instance **exts)
1922{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001923 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001924 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001925 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001926 enum yang_keyword kw;
1927
1928 if (*flags & LYS_SET_REQINST) {
David Sedlákb3077192019-06-19 10:55:37 +02001929 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001930 return LY_EVALID;
1931 }
1932 *flags |= LYS_SET_REQINST;
1933
1934 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001935 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001936
1937 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1938 *reqinst = 1;
1939 } else if ((word_len != 5) || strncmp(word, "false", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001940 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001941 free(buf);
1942 return LY_EVALID;
1943 }
1944 free(buf);
1945
Radek Krejci6d6556c2018-11-08 09:37:45 +01001946 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001947 switch (kw) {
1948 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001949 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_REQINSTANCE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001950 break;
1951 default:
David Sedlákb3077192019-06-19 10:55:37 +02001952 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001953 return LY_EVALID;
1954 }
1955 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001956 return ret;
1957}
1958
Michal Vaskoea5abea2018-09-18 13:10:54 +02001959/**
1960 * @brief Parse the modifier statement. Substatement of type pattern statement.
1961 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001962 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001963 * @param[in,out] data Data to read from, always moved to currently handled character.
1964 * @param[in,out] pat Value to write to.
1965 * @param[in,out] exts Extension instances to add to.
1966 *
1967 * @return LY_ERR values.
1968 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001969static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001970parse_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 +02001971{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001972 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001973 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001974 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001975 enum yang_keyword kw;
1976
1977 if ((*pat)[0] == 0x15) {
David Sedlákb3077192019-06-19 10:55:37 +02001978 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001979 return LY_EVALID;
1980 }
1981
1982 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001983 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001984
1985 if ((word_len != 12) || strncmp(word, "invert-match", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001986 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001987 free(buf);
1988 return LY_EVALID;
1989 }
1990 free(buf);
1991
1992 /* replace the value in the dictionary */
1993 buf = malloc(strlen(*pat) + 1);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001994 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001995 strcpy(buf, *pat);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001996 lydict_remove(ctx->ctx, *pat);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001997
1998 assert(buf[0] == 0x06);
1999 buf[0] = 0x15;
Radek Krejci44ceedc2018-10-02 15:54:31 +02002000 *pat = lydict_insert_zc(ctx->ctx, buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002001
Radek Krejci6d6556c2018-11-08 09:37:45 +01002002 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002003 switch (kw) {
2004 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002005 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MODIFIER, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002006 break;
2007 default:
David Sedlákb3077192019-06-19 10:55:37 +02002008 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002009 return LY_EVALID;
2010 }
2011 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002012 return ret;
2013}
2014
Michal Vaskoea5abea2018-09-18 13:10:54 +02002015/**
2016 * @brief Parse the pattern statement. Substatement of type statement.
2017 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002018 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002019 * @param[in,out] data Data to read from, always moved to currently handled character.
2020 * @param[in,out] patterns Restrictions to add to.
2021 *
2022 * @return LY_ERR values.
2023 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002024static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002025parse_type_pattern(struct lys_parser_ctx *ctx, const char **data, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002026{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002027 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002028 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002029 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002030 enum yang_keyword kw;
2031 struct lysp_restr *restr;
2032
Radek Krejci2c4e7172018-10-19 15:56:26 +02002033 LY_ARRAY_NEW_RET(ctx->ctx, *patterns, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002034
2035 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002036 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002037
2038 /* add special meaning first byte */
2039 if (buf) {
2040 buf = realloc(buf, word_len + 2);
2041 word = buf;
2042 } else {
2043 buf = malloc(word_len + 2);
2044 }
Radek Krejci44ceedc2018-10-02 15:54:31 +02002045 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejci86d106e2018-10-18 09:53:19 +02002046 memmove(buf + 1, word, word_len);
2047 buf[0] = 0x06; /* pattern's default regular-match flag */
2048 buf[word_len + 1] = '\0'; /* terminating NULL byte */
2049 restr->arg = lydict_insert_zc(ctx->ctx, buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002050
Radek Krejci6d6556c2018-11-08 09:37:45 +01002051 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002052 switch (kw) {
2053 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002054 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 +02002055 break;
2056 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002057 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 +02002058 break;
2059 case YANG_ERROR_APP_TAG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002060 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 +02002061 break;
2062 case YANG_ERROR_MESSAGE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002063 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 +02002064 break;
2065 case YANG_MODIFIER:
Radek Krejciceaf2122019-01-02 15:03:26 +01002066 YANG_CHECK_STMTVER2_RET(ctx, "modifier", "pattern");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002067 LY_CHECK_RET(parse_type_pattern_modifier(ctx, data, &restr->arg, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002068 break;
2069 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002070 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002071 break;
2072 default:
David Sedlákb3077192019-06-19 10:55:37 +02002073 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002074 return LY_EVALID;
2075 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002076 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002077 return ret;
2078}
2079
Michal Vaskoea5abea2018-09-18 13:10:54 +02002080/**
2081 * @brief Parse the type statement.
2082 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002083 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002084 * @param[in,out] data Data to read from, always moved to currently handled character.
2085 * @param[in,out] type Type to wrote to.
2086 *
2087 * @return LY_ERR values.
2088 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002089static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002090parse_type(struct lys_parser_ctx *ctx, const char **data, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002091{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002092 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002093 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002094 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002095 enum yang_keyword kw;
2096 struct lysp_type *nest_type;
2097
2098 if (type->name) {
David Sedlákb3077192019-06-19 10:55:37 +02002099 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002100 return LY_EVALID;
2101 }
2102
2103 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002104 LY_CHECK_RET(get_argument(ctx, data, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002105 INSERT_WORD(ctx, buf, type->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002106
Radek Krejci6d6556c2018-11-08 09:37:45 +01002107 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002108 switch (kw) {
2109 case YANG_BASE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002110 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 +01002111 type->flags |= LYS_SET_BASE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002112 break;
2113 case YANG_BIT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002114 LY_CHECK_RET(parse_type_enum(ctx, data, kw, &type->bits));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002115 type->flags |= LYS_SET_BIT;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002116 break;
2117 case YANG_ENUM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002118 LY_CHECK_RET(parse_type_enum(ctx, data, kw, &type->enums));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002119 type->flags |= LYS_SET_ENUM;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002120 break;
2121 case YANG_FRACTION_DIGITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002122 LY_CHECK_RET(parse_type_fracdigits(ctx, data, &type->fraction_digits, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002123 type->flags |= LYS_SET_FRDIGITS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002124 break;
2125 case YANG_LENGTH:
2126 if (type->length) {
David Sedlákb3077192019-06-19 10:55:37 +02002127 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002128 return LY_EVALID;
2129 }
2130 type->length = calloc(1, sizeof *type->length);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002131 LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002132
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002133 LY_CHECK_RET(parse_restr(ctx, data, kw, type->length));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002134 type->flags |= LYS_SET_LENGTH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002135 break;
2136 case YANG_PATH:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002137 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 +01002138 type->flags |= LYS_SET_PATH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002139 break;
2140 case YANG_PATTERN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002141 LY_CHECK_RET(parse_type_pattern(ctx, data, &type->patterns));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002142 type->flags |= LYS_SET_PATTERN;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002143 break;
2144 case YANG_RANGE:
2145 if (type->range) {
David Sedlákb3077192019-06-19 10:55:37 +02002146 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002147 return LY_EVALID;
2148 }
2149 type->range = calloc(1, sizeof *type->range);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002150 LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx->ctx), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002151
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002152 LY_CHECK_RET(parse_restr(ctx, data, kw, type->range));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002153 type->flags |= LYS_SET_RANGE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002154 break;
2155 case YANG_REQUIRE_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002156 LY_CHECK_RET(parse_type_reqinstance(ctx, data, &type->require_instance, &type->flags, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002157 /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002158 break;
2159 case YANG_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002160 LY_ARRAY_NEW_RET(ctx->ctx, type->types, nest_type, LY_EMEM);
2161 LY_CHECK_RET(parse_type(ctx, data, nest_type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002162 type->flags |= LYS_SET_TYPE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002163 break;
2164 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002165 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &type->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002166 break;
2167 default:
David Sedlákb3077192019-06-19 10:55:37 +02002168 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002169 return LY_EVALID;
2170 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002171 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002172 return ret;
2173}
2174
Michal Vaskoea5abea2018-09-18 13:10:54 +02002175/**
2176 * @brief Parse the leaf statement.
2177 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002178 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002179 * @param[in,out] data Data to read from, always moved to currently handled character.
2180 * @param[in,out] siblings Siblings to add to.
2181 *
2182 * @return LY_ERR values.
2183 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002184LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002185parse_leaf(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002186{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002187 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002188 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002189 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002190 enum yang_keyword kw;
2191 struct lysp_node *iter;
2192 struct lysp_node_leaf *leaf;
2193
2194 /* create structure */
2195 leaf = calloc(1, sizeof *leaf);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002196 LY_CHECK_ERR_RET(!leaf, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002197 leaf->nodetype = LYS_LEAF;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002198 leaf->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002199
2200 /* insert into siblings */
2201 if (!*siblings) {
2202 *siblings = (struct lysp_node *)leaf;
2203 } else {
2204 for (iter = *siblings; iter->next; iter = iter->next);
2205 iter->next = (struct lysp_node *)leaf;
2206 }
2207
2208 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002209 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002210 INSERT_WORD(ctx, buf, leaf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002211
2212 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01002213 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002214 switch (kw) {
2215 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002216 LY_CHECK_RET(parse_config(ctx, data, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002217 break;
2218 case YANG_DEFAULT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002219 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 +02002220 break;
2221 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002222 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 +02002223 break;
2224 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002225 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 +02002226 break;
2227 case YANG_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002228 LY_CHECK_RET(parse_mandatory(ctx, data, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002229 break;
2230 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002231 LY_CHECK_RET(parse_restrs(ctx, data, kw, &leaf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002232 break;
2233 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002234 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 +02002235 break;
2236 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002237 LY_CHECK_RET(parse_status(ctx, data, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002238 break;
2239 case YANG_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002240 LY_CHECK_RET(parse_type(ctx, data, &leaf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002241 break;
2242 case YANG_UNITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002243 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 +02002244 break;
2245 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002246 LY_CHECK_RET(parse_when(ctx, data, &leaf->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002247 break;
2248 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002249 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002250 break;
2251 default:
David Sedlákb3077192019-06-19 10:55:37 +02002252 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002253 return LY_EVALID;
2254 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002255 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002256 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002257checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002258 /* mandatory substatements */
2259 if (!leaf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002260 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002261 return LY_EVALID;
2262 }
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002263 if ((leaf->flags & LYS_MAND_TRUE) && (leaf->dflt)) {
David Sedlákb3077192019-06-19 10:55:37 +02002264 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMSCOMB, "mandatory", "default", "leaf");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002265 return LY_EVALID;
2266 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002267
2268 return ret;
2269}
2270
Michal Vaskoea5abea2018-09-18 13:10:54 +02002271/**
2272 * @brief Parse the max-elements statement.
2273 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002274 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002275 * @param[in,out] data Data to read from, always moved to currently handled character.
2276 * @param[in,out] max Value to write to.
2277 * @param[in,out] flags Flags to write to.
2278 * @param[in,out] exts Extension instances to add to.
2279 *
2280 * @return LY_ERR values.
2281 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002282LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002283parse_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 +02002284{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002285 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002286 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002287 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002288 unsigned long int num;
2289 enum yang_keyword kw;
2290
2291 if (*flags & LYS_SET_MAX) {
David Sedlákb3077192019-06-19 10:55:37 +02002292 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002293 return LY_EVALID;
2294 }
2295 *flags |= LYS_SET_MAX;
2296
2297 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002298 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002299
2300 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
David Sedlákb3077192019-06-19 10:55:37 +02002301 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002302 free(buf);
2303 return LY_EVALID;
2304 }
2305
2306 if (strncmp(word, "unbounded", word_len)) {
2307 errno = 0;
2308 num = strtoul(word, &ptr, 10);
2309 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002310 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002311 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002312 free(buf);
2313 return LY_EVALID;
2314 }
2315 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002316 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002317 free(buf);
2318 return LY_EVALID;
2319 }
2320
2321 *max = num;
2322 }
2323 free(buf);
2324
Radek Krejci6d6556c2018-11-08 09:37:45 +01002325 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002326 switch (kw) {
2327 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002328 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MAX, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002329 break;
2330 default:
David Sedlákb3077192019-06-19 10:55:37 +02002331 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002332 return LY_EVALID;
2333 }
2334 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002335 return ret;
2336}
2337
Michal Vaskoea5abea2018-09-18 13:10:54 +02002338/**
2339 * @brief Parse the min-elements statement.
2340 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002341 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002342 * @param[in,out] data Data to read from, always moved to currently handled character.
2343 * @param[in,out] min Value to write to.
2344 * @param[in,out] flags Flags to write to.
2345 * @param[in,out] exts Extension instances to add to.
2346 *
2347 * @return LY_ERR values.
2348 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002349LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002350parse_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 +02002351{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002352 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002353 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002354 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002355 unsigned long int num;
2356 enum yang_keyword kw;
2357
2358 if (*flags & LYS_SET_MIN) {
David Sedlákb3077192019-06-19 10:55:37 +02002359 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002360 return LY_EVALID;
2361 }
2362 *flags |= LYS_SET_MIN;
2363
2364 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002365 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002366
2367 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
David Sedlákb3077192019-06-19 10:55:37 +02002368 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002369 free(buf);
2370 return LY_EVALID;
2371 }
2372
2373 errno = 0;
2374 num = strtoul(word, &ptr, 10);
2375 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002376 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002377 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002378 free(buf);
2379 return LY_EVALID;
2380 }
2381 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002382 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002383 free(buf);
2384 return LY_EVALID;
2385 }
2386 *min = num;
2387 free(buf);
2388
Radek Krejci6d6556c2018-11-08 09:37:45 +01002389 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002390 switch (kw) {
2391 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002392 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MIN, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002393 break;
2394 default:
David Sedlákb3077192019-06-19 10:55:37 +02002395 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002396 return LY_EVALID;
2397 }
2398 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002399 return ret;
2400}
2401
Michal Vaskoea5abea2018-09-18 13:10:54 +02002402/**
2403 * @brief Parse the ordered-by statement.
2404 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002405 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002406 * @param[in,out] data Data to read from, always moved to currently handled character.
2407 * @param[in,out] flags Flags to write to.
2408 * @param[in,out] exts Extension instances to add to.
2409 *
2410 * @return LY_ERR values.
2411 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002412static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002413parse_orderedby(struct lys_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002414{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002415 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002416 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002417 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002418 enum yang_keyword kw;
2419
2420 if (*flags & LYS_ORDBY_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02002421 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002422 return LY_EVALID;
2423 }
2424
2425 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002426 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002427
2428 if ((word_len == 6) && !strncmp(word, "system", word_len)) {
2429 *flags |= LYS_ORDBY_SYSTEM;
2430 } else if ((word_len == 4) && !strncmp(word, "user", word_len)) {
2431 *flags |= LYS_ORDBY_USER;
2432 } else {
David Sedlákb3077192019-06-19 10:55:37 +02002433 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002434 free(buf);
2435 return LY_EVALID;
2436 }
2437 free(buf);
2438
Radek Krejci6d6556c2018-11-08 09:37:45 +01002439 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002440 switch (kw) {
2441 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002442 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_ORDEREDBY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002443 break;
2444 default:
David Sedlákb3077192019-06-19 10:55:37 +02002445 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002446 return LY_EVALID;
2447 }
2448 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002449 return ret;
2450}
2451
Michal Vaskoea5abea2018-09-18 13:10:54 +02002452/**
2453 * @brief Parse the leaf-list statement.
2454 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002455 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002456 * @param[in,out] data Data to read from, always moved to currently handled character.
2457 * @param[in,out] siblings Siblings to add to.
2458 *
2459 * @return LY_ERR values.
2460 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002461LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002462parse_leaflist(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002463{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002464 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002465 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002466 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002467 enum yang_keyword kw;
2468 struct lysp_node *iter;
2469 struct lysp_node_leaflist *llist;
2470
2471 /* create structure */
2472 llist = calloc(1, sizeof *llist);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002473 LY_CHECK_ERR_RET(!llist, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002474 llist->nodetype = LYS_LEAFLIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002475 llist->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002476
2477 /* insert into siblings */
2478 if (!*siblings) {
2479 *siblings = (struct lysp_node *)llist;
2480 } else {
2481 for (iter = *siblings; iter->next; iter = iter->next);
2482 iter->next = (struct lysp_node *)llist;
2483 }
2484
2485 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002486 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002487 INSERT_WORD(ctx, buf, llist->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002488
2489 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01002490 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002491 switch (kw) {
2492 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002493 LY_CHECK_RET(parse_config(ctx, data, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002494 break;
2495 case YANG_DEFAULT:
Radek Krejciceaf2122019-01-02 15:03:26 +01002496 YANG_CHECK_STMTVER2_RET(ctx, "default", "leaf-list");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002497 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 +02002498 break;
2499 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002500 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 +02002501 break;
2502 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002503 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 +02002504 break;
2505 case YANG_MAX_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002506 LY_CHECK_RET(parse_maxelements(ctx, data, &llist->max, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002507 break;
2508 case YANG_MIN_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002509 LY_CHECK_RET(parse_minelements(ctx, data, &llist->min, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002510 break;
2511 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002512 LY_CHECK_RET(parse_restrs(ctx, data, kw, &llist->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002513 break;
2514 case YANG_ORDERED_BY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002515 LY_CHECK_RET(parse_orderedby(ctx, data, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002516 break;
2517 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002518 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 +02002519 break;
2520 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002521 LY_CHECK_RET(parse_status(ctx, data, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002522 break;
2523 case YANG_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002524 LY_CHECK_RET(parse_type(ctx, data, &llist->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002525 break;
2526 case YANG_UNITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002527 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 +02002528 break;
2529 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002530 LY_CHECK_RET(parse_when(ctx, data, &llist->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002531 break;
2532 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002533 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002534 break;
2535 default:
David Sedlákb3077192019-06-19 10:55:37 +02002536 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002537 return LY_EVALID;
2538 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002539 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002540 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002541checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002542 /* mandatory substatements */
2543 if (!llist->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002544 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002545 return LY_EVALID;
2546 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01002547 if ((llist->min) && (llist->dflts)) {
David Sedlákb3077192019-06-19 10:55:37 +02002548 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMSCOMB, "min-elements", "default", "leaf-list");
Radek Krejci0e5d8382018-11-28 16:37:53 +01002549 return LY_EVALID;
2550 }
Radek Krejcidf6cad12018-11-28 17:10:55 +01002551 if (llist->max && llist->min > llist->max) {
David Sedlákb3077192019-06-19 10:55:37 +02002552 LOGVAL_PARSER(ctx, LYVE_SEMANTICS,
Radek Krejcidf6cad12018-11-28 17:10:55 +01002553 "Invalid combination of min-elements and max-elements: min value %u is bigger than the max value %u.",
2554 llist->min, llist->max);
2555 return LY_EVALID;
2556 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002557
2558 return ret;
2559}
2560
Michal Vaskoea5abea2018-09-18 13:10:54 +02002561/**
2562 * @brief Parse the refine statement.
2563 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002564 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002565 * @param[in,out] data Data to read from, always moved to currently handled character.
2566 * @param[in,out] refines Refines to add to.
2567 *
2568 * @return LY_ERR values.
2569 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002570static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002571parse_refine(struct lys_parser_ctx *ctx, const char **data, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002572{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002573 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002574 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002575 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002576 enum yang_keyword kw;
2577 struct lysp_refine *rf;
2578
Radek Krejci2c4e7172018-10-19 15:56:26 +02002579 LY_ARRAY_NEW_RET(ctx->ctx, *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002580
2581 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002582 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Radek Krejcif09e4e82019-06-14 15:08:11 +02002583 YANG_CHECK_NONEMPTY(ctx, NULL, word_len, "refine");
Radek Krejci44ceedc2018-10-02 15:54:31 +02002584 INSERT_WORD(ctx, buf, rf->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002585
Radek Krejci6d6556c2018-11-08 09:37:45 +01002586 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002587 switch (kw) {
2588 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002589 LY_CHECK_RET(parse_config(ctx, data, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002590 break;
2591 case YANG_DEFAULT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002592 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 +02002593 break;
2594 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002595 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 +02002596 break;
2597 case YANG_IF_FEATURE:
Radek Krejciceaf2122019-01-02 15:03:26 +01002598 YANG_CHECK_STMTVER2_RET(ctx, "if-feature", "refine");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002599 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 +02002600 break;
2601 case YANG_MAX_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002602 LY_CHECK_RET(parse_maxelements(ctx, data, &rf->max, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002603 break;
2604 case YANG_MIN_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002605 LY_CHECK_RET(parse_minelements(ctx, data, &rf->min, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002606 break;
2607 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002608 LY_CHECK_RET(parse_restrs(ctx, data, kw, &rf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002609 break;
2610 case YANG_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002611 LY_CHECK_RET(parse_mandatory(ctx, data, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002612 break;
2613 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002614 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 +02002615 break;
2616 case YANG_PRESENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002617 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 +02002618 break;
2619 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002620 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002621 break;
2622 default:
David Sedlákb3077192019-06-19 10:55:37 +02002623 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002624 return LY_EVALID;
2625 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002626 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002627 return ret;
2628}
2629
Michal Vaskoea5abea2018-09-18 13:10:54 +02002630/**
2631 * @brief Parse the typedef statement.
2632 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002633 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002634 * @param[in,out] data Data to read from, always moved to currently handled character.
2635 * @param[in,out] typedefs Typedefs to add to.
2636 *
2637 * @return LY_ERR values.
2638 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002639static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002640parse_typedef(struct lys_parser_ctx *ctx, struct lysp_node *parent, const char **data, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002641{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002642 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002643 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002644 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002645 enum yang_keyword kw;
2646 struct lysp_tpdf *tpdf;
2647
Radek Krejci2c4e7172018-10-19 15:56:26 +02002648 LY_ARRAY_NEW_RET(ctx->ctx, *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002649
2650 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002651 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002652 INSERT_WORD(ctx, buf, tpdf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002653
2654 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01002655 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002656 switch (kw) {
2657 case YANG_DEFAULT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002658 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 +02002659 break;
2660 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002661 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 +02002662 break;
2663 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002664 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 +02002665 break;
2666 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002667 LY_CHECK_RET(parse_status(ctx, data, &tpdf->flags, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002668 break;
2669 case YANG_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002670 LY_CHECK_RET(parse_type(ctx, data, &tpdf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002671 break;
2672 case YANG_UNITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002673 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 +02002674 break;
2675 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002676 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002677 break;
2678 default:
David Sedlákb3077192019-06-19 10:55:37 +02002679 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002680 return LY_EVALID;
2681 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002682 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002683 LY_CHECK_RET(ret);
Radek Krejcibbe09a92018-11-08 09:36:54 +01002684checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002685 /* mandatory substatements */
2686 if (!tpdf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002687 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002688 return LY_EVALID;
2689 }
2690
Radek Krejcibbe09a92018-11-08 09:36:54 +01002691 /* store data for collision check */
Radek Krejci7fc68292019-06-12 13:51:09 +02002692 if (parent && !(parent->nodetype & (LYS_GROUPING | LYS_ACTION | LYS_INOUT | LYS_NOTIF))) {
Radek Krejcibbe09a92018-11-08 09:36:54 +01002693 ly_set_add(&ctx->tpdfs_nodes, parent, 0);
2694 }
2695
Michal Vasko7fbc8162018-09-17 10:35:16 +02002696 return ret;
2697}
2698
Michal Vaskoea5abea2018-09-18 13:10:54 +02002699/**
2700 * @brief Parse the input or output statement.
2701 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002702 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002703 * @param[in,out] data Data to read from, always moved to currently handled character.
2704 * @param[in] kw Type of this particular keyword
2705 * @param[in,out] inout_p Input/output pointer to write to.
2706 *
2707 * @return LY_ERR values.
2708 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002709static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002710parse_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 +02002711{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002712 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002713 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002714 size_t word_len;
Radek Krejci10113652018-11-14 16:56:50 +01002715 enum yang_keyword kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002716
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002717 if (inout_p->nodetype) {
David Sedlákb3077192019-06-19 10:55:37 +02002718 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002719 return LY_EVALID;
2720 }
2721
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002722 /* initiate structure */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002723 inout_p->nodetype = &((struct lysp_action*)parent)->input == inout_p ? LYS_INPUT : LYS_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002724 inout_p->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002725
2726 /* parse substatements */
Radek Krejci7fc68292019-06-12 13:51:09 +02002727 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002728 switch (kw) {
2729 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01002730 YANG_CHECK_STMTVER2_RET(ctx, "anydata", ly_stmt2str(inout_kw));
Radek Krejci10113652018-11-14 16:56:50 +01002731 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002732 case YANG_ANYXML:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002733 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002734 break;
2735 case YANG_CHOICE:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002736 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002737 break;
2738 case YANG_CONTAINER:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002739 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002740 break;
2741 case YANG_LEAF:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002742 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002743 break;
2744 case YANG_LEAF_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002745 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002746 break;
2747 case YANG_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002748 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002749 break;
2750 case YANG_USES:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002751 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002752 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002753 case YANG_TYPEDEF:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002754 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)inout_p, data, &inout_p->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002755 break;
2756 case YANG_MUST:
Radek Krejciceaf2122019-01-02 15:03:26 +01002757 YANG_CHECK_STMTVER2_RET(ctx, "must", ly_stmt2str(inout_kw));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002758 LY_CHECK_RET(parse_restrs(ctx, data, kw, &inout_p->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002759 break;
2760 case YANG_GROUPING:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002761 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)inout_p, &inout_p->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002762 break;
2763 case YANG_CUSTOM:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002764 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inout_p->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002765 break;
2766 default:
David Sedlákb3077192019-06-19 10:55:37 +02002767 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002768 return LY_EVALID;
2769 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002770 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002771 LY_CHECK_RET(ret);
2772checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002773 /* finalize parent pointers to the reallocated items */
Radek Krejci7fc68292019-06-12 13:51:09 +02002774 LY_CHECK_RET(parse_finalize_reallocated(ctx, inout_p->groupings, NULL, NULL, NULL));
2775
Michal Vasko7fbc8162018-09-17 10:35:16 +02002776 return ret;
2777}
2778
Michal Vaskoea5abea2018-09-18 13:10:54 +02002779/**
2780 * @brief Parse the action statement.
2781 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002782 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002783 * @param[in,out] data Data to read from, always moved to currently handled character.
2784 * @param[in,out] actions Actions to add to.
2785 *
2786 * @return LY_ERR values.
2787 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002788LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002789parse_action(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002790{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002791 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002792 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002793 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002794 enum yang_keyword kw;
2795 struct lysp_action *act;
2796
Radek Krejci2c4e7172018-10-19 15:56:26 +02002797 LY_ARRAY_NEW_RET(ctx->ctx, *actions, act, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002798
2799 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002800 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002801 INSERT_WORD(ctx, buf, act->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002802 act->nodetype = LYS_ACTION;
2803 act->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002804
Radek Krejci7fc68292019-06-12 13:51:09 +02002805 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002806 switch (kw) {
2807 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002808 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 +02002809 break;
2810 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002811 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 +02002812 break;
2813 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002814 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 +02002815 break;
2816 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002817 LY_CHECK_RET(parse_status(ctx, data, &act->flags, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002818 break;
2819
2820 case YANG_INPUT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002821 LY_CHECK_RET(parse_inout(ctx, data, kw, (struct lysp_node*)act, &act->input));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002822 break;
2823 case YANG_OUTPUT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002824 LY_CHECK_RET(parse_inout(ctx, data, kw, (struct lysp_node*)act, &act->output));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002825 break;
2826
2827 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01002828 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)act, data, &act->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002829 break;
2830 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002831 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)act, &act->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002832 break;
2833 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002834 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002835 break;
2836 default:
David Sedlákb3077192019-06-19 10:55:37 +02002837 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), parent ? "action" : "rpc");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002838 return LY_EVALID;
2839 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002840 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002841 LY_CHECK_RET(ret);
2842checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002843 /* finalize parent pointers to the reallocated items */
Radek Krejci7fc68292019-06-12 13:51:09 +02002844 LY_CHECK_RET(parse_finalize_reallocated(ctx, act->groupings, NULL, NULL, NULL));
2845
Michal Vasko7fbc8162018-09-17 10:35:16 +02002846 return ret;
2847}
2848
Michal Vaskoea5abea2018-09-18 13:10:54 +02002849/**
2850 * @brief Parse the notification statement.
2851 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002852 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002853 * @param[in,out] data Data to read from, always moved to currently handled character.
2854 * @param[in,out] notifs Notifications to add to.
2855 *
2856 * @return LY_ERR values.
2857 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002858LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002859parse_notif(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002860{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002861 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002862 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002863 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002864 enum yang_keyword kw;
2865 struct lysp_notif *notif;
2866
Radek Krejci2c4e7172018-10-19 15:56:26 +02002867 LY_ARRAY_NEW_RET(ctx->ctx, *notifs, notif, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002868
2869 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002870 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002871 INSERT_WORD(ctx, buf, notif->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002872 notif->nodetype = LYS_NOTIF;
2873 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002874
Radek Krejci7fc68292019-06-12 13:51:09 +02002875 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002876 switch (kw) {
2877 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002878 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 +02002879 break;
2880 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002881 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 +02002882 break;
2883 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002884 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 +02002885 break;
2886 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002887 LY_CHECK_RET(parse_status(ctx, data, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002888 break;
2889
2890 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01002891 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "notification");
Radek Krejci10113652018-11-14 16:56:50 +01002892 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002893 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002894 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002895 break;
2896 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002897 LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002898 break;
2899 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002900 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002901 break;
2902 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002903 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002904 break;
2905 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002906 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002907 break;
2908 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002909 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002910 break;
2911 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002912 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002913 break;
2914
2915 case YANG_MUST:
Radek Krejciceaf2122019-01-02 15:03:26 +01002916 YANG_CHECK_STMTVER2_RET(ctx, "must", "notification");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002917 LY_CHECK_RET(parse_restrs(ctx, data, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002918 break;
2919 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01002920 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)notif, data, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002921 break;
2922 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002923 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002924 break;
2925 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002926 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002927 break;
2928 default:
David Sedlákb3077192019-06-19 10:55:37 +02002929 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002930 return LY_EVALID;
2931 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002932 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002933 LY_CHECK_RET(ret);
2934checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002935 /* finalize parent pointers to the reallocated items */
Radek Krejci7fc68292019-06-12 13:51:09 +02002936 LY_CHECK_RET(parse_finalize_reallocated(ctx, notif->groupings, NULL, NULL, NULL));
2937
Michal Vasko7fbc8162018-09-17 10:35:16 +02002938 return ret;
2939}
2940
Michal Vaskoea5abea2018-09-18 13:10:54 +02002941/**
2942 * @brief Parse the grouping statement.
2943 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002944 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002945 * @param[in,out] data Data to read from, always moved to currently handled character.
2946 * @param[in,out] groupings Groupings to add to.
2947 *
2948 * @return LY_ERR values.
2949 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002950LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002951parse_grouping(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002952{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002953 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002954 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002955 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002956 enum yang_keyword kw;
2957 struct lysp_grp *grp;
2958
Radek Krejci2c4e7172018-10-19 15:56:26 +02002959 LY_ARRAY_NEW_RET(ctx->ctx, *groupings, grp, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002960
2961 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002962 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002963 INSERT_WORD(ctx, buf, grp->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002964 grp->nodetype = LYS_GROUPING;
2965 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002966
Radek Krejci7fc68292019-06-12 13:51:09 +02002967 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002968 switch (kw) {
2969 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002970 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 +02002971 break;
2972 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002973 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 +02002974 break;
2975 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002976 LY_CHECK_RET(parse_status(ctx, data, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002977 break;
2978
2979 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01002980 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "grouping");
Radek Krejci10113652018-11-14 16:56:50 +01002981 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002982 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002983 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002984 break;
2985 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002986 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002987 break;
2988 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002989 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002990 break;
2991 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002992 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002993 break;
2994 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002995 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002996 break;
2997 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002998 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002999 break;
3000 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003001 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003002 break;
3003
3004 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01003005 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)grp, data, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003006 break;
3007 case YANG_ACTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003008 YANG_CHECK_STMTVER2_RET(ctx, "action", "grouping");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003009 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)grp, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003010 break;
3011 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003012 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)grp, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003013 break;
3014 case YANG_NOTIFICATION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003015 YANG_CHECK_STMTVER2_RET(ctx, "notification", "grouping");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003016 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)grp, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003017 break;
3018 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003019 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003020 break;
3021 default:
David Sedlákb3077192019-06-19 10:55:37 +02003022 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003023 return LY_EVALID;
3024 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003025 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003026 LY_CHECK_RET(ret);
3027checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01003028 /* finalize parent pointers to the reallocated items */
Radek Krejci7fc68292019-06-12 13:51:09 +02003029 LY_CHECK_RET(parse_finalize_reallocated(ctx, grp->groupings, NULL, grp->actions, grp->notifs));
3030
Michal Vasko7fbc8162018-09-17 10:35:16 +02003031 return ret;
3032}
3033
Michal Vaskoea5abea2018-09-18 13:10:54 +02003034/**
3035 * @brief Parse the refine statement.
3036 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003037 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003038 * @param[in,out] data Data to read from, always moved to currently handled character.
3039 * @param[in,out] augments Augments to add to.
3040 *
3041 * @return LY_ERR values.
3042 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003043LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003044parse_augment(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003045{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003046 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003047 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003048 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003049 enum yang_keyword kw;
3050 struct lysp_augment *aug;
3051
Radek Krejci2c4e7172018-10-19 15:56:26 +02003052 LY_ARRAY_NEW_RET(ctx->ctx, *augments, aug, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003053
3054 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003055 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Radek Krejcif09e4e82019-06-14 15:08:11 +02003056 YANG_CHECK_NONEMPTY(ctx, NULL, word_len, "augment");
Radek Krejci44ceedc2018-10-02 15:54:31 +02003057 INSERT_WORD(ctx, buf, aug->nodeid, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003058 aug->nodetype = LYS_AUGMENT;
3059 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003060
Radek Krejci7fc68292019-06-12 13:51:09 +02003061 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003062 switch (kw) {
3063 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003064 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 +02003065 break;
3066 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003067 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 +02003068 break;
3069 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003070 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 +02003071 break;
3072 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003073 LY_CHECK_RET(parse_status(ctx, data, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003074 break;
3075 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003076 LY_CHECK_RET(parse_when(ctx, data, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003077 break;
3078
3079 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01003080 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "augment");
Radek Krejci10113652018-11-14 16:56:50 +01003081 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003082 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003083 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003084 break;
3085 case YANG_CASE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003086 LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003087 break;
3088 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003089 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003090 break;
3091 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003092 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003093 break;
3094 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003095 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003096 break;
3097 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003098 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003099 break;
3100 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003101 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003102 break;
3103 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003104 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003105 break;
3106
3107 case YANG_ACTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003108 YANG_CHECK_STMTVER2_RET(ctx, "action", "augment");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003109 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003110 break;
3111 case YANG_NOTIFICATION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003112 YANG_CHECK_STMTVER2_RET(ctx, "notification", "augment");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003113 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003114 break;
3115 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003116 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003117 break;
3118 default:
David Sedlákb3077192019-06-19 10:55:37 +02003119 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003120 return LY_EVALID;
3121 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003122 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003123 LY_CHECK_RET(ret);
3124checks:
3125 /* finalize parent pointers to the reallocated items */
3126 LY_CHECK_RET(parse_finalize_reallocated(ctx, NULL, NULL, aug->actions, aug->notifs));
3127
Michal Vasko7fbc8162018-09-17 10:35:16 +02003128 return ret;
3129}
3130
Michal Vaskoea5abea2018-09-18 13:10:54 +02003131/**
Radek Krejci7fc68292019-06-12 13:51:09 +02003132 * @brief Finalize some of the structures in case they are stored in sized array,
3133 * which can be possibly reallocated and some other data may point to them.
3134 *
3135 * Update parent pointers in the nodes inside grouping/augment/RPC/Notification, which could be reallocated.
3136 *
3137 * @param[in] mod Parsed module to be updated.
3138 * @return LY_ERR value (currently only LY_SUCCESS, but it can change in future).
3139 */
3140static LY_ERR
3141parse_finalize_reallocated(struct lys_parser_ctx *ctx, struct lysp_grp *groupings, struct lysp_augment *augments,
3142 struct lysp_action *actions, struct lysp_notif *notifs)
3143{
3144 unsigned int u, v;
3145 struct lysp_node *child;
3146
3147 /* finalize parent pointers to the reallocated items */
3148
3149 /* gropings */
3150 LY_ARRAY_FOR(groupings, u) {
3151 LY_LIST_FOR(groupings[u].data, child) {
3152 child->parent = (struct lysp_node*)&groupings[u];
3153 }
3154 LY_ARRAY_FOR(groupings[u].actions, v) {
3155 groupings[u].actions[v].parent = (struct lysp_node*)&groupings[u];
3156 }
3157 LY_ARRAY_FOR(groupings[u].notifs, v) {
3158 groupings[u].notifs[v].parent = (struct lysp_node*)&groupings[u];
3159 }
3160 LY_ARRAY_FOR(groupings[u].groupings, v) {
3161 groupings[u].groupings[v].parent = (struct lysp_node*)&groupings[u];
3162 }
3163 if (groupings[u].typedefs) {
3164 ly_set_add(&ctx->tpdfs_nodes, &groupings[u], 0);
3165 }
3166 }
3167
3168 /* augments */
3169 LY_ARRAY_FOR(augments, u) {
3170 LY_LIST_FOR(augments[u].child, child) {
3171 child->parent = (struct lysp_node*)&augments[u];
3172 }
3173 LY_ARRAY_FOR(augments[u].actions, v) {
3174 augments[u].actions[v].parent = (struct lysp_node*)&augments[u];
3175 }
3176 LY_ARRAY_FOR(augments[u].notifs, v) {
3177 augments[u].notifs[v].parent = (struct lysp_node*)&augments[u];
3178 }
3179 }
3180
3181 /* actions */
3182 LY_ARRAY_FOR(actions, u) {
3183 if (actions[u].input.parent) {
3184 actions[u].input.parent = (struct lysp_node*)&actions[u];
3185 LY_LIST_FOR(actions[u].input.data, child) {
3186 child->parent = (struct lysp_node*)&actions[u].input;
3187 }
3188 LY_ARRAY_FOR(actions[u].input.groupings, v) {
3189 actions[u].input.groupings[v].parent = (struct lysp_node*)&actions[u].input;
3190 }
3191 if (actions[u].input.typedefs) {
3192 ly_set_add(&ctx->tpdfs_nodes, &actions[u].input, 0);
3193 }
3194 }
3195 if (actions[u].output.parent) {
3196 actions[u].output.parent = (struct lysp_node*)&actions[u];
3197 LY_LIST_FOR(actions[u].output.data, child) {
3198 child->parent = (struct lysp_node*)&actions[u].output;
3199 }
3200 LY_ARRAY_FOR(actions[u].output.groupings, v) {
3201 actions[u].output.groupings[v].parent = (struct lysp_node*)&actions[u].output;
3202 }
3203 if (actions[u].output.typedefs) {
3204 ly_set_add(&ctx->tpdfs_nodes, &actions[u].output, 0);
3205 }
3206 }
3207 LY_ARRAY_FOR(actions[u].groupings, v) {
3208 actions[u].groupings[v].parent = (struct lysp_node*)&actions[u];
3209 }
3210 if (actions[u].typedefs) {
3211 ly_set_add(&ctx->tpdfs_nodes, &actions[u], 0);
3212 }
3213 }
3214
3215 /* notifications */
3216 LY_ARRAY_FOR(notifs, u) {
3217 LY_LIST_FOR(notifs[u].data, child) {
3218 child->parent = (struct lysp_node*)&notifs[u];
3219 }
3220 LY_ARRAY_FOR(notifs[u].groupings, v) {
3221 notifs[u].groupings[v].parent = (struct lysp_node*)&notifs[u];
3222 }
3223 if (notifs[u].typedefs) {
3224 ly_set_add(&ctx->tpdfs_nodes, &notifs[u], 0);
3225 }
3226 }
3227
3228 return LY_SUCCESS;
3229}
3230
3231/**
Michal Vaskoea5abea2018-09-18 13:10:54 +02003232 * @brief Parse the uses statement.
3233 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003234 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003235 * @param[in,out] data Data to read from, always moved to currently handled character.
3236 * @param[in,out] siblings Siblings to add to.
3237 *
3238 * @return LY_ERR values.
3239 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003240LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003241parse_uses(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003242{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003243 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003244 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003245 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003246 enum yang_keyword kw;
3247 struct lysp_node *iter;
3248 struct lysp_node_uses *uses;
3249
3250 /* create structure */
3251 uses = calloc(1, sizeof *uses);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003252 LY_CHECK_ERR_RET(!uses, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003253 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003254 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003255
3256 /* insert into siblings */
3257 if (!*siblings) {
3258 *siblings = (struct lysp_node *)uses;
3259 } else {
3260 for (iter = *siblings; iter->next; iter = iter->next);
3261 iter->next = (struct lysp_node *)uses;
3262 }
3263
3264 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003265 LY_CHECK_RET(get_argument(ctx, data, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003266 INSERT_WORD(ctx, buf, uses->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003267
3268 /* parse substatements */
Radek Krejci7fc68292019-06-12 13:51:09 +02003269 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003270 switch (kw) {
3271 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003272 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 +02003273 break;
3274 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003275 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 +02003276 break;
3277 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003278 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 +02003279 break;
3280 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003281 LY_CHECK_RET(parse_status(ctx, data, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003282 break;
3283 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003284 LY_CHECK_RET(parse_when(ctx, data, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003285 break;
3286
3287 case YANG_REFINE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003288 LY_CHECK_RET(parse_refine(ctx, data, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003289 break;
3290 case YANG_AUGMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003291 LY_CHECK_RET(parse_augment(ctx, data, (struct lysp_node*)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003292 break;
3293 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003294 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003295 break;
3296 default:
David Sedlákb3077192019-06-19 10:55:37 +02003297 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003298 return LY_EVALID;
3299 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003300 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003301checks:
3302 /* finalize parent pointers to the reallocated items */
3303 LY_CHECK_RET(parse_finalize_reallocated(ctx, NULL, uses->augments, NULL, NULL));
3304
Michal Vasko7fbc8162018-09-17 10:35:16 +02003305 return ret;
3306}
3307
Michal Vaskoea5abea2018-09-18 13:10:54 +02003308/**
3309 * @brief Parse the case statement.
3310 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003311 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003312 * @param[in,out] data Data to read from, always moved to currently handled character.
3313 * @param[in,out] siblings Siblings to add to.
3314 *
3315 * @return LY_ERR values.
3316 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003317LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003318parse_case(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003319{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003320 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003321 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003322 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003323 enum yang_keyword kw;
3324 struct lysp_node *iter;
3325 struct lysp_node_case *cas;
3326
3327 /* create structure */
3328 cas = calloc(1, sizeof *cas);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003329 LY_CHECK_ERR_RET(!cas, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003330 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003331 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003332
3333 /* insert into siblings */
3334 if (!*siblings) {
3335 *siblings = (struct lysp_node *)cas;
3336 } else {
3337 for (iter = *siblings; iter->next; iter = iter->next);
3338 iter->next = (struct lysp_node *)cas;
3339 }
3340
3341 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003342 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003343 INSERT_WORD(ctx, buf, cas->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003344
3345 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01003346 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003347 switch (kw) {
3348 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003349 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 +02003350 break;
3351 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003352 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 +02003353 break;
3354 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003355 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 +02003356 break;
3357 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003358 LY_CHECK_RET(parse_status(ctx, data, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003359 break;
3360 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003361 LY_CHECK_RET(parse_when(ctx, data, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003362 break;
3363
3364 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01003365 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "case");
Radek Krejci10113652018-11-14 16:56:50 +01003366 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003367 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003368 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003369 break;
3370 case YANG_CHOICE:
Radek Krejcia9026eb2018-12-12 16:04:47 +01003371 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003372 break;
3373 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003374 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003375 break;
3376 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003377 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003378 break;
3379 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003380 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003381 break;
3382 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003383 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003384 break;
3385 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003386 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003387 break;
3388 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003389 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003390 break;
3391 default:
David Sedlákb3077192019-06-19 10:55:37 +02003392 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003393 return LY_EVALID;
3394 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003395 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003396 return ret;
3397}
3398
Michal Vaskoea5abea2018-09-18 13:10:54 +02003399/**
3400 * @brief Parse the choice statement.
3401 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003402 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003403 * @param[in,out] data Data to read from, always moved to currently handled character.
3404 * @param[in,out] siblings Siblings to add to.
3405 *
3406 * @return LY_ERR values.
3407 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003408LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003409parse_choice(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003410{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003411 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003412 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003413 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003414 enum yang_keyword kw;
3415 struct lysp_node *iter;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003416 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003417
3418 /* create structure */
Radek Krejci44ceedc2018-10-02 15:54:31 +02003419 choice = calloc(1, sizeof *choice);
3420 LY_CHECK_ERR_RET(!choice, LOGMEM(ctx->ctx), LY_EMEM);
3421 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003422 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003423
3424 /* insert into siblings */
3425 if (!*siblings) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02003426 *siblings = (struct lysp_node *)choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003427 } else {
3428 for (iter = *siblings; iter->next; iter = iter->next);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003429 iter->next = (struct lysp_node *)choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003430 }
3431
3432 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003433 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003434 INSERT_WORD(ctx, buf, choice->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003435
3436 /* parse substatements */
Radek Krejcia9026eb2018-12-12 16:04:47 +01003437 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003438 switch (kw) {
3439 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003440 LY_CHECK_RET(parse_config(ctx, data, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003441 break;
3442 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003443 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 +02003444 break;
3445 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003446 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 +02003447 break;
3448 case YANG_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003449 LY_CHECK_RET(parse_mandatory(ctx, data, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003450 break;
3451 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003452 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 +02003453 break;
3454 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003455 LY_CHECK_RET(parse_status(ctx, data, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003456 break;
3457 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003458 LY_CHECK_RET(parse_when(ctx, data, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003459 break;
3460 case YANG_DEFAULT:
Radek Krejcia9026eb2018-12-12 16:04:47 +01003461 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 +02003462 break;
3463
3464 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01003465 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "choice");
Radek Krejci10113652018-11-14 16:56:50 +01003466 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003467 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003468 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003469 break;
3470 case YANG_CASE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003471 LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003472 break;
3473 case YANG_CHOICE:
Radek Krejciceaf2122019-01-02 15:03:26 +01003474 YANG_CHECK_STMTVER2_RET(ctx, "choice", "choice");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003475 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003476 break;
3477 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003478 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003479 break;
3480 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003481 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003482 break;
3483 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003484 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003485 break;
3486 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003487 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003488 break;
3489 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003490 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003491 break;
3492 default:
David Sedlákb3077192019-06-19 10:55:37 +02003493 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003494 return LY_EVALID;
3495 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003496 }
Radek Krejcia9026eb2018-12-12 16:04:47 +01003497 LY_CHECK_RET(ret);
3498checks:
3499 if ((choice->flags & LYS_MAND_TRUE) && choice->dflt) {
David Sedlákb3077192019-06-19 10:55:37 +02003500 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMSCOMB, "mandatory", "default", "choice");
Radek Krejcia9026eb2018-12-12 16:04:47 +01003501 return LY_EVALID;
3502 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003503 return ret;
3504}
3505
Michal Vaskoea5abea2018-09-18 13:10:54 +02003506/**
3507 * @brief Parse the container statement.
3508 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003509 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003510 * @param[in,out] data Data to read from, always moved to currently handled character.
3511 * @param[in,out] siblings Siblings to add to.
3512 *
3513 * @return LY_ERR values.
3514 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003515LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003516parse_container(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003517{
3518 LY_ERR ret = 0;
3519 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003520 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003521 enum yang_keyword kw;
3522 struct lysp_node *iter;
3523 struct lysp_node_container *cont;
3524
3525 /* create structure */
3526 cont = calloc(1, sizeof *cont);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003527 LY_CHECK_ERR_RET(!cont, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003528 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003529 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003530
3531 /* insert into siblings */
3532 if (!*siblings) {
3533 *siblings = (struct lysp_node *)cont;
3534 } else {
3535 for (iter = *siblings; iter->next; iter = iter->next);
3536 iter->next = (struct lysp_node *)cont;
3537 }
3538
3539 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003540 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003541 INSERT_WORD(ctx, buf, cont->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003542
3543 /* parse substatements */
Radek Krejci7fc68292019-06-12 13:51:09 +02003544 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003545 switch (kw) {
3546 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003547 LY_CHECK_RET(parse_config(ctx, data, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003548 break;
3549 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003550 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 +02003551 break;
3552 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003553 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 +02003554 break;
3555 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003556 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 +02003557 break;
3558 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003559 LY_CHECK_RET(parse_status(ctx, data, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003560 break;
3561 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003562 LY_CHECK_RET(parse_when(ctx, data, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003563 break;
3564 case YANG_PRESENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003565 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 +02003566 break;
3567
3568 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01003569 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "container");
Radek Krejci10113652018-11-14 16:56:50 +01003570 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003571 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003572 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003573 break;
3574 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003575 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003576 break;
3577 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003578 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003579 break;
3580 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003581 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003582 break;
3583 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003584 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003585 break;
3586 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003587 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003588 break;
3589 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003590 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003591 break;
3592
3593 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01003594 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)cont, data, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003595 break;
3596 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003597 LY_CHECK_RET(parse_restrs(ctx, data, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003598 break;
3599 case YANG_ACTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003600 YANG_CHECK_STMTVER2_RET(ctx, "action", "container");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003601 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003602 break;
3603 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003604 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003605 break;
3606 case YANG_NOTIFICATION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003607 YANG_CHECK_STMTVER2_RET(ctx, "notification", "container");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003608 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003609 break;
3610 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003611 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003612 break;
3613 default:
David Sedlákb3077192019-06-19 10:55:37 +02003614 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003615 return LY_EVALID;
3616 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003617 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003618checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01003619 /* finalize parent pointers to the reallocated items */
Radek Krejci7fc68292019-06-12 13:51:09 +02003620 LY_CHECK_RET(parse_finalize_reallocated(ctx, cont->groupings, NULL, cont->actions, cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003621 return ret;
3622}
3623
Michal Vaskoea5abea2018-09-18 13:10:54 +02003624/**
3625 * @brief Parse the list statement.
3626 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003627 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003628 * @param[in,out] data Data to read from, always moved to currently handled character.
3629 * @param[in,out] siblings Siblings to add to.
3630 *
3631 * @return LY_ERR values.
3632 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003633LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003634parse_list(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003635{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003636 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003637 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003638 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003639 enum yang_keyword kw;
3640 struct lysp_node *iter;
3641 struct lysp_node_list *list;
3642
3643 /* create structure */
3644 list = calloc(1, sizeof *list);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003645 LY_CHECK_ERR_RET(!list, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003646 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003647 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003648
3649 /* insert into siblings */
3650 if (!*siblings) {
3651 *siblings = (struct lysp_node *)list;
3652 } else {
3653 for (iter = *siblings; iter->next; iter = iter->next);
3654 iter->next = (struct lysp_node *)list;
3655 }
3656
3657 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003658 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003659 INSERT_WORD(ctx, buf, list->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003660
3661 /* parse substatements */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003662 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003663 switch (kw) {
3664 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003665 LY_CHECK_RET(parse_config(ctx, data, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003666 break;
3667 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003668 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 +02003669 break;
3670 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003671 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 +02003672 break;
3673 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003674 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 +02003675 break;
3676 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003677 LY_CHECK_RET(parse_status(ctx, data, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003678 break;
3679 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003680 LY_CHECK_RET(parse_when(ctx, data, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003681 break;
3682 case YANG_KEY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003683 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 +02003684 break;
3685 case YANG_MAX_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003686 LY_CHECK_RET(parse_maxelements(ctx, data, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003687 break;
3688 case YANG_MIN_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003689 LY_CHECK_RET(parse_minelements(ctx, data, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003690 break;
3691 case YANG_ORDERED_BY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003692 LY_CHECK_RET(parse_orderedby(ctx, data, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003693 break;
3694 case YANG_UNIQUE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003695 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 +02003696 break;
3697
3698 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01003699 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "list");
Radek Krejci10113652018-11-14 16:56:50 +01003700 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003701 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003702 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003703 break;
3704 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003705 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003706 break;
3707 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003708 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003709 break;
3710 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003711 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003712 break;
3713 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003714 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003715 break;
3716 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003717 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003718 break;
3719 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003720 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003721 break;
3722
3723 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01003724 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)list, data, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003725 break;
3726 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003727 LY_CHECK_RET(parse_restrs(ctx, data, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003728 break;
3729 case YANG_ACTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003730 YANG_CHECK_STMTVER2_RET(ctx, "action", "list");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003731 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003732 break;
3733 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003734 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003735 break;
3736 case YANG_NOTIFICATION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003737 YANG_CHECK_STMTVER2_RET(ctx, "notification", "list");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003738 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003739 break;
3740 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003741 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003742 break;
3743 default:
David Sedlákb3077192019-06-19 10:55:37 +02003744 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003745 return LY_EVALID;
3746 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003747 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003748 LY_CHECK_RET(ret);
3749checks:
Radek Krejci7fc68292019-06-12 13:51:09 +02003750 /* finalize parent pointers to the reallocated items */
3751 LY_CHECK_RET(parse_finalize_reallocated(ctx, list->groupings, NULL, list->actions, list->notifs));
3752
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003753 if (list->max && list->min > list->max) {
David Sedlákb3077192019-06-19 10:55:37 +02003754 LOGVAL_PARSER(ctx, LYVE_SEMANTICS,
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003755 "Invalid combination of min-elements and max-elements: min value %u is bigger than the max value %u.",
3756 list->min, list->max);
3757 return LY_EVALID;
3758 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003759
3760 return ret;
3761}
3762
Michal Vaskoea5abea2018-09-18 13:10:54 +02003763/**
3764 * @brief Parse the yin-element statement.
3765 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003766 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003767 * @param[in,out] data Data to read from, always moved to currently handled character.
3768 * @param[in,out] flags Flags to write to.
3769 * @param[in,out] exts Extension instances to add to.
3770 *
3771 * @return LY_ERR values.
3772 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003773static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003774parse_yinelement(struct lys_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003775{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003776 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003777 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003778 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003779 enum yang_keyword kw;
3780
3781 if (*flags & LYS_YINELEM_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02003782 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003783 return LY_EVALID;
3784 }
3785
3786 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003787 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003788
3789 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
3790 *flags |= LYS_YINELEM_TRUE;
3791 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
3792 *flags |= LYS_YINELEM_FALSE;
3793 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003794 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003795 free(buf);
3796 return LY_EVALID;
3797 }
3798 free(buf);
3799
Radek Krejci6d6556c2018-11-08 09:37:45 +01003800 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003801 switch (kw) {
3802 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003803 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_YINELEM, 0, exts));
3804 LY_CHECK_RET(ret); break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003805 default:
David Sedlákb3077192019-06-19 10:55:37 +02003806 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003807 return LY_EVALID;
3808 }
3809 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003810 return ret;
3811}
3812
Michal Vaskoea5abea2018-09-18 13:10:54 +02003813/**
David Sedlák2444f8f2019-07-09 11:02:47 +02003814 * @brief Parse the argument statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003815 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003816 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003817 * @param[in,out] data Data to read from, always moved to currently handled character.
3818 * @param[in,out] argument Value to write to.
3819 * @param[in,out] flags Flags to write to.
3820 * @param[in,out] exts Extension instances to add to.
3821 *
3822 * @return LY_ERR values.
3823 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003824static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003825parse_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 +02003826{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003827 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003828 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003829 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003830 enum yang_keyword kw;
3831
3832 if (*argument) {
David Sedlákb3077192019-06-19 10:55:37 +02003833 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003834 return LY_EVALID;
3835 }
3836
3837 /* get value */
David Sedlák2444f8f2019-07-09 11:02:47 +02003838 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003839 INSERT_WORD(ctx, buf, *argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003840
Radek Krejci6d6556c2018-11-08 09:37:45 +01003841 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003842 switch (kw) {
3843 case YANG_YIN_ELEMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003844 LY_CHECK_RET(parse_yinelement(ctx, data, flags, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003845 break;
3846 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003847 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_ARGUMENT, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003848 break;
3849 default:
David Sedlákb3077192019-06-19 10:55:37 +02003850 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003851 return LY_EVALID;
3852 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003853 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003854 return ret;
3855}
3856
Michal Vaskoea5abea2018-09-18 13:10:54 +02003857/**
3858 * @brief Parse the extension statement.
3859 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003860 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003861 * @param[in,out] data Data to read from, always moved to currently handled character.
3862 * @param[in,out] extensions Extensions to add to.
3863 *
3864 * @return LY_ERR values.
3865 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003866static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003867parse_extension(struct lys_parser_ctx *ctx, const char **data, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003868{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003869 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003870 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003871 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003872 enum yang_keyword kw;
3873 struct lysp_ext *ex;
3874
Radek Krejci2c4e7172018-10-19 15:56:26 +02003875 LY_ARRAY_NEW_RET(ctx->ctx, *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003876
3877 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003878 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003879 INSERT_WORD(ctx, buf, ex->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003880
Radek Krejci6d6556c2018-11-08 09:37:45 +01003881 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003882 switch (kw) {
3883 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003884 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 +02003885 break;
3886 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003887 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 +02003888 break;
3889 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003890 LY_CHECK_RET(parse_status(ctx, data, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003891 break;
3892 case YANG_ARGUMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003893 LY_CHECK_RET(parse_argument(ctx, data, &ex->argument, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003894 break;
3895 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003896 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003897 break;
3898 default:
David Sedlákb3077192019-06-19 10:55:37 +02003899 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003900 return LY_EVALID;
3901 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003902 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003903 return ret;
3904}
3905
Michal Vaskoea5abea2018-09-18 13:10:54 +02003906/**
3907 * @brief Parse the deviate statement.
3908 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003909 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003910 * @param[in,out] data Data to read from, always moved to currently handled character.
3911 * @param[in,out] deviates Deviates to add to.
3912 *
3913 * @return LY_ERR values.
3914 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003915LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003916parse_deviate(struct lys_parser_ctx *ctx, const char **data, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003917{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003918 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003919 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003920 size_t word_len, dev_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003921 enum yang_keyword kw;
3922 struct lysp_deviate *iter, *d;
3923 struct lysp_deviate_add *d_add = NULL;
3924 struct lysp_deviate_rpl *d_rpl = NULL;
3925 struct lysp_deviate_del *d_del = NULL;
3926 const char **d_units, ***d_uniques, ***d_dflts;
3927 struct lysp_restr **d_musts;
3928 uint16_t *d_flags;
3929 uint32_t *d_min, *d_max;
3930
3931 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003932 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003933
3934 if ((word_len == 13) && !strncmp(word, "not-supported", word_len)) {
3935 dev_mod = LYS_DEV_NOT_SUPPORTED;
3936 } else if ((word_len == 3) && !strncmp(word, "add", word_len)) {
3937 dev_mod = LYS_DEV_ADD;
3938 } else if ((word_len == 7) && !strncmp(word, "replace", word_len)) {
3939 dev_mod = LYS_DEV_REPLACE;
3940 } else if ((word_len == 6) && !strncmp(word, "delete", word_len)) {
3941 dev_mod = LYS_DEV_DELETE;
3942 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003943 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003944 free(buf);
3945 return LY_EVALID;
3946 }
3947 free(buf);
3948
3949 /* create structure */
3950 switch (dev_mod) {
3951 case LYS_DEV_NOT_SUPPORTED:
3952 d = calloc(1, sizeof *d);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003953 LY_CHECK_ERR_RET(!d, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003954 break;
3955 case LYS_DEV_ADD:
3956 d_add = calloc(1, sizeof *d_add);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003957 LY_CHECK_ERR_RET(!d_add, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003958 d = (struct lysp_deviate *)d_add;
3959 d_units = &d_add->units;
3960 d_uniques = &d_add->uniques;
3961 d_dflts = &d_add->dflts;
3962 d_musts = &d_add->musts;
3963 d_flags = &d_add->flags;
3964 d_min = &d_add->min;
3965 d_max = &d_add->max;
3966 break;
3967 case LYS_DEV_REPLACE:
3968 d_rpl = calloc(1, sizeof *d_rpl);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003969 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003970 d = (struct lysp_deviate *)d_rpl;
3971 d_units = &d_rpl->units;
3972 d_flags = &d_rpl->flags;
3973 d_min = &d_rpl->min;
3974 d_max = &d_rpl->max;
3975 break;
3976 case LYS_DEV_DELETE:
3977 d_del = calloc(1, sizeof *d_del);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003978 LY_CHECK_ERR_RET(!d_del, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003979 d = (struct lysp_deviate *)d_del;
3980 d_units = &d_del->units;
3981 d_uniques = &d_del->uniques;
3982 d_dflts = &d_del->dflts;
3983 d_musts = &d_del->musts;
3984 d_flags = &d_del->flags;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003985 break;
3986 default:
3987 assert(0);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003988 LOGINT_RET(ctx->ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003989 }
3990 d->mod = dev_mod;
3991
3992 /* insert into siblings */
3993 if (!*deviates) {
3994 *deviates = d;
3995 } else {
3996 for (iter = *deviates; iter->next; iter = iter->next);
3997 iter->next = d;
3998 }
3999
Radek Krejci6d6556c2018-11-08 09:37:45 +01004000 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004001 switch (kw) {
4002 case YANG_CONFIG:
4003 switch (dev_mod) {
4004 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004005 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02004006 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004007 return LY_EVALID;
4008 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004009 LY_CHECK_RET(parse_config(ctx, data, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004010 break;
4011 }
4012 break;
4013 case YANG_DEFAULT:
4014 switch (dev_mod) {
4015 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02004016 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004017 return LY_EVALID;
4018 case LYS_DEV_REPLACE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004019 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 +02004020 break;
4021 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004022 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 +02004023 break;
4024 }
4025 break;
4026 case YANG_MANDATORY:
4027 switch (dev_mod) {
4028 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004029 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02004030 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004031 return LY_EVALID;
4032 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004033 LY_CHECK_RET(parse_mandatory(ctx, data, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004034 break;
4035 }
4036 break;
4037 case YANG_MAX_ELEMENTS:
4038 switch (dev_mod) {
4039 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004040 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02004041 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004042 return LY_EVALID;
4043 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004044 LY_CHECK_RET(parse_maxelements(ctx, data, d_max, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004045 break;
4046 }
4047 break;
4048 case YANG_MIN_ELEMENTS:
4049 switch (dev_mod) {
4050 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004051 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02004052 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004053 return LY_EVALID;
4054 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004055 LY_CHECK_RET(parse_minelements(ctx, data, d_min, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004056 break;
4057 }
4058 break;
4059 case YANG_MUST:
4060 switch (dev_mod) {
4061 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004062 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02004063 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004064 return LY_EVALID;
4065 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004066 LY_CHECK_RET(parse_restrs(ctx, data, kw, d_musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004067 break;
4068 }
4069 break;
4070 case YANG_TYPE:
4071 switch (dev_mod) {
4072 case LYS_DEV_NOT_SUPPORTED:
4073 case LYS_DEV_ADD:
4074 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02004075 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004076 return LY_EVALID;
4077 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004078 if (d_rpl->type) {
David Sedlákb3077192019-06-19 10:55:37 +02004079 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004080 return LY_EVALID;
4081 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004082 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004083 LY_CHECK_ERR_RET(!d_rpl->type, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004084 LY_CHECK_RET(parse_type(ctx, data, d_rpl->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004085 break;
4086 }
4087 break;
4088 case YANG_UNIQUE:
4089 switch (dev_mod) {
4090 case LYS_DEV_NOT_SUPPORTED:
4091 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02004092 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004093 return LY_EVALID;
4094 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004095 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 +02004096 break;
4097 }
4098 break;
4099 case YANG_UNITS:
4100 switch (dev_mod) {
4101 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02004102 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004103 return LY_EVALID;
4104 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004105 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 +02004106 break;
4107 }
4108 break;
4109 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004110 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004111 break;
4112 default:
David Sedlákb3077192019-06-19 10:55:37 +02004113 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004114 return LY_EVALID;
4115 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004116 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004117 return ret;
4118}
4119
Michal Vaskoea5abea2018-09-18 13:10:54 +02004120/**
4121 * @brief Parse the deviation statement.
4122 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004123 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004124 * @param[in,out] data Data to read from, always moved to currently handled character.
4125 * @param[in,out] deviations Deviations to add to.
4126 *
4127 * @return LY_ERR values.
4128 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004129LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02004130parse_deviation(struct lys_parser_ctx *ctx, const char **data, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004131{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004132 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004133 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004134 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004135 enum yang_keyword kw;
4136 struct lysp_deviation *dev;
4137
Radek Krejci2c4e7172018-10-19 15:56:26 +02004138 LY_ARRAY_NEW_RET(ctx->ctx, *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004139
4140 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02004141 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Radek Krejcif09e4e82019-06-14 15:08:11 +02004142 YANG_CHECK_NONEMPTY(ctx, NULL, word_len, "deviation");
Radek Krejci44ceedc2018-10-02 15:54:31 +02004143 INSERT_WORD(ctx, buf, dev->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004144
Radek Krejci6d6556c2018-11-08 09:37:45 +01004145 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004146 switch (kw) {
4147 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004148 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 +02004149 break;
4150 case YANG_DEVIATE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004151 LY_CHECK_RET(parse_deviate(ctx, data, &dev->deviates));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004152 break;
4153 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004154 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 +02004155 break;
4156 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004157 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004158 break;
4159 default:
David Sedlákb3077192019-06-19 10:55:37 +02004160 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004161 return LY_EVALID;
4162 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004163 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004164 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01004165checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004166 /* mandatory substatements */
4167 if (!dev->deviates) {
David Sedlákb3077192019-06-19 10:55:37 +02004168 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004169 return LY_EVALID;
4170 }
4171
4172 return ret;
4173}
4174
Michal Vaskoea5abea2018-09-18 13:10:54 +02004175/**
4176 * @brief Parse the feature statement.
4177 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004178 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004179 * @param[in,out] data Data to read from, always moved to currently handled character.
4180 * @param[in,out] features Features to add to.
4181 *
4182 * @return LY_ERR values.
4183 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004184LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02004185parse_feature(struct lys_parser_ctx *ctx, const char **data, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004186{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004187 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004188 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004189 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004190 enum yang_keyword kw;
4191 struct lysp_feature *feat;
4192
Radek Krejci2c4e7172018-10-19 15:56:26 +02004193 LY_ARRAY_NEW_RET(ctx->ctx, *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004194
4195 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02004196 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02004197 INSERT_WORD(ctx, buf, feat->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004198
Radek Krejci6d6556c2018-11-08 09:37:45 +01004199 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004200 switch (kw) {
4201 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004202 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 +02004203 break;
4204 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004205 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 +02004206 break;
4207 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004208 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 +02004209 break;
4210 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004211 LY_CHECK_RET(parse_status(ctx, data, &feat->flags, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004212 break;
4213 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004214 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004215 break;
4216 default:
David Sedlákb3077192019-06-19 10:55:37 +02004217 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004218 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004219 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004220 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004221 return ret;
4222}
4223
Michal Vaskoea5abea2018-09-18 13:10:54 +02004224/**
4225 * @brief Parse the identity statement.
4226 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004227 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004228 * @param[in,out] data Data to read from, always moved to currently handled character.
4229 * @param[in,out] identities Identities to add to.
4230 *
4231 * @return LY_ERR values.
4232 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004233LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02004234parse_identity(struct lys_parser_ctx *ctx, const char **data, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004235{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004236 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004237 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004238 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004239 enum yang_keyword kw;
4240 struct lysp_ident *ident;
4241
Radek Krejci2c4e7172018-10-19 15:56:26 +02004242 LY_ARRAY_NEW_RET(ctx->ctx, *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004243
4244 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02004245 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02004246 INSERT_WORD(ctx, buf, ident->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004247
Radek Krejci6d6556c2018-11-08 09:37:45 +01004248 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004249 switch (kw) {
4250 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004251 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 +02004252 break;
4253 case YANG_IF_FEATURE:
Radek Krejciceaf2122019-01-02 15:03:26 +01004254 YANG_CHECK_STMTVER2_RET(ctx, "if-feature", "identity");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004255 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 +02004256 break;
4257 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004258 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 +02004259 break;
4260 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004261 LY_CHECK_RET(parse_status(ctx, data, &ident->flags, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004262 break;
4263 case YANG_BASE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004264 if (ident->bases && ctx->mod_version < 2) {
David Sedlákb3077192019-06-19 10:55:37 +02004265 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 +01004266 return LY_EVALID;
4267 }
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004268 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 +02004269 break;
4270 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004271 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004272 break;
4273 default:
David Sedlákb3077192019-06-19 10:55:37 +02004274 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004275 return LY_EVALID;
4276 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004277 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004278 return ret;
4279}
4280
Michal Vaskoea5abea2018-09-18 13:10:54 +02004281/**
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004282 * @brief Parse module substatements.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004283 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004284 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004285 * @param[in,out] data Data to read from, always moved to currently handled character.
4286 * @param[in,out] mod Module to write to.
4287 *
4288 * @return LY_ERR values.
4289 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004290LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02004291parse_module(struct lys_parser_ctx *ctx, const char **data, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004292{
4293 LY_ERR ret = 0;
4294 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004295 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004296 enum yang_keyword kw, prev_kw = 0;
4297 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004298 struct lysp_submodule *dup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004299
4300 /* (sub)module name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02004301 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004302 INSERT_WORD(ctx, buf, mod->mod->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004303
Radek Krejci6d6556c2018-11-08 09:37:45 +01004304 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004305
Radek Krejcie3846472018-10-15 15:24:51 +02004306#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004307 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 +02004308
Michal Vasko7fbc8162018-09-17 10:35:16 +02004309 switch (kw) {
4310 /* module header */
4311 case YANG_NAMESPACE:
4312 case YANG_PREFIX:
Radek Krejcie3846472018-10-15 15:24:51 +02004313 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4314 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004315 case YANG_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004316 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004317 break;
4318 /* linkage */
4319 case YANG_INCLUDE:
4320 case YANG_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004321 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004322 break;
4323 /* meta */
4324 case YANG_ORGANIZATION:
4325 case YANG_CONTACT:
4326 case YANG_DESCRIPTION:
4327 case YANG_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004328 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004329 break;
4330
4331 /* revision */
4332 case YANG_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004333 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004334 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004335 /* body */
4336 case YANG_ANYDATA:
4337 case YANG_ANYXML:
4338 case YANG_AUGMENT:
4339 case YANG_CHOICE:
4340 case YANG_CONTAINER:
4341 case YANG_DEVIATION:
4342 case YANG_EXTENSION:
4343 case YANG_FEATURE:
4344 case YANG_GROUPING:
4345 case YANG_IDENTITY:
4346 case YANG_LEAF:
4347 case YANG_LEAF_LIST:
4348 case YANG_LIST:
4349 case YANG_NOTIFICATION:
4350 case YANG_RPC:
4351 case YANG_TYPEDEF:
4352 case YANG_USES:
4353 case YANG_CUSTOM:
4354 mod_stmt = Y_MOD_BODY;
4355 break;
4356 default:
4357 /* error handled in the next switch */
4358 break;
4359 }
Radek Krejcie3846472018-10-15 15:24:51 +02004360#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004361
Radek Krejcie3846472018-10-15 15:24:51 +02004362 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004363 switch (kw) {
4364 /* module header */
4365 case YANG_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004366 LY_CHECK_RET(parse_yangversion(ctx, data, &mod->mod->version, &mod->exts));
4367 ctx->mod_version = mod->mod->version;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004368 break;
4369 case YANG_NAMESPACE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004370 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 +02004371 break;
4372 case YANG_PREFIX:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004373 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 +02004374 break;
4375
4376 /* linkage */
4377 case YANG_INCLUDE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004378 LY_CHECK_RET(parse_include(ctx, mod->mod->name, data, &mod->includes));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004379 break;
4380 case YANG_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004381 LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, data, &mod->imports));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004382 break;
4383
4384 /* meta */
4385 case YANG_ORGANIZATION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004386 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 +02004387 break;
4388 case YANG_CONTACT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004389 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 +02004390 break;
4391 case YANG_DESCRIPTION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004392 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 +02004393 break;
4394 case YANG_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004395 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 +02004396 break;
4397
4398 /* revision */
4399 case YANG_REVISION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004400 LY_CHECK_RET(parse_revision(ctx, data, &mod->revs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004401 break;
4402
4403 /* body */
4404 case YANG_ANYDATA:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004405 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "module");
Radek Krejci10113652018-11-14 16:56:50 +01004406 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004407 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004408 LY_CHECK_RET(parse_any(ctx, data, kw, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004409 break;
4410 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004411 LY_CHECK_RET(parse_choice(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004412 break;
4413 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004414 LY_CHECK_RET(parse_container(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004415 break;
4416 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004417 LY_CHECK_RET(parse_leaf(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004418 break;
4419 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004420 LY_CHECK_RET(parse_leaflist(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004421 break;
4422 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004423 LY_CHECK_RET(parse_list(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004424 break;
4425 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004426 LY_CHECK_RET(parse_uses(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004427 break;
4428
4429 case YANG_AUGMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004430 LY_CHECK_RET(parse_augment(ctx, data, NULL, &mod->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004431 break;
4432 case YANG_DEVIATION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004433 LY_CHECK_RET(parse_deviation(ctx, data, &mod->deviations));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004434 break;
4435 case YANG_EXTENSION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004436 LY_CHECK_RET(parse_extension(ctx, data, &mod->extensions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004437 break;
4438 case YANG_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004439 LY_CHECK_RET(parse_feature(ctx, data, &mod->features));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004440 break;
4441 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004442 LY_CHECK_RET(parse_grouping(ctx, data, NULL, &mod->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004443 break;
4444 case YANG_IDENTITY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004445 LY_CHECK_RET(parse_identity(ctx, data, &mod->identities));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004446 break;
4447 case YANG_NOTIFICATION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004448 LY_CHECK_RET(parse_notif(ctx, data, NULL, &mod->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004449 break;
4450 case YANG_RPC:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004451 LY_CHECK_RET(parse_action(ctx, data, NULL, &mod->rpcs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004452 break;
4453 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01004454 LY_CHECK_RET(parse_typedef(ctx, NULL, data, &mod->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004455 break;
4456 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004457 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004458 break;
4459
4460 default:
David Sedlákb3077192019-06-19 10:55:37 +02004461 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004462 return LY_EVALID;
4463 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004464 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004465 LY_CHECK_RET(ret);
Radek Krejcie86bf772018-12-14 11:39:53 +01004466
Radek Krejci6d6556c2018-11-08 09:37:45 +01004467checks:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004468 /* finalize parent pointers to the reallocated items */
Radek Krejci7fc68292019-06-12 13:51:09 +02004469 LY_CHECK_RET(parse_finalize_reallocated(ctx, mod->groupings, mod->augments, mod->rpcs, mod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004470
Michal Vasko7fbc8162018-09-17 10:35:16 +02004471 /* mandatory substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004472 if (!mod->mod->ns) {
David Sedlákb3077192019-06-19 10:55:37 +02004473 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004474 return LY_EVALID;
4475 } else if (!mod->mod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004476 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004477 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004478 }
4479
Radek Krejcie9e987e2018-10-31 12:50:27 +01004480 /* submodules share the namespace with the module names, so there must not be
4481 * a submodule of the same name in the context, no need for revision matching */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004482 dup = ly_ctx_get_submodule(ctx->ctx, NULL, mod->mod->name, NULL);
4483 if (dup) {
David Sedlákb3077192019-06-19 10:55:37 +02004484 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 +01004485 return LY_EVALID;
4486 }
4487
4488 return ret;
4489}
4490
4491/**
4492 * @brief Parse submodule substatements.
4493 *
4494 * @param[in] ctx yang parser context for logging.
4495 * @param[in,out] data Data to read from, always moved to currently handled character.
4496 * @param[out] submod Parsed submodule structure.
4497 *
4498 * @return LY_ERR values.
4499 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004500LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02004501parse_submodule(struct lys_parser_ctx *ctx, const char **data, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004502{
4503 LY_ERR ret = 0;
4504 char *buf, *word;
4505 size_t word_len;
4506 enum yang_keyword kw, prev_kw = 0;
4507 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
4508 struct lysp_submodule *dup;
4509
4510 /* submodule name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02004511 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004512 INSERT_WORD(ctx, buf, submod->name, word, word_len);
4513
4514 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
4515
4516#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004517 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 +01004518
4519 switch (kw) {
4520 /* module header */
4521 case YANG_BELONGS_TO:
4522 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4523 break;
4524 case YANG_YANG_VERSION:
4525 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4526 break;
4527 /* linkage */
4528 case YANG_INCLUDE:
4529 case YANG_IMPORT:
4530 CHECK_ORDER(Y_MOD_LINKAGE);
4531 break;
4532 /* meta */
4533 case YANG_ORGANIZATION:
4534 case YANG_CONTACT:
4535 case YANG_DESCRIPTION:
4536 case YANG_REFERENCE:
4537 CHECK_ORDER(Y_MOD_META);
4538 break;
4539
4540 /* revision */
4541 case YANG_REVISION:
4542 CHECK_ORDER(Y_MOD_REVISION);
4543 break;
4544 /* body */
4545 case YANG_ANYDATA:
4546 case YANG_ANYXML:
4547 case YANG_AUGMENT:
4548 case YANG_CHOICE:
4549 case YANG_CONTAINER:
4550 case YANG_DEVIATION:
4551 case YANG_EXTENSION:
4552 case YANG_FEATURE:
4553 case YANG_GROUPING:
4554 case YANG_IDENTITY:
4555 case YANG_LEAF:
4556 case YANG_LEAF_LIST:
4557 case YANG_LIST:
4558 case YANG_NOTIFICATION:
4559 case YANG_RPC:
4560 case YANG_TYPEDEF:
4561 case YANG_USES:
4562 case YANG_CUSTOM:
4563 mod_stmt = Y_MOD_BODY;
4564 break;
4565 default:
4566 /* error handled in the next switch */
4567 break;
4568 }
4569#undef CHECK_ORDER
4570
4571 prev_kw = kw;
4572 switch (kw) {
4573 /* module header */
4574 case YANG_YANG_VERSION:
4575 LY_CHECK_RET(parse_yangversion(ctx, data, &submod->version, &submod->exts));
4576 ctx->mod_version = submod->version;
4577 break;
4578 case YANG_BELONGS_TO:
4579 LY_CHECK_RET(parse_belongsto(ctx, data, &submod->belongsto, &submod->prefix, &submod->exts));
4580 break;
4581
4582 /* linkage */
4583 case YANG_INCLUDE:
4584 LY_CHECK_RET(parse_include(ctx, submod->name, data, &submod->includes));
4585 break;
4586 case YANG_IMPORT:
4587 LY_CHECK_RET(parse_import(ctx, submod->prefix, data, &submod->imports));
4588 break;
4589
4590 /* meta */
4591 case YANG_ORGANIZATION:
4592 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_ORGANIZATION, 0, &submod->org, Y_STR_ARG, &submod->exts));
4593 break;
4594 case YANG_CONTACT:
4595 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_CONTACT, 0, &submod->contact, Y_STR_ARG, &submod->exts));
4596 break;
4597 case YANG_DESCRIPTION:
4598 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &submod->dsc, Y_STR_ARG, &submod->exts));
4599 break;
4600 case YANG_REFERENCE:
4601 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &submod->ref, Y_STR_ARG, &submod->exts));
4602 break;
4603
4604 /* revision */
4605 case YANG_REVISION:
4606 LY_CHECK_RET(parse_revision(ctx, data, &submod->revs));
4607 break;
4608
4609 /* body */
4610 case YANG_ANYDATA:
4611 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "submodule");
4612 /* fall through */
4613 case YANG_ANYXML:
4614 LY_CHECK_RET(parse_any(ctx, data, kw, NULL, &submod->data));
4615 break;
4616 case YANG_CHOICE:
4617 LY_CHECK_RET(parse_choice(ctx, data, NULL, &submod->data));
4618 break;
4619 case YANG_CONTAINER:
4620 LY_CHECK_RET(parse_container(ctx, data, NULL, &submod->data));
4621 break;
4622 case YANG_LEAF:
4623 LY_CHECK_RET(parse_leaf(ctx, data, NULL, &submod->data));
4624 break;
4625 case YANG_LEAF_LIST:
4626 LY_CHECK_RET(parse_leaflist(ctx, data, NULL, &submod->data));
4627 break;
4628 case YANG_LIST:
4629 LY_CHECK_RET(parse_list(ctx, data, NULL, &submod->data));
4630 break;
4631 case YANG_USES:
4632 LY_CHECK_RET(parse_uses(ctx, data, NULL, &submod->data));
4633 break;
4634
4635 case YANG_AUGMENT:
4636 LY_CHECK_RET(parse_augment(ctx, data, NULL, &submod->augments));
4637 break;
4638 case YANG_DEVIATION:
4639 LY_CHECK_RET(parse_deviation(ctx, data, &submod->deviations));
4640 break;
4641 case YANG_EXTENSION:
4642 LY_CHECK_RET(parse_extension(ctx, data, &submod->extensions));
4643 break;
4644 case YANG_FEATURE:
4645 LY_CHECK_RET(parse_feature(ctx, data, &submod->features));
4646 break;
4647 case YANG_GROUPING:
4648 LY_CHECK_RET(parse_grouping(ctx, data, NULL, &submod->groupings));
4649 break;
4650 case YANG_IDENTITY:
4651 LY_CHECK_RET(parse_identity(ctx, data, &submod->identities));
4652 break;
4653 case YANG_NOTIFICATION:
4654 LY_CHECK_RET(parse_notif(ctx, data, NULL, &submod->notifs));
4655 break;
4656 case YANG_RPC:
4657 LY_CHECK_RET(parse_action(ctx, data, NULL, &submod->rpcs));
4658 break;
4659 case YANG_TYPEDEF:
4660 LY_CHECK_RET(parse_typedef(ctx, NULL, data, &submod->typedefs));
4661 break;
4662 case YANG_CUSTOM:
4663 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &submod->exts));
4664 break;
4665
4666 default:
David Sedlákb3077192019-06-19 10:55:37 +02004667 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004668 return LY_EVALID;
4669 }
4670 }
4671 LY_CHECK_RET(ret);
4672
4673checks:
4674 /* finalize parent pointers to the reallocated items */
Radek Krejci7fc68292019-06-12 13:51:09 +02004675 LY_CHECK_RET(parse_finalize_reallocated(ctx, submod->groupings, submod->augments, submod->rpcs, submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004676
4677 /* mandatory substatements */
4678 if (!submod->belongsto) {
David Sedlákb3077192019-06-19 10:55:37 +02004679 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004680 return LY_EVALID;
4681 }
4682
4683 /* submodules share the namespace with the module names, so there must not be
4684 * a submodule of the same name in the context, no need for revision matching */
4685 dup = ly_ctx_get_submodule(ctx->ctx, NULL, submod->name, NULL);
4686 if (dup && strcmp(dup->belongsto, submod->belongsto)) {
David Sedlákb3077192019-06-19 10:55:37 +02004687 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Name collision between submodules of name \"%s\".", dup->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +01004688 return LY_EVALID;
4689 }
4690
Michal Vasko7fbc8162018-09-17 10:35:16 +02004691 return ret;
4692}
4693
Radek Krejcid4557c62018-09-17 11:42:09 +02004694LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02004695yang_parse_submodule(struct lys_parser_ctx *context, const char *data, struct lysp_submodule **submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004696{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004697 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004698 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004699 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004700 enum yang_keyword kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004701 struct lysp_submodule *mod_p = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004702
4703 /* "module"/"submodule" */
Radek Krejcibbe09a92018-11-08 09:36:54 +01004704 ret = get_keyword(context, &data, &kw, &word, &word_len);
4705 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004706
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004707 if (kw == YANG_MODULE) {
4708 LOGERR(context->ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
4709 ret = LY_EINVAL;
4710 goto cleanup;
4711 } else if (kw != YANG_SUBMODULE) {
David Sedlákb3077192019-06-19 10:55:37 +02004712 LOGVAL_PARSER(context, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".",
Radek Krejcic59bc972018-09-17 16:13:06 +02004713 ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004714 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004715 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004716 }
4717
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004718 mod_p = calloc(1, sizeof *mod_p);
4719 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(context->ctx), cleanup);
4720 mod_p->parsing = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004721
4722 /* substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004723 ret = parse_submodule(context, &data, mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004724 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004725
4726 /* read some trailing spaces or new lines */
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004727 while(*data && isspace(*data)) {
4728 data++;
4729 }
4730 if (*data) {
David Sedlákb3077192019-06-19 10:55:37 +02004731 LOGVAL_PARSER(context, LYVE_SYNTAX, "Trailing garbage \"%.*s%s\" after submodule, expected end-of-input.",
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004732 15, data, strlen(data) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004733 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004734 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004735 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004736
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004737 mod_p->parsing = 0;
4738 *submod = mod_p;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004739
Radek Krejcibbe09a92018-11-08 09:36:54 +01004740cleanup:
4741 if (ret) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004742 lysp_submodule_free(context->ctx, mod_p);
4743 }
4744
4745 return ret;
4746}
4747
4748LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02004749yang_parse_module(struct lys_parser_ctx *context, const char *data, struct lys_module *mod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004750{
4751 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004752 char *word;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004753 size_t word_len;
4754 enum yang_keyword kw;
4755 struct lysp_module *mod_p = NULL;
4756
4757 /* "module"/"submodule" */
4758 ret = get_keyword(context, &data, &kw, &word, &word_len);
4759 LY_CHECK_GOTO(ret, cleanup);
4760
4761 if (kw == YANG_SUBMODULE) {
4762 LOGERR(context->ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module.");
4763 ret = LY_EINVAL;
4764 goto cleanup;
4765 } else if (kw != YANG_MODULE) {
David Sedlákb3077192019-06-19 10:55:37 +02004766 LOGVAL_PARSER(context, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004767 ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004768 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004769 goto cleanup;
4770 }
4771
4772 mod_p = calloc(1, sizeof *mod_p);
4773 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(context->ctx), cleanup);
4774 mod_p->mod = mod;
4775 mod_p->parsing = 1;
4776
4777 /* substatements */
4778 ret = parse_module(context, &data, mod_p);
4779 LY_CHECK_GOTO(ret, cleanup);
4780
4781 /* read some trailing spaces or new lines */
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004782 while(*data && isspace(*data)) {
4783 data++;
4784 }
4785 if (*data) {
David Sedlákb3077192019-06-19 10:55:37 +02004786 LOGVAL_PARSER(context, LYVE_SYNTAX, "Trailing garbage \"%.*s%s\" after module, expected end-of-input.",
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004787 15, data, strlen(data) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004788 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004789 goto cleanup;
4790 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004791
4792 mod_p->parsing = 0;
4793 mod->parsed = mod_p;
4794
4795cleanup:
4796 if (ret) {
4797 lysp_module_free(mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004798 }
4799
Michal Vasko7fbc8162018-09-17 10:35:16 +02004800 return ret;
4801}