blob: 7ef4968388e8b5a80153e86c41104f86cb12b03d [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)) { \
60 LOGVAL_YANG(CTX, LY_VCODE_DUPIDENT, IDENT, STMT); \
61 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);}\
76 else {(TARGET) = lydict_insert((CTX)->ctx, 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) { \
Radek Krejci44ceedc2018-10-02 15:54:31 +0200105 LOGVAL_YANG(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) \
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100120 if ((CTX)->mod_version < 2) {LOGVAL_YANG((CTX), LY_VCODE_INCHILDSTMT2, KW, PARENT); return LY_EVALID;}
Radek Krejci10113652018-11-14 16:56:50 +0100121
Radek Krejcie7b95092019-05-15 11:03:07 +0200122static LY_ERR parse_container(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
123static LY_ERR parse_uses(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
124static LY_ERR parse_choice(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
125static LY_ERR parse_case(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
126static LY_ERR parse_list(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
127static LY_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 +0200128
Michal Vaskoea5abea2018-09-18 13:10:54 +0200129/**
130 * @brief Add another character to dynamic buffer, a low-level function.
131 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200132 * Enlarge if needed. Updates \p input as well as \p buf_used.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200133 *
Radek Krejci404251e2018-10-09 12:06:44 +0200134 * @param[in] ctx libyang context for logging.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200135 * @param[in, out] input Input string to process.
136 * @param[in] len Number of bytes to get from the input string and copy into the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200137 * @param[in,out] buf Buffer to use, can be moved by realloc().
138 * @param[in,out] buf_len Current size of the buffer.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200139 * @param[in,out] buf_used Currently used characters of the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200140 *
141 * @return LY_ERR values.
142 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200143static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +0200144buf_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 +0200145{
Radek Krejci44ceedc2018-10-02 15:54:31 +0200146 if (*buf_len <= (*buf_used) + len) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200147 *buf_len += 16;
148 *buf = ly_realloc(*buf, *buf_len);
149 LY_CHECK_ERR_RET(!*buf, LOGMEM(ctx), LY_EMEM);
150 }
Radek Krejcic0917392019-04-10 13:04:04 +0200151 if (*buf_used) {
152 memcpy(&(*buf)[*buf_used], *input, len);
153 } else {
154 memcpy(*buf, *input, len);
155 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200156
Radek Krejci44ceedc2018-10-02 15:54:31 +0200157 (*buf_used) += len;
158 (*input) += len;
159
Michal Vasko7fbc8162018-09-17 10:35:16 +0200160 return LY_SUCCESS;
161}
162
Michal Vaskoea5abea2018-09-18 13:10:54 +0200163/**
Radek Krejci44ceedc2018-10-02 15:54:31 +0200164 * @brief Check that \p c is valid UTF8 code point for YANG string.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200165 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200166 * @param[in] ctx yang parser context for logging.
167 * @param[in] c UTF8 code point of a character to check.
168 * @return LY_ERR values.
169 */
170static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200171check_stringchar(struct lys_parser_ctx *ctx, unsigned int c)
Radek Krejci44ceedc2018-10-02 15:54:31 +0200172{
173 if (!is_yangutf8char(c)) {
174 LOGVAL_YANG(ctx, LY_VCODE_INCHAR, c);
175 return LY_EVALID;
176 }
177 return LY_SUCCESS;
178}
179
180/**
181 * @brief Check that \p c is valid UTF8 code point for YANG identifier.
182 *
183 * @param[in] ctx yang parser context for logging.
184 * @param[in] c UTF8 code point of a character to check.
185 * @param[in] first Flag to check the first character of an identifier, which is more restricted.
Radek Krejcidcc7b322018-10-11 14:24:02 +0200186 * @param[in,out] prefix Storage for internally used flag in case of possible prefixed identifiers:
187 * 0 - colon not yet found (no prefix)
188 * 1 - \p c is the colon character
189 * 2 - prefix already processed, now processing the identifier
190 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200191 * If the identifier cannot be prefixed, NULL is expected.
192 * @return LY_ERR values.
193 */
194static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200195check_identifierchar(struct lys_parser_ctx *ctx, unsigned int c, int first, int *prefix)
Radek Krejci44ceedc2018-10-02 15:54:31 +0200196{
197 if (first || (prefix && (*prefix) == 1)) {
198 if (!is_yangidentstartchar(c)) {
199 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '%c'.", c);
200 return LY_EVALID;
201 }
Radek Krejci9fcacc12018-10-11 15:59:11 +0200202 if (prefix) {
203 if (first) {
204 (*prefix) = 0;
205 } else {
206 (*prefix) = 2;
207 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200208 }
209 } else if (c == ':' && prefix && (*prefix) == 0) {
210 (*prefix) = 1;
211 } else if (!is_yangidentchar(c)) {
212 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Invalid identifier character '%c'.", c);
213 return LY_EVALID;
214 }
215
216 return LY_SUCCESS;
217}
218
219/**
220 * @brief Store a single UTF8 character. It depends whether in a dynamically-allocated buffer or just as a pointer to the data.
221 *
222 * @param[in] ctx yang parser context for logging.
223 * @param[in,out] input Pointer to the string where to get the character to store. Automatically moved to the next character
224 * when function returns.
225 * @param[in] arg Type of the input string to select method of checking character validity.
226 * @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 +0200227 * 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 +0200228 * @param[in,out] word_len Current length of the word pointed to by \p word_p.
229 * @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 +0200230 * @param[in,out] buf_len Current length of \p word_b.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200231 * @param[in] need_buf Flag if the dynamically allocated buffer is required.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200232 *
233 * @return LY_ERR values.
234 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200235static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200236buf_store_char(struct lys_parser_ctx *ctx, const char **input, enum yang_arg arg,
Radek Krejci44ceedc2018-10-02 15:54:31 +0200237 char **word_p, size_t *word_len, char **word_b, size_t *buf_len, int need_buf)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200238{
Radek Krejci404251e2018-10-09 12:06:44 +0200239 int prefix = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200240 unsigned int c;
241 size_t len;
242
Radek Krejcif29b7c32019-04-09 16:17:49 +0200243 /* check valid combination of input paremeters - if need_buf specified, word_b must be provided */
244 assert(!need_buf || (need_buf && word_b));
245
Radek Krejci44ceedc2018-10-02 15:54:31 +0200246 /* get UTF8 code point (and number of bytes coding the character) */
247 LY_CHECK_ERR_RET(ly_getutf8(input, &c, &len),
248 LOGVAL_YANG(ctx, LY_VCODE_INCHAR, (*input)[-len]), LY_EVALID);
249 (*input) -= len;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200250 if (c == '\n') {
251 ctx->indent = 0;
252 } else {
253 /* note - even the multibyte character is count as 1 */
254 ++ctx->indent;
255 }
256
Radek Krejci44ceedc2018-10-02 15:54:31 +0200257 /* check character validity */
258 switch (arg) {
259 case Y_IDENTIF_ARG:
260 LY_CHECK_RET(check_identifierchar(ctx, c, !(*word_len), NULL));
261 break;
262 case Y_PREF_IDENTIF_ARG:
263 LY_CHECK_RET(check_identifierchar(ctx, c, !(*word_len), &prefix));
264 break;
265 case Y_STR_ARG:
266 case Y_MAYBE_STR_ARG:
267 LY_CHECK_RET(check_stringchar(ctx, c));
268 break;
269 }
270
Michal Vasko7fbc8162018-09-17 10:35:16 +0200271 if (word_b && *word_b) {
272 /* add another character into buffer */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200273 if (buf_add_char(ctx->ctx, input, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200274 return LY_EMEM;
275 }
276
277 /* in case of realloc */
278 *word_p = *word_b;
Radek Krejcif29b7c32019-04-09 16:17:49 +0200279 } else if (word_b && need_buf) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200280 /* first time we need a buffer, copy everything read up to now */
281 if (*word_len) {
282 *word_b = malloc(*word_len);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200283 LY_CHECK_ERR_RET(!*word_b, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200284 *buf_len = *word_len;
285 memcpy(*word_b, *word_p, *word_len);
286 }
287
288 /* add this new character into buffer */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200289 if (buf_add_char(ctx->ctx, input, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200290 return LY_EMEM;
291 }
292
293 /* in case of realloc */
294 *word_p = *word_b;
295 } else {
296 /* just remember the first character pointer */
297 if (!*word_p) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200298 *word_p = (char *)(*input);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200299 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200300 /* ... and update the word's length */
301 (*word_len) += len;
302 (*input) += len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200303 }
304
305 return LY_SUCCESS;
306}
307
Michal Vaskoea5abea2018-09-18 13:10:54 +0200308/**
309 * @brief Skip YANG comment in data.
310 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200311 * @param[in] ctx yang parser context for logging.
312 * @param[in,out] data Data to read from, automatically moved after the comment.
313 * @param[in] comment Type of the comment to process:
314 * 1 for a one-line comment,
315 * 2 for a block comment.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200316 *
317 * @return LY_ERR values.
318 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200319static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200320skip_comment(struct lys_parser_ctx *ctx, const char **data, int comment)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200321{
Radek Krejci80dd33e2018-09-26 15:57:18 +0200322 /* internal statuses: 0 - comment ended,
323 * 1 - in line comment,
324 * 2 - in block comment,
325 * 3 - in block comment with last read character '*'
326 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200327 while (**data && comment) {
328 switch (comment) {
329 case 1:
330 if (**data == '\n') {
331 comment = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200332 ++ctx->line;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200333 }
334 break;
335 case 2:
336 if (**data == '*') {
Radek Krejci15c80ca2018-10-09 11:01:31 +0200337 comment = 3;
338 } else if (**data == '\n') {
339 ++ctx->line;
340 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200341 break;
342 case 3:
343 if (**data == '/') {
344 comment = 0;
345 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200346 if (**data == '\n') {
347 ++ctx->line;
348 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200349 comment = 2;
350 }
351 break;
352 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200353 LOGINT_RET(ctx->ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200354 }
355
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200356 if (**data == '\n') {
357 ctx->indent = 0;
358 } else {
359 ++ctx->indent;
360 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200361 ++(*data);
362 }
363
364 if (!**data && (comment > 1)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200365 LOGVAL_YANG(ctx, LYVE_SYNTAX, "Unexpected end-of-file, non-terminated comment.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200366 return LY_EVALID;
367 }
368
369 return LY_SUCCESS;
370}
371
Michal Vaskoea5abea2018-09-18 13:10:54 +0200372/**
373 * @brief Read a quoted string from data.
374 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200375 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200376 * @param[in,out] data Data to read from, always moved to currently handled character.
377 * @param[in] arg Type of YANG keyword argument expected.
378 * @param[out] word_p Pointer to the read quoted string.
379 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read quoted string. If not needed,
380 * set to NULL. Otherwise equal to \p word_p.
381 * @param[out] word_len Length of the read quoted string.
382 * @param[out] buf_len Length of the dynamically-allocated buffer \p word_b.
383 * @param[in] indent Current indent (number of YANG spaces). Needed for correct multi-line string
384 * indenation in the final quoted string.
385 *
386 * @return LY_ERR values.
387 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200388static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200389read_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 +0200390 size_t *buf_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200391{
392 /* string: 0 - string ended, 1 - string with ', 2 - string with ", 3 - string with " with last character \,
393 * 4 - string finished, now skipping whitespaces looking for +,
394 * 5 - string continues after +, skipping whitespaces */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200395 unsigned int string, block_indent = 0, current_indent = 0, need_buf = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200396 const char *c;
397
398 if (**data == '\"') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200399 string = 2;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200400 current_indent = block_indent = ctx->indent + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200401 } else {
402 assert(**data == '\'');
403 string = 1;
404 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200405 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200406
407 while (**data && string) {
408 switch (string) {
409 case 1:
410 switch (**data) {
411 case '\'':
412 /* string may be finished, but check for + */
413 string = 4;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200414 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200415 break;
416 default:
417 /* check and store character */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200418 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 +0200419 break;
420 }
421 break;
422 case 2:
423 switch (**data) {
424 case '\"':
425 /* string may be finished, but check for + */
426 string = 4;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200427 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200428 break;
429 case '\\':
430 /* special character following */
431 string = 3;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200432 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200433 break;
434 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200435 if (current_indent < block_indent) {
436 ++current_indent;
437 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200438 } else {
439 /* check and store character */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200440 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 +0200441 }
442 break;
443 case '\t':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200444 if (current_indent < block_indent) {
445 assert(need_buf);
446 current_indent += 8;
447 ctx->indent += 8;
448 for (; current_indent > block_indent; --current_indent, --ctx->indent) {
449 /* store leftover spaces from the tab */
450 c = " ";
451 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 +0200452 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200453 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200454 } else {
455 /* check and store character */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200456 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 +0200457 /* additional characters for indentation - only 1 was count in buf_store_char */
458 ctx->indent += 7;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200459 }
460 break;
461 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200462 if (block_indent) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200463 /* we will be removing the indents so we need our own buffer */
464 need_buf = 1;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200465
466 /* remove trailing tabs and spaces */
467 while ((*word_len) && ((*word_p)[(*word_len) - 1] == '\t' || (*word_p)[(*word_len) - 1] == ' ')) {
468 --(*word_len);
469 }
470
471 /* start indentation */
472 current_indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200473 }
474
475 /* check and store character */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200476 LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, buf_len, need_buf));
477
478 /* maintain line number */
479 ++ctx->line;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200480
481 /* reset context indentation counter for possible string after this one */
482 ctx->indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200483 break;
484 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200485 /* first non-whitespace character, stop eating indentation */
486 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200487
488 /* check and store character */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200489 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 +0200490 break;
491 }
492 break;
493 case 3:
494 /* string encoded characters */
495 switch (**data) {
496 case 'n':
497 c = "\n";
498 break;
499 case 't':
500 c = "\t";
501 break;
502 case '\"':
503 c = *data;
504 break;
505 case '\\':
506 c = *data;
507 break;
508 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200509 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Double-quoted string unknown special character '\\%c'.", **data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200510 return LY_EVALID;
511 }
512
513 /* check and store character */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200514 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 +0200515
516 string = 2;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200517 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200518 break;
519 case 4:
520 switch (**data) {
521 case '+':
522 /* string continues */
523 string = 5;
Radek Krejciefd22f62018-09-27 11:47:58 +0200524 need_buf = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200525 break;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200526 case '\n':
527 ++ctx->line;
528 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200529 case ' ':
530 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200531 /* just skip */
532 break;
533 default:
534 /* string is finished */
535 goto string_end;
536 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200537 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200538 break;
539 case 5:
540 switch (**data) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200541 case '\n':
542 ++ctx->line;
543 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200544 case ' ':
545 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200546 /* skip */
547 break;
548 case '\'':
549 string = 1;
550 break;
551 case '\"':
552 string = 2;
553 break;
554 default:
555 /* it must be quoted again */
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200556 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Both string parts divided by '+' must be quoted.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200557 return LY_EVALID;
558 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200559 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200560 break;
561 default:
562 return LY_EINT;
563 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200564 }
565
566string_end:
567 return LY_SUCCESS;
568}
569
Michal Vaskoea5abea2018-09-18 13:10:54 +0200570/**
571 * @brief Get another YANG string from the raw data.
572 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200573 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200574 * @param[in,out] data Data to read from, always moved to currently handled character.
575 * @param[in] arg Type of YANG keyword argument expected.
Radek Krejcid3ca0632019-04-16 16:54:54 +0200576 * @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 +0200577 * @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 +0200578 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read string. If not needed,
579 * set to NULL. Otherwise equal to \p word_p.
580 * @param[out] word_len Length of the read string.
581 *
582 * @return LY_ERR values.
Michal Vasko7fbc8162018-09-17 10:35:16 +0200583 */
584static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200585get_argument(struct lys_parser_ctx *ctx, const char **data, enum yang_arg arg,
Radek Krejcid3ca0632019-04-16 16:54:54 +0200586 uint16_t *flags, char **word_p, char **word_b, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200587{
Radek Krejci44ceedc2018-10-02 15:54:31 +0200588 size_t buf_len = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200589
590 /* word buffer - dynamically allocated */
591 *word_b = NULL;
592
593 /* word pointer - just a pointer to data */
594 *word_p = NULL;
595
596 *word_len = 0;
597 while (**data) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200598 switch (**data) {
599 case '\'':
600 case '\"':
601 if (*word_len) {
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200602 /* invalid - quotes cannot be in unquoted string and only optsep, ; or { can follow it */
603 LOGVAL_YANG(ctx, LY_VCODE_INSTREXP, 1, *data,
604 "unquoted string character, optsep, semicolon or opening brace");
605 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200606 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200607 if (flags) {
608 (*flags) |= (**data) == '\'' ? LYS_SINGLEQUOTED : LYS_DOUBLEQUOTED;
609 }
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100610 LY_CHECK_RET(read_qstring(ctx, data, arg, word_p, word_b, word_len, &buf_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200611 goto str_end;
612 case '/':
Radek Krejci44ceedc2018-10-02 15:54:31 +0200613 if ((*data)[1] == '/') {
614 /* one-line comment */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200615 MOVE_INPUT(ctx, data, 2);
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100616 LY_CHECK_RET(skip_comment(ctx, data, 1));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200617 } else if ((*data)[1] == '*') {
618 /* block comment */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200619 MOVE_INPUT(ctx, data, 2);
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100620 LY_CHECK_RET(skip_comment(ctx, data, 2));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200621 } else {
622 /* not a comment after all */
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100623 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 +0200624 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200625 break;
626 case ' ':
627 if (*word_len) {
628 /* word is finished */
629 goto str_end;
630 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200631 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200632 break;
633 case '\t':
634 if (*word_len) {
635 /* word is finished */
636 goto str_end;
637 }
638 /* tabs count for 8 spaces */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200639 ctx->indent += 8;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200640
641 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200642 break;
643 case '\n':
644 if (*word_len) {
645 /* word is finished */
646 goto str_end;
647 }
648 /* reset indent */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200649 ctx->indent = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200650
651 /* track line numbers */
652 ++ctx->line;
653
654 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200655 break;
656 case ';':
657 case '{':
658 if (*word_len || (arg == Y_MAYBE_STR_ARG)) {
659 /* word is finished */
660 goto str_end;
661 }
662
Radek Krejci44ceedc2018-10-02 15:54:31 +0200663 LOGVAL_YANG(ctx, LY_VCODE_INSTREXP, 1, *data, "an argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200664 return LY_EVALID;
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200665 case '}':
666 /* invalid - braces cannot be in unquoted string (opening braces terminates the string and can follow it) */
667 LOGVAL_YANG(ctx, LY_VCODE_INSTREXP, 1, *data,
668 "unquoted string character, optsep, semicolon or opening brace");
669 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200670 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200671 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 +0200672 break;
673 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200674 }
675
676str_end:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200677 /* terminating NULL byte for buf */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200678 if (*word_b) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200679 (*word_b) = ly_realloc(*word_b, (*word_len) + 1);
680 LY_CHECK_ERR_RET(!(*word_b), LOGMEM(ctx->ctx), LY_EMEM);
681 (*word_b)[*word_len] = '\0';
Michal Vasko7fbc8162018-09-17 10:35:16 +0200682 *word_p = *word_b;
683 }
684
685 return LY_SUCCESS;
686}
687
Michal Vaskoea5abea2018-09-18 13:10:54 +0200688/**
689 * @brief Get another YANG keyword from the raw data.
690 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200691 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200692 * @param[in,out] data Data to read from, always moved to currently handled character.
693 * @param[out] kw YANG keyword read.
694 * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances.
695 * @param[out] word_len Length of the keyword in the data. Useful for extension instances.
696 *
697 * @return LY_ERR values.
698 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200699static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200700get_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 +0200701{
Michal Vasko7fbc8162018-09-17 10:35:16 +0200702 int prefix;
703 const char *word_start;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200704 unsigned int c;
705 size_t len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200706
707 if (word_p) {
708 *word_p = NULL;
709 *word_len = 0;
710 }
711
712 /* first skip "optsep", comments */
713 while (**data) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200714 switch (**data) {
715 case '/':
716 if ((*data)[1] == '/') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200717 /* one-line comment */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200718 MOVE_INPUT(ctx, data, 2);
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100719 LY_CHECK_RET(skip_comment(ctx, data, 1));
Radek Krejcidcc7b322018-10-11 14:24:02 +0200720 } else if ((*data)[1] == '*') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200721 /* block comment */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200722 MOVE_INPUT(ctx, data, 2);
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100723 LY_CHECK_RET(skip_comment(ctx, data, 2));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200724 } else {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200725 /* error - not a comment after all, keyword cannot start with slash */
726 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '/'.");
727 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200728 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200729 break;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200730 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200731 /* skip whitespaces (optsep) */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200732 ++ctx->line;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200733 ctx->indent = 0;
734 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200735 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200736 /* skip whitespaces (optsep) */
737 ++ctx->indent;
738 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200739 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200740 /* skip whitespaces (optsep) */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200741 ctx->indent += 8;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200742 break;
743 default:
744 /* either a keyword start or an invalid character */
745 goto keyword_start;
746 }
747
748 ++(*data);
749 }
750
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200751#define IF_KW(STR, LEN, STMT) if (!strncmp(*(data), STR, LEN)) {MOVE_INPUT(ctx, data, LEN);*kw=STMT;}
752#define IF_KW_PREFIX(STR, LEN) if (!strncmp(*(data), STR, LEN)) {MOVE_INPUT(ctx, data, LEN);
753#define IF_KW_PREFIX_END }
754
Michal Vasko7fbc8162018-09-17 10:35:16 +0200755keyword_start:
756 word_start = *data;
757 *kw = YANG_NONE;
758
759 /* read the keyword itself */
760 switch (**data) {
761 case 'a':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200762 MOVE_INPUT(ctx, data, 1);
763 IF_KW("rgument", 7, YANG_ARGUMENT)
764 else IF_KW("ugment", 6, YANG_AUGMENT)
765 else IF_KW("ction", 5, YANG_ACTION)
766 else IF_KW_PREFIX("ny", 2)
767 IF_KW("data", 4, YANG_ANYDATA)
768 else IF_KW("xml", 3, YANG_ANYXML)
769 IF_KW_PREFIX_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200770 break;
771 case 'b':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200772 MOVE_INPUT(ctx, data, 1);
773 IF_KW("ase", 3, YANG_BASE)
774 else IF_KW("elongs-to", 9, YANG_BELONGS_TO)
775 else IF_KW("it", 2, YANG_BIT)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200776 break;
777 case 'c':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200778 MOVE_INPUT(ctx, data, 1);
779 IF_KW("ase", 3, YANG_CASE)
780 else IF_KW("hoice", 5, YANG_CHOICE)
781 else IF_KW_PREFIX("on", 2)
782 IF_KW("fig", 3, YANG_CONFIG)
783 else IF_KW_PREFIX("ta", 2)
784 IF_KW("ct", 2, YANG_CONTACT)
785 else IF_KW("iner", 4, YANG_CONTAINER)
786 IF_KW_PREFIX_END
787 IF_KW_PREFIX_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200788 break;
789 case 'd':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200790 MOVE_INPUT(ctx, data, 1);
791 IF_KW_PREFIX("e", 1)
792 IF_KW("fault", 5, YANG_DEFAULT)
793 else IF_KW("scription", 9, YANG_DESCRIPTION)
794 else IF_KW_PREFIX("viat", 4)
795 IF_KW("e", 1, YANG_DEVIATE)
796 else IF_KW("ion", 3, YANG_DEVIATION)
797 IF_KW_PREFIX_END
798 IF_KW_PREFIX_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200799 break;
800 case 'e':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200801 MOVE_INPUT(ctx, data, 1);
802 IF_KW("num", 3, YANG_ENUM)
803 else IF_KW_PREFIX("rror-", 5)
804 IF_KW("app-tag", 7, YANG_ERROR_APP_TAG)
805 else IF_KW("message", 7, YANG_ERROR_MESSAGE)
806 IF_KW_PREFIX_END
807 else IF_KW("xtension", 8, YANG_EXTENSION)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200808 break;
809 case 'f':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200810 MOVE_INPUT(ctx, data, 1);
811 IF_KW("eature", 6, YANG_FEATURE)
812 else IF_KW("raction-digits", 14, YANG_FRACTION_DIGITS)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200813 break;
814 case 'g':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200815 MOVE_INPUT(ctx, data, 1);
816 IF_KW("rouping", 7, YANG_GROUPING)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200817 break;
818 case 'i':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200819 MOVE_INPUT(ctx, data, 1);
820 IF_KW("dentity", 7, YANG_IDENTITY)
821 else IF_KW("f-feature", 9, YANG_IF_FEATURE)
822 else IF_KW("mport", 5, YANG_IMPORT)
823 else IF_KW_PREFIX("n", 1)
824 IF_KW("clude", 5, YANG_INCLUDE)
825 else IF_KW("put", 3, YANG_INPUT)
826 IF_KW_PREFIX_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200827 break;
828 case 'k':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200829 MOVE_INPUT(ctx, data, 1);
830 IF_KW("ey", 2, YANG_KEY)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200831 break;
832 case 'l':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200833 MOVE_INPUT(ctx, data, 1);
834 IF_KW_PREFIX("e", 1)
835 IF_KW("af-list", 7, YANG_LEAF_LIST)
836 else IF_KW("af", 2, YANG_LEAF)
837 else IF_KW("ngth", 4, YANG_LENGTH)
838 IF_KW_PREFIX_END
839 else IF_KW("ist", 3, YANG_LIST)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200840 break;
841 case 'm':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200842 MOVE_INPUT(ctx, data, 1);
843 IF_KW_PREFIX("a", 1)
844 IF_KW("ndatory", 7, YANG_MANDATORY)
845 else IF_KW("x-elements", 10, YANG_MAX_ELEMENTS)
846 IF_KW_PREFIX_END
847 else IF_KW("in-elements", 11, YANG_MIN_ELEMENTS)
848 else IF_KW("ust", 3, YANG_MUST)
849 else IF_KW_PREFIX("od", 2)
850 IF_KW("ule", 3, YANG_MODULE)
851 else IF_KW("ifier", 5, YANG_MODIFIER)
852 IF_KW_PREFIX_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200853 break;
854 case 'n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200855 MOVE_INPUT(ctx, data, 1);
856 IF_KW("amespace", 8, YANG_NAMESPACE)
857 else IF_KW("otification", 11, YANG_NOTIFICATION)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200858 break;
859 case 'o':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200860 MOVE_INPUT(ctx, data, 1);
861 IF_KW_PREFIX("r", 1)
862 IF_KW("dered-by", 8, YANG_ORDERED_BY)
863 else IF_KW("ganization", 10, YANG_ORGANIZATION)
864 IF_KW_PREFIX_END
865 else IF_KW("utput", 5, YANG_OUTPUT)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200866 break;
867 case 'p':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200868 MOVE_INPUT(ctx, data, 1);
869 IF_KW("ath", 3, YANG_PATH)
870 else IF_KW("attern", 6, YANG_PATTERN)
871 else IF_KW("osition", 7, YANG_POSITION)
872 else IF_KW_PREFIX("re", 2)
873 IF_KW("fix", 3, YANG_PREFIX)
874 else IF_KW("sence", 5, YANG_PRESENCE)
875 IF_KW_PREFIX_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200876 break;
877 case 'r':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200878 MOVE_INPUT(ctx, data, 1);
879 IF_KW("ange", 4, YANG_RANGE)
880 else IF_KW_PREFIX("e", 1)
881 IF_KW_PREFIX("f", 1)
882 IF_KW("erence", 6, YANG_REFERENCE)
883 else IF_KW("ine", 3, YANG_REFINE)
884 IF_KW_PREFIX_END
885 else IF_KW("quire-instance", 14, YANG_REQUIRE_INSTANCE)
886 else IF_KW("vision-date", 11, YANG_REVISION_DATE)
887 else IF_KW("vision", 6, YANG_REVISION)
888 IF_KW_PREFIX_END
889 else IF_KW("pc", 2, YANG_RPC)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200890 break;
891 case 's':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200892 MOVE_INPUT(ctx, data, 1);
893 IF_KW("tatus", 5, YANG_STATUS)
894 else IF_KW("ubmodule", 8, YANG_SUBMODULE)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200895 break;
896 case 't':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200897 MOVE_INPUT(ctx, data, 1);
898 IF_KW("ypedef", 6, YANG_TYPEDEF)
899 else IF_KW("ype", 3, YANG_TYPE)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200900 break;
901 case 'u':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200902 MOVE_INPUT(ctx, data, 1);
903 IF_KW_PREFIX("ni", 2)
904 IF_KW("que", 3, YANG_UNIQUE)
905 else IF_KW("ts", 2, YANG_UNITS)
906 IF_KW_PREFIX_END
907 else IF_KW("ses", 3, YANG_USES)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200908 break;
909 case 'v':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200910 MOVE_INPUT(ctx, data, 1);
911 IF_KW("alue", 4, YANG_VALUE)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200912 break;
913 case 'w':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200914 MOVE_INPUT(ctx, data, 1);
915 IF_KW("hen", 3, YANG_WHEN)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200916 break;
917 case 'y':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200918 MOVE_INPUT(ctx, data, 1);
919 IF_KW("ang-version", 11, YANG_YANG_VERSION)
920 else IF_KW("in-element", 10, YANG_YIN_ELEMENT)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200921 break;
922 case ';':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200923 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200924 *kw = YANG_SEMICOLON;
Radek Krejci626df482018-10-11 15:06:31 +0200925 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200926 case '{':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200927 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200928 *kw = YANG_LEFT_BRACE;
Radek Krejci626df482018-10-11 15:06:31 +0200929 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200930 case '}':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200931 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200932 *kw = YANG_RIGHT_BRACE;
Radek Krejci626df482018-10-11 15:06:31 +0200933 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200934 default:
935 break;
936 }
937
Radek Krejci0904c162019-01-02 15:03:59 +0100938#undef IF_KW
939#undef IF_KW_PREFIX
940#undef IF_KW_PREFIX_END
941
Michal Vasko7fbc8162018-09-17 10:35:16 +0200942 if (*kw != YANG_NONE) {
943 /* make sure we have the whole keyword */
944 switch (**data) {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200945 case '\n':
946 ++ctx->line;
947 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200948 case ' ':
949 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200950 /* mandatory "sep" */
951 break;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200952 case ':':
953 /* keyword is not actually a keyword, but prefix of an extension.
954 * To avoid repeated check of the prefix syntax, move to the point where the colon was read
955 * and we will be checking the keyword (extension instance) itself */
956 prefix = 1;
957 MOVE_INPUT(ctx, data, 1);
958 goto extension;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200959 case '{':
960 /* allowed only for input and output statements which can be without arguments */
961 if (*kw == YANG_INPUT || *kw == YANG_OUTPUT) {
962 break;
963 }
964 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200965 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200966 MOVE_INPUT(ctx, data, 1);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200967 LOGVAL_YANG(ctx, LY_VCODE_INSTREXP, (int)(*data - word_start), word_start,
968 "a keyword followed by a separator");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200969 return LY_EVALID;
970 }
971 } else {
972 /* still can be an extension */
973 prefix = 0;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200974extension:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200975 while (**data && (**data != ' ') && (**data != '\t') && (**data != '\n') && (**data != '{') && (**data != ';')) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200976 LY_CHECK_ERR_RET(ly_getutf8(data, &c, &len),
977 LOGVAL_YANG(ctx, LY_VCODE_INCHAR, (*data)[-len]), LY_EVALID);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200978 ++ctx->indent;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200979 /* check character validity */
980 LY_CHECK_RET(check_identifierchar(ctx, c, *data - len == word_start ? 1 : 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200981 }
982 if (!**data) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200983 LOGVAL_YANG(ctx, LY_VCODE_EOF);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200984 return LY_EVALID;
985 }
986
987 /* prefix is mandatory for extension instances */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200988 if (prefix != 2) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200989 LOGVAL_YANG(ctx, LY_VCODE_INSTREXP, (int)(*data - word_start), word_start, "a keyword");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200990 return LY_EVALID;
991 }
992
993 *kw = YANG_CUSTOM;
994 }
Radek Krejci626df482018-10-11 15:06:31 +0200995success:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200996 if (word_p) {
997 *word_p = (char *)word_start;
998 *word_len = *data - word_start;
999 }
1000
1001 return LY_SUCCESS;
1002}
1003
Michal Vaskoea5abea2018-09-18 13:10:54 +02001004/**
1005 * @brief Parse extension instance substatements.
1006 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001007 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001008 * @param[in,out] data Data to read from, always moved to currently handled character.
1009 * @param[in] word Extension instance substatement name (keyword).
1010 * @param[in] word_len Extension instance substatement name length.
1011 * @param[in,out] child Children of this extension instance to add to.
1012 *
1013 * @return LY_ERR values.
1014 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001015static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001016parse_ext_substmt(struct lys_parser_ctx *ctx, const char **data, char *word, size_t word_len,
Michal Vasko7fbc8162018-09-17 10:35:16 +02001017 struct lysp_stmt **child)
1018{
1019 char *buf;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001020 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001021 enum yang_keyword kw;
1022 struct lysp_stmt *stmt, *par_child;
1023
1024 stmt = calloc(1, sizeof *stmt);
1025 LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM);
1026
Radek Krejcibb9b1982019-04-08 14:24:59 +02001027 /* insert into parent statements */
1028 if (!*child) {
1029 *child = stmt;
1030 } else {
1031 for (par_child = *child; par_child->next; par_child = par_child->next);
1032 par_child->next = stmt;
1033 }
1034
Radek Krejci44ceedc2018-10-02 15:54:31 +02001035 stmt->stmt = lydict_insert(ctx->ctx, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001036
1037 /* get optional argument */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001038 LY_CHECK_RET(get_argument(ctx, data, Y_MAYBE_STR_ARG, &stmt->flags, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001039
Radek Krejci0ae092d2018-09-20 16:43:19 +02001040 if (word) {
1041 if (buf) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001042 stmt->arg = lydict_insert_zc(ctx->ctx, word);
Radek Krejci0ae092d2018-09-20 16:43:19 +02001043 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001044 stmt->arg = lydict_insert(ctx->ctx, word, word_len);
Radek Krejci0ae092d2018-09-20 16:43:19 +02001045 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001046 }
1047
Radek Krejci6d6556c2018-11-08 09:37:45 +01001048 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, ) {
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001049 LY_CHECK_RET(parse_ext_substmt(ctx, data, word, word_len, &stmt->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001050 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001051 return ret;
1052}
1053
Michal Vaskoea5abea2018-09-18 13:10:54 +02001054/**
1055 * @brief Parse extension instance.
1056 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001057 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001058 * @param[in,out] data Data to read from, always moved to currently handled character.
1059 * @param[in] ext_name Extension instance substatement name (keyword).
1060 * @param[in] ext_name_len Extension instance substatement name length.
1061 * @param[in] insubstmt Type of the keyword this extension instance is a substatement of.
1062 * @param[in] insubstmt_index Index of the keyword instance this extension instance is a substatement of.
1063 * @param[in,out] exts Extension instances to add to.
1064 *
1065 * @return LY_ERR values.
1066 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001067static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001068parse_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 +02001069 uint32_t insubstmt_index, struct lysp_ext_instance **exts)
1070{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001071 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001072 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001073 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001074 struct lysp_ext_instance *e;
1075 enum yang_keyword kw;
1076
Radek Krejci2c4e7172018-10-19 15:56:26 +02001077 LY_ARRAY_NEW_RET(ctx->ctx, *exts, e, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001078
1079 /* store name and insubstmt info */
Radek Krejci44ceedc2018-10-02 15:54:31 +02001080 e->name = lydict_insert(ctx->ctx, ext_name, ext_name_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001081 e->insubstmt = insubstmt;
1082 e->insubstmt_index = insubstmt_index;
1083
1084 /* get optional argument */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001085 LY_CHECK_RET(get_argument(ctx, data, Y_MAYBE_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001086
Radek Krejci0ae092d2018-09-20 16:43:19 +02001087 if (word) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001088 INSERT_WORD(ctx, buf, e->argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001089 }
1090
Radek Krejci6d6556c2018-11-08 09:37:45 +01001091 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001092 LY_CHECK_RET(parse_ext_substmt(ctx, data, word, word_len, &e->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001093 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001094 return ret;
1095}
1096
Michal Vaskoea5abea2018-09-18 13:10:54 +02001097/**
1098 * @brief Parse a generic text field without specific constraints. Those are contact, organization,
1099 * description, etc...
1100 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001101 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001102 * @param[in,out] data Data to read from, always moved to currently handled character.
1103 * @param[in] substmt Type of this substatement.
1104 * @param[in] substmt_index Index of this substatement.
1105 * @param[in,out] value Place to store the parsed value.
1106 * @param[in] arg Type of the YANG keyword argument (of the value).
1107 * @param[in,out] exts Extension instances to add to.
1108 *
1109 * @return LY_ERR values.
1110 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001111static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001112parse_text_field(struct lys_parser_ctx *ctx, const char **data, LYEXT_SUBSTMT substmt, uint32_t substmt_index,
Michal Vasko7fbc8162018-09-17 10:35:16 +02001113 const char **value, enum yang_arg arg, struct lysp_ext_instance **exts)
1114{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001115 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001116 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001117 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001118 enum yang_keyword kw;
1119
1120 if (*value) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001121 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001122 return LY_EVALID;
1123 }
1124
1125 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001126 LY_CHECK_RET(get_argument(ctx, data, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001127
1128 /* store value and spend buf if allocated */
Radek Krejci44ceedc2018-10-02 15:54:31 +02001129 INSERT_WORD(ctx, buf, *value, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001130
Radek Krejci6d6556c2018-11-08 09:37:45 +01001131 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001132 switch (kw) {
1133 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001134 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, substmt, substmt_index, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001135 break;
1136 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001137 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001138 return LY_EVALID;
1139 }
1140 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001141 return ret;
1142}
1143
Michal Vaskoea5abea2018-09-18 13:10:54 +02001144/**
1145 * @brief Parse the yang-version statement.
1146 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001147 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001148 * @param[in,out] data Data to read from, always moved to currently handled character.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001149 * @param[out] version Storage for the parsed information.
1150 * @param[in, out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001151 *
1152 * @return LY_ERR values.
1153 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001154static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001155parse_yangversion(struct lys_parser_ctx *ctx, const char **data, uint8_t *version, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001156{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001157 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001158 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001159 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001160 enum yang_keyword kw;
1161
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001162 if (*version) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001163 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001164 return LY_EVALID;
1165 }
1166
1167 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001168 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001169
1170 if ((word_len == 3) && !strncmp(word, "1.0", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001171 *version = LYS_VERSION_1_0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001172 } else if ((word_len == 3) && !strncmp(word, "1.1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001173 *version = LYS_VERSION_1_1;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001174 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001175 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001176 free(buf);
1177 return LY_EVALID;
1178 }
1179 free(buf);
1180
Radek Krejci6d6556c2018-11-08 09:37:45 +01001181 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001182 switch (kw) {
1183 case YANG_CUSTOM:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001184 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_VERSION, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001185 break;
1186 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001187 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001188 return LY_EVALID;
1189 }
1190 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001191 return ret;
1192}
1193
Michal Vaskoea5abea2018-09-18 13:10:54 +02001194/**
1195 * @brief Parse the belongs-to statement.
1196 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001197 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001198 * @param[in,out] data Data to read from, always moved to currently handled character.
1199 * @param[in,out] belongsto Place to store the parsed value.
1200 * @param[in,out] prefix Place to store the parsed belongs-to prefix value.
1201 * @param[in,out] exts Extension instances to add to.
1202 *
1203 * @return LY_ERR values.
1204 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001205static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001206parse_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 +02001207{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001208 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001209 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001210 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001211 enum yang_keyword kw;
1212
1213 if (*belongsto) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001214 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001215 return LY_EVALID;
1216 }
1217
1218 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001219 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001220
Radek Krejci44ceedc2018-10-02 15:54:31 +02001221 INSERT_WORD(ctx, buf, *belongsto, word, word_len);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001222 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001223 switch (kw) {
1224 case YANG_PREFIX:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001225 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_PREFIX, 0, prefix, Y_IDENTIF_ARG, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001226 break;
1227 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001228 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_BELONGSTO, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001229 break;
1230 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001231 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001232 return LY_EVALID;
1233 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001234 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001235 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001236checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001237 /* mandatory substatements */
1238 if (!*prefix) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001239 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001240 return LY_EVALID;
1241 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001242 return ret;
1243}
1244
Michal Vaskoea5abea2018-09-18 13:10:54 +02001245/**
1246 * @brief Parse the revision-date statement.
1247 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001248 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001249 * @param[in,out] data Data to read from, always moved to currently handled character.
1250 * @param[in,out] rev Array to store the parsed value in.
1251 * @param[in,out] exts Extension instances to add to.
1252 *
1253 * @return LY_ERR values.
1254 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001255static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001256parse_revisiondate(struct lys_parser_ctx *ctx, const char **data, char *rev, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001257{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001258 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001259 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001260 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001261 enum yang_keyword kw;
1262
1263 if (rev[0]) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001264 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001265 return LY_EVALID;
1266 }
1267
1268 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001269 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001270
1271 /* check value */
Radek Krejcibbe09a92018-11-08 09:36:54 +01001272 if (lysp_check_date(ctx, word, word_len, "revision-date")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001273 free(buf);
1274 return LY_EVALID;
1275 }
1276
1277 /* store value and spend buf if allocated */
1278 strncpy(rev, word, word_len);
1279 free(buf);
1280
Radek Krejci6d6556c2018-11-08 09:37:45 +01001281 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001282 switch (kw) {
1283 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001284 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_REVISIONDATE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001285 break;
1286 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001287 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001288 return LY_EVALID;
1289 }
1290 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001291 return ret;
1292}
1293
Michal Vaskoea5abea2018-09-18 13:10:54 +02001294/**
1295 * @brief Parse the include statement.
1296 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001297 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001298 * @param[in] module_name Name of the module to check name collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001299 * @param[in,out] data Data to read from, always moved to currently handled character.
1300 * @param[in,out] includes Parsed includes 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_include(struct lys_parser_ctx *ctx, const char *module_name, const char **data, struct lysp_include **includes)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001306{
Radek Krejcid33273d2018-10-25 14:55:52 +02001307 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001308 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001309 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001310 enum yang_keyword kw;
1311 struct lysp_include *inc;
1312
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001313 LY_ARRAY_NEW_RET(ctx->ctx, *includes, inc, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001314
1315 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001316 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001317
Radek Krejci086c7132018-10-26 15:29:04 +02001318 INSERT_WORD(ctx, buf, inc->name, word, word_len);
1319
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001320 /* submodules share the namespace with the module names, so there must not be
1321 * a module of the same name in the context, no need for revision matching */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001322 if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(ctx->ctx, inc->name)) {
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001323 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Name collision between module and submodule of name \"%s\".", inc->name);
1324 return LY_EVALID;
1325 }
1326
Radek Krejci6d6556c2018-11-08 09:37:45 +01001327 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001328 switch (kw) {
1329 case YANG_DESCRIPTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01001330 YANG_CHECK_STMTVER2_RET(ctx, "description", "include");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001331 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 +02001332 break;
1333 case YANG_REFERENCE:
Radek Krejciceaf2122019-01-02 15:03:26 +01001334 YANG_CHECK_STMTVER2_RET(ctx, "reference", "include");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001335 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 +02001336 break;
1337 case YANG_REVISION_DATE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001338 LY_CHECK_RET(parse_revisiondate(ctx, data, inc->rev, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001339 break;
1340 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001341 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001342 break;
1343 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001344 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001345 return LY_EVALID;
1346 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001347 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001348 return ret;
1349}
1350
Michal Vaskoea5abea2018-09-18 13:10:54 +02001351/**
1352 * @brief Parse the import statement.
1353 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001354 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001355 * @param[in] module_prefix Prefix of the module to check prefix collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001356 * @param[in,out] data Data to read from, always moved to currently handled character.
1357 * @param[in,out] imports Parsed imports to add to.
1358 *
1359 * @return LY_ERR values.
1360 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001361static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001362parse_import(struct lys_parser_ctx *ctx, const char *module_prefix, const char **data, struct lysp_import **imports)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001363{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001364 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001365 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001366 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001367 enum yang_keyword kw;
1368 struct lysp_import *imp;
1369
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001370 LY_ARRAY_NEW_RET(ctx->ctx, *imports, imp, LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001371
1372 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001373 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02001374 INSERT_WORD(ctx, buf, imp->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001375
Radek Krejci6d6556c2018-11-08 09:37:45 +01001376 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001377 switch (kw) {
1378 case YANG_PREFIX:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001379 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 +01001380 LY_CHECK_RET(lysp_check_prefix(ctx, *imports, module_prefix, &imp->prefix), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001381 break;
1382 case YANG_DESCRIPTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01001383 YANG_CHECK_STMTVER2_RET(ctx, "description", "import");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001384 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 +02001385 break;
1386 case YANG_REFERENCE:
Radek Krejciceaf2122019-01-02 15:03:26 +01001387 YANG_CHECK_STMTVER2_RET(ctx, "reference", "import");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001388 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 +02001389 break;
1390 case YANG_REVISION_DATE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001391 LY_CHECK_RET(parse_revisiondate(ctx, data, imp->rev, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001392 break;
1393 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001394 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001395 break;
1396 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001397 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001398 return LY_EVALID;
1399 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001400 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001401 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001402checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001403 /* mandatory substatements */
Radek Krejci086c7132018-10-26 15:29:04 +02001404 LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001405
1406 return ret;
1407}
1408
Michal Vaskoea5abea2018-09-18 13:10:54 +02001409/**
1410 * @brief Parse the revision statement.
1411 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001412 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001413 * @param[in,out] data Data to read from, always moved to currently handled character.
1414 * @param[in,out] revs Parsed revisions to add to.
1415 *
1416 * @return LY_ERR values.
1417 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001418static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001419parse_revision(struct lys_parser_ctx *ctx, const char **data, struct lysp_revision **revs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001420{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001421 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001422 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001423 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001424 enum yang_keyword kw;
1425 struct lysp_revision *rev;
1426
Radek Krejci2c4e7172018-10-19 15:56:26 +02001427 LY_ARRAY_NEW_RET(ctx->ctx, *revs, rev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001428
1429 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001430 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001431
1432 /* check value */
Radek Krejcibbe09a92018-11-08 09:36:54 +01001433 if (lysp_check_date(ctx, word, word_len, "revision")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001434 return LY_EVALID;
1435 }
1436
Radek Krejcib7db73a2018-10-24 14:18:40 +02001437 strncpy(rev->date, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001438 free(buf);
1439
Radek Krejci6d6556c2018-11-08 09:37:45 +01001440 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001441 switch (kw) {
1442 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001443 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 +02001444 break;
1445 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001446 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 +02001447 break;
1448 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001449 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001450 break;
1451 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001452 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001453 return LY_EVALID;
1454 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001455 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001456 return ret;
1457}
1458
Michal Vaskoea5abea2018-09-18 13:10:54 +02001459/**
1460 * @brief Parse a generic text field that can have more instances such as base.
1461 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001462 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001463 * @param[in,out] data Data to read from, always moved to currently handled character.
1464 * @param[in] substmt Type of this substatement.
1465 * @param[in,out] texts Parsed values to add to.
1466 * @param[in] arg Type of the expected argument.
1467 * @param[in,out] exts Extension instances to add to.
1468 *
1469 * @return LY_ERR values.
1470 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001471static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001472parse_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 +02001473 struct lysp_ext_instance **exts)
1474{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001475 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001476 char *buf, *word;
Radek Krejci151a5b72018-10-19 14:21:44 +02001477 const char **item;
1478 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001479 enum yang_keyword kw;
1480
1481 /* allocate new pointer */
Radek Krejci2c4e7172018-10-19 15:56:26 +02001482 LY_ARRAY_NEW_RET(ctx->ctx, *texts, item, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001483
1484 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001485 LY_CHECK_RET(get_argument(ctx, data, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001486
Radek Krejci151a5b72018-10-19 14:21:44 +02001487 INSERT_WORD(ctx, buf, *item, word, word_len);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001488 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001489 switch (kw) {
1490 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001491 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, substmt, LY_ARRAY_SIZE(*texts) - 1, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001492 break;
1493 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001494 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001495 return LY_EVALID;
1496 }
1497 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001498 return ret;
1499}
1500
Michal Vaskoea5abea2018-09-18 13:10:54 +02001501/**
1502 * @brief Parse the config 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_config(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_CONFIG_MASK) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001520 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "config");
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 == 4) && !strncmp(word, "true", word_len)) {
1528 *flags |= LYS_CONFIG_W;
1529 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1530 *flags |= LYS_CONFIG_R;
1531 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001532 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001533 free(buf);
1534 return LY_EVALID;
1535 }
1536 free(buf);
1537
Radek Krejci6d6556c2018-11-08 09:37:45 +01001538 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001539 switch (kw) {
1540 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001541 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_CONFIG, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001542 break;
1543 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001544 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001545 return LY_EVALID;
1546 }
1547 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001548 return ret;
1549}
1550
Michal Vaskoea5abea2018-09-18 13:10:54 +02001551/**
1552 * @brief Parse the mandatory statement.
1553 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001554 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001555 * @param[in,out] data Data to read from, always moved to currently handled character.
1556 * @param[in,out] flags Flags to add to.
1557 * @param[in,out] exts Extension instances to add to.
1558 *
1559 * @return LY_ERR values.
1560 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001561static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001562parse_mandatory(struct lys_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001563{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001564 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001565 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001566 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001567 enum yang_keyword kw;
1568
1569 if (*flags & LYS_MAND_MASK) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001570 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001571 return LY_EVALID;
1572 }
1573
1574 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001575 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001576
1577 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1578 *flags |= LYS_MAND_TRUE;
1579 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1580 *flags |= LYS_MAND_FALSE;
1581 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001582 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001583 free(buf);
1584 return LY_EVALID;
1585 }
1586 free(buf);
1587
Radek Krejci6d6556c2018-11-08 09:37:45 +01001588 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001589 switch (kw) {
1590 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001591 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MANDATORY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001592 break;
1593 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001594 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001595 return LY_EVALID;
1596 }
1597 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001598 return ret;
1599}
1600
Michal Vaskoea5abea2018-09-18 13:10:54 +02001601/**
1602 * @brief Parse a restriction such as range or length.
1603 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001604 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001605 * @param[in,out] data Data to read from, always moved to currently handled character.
1606 * @param[in] restr_kw Type of this particular restriction.
1607 * @param[in,out] exts Extension instances to add to.
1608 *
1609 * @return LY_ERR values.
1610 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001611static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001612parse_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 +02001613{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001614 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001615 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001616 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001617 enum yang_keyword kw;
1618
1619 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001620 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001621
Radek Krejci44ceedc2018-10-02 15:54:31 +02001622 INSERT_WORD(ctx, buf, restr->arg, word, word_len);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001623 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001624 switch (kw) {
1625 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001626 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 +02001627 break;
1628 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001629 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 +02001630 break;
1631 case YANG_ERROR_APP_TAG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001632 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 +02001633 break;
1634 case YANG_ERROR_MESSAGE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001635 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 +02001636 break;
1637 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001638 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001639 break;
1640 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001641 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001642 return LY_EVALID;
1643 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001644 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001645 return ret;
1646}
1647
Michal Vaskoea5abea2018-09-18 13:10:54 +02001648/**
1649 * @brief Parse a restriction that can have more instances such as must.
1650 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001651 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001652 * @param[in,out] data Data to read from, always moved to currently handled character.
1653 * @param[in] restr_kw Type of this particular restriction.
1654 * @param[in,out] restrs Restrictions to add to.
1655 *
1656 * @return LY_ERR values.
1657 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001658static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001659parse_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 +02001660{
1661 struct lysp_restr *restr;
1662
Radek Krejci2c4e7172018-10-19 15:56:26 +02001663 LY_ARRAY_NEW_RET(ctx->ctx, *restrs, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001664 return parse_restr(ctx, data, restr_kw, restr);
1665}
1666
Michal Vaskoea5abea2018-09-18 13:10:54 +02001667/**
1668 * @brief Parse the status statement.
1669 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001670 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001671 * @param[in,out] data Data to read from, always moved to currently handled character.
1672 * @param[in,out] flags Flags to add to.
1673 * @param[in,out] exts Extension instances to add to.
1674 *
1675 * @return LY_ERR values.
1676 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001677static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001678parse_status(struct lys_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001679{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001680 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001681 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001682 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001683 enum yang_keyword kw;
1684
1685 if (*flags & LYS_STATUS_MASK) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001686 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001687 return LY_EVALID;
1688 }
1689
1690 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001691 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001692
1693 if ((word_len == 7) && !strncmp(word, "current", word_len)) {
1694 *flags |= LYS_STATUS_CURR;
1695 } else if ((word_len == 10) && !strncmp(word, "deprecated", word_len)) {
1696 *flags |= LYS_STATUS_DEPRC;
1697 } else if ((word_len == 8) && !strncmp(word, "obsolete", word_len)) {
1698 *flags |= LYS_STATUS_OBSLT;
1699 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001700 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001701 free(buf);
1702 return LY_EVALID;
1703 }
1704 free(buf);
1705
Radek Krejci6d6556c2018-11-08 09:37:45 +01001706 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001707 switch (kw) {
1708 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001709 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_STATUS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001710 break;
1711 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001712 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001713 return LY_EVALID;
1714 }
1715 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001716 return ret;
1717}
1718
Michal Vaskoea5abea2018-09-18 13:10:54 +02001719/**
1720 * @brief Parse the when statement.
1721 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001722 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001723 * @param[in,out] data Data to read from, always moved to currently handled character.
1724 * @param[in,out] when_p When pointer to parse to.
1725 *
1726 * @return LY_ERR values.
1727 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001728static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001729parse_when(struct lys_parser_ctx *ctx, const char **data, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001730{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001731 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001732 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001733 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001734 enum yang_keyword kw;
1735 struct lysp_when *when;
1736
1737 if (*when_p) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001738 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001739 return LY_EVALID;
1740 }
1741
1742 when = calloc(1, sizeof *when);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001743 LY_CHECK_ERR_RET(!when, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001744 *when_p = when;
1745
1746 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001747 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02001748 INSERT_WORD(ctx, buf, when->cond, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001749
Radek Krejci6d6556c2018-11-08 09:37:45 +01001750 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001751 switch (kw) {
1752 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001753 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 +02001754 break;
1755 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001756 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 +02001757 break;
1758 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001759 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001760 break;
1761 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001762 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001763 return LY_EVALID;
1764 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001765 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001766 return ret;
1767}
1768
Michal Vaskoea5abea2018-09-18 13:10:54 +02001769/**
1770 * @brief Parse the anydata or anyxml statement.
1771 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001772 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001773 * @param[in,out] data Data to read from, always moved to currently handled character.
1774 * @param[in] kw Type of this particular keyword.
1775 * @param[in,out] siblings Siblings to add to.
1776 *
1777 * @return LY_ERR values.
1778 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001779static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001780parse_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 +02001781{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001782 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001783 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001784 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001785 struct lysp_node *iter;
1786 struct lysp_node_anydata *any;
1787
1788 /* create structure */
1789 any = calloc(1, sizeof *any);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001790 LY_CHECK_ERR_RET(!any, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001791 any->nodetype = kw == YANG_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001792 any->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001793
1794 /* insert into siblings */
1795 if (!*siblings) {
1796 *siblings = (struct lysp_node *)any;
1797 } else {
1798 for (iter = *siblings; iter->next; iter = iter->next);
1799 iter->next = (struct lysp_node *)any;
1800 }
1801
1802 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001803 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02001804 INSERT_WORD(ctx, buf, any->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001805
1806 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01001807 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001808 switch (kw) {
1809 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001810 LY_CHECK_RET(parse_config(ctx, data, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001811 break;
1812 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001813 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 +02001814 break;
1815 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001816 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 +02001817 break;
1818 case YANG_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001819 LY_CHECK_RET(parse_mandatory(ctx, data, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001820 break;
1821 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001822 LY_CHECK_RET(parse_restrs(ctx, data, kw, &any->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001823 break;
1824 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001825 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 +02001826 break;
1827 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001828 LY_CHECK_RET(parse_status(ctx, data, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001829 break;
1830 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001831 LY_CHECK_RET(parse_when(ctx, data, &any->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001832 break;
1833 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001834 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001835 break;
1836 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001837 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
Radek Krejcic59bc972018-09-17 16:13:06 +02001838 (any->nodetype & LYS_ANYDATA) == LYS_ANYDATA ? ly_stmt2str(YANG_ANYDATA) : ly_stmt2str(YANG_ANYXML));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001839 return LY_EVALID;
1840 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001841 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001842 return ret;
1843}
1844
Michal Vaskoea5abea2018-09-18 13:10:54 +02001845/**
1846 * @brief Parse the value or position statement. Substatement of type enum statement.
1847 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001848 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001849 * @param[in,out] data Data to read from, always moved to currently handled character.
1850 * @param[in] val_kw Type of this particular keyword.
1851 * @param[in,out] value Value to write to.
1852 * @param[in,out] flags Flags to write to.
1853 * @param[in,out] exts Extension instances to add to.
1854 *
1855 * @return LY_ERR values.
1856 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001857static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001858parse_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 +02001859 struct lysp_ext_instance **exts)
1860{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001861 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001862 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001863 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001864 long int num;
1865 unsigned long int unum;
1866 enum yang_keyword kw;
1867
1868 if (*flags & LYS_SET_VALUE) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001869 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001870 return LY_EVALID;
1871 }
1872 *flags |= LYS_SET_VALUE;
1873
1874 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001875 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001876
1877 if (!word_len || (word[0] == '+') || ((word[0] == '0') && (word_len > 1)) || ((val_kw == YANG_VALUE) && !strncmp(word, "-0", 2))) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001878 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001879 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001880 }
1881
1882 errno = 0;
1883 if (val_kw == YANG_VALUE) {
1884 num = strtol(word, &ptr, 10);
Radek Krejci8b764662018-11-14 14:15:13 +01001885 if (num < INT64_C(-2147483648) || num > INT64_C(2147483647)) {
1886 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
1887 goto error;
1888 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001889 } else {
1890 unum = strtoul(word, &ptr, 10);
Radek Krejci8b764662018-11-14 14:15:13 +01001891 if (unum > UINT64_C(4294967295)) {
1892 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
1893 goto error;
1894 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001895 }
1896 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001897 if ((size_t)(ptr - word) != word_len) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001898 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001899 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001900 }
1901 if (errno == ERANGE) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001902 LOGVAL_YANG(ctx, LY_VCODE_OOB, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001903 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001904 }
1905 if (val_kw == YANG_VALUE) {
1906 *value = num;
1907 } else {
1908 *value = unum;
1909 }
1910 free(buf);
1911
Radek Krejci6d6556c2018-11-08 09:37:45 +01001912 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001913 switch (kw) {
1914 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001915 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 +02001916 break;
1917 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001918 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001919 return LY_EVALID;
1920 }
1921 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001922 return ret;
Radek Krejci8b764662018-11-14 14:15:13 +01001923
1924error:
1925 free(buf);
1926 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001927}
1928
Michal Vaskoea5abea2018-09-18 13:10:54 +02001929/**
1930 * @brief Parse the enum or bit statement. Substatement of type statement.
1931 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001932 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001933 * @param[in,out] data Data to read from, always moved to currently handled character.
1934 * @param[in] enum_kw Type of this particular keyword.
1935 * @param[in,out] enums Enums or bits to add to.
1936 *
1937 * @return LY_ERR values.
1938 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001939static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001940parse_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 +02001941{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001942 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001943 char *buf, *word;
Radek Krejci8b764662018-11-14 14:15:13 +01001944 size_t word_len, u;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001945 enum yang_keyword kw;
1946 struct lysp_type_enum *enm;
1947
Radek Krejci2c4e7172018-10-19 15:56:26 +02001948 LY_ARRAY_NEW_RET(ctx->ctx, *enums, enm, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001949
1950 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001951 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 +01001952 if (enum_kw == YANG_ENUM) {
1953 if (!word_len) {
1954 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Enum name must not be zero-length.");
1955 free(buf);
1956 return LY_EVALID;
1957 } else if (isspace(word[0]) || isspace(word[word_len - 1])) {
1958 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Enum name must not have any leading or trailing whitespaces (\"%.*s\").",
1959 word_len, word);
1960 free(buf);
1961 return LY_EVALID;
1962 } else {
1963 for (u = 0; u < word_len; ++u) {
1964 if (iscntrl(word[u])) {
1965 LOGWRN(ctx->ctx, "Control characters in enum name should be avoided (\"%.*s\", character number %d).",
1966 word_len, word, u + 1);
1967 break;
1968 }
1969 }
1970 }
1971 } else { /* YANG_BIT */
1972
1973 }
Radek Krejci44ceedc2018-10-02 15:54:31 +02001974 INSERT_WORD(ctx, buf, enm->name, word, word_len);
Radek Krejci8b764662018-11-14 14:15:13 +01001975 CHECK_UNIQUENESS(ctx, *enums, name, ly_stmt2str(enum_kw), enm->name);
1976
Radek Krejci6d6556c2018-11-08 09:37:45 +01001977 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001978 switch (kw) {
1979 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001980 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 +02001981 break;
1982 case YANG_IF_FEATURE:
Radek Krejciceaf2122019-01-02 15:03:26 +01001983 YANG_CHECK_STMTVER2_RET(ctx, "if-feature", ly_stmt2str(enum_kw));
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001984 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 +02001985 break;
1986 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001987 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 +02001988 break;
1989 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001990 LY_CHECK_RET(parse_status(ctx, data, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001991 break;
1992 case YANG_VALUE:
1993 case YANG_POSITION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001994 LY_CHECK_RET(parse_type_enum_value_pos(ctx, data, kw, &enm->value, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001995 break;
1996 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001997 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001998 break;
1999 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002000 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002001 return LY_EVALID;
2002 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002003 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002004 return ret;
2005}
2006
Michal Vaskoea5abea2018-09-18 13:10:54 +02002007/**
2008 * @brief Parse the fraction-digits statement. Substatement of type statement.
2009 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002010 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002011 * @param[in,out] data Data to read from, always moved to currently handled character.
2012 * @param[in,out] fracdig Value to write to.
2013 * @param[in,out] exts Extension instances to add to.
2014 *
2015 * @return LY_ERR values.
2016 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002017static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002018parse_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 +02002019{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002020 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002021 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002022 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002023 unsigned long int num;
2024 enum yang_keyword kw;
2025
2026 if (*fracdig) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002027 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002028 return LY_EVALID;
2029 }
2030
2031 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002032 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002033
2034 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002035 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002036 free(buf);
2037 return LY_EVALID;
2038 }
2039
2040 errno = 0;
2041 num = strtoul(word, &ptr, 10);
2042 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002043 if ((size_t)(ptr - word) != word_len) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002044 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002045 free(buf);
2046 return LY_EVALID;
2047 }
2048 if ((errno == ERANGE) || (num > 18)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002049 LOGVAL_YANG(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002050 free(buf);
2051 return LY_EVALID;
2052 }
2053 *fracdig = num;
2054 free(buf);
2055
Radek Krejci6d6556c2018-11-08 09:37:45 +01002056 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002057 switch (kw) {
2058 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002059 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_FRACDIGITS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002060 break;
2061 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002062 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002063 return LY_EVALID;
2064 }
2065 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002066 return ret;
2067}
2068
Michal Vaskoea5abea2018-09-18 13:10:54 +02002069/**
2070 * @brief Parse the require-instance statement. Substatement of type statement.
2071 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002072 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002073 * @param[in,out] data Data to read from, always moved to currently handled character.
2074 * @param[in,out] reqinst Value to write to.
2075 * @param[in,out] flags Flags to write to.
2076 * @param[in,out] exts Extension instances to add to.
2077 *
2078 * @return LY_ERR values.
2079 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002080static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002081parse_type_reqinstance(struct lys_parser_ctx *ctx, const char **data, uint8_t *reqinst, uint16_t *flags,
Michal Vasko7fbc8162018-09-17 10:35:16 +02002082 struct lysp_ext_instance **exts)
2083{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002084 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002085 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002086 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002087 enum yang_keyword kw;
2088
2089 if (*flags & LYS_SET_REQINST) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002090 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002091 return LY_EVALID;
2092 }
2093 *flags |= LYS_SET_REQINST;
2094
2095 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002096 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002097
2098 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
2099 *reqinst = 1;
2100 } else if ((word_len != 5) || strncmp(word, "false", word_len)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002101 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002102 free(buf);
2103 return LY_EVALID;
2104 }
2105 free(buf);
2106
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_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002110 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_REQINSTANCE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002111 break;
2112 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002113 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002114 return LY_EVALID;
2115 }
2116 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002117 return ret;
2118}
2119
Michal Vaskoea5abea2018-09-18 13:10:54 +02002120/**
2121 * @brief Parse the modifier statement. Substatement of type pattern statement.
2122 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002123 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002124 * @param[in,out] data Data to read from, always moved to currently handled character.
2125 * @param[in,out] pat Value to write to.
2126 * @param[in,out] exts Extension instances to add to.
2127 *
2128 * @return LY_ERR values.
2129 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002130static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002131parse_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 +02002132{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002133 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002134 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002135 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002136 enum yang_keyword kw;
2137
2138 if ((*pat)[0] == 0x15) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002139 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002140 return LY_EVALID;
2141 }
2142
2143 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002144 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002145
2146 if ((word_len != 12) || strncmp(word, "invert-match", word_len)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002147 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002148 free(buf);
2149 return LY_EVALID;
2150 }
2151 free(buf);
2152
2153 /* replace the value in the dictionary */
2154 buf = malloc(strlen(*pat) + 1);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002155 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002156 strcpy(buf, *pat);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002157 lydict_remove(ctx->ctx, *pat);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002158
2159 assert(buf[0] == 0x06);
2160 buf[0] = 0x15;
Radek Krejci44ceedc2018-10-02 15:54:31 +02002161 *pat = lydict_insert_zc(ctx->ctx, buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002162
Radek Krejci6d6556c2018-11-08 09:37:45 +01002163 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002164 switch (kw) {
2165 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002166 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MODIFIER, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002167 break;
2168 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002169 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002170 return LY_EVALID;
2171 }
2172 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002173 return ret;
2174}
2175
Michal Vaskoea5abea2018-09-18 13:10:54 +02002176/**
2177 * @brief Parse the pattern statement. Substatement of type statement.
2178 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002179 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002180 * @param[in,out] data Data to read from, always moved to currently handled character.
2181 * @param[in,out] patterns Restrictions to add to.
2182 *
2183 * @return LY_ERR values.
2184 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002185static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002186parse_type_pattern(struct lys_parser_ctx *ctx, const char **data, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002187{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002188 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002189 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002190 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002191 enum yang_keyword kw;
2192 struct lysp_restr *restr;
2193
Radek Krejci2c4e7172018-10-19 15:56:26 +02002194 LY_ARRAY_NEW_RET(ctx->ctx, *patterns, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002195
2196 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002197 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002198
2199 /* add special meaning first byte */
2200 if (buf) {
2201 buf = realloc(buf, word_len + 2);
2202 word = buf;
2203 } else {
2204 buf = malloc(word_len + 2);
2205 }
Radek Krejci44ceedc2018-10-02 15:54:31 +02002206 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejci86d106e2018-10-18 09:53:19 +02002207 memmove(buf + 1, word, word_len);
2208 buf[0] = 0x06; /* pattern's default regular-match flag */
2209 buf[word_len + 1] = '\0'; /* terminating NULL byte */
2210 restr->arg = lydict_insert_zc(ctx->ctx, buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002211
Radek Krejci6d6556c2018-11-08 09:37:45 +01002212 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002213 switch (kw) {
2214 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002215 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 +02002216 break;
2217 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002218 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 +02002219 break;
2220 case YANG_ERROR_APP_TAG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002221 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 +02002222 break;
2223 case YANG_ERROR_MESSAGE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002224 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 +02002225 break;
2226 case YANG_MODIFIER:
Radek Krejciceaf2122019-01-02 15:03:26 +01002227 YANG_CHECK_STMTVER2_RET(ctx, "modifier", "pattern");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002228 LY_CHECK_RET(parse_type_pattern_modifier(ctx, data, &restr->arg, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002229 break;
2230 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002231 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002232 break;
2233 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002234 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002235 return LY_EVALID;
2236 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002237 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002238 return ret;
2239}
2240
Michal Vaskoea5abea2018-09-18 13:10:54 +02002241/**
2242 * @brief Parse the type statement.
2243 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002244 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002245 * @param[in,out] data Data to read from, always moved to currently handled character.
2246 * @param[in,out] type Type to wrote to.
2247 *
2248 * @return LY_ERR values.
2249 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002250static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002251parse_type(struct lys_parser_ctx *ctx, const char **data, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002252{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002253 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002254 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002255 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002256 enum yang_keyword kw;
2257 struct lysp_type *nest_type;
2258
2259 if (type->name) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002260 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002261 return LY_EVALID;
2262 }
2263
2264 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002265 LY_CHECK_RET(get_argument(ctx, data, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002266 INSERT_WORD(ctx, buf, type->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002267
Radek Krejci6d6556c2018-11-08 09:37:45 +01002268 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002269 switch (kw) {
2270 case YANG_BASE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002271 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 +01002272 type->flags |= LYS_SET_BASE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002273 break;
2274 case YANG_BIT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002275 LY_CHECK_RET(parse_type_enum(ctx, data, kw, &type->bits));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002276 type->flags |= LYS_SET_BIT;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002277 break;
2278 case YANG_ENUM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002279 LY_CHECK_RET(parse_type_enum(ctx, data, kw, &type->enums));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002280 type->flags |= LYS_SET_ENUM;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002281 break;
2282 case YANG_FRACTION_DIGITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002283 LY_CHECK_RET(parse_type_fracdigits(ctx, data, &type->fraction_digits, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002284 type->flags |= LYS_SET_FRDIGITS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002285 break;
2286 case YANG_LENGTH:
2287 if (type->length) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002288 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002289 return LY_EVALID;
2290 }
2291 type->length = calloc(1, sizeof *type->length);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002292 LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002293
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002294 LY_CHECK_RET(parse_restr(ctx, data, kw, type->length));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002295 type->flags |= LYS_SET_LENGTH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002296 break;
2297 case YANG_PATH:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002298 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 +01002299 type->flags |= LYS_SET_PATH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002300 break;
2301 case YANG_PATTERN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002302 LY_CHECK_RET(parse_type_pattern(ctx, data, &type->patterns));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002303 type->flags |= LYS_SET_PATTERN;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002304 break;
2305 case YANG_RANGE:
2306 if (type->range) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002307 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002308 return LY_EVALID;
2309 }
2310 type->range = calloc(1, sizeof *type->range);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002311 LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx->ctx), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002312
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002313 LY_CHECK_RET(parse_restr(ctx, data, kw, type->range));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002314 type->flags |= LYS_SET_RANGE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002315 break;
2316 case YANG_REQUIRE_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002317 LY_CHECK_RET(parse_type_reqinstance(ctx, data, &type->require_instance, &type->flags, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002318 /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002319 break;
2320 case YANG_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002321 LY_ARRAY_NEW_RET(ctx->ctx, type->types, nest_type, LY_EMEM);
2322 LY_CHECK_RET(parse_type(ctx, data, nest_type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002323 type->flags |= LYS_SET_TYPE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002324 break;
2325 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002326 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &type->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002327 break;
2328 default:
Radek Krejci8b764662018-11-14 14:15:13 +01002329 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002330 return LY_EVALID;
2331 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002332 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002333 return ret;
2334}
2335
Michal Vaskoea5abea2018-09-18 13:10:54 +02002336/**
2337 * @brief Parse the leaf statement.
2338 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002339 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002340 * @param[in,out] data Data to read from, always moved to currently handled character.
2341 * @param[in,out] siblings Siblings to add to.
2342 *
2343 * @return LY_ERR values.
2344 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002345static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002346parse_leaf(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002347{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002348 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002349 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002350 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002351 enum yang_keyword kw;
2352 struct lysp_node *iter;
2353 struct lysp_node_leaf *leaf;
2354
2355 /* create structure */
2356 leaf = calloc(1, sizeof *leaf);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002357 LY_CHECK_ERR_RET(!leaf, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002358 leaf->nodetype = LYS_LEAF;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002359 leaf->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002360
2361 /* insert into siblings */
2362 if (!*siblings) {
2363 *siblings = (struct lysp_node *)leaf;
2364 } else {
2365 for (iter = *siblings; iter->next; iter = iter->next);
2366 iter->next = (struct lysp_node *)leaf;
2367 }
2368
2369 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002370 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002371 INSERT_WORD(ctx, buf, leaf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002372
2373 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01002374 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002375 switch (kw) {
2376 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002377 LY_CHECK_RET(parse_config(ctx, data, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002378 break;
2379 case YANG_DEFAULT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002380 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 +02002381 break;
2382 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002383 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 +02002384 break;
2385 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002386 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 +02002387 break;
2388 case YANG_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002389 LY_CHECK_RET(parse_mandatory(ctx, data, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002390 break;
2391 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002392 LY_CHECK_RET(parse_restrs(ctx, data, kw, &leaf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002393 break;
2394 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002395 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 +02002396 break;
2397 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002398 LY_CHECK_RET(parse_status(ctx, data, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002399 break;
2400 case YANG_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002401 LY_CHECK_RET(parse_type(ctx, data, &leaf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002402 break;
2403 case YANG_UNITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002404 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 +02002405 break;
2406 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002407 LY_CHECK_RET(parse_when(ctx, data, &leaf->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002408 break;
2409 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002410 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002411 break;
2412 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002413 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002414 return LY_EVALID;
2415 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002416 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002417 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002418checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002419 /* mandatory substatements */
2420 if (!leaf->type.name) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002421 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002422 return LY_EVALID;
2423 }
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002424 if ((leaf->flags & LYS_MAND_TRUE) && (leaf->dflt)) {
2425 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMSCOMB, "mandatory", "default", "leaf");
2426 return LY_EVALID;
2427 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002428
2429 return ret;
2430}
2431
Michal Vaskoea5abea2018-09-18 13:10:54 +02002432/**
2433 * @brief Parse the max-elements statement.
2434 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002435 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002436 * @param[in,out] data Data to read from, always moved to currently handled character.
2437 * @param[in,out] max Value to write to.
2438 * @param[in,out] flags Flags to write to.
2439 * @param[in,out] exts Extension instances to add to.
2440 *
2441 * @return LY_ERR values.
2442 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002443static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002444parse_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 +02002445{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002446 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002447 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002448 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002449 unsigned long int num;
2450 enum yang_keyword kw;
2451
2452 if (*flags & LYS_SET_MAX) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002453 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002454 return LY_EVALID;
2455 }
2456 *flags |= LYS_SET_MAX;
2457
2458 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002459 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002460
2461 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002462 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002463 free(buf);
2464 return LY_EVALID;
2465 }
2466
2467 if (strncmp(word, "unbounded", word_len)) {
2468 errno = 0;
2469 num = strtoul(word, &ptr, 10);
2470 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002471 if ((size_t)(ptr - word) != word_len) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002472 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002473 free(buf);
2474 return LY_EVALID;
2475 }
2476 if ((errno == ERANGE) || (num > UINT32_MAX)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002477 LOGVAL_YANG(ctx, LY_VCODE_OOB, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002478 free(buf);
2479 return LY_EVALID;
2480 }
2481
2482 *max = num;
2483 }
2484 free(buf);
2485
Radek Krejci6d6556c2018-11-08 09:37:45 +01002486 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002487 switch (kw) {
2488 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002489 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MAX, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002490 break;
2491 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002492 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002493 return LY_EVALID;
2494 }
2495 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002496 return ret;
2497}
2498
Michal Vaskoea5abea2018-09-18 13:10:54 +02002499/**
2500 * @brief Parse the min-elements statement.
2501 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002502 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002503 * @param[in,out] data Data to read from, always moved to currently handled character.
2504 * @param[in,out] min Value to write to.
2505 * @param[in,out] flags Flags to write to.
2506 * @param[in,out] exts Extension instances to add to.
2507 *
2508 * @return LY_ERR values.
2509 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002510static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002511parse_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 +02002512{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002513 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002514 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002515 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002516 unsigned long int num;
2517 enum yang_keyword kw;
2518
2519 if (*flags & LYS_SET_MIN) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002520 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002521 return LY_EVALID;
2522 }
2523 *flags |= LYS_SET_MIN;
2524
2525 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002526 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002527
2528 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002529 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002530 free(buf);
2531 return LY_EVALID;
2532 }
2533
2534 errno = 0;
2535 num = strtoul(word, &ptr, 10);
2536 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002537 if ((size_t)(ptr - word) != word_len) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002538 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002539 free(buf);
2540 return LY_EVALID;
2541 }
2542 if ((errno == ERANGE) || (num > UINT32_MAX)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002543 LOGVAL_YANG(ctx, LY_VCODE_OOB, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002544 free(buf);
2545 return LY_EVALID;
2546 }
2547 *min = num;
2548 free(buf);
2549
Radek Krejci6d6556c2018-11-08 09:37:45 +01002550 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002551 switch (kw) {
2552 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002553 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MIN, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002554 break;
2555 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002556 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002557 return LY_EVALID;
2558 }
2559 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002560 return ret;
2561}
2562
Michal Vaskoea5abea2018-09-18 13:10:54 +02002563/**
2564 * @brief Parse the ordered-by statement.
2565 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002566 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002567 * @param[in,out] data Data to read from, always moved to currently handled character.
2568 * @param[in,out] flags Flags to write to.
2569 * @param[in,out] exts Extension instances to add to.
2570 *
2571 * @return LY_ERR values.
2572 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002573static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002574parse_orderedby(struct lys_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002575{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002576 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002577 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002578 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002579 enum yang_keyword kw;
2580
2581 if (*flags & LYS_ORDBY_MASK) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002582 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002583 return LY_EVALID;
2584 }
2585
2586 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002587 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002588
2589 if ((word_len == 6) && !strncmp(word, "system", word_len)) {
2590 *flags |= LYS_ORDBY_SYSTEM;
2591 } else if ((word_len == 4) && !strncmp(word, "user", word_len)) {
2592 *flags |= LYS_ORDBY_USER;
2593 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002594 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002595 free(buf);
2596 return LY_EVALID;
2597 }
2598 free(buf);
2599
Radek Krejci6d6556c2018-11-08 09:37:45 +01002600 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002601 switch (kw) {
2602 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002603 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_ORDEREDBY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002604 break;
2605 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002606 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002607 return LY_EVALID;
2608 }
2609 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002610 return ret;
2611}
2612
Michal Vaskoea5abea2018-09-18 13:10:54 +02002613/**
2614 * @brief Parse the leaf-list statement.
2615 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002616 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002617 * @param[in,out] data Data to read from, always moved to currently handled character.
2618 * @param[in,out] siblings Siblings to add to.
2619 *
2620 * @return LY_ERR values.
2621 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002622static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002623parse_leaflist(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002624{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002625 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002626 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002627 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002628 enum yang_keyword kw;
2629 struct lysp_node *iter;
2630 struct lysp_node_leaflist *llist;
2631
2632 /* create structure */
2633 llist = calloc(1, sizeof *llist);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002634 LY_CHECK_ERR_RET(!llist, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002635 llist->nodetype = LYS_LEAFLIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002636 llist->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002637
2638 /* insert into siblings */
2639 if (!*siblings) {
2640 *siblings = (struct lysp_node *)llist;
2641 } else {
2642 for (iter = *siblings; iter->next; iter = iter->next);
2643 iter->next = (struct lysp_node *)llist;
2644 }
2645
2646 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002647 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002648 INSERT_WORD(ctx, buf, llist->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002649
2650 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01002651 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002652 switch (kw) {
2653 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002654 LY_CHECK_RET(parse_config(ctx, data, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002655 break;
2656 case YANG_DEFAULT:
Radek Krejciceaf2122019-01-02 15:03:26 +01002657 YANG_CHECK_STMTVER2_RET(ctx, "default", "leaf-list");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002658 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 +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, &llist->dsc, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002662 break;
2663 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002664 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 +02002665 break;
2666 case YANG_MAX_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002667 LY_CHECK_RET(parse_maxelements(ctx, data, &llist->max, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002668 break;
2669 case YANG_MIN_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002670 LY_CHECK_RET(parse_minelements(ctx, data, &llist->min, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002671 break;
2672 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002673 LY_CHECK_RET(parse_restrs(ctx, data, kw, &llist->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002674 break;
2675 case YANG_ORDERED_BY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002676 LY_CHECK_RET(parse_orderedby(ctx, data, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002677 break;
2678 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002679 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 +02002680 break;
2681 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002682 LY_CHECK_RET(parse_status(ctx, data, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002683 break;
2684 case YANG_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002685 LY_CHECK_RET(parse_type(ctx, data, &llist->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002686 break;
2687 case YANG_UNITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002688 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 +02002689 break;
2690 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002691 LY_CHECK_RET(parse_when(ctx, data, &llist->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002692 break;
2693 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002694 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002695 break;
2696 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002697 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002698 return LY_EVALID;
2699 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002700 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002701 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002702checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002703 /* mandatory substatements */
2704 if (!llist->type.name) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002705 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002706 return LY_EVALID;
2707 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01002708 if ((llist->min) && (llist->dflts)) {
2709 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMSCOMB, "min-elements", "default", "leaf-list");
2710 return LY_EVALID;
2711 }
Radek Krejcidf6cad12018-11-28 17:10:55 +01002712 if (llist->max && llist->min > llist->max) {
2713 LOGVAL_YANG(ctx, LYVE_SEMANTICS,
2714 "Invalid combination of min-elements and max-elements: min value %u is bigger than the max value %u.",
2715 llist->min, llist->max);
2716 return LY_EVALID;
2717 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002718
2719 return ret;
2720}
2721
Michal Vaskoea5abea2018-09-18 13:10:54 +02002722/**
2723 * @brief Parse the refine statement.
2724 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002725 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002726 * @param[in,out] data Data to read from, always moved to currently handled character.
2727 * @param[in,out] refines Refines to add to.
2728 *
2729 * @return LY_ERR values.
2730 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002731static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002732parse_refine(struct lys_parser_ctx *ctx, const char **data, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002733{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002734 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002735 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002736 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002737 enum yang_keyword kw;
2738 struct lysp_refine *rf;
2739
Radek Krejci2c4e7172018-10-19 15:56:26 +02002740 LY_ARRAY_NEW_RET(ctx->ctx, *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002741
2742 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002743 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002744 INSERT_WORD(ctx, buf, rf->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002745
Radek Krejci6d6556c2018-11-08 09:37:45 +01002746 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002747 switch (kw) {
2748 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002749 LY_CHECK_RET(parse_config(ctx, data, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002750 break;
2751 case YANG_DEFAULT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002752 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 +02002753 break;
2754 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002755 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 +02002756 break;
2757 case YANG_IF_FEATURE:
Radek Krejciceaf2122019-01-02 15:03:26 +01002758 YANG_CHECK_STMTVER2_RET(ctx, "if-feature", "refine");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002759 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 +02002760 break;
2761 case YANG_MAX_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002762 LY_CHECK_RET(parse_maxelements(ctx, data, &rf->max, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002763 break;
2764 case YANG_MIN_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002765 LY_CHECK_RET(parse_minelements(ctx, data, &rf->min, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002766 break;
2767 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002768 LY_CHECK_RET(parse_restrs(ctx, data, kw, &rf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002769 break;
2770 case YANG_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002771 LY_CHECK_RET(parse_mandatory(ctx, data, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002772 break;
2773 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002774 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 +02002775 break;
2776 case YANG_PRESENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002777 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 +02002778 break;
2779 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002780 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002781 break;
2782 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002783 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002784 return LY_EVALID;
2785 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002786 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002787 return ret;
2788}
2789
Michal Vaskoea5abea2018-09-18 13:10:54 +02002790/**
2791 * @brief Parse the typedef statement.
2792 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002793 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002794 * @param[in,out] data Data to read from, always moved to currently handled character.
2795 * @param[in,out] typedefs Typedefs to add to.
2796 *
2797 * @return LY_ERR values.
2798 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002799static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002800parse_typedef(struct lys_parser_ctx *ctx, struct lysp_node *parent, const char **data, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002801{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002802 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002803 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002804 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002805 enum yang_keyword kw;
2806 struct lysp_tpdf *tpdf;
2807
Radek Krejci2c4e7172018-10-19 15:56:26 +02002808 LY_ARRAY_NEW_RET(ctx->ctx, *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002809
2810 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002811 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002812 INSERT_WORD(ctx, buf, tpdf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002813
2814 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01002815 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002816 switch (kw) {
2817 case YANG_DEFAULT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002818 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 +02002819 break;
2820 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002821 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 +02002822 break;
2823 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002824 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 +02002825 break;
2826 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002827 LY_CHECK_RET(parse_status(ctx, data, &tpdf->flags, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002828 break;
2829 case YANG_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002830 LY_CHECK_RET(parse_type(ctx, data, &tpdf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002831 break;
2832 case YANG_UNITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002833 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 +02002834 break;
2835 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002836 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002837 break;
2838 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002839 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002840 return LY_EVALID;
2841 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002842 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002843 LY_CHECK_RET(ret);
Radek Krejcibbe09a92018-11-08 09:36:54 +01002844checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002845 /* mandatory substatements */
2846 if (!tpdf->type.name) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002847 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002848 return LY_EVALID;
2849 }
2850
Radek Krejcibbe09a92018-11-08 09:36:54 +01002851 /* store data for collision check */
2852 if (parent) {
2853 ly_set_add(&ctx->tpdfs_nodes, parent, 0);
2854 }
2855
Michal Vasko7fbc8162018-09-17 10:35:16 +02002856 return ret;
2857}
2858
Michal Vaskoea5abea2018-09-18 13:10:54 +02002859/**
2860 * @brief Parse the input or output statement.
2861 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002862 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002863 * @param[in,out] data Data to read from, always moved to currently handled character.
2864 * @param[in] kw Type of this particular keyword
2865 * @param[in,out] inout_p Input/output pointer to write to.
2866 *
2867 * @return LY_ERR values.
2868 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002869static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002870parse_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 +02002871{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002872 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002873 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002874 size_t word_len;
Radek Krejci10113652018-11-14 16:56:50 +01002875 enum yang_keyword kw;
Radek Krejcie86bf772018-12-14 11:39:53 +01002876 unsigned int u;
2877 struct lysp_node *child;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002878
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002879 if (inout_p->nodetype) {
Radek Krejci10113652018-11-14 16:56:50 +01002880 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002881 return LY_EVALID;
2882 }
2883
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002884 /* initiate structure */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002885 inout_p->nodetype = &((struct lysp_action*)parent)->input == inout_p ? LYS_INPUT : LYS_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002886 inout_p->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002887
2888 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01002889 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002890 switch (kw) {
2891 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01002892 YANG_CHECK_STMTVER2_RET(ctx, "anydata", ly_stmt2str(inout_kw));
Radek Krejci10113652018-11-14 16:56:50 +01002893 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002894 case YANG_ANYXML:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002895 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002896 break;
2897 case YANG_CHOICE:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002898 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002899 break;
2900 case YANG_CONTAINER:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002901 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002902 break;
2903 case YANG_LEAF:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002904 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002905 break;
2906 case YANG_LEAF_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002907 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002908 break;
2909 case YANG_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002910 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002911 break;
2912 case YANG_USES:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002913 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002914 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002915 case YANG_TYPEDEF:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002916 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)inout_p, data, &inout_p->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002917 break;
2918 case YANG_MUST:
Radek Krejciceaf2122019-01-02 15:03:26 +01002919 YANG_CHECK_STMTVER2_RET(ctx, "must", ly_stmt2str(inout_kw));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002920 LY_CHECK_RET(parse_restrs(ctx, data, kw, &inout_p->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002921 break;
2922 case YANG_GROUPING:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002923 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)inout_p, &inout_p->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002924 break;
2925 case YANG_CUSTOM:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002926 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inout_p->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002927 break;
2928 default:
Radek Krejci10113652018-11-14 16:56:50 +01002929 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002930 return LY_EVALID;
2931 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002932 }
Radek Krejcie86bf772018-12-14 11:39:53 +01002933 /* finalize parent pointers to the reallocated items */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002934 LY_ARRAY_FOR(inout_p->groupings, u) {
2935 LY_LIST_FOR(inout_p->groupings[u].data, child) {
2936 child->parent = (struct lysp_node*)&inout_p->groupings[u];
Radek Krejcie86bf772018-12-14 11:39:53 +01002937 }
2938 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002939 return ret;
2940}
2941
Michal Vaskoea5abea2018-09-18 13:10:54 +02002942/**
2943 * @brief Parse the action statement.
2944 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002945 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002946 * @param[in,out] data Data to read from, always moved to currently handled character.
2947 * @param[in,out] actions Actions to add to.
2948 *
2949 * @return LY_ERR values.
2950 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002951static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002952parse_action(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002953{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002954 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002955 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002956 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002957 enum yang_keyword kw;
2958 struct lysp_action *act;
Radek Krejcie86bf772018-12-14 11:39:53 +01002959 struct lysp_node *child;
2960 unsigned int u;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002961
Radek Krejci2c4e7172018-10-19 15:56:26 +02002962 LY_ARRAY_NEW_RET(ctx->ctx, *actions, act, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002963
2964 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002965 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002966 INSERT_WORD(ctx, buf, act->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002967 act->nodetype = LYS_ACTION;
2968 act->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002969
Radek Krejci6d6556c2018-11-08 09:37:45 +01002970 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002971 switch (kw) {
2972 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002973 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 +02002974 break;
2975 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002976 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 +02002977 break;
2978 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002979 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 +02002980 break;
2981 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002982 LY_CHECK_RET(parse_status(ctx, data, &act->flags, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002983 break;
2984
2985 case YANG_INPUT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002986 LY_CHECK_RET(parse_inout(ctx, data, kw, (struct lysp_node*)act, &act->input));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002987 break;
2988 case YANG_OUTPUT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002989 LY_CHECK_RET(parse_inout(ctx, data, kw, (struct lysp_node*)act, &act->output));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002990 break;
2991
2992 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01002993 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)act, data, &act->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002994 break;
2995 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002996 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)act, &act->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002997 break;
2998 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002999 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003000 break;
3001 default:
Radek Krejcif538ce52019-03-05 10:46:14 +01003002 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), parent ? "action" : "rpc");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003003 return LY_EVALID;
3004 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003005 }
Radek Krejcie86bf772018-12-14 11:39:53 +01003006 /* finalize parent pointers to the reallocated items */
3007 LY_ARRAY_FOR(act->groupings, u) {
3008 LY_LIST_FOR(act->groupings[u].data, child) {
3009 child->parent = (struct lysp_node*)&act->groupings[u];
3010 }
3011 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003012 return ret;
3013}
3014
Michal Vaskoea5abea2018-09-18 13:10:54 +02003015/**
3016 * @brief Parse the notification statement.
3017 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003018 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003019 * @param[in,out] data Data to read from, always moved to currently handled character.
3020 * @param[in,out] notifs Notifications to add to.
3021 *
3022 * @return LY_ERR values.
3023 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003024static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003025parse_notif(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003026{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003027 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003028 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003029 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003030 enum yang_keyword kw;
3031 struct lysp_notif *notif;
Radek Krejcie86bf772018-12-14 11:39:53 +01003032 struct lysp_node *child;
3033 unsigned int u;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003034
Radek Krejci2c4e7172018-10-19 15:56:26 +02003035 LY_ARRAY_NEW_RET(ctx->ctx, *notifs, notif, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003036
3037 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003038 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003039 INSERT_WORD(ctx, buf, notif->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003040 notif->nodetype = LYS_NOTIF;
3041 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003042
Radek Krejci6d6556c2018-11-08 09:37:45 +01003043 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003044 switch (kw) {
3045 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003046 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 +02003047 break;
3048 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003049 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 +02003050 break;
3051 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003052 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 +02003053 break;
3054 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003055 LY_CHECK_RET(parse_status(ctx, data, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003056 break;
3057
3058 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01003059 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "notification");
Radek Krejci10113652018-11-14 16:56:50 +01003060 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003061 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003062 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003063 break;
3064 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003065 LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003066 break;
3067 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003068 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003069 break;
3070 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003071 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003072 break;
3073 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003074 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003075 break;
3076 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003077 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003078 break;
3079 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003080 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003081 break;
3082
3083 case YANG_MUST:
Radek Krejciceaf2122019-01-02 15:03:26 +01003084 YANG_CHECK_STMTVER2_RET(ctx, "must", "notification");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003085 LY_CHECK_RET(parse_restrs(ctx, data, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003086 break;
3087 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01003088 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)notif, data, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003089 break;
3090 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003091 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003092 break;
3093 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003094 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003095 break;
3096 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003097 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003098 return LY_EVALID;
3099 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003100 }
Radek Krejcie86bf772018-12-14 11:39:53 +01003101 /* finalize parent pointers to the reallocated items */
3102 LY_ARRAY_FOR(notif->groupings, u) {
3103 LY_LIST_FOR(notif->groupings[u].data, child) {
3104 child->parent = (struct lysp_node*)&notif->groupings[u];
3105 }
3106 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003107 return ret;
3108}
3109
Michal Vaskoea5abea2018-09-18 13:10:54 +02003110/**
3111 * @brief Parse the grouping statement.
3112 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003113 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003114 * @param[in,out] data Data to read from, always moved to currently handled character.
3115 * @param[in,out] groupings Groupings to add to.
3116 *
3117 * @return LY_ERR values.
3118 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003119static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003120parse_grouping(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003121{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003122 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003123 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003124 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003125 enum yang_keyword kw;
3126 struct lysp_grp *grp;
Radek Krejcie86bf772018-12-14 11:39:53 +01003127 struct lysp_node *child;
3128 unsigned int u;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003129
Radek Krejci2c4e7172018-10-19 15:56:26 +02003130 LY_ARRAY_NEW_RET(ctx->ctx, *groupings, grp, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003131
3132 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003133 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003134 INSERT_WORD(ctx, buf, grp->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003135 grp->nodetype = LYS_GROUPING;
3136 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003137
Radek Krejci6d6556c2018-11-08 09:37:45 +01003138 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003139 switch (kw) {
3140 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003141 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 +02003142 break;
3143 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003144 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 +02003145 break;
3146 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003147 LY_CHECK_RET(parse_status(ctx, data, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003148 break;
3149
3150 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01003151 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "grouping");
Radek Krejci10113652018-11-14 16:56:50 +01003152 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003153 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003154 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003155 break;
3156 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003157 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003158 break;
3159 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003160 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003161 break;
3162 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003163 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003164 break;
3165 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003166 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003167 break;
3168 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003169 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003170 break;
3171 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003172 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003173 break;
3174
3175 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01003176 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)grp, data, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003177 break;
3178 case YANG_ACTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003179 YANG_CHECK_STMTVER2_RET(ctx, "action", "grouping");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003180 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)grp, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003181 break;
3182 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003183 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)grp, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003184 break;
3185 case YANG_NOTIFICATION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003186 YANG_CHECK_STMTVER2_RET(ctx, "notification", "grouping");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003187 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)grp, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003188 break;
3189 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003190 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003191 break;
3192 default:
Radek Krejci10113652018-11-14 16:56:50 +01003193 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003194 return LY_EVALID;
3195 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003196 }
Radek Krejcie86bf772018-12-14 11:39:53 +01003197 /* finalize parent pointers to the reallocated items */
3198 LY_ARRAY_FOR(grp->groupings, u) {
3199 LY_LIST_FOR(grp->groupings[u].data, child) {
3200 child->parent = (struct lysp_node*)&grp->groupings[u];
3201 }
3202 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003203 return ret;
3204}
3205
Michal Vaskoea5abea2018-09-18 13:10:54 +02003206/**
3207 * @brief Parse the refine statement.
3208 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003209 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003210 * @param[in,out] data Data to read from, always moved to currently handled character.
3211 * @param[in,out] augments Augments to add to.
3212 *
3213 * @return LY_ERR values.
3214 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003215static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003216parse_augment(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003217{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003218 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003219 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003220 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003221 enum yang_keyword kw;
3222 struct lysp_augment *aug;
3223
Radek Krejci2c4e7172018-10-19 15:56:26 +02003224 LY_ARRAY_NEW_RET(ctx->ctx, *augments, aug, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003225
3226 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003227 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003228 INSERT_WORD(ctx, buf, aug->nodeid, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003229 aug->nodetype = LYS_AUGMENT;
3230 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003231
Radek Krejci6d6556c2018-11-08 09:37:45 +01003232 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003233 switch (kw) {
3234 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003235 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 +02003236 break;
3237 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003238 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 +02003239 break;
3240 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003241 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 +02003242 break;
3243 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003244 LY_CHECK_RET(parse_status(ctx, data, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003245 break;
3246 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003247 LY_CHECK_RET(parse_when(ctx, data, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003248 break;
3249
3250 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01003251 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "augment");
Radek Krejci10113652018-11-14 16:56:50 +01003252 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003253 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003254 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003255 break;
3256 case YANG_CASE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003257 LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003258 break;
3259 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003260 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003261 break;
3262 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003263 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003264 break;
3265 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003266 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003267 break;
3268 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003269 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003270 break;
3271 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003272 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003273 break;
3274 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003275 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003276 break;
3277
3278 case YANG_ACTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003279 YANG_CHECK_STMTVER2_RET(ctx, "action", "augment");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003280 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003281 break;
3282 case YANG_NOTIFICATION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003283 YANG_CHECK_STMTVER2_RET(ctx, "notification", "augment");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003284 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003285 break;
3286 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003287 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003288 break;
3289 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003290 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003291 return LY_EVALID;
3292 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003293 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003294 return ret;
3295}
3296
Michal Vaskoea5abea2018-09-18 13:10:54 +02003297/**
3298 * @brief Parse the uses statement.
3299 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003300 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003301 * @param[in,out] data Data to read from, always moved to currently handled character.
3302 * @param[in,out] siblings Siblings to add to.
3303 *
3304 * @return LY_ERR values.
3305 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003306static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003307parse_uses(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003308{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003309 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003310 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003311 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003312 enum yang_keyword kw;
3313 struct lysp_node *iter;
3314 struct lysp_node_uses *uses;
3315
3316 /* create structure */
3317 uses = calloc(1, sizeof *uses);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003318 LY_CHECK_ERR_RET(!uses, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003319 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003320 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003321
3322 /* insert into siblings */
3323 if (!*siblings) {
3324 *siblings = (struct lysp_node *)uses;
3325 } else {
3326 for (iter = *siblings; iter->next; iter = iter->next);
3327 iter->next = (struct lysp_node *)uses;
3328 }
3329
3330 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003331 LY_CHECK_RET(get_argument(ctx, data, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003332 INSERT_WORD(ctx, buf, uses->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003333
3334 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01003335 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003336 switch (kw) {
3337 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003338 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 +02003339 break;
3340 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003341 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 +02003342 break;
3343 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003344 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 +02003345 break;
3346 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003347 LY_CHECK_RET(parse_status(ctx, data, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003348 break;
3349 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003350 LY_CHECK_RET(parse_when(ctx, data, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003351 break;
3352
3353 case YANG_REFINE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003354 LY_CHECK_RET(parse_refine(ctx, data, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003355 break;
3356 case YANG_AUGMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003357 LY_CHECK_RET(parse_augment(ctx, data, (struct lysp_node*)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003358 break;
3359 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003360 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003361 break;
3362 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003363 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003364 return LY_EVALID;
3365 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003366 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003367 return ret;
3368}
3369
Michal Vaskoea5abea2018-09-18 13:10:54 +02003370/**
3371 * @brief Parse the case statement.
3372 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003373 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003374 * @param[in,out] data Data to read from, always moved to currently handled character.
3375 * @param[in,out] siblings Siblings to add to.
3376 *
3377 * @return LY_ERR values.
3378 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003379static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003380parse_case(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003381{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003382 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003383 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003384 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003385 enum yang_keyword kw;
3386 struct lysp_node *iter;
3387 struct lysp_node_case *cas;
3388
3389 /* create structure */
3390 cas = calloc(1, sizeof *cas);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003391 LY_CHECK_ERR_RET(!cas, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003392 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003393 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003394
3395 /* insert into siblings */
3396 if (!*siblings) {
3397 *siblings = (struct lysp_node *)cas;
3398 } else {
3399 for (iter = *siblings; iter->next; iter = iter->next);
3400 iter->next = (struct lysp_node *)cas;
3401 }
3402
3403 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003404 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003405 INSERT_WORD(ctx, buf, cas->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003406
3407 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01003408 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003409 switch (kw) {
3410 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003411 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 +02003412 break;
3413 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003414 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 +02003415 break;
3416 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003417 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 +02003418 break;
3419 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003420 LY_CHECK_RET(parse_status(ctx, data, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003421 break;
3422 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003423 LY_CHECK_RET(parse_when(ctx, data, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003424 break;
3425
3426 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01003427 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "case");
Radek Krejci10113652018-11-14 16:56:50 +01003428 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003429 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003430 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003431 break;
3432 case YANG_CHOICE:
Radek Krejcia9026eb2018-12-12 16:04:47 +01003433 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003434 break;
3435 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003436 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003437 break;
3438 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003439 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003440 break;
3441 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003442 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003443 break;
3444 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003445 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003446 break;
3447 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003448 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003449 break;
3450 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003451 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003452 break;
3453 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003454 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003455 return LY_EVALID;
3456 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003457 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003458 return ret;
3459}
3460
Michal Vaskoea5abea2018-09-18 13:10:54 +02003461/**
3462 * @brief Parse the choice statement.
3463 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003464 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003465 * @param[in,out] data Data to read from, always moved to currently handled character.
3466 * @param[in,out] siblings Siblings to add to.
3467 *
3468 * @return LY_ERR values.
3469 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003470static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003471parse_choice(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003472{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003473 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003474 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003475 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003476 enum yang_keyword kw;
3477 struct lysp_node *iter;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003478 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003479
3480 /* create structure */
Radek Krejci44ceedc2018-10-02 15:54:31 +02003481 choice = calloc(1, sizeof *choice);
3482 LY_CHECK_ERR_RET(!choice, LOGMEM(ctx->ctx), LY_EMEM);
3483 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003484 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003485
3486 /* insert into siblings */
3487 if (!*siblings) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02003488 *siblings = (struct lysp_node *)choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003489 } else {
3490 for (iter = *siblings; iter->next; iter = iter->next);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003491 iter->next = (struct lysp_node *)choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003492 }
3493
3494 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003495 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003496 INSERT_WORD(ctx, buf, choice->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003497
3498 /* parse substatements */
Radek Krejcia9026eb2018-12-12 16:04:47 +01003499 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003500 switch (kw) {
3501 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003502 LY_CHECK_RET(parse_config(ctx, data, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003503 break;
3504 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003505 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 +02003506 break;
3507 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003508 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 +02003509 break;
3510 case YANG_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003511 LY_CHECK_RET(parse_mandatory(ctx, data, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003512 break;
3513 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003514 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 +02003515 break;
3516 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003517 LY_CHECK_RET(parse_status(ctx, data, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003518 break;
3519 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003520 LY_CHECK_RET(parse_when(ctx, data, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003521 break;
3522 case YANG_DEFAULT:
Radek Krejcia9026eb2018-12-12 16:04:47 +01003523 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 +02003524 break;
3525
3526 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01003527 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "choice");
Radek Krejci10113652018-11-14 16:56:50 +01003528 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003529 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003530 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003531 break;
3532 case YANG_CASE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003533 LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003534 break;
3535 case YANG_CHOICE:
Radek Krejciceaf2122019-01-02 15:03:26 +01003536 YANG_CHECK_STMTVER2_RET(ctx, "choice", "choice");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003537 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003538 break;
3539 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003540 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003541 break;
3542 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003543 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003544 break;
3545 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003546 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003547 break;
3548 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003549 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003550 break;
3551 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003552 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003553 break;
3554 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003555 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003556 return LY_EVALID;
3557 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003558 }
Radek Krejcia9026eb2018-12-12 16:04:47 +01003559 LY_CHECK_RET(ret);
3560checks:
3561 if ((choice->flags & LYS_MAND_TRUE) && choice->dflt) {
3562 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMSCOMB, "mandatory", "default", "choice");
3563 return LY_EVALID;
3564 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003565 return ret;
3566}
3567
Michal Vaskoea5abea2018-09-18 13:10:54 +02003568/**
3569 * @brief Parse the container statement.
3570 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003571 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003572 * @param[in,out] data Data to read from, always moved to currently handled character.
3573 * @param[in,out] siblings Siblings to add to.
3574 *
3575 * @return LY_ERR values.
3576 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003577static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003578parse_container(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003579{
3580 LY_ERR ret = 0;
3581 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003582 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003583 enum yang_keyword kw;
3584 struct lysp_node *iter;
3585 struct lysp_node_container *cont;
Radek Krejcie86bf772018-12-14 11:39:53 +01003586 unsigned int u;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003587
3588 /* create structure */
3589 cont = calloc(1, sizeof *cont);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003590 LY_CHECK_ERR_RET(!cont, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003591 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003592 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003593
3594 /* insert into siblings */
3595 if (!*siblings) {
3596 *siblings = (struct lysp_node *)cont;
3597 } else {
3598 for (iter = *siblings; iter->next; iter = iter->next);
3599 iter->next = (struct lysp_node *)cont;
3600 }
3601
3602 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003603 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003604 INSERT_WORD(ctx, buf, cont->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003605
3606 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01003607 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003608 switch (kw) {
3609 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003610 LY_CHECK_RET(parse_config(ctx, data, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003611 break;
3612 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003613 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 +02003614 break;
3615 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003616 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 +02003617 break;
3618 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003619 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 +02003620 break;
3621 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003622 LY_CHECK_RET(parse_status(ctx, data, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003623 break;
3624 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003625 LY_CHECK_RET(parse_when(ctx, data, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003626 break;
3627 case YANG_PRESENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003628 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 +02003629 break;
3630
3631 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01003632 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "container");
Radek Krejci10113652018-11-14 16:56:50 +01003633 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003634 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003635 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003636 break;
3637 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003638 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003639 break;
3640 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003641 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003642 break;
3643 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003644 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003645 break;
3646 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003647 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003648 break;
3649 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003650 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003651 break;
3652 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003653 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003654 break;
3655
3656 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01003657 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)cont, data, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003658 break;
3659 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003660 LY_CHECK_RET(parse_restrs(ctx, data, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003661 break;
3662 case YANG_ACTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003663 YANG_CHECK_STMTVER2_RET(ctx, "action", "container");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003664 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003665 break;
3666 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003667 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003668 break;
3669 case YANG_NOTIFICATION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003670 YANG_CHECK_STMTVER2_RET(ctx, "notification", "container");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003671 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003672 break;
3673 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003674 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003675 break;
3676 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003677 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003678 return LY_EVALID;
3679 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003680 }
Radek Krejcie86bf772018-12-14 11:39:53 +01003681 /* finalize parent pointers to the reallocated items */
3682 LY_ARRAY_FOR(cont->groupings, u) {
3683 LY_LIST_FOR(cont->groupings[u].data, iter) {
3684 iter->parent = (struct lysp_node*)&cont->groupings[u];
3685 }
3686 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003687 return ret;
3688}
3689
Michal Vaskoea5abea2018-09-18 13:10:54 +02003690/**
3691 * @brief Parse the list statement.
3692 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003693 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003694 * @param[in,out] data Data to read from, always moved to currently handled character.
3695 * @param[in,out] siblings Siblings to add to.
3696 *
3697 * @return LY_ERR values.
3698 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003699static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003700parse_list(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003701{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003702 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003703 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003704 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003705 enum yang_keyword kw;
3706 struct lysp_node *iter;
3707 struct lysp_node_list *list;
Radek Krejcie86bf772018-12-14 11:39:53 +01003708 unsigned int u;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003709
3710 /* create structure */
3711 list = calloc(1, sizeof *list);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003712 LY_CHECK_ERR_RET(!list, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003713 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003714 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003715
3716 /* insert into siblings */
3717 if (!*siblings) {
3718 *siblings = (struct lysp_node *)list;
3719 } else {
3720 for (iter = *siblings; iter->next; iter = iter->next);
3721 iter->next = (struct lysp_node *)list;
3722 }
3723
3724 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003725 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003726 INSERT_WORD(ctx, buf, list->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003727
3728 /* parse substatements */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003729 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003730 switch (kw) {
3731 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003732 LY_CHECK_RET(parse_config(ctx, data, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003733 break;
3734 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003735 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 +02003736 break;
3737 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003738 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 +02003739 break;
3740 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003741 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 +02003742 break;
3743 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003744 LY_CHECK_RET(parse_status(ctx, data, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003745 break;
3746 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003747 LY_CHECK_RET(parse_when(ctx, data, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003748 break;
3749 case YANG_KEY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003750 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 +02003751 break;
3752 case YANG_MAX_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003753 LY_CHECK_RET(parse_maxelements(ctx, data, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003754 break;
3755 case YANG_MIN_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003756 LY_CHECK_RET(parse_minelements(ctx, data, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003757 break;
3758 case YANG_ORDERED_BY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003759 LY_CHECK_RET(parse_orderedby(ctx, data, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003760 break;
3761 case YANG_UNIQUE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003762 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 +02003763 break;
3764
3765 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01003766 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "list");
Radek Krejci10113652018-11-14 16:56:50 +01003767 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003768 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003769 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003770 break;
3771 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003772 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003773 break;
3774 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003775 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003776 break;
3777 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003778 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003779 break;
3780 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003781 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003782 break;
3783 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003784 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003785 break;
3786 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003787 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003788 break;
3789
3790 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01003791 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)list, data, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003792 break;
3793 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003794 LY_CHECK_RET(parse_restrs(ctx, data, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003795 break;
3796 case YANG_ACTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003797 YANG_CHECK_STMTVER2_RET(ctx, "action", "list");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003798 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003799 break;
3800 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003801 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003802 break;
3803 case YANG_NOTIFICATION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003804 YANG_CHECK_STMTVER2_RET(ctx, "notification", "list");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003805 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003806 break;
3807 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003808 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003809 break;
3810 default:
Radek Krejci10113652018-11-14 16:56:50 +01003811 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003812 return LY_EVALID;
3813 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003814 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003815 LY_CHECK_RET(ret);
Radek Krejcie86bf772018-12-14 11:39:53 +01003816 /* finalize parent pointers to the reallocated items */
3817 LY_ARRAY_FOR(list->groupings, u) {
3818 LY_LIST_FOR(list->groupings[u].data, iter) {
3819 iter->parent = (struct lysp_node*)&list->groupings[u];
3820 }
3821 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003822checks:
3823 if (list->max && list->min > list->max) {
3824 LOGVAL_YANG(ctx, LYVE_SEMANTICS,
3825 "Invalid combination of min-elements and max-elements: min value %u is bigger than the max value %u.",
3826 list->min, list->max);
3827 return LY_EVALID;
3828 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003829
3830 return ret;
3831}
3832
Michal Vaskoea5abea2018-09-18 13:10:54 +02003833/**
3834 * @brief Parse the yin-element statement.
3835 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003836 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003837 * @param[in,out] data Data to read from, always moved to currently handled character.
3838 * @param[in,out] flags Flags to write to.
3839 * @param[in,out] exts Extension instances to add to.
3840 *
3841 * @return LY_ERR values.
3842 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003843static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003844parse_yinelement(struct lys_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003845{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003846 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003847 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003848 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003849 enum yang_keyword kw;
3850
3851 if (*flags & LYS_YINELEM_MASK) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02003852 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003853 return LY_EVALID;
3854 }
3855
3856 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003857 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003858
3859 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
3860 *flags |= LYS_YINELEM_TRUE;
3861 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
3862 *flags |= LYS_YINELEM_FALSE;
3863 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02003864 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003865 free(buf);
3866 return LY_EVALID;
3867 }
3868 free(buf);
3869
Radek Krejci6d6556c2018-11-08 09:37:45 +01003870 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003871 switch (kw) {
3872 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003873 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_YINELEM, 0, exts));
3874 LY_CHECK_RET(ret); break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003875 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003876 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003877 return LY_EVALID;
3878 }
3879 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003880 return ret;
3881}
3882
Michal Vaskoea5abea2018-09-18 13:10:54 +02003883/**
3884 * @brief Parse the yin-element statement.
3885 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003886 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003887 * @param[in,out] data Data to read from, always moved to currently handled character.
3888 * @param[in,out] argument Value to write to.
3889 * @param[in,out] flags Flags to write to.
3890 * @param[in,out] exts Extension instances to add to.
3891 *
3892 * @return LY_ERR values.
3893 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003894static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003895parse_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 +02003896{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003897 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003898 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003899 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003900 enum yang_keyword kw;
3901
3902 if (*argument) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02003903 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003904 return LY_EVALID;
3905 }
3906
3907 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003908 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003909 INSERT_WORD(ctx, buf, *argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003910
Radek Krejci6d6556c2018-11-08 09:37:45 +01003911 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003912 switch (kw) {
3913 case YANG_YIN_ELEMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003914 LY_CHECK_RET(parse_yinelement(ctx, data, flags, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003915 break;
3916 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003917 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_ARGUMENT, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003918 break;
3919 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003920 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003921 return LY_EVALID;
3922 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003923 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003924 return ret;
3925}
3926
Michal Vaskoea5abea2018-09-18 13:10:54 +02003927/**
3928 * @brief Parse the extension statement.
3929 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003930 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003931 * @param[in,out] data Data to read from, always moved to currently handled character.
3932 * @param[in,out] extensions Extensions to add to.
3933 *
3934 * @return LY_ERR values.
3935 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003936static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003937parse_extension(struct lys_parser_ctx *ctx, const char **data, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003938{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003939 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003940 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003941 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003942 enum yang_keyword kw;
3943 struct lysp_ext *ex;
3944
Radek Krejci2c4e7172018-10-19 15:56:26 +02003945 LY_ARRAY_NEW_RET(ctx->ctx, *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003946
3947 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003948 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003949 INSERT_WORD(ctx, buf, ex->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003950
Radek Krejci6d6556c2018-11-08 09:37:45 +01003951 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003952 switch (kw) {
3953 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003954 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 +02003955 break;
3956 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003957 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 +02003958 break;
3959 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003960 LY_CHECK_RET(parse_status(ctx, data, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003961 break;
3962 case YANG_ARGUMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003963 LY_CHECK_RET(parse_argument(ctx, data, &ex->argument, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003964 break;
3965 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003966 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003967 break;
3968 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003969 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003970 return LY_EVALID;
3971 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003972 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003973 return ret;
3974}
3975
Michal Vaskoea5abea2018-09-18 13:10:54 +02003976/**
3977 * @brief Parse the deviate statement.
3978 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003979 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003980 * @param[in,out] data Data to read from, always moved to currently handled character.
3981 * @param[in,out] deviates Deviates to add to.
3982 *
3983 * @return LY_ERR values.
3984 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003985static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003986parse_deviate(struct lys_parser_ctx *ctx, const char **data, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003987{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003988 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003989 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003990 size_t word_len, dev_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003991 enum yang_keyword kw;
3992 struct lysp_deviate *iter, *d;
3993 struct lysp_deviate_add *d_add = NULL;
3994 struct lysp_deviate_rpl *d_rpl = NULL;
3995 struct lysp_deviate_del *d_del = NULL;
3996 const char **d_units, ***d_uniques, ***d_dflts;
3997 struct lysp_restr **d_musts;
3998 uint16_t *d_flags;
3999 uint32_t *d_min, *d_max;
4000
4001 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02004002 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004003
4004 if ((word_len == 13) && !strncmp(word, "not-supported", word_len)) {
4005 dev_mod = LYS_DEV_NOT_SUPPORTED;
4006 } else if ((word_len == 3) && !strncmp(word, "add", word_len)) {
4007 dev_mod = LYS_DEV_ADD;
4008 } else if ((word_len == 7) && !strncmp(word, "replace", word_len)) {
4009 dev_mod = LYS_DEV_REPLACE;
4010 } else if ((word_len == 6) && !strncmp(word, "delete", word_len)) {
4011 dev_mod = LYS_DEV_DELETE;
4012 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004013 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004014 free(buf);
4015 return LY_EVALID;
4016 }
4017 free(buf);
4018
4019 /* create structure */
4020 switch (dev_mod) {
4021 case LYS_DEV_NOT_SUPPORTED:
4022 d = calloc(1, sizeof *d);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004023 LY_CHECK_ERR_RET(!d, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004024 break;
4025 case LYS_DEV_ADD:
4026 d_add = calloc(1, sizeof *d_add);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004027 LY_CHECK_ERR_RET(!d_add, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004028 d = (struct lysp_deviate *)d_add;
4029 d_units = &d_add->units;
4030 d_uniques = &d_add->uniques;
4031 d_dflts = &d_add->dflts;
4032 d_musts = &d_add->musts;
4033 d_flags = &d_add->flags;
4034 d_min = &d_add->min;
4035 d_max = &d_add->max;
4036 break;
4037 case LYS_DEV_REPLACE:
4038 d_rpl = calloc(1, sizeof *d_rpl);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004039 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004040 d = (struct lysp_deviate *)d_rpl;
4041 d_units = &d_rpl->units;
4042 d_flags = &d_rpl->flags;
4043 d_min = &d_rpl->min;
4044 d_max = &d_rpl->max;
4045 break;
4046 case LYS_DEV_DELETE:
4047 d_del = calloc(1, sizeof *d_del);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004048 LY_CHECK_ERR_RET(!d_del, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004049 d = (struct lysp_deviate *)d_del;
4050 d_units = &d_del->units;
4051 d_uniques = &d_del->uniques;
4052 d_dflts = &d_del->dflts;
4053 d_musts = &d_del->musts;
4054 d_flags = &d_del->flags;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004055 break;
4056 default:
4057 assert(0);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004058 LOGINT_RET(ctx->ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004059 }
4060 d->mod = dev_mod;
4061
4062 /* insert into siblings */
4063 if (!*deviates) {
4064 *deviates = d;
4065 } else {
4066 for (iter = *deviates; iter->next; iter = iter->next);
4067 iter->next = d;
4068 }
4069
Radek Krejci6d6556c2018-11-08 09:37:45 +01004070 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004071 switch (kw) {
4072 case YANG_CONFIG:
4073 switch (dev_mod) {
4074 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004075 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004076 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004077 return LY_EVALID;
4078 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004079 LY_CHECK_RET(parse_config(ctx, data, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004080 break;
4081 }
4082 break;
4083 case YANG_DEFAULT:
4084 switch (dev_mod) {
4085 case LYS_DEV_NOT_SUPPORTED:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004086 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004087 return LY_EVALID;
4088 case LYS_DEV_REPLACE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004089 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 +02004090 break;
4091 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004092 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 +02004093 break;
4094 }
4095 break;
4096 case YANG_MANDATORY:
4097 switch (dev_mod) {
4098 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004099 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004100 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004101 return LY_EVALID;
4102 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004103 LY_CHECK_RET(parse_mandatory(ctx, data, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004104 break;
4105 }
4106 break;
4107 case YANG_MAX_ELEMENTS:
4108 switch (dev_mod) {
4109 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004110 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004111 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004112 return LY_EVALID;
4113 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004114 LY_CHECK_RET(parse_maxelements(ctx, data, d_max, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004115 break;
4116 }
4117 break;
4118 case YANG_MIN_ELEMENTS:
4119 switch (dev_mod) {
4120 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004121 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004122 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004123 return LY_EVALID;
4124 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004125 LY_CHECK_RET(parse_minelements(ctx, data, d_min, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004126 break;
4127 }
4128 break;
4129 case YANG_MUST:
4130 switch (dev_mod) {
4131 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004132 case LYS_DEV_REPLACE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004133 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004134 return LY_EVALID;
4135 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004136 LY_CHECK_RET(parse_restrs(ctx, data, kw, d_musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004137 break;
4138 }
4139 break;
4140 case YANG_TYPE:
4141 switch (dev_mod) {
4142 case LYS_DEV_NOT_SUPPORTED:
4143 case LYS_DEV_ADD:
4144 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004145 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004146 return LY_EVALID;
4147 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004148 if (d_rpl->type) {
4149 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
4150 return LY_EVALID;
4151 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004152 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004153 LY_CHECK_ERR_RET(!d_rpl->type, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004154 LY_CHECK_RET(parse_type(ctx, data, d_rpl->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004155 break;
4156 }
4157 break;
4158 case YANG_UNIQUE:
4159 switch (dev_mod) {
4160 case LYS_DEV_NOT_SUPPORTED:
4161 case LYS_DEV_REPLACE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004162 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004163 return LY_EVALID;
4164 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004165 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 +02004166 break;
4167 }
4168 break;
4169 case YANG_UNITS:
4170 switch (dev_mod) {
4171 case LYS_DEV_NOT_SUPPORTED:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004172 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004173 return LY_EVALID;
4174 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004175 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 +02004176 break;
4177 }
4178 break;
4179 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004180 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004181 break;
4182 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004183 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004184 return LY_EVALID;
4185 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004186 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004187 return ret;
4188}
4189
Michal Vaskoea5abea2018-09-18 13:10:54 +02004190/**
4191 * @brief Parse the deviation statement.
4192 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004193 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004194 * @param[in,out] data Data to read from, always moved to currently handled character.
4195 * @param[in,out] deviations Deviations to add to.
4196 *
4197 * @return LY_ERR values.
4198 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004199static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02004200parse_deviation(struct lys_parser_ctx *ctx, const char **data, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004201{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004202 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004203 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004204 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004205 enum yang_keyword kw;
4206 struct lysp_deviation *dev;
4207
Radek Krejci2c4e7172018-10-19 15:56:26 +02004208 LY_ARRAY_NEW_RET(ctx->ctx, *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004209
4210 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02004211 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02004212 INSERT_WORD(ctx, buf, dev->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004213
Radek Krejci6d6556c2018-11-08 09:37:45 +01004214 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004215 switch (kw) {
4216 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004217 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 +02004218 break;
4219 case YANG_DEVIATE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004220 LY_CHECK_RET(parse_deviate(ctx, data, &dev->deviates));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004221 break;
4222 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004223 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 +02004224 break;
4225 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004226 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004227 break;
4228 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004229 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004230 return LY_EVALID;
4231 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004232 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004233 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01004234checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004235 /* mandatory substatements */
4236 if (!dev->deviates) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004237 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004238 return LY_EVALID;
4239 }
4240
4241 return ret;
4242}
4243
Michal Vaskoea5abea2018-09-18 13:10:54 +02004244/**
4245 * @brief Parse the feature statement.
4246 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004247 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004248 * @param[in,out] data Data to read from, always moved to currently handled character.
4249 * @param[in,out] features Features to add to.
4250 *
4251 * @return LY_ERR values.
4252 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004253static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02004254parse_feature(struct lys_parser_ctx *ctx, const char **data, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004255{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004256 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004257 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004258 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004259 enum yang_keyword kw;
4260 struct lysp_feature *feat;
4261
Radek Krejci2c4e7172018-10-19 15:56:26 +02004262 LY_ARRAY_NEW_RET(ctx->ctx, *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004263
4264 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02004265 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02004266 INSERT_WORD(ctx, buf, feat->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004267
Radek Krejci6d6556c2018-11-08 09:37:45 +01004268 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004269 switch (kw) {
4270 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004271 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 +02004272 break;
4273 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004274 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 +02004275 break;
4276 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004277 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 +02004278 break;
4279 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004280 LY_CHECK_RET(parse_status(ctx, data, &feat->flags, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004281 break;
4282 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004283 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004284 break;
4285 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004286 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004287 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004288 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004289 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004290 return ret;
4291}
4292
Michal Vaskoea5abea2018-09-18 13:10:54 +02004293/**
4294 * @brief Parse the identity statement.
4295 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004296 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004297 * @param[in,out] data Data to read from, always moved to currently handled character.
4298 * @param[in,out] identities Identities to add to.
4299 *
4300 * @return LY_ERR values.
4301 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004302static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02004303parse_identity(struct lys_parser_ctx *ctx, const char **data, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004304{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004305 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004306 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004307 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004308 enum yang_keyword kw;
4309 struct lysp_ident *ident;
4310
Radek Krejci2c4e7172018-10-19 15:56:26 +02004311 LY_ARRAY_NEW_RET(ctx->ctx, *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004312
4313 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02004314 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02004315 INSERT_WORD(ctx, buf, ident->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004316
Radek Krejci6d6556c2018-11-08 09:37:45 +01004317 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004318 switch (kw) {
4319 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004320 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 +02004321 break;
4322 case YANG_IF_FEATURE:
Radek Krejciceaf2122019-01-02 15:03:26 +01004323 YANG_CHECK_STMTVER2_RET(ctx, "if-feature", "identity");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004324 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 +02004325 break;
4326 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004327 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 +02004328 break;
4329 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004330 LY_CHECK_RET(parse_status(ctx, data, &ident->flags, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004331 break;
4332 case YANG_BASE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004333 if (ident->bases && ctx->mod_version < 2) {
Radek Krejci10113652018-11-14 16:56:50 +01004334 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Identity can be derived from multiple base identities only in YANG 1.1 modules");
4335 return LY_EVALID;
4336 }
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004337 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 +02004338 break;
4339 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004340 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004341 break;
4342 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004343 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004344 return LY_EVALID;
4345 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004346 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004347 return ret;
4348}
4349
Michal Vaskoea5abea2018-09-18 13:10:54 +02004350/**
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004351 * @brief Finalize some of the (sub)module structure after parsing.
4352 *
4353 * Update parent pointers in the nodes inside grouping/RPC/Notification, which could be reallocated.
4354 *
4355 * @param[in] mod Parsed module to be updated.
4356 * @return LY_ERR value (currently only LY_SUCCESS, but it can change in future).
4357 */
4358static LY_ERR
4359parse_sub_module_finalize(struct lysp_module *mod)
4360{
4361 unsigned int u;
4362 struct lysp_node *child;
4363
4364 /* finalize parent pointers to the reallocated items */
4365 LY_ARRAY_FOR(mod->groupings, u) {
4366 LY_LIST_FOR(mod->groupings[u].data, child) {
4367 child->parent = (struct lysp_node*)&mod->groupings[u];
4368 }
4369 }
4370
4371 /* TODO the same finalization for rpcs and notifications, do also in the relevant nodes */
4372
4373 return LY_SUCCESS;
4374}
4375
4376/**
4377 * @brief Parse module substatements.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004378 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004379 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004380 * @param[in,out] data Data to read from, always moved to currently handled character.
4381 * @param[in,out] mod Module to write to.
4382 *
4383 * @return LY_ERR values.
4384 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004385static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02004386parse_module(struct lys_parser_ctx *ctx, const char **data, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004387{
4388 LY_ERR ret = 0;
4389 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004390 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004391 enum yang_keyword kw, prev_kw = 0;
4392 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004393 struct lysp_submodule *dup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004394
4395 /* (sub)module name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02004396 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004397 INSERT_WORD(ctx, buf, mod->mod->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004398
Radek Krejci6d6556c2018-11-08 09:37:45 +01004399 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004400
Radek Krejcie3846472018-10-15 15:24:51 +02004401#define CHECK_ORDER(SECTION) \
4402 if (mod_stmt > SECTION) {LOGVAL_YANG(ctx, LY_VCODE_INORD, ly_stmt2str(kw), ly_stmt2str(prev_kw)); return LY_EVALID;}mod_stmt = SECTION
4403
Michal Vasko7fbc8162018-09-17 10:35:16 +02004404 switch (kw) {
4405 /* module header */
4406 case YANG_NAMESPACE:
4407 case YANG_PREFIX:
Radek Krejcie3846472018-10-15 15:24:51 +02004408 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4409 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004410 case YANG_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004411 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004412 break;
4413 /* linkage */
4414 case YANG_INCLUDE:
4415 case YANG_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004416 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004417 break;
4418 /* meta */
4419 case YANG_ORGANIZATION:
4420 case YANG_CONTACT:
4421 case YANG_DESCRIPTION:
4422 case YANG_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004423 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004424 break;
4425
4426 /* revision */
4427 case YANG_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004428 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004429 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004430 /* body */
4431 case YANG_ANYDATA:
4432 case YANG_ANYXML:
4433 case YANG_AUGMENT:
4434 case YANG_CHOICE:
4435 case YANG_CONTAINER:
4436 case YANG_DEVIATION:
4437 case YANG_EXTENSION:
4438 case YANG_FEATURE:
4439 case YANG_GROUPING:
4440 case YANG_IDENTITY:
4441 case YANG_LEAF:
4442 case YANG_LEAF_LIST:
4443 case YANG_LIST:
4444 case YANG_NOTIFICATION:
4445 case YANG_RPC:
4446 case YANG_TYPEDEF:
4447 case YANG_USES:
4448 case YANG_CUSTOM:
4449 mod_stmt = Y_MOD_BODY;
4450 break;
4451 default:
4452 /* error handled in the next switch */
4453 break;
4454 }
Radek Krejcie3846472018-10-15 15:24:51 +02004455#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004456
Radek Krejcie3846472018-10-15 15:24:51 +02004457 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004458 switch (kw) {
4459 /* module header */
4460 case YANG_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004461 LY_CHECK_RET(parse_yangversion(ctx, data, &mod->mod->version, &mod->exts));
4462 ctx->mod_version = mod->mod->version;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004463 break;
4464 case YANG_NAMESPACE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004465 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 +02004466 break;
4467 case YANG_PREFIX:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004468 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 +02004469 break;
4470
4471 /* linkage */
4472 case YANG_INCLUDE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004473 LY_CHECK_RET(parse_include(ctx, mod->mod->name, data, &mod->includes));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004474 break;
4475 case YANG_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004476 LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, data, &mod->imports));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004477 break;
4478
4479 /* meta */
4480 case YANG_ORGANIZATION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004481 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 +02004482 break;
4483 case YANG_CONTACT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004484 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 +02004485 break;
4486 case YANG_DESCRIPTION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004487 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 +02004488 break;
4489 case YANG_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004490 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 +02004491 break;
4492
4493 /* revision */
4494 case YANG_REVISION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004495 LY_CHECK_RET(parse_revision(ctx, data, &mod->revs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004496 break;
4497
4498 /* body */
4499 case YANG_ANYDATA:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004500 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "module");
Radek Krejci10113652018-11-14 16:56:50 +01004501 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004502 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004503 LY_CHECK_RET(parse_any(ctx, data, kw, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004504 break;
4505 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004506 LY_CHECK_RET(parse_choice(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004507 break;
4508 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004509 LY_CHECK_RET(parse_container(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004510 break;
4511 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004512 LY_CHECK_RET(parse_leaf(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004513 break;
4514 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004515 LY_CHECK_RET(parse_leaflist(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004516 break;
4517 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004518 LY_CHECK_RET(parse_list(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004519 break;
4520 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004521 LY_CHECK_RET(parse_uses(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004522 break;
4523
4524 case YANG_AUGMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004525 LY_CHECK_RET(parse_augment(ctx, data, NULL, &mod->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004526 break;
4527 case YANG_DEVIATION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004528 LY_CHECK_RET(parse_deviation(ctx, data, &mod->deviations));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004529 break;
4530 case YANG_EXTENSION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004531 LY_CHECK_RET(parse_extension(ctx, data, &mod->extensions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004532 break;
4533 case YANG_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004534 LY_CHECK_RET(parse_feature(ctx, data, &mod->features));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004535 break;
4536 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004537 LY_CHECK_RET(parse_grouping(ctx, data, NULL, &mod->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004538 break;
4539 case YANG_IDENTITY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004540 LY_CHECK_RET(parse_identity(ctx, data, &mod->identities));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004541 break;
4542 case YANG_NOTIFICATION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004543 LY_CHECK_RET(parse_notif(ctx, data, NULL, &mod->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004544 break;
4545 case YANG_RPC:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004546 LY_CHECK_RET(parse_action(ctx, data, NULL, &mod->rpcs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004547 break;
4548 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01004549 LY_CHECK_RET(parse_typedef(ctx, NULL, data, &mod->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004550 break;
4551 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004552 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004553 break;
4554
4555 default:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004556 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004557 return LY_EVALID;
4558 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004559 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004560 LY_CHECK_RET(ret);
Radek Krejcie86bf772018-12-14 11:39:53 +01004561
Radek Krejci6d6556c2018-11-08 09:37:45 +01004562checks:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004563 /* finalize parent pointers to the reallocated items */
4564 LY_CHECK_RET(parse_sub_module_finalize(mod));
4565
Michal Vasko7fbc8162018-09-17 10:35:16 +02004566 /* mandatory substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004567 if (!mod->mod->ns) {
4568 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "namespace", "module");
4569 return LY_EVALID;
4570 } else if (!mod->mod->prefix) {
4571 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "prefix", "module");
4572 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004573 }
4574
Radek Krejcie9e987e2018-10-31 12:50:27 +01004575 /* submodules share the namespace with the module names, so there must not be
4576 * a submodule of the same name in the context, no need for revision matching */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004577 dup = ly_ctx_get_submodule(ctx->ctx, NULL, mod->mod->name, NULL);
4578 if (dup) {
4579 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Name collision between module and submodule of name \"%s\".", mod->mod->name);
4580 return LY_EVALID;
4581 }
4582
4583 return ret;
4584}
4585
4586/**
4587 * @brief Parse submodule substatements.
4588 *
4589 * @param[in] ctx yang parser context for logging.
4590 * @param[in,out] data Data to read from, always moved to currently handled character.
4591 * @param[out] submod Parsed submodule structure.
4592 *
4593 * @return LY_ERR values.
4594 */
4595static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02004596parse_submodule(struct lys_parser_ctx *ctx, const char **data, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004597{
4598 LY_ERR ret = 0;
4599 char *buf, *word;
4600 size_t word_len;
4601 enum yang_keyword kw, prev_kw = 0;
4602 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
4603 struct lysp_submodule *dup;
4604
4605 /* submodule name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02004606 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004607 INSERT_WORD(ctx, buf, submod->name, word, word_len);
4608
4609 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
4610
4611#define CHECK_ORDER(SECTION) \
4612 if (mod_stmt > SECTION) {LOGVAL_YANG(ctx, LY_VCODE_INORD, ly_stmt2str(kw), ly_stmt2str(prev_kw)); return LY_EVALID;}mod_stmt = SECTION
4613
4614 switch (kw) {
4615 /* module header */
4616 case YANG_BELONGS_TO:
4617 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4618 break;
4619 case YANG_YANG_VERSION:
4620 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4621 break;
4622 /* linkage */
4623 case YANG_INCLUDE:
4624 case YANG_IMPORT:
4625 CHECK_ORDER(Y_MOD_LINKAGE);
4626 break;
4627 /* meta */
4628 case YANG_ORGANIZATION:
4629 case YANG_CONTACT:
4630 case YANG_DESCRIPTION:
4631 case YANG_REFERENCE:
4632 CHECK_ORDER(Y_MOD_META);
4633 break;
4634
4635 /* revision */
4636 case YANG_REVISION:
4637 CHECK_ORDER(Y_MOD_REVISION);
4638 break;
4639 /* body */
4640 case YANG_ANYDATA:
4641 case YANG_ANYXML:
4642 case YANG_AUGMENT:
4643 case YANG_CHOICE:
4644 case YANG_CONTAINER:
4645 case YANG_DEVIATION:
4646 case YANG_EXTENSION:
4647 case YANG_FEATURE:
4648 case YANG_GROUPING:
4649 case YANG_IDENTITY:
4650 case YANG_LEAF:
4651 case YANG_LEAF_LIST:
4652 case YANG_LIST:
4653 case YANG_NOTIFICATION:
4654 case YANG_RPC:
4655 case YANG_TYPEDEF:
4656 case YANG_USES:
4657 case YANG_CUSTOM:
4658 mod_stmt = Y_MOD_BODY;
4659 break;
4660 default:
4661 /* error handled in the next switch */
4662 break;
4663 }
4664#undef CHECK_ORDER
4665
4666 prev_kw = kw;
4667 switch (kw) {
4668 /* module header */
4669 case YANG_YANG_VERSION:
4670 LY_CHECK_RET(parse_yangversion(ctx, data, &submod->version, &submod->exts));
4671 ctx->mod_version = submod->version;
4672 break;
4673 case YANG_BELONGS_TO:
4674 LY_CHECK_RET(parse_belongsto(ctx, data, &submod->belongsto, &submod->prefix, &submod->exts));
4675 break;
4676
4677 /* linkage */
4678 case YANG_INCLUDE:
4679 LY_CHECK_RET(parse_include(ctx, submod->name, data, &submod->includes));
4680 break;
4681 case YANG_IMPORT:
4682 LY_CHECK_RET(parse_import(ctx, submod->prefix, data, &submod->imports));
4683 break;
4684
4685 /* meta */
4686 case YANG_ORGANIZATION:
4687 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_ORGANIZATION, 0, &submod->org, Y_STR_ARG, &submod->exts));
4688 break;
4689 case YANG_CONTACT:
4690 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_CONTACT, 0, &submod->contact, Y_STR_ARG, &submod->exts));
4691 break;
4692 case YANG_DESCRIPTION:
4693 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &submod->dsc, Y_STR_ARG, &submod->exts));
4694 break;
4695 case YANG_REFERENCE:
4696 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &submod->ref, Y_STR_ARG, &submod->exts));
4697 break;
4698
4699 /* revision */
4700 case YANG_REVISION:
4701 LY_CHECK_RET(parse_revision(ctx, data, &submod->revs));
4702 break;
4703
4704 /* body */
4705 case YANG_ANYDATA:
4706 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "submodule");
4707 /* fall through */
4708 case YANG_ANYXML:
4709 LY_CHECK_RET(parse_any(ctx, data, kw, NULL, &submod->data));
4710 break;
4711 case YANG_CHOICE:
4712 LY_CHECK_RET(parse_choice(ctx, data, NULL, &submod->data));
4713 break;
4714 case YANG_CONTAINER:
4715 LY_CHECK_RET(parse_container(ctx, data, NULL, &submod->data));
4716 break;
4717 case YANG_LEAF:
4718 LY_CHECK_RET(parse_leaf(ctx, data, NULL, &submod->data));
4719 break;
4720 case YANG_LEAF_LIST:
4721 LY_CHECK_RET(parse_leaflist(ctx, data, NULL, &submod->data));
4722 break;
4723 case YANG_LIST:
4724 LY_CHECK_RET(parse_list(ctx, data, NULL, &submod->data));
4725 break;
4726 case YANG_USES:
4727 LY_CHECK_RET(parse_uses(ctx, data, NULL, &submod->data));
4728 break;
4729
4730 case YANG_AUGMENT:
4731 LY_CHECK_RET(parse_augment(ctx, data, NULL, &submod->augments));
4732 break;
4733 case YANG_DEVIATION:
4734 LY_CHECK_RET(parse_deviation(ctx, data, &submod->deviations));
4735 break;
4736 case YANG_EXTENSION:
4737 LY_CHECK_RET(parse_extension(ctx, data, &submod->extensions));
4738 break;
4739 case YANG_FEATURE:
4740 LY_CHECK_RET(parse_feature(ctx, data, &submod->features));
4741 break;
4742 case YANG_GROUPING:
4743 LY_CHECK_RET(parse_grouping(ctx, data, NULL, &submod->groupings));
4744 break;
4745 case YANG_IDENTITY:
4746 LY_CHECK_RET(parse_identity(ctx, data, &submod->identities));
4747 break;
4748 case YANG_NOTIFICATION:
4749 LY_CHECK_RET(parse_notif(ctx, data, NULL, &submod->notifs));
4750 break;
4751 case YANG_RPC:
4752 LY_CHECK_RET(parse_action(ctx, data, NULL, &submod->rpcs));
4753 break;
4754 case YANG_TYPEDEF:
4755 LY_CHECK_RET(parse_typedef(ctx, NULL, data, &submod->typedefs));
4756 break;
4757 case YANG_CUSTOM:
4758 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &submod->exts));
4759 break;
4760
4761 default:
4762 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule");
4763 return LY_EVALID;
4764 }
4765 }
4766 LY_CHECK_RET(ret);
4767
4768checks:
4769 /* finalize parent pointers to the reallocated items */
4770 LY_CHECK_RET(parse_sub_module_finalize((struct lysp_module*)submod));
4771
4772 /* mandatory substatements */
4773 if (!submod->belongsto) {
4774 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
4775 return LY_EVALID;
4776 }
4777
4778 /* submodules share the namespace with the module names, so there must not be
4779 * a submodule of the same name in the context, no need for revision matching */
4780 dup = ly_ctx_get_submodule(ctx->ctx, NULL, submod->name, NULL);
4781 if (dup && strcmp(dup->belongsto, submod->belongsto)) {
4782 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Name collision between submodules of name \"%s\".", dup->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +01004783 return LY_EVALID;
4784 }
4785
Michal Vasko7fbc8162018-09-17 10:35:16 +02004786 return ret;
4787}
4788
Radek Krejcid4557c62018-09-17 11:42:09 +02004789LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02004790yang_parse_submodule(struct lys_parser_ctx *context, const char *data, struct lysp_submodule **submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004791{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004792 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004793 char *word, *buf;
Radek Krejciefd22f62018-09-27 11:47:58 +02004794 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004795 enum yang_keyword kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004796 struct lysp_submodule *mod_p = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004797
4798 /* "module"/"submodule" */
Radek Krejcibbe09a92018-11-08 09:36:54 +01004799 ret = get_keyword(context, &data, &kw, &word, &word_len);
4800 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004801
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004802 if (kw == YANG_MODULE) {
4803 LOGERR(context->ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
4804 ret = LY_EINVAL;
4805 goto cleanup;
4806 } else if (kw != YANG_SUBMODULE) {
Radek Krejcibbe09a92018-11-08 09:36:54 +01004807 LOGVAL_YANG(context, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".",
Radek Krejcic59bc972018-09-17 16:13:06 +02004808 ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004809 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004810 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004811 }
4812
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004813 mod_p = calloc(1, sizeof *mod_p);
4814 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(context->ctx), cleanup);
4815 mod_p->parsing = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004816
4817 /* substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004818 ret = parse_submodule(context, &data, mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004819 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004820
4821 /* read some trailing spaces or new lines */
Radek Krejcid3ca0632019-04-16 16:54:54 +02004822 ret = get_argument(context, &data, Y_MAYBE_STR_ARG, NULL, &word, &buf, &word_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004823 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004824
4825 if (word) {
Radek Krejcibbe09a92018-11-08 09:36:54 +01004826 LOGVAL_YANG(context, LYVE_SYNTAX, "Invalid character sequence \"%.*s\", expected end-of-file.",
Michal Vasko7fbc8162018-09-17 10:35:16 +02004827 word_len, word);
4828 free(buf);
Radek Krejci40544fa2019-01-11 09:38:37 +01004829 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004830 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004831 }
4832 assert(!buf);
4833
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004834 mod_p->parsing = 0;
4835 *submod = mod_p;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004836
Radek Krejcibbe09a92018-11-08 09:36:54 +01004837cleanup:
4838 if (ret) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004839 lysp_submodule_free(context->ctx, mod_p);
4840 }
4841
4842 return ret;
4843}
4844
4845LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02004846yang_parse_module(struct lys_parser_ctx *context, const char *data, struct lys_module *mod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004847{
4848 LY_ERR ret = LY_SUCCESS;
4849 char *word, *buf;
4850 size_t word_len;
4851 enum yang_keyword kw;
4852 struct lysp_module *mod_p = NULL;
4853
4854 /* "module"/"submodule" */
4855 ret = get_keyword(context, &data, &kw, &word, &word_len);
4856 LY_CHECK_GOTO(ret, cleanup);
4857
4858 if (kw == YANG_SUBMODULE) {
4859 LOGERR(context->ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module.");
4860 ret = LY_EINVAL;
4861 goto cleanup;
4862 } else if (kw != YANG_MODULE) {
4863 LOGVAL_YANG(context, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".",
4864 ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004865 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004866 goto cleanup;
4867 }
4868
4869 mod_p = calloc(1, sizeof *mod_p);
4870 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(context->ctx), cleanup);
4871 mod_p->mod = mod;
4872 mod_p->parsing = 1;
4873
4874 /* substatements */
4875 ret = parse_module(context, &data, mod_p);
4876 LY_CHECK_GOTO(ret, cleanup);
4877
4878 /* read some trailing spaces or new lines */
Radek Krejcid3ca0632019-04-16 16:54:54 +02004879 ret = get_argument(context, &data, Y_MAYBE_STR_ARG, NULL, &word, &buf, &word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004880 LY_CHECK_GOTO(ret, cleanup);
4881
4882 if (word) {
4883 LOGVAL_YANG(context, LYVE_SYNTAX, "Invalid character sequence \"%.*s\", expected end-of-file.",
4884 word_len, word);
4885 free(buf);
Radek Krejci40544fa2019-01-11 09:38:37 +01004886 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004887 goto cleanup;
4888 }
4889 assert(!buf);
4890
4891 mod_p->parsing = 0;
4892 mod->parsed = mod_p;
4893
4894cleanup:
4895 if (ret) {
4896 lysp_module_free(mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004897 }
4898
Michal Vasko7fbc8162018-09-17 10:35:16 +02004899 return ret;
4900}