blob: 1a6c80a37ab73a3e2a8c26a2dca45d5824ba070e [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 */
14#include <sys/types.h>
15#include <sys/stat.h>
16#include <unistd.h>
17#include <fcntl.h>
18#include <stdio.h>
19#include <stdlib.h>
20#include <stdint.h>
21#include <errno.h>
22#include <ctype.h>
23#include <string.h>
24#include <dirent.h>
25#include <assert.h>
26
27#include "common.h"
28#include "context.h"
29#include "libyang.h"
Radek Krejci70853c52018-10-15 14:46:16 +020030#include "tree_schema_internal.h"
Radek Krejci44ceedc2018-10-02 15:54:31 +020031
32/* Macro to check YANG's yang-char grammar rule */
33#define is_yangutf8char(c) ((c >= 0x20 && c <= 0xd77) || c == 0x09 || c == 0x0a || c == 0x0d || \
34 (c >= 0xe000 && c <= 0xfdcf) || (c >= 0xfdf0 && c <= 0xfffd) || \
35 (c >= 0x10000 && c <= 0x1fffd) || (c >= 0x20000 && c <= 0x2fffd) || \
36 (c >= 0x30000 && c <= 0x3fffd) || (c >= 0x40000 && c <= 0x2fffd) || \
37 (c >= 0x50000 && c <= 0x5fffd) || (c >= 0x60000 && c <= 0x6fffd) || \
38 (c >= 0x70000 && c <= 0x7fffd) || (c >= 0x80000 && c <= 0x8fffd) || \
39 (c >= 0x90000 && c <= 0x9fffd) || (c >= 0xa0000 && c <= 0xafffd) || \
40 (c >= 0xb0000 && c <= 0xbfffd) || (c >= 0xc0000 && c <= 0xcfffd) || \
41 (c >= 0xd0000 && c <= 0xdfffd) || (c >= 0xe0000 && c <= 0xefffd) || \
42 (c >= 0xf0000 && c <= 0xffffd) || (c >= 0x100000 && c <= 0x10fffd))
43
Radek Krejciceaf2122019-01-02 15:03:26 +010044/**
45 * @brief Try to find object with MEMBER string matching the IDENT in the given ARRAY.
46 * Macro logs an error message and returns LY_EVALID in case of existence of a matching object.
47 *
48 * @param[in] CTX yang parser context for logging.
49 * @param[in] ARRAY [sized array](@ref sizedarrays) of a generic objects with member named MEMBER to search.
50 * @param[in] MEMBER Name of the member of the objects in the ARRAY to compare.
51 * @param[in] STMT Name of the compared YANG statements for logging.
52 * @param[in] IDENT String trying to find in the ARRAY's objects inside the MEMBER member.
53 */
Radek Krejcifaa1eac2018-10-30 14:34:55 +010054#define CHECK_UNIQUENESS(CTX, ARRAY, MEMBER, STMT, IDENT) \
55 if (ARRAY) { \
56 for (unsigned int u = 0; u < LY_ARRAY_SIZE(ARRAY) - 1; ++u) { \
57 if (!strcmp((ARRAY)[u].MEMBER, IDENT)) { \
58 LOGVAL_YANG(CTX, LY_VCODE_DUPIDENT, IDENT, STMT); \
59 return LY_EVALID; \
60 } \
61 } \
62 }
63
Radek Krejciceaf2122019-01-02 15:03:26 +010064/**
65 * @brief Insert WORD into the libyang context's dictionary and store as TARGET.
66 * @param[in] CTX yang parser context to access libyang context.
67 * @param[in] BUF buffer in case the word is not a constant and can be inserted directly (zero-copy)
68 * @param[out] TARGET variable where to store the pointer to the inserted value.
69 * @param[in] WORD string to store.
70 * @param[in] LEN length of the string in WORD to store.
71 */
Radek Krejci9fcacc12018-10-11 15:59:11 +020072#define INSERT_WORD(CTX, BUF, TARGET, WORD, LEN) \
73 if (BUF) {(TARGET) = lydict_insert_zc((CTX)->ctx, WORD);}\
74 else {(TARGET) = lydict_insert((CTX)->ctx, WORD, LEN);}
Radek Krejci44ceedc2018-10-02 15:54:31 +020075
Radek Krejciceaf2122019-01-02 15:03:26 +010076/**
77 * @brief Move the DATA pointer by COUNT items. Also updates the indent value in yang parser context
78 * @param[in] CTX yang parser context to update its indent value.
79 * @param[in,out] DATA pointer to move
80 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
81 */
Radek Krejci2b610482019-01-02 13:36:09 +010082#define MOVE_INPUT(CTX, DATA, COUNT) (*(DATA))+=COUNT;(CTX)->indent+=COUNT
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020083
Michal Vaskoea5abea2018-09-18 13:10:54 +020084/**
85 * @brief Loop through all substatements providing, return if there are none.
86 *
Radek Krejci44ceedc2018-10-02 15:54:31 +020087 * @param[in] CTX yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +020088 * @param[in] DATA Raw data to read from.
89 * @param[out] KW YANG keyword read.
90 * @param[out] WORD Pointer to the keyword itself.
91 * @param[out] WORD_LEN Length of the keyword.
92 * @param[out] ERR Variable for error storing.
93 *
94 * @return In case there are no substatements or a fatal error encountered.
95 */
Radek Krejci6d6556c2018-11-08 09:37:45 +010096#define YANG_READ_SUBSTMT_FOR(CTX, DATA, KW, WORD, WORD_LEN, ERR, CHECKGOTO) \
Radek Krejci6d9b9b52018-11-02 12:43:39 +010097 LY_CHECK_RET(get_keyword(CTX, DATA, &KW, &WORD, &WORD_LEN)); \
Michal Vasko7fbc8162018-09-17 10:35:16 +020098 if (KW == YANG_SEMICOLON) { \
Radek Krejci6d6556c2018-11-08 09:37:45 +010099 CHECKGOTO; \
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100100 return LY_SUCCESS; \
Michal Vasko7fbc8162018-09-17 10:35:16 +0200101 } \
102 if (KW != YANG_LEFT_BRACE) { \
Radek Krejci44ceedc2018-10-02 15:54:31 +0200103 LOGVAL_YANG(CTX, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\", expected \";\" or \"{\".", ly_stmt2str(KW)); \
Michal Vasko7fbc8162018-09-17 10:35:16 +0200104 return LY_EVALID; \
105 } \
106 for (ERR = get_keyword(CTX, DATA, &KW, &WORD, &WORD_LEN); \
107 !ERR && (KW != YANG_RIGHT_BRACE); \
108 ERR = get_keyword(CTX, DATA, &KW, &WORD, &WORD_LEN))
109
Radek Krejciceaf2122019-01-02 15:03:26 +0100110/**
111 * @brief Check module version is at least 2 (YANG 1.1) because of the keyword presence.
112 * Logs error message and returns LY_EVALID in case of module in YANG version 1.0.
113 * @param[in] CTX yang parser context to get current module and for logging.
114 * @param[in] KW keyword allowed only in YANG version 1.1 (or later) - for logging.
115 * @param[in] PARENT parent statement where the KW is present - for logging.
116 */
117#define YANG_CHECK_STMTVER2_RET(CTX, KW, PARENT) \
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100118 if ((CTX)->mod_version < 2) {LOGVAL_YANG((CTX), LY_VCODE_INCHILDSTMT2, KW, PARENT); return LY_EVALID;}
Radek Krejci10113652018-11-14 16:56:50 +0100119
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100120static LY_ERR parse_container(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
121static LY_ERR parse_uses(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
122static LY_ERR parse_choice(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
123static LY_ERR parse_case(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
124static LY_ERR parse_list(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
125static LY_ERR parse_grouping(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_grp **groupings);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200126
Michal Vaskoea5abea2018-09-18 13:10:54 +0200127/**
128 * @brief Add another character to dynamic buffer, a low-level function.
129 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200130 * Enlarge if needed. Updates \p input as well as \p buf_used.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200131 *
Radek Krejci404251e2018-10-09 12:06:44 +0200132 * @param[in] ctx libyang context for logging.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200133 * @param[in, out] input Input string to process.
134 * @param[in] len Number of bytes to get from the input string and copy into the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200135 * @param[in,out] buf Buffer to use, can be moved by realloc().
136 * @param[in,out] buf_len Current size of the buffer.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200137 * @param[in,out] buf_used Currently used characters of the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200138 *
139 * @return LY_ERR values.
140 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200141static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +0200142buf_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 +0200143{
Radek Krejci44ceedc2018-10-02 15:54:31 +0200144 if (*buf_len <= (*buf_used) + len) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200145 *buf_len += 16;
146 *buf = ly_realloc(*buf, *buf_len);
147 LY_CHECK_ERR_RET(!*buf, LOGMEM(ctx), LY_EMEM);
148 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200149 memcpy(&(*buf)[*buf_used], *input, len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200150
Radek Krejci44ceedc2018-10-02 15:54:31 +0200151 (*buf_used) += len;
152 (*input) += len;
153
Michal Vasko7fbc8162018-09-17 10:35:16 +0200154 return LY_SUCCESS;
155}
156
Michal Vaskoea5abea2018-09-18 13:10:54 +0200157/**
Radek Krejci44ceedc2018-10-02 15:54:31 +0200158 * @brief Check that \p c is valid UTF8 code point for YANG string.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200159 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200160 * @param[in] ctx yang parser context for logging.
161 * @param[in] c UTF8 code point of a character to check.
162 * @return LY_ERR values.
163 */
164static LY_ERR
165check_stringchar(struct ly_parser_ctx *ctx, unsigned int c)
166{
167 if (!is_yangutf8char(c)) {
168 LOGVAL_YANG(ctx, LY_VCODE_INCHAR, c);
169 return LY_EVALID;
170 }
171 return LY_SUCCESS;
172}
173
174/**
175 * @brief Check that \p c is valid UTF8 code point for YANG identifier.
176 *
177 * @param[in] ctx yang parser context for logging.
178 * @param[in] c UTF8 code point of a character to check.
179 * @param[in] first Flag to check the first character of an identifier, which is more restricted.
Radek Krejcidcc7b322018-10-11 14:24:02 +0200180 * @param[in,out] prefix Storage for internally used flag in case of possible prefixed identifiers:
181 * 0 - colon not yet found (no prefix)
182 * 1 - \p c is the colon character
183 * 2 - prefix already processed, now processing the identifier
184 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200185 * If the identifier cannot be prefixed, NULL is expected.
186 * @return LY_ERR values.
187 */
188static LY_ERR
189check_identifierchar(struct ly_parser_ctx *ctx, unsigned int c, int first, int *prefix)
190{
191 if (first || (prefix && (*prefix) == 1)) {
192 if (!is_yangidentstartchar(c)) {
193 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '%c'.", c);
194 return LY_EVALID;
195 }
Radek Krejci9fcacc12018-10-11 15:59:11 +0200196 if (prefix) {
197 if (first) {
198 (*prefix) = 0;
199 } else {
200 (*prefix) = 2;
201 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200202 }
203 } else if (c == ':' && prefix && (*prefix) == 0) {
204 (*prefix) = 1;
205 } else if (!is_yangidentchar(c)) {
206 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Invalid identifier character '%c'.", c);
207 return LY_EVALID;
208 }
209
210 return LY_SUCCESS;
211}
212
213/**
214 * @brief Store a single UTF8 character. It depends whether in a dynamically-allocated buffer or just as a pointer to the data.
215 *
216 * @param[in] ctx yang parser context for logging.
217 * @param[in,out] input Pointer to the string where to get the character to store. Automatically moved to the next character
218 * when function returns.
219 * @param[in] arg Type of the input string to select method of checking character validity.
220 * @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 +0200221 * 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 +0200222 * @param[in,out] word_len Current length of the word pointed to by \p word_p.
223 * @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 +0200224 * @param[in,out] buf_len Current length of \p word_b.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200225 * @param[in] need_buf Flag if the dynamically allocated buffer is required.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200226 *
227 * @return LY_ERR values.
228 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200229static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +0200230buf_store_char(struct ly_parser_ctx *ctx, const char **input, enum yang_arg arg,
231 char **word_p, size_t *word_len, char **word_b, size_t *buf_len, int need_buf)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200232{
Radek Krejci404251e2018-10-09 12:06:44 +0200233 int prefix = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200234 unsigned int c;
235 size_t len;
236
Radek Krejcif29b7c32019-04-09 16:17:49 +0200237 /* check valid combination of input paremeters - if need_buf specified, word_b must be provided */
238 assert(!need_buf || (need_buf && word_b));
239
Radek Krejci44ceedc2018-10-02 15:54:31 +0200240 /* get UTF8 code point (and number of bytes coding the character) */
241 LY_CHECK_ERR_RET(ly_getutf8(input, &c, &len),
242 LOGVAL_YANG(ctx, LY_VCODE_INCHAR, (*input)[-len]), LY_EVALID);
243 (*input) -= len;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200244 if (c == '\n') {
245 ctx->indent = 0;
246 } else {
247 /* note - even the multibyte character is count as 1 */
248 ++ctx->indent;
249 }
250
Radek Krejci44ceedc2018-10-02 15:54:31 +0200251 /* check character validity */
252 switch (arg) {
253 case Y_IDENTIF_ARG:
254 LY_CHECK_RET(check_identifierchar(ctx, c, !(*word_len), NULL));
255 break;
256 case Y_PREF_IDENTIF_ARG:
257 LY_CHECK_RET(check_identifierchar(ctx, c, !(*word_len), &prefix));
258 break;
259 case Y_STR_ARG:
260 case Y_MAYBE_STR_ARG:
261 LY_CHECK_RET(check_stringchar(ctx, c));
262 break;
263 }
264
Michal Vasko7fbc8162018-09-17 10:35:16 +0200265 if (word_b && *word_b) {
266 /* add another character into buffer */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200267 if (buf_add_char(ctx->ctx, input, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200268 return LY_EMEM;
269 }
270
271 /* in case of realloc */
272 *word_p = *word_b;
Radek Krejcif29b7c32019-04-09 16:17:49 +0200273 } else if (word_b && need_buf) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200274 /* first time we need a buffer, copy everything read up to now */
275 if (*word_len) {
276 *word_b = malloc(*word_len);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200277 LY_CHECK_ERR_RET(!*word_b, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200278 *buf_len = *word_len;
279 memcpy(*word_b, *word_p, *word_len);
280 }
281
282 /* add this new character into buffer */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200283 if (buf_add_char(ctx->ctx, input, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200284 return LY_EMEM;
285 }
286
287 /* in case of realloc */
288 *word_p = *word_b;
289 } else {
290 /* just remember the first character pointer */
291 if (!*word_p) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200292 *word_p = (char *)(*input);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200293 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200294 /* ... and update the word's length */
295 (*word_len) += len;
296 (*input) += len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200297 }
298
299 return LY_SUCCESS;
300}
301
Michal Vaskoea5abea2018-09-18 13:10:54 +0200302/**
303 * @brief Skip YANG comment in data.
304 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200305 * @param[in] ctx yang parser context for logging.
306 * @param[in,out] data Data to read from, automatically moved after the comment.
307 * @param[in] comment Type of the comment to process:
308 * 1 for a one-line comment,
309 * 2 for a block comment.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200310 *
311 * @return LY_ERR values.
312 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200313static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +0200314skip_comment(struct ly_parser_ctx *ctx, const char **data, int comment)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200315{
Radek Krejci80dd33e2018-09-26 15:57:18 +0200316 /* internal statuses: 0 - comment ended,
317 * 1 - in line comment,
318 * 2 - in block comment,
319 * 3 - in block comment with last read character '*'
320 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200321 while (**data && comment) {
322 switch (comment) {
323 case 1:
324 if (**data == '\n') {
325 comment = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200326 ++ctx->line;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200327 }
328 break;
329 case 2:
330 if (**data == '*') {
Radek Krejci15c80ca2018-10-09 11:01:31 +0200331 comment = 3;
332 } else if (**data == '\n') {
333 ++ctx->line;
334 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200335 break;
336 case 3:
337 if (**data == '/') {
338 comment = 0;
339 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200340 if (**data == '\n') {
341 ++ctx->line;
342 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200343 comment = 2;
344 }
345 break;
346 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200347 LOGINT_RET(ctx->ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200348 }
349
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200350 if (**data == '\n') {
351 ctx->indent = 0;
352 } else {
353 ++ctx->indent;
354 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200355 ++(*data);
356 }
357
358 if (!**data && (comment > 1)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200359 LOGVAL_YANG(ctx, LYVE_SYNTAX, "Unexpected end-of-file, non-terminated comment.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200360 return LY_EVALID;
361 }
362
363 return LY_SUCCESS;
364}
365
Michal Vaskoea5abea2018-09-18 13:10:54 +0200366/**
367 * @brief Read a quoted string from data.
368 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200369 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200370 * @param[in,out] data Data to read from, always moved to currently handled character.
371 * @param[in] arg Type of YANG keyword argument expected.
372 * @param[out] word_p Pointer to the read quoted string.
373 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read quoted string. If not needed,
374 * set to NULL. Otherwise equal to \p word_p.
375 * @param[out] word_len Length of the read quoted string.
376 * @param[out] buf_len Length of the dynamically-allocated buffer \p word_b.
377 * @param[in] indent Current indent (number of YANG spaces). Needed for correct multi-line string
378 * indenation in the final quoted string.
379 *
380 * @return LY_ERR values.
381 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200382static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +0200383read_qstring(struct ly_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 +0200384 size_t *buf_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200385{
386 /* string: 0 - string ended, 1 - string with ', 2 - string with ", 3 - string with " with last character \,
387 * 4 - string finished, now skipping whitespaces looking for +,
388 * 5 - string continues after +, skipping whitespaces */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200389 unsigned int string, block_indent = 0, current_indent = 0, need_buf = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200390 const char *c;
391
392 if (**data == '\"') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200393 string = 2;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200394 current_indent = block_indent = ctx->indent + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200395 } else {
396 assert(**data == '\'');
397 string = 1;
398 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200399 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200400
401 while (**data && string) {
402 switch (string) {
403 case 1:
404 switch (**data) {
405 case '\'':
406 /* string may be finished, but check for + */
407 string = 4;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200408 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200409 break;
410 default:
411 /* check and store character */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200412 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 +0200413 break;
414 }
415 break;
416 case 2:
417 switch (**data) {
418 case '\"':
419 /* string may be finished, but check for + */
420 string = 4;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200421 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200422 break;
423 case '\\':
424 /* special character following */
425 string = 3;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200426 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200427 break;
428 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200429 if (current_indent < block_indent) {
430 ++current_indent;
431 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200432 } else {
433 /* check and store character */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200434 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 +0200435 }
436 break;
437 case '\t':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200438 if (current_indent < block_indent) {
439 assert(need_buf);
440 current_indent += 8;
441 ctx->indent += 8;
442 for (; current_indent > block_indent; --current_indent, --ctx->indent) {
443 /* store leftover spaces from the tab */
444 c = " ";
445 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 +0200446 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200447 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200448 } else {
449 /* check and store character */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200450 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 +0200451 /* additional characters for indentation - only 1 was count in buf_store_char */
452 ctx->indent += 7;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200453 }
454 break;
455 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200456 if (block_indent) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200457 /* we will be removing the indents so we need our own buffer */
458 need_buf = 1;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200459
460 /* remove trailing tabs and spaces */
461 while ((*word_len) && ((*word_p)[(*word_len) - 1] == '\t' || (*word_p)[(*word_len) - 1] == ' ')) {
462 --(*word_len);
463 }
464
465 /* start indentation */
466 current_indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200467 }
468
469 /* check and store character */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200470 LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, buf_len, need_buf));
471
472 /* maintain line number */
473 ++ctx->line;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200474
475 /* reset context indentation counter for possible string after this one */
476 ctx->indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200477 break;
478 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200479 /* first non-whitespace character, stop eating indentation */
480 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200481
482 /* check and store character */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200483 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 +0200484 break;
485 }
486 break;
487 case 3:
488 /* string encoded characters */
489 switch (**data) {
490 case 'n':
491 c = "\n";
492 break;
493 case 't':
494 c = "\t";
495 break;
496 case '\"':
497 c = *data;
498 break;
499 case '\\':
500 c = *data;
501 break;
502 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200503 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Double-quoted string unknown special character '\\%c'.", **data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200504 return LY_EVALID;
505 }
506
507 /* check and store character */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200508 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 +0200509
510 string = 2;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200511 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200512 break;
513 case 4:
514 switch (**data) {
515 case '+':
516 /* string continues */
517 string = 5;
Radek Krejciefd22f62018-09-27 11:47:58 +0200518 need_buf = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200519 break;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200520 case '\n':
521 ++ctx->line;
522 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200523 case ' ':
524 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200525 /* just skip */
526 break;
527 default:
528 /* string is finished */
529 goto string_end;
530 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200531 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200532 break;
533 case 5:
534 switch (**data) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200535 case '\n':
536 ++ctx->line;
537 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200538 case ' ':
539 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200540 /* skip */
541 break;
542 case '\'':
543 string = 1;
544 break;
545 case '\"':
546 string = 2;
547 break;
548 default:
549 /* it must be quoted again */
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200550 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Both string parts divided by '+' must be quoted.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200551 return LY_EVALID;
552 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200553 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200554 break;
555 default:
556 return LY_EINT;
557 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200558 }
559
560string_end:
561 return LY_SUCCESS;
562}
563
Michal Vaskoea5abea2018-09-18 13:10:54 +0200564/**
565 * @brief Get another YANG string from the raw data.
566 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200567 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200568 * @param[in,out] data Data to read from, always moved to currently handled character.
569 * @param[in] arg Type of YANG keyword argument expected.
Michal Vasko2ca70f52018-09-27 11:04:51 +0200570 * @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 +0200571 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read string. If not needed,
572 * set to NULL. Otherwise equal to \p word_p.
573 * @param[out] word_len Length of the read string.
574 *
575 * @return LY_ERR values.
Michal Vasko7fbc8162018-09-17 10:35:16 +0200576 */
577static LY_ERR
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200578get_argument(struct ly_parser_ctx *ctx, const char **data, enum yang_arg arg, char **word_p, char **word_b, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200579{
Radek Krejci44ceedc2018-10-02 15:54:31 +0200580 size_t buf_len = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200581
582 /* word buffer - dynamically allocated */
583 *word_b = NULL;
584
585 /* word pointer - just a pointer to data */
586 *word_p = NULL;
587
588 *word_len = 0;
589 while (**data) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200590 switch (**data) {
591 case '\'':
592 case '\"':
593 if (*word_len) {
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200594 /* invalid - quotes cannot be in unquoted string and only optsep, ; or { can follow it */
595 LOGVAL_YANG(ctx, LY_VCODE_INSTREXP, 1, *data,
596 "unquoted string character, optsep, semicolon or opening brace");
597 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200598 }
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100599 LY_CHECK_RET(read_qstring(ctx, data, arg, word_p, word_b, word_len, &buf_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200600 goto str_end;
601 case '/':
Radek Krejci44ceedc2018-10-02 15:54:31 +0200602 if ((*data)[1] == '/') {
603 /* one-line comment */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200604 MOVE_INPUT(ctx, data, 2);
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100605 LY_CHECK_RET(skip_comment(ctx, data, 1));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200606 } else if ((*data)[1] == '*') {
607 /* block comment */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200608 MOVE_INPUT(ctx, data, 2);
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100609 LY_CHECK_RET(skip_comment(ctx, data, 2));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200610 } else {
611 /* not a comment after all */
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100612 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 +0200613 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200614 break;
615 case ' ':
616 if (*word_len) {
617 /* word is finished */
618 goto str_end;
619 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200620 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200621 break;
622 case '\t':
623 if (*word_len) {
624 /* word is finished */
625 goto str_end;
626 }
627 /* tabs count for 8 spaces */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200628 ctx->indent += 8;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200629
630 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200631 break;
632 case '\n':
633 if (*word_len) {
634 /* word is finished */
635 goto str_end;
636 }
637 /* reset indent */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200638 ctx->indent = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200639
640 /* track line numbers */
641 ++ctx->line;
642
643 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200644 break;
645 case ';':
646 case '{':
647 if (*word_len || (arg == Y_MAYBE_STR_ARG)) {
648 /* word is finished */
649 goto str_end;
650 }
651
Radek Krejci44ceedc2018-10-02 15:54:31 +0200652 LOGVAL_YANG(ctx, LY_VCODE_INSTREXP, 1, *data, "an argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200653 return LY_EVALID;
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200654 case '}':
655 /* invalid - braces cannot be in unquoted string (opening braces terminates the string and can follow it) */
656 LOGVAL_YANG(ctx, LY_VCODE_INSTREXP, 1, *data,
657 "unquoted string character, optsep, semicolon or opening brace");
658 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200659 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200660 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 +0200661 break;
662 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200663 }
664
665str_end:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200666 /* terminating NULL byte for buf */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200667 if (*word_b) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200668 (*word_b) = ly_realloc(*word_b, (*word_len) + 1);
669 LY_CHECK_ERR_RET(!(*word_b), LOGMEM(ctx->ctx), LY_EMEM);
670 (*word_b)[*word_len] = '\0';
Michal Vasko7fbc8162018-09-17 10:35:16 +0200671 *word_p = *word_b;
672 }
673
674 return LY_SUCCESS;
675}
676
Michal Vaskoea5abea2018-09-18 13:10:54 +0200677/**
678 * @brief Get another YANG keyword from the raw data.
679 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200680 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200681 * @param[in,out] data Data to read from, always moved to currently handled character.
682 * @param[out] kw YANG keyword read.
683 * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances.
684 * @param[out] word_len Length of the keyword in the data. Useful for extension instances.
685 *
686 * @return LY_ERR values.
687 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200688static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +0200689get_keyword(struct ly_parser_ctx *ctx, const char **data, enum yang_keyword *kw, char **word_p, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200690{
Michal Vasko7fbc8162018-09-17 10:35:16 +0200691 int prefix;
692 const char *word_start;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200693 unsigned int c;
694 size_t len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200695
696 if (word_p) {
697 *word_p = NULL;
698 *word_len = 0;
699 }
700
701 /* first skip "optsep", comments */
702 while (**data) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200703 switch (**data) {
704 case '/':
705 if ((*data)[1] == '/') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200706 /* one-line comment */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200707 MOVE_INPUT(ctx, data, 2);
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100708 LY_CHECK_RET(skip_comment(ctx, data, 1));
Radek Krejcidcc7b322018-10-11 14:24:02 +0200709 } else if ((*data)[1] == '*') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200710 /* block comment */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200711 MOVE_INPUT(ctx, data, 2);
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100712 LY_CHECK_RET(skip_comment(ctx, data, 2));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200713 } else {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200714 /* error - not a comment after all, keyword cannot start with slash */
715 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '/'.");
716 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200717 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200718 break;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200719 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200720 /* skip whitespaces (optsep) */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200721 ++ctx->line;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200722 ctx->indent = 0;
723 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200724 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200725 /* skip whitespaces (optsep) */
726 ++ctx->indent;
727 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200728 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200729 /* skip whitespaces (optsep) */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200730 ctx->indent += 8;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200731 break;
732 default:
733 /* either a keyword start or an invalid character */
734 goto keyword_start;
735 }
736
737 ++(*data);
738 }
739
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200740#define IF_KW(STR, LEN, STMT) if (!strncmp(*(data), STR, LEN)) {MOVE_INPUT(ctx, data, LEN);*kw=STMT;}
741#define IF_KW_PREFIX(STR, LEN) if (!strncmp(*(data), STR, LEN)) {MOVE_INPUT(ctx, data, LEN);
742#define IF_KW_PREFIX_END }
743
Michal Vasko7fbc8162018-09-17 10:35:16 +0200744keyword_start:
745 word_start = *data;
746 *kw = YANG_NONE;
747
748 /* read the keyword itself */
749 switch (**data) {
750 case 'a':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200751 MOVE_INPUT(ctx, data, 1);
752 IF_KW("rgument", 7, YANG_ARGUMENT)
753 else IF_KW("ugment", 6, YANG_AUGMENT)
754 else IF_KW("ction", 5, YANG_ACTION)
755 else IF_KW_PREFIX("ny", 2)
756 IF_KW("data", 4, YANG_ANYDATA)
757 else IF_KW("xml", 3, YANG_ANYXML)
758 IF_KW_PREFIX_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200759 break;
760 case 'b':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200761 MOVE_INPUT(ctx, data, 1);
762 IF_KW("ase", 3, YANG_BASE)
763 else IF_KW("elongs-to", 9, YANG_BELONGS_TO)
764 else IF_KW("it", 2, YANG_BIT)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200765 break;
766 case 'c':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200767 MOVE_INPUT(ctx, data, 1);
768 IF_KW("ase", 3, YANG_CASE)
769 else IF_KW("hoice", 5, YANG_CHOICE)
770 else IF_KW_PREFIX("on", 2)
771 IF_KW("fig", 3, YANG_CONFIG)
772 else IF_KW_PREFIX("ta", 2)
773 IF_KW("ct", 2, YANG_CONTACT)
774 else IF_KW("iner", 4, YANG_CONTAINER)
775 IF_KW_PREFIX_END
776 IF_KW_PREFIX_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200777 break;
778 case 'd':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200779 MOVE_INPUT(ctx, data, 1);
780 IF_KW_PREFIX("e", 1)
781 IF_KW("fault", 5, YANG_DEFAULT)
782 else IF_KW("scription", 9, YANG_DESCRIPTION)
783 else IF_KW_PREFIX("viat", 4)
784 IF_KW("e", 1, YANG_DEVIATE)
785 else IF_KW("ion", 3, YANG_DEVIATION)
786 IF_KW_PREFIX_END
787 IF_KW_PREFIX_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200788 break;
789 case 'e':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200790 MOVE_INPUT(ctx, data, 1);
791 IF_KW("num", 3, YANG_ENUM)
792 else IF_KW_PREFIX("rror-", 5)
793 IF_KW("app-tag", 7, YANG_ERROR_APP_TAG)
794 else IF_KW("message", 7, YANG_ERROR_MESSAGE)
795 IF_KW_PREFIX_END
796 else IF_KW("xtension", 8, YANG_EXTENSION)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200797 break;
798 case 'f':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200799 MOVE_INPUT(ctx, data, 1);
800 IF_KW("eature", 6, YANG_FEATURE)
801 else IF_KW("raction-digits", 14, YANG_FRACTION_DIGITS)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200802 break;
803 case 'g':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200804 MOVE_INPUT(ctx, data, 1);
805 IF_KW("rouping", 7, YANG_GROUPING)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200806 break;
807 case 'i':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200808 MOVE_INPUT(ctx, data, 1);
809 IF_KW("dentity", 7, YANG_IDENTITY)
810 else IF_KW("f-feature", 9, YANG_IF_FEATURE)
811 else IF_KW("mport", 5, YANG_IMPORT)
812 else IF_KW_PREFIX("n", 1)
813 IF_KW("clude", 5, YANG_INCLUDE)
814 else IF_KW("put", 3, YANG_INPUT)
815 IF_KW_PREFIX_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200816 break;
817 case 'k':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200818 MOVE_INPUT(ctx, data, 1);
819 IF_KW("ey", 2, YANG_KEY)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200820 break;
821 case 'l':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200822 MOVE_INPUT(ctx, data, 1);
823 IF_KW_PREFIX("e", 1)
824 IF_KW("af-list", 7, YANG_LEAF_LIST)
825 else IF_KW("af", 2, YANG_LEAF)
826 else IF_KW("ngth", 4, YANG_LENGTH)
827 IF_KW_PREFIX_END
828 else IF_KW("ist", 3, YANG_LIST)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200829 break;
830 case 'm':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200831 MOVE_INPUT(ctx, data, 1);
832 IF_KW_PREFIX("a", 1)
833 IF_KW("ndatory", 7, YANG_MANDATORY)
834 else IF_KW("x-elements", 10, YANG_MAX_ELEMENTS)
835 IF_KW_PREFIX_END
836 else IF_KW("in-elements", 11, YANG_MIN_ELEMENTS)
837 else IF_KW("ust", 3, YANG_MUST)
838 else IF_KW_PREFIX("od", 2)
839 IF_KW("ule", 3, YANG_MODULE)
840 else IF_KW("ifier", 5, YANG_MODIFIER)
841 IF_KW_PREFIX_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200842 break;
843 case 'n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200844 MOVE_INPUT(ctx, data, 1);
845 IF_KW("amespace", 8, YANG_NAMESPACE)
846 else IF_KW("otification", 11, YANG_NOTIFICATION)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200847 break;
848 case 'o':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200849 MOVE_INPUT(ctx, data, 1);
850 IF_KW_PREFIX("r", 1)
851 IF_KW("dered-by", 8, YANG_ORDERED_BY)
852 else IF_KW("ganization", 10, YANG_ORGANIZATION)
853 IF_KW_PREFIX_END
854 else IF_KW("utput", 5, YANG_OUTPUT)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200855 break;
856 case 'p':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200857 MOVE_INPUT(ctx, data, 1);
858 IF_KW("ath", 3, YANG_PATH)
859 else IF_KW("attern", 6, YANG_PATTERN)
860 else IF_KW("osition", 7, YANG_POSITION)
861 else IF_KW_PREFIX("re", 2)
862 IF_KW("fix", 3, YANG_PREFIX)
863 else IF_KW("sence", 5, YANG_PRESENCE)
864 IF_KW_PREFIX_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200865 break;
866 case 'r':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200867 MOVE_INPUT(ctx, data, 1);
868 IF_KW("ange", 4, YANG_RANGE)
869 else IF_KW_PREFIX("e", 1)
870 IF_KW_PREFIX("f", 1)
871 IF_KW("erence", 6, YANG_REFERENCE)
872 else IF_KW("ine", 3, YANG_REFINE)
873 IF_KW_PREFIX_END
874 else IF_KW("quire-instance", 14, YANG_REQUIRE_INSTANCE)
875 else IF_KW("vision-date", 11, YANG_REVISION_DATE)
876 else IF_KW("vision", 6, YANG_REVISION)
877 IF_KW_PREFIX_END
878 else IF_KW("pc", 2, YANG_RPC)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200879 break;
880 case 's':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200881 MOVE_INPUT(ctx, data, 1);
882 IF_KW("tatus", 5, YANG_STATUS)
883 else IF_KW("ubmodule", 8, YANG_SUBMODULE)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200884 break;
885 case 't':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200886 MOVE_INPUT(ctx, data, 1);
887 IF_KW("ypedef", 6, YANG_TYPEDEF)
888 else IF_KW("ype", 3, YANG_TYPE)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200889 break;
890 case 'u':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200891 MOVE_INPUT(ctx, data, 1);
892 IF_KW_PREFIX("ni", 2)
893 IF_KW("que", 3, YANG_UNIQUE)
894 else IF_KW("ts", 2, YANG_UNITS)
895 IF_KW_PREFIX_END
896 else IF_KW("ses", 3, YANG_USES)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200897 break;
898 case 'v':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200899 MOVE_INPUT(ctx, data, 1);
900 IF_KW("alue", 4, YANG_VALUE)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200901 break;
902 case 'w':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200903 MOVE_INPUT(ctx, data, 1);
904 IF_KW("hen", 3, YANG_WHEN)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200905 break;
906 case 'y':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200907 MOVE_INPUT(ctx, data, 1);
908 IF_KW("ang-version", 11, YANG_YANG_VERSION)
909 else IF_KW("in-element", 10, YANG_YIN_ELEMENT)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200910 break;
911 case ';':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200912 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200913 *kw = YANG_SEMICOLON;
Radek Krejci626df482018-10-11 15:06:31 +0200914 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200915 case '{':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200916 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200917 *kw = YANG_LEFT_BRACE;
Radek Krejci626df482018-10-11 15:06:31 +0200918 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200919 case '}':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200920 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200921 *kw = YANG_RIGHT_BRACE;
Radek Krejci626df482018-10-11 15:06:31 +0200922 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200923 default:
924 break;
925 }
926
Radek Krejci0904c162019-01-02 15:03:59 +0100927#undef IF_KW
928#undef IF_KW_PREFIX
929#undef IF_KW_PREFIX_END
930
Michal Vasko7fbc8162018-09-17 10:35:16 +0200931 if (*kw != YANG_NONE) {
932 /* make sure we have the whole keyword */
933 switch (**data) {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200934 case '\n':
935 ++ctx->line;
936 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200937 case ' ':
938 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200939 /* mandatory "sep" */
940 break;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200941 case ':':
942 /* keyword is not actually a keyword, but prefix of an extension.
943 * To avoid repeated check of the prefix syntax, move to the point where the colon was read
944 * and we will be checking the keyword (extension instance) itself */
945 prefix = 1;
946 MOVE_INPUT(ctx, data, 1);
947 goto extension;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200948 case '{':
949 /* allowed only for input and output statements which can be without arguments */
950 if (*kw == YANG_INPUT || *kw == YANG_OUTPUT) {
951 break;
952 }
953 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200954 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200955 MOVE_INPUT(ctx, data, 1);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200956 LOGVAL_YANG(ctx, LY_VCODE_INSTREXP, (int)(*data - word_start), word_start,
957 "a keyword followed by a separator");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200958 return LY_EVALID;
959 }
960 } else {
961 /* still can be an extension */
962 prefix = 0;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200963extension:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200964 while (**data && (**data != ' ') && (**data != '\t') && (**data != '\n') && (**data != '{') && (**data != ';')) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200965 LY_CHECK_ERR_RET(ly_getutf8(data, &c, &len),
966 LOGVAL_YANG(ctx, LY_VCODE_INCHAR, (*data)[-len]), LY_EVALID);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200967 ++ctx->indent;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200968 /* check character validity */
969 LY_CHECK_RET(check_identifierchar(ctx, c, *data - len == word_start ? 1 : 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200970 }
971 if (!**data) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200972 LOGVAL_YANG(ctx, LY_VCODE_EOF);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200973 return LY_EVALID;
974 }
975
976 /* prefix is mandatory for extension instances */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200977 if (prefix != 2) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200978 LOGVAL_YANG(ctx, LY_VCODE_INSTREXP, (int)(*data - word_start), word_start, "a keyword");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200979 return LY_EVALID;
980 }
981
982 *kw = YANG_CUSTOM;
983 }
Radek Krejci626df482018-10-11 15:06:31 +0200984success:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200985 if (word_p) {
986 *word_p = (char *)word_start;
987 *word_len = *data - word_start;
988 }
989
990 return LY_SUCCESS;
991}
992
Michal Vaskoea5abea2018-09-18 13:10:54 +0200993/**
994 * @brief Parse extension instance substatements.
995 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200996 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200997 * @param[in,out] data Data to read from, always moved to currently handled character.
998 * @param[in] word Extension instance substatement name (keyword).
999 * @param[in] word_len Extension instance substatement name length.
1000 * @param[in,out] child Children of this extension instance to add to.
1001 *
1002 * @return LY_ERR values.
1003 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001004static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001005parse_ext_substmt(struct ly_parser_ctx *ctx, const char **data, char *word, size_t word_len,
Michal Vasko7fbc8162018-09-17 10:35:16 +02001006 struct lysp_stmt **child)
1007{
1008 char *buf;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001009 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001010 enum yang_keyword kw;
1011 struct lysp_stmt *stmt, *par_child;
1012
1013 stmt = calloc(1, sizeof *stmt);
1014 LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM);
1015
Radek Krejcibb9b1982019-04-08 14:24:59 +02001016 /* insert into parent statements */
1017 if (!*child) {
1018 *child = stmt;
1019 } else {
1020 for (par_child = *child; par_child->next; par_child = par_child->next);
1021 par_child->next = stmt;
1022 }
1023
Radek Krejci44ceedc2018-10-02 15:54:31 +02001024 stmt->stmt = lydict_insert(ctx->ctx, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001025
1026 /* get optional argument */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001027 LY_CHECK_RET(get_argument(ctx, data, Y_MAYBE_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001028
Radek Krejci0ae092d2018-09-20 16:43:19 +02001029 if (word) {
1030 if (buf) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001031 stmt->arg = lydict_insert_zc(ctx->ctx, word);
Radek Krejci0ae092d2018-09-20 16:43:19 +02001032 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001033 stmt->arg = lydict_insert(ctx->ctx, word, word_len);
Radek Krejci0ae092d2018-09-20 16:43:19 +02001034 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001035 }
1036
Radek Krejci6d6556c2018-11-08 09:37:45 +01001037 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, ) {
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001038 LY_CHECK_RET(parse_ext_substmt(ctx, data, word, word_len, &stmt->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001039 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001040 return ret;
1041}
1042
Michal Vaskoea5abea2018-09-18 13:10:54 +02001043/**
1044 * @brief Parse extension instance.
1045 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001046 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001047 * @param[in,out] data Data to read from, always moved to currently handled character.
1048 * @param[in] ext_name Extension instance substatement name (keyword).
1049 * @param[in] ext_name_len Extension instance substatement name length.
1050 * @param[in] insubstmt Type of the keyword this extension instance is a substatement of.
1051 * @param[in] insubstmt_index Index of the keyword instance this extension instance is a substatement of.
1052 * @param[in,out] exts Extension instances to add to.
1053 *
1054 * @return LY_ERR values.
1055 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001056static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001057parse_ext(struct ly_parser_ctx *ctx, const char **data, const char *ext_name, int ext_name_len, LYEXT_SUBSTMT insubstmt,
Michal Vasko7fbc8162018-09-17 10:35:16 +02001058 uint32_t insubstmt_index, struct lysp_ext_instance **exts)
1059{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001060 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001061 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001062 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001063 struct lysp_ext_instance *e;
1064 enum yang_keyword kw;
1065
Radek Krejci2c4e7172018-10-19 15:56:26 +02001066 LY_ARRAY_NEW_RET(ctx->ctx, *exts, e, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001067
1068 /* store name and insubstmt info */
Radek Krejci44ceedc2018-10-02 15:54:31 +02001069 e->name = lydict_insert(ctx->ctx, ext_name, ext_name_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001070 e->insubstmt = insubstmt;
1071 e->insubstmt_index = insubstmt_index;
1072
1073 /* get optional argument */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001074 LY_CHECK_RET(get_argument(ctx, data, Y_MAYBE_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001075
Radek Krejci0ae092d2018-09-20 16:43:19 +02001076 if (word) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001077 INSERT_WORD(ctx, buf, e->argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001078 }
1079
Radek Krejci6d6556c2018-11-08 09:37:45 +01001080 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001081 LY_CHECK_RET(parse_ext_substmt(ctx, data, word, word_len, &e->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001082 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001083 return ret;
1084}
1085
Michal Vaskoea5abea2018-09-18 13:10:54 +02001086/**
1087 * @brief Parse a generic text field without specific constraints. Those are contact, organization,
1088 * description, etc...
1089 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001090 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001091 * @param[in,out] data Data to read from, always moved to currently handled character.
1092 * @param[in] substmt Type of this substatement.
1093 * @param[in] substmt_index Index of this substatement.
1094 * @param[in,out] value Place to store the parsed value.
1095 * @param[in] arg Type of the YANG keyword argument (of the value).
1096 * @param[in,out] exts Extension instances to add to.
1097 *
1098 * @return LY_ERR values.
1099 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001100static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001101parse_text_field(struct ly_parser_ctx *ctx, const char **data, LYEXT_SUBSTMT substmt, uint32_t substmt_index,
Michal Vasko7fbc8162018-09-17 10:35:16 +02001102 const char **value, enum yang_arg arg, struct lysp_ext_instance **exts)
1103{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001104 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001105 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001106 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001107 enum yang_keyword kw;
1108
1109 if (*value) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001110 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001111 return LY_EVALID;
1112 }
1113
1114 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001115 LY_CHECK_RET(get_argument(ctx, data, arg, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001116
1117 /* store value and spend buf if allocated */
Radek Krejci44ceedc2018-10-02 15:54:31 +02001118 INSERT_WORD(ctx, buf, *value, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001119
Radek Krejci6d6556c2018-11-08 09:37:45 +01001120 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001121 switch (kw) {
1122 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001123 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, substmt, substmt_index, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001124 break;
1125 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001126 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001127 return LY_EVALID;
1128 }
1129 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001130 return ret;
1131}
1132
Michal Vaskoea5abea2018-09-18 13:10:54 +02001133/**
1134 * @brief Parse the yang-version statement.
1135 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001136 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001137 * @param[in,out] data Data to read from, always moved to currently handled character.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001138 * @param[out] version Storage for the parsed information.
1139 * @param[in, out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001140 *
1141 * @return LY_ERR values.
1142 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001143static LY_ERR
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001144parse_yangversion(struct ly_parser_ctx *ctx, const char **data, uint8_t *version, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001145{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001146 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001147 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001148 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001149 enum yang_keyword kw;
1150
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001151 if (*version) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001152 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001153 return LY_EVALID;
1154 }
1155
1156 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001157 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001158
1159 if ((word_len == 3) && !strncmp(word, "1.0", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001160 *version = LYS_VERSION_1_0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001161 } else if ((word_len == 3) && !strncmp(word, "1.1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001162 *version = LYS_VERSION_1_1;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001163 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001164 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001165 free(buf);
1166 return LY_EVALID;
1167 }
1168 free(buf);
1169
Radek Krejci6d6556c2018-11-08 09:37:45 +01001170 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001171 switch (kw) {
1172 case YANG_CUSTOM:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001173 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_VERSION, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001174 break;
1175 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001176 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001177 return LY_EVALID;
1178 }
1179 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001180 return ret;
1181}
1182
Michal Vaskoea5abea2018-09-18 13:10:54 +02001183/**
1184 * @brief Parse the belongs-to statement.
1185 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001186 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001187 * @param[in,out] data Data to read from, always moved to currently handled character.
1188 * @param[in,out] belongsto Place to store the parsed value.
1189 * @param[in,out] prefix Place to store the parsed belongs-to prefix value.
1190 * @param[in,out] exts Extension instances to add to.
1191 *
1192 * @return LY_ERR values.
1193 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001194static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001195parse_belongsto(struct ly_parser_ctx *ctx, const char **data, const char **belongsto, const char **prefix, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001196{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001197 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001198 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001199 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001200 enum yang_keyword kw;
1201
1202 if (*belongsto) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001203 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001204 return LY_EVALID;
1205 }
1206
1207 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001208 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001209
Radek Krejci44ceedc2018-10-02 15:54:31 +02001210 INSERT_WORD(ctx, buf, *belongsto, word, word_len);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001211 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001212 switch (kw) {
1213 case YANG_PREFIX:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001214 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_PREFIX, 0, prefix, Y_IDENTIF_ARG, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001215 break;
1216 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001217 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_BELONGSTO, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001218 break;
1219 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001220 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001221 return LY_EVALID;
1222 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001223 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001224 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001225checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001226 /* mandatory substatements */
1227 if (!*prefix) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001228 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001229 return LY_EVALID;
1230 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001231 return ret;
1232}
1233
Michal Vaskoea5abea2018-09-18 13:10:54 +02001234/**
1235 * @brief Parse the revision-date statement.
1236 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001237 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001238 * @param[in,out] data Data to read from, always moved to currently handled character.
1239 * @param[in,out] rev Array to store the parsed value in.
1240 * @param[in,out] exts Extension instances to add to.
1241 *
1242 * @return LY_ERR values.
1243 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001244static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001245parse_revisiondate(struct ly_parser_ctx *ctx, const char **data, char *rev, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001246{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001247 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001248 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001249 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001250 enum yang_keyword kw;
1251
1252 if (rev[0]) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001253 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001254 return LY_EVALID;
1255 }
1256
1257 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001258 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001259
1260 /* check value */
Radek Krejcibbe09a92018-11-08 09:36:54 +01001261 if (lysp_check_date(ctx, word, word_len, "revision-date")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001262 free(buf);
1263 return LY_EVALID;
1264 }
1265
1266 /* store value and spend buf if allocated */
1267 strncpy(rev, word, word_len);
1268 free(buf);
1269
Radek Krejci6d6556c2018-11-08 09:37:45 +01001270 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001271 switch (kw) {
1272 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001273 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_REVISIONDATE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001274 break;
1275 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001276 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001277 return LY_EVALID;
1278 }
1279 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001280 return ret;
1281}
1282
Michal Vaskoea5abea2018-09-18 13:10:54 +02001283/**
1284 * @brief Parse the include statement.
1285 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001286 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001287 * @param[in] module_name Name of the module to check name collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001288 * @param[in,out] data Data to read from, always moved to currently handled character.
1289 * @param[in,out] includes Parsed includes to add to.
1290 *
1291 * @return LY_ERR values.
1292 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001293static LY_ERR
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001294parse_include(struct ly_parser_ctx *ctx, const char *module_name, const char **data, struct lysp_include **includes)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001295{
Radek Krejcid33273d2018-10-25 14:55:52 +02001296 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001297 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001298 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001299 enum yang_keyword kw;
1300 struct lysp_include *inc;
1301
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001302 LY_ARRAY_NEW_RET(ctx->ctx, *includes, inc, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001303
1304 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001305 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001306
Radek Krejci086c7132018-10-26 15:29:04 +02001307 INSERT_WORD(ctx, buf, inc->name, word, word_len);
1308
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001309 /* submodules share the namespace with the module names, so there must not be
1310 * a module of the same name in the context, no need for revision matching */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001311 if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(ctx->ctx, inc->name)) {
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001312 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Name collision between module and submodule of name \"%s\".", inc->name);
1313 return LY_EVALID;
1314 }
1315
Radek Krejci6d6556c2018-11-08 09:37:45 +01001316 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001317 switch (kw) {
1318 case YANG_DESCRIPTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01001319 YANG_CHECK_STMTVER2_RET(ctx, "description", "include");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001320 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 +02001321 break;
1322 case YANG_REFERENCE:
Radek Krejciceaf2122019-01-02 15:03:26 +01001323 YANG_CHECK_STMTVER2_RET(ctx, "reference", "include");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001324 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 +02001325 break;
1326 case YANG_REVISION_DATE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001327 LY_CHECK_RET(parse_revisiondate(ctx, data, inc->rev, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001328 break;
1329 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001330 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001331 break;
1332 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001333 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001334 return LY_EVALID;
1335 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001336 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001337 return ret;
1338}
1339
Michal Vaskoea5abea2018-09-18 13:10:54 +02001340/**
1341 * @brief Parse the import statement.
1342 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001343 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001344 * @param[in] module_prefix Prefix of the module to check prefix collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001345 * @param[in,out] data Data to read from, always moved to currently handled character.
1346 * @param[in,out] imports Parsed imports to add to.
1347 *
1348 * @return LY_ERR values.
1349 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001350static LY_ERR
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001351parse_import(struct ly_parser_ctx *ctx, const char *module_prefix, const char **data, struct lysp_import **imports)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001352{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001353 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001354 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001355 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001356 enum yang_keyword kw;
1357 struct lysp_import *imp;
1358
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001359 LY_ARRAY_NEW_RET(ctx->ctx, *imports, imp, LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001360
1361 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001362 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02001363 INSERT_WORD(ctx, buf, imp->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001364
Radek Krejci6d6556c2018-11-08 09:37:45 +01001365 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001366 switch (kw) {
1367 case YANG_PREFIX:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001368 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 +01001369 LY_CHECK_RET(lysp_check_prefix(ctx, *imports, module_prefix, &imp->prefix), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001370 break;
1371 case YANG_DESCRIPTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01001372 YANG_CHECK_STMTVER2_RET(ctx, "description", "import");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001373 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 +02001374 break;
1375 case YANG_REFERENCE:
Radek Krejciceaf2122019-01-02 15:03:26 +01001376 YANG_CHECK_STMTVER2_RET(ctx, "reference", "import");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001377 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 +02001378 break;
1379 case YANG_REVISION_DATE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001380 LY_CHECK_RET(parse_revisiondate(ctx, data, imp->rev, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001381 break;
1382 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001383 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001384 break;
1385 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001386 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001387 return LY_EVALID;
1388 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001389 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001390 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001391checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001392 /* mandatory substatements */
Radek Krejci086c7132018-10-26 15:29:04 +02001393 LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001394
1395 return ret;
1396}
1397
Michal Vaskoea5abea2018-09-18 13:10:54 +02001398/**
1399 * @brief Parse the revision statement.
1400 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001401 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001402 * @param[in,out] data Data to read from, always moved to currently handled character.
1403 * @param[in,out] revs Parsed revisions to add to.
1404 *
1405 * @return LY_ERR values.
1406 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001407static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001408parse_revision(struct ly_parser_ctx *ctx, const char **data, struct lysp_revision **revs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001409{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001410 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001411 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001412 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001413 enum yang_keyword kw;
1414 struct lysp_revision *rev;
1415
Radek Krejci2c4e7172018-10-19 15:56:26 +02001416 LY_ARRAY_NEW_RET(ctx->ctx, *revs, rev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001417
1418 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001419 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001420
1421 /* check value */
Radek Krejcibbe09a92018-11-08 09:36:54 +01001422 if (lysp_check_date(ctx, word, word_len, "revision")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001423 return LY_EVALID;
1424 }
1425
Radek Krejcib7db73a2018-10-24 14:18:40 +02001426 strncpy(rev->date, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001427 free(buf);
1428
Radek Krejci6d6556c2018-11-08 09:37:45 +01001429 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001430 switch (kw) {
1431 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001432 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 +02001433 break;
1434 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001435 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 +02001436 break;
1437 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001438 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001439 break;
1440 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001441 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001442 return LY_EVALID;
1443 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001444 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001445 return ret;
1446}
1447
Michal Vaskoea5abea2018-09-18 13:10:54 +02001448/**
1449 * @brief Parse a generic text field that can have more instances such as base.
1450 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001451 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001452 * @param[in,out] data Data to read from, always moved to currently handled character.
1453 * @param[in] substmt Type of this substatement.
1454 * @param[in,out] texts Parsed values to add to.
1455 * @param[in] arg Type of the expected argument.
1456 * @param[in,out] exts Extension instances to add to.
1457 *
1458 * @return LY_ERR values.
1459 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001460static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001461parse_text_fields(struct ly_parser_ctx *ctx, const char **data, LYEXT_SUBSTMT substmt, const char ***texts, enum yang_arg arg,
Michal Vasko7fbc8162018-09-17 10:35:16 +02001462 struct lysp_ext_instance **exts)
1463{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001464 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001465 char *buf, *word;
Radek Krejci151a5b72018-10-19 14:21:44 +02001466 const char **item;
1467 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001468 enum yang_keyword kw;
1469
1470 /* allocate new pointer */
Radek Krejci2c4e7172018-10-19 15:56:26 +02001471 LY_ARRAY_NEW_RET(ctx->ctx, *texts, item, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001472
1473 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001474 LY_CHECK_RET(get_argument(ctx, data, arg, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001475
Radek Krejci151a5b72018-10-19 14:21:44 +02001476 INSERT_WORD(ctx, buf, *item, word, word_len);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001477 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001478 switch (kw) {
1479 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001480 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, substmt, LY_ARRAY_SIZE(*texts) - 1, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001481 break;
1482 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001483 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001484 return LY_EVALID;
1485 }
1486 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001487 return ret;
1488}
1489
Michal Vaskoea5abea2018-09-18 13:10:54 +02001490/**
1491 * @brief Parse the config statement.
1492 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001493 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001494 * @param[in,out] data Data to read from, always moved to currently handled character.
1495 * @param[in,out] flags Flags to add to.
1496 * @param[in,out] exts Extension instances to add to.
1497 *
1498 * @return LY_ERR values.
1499 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001500static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001501parse_config(struct ly_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001502{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001503 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001504 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001505 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001506 enum yang_keyword kw;
1507
1508 if (*flags & LYS_CONFIG_MASK) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001509 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001510 return LY_EVALID;
1511 }
1512
1513 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001514 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001515
1516 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1517 *flags |= LYS_CONFIG_W;
1518 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1519 *flags |= LYS_CONFIG_R;
1520 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001521 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001522 free(buf);
1523 return LY_EVALID;
1524 }
1525 free(buf);
1526
Radek Krejci6d6556c2018-11-08 09:37:45 +01001527 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001528 switch (kw) {
1529 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001530 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_CONFIG, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001531 break;
1532 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001533 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001534 return LY_EVALID;
1535 }
1536 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001537 return ret;
1538}
1539
Michal Vaskoea5abea2018-09-18 13:10:54 +02001540/**
1541 * @brief Parse the mandatory statement.
1542 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001543 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001544 * @param[in,out] data Data to read from, always moved to currently handled character.
1545 * @param[in,out] flags Flags to add to.
1546 * @param[in,out] exts Extension instances to add to.
1547 *
1548 * @return LY_ERR values.
1549 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001550static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001551parse_mandatory(struct ly_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001552{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001553 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001554 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001555 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001556 enum yang_keyword kw;
1557
1558 if (*flags & LYS_MAND_MASK) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001559 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001560 return LY_EVALID;
1561 }
1562
1563 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001564 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001565
1566 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1567 *flags |= LYS_MAND_TRUE;
1568 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1569 *flags |= LYS_MAND_FALSE;
1570 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001571 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001572 free(buf);
1573 return LY_EVALID;
1574 }
1575 free(buf);
1576
Radek Krejci6d6556c2018-11-08 09:37:45 +01001577 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001578 switch (kw) {
1579 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001580 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MANDATORY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001581 break;
1582 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001583 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001584 return LY_EVALID;
1585 }
1586 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001587 return ret;
1588}
1589
Michal Vaskoea5abea2018-09-18 13:10:54 +02001590/**
1591 * @brief Parse a restriction such as range or length.
1592 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001593 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001594 * @param[in,out] data Data to read from, always moved to currently handled character.
1595 * @param[in] restr_kw Type of this particular restriction.
1596 * @param[in,out] exts Extension instances to add to.
1597 *
1598 * @return LY_ERR values.
1599 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001600static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001601parse_restr(struct ly_parser_ctx *ctx, const char **data, enum yang_keyword restr_kw, struct lysp_restr *restr)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001602{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001603 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001604 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001605 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001606 enum yang_keyword kw;
1607
1608 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001609 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001610
Radek Krejci44ceedc2018-10-02 15:54:31 +02001611 INSERT_WORD(ctx, buf, restr->arg, word, word_len);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001612 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001613 switch (kw) {
1614 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001615 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 +02001616 break;
1617 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001618 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 +02001619 break;
1620 case YANG_ERROR_APP_TAG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001621 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 +02001622 break;
1623 case YANG_ERROR_MESSAGE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001624 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 +02001625 break;
1626 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001627 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001628 break;
1629 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001630 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001631 return LY_EVALID;
1632 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001633 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001634 return ret;
1635}
1636
Michal Vaskoea5abea2018-09-18 13:10:54 +02001637/**
1638 * @brief Parse a restriction that can have more instances such as must.
1639 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001640 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001641 * @param[in,out] data Data to read from, always moved to currently handled character.
1642 * @param[in] restr_kw Type of this particular restriction.
1643 * @param[in,out] restrs Restrictions to add to.
1644 *
1645 * @return LY_ERR values.
1646 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001647static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001648parse_restrs(struct ly_parser_ctx *ctx, const char **data, enum yang_keyword restr_kw, struct lysp_restr **restrs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001649{
1650 struct lysp_restr *restr;
1651
Radek Krejci2c4e7172018-10-19 15:56:26 +02001652 LY_ARRAY_NEW_RET(ctx->ctx, *restrs, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001653 return parse_restr(ctx, data, restr_kw, restr);
1654}
1655
Michal Vaskoea5abea2018-09-18 13:10:54 +02001656/**
1657 * @brief Parse the status statement.
1658 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001659 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001660 * @param[in,out] data Data to read from, always moved to currently handled character.
1661 * @param[in,out] flags Flags to add to.
1662 * @param[in,out] exts Extension instances to add to.
1663 *
1664 * @return LY_ERR values.
1665 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001666static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001667parse_status(struct ly_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001668{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001669 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001670 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001671 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001672 enum yang_keyword kw;
1673
1674 if (*flags & LYS_STATUS_MASK) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001675 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001676 return LY_EVALID;
1677 }
1678
1679 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001680 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001681
1682 if ((word_len == 7) && !strncmp(word, "current", word_len)) {
1683 *flags |= LYS_STATUS_CURR;
1684 } else if ((word_len == 10) && !strncmp(word, "deprecated", word_len)) {
1685 *flags |= LYS_STATUS_DEPRC;
1686 } else if ((word_len == 8) && !strncmp(word, "obsolete", word_len)) {
1687 *flags |= LYS_STATUS_OBSLT;
1688 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001689 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001690 free(buf);
1691 return LY_EVALID;
1692 }
1693 free(buf);
1694
Radek Krejci6d6556c2018-11-08 09:37:45 +01001695 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001696 switch (kw) {
1697 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001698 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_STATUS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001699 break;
1700 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001701 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001702 return LY_EVALID;
1703 }
1704 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001705 return ret;
1706}
1707
Michal Vaskoea5abea2018-09-18 13:10:54 +02001708/**
1709 * @brief Parse the when statement.
1710 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001711 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001712 * @param[in,out] data Data to read from, always moved to currently handled character.
1713 * @param[in,out] when_p When pointer to parse to.
1714 *
1715 * @return LY_ERR values.
1716 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001717static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001718parse_when(struct ly_parser_ctx *ctx, const char **data, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001719{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001720 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001721 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001722 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001723 enum yang_keyword kw;
1724 struct lysp_when *when;
1725
1726 if (*when_p) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001727 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001728 return LY_EVALID;
1729 }
1730
1731 when = calloc(1, sizeof *when);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001732 LY_CHECK_ERR_RET(!when, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001733 *when_p = when;
1734
1735 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001736 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02001737 INSERT_WORD(ctx, buf, when->cond, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001738
Radek Krejci6d6556c2018-11-08 09:37:45 +01001739 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001740 switch (kw) {
1741 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001742 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 +02001743 break;
1744 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001745 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 +02001746 break;
1747 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001748 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001749 break;
1750 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001751 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001752 return LY_EVALID;
1753 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001754 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001755 return ret;
1756}
1757
Michal Vaskoea5abea2018-09-18 13:10:54 +02001758/**
1759 * @brief Parse the anydata or anyxml statement.
1760 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001761 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001762 * @param[in,out] data Data to read from, always moved to currently handled character.
1763 * @param[in] kw Type of this particular keyword.
1764 * @param[in,out] siblings Siblings to add to.
1765 *
1766 * @return LY_ERR values.
1767 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001768static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001769parse_any(struct ly_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 +02001770{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001771 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001772 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001773 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001774 struct lysp_node *iter;
1775 struct lysp_node_anydata *any;
1776
1777 /* create structure */
1778 any = calloc(1, sizeof *any);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001779 LY_CHECK_ERR_RET(!any, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001780 any->nodetype = kw == YANG_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001781 any->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001782
1783 /* insert into siblings */
1784 if (!*siblings) {
1785 *siblings = (struct lysp_node *)any;
1786 } else {
1787 for (iter = *siblings; iter->next; iter = iter->next);
1788 iter->next = (struct lysp_node *)any;
1789 }
1790
1791 /* get name */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001792 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02001793 INSERT_WORD(ctx, buf, any->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001794
1795 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01001796 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001797 switch (kw) {
1798 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001799 LY_CHECK_RET(parse_config(ctx, data, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001800 break;
1801 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001802 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 +02001803 break;
1804 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001805 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 +02001806 break;
1807 case YANG_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001808 LY_CHECK_RET(parse_mandatory(ctx, data, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001809 break;
1810 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001811 LY_CHECK_RET(parse_restrs(ctx, data, kw, &any->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001812 break;
1813 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001814 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 +02001815 break;
1816 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001817 LY_CHECK_RET(parse_status(ctx, data, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001818 break;
1819 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001820 LY_CHECK_RET(parse_when(ctx, data, &any->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001821 break;
1822 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001823 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001824 break;
1825 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001826 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
Radek Krejcic59bc972018-09-17 16:13:06 +02001827 (any->nodetype & LYS_ANYDATA) == LYS_ANYDATA ? ly_stmt2str(YANG_ANYDATA) : ly_stmt2str(YANG_ANYXML));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001828 return LY_EVALID;
1829 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001830 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001831 return ret;
1832}
1833
Michal Vaskoea5abea2018-09-18 13:10:54 +02001834/**
1835 * @brief Parse the value or position statement. Substatement of type enum statement.
1836 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001837 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001838 * @param[in,out] data Data to read from, always moved to currently handled character.
1839 * @param[in] val_kw Type of this particular keyword.
1840 * @param[in,out] value Value to write to.
1841 * @param[in,out] flags Flags to write to.
1842 * @param[in,out] exts Extension instances to add to.
1843 *
1844 * @return LY_ERR values.
1845 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001846static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001847parse_type_enum_value_pos(struct ly_parser_ctx *ctx, const char **data, enum yang_keyword val_kw, int64_t *value, uint16_t *flags,
Michal Vasko7fbc8162018-09-17 10:35:16 +02001848 struct lysp_ext_instance **exts)
1849{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001850 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001851 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001852 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001853 long int num;
1854 unsigned long int unum;
1855 enum yang_keyword kw;
1856
1857 if (*flags & LYS_SET_VALUE) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001858 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001859 return LY_EVALID;
1860 }
1861 *flags |= LYS_SET_VALUE;
1862
1863 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001864 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001865
1866 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 +02001867 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001868 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001869 }
1870
1871 errno = 0;
1872 if (val_kw == YANG_VALUE) {
1873 num = strtol(word, &ptr, 10);
Radek Krejci8b764662018-11-14 14:15:13 +01001874 if (num < INT64_C(-2147483648) || num > INT64_C(2147483647)) {
1875 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
1876 goto error;
1877 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001878 } else {
1879 unum = strtoul(word, &ptr, 10);
Radek Krejci8b764662018-11-14 14:15:13 +01001880 if (unum > UINT64_C(4294967295)) {
1881 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
1882 goto error;
1883 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001884 }
1885 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001886 if ((size_t)(ptr - word) != word_len) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001887 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001888 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001889 }
1890 if (errno == ERANGE) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001891 LOGVAL_YANG(ctx, LY_VCODE_OOB, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001892 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001893 }
1894 if (val_kw == YANG_VALUE) {
1895 *value = num;
1896 } else {
1897 *value = unum;
1898 }
1899 free(buf);
1900
Radek Krejci6d6556c2018-11-08 09:37:45 +01001901 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001902 switch (kw) {
1903 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001904 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 +02001905 break;
1906 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001907 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001908 return LY_EVALID;
1909 }
1910 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001911 return ret;
Radek Krejci8b764662018-11-14 14:15:13 +01001912
1913error:
1914 free(buf);
1915 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001916}
1917
Michal Vaskoea5abea2018-09-18 13:10:54 +02001918/**
1919 * @brief Parse the enum or bit statement. Substatement of type statement.
1920 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001921 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001922 * @param[in,out] data Data to read from, always moved to currently handled character.
1923 * @param[in] enum_kw Type of this particular keyword.
1924 * @param[in,out] enums Enums or bits to add to.
1925 *
1926 * @return LY_ERR values.
1927 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001928static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001929parse_type_enum(struct ly_parser_ctx *ctx, const char **data, enum yang_keyword enum_kw, struct lysp_type_enum **enums)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001930{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001931 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001932 char *buf, *word;
Radek Krejci8b764662018-11-14 14:15:13 +01001933 size_t word_len, u;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001934 enum yang_keyword kw;
1935 struct lysp_type_enum *enm;
1936
Radek Krejci2c4e7172018-10-19 15:56:26 +02001937 LY_ARRAY_NEW_RET(ctx->ctx, *enums, enm, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001938
1939 /* get value */
Radek Krejci8b764662018-11-14 14:15:13 +01001940 LY_CHECK_RET(get_argument(ctx, data, enum_kw == YANG_ENUM ? Y_STR_ARG : Y_IDENTIF_ARG, &word, &buf, &word_len));
1941 if (enum_kw == YANG_ENUM) {
1942 if (!word_len) {
1943 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Enum name must not be zero-length.");
1944 free(buf);
1945 return LY_EVALID;
1946 } else if (isspace(word[0]) || isspace(word[word_len - 1])) {
1947 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Enum name must not have any leading or trailing whitespaces (\"%.*s\").",
1948 word_len, word);
1949 free(buf);
1950 return LY_EVALID;
1951 } else {
1952 for (u = 0; u < word_len; ++u) {
1953 if (iscntrl(word[u])) {
1954 LOGWRN(ctx->ctx, "Control characters in enum name should be avoided (\"%.*s\", character number %d).",
1955 word_len, word, u + 1);
1956 break;
1957 }
1958 }
1959 }
1960 } else { /* YANG_BIT */
1961
1962 }
Radek Krejci44ceedc2018-10-02 15:54:31 +02001963 INSERT_WORD(ctx, buf, enm->name, word, word_len);
Radek Krejci8b764662018-11-14 14:15:13 +01001964 CHECK_UNIQUENESS(ctx, *enums, name, ly_stmt2str(enum_kw), enm->name);
1965
Radek Krejci6d6556c2018-11-08 09:37:45 +01001966 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001967 switch (kw) {
1968 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001969 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 +02001970 break;
1971 case YANG_IF_FEATURE:
Radek Krejciceaf2122019-01-02 15:03:26 +01001972 YANG_CHECK_STMTVER2_RET(ctx, "if-feature", ly_stmt2str(enum_kw));
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001973 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 +02001974 break;
1975 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001976 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 +02001977 break;
1978 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001979 LY_CHECK_RET(parse_status(ctx, data, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001980 break;
1981 case YANG_VALUE:
1982 case YANG_POSITION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001983 LY_CHECK_RET(parse_type_enum_value_pos(ctx, data, kw, &enm->value, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001984 break;
1985 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001986 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001987 break;
1988 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001989 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001990 return LY_EVALID;
1991 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001992 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001993 return ret;
1994}
1995
Michal Vaskoea5abea2018-09-18 13:10:54 +02001996/**
1997 * @brief Parse the fraction-digits statement. Substatement of type statement.
1998 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001999 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002000 * @param[in,out] data Data to read from, always moved to currently handled character.
2001 * @param[in,out] fracdig Value to write to.
2002 * @param[in,out] exts Extension instances to add to.
2003 *
2004 * @return LY_ERR values.
2005 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002006static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002007parse_type_fracdigits(struct ly_parser_ctx *ctx, const char **data, uint8_t *fracdig, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002008{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002009 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002010 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002011 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002012 unsigned long int num;
2013 enum yang_keyword kw;
2014
2015 if (*fracdig) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002016 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002017 return LY_EVALID;
2018 }
2019
2020 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002021 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002022
2023 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002024 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002025 free(buf);
2026 return LY_EVALID;
2027 }
2028
2029 errno = 0;
2030 num = strtoul(word, &ptr, 10);
2031 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002032 if ((size_t)(ptr - word) != word_len) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002033 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002034 free(buf);
2035 return LY_EVALID;
2036 }
2037 if ((errno == ERANGE) || (num > 18)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002038 LOGVAL_YANG(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002039 free(buf);
2040 return LY_EVALID;
2041 }
2042 *fracdig = num;
2043 free(buf);
2044
Radek Krejci6d6556c2018-11-08 09:37:45 +01002045 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002046 switch (kw) {
2047 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002048 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_FRACDIGITS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002049 break;
2050 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002051 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002052 return LY_EVALID;
2053 }
2054 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002055 return ret;
2056}
2057
Michal Vaskoea5abea2018-09-18 13:10:54 +02002058/**
2059 * @brief Parse the require-instance statement. Substatement of type statement.
2060 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002061 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002062 * @param[in,out] data Data to read from, always moved to currently handled character.
2063 * @param[in,out] reqinst Value to write to.
2064 * @param[in,out] flags Flags to write to.
2065 * @param[in,out] exts Extension instances to add to.
2066 *
2067 * @return LY_ERR values.
2068 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002069static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002070parse_type_reqinstance(struct ly_parser_ctx *ctx, const char **data, uint8_t *reqinst, uint16_t *flags,
Michal Vasko7fbc8162018-09-17 10:35:16 +02002071 struct lysp_ext_instance **exts)
2072{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002073 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002074 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002075 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002076 enum yang_keyword kw;
2077
2078 if (*flags & LYS_SET_REQINST) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002079 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002080 return LY_EVALID;
2081 }
2082 *flags |= LYS_SET_REQINST;
2083
2084 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002085 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002086
2087 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
2088 *reqinst = 1;
2089 } else if ((word_len != 5) || strncmp(word, "false", word_len)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002090 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002091 free(buf);
2092 return LY_EVALID;
2093 }
2094 free(buf);
2095
Radek Krejci6d6556c2018-11-08 09:37:45 +01002096 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002097 switch (kw) {
2098 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002099 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_REQINSTANCE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002100 break;
2101 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002102 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002103 return LY_EVALID;
2104 }
2105 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002106 return ret;
2107}
2108
Michal Vaskoea5abea2018-09-18 13:10:54 +02002109/**
2110 * @brief Parse the modifier statement. Substatement of type pattern statement.
2111 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002112 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002113 * @param[in,out] data Data to read from, always moved to currently handled character.
2114 * @param[in,out] pat Value to write to.
2115 * @param[in,out] exts Extension instances to add to.
2116 *
2117 * @return LY_ERR values.
2118 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002119static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002120parse_type_pattern_modifier(struct ly_parser_ctx *ctx, const char **data, const char **pat, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002121{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002122 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002123 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002124 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002125 enum yang_keyword kw;
2126
2127 if ((*pat)[0] == 0x15) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002128 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002129 return LY_EVALID;
2130 }
2131
2132 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002133 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002134
2135 if ((word_len != 12) || strncmp(word, "invert-match", word_len)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002136 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002137 free(buf);
2138 return LY_EVALID;
2139 }
2140 free(buf);
2141
2142 /* replace the value in the dictionary */
2143 buf = malloc(strlen(*pat) + 1);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002144 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002145 strcpy(buf, *pat);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002146 lydict_remove(ctx->ctx, *pat);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002147
2148 assert(buf[0] == 0x06);
2149 buf[0] = 0x15;
Radek Krejci44ceedc2018-10-02 15:54:31 +02002150 *pat = lydict_insert_zc(ctx->ctx, buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002151
Radek Krejci6d6556c2018-11-08 09:37:45 +01002152 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002153 switch (kw) {
2154 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002155 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MODIFIER, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002156 break;
2157 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002158 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002159 return LY_EVALID;
2160 }
2161 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002162 return ret;
2163}
2164
Michal Vaskoea5abea2018-09-18 13:10:54 +02002165/**
2166 * @brief Parse the pattern statement. Substatement of type statement.
2167 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002168 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002169 * @param[in,out] data Data to read from, always moved to currently handled character.
2170 * @param[in,out] patterns Restrictions to add to.
2171 *
2172 * @return LY_ERR values.
2173 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002174static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002175parse_type_pattern(struct ly_parser_ctx *ctx, const char **data, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002176{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002177 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002178 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002179 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002180 enum yang_keyword kw;
2181 struct lysp_restr *restr;
2182
Radek Krejci2c4e7172018-10-19 15:56:26 +02002183 LY_ARRAY_NEW_RET(ctx->ctx, *patterns, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002184
2185 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002186 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002187
2188 /* add special meaning first byte */
2189 if (buf) {
2190 buf = realloc(buf, word_len + 2);
2191 word = buf;
2192 } else {
2193 buf = malloc(word_len + 2);
2194 }
Radek Krejci44ceedc2018-10-02 15:54:31 +02002195 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejci86d106e2018-10-18 09:53:19 +02002196 memmove(buf + 1, word, word_len);
2197 buf[0] = 0x06; /* pattern's default regular-match flag */
2198 buf[word_len + 1] = '\0'; /* terminating NULL byte */
2199 restr->arg = lydict_insert_zc(ctx->ctx, buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002200
Radek Krejci6d6556c2018-11-08 09:37:45 +01002201 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002202 switch (kw) {
2203 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002204 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 +02002205 break;
2206 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002207 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 +02002208 break;
2209 case YANG_ERROR_APP_TAG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002210 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 +02002211 break;
2212 case YANG_ERROR_MESSAGE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002213 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 +02002214 break;
2215 case YANG_MODIFIER:
Radek Krejciceaf2122019-01-02 15:03:26 +01002216 YANG_CHECK_STMTVER2_RET(ctx, "modifier", "pattern");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002217 LY_CHECK_RET(parse_type_pattern_modifier(ctx, data, &restr->arg, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002218 break;
2219 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002220 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002221 break;
2222 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002223 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002224 return LY_EVALID;
2225 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002226 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002227 return ret;
2228}
2229
Michal Vaskoea5abea2018-09-18 13:10:54 +02002230/**
2231 * @brief Parse the type statement.
2232 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002233 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002234 * @param[in,out] data Data to read from, always moved to currently handled character.
2235 * @param[in,out] type Type to wrote to.
2236 *
2237 * @return LY_ERR values.
2238 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002239static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002240parse_type(struct ly_parser_ctx *ctx, const char **data, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002241{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002242 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002243 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002244 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002245 enum yang_keyword kw;
2246 struct lysp_type *nest_type;
2247
2248 if (type->name) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002249 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002250 return LY_EVALID;
2251 }
2252
2253 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002254 LY_CHECK_RET(get_argument(ctx, data, Y_PREF_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002255 INSERT_WORD(ctx, buf, type->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002256
Radek Krejci6d6556c2018-11-08 09:37:45 +01002257 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002258 switch (kw) {
2259 case YANG_BASE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002260 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 +01002261 type->flags |= LYS_SET_BASE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002262 break;
2263 case YANG_BIT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002264 LY_CHECK_RET(parse_type_enum(ctx, data, kw, &type->bits));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002265 type->flags |= LYS_SET_BIT;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002266 break;
2267 case YANG_ENUM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002268 LY_CHECK_RET(parse_type_enum(ctx, data, kw, &type->enums));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002269 type->flags |= LYS_SET_ENUM;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002270 break;
2271 case YANG_FRACTION_DIGITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002272 LY_CHECK_RET(parse_type_fracdigits(ctx, data, &type->fraction_digits, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002273 type->flags |= LYS_SET_FRDIGITS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002274 break;
2275 case YANG_LENGTH:
2276 if (type->length) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002277 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002278 return LY_EVALID;
2279 }
2280 type->length = calloc(1, sizeof *type->length);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002281 LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002282
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002283 LY_CHECK_RET(parse_restr(ctx, data, kw, type->length));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002284 type->flags |= LYS_SET_LENGTH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002285 break;
2286 case YANG_PATH:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002287 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 +01002288 type->flags |= LYS_SET_PATH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002289 break;
2290 case YANG_PATTERN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002291 LY_CHECK_RET(parse_type_pattern(ctx, data, &type->patterns));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002292 type->flags |= LYS_SET_PATTERN;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002293 break;
2294 case YANG_RANGE:
2295 if (type->range) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002296 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002297 return LY_EVALID;
2298 }
2299 type->range = calloc(1, sizeof *type->range);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002300 LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx->ctx), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002301
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002302 LY_CHECK_RET(parse_restr(ctx, data, kw, type->range));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002303 type->flags |= LYS_SET_RANGE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002304 break;
2305 case YANG_REQUIRE_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002306 LY_CHECK_RET(parse_type_reqinstance(ctx, data, &type->require_instance, &type->flags, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002307 /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002308 break;
2309 case YANG_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002310 LY_ARRAY_NEW_RET(ctx->ctx, type->types, nest_type, LY_EMEM);
2311 LY_CHECK_RET(parse_type(ctx, data, nest_type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002312 type->flags |= LYS_SET_TYPE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002313 break;
2314 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002315 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &type->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002316 break;
2317 default:
Radek Krejci8b764662018-11-14 14:15:13 +01002318 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002319 return LY_EVALID;
2320 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002321 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002322 return ret;
2323}
2324
Michal Vaskoea5abea2018-09-18 13:10:54 +02002325/**
2326 * @brief Parse the leaf statement.
2327 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002328 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002329 * @param[in,out] data Data to read from, always moved to currently handled character.
2330 * @param[in,out] siblings Siblings to add to.
2331 *
2332 * @return LY_ERR values.
2333 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002334static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002335parse_leaf(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002336{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002337 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002338 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002339 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002340 enum yang_keyword kw;
2341 struct lysp_node *iter;
2342 struct lysp_node_leaf *leaf;
2343
2344 /* create structure */
2345 leaf = calloc(1, sizeof *leaf);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002346 LY_CHECK_ERR_RET(!leaf, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002347 leaf->nodetype = LYS_LEAF;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002348 leaf->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002349
2350 /* insert into siblings */
2351 if (!*siblings) {
2352 *siblings = (struct lysp_node *)leaf;
2353 } else {
2354 for (iter = *siblings; iter->next; iter = iter->next);
2355 iter->next = (struct lysp_node *)leaf;
2356 }
2357
2358 /* get name */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002359 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002360 INSERT_WORD(ctx, buf, leaf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002361
2362 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01002363 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002364 switch (kw) {
2365 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002366 LY_CHECK_RET(parse_config(ctx, data, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002367 break;
2368 case YANG_DEFAULT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002369 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 +02002370 break;
2371 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002372 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 +02002373 break;
2374 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002375 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 +02002376 break;
2377 case YANG_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002378 LY_CHECK_RET(parse_mandatory(ctx, data, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002379 break;
2380 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002381 LY_CHECK_RET(parse_restrs(ctx, data, kw, &leaf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002382 break;
2383 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002384 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 +02002385 break;
2386 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002387 LY_CHECK_RET(parse_status(ctx, data, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002388 break;
2389 case YANG_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002390 LY_CHECK_RET(parse_type(ctx, data, &leaf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002391 break;
2392 case YANG_UNITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002393 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 +02002394 break;
2395 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002396 LY_CHECK_RET(parse_when(ctx, data, &leaf->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002397 break;
2398 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002399 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002400 break;
2401 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002402 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002403 return LY_EVALID;
2404 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002405 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002406 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002407checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002408 /* mandatory substatements */
2409 if (!leaf->type.name) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002410 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002411 return LY_EVALID;
2412 }
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002413 if ((leaf->flags & LYS_MAND_TRUE) && (leaf->dflt)) {
2414 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMSCOMB, "mandatory", "default", "leaf");
2415 return LY_EVALID;
2416 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002417
2418 return ret;
2419}
2420
Michal Vaskoea5abea2018-09-18 13:10:54 +02002421/**
2422 * @brief Parse the max-elements statement.
2423 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002424 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002425 * @param[in,out] data Data to read from, always moved to currently handled character.
2426 * @param[in,out] max Value to write to.
2427 * @param[in,out] flags Flags to write to.
2428 * @param[in,out] exts Extension instances to add to.
2429 *
2430 * @return LY_ERR values.
2431 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002432static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002433parse_maxelements(struct ly_parser_ctx *ctx, const char **data, uint32_t *max, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002434{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002435 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002436 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002437 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002438 unsigned long int num;
2439 enum yang_keyword kw;
2440
2441 if (*flags & LYS_SET_MAX) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002442 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002443 return LY_EVALID;
2444 }
2445 *flags |= LYS_SET_MAX;
2446
2447 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002448 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002449
2450 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002451 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002452 free(buf);
2453 return LY_EVALID;
2454 }
2455
2456 if (strncmp(word, "unbounded", word_len)) {
2457 errno = 0;
2458 num = strtoul(word, &ptr, 10);
2459 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002460 if ((size_t)(ptr - word) != word_len) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002461 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002462 free(buf);
2463 return LY_EVALID;
2464 }
2465 if ((errno == ERANGE) || (num > UINT32_MAX)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002466 LOGVAL_YANG(ctx, LY_VCODE_OOB, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002467 free(buf);
2468 return LY_EVALID;
2469 }
2470
2471 *max = num;
2472 }
2473 free(buf);
2474
Radek Krejci6d6556c2018-11-08 09:37:45 +01002475 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002476 switch (kw) {
2477 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002478 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MAX, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002479 break;
2480 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002481 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002482 return LY_EVALID;
2483 }
2484 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002485 return ret;
2486}
2487
Michal Vaskoea5abea2018-09-18 13:10:54 +02002488/**
2489 * @brief Parse the min-elements statement.
2490 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002491 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002492 * @param[in,out] data Data to read from, always moved to currently handled character.
2493 * @param[in,out] min Value to write to.
2494 * @param[in,out] flags Flags to write to.
2495 * @param[in,out] exts Extension instances to add to.
2496 *
2497 * @return LY_ERR values.
2498 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002499static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002500parse_minelements(struct ly_parser_ctx *ctx, const char **data, uint32_t *min, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002501{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002502 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002503 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002504 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002505 unsigned long int num;
2506 enum yang_keyword kw;
2507
2508 if (*flags & LYS_SET_MIN) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002509 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002510 return LY_EVALID;
2511 }
2512 *flags |= LYS_SET_MIN;
2513
2514 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002515 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002516
2517 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002518 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002519 free(buf);
2520 return LY_EVALID;
2521 }
2522
2523 errno = 0;
2524 num = strtoul(word, &ptr, 10);
2525 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002526 if ((size_t)(ptr - word) != word_len) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002527 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002528 free(buf);
2529 return LY_EVALID;
2530 }
2531 if ((errno == ERANGE) || (num > UINT32_MAX)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002532 LOGVAL_YANG(ctx, LY_VCODE_OOB, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002533 free(buf);
2534 return LY_EVALID;
2535 }
2536 *min = num;
2537 free(buf);
2538
Radek Krejci6d6556c2018-11-08 09:37:45 +01002539 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002540 switch (kw) {
2541 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002542 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MIN, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002543 break;
2544 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002545 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002546 return LY_EVALID;
2547 }
2548 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002549 return ret;
2550}
2551
Michal Vaskoea5abea2018-09-18 13:10:54 +02002552/**
2553 * @brief Parse the ordered-by statement.
2554 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002555 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002556 * @param[in,out] data Data to read from, always moved to currently handled character.
2557 * @param[in,out] flags Flags to write to.
2558 * @param[in,out] exts Extension instances to add to.
2559 *
2560 * @return LY_ERR values.
2561 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002562static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002563parse_orderedby(struct ly_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002564{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002565 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002566 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002567 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002568 enum yang_keyword kw;
2569
2570 if (*flags & LYS_ORDBY_MASK) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002571 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002572 return LY_EVALID;
2573 }
2574
2575 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002576 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002577
2578 if ((word_len == 6) && !strncmp(word, "system", word_len)) {
2579 *flags |= LYS_ORDBY_SYSTEM;
2580 } else if ((word_len == 4) && !strncmp(word, "user", word_len)) {
2581 *flags |= LYS_ORDBY_USER;
2582 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002583 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002584 free(buf);
2585 return LY_EVALID;
2586 }
2587 free(buf);
2588
Radek Krejci6d6556c2018-11-08 09:37:45 +01002589 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002590 switch (kw) {
2591 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002592 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_ORDEREDBY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002593 break;
2594 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002595 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002596 return LY_EVALID;
2597 }
2598 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002599 return ret;
2600}
2601
Michal Vaskoea5abea2018-09-18 13:10:54 +02002602/**
2603 * @brief Parse the leaf-list statement.
2604 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002605 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002606 * @param[in,out] data Data to read from, always moved to currently handled character.
2607 * @param[in,out] siblings Siblings to add to.
2608 *
2609 * @return LY_ERR values.
2610 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002611static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002612parse_leaflist(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002613{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002614 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002615 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002616 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002617 enum yang_keyword kw;
2618 struct lysp_node *iter;
2619 struct lysp_node_leaflist *llist;
2620
2621 /* create structure */
2622 llist = calloc(1, sizeof *llist);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002623 LY_CHECK_ERR_RET(!llist, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002624 llist->nodetype = LYS_LEAFLIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002625 llist->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002626
2627 /* insert into siblings */
2628 if (!*siblings) {
2629 *siblings = (struct lysp_node *)llist;
2630 } else {
2631 for (iter = *siblings; iter->next; iter = iter->next);
2632 iter->next = (struct lysp_node *)llist;
2633 }
2634
2635 /* get name */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002636 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002637 INSERT_WORD(ctx, buf, llist->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002638
2639 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01002640 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002641 switch (kw) {
2642 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002643 LY_CHECK_RET(parse_config(ctx, data, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002644 break;
2645 case YANG_DEFAULT:
Radek Krejciceaf2122019-01-02 15:03:26 +01002646 YANG_CHECK_STMTVER2_RET(ctx, "default", "leaf-list");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002647 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 +02002648 break;
2649 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002650 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 +02002651 break;
2652 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002653 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 +02002654 break;
2655 case YANG_MAX_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002656 LY_CHECK_RET(parse_maxelements(ctx, data, &llist->max, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002657 break;
2658 case YANG_MIN_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002659 LY_CHECK_RET(parse_minelements(ctx, data, &llist->min, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002660 break;
2661 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002662 LY_CHECK_RET(parse_restrs(ctx, data, kw, &llist->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002663 break;
2664 case YANG_ORDERED_BY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002665 LY_CHECK_RET(parse_orderedby(ctx, data, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002666 break;
2667 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002668 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 +02002669 break;
2670 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002671 LY_CHECK_RET(parse_status(ctx, data, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002672 break;
2673 case YANG_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002674 LY_CHECK_RET(parse_type(ctx, data, &llist->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002675 break;
2676 case YANG_UNITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002677 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 +02002678 break;
2679 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002680 LY_CHECK_RET(parse_when(ctx, data, &llist->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002681 break;
2682 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002683 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002684 break;
2685 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002686 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002687 return LY_EVALID;
2688 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002689 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002690 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002691checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002692 /* mandatory substatements */
2693 if (!llist->type.name) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002694 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002695 return LY_EVALID;
2696 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01002697 if ((llist->min) && (llist->dflts)) {
2698 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMSCOMB, "min-elements", "default", "leaf-list");
2699 return LY_EVALID;
2700 }
Radek Krejcidf6cad12018-11-28 17:10:55 +01002701 if (llist->max && llist->min > llist->max) {
2702 LOGVAL_YANG(ctx, LYVE_SEMANTICS,
2703 "Invalid combination of min-elements and max-elements: min value %u is bigger than the max value %u.",
2704 llist->min, llist->max);
2705 return LY_EVALID;
2706 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002707
2708 return ret;
2709}
2710
Michal Vaskoea5abea2018-09-18 13:10:54 +02002711/**
2712 * @brief Parse the refine statement.
2713 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002714 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002715 * @param[in,out] data Data to read from, always moved to currently handled character.
2716 * @param[in,out] refines Refines to add to.
2717 *
2718 * @return LY_ERR values.
2719 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002720static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002721parse_refine(struct ly_parser_ctx *ctx, const char **data, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002722{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002723 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002724 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002725 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002726 enum yang_keyword kw;
2727 struct lysp_refine *rf;
2728
Radek Krejci2c4e7172018-10-19 15:56:26 +02002729 LY_ARRAY_NEW_RET(ctx->ctx, *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002730
2731 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002732 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002733 INSERT_WORD(ctx, buf, rf->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002734
Radek Krejci6d6556c2018-11-08 09:37:45 +01002735 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002736 switch (kw) {
2737 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002738 LY_CHECK_RET(parse_config(ctx, data, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002739 break;
2740 case YANG_DEFAULT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002741 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 +02002742 break;
2743 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002744 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 +02002745 break;
2746 case YANG_IF_FEATURE:
Radek Krejciceaf2122019-01-02 15:03:26 +01002747 YANG_CHECK_STMTVER2_RET(ctx, "if-feature", "refine");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002748 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 +02002749 break;
2750 case YANG_MAX_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002751 LY_CHECK_RET(parse_maxelements(ctx, data, &rf->max, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002752 break;
2753 case YANG_MIN_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002754 LY_CHECK_RET(parse_minelements(ctx, data, &rf->min, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002755 break;
2756 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002757 LY_CHECK_RET(parse_restrs(ctx, data, kw, &rf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002758 break;
2759 case YANG_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002760 LY_CHECK_RET(parse_mandatory(ctx, data, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002761 break;
2762 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002763 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 +02002764 break;
2765 case YANG_PRESENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002766 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 +02002767 break;
2768 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002769 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002770 break;
2771 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002772 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002773 return LY_EVALID;
2774 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002775 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002776 return ret;
2777}
2778
Michal Vaskoea5abea2018-09-18 13:10:54 +02002779/**
2780 * @brief Parse the typedef statement.
2781 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002782 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002783 * @param[in,out] data Data to read from, always moved to currently handled character.
2784 * @param[in,out] typedefs Typedefs to add to.
2785 *
2786 * @return LY_ERR values.
2787 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002788static LY_ERR
Radek Krejcibbe09a92018-11-08 09:36:54 +01002789parse_typedef(struct ly_parser_ctx *ctx, struct lysp_node *parent, const char **data, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002790{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002791 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002792 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002793 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002794 enum yang_keyword kw;
2795 struct lysp_tpdf *tpdf;
2796
Radek Krejci2c4e7172018-10-19 15:56:26 +02002797 LY_ARRAY_NEW_RET(ctx->ctx, *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002798
2799 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002800 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002801 INSERT_WORD(ctx, buf, tpdf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002802
2803 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01002804 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002805 switch (kw) {
2806 case YANG_DEFAULT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002807 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 +02002808 break;
2809 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002810 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 +02002811 break;
2812 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002813 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 +02002814 break;
2815 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002816 LY_CHECK_RET(parse_status(ctx, data, &tpdf->flags, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002817 break;
2818 case YANG_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002819 LY_CHECK_RET(parse_type(ctx, data, &tpdf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002820 break;
2821 case YANG_UNITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002822 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 +02002823 break;
2824 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002825 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002826 break;
2827 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002828 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002829 return LY_EVALID;
2830 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002831 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002832 LY_CHECK_RET(ret);
Radek Krejcibbe09a92018-11-08 09:36:54 +01002833checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002834 /* mandatory substatements */
2835 if (!tpdf->type.name) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002836 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002837 return LY_EVALID;
2838 }
2839
Radek Krejcibbe09a92018-11-08 09:36:54 +01002840 /* store data for collision check */
2841 if (parent) {
2842 ly_set_add(&ctx->tpdfs_nodes, parent, 0);
2843 }
2844
Michal Vasko7fbc8162018-09-17 10:35:16 +02002845 return ret;
2846}
2847
Michal Vaskoea5abea2018-09-18 13:10:54 +02002848/**
2849 * @brief Parse the input or output statement.
2850 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002851 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002852 * @param[in,out] data Data to read from, always moved to currently handled character.
2853 * @param[in] kw Type of this particular keyword
2854 * @param[in,out] inout_p Input/output pointer to write to.
2855 *
2856 * @return LY_ERR values.
2857 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002858static LY_ERR
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002859parse_inout(struct ly_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 +02002860{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002861 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002862 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002863 size_t word_len;
Radek Krejci10113652018-11-14 16:56:50 +01002864 enum yang_keyword kw;
Radek Krejcie86bf772018-12-14 11:39:53 +01002865 unsigned int u;
2866 struct lysp_node *child;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002867
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002868 if (inout_p->nodetype) {
Radek Krejci10113652018-11-14 16:56:50 +01002869 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002870 return LY_EVALID;
2871 }
2872
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002873 /* initiate structure */
2874 inout_p->nodetype = LYS_INOUT;
2875 inout_p->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002876
2877 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01002878 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002879 switch (kw) {
2880 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01002881 YANG_CHECK_STMTVER2_RET(ctx, "anydata", ly_stmt2str(inout_kw));
Radek Krejci10113652018-11-14 16:56:50 +01002882 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002883 case YANG_ANYXML:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002884 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002885 break;
2886 case YANG_CHOICE:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002887 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002888 break;
2889 case YANG_CONTAINER:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002890 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002891 break;
2892 case YANG_LEAF:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002893 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002894 break;
2895 case YANG_LEAF_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002896 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002897 break;
2898 case YANG_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002899 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002900 break;
2901 case YANG_USES:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002902 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002903 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002904 case YANG_TYPEDEF:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002905 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)inout_p, data, &inout_p->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002906 break;
2907 case YANG_MUST:
Radek Krejciceaf2122019-01-02 15:03:26 +01002908 YANG_CHECK_STMTVER2_RET(ctx, "must", ly_stmt2str(inout_kw));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002909 LY_CHECK_RET(parse_restrs(ctx, data, kw, &inout_p->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002910 break;
2911 case YANG_GROUPING:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002912 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)inout_p, &inout_p->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002913 break;
2914 case YANG_CUSTOM:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002915 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inout_p->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002916 break;
2917 default:
Radek Krejci10113652018-11-14 16:56:50 +01002918 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002919 return LY_EVALID;
2920 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002921 }
Radek Krejcie86bf772018-12-14 11:39:53 +01002922 /* finalize parent pointers to the reallocated items */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002923 LY_ARRAY_FOR(inout_p->groupings, u) {
2924 LY_LIST_FOR(inout_p->groupings[u].data, child) {
2925 child->parent = (struct lysp_node*)&inout_p->groupings[u];
Radek Krejcie86bf772018-12-14 11:39:53 +01002926 }
2927 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002928 return ret;
2929}
2930
Michal Vaskoea5abea2018-09-18 13:10:54 +02002931/**
2932 * @brief Parse the action statement.
2933 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002934 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002935 * @param[in,out] data Data to read from, always moved to currently handled character.
2936 * @param[in,out] actions Actions to add to.
2937 *
2938 * @return LY_ERR values.
2939 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002940static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002941parse_action(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002942{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002943 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002944 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002945 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002946 enum yang_keyword kw;
2947 struct lysp_action *act;
Radek Krejcie86bf772018-12-14 11:39:53 +01002948 struct lysp_node *child;
2949 unsigned int u;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002950
Radek Krejci2c4e7172018-10-19 15:56:26 +02002951 LY_ARRAY_NEW_RET(ctx->ctx, *actions, act, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002952
2953 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002954 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002955 INSERT_WORD(ctx, buf, act->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002956 act->nodetype = LYS_ACTION;
2957 act->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002958
Radek Krejci6d6556c2018-11-08 09:37:45 +01002959 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002960 switch (kw) {
2961 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002962 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 +02002963 break;
2964 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002965 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 +02002966 break;
2967 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002968 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 +02002969 break;
2970 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002971 LY_CHECK_RET(parse_status(ctx, data, &act->flags, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002972 break;
2973
2974 case YANG_INPUT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002975 LY_CHECK_RET(parse_inout(ctx, data, kw, (struct lysp_node*)act, &act->input));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002976 break;
2977 case YANG_OUTPUT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002978 LY_CHECK_RET(parse_inout(ctx, data, kw, (struct lysp_node*)act, &act->output));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002979 break;
2980
2981 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01002982 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)act, data, &act->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002983 break;
2984 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002985 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)act, &act->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002986 break;
2987 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002988 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002989 break;
2990 default:
Radek Krejcif538ce52019-03-05 10:46:14 +01002991 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), parent ? "action" : "rpc");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002992 return LY_EVALID;
2993 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002994 }
Radek Krejcie86bf772018-12-14 11:39:53 +01002995 /* finalize parent pointers to the reallocated items */
2996 LY_ARRAY_FOR(act->groupings, u) {
2997 LY_LIST_FOR(act->groupings[u].data, child) {
2998 child->parent = (struct lysp_node*)&act->groupings[u];
2999 }
3000 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003001 return ret;
3002}
3003
Michal Vaskoea5abea2018-09-18 13:10:54 +02003004/**
3005 * @brief Parse the notification statement.
3006 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003007 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003008 * @param[in,out] data Data to read from, always moved to currently handled character.
3009 * @param[in,out] notifs Notifications to add to.
3010 *
3011 * @return LY_ERR values.
3012 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003013static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003014parse_notif(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003015{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003016 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003017 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003018 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003019 enum yang_keyword kw;
3020 struct lysp_notif *notif;
Radek Krejcie86bf772018-12-14 11:39:53 +01003021 struct lysp_node *child;
3022 unsigned int u;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003023
Radek Krejci2c4e7172018-10-19 15:56:26 +02003024 LY_ARRAY_NEW_RET(ctx->ctx, *notifs, notif, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003025
3026 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003027 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003028 INSERT_WORD(ctx, buf, notif->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003029 notif->nodetype = LYS_NOTIF;
3030 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003031
Radek Krejci6d6556c2018-11-08 09:37:45 +01003032 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003033 switch (kw) {
3034 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003035 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 +02003036 break;
3037 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003038 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 +02003039 break;
3040 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003041 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 +02003042 break;
3043 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003044 LY_CHECK_RET(parse_status(ctx, data, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003045 break;
3046
3047 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01003048 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "notification");
Radek Krejci10113652018-11-14 16:56:50 +01003049 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003050 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003051 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003052 break;
3053 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003054 LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003055 break;
3056 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003057 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003058 break;
3059 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003060 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003061 break;
3062 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003063 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003064 break;
3065 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003066 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003067 break;
3068 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003069 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003070 break;
3071
3072 case YANG_MUST:
Radek Krejciceaf2122019-01-02 15:03:26 +01003073 YANG_CHECK_STMTVER2_RET(ctx, "must", "notification");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003074 LY_CHECK_RET(parse_restrs(ctx, data, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003075 break;
3076 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01003077 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)notif, data, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003078 break;
3079 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003080 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003081 break;
3082 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003083 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003084 break;
3085 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003086 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003087 return LY_EVALID;
3088 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003089 }
Radek Krejcie86bf772018-12-14 11:39:53 +01003090 /* finalize parent pointers to the reallocated items */
3091 LY_ARRAY_FOR(notif->groupings, u) {
3092 LY_LIST_FOR(notif->groupings[u].data, child) {
3093 child->parent = (struct lysp_node*)&notif->groupings[u];
3094 }
3095 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003096 return ret;
3097}
3098
Michal Vaskoea5abea2018-09-18 13:10:54 +02003099/**
3100 * @brief Parse the grouping statement.
3101 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003102 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003103 * @param[in,out] data Data to read from, always moved to currently handled character.
3104 * @param[in,out] groupings Groupings to add to.
3105 *
3106 * @return LY_ERR values.
3107 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003108static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003109parse_grouping(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003110{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003111 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003112 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003113 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003114 enum yang_keyword kw;
3115 struct lysp_grp *grp;
Radek Krejcie86bf772018-12-14 11:39:53 +01003116 struct lysp_node *child;
3117 unsigned int u;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003118
Radek Krejci2c4e7172018-10-19 15:56:26 +02003119 LY_ARRAY_NEW_RET(ctx->ctx, *groupings, grp, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003120
3121 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003122 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003123 INSERT_WORD(ctx, buf, grp->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003124 grp->nodetype = LYS_GROUPING;
3125 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003126
Radek Krejci6d6556c2018-11-08 09:37:45 +01003127 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003128 switch (kw) {
3129 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003130 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 +02003131 break;
3132 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003133 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 +02003134 break;
3135 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003136 LY_CHECK_RET(parse_status(ctx, data, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003137 break;
3138
3139 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01003140 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "grouping");
Radek Krejci10113652018-11-14 16:56:50 +01003141 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003142 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003143 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003144 break;
3145 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003146 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003147 break;
3148 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003149 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003150 break;
3151 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003152 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003153 break;
3154 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003155 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003156 break;
3157 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003158 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003159 break;
3160 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003161 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003162 break;
3163
3164 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01003165 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)grp, data, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003166 break;
3167 case YANG_ACTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003168 YANG_CHECK_STMTVER2_RET(ctx, "action", "grouping");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003169 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)grp, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003170 break;
3171 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003172 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)grp, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003173 break;
3174 case YANG_NOTIFICATION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003175 YANG_CHECK_STMTVER2_RET(ctx, "notification", "grouping");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003176 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)grp, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003177 break;
3178 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003179 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003180 break;
3181 default:
Radek Krejci10113652018-11-14 16:56:50 +01003182 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003183 return LY_EVALID;
3184 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003185 }
Radek Krejcie86bf772018-12-14 11:39:53 +01003186 /* finalize parent pointers to the reallocated items */
3187 LY_ARRAY_FOR(grp->groupings, u) {
3188 LY_LIST_FOR(grp->groupings[u].data, child) {
3189 child->parent = (struct lysp_node*)&grp->groupings[u];
3190 }
3191 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003192 return ret;
3193}
3194
Michal Vaskoea5abea2018-09-18 13:10:54 +02003195/**
3196 * @brief Parse the refine statement.
3197 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003198 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003199 * @param[in,out] data Data to read from, always moved to currently handled character.
3200 * @param[in,out] augments Augments to add to.
3201 *
3202 * @return LY_ERR values.
3203 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003204static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003205parse_augment(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003206{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003207 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003208 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003209 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003210 enum yang_keyword kw;
3211 struct lysp_augment *aug;
3212
Radek Krejci2c4e7172018-10-19 15:56:26 +02003213 LY_ARRAY_NEW_RET(ctx->ctx, *augments, aug, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003214
3215 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003216 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003217 INSERT_WORD(ctx, buf, aug->nodeid, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003218 aug->nodetype = LYS_AUGMENT;
3219 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003220
Radek Krejci6d6556c2018-11-08 09:37:45 +01003221 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003222 switch (kw) {
3223 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003224 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 +02003225 break;
3226 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003227 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 +02003228 break;
3229 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003230 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 +02003231 break;
3232 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003233 LY_CHECK_RET(parse_status(ctx, data, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003234 break;
3235 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003236 LY_CHECK_RET(parse_when(ctx, data, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003237 break;
3238
3239 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01003240 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "augment");
Radek Krejci10113652018-11-14 16:56:50 +01003241 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003242 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003243 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003244 break;
3245 case YANG_CASE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003246 LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003247 break;
3248 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003249 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003250 break;
3251 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003252 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003253 break;
3254 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003255 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003256 break;
3257 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003258 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003259 break;
3260 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003261 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003262 break;
3263 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003264 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003265 break;
3266
3267 case YANG_ACTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003268 YANG_CHECK_STMTVER2_RET(ctx, "action", "augment");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003269 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003270 break;
3271 case YANG_NOTIFICATION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003272 YANG_CHECK_STMTVER2_RET(ctx, "notification", "augment");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003273 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003274 break;
3275 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003276 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003277 break;
3278 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003279 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003280 return LY_EVALID;
3281 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003282 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003283 return ret;
3284}
3285
Michal Vaskoea5abea2018-09-18 13:10:54 +02003286/**
3287 * @brief Parse the uses statement.
3288 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003289 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003290 * @param[in,out] data Data to read from, always moved to currently handled character.
3291 * @param[in,out] siblings Siblings to add to.
3292 *
3293 * @return LY_ERR values.
3294 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003295static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003296parse_uses(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003297{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003298 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003299 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003300 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003301 enum yang_keyword kw;
3302 struct lysp_node *iter;
3303 struct lysp_node_uses *uses;
3304
3305 /* create structure */
3306 uses = calloc(1, sizeof *uses);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003307 LY_CHECK_ERR_RET(!uses, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003308 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003309 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003310
3311 /* insert into siblings */
3312 if (!*siblings) {
3313 *siblings = (struct lysp_node *)uses;
3314 } else {
3315 for (iter = *siblings; iter->next; iter = iter->next);
3316 iter->next = (struct lysp_node *)uses;
3317 }
3318
3319 /* get name */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003320 LY_CHECK_RET(get_argument(ctx, data, Y_PREF_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003321 INSERT_WORD(ctx, buf, uses->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003322
3323 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01003324 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003325 switch (kw) {
3326 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003327 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 +02003328 break;
3329 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003330 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 +02003331 break;
3332 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003333 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 +02003334 break;
3335 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003336 LY_CHECK_RET(parse_status(ctx, data, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003337 break;
3338 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003339 LY_CHECK_RET(parse_when(ctx, data, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003340 break;
3341
3342 case YANG_REFINE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003343 LY_CHECK_RET(parse_refine(ctx, data, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003344 break;
3345 case YANG_AUGMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003346 LY_CHECK_RET(parse_augment(ctx, data, (struct lysp_node*)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003347 break;
3348 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003349 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003350 break;
3351 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003352 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003353 return LY_EVALID;
3354 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003355 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003356 return ret;
3357}
3358
Michal Vaskoea5abea2018-09-18 13:10:54 +02003359/**
3360 * @brief Parse the case statement.
3361 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003362 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003363 * @param[in,out] data Data to read from, always moved to currently handled character.
3364 * @param[in,out] siblings Siblings to add to.
3365 *
3366 * @return LY_ERR values.
3367 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003368static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003369parse_case(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003370{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003371 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003372 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003373 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003374 enum yang_keyword kw;
3375 struct lysp_node *iter;
3376 struct lysp_node_case *cas;
3377
3378 /* create structure */
3379 cas = calloc(1, sizeof *cas);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003380 LY_CHECK_ERR_RET(!cas, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003381 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003382 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003383
3384 /* insert into siblings */
3385 if (!*siblings) {
3386 *siblings = (struct lysp_node *)cas;
3387 } else {
3388 for (iter = *siblings; iter->next; iter = iter->next);
3389 iter->next = (struct lysp_node *)cas;
3390 }
3391
3392 /* get name */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003393 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003394 INSERT_WORD(ctx, buf, cas->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003395
3396 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01003397 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003398 switch (kw) {
3399 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003400 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 +02003401 break;
3402 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003403 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 +02003404 break;
3405 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003406 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 +02003407 break;
3408 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003409 LY_CHECK_RET(parse_status(ctx, data, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003410 break;
3411 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003412 LY_CHECK_RET(parse_when(ctx, data, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003413 break;
3414
3415 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01003416 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "case");
Radek Krejci10113652018-11-14 16:56:50 +01003417 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003418 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003419 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003420 break;
3421 case YANG_CHOICE:
Radek Krejcia9026eb2018-12-12 16:04:47 +01003422 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003423 break;
3424 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003425 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003426 break;
3427 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003428 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003429 break;
3430 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003431 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003432 break;
3433 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003434 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003435 break;
3436 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003437 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003438 break;
3439 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003440 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003441 break;
3442 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003443 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003444 return LY_EVALID;
3445 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003446 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003447 return ret;
3448}
3449
Michal Vaskoea5abea2018-09-18 13:10:54 +02003450/**
3451 * @brief Parse the choice statement.
3452 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003453 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003454 * @param[in,out] data Data to read from, always moved to currently handled character.
3455 * @param[in,out] siblings Siblings to add to.
3456 *
3457 * @return LY_ERR values.
3458 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003459static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003460parse_choice(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003461{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003462 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003463 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003464 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003465 enum yang_keyword kw;
3466 struct lysp_node *iter;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003467 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003468
3469 /* create structure */
Radek Krejci44ceedc2018-10-02 15:54:31 +02003470 choice = calloc(1, sizeof *choice);
3471 LY_CHECK_ERR_RET(!choice, LOGMEM(ctx->ctx), LY_EMEM);
3472 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003473 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003474
3475 /* insert into siblings */
3476 if (!*siblings) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02003477 *siblings = (struct lysp_node *)choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003478 } else {
3479 for (iter = *siblings; iter->next; iter = iter->next);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003480 iter->next = (struct lysp_node *)choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003481 }
3482
3483 /* get name */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003484 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003485 INSERT_WORD(ctx, buf, choice->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003486
3487 /* parse substatements */
Radek Krejcia9026eb2018-12-12 16:04:47 +01003488 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003489 switch (kw) {
3490 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003491 LY_CHECK_RET(parse_config(ctx, data, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003492 break;
3493 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003494 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 +02003495 break;
3496 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003497 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 +02003498 break;
3499 case YANG_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003500 LY_CHECK_RET(parse_mandatory(ctx, data, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003501 break;
3502 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003503 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 +02003504 break;
3505 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003506 LY_CHECK_RET(parse_status(ctx, data, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003507 break;
3508 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003509 LY_CHECK_RET(parse_when(ctx, data, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003510 break;
3511 case YANG_DEFAULT:
Radek Krejcia9026eb2018-12-12 16:04:47 +01003512 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 +02003513 break;
3514
3515 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01003516 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "choice");
Radek Krejci10113652018-11-14 16:56:50 +01003517 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003518 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003519 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003520 break;
3521 case YANG_CASE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003522 LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003523 break;
3524 case YANG_CHOICE:
Radek Krejciceaf2122019-01-02 15:03:26 +01003525 YANG_CHECK_STMTVER2_RET(ctx, "choice", "choice");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003526 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003527 break;
3528 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003529 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003530 break;
3531 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003532 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003533 break;
3534 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003535 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003536 break;
3537 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003538 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003539 break;
3540 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003541 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003542 break;
3543 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003544 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003545 return LY_EVALID;
3546 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003547 }
Radek Krejcia9026eb2018-12-12 16:04:47 +01003548 LY_CHECK_RET(ret);
3549checks:
3550 if ((choice->flags & LYS_MAND_TRUE) && choice->dflt) {
3551 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMSCOMB, "mandatory", "default", "choice");
3552 return LY_EVALID;
3553 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003554 return ret;
3555}
3556
Michal Vaskoea5abea2018-09-18 13:10:54 +02003557/**
3558 * @brief Parse the container statement.
3559 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003560 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003561 * @param[in,out] data Data to read from, always moved to currently handled character.
3562 * @param[in,out] siblings Siblings to add to.
3563 *
3564 * @return LY_ERR values.
3565 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003566static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003567parse_container(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003568{
3569 LY_ERR ret = 0;
3570 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003571 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003572 enum yang_keyword kw;
3573 struct lysp_node *iter;
3574 struct lysp_node_container *cont;
Radek Krejcie86bf772018-12-14 11:39:53 +01003575 unsigned int u;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003576
3577 /* create structure */
3578 cont = calloc(1, sizeof *cont);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003579 LY_CHECK_ERR_RET(!cont, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003580 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003581 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003582
3583 /* insert into siblings */
3584 if (!*siblings) {
3585 *siblings = (struct lysp_node *)cont;
3586 } else {
3587 for (iter = *siblings; iter->next; iter = iter->next);
3588 iter->next = (struct lysp_node *)cont;
3589 }
3590
3591 /* get name */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003592 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003593 INSERT_WORD(ctx, buf, cont->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003594
3595 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01003596 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003597 switch (kw) {
3598 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003599 LY_CHECK_RET(parse_config(ctx, data, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003600 break;
3601 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003602 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 +02003603 break;
3604 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003605 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 +02003606 break;
3607 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003608 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 +02003609 break;
3610 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003611 LY_CHECK_RET(parse_status(ctx, data, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003612 break;
3613 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003614 LY_CHECK_RET(parse_when(ctx, data, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003615 break;
3616 case YANG_PRESENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003617 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 +02003618 break;
3619
3620 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01003621 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "container");
Radek Krejci10113652018-11-14 16:56:50 +01003622 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003623 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003624 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003625 break;
3626 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003627 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003628 break;
3629 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003630 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003631 break;
3632 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003633 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003634 break;
3635 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003636 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003637 break;
3638 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003639 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003640 break;
3641 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003642 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003643 break;
3644
3645 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01003646 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)cont, data, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003647 break;
3648 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003649 LY_CHECK_RET(parse_restrs(ctx, data, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003650 break;
3651 case YANG_ACTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003652 YANG_CHECK_STMTVER2_RET(ctx, "action", "container");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003653 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003654 break;
3655 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003656 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003657 break;
3658 case YANG_NOTIFICATION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003659 YANG_CHECK_STMTVER2_RET(ctx, "notification", "container");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003660 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003661 break;
3662 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003663 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003664 break;
3665 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003666 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003667 return LY_EVALID;
3668 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003669 }
Radek Krejcie86bf772018-12-14 11:39:53 +01003670 /* finalize parent pointers to the reallocated items */
3671 LY_ARRAY_FOR(cont->groupings, u) {
3672 LY_LIST_FOR(cont->groupings[u].data, iter) {
3673 iter->parent = (struct lysp_node*)&cont->groupings[u];
3674 }
3675 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003676 return ret;
3677}
3678
Michal Vaskoea5abea2018-09-18 13:10:54 +02003679/**
3680 * @brief Parse the list statement.
3681 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003682 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003683 * @param[in,out] data Data to read from, always moved to currently handled character.
3684 * @param[in,out] siblings Siblings to add to.
3685 *
3686 * @return LY_ERR values.
3687 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003688static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003689parse_list(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003690{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003691 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003692 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003693 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003694 enum yang_keyword kw;
3695 struct lysp_node *iter;
3696 struct lysp_node_list *list;
Radek Krejcie86bf772018-12-14 11:39:53 +01003697 unsigned int u;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003698
3699 /* create structure */
3700 list = calloc(1, sizeof *list);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003701 LY_CHECK_ERR_RET(!list, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003702 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003703 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003704
3705 /* insert into siblings */
3706 if (!*siblings) {
3707 *siblings = (struct lysp_node *)list;
3708 } else {
3709 for (iter = *siblings; iter->next; iter = iter->next);
3710 iter->next = (struct lysp_node *)list;
3711 }
3712
3713 /* get name */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003714 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003715 INSERT_WORD(ctx, buf, list->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003716
3717 /* parse substatements */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003718 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003719 switch (kw) {
3720 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003721 LY_CHECK_RET(parse_config(ctx, data, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003722 break;
3723 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003724 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 +02003725 break;
3726 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003727 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 +02003728 break;
3729 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003730 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 +02003731 break;
3732 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003733 LY_CHECK_RET(parse_status(ctx, data, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003734 break;
3735 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003736 LY_CHECK_RET(parse_when(ctx, data, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003737 break;
3738 case YANG_KEY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003739 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 +02003740 break;
3741 case YANG_MAX_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003742 LY_CHECK_RET(parse_maxelements(ctx, data, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003743 break;
3744 case YANG_MIN_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003745 LY_CHECK_RET(parse_minelements(ctx, data, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003746 break;
3747 case YANG_ORDERED_BY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003748 LY_CHECK_RET(parse_orderedby(ctx, data, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003749 break;
3750 case YANG_UNIQUE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003751 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 +02003752 break;
3753
3754 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01003755 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "list");
Radek Krejci10113652018-11-14 16:56:50 +01003756 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003757 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003758 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003759 break;
3760 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003761 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003762 break;
3763 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003764 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003765 break;
3766 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003767 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003768 break;
3769 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003770 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003771 break;
3772 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003773 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003774 break;
3775 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003776 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003777 break;
3778
3779 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01003780 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)list, data, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003781 break;
3782 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003783 LY_CHECK_RET(parse_restrs(ctx, data, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003784 break;
3785 case YANG_ACTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003786 YANG_CHECK_STMTVER2_RET(ctx, "action", "list");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003787 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003788 break;
3789 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003790 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003791 break;
3792 case YANG_NOTIFICATION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003793 YANG_CHECK_STMTVER2_RET(ctx, "notification", "list");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003794 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003795 break;
3796 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003797 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003798 break;
3799 default:
Radek Krejci10113652018-11-14 16:56:50 +01003800 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003801 return LY_EVALID;
3802 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003803 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003804 LY_CHECK_RET(ret);
Radek Krejcie86bf772018-12-14 11:39:53 +01003805 /* finalize parent pointers to the reallocated items */
3806 LY_ARRAY_FOR(list->groupings, u) {
3807 LY_LIST_FOR(list->groupings[u].data, iter) {
3808 iter->parent = (struct lysp_node*)&list->groupings[u];
3809 }
3810 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003811checks:
3812 if (list->max && list->min > list->max) {
3813 LOGVAL_YANG(ctx, LYVE_SEMANTICS,
3814 "Invalid combination of min-elements and max-elements: min value %u is bigger than the max value %u.",
3815 list->min, list->max);
3816 return LY_EVALID;
3817 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003818
3819 return ret;
3820}
3821
Michal Vaskoea5abea2018-09-18 13:10:54 +02003822/**
3823 * @brief Parse the yin-element statement.
3824 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003825 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003826 * @param[in,out] data Data to read from, always moved to currently handled character.
3827 * @param[in,out] flags Flags to write to.
3828 * @param[in,out] exts Extension instances to add to.
3829 *
3830 * @return LY_ERR values.
3831 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003832static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003833parse_yinelement(struct ly_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003834{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003835 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003836 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003837 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003838 enum yang_keyword kw;
3839
3840 if (*flags & LYS_YINELEM_MASK) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02003841 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003842 return LY_EVALID;
3843 }
3844
3845 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003846 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003847
3848 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
3849 *flags |= LYS_YINELEM_TRUE;
3850 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
3851 *flags |= LYS_YINELEM_FALSE;
3852 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02003853 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003854 free(buf);
3855 return LY_EVALID;
3856 }
3857 free(buf);
3858
Radek Krejci6d6556c2018-11-08 09:37:45 +01003859 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003860 switch (kw) {
3861 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003862 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_YINELEM, 0, exts));
3863 LY_CHECK_RET(ret); break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003864 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003865 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003866 return LY_EVALID;
3867 }
3868 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003869 return ret;
3870}
3871
Michal Vaskoea5abea2018-09-18 13:10:54 +02003872/**
3873 * @brief Parse the yin-element statement.
3874 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003875 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003876 * @param[in,out] data Data to read from, always moved to currently handled character.
3877 * @param[in,out] argument Value to write to.
3878 * @param[in,out] flags Flags to write to.
3879 * @param[in,out] exts Extension instances to add to.
3880 *
3881 * @return LY_ERR values.
3882 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003883static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003884parse_argument(struct ly_parser_ctx *ctx, const char **data, const char **argument, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003885{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003886 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003887 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003888 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003889 enum yang_keyword kw;
3890
3891 if (*argument) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02003892 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003893 return LY_EVALID;
3894 }
3895
3896 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003897 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003898 INSERT_WORD(ctx, buf, *argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003899
Radek Krejci6d6556c2018-11-08 09:37:45 +01003900 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003901 switch (kw) {
3902 case YANG_YIN_ELEMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003903 LY_CHECK_RET(parse_yinelement(ctx, data, flags, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003904 break;
3905 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003906 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_ARGUMENT, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003907 break;
3908 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003909 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003910 return LY_EVALID;
3911 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003912 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003913 return ret;
3914}
3915
Michal Vaskoea5abea2018-09-18 13:10:54 +02003916/**
3917 * @brief Parse the extension statement.
3918 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003919 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003920 * @param[in,out] data Data to read from, always moved to currently handled character.
3921 * @param[in,out] extensions Extensions to add to.
3922 *
3923 * @return LY_ERR values.
3924 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003925static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003926parse_extension(struct ly_parser_ctx *ctx, const char **data, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003927{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003928 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003929 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003930 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003931 enum yang_keyword kw;
3932 struct lysp_ext *ex;
3933
Radek Krejci2c4e7172018-10-19 15:56:26 +02003934 LY_ARRAY_NEW_RET(ctx->ctx, *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003935
3936 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003937 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003938 INSERT_WORD(ctx, buf, ex->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003939
Radek Krejci6d6556c2018-11-08 09:37:45 +01003940 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003941 switch (kw) {
3942 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003943 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 +02003944 break;
3945 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003946 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 +02003947 break;
3948 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003949 LY_CHECK_RET(parse_status(ctx, data, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003950 break;
3951 case YANG_ARGUMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003952 LY_CHECK_RET(parse_argument(ctx, data, &ex->argument, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003953 break;
3954 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003955 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003956 break;
3957 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003958 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003959 return LY_EVALID;
3960 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003961 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003962 return ret;
3963}
3964
Michal Vaskoea5abea2018-09-18 13:10:54 +02003965/**
3966 * @brief Parse the deviate statement.
3967 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003968 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003969 * @param[in,out] data Data to read from, always moved to currently handled character.
3970 * @param[in,out] deviates Deviates to add to.
3971 *
3972 * @return LY_ERR values.
3973 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003974static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003975parse_deviate(struct ly_parser_ctx *ctx, const char **data, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003976{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003977 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003978 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003979 size_t word_len, dev_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003980 enum yang_keyword kw;
3981 struct lysp_deviate *iter, *d;
3982 struct lysp_deviate_add *d_add = NULL;
3983 struct lysp_deviate_rpl *d_rpl = NULL;
3984 struct lysp_deviate_del *d_del = NULL;
3985 const char **d_units, ***d_uniques, ***d_dflts;
3986 struct lysp_restr **d_musts;
3987 uint16_t *d_flags;
3988 uint32_t *d_min, *d_max;
3989
3990 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003991 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003992
3993 if ((word_len == 13) && !strncmp(word, "not-supported", word_len)) {
3994 dev_mod = LYS_DEV_NOT_SUPPORTED;
3995 } else if ((word_len == 3) && !strncmp(word, "add", word_len)) {
3996 dev_mod = LYS_DEV_ADD;
3997 } else if ((word_len == 7) && !strncmp(word, "replace", word_len)) {
3998 dev_mod = LYS_DEV_REPLACE;
3999 } else if ((word_len == 6) && !strncmp(word, "delete", word_len)) {
4000 dev_mod = LYS_DEV_DELETE;
4001 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004002 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004003 free(buf);
4004 return LY_EVALID;
4005 }
4006 free(buf);
4007
4008 /* create structure */
4009 switch (dev_mod) {
4010 case LYS_DEV_NOT_SUPPORTED:
4011 d = calloc(1, sizeof *d);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004012 LY_CHECK_ERR_RET(!d, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004013 break;
4014 case LYS_DEV_ADD:
4015 d_add = calloc(1, sizeof *d_add);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004016 LY_CHECK_ERR_RET(!d_add, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004017 d = (struct lysp_deviate *)d_add;
4018 d_units = &d_add->units;
4019 d_uniques = &d_add->uniques;
4020 d_dflts = &d_add->dflts;
4021 d_musts = &d_add->musts;
4022 d_flags = &d_add->flags;
4023 d_min = &d_add->min;
4024 d_max = &d_add->max;
4025 break;
4026 case LYS_DEV_REPLACE:
4027 d_rpl = calloc(1, sizeof *d_rpl);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004028 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004029 d = (struct lysp_deviate *)d_rpl;
4030 d_units = &d_rpl->units;
4031 d_flags = &d_rpl->flags;
4032 d_min = &d_rpl->min;
4033 d_max = &d_rpl->max;
4034 break;
4035 case LYS_DEV_DELETE:
4036 d_del = calloc(1, sizeof *d_del);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004037 LY_CHECK_ERR_RET(!d_del, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004038 d = (struct lysp_deviate *)d_del;
4039 d_units = &d_del->units;
4040 d_uniques = &d_del->uniques;
4041 d_dflts = &d_del->dflts;
4042 d_musts = &d_del->musts;
4043 d_flags = &d_del->flags;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004044 break;
4045 default:
4046 assert(0);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004047 LOGINT_RET(ctx->ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004048 }
4049 d->mod = dev_mod;
4050
4051 /* insert into siblings */
4052 if (!*deviates) {
4053 *deviates = d;
4054 } else {
4055 for (iter = *deviates; iter->next; iter = iter->next);
4056 iter->next = d;
4057 }
4058
Radek Krejci6d6556c2018-11-08 09:37:45 +01004059 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004060 switch (kw) {
4061 case YANG_CONFIG:
4062 switch (dev_mod) {
4063 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004064 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004065 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004066 return LY_EVALID;
4067 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004068 LY_CHECK_RET(parse_config(ctx, data, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004069 break;
4070 }
4071 break;
4072 case YANG_DEFAULT:
4073 switch (dev_mod) {
4074 case LYS_DEV_NOT_SUPPORTED:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004075 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004076 return LY_EVALID;
4077 case LYS_DEV_REPLACE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004078 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 +02004079 break;
4080 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004081 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 +02004082 break;
4083 }
4084 break;
4085 case YANG_MANDATORY:
4086 switch (dev_mod) {
4087 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004088 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004089 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004090 return LY_EVALID;
4091 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004092 LY_CHECK_RET(parse_mandatory(ctx, data, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004093 break;
4094 }
4095 break;
4096 case YANG_MAX_ELEMENTS:
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_maxelements(ctx, data, d_max, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004104 break;
4105 }
4106 break;
4107 case YANG_MIN_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_minelements(ctx, data, d_min, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004115 break;
4116 }
4117 break;
4118 case YANG_MUST:
4119 switch (dev_mod) {
4120 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004121 case LYS_DEV_REPLACE:
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_restrs(ctx, data, kw, d_musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004126 break;
4127 }
4128 break;
4129 case YANG_TYPE:
4130 switch (dev_mod) {
4131 case LYS_DEV_NOT_SUPPORTED:
4132 case LYS_DEV_ADD:
4133 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004134 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004135 return LY_EVALID;
4136 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004137 if (d_rpl->type) {
4138 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
4139 return LY_EVALID;
4140 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004141 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004142 LY_CHECK_ERR_RET(!d_rpl->type, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004143 LY_CHECK_RET(parse_type(ctx, data, d_rpl->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004144 break;
4145 }
4146 break;
4147 case YANG_UNIQUE:
4148 switch (dev_mod) {
4149 case LYS_DEV_NOT_SUPPORTED:
4150 case LYS_DEV_REPLACE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004151 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004152 return LY_EVALID;
4153 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004154 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 +02004155 break;
4156 }
4157 break;
4158 case YANG_UNITS:
4159 switch (dev_mod) {
4160 case LYS_DEV_NOT_SUPPORTED:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004161 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004162 return LY_EVALID;
4163 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004164 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 +02004165 break;
4166 }
4167 break;
4168 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004169 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004170 break;
4171 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004172 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004173 return LY_EVALID;
4174 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004175 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004176 return ret;
4177}
4178
Michal Vaskoea5abea2018-09-18 13:10:54 +02004179/**
4180 * @brief Parse the deviation statement.
4181 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004182 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004183 * @param[in,out] data Data to read from, always moved to currently handled character.
4184 * @param[in,out] deviations Deviations to add to.
4185 *
4186 * @return LY_ERR values.
4187 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004188static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02004189parse_deviation(struct ly_parser_ctx *ctx, const char **data, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004190{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004191 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004192 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004193 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004194 enum yang_keyword kw;
4195 struct lysp_deviation *dev;
4196
Radek Krejci2c4e7172018-10-19 15:56:26 +02004197 LY_ARRAY_NEW_RET(ctx->ctx, *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004198
4199 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004200 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02004201 INSERT_WORD(ctx, buf, dev->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004202
Radek Krejci6d6556c2018-11-08 09:37:45 +01004203 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004204 switch (kw) {
4205 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004206 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 +02004207 break;
4208 case YANG_DEVIATE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004209 LY_CHECK_RET(parse_deviate(ctx, data, &dev->deviates));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004210 break;
4211 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004212 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 +02004213 break;
4214 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004215 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004216 break;
4217 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004218 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004219 return LY_EVALID;
4220 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004221 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004222 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01004223checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004224 /* mandatory substatements */
4225 if (!dev->deviates) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004226 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004227 return LY_EVALID;
4228 }
4229
4230 return ret;
4231}
4232
Michal Vaskoea5abea2018-09-18 13:10:54 +02004233/**
4234 * @brief Parse the feature statement.
4235 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004236 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004237 * @param[in,out] data Data to read from, always moved to currently handled character.
4238 * @param[in,out] features Features to add to.
4239 *
4240 * @return LY_ERR values.
4241 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004242static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02004243parse_feature(struct ly_parser_ctx *ctx, const char **data, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004244{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004245 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004246 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004247 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004248 enum yang_keyword kw;
4249 struct lysp_feature *feat;
4250
Radek Krejci2c4e7172018-10-19 15:56:26 +02004251 LY_ARRAY_NEW_RET(ctx->ctx, *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004252
4253 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004254 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02004255 INSERT_WORD(ctx, buf, feat->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004256
Radek Krejci6d6556c2018-11-08 09:37:45 +01004257 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004258 switch (kw) {
4259 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004260 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 +02004261 break;
4262 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004263 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 +02004264 break;
4265 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004266 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 +02004267 break;
4268 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004269 LY_CHECK_RET(parse_status(ctx, data, &feat->flags, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004270 break;
4271 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004272 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004273 break;
4274 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004275 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004276 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004277 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004278 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004279 return ret;
4280}
4281
Michal Vaskoea5abea2018-09-18 13:10:54 +02004282/**
4283 * @brief Parse the identity statement.
4284 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004285 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004286 * @param[in,out] data Data to read from, always moved to currently handled character.
4287 * @param[in,out] identities Identities to add to.
4288 *
4289 * @return LY_ERR values.
4290 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004291static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02004292parse_identity(struct ly_parser_ctx *ctx, const char **data, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004293{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004294 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004295 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004296 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004297 enum yang_keyword kw;
4298 struct lysp_ident *ident;
4299
Radek Krejci2c4e7172018-10-19 15:56:26 +02004300 LY_ARRAY_NEW_RET(ctx->ctx, *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004301
4302 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004303 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02004304 INSERT_WORD(ctx, buf, ident->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004305
Radek Krejci6d6556c2018-11-08 09:37:45 +01004306 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004307 switch (kw) {
4308 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004309 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 +02004310 break;
4311 case YANG_IF_FEATURE:
Radek Krejciceaf2122019-01-02 15:03:26 +01004312 YANG_CHECK_STMTVER2_RET(ctx, "if-feature", "identity");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004313 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 +02004314 break;
4315 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004316 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 +02004317 break;
4318 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004319 LY_CHECK_RET(parse_status(ctx, data, &ident->flags, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004320 break;
4321 case YANG_BASE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004322 if (ident->bases && ctx->mod_version < 2) {
Radek Krejci10113652018-11-14 16:56:50 +01004323 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Identity can be derived from multiple base identities only in YANG 1.1 modules");
4324 return LY_EVALID;
4325 }
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004326 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 +02004327 break;
4328 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004329 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004330 break;
4331 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004332 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004333 return LY_EVALID;
4334 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004335 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004336 return ret;
4337}
4338
Michal Vaskoea5abea2018-09-18 13:10:54 +02004339/**
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004340 * @brief Finalize some of the (sub)module structure after parsing.
4341 *
4342 * Update parent pointers in the nodes inside grouping/RPC/Notification, which could be reallocated.
4343 *
4344 * @param[in] mod Parsed module to be updated.
4345 * @return LY_ERR value (currently only LY_SUCCESS, but it can change in future).
4346 */
4347static LY_ERR
4348parse_sub_module_finalize(struct lysp_module *mod)
4349{
4350 unsigned int u;
4351 struct lysp_node *child;
4352
4353 /* finalize parent pointers to the reallocated items */
4354 LY_ARRAY_FOR(mod->groupings, u) {
4355 LY_LIST_FOR(mod->groupings[u].data, child) {
4356 child->parent = (struct lysp_node*)&mod->groupings[u];
4357 }
4358 }
4359
4360 /* TODO the same finalization for rpcs and notifications, do also in the relevant nodes */
4361
4362 return LY_SUCCESS;
4363}
4364
4365/**
4366 * @brief Parse module substatements.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004367 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004368 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004369 * @param[in,out] data Data to read from, always moved to currently handled character.
4370 * @param[in,out] mod Module to write to.
4371 *
4372 * @return LY_ERR values.
4373 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004374static LY_ERR
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004375parse_module(struct ly_parser_ctx *ctx, const char **data, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004376{
4377 LY_ERR ret = 0;
4378 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004379 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004380 enum yang_keyword kw, prev_kw = 0;
4381 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004382 struct lysp_submodule *dup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004383
4384 /* (sub)module name */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004385 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004386 INSERT_WORD(ctx, buf, mod->mod->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004387
Radek Krejci6d6556c2018-11-08 09:37:45 +01004388 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004389
Radek Krejcie3846472018-10-15 15:24:51 +02004390#define CHECK_ORDER(SECTION) \
4391 if (mod_stmt > SECTION) {LOGVAL_YANG(ctx, LY_VCODE_INORD, ly_stmt2str(kw), ly_stmt2str(prev_kw)); return LY_EVALID;}mod_stmt = SECTION
4392
Michal Vasko7fbc8162018-09-17 10:35:16 +02004393 switch (kw) {
4394 /* module header */
4395 case YANG_NAMESPACE:
4396 case YANG_PREFIX:
Radek Krejcie3846472018-10-15 15:24:51 +02004397 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4398 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004399 case YANG_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004400 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004401 break;
4402 /* linkage */
4403 case YANG_INCLUDE:
4404 case YANG_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004405 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004406 break;
4407 /* meta */
4408 case YANG_ORGANIZATION:
4409 case YANG_CONTACT:
4410 case YANG_DESCRIPTION:
4411 case YANG_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004412 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004413 break;
4414
4415 /* revision */
4416 case YANG_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004417 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004418 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004419 /* body */
4420 case YANG_ANYDATA:
4421 case YANG_ANYXML:
4422 case YANG_AUGMENT:
4423 case YANG_CHOICE:
4424 case YANG_CONTAINER:
4425 case YANG_DEVIATION:
4426 case YANG_EXTENSION:
4427 case YANG_FEATURE:
4428 case YANG_GROUPING:
4429 case YANG_IDENTITY:
4430 case YANG_LEAF:
4431 case YANG_LEAF_LIST:
4432 case YANG_LIST:
4433 case YANG_NOTIFICATION:
4434 case YANG_RPC:
4435 case YANG_TYPEDEF:
4436 case YANG_USES:
4437 case YANG_CUSTOM:
4438 mod_stmt = Y_MOD_BODY;
4439 break;
4440 default:
4441 /* error handled in the next switch */
4442 break;
4443 }
Radek Krejcie3846472018-10-15 15:24:51 +02004444#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004445
Radek Krejcie3846472018-10-15 15:24:51 +02004446 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004447 switch (kw) {
4448 /* module header */
4449 case YANG_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004450 LY_CHECK_RET(parse_yangversion(ctx, data, &mod->mod->version, &mod->exts));
4451 ctx->mod_version = mod->mod->version;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004452 break;
4453 case YANG_NAMESPACE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004454 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 +02004455 break;
4456 case YANG_PREFIX:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004457 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 +02004458 break;
4459
4460 /* linkage */
4461 case YANG_INCLUDE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004462 LY_CHECK_RET(parse_include(ctx, mod->mod->name, data, &mod->includes));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004463 break;
4464 case YANG_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004465 LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, data, &mod->imports));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004466 break;
4467
4468 /* meta */
4469 case YANG_ORGANIZATION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004470 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 +02004471 break;
4472 case YANG_CONTACT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004473 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 +02004474 break;
4475 case YANG_DESCRIPTION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004476 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 +02004477 break;
4478 case YANG_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004479 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 +02004480 break;
4481
4482 /* revision */
4483 case YANG_REVISION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004484 LY_CHECK_RET(parse_revision(ctx, data, &mod->revs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004485 break;
4486
4487 /* body */
4488 case YANG_ANYDATA:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004489 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "module");
Radek Krejci10113652018-11-14 16:56:50 +01004490 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004491 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004492 LY_CHECK_RET(parse_any(ctx, data, kw, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004493 break;
4494 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004495 LY_CHECK_RET(parse_choice(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004496 break;
4497 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004498 LY_CHECK_RET(parse_container(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004499 break;
4500 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004501 LY_CHECK_RET(parse_leaf(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004502 break;
4503 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004504 LY_CHECK_RET(parse_leaflist(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004505 break;
4506 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004507 LY_CHECK_RET(parse_list(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004508 break;
4509 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004510 LY_CHECK_RET(parse_uses(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004511 break;
4512
4513 case YANG_AUGMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004514 LY_CHECK_RET(parse_augment(ctx, data, NULL, &mod->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004515 break;
4516 case YANG_DEVIATION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004517 LY_CHECK_RET(parse_deviation(ctx, data, &mod->deviations));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004518 break;
4519 case YANG_EXTENSION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004520 LY_CHECK_RET(parse_extension(ctx, data, &mod->extensions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004521 break;
4522 case YANG_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004523 LY_CHECK_RET(parse_feature(ctx, data, &mod->features));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004524 break;
4525 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004526 LY_CHECK_RET(parse_grouping(ctx, data, NULL, &mod->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004527 break;
4528 case YANG_IDENTITY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004529 LY_CHECK_RET(parse_identity(ctx, data, &mod->identities));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004530 break;
4531 case YANG_NOTIFICATION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004532 LY_CHECK_RET(parse_notif(ctx, data, NULL, &mod->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004533 break;
4534 case YANG_RPC:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004535 LY_CHECK_RET(parse_action(ctx, data, NULL, &mod->rpcs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004536 break;
4537 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01004538 LY_CHECK_RET(parse_typedef(ctx, NULL, data, &mod->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004539 break;
4540 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004541 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004542 break;
4543
4544 default:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004545 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004546 return LY_EVALID;
4547 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004548 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004549 LY_CHECK_RET(ret);
Radek Krejcie86bf772018-12-14 11:39:53 +01004550
Radek Krejci6d6556c2018-11-08 09:37:45 +01004551checks:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004552 /* finalize parent pointers to the reallocated items */
4553 LY_CHECK_RET(parse_sub_module_finalize(mod));
4554
Michal Vasko7fbc8162018-09-17 10:35:16 +02004555 /* mandatory substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004556 if (!mod->mod->ns) {
4557 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "namespace", "module");
4558 return LY_EVALID;
4559 } else if (!mod->mod->prefix) {
4560 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "prefix", "module");
4561 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004562 }
4563
Radek Krejcie9e987e2018-10-31 12:50:27 +01004564 /* submodules share the namespace with the module names, so there must not be
4565 * a submodule of the same name in the context, no need for revision matching */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004566 dup = ly_ctx_get_submodule(ctx->ctx, NULL, mod->mod->name, NULL);
4567 if (dup) {
4568 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Name collision between module and submodule of name \"%s\".", mod->mod->name);
4569 return LY_EVALID;
4570 }
4571
4572 return ret;
4573}
4574
4575/**
4576 * @brief Parse submodule substatements.
4577 *
4578 * @param[in] ctx yang parser context for logging.
4579 * @param[in,out] data Data to read from, always moved to currently handled character.
4580 * @param[out] submod Parsed submodule structure.
4581 *
4582 * @return LY_ERR values.
4583 */
4584static LY_ERR
4585parse_submodule(struct ly_parser_ctx *ctx, const char **data, struct lysp_submodule *submod)
4586{
4587 LY_ERR ret = 0;
4588 char *buf, *word;
4589 size_t word_len;
4590 enum yang_keyword kw, prev_kw = 0;
4591 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
4592 struct lysp_submodule *dup;
4593
4594 /* submodule name */
4595 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
4596 INSERT_WORD(ctx, buf, submod->name, word, word_len);
4597
4598 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
4599
4600#define CHECK_ORDER(SECTION) \
4601 if (mod_stmt > SECTION) {LOGVAL_YANG(ctx, LY_VCODE_INORD, ly_stmt2str(kw), ly_stmt2str(prev_kw)); return LY_EVALID;}mod_stmt = SECTION
4602
4603 switch (kw) {
4604 /* module header */
4605 case YANG_BELONGS_TO:
4606 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4607 break;
4608 case YANG_YANG_VERSION:
4609 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4610 break;
4611 /* linkage */
4612 case YANG_INCLUDE:
4613 case YANG_IMPORT:
4614 CHECK_ORDER(Y_MOD_LINKAGE);
4615 break;
4616 /* meta */
4617 case YANG_ORGANIZATION:
4618 case YANG_CONTACT:
4619 case YANG_DESCRIPTION:
4620 case YANG_REFERENCE:
4621 CHECK_ORDER(Y_MOD_META);
4622 break;
4623
4624 /* revision */
4625 case YANG_REVISION:
4626 CHECK_ORDER(Y_MOD_REVISION);
4627 break;
4628 /* body */
4629 case YANG_ANYDATA:
4630 case YANG_ANYXML:
4631 case YANG_AUGMENT:
4632 case YANG_CHOICE:
4633 case YANG_CONTAINER:
4634 case YANG_DEVIATION:
4635 case YANG_EXTENSION:
4636 case YANG_FEATURE:
4637 case YANG_GROUPING:
4638 case YANG_IDENTITY:
4639 case YANG_LEAF:
4640 case YANG_LEAF_LIST:
4641 case YANG_LIST:
4642 case YANG_NOTIFICATION:
4643 case YANG_RPC:
4644 case YANG_TYPEDEF:
4645 case YANG_USES:
4646 case YANG_CUSTOM:
4647 mod_stmt = Y_MOD_BODY;
4648 break;
4649 default:
4650 /* error handled in the next switch */
4651 break;
4652 }
4653#undef CHECK_ORDER
4654
4655 prev_kw = kw;
4656 switch (kw) {
4657 /* module header */
4658 case YANG_YANG_VERSION:
4659 LY_CHECK_RET(parse_yangversion(ctx, data, &submod->version, &submod->exts));
4660 ctx->mod_version = submod->version;
4661 break;
4662 case YANG_BELONGS_TO:
4663 LY_CHECK_RET(parse_belongsto(ctx, data, &submod->belongsto, &submod->prefix, &submod->exts));
4664 break;
4665
4666 /* linkage */
4667 case YANG_INCLUDE:
4668 LY_CHECK_RET(parse_include(ctx, submod->name, data, &submod->includes));
4669 break;
4670 case YANG_IMPORT:
4671 LY_CHECK_RET(parse_import(ctx, submod->prefix, data, &submod->imports));
4672 break;
4673
4674 /* meta */
4675 case YANG_ORGANIZATION:
4676 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_ORGANIZATION, 0, &submod->org, Y_STR_ARG, &submod->exts));
4677 break;
4678 case YANG_CONTACT:
4679 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_CONTACT, 0, &submod->contact, Y_STR_ARG, &submod->exts));
4680 break;
4681 case YANG_DESCRIPTION:
4682 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &submod->dsc, Y_STR_ARG, &submod->exts));
4683 break;
4684 case YANG_REFERENCE:
4685 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &submod->ref, Y_STR_ARG, &submod->exts));
4686 break;
4687
4688 /* revision */
4689 case YANG_REVISION:
4690 LY_CHECK_RET(parse_revision(ctx, data, &submod->revs));
4691 break;
4692
4693 /* body */
4694 case YANG_ANYDATA:
4695 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "submodule");
4696 /* fall through */
4697 case YANG_ANYXML:
4698 LY_CHECK_RET(parse_any(ctx, data, kw, NULL, &submod->data));
4699 break;
4700 case YANG_CHOICE:
4701 LY_CHECK_RET(parse_choice(ctx, data, NULL, &submod->data));
4702 break;
4703 case YANG_CONTAINER:
4704 LY_CHECK_RET(parse_container(ctx, data, NULL, &submod->data));
4705 break;
4706 case YANG_LEAF:
4707 LY_CHECK_RET(parse_leaf(ctx, data, NULL, &submod->data));
4708 break;
4709 case YANG_LEAF_LIST:
4710 LY_CHECK_RET(parse_leaflist(ctx, data, NULL, &submod->data));
4711 break;
4712 case YANG_LIST:
4713 LY_CHECK_RET(parse_list(ctx, data, NULL, &submod->data));
4714 break;
4715 case YANG_USES:
4716 LY_CHECK_RET(parse_uses(ctx, data, NULL, &submod->data));
4717 break;
4718
4719 case YANG_AUGMENT:
4720 LY_CHECK_RET(parse_augment(ctx, data, NULL, &submod->augments));
4721 break;
4722 case YANG_DEVIATION:
4723 LY_CHECK_RET(parse_deviation(ctx, data, &submod->deviations));
4724 break;
4725 case YANG_EXTENSION:
4726 LY_CHECK_RET(parse_extension(ctx, data, &submod->extensions));
4727 break;
4728 case YANG_FEATURE:
4729 LY_CHECK_RET(parse_feature(ctx, data, &submod->features));
4730 break;
4731 case YANG_GROUPING:
4732 LY_CHECK_RET(parse_grouping(ctx, data, NULL, &submod->groupings));
4733 break;
4734 case YANG_IDENTITY:
4735 LY_CHECK_RET(parse_identity(ctx, data, &submod->identities));
4736 break;
4737 case YANG_NOTIFICATION:
4738 LY_CHECK_RET(parse_notif(ctx, data, NULL, &submod->notifs));
4739 break;
4740 case YANG_RPC:
4741 LY_CHECK_RET(parse_action(ctx, data, NULL, &submod->rpcs));
4742 break;
4743 case YANG_TYPEDEF:
4744 LY_CHECK_RET(parse_typedef(ctx, NULL, data, &submod->typedefs));
4745 break;
4746 case YANG_CUSTOM:
4747 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &submod->exts));
4748 break;
4749
4750 default:
4751 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule");
4752 return LY_EVALID;
4753 }
4754 }
4755 LY_CHECK_RET(ret);
4756
4757checks:
4758 /* finalize parent pointers to the reallocated items */
4759 LY_CHECK_RET(parse_sub_module_finalize((struct lysp_module*)submod));
4760
4761 /* mandatory substatements */
4762 if (!submod->belongsto) {
4763 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
4764 return LY_EVALID;
4765 }
4766
4767 /* submodules share the namespace with the module names, so there must not be
4768 * a submodule of the same name in the context, no need for revision matching */
4769 dup = ly_ctx_get_submodule(ctx->ctx, NULL, submod->name, NULL);
4770 if (dup && strcmp(dup->belongsto, submod->belongsto)) {
4771 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Name collision between submodules of name \"%s\".", dup->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +01004772 return LY_EVALID;
4773 }
4774
Michal Vasko7fbc8162018-09-17 10:35:16 +02004775 return ret;
4776}
4777
Radek Krejcid4557c62018-09-17 11:42:09 +02004778LY_ERR
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004779yang_parse_submodule(struct ly_parser_ctx *context, const char *data, struct lysp_submodule **submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004780{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004781 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004782 char *word, *buf;
Radek Krejciefd22f62018-09-27 11:47:58 +02004783 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004784 enum yang_keyword kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004785 struct lysp_submodule *mod_p = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004786
4787 /* "module"/"submodule" */
Radek Krejcibbe09a92018-11-08 09:36:54 +01004788 ret = get_keyword(context, &data, &kw, &word, &word_len);
4789 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004790
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004791 if (kw == YANG_MODULE) {
4792 LOGERR(context->ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
4793 ret = LY_EINVAL;
4794 goto cleanup;
4795 } else if (kw != YANG_SUBMODULE) {
Radek Krejcibbe09a92018-11-08 09:36:54 +01004796 LOGVAL_YANG(context, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".",
Radek Krejcic59bc972018-09-17 16:13:06 +02004797 ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004798 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004799 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004800 }
4801
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004802 mod_p = calloc(1, sizeof *mod_p);
4803 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(context->ctx), cleanup);
4804 mod_p->parsing = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004805
4806 /* substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004807 ret = parse_submodule(context, &data, mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004808 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004809
4810 /* read some trailing spaces or new lines */
Radek Krejcibbe09a92018-11-08 09:36:54 +01004811 ret = get_argument(context, &data, Y_MAYBE_STR_ARG, &word, &buf, &word_len);
4812 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004813
4814 if (word) {
Radek Krejcibbe09a92018-11-08 09:36:54 +01004815 LOGVAL_YANG(context, LYVE_SYNTAX, "Invalid character sequence \"%.*s\", expected end-of-file.",
Michal Vasko7fbc8162018-09-17 10:35:16 +02004816 word_len, word);
4817 free(buf);
Radek Krejci40544fa2019-01-11 09:38:37 +01004818 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004819 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004820 }
4821 assert(!buf);
4822
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004823 mod_p->parsing = 0;
4824 *submod = mod_p;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004825
Radek Krejcibbe09a92018-11-08 09:36:54 +01004826cleanup:
4827 if (ret) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004828 lysp_submodule_free(context->ctx, mod_p);
4829 }
4830
4831 return ret;
4832}
4833
4834LY_ERR
4835yang_parse_module(struct ly_parser_ctx *context, const char *data, struct lys_module *mod)
4836{
4837 LY_ERR ret = LY_SUCCESS;
4838 char *word, *buf;
4839 size_t word_len;
4840 enum yang_keyword kw;
4841 struct lysp_module *mod_p = NULL;
4842
4843 /* "module"/"submodule" */
4844 ret = get_keyword(context, &data, &kw, &word, &word_len);
4845 LY_CHECK_GOTO(ret, cleanup);
4846
4847 if (kw == YANG_SUBMODULE) {
4848 LOGERR(context->ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module.");
4849 ret = LY_EINVAL;
4850 goto cleanup;
4851 } else if (kw != YANG_MODULE) {
4852 LOGVAL_YANG(context, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".",
4853 ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004854 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004855 goto cleanup;
4856 }
4857
4858 mod_p = calloc(1, sizeof *mod_p);
4859 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(context->ctx), cleanup);
4860 mod_p->mod = mod;
4861 mod_p->parsing = 1;
4862
4863 /* substatements */
4864 ret = parse_module(context, &data, mod_p);
4865 LY_CHECK_GOTO(ret, cleanup);
4866
4867 /* read some trailing spaces or new lines */
4868 ret = get_argument(context, &data, Y_MAYBE_STR_ARG, &word, &buf, &word_len);
4869 LY_CHECK_GOTO(ret, cleanup);
4870
4871 if (word) {
4872 LOGVAL_YANG(context, LYVE_SYNTAX, "Invalid character sequence \"%.*s\", expected end-of-file.",
4873 word_len, word);
4874 free(buf);
Radek Krejci40544fa2019-01-11 09:38:37 +01004875 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004876 goto cleanup;
4877 }
4878 assert(!buf);
4879
4880 mod_p->parsing = 0;
4881 mod->parsed = mod_p;
4882
4883cleanup:
4884 if (ret) {
4885 lysp_module_free(mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004886 }
4887
Michal Vasko7fbc8162018-09-17 10:35:16 +02004888 return ret;
4889}