blob: df54dadcfbcc76b36659d876acd39acd4e791913 [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 Krejci10113652018-11-14 16:56:50 +0100118 if ((CTX)->mod->version < 2) {LOGVAL_YANG((CTX), LY_VCODE_INCHILDSTMT2, KW, PARENT); return LY_EVALID;}
119
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
237 /* get UTF8 code point (and number of bytes coding the character) */
238 LY_CHECK_ERR_RET(ly_getutf8(input, &c, &len),
239 LOGVAL_YANG(ctx, LY_VCODE_INCHAR, (*input)[-len]), LY_EVALID);
240 (*input) -= len;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200241 if (c == '\n') {
242 ctx->indent = 0;
243 } else {
244 /* note - even the multibyte character is count as 1 */
245 ++ctx->indent;
246 }
247
Radek Krejci44ceedc2018-10-02 15:54:31 +0200248 /* check character validity */
249 switch (arg) {
250 case Y_IDENTIF_ARG:
251 LY_CHECK_RET(check_identifierchar(ctx, c, !(*word_len), NULL));
252 break;
253 case Y_PREF_IDENTIF_ARG:
254 LY_CHECK_RET(check_identifierchar(ctx, c, !(*word_len), &prefix));
255 break;
256 case Y_STR_ARG:
257 case Y_MAYBE_STR_ARG:
258 LY_CHECK_RET(check_stringchar(ctx, c));
259 break;
260 }
261
Michal Vasko7fbc8162018-09-17 10:35:16 +0200262 if (word_b && *word_b) {
263 /* add another character into buffer */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200264 if (buf_add_char(ctx->ctx, input, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200265 return LY_EMEM;
266 }
267
268 /* in case of realloc */
269 *word_p = *word_b;
270 } else if (need_buf) {
271 /* first time we need a buffer, copy everything read up to now */
272 if (*word_len) {
273 *word_b = malloc(*word_len);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200274 LY_CHECK_ERR_RET(!*word_b, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200275 *buf_len = *word_len;
276 memcpy(*word_b, *word_p, *word_len);
277 }
278
279 /* add this new character into buffer */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200280 if (buf_add_char(ctx->ctx, input, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200281 return LY_EMEM;
282 }
283
284 /* in case of realloc */
285 *word_p = *word_b;
286 } else {
287 /* just remember the first character pointer */
288 if (!*word_p) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200289 *word_p = (char *)(*input);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200290 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200291 /* ... and update the word's length */
292 (*word_len) += len;
293 (*input) += len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200294 }
295
296 return LY_SUCCESS;
297}
298
Michal Vaskoea5abea2018-09-18 13:10:54 +0200299/**
300 * @brief Skip YANG comment in data.
301 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200302 * @param[in] ctx yang parser context for logging.
303 * @param[in,out] data Data to read from, automatically moved after the comment.
304 * @param[in] comment Type of the comment to process:
305 * 1 for a one-line comment,
306 * 2 for a block comment.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200307 *
308 * @return LY_ERR values.
309 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200310static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +0200311skip_comment(struct ly_parser_ctx *ctx, const char **data, int comment)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200312{
Radek Krejci80dd33e2018-09-26 15:57:18 +0200313 /* internal statuses: 0 - comment ended,
314 * 1 - in line comment,
315 * 2 - in block comment,
316 * 3 - in block comment with last read character '*'
317 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200318 while (**data && comment) {
319 switch (comment) {
320 case 1:
321 if (**data == '\n') {
322 comment = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200323 ++ctx->line;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200324 }
325 break;
326 case 2:
327 if (**data == '*') {
Radek Krejci15c80ca2018-10-09 11:01:31 +0200328 comment = 3;
329 } else if (**data == '\n') {
330 ++ctx->line;
331 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200332 break;
333 case 3:
334 if (**data == '/') {
335 comment = 0;
336 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200337 if (**data == '\n') {
338 ++ctx->line;
339 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200340 comment = 2;
341 }
342 break;
343 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200344 LOGINT_RET(ctx->ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200345 }
346
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200347 if (**data == '\n') {
348 ctx->indent = 0;
349 } else {
350 ++ctx->indent;
351 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200352 ++(*data);
353 }
354
355 if (!**data && (comment > 1)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200356 LOGVAL_YANG(ctx, LYVE_SYNTAX, "Unexpected end-of-file, non-terminated comment.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200357 return LY_EVALID;
358 }
359
360 return LY_SUCCESS;
361}
362
Michal Vaskoea5abea2018-09-18 13:10:54 +0200363/**
364 * @brief Read a quoted string from data.
365 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200366 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200367 * @param[in,out] data Data to read from, always moved to currently handled character.
368 * @param[in] arg Type of YANG keyword argument expected.
369 * @param[out] word_p Pointer to the read quoted string.
370 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read quoted string. If not needed,
371 * set to NULL. Otherwise equal to \p word_p.
372 * @param[out] word_len Length of the read quoted string.
373 * @param[out] buf_len Length of the dynamically-allocated buffer \p word_b.
374 * @param[in] indent Current indent (number of YANG spaces). Needed for correct multi-line string
375 * indenation in the final quoted string.
376 *
377 * @return LY_ERR values.
378 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200379static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +0200380read_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 +0200381 size_t *buf_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200382{
383 /* string: 0 - string ended, 1 - string with ', 2 - string with ", 3 - string with " with last character \,
384 * 4 - string finished, now skipping whitespaces looking for +,
385 * 5 - string continues after +, skipping whitespaces */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200386 unsigned int string, block_indent = 0, current_indent = 0, need_buf = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200387 const char *c;
388
389 if (**data == '\"') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200390 string = 2;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200391 current_indent = block_indent = ctx->indent + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200392 } else {
393 assert(**data == '\'');
394 string = 1;
395 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200396 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200397
398 while (**data && string) {
399 switch (string) {
400 case 1:
401 switch (**data) {
402 case '\'':
403 /* string may be finished, but check for + */
404 string = 4;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200405 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200406 break;
407 default:
408 /* check and store character */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200409 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 +0200410 break;
411 }
412 break;
413 case 2:
414 switch (**data) {
415 case '\"':
416 /* string may be finished, but check for + */
417 string = 4;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200418 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200419 break;
420 case '\\':
421 /* special character following */
422 string = 3;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200423 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200424 break;
425 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200426 if (current_indent < block_indent) {
427 ++current_indent;
428 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200429 } else {
430 /* check and store character */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200431 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 +0200432 }
433 break;
434 case '\t':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200435 if (current_indent < block_indent) {
436 assert(need_buf);
437 current_indent += 8;
438 ctx->indent += 8;
439 for (; current_indent > block_indent; --current_indent, --ctx->indent) {
440 /* store leftover spaces from the tab */
441 c = " ";
442 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 +0200443 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200444 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200445 } else {
446 /* check and store character */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200447 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 +0200448 /* additional characters for indentation - only 1 was count in buf_store_char */
449 ctx->indent += 7;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200450 }
451 break;
452 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200453 if (block_indent) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200454 /* we will be removing the indents so we need our own buffer */
455 need_buf = 1;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200456
457 /* remove trailing tabs and spaces */
458 while ((*word_len) && ((*word_p)[(*word_len) - 1] == '\t' || (*word_p)[(*word_len) - 1] == ' ')) {
459 --(*word_len);
460 }
461
462 /* start indentation */
463 current_indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200464 }
465
466 /* check and store character */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200467 LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, buf_len, need_buf));
468
469 /* maintain line number */
470 ++ctx->line;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200471
472 /* reset context indentation counter for possible string after this one */
473 ctx->indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200474 break;
475 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200476 /* first non-whitespace character, stop eating indentation */
477 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200478
479 /* check and store character */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200480 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 +0200481 break;
482 }
483 break;
484 case 3:
485 /* string encoded characters */
486 switch (**data) {
487 case 'n':
488 c = "\n";
489 break;
490 case 't':
491 c = "\t";
492 break;
493 case '\"':
494 c = *data;
495 break;
496 case '\\':
497 c = *data;
498 break;
499 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200500 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Double-quoted string unknown special character '\\%c'.", **data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200501 return LY_EVALID;
502 }
503
504 /* check and store character */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200505 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 +0200506
507 string = 2;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200508 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200509 break;
510 case 4:
511 switch (**data) {
512 case '+':
513 /* string continues */
514 string = 5;
Radek Krejciefd22f62018-09-27 11:47:58 +0200515 need_buf = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200516 break;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200517 case '\n':
518 ++ctx->line;
519 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200520 case ' ':
521 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200522 /* just skip */
523 break;
524 default:
525 /* string is finished */
526 goto string_end;
527 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200528 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200529 break;
530 case 5:
531 switch (**data) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200532 case '\n':
533 ++ctx->line;
534 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200535 case ' ':
536 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200537 /* skip */
538 break;
539 case '\'':
540 string = 1;
541 break;
542 case '\"':
543 string = 2;
544 break;
545 default:
546 /* it must be quoted again */
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200547 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Both string parts divided by '+' must be quoted.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200548 return LY_EVALID;
549 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200550 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200551 break;
552 default:
553 return LY_EINT;
554 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200555 }
556
557string_end:
558 return LY_SUCCESS;
559}
560
Michal Vaskoea5abea2018-09-18 13:10:54 +0200561/**
562 * @brief Get another YANG string from the raw data.
563 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200564 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200565 * @param[in,out] data Data to read from, always moved to currently handled character.
566 * @param[in] arg Type of YANG keyword argument expected.
Michal Vasko2ca70f52018-09-27 11:04:51 +0200567 * @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 +0200568 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read string. If not needed,
569 * set to NULL. Otherwise equal to \p word_p.
570 * @param[out] word_len Length of the read string.
571 *
572 * @return LY_ERR values.
Michal Vasko7fbc8162018-09-17 10:35:16 +0200573 */
574static LY_ERR
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200575get_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 +0200576{
Radek Krejci44ceedc2018-10-02 15:54:31 +0200577 size_t buf_len = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200578
579 /* word buffer - dynamically allocated */
580 *word_b = NULL;
581
582 /* word pointer - just a pointer to data */
583 *word_p = NULL;
584
585 *word_len = 0;
586 while (**data) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200587 switch (**data) {
588 case '\'':
589 case '\"':
590 if (*word_len) {
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200591 /* invalid - quotes cannot be in unquoted string and only optsep, ; or { can follow it */
592 LOGVAL_YANG(ctx, LY_VCODE_INSTREXP, 1, *data,
593 "unquoted string character, optsep, semicolon or opening brace");
594 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200595 }
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100596 LY_CHECK_RET(read_qstring(ctx, data, arg, word_p, word_b, word_len, &buf_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200597 goto str_end;
598 case '/':
Radek Krejci44ceedc2018-10-02 15:54:31 +0200599 if ((*data)[1] == '/') {
600 /* one-line comment */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200601 MOVE_INPUT(ctx, data, 2);
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100602 LY_CHECK_RET(skip_comment(ctx, data, 1));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200603 } else if ((*data)[1] == '*') {
604 /* block comment */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200605 MOVE_INPUT(ctx, data, 2);
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100606 LY_CHECK_RET(skip_comment(ctx, data, 2));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200607 } else {
608 /* not a comment after all */
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100609 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 +0200610 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200611 break;
612 case ' ':
613 if (*word_len) {
614 /* word is finished */
615 goto str_end;
616 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200617 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200618 break;
619 case '\t':
620 if (*word_len) {
621 /* word is finished */
622 goto str_end;
623 }
624 /* tabs count for 8 spaces */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200625 ctx->indent += 8;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200626
627 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200628 break;
629 case '\n':
630 if (*word_len) {
631 /* word is finished */
632 goto str_end;
633 }
634 /* reset indent */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200635 ctx->indent = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200636
637 /* track line numbers */
638 ++ctx->line;
639
640 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200641 break;
642 case ';':
643 case '{':
644 if (*word_len || (arg == Y_MAYBE_STR_ARG)) {
645 /* word is finished */
646 goto str_end;
647 }
648
Radek Krejci44ceedc2018-10-02 15:54:31 +0200649 LOGVAL_YANG(ctx, LY_VCODE_INSTREXP, 1, *data, "an argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200650 return LY_EVALID;
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200651 case '}':
652 /* invalid - braces cannot be in unquoted string (opening braces terminates the string and can follow it) */
653 LOGVAL_YANG(ctx, LY_VCODE_INSTREXP, 1, *data,
654 "unquoted string character, optsep, semicolon or opening brace");
655 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200656 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200657 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 +0200658 break;
659 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200660 }
661
662str_end:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200663 /* terminating NULL byte for buf */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200664 if (*word_b) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200665 (*word_b) = ly_realloc(*word_b, (*word_len) + 1);
666 LY_CHECK_ERR_RET(!(*word_b), LOGMEM(ctx->ctx), LY_EMEM);
667 (*word_b)[*word_len] = '\0';
Michal Vasko7fbc8162018-09-17 10:35:16 +0200668 *word_p = *word_b;
669 }
670
671 return LY_SUCCESS;
672}
673
Michal Vaskoea5abea2018-09-18 13:10:54 +0200674/**
675 * @brief Get another YANG keyword from the raw data.
676 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200677 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200678 * @param[in,out] data Data to read from, always moved to currently handled character.
679 * @param[out] kw YANG keyword read.
680 * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances.
681 * @param[out] word_len Length of the keyword in the data. Useful for extension instances.
682 *
683 * @return LY_ERR values.
684 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200685static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +0200686get_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 +0200687{
Michal Vasko7fbc8162018-09-17 10:35:16 +0200688 int prefix;
689 const char *word_start;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200690 unsigned int c;
691 size_t len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200692
693 if (word_p) {
694 *word_p = NULL;
695 *word_len = 0;
696 }
697
698 /* first skip "optsep", comments */
699 while (**data) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200700 switch (**data) {
701 case '/':
702 if ((*data)[1] == '/') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200703 /* one-line comment */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200704 MOVE_INPUT(ctx, data, 2);
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100705 LY_CHECK_RET(skip_comment(ctx, data, 1));
Radek Krejcidcc7b322018-10-11 14:24:02 +0200706 } else if ((*data)[1] == '*') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200707 /* block comment */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200708 MOVE_INPUT(ctx, data, 2);
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100709 LY_CHECK_RET(skip_comment(ctx, data, 2));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200710 } else {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200711 /* error - not a comment after all, keyword cannot start with slash */
712 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '/'.");
713 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200714 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200715 break;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200716 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200717 /* skip whitespaces (optsep) */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200718 ++ctx->line;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200719 ctx->indent = 0;
720 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200721 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200722 /* skip whitespaces (optsep) */
723 ++ctx->indent;
724 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200725 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200726 /* skip whitespaces (optsep) */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200727 ctx->indent += 8;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200728 break;
729 default:
730 /* either a keyword start or an invalid character */
731 goto keyword_start;
732 }
733
734 ++(*data);
735 }
736
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200737#define IF_KW(STR, LEN, STMT) if (!strncmp(*(data), STR, LEN)) {MOVE_INPUT(ctx, data, LEN);*kw=STMT;}
738#define IF_KW_PREFIX(STR, LEN) if (!strncmp(*(data), STR, LEN)) {MOVE_INPUT(ctx, data, LEN);
739#define IF_KW_PREFIX_END }
740
Michal Vasko7fbc8162018-09-17 10:35:16 +0200741keyword_start:
742 word_start = *data;
743 *kw = YANG_NONE;
744
745 /* read the keyword itself */
746 switch (**data) {
747 case 'a':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200748 MOVE_INPUT(ctx, data, 1);
749 IF_KW("rgument", 7, YANG_ARGUMENT)
750 else IF_KW("ugment", 6, YANG_AUGMENT)
751 else IF_KW("ction", 5, YANG_ACTION)
752 else IF_KW_PREFIX("ny", 2)
753 IF_KW("data", 4, YANG_ANYDATA)
754 else IF_KW("xml", 3, YANG_ANYXML)
755 IF_KW_PREFIX_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200756 break;
757 case 'b':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200758 MOVE_INPUT(ctx, data, 1);
759 IF_KW("ase", 3, YANG_BASE)
760 else IF_KW("elongs-to", 9, YANG_BELONGS_TO)
761 else IF_KW("it", 2, YANG_BIT)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200762 break;
763 case 'c':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200764 MOVE_INPUT(ctx, data, 1);
765 IF_KW("ase", 3, YANG_CASE)
766 else IF_KW("hoice", 5, YANG_CHOICE)
767 else IF_KW_PREFIX("on", 2)
768 IF_KW("fig", 3, YANG_CONFIG)
769 else IF_KW_PREFIX("ta", 2)
770 IF_KW("ct", 2, YANG_CONTACT)
771 else IF_KW("iner", 4, YANG_CONTAINER)
772 IF_KW_PREFIX_END
773 IF_KW_PREFIX_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200774 break;
775 case 'd':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200776 MOVE_INPUT(ctx, data, 1);
777 IF_KW_PREFIX("e", 1)
778 IF_KW("fault", 5, YANG_DEFAULT)
779 else IF_KW("scription", 9, YANG_DESCRIPTION)
780 else IF_KW_PREFIX("viat", 4)
781 IF_KW("e", 1, YANG_DEVIATE)
782 else IF_KW("ion", 3, YANG_DEVIATION)
783 IF_KW_PREFIX_END
784 IF_KW_PREFIX_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200785 break;
786 case 'e':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200787 MOVE_INPUT(ctx, data, 1);
788 IF_KW("num", 3, YANG_ENUM)
789 else IF_KW_PREFIX("rror-", 5)
790 IF_KW("app-tag", 7, YANG_ERROR_APP_TAG)
791 else IF_KW("message", 7, YANG_ERROR_MESSAGE)
792 IF_KW_PREFIX_END
793 else IF_KW("xtension", 8, YANG_EXTENSION)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200794 break;
795 case 'f':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200796 MOVE_INPUT(ctx, data, 1);
797 IF_KW("eature", 6, YANG_FEATURE)
798 else IF_KW("raction-digits", 14, YANG_FRACTION_DIGITS)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200799 break;
800 case 'g':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200801 MOVE_INPUT(ctx, data, 1);
802 IF_KW("rouping", 7, YANG_GROUPING)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200803 break;
804 case 'i':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200805 MOVE_INPUT(ctx, data, 1);
806 IF_KW("dentity", 7, YANG_IDENTITY)
807 else IF_KW("f-feature", 9, YANG_IF_FEATURE)
808 else IF_KW("mport", 5, YANG_IMPORT)
809 else IF_KW_PREFIX("n", 1)
810 IF_KW("clude", 5, YANG_INCLUDE)
811 else IF_KW("put", 3, YANG_INPUT)
812 IF_KW_PREFIX_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200813 break;
814 case 'k':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200815 MOVE_INPUT(ctx, data, 1);
816 IF_KW("ey", 2, YANG_KEY)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200817 break;
818 case 'l':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200819 MOVE_INPUT(ctx, data, 1);
820 IF_KW_PREFIX("e", 1)
821 IF_KW("af-list", 7, YANG_LEAF_LIST)
822 else IF_KW("af", 2, YANG_LEAF)
823 else IF_KW("ngth", 4, YANG_LENGTH)
824 IF_KW_PREFIX_END
825 else IF_KW("ist", 3, YANG_LIST)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200826 break;
827 case 'm':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200828 MOVE_INPUT(ctx, data, 1);
829 IF_KW_PREFIX("a", 1)
830 IF_KW("ndatory", 7, YANG_MANDATORY)
831 else IF_KW("x-elements", 10, YANG_MAX_ELEMENTS)
832 IF_KW_PREFIX_END
833 else IF_KW("in-elements", 11, YANG_MIN_ELEMENTS)
834 else IF_KW("ust", 3, YANG_MUST)
835 else IF_KW_PREFIX("od", 2)
836 IF_KW("ule", 3, YANG_MODULE)
837 else IF_KW("ifier", 5, YANG_MODIFIER)
838 IF_KW_PREFIX_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200839 break;
840 case 'n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200841 MOVE_INPUT(ctx, data, 1);
842 IF_KW("amespace", 8, YANG_NAMESPACE)
843 else IF_KW("otification", 11, YANG_NOTIFICATION)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200844 break;
845 case 'o':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200846 MOVE_INPUT(ctx, data, 1);
847 IF_KW_PREFIX("r", 1)
848 IF_KW("dered-by", 8, YANG_ORDERED_BY)
849 else IF_KW("ganization", 10, YANG_ORGANIZATION)
850 IF_KW_PREFIX_END
851 else IF_KW("utput", 5, YANG_OUTPUT)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200852 break;
853 case 'p':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200854 MOVE_INPUT(ctx, data, 1);
855 IF_KW("ath", 3, YANG_PATH)
856 else IF_KW("attern", 6, YANG_PATTERN)
857 else IF_KW("osition", 7, YANG_POSITION)
858 else IF_KW_PREFIX("re", 2)
859 IF_KW("fix", 3, YANG_PREFIX)
860 else IF_KW("sence", 5, YANG_PRESENCE)
861 IF_KW_PREFIX_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200862 break;
863 case 'r':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200864 MOVE_INPUT(ctx, data, 1);
865 IF_KW("ange", 4, YANG_RANGE)
866 else IF_KW_PREFIX("e", 1)
867 IF_KW_PREFIX("f", 1)
868 IF_KW("erence", 6, YANG_REFERENCE)
869 else IF_KW("ine", 3, YANG_REFINE)
870 IF_KW_PREFIX_END
871 else IF_KW("quire-instance", 14, YANG_REQUIRE_INSTANCE)
872 else IF_KW("vision-date", 11, YANG_REVISION_DATE)
873 else IF_KW("vision", 6, YANG_REVISION)
874 IF_KW_PREFIX_END
875 else IF_KW("pc", 2, YANG_RPC)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200876 break;
877 case 's':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200878 MOVE_INPUT(ctx, data, 1);
879 IF_KW("tatus", 5, YANG_STATUS)
880 else IF_KW("ubmodule", 8, YANG_SUBMODULE)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200881 break;
882 case 't':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200883 MOVE_INPUT(ctx, data, 1);
884 IF_KW("ypedef", 6, YANG_TYPEDEF)
885 else IF_KW("ype", 3, YANG_TYPE)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200886 break;
887 case 'u':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200888 MOVE_INPUT(ctx, data, 1);
889 IF_KW_PREFIX("ni", 2)
890 IF_KW("que", 3, YANG_UNIQUE)
891 else IF_KW("ts", 2, YANG_UNITS)
892 IF_KW_PREFIX_END
893 else IF_KW("ses", 3, YANG_USES)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200894 break;
895 case 'v':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200896 MOVE_INPUT(ctx, data, 1);
897 IF_KW("alue", 4, YANG_VALUE)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200898 break;
899 case 'w':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200900 MOVE_INPUT(ctx, data, 1);
901 IF_KW("hen", 3, YANG_WHEN)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200902 break;
903 case 'y':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200904 MOVE_INPUT(ctx, data, 1);
905 IF_KW("ang-version", 11, YANG_YANG_VERSION)
906 else IF_KW("in-element", 10, YANG_YIN_ELEMENT)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200907 break;
908 case ';':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200909 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200910 *kw = YANG_SEMICOLON;
Radek Krejci626df482018-10-11 15:06:31 +0200911 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200912 case '{':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200913 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200914 *kw = YANG_LEFT_BRACE;
Radek Krejci626df482018-10-11 15:06:31 +0200915 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200916 case '}':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200917 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200918 *kw = YANG_RIGHT_BRACE;
Radek Krejci626df482018-10-11 15:06:31 +0200919 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200920 default:
921 break;
922 }
923
Radek Krejci0904c162019-01-02 15:03:59 +0100924#undef IF_KW
925#undef IF_KW_PREFIX
926#undef IF_KW_PREFIX_END
927
Michal Vasko7fbc8162018-09-17 10:35:16 +0200928 if (*kw != YANG_NONE) {
929 /* make sure we have the whole keyword */
930 switch (**data) {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200931 case '\n':
932 ++ctx->line;
933 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200934 case ' ':
935 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200936 /* mandatory "sep" */
937 break;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200938 case ':':
939 /* keyword is not actually a keyword, but prefix of an extension.
940 * To avoid repeated check of the prefix syntax, move to the point where the colon was read
941 * and we will be checking the keyword (extension instance) itself */
942 prefix = 1;
943 MOVE_INPUT(ctx, data, 1);
944 goto extension;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200945 case '{':
946 /* allowed only for input and output statements which can be without arguments */
947 if (*kw == YANG_INPUT || *kw == YANG_OUTPUT) {
948 break;
949 }
950 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200951 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200952 MOVE_INPUT(ctx, data, 1);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200953 LOGVAL_YANG(ctx, LY_VCODE_INSTREXP, (int)(*data - word_start), word_start,
954 "a keyword followed by a separator");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200955 return LY_EVALID;
956 }
957 } else {
958 /* still can be an extension */
959 prefix = 0;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200960extension:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200961 while (**data && (**data != ' ') && (**data != '\t') && (**data != '\n') && (**data != '{') && (**data != ';')) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200962 LY_CHECK_ERR_RET(ly_getutf8(data, &c, &len),
963 LOGVAL_YANG(ctx, LY_VCODE_INCHAR, (*data)[-len]), LY_EVALID);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200964 ++ctx->indent;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200965 /* check character validity */
966 LY_CHECK_RET(check_identifierchar(ctx, c, *data - len == word_start ? 1 : 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200967 }
968 if (!**data) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200969 LOGVAL_YANG(ctx, LY_VCODE_EOF);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200970 return LY_EVALID;
971 }
972
973 /* prefix is mandatory for extension instances */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200974 if (prefix != 2) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200975 LOGVAL_YANG(ctx, LY_VCODE_INSTREXP, (int)(*data - word_start), word_start, "a keyword");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200976 return LY_EVALID;
977 }
978
979 *kw = YANG_CUSTOM;
980 }
Radek Krejci626df482018-10-11 15:06:31 +0200981success:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200982 if (word_p) {
983 *word_p = (char *)word_start;
984 *word_len = *data - word_start;
985 }
986
987 return LY_SUCCESS;
988}
989
Michal Vaskoea5abea2018-09-18 13:10:54 +0200990/**
991 * @brief Parse extension instance substatements.
992 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200993 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200994 * @param[in,out] data Data to read from, always moved to currently handled character.
995 * @param[in] word Extension instance substatement name (keyword).
996 * @param[in] word_len Extension instance substatement name length.
997 * @param[in,out] child Children of this extension instance to add to.
998 *
999 * @return LY_ERR values.
1000 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001001static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001002parse_ext_substmt(struct ly_parser_ctx *ctx, const char **data, char *word, size_t word_len,
Michal Vasko7fbc8162018-09-17 10:35:16 +02001003 struct lysp_stmt **child)
1004{
1005 char *buf;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001006 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001007 enum yang_keyword kw;
1008 struct lysp_stmt *stmt, *par_child;
1009
1010 stmt = calloc(1, sizeof *stmt);
1011 LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM);
1012
Radek Krejci44ceedc2018-10-02 15:54:31 +02001013 stmt->stmt = lydict_insert(ctx->ctx, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001014
1015 /* get optional argument */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001016 LY_CHECK_RET(get_argument(ctx, data, Y_MAYBE_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001017
Radek Krejci0ae092d2018-09-20 16:43:19 +02001018 if (word) {
1019 if (buf) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001020 stmt->arg = lydict_insert_zc(ctx->ctx, word);
Radek Krejci0ae092d2018-09-20 16:43:19 +02001021 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001022 stmt->arg = lydict_insert(ctx->ctx, word, word_len);
Radek Krejci0ae092d2018-09-20 16:43:19 +02001023 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001024 }
1025
1026 /* insert into parent statements */
1027 if (!*child) {
1028 *child = stmt;
1029 } else {
1030 for (par_child = *child; par_child->next; par_child = par_child->next);
1031 par_child->next = stmt;
1032 }
1033
Radek Krejci6d6556c2018-11-08 09:37:45 +01001034 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, ) {
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001035 LY_CHECK_RET(parse_ext_substmt(ctx, data, word, word_len, &stmt->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001036 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001037 return ret;
1038}
1039
Michal Vaskoea5abea2018-09-18 13:10:54 +02001040/**
1041 * @brief Parse extension instance.
1042 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001043 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001044 * @param[in,out] data Data to read from, always moved to currently handled character.
1045 * @param[in] ext_name Extension instance substatement name (keyword).
1046 * @param[in] ext_name_len Extension instance substatement name length.
1047 * @param[in] insubstmt Type of the keyword this extension instance is a substatement of.
1048 * @param[in] insubstmt_index Index of the keyword instance this extension instance is a substatement of.
1049 * @param[in,out] exts Extension instances to add to.
1050 *
1051 * @return LY_ERR values.
1052 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001053static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001054parse_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 +02001055 uint32_t insubstmt_index, struct lysp_ext_instance **exts)
1056{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001057 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001058 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001059 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001060 struct lysp_ext_instance *e;
1061 enum yang_keyword kw;
1062
Radek Krejci2c4e7172018-10-19 15:56:26 +02001063 LY_ARRAY_NEW_RET(ctx->ctx, *exts, e, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001064
1065 /* store name and insubstmt info */
Radek Krejci44ceedc2018-10-02 15:54:31 +02001066 e->name = lydict_insert(ctx->ctx, ext_name, ext_name_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001067 e->insubstmt = insubstmt;
1068 e->insubstmt_index = insubstmt_index;
1069
1070 /* get optional argument */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001071 LY_CHECK_RET(get_argument(ctx, data, Y_MAYBE_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001072
Radek Krejci0ae092d2018-09-20 16:43:19 +02001073 if (word) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001074 INSERT_WORD(ctx, buf, e->argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001075 }
1076
Radek Krejci6d6556c2018-11-08 09:37:45 +01001077 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001078 LY_CHECK_RET(parse_ext_substmt(ctx, data, word, word_len, &e->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001079 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001080 return ret;
1081}
1082
Michal Vaskoea5abea2018-09-18 13:10:54 +02001083/**
1084 * @brief Parse a generic text field without specific constraints. Those are contact, organization,
1085 * description, etc...
1086 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001087 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001088 * @param[in,out] data Data to read from, always moved to currently handled character.
1089 * @param[in] substmt Type of this substatement.
1090 * @param[in] substmt_index Index of this substatement.
1091 * @param[in,out] value Place to store the parsed value.
1092 * @param[in] arg Type of the YANG keyword argument (of the value).
1093 * @param[in,out] exts Extension instances to add to.
1094 *
1095 * @return LY_ERR values.
1096 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001097static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001098parse_text_field(struct ly_parser_ctx *ctx, const char **data, LYEXT_SUBSTMT substmt, uint32_t substmt_index,
Michal Vasko7fbc8162018-09-17 10:35:16 +02001099 const char **value, enum yang_arg arg, struct lysp_ext_instance **exts)
1100{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001101 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001102 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001103 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001104 enum yang_keyword kw;
1105
1106 if (*value) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001107 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001108 return LY_EVALID;
1109 }
1110
1111 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001112 LY_CHECK_RET(get_argument(ctx, data, arg, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001113
1114 /* store value and spend buf if allocated */
Radek Krejci44ceedc2018-10-02 15:54:31 +02001115 INSERT_WORD(ctx, buf, *value, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001116
Radek Krejci6d6556c2018-11-08 09:37:45 +01001117 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001118 switch (kw) {
1119 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001120 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, substmt, substmt_index, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001121 break;
1122 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001123 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001124 return LY_EVALID;
1125 }
1126 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001127 return ret;
1128}
1129
Michal Vaskoea5abea2018-09-18 13:10:54 +02001130/**
1131 * @brief Parse the yang-version statement.
1132 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001133 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001134 * @param[in,out] data Data to read from, always moved to currently handled character.
1135 * @param[in] mod Module to store the parsed information in.
1136 *
1137 * @return LY_ERR values.
1138 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001139static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001140parse_yangversion(struct ly_parser_ctx *ctx, const char **data, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001141{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001142 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001143 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001144 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001145 enum yang_keyword kw;
1146
1147 if (mod->version) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001148 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001149 return LY_EVALID;
1150 }
1151
1152 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001153 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001154
1155 if ((word_len == 3) && !strncmp(word, "1.0", word_len)) {
1156 mod->version = LYS_VERSION_1_0;
1157 } else if ((word_len == 3) && !strncmp(word, "1.1", word_len)) {
1158 mod->version = LYS_VERSION_1_1;
1159 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001160 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001161 free(buf);
1162 return LY_EVALID;
1163 }
1164 free(buf);
1165
Radek Krejci6d6556c2018-11-08 09:37:45 +01001166 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001167 switch (kw) {
1168 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001169 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_VERSION, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001170 break;
1171 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001172 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001173 return LY_EVALID;
1174 }
1175 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001176 return ret;
1177}
1178
Michal Vaskoea5abea2018-09-18 13:10:54 +02001179/**
1180 * @brief Parse the belongs-to statement.
1181 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001182 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001183 * @param[in,out] data Data to read from, always moved to currently handled character.
1184 * @param[in,out] belongsto Place to store the parsed value.
1185 * @param[in,out] prefix Place to store the parsed belongs-to prefix value.
1186 * @param[in,out] exts Extension instances to add to.
1187 *
1188 * @return LY_ERR values.
1189 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001190static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001191parse_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 +02001192{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001193 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001194 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001195 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001196 enum yang_keyword kw;
1197
1198 if (*belongsto) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001199 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001200 return LY_EVALID;
1201 }
1202
1203 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001204 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001205
Radek Krejci44ceedc2018-10-02 15:54:31 +02001206 INSERT_WORD(ctx, buf, *belongsto, word, word_len);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001207 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001208 switch (kw) {
1209 case YANG_PREFIX:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001210 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_PREFIX, 0, prefix, Y_IDENTIF_ARG, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001211 break;
1212 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001213 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_BELONGSTO, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001214 break;
1215 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001216 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001217 return LY_EVALID;
1218 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001219 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001220 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001221checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001222 /* mandatory substatements */
1223 if (!*prefix) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001224 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001225 return LY_EVALID;
1226 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001227 return ret;
1228}
1229
Michal Vaskoea5abea2018-09-18 13:10:54 +02001230/**
1231 * @brief Parse the revision-date statement.
1232 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001233 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001234 * @param[in,out] data Data to read from, always moved to currently handled character.
1235 * @param[in,out] rev Array to store the parsed value in.
1236 * @param[in,out] exts Extension instances to add to.
1237 *
1238 * @return LY_ERR values.
1239 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001240static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001241parse_revisiondate(struct ly_parser_ctx *ctx, const char **data, char *rev, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001242{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001243 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001244 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001245 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001246 enum yang_keyword kw;
1247
1248 if (rev[0]) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001249 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001250 return LY_EVALID;
1251 }
1252
1253 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001254 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001255
1256 /* check value */
Radek Krejcibbe09a92018-11-08 09:36:54 +01001257 if (lysp_check_date(ctx, word, word_len, "revision-date")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001258 free(buf);
1259 return LY_EVALID;
1260 }
1261
1262 /* store value and spend buf if allocated */
1263 strncpy(rev, word, word_len);
1264 free(buf);
1265
Radek Krejci6d6556c2018-11-08 09:37:45 +01001266 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001267 switch (kw) {
1268 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001269 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_REVISIONDATE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001270 break;
1271 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001272 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001273 return LY_EVALID;
1274 }
1275 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001276 return ret;
1277}
1278
Michal Vaskoea5abea2018-09-18 13:10:54 +02001279/**
1280 * @brief Parse the include statement.
1281 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001282 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001283 * @param[in,out] data Data to read from, always moved to currently handled character.
1284 * @param[in,out] includes Parsed includes to add to.
1285 *
1286 * @return LY_ERR values.
1287 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001288static LY_ERR
Radek Krejcid33273d2018-10-25 14:55:52 +02001289parse_include(struct ly_parser_ctx *ctx, const char **data, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001290{
Radek Krejcid33273d2018-10-25 14:55:52 +02001291 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001292 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001293 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001294 enum yang_keyword kw;
1295 struct lysp_include *inc;
1296
Radek Krejcid33273d2018-10-25 14:55:52 +02001297 LY_ARRAY_NEW_RET(ctx->ctx, mod->includes, inc, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001298
1299 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001300 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001301
Radek Krejci086c7132018-10-26 15:29:04 +02001302 INSERT_WORD(ctx, buf, inc->name, word, word_len);
1303
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001304 /* submodules share the namespace with the module names, so there must not be
1305 * a module of the same name in the context, no need for revision matching */
1306 if (!strcmp(ctx->mod->name, inc->name) || ly_ctx_get_module_latest(ctx->ctx, inc->name)) {
1307 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Name collision between module and submodule of name \"%s\".", inc->name);
1308 return LY_EVALID;
1309 }
1310
Radek Krejci6d6556c2018-11-08 09:37:45 +01001311 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001312 switch (kw) {
1313 case YANG_DESCRIPTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01001314 YANG_CHECK_STMTVER2_RET(ctx, "description", "include");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001315 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 +02001316 break;
1317 case YANG_REFERENCE:
Radek Krejciceaf2122019-01-02 15:03:26 +01001318 YANG_CHECK_STMTVER2_RET(ctx, "reference", "include");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001319 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 +02001320 break;
1321 case YANG_REVISION_DATE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001322 LY_CHECK_RET(parse_revisiondate(ctx, data, inc->rev, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001323 break;
1324 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001325 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001326 break;
1327 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001328 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001329 return LY_EVALID;
1330 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001331 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001332 return ret;
1333}
1334
Michal Vaskoea5abea2018-09-18 13:10:54 +02001335/**
1336 * @brief Parse the import statement.
1337 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001338 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001339 * @param[in,out] data Data to read from, always moved to currently handled character.
1340 * @param[in,out] imports Parsed imports to add to.
1341 *
1342 * @return LY_ERR values.
1343 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001344static LY_ERR
Radek Krejci70853c52018-10-15 14:46:16 +02001345parse_import(struct ly_parser_ctx *ctx, const char **data, struct lysp_module *module)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001346{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001347 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001348 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001349 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001350 enum yang_keyword kw;
1351 struct lysp_import *imp;
1352
Radek Krejci2c4e7172018-10-19 15:56:26 +02001353 LY_ARRAY_NEW_RET(ctx->ctx, module->imports, imp, LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001354
1355 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001356 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02001357 INSERT_WORD(ctx, buf, imp->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001358
Radek Krejci6d6556c2018-11-08 09:37:45 +01001359 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001360 switch (kw) {
1361 case YANG_PREFIX:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001362 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_PREFIX, 0, &imp->prefix, Y_IDENTIF_ARG, &imp->exts));
Radek Krejci70853c52018-10-15 14:46:16 +02001363 LY_CHECK_RET(lysp_check_prefix(ctx, module, &imp->prefix), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001364 break;
1365 case YANG_DESCRIPTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01001366 YANG_CHECK_STMTVER2_RET(ctx, "description", "import");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001367 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 +02001368 break;
1369 case YANG_REFERENCE:
Radek Krejciceaf2122019-01-02 15:03:26 +01001370 YANG_CHECK_STMTVER2_RET(ctx, "reference", "import");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001371 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 +02001372 break;
1373 case YANG_REVISION_DATE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001374 LY_CHECK_RET(parse_revisiondate(ctx, data, imp->rev, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001375 break;
1376 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001377 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001378 break;
1379 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001380 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001381 return LY_EVALID;
1382 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001383 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001384 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001385checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001386 /* mandatory substatements */
Radek Krejci086c7132018-10-26 15:29:04 +02001387 LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001388
1389 return ret;
1390}
1391
Michal Vaskoea5abea2018-09-18 13:10:54 +02001392/**
1393 * @brief Parse the revision statement.
1394 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001395 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001396 * @param[in,out] data Data to read from, always moved to currently handled character.
1397 * @param[in,out] revs Parsed revisions to add to.
1398 *
1399 * @return LY_ERR values.
1400 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001401static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001402parse_revision(struct ly_parser_ctx *ctx, const char **data, struct lysp_revision **revs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001403{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001404 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001405 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001406 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001407 enum yang_keyword kw;
1408 struct lysp_revision *rev;
1409
Radek Krejci2c4e7172018-10-19 15:56:26 +02001410 LY_ARRAY_NEW_RET(ctx->ctx, *revs, rev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001411
1412 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001413 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001414
1415 /* check value */
Radek Krejcibbe09a92018-11-08 09:36:54 +01001416 if (lysp_check_date(ctx, word, word_len, "revision")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001417 return LY_EVALID;
1418 }
1419
Radek Krejcib7db73a2018-10-24 14:18:40 +02001420 strncpy(rev->date, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001421 free(buf);
1422
Radek Krejci6d6556c2018-11-08 09:37:45 +01001423 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001424 switch (kw) {
1425 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001426 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 +02001427 break;
1428 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001429 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 +02001430 break;
1431 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001432 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001433 break;
1434 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001435 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001436 return LY_EVALID;
1437 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001438 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001439 return ret;
1440}
1441
Michal Vaskoea5abea2018-09-18 13:10:54 +02001442/**
1443 * @brief Parse a generic text field that can have more instances such as base.
1444 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001445 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001446 * @param[in,out] data Data to read from, always moved to currently handled character.
1447 * @param[in] substmt Type of this substatement.
1448 * @param[in,out] texts Parsed values to add to.
1449 * @param[in] arg Type of the expected argument.
1450 * @param[in,out] exts Extension instances to add to.
1451 *
1452 * @return LY_ERR values.
1453 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001454static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001455parse_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 +02001456 struct lysp_ext_instance **exts)
1457{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001458 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001459 char *buf, *word;
Radek Krejci151a5b72018-10-19 14:21:44 +02001460 const char **item;
1461 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001462 enum yang_keyword kw;
1463
1464 /* allocate new pointer */
Radek Krejci2c4e7172018-10-19 15:56:26 +02001465 LY_ARRAY_NEW_RET(ctx->ctx, *texts, item, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001466
1467 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001468 LY_CHECK_RET(get_argument(ctx, data, arg, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001469
Radek Krejci151a5b72018-10-19 14:21:44 +02001470 INSERT_WORD(ctx, buf, *item, word, word_len);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001471 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001472 switch (kw) {
1473 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001474 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, substmt, LY_ARRAY_SIZE(*texts) - 1, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001475 break;
1476 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001477 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001478 return LY_EVALID;
1479 }
1480 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001481 return ret;
1482}
1483
Michal Vaskoea5abea2018-09-18 13:10:54 +02001484/**
1485 * @brief Parse the config statement.
1486 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001487 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001488 * @param[in,out] data Data to read from, always moved to currently handled character.
1489 * @param[in,out] flags Flags to add to.
1490 * @param[in,out] exts Extension instances to add to.
1491 *
1492 * @return LY_ERR values.
1493 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001494static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001495parse_config(struct ly_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001496{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001497 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001498 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001499 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001500 enum yang_keyword kw;
1501
1502 if (*flags & LYS_CONFIG_MASK) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001503 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001504 return LY_EVALID;
1505 }
1506
1507 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001508 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001509
1510 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1511 *flags |= LYS_CONFIG_W;
1512 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1513 *flags |= LYS_CONFIG_R;
1514 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001515 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001516 free(buf);
1517 return LY_EVALID;
1518 }
1519 free(buf);
1520
Radek Krejci6d6556c2018-11-08 09:37:45 +01001521 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001522 switch (kw) {
1523 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001524 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_CONFIG, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001525 break;
1526 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001527 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001528 return LY_EVALID;
1529 }
1530 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001531 return ret;
1532}
1533
Michal Vaskoea5abea2018-09-18 13:10:54 +02001534/**
1535 * @brief Parse the mandatory statement.
1536 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001537 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001538 * @param[in,out] data Data to read from, always moved to currently handled character.
1539 * @param[in,out] flags Flags to add to.
1540 * @param[in,out] exts Extension instances to add to.
1541 *
1542 * @return LY_ERR values.
1543 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001544static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001545parse_mandatory(struct ly_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001546{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001547 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001548 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001549 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001550 enum yang_keyword kw;
1551
1552 if (*flags & LYS_MAND_MASK) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001553 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001554 return LY_EVALID;
1555 }
1556
1557 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001558 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001559
1560 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1561 *flags |= LYS_MAND_TRUE;
1562 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1563 *flags |= LYS_MAND_FALSE;
1564 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001565 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001566 free(buf);
1567 return LY_EVALID;
1568 }
1569 free(buf);
1570
Radek Krejci6d6556c2018-11-08 09:37:45 +01001571 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001572 switch (kw) {
1573 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001574 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MANDATORY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001575 break;
1576 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001577 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001578 return LY_EVALID;
1579 }
1580 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001581 return ret;
1582}
1583
Michal Vaskoea5abea2018-09-18 13:10:54 +02001584/**
1585 * @brief Parse a restriction such as range or length.
1586 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001587 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001588 * @param[in,out] data Data to read from, always moved to currently handled character.
1589 * @param[in] restr_kw Type of this particular restriction.
1590 * @param[in,out] exts Extension instances to add to.
1591 *
1592 * @return LY_ERR values.
1593 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001594static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001595parse_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 +02001596{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001597 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001598 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001599 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001600 enum yang_keyword kw;
1601
1602 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001603 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001604
Radek Krejci44ceedc2018-10-02 15:54:31 +02001605 INSERT_WORD(ctx, buf, restr->arg, word, word_len);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001606 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001607 switch (kw) {
1608 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001609 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 +02001610 break;
1611 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001612 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 +02001613 break;
1614 case YANG_ERROR_APP_TAG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001615 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 +02001616 break;
1617 case YANG_ERROR_MESSAGE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001618 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 +02001619 break;
1620 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001621 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001622 break;
1623 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001624 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001625 return LY_EVALID;
1626 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001627 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001628 return ret;
1629}
1630
Michal Vaskoea5abea2018-09-18 13:10:54 +02001631/**
1632 * @brief Parse a restriction that can have more instances such as must.
1633 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001634 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001635 * @param[in,out] data Data to read from, always moved to currently handled character.
1636 * @param[in] restr_kw Type of this particular restriction.
1637 * @param[in,out] restrs Restrictions to add to.
1638 *
1639 * @return LY_ERR values.
1640 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001641static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001642parse_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 +02001643{
1644 struct lysp_restr *restr;
1645
Radek Krejci2c4e7172018-10-19 15:56:26 +02001646 LY_ARRAY_NEW_RET(ctx->ctx, *restrs, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001647 return parse_restr(ctx, data, restr_kw, restr);
1648}
1649
Michal Vaskoea5abea2018-09-18 13:10:54 +02001650/**
1651 * @brief Parse the status statement.
1652 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001653 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001654 * @param[in,out] data Data to read from, always moved to currently handled character.
1655 * @param[in,out] flags Flags to add to.
1656 * @param[in,out] exts Extension instances to add to.
1657 *
1658 * @return LY_ERR values.
1659 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001660static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001661parse_status(struct ly_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001662{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001663 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001664 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001665 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001666 enum yang_keyword kw;
1667
1668 if (*flags & LYS_STATUS_MASK) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001669 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001670 return LY_EVALID;
1671 }
1672
1673 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001674 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001675
1676 if ((word_len == 7) && !strncmp(word, "current", word_len)) {
1677 *flags |= LYS_STATUS_CURR;
1678 } else if ((word_len == 10) && !strncmp(word, "deprecated", word_len)) {
1679 *flags |= LYS_STATUS_DEPRC;
1680 } else if ((word_len == 8) && !strncmp(word, "obsolete", word_len)) {
1681 *flags |= LYS_STATUS_OBSLT;
1682 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001683 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001684 free(buf);
1685 return LY_EVALID;
1686 }
1687 free(buf);
1688
Radek Krejci6d6556c2018-11-08 09:37:45 +01001689 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001690 switch (kw) {
1691 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001692 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_STATUS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001693 break;
1694 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001695 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001696 return LY_EVALID;
1697 }
1698 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001699 return ret;
1700}
1701
Michal Vaskoea5abea2018-09-18 13:10:54 +02001702/**
1703 * @brief Parse the when statement.
1704 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001705 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001706 * @param[in,out] data Data to read from, always moved to currently handled character.
1707 * @param[in,out] when_p When pointer to parse to.
1708 *
1709 * @return LY_ERR values.
1710 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001711static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001712parse_when(struct ly_parser_ctx *ctx, const char **data, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001713{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001714 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001715 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001716 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001717 enum yang_keyword kw;
1718 struct lysp_when *when;
1719
1720 if (*when_p) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001721 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001722 return LY_EVALID;
1723 }
1724
1725 when = calloc(1, sizeof *when);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001726 LY_CHECK_ERR_RET(!when, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001727 *when_p = when;
1728
1729 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001730 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02001731 INSERT_WORD(ctx, buf, when->cond, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001732
Radek Krejci6d6556c2018-11-08 09:37:45 +01001733 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001734 switch (kw) {
1735 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001736 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 +02001737 break;
1738 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001739 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 +02001740 break;
1741 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001742 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001743 break;
1744 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001745 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001746 return LY_EVALID;
1747 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001748 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001749 return ret;
1750}
1751
Michal Vaskoea5abea2018-09-18 13:10:54 +02001752/**
1753 * @brief Parse the anydata or anyxml statement.
1754 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001755 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001756 * @param[in,out] data Data to read from, always moved to currently handled character.
1757 * @param[in] kw Type of this particular keyword.
1758 * @param[in,out] siblings Siblings to add to.
1759 *
1760 * @return LY_ERR values.
1761 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001762static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001763parse_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 +02001764{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001765 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001766 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001767 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001768 struct lysp_node *iter;
1769 struct lysp_node_anydata *any;
1770
1771 /* create structure */
1772 any = calloc(1, sizeof *any);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001773 LY_CHECK_ERR_RET(!any, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001774 any->nodetype = kw == YANG_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001775 any->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001776
1777 /* insert into siblings */
1778 if (!*siblings) {
1779 *siblings = (struct lysp_node *)any;
1780 } else {
1781 for (iter = *siblings; iter->next; iter = iter->next);
1782 iter->next = (struct lysp_node *)any;
1783 }
1784
1785 /* get name */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001786 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02001787 INSERT_WORD(ctx, buf, any->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001788
1789 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01001790 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001791 switch (kw) {
1792 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001793 LY_CHECK_RET(parse_config(ctx, data, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001794 break;
1795 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001796 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 +02001797 break;
1798 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001799 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 +02001800 break;
1801 case YANG_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001802 LY_CHECK_RET(parse_mandatory(ctx, data, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001803 break;
1804 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001805 LY_CHECK_RET(parse_restrs(ctx, data, kw, &any->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001806 break;
1807 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001808 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 +02001809 break;
1810 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001811 LY_CHECK_RET(parse_status(ctx, data, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001812 break;
1813 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001814 LY_CHECK_RET(parse_when(ctx, data, &any->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001815 break;
1816 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001817 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001818 break;
1819 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001820 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
Radek Krejcic59bc972018-09-17 16:13:06 +02001821 (any->nodetype & LYS_ANYDATA) == LYS_ANYDATA ? ly_stmt2str(YANG_ANYDATA) : ly_stmt2str(YANG_ANYXML));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001822 return LY_EVALID;
1823 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001824 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001825 return ret;
1826}
1827
Michal Vaskoea5abea2018-09-18 13:10:54 +02001828/**
1829 * @brief Parse the value or position statement. Substatement of type enum statement.
1830 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001831 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001832 * @param[in,out] data Data to read from, always moved to currently handled character.
1833 * @param[in] val_kw Type of this particular keyword.
1834 * @param[in,out] value Value to write to.
1835 * @param[in,out] flags Flags to write to.
1836 * @param[in,out] exts Extension instances to add to.
1837 *
1838 * @return LY_ERR values.
1839 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001840static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001841parse_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 +02001842 struct lysp_ext_instance **exts)
1843{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001844 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001845 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001846 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001847 long int num;
1848 unsigned long int unum;
1849 enum yang_keyword kw;
1850
1851 if (*flags & LYS_SET_VALUE) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001852 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001853 return LY_EVALID;
1854 }
1855 *flags |= LYS_SET_VALUE;
1856
1857 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001858 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001859
1860 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 +02001861 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001862 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001863 }
1864
1865 errno = 0;
1866 if (val_kw == YANG_VALUE) {
1867 num = strtol(word, &ptr, 10);
Radek Krejci8b764662018-11-14 14:15:13 +01001868 if (num < INT64_C(-2147483648) || num > INT64_C(2147483647)) {
1869 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
1870 goto error;
1871 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001872 } else {
1873 unum = strtoul(word, &ptr, 10);
Radek Krejci8b764662018-11-14 14:15:13 +01001874 if (unum > UINT64_C(4294967295)) {
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 }
1879 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001880 if ((size_t)(ptr - word) != word_len) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001881 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001882 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001883 }
1884 if (errno == ERANGE) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001885 LOGVAL_YANG(ctx, LY_VCODE_OOB, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001886 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001887 }
1888 if (val_kw == YANG_VALUE) {
1889 *value = num;
1890 } else {
1891 *value = unum;
1892 }
1893 free(buf);
1894
Radek Krejci6d6556c2018-11-08 09:37:45 +01001895 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001896 switch (kw) {
1897 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001898 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, val_kw == YANG_VALUE ? LYEXT_SUBSTMT_VALUE : LYEXT_SUBSTMT_POSITION, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001899 break;
1900 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001901 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001902 return LY_EVALID;
1903 }
1904 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001905 return ret;
Radek Krejci8b764662018-11-14 14:15:13 +01001906
1907error:
1908 free(buf);
1909 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001910}
1911
Michal Vaskoea5abea2018-09-18 13:10:54 +02001912/**
1913 * @brief Parse the enum or bit statement. Substatement of type statement.
1914 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001915 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001916 * @param[in,out] data Data to read from, always moved to currently handled character.
1917 * @param[in] enum_kw Type of this particular keyword.
1918 * @param[in,out] enums Enums or bits to add to.
1919 *
1920 * @return LY_ERR values.
1921 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001922static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001923parse_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 +02001924{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001925 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001926 char *buf, *word;
Radek Krejci8b764662018-11-14 14:15:13 +01001927 size_t word_len, u;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001928 enum yang_keyword kw;
1929 struct lysp_type_enum *enm;
1930
Radek Krejci2c4e7172018-10-19 15:56:26 +02001931 LY_ARRAY_NEW_RET(ctx->ctx, *enums, enm, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001932
1933 /* get value */
Radek Krejci8b764662018-11-14 14:15:13 +01001934 LY_CHECK_RET(get_argument(ctx, data, enum_kw == YANG_ENUM ? Y_STR_ARG : Y_IDENTIF_ARG, &word, &buf, &word_len));
1935 if (enum_kw == YANG_ENUM) {
1936 if (!word_len) {
1937 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Enum name must not be zero-length.");
1938 free(buf);
1939 return LY_EVALID;
1940 } else if (isspace(word[0]) || isspace(word[word_len - 1])) {
1941 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Enum name must not have any leading or trailing whitespaces (\"%.*s\").",
1942 word_len, word);
1943 free(buf);
1944 return LY_EVALID;
1945 } else {
1946 for (u = 0; u < word_len; ++u) {
1947 if (iscntrl(word[u])) {
1948 LOGWRN(ctx->ctx, "Control characters in enum name should be avoided (\"%.*s\", character number %d).",
1949 word_len, word, u + 1);
1950 break;
1951 }
1952 }
1953 }
1954 } else { /* YANG_BIT */
1955
1956 }
Radek Krejci44ceedc2018-10-02 15:54:31 +02001957 INSERT_WORD(ctx, buf, enm->name, word, word_len);
Radek Krejci8b764662018-11-14 14:15:13 +01001958 CHECK_UNIQUENESS(ctx, *enums, name, ly_stmt2str(enum_kw), enm->name);
1959
Radek Krejci6d6556c2018-11-08 09:37:45 +01001960 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001961 switch (kw) {
1962 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001963 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 +02001964 break;
1965 case YANG_IF_FEATURE:
Radek Krejciceaf2122019-01-02 15:03:26 +01001966 YANG_CHECK_STMTVER2_RET(ctx, "if-feature", ly_stmt2str(enum_kw));
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001967 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 +02001968 break;
1969 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001970 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 +02001971 break;
1972 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001973 LY_CHECK_RET(parse_status(ctx, data, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001974 break;
1975 case YANG_VALUE:
1976 case YANG_POSITION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001977 LY_CHECK_RET(parse_type_enum_value_pos(ctx, data, kw, &enm->value, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001978 break;
1979 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001980 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001981 break;
1982 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001983 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001984 return LY_EVALID;
1985 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001986 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001987 return ret;
1988}
1989
Michal Vaskoea5abea2018-09-18 13:10:54 +02001990/**
1991 * @brief Parse the fraction-digits statement. Substatement of type statement.
1992 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001993 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001994 * @param[in,out] data Data to read from, always moved to currently handled character.
1995 * @param[in,out] fracdig Value to write to.
1996 * @param[in,out] exts Extension instances to add to.
1997 *
1998 * @return LY_ERR values.
1999 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002000static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002001parse_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 +02002002{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002003 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002004 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002005 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002006 unsigned long int num;
2007 enum yang_keyword kw;
2008
2009 if (*fracdig) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002010 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002011 return LY_EVALID;
2012 }
2013
2014 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002015 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002016
2017 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002018 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002019 free(buf);
2020 return LY_EVALID;
2021 }
2022
2023 errno = 0;
2024 num = strtoul(word, &ptr, 10);
2025 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002026 if ((size_t)(ptr - word) != word_len) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002027 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002028 free(buf);
2029 return LY_EVALID;
2030 }
2031 if ((errno == ERANGE) || (num > 18)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002032 LOGVAL_YANG(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002033 free(buf);
2034 return LY_EVALID;
2035 }
2036 *fracdig = num;
2037 free(buf);
2038
Radek Krejci6d6556c2018-11-08 09:37:45 +01002039 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002040 switch (kw) {
2041 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002042 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_FRACDIGITS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002043 break;
2044 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002045 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002046 return LY_EVALID;
2047 }
2048 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002049 return ret;
2050}
2051
Michal Vaskoea5abea2018-09-18 13:10:54 +02002052/**
2053 * @brief Parse the require-instance statement. Substatement of type statement.
2054 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002055 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002056 * @param[in,out] data Data to read from, always moved to currently handled character.
2057 * @param[in,out] reqinst Value to write to.
2058 * @param[in,out] flags Flags to write to.
2059 * @param[in,out] exts Extension instances to add to.
2060 *
2061 * @return LY_ERR values.
2062 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002063static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002064parse_type_reqinstance(struct ly_parser_ctx *ctx, const char **data, uint8_t *reqinst, uint16_t *flags,
Michal Vasko7fbc8162018-09-17 10:35:16 +02002065 struct lysp_ext_instance **exts)
2066{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002067 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002068 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002069 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002070 enum yang_keyword kw;
2071
2072 if (*flags & LYS_SET_REQINST) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002073 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002074 return LY_EVALID;
2075 }
2076 *flags |= LYS_SET_REQINST;
2077
2078 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002079 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002080
2081 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
2082 *reqinst = 1;
2083 } else if ((word_len != 5) || strncmp(word, "false", word_len)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002084 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002085 free(buf);
2086 return LY_EVALID;
2087 }
2088 free(buf);
2089
Radek Krejci6d6556c2018-11-08 09:37:45 +01002090 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002091 switch (kw) {
2092 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002093 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_REQINSTANCE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002094 break;
2095 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002096 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002097 return LY_EVALID;
2098 }
2099 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002100 return ret;
2101}
2102
Michal Vaskoea5abea2018-09-18 13:10:54 +02002103/**
2104 * @brief Parse the modifier statement. Substatement of type pattern statement.
2105 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002106 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002107 * @param[in,out] data Data to read from, always moved to currently handled character.
2108 * @param[in,out] pat Value to write to.
2109 * @param[in,out] exts Extension instances to add to.
2110 *
2111 * @return LY_ERR values.
2112 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002113static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002114parse_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 +02002115{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002116 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002117 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002118 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002119 enum yang_keyword kw;
2120
2121 if ((*pat)[0] == 0x15) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002122 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002123 return LY_EVALID;
2124 }
2125
2126 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002127 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002128
2129 if ((word_len != 12) || strncmp(word, "invert-match", word_len)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002130 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002131 free(buf);
2132 return LY_EVALID;
2133 }
2134 free(buf);
2135
2136 /* replace the value in the dictionary */
2137 buf = malloc(strlen(*pat) + 1);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002138 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002139 strcpy(buf, *pat);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002140 lydict_remove(ctx->ctx, *pat);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002141
2142 assert(buf[0] == 0x06);
2143 buf[0] = 0x15;
Radek Krejci44ceedc2018-10-02 15:54:31 +02002144 *pat = lydict_insert_zc(ctx->ctx, buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002145
Radek Krejci6d6556c2018-11-08 09:37:45 +01002146 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002147 switch (kw) {
2148 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002149 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MODIFIER, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002150 break;
2151 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002152 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002153 return LY_EVALID;
2154 }
2155 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002156 return ret;
2157}
2158
Michal Vaskoea5abea2018-09-18 13:10:54 +02002159/**
2160 * @brief Parse the pattern statement. Substatement of type statement.
2161 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002162 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002163 * @param[in,out] data Data to read from, always moved to currently handled character.
2164 * @param[in,out] patterns Restrictions to add to.
2165 *
2166 * @return LY_ERR values.
2167 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002168static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002169parse_type_pattern(struct ly_parser_ctx *ctx, const char **data, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002170{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002171 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002172 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002173 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002174 enum yang_keyword kw;
2175 struct lysp_restr *restr;
2176
Radek Krejci2c4e7172018-10-19 15:56:26 +02002177 LY_ARRAY_NEW_RET(ctx->ctx, *patterns, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002178
2179 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002180 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002181
2182 /* add special meaning first byte */
2183 if (buf) {
2184 buf = realloc(buf, word_len + 2);
2185 word = buf;
2186 } else {
2187 buf = malloc(word_len + 2);
2188 }
Radek Krejci44ceedc2018-10-02 15:54:31 +02002189 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejci86d106e2018-10-18 09:53:19 +02002190 memmove(buf + 1, word, word_len);
2191 buf[0] = 0x06; /* pattern's default regular-match flag */
2192 buf[word_len + 1] = '\0'; /* terminating NULL byte */
2193 restr->arg = lydict_insert_zc(ctx->ctx, buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002194
Radek Krejci6d6556c2018-11-08 09:37:45 +01002195 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002196 switch (kw) {
2197 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002198 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 +02002199 break;
2200 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002201 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 +02002202 break;
2203 case YANG_ERROR_APP_TAG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002204 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 +02002205 break;
2206 case YANG_ERROR_MESSAGE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002207 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 +02002208 break;
2209 case YANG_MODIFIER:
Radek Krejciceaf2122019-01-02 15:03:26 +01002210 YANG_CHECK_STMTVER2_RET(ctx, "modifier", "pattern");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002211 LY_CHECK_RET(parse_type_pattern_modifier(ctx, data, &restr->arg, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002212 break;
2213 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002214 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002215 break;
2216 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002217 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002218 return LY_EVALID;
2219 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002220 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002221 return ret;
2222}
2223
Michal Vaskoea5abea2018-09-18 13:10:54 +02002224/**
2225 * @brief Parse the type statement.
2226 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002227 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002228 * @param[in,out] data Data to read from, always moved to currently handled character.
2229 * @param[in,out] type Type to wrote to.
2230 *
2231 * @return LY_ERR values.
2232 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002233static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002234parse_type(struct ly_parser_ctx *ctx, const char **data, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002235{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002236 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002237 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002238 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002239 enum yang_keyword kw;
2240 struct lysp_type *nest_type;
2241
2242 if (type->name) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002243 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002244 return LY_EVALID;
2245 }
2246
2247 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002248 LY_CHECK_RET(get_argument(ctx, data, Y_PREF_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002249 INSERT_WORD(ctx, buf, type->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002250
Radek Krejci6d6556c2018-11-08 09:37:45 +01002251 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002252 switch (kw) {
2253 case YANG_BASE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002254 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 +01002255 type->flags |= LYS_SET_BASE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002256 break;
2257 case YANG_BIT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002258 LY_CHECK_RET(parse_type_enum(ctx, data, kw, &type->bits));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002259 type->flags |= LYS_SET_BIT;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002260 break;
2261 case YANG_ENUM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002262 LY_CHECK_RET(parse_type_enum(ctx, data, kw, &type->enums));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002263 type->flags |= LYS_SET_ENUM;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002264 break;
2265 case YANG_FRACTION_DIGITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002266 LY_CHECK_RET(parse_type_fracdigits(ctx, data, &type->fraction_digits, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002267 type->flags |= LYS_SET_FRDIGITS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002268 break;
2269 case YANG_LENGTH:
2270 if (type->length) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002271 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002272 return LY_EVALID;
2273 }
2274 type->length = calloc(1, sizeof *type->length);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002275 LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002276
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002277 LY_CHECK_RET(parse_restr(ctx, data, kw, type->length));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002278 type->flags |= LYS_SET_LENGTH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002279 break;
2280 case YANG_PATH:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002281 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 +01002282 type->flags |= LYS_SET_PATH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002283 break;
2284 case YANG_PATTERN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002285 LY_CHECK_RET(parse_type_pattern(ctx, data, &type->patterns));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002286 type->flags |= LYS_SET_PATTERN;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002287 break;
2288 case YANG_RANGE:
2289 if (type->range) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002290 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002291 return LY_EVALID;
2292 }
2293 type->range = calloc(1, sizeof *type->range);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002294 LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx->ctx), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002295
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002296 LY_CHECK_RET(parse_restr(ctx, data, kw, type->range));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002297 type->flags |= LYS_SET_RANGE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002298 break;
2299 case YANG_REQUIRE_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002300 LY_CHECK_RET(parse_type_reqinstance(ctx, data, &type->require_instance, &type->flags, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002301 /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002302 break;
2303 case YANG_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002304 LY_ARRAY_NEW_RET(ctx->ctx, type->types, nest_type, LY_EMEM);
2305 LY_CHECK_RET(parse_type(ctx, data, nest_type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002306 type->flags |= LYS_SET_TYPE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002307 break;
2308 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002309 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &type->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002310 break;
2311 default:
Radek Krejci8b764662018-11-14 14:15:13 +01002312 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002313 return LY_EVALID;
2314 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002315 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002316 return ret;
2317}
2318
Michal Vaskoea5abea2018-09-18 13:10:54 +02002319/**
2320 * @brief Parse the leaf statement.
2321 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002322 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002323 * @param[in,out] data Data to read from, always moved to currently handled character.
2324 * @param[in,out] siblings Siblings to add to.
2325 *
2326 * @return LY_ERR values.
2327 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002328static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002329parse_leaf(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002330{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002331 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002332 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002333 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002334 enum yang_keyword kw;
2335 struct lysp_node *iter;
2336 struct lysp_node_leaf *leaf;
2337
2338 /* create structure */
2339 leaf = calloc(1, sizeof *leaf);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002340 LY_CHECK_ERR_RET(!leaf, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002341 leaf->nodetype = LYS_LEAF;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002342 leaf->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002343
2344 /* insert into siblings */
2345 if (!*siblings) {
2346 *siblings = (struct lysp_node *)leaf;
2347 } else {
2348 for (iter = *siblings; iter->next; iter = iter->next);
2349 iter->next = (struct lysp_node *)leaf;
2350 }
2351
2352 /* get name */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002353 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002354 INSERT_WORD(ctx, buf, leaf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002355
2356 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01002357 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002358 switch (kw) {
2359 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002360 LY_CHECK_RET(parse_config(ctx, data, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002361 break;
2362 case YANG_DEFAULT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002363 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 +02002364 break;
2365 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002366 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 +02002367 break;
2368 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002369 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 +02002370 break;
2371 case YANG_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002372 LY_CHECK_RET(parse_mandatory(ctx, data, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002373 break;
2374 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002375 LY_CHECK_RET(parse_restrs(ctx, data, kw, &leaf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002376 break;
2377 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002378 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 +02002379 break;
2380 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002381 LY_CHECK_RET(parse_status(ctx, data, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002382 break;
2383 case YANG_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002384 LY_CHECK_RET(parse_type(ctx, data, &leaf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002385 break;
2386 case YANG_UNITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002387 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 +02002388 break;
2389 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002390 LY_CHECK_RET(parse_when(ctx, data, &leaf->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002391 break;
2392 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002393 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002394 break;
2395 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002396 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002397 return LY_EVALID;
2398 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002399 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002400 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002401checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002402 /* mandatory substatements */
2403 if (!leaf->type.name) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002404 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002405 return LY_EVALID;
2406 }
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002407 if ((leaf->flags & LYS_MAND_TRUE) && (leaf->dflt)) {
2408 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMSCOMB, "mandatory", "default", "leaf");
2409 return LY_EVALID;
2410 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002411
2412 return ret;
2413}
2414
Michal Vaskoea5abea2018-09-18 13:10:54 +02002415/**
2416 * @brief Parse the max-elements statement.
2417 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002418 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002419 * @param[in,out] data Data to read from, always moved to currently handled character.
2420 * @param[in,out] max Value to write to.
2421 * @param[in,out] flags Flags to write to.
2422 * @param[in,out] exts Extension instances to add to.
2423 *
2424 * @return LY_ERR values.
2425 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002426static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002427parse_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 +02002428{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002429 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002430 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002431 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002432 unsigned long int num;
2433 enum yang_keyword kw;
2434
2435 if (*flags & LYS_SET_MAX) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002436 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002437 return LY_EVALID;
2438 }
2439 *flags |= LYS_SET_MAX;
2440
2441 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002442 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002443
2444 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002445 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002446 free(buf);
2447 return LY_EVALID;
2448 }
2449
2450 if (strncmp(word, "unbounded", word_len)) {
2451 errno = 0;
2452 num = strtoul(word, &ptr, 10);
2453 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002454 if ((size_t)(ptr - word) != word_len) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002455 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002456 free(buf);
2457 return LY_EVALID;
2458 }
2459 if ((errno == ERANGE) || (num > UINT32_MAX)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002460 LOGVAL_YANG(ctx, LY_VCODE_OOB, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002461 free(buf);
2462 return LY_EVALID;
2463 }
2464
2465 *max = num;
2466 }
2467 free(buf);
2468
Radek Krejci6d6556c2018-11-08 09:37:45 +01002469 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002470 switch (kw) {
2471 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002472 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MAX, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002473 break;
2474 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002475 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002476 return LY_EVALID;
2477 }
2478 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002479 return ret;
2480}
2481
Michal Vaskoea5abea2018-09-18 13:10:54 +02002482/**
2483 * @brief Parse the min-elements statement.
2484 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002485 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002486 * @param[in,out] data Data to read from, always moved to currently handled character.
2487 * @param[in,out] min Value to write to.
2488 * @param[in,out] flags Flags to write to.
2489 * @param[in,out] exts Extension instances to add to.
2490 *
2491 * @return LY_ERR values.
2492 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002493static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002494parse_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 +02002495{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002496 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002497 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002498 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002499 unsigned long int num;
2500 enum yang_keyword kw;
2501
2502 if (*flags & LYS_SET_MIN) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002503 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002504 return LY_EVALID;
2505 }
2506 *flags |= LYS_SET_MIN;
2507
2508 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002509 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002510
2511 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002512 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002513 free(buf);
2514 return LY_EVALID;
2515 }
2516
2517 errno = 0;
2518 num = strtoul(word, &ptr, 10);
2519 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002520 if ((size_t)(ptr - word) != word_len) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002521 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002522 free(buf);
2523 return LY_EVALID;
2524 }
2525 if ((errno == ERANGE) || (num > UINT32_MAX)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002526 LOGVAL_YANG(ctx, LY_VCODE_OOB, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002527 free(buf);
2528 return LY_EVALID;
2529 }
2530 *min = num;
2531 free(buf);
2532
Radek Krejci6d6556c2018-11-08 09:37:45 +01002533 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002534 switch (kw) {
2535 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002536 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MIN, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002537 break;
2538 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002539 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002540 return LY_EVALID;
2541 }
2542 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002543 return ret;
2544}
2545
Michal Vaskoea5abea2018-09-18 13:10:54 +02002546/**
2547 * @brief Parse the ordered-by statement.
2548 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002549 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002550 * @param[in,out] data Data to read from, always moved to currently handled character.
2551 * @param[in,out] flags Flags to write to.
2552 * @param[in,out] exts Extension instances to add to.
2553 *
2554 * @return LY_ERR values.
2555 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002556static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002557parse_orderedby(struct ly_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002558{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002559 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002560 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002561 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002562 enum yang_keyword kw;
2563
2564 if (*flags & LYS_ORDBY_MASK) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002565 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002566 return LY_EVALID;
2567 }
2568
2569 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002570 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002571
2572 if ((word_len == 6) && !strncmp(word, "system", word_len)) {
2573 *flags |= LYS_ORDBY_SYSTEM;
2574 } else if ((word_len == 4) && !strncmp(word, "user", word_len)) {
2575 *flags |= LYS_ORDBY_USER;
2576 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002577 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002578 free(buf);
2579 return LY_EVALID;
2580 }
2581 free(buf);
2582
Radek Krejci6d6556c2018-11-08 09:37:45 +01002583 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002584 switch (kw) {
2585 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002586 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_ORDEREDBY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002587 break;
2588 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002589 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002590 return LY_EVALID;
2591 }
2592 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002593 return ret;
2594}
2595
Michal Vaskoea5abea2018-09-18 13:10:54 +02002596/**
2597 * @brief Parse the leaf-list statement.
2598 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002599 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002600 * @param[in,out] data Data to read from, always moved to currently handled character.
2601 * @param[in,out] siblings Siblings to add to.
2602 *
2603 * @return LY_ERR values.
2604 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002605static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002606parse_leaflist(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002607{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002608 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002609 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002610 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002611 enum yang_keyword kw;
2612 struct lysp_node *iter;
2613 struct lysp_node_leaflist *llist;
2614
2615 /* create structure */
2616 llist = calloc(1, sizeof *llist);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002617 LY_CHECK_ERR_RET(!llist, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002618 llist->nodetype = LYS_LEAFLIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002619 llist->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002620
2621 /* insert into siblings */
2622 if (!*siblings) {
2623 *siblings = (struct lysp_node *)llist;
2624 } else {
2625 for (iter = *siblings; iter->next; iter = iter->next);
2626 iter->next = (struct lysp_node *)llist;
2627 }
2628
2629 /* get name */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002630 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002631 INSERT_WORD(ctx, buf, llist->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002632
2633 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01002634 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002635 switch (kw) {
2636 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002637 LY_CHECK_RET(parse_config(ctx, data, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002638 break;
2639 case YANG_DEFAULT:
Radek Krejciceaf2122019-01-02 15:03:26 +01002640 YANG_CHECK_STMTVER2_RET(ctx, "default", "leaf-list");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002641 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 +02002642 break;
2643 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002644 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 +02002645 break;
2646 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002647 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 +02002648 break;
2649 case YANG_MAX_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002650 LY_CHECK_RET(parse_maxelements(ctx, data, &llist->max, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002651 break;
2652 case YANG_MIN_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002653 LY_CHECK_RET(parse_minelements(ctx, data, &llist->min, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002654 break;
2655 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002656 LY_CHECK_RET(parse_restrs(ctx, data, kw, &llist->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002657 break;
2658 case YANG_ORDERED_BY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002659 LY_CHECK_RET(parse_orderedby(ctx, data, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002660 break;
2661 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002662 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 +02002663 break;
2664 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002665 LY_CHECK_RET(parse_status(ctx, data, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002666 break;
2667 case YANG_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002668 LY_CHECK_RET(parse_type(ctx, data, &llist->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002669 break;
2670 case YANG_UNITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002671 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 +02002672 break;
2673 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002674 LY_CHECK_RET(parse_when(ctx, data, &llist->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002675 break;
2676 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002677 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002678 break;
2679 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002680 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002681 return LY_EVALID;
2682 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002683 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002684 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002685checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002686 /* mandatory substatements */
2687 if (!llist->type.name) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002688 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002689 return LY_EVALID;
2690 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01002691 if ((llist->min) && (llist->dflts)) {
2692 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMSCOMB, "min-elements", "default", "leaf-list");
2693 return LY_EVALID;
2694 }
Radek Krejcidf6cad12018-11-28 17:10:55 +01002695 if (llist->max && llist->min > llist->max) {
2696 LOGVAL_YANG(ctx, LYVE_SEMANTICS,
2697 "Invalid combination of min-elements and max-elements: min value %u is bigger than the max value %u.",
2698 llist->min, llist->max);
2699 return LY_EVALID;
2700 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002701
2702 return ret;
2703}
2704
Michal Vaskoea5abea2018-09-18 13:10:54 +02002705/**
2706 * @brief Parse the refine statement.
2707 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002708 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002709 * @param[in,out] data Data to read from, always moved to currently handled character.
2710 * @param[in,out] refines Refines to add to.
2711 *
2712 * @return LY_ERR values.
2713 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002714static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002715parse_refine(struct ly_parser_ctx *ctx, const char **data, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002716{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002717 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002718 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002719 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002720 enum yang_keyword kw;
2721 struct lysp_refine *rf;
2722
Radek Krejci2c4e7172018-10-19 15:56:26 +02002723 LY_ARRAY_NEW_RET(ctx->ctx, *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002724
2725 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002726 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002727 INSERT_WORD(ctx, buf, rf->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002728
Radek Krejci6d6556c2018-11-08 09:37:45 +01002729 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002730 switch (kw) {
2731 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002732 LY_CHECK_RET(parse_config(ctx, data, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002733 break;
2734 case YANG_DEFAULT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002735 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 +02002736 break;
2737 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002738 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 +02002739 break;
2740 case YANG_IF_FEATURE:
Radek Krejciceaf2122019-01-02 15:03:26 +01002741 YANG_CHECK_STMTVER2_RET(ctx, "if-feature", "refine");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002742 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 +02002743 break;
2744 case YANG_MAX_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002745 LY_CHECK_RET(parse_maxelements(ctx, data, &rf->max, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002746 break;
2747 case YANG_MIN_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002748 LY_CHECK_RET(parse_minelements(ctx, data, &rf->min, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002749 break;
2750 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002751 LY_CHECK_RET(parse_restrs(ctx, data, kw, &rf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002752 break;
2753 case YANG_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002754 LY_CHECK_RET(parse_mandatory(ctx, data, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002755 break;
2756 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002757 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 +02002758 break;
2759 case YANG_PRESENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002760 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 +02002761 break;
2762 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002763 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002764 break;
2765 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002766 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002767 return LY_EVALID;
2768 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002769 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002770 return ret;
2771}
2772
Michal Vaskoea5abea2018-09-18 13:10:54 +02002773/**
2774 * @brief Parse the typedef statement.
2775 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002776 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002777 * @param[in,out] data Data to read from, always moved to currently handled character.
2778 * @param[in,out] typedefs Typedefs to add to.
2779 *
2780 * @return LY_ERR values.
2781 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002782static LY_ERR
Radek Krejcibbe09a92018-11-08 09:36:54 +01002783parse_typedef(struct ly_parser_ctx *ctx, struct lysp_node *parent, const char **data, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002784{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002785 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002786 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002787 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002788 enum yang_keyword kw;
2789 struct lysp_tpdf *tpdf;
2790
Radek Krejci2c4e7172018-10-19 15:56:26 +02002791 LY_ARRAY_NEW_RET(ctx->ctx, *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002792
2793 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002794 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002795 INSERT_WORD(ctx, buf, tpdf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002796
2797 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01002798 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002799 switch (kw) {
2800 case YANG_DEFAULT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002801 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 +02002802 break;
2803 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002804 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 +02002805 break;
2806 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002807 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 +02002808 break;
2809 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002810 LY_CHECK_RET(parse_status(ctx, data, &tpdf->flags, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002811 break;
2812 case YANG_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002813 LY_CHECK_RET(parse_type(ctx, data, &tpdf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002814 break;
2815 case YANG_UNITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002816 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 +02002817 break;
2818 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002819 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002820 break;
2821 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002822 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002823 return LY_EVALID;
2824 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002825 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002826 LY_CHECK_RET(ret);
Radek Krejcibbe09a92018-11-08 09:36:54 +01002827checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002828 /* mandatory substatements */
2829 if (!tpdf->type.name) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002830 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002831 return LY_EVALID;
2832 }
2833
Radek Krejcibbe09a92018-11-08 09:36:54 +01002834 /* store data for collision check */
2835 if (parent) {
2836 ly_set_add(&ctx->tpdfs_nodes, parent, 0);
2837 }
2838
Michal Vasko7fbc8162018-09-17 10:35:16 +02002839 return ret;
2840}
2841
Michal Vaskoea5abea2018-09-18 13:10:54 +02002842/**
2843 * @brief Parse the input or output statement.
2844 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002845 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002846 * @param[in,out] data Data to read from, always moved to currently handled character.
2847 * @param[in] kw Type of this particular keyword
2848 * @param[in,out] inout_p Input/output pointer to write to.
2849 *
2850 * @return LY_ERR values.
2851 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002852static LY_ERR
Radek Krejci10113652018-11-14 16:56:50 +01002853parse_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 +02002854{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002855 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002856 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002857 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002858 struct lysp_action_inout *inout;
Radek Krejci10113652018-11-14 16:56:50 +01002859 enum yang_keyword kw;
Radek Krejcie86bf772018-12-14 11:39:53 +01002860 unsigned int u;
2861 struct lysp_node *child;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002862
2863 if (*inout_p) {
Radek Krejci10113652018-11-14 16:56:50 +01002864 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002865 return LY_EVALID;
2866 }
2867
2868 /* create structure */
2869 inout = calloc(1, sizeof *inout);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002870 LY_CHECK_ERR_RET(!inout, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002871 *inout_p = inout;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002872 inout->nodetype = LYS_INOUT;
2873 inout->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002874
2875 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01002876 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002877 switch (kw) {
2878 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01002879 YANG_CHECK_STMTVER2_RET(ctx, "anydata", ly_stmt2str(inout_kw));
Radek Krejci10113652018-11-14 16:56:50 +01002880 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002881 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002882 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)inout, &inout->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002883 break;
2884 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002885 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)inout, &inout->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002886 break;
2887 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002888 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)inout, &inout->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002889 break;
2890 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002891 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)inout, &inout->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002892 break;
2893 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002894 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)inout, &inout->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002895 break;
2896 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002897 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)inout, &inout->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002898 break;
2899 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002900 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)inout, &inout->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002901 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002902 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01002903 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)inout, data, &inout->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002904 break;
2905 case YANG_MUST:
Radek Krejciceaf2122019-01-02 15:03:26 +01002906 YANG_CHECK_STMTVER2_RET(ctx, "must", ly_stmt2str(inout_kw));
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002907 LY_CHECK_RET(parse_restrs(ctx, data, kw, &inout->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002908 break;
2909 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002910 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)inout, &inout->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002911 break;
2912 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002913 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inout->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002914 break;
2915 default:
Radek Krejci10113652018-11-14 16:56:50 +01002916 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002917 return LY_EVALID;
2918 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002919 }
Radek Krejcie86bf772018-12-14 11:39:53 +01002920 /* finalize parent pointers to the reallocated items */
2921 LY_ARRAY_FOR(inout->groupings, u) {
2922 LY_LIST_FOR(inout->groupings[u].data, child) {
2923 child->parent = (struct lysp_node*)&inout->groupings[u];
2924 }
2925 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002926 return ret;
2927}
2928
Michal Vaskoea5abea2018-09-18 13:10:54 +02002929/**
2930 * @brief Parse the action statement.
2931 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002932 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002933 * @param[in,out] data Data to read from, always moved to currently handled character.
2934 * @param[in,out] actions Actions to add to.
2935 *
2936 * @return LY_ERR values.
2937 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002938static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002939parse_action(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002940{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002941 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002942 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002943 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002944 enum yang_keyword kw;
2945 struct lysp_action *act;
Radek Krejcie86bf772018-12-14 11:39:53 +01002946 struct lysp_node *child;
2947 unsigned int u;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002948
Radek Krejci2c4e7172018-10-19 15:56:26 +02002949 LY_ARRAY_NEW_RET(ctx->ctx, *actions, act, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002950
2951 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002952 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002953 INSERT_WORD(ctx, buf, act->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002954 act->nodetype = LYS_ACTION;
2955 act->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002956
Radek Krejci6d6556c2018-11-08 09:37:45 +01002957 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002958 switch (kw) {
2959 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002960 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 +02002961 break;
2962 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002963 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 +02002964 break;
2965 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002966 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 +02002967 break;
2968 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002969 LY_CHECK_RET(parse_status(ctx, data, &act->flags, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002970 break;
2971
2972 case YANG_INPUT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002973 LY_CHECK_RET(parse_inout(ctx, data, kw, (struct lysp_node*)act, &act->input));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002974 break;
2975 case YANG_OUTPUT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002976 LY_CHECK_RET(parse_inout(ctx, data, kw, (struct lysp_node*)act, &act->output));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002977 break;
2978
2979 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01002980 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)act, data, &act->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002981 break;
2982 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002983 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)act, &act->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002984 break;
2985 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002986 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002987 break;
2988 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002989 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "action");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002990 return LY_EVALID;
2991 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002992 }
Radek Krejcie86bf772018-12-14 11:39:53 +01002993 /* finalize parent pointers to the reallocated items */
2994 LY_ARRAY_FOR(act->groupings, u) {
2995 LY_LIST_FOR(act->groupings[u].data, child) {
2996 child->parent = (struct lysp_node*)&act->groupings[u];
2997 }
2998 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002999 return ret;
3000}
3001
Michal Vaskoea5abea2018-09-18 13:10:54 +02003002/**
3003 * @brief Parse the notification statement.
3004 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003005 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003006 * @param[in,out] data Data to read from, always moved to currently handled character.
3007 * @param[in,out] notifs Notifications to add to.
3008 *
3009 * @return LY_ERR values.
3010 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003011static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003012parse_notif(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003013{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003014 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003015 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003016 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003017 enum yang_keyword kw;
3018 struct lysp_notif *notif;
Radek Krejcie86bf772018-12-14 11:39:53 +01003019 struct lysp_node *child;
3020 unsigned int u;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003021
Radek Krejci2c4e7172018-10-19 15:56:26 +02003022 LY_ARRAY_NEW_RET(ctx->ctx, *notifs, notif, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003023
3024 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003025 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003026 INSERT_WORD(ctx, buf, notif->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003027 notif->nodetype = LYS_NOTIF;
3028 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003029
Radek Krejci6d6556c2018-11-08 09:37:45 +01003030 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003031 switch (kw) {
3032 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003033 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 +02003034 break;
3035 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003036 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 +02003037 break;
3038 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003039 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 +02003040 break;
3041 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003042 LY_CHECK_RET(parse_status(ctx, data, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003043 break;
3044
3045 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01003046 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "notification");
Radek Krejci10113652018-11-14 16:56:50 +01003047 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003048 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003049 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003050 break;
3051 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003052 LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003053 break;
3054 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003055 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003056 break;
3057 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003058 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003059 break;
3060 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003061 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003062 break;
3063 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003064 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003065 break;
3066 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003067 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003068 break;
3069
3070 case YANG_MUST:
Radek Krejciceaf2122019-01-02 15:03:26 +01003071 YANG_CHECK_STMTVER2_RET(ctx, "must", "notification");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003072 LY_CHECK_RET(parse_restrs(ctx, data, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003073 break;
3074 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01003075 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)notif, data, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003076 break;
3077 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003078 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003079 break;
3080 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003081 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003082 break;
3083 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003084 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003085 return LY_EVALID;
3086 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003087 }
Radek Krejcie86bf772018-12-14 11:39:53 +01003088 /* finalize parent pointers to the reallocated items */
3089 LY_ARRAY_FOR(notif->groupings, u) {
3090 LY_LIST_FOR(notif->groupings[u].data, child) {
3091 child->parent = (struct lysp_node*)&notif->groupings[u];
3092 }
3093 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003094 return ret;
3095}
3096
Michal Vaskoea5abea2018-09-18 13:10:54 +02003097/**
3098 * @brief Parse the grouping statement.
3099 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003100 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003101 * @param[in,out] data Data to read from, always moved to currently handled character.
3102 * @param[in,out] groupings Groupings to add to.
3103 *
3104 * @return LY_ERR values.
3105 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003106static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003107parse_grouping(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003108{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003109 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003110 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003111 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003112 enum yang_keyword kw;
3113 struct lysp_grp *grp;
Radek Krejcie86bf772018-12-14 11:39:53 +01003114 struct lysp_node *child;
3115 unsigned int u;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003116
Radek Krejci2c4e7172018-10-19 15:56:26 +02003117 LY_ARRAY_NEW_RET(ctx->ctx, *groupings, grp, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003118
3119 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003120 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003121 INSERT_WORD(ctx, buf, grp->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003122 grp->nodetype = LYS_GROUPING;
3123 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003124
Radek Krejci6d6556c2018-11-08 09:37:45 +01003125 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003126 switch (kw) {
3127 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003128 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 +02003129 break;
3130 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003131 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 +02003132 break;
3133 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003134 LY_CHECK_RET(parse_status(ctx, data, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003135 break;
3136
3137 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01003138 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "grouping");
Radek Krejci10113652018-11-14 16:56:50 +01003139 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003140 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003141 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003142 break;
3143 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003144 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003145 break;
3146 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003147 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003148 break;
3149 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003150 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003151 break;
3152 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003153 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003154 break;
3155 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003156 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003157 break;
3158 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003159 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003160 break;
3161
3162 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01003163 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)grp, data, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003164 break;
3165 case YANG_ACTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003166 YANG_CHECK_STMTVER2_RET(ctx, "action", "grouping");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003167 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)grp, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003168 break;
3169 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003170 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)grp, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003171 break;
3172 case YANG_NOTIFICATION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003173 YANG_CHECK_STMTVER2_RET(ctx, "notification", "grouping");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003174 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)grp, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003175 break;
3176 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003177 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003178 break;
3179 default:
Radek Krejci10113652018-11-14 16:56:50 +01003180 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003181 return LY_EVALID;
3182 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003183 }
Radek Krejcie86bf772018-12-14 11:39:53 +01003184 /* finalize parent pointers to the reallocated items */
3185 LY_ARRAY_FOR(grp->groupings, u) {
3186 LY_LIST_FOR(grp->groupings[u].data, child) {
3187 child->parent = (struct lysp_node*)&grp->groupings[u];
3188 }
3189 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003190 return ret;
3191}
3192
Michal Vaskoea5abea2018-09-18 13:10:54 +02003193/**
3194 * @brief Parse the refine statement.
3195 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003196 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003197 * @param[in,out] data Data to read from, always moved to currently handled character.
3198 * @param[in,out] augments Augments to add to.
3199 *
3200 * @return LY_ERR values.
3201 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003202static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003203parse_augment(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003204{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003205 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003206 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003207 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003208 enum yang_keyword kw;
3209 struct lysp_augment *aug;
3210
Radek Krejci2c4e7172018-10-19 15:56:26 +02003211 LY_ARRAY_NEW_RET(ctx->ctx, *augments, aug, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003212
3213 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003214 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003215 INSERT_WORD(ctx, buf, aug->nodeid, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003216 aug->nodetype = LYS_AUGMENT;
3217 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003218
Radek Krejci6d6556c2018-11-08 09:37:45 +01003219 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003220 switch (kw) {
3221 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003222 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 +02003223 break;
3224 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003225 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 +02003226 break;
3227 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003228 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 +02003229 break;
3230 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003231 LY_CHECK_RET(parse_status(ctx, data, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003232 break;
3233 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003234 LY_CHECK_RET(parse_when(ctx, data, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003235 break;
3236
3237 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01003238 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "augment");
Radek Krejci10113652018-11-14 16:56:50 +01003239 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003240 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003241 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003242 break;
3243 case YANG_CASE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003244 LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003245 break;
3246 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003247 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003248 break;
3249 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003250 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003251 break;
3252 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003253 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003254 break;
3255 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003256 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003257 break;
3258 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003259 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003260 break;
3261 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003262 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003263 break;
3264
3265 case YANG_ACTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003266 YANG_CHECK_STMTVER2_RET(ctx, "action", "augment");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003267 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003268 break;
3269 case YANG_NOTIFICATION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003270 YANG_CHECK_STMTVER2_RET(ctx, "notification", "augment");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003271 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003272 break;
3273 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003274 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003275 break;
3276 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003277 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003278 return LY_EVALID;
3279 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003280 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003281 return ret;
3282}
3283
Michal Vaskoea5abea2018-09-18 13:10:54 +02003284/**
3285 * @brief Parse the uses statement.
3286 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003287 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003288 * @param[in,out] data Data to read from, always moved to currently handled character.
3289 * @param[in,out] siblings Siblings to add to.
3290 *
3291 * @return LY_ERR values.
3292 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003293static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003294parse_uses(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003295{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003296 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003297 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003298 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003299 enum yang_keyword kw;
3300 struct lysp_node *iter;
3301 struct lysp_node_uses *uses;
3302
3303 /* create structure */
3304 uses = calloc(1, sizeof *uses);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003305 LY_CHECK_ERR_RET(!uses, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003306 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003307 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003308
3309 /* insert into siblings */
3310 if (!*siblings) {
3311 *siblings = (struct lysp_node *)uses;
3312 } else {
3313 for (iter = *siblings; iter->next; iter = iter->next);
3314 iter->next = (struct lysp_node *)uses;
3315 }
3316
3317 /* get name */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003318 LY_CHECK_RET(get_argument(ctx, data, Y_PREF_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003319 INSERT_WORD(ctx, buf, uses->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003320
3321 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01003322 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003323 switch (kw) {
3324 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003325 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 +02003326 break;
3327 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003328 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 +02003329 break;
3330 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003331 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 +02003332 break;
3333 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003334 LY_CHECK_RET(parse_status(ctx, data, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003335 break;
3336 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003337 LY_CHECK_RET(parse_when(ctx, data, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003338 break;
3339
3340 case YANG_REFINE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003341 LY_CHECK_RET(parse_refine(ctx, data, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003342 break;
3343 case YANG_AUGMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003344 LY_CHECK_RET(parse_augment(ctx, data, (struct lysp_node*)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003345 break;
3346 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003347 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003348 break;
3349 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003350 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003351 return LY_EVALID;
3352 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003353 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003354 return ret;
3355}
3356
Michal Vaskoea5abea2018-09-18 13:10:54 +02003357/**
3358 * @brief Parse the case statement.
3359 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003360 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003361 * @param[in,out] data Data to read from, always moved to currently handled character.
3362 * @param[in,out] siblings Siblings to add to.
3363 *
3364 * @return LY_ERR values.
3365 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003366static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003367parse_case(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003368{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003369 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003370 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003371 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003372 enum yang_keyword kw;
3373 struct lysp_node *iter;
3374 struct lysp_node_case *cas;
3375
3376 /* create structure */
3377 cas = calloc(1, sizeof *cas);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003378 LY_CHECK_ERR_RET(!cas, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003379 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003380 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003381
3382 /* insert into siblings */
3383 if (!*siblings) {
3384 *siblings = (struct lysp_node *)cas;
3385 } else {
3386 for (iter = *siblings; iter->next; iter = iter->next);
3387 iter->next = (struct lysp_node *)cas;
3388 }
3389
3390 /* get name */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003391 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003392 INSERT_WORD(ctx, buf, cas->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003393
3394 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01003395 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003396 switch (kw) {
3397 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003398 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 +02003399 break;
3400 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003401 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 +02003402 break;
3403 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003404 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 +02003405 break;
3406 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003407 LY_CHECK_RET(parse_status(ctx, data, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003408 break;
3409 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003410 LY_CHECK_RET(parse_when(ctx, data, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003411 break;
3412
3413 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01003414 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "case");
Radek Krejci10113652018-11-14 16:56:50 +01003415 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003416 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003417 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003418 break;
3419 case YANG_CHOICE:
Radek Krejcia9026eb2018-12-12 16:04:47 +01003420 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003421 break;
3422 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003423 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003424 break;
3425 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003426 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003427 break;
3428 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003429 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003430 break;
3431 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003432 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003433 break;
3434 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003435 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003436 break;
3437 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003438 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003439 break;
3440 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003441 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003442 return LY_EVALID;
3443 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003444 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003445 return ret;
3446}
3447
Michal Vaskoea5abea2018-09-18 13:10:54 +02003448/**
3449 * @brief Parse the choice statement.
3450 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003451 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003452 * @param[in,out] data Data to read from, always moved to currently handled character.
3453 * @param[in,out] siblings Siblings to add to.
3454 *
3455 * @return LY_ERR values.
3456 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003457static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003458parse_choice(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003459{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003460 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003461 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003462 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003463 enum yang_keyword kw;
3464 struct lysp_node *iter;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003465 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003466
3467 /* create structure */
Radek Krejci44ceedc2018-10-02 15:54:31 +02003468 choice = calloc(1, sizeof *choice);
3469 LY_CHECK_ERR_RET(!choice, LOGMEM(ctx->ctx), LY_EMEM);
3470 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003471 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003472
3473 /* insert into siblings */
3474 if (!*siblings) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02003475 *siblings = (struct lysp_node *)choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003476 } else {
3477 for (iter = *siblings; iter->next; iter = iter->next);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003478 iter->next = (struct lysp_node *)choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003479 }
3480
3481 /* get name */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003482 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003483 INSERT_WORD(ctx, buf, choice->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003484
3485 /* parse substatements */
Radek Krejcia9026eb2018-12-12 16:04:47 +01003486 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003487 switch (kw) {
3488 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003489 LY_CHECK_RET(parse_config(ctx, data, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003490 break;
3491 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003492 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 +02003493 break;
3494 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003495 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 +02003496 break;
3497 case YANG_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003498 LY_CHECK_RET(parse_mandatory(ctx, data, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003499 break;
3500 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003501 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 +02003502 break;
3503 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003504 LY_CHECK_RET(parse_status(ctx, data, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003505 break;
3506 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003507 LY_CHECK_RET(parse_when(ctx, data, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003508 break;
3509 case YANG_DEFAULT:
Radek Krejcia9026eb2018-12-12 16:04:47 +01003510 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 +02003511 break;
3512
3513 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01003514 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "choice");
Radek Krejci10113652018-11-14 16:56:50 +01003515 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003516 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003517 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003518 break;
3519 case YANG_CASE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003520 LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003521 break;
3522 case YANG_CHOICE:
Radek Krejciceaf2122019-01-02 15:03:26 +01003523 YANG_CHECK_STMTVER2_RET(ctx, "choice", "choice");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003524 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003525 break;
3526 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003527 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003528 break;
3529 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003530 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003531 break;
3532 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003533 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003534 break;
3535 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003536 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003537 break;
3538 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003539 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003540 break;
3541 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003542 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003543 return LY_EVALID;
3544 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003545 }
Radek Krejcia9026eb2018-12-12 16:04:47 +01003546 LY_CHECK_RET(ret);
3547checks:
3548 if ((choice->flags & LYS_MAND_TRUE) && choice->dflt) {
3549 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMSCOMB, "mandatory", "default", "choice");
3550 return LY_EVALID;
3551 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003552 return ret;
3553}
3554
Michal Vaskoea5abea2018-09-18 13:10:54 +02003555/**
3556 * @brief Parse the container statement.
3557 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003558 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003559 * @param[in,out] data Data to read from, always moved to currently handled character.
3560 * @param[in,out] siblings Siblings to add to.
3561 *
3562 * @return LY_ERR values.
3563 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003564static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003565parse_container(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003566{
3567 LY_ERR ret = 0;
3568 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003569 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003570 enum yang_keyword kw;
3571 struct lysp_node *iter;
3572 struct lysp_node_container *cont;
Radek Krejcie86bf772018-12-14 11:39:53 +01003573 unsigned int u;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003574
3575 /* create structure */
3576 cont = calloc(1, sizeof *cont);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003577 LY_CHECK_ERR_RET(!cont, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003578 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003579 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003580
3581 /* insert into siblings */
3582 if (!*siblings) {
3583 *siblings = (struct lysp_node *)cont;
3584 } else {
3585 for (iter = *siblings; iter->next; iter = iter->next);
3586 iter->next = (struct lysp_node *)cont;
3587 }
3588
3589 /* get name */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003590 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003591 INSERT_WORD(ctx, buf, cont->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003592
3593 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01003594 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003595 switch (kw) {
3596 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003597 LY_CHECK_RET(parse_config(ctx, data, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003598 break;
3599 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003600 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &cont->dsc, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003601 break;
3602 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003603 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &cont->iffeatures, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003604 break;
3605 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003606 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &cont->ref, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003607 break;
3608 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003609 LY_CHECK_RET(parse_status(ctx, data, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003610 break;
3611 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003612 LY_CHECK_RET(parse_when(ctx, data, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003613 break;
3614 case YANG_PRESENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003615 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 +02003616 break;
3617
3618 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01003619 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "container");
Radek Krejci10113652018-11-14 16:56:50 +01003620 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003621 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003622 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003623 break;
3624 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003625 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003626 break;
3627 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003628 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003629 break;
3630 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003631 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003632 break;
3633 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003634 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003635 break;
3636 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003637 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003638 break;
3639 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003640 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003641 break;
3642
3643 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01003644 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)cont, data, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003645 break;
3646 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003647 LY_CHECK_RET(parse_restrs(ctx, data, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003648 break;
3649 case YANG_ACTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003650 YANG_CHECK_STMTVER2_RET(ctx, "action", "container");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003651 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003652 break;
3653 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003654 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003655 break;
3656 case YANG_NOTIFICATION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003657 YANG_CHECK_STMTVER2_RET(ctx, "notification", "container");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003658 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003659 break;
3660 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003661 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003662 break;
3663 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003664 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003665 return LY_EVALID;
3666 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003667 }
Radek Krejcie86bf772018-12-14 11:39:53 +01003668 /* finalize parent pointers to the reallocated items */
3669 LY_ARRAY_FOR(cont->groupings, u) {
3670 LY_LIST_FOR(cont->groupings[u].data, iter) {
3671 iter->parent = (struct lysp_node*)&cont->groupings[u];
3672 }
3673 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003674 return ret;
3675}
3676
Michal Vaskoea5abea2018-09-18 13:10:54 +02003677/**
3678 * @brief Parse the list statement.
3679 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003680 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003681 * @param[in,out] data Data to read from, always moved to currently handled character.
3682 * @param[in,out] siblings Siblings to add to.
3683 *
3684 * @return LY_ERR values.
3685 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003686static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003687parse_list(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003688{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003689 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003690 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003691 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003692 enum yang_keyword kw;
3693 struct lysp_node *iter;
3694 struct lysp_node_list *list;
Radek Krejcie86bf772018-12-14 11:39:53 +01003695 unsigned int u;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003696
3697 /* create structure */
3698 list = calloc(1, sizeof *list);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003699 LY_CHECK_ERR_RET(!list, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003700 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003701 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003702
3703 /* insert into siblings */
3704 if (!*siblings) {
3705 *siblings = (struct lysp_node *)list;
3706 } else {
3707 for (iter = *siblings; iter->next; iter = iter->next);
3708 iter->next = (struct lysp_node *)list;
3709 }
3710
3711 /* get name */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003712 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003713 INSERT_WORD(ctx, buf, list->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003714
3715 /* parse substatements */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003716 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003717 switch (kw) {
3718 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003719 LY_CHECK_RET(parse_config(ctx, data, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003720 break;
3721 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003722 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 +02003723 break;
3724 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003725 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 +02003726 break;
3727 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003728 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 +02003729 break;
3730 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003731 LY_CHECK_RET(parse_status(ctx, data, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003732 break;
3733 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003734 LY_CHECK_RET(parse_when(ctx, data, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003735 break;
3736 case YANG_KEY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003737 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 +02003738 break;
3739 case YANG_MAX_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003740 LY_CHECK_RET(parse_maxelements(ctx, data, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003741 break;
3742 case YANG_MIN_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003743 LY_CHECK_RET(parse_minelements(ctx, data, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003744 break;
3745 case YANG_ORDERED_BY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003746 LY_CHECK_RET(parse_orderedby(ctx, data, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003747 break;
3748 case YANG_UNIQUE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003749 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 +02003750 break;
3751
3752 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01003753 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "list");
Radek Krejci10113652018-11-14 16:56:50 +01003754 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003755 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003756 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003757 break;
3758 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003759 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003760 break;
3761 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003762 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003763 break;
3764 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003765 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003766 break;
3767 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003768 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003769 break;
3770 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003771 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003772 break;
3773 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003774 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003775 break;
3776
3777 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01003778 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)list, data, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003779 break;
3780 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003781 LY_CHECK_RET(parse_restrs(ctx, data, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003782 break;
3783 case YANG_ACTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003784 YANG_CHECK_STMTVER2_RET(ctx, "action", "list");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003785 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003786 break;
3787 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003788 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003789 break;
3790 case YANG_NOTIFICATION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003791 YANG_CHECK_STMTVER2_RET(ctx, "notification", "list");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003792 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003793 break;
3794 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003795 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003796 break;
3797 default:
Radek Krejci10113652018-11-14 16:56:50 +01003798 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003799 return LY_EVALID;
3800 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003801 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003802 LY_CHECK_RET(ret);
Radek Krejcie86bf772018-12-14 11:39:53 +01003803 /* finalize parent pointers to the reallocated items */
3804 LY_ARRAY_FOR(list->groupings, u) {
3805 LY_LIST_FOR(list->groupings[u].data, iter) {
3806 iter->parent = (struct lysp_node*)&list->groupings[u];
3807 }
3808 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003809checks:
3810 if (list->max && list->min > list->max) {
3811 LOGVAL_YANG(ctx, LYVE_SEMANTICS,
3812 "Invalid combination of min-elements and max-elements: min value %u is bigger than the max value %u.",
3813 list->min, list->max);
3814 return LY_EVALID;
3815 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003816
3817 return ret;
3818}
3819
Michal Vaskoea5abea2018-09-18 13:10:54 +02003820/**
3821 * @brief Parse the yin-element statement.
3822 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003823 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003824 * @param[in,out] data Data to read from, always moved to currently handled character.
3825 * @param[in,out] flags Flags to write to.
3826 * @param[in,out] exts Extension instances to add to.
3827 *
3828 * @return LY_ERR values.
3829 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003830static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003831parse_yinelement(struct ly_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003832{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003833 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003834 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003835 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003836 enum yang_keyword kw;
3837
3838 if (*flags & LYS_YINELEM_MASK) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02003839 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003840 return LY_EVALID;
3841 }
3842
3843 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003844 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003845
3846 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
3847 *flags |= LYS_YINELEM_TRUE;
3848 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
3849 *flags |= LYS_YINELEM_FALSE;
3850 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02003851 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003852 free(buf);
3853 return LY_EVALID;
3854 }
3855 free(buf);
3856
Radek Krejci6d6556c2018-11-08 09:37:45 +01003857 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003858 switch (kw) {
3859 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003860 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_YINELEM, 0, exts));
3861 LY_CHECK_RET(ret); break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003862 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003863 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003864 return LY_EVALID;
3865 }
3866 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003867 return ret;
3868}
3869
Michal Vaskoea5abea2018-09-18 13:10:54 +02003870/**
3871 * @brief Parse the yin-element statement.
3872 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003873 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003874 * @param[in,out] data Data to read from, always moved to currently handled character.
3875 * @param[in,out] argument Value to write to.
3876 * @param[in,out] flags Flags to write to.
3877 * @param[in,out] exts Extension instances to add to.
3878 *
3879 * @return LY_ERR values.
3880 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003881static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003882parse_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 +02003883{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003884 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003885 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003886 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003887 enum yang_keyword kw;
3888
3889 if (*argument) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02003890 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003891 return LY_EVALID;
3892 }
3893
3894 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003895 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003896 INSERT_WORD(ctx, buf, *argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003897
Radek Krejci6d6556c2018-11-08 09:37:45 +01003898 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003899 switch (kw) {
3900 case YANG_YIN_ELEMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003901 LY_CHECK_RET(parse_yinelement(ctx, data, flags, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003902 break;
3903 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003904 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_ARGUMENT, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003905 break;
3906 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003907 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003908 return LY_EVALID;
3909 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003910 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003911 return ret;
3912}
3913
Michal Vaskoea5abea2018-09-18 13:10:54 +02003914/**
3915 * @brief Parse the extension statement.
3916 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003917 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003918 * @param[in,out] data Data to read from, always moved to currently handled character.
3919 * @param[in,out] extensions Extensions to add to.
3920 *
3921 * @return LY_ERR values.
3922 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003923static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003924parse_extension(struct ly_parser_ctx *ctx, const char **data, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003925{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003926 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003927 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003928 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003929 enum yang_keyword kw;
3930 struct lysp_ext *ex;
3931
Radek Krejci2c4e7172018-10-19 15:56:26 +02003932 LY_ARRAY_NEW_RET(ctx->ctx, *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003933
3934 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003935 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003936 INSERT_WORD(ctx, buf, ex->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003937
Radek Krejci6d6556c2018-11-08 09:37:45 +01003938 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003939 switch (kw) {
3940 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003941 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 +02003942 break;
3943 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003944 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 +02003945 break;
3946 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003947 LY_CHECK_RET(parse_status(ctx, data, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003948 break;
3949 case YANG_ARGUMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003950 LY_CHECK_RET(parse_argument(ctx, data, &ex->argument, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003951 break;
3952 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003953 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003954 break;
3955 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003956 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003957 return LY_EVALID;
3958 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003959 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003960 return ret;
3961}
3962
Michal Vaskoea5abea2018-09-18 13:10:54 +02003963/**
3964 * @brief Parse the deviate statement.
3965 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003966 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003967 * @param[in,out] data Data to read from, always moved to currently handled character.
3968 * @param[in,out] deviates Deviates to add to.
3969 *
3970 * @return LY_ERR values.
3971 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003972static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003973parse_deviate(struct ly_parser_ctx *ctx, const char **data, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003974{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003975 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003976 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003977 size_t word_len, dev_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003978 enum yang_keyword kw;
3979 struct lysp_deviate *iter, *d;
3980 struct lysp_deviate_add *d_add = NULL;
3981 struct lysp_deviate_rpl *d_rpl = NULL;
3982 struct lysp_deviate_del *d_del = NULL;
3983 const char **d_units, ***d_uniques, ***d_dflts;
3984 struct lysp_restr **d_musts;
3985 uint16_t *d_flags;
3986 uint32_t *d_min, *d_max;
3987
3988 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003989 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003990
3991 if ((word_len == 13) && !strncmp(word, "not-supported", word_len)) {
3992 dev_mod = LYS_DEV_NOT_SUPPORTED;
3993 } else if ((word_len == 3) && !strncmp(word, "add", word_len)) {
3994 dev_mod = LYS_DEV_ADD;
3995 } else if ((word_len == 7) && !strncmp(word, "replace", word_len)) {
3996 dev_mod = LYS_DEV_REPLACE;
3997 } else if ((word_len == 6) && !strncmp(word, "delete", word_len)) {
3998 dev_mod = LYS_DEV_DELETE;
3999 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004000 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004001 free(buf);
4002 return LY_EVALID;
4003 }
4004 free(buf);
4005
4006 /* create structure */
4007 switch (dev_mod) {
4008 case LYS_DEV_NOT_SUPPORTED:
4009 d = calloc(1, sizeof *d);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004010 LY_CHECK_ERR_RET(!d, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004011 break;
4012 case LYS_DEV_ADD:
4013 d_add = calloc(1, sizeof *d_add);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004014 LY_CHECK_ERR_RET(!d_add, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004015 d = (struct lysp_deviate *)d_add;
4016 d_units = &d_add->units;
4017 d_uniques = &d_add->uniques;
4018 d_dflts = &d_add->dflts;
4019 d_musts = &d_add->musts;
4020 d_flags = &d_add->flags;
4021 d_min = &d_add->min;
4022 d_max = &d_add->max;
4023 break;
4024 case LYS_DEV_REPLACE:
4025 d_rpl = calloc(1, sizeof *d_rpl);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004026 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004027 d = (struct lysp_deviate *)d_rpl;
4028 d_units = &d_rpl->units;
4029 d_flags = &d_rpl->flags;
4030 d_min = &d_rpl->min;
4031 d_max = &d_rpl->max;
4032 break;
4033 case LYS_DEV_DELETE:
4034 d_del = calloc(1, sizeof *d_del);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004035 LY_CHECK_ERR_RET(!d_del, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004036 d = (struct lysp_deviate *)d_del;
4037 d_units = &d_del->units;
4038 d_uniques = &d_del->uniques;
4039 d_dflts = &d_del->dflts;
4040 d_musts = &d_del->musts;
4041 d_flags = &d_del->flags;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004042 break;
4043 default:
4044 assert(0);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004045 LOGINT_RET(ctx->ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004046 }
4047 d->mod = dev_mod;
4048
4049 /* insert into siblings */
4050 if (!*deviates) {
4051 *deviates = d;
4052 } else {
4053 for (iter = *deviates; iter->next; iter = iter->next);
4054 iter->next = d;
4055 }
4056
Radek Krejci6d6556c2018-11-08 09:37:45 +01004057 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004058 switch (kw) {
4059 case YANG_CONFIG:
4060 switch (dev_mod) {
4061 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004062 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004063 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004064 return LY_EVALID;
4065 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004066 LY_CHECK_RET(parse_config(ctx, data, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004067 break;
4068 }
4069 break;
4070 case YANG_DEFAULT:
4071 switch (dev_mod) {
4072 case LYS_DEV_NOT_SUPPORTED:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004073 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004074 return LY_EVALID;
4075 case LYS_DEV_REPLACE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004076 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 +02004077 break;
4078 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004079 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 +02004080 break;
4081 }
4082 break;
4083 case YANG_MANDATORY:
4084 switch (dev_mod) {
4085 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004086 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004087 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004088 return LY_EVALID;
4089 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004090 LY_CHECK_RET(parse_mandatory(ctx, data, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004091 break;
4092 }
4093 break;
4094 case YANG_MAX_ELEMENTS:
4095 switch (dev_mod) {
4096 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004097 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004098 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004099 return LY_EVALID;
4100 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004101 LY_CHECK_RET(parse_maxelements(ctx, data, d_max, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004102 break;
4103 }
4104 break;
4105 case YANG_MIN_ELEMENTS:
4106 switch (dev_mod) {
4107 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004108 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004109 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004110 return LY_EVALID;
4111 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004112 LY_CHECK_RET(parse_minelements(ctx, data, d_min, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004113 break;
4114 }
4115 break;
4116 case YANG_MUST:
4117 switch (dev_mod) {
4118 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004119 case LYS_DEV_REPLACE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004120 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004121 return LY_EVALID;
4122 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004123 LY_CHECK_RET(parse_restrs(ctx, data, kw, d_musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004124 break;
4125 }
4126 break;
4127 case YANG_TYPE:
4128 switch (dev_mod) {
4129 case LYS_DEV_NOT_SUPPORTED:
4130 case LYS_DEV_ADD:
4131 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004132 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004133 return LY_EVALID;
4134 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004135 if (d_rpl->type) {
4136 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
4137 return LY_EVALID;
4138 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004139 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004140 LY_CHECK_ERR_RET(!d_rpl->type, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004141 LY_CHECK_RET(parse_type(ctx, data, d_rpl->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004142 break;
4143 }
4144 break;
4145 case YANG_UNIQUE:
4146 switch (dev_mod) {
4147 case LYS_DEV_NOT_SUPPORTED:
4148 case LYS_DEV_REPLACE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004149 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004150 return LY_EVALID;
4151 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004152 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 +02004153 break;
4154 }
4155 break;
4156 case YANG_UNITS:
4157 switch (dev_mod) {
4158 case LYS_DEV_NOT_SUPPORTED:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004159 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004160 return LY_EVALID;
4161 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004162 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 +02004163 break;
4164 }
4165 break;
4166 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004167 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004168 break;
4169 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004170 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004171 return LY_EVALID;
4172 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004173 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004174 return ret;
4175}
4176
Michal Vaskoea5abea2018-09-18 13:10:54 +02004177/**
4178 * @brief Parse the deviation statement.
4179 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004180 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004181 * @param[in,out] data Data to read from, always moved to currently handled character.
4182 * @param[in,out] deviations Deviations to add to.
4183 *
4184 * @return LY_ERR values.
4185 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004186static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02004187parse_deviation(struct ly_parser_ctx *ctx, const char **data, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004188{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004189 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004190 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004191 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004192 enum yang_keyword kw;
4193 struct lysp_deviation *dev;
4194
Radek Krejci2c4e7172018-10-19 15:56:26 +02004195 LY_ARRAY_NEW_RET(ctx->ctx, *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004196
4197 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004198 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02004199 INSERT_WORD(ctx, buf, dev->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004200
Radek Krejci6d6556c2018-11-08 09:37:45 +01004201 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004202 switch (kw) {
4203 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004204 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 +02004205 break;
4206 case YANG_DEVIATE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004207 LY_CHECK_RET(parse_deviate(ctx, data, &dev->deviates));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004208 break;
4209 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004210 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 +02004211 break;
4212 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004213 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004214 break;
4215 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004216 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004217 return LY_EVALID;
4218 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004219 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004220 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01004221checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004222 /* mandatory substatements */
4223 if (!dev->deviates) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004224 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004225 return LY_EVALID;
4226 }
4227
4228 return ret;
4229}
4230
Michal Vaskoea5abea2018-09-18 13:10:54 +02004231/**
4232 * @brief Parse the feature statement.
4233 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004234 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004235 * @param[in,out] data Data to read from, always moved to currently handled character.
4236 * @param[in,out] features Features to add to.
4237 *
4238 * @return LY_ERR values.
4239 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004240static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02004241parse_feature(struct ly_parser_ctx *ctx, const char **data, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004242{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004243 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004244 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004245 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004246 enum yang_keyword kw;
4247 struct lysp_feature *feat;
4248
Radek Krejci2c4e7172018-10-19 15:56:26 +02004249 LY_ARRAY_NEW_RET(ctx->ctx, *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004250
4251 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004252 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02004253 INSERT_WORD(ctx, buf, feat->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004254
Radek Krejci6d6556c2018-11-08 09:37:45 +01004255 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004256 switch (kw) {
4257 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004258 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 +02004259 break;
4260 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004261 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 +02004262 break;
4263 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004264 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 +02004265 break;
4266 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004267 LY_CHECK_RET(parse_status(ctx, data, &feat->flags, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004268 break;
4269 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004270 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004271 break;
4272 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004273 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004274 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004275 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004276 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004277 return ret;
4278}
4279
Michal Vaskoea5abea2018-09-18 13:10:54 +02004280/**
4281 * @brief Parse the identity statement.
4282 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004283 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004284 * @param[in,out] data Data to read from, always moved to currently handled character.
4285 * @param[in,out] identities Identities to add to.
4286 *
4287 * @return LY_ERR values.
4288 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004289static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02004290parse_identity(struct ly_parser_ctx *ctx, const char **data, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004291{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004292 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004293 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004294 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004295 enum yang_keyword kw;
4296 struct lysp_ident *ident;
4297
Radek Krejci2c4e7172018-10-19 15:56:26 +02004298 LY_ARRAY_NEW_RET(ctx->ctx, *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004299
4300 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004301 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02004302 INSERT_WORD(ctx, buf, ident->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004303
Radek Krejci6d6556c2018-11-08 09:37:45 +01004304 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004305 switch (kw) {
4306 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004307 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 +02004308 break;
4309 case YANG_IF_FEATURE:
Radek Krejciceaf2122019-01-02 15:03:26 +01004310 YANG_CHECK_STMTVER2_RET(ctx, "if-feature", "identity");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004311 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 +02004312 break;
4313 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004314 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 +02004315 break;
4316 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004317 LY_CHECK_RET(parse_status(ctx, data, &ident->flags, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004318 break;
4319 case YANG_BASE:
Radek Krejci10113652018-11-14 16:56:50 +01004320 if (ident->bases && ctx->mod->version < 2) {
4321 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Identity can be derived from multiple base identities only in YANG 1.1 modules");
4322 return LY_EVALID;
4323 }
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004324 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 +02004325 break;
4326 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004327 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004328 break;
4329 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004330 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004331 return LY_EVALID;
4332 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004333 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004334 return ret;
4335}
4336
Michal Vaskoea5abea2018-09-18 13:10:54 +02004337/**
4338 * @brief Parse the module or submodule statement.
4339 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004340 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004341 * @param[in,out] data Data to read from, always moved to currently handled character.
4342 * @param[in,out] mod Module to write to.
4343 *
4344 * @return LY_ERR values.
4345 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004346static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02004347parse_sub_module(struct ly_parser_ctx *ctx, const char **data, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004348{
4349 LY_ERR ret = 0;
4350 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004351 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004352 enum yang_keyword kw, prev_kw = 0;
4353 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Radek Krejcie9e987e2018-10-31 12:50:27 +01004354 struct lysp_module *dup;
Radek Krejcie86bf772018-12-14 11:39:53 +01004355 struct lysp_node *child;
4356 unsigned int u;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004357
4358 /* (sub)module name */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004359 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02004360 INSERT_WORD(ctx, buf, mod->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004361
Radek Krejci6d6556c2018-11-08 09:37:45 +01004362 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004363
Radek Krejcie3846472018-10-15 15:24:51 +02004364#define CHECK_ORDER(SECTION) \
4365 if (mod_stmt > SECTION) {LOGVAL_YANG(ctx, LY_VCODE_INORD, ly_stmt2str(kw), ly_stmt2str(prev_kw)); return LY_EVALID;}mod_stmt = SECTION
4366
Michal Vasko7fbc8162018-09-17 10:35:16 +02004367 switch (kw) {
4368 /* module header */
4369 case YANG_NAMESPACE:
4370 case YANG_PREFIX:
4371 if (mod->submodule) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004372 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004373 return LY_EVALID;
4374 }
Radek Krejcie3846472018-10-15 15:24:51 +02004375 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4376 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004377 case YANG_BELONGS_TO:
Radek Krejcie3846472018-10-15 15:24:51 +02004378 if (!mod->submodule) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004379 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004380 return LY_EVALID;
4381 }
Radek Krejcie3846472018-10-15 15:24:51 +02004382 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4383 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004384 case YANG_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004385 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004386 break;
4387 /* linkage */
4388 case YANG_INCLUDE:
4389 case YANG_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004390 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004391 break;
4392 /* meta */
4393 case YANG_ORGANIZATION:
4394 case YANG_CONTACT:
4395 case YANG_DESCRIPTION:
4396 case YANG_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004397 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004398 break;
4399
4400 /* revision */
4401 case YANG_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004402 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004403 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004404 /* body */
4405 case YANG_ANYDATA:
4406 case YANG_ANYXML:
4407 case YANG_AUGMENT:
4408 case YANG_CHOICE:
4409 case YANG_CONTAINER:
4410 case YANG_DEVIATION:
4411 case YANG_EXTENSION:
4412 case YANG_FEATURE:
4413 case YANG_GROUPING:
4414 case YANG_IDENTITY:
4415 case YANG_LEAF:
4416 case YANG_LEAF_LIST:
4417 case YANG_LIST:
4418 case YANG_NOTIFICATION:
4419 case YANG_RPC:
4420 case YANG_TYPEDEF:
4421 case YANG_USES:
4422 case YANG_CUSTOM:
4423 mod_stmt = Y_MOD_BODY;
4424 break;
4425 default:
4426 /* error handled in the next switch */
4427 break;
4428 }
Radek Krejcie3846472018-10-15 15:24:51 +02004429#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004430
Radek Krejcie3846472018-10-15 15:24:51 +02004431 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004432 switch (kw) {
4433 /* module header */
4434 case YANG_YANG_VERSION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004435 LY_CHECK_RET(parse_yangversion(ctx, data, mod));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004436 break;
4437 case YANG_NAMESPACE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004438 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_NAMESPACE, 0, &mod->ns, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004439 break;
4440 case YANG_PREFIX:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004441 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_PREFIX, 0, &mod->prefix, Y_IDENTIF_ARG, &mod->exts));
Radek Krejci70853c52018-10-15 14:46:16 +02004442 LY_CHECK_RET(lysp_check_prefix(ctx, mod, &mod->prefix), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004443 break;
4444 case YANG_BELONGS_TO:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004445 LY_CHECK_RET(parse_belongsto(ctx, data, &mod->belongsto, &mod->prefix, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004446 break;
4447
4448 /* linkage */
4449 case YANG_INCLUDE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004450 LY_CHECK_RET(parse_include(ctx, data, mod));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004451 break;
4452 case YANG_IMPORT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004453 LY_CHECK_RET(parse_import(ctx, data, mod));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004454 break;
4455
4456 /* meta */
4457 case YANG_ORGANIZATION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004458 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_ORGANIZATION, 0, &mod->org, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004459 break;
4460 case YANG_CONTACT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004461 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_CONTACT, 0, &mod->contact, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004462 break;
4463 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004464 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &mod->dsc, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004465 break;
4466 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004467 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &mod->ref, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004468 break;
4469
4470 /* revision */
4471 case YANG_REVISION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004472 LY_CHECK_RET(parse_revision(ctx, data, &mod->revs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004473 break;
4474
4475 /* body */
4476 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01004477 YANG_CHECK_STMTVER2_RET(ctx, "anydata", mod->submodule ? "submodule" : "module");
Radek Krejci10113652018-11-14 16:56:50 +01004478 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004479 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004480 LY_CHECK_RET(parse_any(ctx, data, kw, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004481 break;
4482 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004483 LY_CHECK_RET(parse_choice(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004484 break;
4485 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004486 LY_CHECK_RET(parse_container(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004487 break;
4488 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004489 LY_CHECK_RET(parse_leaf(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004490 break;
4491 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004492 LY_CHECK_RET(parse_leaflist(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004493 break;
4494 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004495 LY_CHECK_RET(parse_list(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004496 break;
4497 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004498 LY_CHECK_RET(parse_uses(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004499 break;
4500
4501 case YANG_AUGMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004502 LY_CHECK_RET(parse_augment(ctx, data, NULL, &mod->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004503 break;
4504 case YANG_DEVIATION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004505 LY_CHECK_RET(parse_deviation(ctx, data, &mod->deviations));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004506 break;
4507 case YANG_EXTENSION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004508 LY_CHECK_RET(parse_extension(ctx, data, &mod->extensions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004509 break;
4510 case YANG_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004511 LY_CHECK_RET(parse_feature(ctx, data, &mod->features));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004512 break;
4513 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004514 LY_CHECK_RET(parse_grouping(ctx, data, NULL, &mod->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004515 break;
4516 case YANG_IDENTITY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004517 LY_CHECK_RET(parse_identity(ctx, data, &mod->identities));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004518 break;
4519 case YANG_NOTIFICATION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004520 LY_CHECK_RET(parse_notif(ctx, data, NULL, &mod->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004521 break;
4522 case YANG_RPC:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004523 LY_CHECK_RET(parse_action(ctx, data, NULL, &mod->rpcs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004524 break;
4525 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01004526 LY_CHECK_RET(parse_typedef(ctx, NULL, data, &mod->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004527 break;
4528 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004529 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004530 break;
4531
4532 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004533 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), mod->submodule ? "submodule" : "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004534 return LY_EVALID;
4535 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004536 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004537 LY_CHECK_RET(ret);
Radek Krejcie86bf772018-12-14 11:39:53 +01004538
4539 /* finalize parent pointers to the reallocated items */
4540 LY_ARRAY_FOR(mod->groupings, u) {
4541 LY_LIST_FOR(mod->groupings[u].data, child) {
4542 child->parent = (struct lysp_node*)&mod->groupings[u];
4543 }
4544 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004545 /* TODO the same finalization for rpcs and notifications, do also in the relevant nodes */
Radek Krejcie86bf772018-12-14 11:39:53 +01004546
Radek Krejci6d6556c2018-11-08 09:37:45 +01004547checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004548 /* mandatory substatements */
4549 if (mod->submodule) {
4550 if (!mod->belongsto) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004551 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004552 return LY_EVALID;
4553 }
4554 } else {
4555 if (!mod->ns) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004556 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004557 return LY_EVALID;
4558 } else if (!mod->prefix) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004559 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004560 return LY_EVALID;
4561 }
4562 }
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 */
4566 dup = ly_ctx_get_submodule(ctx->ctx, NULL, mod->name, NULL);
4567 if (dup && (!mod->submodule || strcmp(dup->belongsto, mod->belongsto))) {
4568 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Name collision between %s of name \"%s\".",
4569 mod->submodule ? "submodules" : "module and submodule", mod->name);
4570 return LY_EVALID;
4571 }
4572
Michal Vasko7fbc8162018-09-17 10:35:16 +02004573 return ret;
4574}
4575
Radek Krejcid4557c62018-09-17 11:42:09 +02004576LY_ERR
Radek Krejcibbe09a92018-11-08 09:36:54 +01004577yang_parse(struct ly_parser_ctx *context, const char *data, struct lysp_module **mod_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004578{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004579 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004580 char *word, *buf;
Radek Krejciefd22f62018-09-27 11:47:58 +02004581 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004582 enum yang_keyword kw;
Radek Krejci0c2cf322018-10-13 08:02:30 +02004583 struct lysp_module *mod = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004584
4585 /* "module"/"submodule" */
Radek Krejcibbe09a92018-11-08 09:36:54 +01004586 ret = get_keyword(context, &data, &kw, &word, &word_len);
4587 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004588
4589 if ((kw != YANG_MODULE) && (kw != YANG_SUBMODULE)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +01004590 LOGVAL_YANG(context, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".",
Radek Krejcic59bc972018-09-17 16:13:06 +02004591 ly_stmt2str(kw));
Radek Krejcibbe09a92018-11-08 09:36:54 +01004592 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004593 }
4594
4595 mod = calloc(1, sizeof *mod);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004596 LY_CHECK_ERR_GOTO(!mod, LOGMEM(context->ctx), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004597 if (kw == YANG_SUBMODULE) {
4598 mod->submodule = 1;
4599 }
Radek Krejcibbe09a92018-11-08 09:36:54 +01004600 mod->parsing = 1;
4601 mod->ctx = context->ctx;
4602 context->mod = mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004603
4604 /* substatements */
Radek Krejcibbe09a92018-11-08 09:36:54 +01004605 ret = parse_sub_module(context, &data, mod);
4606 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004607
4608 /* read some trailing spaces or new lines */
Radek Krejcibbe09a92018-11-08 09:36:54 +01004609 ret = get_argument(context, &data, Y_MAYBE_STR_ARG, &word, &buf, &word_len);
4610 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004611
4612 if (word) {
Radek Krejcibbe09a92018-11-08 09:36:54 +01004613 LOGVAL_YANG(context, LYVE_SYNTAX, "Invalid character sequence \"%.*s\", expected end-of-file.",
Michal Vasko7fbc8162018-09-17 10:35:16 +02004614 word_len, word);
4615 free(buf);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004616 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004617 }
4618 assert(!buf);
4619
Radek Krejcibbe09a92018-11-08 09:36:54 +01004620 mod->parsing = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004621 *mod_p = mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004622
Radek Krejcibbe09a92018-11-08 09:36:54 +01004623cleanup:
4624 if (ret) {
4625 lysp_module_free(mod);
4626 }
4627
Michal Vasko7fbc8162018-09-17 10:35:16 +02004628 return ret;
4629}