blob: 98c27d27489c3db0a886f91730ac5dc16326790e [file] [log] [blame]
Michal Vasko7fbc8162018-09-17 10:35:16 +02001/**
2 * @file parser_yang.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief YANG parser
5 *
6 * Copyright (c) 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14#include <sys/types.h>
15#include <sys/stat.h>
16#include <unistd.h>
17#include <fcntl.h>
18#include <stdio.h>
19#include <stdlib.h>
20#include <stdint.h>
21#include <errno.h>
22#include <ctype.h>
23#include <string.h>
24#include <dirent.h>
25#include <assert.h>
26
27#include "common.h"
28#include "context.h"
29#include "libyang.h"
Radek Krejci70853c52018-10-15 14:46:16 +020030#include "tree_schema_internal.h"
Radek Krejci44ceedc2018-10-02 15:54:31 +020031
32/* Macro to check YANG's yang-char grammar rule */
33#define is_yangutf8char(c) ((c >= 0x20 && c <= 0xd77) || c == 0x09 || c == 0x0a || c == 0x0d || \
34 (c >= 0xe000 && c <= 0xfdcf) || (c >= 0xfdf0 && c <= 0xfffd) || \
35 (c >= 0x10000 && c <= 0x1fffd) || (c >= 0x20000 && c <= 0x2fffd) || \
36 (c >= 0x30000 && c <= 0x3fffd) || (c >= 0x40000 && c <= 0x2fffd) || \
37 (c >= 0x50000 && c <= 0x5fffd) || (c >= 0x60000 && c <= 0x6fffd) || \
38 (c >= 0x70000 && c <= 0x7fffd) || (c >= 0x80000 && c <= 0x8fffd) || \
39 (c >= 0x90000 && c <= 0x9fffd) || (c >= 0xa0000 && c <= 0xafffd) || \
40 (c >= 0xb0000 && c <= 0xbfffd) || (c >= 0xc0000 && c <= 0xcfffd) || \
41 (c >= 0xd0000 && c <= 0xdfffd) || (c >= 0xe0000 && c <= 0xefffd) || \
42 (c >= 0xf0000 && c <= 0xffffd) || (c >= 0x100000 && c <= 0x10fffd))
43
Radek Krejciceaf2122019-01-02 15:03:26 +010044/**
45 * @brief Try to find object with MEMBER string matching the IDENT in the given ARRAY.
46 * Macro logs an error message and returns LY_EVALID in case of existence of a matching object.
47 *
48 * @param[in] CTX yang parser context for logging.
49 * @param[in] ARRAY [sized array](@ref sizedarrays) of a generic objects with member named MEMBER to search.
50 * @param[in] MEMBER Name of the member of the objects in the ARRAY to compare.
51 * @param[in] STMT Name of the compared YANG statements for logging.
52 * @param[in] IDENT String trying to find in the ARRAY's objects inside the MEMBER member.
53 */
Radek Krejcifaa1eac2018-10-30 14:34:55 +010054#define CHECK_UNIQUENESS(CTX, ARRAY, MEMBER, STMT, IDENT) \
55 if (ARRAY) { \
56 for (unsigned int u = 0; u < LY_ARRAY_SIZE(ARRAY) - 1; ++u) { \
57 if (!strcmp((ARRAY)[u].MEMBER, IDENT)) { \
58 LOGVAL_YANG(CTX, LY_VCODE_DUPIDENT, IDENT, STMT); \
59 return LY_EVALID; \
60 } \
61 } \
62 }
63
Radek Krejciceaf2122019-01-02 15:03:26 +010064/**
65 * @brief Insert WORD into the libyang context's dictionary and store as TARGET.
66 * @param[in] CTX yang parser context to access libyang context.
67 * @param[in] BUF buffer in case the word is not a constant and can be inserted directly (zero-copy)
68 * @param[out] TARGET variable where to store the pointer to the inserted value.
69 * @param[in] WORD string to store.
70 * @param[in] LEN length of the string in WORD to store.
71 */
Radek Krejci9fcacc12018-10-11 15:59:11 +020072#define INSERT_WORD(CTX, BUF, TARGET, WORD, LEN) \
73 if (BUF) {(TARGET) = lydict_insert_zc((CTX)->ctx, WORD);}\
74 else {(TARGET) = lydict_insert((CTX)->ctx, WORD, LEN);}
Radek Krejci44ceedc2018-10-02 15:54:31 +020075
Radek Krejciceaf2122019-01-02 15:03:26 +010076/**
77 * @brief Move the DATA pointer by COUNT items. Also updates the indent value in yang parser context
78 * @param[in] CTX yang parser context to update its indent value.
79 * @param[in,out] DATA pointer to move
80 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
81 */
Radek Krejci2b610482019-01-02 13:36:09 +010082#define MOVE_INPUT(CTX, DATA, COUNT) (*(DATA))+=COUNT;(CTX)->indent+=COUNT
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020083
Michal Vaskoea5abea2018-09-18 13:10:54 +020084/**
85 * @brief Loop through all substatements providing, return if there are none.
86 *
Radek Krejci44ceedc2018-10-02 15:54:31 +020087 * @param[in] CTX yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +020088 * @param[in] DATA Raw data to read from.
89 * @param[out] KW YANG keyword read.
90 * @param[out] WORD Pointer to the keyword itself.
91 * @param[out] WORD_LEN Length of the keyword.
92 * @param[out] ERR Variable for error storing.
93 *
94 * @return In case there are no substatements or a fatal error encountered.
95 */
Radek Krejci6d6556c2018-11-08 09:37:45 +010096#define YANG_READ_SUBSTMT_FOR(CTX, DATA, KW, WORD, WORD_LEN, ERR, CHECKGOTO) \
Radek Krejci6d9b9b52018-11-02 12:43:39 +010097 LY_CHECK_RET(get_keyword(CTX, DATA, &KW, &WORD, &WORD_LEN)); \
Michal Vasko7fbc8162018-09-17 10:35:16 +020098 if (KW == YANG_SEMICOLON) { \
Radek Krejci6d6556c2018-11-08 09:37:45 +010099 CHECKGOTO; \
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100100 return LY_SUCCESS; \
Michal Vasko7fbc8162018-09-17 10:35:16 +0200101 } \
102 if (KW != YANG_LEFT_BRACE) { \
Radek Krejci44ceedc2018-10-02 15:54:31 +0200103 LOGVAL_YANG(CTX, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\", expected \";\" or \"{\".", ly_stmt2str(KW)); \
Michal Vasko7fbc8162018-09-17 10:35:16 +0200104 return LY_EVALID; \
105 } \
106 for (ERR = get_keyword(CTX, DATA, &KW, &WORD, &WORD_LEN); \
107 !ERR && (KW != YANG_RIGHT_BRACE); \
108 ERR = get_keyword(CTX, DATA, &KW, &WORD, &WORD_LEN))
109
Radek Krejciceaf2122019-01-02 15:03:26 +0100110/**
111 * @brief Check module version is at least 2 (YANG 1.1) because of the keyword presence.
112 * Logs error message and returns LY_EVALID in case of module in YANG version 1.0.
113 * @param[in] CTX yang parser context to get current module and for logging.
114 * @param[in] KW keyword allowed only in YANG version 1.1 (or later) - for logging.
115 * @param[in] PARENT parent statement where the KW is present - for logging.
116 */
117#define YANG_CHECK_STMTVER2_RET(CTX, KW, PARENT) \
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100118 if ((CTX)->mod_version < 2) {LOGVAL_YANG((CTX), LY_VCODE_INCHILDSTMT2, KW, PARENT); return LY_EVALID;}
Radek Krejci10113652018-11-14 16:56:50 +0100119
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100120static LY_ERR parse_container(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
121static LY_ERR parse_uses(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
122static LY_ERR parse_choice(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
123static LY_ERR parse_case(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
124static LY_ERR parse_list(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
125static LY_ERR parse_grouping(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_grp **groupings);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200126
Michal Vaskoea5abea2018-09-18 13:10:54 +0200127/**
128 * @brief Add another character to dynamic buffer, a low-level function.
129 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200130 * Enlarge if needed. Updates \p input as well as \p buf_used.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200131 *
Radek Krejci404251e2018-10-09 12:06:44 +0200132 * @param[in] ctx libyang context for logging.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200133 * @param[in, out] input Input string to process.
134 * @param[in] len Number of bytes to get from the input string and copy into the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200135 * @param[in,out] buf Buffer to use, can be moved by realloc().
136 * @param[in,out] buf_len Current size of the buffer.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200137 * @param[in,out] buf_used Currently used characters of the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200138 *
139 * @return LY_ERR values.
140 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200141static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +0200142buf_add_char(struct ly_ctx *ctx, const char **input, size_t len, char **buf, size_t *buf_len, size_t *buf_used)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200143{
Radek Krejci44ceedc2018-10-02 15:54:31 +0200144 if (*buf_len <= (*buf_used) + len) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200145 *buf_len += 16;
146 *buf = ly_realloc(*buf, *buf_len);
147 LY_CHECK_ERR_RET(!*buf, LOGMEM(ctx), LY_EMEM);
148 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200149 memcpy(&(*buf)[*buf_used], *input, len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200150
Radek Krejci44ceedc2018-10-02 15:54:31 +0200151 (*buf_used) += len;
152 (*input) += len;
153
Michal Vasko7fbc8162018-09-17 10:35:16 +0200154 return LY_SUCCESS;
155}
156
Michal Vaskoea5abea2018-09-18 13:10:54 +0200157/**
Radek Krejci44ceedc2018-10-02 15:54:31 +0200158 * @brief Check that \p c is valid UTF8 code point for YANG string.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200159 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200160 * @param[in] ctx yang parser context for logging.
161 * @param[in] c UTF8 code point of a character to check.
162 * @return LY_ERR values.
163 */
164static LY_ERR
165check_stringchar(struct ly_parser_ctx *ctx, unsigned int c)
166{
167 if (!is_yangutf8char(c)) {
168 LOGVAL_YANG(ctx, LY_VCODE_INCHAR, c);
169 return LY_EVALID;
170 }
171 return LY_SUCCESS;
172}
173
174/**
175 * @brief Check that \p c is valid UTF8 code point for YANG identifier.
176 *
177 * @param[in] ctx yang parser context for logging.
178 * @param[in] c UTF8 code point of a character to check.
179 * @param[in] first Flag to check the first character of an identifier, which is more restricted.
Radek Krejcidcc7b322018-10-11 14:24:02 +0200180 * @param[in,out] prefix Storage for internally used flag in case of possible prefixed identifiers:
181 * 0 - colon not yet found (no prefix)
182 * 1 - \p c is the colon character
183 * 2 - prefix already processed, now processing the identifier
184 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200185 * If the identifier cannot be prefixed, NULL is expected.
186 * @return LY_ERR values.
187 */
188static LY_ERR
189check_identifierchar(struct ly_parser_ctx *ctx, unsigned int c, int first, int *prefix)
190{
191 if (first || (prefix && (*prefix) == 1)) {
192 if (!is_yangidentstartchar(c)) {
193 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '%c'.", c);
194 return LY_EVALID;
195 }
Radek Krejci9fcacc12018-10-11 15:59:11 +0200196 if (prefix) {
197 if (first) {
198 (*prefix) = 0;
199 } else {
200 (*prefix) = 2;
201 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200202 }
203 } else if (c == ':' && prefix && (*prefix) == 0) {
204 (*prefix) = 1;
205 } else if (!is_yangidentchar(c)) {
206 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Invalid identifier character '%c'.", c);
207 return LY_EVALID;
208 }
209
210 return LY_SUCCESS;
211}
212
213/**
214 * @brief Store a single UTF8 character. It depends whether in a dynamically-allocated buffer or just as a pointer to the data.
215 *
216 * @param[in] ctx yang parser context for logging.
217 * @param[in,out] input Pointer to the string where to get the character to store. Automatically moved to the next character
218 * when function returns.
219 * @param[in] arg Type of the input string to select method of checking character validity.
220 * @param[in,out] word_p Word pointer. If buffer (\p word_b) was not yet needed, it is just a pointer to the first
Michal Vaskoea5abea2018-09-18 13:10:54 +0200221 * stored character. If buffer was needed (\p word_b is non-NULL or \p need_buf is set), it is pointing to the buffer.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200222 * @param[in,out] word_len Current length of the word pointed to by \p word_p.
223 * @param[in,out] word_b Word buffer. Is kept NULL as long as it is not requested (word is a substring of the data).
Michal Vaskoea5abea2018-09-18 13:10:54 +0200224 * @param[in,out] buf_len Current length of \p word_b.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200225 * @param[in] need_buf Flag if the dynamically allocated buffer is required.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200226 *
227 * @return LY_ERR values.
228 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200229static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +0200230buf_store_char(struct ly_parser_ctx *ctx, const char **input, enum yang_arg arg,
231 char **word_p, size_t *word_len, char **word_b, size_t *buf_len, int need_buf)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200232{
Radek Krejci404251e2018-10-09 12:06:44 +0200233 int prefix = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200234 unsigned int c;
235 size_t len;
236
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.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001135 * @param[out] version Storage for the parsed information.
1136 * @param[in, out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001137 *
1138 * @return LY_ERR values.
1139 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001140static LY_ERR
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001141parse_yangversion(struct ly_parser_ctx *ctx, const char **data, uint8_t *version, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001142{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001143 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001144 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001145 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001146 enum yang_keyword kw;
1147
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001148 if (*version) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001149 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001150 return LY_EVALID;
1151 }
1152
1153 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001154 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001155
1156 if ((word_len == 3) && !strncmp(word, "1.0", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001157 *version = LYS_VERSION_1_0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001158 } else if ((word_len == 3) && !strncmp(word, "1.1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001159 *version = LYS_VERSION_1_1;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001160 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001161 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001162 free(buf);
1163 return LY_EVALID;
1164 }
1165 free(buf);
1166
Radek Krejci6d6556c2018-11-08 09:37:45 +01001167 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001168 switch (kw) {
1169 case YANG_CUSTOM:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001170 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_VERSION, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001171 break;
1172 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001173 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001174 return LY_EVALID;
1175 }
1176 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001177 return ret;
1178}
1179
Michal Vaskoea5abea2018-09-18 13:10:54 +02001180/**
1181 * @brief Parse the belongs-to statement.
1182 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001183 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001184 * @param[in,out] data Data to read from, always moved to currently handled character.
1185 * @param[in,out] belongsto Place to store the parsed value.
1186 * @param[in,out] prefix Place to store the parsed belongs-to prefix value.
1187 * @param[in,out] exts Extension instances to add to.
1188 *
1189 * @return LY_ERR values.
1190 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001191static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001192parse_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 +02001193{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001194 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001195 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001196 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001197 enum yang_keyword kw;
1198
1199 if (*belongsto) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001200 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001201 return LY_EVALID;
1202 }
1203
1204 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001205 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001206
Radek Krejci44ceedc2018-10-02 15:54:31 +02001207 INSERT_WORD(ctx, buf, *belongsto, word, word_len);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001208 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001209 switch (kw) {
1210 case YANG_PREFIX:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001211 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_PREFIX, 0, prefix, Y_IDENTIF_ARG, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001212 break;
1213 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001214 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_BELONGSTO, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001215 break;
1216 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001217 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001218 return LY_EVALID;
1219 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001220 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001221 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001222checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001223 /* mandatory substatements */
1224 if (!*prefix) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001225 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001226 return LY_EVALID;
1227 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001228 return ret;
1229}
1230
Michal Vaskoea5abea2018-09-18 13:10:54 +02001231/**
1232 * @brief Parse the revision-date statement.
1233 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001234 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001235 * @param[in,out] data Data to read from, always moved to currently handled character.
1236 * @param[in,out] rev Array to store the parsed value in.
1237 * @param[in,out] exts Extension instances to add to.
1238 *
1239 * @return LY_ERR values.
1240 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001241static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001242parse_revisiondate(struct ly_parser_ctx *ctx, const char **data, char *rev, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001243{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001244 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001245 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001246 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001247 enum yang_keyword kw;
1248
1249 if (rev[0]) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001250 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001251 return LY_EVALID;
1252 }
1253
1254 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001255 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001256
1257 /* check value */
Radek Krejcibbe09a92018-11-08 09:36:54 +01001258 if (lysp_check_date(ctx, word, word_len, "revision-date")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001259 free(buf);
1260 return LY_EVALID;
1261 }
1262
1263 /* store value and spend buf if allocated */
1264 strncpy(rev, word, word_len);
1265 free(buf);
1266
Radek Krejci6d6556c2018-11-08 09:37:45 +01001267 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001268 switch (kw) {
1269 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001270 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_REVISIONDATE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001271 break;
1272 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001273 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001274 return LY_EVALID;
1275 }
1276 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001277 return ret;
1278}
1279
Michal Vaskoea5abea2018-09-18 13:10:54 +02001280/**
1281 * @brief Parse the include statement.
1282 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001283 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001284 * @param[in] module_name Name of the module to check name collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001285 * @param[in,out] data Data to read from, always moved to currently handled character.
1286 * @param[in,out] includes Parsed includes to add to.
1287 *
1288 * @return LY_ERR values.
1289 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001290static LY_ERR
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001291parse_include(struct ly_parser_ctx *ctx, const char *module_name, const char **data, struct lysp_include **includes)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001292{
Radek Krejcid33273d2018-10-25 14:55:52 +02001293 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001294 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001295 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001296 enum yang_keyword kw;
1297 struct lysp_include *inc;
1298
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001299 LY_ARRAY_NEW_RET(ctx->ctx, *includes, inc, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001300
1301 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001302 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001303
Radek Krejci086c7132018-10-26 15:29:04 +02001304 INSERT_WORD(ctx, buf, inc->name, word, word_len);
1305
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001306 /* submodules share the namespace with the module names, so there must not be
1307 * a module of the same name in the context, no need for revision matching */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001308 if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(ctx->ctx, inc->name)) {
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001309 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Name collision between module and submodule of name \"%s\".", inc->name);
1310 return LY_EVALID;
1311 }
1312
Radek Krejci6d6556c2018-11-08 09:37:45 +01001313 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001314 switch (kw) {
1315 case YANG_DESCRIPTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01001316 YANG_CHECK_STMTVER2_RET(ctx, "description", "include");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001317 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 +02001318 break;
1319 case YANG_REFERENCE:
Radek Krejciceaf2122019-01-02 15:03:26 +01001320 YANG_CHECK_STMTVER2_RET(ctx, "reference", "include");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001321 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 +02001322 break;
1323 case YANG_REVISION_DATE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001324 LY_CHECK_RET(parse_revisiondate(ctx, data, inc->rev, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001325 break;
1326 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001327 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001328 break;
1329 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001330 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001331 return LY_EVALID;
1332 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001333 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001334 return ret;
1335}
1336
Michal Vaskoea5abea2018-09-18 13:10:54 +02001337/**
1338 * @brief Parse the import statement.
1339 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001340 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001341 * @param[in] module_prefix Prefix of the module to check prefix collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001342 * @param[in,out] data Data to read from, always moved to currently handled character.
1343 * @param[in,out] imports Parsed imports to add to.
1344 *
1345 * @return LY_ERR values.
1346 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001347static LY_ERR
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001348parse_import(struct ly_parser_ctx *ctx, const char *module_prefix, const char **data, struct lysp_import **imports)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001349{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001350 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001351 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001352 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001353 enum yang_keyword kw;
1354 struct lysp_import *imp;
1355
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001356 LY_ARRAY_NEW_RET(ctx->ctx, *imports, imp, LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001357
1358 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001359 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02001360 INSERT_WORD(ctx, buf, imp->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001361
Radek Krejci6d6556c2018-11-08 09:37:45 +01001362 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001363 switch (kw) {
1364 case YANG_PREFIX:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001365 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_PREFIX, 0, &imp->prefix, Y_IDENTIF_ARG, &imp->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001366 LY_CHECK_RET(lysp_check_prefix(ctx, *imports, module_prefix, &imp->prefix), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001367 break;
1368 case YANG_DESCRIPTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01001369 YANG_CHECK_STMTVER2_RET(ctx, "description", "import");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001370 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 +02001371 break;
1372 case YANG_REFERENCE:
Radek Krejciceaf2122019-01-02 15:03:26 +01001373 YANG_CHECK_STMTVER2_RET(ctx, "reference", "import");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001374 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 +02001375 break;
1376 case YANG_REVISION_DATE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001377 LY_CHECK_RET(parse_revisiondate(ctx, data, imp->rev, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001378 break;
1379 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001380 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001381 break;
1382 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001383 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001384 return LY_EVALID;
1385 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001386 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001387 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001388checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001389 /* mandatory substatements */
Radek Krejci086c7132018-10-26 15:29:04 +02001390 LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001391
1392 return ret;
1393}
1394
Michal Vaskoea5abea2018-09-18 13:10:54 +02001395/**
1396 * @brief Parse the revision statement.
1397 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001398 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001399 * @param[in,out] data Data to read from, always moved to currently handled character.
1400 * @param[in,out] revs Parsed revisions to add to.
1401 *
1402 * @return LY_ERR values.
1403 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001404static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001405parse_revision(struct ly_parser_ctx *ctx, const char **data, struct lysp_revision **revs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001406{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001407 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001408 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001409 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001410 enum yang_keyword kw;
1411 struct lysp_revision *rev;
1412
Radek Krejci2c4e7172018-10-19 15:56:26 +02001413 LY_ARRAY_NEW_RET(ctx->ctx, *revs, rev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001414
1415 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001416 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001417
1418 /* check value */
Radek Krejcibbe09a92018-11-08 09:36:54 +01001419 if (lysp_check_date(ctx, word, word_len, "revision")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001420 return LY_EVALID;
1421 }
1422
Radek Krejcib7db73a2018-10-24 14:18:40 +02001423 strncpy(rev->date, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001424 free(buf);
1425
Radek Krejci6d6556c2018-11-08 09:37:45 +01001426 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001427 switch (kw) {
1428 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001429 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 +02001430 break;
1431 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001432 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 +02001433 break;
1434 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001435 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001436 break;
1437 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001438 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001439 return LY_EVALID;
1440 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001441 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001442 return ret;
1443}
1444
Michal Vaskoea5abea2018-09-18 13:10:54 +02001445/**
1446 * @brief Parse a generic text field that can have more instances such as base.
1447 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001448 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001449 * @param[in,out] data Data to read from, always moved to currently handled character.
1450 * @param[in] substmt Type of this substatement.
1451 * @param[in,out] texts Parsed values to add to.
1452 * @param[in] arg Type of the expected argument.
1453 * @param[in,out] exts Extension instances to add to.
1454 *
1455 * @return LY_ERR values.
1456 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001457static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001458parse_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 +02001459 struct lysp_ext_instance **exts)
1460{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001461 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001462 char *buf, *word;
Radek Krejci151a5b72018-10-19 14:21:44 +02001463 const char **item;
1464 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001465 enum yang_keyword kw;
1466
1467 /* allocate new pointer */
Radek Krejci2c4e7172018-10-19 15:56:26 +02001468 LY_ARRAY_NEW_RET(ctx->ctx, *texts, item, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001469
1470 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001471 LY_CHECK_RET(get_argument(ctx, data, arg, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001472
Radek Krejci151a5b72018-10-19 14:21:44 +02001473 INSERT_WORD(ctx, buf, *item, word, word_len);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001474 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001475 switch (kw) {
1476 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001477 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, substmt, LY_ARRAY_SIZE(*texts) - 1, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001478 break;
1479 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001480 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001481 return LY_EVALID;
1482 }
1483 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001484 return ret;
1485}
1486
Michal Vaskoea5abea2018-09-18 13:10:54 +02001487/**
1488 * @brief Parse the config statement.
1489 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001490 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001491 * @param[in,out] data Data to read from, always moved to currently handled character.
1492 * @param[in,out] flags Flags to add to.
1493 * @param[in,out] exts Extension instances to add to.
1494 *
1495 * @return LY_ERR values.
1496 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001497static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001498parse_config(struct ly_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001499{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001500 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001501 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001502 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001503 enum yang_keyword kw;
1504
1505 if (*flags & LYS_CONFIG_MASK) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001506 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001507 return LY_EVALID;
1508 }
1509
1510 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001511 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001512
1513 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1514 *flags |= LYS_CONFIG_W;
1515 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1516 *flags |= LYS_CONFIG_R;
1517 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001518 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001519 free(buf);
1520 return LY_EVALID;
1521 }
1522 free(buf);
1523
Radek Krejci6d6556c2018-11-08 09:37:45 +01001524 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001525 switch (kw) {
1526 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001527 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_CONFIG, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001528 break;
1529 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001530 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001531 return LY_EVALID;
1532 }
1533 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001534 return ret;
1535}
1536
Michal Vaskoea5abea2018-09-18 13:10:54 +02001537/**
1538 * @brief Parse the mandatory statement.
1539 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001540 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001541 * @param[in,out] data Data to read from, always moved to currently handled character.
1542 * @param[in,out] flags Flags to add to.
1543 * @param[in,out] exts Extension instances to add to.
1544 *
1545 * @return LY_ERR values.
1546 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001547static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001548parse_mandatory(struct ly_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001549{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001550 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001551 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001552 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001553 enum yang_keyword kw;
1554
1555 if (*flags & LYS_MAND_MASK) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001556 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001557 return LY_EVALID;
1558 }
1559
1560 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001561 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001562
1563 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1564 *flags |= LYS_MAND_TRUE;
1565 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1566 *flags |= LYS_MAND_FALSE;
1567 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001568 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001569 free(buf);
1570 return LY_EVALID;
1571 }
1572 free(buf);
1573
Radek Krejci6d6556c2018-11-08 09:37:45 +01001574 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001575 switch (kw) {
1576 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001577 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MANDATORY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001578 break;
1579 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001580 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001581 return LY_EVALID;
1582 }
1583 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001584 return ret;
1585}
1586
Michal Vaskoea5abea2018-09-18 13:10:54 +02001587/**
1588 * @brief Parse a restriction such as range or length.
1589 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001590 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001591 * @param[in,out] data Data to read from, always moved to currently handled character.
1592 * @param[in] restr_kw Type of this particular restriction.
1593 * @param[in,out] exts Extension instances to add to.
1594 *
1595 * @return LY_ERR values.
1596 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001597static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001598parse_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 +02001599{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001600 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001601 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001602 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001603 enum yang_keyword kw;
1604
1605 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001606 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001607
Radek Krejci44ceedc2018-10-02 15:54:31 +02001608 INSERT_WORD(ctx, buf, restr->arg, word, word_len);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001609 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001610 switch (kw) {
1611 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001612 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 +02001613 break;
1614 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001615 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 +02001616 break;
1617 case YANG_ERROR_APP_TAG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001618 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 +02001619 break;
1620 case YANG_ERROR_MESSAGE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001621 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 +02001622 break;
1623 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001624 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001625 break;
1626 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001627 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001628 return LY_EVALID;
1629 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001630 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001631 return ret;
1632}
1633
Michal Vaskoea5abea2018-09-18 13:10:54 +02001634/**
1635 * @brief Parse a restriction that can have more instances such as must.
1636 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001637 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001638 * @param[in,out] data Data to read from, always moved to currently handled character.
1639 * @param[in] restr_kw Type of this particular restriction.
1640 * @param[in,out] restrs Restrictions to add to.
1641 *
1642 * @return LY_ERR values.
1643 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001644static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001645parse_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 +02001646{
1647 struct lysp_restr *restr;
1648
Radek Krejci2c4e7172018-10-19 15:56:26 +02001649 LY_ARRAY_NEW_RET(ctx->ctx, *restrs, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001650 return parse_restr(ctx, data, restr_kw, restr);
1651}
1652
Michal Vaskoea5abea2018-09-18 13:10:54 +02001653/**
1654 * @brief Parse the status statement.
1655 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001656 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001657 * @param[in,out] data Data to read from, always moved to currently handled character.
1658 * @param[in,out] flags Flags to add to.
1659 * @param[in,out] exts Extension instances to add to.
1660 *
1661 * @return LY_ERR values.
1662 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001663static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001664parse_status(struct ly_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001665{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001666 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001667 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001668 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001669 enum yang_keyword kw;
1670
1671 if (*flags & LYS_STATUS_MASK) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001672 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001673 return LY_EVALID;
1674 }
1675
1676 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001677 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001678
1679 if ((word_len == 7) && !strncmp(word, "current", word_len)) {
1680 *flags |= LYS_STATUS_CURR;
1681 } else if ((word_len == 10) && !strncmp(word, "deprecated", word_len)) {
1682 *flags |= LYS_STATUS_DEPRC;
1683 } else if ((word_len == 8) && !strncmp(word, "obsolete", word_len)) {
1684 *flags |= LYS_STATUS_OBSLT;
1685 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001686 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001687 free(buf);
1688 return LY_EVALID;
1689 }
1690 free(buf);
1691
Radek Krejci6d6556c2018-11-08 09:37:45 +01001692 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001693 switch (kw) {
1694 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001695 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_STATUS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001696 break;
1697 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001698 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001699 return LY_EVALID;
1700 }
1701 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001702 return ret;
1703}
1704
Michal Vaskoea5abea2018-09-18 13:10:54 +02001705/**
1706 * @brief Parse the when statement.
1707 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001708 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001709 * @param[in,out] data Data to read from, always moved to currently handled character.
1710 * @param[in,out] when_p When pointer to parse to.
1711 *
1712 * @return LY_ERR values.
1713 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001714static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001715parse_when(struct ly_parser_ctx *ctx, const char **data, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001716{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001717 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001718 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001719 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001720 enum yang_keyword kw;
1721 struct lysp_when *when;
1722
1723 if (*when_p) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001724 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001725 return LY_EVALID;
1726 }
1727
1728 when = calloc(1, sizeof *when);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001729 LY_CHECK_ERR_RET(!when, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001730 *when_p = when;
1731
1732 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001733 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02001734 INSERT_WORD(ctx, buf, when->cond, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001735
Radek Krejci6d6556c2018-11-08 09:37:45 +01001736 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001737 switch (kw) {
1738 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001739 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 +02001740 break;
1741 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001742 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 +02001743 break;
1744 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001745 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001746 break;
1747 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001748 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001749 return LY_EVALID;
1750 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001751 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001752 return ret;
1753}
1754
Michal Vaskoea5abea2018-09-18 13:10:54 +02001755/**
1756 * @brief Parse the anydata or anyxml statement.
1757 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001758 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001759 * @param[in,out] data Data to read from, always moved to currently handled character.
1760 * @param[in] kw Type of this particular keyword.
1761 * @param[in,out] siblings Siblings to add to.
1762 *
1763 * @return LY_ERR values.
1764 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001765static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001766parse_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 +02001767{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001768 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001769 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001770 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001771 struct lysp_node *iter;
1772 struct lysp_node_anydata *any;
1773
1774 /* create structure */
1775 any = calloc(1, sizeof *any);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001776 LY_CHECK_ERR_RET(!any, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001777 any->nodetype = kw == YANG_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001778 any->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001779
1780 /* insert into siblings */
1781 if (!*siblings) {
1782 *siblings = (struct lysp_node *)any;
1783 } else {
1784 for (iter = *siblings; iter->next; iter = iter->next);
1785 iter->next = (struct lysp_node *)any;
1786 }
1787
1788 /* get name */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001789 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02001790 INSERT_WORD(ctx, buf, any->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001791
1792 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01001793 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001794 switch (kw) {
1795 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001796 LY_CHECK_RET(parse_config(ctx, data, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001797 break;
1798 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001799 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 +02001800 break;
1801 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001802 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 +02001803 break;
1804 case YANG_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001805 LY_CHECK_RET(parse_mandatory(ctx, data, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001806 break;
1807 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001808 LY_CHECK_RET(parse_restrs(ctx, data, kw, &any->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001809 break;
1810 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001811 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 +02001812 break;
1813 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001814 LY_CHECK_RET(parse_status(ctx, data, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001815 break;
1816 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001817 LY_CHECK_RET(parse_when(ctx, data, &any->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001818 break;
1819 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001820 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001821 break;
1822 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001823 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
Radek Krejcic59bc972018-09-17 16:13:06 +02001824 (any->nodetype & LYS_ANYDATA) == LYS_ANYDATA ? ly_stmt2str(YANG_ANYDATA) : ly_stmt2str(YANG_ANYXML));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001825 return LY_EVALID;
1826 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001827 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001828 return ret;
1829}
1830
Michal Vaskoea5abea2018-09-18 13:10:54 +02001831/**
1832 * @brief Parse the value or position statement. Substatement of type enum statement.
1833 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001834 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001835 * @param[in,out] data Data to read from, always moved to currently handled character.
1836 * @param[in] val_kw Type of this particular keyword.
1837 * @param[in,out] value Value to write to.
1838 * @param[in,out] flags Flags to write to.
1839 * @param[in,out] exts Extension instances to add to.
1840 *
1841 * @return LY_ERR values.
1842 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001843static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001844parse_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 +02001845 struct lysp_ext_instance **exts)
1846{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001847 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001848 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001849 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001850 long int num;
1851 unsigned long int unum;
1852 enum yang_keyword kw;
1853
1854 if (*flags & LYS_SET_VALUE) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001855 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001856 return LY_EVALID;
1857 }
1858 *flags |= LYS_SET_VALUE;
1859
1860 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001861 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001862
1863 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 +02001864 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001865 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001866 }
1867
1868 errno = 0;
1869 if (val_kw == YANG_VALUE) {
1870 num = strtol(word, &ptr, 10);
Radek Krejci8b764662018-11-14 14:15:13 +01001871 if (num < INT64_C(-2147483648) || num > INT64_C(2147483647)) {
1872 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
1873 goto error;
1874 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001875 } else {
1876 unum = strtoul(word, &ptr, 10);
Radek Krejci8b764662018-11-14 14:15:13 +01001877 if (unum > UINT64_C(4294967295)) {
1878 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
1879 goto error;
1880 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001881 }
1882 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001883 if ((size_t)(ptr - word) != word_len) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001884 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001885 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001886 }
1887 if (errno == ERANGE) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001888 LOGVAL_YANG(ctx, LY_VCODE_OOB, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001889 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001890 }
1891 if (val_kw == YANG_VALUE) {
1892 *value = num;
1893 } else {
1894 *value = unum;
1895 }
1896 free(buf);
1897
Radek Krejci6d6556c2018-11-08 09:37:45 +01001898 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001899 switch (kw) {
1900 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001901 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 +02001902 break;
1903 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001904 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001905 return LY_EVALID;
1906 }
1907 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001908 return ret;
Radek Krejci8b764662018-11-14 14:15:13 +01001909
1910error:
1911 free(buf);
1912 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001913}
1914
Michal Vaskoea5abea2018-09-18 13:10:54 +02001915/**
1916 * @brief Parse the enum or bit statement. Substatement of type statement.
1917 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001918 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001919 * @param[in,out] data Data to read from, always moved to currently handled character.
1920 * @param[in] enum_kw Type of this particular keyword.
1921 * @param[in,out] enums Enums or bits to add to.
1922 *
1923 * @return LY_ERR values.
1924 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001925static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001926parse_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 +02001927{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001928 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001929 char *buf, *word;
Radek Krejci8b764662018-11-14 14:15:13 +01001930 size_t word_len, u;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001931 enum yang_keyword kw;
1932 struct lysp_type_enum *enm;
1933
Radek Krejci2c4e7172018-10-19 15:56:26 +02001934 LY_ARRAY_NEW_RET(ctx->ctx, *enums, enm, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001935
1936 /* get value */
Radek Krejci8b764662018-11-14 14:15:13 +01001937 LY_CHECK_RET(get_argument(ctx, data, enum_kw == YANG_ENUM ? Y_STR_ARG : Y_IDENTIF_ARG, &word, &buf, &word_len));
1938 if (enum_kw == YANG_ENUM) {
1939 if (!word_len) {
1940 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Enum name must not be zero-length.");
1941 free(buf);
1942 return LY_EVALID;
1943 } else if (isspace(word[0]) || isspace(word[word_len - 1])) {
1944 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Enum name must not have any leading or trailing whitespaces (\"%.*s\").",
1945 word_len, word);
1946 free(buf);
1947 return LY_EVALID;
1948 } else {
1949 for (u = 0; u < word_len; ++u) {
1950 if (iscntrl(word[u])) {
1951 LOGWRN(ctx->ctx, "Control characters in enum name should be avoided (\"%.*s\", character number %d).",
1952 word_len, word, u + 1);
1953 break;
1954 }
1955 }
1956 }
1957 } else { /* YANG_BIT */
1958
1959 }
Radek Krejci44ceedc2018-10-02 15:54:31 +02001960 INSERT_WORD(ctx, buf, enm->name, word, word_len);
Radek Krejci8b764662018-11-14 14:15:13 +01001961 CHECK_UNIQUENESS(ctx, *enums, name, ly_stmt2str(enum_kw), enm->name);
1962
Radek Krejci6d6556c2018-11-08 09:37:45 +01001963 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001964 switch (kw) {
1965 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001966 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 +02001967 break;
1968 case YANG_IF_FEATURE:
Radek Krejciceaf2122019-01-02 15:03:26 +01001969 YANG_CHECK_STMTVER2_RET(ctx, "if-feature", ly_stmt2str(enum_kw));
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001970 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 +02001971 break;
1972 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001973 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 +02001974 break;
1975 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001976 LY_CHECK_RET(parse_status(ctx, data, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001977 break;
1978 case YANG_VALUE:
1979 case YANG_POSITION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001980 LY_CHECK_RET(parse_type_enum_value_pos(ctx, data, kw, &enm->value, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001981 break;
1982 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001983 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001984 break;
1985 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001986 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001987 return LY_EVALID;
1988 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001989 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001990 return ret;
1991}
1992
Michal Vaskoea5abea2018-09-18 13:10:54 +02001993/**
1994 * @brief Parse the fraction-digits statement. Substatement of type statement.
1995 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001996 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001997 * @param[in,out] data Data to read from, always moved to currently handled character.
1998 * @param[in,out] fracdig Value to write to.
1999 * @param[in,out] exts Extension instances to add to.
2000 *
2001 * @return LY_ERR values.
2002 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002003static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002004parse_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 +02002005{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002006 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002007 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002008 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002009 unsigned long int num;
2010 enum yang_keyword kw;
2011
2012 if (*fracdig) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002013 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002014 return LY_EVALID;
2015 }
2016
2017 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002018 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002019
2020 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002021 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002022 free(buf);
2023 return LY_EVALID;
2024 }
2025
2026 errno = 0;
2027 num = strtoul(word, &ptr, 10);
2028 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002029 if ((size_t)(ptr - word) != word_len) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002030 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002031 free(buf);
2032 return LY_EVALID;
2033 }
2034 if ((errno == ERANGE) || (num > 18)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002035 LOGVAL_YANG(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002036 free(buf);
2037 return LY_EVALID;
2038 }
2039 *fracdig = num;
2040 free(buf);
2041
Radek Krejci6d6556c2018-11-08 09:37:45 +01002042 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002043 switch (kw) {
2044 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002045 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_FRACDIGITS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002046 break;
2047 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002048 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002049 return LY_EVALID;
2050 }
2051 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002052 return ret;
2053}
2054
Michal Vaskoea5abea2018-09-18 13:10:54 +02002055/**
2056 * @brief Parse the require-instance statement. Substatement of type statement.
2057 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002058 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002059 * @param[in,out] data Data to read from, always moved to currently handled character.
2060 * @param[in,out] reqinst Value to write to.
2061 * @param[in,out] flags Flags to write to.
2062 * @param[in,out] exts Extension instances to add to.
2063 *
2064 * @return LY_ERR values.
2065 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002066static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002067parse_type_reqinstance(struct ly_parser_ctx *ctx, const char **data, uint8_t *reqinst, uint16_t *flags,
Michal Vasko7fbc8162018-09-17 10:35:16 +02002068 struct lysp_ext_instance **exts)
2069{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002070 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002071 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002072 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002073 enum yang_keyword kw;
2074
2075 if (*flags & LYS_SET_REQINST) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002076 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002077 return LY_EVALID;
2078 }
2079 *flags |= LYS_SET_REQINST;
2080
2081 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002082 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002083
2084 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
2085 *reqinst = 1;
2086 } else if ((word_len != 5) || strncmp(word, "false", word_len)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002087 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002088 free(buf);
2089 return LY_EVALID;
2090 }
2091 free(buf);
2092
Radek Krejci6d6556c2018-11-08 09:37:45 +01002093 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002094 switch (kw) {
2095 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002096 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_REQINSTANCE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002097 break;
2098 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002099 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002100 return LY_EVALID;
2101 }
2102 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002103 return ret;
2104}
2105
Michal Vaskoea5abea2018-09-18 13:10:54 +02002106/**
2107 * @brief Parse the modifier statement. Substatement of type pattern statement.
2108 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002109 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002110 * @param[in,out] data Data to read from, always moved to currently handled character.
2111 * @param[in,out] pat Value to write to.
2112 * @param[in,out] exts Extension instances to add to.
2113 *
2114 * @return LY_ERR values.
2115 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002116static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002117parse_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 +02002118{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002119 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002120 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002121 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002122 enum yang_keyword kw;
2123
2124 if ((*pat)[0] == 0x15) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002125 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002126 return LY_EVALID;
2127 }
2128
2129 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002130 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002131
2132 if ((word_len != 12) || strncmp(word, "invert-match", word_len)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002133 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002134 free(buf);
2135 return LY_EVALID;
2136 }
2137 free(buf);
2138
2139 /* replace the value in the dictionary */
2140 buf = malloc(strlen(*pat) + 1);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002141 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002142 strcpy(buf, *pat);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002143 lydict_remove(ctx->ctx, *pat);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002144
2145 assert(buf[0] == 0x06);
2146 buf[0] = 0x15;
Radek Krejci44ceedc2018-10-02 15:54:31 +02002147 *pat = lydict_insert_zc(ctx->ctx, buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002148
Radek Krejci6d6556c2018-11-08 09:37:45 +01002149 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002150 switch (kw) {
2151 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002152 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MODIFIER, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002153 break;
2154 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002155 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002156 return LY_EVALID;
2157 }
2158 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002159 return ret;
2160}
2161
Michal Vaskoea5abea2018-09-18 13:10:54 +02002162/**
2163 * @brief Parse the pattern statement. Substatement of type statement.
2164 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002165 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002166 * @param[in,out] data Data to read from, always moved to currently handled character.
2167 * @param[in,out] patterns Restrictions to add to.
2168 *
2169 * @return LY_ERR values.
2170 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002171static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002172parse_type_pattern(struct ly_parser_ctx *ctx, const char **data, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002173{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002174 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002175 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002176 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002177 enum yang_keyword kw;
2178 struct lysp_restr *restr;
2179
Radek Krejci2c4e7172018-10-19 15:56:26 +02002180 LY_ARRAY_NEW_RET(ctx->ctx, *patterns, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002181
2182 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002183 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002184
2185 /* add special meaning first byte */
2186 if (buf) {
2187 buf = realloc(buf, word_len + 2);
2188 word = buf;
2189 } else {
2190 buf = malloc(word_len + 2);
2191 }
Radek Krejci44ceedc2018-10-02 15:54:31 +02002192 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejci86d106e2018-10-18 09:53:19 +02002193 memmove(buf + 1, word, word_len);
2194 buf[0] = 0x06; /* pattern's default regular-match flag */
2195 buf[word_len + 1] = '\0'; /* terminating NULL byte */
2196 restr->arg = lydict_insert_zc(ctx->ctx, buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002197
Radek Krejci6d6556c2018-11-08 09:37:45 +01002198 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002199 switch (kw) {
2200 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002201 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 +02002202 break;
2203 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002204 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 +02002205 break;
2206 case YANG_ERROR_APP_TAG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002207 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 +02002208 break;
2209 case YANG_ERROR_MESSAGE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002210 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 +02002211 break;
2212 case YANG_MODIFIER:
Radek Krejciceaf2122019-01-02 15:03:26 +01002213 YANG_CHECK_STMTVER2_RET(ctx, "modifier", "pattern");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002214 LY_CHECK_RET(parse_type_pattern_modifier(ctx, data, &restr->arg, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002215 break;
2216 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002217 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002218 break;
2219 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002220 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002221 return LY_EVALID;
2222 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002223 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002224 return ret;
2225}
2226
Michal Vaskoea5abea2018-09-18 13:10:54 +02002227/**
2228 * @brief Parse the type statement.
2229 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002230 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002231 * @param[in,out] data Data to read from, always moved to currently handled character.
2232 * @param[in,out] type Type to wrote to.
2233 *
2234 * @return LY_ERR values.
2235 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002236static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002237parse_type(struct ly_parser_ctx *ctx, const char **data, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002238{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002239 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002240 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002241 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002242 enum yang_keyword kw;
2243 struct lysp_type *nest_type;
2244
2245 if (type->name) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002246 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002247 return LY_EVALID;
2248 }
2249
2250 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002251 LY_CHECK_RET(get_argument(ctx, data, Y_PREF_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002252 INSERT_WORD(ctx, buf, type->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002253
Radek Krejci6d6556c2018-11-08 09:37:45 +01002254 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002255 switch (kw) {
2256 case YANG_BASE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002257 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 +01002258 type->flags |= LYS_SET_BASE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002259 break;
2260 case YANG_BIT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002261 LY_CHECK_RET(parse_type_enum(ctx, data, kw, &type->bits));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002262 type->flags |= LYS_SET_BIT;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002263 break;
2264 case YANG_ENUM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002265 LY_CHECK_RET(parse_type_enum(ctx, data, kw, &type->enums));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002266 type->flags |= LYS_SET_ENUM;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002267 break;
2268 case YANG_FRACTION_DIGITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002269 LY_CHECK_RET(parse_type_fracdigits(ctx, data, &type->fraction_digits, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002270 type->flags |= LYS_SET_FRDIGITS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002271 break;
2272 case YANG_LENGTH:
2273 if (type->length) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002274 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002275 return LY_EVALID;
2276 }
2277 type->length = calloc(1, sizeof *type->length);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002278 LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002279
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002280 LY_CHECK_RET(parse_restr(ctx, data, kw, type->length));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002281 type->flags |= LYS_SET_LENGTH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002282 break;
2283 case YANG_PATH:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002284 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 +01002285 type->flags |= LYS_SET_PATH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002286 break;
2287 case YANG_PATTERN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002288 LY_CHECK_RET(parse_type_pattern(ctx, data, &type->patterns));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002289 type->flags |= LYS_SET_PATTERN;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002290 break;
2291 case YANG_RANGE:
2292 if (type->range) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002293 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002294 return LY_EVALID;
2295 }
2296 type->range = calloc(1, sizeof *type->range);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002297 LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx->ctx), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002298
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002299 LY_CHECK_RET(parse_restr(ctx, data, kw, type->range));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002300 type->flags |= LYS_SET_RANGE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002301 break;
2302 case YANG_REQUIRE_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002303 LY_CHECK_RET(parse_type_reqinstance(ctx, data, &type->require_instance, &type->flags, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002304 /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002305 break;
2306 case YANG_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002307 LY_ARRAY_NEW_RET(ctx->ctx, type->types, nest_type, LY_EMEM);
2308 LY_CHECK_RET(parse_type(ctx, data, nest_type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002309 type->flags |= LYS_SET_TYPE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002310 break;
2311 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002312 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &type->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002313 break;
2314 default:
Radek Krejci8b764662018-11-14 14:15:13 +01002315 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002316 return LY_EVALID;
2317 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002318 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002319 return ret;
2320}
2321
Michal Vaskoea5abea2018-09-18 13:10:54 +02002322/**
2323 * @brief Parse the leaf statement.
2324 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002325 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002326 * @param[in,out] data Data to read from, always moved to currently handled character.
2327 * @param[in,out] siblings Siblings to add to.
2328 *
2329 * @return LY_ERR values.
2330 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002331static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002332parse_leaf(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002333{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002334 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002335 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002336 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002337 enum yang_keyword kw;
2338 struct lysp_node *iter;
2339 struct lysp_node_leaf *leaf;
2340
2341 /* create structure */
2342 leaf = calloc(1, sizeof *leaf);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002343 LY_CHECK_ERR_RET(!leaf, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002344 leaf->nodetype = LYS_LEAF;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002345 leaf->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002346
2347 /* insert into siblings */
2348 if (!*siblings) {
2349 *siblings = (struct lysp_node *)leaf;
2350 } else {
2351 for (iter = *siblings; iter->next; iter = iter->next);
2352 iter->next = (struct lysp_node *)leaf;
2353 }
2354
2355 /* get name */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002356 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002357 INSERT_WORD(ctx, buf, leaf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002358
2359 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01002360 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002361 switch (kw) {
2362 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002363 LY_CHECK_RET(parse_config(ctx, data, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002364 break;
2365 case YANG_DEFAULT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002366 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 +02002367 break;
2368 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002369 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 +02002370 break;
2371 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002372 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 +02002373 break;
2374 case YANG_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002375 LY_CHECK_RET(parse_mandatory(ctx, data, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002376 break;
2377 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002378 LY_CHECK_RET(parse_restrs(ctx, data, kw, &leaf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002379 break;
2380 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002381 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 +02002382 break;
2383 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002384 LY_CHECK_RET(parse_status(ctx, data, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002385 break;
2386 case YANG_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002387 LY_CHECK_RET(parse_type(ctx, data, &leaf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002388 break;
2389 case YANG_UNITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002390 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 +02002391 break;
2392 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002393 LY_CHECK_RET(parse_when(ctx, data, &leaf->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002394 break;
2395 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002396 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002397 break;
2398 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002399 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002400 return LY_EVALID;
2401 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002402 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002403 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002404checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002405 /* mandatory substatements */
2406 if (!leaf->type.name) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002407 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002408 return LY_EVALID;
2409 }
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002410 if ((leaf->flags & LYS_MAND_TRUE) && (leaf->dflt)) {
2411 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMSCOMB, "mandatory", "default", "leaf");
2412 return LY_EVALID;
2413 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002414
2415 return ret;
2416}
2417
Michal Vaskoea5abea2018-09-18 13:10:54 +02002418/**
2419 * @brief Parse the max-elements statement.
2420 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002421 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002422 * @param[in,out] data Data to read from, always moved to currently handled character.
2423 * @param[in,out] max Value to write to.
2424 * @param[in,out] flags Flags to write to.
2425 * @param[in,out] exts Extension instances to add to.
2426 *
2427 * @return LY_ERR values.
2428 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002429static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002430parse_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 +02002431{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002432 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002433 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002434 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002435 unsigned long int num;
2436 enum yang_keyword kw;
2437
2438 if (*flags & LYS_SET_MAX) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002439 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002440 return LY_EVALID;
2441 }
2442 *flags |= LYS_SET_MAX;
2443
2444 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002445 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002446
2447 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002448 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002449 free(buf);
2450 return LY_EVALID;
2451 }
2452
2453 if (strncmp(word, "unbounded", word_len)) {
2454 errno = 0;
2455 num = strtoul(word, &ptr, 10);
2456 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002457 if ((size_t)(ptr - word) != word_len) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002458 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002459 free(buf);
2460 return LY_EVALID;
2461 }
2462 if ((errno == ERANGE) || (num > UINT32_MAX)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002463 LOGVAL_YANG(ctx, LY_VCODE_OOB, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002464 free(buf);
2465 return LY_EVALID;
2466 }
2467
2468 *max = num;
2469 }
2470 free(buf);
2471
Radek Krejci6d6556c2018-11-08 09:37:45 +01002472 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002473 switch (kw) {
2474 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002475 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MAX, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002476 break;
2477 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002478 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002479 return LY_EVALID;
2480 }
2481 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002482 return ret;
2483}
2484
Michal Vaskoea5abea2018-09-18 13:10:54 +02002485/**
2486 * @brief Parse the min-elements statement.
2487 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002488 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002489 * @param[in,out] data Data to read from, always moved to currently handled character.
2490 * @param[in,out] min Value to write to.
2491 * @param[in,out] flags Flags to write to.
2492 * @param[in,out] exts Extension instances to add to.
2493 *
2494 * @return LY_ERR values.
2495 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002496static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002497parse_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 +02002498{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002499 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002500 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002501 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002502 unsigned long int num;
2503 enum yang_keyword kw;
2504
2505 if (*flags & LYS_SET_MIN) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002506 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002507 return LY_EVALID;
2508 }
2509 *flags |= LYS_SET_MIN;
2510
2511 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002512 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002513
2514 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002515 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002516 free(buf);
2517 return LY_EVALID;
2518 }
2519
2520 errno = 0;
2521 num = strtoul(word, &ptr, 10);
2522 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002523 if ((size_t)(ptr - word) != word_len) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002524 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002525 free(buf);
2526 return LY_EVALID;
2527 }
2528 if ((errno == ERANGE) || (num > UINT32_MAX)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002529 LOGVAL_YANG(ctx, LY_VCODE_OOB, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002530 free(buf);
2531 return LY_EVALID;
2532 }
2533 *min = num;
2534 free(buf);
2535
Radek Krejci6d6556c2018-11-08 09:37:45 +01002536 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002537 switch (kw) {
2538 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002539 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MIN, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002540 break;
2541 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002542 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002543 return LY_EVALID;
2544 }
2545 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002546 return ret;
2547}
2548
Michal Vaskoea5abea2018-09-18 13:10:54 +02002549/**
2550 * @brief Parse the ordered-by statement.
2551 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002552 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002553 * @param[in,out] data Data to read from, always moved to currently handled character.
2554 * @param[in,out] flags Flags to write to.
2555 * @param[in,out] exts Extension instances to add to.
2556 *
2557 * @return LY_ERR values.
2558 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002559static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002560parse_orderedby(struct ly_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002561{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002562 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002563 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002564 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002565 enum yang_keyword kw;
2566
2567 if (*flags & LYS_ORDBY_MASK) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002568 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002569 return LY_EVALID;
2570 }
2571
2572 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002573 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002574
2575 if ((word_len == 6) && !strncmp(word, "system", word_len)) {
2576 *flags |= LYS_ORDBY_SYSTEM;
2577 } else if ((word_len == 4) && !strncmp(word, "user", word_len)) {
2578 *flags |= LYS_ORDBY_USER;
2579 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002580 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002581 free(buf);
2582 return LY_EVALID;
2583 }
2584 free(buf);
2585
Radek Krejci6d6556c2018-11-08 09:37:45 +01002586 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002587 switch (kw) {
2588 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002589 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_ORDEREDBY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002590 break;
2591 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002592 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002593 return LY_EVALID;
2594 }
2595 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002596 return ret;
2597}
2598
Michal Vaskoea5abea2018-09-18 13:10:54 +02002599/**
2600 * @brief Parse the leaf-list statement.
2601 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002602 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002603 * @param[in,out] data Data to read from, always moved to currently handled character.
2604 * @param[in,out] siblings Siblings to add to.
2605 *
2606 * @return LY_ERR values.
2607 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002608static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002609parse_leaflist(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002610{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002611 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002612 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002613 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002614 enum yang_keyword kw;
2615 struct lysp_node *iter;
2616 struct lysp_node_leaflist *llist;
2617
2618 /* create structure */
2619 llist = calloc(1, sizeof *llist);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002620 LY_CHECK_ERR_RET(!llist, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002621 llist->nodetype = LYS_LEAFLIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002622 llist->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002623
2624 /* insert into siblings */
2625 if (!*siblings) {
2626 *siblings = (struct lysp_node *)llist;
2627 } else {
2628 for (iter = *siblings; iter->next; iter = iter->next);
2629 iter->next = (struct lysp_node *)llist;
2630 }
2631
2632 /* get name */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002633 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002634 INSERT_WORD(ctx, buf, llist->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002635
2636 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01002637 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002638 switch (kw) {
2639 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002640 LY_CHECK_RET(parse_config(ctx, data, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002641 break;
2642 case YANG_DEFAULT:
Radek Krejciceaf2122019-01-02 15:03:26 +01002643 YANG_CHECK_STMTVER2_RET(ctx, "default", "leaf-list");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002644 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 +02002645 break;
2646 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002647 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 +02002648 break;
2649 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002650 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 +02002651 break;
2652 case YANG_MAX_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002653 LY_CHECK_RET(parse_maxelements(ctx, data, &llist->max, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002654 break;
2655 case YANG_MIN_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002656 LY_CHECK_RET(parse_minelements(ctx, data, &llist->min, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002657 break;
2658 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002659 LY_CHECK_RET(parse_restrs(ctx, data, kw, &llist->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002660 break;
2661 case YANG_ORDERED_BY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002662 LY_CHECK_RET(parse_orderedby(ctx, data, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002663 break;
2664 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002665 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 +02002666 break;
2667 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002668 LY_CHECK_RET(parse_status(ctx, data, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002669 break;
2670 case YANG_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002671 LY_CHECK_RET(parse_type(ctx, data, &llist->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002672 break;
2673 case YANG_UNITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002674 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 +02002675 break;
2676 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002677 LY_CHECK_RET(parse_when(ctx, data, &llist->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002678 break;
2679 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002680 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002681 break;
2682 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002683 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002684 return LY_EVALID;
2685 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002686 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002687 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002688checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002689 /* mandatory substatements */
2690 if (!llist->type.name) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002691 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002692 return LY_EVALID;
2693 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01002694 if ((llist->min) && (llist->dflts)) {
2695 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMSCOMB, "min-elements", "default", "leaf-list");
2696 return LY_EVALID;
2697 }
Radek Krejcidf6cad12018-11-28 17:10:55 +01002698 if (llist->max && llist->min > llist->max) {
2699 LOGVAL_YANG(ctx, LYVE_SEMANTICS,
2700 "Invalid combination of min-elements and max-elements: min value %u is bigger than the max value %u.",
2701 llist->min, llist->max);
2702 return LY_EVALID;
2703 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002704
2705 return ret;
2706}
2707
Michal Vaskoea5abea2018-09-18 13:10:54 +02002708/**
2709 * @brief Parse the refine statement.
2710 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002711 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002712 * @param[in,out] data Data to read from, always moved to currently handled character.
2713 * @param[in,out] refines Refines to add to.
2714 *
2715 * @return LY_ERR values.
2716 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002717static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002718parse_refine(struct ly_parser_ctx *ctx, const char **data, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002719{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002720 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002721 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002722 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002723 enum yang_keyword kw;
2724 struct lysp_refine *rf;
2725
Radek Krejci2c4e7172018-10-19 15:56:26 +02002726 LY_ARRAY_NEW_RET(ctx->ctx, *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002727
2728 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002729 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002730 INSERT_WORD(ctx, buf, rf->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002731
Radek Krejci6d6556c2018-11-08 09:37:45 +01002732 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002733 switch (kw) {
2734 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002735 LY_CHECK_RET(parse_config(ctx, data, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002736 break;
2737 case YANG_DEFAULT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002738 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 +02002739 break;
2740 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002741 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 +02002742 break;
2743 case YANG_IF_FEATURE:
Radek Krejciceaf2122019-01-02 15:03:26 +01002744 YANG_CHECK_STMTVER2_RET(ctx, "if-feature", "refine");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002745 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 +02002746 break;
2747 case YANG_MAX_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002748 LY_CHECK_RET(parse_maxelements(ctx, data, &rf->max, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002749 break;
2750 case YANG_MIN_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002751 LY_CHECK_RET(parse_minelements(ctx, data, &rf->min, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002752 break;
2753 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002754 LY_CHECK_RET(parse_restrs(ctx, data, kw, &rf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002755 break;
2756 case YANG_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002757 LY_CHECK_RET(parse_mandatory(ctx, data, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002758 break;
2759 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002760 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 +02002761 break;
2762 case YANG_PRESENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002763 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 +02002764 break;
2765 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002766 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002767 break;
2768 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002769 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002770 return LY_EVALID;
2771 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002772 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002773 return ret;
2774}
2775
Michal Vaskoea5abea2018-09-18 13:10:54 +02002776/**
2777 * @brief Parse the typedef statement.
2778 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002779 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002780 * @param[in,out] data Data to read from, always moved to currently handled character.
2781 * @param[in,out] typedefs Typedefs to add to.
2782 *
2783 * @return LY_ERR values.
2784 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002785static LY_ERR
Radek Krejcibbe09a92018-11-08 09:36:54 +01002786parse_typedef(struct ly_parser_ctx *ctx, struct lysp_node *parent, const char **data, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002787{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002788 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002789 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002790 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002791 enum yang_keyword kw;
2792 struct lysp_tpdf *tpdf;
2793
Radek Krejci2c4e7172018-10-19 15:56:26 +02002794 LY_ARRAY_NEW_RET(ctx->ctx, *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002795
2796 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002797 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002798 INSERT_WORD(ctx, buf, tpdf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002799
2800 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01002801 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002802 switch (kw) {
2803 case YANG_DEFAULT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002804 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 +02002805 break;
2806 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002807 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 +02002808 break;
2809 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002810 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 +02002811 break;
2812 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002813 LY_CHECK_RET(parse_status(ctx, data, &tpdf->flags, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002814 break;
2815 case YANG_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002816 LY_CHECK_RET(parse_type(ctx, data, &tpdf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002817 break;
2818 case YANG_UNITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002819 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 +02002820 break;
2821 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002822 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002823 break;
2824 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002825 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002826 return LY_EVALID;
2827 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002828 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002829 LY_CHECK_RET(ret);
Radek Krejcibbe09a92018-11-08 09:36:54 +01002830checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002831 /* mandatory substatements */
2832 if (!tpdf->type.name) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002833 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002834 return LY_EVALID;
2835 }
2836
Radek Krejcibbe09a92018-11-08 09:36:54 +01002837 /* store data for collision check */
2838 if (parent) {
2839 ly_set_add(&ctx->tpdfs_nodes, parent, 0);
2840 }
2841
Michal Vasko7fbc8162018-09-17 10:35:16 +02002842 return ret;
2843}
2844
Michal Vaskoea5abea2018-09-18 13:10:54 +02002845/**
2846 * @brief Parse the input or output statement.
2847 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002848 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002849 * @param[in,out] data Data to read from, always moved to currently handled character.
2850 * @param[in] kw Type of this particular keyword
2851 * @param[in,out] inout_p Input/output pointer to write to.
2852 *
2853 * @return LY_ERR values.
2854 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002855static LY_ERR
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002856parse_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 +02002857{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002858 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002859 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002860 size_t word_len;
Radek Krejci10113652018-11-14 16:56:50 +01002861 enum yang_keyword kw;
Radek Krejcie86bf772018-12-14 11:39:53 +01002862 unsigned int u;
2863 struct lysp_node *child;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002864
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002865 if (inout_p->nodetype) {
Radek Krejci10113652018-11-14 16:56:50 +01002866 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002867 return LY_EVALID;
2868 }
2869
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002870 /* initiate structure */
2871 inout_p->nodetype = LYS_INOUT;
2872 inout_p->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002873
2874 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01002875 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002876 switch (kw) {
2877 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01002878 YANG_CHECK_STMTVER2_RET(ctx, "anydata", ly_stmt2str(inout_kw));
Radek Krejci10113652018-11-14 16:56:50 +01002879 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002880 case YANG_ANYXML:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002881 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002882 break;
2883 case YANG_CHOICE:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002884 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002885 break;
2886 case YANG_CONTAINER:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002887 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002888 break;
2889 case YANG_LEAF:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002890 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002891 break;
2892 case YANG_LEAF_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002893 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002894 break;
2895 case YANG_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002896 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002897 break;
2898 case YANG_USES:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002899 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002900 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002901 case YANG_TYPEDEF:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002902 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)inout_p, data, &inout_p->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002903 break;
2904 case YANG_MUST:
Radek Krejciceaf2122019-01-02 15:03:26 +01002905 YANG_CHECK_STMTVER2_RET(ctx, "must", ly_stmt2str(inout_kw));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002906 LY_CHECK_RET(parse_restrs(ctx, data, kw, &inout_p->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002907 break;
2908 case YANG_GROUPING:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002909 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)inout_p, &inout_p->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002910 break;
2911 case YANG_CUSTOM:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002912 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inout_p->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002913 break;
2914 default:
Radek Krejci10113652018-11-14 16:56:50 +01002915 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002916 return LY_EVALID;
2917 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002918 }
Radek Krejcie86bf772018-12-14 11:39:53 +01002919 /* finalize parent pointers to the reallocated items */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002920 LY_ARRAY_FOR(inout_p->groupings, u) {
2921 LY_LIST_FOR(inout_p->groupings[u].data, child) {
2922 child->parent = (struct lysp_node*)&inout_p->groupings[u];
Radek Krejcie86bf772018-12-14 11:39:53 +01002923 }
2924 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002925 return ret;
2926}
2927
Michal Vaskoea5abea2018-09-18 13:10:54 +02002928/**
2929 * @brief Parse the action statement.
2930 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002931 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002932 * @param[in,out] data Data to read from, always moved to currently handled character.
2933 * @param[in,out] actions Actions to add to.
2934 *
2935 * @return LY_ERR values.
2936 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002937static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002938parse_action(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002939{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002940 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002941 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002942 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002943 enum yang_keyword kw;
2944 struct lysp_action *act;
Radek Krejcie86bf772018-12-14 11:39:53 +01002945 struct lysp_node *child;
2946 unsigned int u;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002947
Radek Krejci2c4e7172018-10-19 15:56:26 +02002948 LY_ARRAY_NEW_RET(ctx->ctx, *actions, act, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002949
2950 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002951 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002952 INSERT_WORD(ctx, buf, act->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002953 act->nodetype = LYS_ACTION;
2954 act->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002955
Radek Krejci6d6556c2018-11-08 09:37:45 +01002956 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002957 switch (kw) {
2958 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002959 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 +02002960 break;
2961 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002962 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 +02002963 break;
2964 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002965 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 +02002966 break;
2967 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002968 LY_CHECK_RET(parse_status(ctx, data, &act->flags, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002969 break;
2970
2971 case YANG_INPUT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002972 LY_CHECK_RET(parse_inout(ctx, data, kw, (struct lysp_node*)act, &act->input));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002973 break;
2974 case YANG_OUTPUT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002975 LY_CHECK_RET(parse_inout(ctx, data, kw, (struct lysp_node*)act, &act->output));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002976 break;
2977
2978 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01002979 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)act, data, &act->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002980 break;
2981 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002982 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)act, &act->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002983 break;
2984 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002985 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002986 break;
2987 default:
Radek Krejcif538ce52019-03-05 10:46:14 +01002988 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), parent ? "action" : "rpc");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002989 return LY_EVALID;
2990 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002991 }
Radek Krejcie86bf772018-12-14 11:39:53 +01002992 /* finalize parent pointers to the reallocated items */
2993 LY_ARRAY_FOR(act->groupings, u) {
2994 LY_LIST_FOR(act->groupings[u].data, child) {
2995 child->parent = (struct lysp_node*)&act->groupings[u];
2996 }
2997 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002998 return ret;
2999}
3000
Michal Vaskoea5abea2018-09-18 13:10:54 +02003001/**
3002 * @brief Parse the notification statement.
3003 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003004 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003005 * @param[in,out] data Data to read from, always moved to currently handled character.
3006 * @param[in,out] notifs Notifications to add to.
3007 *
3008 * @return LY_ERR values.
3009 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003010static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003011parse_notif(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003012{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003013 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003014 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003015 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003016 enum yang_keyword kw;
3017 struct lysp_notif *notif;
Radek Krejcie86bf772018-12-14 11:39:53 +01003018 struct lysp_node *child;
3019 unsigned int u;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003020
Radek Krejci2c4e7172018-10-19 15:56:26 +02003021 LY_ARRAY_NEW_RET(ctx->ctx, *notifs, notif, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003022
3023 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003024 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003025 INSERT_WORD(ctx, buf, notif->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003026 notif->nodetype = LYS_NOTIF;
3027 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003028
Radek Krejci6d6556c2018-11-08 09:37:45 +01003029 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003030 switch (kw) {
3031 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003032 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 +02003033 break;
3034 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003035 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 +02003036 break;
3037 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003038 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 +02003039 break;
3040 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003041 LY_CHECK_RET(parse_status(ctx, data, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003042 break;
3043
3044 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01003045 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "notification");
Radek Krejci10113652018-11-14 16:56:50 +01003046 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003047 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003048 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003049 break;
3050 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003051 LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003052 break;
3053 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003054 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003055 break;
3056 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003057 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003058 break;
3059 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003060 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003061 break;
3062 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003063 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003064 break;
3065 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003066 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003067 break;
3068
3069 case YANG_MUST:
Radek Krejciceaf2122019-01-02 15:03:26 +01003070 YANG_CHECK_STMTVER2_RET(ctx, "must", "notification");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003071 LY_CHECK_RET(parse_restrs(ctx, data, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003072 break;
3073 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01003074 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)notif, data, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003075 break;
3076 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003077 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003078 break;
3079 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003080 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003081 break;
3082 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003083 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003084 return LY_EVALID;
3085 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003086 }
Radek Krejcie86bf772018-12-14 11:39:53 +01003087 /* finalize parent pointers to the reallocated items */
3088 LY_ARRAY_FOR(notif->groupings, u) {
3089 LY_LIST_FOR(notif->groupings[u].data, child) {
3090 child->parent = (struct lysp_node*)&notif->groupings[u];
3091 }
3092 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003093 return ret;
3094}
3095
Michal Vaskoea5abea2018-09-18 13:10:54 +02003096/**
3097 * @brief Parse the grouping statement.
3098 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003099 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003100 * @param[in,out] data Data to read from, always moved to currently handled character.
3101 * @param[in,out] groupings Groupings to add to.
3102 *
3103 * @return LY_ERR values.
3104 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003105static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003106parse_grouping(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003107{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003108 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003109 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003110 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003111 enum yang_keyword kw;
3112 struct lysp_grp *grp;
Radek Krejcie86bf772018-12-14 11:39:53 +01003113 struct lysp_node *child;
3114 unsigned int u;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003115
Radek Krejci2c4e7172018-10-19 15:56:26 +02003116 LY_ARRAY_NEW_RET(ctx->ctx, *groupings, grp, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003117
3118 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003119 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003120 INSERT_WORD(ctx, buf, grp->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003121 grp->nodetype = LYS_GROUPING;
3122 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003123
Radek Krejci6d6556c2018-11-08 09:37:45 +01003124 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003125 switch (kw) {
3126 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003127 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 +02003128 break;
3129 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003130 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 +02003131 break;
3132 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003133 LY_CHECK_RET(parse_status(ctx, data, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003134 break;
3135
3136 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01003137 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "grouping");
Radek Krejci10113652018-11-14 16:56:50 +01003138 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003139 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003140 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003141 break;
3142 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003143 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003144 break;
3145 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003146 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003147 break;
3148 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003149 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003150 break;
3151 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003152 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003153 break;
3154 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003155 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003156 break;
3157 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003158 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003159 break;
3160
3161 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01003162 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)grp, data, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003163 break;
3164 case YANG_ACTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003165 YANG_CHECK_STMTVER2_RET(ctx, "action", "grouping");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003166 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)grp, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003167 break;
3168 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003169 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)grp, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003170 break;
3171 case YANG_NOTIFICATION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003172 YANG_CHECK_STMTVER2_RET(ctx, "notification", "grouping");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003173 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)grp, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003174 break;
3175 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003176 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003177 break;
3178 default:
Radek Krejci10113652018-11-14 16:56:50 +01003179 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003180 return LY_EVALID;
3181 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003182 }
Radek Krejcie86bf772018-12-14 11:39:53 +01003183 /* finalize parent pointers to the reallocated items */
3184 LY_ARRAY_FOR(grp->groupings, u) {
3185 LY_LIST_FOR(grp->groupings[u].data, child) {
3186 child->parent = (struct lysp_node*)&grp->groupings[u];
3187 }
3188 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003189 return ret;
3190}
3191
Michal Vaskoea5abea2018-09-18 13:10:54 +02003192/**
3193 * @brief Parse the refine statement.
3194 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003195 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003196 * @param[in,out] data Data to read from, always moved to currently handled character.
3197 * @param[in,out] augments Augments to add to.
3198 *
3199 * @return LY_ERR values.
3200 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003201static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003202parse_augment(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003203{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003204 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003205 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003206 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003207 enum yang_keyword kw;
3208 struct lysp_augment *aug;
3209
Radek Krejci2c4e7172018-10-19 15:56:26 +02003210 LY_ARRAY_NEW_RET(ctx->ctx, *augments, aug, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003211
3212 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003213 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003214 INSERT_WORD(ctx, buf, aug->nodeid, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003215 aug->nodetype = LYS_AUGMENT;
3216 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003217
Radek Krejci6d6556c2018-11-08 09:37:45 +01003218 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003219 switch (kw) {
3220 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003221 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 +02003222 break;
3223 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003224 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 +02003225 break;
3226 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003227 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 +02003228 break;
3229 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003230 LY_CHECK_RET(parse_status(ctx, data, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003231 break;
3232 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003233 LY_CHECK_RET(parse_when(ctx, data, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003234 break;
3235
3236 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01003237 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "augment");
Radek Krejci10113652018-11-14 16:56:50 +01003238 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003239 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003240 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003241 break;
3242 case YANG_CASE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003243 LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003244 break;
3245 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003246 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003247 break;
3248 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003249 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003250 break;
3251 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003252 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003253 break;
3254 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003255 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003256 break;
3257 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003258 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003259 break;
3260 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003261 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003262 break;
3263
3264 case YANG_ACTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003265 YANG_CHECK_STMTVER2_RET(ctx, "action", "augment");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003266 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003267 break;
3268 case YANG_NOTIFICATION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003269 YANG_CHECK_STMTVER2_RET(ctx, "notification", "augment");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003270 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003271 break;
3272 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003273 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003274 break;
3275 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003276 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003277 return LY_EVALID;
3278 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003279 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003280 return ret;
3281}
3282
Michal Vaskoea5abea2018-09-18 13:10:54 +02003283/**
3284 * @brief Parse the uses statement.
3285 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003286 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003287 * @param[in,out] data Data to read from, always moved to currently handled character.
3288 * @param[in,out] siblings Siblings to add to.
3289 *
3290 * @return LY_ERR values.
3291 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003292static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003293parse_uses(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003294{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003295 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003296 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003297 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003298 enum yang_keyword kw;
3299 struct lysp_node *iter;
3300 struct lysp_node_uses *uses;
3301
3302 /* create structure */
3303 uses = calloc(1, sizeof *uses);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003304 LY_CHECK_ERR_RET(!uses, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003305 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003306 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003307
3308 /* insert into siblings */
3309 if (!*siblings) {
3310 *siblings = (struct lysp_node *)uses;
3311 } else {
3312 for (iter = *siblings; iter->next; iter = iter->next);
3313 iter->next = (struct lysp_node *)uses;
3314 }
3315
3316 /* get name */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003317 LY_CHECK_RET(get_argument(ctx, data, Y_PREF_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003318 INSERT_WORD(ctx, buf, uses->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003319
3320 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01003321 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003322 switch (kw) {
3323 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003324 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 +02003325 break;
3326 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003327 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 +02003328 break;
3329 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003330 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 +02003331 break;
3332 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003333 LY_CHECK_RET(parse_status(ctx, data, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003334 break;
3335 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003336 LY_CHECK_RET(parse_when(ctx, data, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003337 break;
3338
3339 case YANG_REFINE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003340 LY_CHECK_RET(parse_refine(ctx, data, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003341 break;
3342 case YANG_AUGMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003343 LY_CHECK_RET(parse_augment(ctx, data, (struct lysp_node*)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003344 break;
3345 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003346 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003347 break;
3348 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003349 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003350 return LY_EVALID;
3351 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003352 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003353 return ret;
3354}
3355
Michal Vaskoea5abea2018-09-18 13:10:54 +02003356/**
3357 * @brief Parse the case statement.
3358 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003359 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003360 * @param[in,out] data Data to read from, always moved to currently handled character.
3361 * @param[in,out] siblings Siblings to add to.
3362 *
3363 * @return LY_ERR values.
3364 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003365static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003366parse_case(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003367{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003368 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003369 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003370 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003371 enum yang_keyword kw;
3372 struct lysp_node *iter;
3373 struct lysp_node_case *cas;
3374
3375 /* create structure */
3376 cas = calloc(1, sizeof *cas);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003377 LY_CHECK_ERR_RET(!cas, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003378 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003379 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003380
3381 /* insert into siblings */
3382 if (!*siblings) {
3383 *siblings = (struct lysp_node *)cas;
3384 } else {
3385 for (iter = *siblings; iter->next; iter = iter->next);
3386 iter->next = (struct lysp_node *)cas;
3387 }
3388
3389 /* get name */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003390 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003391 INSERT_WORD(ctx, buf, cas->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003392
3393 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01003394 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003395 switch (kw) {
3396 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003397 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 +02003398 break;
3399 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003400 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 +02003401 break;
3402 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003403 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 +02003404 break;
3405 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003406 LY_CHECK_RET(parse_status(ctx, data, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003407 break;
3408 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003409 LY_CHECK_RET(parse_when(ctx, data, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003410 break;
3411
3412 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01003413 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "case");
Radek Krejci10113652018-11-14 16:56:50 +01003414 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003415 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003416 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003417 break;
3418 case YANG_CHOICE:
Radek Krejcia9026eb2018-12-12 16:04:47 +01003419 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003420 break;
3421 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003422 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003423 break;
3424 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003425 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003426 break;
3427 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003428 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003429 break;
3430 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003431 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003432 break;
3433 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003434 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003435 break;
3436 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003437 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003438 break;
3439 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003440 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003441 return LY_EVALID;
3442 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003443 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003444 return ret;
3445}
3446
Michal Vaskoea5abea2018-09-18 13:10:54 +02003447/**
3448 * @brief Parse the choice statement.
3449 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003450 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003451 * @param[in,out] data Data to read from, always moved to currently handled character.
3452 * @param[in,out] siblings Siblings to add to.
3453 *
3454 * @return LY_ERR values.
3455 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003456static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003457parse_choice(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003458{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003459 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003460 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003461 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003462 enum yang_keyword kw;
3463 struct lysp_node *iter;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003464 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003465
3466 /* create structure */
Radek Krejci44ceedc2018-10-02 15:54:31 +02003467 choice = calloc(1, sizeof *choice);
3468 LY_CHECK_ERR_RET(!choice, LOGMEM(ctx->ctx), LY_EMEM);
3469 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003470 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003471
3472 /* insert into siblings */
3473 if (!*siblings) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02003474 *siblings = (struct lysp_node *)choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003475 } else {
3476 for (iter = *siblings; iter->next; iter = iter->next);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003477 iter->next = (struct lysp_node *)choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003478 }
3479
3480 /* get name */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003481 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003482 INSERT_WORD(ctx, buf, choice->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003483
3484 /* parse substatements */
Radek Krejcia9026eb2018-12-12 16:04:47 +01003485 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003486 switch (kw) {
3487 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003488 LY_CHECK_RET(parse_config(ctx, data, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003489 break;
3490 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003491 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 +02003492 break;
3493 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003494 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 +02003495 break;
3496 case YANG_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003497 LY_CHECK_RET(parse_mandatory(ctx, data, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003498 break;
3499 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003500 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 +02003501 break;
3502 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003503 LY_CHECK_RET(parse_status(ctx, data, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003504 break;
3505 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003506 LY_CHECK_RET(parse_when(ctx, data, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003507 break;
3508 case YANG_DEFAULT:
Radek Krejcia9026eb2018-12-12 16:04:47 +01003509 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 +02003510 break;
3511
3512 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01003513 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "choice");
Radek Krejci10113652018-11-14 16:56:50 +01003514 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003515 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003516 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003517 break;
3518 case YANG_CASE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003519 LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003520 break;
3521 case YANG_CHOICE:
Radek Krejciceaf2122019-01-02 15:03:26 +01003522 YANG_CHECK_STMTVER2_RET(ctx, "choice", "choice");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003523 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003524 break;
3525 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003526 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003527 break;
3528 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003529 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003530 break;
3531 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003532 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003533 break;
3534 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003535 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003536 break;
3537 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003538 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003539 break;
3540 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003541 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003542 return LY_EVALID;
3543 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003544 }
Radek Krejcia9026eb2018-12-12 16:04:47 +01003545 LY_CHECK_RET(ret);
3546checks:
3547 if ((choice->flags & LYS_MAND_TRUE) && choice->dflt) {
3548 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMSCOMB, "mandatory", "default", "choice");
3549 return LY_EVALID;
3550 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003551 return ret;
3552}
3553
Michal Vaskoea5abea2018-09-18 13:10:54 +02003554/**
3555 * @brief Parse the container statement.
3556 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003557 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003558 * @param[in,out] data Data to read from, always moved to currently handled character.
3559 * @param[in,out] siblings Siblings to add to.
3560 *
3561 * @return LY_ERR values.
3562 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003563static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003564parse_container(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003565{
3566 LY_ERR ret = 0;
3567 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003568 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003569 enum yang_keyword kw;
3570 struct lysp_node *iter;
3571 struct lysp_node_container *cont;
Radek Krejcie86bf772018-12-14 11:39:53 +01003572 unsigned int u;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003573
3574 /* create structure */
3575 cont = calloc(1, sizeof *cont);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003576 LY_CHECK_ERR_RET(!cont, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003577 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003578 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003579
3580 /* insert into siblings */
3581 if (!*siblings) {
3582 *siblings = (struct lysp_node *)cont;
3583 } else {
3584 for (iter = *siblings; iter->next; iter = iter->next);
3585 iter->next = (struct lysp_node *)cont;
3586 }
3587
3588 /* get name */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003589 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003590 INSERT_WORD(ctx, buf, cont->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003591
3592 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01003593 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003594 switch (kw) {
3595 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003596 LY_CHECK_RET(parse_config(ctx, data, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003597 break;
3598 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003599 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 +02003600 break;
3601 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003602 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 +02003603 break;
3604 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003605 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 +02003606 break;
3607 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003608 LY_CHECK_RET(parse_status(ctx, data, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003609 break;
3610 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003611 LY_CHECK_RET(parse_when(ctx, data, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003612 break;
3613 case YANG_PRESENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003614 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 +02003615 break;
3616
3617 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01003618 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "container");
Radek Krejci10113652018-11-14 16:56:50 +01003619 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003620 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003621 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003622 break;
3623 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003624 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003625 break;
3626 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003627 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003628 break;
3629 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003630 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003631 break;
3632 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003633 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003634 break;
3635 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003636 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003637 break;
3638 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003639 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003640 break;
3641
3642 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01003643 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)cont, data, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003644 break;
3645 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003646 LY_CHECK_RET(parse_restrs(ctx, data, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003647 break;
3648 case YANG_ACTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003649 YANG_CHECK_STMTVER2_RET(ctx, "action", "container");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003650 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003651 break;
3652 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003653 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003654 break;
3655 case YANG_NOTIFICATION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003656 YANG_CHECK_STMTVER2_RET(ctx, "notification", "container");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003657 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003658 break;
3659 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003660 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003661 break;
3662 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003663 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003664 return LY_EVALID;
3665 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003666 }
Radek Krejcie86bf772018-12-14 11:39:53 +01003667 /* finalize parent pointers to the reallocated items */
3668 LY_ARRAY_FOR(cont->groupings, u) {
3669 LY_LIST_FOR(cont->groupings[u].data, iter) {
3670 iter->parent = (struct lysp_node*)&cont->groupings[u];
3671 }
3672 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003673 return ret;
3674}
3675
Michal Vaskoea5abea2018-09-18 13:10:54 +02003676/**
3677 * @brief Parse the list statement.
3678 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003679 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003680 * @param[in,out] data Data to read from, always moved to currently handled character.
3681 * @param[in,out] siblings Siblings to add to.
3682 *
3683 * @return LY_ERR values.
3684 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003685static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003686parse_list(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003687{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003688 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003689 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003690 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003691 enum yang_keyword kw;
3692 struct lysp_node *iter;
3693 struct lysp_node_list *list;
Radek Krejcie86bf772018-12-14 11:39:53 +01003694 unsigned int u;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003695
3696 /* create structure */
3697 list = calloc(1, sizeof *list);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003698 LY_CHECK_ERR_RET(!list, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003699 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003700 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003701
3702 /* insert into siblings */
3703 if (!*siblings) {
3704 *siblings = (struct lysp_node *)list;
3705 } else {
3706 for (iter = *siblings; iter->next; iter = iter->next);
3707 iter->next = (struct lysp_node *)list;
3708 }
3709
3710 /* get name */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003711 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003712 INSERT_WORD(ctx, buf, list->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003713
3714 /* parse substatements */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003715 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003716 switch (kw) {
3717 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003718 LY_CHECK_RET(parse_config(ctx, data, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003719 break;
3720 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003721 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 +02003722 break;
3723 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003724 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 +02003725 break;
3726 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003727 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 +02003728 break;
3729 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003730 LY_CHECK_RET(parse_status(ctx, data, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003731 break;
3732 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003733 LY_CHECK_RET(parse_when(ctx, data, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003734 break;
3735 case YANG_KEY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003736 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 +02003737 break;
3738 case YANG_MAX_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003739 LY_CHECK_RET(parse_maxelements(ctx, data, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003740 break;
3741 case YANG_MIN_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003742 LY_CHECK_RET(parse_minelements(ctx, data, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003743 break;
3744 case YANG_ORDERED_BY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003745 LY_CHECK_RET(parse_orderedby(ctx, data, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003746 break;
3747 case YANG_UNIQUE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003748 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 +02003749 break;
3750
3751 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01003752 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "list");
Radek Krejci10113652018-11-14 16:56:50 +01003753 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003754 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003755 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003756 break;
3757 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003758 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003759 break;
3760 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003761 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003762 break;
3763 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003764 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003765 break;
3766 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003767 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003768 break;
3769 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003770 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003771 break;
3772 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003773 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003774 break;
3775
3776 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01003777 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)list, data, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003778 break;
3779 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003780 LY_CHECK_RET(parse_restrs(ctx, data, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003781 break;
3782 case YANG_ACTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003783 YANG_CHECK_STMTVER2_RET(ctx, "action", "list");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003784 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003785 break;
3786 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003787 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003788 break;
3789 case YANG_NOTIFICATION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003790 YANG_CHECK_STMTVER2_RET(ctx, "notification", "list");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003791 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003792 break;
3793 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003794 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003795 break;
3796 default:
Radek Krejci10113652018-11-14 16:56:50 +01003797 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003798 return LY_EVALID;
3799 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003800 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003801 LY_CHECK_RET(ret);
Radek Krejcie86bf772018-12-14 11:39:53 +01003802 /* finalize parent pointers to the reallocated items */
3803 LY_ARRAY_FOR(list->groupings, u) {
3804 LY_LIST_FOR(list->groupings[u].data, iter) {
3805 iter->parent = (struct lysp_node*)&list->groupings[u];
3806 }
3807 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003808checks:
3809 if (list->max && list->min > list->max) {
3810 LOGVAL_YANG(ctx, LYVE_SEMANTICS,
3811 "Invalid combination of min-elements and max-elements: min value %u is bigger than the max value %u.",
3812 list->min, list->max);
3813 return LY_EVALID;
3814 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003815
3816 return ret;
3817}
3818
Michal Vaskoea5abea2018-09-18 13:10:54 +02003819/**
3820 * @brief Parse the yin-element statement.
3821 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003822 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003823 * @param[in,out] data Data to read from, always moved to currently handled character.
3824 * @param[in,out] flags Flags to write to.
3825 * @param[in,out] exts Extension instances to add to.
3826 *
3827 * @return LY_ERR values.
3828 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003829static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003830parse_yinelement(struct ly_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003831{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003832 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003833 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003834 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003835 enum yang_keyword kw;
3836
3837 if (*flags & LYS_YINELEM_MASK) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02003838 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003839 return LY_EVALID;
3840 }
3841
3842 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003843 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003844
3845 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
3846 *flags |= LYS_YINELEM_TRUE;
3847 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
3848 *flags |= LYS_YINELEM_FALSE;
3849 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02003850 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003851 free(buf);
3852 return LY_EVALID;
3853 }
3854 free(buf);
3855
Radek Krejci6d6556c2018-11-08 09:37:45 +01003856 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003857 switch (kw) {
3858 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003859 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_YINELEM, 0, exts));
3860 LY_CHECK_RET(ret); break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003861 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003862 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003863 return LY_EVALID;
3864 }
3865 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003866 return ret;
3867}
3868
Michal Vaskoea5abea2018-09-18 13:10:54 +02003869/**
3870 * @brief Parse the yin-element statement.
3871 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003872 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003873 * @param[in,out] data Data to read from, always moved to currently handled character.
3874 * @param[in,out] argument Value to write to.
3875 * @param[in,out] flags Flags to write to.
3876 * @param[in,out] exts Extension instances to add to.
3877 *
3878 * @return LY_ERR values.
3879 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003880static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003881parse_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 +02003882{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003883 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003884 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003885 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003886 enum yang_keyword kw;
3887
3888 if (*argument) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02003889 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003890 return LY_EVALID;
3891 }
3892
3893 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003894 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003895 INSERT_WORD(ctx, buf, *argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003896
Radek Krejci6d6556c2018-11-08 09:37:45 +01003897 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003898 switch (kw) {
3899 case YANG_YIN_ELEMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003900 LY_CHECK_RET(parse_yinelement(ctx, data, flags, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003901 break;
3902 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003903 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_ARGUMENT, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003904 break;
3905 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003906 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003907 return LY_EVALID;
3908 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003909 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003910 return ret;
3911}
3912
Michal Vaskoea5abea2018-09-18 13:10:54 +02003913/**
3914 * @brief Parse the extension statement.
3915 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003916 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003917 * @param[in,out] data Data to read from, always moved to currently handled character.
3918 * @param[in,out] extensions Extensions to add to.
3919 *
3920 * @return LY_ERR values.
3921 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003922static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003923parse_extension(struct ly_parser_ctx *ctx, const char **data, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003924{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003925 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003926 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003927 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003928 enum yang_keyword kw;
3929 struct lysp_ext *ex;
3930
Radek Krejci2c4e7172018-10-19 15:56:26 +02003931 LY_ARRAY_NEW_RET(ctx->ctx, *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003932
3933 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003934 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003935 INSERT_WORD(ctx, buf, ex->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003936
Radek Krejci6d6556c2018-11-08 09:37:45 +01003937 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003938 switch (kw) {
3939 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003940 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 +02003941 break;
3942 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003943 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 +02003944 break;
3945 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003946 LY_CHECK_RET(parse_status(ctx, data, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003947 break;
3948 case YANG_ARGUMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003949 LY_CHECK_RET(parse_argument(ctx, data, &ex->argument, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003950 break;
3951 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003952 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003953 break;
3954 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003955 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003956 return LY_EVALID;
3957 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003958 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003959 return ret;
3960}
3961
Michal Vaskoea5abea2018-09-18 13:10:54 +02003962/**
3963 * @brief Parse the deviate statement.
3964 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003965 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003966 * @param[in,out] data Data to read from, always moved to currently handled character.
3967 * @param[in,out] deviates Deviates to add to.
3968 *
3969 * @return LY_ERR values.
3970 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003971static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003972parse_deviate(struct ly_parser_ctx *ctx, const char **data, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003973{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003974 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003975 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003976 size_t word_len, dev_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003977 enum yang_keyword kw;
3978 struct lysp_deviate *iter, *d;
3979 struct lysp_deviate_add *d_add = NULL;
3980 struct lysp_deviate_rpl *d_rpl = NULL;
3981 struct lysp_deviate_del *d_del = NULL;
3982 const char **d_units, ***d_uniques, ***d_dflts;
3983 struct lysp_restr **d_musts;
3984 uint16_t *d_flags;
3985 uint32_t *d_min, *d_max;
3986
3987 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003988 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003989
3990 if ((word_len == 13) && !strncmp(word, "not-supported", word_len)) {
3991 dev_mod = LYS_DEV_NOT_SUPPORTED;
3992 } else if ((word_len == 3) && !strncmp(word, "add", word_len)) {
3993 dev_mod = LYS_DEV_ADD;
3994 } else if ((word_len == 7) && !strncmp(word, "replace", word_len)) {
3995 dev_mod = LYS_DEV_REPLACE;
3996 } else if ((word_len == 6) && !strncmp(word, "delete", word_len)) {
3997 dev_mod = LYS_DEV_DELETE;
3998 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02003999 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004000 free(buf);
4001 return LY_EVALID;
4002 }
4003 free(buf);
4004
4005 /* create structure */
4006 switch (dev_mod) {
4007 case LYS_DEV_NOT_SUPPORTED:
4008 d = calloc(1, sizeof *d);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004009 LY_CHECK_ERR_RET(!d, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004010 break;
4011 case LYS_DEV_ADD:
4012 d_add = calloc(1, sizeof *d_add);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004013 LY_CHECK_ERR_RET(!d_add, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004014 d = (struct lysp_deviate *)d_add;
4015 d_units = &d_add->units;
4016 d_uniques = &d_add->uniques;
4017 d_dflts = &d_add->dflts;
4018 d_musts = &d_add->musts;
4019 d_flags = &d_add->flags;
4020 d_min = &d_add->min;
4021 d_max = &d_add->max;
4022 break;
4023 case LYS_DEV_REPLACE:
4024 d_rpl = calloc(1, sizeof *d_rpl);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004025 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004026 d = (struct lysp_deviate *)d_rpl;
4027 d_units = &d_rpl->units;
4028 d_flags = &d_rpl->flags;
4029 d_min = &d_rpl->min;
4030 d_max = &d_rpl->max;
4031 break;
4032 case LYS_DEV_DELETE:
4033 d_del = calloc(1, sizeof *d_del);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004034 LY_CHECK_ERR_RET(!d_del, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004035 d = (struct lysp_deviate *)d_del;
4036 d_units = &d_del->units;
4037 d_uniques = &d_del->uniques;
4038 d_dflts = &d_del->dflts;
4039 d_musts = &d_del->musts;
4040 d_flags = &d_del->flags;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004041 break;
4042 default:
4043 assert(0);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004044 LOGINT_RET(ctx->ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004045 }
4046 d->mod = dev_mod;
4047
4048 /* insert into siblings */
4049 if (!*deviates) {
4050 *deviates = d;
4051 } else {
4052 for (iter = *deviates; iter->next; iter = iter->next);
4053 iter->next = d;
4054 }
4055
Radek Krejci6d6556c2018-11-08 09:37:45 +01004056 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004057 switch (kw) {
4058 case YANG_CONFIG:
4059 switch (dev_mod) {
4060 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004061 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004062 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004063 return LY_EVALID;
4064 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004065 LY_CHECK_RET(parse_config(ctx, data, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004066 break;
4067 }
4068 break;
4069 case YANG_DEFAULT:
4070 switch (dev_mod) {
4071 case LYS_DEV_NOT_SUPPORTED:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004072 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004073 return LY_EVALID;
4074 case LYS_DEV_REPLACE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004075 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 +02004076 break;
4077 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004078 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 +02004079 break;
4080 }
4081 break;
4082 case YANG_MANDATORY:
4083 switch (dev_mod) {
4084 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004085 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004086 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004087 return LY_EVALID;
4088 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004089 LY_CHECK_RET(parse_mandatory(ctx, data, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004090 break;
4091 }
4092 break;
4093 case YANG_MAX_ELEMENTS:
4094 switch (dev_mod) {
4095 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004096 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004097 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004098 return LY_EVALID;
4099 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004100 LY_CHECK_RET(parse_maxelements(ctx, data, d_max, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004101 break;
4102 }
4103 break;
4104 case YANG_MIN_ELEMENTS:
4105 switch (dev_mod) {
4106 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004107 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004108 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004109 return LY_EVALID;
4110 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004111 LY_CHECK_RET(parse_minelements(ctx, data, d_min, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004112 break;
4113 }
4114 break;
4115 case YANG_MUST:
4116 switch (dev_mod) {
4117 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004118 case LYS_DEV_REPLACE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004119 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004120 return LY_EVALID;
4121 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004122 LY_CHECK_RET(parse_restrs(ctx, data, kw, d_musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004123 break;
4124 }
4125 break;
4126 case YANG_TYPE:
4127 switch (dev_mod) {
4128 case LYS_DEV_NOT_SUPPORTED:
4129 case LYS_DEV_ADD:
4130 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004131 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004132 return LY_EVALID;
4133 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004134 if (d_rpl->type) {
4135 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
4136 return LY_EVALID;
4137 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004138 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004139 LY_CHECK_ERR_RET(!d_rpl->type, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004140 LY_CHECK_RET(parse_type(ctx, data, d_rpl->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004141 break;
4142 }
4143 break;
4144 case YANG_UNIQUE:
4145 switch (dev_mod) {
4146 case LYS_DEV_NOT_SUPPORTED:
4147 case LYS_DEV_REPLACE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004148 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004149 return LY_EVALID;
4150 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004151 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 +02004152 break;
4153 }
4154 break;
4155 case YANG_UNITS:
4156 switch (dev_mod) {
4157 case LYS_DEV_NOT_SUPPORTED:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004158 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004159 return LY_EVALID;
4160 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004161 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 +02004162 break;
4163 }
4164 break;
4165 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004166 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004167 break;
4168 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004169 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004170 return LY_EVALID;
4171 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004172 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004173 return ret;
4174}
4175
Michal Vaskoea5abea2018-09-18 13:10:54 +02004176/**
4177 * @brief Parse the deviation statement.
4178 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004179 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004180 * @param[in,out] data Data to read from, always moved to currently handled character.
4181 * @param[in,out] deviations Deviations to add to.
4182 *
4183 * @return LY_ERR values.
4184 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004185static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02004186parse_deviation(struct ly_parser_ctx *ctx, const char **data, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004187{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004188 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004189 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004190 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004191 enum yang_keyword kw;
4192 struct lysp_deviation *dev;
4193
Radek Krejci2c4e7172018-10-19 15:56:26 +02004194 LY_ARRAY_NEW_RET(ctx->ctx, *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004195
4196 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004197 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02004198 INSERT_WORD(ctx, buf, dev->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004199
Radek Krejci6d6556c2018-11-08 09:37:45 +01004200 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004201 switch (kw) {
4202 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004203 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 +02004204 break;
4205 case YANG_DEVIATE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004206 LY_CHECK_RET(parse_deviate(ctx, data, &dev->deviates));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004207 break;
4208 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004209 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 +02004210 break;
4211 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004212 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004213 break;
4214 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004215 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004216 return LY_EVALID;
4217 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004218 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004219 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01004220checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004221 /* mandatory substatements */
4222 if (!dev->deviates) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004223 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004224 return LY_EVALID;
4225 }
4226
4227 return ret;
4228}
4229
Michal Vaskoea5abea2018-09-18 13:10:54 +02004230/**
4231 * @brief Parse the feature statement.
4232 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004233 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004234 * @param[in,out] data Data to read from, always moved to currently handled character.
4235 * @param[in,out] features Features to add to.
4236 *
4237 * @return LY_ERR values.
4238 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004239static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02004240parse_feature(struct ly_parser_ctx *ctx, const char **data, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004241{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004242 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004243 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004244 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004245 enum yang_keyword kw;
4246 struct lysp_feature *feat;
4247
Radek Krejci2c4e7172018-10-19 15:56:26 +02004248 LY_ARRAY_NEW_RET(ctx->ctx, *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004249
4250 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004251 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02004252 INSERT_WORD(ctx, buf, feat->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004253
Radek Krejci6d6556c2018-11-08 09:37:45 +01004254 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004255 switch (kw) {
4256 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004257 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 +02004258 break;
4259 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004260 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 +02004261 break;
4262 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004263 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 +02004264 break;
4265 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004266 LY_CHECK_RET(parse_status(ctx, data, &feat->flags, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004267 break;
4268 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004269 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004270 break;
4271 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004272 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004273 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004274 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004275 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004276 return ret;
4277}
4278
Michal Vaskoea5abea2018-09-18 13:10:54 +02004279/**
4280 * @brief Parse the identity statement.
4281 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004282 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004283 * @param[in,out] data Data to read from, always moved to currently handled character.
4284 * @param[in,out] identities Identities to add to.
4285 *
4286 * @return LY_ERR values.
4287 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004288static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02004289parse_identity(struct ly_parser_ctx *ctx, const char **data, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004290{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004291 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004292 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004293 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004294 enum yang_keyword kw;
4295 struct lysp_ident *ident;
4296
Radek Krejci2c4e7172018-10-19 15:56:26 +02004297 LY_ARRAY_NEW_RET(ctx->ctx, *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004298
4299 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004300 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02004301 INSERT_WORD(ctx, buf, ident->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004302
Radek Krejci6d6556c2018-11-08 09:37:45 +01004303 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004304 switch (kw) {
4305 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004306 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 +02004307 break;
4308 case YANG_IF_FEATURE:
Radek Krejciceaf2122019-01-02 15:03:26 +01004309 YANG_CHECK_STMTVER2_RET(ctx, "if-feature", "identity");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004310 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 +02004311 break;
4312 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004313 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 +02004314 break;
4315 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004316 LY_CHECK_RET(parse_status(ctx, data, &ident->flags, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004317 break;
4318 case YANG_BASE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004319 if (ident->bases && ctx->mod_version < 2) {
Radek Krejci10113652018-11-14 16:56:50 +01004320 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Identity can be derived from multiple base identities only in YANG 1.1 modules");
4321 return LY_EVALID;
4322 }
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004323 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 +02004324 break;
4325 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004326 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004327 break;
4328 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004329 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004330 return LY_EVALID;
4331 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004332 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004333 return ret;
4334}
4335
Michal Vaskoea5abea2018-09-18 13:10:54 +02004336/**
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004337 * @brief Finalize some of the (sub)module structure after parsing.
4338 *
4339 * Update parent pointers in the nodes inside grouping/RPC/Notification, which could be reallocated.
4340 *
4341 * @param[in] mod Parsed module to be updated.
4342 * @return LY_ERR value (currently only LY_SUCCESS, but it can change in future).
4343 */
4344static LY_ERR
4345parse_sub_module_finalize(struct lysp_module *mod)
4346{
4347 unsigned int u;
4348 struct lysp_node *child;
4349
4350 /* finalize parent pointers to the reallocated items */
4351 LY_ARRAY_FOR(mod->groupings, u) {
4352 LY_LIST_FOR(mod->groupings[u].data, child) {
4353 child->parent = (struct lysp_node*)&mod->groupings[u];
4354 }
4355 }
4356
4357 /* TODO the same finalization for rpcs and notifications, do also in the relevant nodes */
4358
4359 return LY_SUCCESS;
4360}
4361
4362/**
4363 * @brief Parse module substatements.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004364 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004365 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004366 * @param[in,out] data Data to read from, always moved to currently handled character.
4367 * @param[in,out] mod Module to write to.
4368 *
4369 * @return LY_ERR values.
4370 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004371static LY_ERR
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004372parse_module(struct ly_parser_ctx *ctx, const char **data, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004373{
4374 LY_ERR ret = 0;
4375 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004376 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004377 enum yang_keyword kw, prev_kw = 0;
4378 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004379 struct lysp_submodule *dup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004380
4381 /* (sub)module name */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004382 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004383 INSERT_WORD(ctx, buf, mod->mod->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004384
Radek Krejci6d6556c2018-11-08 09:37:45 +01004385 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004386
Radek Krejcie3846472018-10-15 15:24:51 +02004387#define CHECK_ORDER(SECTION) \
4388 if (mod_stmt > SECTION) {LOGVAL_YANG(ctx, LY_VCODE_INORD, ly_stmt2str(kw), ly_stmt2str(prev_kw)); return LY_EVALID;}mod_stmt = SECTION
4389
Michal Vasko7fbc8162018-09-17 10:35:16 +02004390 switch (kw) {
4391 /* module header */
4392 case YANG_NAMESPACE:
4393 case YANG_PREFIX:
Radek Krejcie3846472018-10-15 15:24:51 +02004394 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4395 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004396 case YANG_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004397 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004398 break;
4399 /* linkage */
4400 case YANG_INCLUDE:
4401 case YANG_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004402 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004403 break;
4404 /* meta */
4405 case YANG_ORGANIZATION:
4406 case YANG_CONTACT:
4407 case YANG_DESCRIPTION:
4408 case YANG_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004409 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004410 break;
4411
4412 /* revision */
4413 case YANG_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004414 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004415 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004416 /* body */
4417 case YANG_ANYDATA:
4418 case YANG_ANYXML:
4419 case YANG_AUGMENT:
4420 case YANG_CHOICE:
4421 case YANG_CONTAINER:
4422 case YANG_DEVIATION:
4423 case YANG_EXTENSION:
4424 case YANG_FEATURE:
4425 case YANG_GROUPING:
4426 case YANG_IDENTITY:
4427 case YANG_LEAF:
4428 case YANG_LEAF_LIST:
4429 case YANG_LIST:
4430 case YANG_NOTIFICATION:
4431 case YANG_RPC:
4432 case YANG_TYPEDEF:
4433 case YANG_USES:
4434 case YANG_CUSTOM:
4435 mod_stmt = Y_MOD_BODY;
4436 break;
4437 default:
4438 /* error handled in the next switch */
4439 break;
4440 }
Radek Krejcie3846472018-10-15 15:24:51 +02004441#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004442
Radek Krejcie3846472018-10-15 15:24:51 +02004443 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004444 switch (kw) {
4445 /* module header */
4446 case YANG_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004447 LY_CHECK_RET(parse_yangversion(ctx, data, &mod->mod->version, &mod->exts));
4448 ctx->mod_version = mod->mod->version;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004449 break;
4450 case YANG_NAMESPACE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004451 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_NAMESPACE, 0, &mod->mod->ns, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004452 break;
4453 case YANG_PREFIX:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004454 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_PREFIX, 0, &mod->mod->prefix, Y_IDENTIF_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004455 break;
4456
4457 /* linkage */
4458 case YANG_INCLUDE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004459 LY_CHECK_RET(parse_include(ctx, mod->mod->name, data, &mod->includes));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004460 break;
4461 case YANG_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004462 LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, data, &mod->imports));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004463 break;
4464
4465 /* meta */
4466 case YANG_ORGANIZATION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004467 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_ORGANIZATION, 0, &mod->mod->org, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004468 break;
4469 case YANG_CONTACT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004470 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_CONTACT, 0, &mod->mod->contact, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004471 break;
4472 case YANG_DESCRIPTION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004473 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &mod->mod->dsc, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004474 break;
4475 case YANG_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004476 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &mod->mod->ref, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004477 break;
4478
4479 /* revision */
4480 case YANG_REVISION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004481 LY_CHECK_RET(parse_revision(ctx, data, &mod->revs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004482 break;
4483
4484 /* body */
4485 case YANG_ANYDATA:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004486 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "module");
Radek Krejci10113652018-11-14 16:56:50 +01004487 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004488 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004489 LY_CHECK_RET(parse_any(ctx, data, kw, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004490 break;
4491 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004492 LY_CHECK_RET(parse_choice(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004493 break;
4494 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004495 LY_CHECK_RET(parse_container(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004496 break;
4497 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004498 LY_CHECK_RET(parse_leaf(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004499 break;
4500 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004501 LY_CHECK_RET(parse_leaflist(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004502 break;
4503 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004504 LY_CHECK_RET(parse_list(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004505 break;
4506 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004507 LY_CHECK_RET(parse_uses(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004508 break;
4509
4510 case YANG_AUGMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004511 LY_CHECK_RET(parse_augment(ctx, data, NULL, &mod->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004512 break;
4513 case YANG_DEVIATION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004514 LY_CHECK_RET(parse_deviation(ctx, data, &mod->deviations));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004515 break;
4516 case YANG_EXTENSION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004517 LY_CHECK_RET(parse_extension(ctx, data, &mod->extensions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004518 break;
4519 case YANG_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004520 LY_CHECK_RET(parse_feature(ctx, data, &mod->features));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004521 break;
4522 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004523 LY_CHECK_RET(parse_grouping(ctx, data, NULL, &mod->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004524 break;
4525 case YANG_IDENTITY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004526 LY_CHECK_RET(parse_identity(ctx, data, &mod->identities));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004527 break;
4528 case YANG_NOTIFICATION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004529 LY_CHECK_RET(parse_notif(ctx, data, NULL, &mod->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004530 break;
4531 case YANG_RPC:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004532 LY_CHECK_RET(parse_action(ctx, data, NULL, &mod->rpcs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004533 break;
4534 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01004535 LY_CHECK_RET(parse_typedef(ctx, NULL, data, &mod->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004536 break;
4537 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004538 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004539 break;
4540
4541 default:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004542 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004543 return LY_EVALID;
4544 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004545 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004546 LY_CHECK_RET(ret);
Radek Krejcie86bf772018-12-14 11:39:53 +01004547
Radek Krejci6d6556c2018-11-08 09:37:45 +01004548checks:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004549 /* finalize parent pointers to the reallocated items */
4550 LY_CHECK_RET(parse_sub_module_finalize(mod));
4551
Michal Vasko7fbc8162018-09-17 10:35:16 +02004552 /* mandatory substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004553 if (!mod->mod->ns) {
4554 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "namespace", "module");
4555 return LY_EVALID;
4556 } else if (!mod->mod->prefix) {
4557 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "prefix", "module");
4558 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004559 }
4560
Radek Krejcie9e987e2018-10-31 12:50:27 +01004561 /* submodules share the namespace with the module names, so there must not be
4562 * a submodule of the same name in the context, no need for revision matching */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004563 dup = ly_ctx_get_submodule(ctx->ctx, NULL, mod->mod->name, NULL);
4564 if (dup) {
4565 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Name collision between module and submodule of name \"%s\".", mod->mod->name);
4566 return LY_EVALID;
4567 }
4568
4569 return ret;
4570}
4571
4572/**
4573 * @brief Parse submodule substatements.
4574 *
4575 * @param[in] ctx yang parser context for logging.
4576 * @param[in,out] data Data to read from, always moved to currently handled character.
4577 * @param[out] submod Parsed submodule structure.
4578 *
4579 * @return LY_ERR values.
4580 */
4581static LY_ERR
4582parse_submodule(struct ly_parser_ctx *ctx, const char **data, struct lysp_submodule *submod)
4583{
4584 LY_ERR ret = 0;
4585 char *buf, *word;
4586 size_t word_len;
4587 enum yang_keyword kw, prev_kw = 0;
4588 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
4589 struct lysp_submodule *dup;
4590
4591 /* submodule name */
4592 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
4593 INSERT_WORD(ctx, buf, submod->name, word, word_len);
4594
4595 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
4596
4597#define CHECK_ORDER(SECTION) \
4598 if (mod_stmt > SECTION) {LOGVAL_YANG(ctx, LY_VCODE_INORD, ly_stmt2str(kw), ly_stmt2str(prev_kw)); return LY_EVALID;}mod_stmt = SECTION
4599
4600 switch (kw) {
4601 /* module header */
4602 case YANG_BELONGS_TO:
4603 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4604 break;
4605 case YANG_YANG_VERSION:
4606 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4607 break;
4608 /* linkage */
4609 case YANG_INCLUDE:
4610 case YANG_IMPORT:
4611 CHECK_ORDER(Y_MOD_LINKAGE);
4612 break;
4613 /* meta */
4614 case YANG_ORGANIZATION:
4615 case YANG_CONTACT:
4616 case YANG_DESCRIPTION:
4617 case YANG_REFERENCE:
4618 CHECK_ORDER(Y_MOD_META);
4619 break;
4620
4621 /* revision */
4622 case YANG_REVISION:
4623 CHECK_ORDER(Y_MOD_REVISION);
4624 break;
4625 /* body */
4626 case YANG_ANYDATA:
4627 case YANG_ANYXML:
4628 case YANG_AUGMENT:
4629 case YANG_CHOICE:
4630 case YANG_CONTAINER:
4631 case YANG_DEVIATION:
4632 case YANG_EXTENSION:
4633 case YANG_FEATURE:
4634 case YANG_GROUPING:
4635 case YANG_IDENTITY:
4636 case YANG_LEAF:
4637 case YANG_LEAF_LIST:
4638 case YANG_LIST:
4639 case YANG_NOTIFICATION:
4640 case YANG_RPC:
4641 case YANG_TYPEDEF:
4642 case YANG_USES:
4643 case YANG_CUSTOM:
4644 mod_stmt = Y_MOD_BODY;
4645 break;
4646 default:
4647 /* error handled in the next switch */
4648 break;
4649 }
4650#undef CHECK_ORDER
4651
4652 prev_kw = kw;
4653 switch (kw) {
4654 /* module header */
4655 case YANG_YANG_VERSION:
4656 LY_CHECK_RET(parse_yangversion(ctx, data, &submod->version, &submod->exts));
4657 ctx->mod_version = submod->version;
4658 break;
4659 case YANG_BELONGS_TO:
4660 LY_CHECK_RET(parse_belongsto(ctx, data, &submod->belongsto, &submod->prefix, &submod->exts));
4661 break;
4662
4663 /* linkage */
4664 case YANG_INCLUDE:
4665 LY_CHECK_RET(parse_include(ctx, submod->name, data, &submod->includes));
4666 break;
4667 case YANG_IMPORT:
4668 LY_CHECK_RET(parse_import(ctx, submod->prefix, data, &submod->imports));
4669 break;
4670
4671 /* meta */
4672 case YANG_ORGANIZATION:
4673 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_ORGANIZATION, 0, &submod->org, Y_STR_ARG, &submod->exts));
4674 break;
4675 case YANG_CONTACT:
4676 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_CONTACT, 0, &submod->contact, Y_STR_ARG, &submod->exts));
4677 break;
4678 case YANG_DESCRIPTION:
4679 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &submod->dsc, Y_STR_ARG, &submod->exts));
4680 break;
4681 case YANG_REFERENCE:
4682 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &submod->ref, Y_STR_ARG, &submod->exts));
4683 break;
4684
4685 /* revision */
4686 case YANG_REVISION:
4687 LY_CHECK_RET(parse_revision(ctx, data, &submod->revs));
4688 break;
4689
4690 /* body */
4691 case YANG_ANYDATA:
4692 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "submodule");
4693 /* fall through */
4694 case YANG_ANYXML:
4695 LY_CHECK_RET(parse_any(ctx, data, kw, NULL, &submod->data));
4696 break;
4697 case YANG_CHOICE:
4698 LY_CHECK_RET(parse_choice(ctx, data, NULL, &submod->data));
4699 break;
4700 case YANG_CONTAINER:
4701 LY_CHECK_RET(parse_container(ctx, data, NULL, &submod->data));
4702 break;
4703 case YANG_LEAF:
4704 LY_CHECK_RET(parse_leaf(ctx, data, NULL, &submod->data));
4705 break;
4706 case YANG_LEAF_LIST:
4707 LY_CHECK_RET(parse_leaflist(ctx, data, NULL, &submod->data));
4708 break;
4709 case YANG_LIST:
4710 LY_CHECK_RET(parse_list(ctx, data, NULL, &submod->data));
4711 break;
4712 case YANG_USES:
4713 LY_CHECK_RET(parse_uses(ctx, data, NULL, &submod->data));
4714 break;
4715
4716 case YANG_AUGMENT:
4717 LY_CHECK_RET(parse_augment(ctx, data, NULL, &submod->augments));
4718 break;
4719 case YANG_DEVIATION:
4720 LY_CHECK_RET(parse_deviation(ctx, data, &submod->deviations));
4721 break;
4722 case YANG_EXTENSION:
4723 LY_CHECK_RET(parse_extension(ctx, data, &submod->extensions));
4724 break;
4725 case YANG_FEATURE:
4726 LY_CHECK_RET(parse_feature(ctx, data, &submod->features));
4727 break;
4728 case YANG_GROUPING:
4729 LY_CHECK_RET(parse_grouping(ctx, data, NULL, &submod->groupings));
4730 break;
4731 case YANG_IDENTITY:
4732 LY_CHECK_RET(parse_identity(ctx, data, &submod->identities));
4733 break;
4734 case YANG_NOTIFICATION:
4735 LY_CHECK_RET(parse_notif(ctx, data, NULL, &submod->notifs));
4736 break;
4737 case YANG_RPC:
4738 LY_CHECK_RET(parse_action(ctx, data, NULL, &submod->rpcs));
4739 break;
4740 case YANG_TYPEDEF:
4741 LY_CHECK_RET(parse_typedef(ctx, NULL, data, &submod->typedefs));
4742 break;
4743 case YANG_CUSTOM:
4744 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &submod->exts));
4745 break;
4746
4747 default:
4748 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule");
4749 return LY_EVALID;
4750 }
4751 }
4752 LY_CHECK_RET(ret);
4753
4754checks:
4755 /* finalize parent pointers to the reallocated items */
4756 LY_CHECK_RET(parse_sub_module_finalize((struct lysp_module*)submod));
4757
4758 /* mandatory substatements */
4759 if (!submod->belongsto) {
4760 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
4761 return LY_EVALID;
4762 }
4763
4764 /* submodules share the namespace with the module names, so there must not be
4765 * a submodule of the same name in the context, no need for revision matching */
4766 dup = ly_ctx_get_submodule(ctx->ctx, NULL, submod->name, NULL);
4767 if (dup && strcmp(dup->belongsto, submod->belongsto)) {
4768 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Name collision between submodules of name \"%s\".", dup->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +01004769 return LY_EVALID;
4770 }
4771
Michal Vasko7fbc8162018-09-17 10:35:16 +02004772 return ret;
4773}
4774
Radek Krejcid4557c62018-09-17 11:42:09 +02004775LY_ERR
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004776yang_parse_submodule(struct ly_parser_ctx *context, const char *data, struct lysp_submodule **submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004777{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004778 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004779 char *word, *buf;
Radek Krejciefd22f62018-09-27 11:47:58 +02004780 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004781 enum yang_keyword kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004782 struct lysp_submodule *mod_p = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004783
4784 /* "module"/"submodule" */
Radek Krejcibbe09a92018-11-08 09:36:54 +01004785 ret = get_keyword(context, &data, &kw, &word, &word_len);
4786 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004787
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004788 if (kw == YANG_MODULE) {
4789 LOGERR(context->ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
4790 ret = LY_EINVAL;
4791 goto cleanup;
4792 } else if (kw != YANG_SUBMODULE) {
Radek Krejcibbe09a92018-11-08 09:36:54 +01004793 LOGVAL_YANG(context, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".",
Radek Krejcic59bc972018-09-17 16:13:06 +02004794 ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004795 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004796 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004797 }
4798
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004799 mod_p = calloc(1, sizeof *mod_p);
4800 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(context->ctx), cleanup);
4801 mod_p->parsing = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004802
4803 /* substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004804 ret = parse_submodule(context, &data, mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004805 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004806
4807 /* read some trailing spaces or new lines */
Radek Krejcibbe09a92018-11-08 09:36:54 +01004808 ret = get_argument(context, &data, Y_MAYBE_STR_ARG, &word, &buf, &word_len);
4809 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004810
4811 if (word) {
Radek Krejcibbe09a92018-11-08 09:36:54 +01004812 LOGVAL_YANG(context, LYVE_SYNTAX, "Invalid character sequence \"%.*s\", expected end-of-file.",
Michal Vasko7fbc8162018-09-17 10:35:16 +02004813 word_len, word);
4814 free(buf);
Radek Krejci40544fa2019-01-11 09:38:37 +01004815 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004816 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004817 }
4818 assert(!buf);
4819
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004820 mod_p->parsing = 0;
4821 *submod = mod_p;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004822
Radek Krejcibbe09a92018-11-08 09:36:54 +01004823cleanup:
4824 if (ret) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004825 lysp_submodule_free(context->ctx, mod_p);
4826 }
4827
4828 return ret;
4829}
4830
4831LY_ERR
4832yang_parse_module(struct ly_parser_ctx *context, const char *data, struct lys_module *mod)
4833{
4834 LY_ERR ret = LY_SUCCESS;
4835 char *word, *buf;
4836 size_t word_len;
4837 enum yang_keyword kw;
4838 struct lysp_module *mod_p = NULL;
4839
4840 /* "module"/"submodule" */
4841 ret = get_keyword(context, &data, &kw, &word, &word_len);
4842 LY_CHECK_GOTO(ret, cleanup);
4843
4844 if (kw == YANG_SUBMODULE) {
4845 LOGERR(context->ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module.");
4846 ret = LY_EINVAL;
4847 goto cleanup;
4848 } else if (kw != YANG_MODULE) {
4849 LOGVAL_YANG(context, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".",
4850 ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004851 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004852 goto cleanup;
4853 }
4854
4855 mod_p = calloc(1, sizeof *mod_p);
4856 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(context->ctx), cleanup);
4857 mod_p->mod = mod;
4858 mod_p->parsing = 1;
4859
4860 /* substatements */
4861 ret = parse_module(context, &data, mod_p);
4862 LY_CHECK_GOTO(ret, cleanup);
4863
4864 /* read some trailing spaces or new lines */
4865 ret = get_argument(context, &data, Y_MAYBE_STR_ARG, &word, &buf, &word_len);
4866 LY_CHECK_GOTO(ret, cleanup);
4867
4868 if (word) {
4869 LOGVAL_YANG(context, LYVE_SYNTAX, "Invalid character sequence \"%.*s\", expected end-of-file.",
4870 word_len, word);
4871 free(buf);
Radek Krejci40544fa2019-01-11 09:38:37 +01004872 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004873 goto cleanup;
4874 }
4875 assert(!buf);
4876
4877 mod_p->parsing = 0;
4878 mod->parsed = mod_p;
4879
4880cleanup:
4881 if (ret) {
4882 lysp_module_free(mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004883 }
4884
Michal Vasko7fbc8162018-09-17 10:35:16 +02004885 return ret;
4886}