blob: 82a9ba33ca7759fc44994dbb574524f1684d5832 [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 Krejcifaa1eac2018-10-30 14:34:55 +010044#define CHECK_UNIQUENESS(CTX, ARRAY, MEMBER, STMT, IDENT) \
45 if (ARRAY) { \
46 for (unsigned int u = 0; u < LY_ARRAY_SIZE(ARRAY) - 1; ++u) { \
47 if (!strcmp((ARRAY)[u].MEMBER, IDENT)) { \
48 LOGVAL_YANG(CTX, LY_VCODE_DUPIDENT, IDENT, STMT); \
49 return LY_EVALID; \
50 } \
51 } \
52 }
53
Radek Krejci9fcacc12018-10-11 15:59:11 +020054#define INSERT_WORD(CTX, BUF, TARGET, WORD, LEN) \
55 if (BUF) {(TARGET) = lydict_insert_zc((CTX)->ctx, WORD);}\
56 else {(TARGET) = lydict_insert((CTX)->ctx, WORD, LEN);}
Radek Krejci44ceedc2018-10-02 15:54:31 +020057
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020058#define MOVE_INPUT(CTX, DATA, COUNT) (*(data))+=COUNT;(CTX)->indent+=COUNT
59
Michal Vaskoea5abea2018-09-18 13:10:54 +020060/**
61 * @brief Loop through all substatements providing, return if there are none.
62 *
Radek Krejci44ceedc2018-10-02 15:54:31 +020063 * @param[in] CTX yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +020064 * @param[in] DATA Raw data to read from.
65 * @param[out] KW YANG keyword read.
66 * @param[out] WORD Pointer to the keyword itself.
67 * @param[out] WORD_LEN Length of the keyword.
68 * @param[out] ERR Variable for error storing.
69 *
70 * @return In case there are no substatements or a fatal error encountered.
71 */
Radek Krejci6d6556c2018-11-08 09:37:45 +010072#define YANG_READ_SUBSTMT_FOR(CTX, DATA, KW, WORD, WORD_LEN, ERR, CHECKGOTO) \
Radek Krejci6d9b9b52018-11-02 12:43:39 +010073 LY_CHECK_RET(get_keyword(CTX, DATA, &KW, &WORD, &WORD_LEN)); \
Michal Vasko7fbc8162018-09-17 10:35:16 +020074 if (KW == YANG_SEMICOLON) { \
Radek Krejci6d6556c2018-11-08 09:37:45 +010075 CHECKGOTO; \
Radek Krejci6d9b9b52018-11-02 12:43:39 +010076 return LY_SUCCESS; \
Michal Vasko7fbc8162018-09-17 10:35:16 +020077 } \
78 if (KW != YANG_LEFT_BRACE) { \
Radek Krejci44ceedc2018-10-02 15:54:31 +020079 LOGVAL_YANG(CTX, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\", expected \";\" or \"{\".", ly_stmt2str(KW)); \
Michal Vasko7fbc8162018-09-17 10:35:16 +020080 return LY_EVALID; \
81 } \
82 for (ERR = get_keyword(CTX, DATA, &KW, &WORD, &WORD_LEN); \
83 !ERR && (KW != YANG_RIGHT_BRACE); \
84 ERR = get_keyword(CTX, DATA, &KW, &WORD, &WORD_LEN))
85
Radek Krejci10113652018-11-14 16:56:50 +010086#define YANG_CHECK_STMTVER_RET(CTX, KW, PARENT) \
87 if ((CTX)->mod->version < 2) {LOGVAL_YANG((CTX), LY_VCODE_INCHILDSTMT2, KW, PARENT); return LY_EVALID;}
88
Radek Krejci6d9b9b52018-11-02 12:43:39 +010089static LY_ERR parse_container(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
90static LY_ERR parse_uses(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
91static LY_ERR parse_choice(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
92static LY_ERR parse_case(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
93static LY_ERR parse_list(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
94static 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 +020095
Michal Vaskoea5abea2018-09-18 13:10:54 +020096/**
97 * @brief Add another character to dynamic buffer, a low-level function.
98 *
Radek Krejci44ceedc2018-10-02 15:54:31 +020099 * Enlarge if needed. Updates \p input as well as \p buf_used.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200100 *
Radek Krejci404251e2018-10-09 12:06:44 +0200101 * @param[in] ctx libyang context for logging.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200102 * @param[in, out] input Input string to process.
103 * @param[in] len Number of bytes to get from the input string and copy into the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200104 * @param[in,out] buf Buffer to use, can be moved by realloc().
105 * @param[in,out] buf_len Current size of the buffer.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200106 * @param[in,out] buf_used Currently used characters of the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200107 *
108 * @return LY_ERR values.
109 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200110static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +0200111buf_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 +0200112{
Radek Krejci44ceedc2018-10-02 15:54:31 +0200113 if (*buf_len <= (*buf_used) + len) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200114 *buf_len += 16;
115 *buf = ly_realloc(*buf, *buf_len);
116 LY_CHECK_ERR_RET(!*buf, LOGMEM(ctx), LY_EMEM);
117 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200118 memcpy(&(*buf)[*buf_used], *input, len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200119
Radek Krejci44ceedc2018-10-02 15:54:31 +0200120 (*buf_used) += len;
121 (*input) += len;
122
Michal Vasko7fbc8162018-09-17 10:35:16 +0200123 return LY_SUCCESS;
124}
125
Michal Vaskoea5abea2018-09-18 13:10:54 +0200126/**
Radek Krejci44ceedc2018-10-02 15:54:31 +0200127 * @brief Check that \p c is valid UTF8 code point for YANG string.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200128 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200129 * @param[in] ctx yang parser context for logging.
130 * @param[in] c UTF8 code point of a character to check.
131 * @return LY_ERR values.
132 */
133static LY_ERR
134check_stringchar(struct ly_parser_ctx *ctx, unsigned int c)
135{
136 if (!is_yangutf8char(c)) {
137 LOGVAL_YANG(ctx, LY_VCODE_INCHAR, c);
138 return LY_EVALID;
139 }
140 return LY_SUCCESS;
141}
142
143/**
144 * @brief Check that \p c is valid UTF8 code point for YANG identifier.
145 *
146 * @param[in] ctx yang parser context for logging.
147 * @param[in] c UTF8 code point of a character to check.
148 * @param[in] first Flag to check the first character of an identifier, which is more restricted.
Radek Krejcidcc7b322018-10-11 14:24:02 +0200149 * @param[in,out] prefix Storage for internally used flag in case of possible prefixed identifiers:
150 * 0 - colon not yet found (no prefix)
151 * 1 - \p c is the colon character
152 * 2 - prefix already processed, now processing the identifier
153 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200154 * If the identifier cannot be prefixed, NULL is expected.
155 * @return LY_ERR values.
156 */
157static LY_ERR
158check_identifierchar(struct ly_parser_ctx *ctx, unsigned int c, int first, int *prefix)
159{
160 if (first || (prefix && (*prefix) == 1)) {
161 if (!is_yangidentstartchar(c)) {
162 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '%c'.", c);
163 return LY_EVALID;
164 }
Radek Krejci9fcacc12018-10-11 15:59:11 +0200165 if (prefix) {
166 if (first) {
167 (*prefix) = 0;
168 } else {
169 (*prefix) = 2;
170 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200171 }
172 } else if (c == ':' && prefix && (*prefix) == 0) {
173 (*prefix) = 1;
174 } else if (!is_yangidentchar(c)) {
175 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Invalid identifier character '%c'.", c);
176 return LY_EVALID;
177 }
178
179 return LY_SUCCESS;
180}
181
182/**
183 * @brief Store a single UTF8 character. It depends whether in a dynamically-allocated buffer or just as a pointer to the data.
184 *
185 * @param[in] ctx yang parser context for logging.
186 * @param[in,out] input Pointer to the string where to get the character to store. Automatically moved to the next character
187 * when function returns.
188 * @param[in] arg Type of the input string to select method of checking character validity.
189 * @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 +0200190 * 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 +0200191 * @param[in,out] word_len Current length of the word pointed to by \p word_p.
192 * @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 +0200193 * @param[in,out] buf_len Current length of \p word_b.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200194 * @param[in] need_buf Flag if the dynamically allocated buffer is required.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200195 *
196 * @return LY_ERR values.
197 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200198static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +0200199buf_store_char(struct ly_parser_ctx *ctx, const char **input, enum yang_arg arg,
200 char **word_p, size_t *word_len, char **word_b, size_t *buf_len, int need_buf)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200201{
Radek Krejci404251e2018-10-09 12:06:44 +0200202 int prefix = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200203 unsigned int c;
204 size_t len;
205
206 /* get UTF8 code point (and number of bytes coding the character) */
207 LY_CHECK_ERR_RET(ly_getutf8(input, &c, &len),
208 LOGVAL_YANG(ctx, LY_VCODE_INCHAR, (*input)[-len]), LY_EVALID);
209 (*input) -= len;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200210 if (c == '\n') {
211 ctx->indent = 0;
212 } else {
213 /* note - even the multibyte character is count as 1 */
214 ++ctx->indent;
215 }
216
Radek Krejci44ceedc2018-10-02 15:54:31 +0200217 /* check character validity */
218 switch (arg) {
219 case Y_IDENTIF_ARG:
220 LY_CHECK_RET(check_identifierchar(ctx, c, !(*word_len), NULL));
221 break;
222 case Y_PREF_IDENTIF_ARG:
223 LY_CHECK_RET(check_identifierchar(ctx, c, !(*word_len), &prefix));
224 break;
225 case Y_STR_ARG:
226 case Y_MAYBE_STR_ARG:
227 LY_CHECK_RET(check_stringchar(ctx, c));
228 break;
229 }
230
Michal Vasko7fbc8162018-09-17 10:35:16 +0200231 if (word_b && *word_b) {
232 /* add another character into buffer */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200233 if (buf_add_char(ctx->ctx, input, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200234 return LY_EMEM;
235 }
236
237 /* in case of realloc */
238 *word_p = *word_b;
239 } else if (need_buf) {
240 /* first time we need a buffer, copy everything read up to now */
241 if (*word_len) {
242 *word_b = malloc(*word_len);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200243 LY_CHECK_ERR_RET(!*word_b, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200244 *buf_len = *word_len;
245 memcpy(*word_b, *word_p, *word_len);
246 }
247
248 /* add this new character into buffer */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200249 if (buf_add_char(ctx->ctx, input, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200250 return LY_EMEM;
251 }
252
253 /* in case of realloc */
254 *word_p = *word_b;
255 } else {
256 /* just remember the first character pointer */
257 if (!*word_p) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200258 *word_p = (char *)(*input);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200259 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200260 /* ... and update the word's length */
261 (*word_len) += len;
262 (*input) += len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200263 }
264
265 return LY_SUCCESS;
266}
267
Michal Vaskoea5abea2018-09-18 13:10:54 +0200268/**
269 * @brief Skip YANG comment in data.
270 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200271 * @param[in] ctx yang parser context for logging.
272 * @param[in,out] data Data to read from, automatically moved after the comment.
273 * @param[in] comment Type of the comment to process:
274 * 1 for a one-line comment,
275 * 2 for a block comment.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200276 *
277 * @return LY_ERR values.
278 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200279static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +0200280skip_comment(struct ly_parser_ctx *ctx, const char **data, int comment)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200281{
Radek Krejci80dd33e2018-09-26 15:57:18 +0200282 /* internal statuses: 0 - comment ended,
283 * 1 - in line comment,
284 * 2 - in block comment,
285 * 3 - in block comment with last read character '*'
286 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200287 while (**data && comment) {
288 switch (comment) {
289 case 1:
290 if (**data == '\n') {
291 comment = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200292 ++ctx->line;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200293 }
294 break;
295 case 2:
296 if (**data == '*') {
Radek Krejci15c80ca2018-10-09 11:01:31 +0200297 comment = 3;
298 } else if (**data == '\n') {
299 ++ctx->line;
300 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200301 break;
302 case 3:
303 if (**data == '/') {
304 comment = 0;
305 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200306 if (**data == '\n') {
307 ++ctx->line;
308 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200309 comment = 2;
310 }
311 break;
312 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200313 LOGINT_RET(ctx->ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200314 }
315
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200316 if (**data == '\n') {
317 ctx->indent = 0;
318 } else {
319 ++ctx->indent;
320 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200321 ++(*data);
322 }
323
324 if (!**data && (comment > 1)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200325 LOGVAL_YANG(ctx, LYVE_SYNTAX, "Unexpected end-of-file, non-terminated comment.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200326 return LY_EVALID;
327 }
328
329 return LY_SUCCESS;
330}
331
Michal Vaskoea5abea2018-09-18 13:10:54 +0200332/**
333 * @brief Read a quoted string from data.
334 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200335 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200336 * @param[in,out] data Data to read from, always moved to currently handled character.
337 * @param[in] arg Type of YANG keyword argument expected.
338 * @param[out] word_p Pointer to the read quoted string.
339 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read quoted string. If not needed,
340 * set to NULL. Otherwise equal to \p word_p.
341 * @param[out] word_len Length of the read quoted string.
342 * @param[out] buf_len Length of the dynamically-allocated buffer \p word_b.
343 * @param[in] indent Current indent (number of YANG spaces). Needed for correct multi-line string
344 * indenation in the final quoted string.
345 *
346 * @return LY_ERR values.
347 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200348static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +0200349read_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 +0200350 size_t *buf_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200351{
352 /* string: 0 - string ended, 1 - string with ', 2 - string with ", 3 - string with " with last character \,
353 * 4 - string finished, now skipping whitespaces looking for +,
354 * 5 - string continues after +, skipping whitespaces */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200355 unsigned int string, block_indent = 0, current_indent = 0, need_buf = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200356 const char *c;
357
358 if (**data == '\"') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200359 string = 2;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200360 current_indent = block_indent = ctx->indent + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200361 } else {
362 assert(**data == '\'');
363 string = 1;
364 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200365 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200366
367 while (**data && string) {
368 switch (string) {
369 case 1:
370 switch (**data) {
371 case '\'':
372 /* string may be finished, but check for + */
373 string = 4;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200374 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200375 break;
376 default:
377 /* check and store character */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200378 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 +0200379 break;
380 }
381 break;
382 case 2:
383 switch (**data) {
384 case '\"':
385 /* string may be finished, but check for + */
386 string = 4;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200387 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200388 break;
389 case '\\':
390 /* special character following */
391 string = 3;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200392 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200393 break;
394 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200395 if (current_indent < block_indent) {
396 ++current_indent;
397 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200398 } else {
399 /* check and store character */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200400 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 +0200401 }
402 break;
403 case '\t':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200404 if (current_indent < block_indent) {
405 assert(need_buf);
406 current_indent += 8;
407 ctx->indent += 8;
408 for (; current_indent > block_indent; --current_indent, --ctx->indent) {
409 /* store leftover spaces from the tab */
410 c = " ";
411 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 +0200412 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200413 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200414 } else {
415 /* check and store character */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200416 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 +0200417 /* additional characters for indentation - only 1 was count in buf_store_char */
418 ctx->indent += 7;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200419 }
420 break;
421 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200422 if (block_indent) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200423 /* we will be removing the indents so we need our own buffer */
424 need_buf = 1;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200425
426 /* remove trailing tabs and spaces */
427 while ((*word_len) && ((*word_p)[(*word_len) - 1] == '\t' || (*word_p)[(*word_len) - 1] == ' ')) {
428 --(*word_len);
429 }
430
431 /* start indentation */
432 current_indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200433 }
434
435 /* check and store character */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200436 LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, buf_len, need_buf));
437
438 /* maintain line number */
439 ++ctx->line;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200440
441 /* reset context indentation counter for possible string after this one */
442 ctx->indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200443 break;
444 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200445 /* first non-whitespace character, stop eating indentation */
446 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200447
448 /* check and store character */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200449 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 +0200450 break;
451 }
452 break;
453 case 3:
454 /* string encoded characters */
455 switch (**data) {
456 case 'n':
457 c = "\n";
458 break;
459 case 't':
460 c = "\t";
461 break;
462 case '\"':
463 c = *data;
464 break;
465 case '\\':
466 c = *data;
467 break;
468 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200469 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Double-quoted string unknown special character '\\%c'.", **data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200470 return LY_EVALID;
471 }
472
473 /* check and store character */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200474 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 +0200475
476 string = 2;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200477 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200478 break;
479 case 4:
480 switch (**data) {
481 case '+':
482 /* string continues */
483 string = 5;
Radek Krejciefd22f62018-09-27 11:47:58 +0200484 need_buf = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200485 break;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200486 case '\n':
487 ++ctx->line;
488 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200489 case ' ':
490 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200491 /* just skip */
492 break;
493 default:
494 /* string is finished */
495 goto string_end;
496 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200497 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200498 break;
499 case 5:
500 switch (**data) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200501 case '\n':
502 ++ctx->line;
503 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200504 case ' ':
505 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200506 /* skip */
507 break;
508 case '\'':
509 string = 1;
510 break;
511 case '\"':
512 string = 2;
513 break;
514 default:
515 /* it must be quoted again */
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200516 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Both string parts divided by '+' must be quoted.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200517 return LY_EVALID;
518 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200519 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200520 break;
521 default:
522 return LY_EINT;
523 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200524 }
525
526string_end:
527 return LY_SUCCESS;
528}
529
Michal Vaskoea5abea2018-09-18 13:10:54 +0200530/**
531 * @brief Get another YANG string from the raw data.
532 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200533 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200534 * @param[in,out] data Data to read from, always moved to currently handled character.
535 * @param[in] arg Type of YANG keyword argument expected.
Michal Vasko2ca70f52018-09-27 11:04:51 +0200536 * @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 +0200537 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read string. If not needed,
538 * set to NULL. Otherwise equal to \p word_p.
539 * @param[out] word_len Length of the read string.
540 *
541 * @return LY_ERR values.
Michal Vasko7fbc8162018-09-17 10:35:16 +0200542 */
543static LY_ERR
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200544get_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 +0200545{
Radek Krejci44ceedc2018-10-02 15:54:31 +0200546 size_t buf_len = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200547
548 /* word buffer - dynamically allocated */
549 *word_b = NULL;
550
551 /* word pointer - just a pointer to data */
552 *word_p = NULL;
553
554 *word_len = 0;
555 while (**data) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200556 switch (**data) {
557 case '\'':
558 case '\"':
559 if (*word_len) {
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200560 /* invalid - quotes cannot be in unquoted string and only optsep, ; or { can follow it */
561 LOGVAL_YANG(ctx, LY_VCODE_INSTREXP, 1, *data,
562 "unquoted string character, optsep, semicolon or opening brace");
563 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200564 }
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100565 LY_CHECK_RET(read_qstring(ctx, data, arg, word_p, word_b, word_len, &buf_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200566 goto str_end;
567 case '/':
Radek Krejci44ceedc2018-10-02 15:54:31 +0200568 if ((*data)[1] == '/') {
569 /* one-line comment */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200570 MOVE_INPUT(ctx, data, 2);
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100571 LY_CHECK_RET(skip_comment(ctx, data, 1));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200572 } else if ((*data)[1] == '*') {
573 /* block comment */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200574 MOVE_INPUT(ctx, data, 2);
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100575 LY_CHECK_RET(skip_comment(ctx, data, 2));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200576 } else {
577 /* not a comment after all */
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100578 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 +0200579 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200580 break;
581 case ' ':
582 if (*word_len) {
583 /* word is finished */
584 goto str_end;
585 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200586 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200587 break;
588 case '\t':
589 if (*word_len) {
590 /* word is finished */
591 goto str_end;
592 }
593 /* tabs count for 8 spaces */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200594 ctx->indent += 8;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200595
596 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200597 break;
598 case '\n':
599 if (*word_len) {
600 /* word is finished */
601 goto str_end;
602 }
603 /* reset indent */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200604 ctx->indent = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200605
606 /* track line numbers */
607 ++ctx->line;
608
609 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200610 break;
611 case ';':
612 case '{':
613 if (*word_len || (arg == Y_MAYBE_STR_ARG)) {
614 /* word is finished */
615 goto str_end;
616 }
617
Radek Krejci44ceedc2018-10-02 15:54:31 +0200618 LOGVAL_YANG(ctx, LY_VCODE_INSTREXP, 1, *data, "an argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200619 return LY_EVALID;
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200620 case '}':
621 /* invalid - braces cannot be in unquoted string (opening braces terminates the string and can follow it) */
622 LOGVAL_YANG(ctx, LY_VCODE_INSTREXP, 1, *data,
623 "unquoted string character, optsep, semicolon or opening brace");
624 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200625 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200626 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 +0200627 break;
628 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200629 }
630
631str_end:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200632 /* terminating NULL byte for buf */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200633 if (*word_b) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200634 (*word_b) = ly_realloc(*word_b, (*word_len) + 1);
635 LY_CHECK_ERR_RET(!(*word_b), LOGMEM(ctx->ctx), LY_EMEM);
636 (*word_b)[*word_len] = '\0';
Michal Vasko7fbc8162018-09-17 10:35:16 +0200637 *word_p = *word_b;
638 }
639
640 return LY_SUCCESS;
641}
642
Michal Vaskoea5abea2018-09-18 13:10:54 +0200643/**
644 * @brief Get another YANG keyword from the raw data.
645 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200646 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200647 * @param[in,out] data Data to read from, always moved to currently handled character.
648 * @param[out] kw YANG keyword read.
649 * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances.
650 * @param[out] word_len Length of the keyword in the data. Useful for extension instances.
651 *
652 * @return LY_ERR values.
653 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200654static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +0200655get_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 +0200656{
Michal Vasko7fbc8162018-09-17 10:35:16 +0200657 int prefix;
658 const char *word_start;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200659 unsigned int c;
660 size_t len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200661
662 if (word_p) {
663 *word_p = NULL;
664 *word_len = 0;
665 }
666
667 /* first skip "optsep", comments */
668 while (**data) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200669 switch (**data) {
670 case '/':
671 if ((*data)[1] == '/') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200672 /* one-line comment */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200673 MOVE_INPUT(ctx, data, 2);
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100674 LY_CHECK_RET(skip_comment(ctx, data, 1));
Radek Krejcidcc7b322018-10-11 14:24:02 +0200675 } else if ((*data)[1] == '*') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200676 /* block comment */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200677 MOVE_INPUT(ctx, data, 2);
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100678 LY_CHECK_RET(skip_comment(ctx, data, 2));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200679 } else {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200680 /* error - not a comment after all, keyword cannot start with slash */
681 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '/'.");
682 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200683 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200684 break;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200685 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200686 /* skip whitespaces (optsep) */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200687 ++ctx->line;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200688 ctx->indent = 0;
689 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200690 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200691 /* skip whitespaces (optsep) */
692 ++ctx->indent;
693 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200694 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200695 /* skip whitespaces (optsep) */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200696 ctx->indent += 8;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200697 break;
698 default:
699 /* either a keyword start or an invalid character */
700 goto keyword_start;
701 }
702
703 ++(*data);
704 }
705
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200706#define IF_KW(STR, LEN, STMT) if (!strncmp(*(data), STR, LEN)) {MOVE_INPUT(ctx, data, LEN);*kw=STMT;}
707#define IF_KW_PREFIX(STR, LEN) if (!strncmp(*(data), STR, LEN)) {MOVE_INPUT(ctx, data, LEN);
708#define IF_KW_PREFIX_END }
709
Michal Vasko7fbc8162018-09-17 10:35:16 +0200710keyword_start:
711 word_start = *data;
712 *kw = YANG_NONE;
713
714 /* read the keyword itself */
715 switch (**data) {
716 case 'a':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200717 MOVE_INPUT(ctx, data, 1);
718 IF_KW("rgument", 7, YANG_ARGUMENT)
719 else IF_KW("ugment", 6, YANG_AUGMENT)
720 else IF_KW("ction", 5, YANG_ACTION)
721 else IF_KW_PREFIX("ny", 2)
722 IF_KW("data", 4, YANG_ANYDATA)
723 else IF_KW("xml", 3, YANG_ANYXML)
724 IF_KW_PREFIX_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200725 break;
726 case 'b':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200727 MOVE_INPUT(ctx, data, 1);
728 IF_KW("ase", 3, YANG_BASE)
729 else IF_KW("elongs-to", 9, YANG_BELONGS_TO)
730 else IF_KW("it", 2, YANG_BIT)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200731 break;
732 case 'c':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200733 MOVE_INPUT(ctx, data, 1);
734 IF_KW("ase", 3, YANG_CASE)
735 else IF_KW("hoice", 5, YANG_CHOICE)
736 else IF_KW_PREFIX("on", 2)
737 IF_KW("fig", 3, YANG_CONFIG)
738 else IF_KW_PREFIX("ta", 2)
739 IF_KW("ct", 2, YANG_CONTACT)
740 else IF_KW("iner", 4, YANG_CONTAINER)
741 IF_KW_PREFIX_END
742 IF_KW_PREFIX_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200743 break;
744 case 'd':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200745 MOVE_INPUT(ctx, data, 1);
746 IF_KW_PREFIX("e", 1)
747 IF_KW("fault", 5, YANG_DEFAULT)
748 else IF_KW("scription", 9, YANG_DESCRIPTION)
749 else IF_KW_PREFIX("viat", 4)
750 IF_KW("e", 1, YANG_DEVIATE)
751 else IF_KW("ion", 3, YANG_DEVIATION)
752 IF_KW_PREFIX_END
753 IF_KW_PREFIX_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200754 break;
755 case 'e':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200756 MOVE_INPUT(ctx, data, 1);
757 IF_KW("num", 3, YANG_ENUM)
758 else IF_KW_PREFIX("rror-", 5)
759 IF_KW("app-tag", 7, YANG_ERROR_APP_TAG)
760 else IF_KW("message", 7, YANG_ERROR_MESSAGE)
761 IF_KW_PREFIX_END
762 else IF_KW("xtension", 8, YANG_EXTENSION)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200763 break;
764 case 'f':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200765 MOVE_INPUT(ctx, data, 1);
766 IF_KW("eature", 6, YANG_FEATURE)
767 else IF_KW("raction-digits", 14, YANG_FRACTION_DIGITS)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200768 break;
769 case 'g':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200770 MOVE_INPUT(ctx, data, 1);
771 IF_KW("rouping", 7, YANG_GROUPING)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200772 break;
773 case 'i':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200774 MOVE_INPUT(ctx, data, 1);
775 IF_KW("dentity", 7, YANG_IDENTITY)
776 else IF_KW("f-feature", 9, YANG_IF_FEATURE)
777 else IF_KW("mport", 5, YANG_IMPORT)
778 else IF_KW_PREFIX("n", 1)
779 IF_KW("clude", 5, YANG_INCLUDE)
780 else IF_KW("put", 3, YANG_INPUT)
781 IF_KW_PREFIX_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200782 break;
783 case 'k':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200784 MOVE_INPUT(ctx, data, 1);
785 IF_KW("ey", 2, YANG_KEY)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200786 break;
787 case 'l':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200788 MOVE_INPUT(ctx, data, 1);
789 IF_KW_PREFIX("e", 1)
790 IF_KW("af-list", 7, YANG_LEAF_LIST)
791 else IF_KW("af", 2, YANG_LEAF)
792 else IF_KW("ngth", 4, YANG_LENGTH)
793 IF_KW_PREFIX_END
794 else IF_KW("ist", 3, YANG_LIST)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200795 break;
796 case 'm':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200797 MOVE_INPUT(ctx, data, 1);
798 IF_KW_PREFIX("a", 1)
799 IF_KW("ndatory", 7, YANG_MANDATORY)
800 else IF_KW("x-elements", 10, YANG_MAX_ELEMENTS)
801 IF_KW_PREFIX_END
802 else IF_KW("in-elements", 11, YANG_MIN_ELEMENTS)
803 else IF_KW("ust", 3, YANG_MUST)
804 else IF_KW_PREFIX("od", 2)
805 IF_KW("ule", 3, YANG_MODULE)
806 else IF_KW("ifier", 5, YANG_MODIFIER)
807 IF_KW_PREFIX_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200808 break;
809 case 'n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200810 MOVE_INPUT(ctx, data, 1);
811 IF_KW("amespace", 8, YANG_NAMESPACE)
812 else IF_KW("otification", 11, YANG_NOTIFICATION)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200813 break;
814 case 'o':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200815 MOVE_INPUT(ctx, data, 1);
816 IF_KW_PREFIX("r", 1)
817 IF_KW("dered-by", 8, YANG_ORDERED_BY)
818 else IF_KW("ganization", 10, YANG_ORGANIZATION)
819 IF_KW_PREFIX_END
820 else IF_KW("utput", 5, YANG_OUTPUT)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200821 break;
822 case 'p':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200823 MOVE_INPUT(ctx, data, 1);
824 IF_KW("ath", 3, YANG_PATH)
825 else IF_KW("attern", 6, YANG_PATTERN)
826 else IF_KW("osition", 7, YANG_POSITION)
827 else IF_KW_PREFIX("re", 2)
828 IF_KW("fix", 3, YANG_PREFIX)
829 else IF_KW("sence", 5, YANG_PRESENCE)
830 IF_KW_PREFIX_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200831 break;
832 case 'r':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200833 MOVE_INPUT(ctx, data, 1);
834 IF_KW("ange", 4, YANG_RANGE)
835 else IF_KW_PREFIX("e", 1)
836 IF_KW_PREFIX("f", 1)
837 IF_KW("erence", 6, YANG_REFERENCE)
838 else IF_KW("ine", 3, YANG_REFINE)
839 IF_KW_PREFIX_END
840 else IF_KW("quire-instance", 14, YANG_REQUIRE_INSTANCE)
841 else IF_KW("vision-date", 11, YANG_REVISION_DATE)
842 else IF_KW("vision", 6, YANG_REVISION)
843 IF_KW_PREFIX_END
844 else IF_KW("pc", 2, YANG_RPC)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200845 break;
846 case 's':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200847 MOVE_INPUT(ctx, data, 1);
848 IF_KW("tatus", 5, YANG_STATUS)
849 else IF_KW("ubmodule", 8, YANG_SUBMODULE)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200850 break;
851 case 't':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200852 MOVE_INPUT(ctx, data, 1);
853 IF_KW("ypedef", 6, YANG_TYPEDEF)
854 else IF_KW("ype", 3, YANG_TYPE)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200855 break;
856 case 'u':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200857 MOVE_INPUT(ctx, data, 1);
858 IF_KW_PREFIX("ni", 2)
859 IF_KW("que", 3, YANG_UNIQUE)
860 else IF_KW("ts", 2, YANG_UNITS)
861 IF_KW_PREFIX_END
862 else IF_KW("ses", 3, YANG_USES)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200863 break;
864 case 'v':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200865 MOVE_INPUT(ctx, data, 1);
866 IF_KW("alue", 4, YANG_VALUE)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200867 break;
868 case 'w':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200869 MOVE_INPUT(ctx, data, 1);
870 IF_KW("hen", 3, YANG_WHEN)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200871 break;
872 case 'y':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200873 MOVE_INPUT(ctx, data, 1);
874 IF_KW("ang-version", 11, YANG_YANG_VERSION)
875 else IF_KW("in-element", 10, YANG_YIN_ELEMENT)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200876 break;
877 case ';':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200878 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200879 *kw = YANG_SEMICOLON;
Radek Krejci626df482018-10-11 15:06:31 +0200880 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200881 case '{':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200882 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200883 *kw = YANG_LEFT_BRACE;
Radek Krejci626df482018-10-11 15:06:31 +0200884 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200885 case '}':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200886 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200887 *kw = YANG_RIGHT_BRACE;
Radek Krejci626df482018-10-11 15:06:31 +0200888 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200889 default:
890 break;
891 }
892
893 if (*kw != YANG_NONE) {
894 /* make sure we have the whole keyword */
895 switch (**data) {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200896 case '\n':
897 ++ctx->line;
898 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200899 case ' ':
900 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200901 /* mandatory "sep" */
902 break;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200903 case ':':
904 /* keyword is not actually a keyword, but prefix of an extension.
905 * To avoid repeated check of the prefix syntax, move to the point where the colon was read
906 * and we will be checking the keyword (extension instance) itself */
907 prefix = 1;
908 MOVE_INPUT(ctx, data, 1);
909 goto extension;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200910 case '{':
911 /* allowed only for input and output statements which can be without arguments */
912 if (*kw == YANG_INPUT || *kw == YANG_OUTPUT) {
913 break;
914 }
915 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200916 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200917 MOVE_INPUT(ctx, data, 1);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200918 LOGVAL_YANG(ctx, LY_VCODE_INSTREXP, (int)(*data - word_start), word_start,
919 "a keyword followed by a separator");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200920 return LY_EVALID;
921 }
922 } else {
923 /* still can be an extension */
924 prefix = 0;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200925extension:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200926 while (**data && (**data != ' ') && (**data != '\t') && (**data != '\n') && (**data != '{') && (**data != ';')) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200927 LY_CHECK_ERR_RET(ly_getutf8(data, &c, &len),
928 LOGVAL_YANG(ctx, LY_VCODE_INCHAR, (*data)[-len]), LY_EVALID);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200929 ++ctx->indent;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200930 /* check character validity */
931 LY_CHECK_RET(check_identifierchar(ctx, c, *data - len == word_start ? 1 : 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200932 }
933 if (!**data) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200934 LOGVAL_YANG(ctx, LY_VCODE_EOF);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200935 return LY_EVALID;
936 }
937
938 /* prefix is mandatory for extension instances */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200939 if (prefix != 2) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200940 LOGVAL_YANG(ctx, LY_VCODE_INSTREXP, (int)(*data - word_start), word_start, "a keyword");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200941 return LY_EVALID;
942 }
943
944 *kw = YANG_CUSTOM;
945 }
Radek Krejci626df482018-10-11 15:06:31 +0200946success:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200947 if (word_p) {
948 *word_p = (char *)word_start;
949 *word_len = *data - word_start;
950 }
951
952 return LY_SUCCESS;
953}
954
Michal Vaskoea5abea2018-09-18 13:10:54 +0200955/**
956 * @brief Parse extension instance substatements.
957 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200958 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200959 * @param[in,out] data Data to read from, always moved to currently handled character.
960 * @param[in] word Extension instance substatement name (keyword).
961 * @param[in] word_len Extension instance substatement name length.
962 * @param[in,out] child Children of this extension instance to add to.
963 *
964 * @return LY_ERR values.
965 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200966static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +0200967parse_ext_substmt(struct ly_parser_ctx *ctx, const char **data, char *word, size_t word_len,
Michal Vasko7fbc8162018-09-17 10:35:16 +0200968 struct lysp_stmt **child)
969{
970 char *buf;
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100971 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200972 enum yang_keyword kw;
973 struct lysp_stmt *stmt, *par_child;
974
975 stmt = calloc(1, sizeof *stmt);
976 LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM);
977
Radek Krejci44ceedc2018-10-02 15:54:31 +0200978 stmt->stmt = lydict_insert(ctx->ctx, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200979
980 /* get optional argument */
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100981 LY_CHECK_RET(get_argument(ctx, data, Y_MAYBE_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200982
Radek Krejci0ae092d2018-09-20 16:43:19 +0200983 if (word) {
984 if (buf) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200985 stmt->arg = lydict_insert_zc(ctx->ctx, word);
Radek Krejci0ae092d2018-09-20 16:43:19 +0200986 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200987 stmt->arg = lydict_insert(ctx->ctx, word, word_len);
Radek Krejci0ae092d2018-09-20 16:43:19 +0200988 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200989 }
990
991 /* insert into parent statements */
992 if (!*child) {
993 *child = stmt;
994 } else {
995 for (par_child = *child; par_child->next; par_child = par_child->next);
996 par_child->next = stmt;
997 }
998
Radek Krejci6d6556c2018-11-08 09:37:45 +0100999 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, ) {
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001000 LY_CHECK_RET(parse_ext_substmt(ctx, data, word, word_len, &stmt->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001001 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001002 return ret;
1003}
1004
Michal Vaskoea5abea2018-09-18 13:10:54 +02001005/**
1006 * @brief Parse extension instance.
1007 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001008 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001009 * @param[in,out] data Data to read from, always moved to currently handled character.
1010 * @param[in] ext_name Extension instance substatement name (keyword).
1011 * @param[in] ext_name_len Extension instance substatement name length.
1012 * @param[in] insubstmt Type of the keyword this extension instance is a substatement of.
1013 * @param[in] insubstmt_index Index of the keyword instance this extension instance is a substatement of.
1014 * @param[in,out] exts Extension instances to add to.
1015 *
1016 * @return LY_ERR values.
1017 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001018static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001019parse_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 +02001020 uint32_t insubstmt_index, struct lysp_ext_instance **exts)
1021{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001022 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001023 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001024 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001025 struct lysp_ext_instance *e;
1026 enum yang_keyword kw;
1027
Radek Krejci2c4e7172018-10-19 15:56:26 +02001028 LY_ARRAY_NEW_RET(ctx->ctx, *exts, e, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001029
1030 /* store name and insubstmt info */
Radek Krejci44ceedc2018-10-02 15:54:31 +02001031 e->name = lydict_insert(ctx->ctx, ext_name, ext_name_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001032 e->insubstmt = insubstmt;
1033 e->insubstmt_index = insubstmt_index;
1034
1035 /* get optional argument */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001036 LY_CHECK_RET(get_argument(ctx, data, Y_MAYBE_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001037
Radek Krejci0ae092d2018-09-20 16:43:19 +02001038 if (word) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001039 INSERT_WORD(ctx, buf, e->argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001040 }
1041
Radek Krejci6d6556c2018-11-08 09:37:45 +01001042 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001043 LY_CHECK_RET(parse_ext_substmt(ctx, data, word, word_len, &e->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001044 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001045 return ret;
1046}
1047
Michal Vaskoea5abea2018-09-18 13:10:54 +02001048/**
1049 * @brief Parse a generic text field without specific constraints. Those are contact, organization,
1050 * description, etc...
1051 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001052 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001053 * @param[in,out] data Data to read from, always moved to currently handled character.
1054 * @param[in] substmt Type of this substatement.
1055 * @param[in] substmt_index Index of this substatement.
1056 * @param[in,out] value Place to store the parsed value.
1057 * @param[in] arg Type of the YANG keyword argument (of the value).
1058 * @param[in,out] exts Extension instances to add to.
1059 *
1060 * @return LY_ERR values.
1061 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001062static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001063parse_text_field(struct ly_parser_ctx *ctx, const char **data, LYEXT_SUBSTMT substmt, uint32_t substmt_index,
Michal Vasko7fbc8162018-09-17 10:35:16 +02001064 const char **value, enum yang_arg arg, struct lysp_ext_instance **exts)
1065{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001066 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001067 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001068 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001069 enum yang_keyword kw;
1070
1071 if (*value) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001072 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001073 return LY_EVALID;
1074 }
1075
1076 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001077 LY_CHECK_RET(get_argument(ctx, data, arg, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001078
1079 /* store value and spend buf if allocated */
Radek Krejci44ceedc2018-10-02 15:54:31 +02001080 INSERT_WORD(ctx, buf, *value, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001081
Radek Krejci6d6556c2018-11-08 09:37:45 +01001082 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001083 switch (kw) {
1084 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001085 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, substmt, substmt_index, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001086 break;
1087 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001088 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001089 return LY_EVALID;
1090 }
1091 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001092 return ret;
1093}
1094
Michal Vaskoea5abea2018-09-18 13:10:54 +02001095/**
1096 * @brief Parse the yang-version statement.
1097 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001098 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001099 * @param[in,out] data Data to read from, always moved to currently handled character.
1100 * @param[in] mod Module to store the parsed information in.
1101 *
1102 * @return LY_ERR values.
1103 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001104static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001105parse_yangversion(struct ly_parser_ctx *ctx, const char **data, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001106{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001107 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001108 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001109 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001110 enum yang_keyword kw;
1111
1112 if (mod->version) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001113 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001114 return LY_EVALID;
1115 }
1116
1117 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001118 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001119
1120 if ((word_len == 3) && !strncmp(word, "1.0", word_len)) {
1121 mod->version = LYS_VERSION_1_0;
1122 } else if ((word_len == 3) && !strncmp(word, "1.1", word_len)) {
1123 mod->version = LYS_VERSION_1_1;
1124 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001125 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001126 free(buf);
1127 return LY_EVALID;
1128 }
1129 free(buf);
1130
Radek Krejci6d6556c2018-11-08 09:37:45 +01001131 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001132 switch (kw) {
1133 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001134 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_VERSION, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001135 break;
1136 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001137 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001138 return LY_EVALID;
1139 }
1140 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001141 return ret;
1142}
1143
Michal Vaskoea5abea2018-09-18 13:10:54 +02001144/**
1145 * @brief Parse the belongs-to statement.
1146 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001147 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001148 * @param[in,out] data Data to read from, always moved to currently handled character.
1149 * @param[in,out] belongsto Place to store the parsed value.
1150 * @param[in,out] prefix Place to store the parsed belongs-to prefix value.
1151 * @param[in,out] exts Extension instances to add to.
1152 *
1153 * @return LY_ERR values.
1154 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001155static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001156parse_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 +02001157{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001158 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001159 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001160 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001161 enum yang_keyword kw;
1162
1163 if (*belongsto) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001164 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001165 return LY_EVALID;
1166 }
1167
1168 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001169 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001170
Radek Krejci44ceedc2018-10-02 15:54:31 +02001171 INSERT_WORD(ctx, buf, *belongsto, word, word_len);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001172 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001173 switch (kw) {
1174 case YANG_PREFIX:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001175 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_PREFIX, 0, prefix, Y_IDENTIF_ARG, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001176 break;
1177 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001178 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_BELONGSTO, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001179 break;
1180 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001181 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001182 return LY_EVALID;
1183 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001184 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001185 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001186checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001187 /* mandatory substatements */
1188 if (!*prefix) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001189 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001190 return LY_EVALID;
1191 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001192 return ret;
1193}
1194
Michal Vaskoea5abea2018-09-18 13:10:54 +02001195/**
1196 * @brief Parse the revision-date statement.
1197 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001198 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001199 * @param[in,out] data Data to read from, always moved to currently handled character.
1200 * @param[in,out] rev Array to store the parsed value in.
1201 * @param[in,out] exts Extension instances to add to.
1202 *
1203 * @return LY_ERR values.
1204 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001205static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001206parse_revisiondate(struct ly_parser_ctx *ctx, const char **data, char *rev, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001207{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001208 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001209 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001210 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001211 enum yang_keyword kw;
1212
1213 if (rev[0]) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001214 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001215 return LY_EVALID;
1216 }
1217
1218 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001219 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001220
1221 /* check value */
Radek Krejcibbe09a92018-11-08 09:36:54 +01001222 if (lysp_check_date(ctx, word, word_len, "revision-date")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001223 free(buf);
1224 return LY_EVALID;
1225 }
1226
1227 /* store value and spend buf if allocated */
1228 strncpy(rev, word, word_len);
1229 free(buf);
1230
Radek Krejci6d6556c2018-11-08 09:37:45 +01001231 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001232 switch (kw) {
1233 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001234 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_REVISIONDATE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001235 break;
1236 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001237 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001238 return LY_EVALID;
1239 }
1240 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001241 return ret;
1242}
1243
Michal Vaskoea5abea2018-09-18 13:10:54 +02001244/**
1245 * @brief Parse the include statement.
1246 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001247 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001248 * @param[in,out] data Data to read from, always moved to currently handled character.
1249 * @param[in,out] includes Parsed includes to add to.
1250 *
1251 * @return LY_ERR values.
1252 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001253static LY_ERR
Radek Krejcid33273d2018-10-25 14:55:52 +02001254parse_include(struct ly_parser_ctx *ctx, const char **data, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001255{
Radek Krejcid33273d2018-10-25 14:55:52 +02001256 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001257 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001258 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001259 enum yang_keyword kw;
1260 struct lysp_include *inc;
1261
Radek Krejcid33273d2018-10-25 14:55:52 +02001262 LY_ARRAY_NEW_RET(ctx->ctx, mod->includes, inc, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001263
1264 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001265 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001266
Radek Krejci086c7132018-10-26 15:29:04 +02001267 INSERT_WORD(ctx, buf, inc->name, word, word_len);
1268
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001269 /* submodules share the namespace with the module names, so there must not be
1270 * a module of the same name in the context, no need for revision matching */
1271 if (!strcmp(ctx->mod->name, inc->name) || ly_ctx_get_module_latest(ctx->ctx, inc->name)) {
1272 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Name collision between module and submodule of name \"%s\".", inc->name);
1273 return LY_EVALID;
1274 }
1275
Radek Krejci6d6556c2018-11-08 09:37:45 +01001276 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001277 switch (kw) {
1278 case YANG_DESCRIPTION:
Radek Krejci10113652018-11-14 16:56:50 +01001279 YANG_CHECK_STMTVER_RET(ctx, "description", "include");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001280 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 +02001281 break;
1282 case YANG_REFERENCE:
Radek Krejci10113652018-11-14 16:56:50 +01001283 YANG_CHECK_STMTVER_RET(ctx, "reference", "include");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001284 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 +02001285 break;
1286 case YANG_REVISION_DATE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001287 LY_CHECK_RET(parse_revisiondate(ctx, data, inc->rev, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001288 break;
1289 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001290 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001291 break;
1292 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001293 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001294 return LY_EVALID;
1295 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001296 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001297 return ret;
1298}
1299
Michal Vaskoea5abea2018-09-18 13:10:54 +02001300/**
1301 * @brief Parse the import statement.
1302 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001303 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001304 * @param[in,out] data Data to read from, always moved to currently handled character.
1305 * @param[in,out] imports Parsed imports to add to.
1306 *
1307 * @return LY_ERR values.
1308 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001309static LY_ERR
Radek Krejci70853c52018-10-15 14:46:16 +02001310parse_import(struct ly_parser_ctx *ctx, const char **data, struct lysp_module *module)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001311{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001312 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001313 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001314 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001315 enum yang_keyword kw;
1316 struct lysp_import *imp;
1317
Radek Krejci2c4e7172018-10-19 15:56:26 +02001318 LY_ARRAY_NEW_RET(ctx->ctx, module->imports, imp, LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001319
1320 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001321 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02001322 INSERT_WORD(ctx, buf, imp->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001323
Radek Krejci6d6556c2018-11-08 09:37:45 +01001324 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001325 switch (kw) {
1326 case YANG_PREFIX:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001327 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_PREFIX, 0, &imp->prefix, Y_IDENTIF_ARG, &imp->exts));
Radek Krejci70853c52018-10-15 14:46:16 +02001328 LY_CHECK_RET(lysp_check_prefix(ctx, module, &imp->prefix), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001329 break;
1330 case YANG_DESCRIPTION:
Radek Krejci10113652018-11-14 16:56:50 +01001331 YANG_CHECK_STMTVER_RET(ctx, "description", "import");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001332 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 +02001333 break;
1334 case YANG_REFERENCE:
Radek Krejci10113652018-11-14 16:56:50 +01001335 YANG_CHECK_STMTVER_RET(ctx, "reference", "import");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001336 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 +02001337 break;
1338 case YANG_REVISION_DATE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001339 LY_CHECK_RET(parse_revisiondate(ctx, data, imp->rev, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001340 break;
1341 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001342 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001343 break;
1344 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001345 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001346 return LY_EVALID;
1347 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001348 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001349 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001350checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001351 /* mandatory substatements */
Radek Krejci086c7132018-10-26 15:29:04 +02001352 LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001353
1354 return ret;
1355}
1356
Michal Vaskoea5abea2018-09-18 13:10:54 +02001357/**
1358 * @brief Parse the revision statement.
1359 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001360 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001361 * @param[in,out] data Data to read from, always moved to currently handled character.
1362 * @param[in,out] revs Parsed revisions to add to.
1363 *
1364 * @return LY_ERR values.
1365 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001366static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001367parse_revision(struct ly_parser_ctx *ctx, const char **data, struct lysp_revision **revs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001368{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001369 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001370 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001371 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001372 enum yang_keyword kw;
1373 struct lysp_revision *rev;
1374
Radek Krejci2c4e7172018-10-19 15:56:26 +02001375 LY_ARRAY_NEW_RET(ctx->ctx, *revs, rev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001376
1377 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001378 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001379
1380 /* check value */
Radek Krejcibbe09a92018-11-08 09:36:54 +01001381 if (lysp_check_date(ctx, word, word_len, "revision")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001382 return LY_EVALID;
1383 }
1384
Radek Krejcib7db73a2018-10-24 14:18:40 +02001385 strncpy(rev->date, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001386 free(buf);
1387
Radek Krejci6d6556c2018-11-08 09:37:45 +01001388 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001389 switch (kw) {
1390 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001391 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 +02001392 break;
1393 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001394 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 +02001395 break;
1396 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001397 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001398 break;
1399 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001400 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001401 return LY_EVALID;
1402 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001403 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001404 return ret;
1405}
1406
Michal Vaskoea5abea2018-09-18 13:10:54 +02001407/**
1408 * @brief Parse a generic text field that can have more instances such as base.
1409 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001410 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001411 * @param[in,out] data Data to read from, always moved to currently handled character.
1412 * @param[in] substmt Type of this substatement.
1413 * @param[in,out] texts Parsed values to add to.
1414 * @param[in] arg Type of the expected argument.
1415 * @param[in,out] exts Extension instances to add to.
1416 *
1417 * @return LY_ERR values.
1418 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001419static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001420parse_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 +02001421 struct lysp_ext_instance **exts)
1422{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001423 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001424 char *buf, *word;
Radek Krejci151a5b72018-10-19 14:21:44 +02001425 const char **item;
1426 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001427 enum yang_keyword kw;
1428
1429 /* allocate new pointer */
Radek Krejci2c4e7172018-10-19 15:56:26 +02001430 LY_ARRAY_NEW_RET(ctx->ctx, *texts, item, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001431
1432 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001433 LY_CHECK_RET(get_argument(ctx, data, arg, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001434
Radek Krejci151a5b72018-10-19 14:21:44 +02001435 INSERT_WORD(ctx, buf, *item, word, word_len);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001436 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001437 switch (kw) {
1438 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001439 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, substmt, LY_ARRAY_SIZE(*texts) - 1, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001440 break;
1441 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001442 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001443 return LY_EVALID;
1444 }
1445 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001446 return ret;
1447}
1448
Michal Vaskoea5abea2018-09-18 13:10:54 +02001449/**
1450 * @brief Parse the config statement.
1451 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001452 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001453 * @param[in,out] data Data to read from, always moved to currently handled character.
1454 * @param[in,out] flags Flags to add to.
1455 * @param[in,out] exts Extension instances to add to.
1456 *
1457 * @return LY_ERR values.
1458 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001459static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001460parse_config(struct ly_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001461{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001462 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001463 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001464 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001465 enum yang_keyword kw;
1466
1467 if (*flags & LYS_CONFIG_MASK) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001468 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001469 return LY_EVALID;
1470 }
1471
1472 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001473 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001474
1475 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1476 *flags |= LYS_CONFIG_W;
1477 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1478 *flags |= LYS_CONFIG_R;
1479 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001480 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001481 free(buf);
1482 return LY_EVALID;
1483 }
1484 free(buf);
1485
Radek Krejci6d6556c2018-11-08 09:37:45 +01001486 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001487 switch (kw) {
1488 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001489 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_CONFIG, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001490 break;
1491 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001492 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001493 return LY_EVALID;
1494 }
1495 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001496 return ret;
1497}
1498
Michal Vaskoea5abea2018-09-18 13:10:54 +02001499/**
1500 * @brief Parse the mandatory statement.
1501 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001502 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001503 * @param[in,out] data Data to read from, always moved to currently handled character.
1504 * @param[in,out] flags Flags to add to.
1505 * @param[in,out] exts Extension instances to add to.
1506 *
1507 * @return LY_ERR values.
1508 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001509static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001510parse_mandatory(struct ly_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001511{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001512 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001513 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001514 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001515 enum yang_keyword kw;
1516
1517 if (*flags & LYS_MAND_MASK) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001518 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001519 return LY_EVALID;
1520 }
1521
1522 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001523 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001524
1525 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1526 *flags |= LYS_MAND_TRUE;
1527 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1528 *flags |= LYS_MAND_FALSE;
1529 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001530 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001531 free(buf);
1532 return LY_EVALID;
1533 }
1534 free(buf);
1535
Radek Krejci6d6556c2018-11-08 09:37:45 +01001536 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001537 switch (kw) {
1538 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001539 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MANDATORY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001540 break;
1541 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001542 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001543 return LY_EVALID;
1544 }
1545 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001546 return ret;
1547}
1548
Michal Vaskoea5abea2018-09-18 13:10:54 +02001549/**
1550 * @brief Parse a restriction such as range or length.
1551 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001552 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001553 * @param[in,out] data Data to read from, always moved to currently handled character.
1554 * @param[in] restr_kw Type of this particular restriction.
1555 * @param[in,out] exts Extension instances to add to.
1556 *
1557 * @return LY_ERR values.
1558 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001559static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001560parse_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 +02001561{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001562 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001563 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001564 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001565 enum yang_keyword kw;
1566
1567 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001568 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001569
Radek Krejci44ceedc2018-10-02 15:54:31 +02001570 INSERT_WORD(ctx, buf, restr->arg, word, word_len);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001571 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001572 switch (kw) {
1573 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001574 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 +02001575 break;
1576 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001577 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 +02001578 break;
1579 case YANG_ERROR_APP_TAG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001580 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 +02001581 break;
1582 case YANG_ERROR_MESSAGE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001583 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 +02001584 break;
1585 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001586 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001587 break;
1588 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001589 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001590 return LY_EVALID;
1591 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001592 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001593 return ret;
1594}
1595
Michal Vaskoea5abea2018-09-18 13:10:54 +02001596/**
1597 * @brief Parse a restriction that can have more instances such as must.
1598 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001599 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001600 * @param[in,out] data Data to read from, always moved to currently handled character.
1601 * @param[in] restr_kw Type of this particular restriction.
1602 * @param[in,out] restrs Restrictions to add to.
1603 *
1604 * @return LY_ERR values.
1605 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001606static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001607parse_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 +02001608{
1609 struct lysp_restr *restr;
1610
Radek Krejci2c4e7172018-10-19 15:56:26 +02001611 LY_ARRAY_NEW_RET(ctx->ctx, *restrs, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001612 return parse_restr(ctx, data, restr_kw, restr);
1613}
1614
Michal Vaskoea5abea2018-09-18 13:10:54 +02001615/**
1616 * @brief Parse the status statement.
1617 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001618 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001619 * @param[in,out] data Data to read from, always moved to currently handled character.
1620 * @param[in,out] flags Flags to add to.
1621 * @param[in,out] exts Extension instances to add to.
1622 *
1623 * @return LY_ERR values.
1624 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001625static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001626parse_status(struct ly_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001627{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001628 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001629 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001630 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001631 enum yang_keyword kw;
1632
1633 if (*flags & LYS_STATUS_MASK) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001634 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001635 return LY_EVALID;
1636 }
1637
1638 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001639 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001640
1641 if ((word_len == 7) && !strncmp(word, "current", word_len)) {
1642 *flags |= LYS_STATUS_CURR;
1643 } else if ((word_len == 10) && !strncmp(word, "deprecated", word_len)) {
1644 *flags |= LYS_STATUS_DEPRC;
1645 } else if ((word_len == 8) && !strncmp(word, "obsolete", word_len)) {
1646 *flags |= LYS_STATUS_OBSLT;
1647 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001648 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001649 free(buf);
1650 return LY_EVALID;
1651 }
1652 free(buf);
1653
Radek Krejci6d6556c2018-11-08 09:37:45 +01001654 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001655 switch (kw) {
1656 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001657 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_STATUS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001658 break;
1659 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001660 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001661 return LY_EVALID;
1662 }
1663 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001664 return ret;
1665}
1666
Michal Vaskoea5abea2018-09-18 13:10:54 +02001667/**
1668 * @brief Parse the when statement.
1669 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001670 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001671 * @param[in,out] data Data to read from, always moved to currently handled character.
1672 * @param[in,out] when_p When pointer to parse to.
1673 *
1674 * @return LY_ERR values.
1675 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001676static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001677parse_when(struct ly_parser_ctx *ctx, const char **data, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001678{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001679 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001680 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001681 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001682 enum yang_keyword kw;
1683 struct lysp_when *when;
1684
1685 if (*when_p) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001686 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001687 return LY_EVALID;
1688 }
1689
1690 when = calloc(1, sizeof *when);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001691 LY_CHECK_ERR_RET(!when, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001692 *when_p = when;
1693
1694 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001695 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02001696 INSERT_WORD(ctx, buf, when->cond, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001697
Radek Krejci6d6556c2018-11-08 09:37:45 +01001698 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001699 switch (kw) {
1700 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001701 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 +02001702 break;
1703 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001704 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 +02001705 break;
1706 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001707 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001708 break;
1709 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001710 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001711 return LY_EVALID;
1712 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001713 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001714 return ret;
1715}
1716
Michal Vaskoea5abea2018-09-18 13:10:54 +02001717/**
1718 * @brief Parse the anydata or anyxml statement.
1719 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001720 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001721 * @param[in,out] data Data to read from, always moved to currently handled character.
1722 * @param[in] kw Type of this particular keyword.
1723 * @param[in,out] siblings Siblings to add to.
1724 *
1725 * @return LY_ERR values.
1726 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001727static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001728parse_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 +02001729{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001730 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001731 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001732 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001733 struct lysp_node *iter;
1734 struct lysp_node_anydata *any;
1735
1736 /* create structure */
1737 any = calloc(1, sizeof *any);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001738 LY_CHECK_ERR_RET(!any, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001739 any->nodetype = kw == YANG_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001740 any->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001741
1742 /* insert into siblings */
1743 if (!*siblings) {
1744 *siblings = (struct lysp_node *)any;
1745 } else {
1746 for (iter = *siblings; iter->next; iter = iter->next);
1747 iter->next = (struct lysp_node *)any;
1748 }
1749
1750 /* get name */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001751 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02001752 INSERT_WORD(ctx, buf, any->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001753
1754 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01001755 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001756 switch (kw) {
1757 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001758 LY_CHECK_RET(parse_config(ctx, data, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001759 break;
1760 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001761 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 +02001762 break;
1763 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001764 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 +02001765 break;
1766 case YANG_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001767 LY_CHECK_RET(parse_mandatory(ctx, data, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001768 break;
1769 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001770 LY_CHECK_RET(parse_restrs(ctx, data, kw, &any->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001771 break;
1772 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001773 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 +02001774 break;
1775 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001776 LY_CHECK_RET(parse_status(ctx, data, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001777 break;
1778 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001779 LY_CHECK_RET(parse_when(ctx, data, &any->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001780 break;
1781 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001782 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001783 break;
1784 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001785 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
Radek Krejcic59bc972018-09-17 16:13:06 +02001786 (any->nodetype & LYS_ANYDATA) == LYS_ANYDATA ? ly_stmt2str(YANG_ANYDATA) : ly_stmt2str(YANG_ANYXML));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001787 return LY_EVALID;
1788 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001789 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001790 return ret;
1791}
1792
Michal Vaskoea5abea2018-09-18 13:10:54 +02001793/**
1794 * @brief Parse the value or position statement. Substatement of type enum statement.
1795 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001796 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001797 * @param[in,out] data Data to read from, always moved to currently handled character.
1798 * @param[in] val_kw Type of this particular keyword.
1799 * @param[in,out] value Value to write to.
1800 * @param[in,out] flags Flags to write to.
1801 * @param[in,out] exts Extension instances to add to.
1802 *
1803 * @return LY_ERR values.
1804 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001805static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001806parse_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 +02001807 struct lysp_ext_instance **exts)
1808{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001809 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001810 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001811 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001812 long int num;
1813 unsigned long int unum;
1814 enum yang_keyword kw;
1815
1816 if (*flags & LYS_SET_VALUE) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001817 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001818 return LY_EVALID;
1819 }
1820 *flags |= LYS_SET_VALUE;
1821
1822 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001823 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001824
1825 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 +02001826 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001827 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001828 }
1829
1830 errno = 0;
1831 if (val_kw == YANG_VALUE) {
1832 num = strtol(word, &ptr, 10);
Radek Krejci8b764662018-11-14 14:15:13 +01001833 if (num < INT64_C(-2147483648) || num > INT64_C(2147483647)) {
1834 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
1835 goto error;
1836 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001837 } else {
1838 unum = strtoul(word, &ptr, 10);
Radek Krejci8b764662018-11-14 14:15:13 +01001839 if (unum > UINT64_C(4294967295)) {
1840 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
1841 goto error;
1842 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001843 }
1844 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001845 if ((size_t)(ptr - word) != word_len) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001846 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001847 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001848 }
1849 if (errno == ERANGE) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001850 LOGVAL_YANG(ctx, LY_VCODE_OOB, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001851 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001852 }
1853 if (val_kw == YANG_VALUE) {
1854 *value = num;
1855 } else {
1856 *value = unum;
1857 }
1858 free(buf);
1859
Radek Krejci6d6556c2018-11-08 09:37:45 +01001860 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001861 switch (kw) {
1862 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001863 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 +02001864 break;
1865 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001866 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001867 return LY_EVALID;
1868 }
1869 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001870 return ret;
Radek Krejci8b764662018-11-14 14:15:13 +01001871
1872error:
1873 free(buf);
1874 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001875}
1876
Michal Vaskoea5abea2018-09-18 13:10:54 +02001877/**
1878 * @brief Parse the enum or bit statement. Substatement of type statement.
1879 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001880 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001881 * @param[in,out] data Data to read from, always moved to currently handled character.
1882 * @param[in] enum_kw Type of this particular keyword.
1883 * @param[in,out] enums Enums or bits to add to.
1884 *
1885 * @return LY_ERR values.
1886 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001887static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001888parse_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 +02001889{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001890 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001891 char *buf, *word;
Radek Krejci8b764662018-11-14 14:15:13 +01001892 size_t word_len, u;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001893 enum yang_keyword kw;
1894 struct lysp_type_enum *enm;
1895
Radek Krejci2c4e7172018-10-19 15:56:26 +02001896 LY_ARRAY_NEW_RET(ctx->ctx, *enums, enm, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001897
1898 /* get value */
Radek Krejci8b764662018-11-14 14:15:13 +01001899 LY_CHECK_RET(get_argument(ctx, data, enum_kw == YANG_ENUM ? Y_STR_ARG : Y_IDENTIF_ARG, &word, &buf, &word_len));
1900 if (enum_kw == YANG_ENUM) {
1901 if (!word_len) {
1902 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Enum name must not be zero-length.");
1903 free(buf);
1904 return LY_EVALID;
1905 } else if (isspace(word[0]) || isspace(word[word_len - 1])) {
1906 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Enum name must not have any leading or trailing whitespaces (\"%.*s\").",
1907 word_len, word);
1908 free(buf);
1909 return LY_EVALID;
1910 } else {
1911 for (u = 0; u < word_len; ++u) {
1912 if (iscntrl(word[u])) {
1913 LOGWRN(ctx->ctx, "Control characters in enum name should be avoided (\"%.*s\", character number %d).",
1914 word_len, word, u + 1);
1915 break;
1916 }
1917 }
1918 }
1919 } else { /* YANG_BIT */
1920
1921 }
Radek Krejci44ceedc2018-10-02 15:54:31 +02001922 INSERT_WORD(ctx, buf, enm->name, word, word_len);
Radek Krejci8b764662018-11-14 14:15:13 +01001923 CHECK_UNIQUENESS(ctx, *enums, name, ly_stmt2str(enum_kw), enm->name);
1924
Radek Krejci6d6556c2018-11-08 09:37:45 +01001925 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001926 switch (kw) {
1927 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001928 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 +02001929 break;
1930 case YANG_IF_FEATURE:
Radek Krejci10113652018-11-14 16:56:50 +01001931 YANG_CHECK_STMTVER_RET(ctx, "if-feature", ly_stmt2str(enum_kw));
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001932 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 +02001933 break;
1934 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001935 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 +02001936 break;
1937 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001938 LY_CHECK_RET(parse_status(ctx, data, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001939 break;
1940 case YANG_VALUE:
1941 case YANG_POSITION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001942 LY_CHECK_RET(parse_type_enum_value_pos(ctx, data, kw, &enm->value, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001943 break;
1944 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001945 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001946 break;
1947 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001948 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001949 return LY_EVALID;
1950 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001951 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001952 return ret;
1953}
1954
Michal Vaskoea5abea2018-09-18 13:10:54 +02001955/**
1956 * @brief Parse the fraction-digits statement. Substatement of type statement.
1957 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001958 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001959 * @param[in,out] data Data to read from, always moved to currently handled character.
1960 * @param[in,out] fracdig Value to write to.
1961 * @param[in,out] exts Extension instances to add to.
1962 *
1963 * @return LY_ERR values.
1964 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001965static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001966parse_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 +02001967{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001968 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001969 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001970 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001971 unsigned long int num;
1972 enum yang_keyword kw;
1973
1974 if (*fracdig) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001975 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001976 return LY_EVALID;
1977 }
1978
1979 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001980 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001981
1982 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001983 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001984 free(buf);
1985 return LY_EVALID;
1986 }
1987
1988 errno = 0;
1989 num = strtoul(word, &ptr, 10);
1990 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001991 if ((size_t)(ptr - word) != word_len) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001992 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001993 free(buf);
1994 return LY_EVALID;
1995 }
1996 if ((errno == ERANGE) || (num > 18)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001997 LOGVAL_YANG(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001998 free(buf);
1999 return LY_EVALID;
2000 }
2001 *fracdig = num;
2002 free(buf);
2003
Radek Krejci6d6556c2018-11-08 09:37:45 +01002004 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002005 switch (kw) {
2006 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002007 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_FRACDIGITS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002008 break;
2009 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002010 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002011 return LY_EVALID;
2012 }
2013 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002014 return ret;
2015}
2016
Michal Vaskoea5abea2018-09-18 13:10:54 +02002017/**
2018 * @brief Parse the require-instance statement. Substatement of type statement.
2019 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002020 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002021 * @param[in,out] data Data to read from, always moved to currently handled character.
2022 * @param[in,out] reqinst Value to write to.
2023 * @param[in,out] flags Flags to write to.
2024 * @param[in,out] exts Extension instances to add to.
2025 *
2026 * @return LY_ERR values.
2027 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002028static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002029parse_type_reqinstance(struct ly_parser_ctx *ctx, const char **data, uint8_t *reqinst, uint16_t *flags,
Michal Vasko7fbc8162018-09-17 10:35:16 +02002030 struct lysp_ext_instance **exts)
2031{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002032 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002033 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002034 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002035 enum yang_keyword kw;
2036
2037 if (*flags & LYS_SET_REQINST) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002038 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002039 return LY_EVALID;
2040 }
2041 *flags |= LYS_SET_REQINST;
2042
2043 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002044 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002045
2046 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
2047 *reqinst = 1;
2048 } else if ((word_len != 5) || strncmp(word, "false", word_len)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002049 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002050 free(buf);
2051 return LY_EVALID;
2052 }
2053 free(buf);
2054
Radek Krejci6d6556c2018-11-08 09:37:45 +01002055 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002056 switch (kw) {
2057 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002058 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_REQINSTANCE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002059 break;
2060 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002061 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002062 return LY_EVALID;
2063 }
2064 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002065 return ret;
2066}
2067
Michal Vaskoea5abea2018-09-18 13:10:54 +02002068/**
2069 * @brief Parse the modifier statement. Substatement of type pattern statement.
2070 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002071 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002072 * @param[in,out] data Data to read from, always moved to currently handled character.
2073 * @param[in,out] pat Value to write to.
2074 * @param[in,out] exts Extension instances to add to.
2075 *
2076 * @return LY_ERR values.
2077 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002078static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002079parse_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 +02002080{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002081 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002082 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002083 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002084 enum yang_keyword kw;
2085
2086 if ((*pat)[0] == 0x15) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002087 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002088 return LY_EVALID;
2089 }
2090
2091 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002092 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002093
2094 if ((word_len != 12) || strncmp(word, "invert-match", word_len)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002095 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002096 free(buf);
2097 return LY_EVALID;
2098 }
2099 free(buf);
2100
2101 /* replace the value in the dictionary */
2102 buf = malloc(strlen(*pat) + 1);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002103 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002104 strcpy(buf, *pat);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002105 lydict_remove(ctx->ctx, *pat);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002106
2107 assert(buf[0] == 0x06);
2108 buf[0] = 0x15;
Radek Krejci44ceedc2018-10-02 15:54:31 +02002109 *pat = lydict_insert_zc(ctx->ctx, buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002110
Radek Krejci6d6556c2018-11-08 09:37:45 +01002111 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002112 switch (kw) {
2113 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002114 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MODIFIER, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002115 break;
2116 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002117 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002118 return LY_EVALID;
2119 }
2120 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002121 return ret;
2122}
2123
Michal Vaskoea5abea2018-09-18 13:10:54 +02002124/**
2125 * @brief Parse the pattern statement. Substatement of type statement.
2126 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002127 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002128 * @param[in,out] data Data to read from, always moved to currently handled character.
2129 * @param[in,out] patterns Restrictions to add to.
2130 *
2131 * @return LY_ERR values.
2132 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002133static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002134parse_type_pattern(struct ly_parser_ctx *ctx, const char **data, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002135{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002136 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002137 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002138 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002139 enum yang_keyword kw;
2140 struct lysp_restr *restr;
2141
Radek Krejci2c4e7172018-10-19 15:56:26 +02002142 LY_ARRAY_NEW_RET(ctx->ctx, *patterns, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002143
2144 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002145 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002146
2147 /* add special meaning first byte */
2148 if (buf) {
2149 buf = realloc(buf, word_len + 2);
2150 word = buf;
2151 } else {
2152 buf = malloc(word_len + 2);
2153 }
Radek Krejci44ceedc2018-10-02 15:54:31 +02002154 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejci86d106e2018-10-18 09:53:19 +02002155 memmove(buf + 1, word, word_len);
2156 buf[0] = 0x06; /* pattern's default regular-match flag */
2157 buf[word_len + 1] = '\0'; /* terminating NULL byte */
2158 restr->arg = lydict_insert_zc(ctx->ctx, buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002159
Radek Krejci6d6556c2018-11-08 09:37:45 +01002160 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002161 switch (kw) {
2162 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002163 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 +02002164 break;
2165 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002166 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 +02002167 break;
2168 case YANG_ERROR_APP_TAG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002169 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 +02002170 break;
2171 case YANG_ERROR_MESSAGE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002172 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 +02002173 break;
2174 case YANG_MODIFIER:
Radek Krejci10113652018-11-14 16:56:50 +01002175 YANG_CHECK_STMTVER_RET(ctx, "modifier", "pattern");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002176 LY_CHECK_RET(parse_type_pattern_modifier(ctx, data, &restr->arg, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002177 break;
2178 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002179 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002180 break;
2181 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002182 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002183 return LY_EVALID;
2184 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002185 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002186 return ret;
2187}
2188
Michal Vaskoea5abea2018-09-18 13:10:54 +02002189/**
2190 * @brief Parse the type statement.
2191 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002192 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002193 * @param[in,out] data Data to read from, always moved to currently handled character.
2194 * @param[in,out] type Type to wrote to.
2195 *
2196 * @return LY_ERR values.
2197 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002198static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002199parse_type(struct ly_parser_ctx *ctx, const char **data, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002200{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002201 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002202 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002203 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002204 enum yang_keyword kw;
2205 struct lysp_type *nest_type;
2206
2207 if (type->name) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002208 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002209 return LY_EVALID;
2210 }
2211
2212 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002213 LY_CHECK_RET(get_argument(ctx, data, Y_PREF_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002214 INSERT_WORD(ctx, buf, type->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002215
Radek Krejci6d6556c2018-11-08 09:37:45 +01002216 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002217 switch (kw) {
2218 case YANG_BASE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002219 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 +01002220 type->flags |= LYS_SET_BASE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002221 break;
2222 case YANG_BIT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002223 LY_CHECK_RET(parse_type_enum(ctx, data, kw, &type->bits));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002224 type->flags |= LYS_SET_BIT;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002225 break;
2226 case YANG_ENUM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002227 LY_CHECK_RET(parse_type_enum(ctx, data, kw, &type->enums));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002228 type->flags |= LYS_SET_ENUM;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002229 break;
2230 case YANG_FRACTION_DIGITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002231 LY_CHECK_RET(parse_type_fracdigits(ctx, data, &type->fraction_digits, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002232 type->flags |= LYS_SET_FRDIGITS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002233 break;
2234 case YANG_LENGTH:
2235 if (type->length) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002236 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002237 return LY_EVALID;
2238 }
2239 type->length = calloc(1, sizeof *type->length);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002240 LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002241
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002242 LY_CHECK_RET(parse_restr(ctx, data, kw, type->length));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002243 type->flags |= LYS_SET_LENGTH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002244 break;
2245 case YANG_PATH:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002246 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 +01002247 type->flags |= LYS_SET_PATH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002248 break;
2249 case YANG_PATTERN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002250 LY_CHECK_RET(parse_type_pattern(ctx, data, &type->patterns));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002251 type->flags |= LYS_SET_PATTERN;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002252 break;
2253 case YANG_RANGE:
2254 if (type->range) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002255 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002256 return LY_EVALID;
2257 }
2258 type->range = calloc(1, sizeof *type->range);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002259 LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx->ctx), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002260
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002261 LY_CHECK_RET(parse_restr(ctx, data, kw, type->range));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002262 type->flags |= LYS_SET_RANGE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002263 break;
2264 case YANG_REQUIRE_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002265 LY_CHECK_RET(parse_type_reqinstance(ctx, data, &type->require_instance, &type->flags, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002266 /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002267 break;
2268 case YANG_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002269 LY_ARRAY_NEW_RET(ctx->ctx, type->types, nest_type, LY_EMEM);
2270 LY_CHECK_RET(parse_type(ctx, data, nest_type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002271 type->flags |= LYS_SET_TYPE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002272 break;
2273 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002274 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &type->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002275 break;
2276 default:
Radek Krejci8b764662018-11-14 14:15:13 +01002277 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002278 return LY_EVALID;
2279 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002280 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002281 return ret;
2282}
2283
Michal Vaskoea5abea2018-09-18 13:10:54 +02002284/**
2285 * @brief Parse the leaf statement.
2286 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002287 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002288 * @param[in,out] data Data to read from, always moved to currently handled character.
2289 * @param[in,out] siblings Siblings to add to.
2290 *
2291 * @return LY_ERR values.
2292 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002293static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002294parse_leaf(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002295{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002296 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002297 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002298 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002299 enum yang_keyword kw;
2300 struct lysp_node *iter;
2301 struct lysp_node_leaf *leaf;
2302
2303 /* create structure */
2304 leaf = calloc(1, sizeof *leaf);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002305 LY_CHECK_ERR_RET(!leaf, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002306 leaf->nodetype = LYS_LEAF;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002307 leaf->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002308
2309 /* insert into siblings */
2310 if (!*siblings) {
2311 *siblings = (struct lysp_node *)leaf;
2312 } else {
2313 for (iter = *siblings; iter->next; iter = iter->next);
2314 iter->next = (struct lysp_node *)leaf;
2315 }
2316
2317 /* get name */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002318 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002319 INSERT_WORD(ctx, buf, leaf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002320
2321 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01002322 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002323 switch (kw) {
2324 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002325 LY_CHECK_RET(parse_config(ctx, data, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002326 break;
2327 case YANG_DEFAULT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002328 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 +02002329 break;
2330 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002331 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 +02002332 break;
2333 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002334 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 +02002335 break;
2336 case YANG_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002337 LY_CHECK_RET(parse_mandatory(ctx, data, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002338 break;
2339 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002340 LY_CHECK_RET(parse_restrs(ctx, data, kw, &leaf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002341 break;
2342 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002343 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 +02002344 break;
2345 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002346 LY_CHECK_RET(parse_status(ctx, data, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002347 break;
2348 case YANG_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002349 LY_CHECK_RET(parse_type(ctx, data, &leaf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002350 break;
2351 case YANG_UNITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002352 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 +02002353 break;
2354 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002355 LY_CHECK_RET(parse_when(ctx, data, &leaf->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002356 break;
2357 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002358 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002359 break;
2360 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002361 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002362 return LY_EVALID;
2363 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002364 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002365 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002366checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002367 /* mandatory substatements */
2368 if (!leaf->type.name) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002369 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002370 return LY_EVALID;
2371 }
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002372 if ((leaf->flags & LYS_MAND_TRUE) && (leaf->dflt)) {
2373 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMSCOMB, "mandatory", "default", "leaf");
2374 return LY_EVALID;
2375 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002376
2377 return ret;
2378}
2379
Michal Vaskoea5abea2018-09-18 13:10:54 +02002380/**
2381 * @brief Parse the max-elements statement.
2382 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002383 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002384 * @param[in,out] data Data to read from, always moved to currently handled character.
2385 * @param[in,out] max Value to write to.
2386 * @param[in,out] flags Flags to write to.
2387 * @param[in,out] exts Extension instances to add to.
2388 *
2389 * @return LY_ERR values.
2390 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002391static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002392parse_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 +02002393{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002394 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002395 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002396 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002397 unsigned long int num;
2398 enum yang_keyword kw;
2399
2400 if (*flags & LYS_SET_MAX) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002401 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002402 return LY_EVALID;
2403 }
2404 *flags |= LYS_SET_MAX;
2405
2406 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002407 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002408
2409 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002410 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002411 free(buf);
2412 return LY_EVALID;
2413 }
2414
2415 if (strncmp(word, "unbounded", word_len)) {
2416 errno = 0;
2417 num = strtoul(word, &ptr, 10);
2418 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002419 if ((size_t)(ptr - word) != word_len) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002420 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002421 free(buf);
2422 return LY_EVALID;
2423 }
2424 if ((errno == ERANGE) || (num > UINT32_MAX)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002425 LOGVAL_YANG(ctx, LY_VCODE_OOB, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002426 free(buf);
2427 return LY_EVALID;
2428 }
2429
2430 *max = num;
2431 }
2432 free(buf);
2433
Radek Krejci6d6556c2018-11-08 09:37:45 +01002434 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002435 switch (kw) {
2436 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002437 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MAX, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002438 break;
2439 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002440 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002441 return LY_EVALID;
2442 }
2443 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002444 return ret;
2445}
2446
Michal Vaskoea5abea2018-09-18 13:10:54 +02002447/**
2448 * @brief Parse the min-elements statement.
2449 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002450 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002451 * @param[in,out] data Data to read from, always moved to currently handled character.
2452 * @param[in,out] min Value to write to.
2453 * @param[in,out] flags Flags to write to.
2454 * @param[in,out] exts Extension instances to add to.
2455 *
2456 * @return LY_ERR values.
2457 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002458static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002459parse_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 +02002460{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002461 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002462 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002463 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002464 unsigned long int num;
2465 enum yang_keyword kw;
2466
2467 if (*flags & LYS_SET_MIN) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002468 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002469 return LY_EVALID;
2470 }
2471 *flags |= LYS_SET_MIN;
2472
2473 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002474 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002475
2476 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002477 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002478 free(buf);
2479 return LY_EVALID;
2480 }
2481
2482 errno = 0;
2483 num = strtoul(word, &ptr, 10);
2484 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002485 if ((size_t)(ptr - word) != word_len) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002486 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002487 free(buf);
2488 return LY_EVALID;
2489 }
2490 if ((errno == ERANGE) || (num > UINT32_MAX)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002491 LOGVAL_YANG(ctx, LY_VCODE_OOB, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002492 free(buf);
2493 return LY_EVALID;
2494 }
2495 *min = num;
2496 free(buf);
2497
Radek Krejci6d6556c2018-11-08 09:37:45 +01002498 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002499 switch (kw) {
2500 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002501 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MIN, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002502 break;
2503 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002504 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002505 return LY_EVALID;
2506 }
2507 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002508 return ret;
2509}
2510
Michal Vaskoea5abea2018-09-18 13:10:54 +02002511/**
2512 * @brief Parse the ordered-by statement.
2513 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002514 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002515 * @param[in,out] data Data to read from, always moved to currently handled character.
2516 * @param[in,out] flags Flags to write to.
2517 * @param[in,out] exts Extension instances to add to.
2518 *
2519 * @return LY_ERR values.
2520 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002521static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002522parse_orderedby(struct ly_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002523{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002524 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002525 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002526 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002527 enum yang_keyword kw;
2528
2529 if (*flags & LYS_ORDBY_MASK) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002530 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002531 return LY_EVALID;
2532 }
2533
2534 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002535 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002536
2537 if ((word_len == 6) && !strncmp(word, "system", word_len)) {
2538 *flags |= LYS_ORDBY_SYSTEM;
2539 } else if ((word_len == 4) && !strncmp(word, "user", word_len)) {
2540 *flags |= LYS_ORDBY_USER;
2541 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002542 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002543 free(buf);
2544 return LY_EVALID;
2545 }
2546 free(buf);
2547
Radek Krejci6d6556c2018-11-08 09:37:45 +01002548 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002549 switch (kw) {
2550 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002551 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_ORDEREDBY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002552 break;
2553 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002554 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002555 return LY_EVALID;
2556 }
2557 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002558 return ret;
2559}
2560
Michal Vaskoea5abea2018-09-18 13:10:54 +02002561/**
2562 * @brief Parse the leaf-list statement.
2563 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002564 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002565 * @param[in,out] data Data to read from, always moved to currently handled character.
2566 * @param[in,out] siblings Siblings to add to.
2567 *
2568 * @return LY_ERR values.
2569 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002570static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002571parse_leaflist(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002572{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002573 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002574 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002575 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002576 enum yang_keyword kw;
2577 struct lysp_node *iter;
2578 struct lysp_node_leaflist *llist;
2579
2580 /* create structure */
2581 llist = calloc(1, sizeof *llist);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002582 LY_CHECK_ERR_RET(!llist, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002583 llist->nodetype = LYS_LEAFLIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002584 llist->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002585
2586 /* insert into siblings */
2587 if (!*siblings) {
2588 *siblings = (struct lysp_node *)llist;
2589 } else {
2590 for (iter = *siblings; iter->next; iter = iter->next);
2591 iter->next = (struct lysp_node *)llist;
2592 }
2593
2594 /* get name */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002595 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002596 INSERT_WORD(ctx, buf, llist->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002597
2598 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01002599 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002600 switch (kw) {
2601 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002602 LY_CHECK_RET(parse_config(ctx, data, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002603 break;
2604 case YANG_DEFAULT:
Radek Krejci10113652018-11-14 16:56:50 +01002605 YANG_CHECK_STMTVER_RET(ctx, "default", "leaf-list");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002606 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 +02002607 break;
2608 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002609 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 +02002610 break;
2611 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002612 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 +02002613 break;
2614 case YANG_MAX_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002615 LY_CHECK_RET(parse_maxelements(ctx, data, &llist->max, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002616 break;
2617 case YANG_MIN_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002618 LY_CHECK_RET(parse_minelements(ctx, data, &llist->min, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002619 break;
2620 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002621 LY_CHECK_RET(parse_restrs(ctx, data, kw, &llist->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002622 break;
2623 case YANG_ORDERED_BY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002624 LY_CHECK_RET(parse_orderedby(ctx, data, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002625 break;
2626 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002627 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 +02002628 break;
2629 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002630 LY_CHECK_RET(parse_status(ctx, data, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002631 break;
2632 case YANG_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002633 LY_CHECK_RET(parse_type(ctx, data, &llist->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002634 break;
2635 case YANG_UNITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002636 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 +02002637 break;
2638 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002639 LY_CHECK_RET(parse_when(ctx, data, &llist->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002640 break;
2641 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002642 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002643 break;
2644 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002645 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002646 return LY_EVALID;
2647 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002648 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002649 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002650checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002651 /* mandatory substatements */
2652 if (!llist->type.name) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002653 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002654 return LY_EVALID;
2655 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01002656 if ((llist->min) && (llist->dflts)) {
2657 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMSCOMB, "min-elements", "default", "leaf-list");
2658 return LY_EVALID;
2659 }
Radek Krejcidf6cad12018-11-28 17:10:55 +01002660 if (llist->max && llist->min > llist->max) {
2661 LOGVAL_YANG(ctx, LYVE_SEMANTICS,
2662 "Invalid combination of min-elements and max-elements: min value %u is bigger than the max value %u.",
2663 llist->min, llist->max);
2664 return LY_EVALID;
2665 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002666
2667 return ret;
2668}
2669
Michal Vaskoea5abea2018-09-18 13:10:54 +02002670/**
2671 * @brief Parse the refine statement.
2672 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002673 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002674 * @param[in,out] data Data to read from, always moved to currently handled character.
2675 * @param[in,out] refines Refines to add to.
2676 *
2677 * @return LY_ERR values.
2678 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002679static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002680parse_refine(struct ly_parser_ctx *ctx, const char **data, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002681{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002682 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002683 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002684 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002685 enum yang_keyword kw;
2686 struct lysp_refine *rf;
2687
Radek Krejci2c4e7172018-10-19 15:56:26 +02002688 LY_ARRAY_NEW_RET(ctx->ctx, *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002689
2690 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002691 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002692 INSERT_WORD(ctx, buf, rf->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002693
Radek Krejci6d6556c2018-11-08 09:37:45 +01002694 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002695 switch (kw) {
2696 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002697 LY_CHECK_RET(parse_config(ctx, data, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002698 break;
2699 case YANG_DEFAULT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002700 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 +02002701 break;
2702 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002703 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 +02002704 break;
2705 case YANG_IF_FEATURE:
Radek Krejci10113652018-11-14 16:56:50 +01002706 YANG_CHECK_STMTVER_RET(ctx, "if-feature", "refine");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002707 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 +02002708 break;
2709 case YANG_MAX_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002710 LY_CHECK_RET(parse_maxelements(ctx, data, &rf->max, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002711 break;
2712 case YANG_MIN_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002713 LY_CHECK_RET(parse_minelements(ctx, data, &rf->min, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002714 break;
2715 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002716 LY_CHECK_RET(parse_restrs(ctx, data, kw, &rf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002717 break;
2718 case YANG_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002719 LY_CHECK_RET(parse_mandatory(ctx, data, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002720 break;
2721 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002722 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 +02002723 break;
2724 case YANG_PRESENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002725 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 +02002726 break;
2727 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002728 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002729 break;
2730 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002731 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002732 return LY_EVALID;
2733 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002734 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002735 return ret;
2736}
2737
Michal Vaskoea5abea2018-09-18 13:10:54 +02002738/**
2739 * @brief Parse the typedef statement.
2740 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002741 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002742 * @param[in,out] data Data to read from, always moved to currently handled character.
2743 * @param[in,out] typedefs Typedefs to add to.
2744 *
2745 * @return LY_ERR values.
2746 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002747static LY_ERR
Radek Krejcibbe09a92018-11-08 09:36:54 +01002748parse_typedef(struct ly_parser_ctx *ctx, struct lysp_node *parent, const char **data, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002749{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002750 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002751 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002752 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002753 enum yang_keyword kw;
2754 struct lysp_tpdf *tpdf;
2755
Radek Krejci2c4e7172018-10-19 15:56:26 +02002756 LY_ARRAY_NEW_RET(ctx->ctx, *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002757
2758 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002759 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002760 INSERT_WORD(ctx, buf, tpdf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002761
2762 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01002763 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002764 switch (kw) {
2765 case YANG_DEFAULT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002766 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 +02002767 break;
2768 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002769 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 +02002770 break;
2771 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002772 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 +02002773 break;
2774 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002775 LY_CHECK_RET(parse_status(ctx, data, &tpdf->flags, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002776 break;
2777 case YANG_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002778 LY_CHECK_RET(parse_type(ctx, data, &tpdf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002779 break;
2780 case YANG_UNITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002781 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 +02002782 break;
2783 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002784 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002785 break;
2786 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002787 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002788 return LY_EVALID;
2789 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002790 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002791 LY_CHECK_RET(ret);
Radek Krejcibbe09a92018-11-08 09:36:54 +01002792checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002793 /* mandatory substatements */
2794 if (!tpdf->type.name) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002795 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002796 return LY_EVALID;
2797 }
2798
Radek Krejcibbe09a92018-11-08 09:36:54 +01002799 /* store data for collision check */
2800 if (parent) {
2801 ly_set_add(&ctx->tpdfs_nodes, parent, 0);
2802 }
2803
Michal Vasko7fbc8162018-09-17 10:35:16 +02002804 return ret;
2805}
2806
Michal Vaskoea5abea2018-09-18 13:10:54 +02002807/**
2808 * @brief Parse the input or output statement.
2809 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002810 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002811 * @param[in,out] data Data to read from, always moved to currently handled character.
2812 * @param[in] kw Type of this particular keyword
2813 * @param[in,out] inout_p Input/output pointer to write to.
2814 *
2815 * @return LY_ERR values.
2816 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002817static LY_ERR
Radek Krejci10113652018-11-14 16:56:50 +01002818parse_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 +02002819{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002820 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002821 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002822 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002823 struct lysp_action_inout *inout;
Radek Krejci10113652018-11-14 16:56:50 +01002824 enum yang_keyword kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002825
2826 if (*inout_p) {
Radek Krejci10113652018-11-14 16:56:50 +01002827 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002828 return LY_EVALID;
2829 }
2830
2831 /* create structure */
2832 inout = calloc(1, sizeof *inout);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002833 LY_CHECK_ERR_RET(!inout, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002834 *inout_p = inout;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002835 inout->nodetype = LYS_INOUT;
2836 inout->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002837
2838 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01002839 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002840 switch (kw) {
2841 case YANG_ANYDATA:
Radek Krejci10113652018-11-14 16:56:50 +01002842 YANG_CHECK_STMTVER_RET(ctx, "anydata", ly_stmt2str(inout_kw));
2843 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002844 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002845 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)inout, &inout->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002846 break;
2847 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002848 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)inout, &inout->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002849 break;
2850 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002851 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)inout, &inout->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002852 break;
2853 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002854 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)inout, &inout->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002855 break;
2856 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002857 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)inout, &inout->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002858 break;
2859 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002860 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)inout, &inout->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002861 break;
2862 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002863 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)inout, &inout->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002864 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002865 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01002866 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)inout, data, &inout->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002867 break;
2868 case YANG_MUST:
Radek Krejci10113652018-11-14 16:56:50 +01002869 YANG_CHECK_STMTVER_RET(ctx, "must", ly_stmt2str(inout_kw));
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002870 LY_CHECK_RET(parse_restrs(ctx, data, kw, &inout->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002871 break;
2872 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002873 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)inout, &inout->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002874 break;
2875 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002876 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inout->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002877 break;
2878 default:
Radek Krejci10113652018-11-14 16:56:50 +01002879 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002880 return LY_EVALID;
2881 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002882 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002883 return ret;
2884}
2885
Michal Vaskoea5abea2018-09-18 13:10:54 +02002886/**
2887 * @brief Parse the action statement.
2888 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002889 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002890 * @param[in,out] data Data to read from, always moved to currently handled character.
2891 * @param[in,out] actions Actions to add to.
2892 *
2893 * @return LY_ERR values.
2894 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002895static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002896parse_action(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002897{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002898 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002899 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002900 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002901 enum yang_keyword kw;
2902 struct lysp_action *act;
2903
Radek Krejci2c4e7172018-10-19 15:56:26 +02002904 LY_ARRAY_NEW_RET(ctx->ctx, *actions, act, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002905
2906 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002907 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002908 INSERT_WORD(ctx, buf, act->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002909 act->nodetype = LYS_ACTION;
2910 act->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002911
Radek Krejci6d6556c2018-11-08 09:37:45 +01002912 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002913 switch (kw) {
2914 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002915 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 +02002916 break;
2917 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002918 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 +02002919 break;
2920 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002921 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 +02002922 break;
2923 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002924 LY_CHECK_RET(parse_status(ctx, data, &act->flags, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002925 break;
2926
2927 case YANG_INPUT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002928 LY_CHECK_RET(parse_inout(ctx, data, kw, (struct lysp_node*)act, &act->input));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002929 break;
2930 case YANG_OUTPUT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002931 LY_CHECK_RET(parse_inout(ctx, data, kw, (struct lysp_node*)act, &act->output));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002932 break;
2933
2934 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01002935 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)act, data, &act->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002936 break;
2937 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002938 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)act, &act->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002939 break;
2940 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002941 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002942 break;
2943 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002944 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "action");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002945 return LY_EVALID;
2946 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002947 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002948 return ret;
2949}
2950
Michal Vaskoea5abea2018-09-18 13:10:54 +02002951/**
2952 * @brief Parse the notification statement.
2953 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002954 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002955 * @param[in,out] data Data to read from, always moved to currently handled character.
2956 * @param[in,out] notifs Notifications to add to.
2957 *
2958 * @return LY_ERR values.
2959 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002960static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002961parse_notif(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002962{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002963 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002964 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002965 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002966 enum yang_keyword kw;
2967 struct lysp_notif *notif;
2968
Radek Krejci2c4e7172018-10-19 15:56:26 +02002969 LY_ARRAY_NEW_RET(ctx->ctx, *notifs, notif, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002970
2971 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002972 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002973 INSERT_WORD(ctx, buf, notif->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002974 notif->nodetype = LYS_NOTIF;
2975 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002976
Radek Krejci6d6556c2018-11-08 09:37:45 +01002977 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002978 switch (kw) {
2979 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002980 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 +02002981 break;
2982 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002983 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 +02002984 break;
2985 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002986 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 +02002987 break;
2988 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002989 LY_CHECK_RET(parse_status(ctx, data, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002990 break;
2991
2992 case YANG_ANYDATA:
Radek Krejci10113652018-11-14 16:56:50 +01002993 YANG_CHECK_STMTVER_RET(ctx, "anydata", "notification");
2994 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002995 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002996 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002997 break;
2998 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002999 LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003000 break;
3001 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003002 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003003 break;
3004 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003005 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003006 break;
3007 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003008 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003009 break;
3010 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003011 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003012 break;
3013 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003014 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003015 break;
3016
3017 case YANG_MUST:
Radek Krejci10113652018-11-14 16:56:50 +01003018 YANG_CHECK_STMTVER_RET(ctx, "must", "notification");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003019 LY_CHECK_RET(parse_restrs(ctx, data, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003020 break;
3021 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01003022 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)notif, data, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003023 break;
3024 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003025 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003026 break;
3027 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003028 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003029 break;
3030 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003031 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003032 return LY_EVALID;
3033 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003034 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003035 return ret;
3036}
3037
Michal Vaskoea5abea2018-09-18 13:10:54 +02003038/**
3039 * @brief Parse the grouping statement.
3040 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003041 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003042 * @param[in,out] data Data to read from, always moved to currently handled character.
3043 * @param[in,out] groupings Groupings to add to.
3044 *
3045 * @return LY_ERR values.
3046 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003047static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003048parse_grouping(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003049{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003050 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003051 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003052 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003053 enum yang_keyword kw;
3054 struct lysp_grp *grp;
3055
Radek Krejci2c4e7172018-10-19 15:56:26 +02003056 LY_ARRAY_NEW_RET(ctx->ctx, *groupings, grp, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003057
3058 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003059 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003060 INSERT_WORD(ctx, buf, grp->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003061 grp->nodetype = LYS_GROUPING;
3062 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003063
Radek Krejci6d6556c2018-11-08 09:37:45 +01003064 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003065 switch (kw) {
3066 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003067 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 +02003068 break;
3069 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003070 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &grp->ref, Y_STR_ARG, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003071 break;
3072 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003073 LY_CHECK_RET(parse_status(ctx, data, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003074 break;
3075
3076 case YANG_ANYDATA:
Radek Krejci10113652018-11-14 16:56:50 +01003077 YANG_CHECK_STMTVER_RET(ctx, "anydata", "grouping");
3078 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003079 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003080 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003081 break;
3082 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003083 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003084 break;
3085 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003086 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003087 break;
3088 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003089 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003090 break;
3091 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003092 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003093 break;
3094 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003095 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003096 break;
3097 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003098 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003099 break;
3100
3101 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01003102 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)grp, data, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003103 break;
3104 case YANG_ACTION:
Radek Krejci10113652018-11-14 16:56:50 +01003105 YANG_CHECK_STMTVER_RET(ctx, "action", "grouping");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003106 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)grp, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003107 break;
3108 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003109 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)grp, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003110 break;
3111 case YANG_NOTIFICATION:
Radek Krejci10113652018-11-14 16:56:50 +01003112 YANG_CHECK_STMTVER_RET(ctx, "notification", "grouping");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003113 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)grp, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003114 break;
3115 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003116 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003117 break;
3118 default:
Radek Krejci10113652018-11-14 16:56:50 +01003119 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003120 return LY_EVALID;
3121 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003122 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003123 return ret;
3124}
3125
Michal Vaskoea5abea2018-09-18 13:10:54 +02003126/**
3127 * @brief Parse the refine statement.
3128 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003129 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003130 * @param[in,out] data Data to read from, always moved to currently handled character.
3131 * @param[in,out] augments Augments to add to.
3132 *
3133 * @return LY_ERR values.
3134 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003135static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003136parse_augment(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003137{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003138 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003139 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003140 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003141 enum yang_keyword kw;
3142 struct lysp_augment *aug;
3143
Radek Krejci2c4e7172018-10-19 15:56:26 +02003144 LY_ARRAY_NEW_RET(ctx->ctx, *augments, aug, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003145
3146 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003147 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003148 INSERT_WORD(ctx, buf, aug->nodeid, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003149 aug->nodetype = LYS_AUGMENT;
3150 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003151
Radek Krejci6d6556c2018-11-08 09:37:45 +01003152 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003153 switch (kw) {
3154 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003155 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 +02003156 break;
3157 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003158 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 +02003159 break;
3160 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003161 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 +02003162 break;
3163 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003164 LY_CHECK_RET(parse_status(ctx, data, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003165 break;
3166 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003167 LY_CHECK_RET(parse_when(ctx, data, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003168 break;
3169
3170 case YANG_ANYDATA:
Radek Krejci10113652018-11-14 16:56:50 +01003171 YANG_CHECK_STMTVER_RET(ctx, "anydata", "augment");
3172 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003173 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003174 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003175 break;
3176 case YANG_CASE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003177 LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003178 break;
3179 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003180 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003181 break;
3182 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003183 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003184 break;
3185 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003186 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003187 break;
3188 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003189 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003190 break;
3191 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003192 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003193 break;
3194 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003195 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003196 break;
3197
3198 case YANG_ACTION:
Radek Krejci10113652018-11-14 16:56:50 +01003199 YANG_CHECK_STMTVER_RET(ctx, "action", "augment");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003200 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003201 break;
3202 case YANG_NOTIFICATION:
Radek Krejci10113652018-11-14 16:56:50 +01003203 YANG_CHECK_STMTVER_RET(ctx, "notification", "augment");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003204 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003205 break;
3206 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003207 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003208 break;
3209 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003210 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003211 return LY_EVALID;
3212 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003213 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003214 return ret;
3215}
3216
Michal Vaskoea5abea2018-09-18 13:10:54 +02003217/**
3218 * @brief Parse the uses statement.
3219 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003220 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003221 * @param[in,out] data Data to read from, always moved to currently handled character.
3222 * @param[in,out] siblings Siblings to add to.
3223 *
3224 * @return LY_ERR values.
3225 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003226static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003227parse_uses(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003228{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003229 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003230 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003231 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003232 enum yang_keyword kw;
3233 struct lysp_node *iter;
3234 struct lysp_node_uses *uses;
3235
3236 /* create structure */
3237 uses = calloc(1, sizeof *uses);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003238 LY_CHECK_ERR_RET(!uses, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003239 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003240 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003241
3242 /* insert into siblings */
3243 if (!*siblings) {
3244 *siblings = (struct lysp_node *)uses;
3245 } else {
3246 for (iter = *siblings; iter->next; iter = iter->next);
3247 iter->next = (struct lysp_node *)uses;
3248 }
3249
3250 /* get name */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003251 LY_CHECK_RET(get_argument(ctx, data, Y_PREF_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003252 INSERT_WORD(ctx, buf, uses->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003253
3254 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01003255 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003256 switch (kw) {
3257 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003258 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 +02003259 break;
3260 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003261 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 +02003262 break;
3263 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003264 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 +02003265 break;
3266 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003267 LY_CHECK_RET(parse_status(ctx, data, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003268 break;
3269 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003270 LY_CHECK_RET(parse_when(ctx, data, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003271 break;
3272
3273 case YANG_REFINE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003274 LY_CHECK_RET(parse_refine(ctx, data, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003275 break;
3276 case YANG_AUGMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003277 LY_CHECK_RET(parse_augment(ctx, data, (struct lysp_node*)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003278 break;
3279 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003280 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003281 break;
3282 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003283 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003284 return LY_EVALID;
3285 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003286 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003287 return ret;
3288}
3289
Michal Vaskoea5abea2018-09-18 13:10:54 +02003290/**
3291 * @brief Parse the case statement.
3292 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003293 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003294 * @param[in,out] data Data to read from, always moved to currently handled character.
3295 * @param[in,out] siblings Siblings to add to.
3296 *
3297 * @return LY_ERR values.
3298 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003299static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003300parse_case(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003301{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003302 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003303 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003304 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003305 enum yang_keyword kw;
3306 struct lysp_node *iter;
3307 struct lysp_node_case *cas;
3308
3309 /* create structure */
3310 cas = calloc(1, sizeof *cas);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003311 LY_CHECK_ERR_RET(!cas, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003312 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003313 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003314
3315 /* insert into siblings */
3316 if (!*siblings) {
3317 *siblings = (struct lysp_node *)cas;
3318 } else {
3319 for (iter = *siblings; iter->next; iter = iter->next);
3320 iter->next = (struct lysp_node *)cas;
3321 }
3322
3323 /* get name */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003324 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003325 INSERT_WORD(ctx, buf, cas->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003326
3327 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01003328 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003329 switch (kw) {
3330 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003331 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 +02003332 break;
3333 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003334 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 +02003335 break;
3336 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003337 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 +02003338 break;
3339 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003340 LY_CHECK_RET(parse_status(ctx, data, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003341 break;
3342 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003343 LY_CHECK_RET(parse_when(ctx, data, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003344 break;
3345
3346 case YANG_ANYDATA:
Radek Krejci10113652018-11-14 16:56:50 +01003347 YANG_CHECK_STMTVER_RET(ctx, "anydata", "case");
3348 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003349 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003350 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003351 break;
3352 case YANG_CHOICE:
Radek Krejcia9026eb2018-12-12 16:04:47 +01003353 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003354 break;
3355 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003356 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003357 break;
3358 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003359 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003360 break;
3361 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003362 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003363 break;
3364 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003365 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003366 break;
3367 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003368 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003369 break;
3370 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003371 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003372 break;
3373 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003374 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003375 return LY_EVALID;
3376 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003377 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003378 return ret;
3379}
3380
Michal Vaskoea5abea2018-09-18 13:10:54 +02003381/**
3382 * @brief Parse the choice statement.
3383 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003384 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003385 * @param[in,out] data Data to read from, always moved to currently handled character.
3386 * @param[in,out] siblings Siblings to add to.
3387 *
3388 * @return LY_ERR values.
3389 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003390static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003391parse_choice(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003392{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003393 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003394 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003395 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003396 enum yang_keyword kw;
3397 struct lysp_node *iter;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003398 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003399
3400 /* create structure */
Radek Krejci44ceedc2018-10-02 15:54:31 +02003401 choice = calloc(1, sizeof *choice);
3402 LY_CHECK_ERR_RET(!choice, LOGMEM(ctx->ctx), LY_EMEM);
3403 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003404 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003405
3406 /* insert into siblings */
3407 if (!*siblings) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02003408 *siblings = (struct lysp_node *)choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003409 } else {
3410 for (iter = *siblings; iter->next; iter = iter->next);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003411 iter->next = (struct lysp_node *)choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003412 }
3413
3414 /* get name */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003415 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003416 INSERT_WORD(ctx, buf, choice->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003417
3418 /* parse substatements */
Radek Krejcia9026eb2018-12-12 16:04:47 +01003419 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003420 switch (kw) {
3421 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003422 LY_CHECK_RET(parse_config(ctx, data, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003423 break;
3424 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003425 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 +02003426 break;
3427 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003428 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 +02003429 break;
3430 case YANG_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003431 LY_CHECK_RET(parse_mandatory(ctx, data, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003432 break;
3433 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003434 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 +02003435 break;
3436 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003437 LY_CHECK_RET(parse_status(ctx, data, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003438 break;
3439 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003440 LY_CHECK_RET(parse_when(ctx, data, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003441 break;
3442 case YANG_DEFAULT:
Radek Krejcia9026eb2018-12-12 16:04:47 +01003443 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 +02003444 break;
3445
3446 case YANG_ANYDATA:
Radek Krejci10113652018-11-14 16:56:50 +01003447 YANG_CHECK_STMTVER_RET(ctx, "anydata", "choice");
3448 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003449 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003450 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003451 break;
3452 case YANG_CASE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003453 LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003454 break;
3455 case YANG_CHOICE:
Radek Krejci10113652018-11-14 16:56:50 +01003456 YANG_CHECK_STMTVER_RET(ctx, "choice", "choice");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003457 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003458 break;
3459 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003460 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003461 break;
3462 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003463 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003464 break;
3465 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003466 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003467 break;
3468 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003469 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003470 break;
3471 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003472 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003473 break;
3474 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003475 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003476 return LY_EVALID;
3477 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003478 }
Radek Krejcia9026eb2018-12-12 16:04:47 +01003479 LY_CHECK_RET(ret);
3480checks:
3481 if ((choice->flags & LYS_MAND_TRUE) && choice->dflt) {
3482 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMSCOMB, "mandatory", "default", "choice");
3483 return LY_EVALID;
3484 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003485 return ret;
3486}
3487
Michal Vaskoea5abea2018-09-18 13:10:54 +02003488/**
3489 * @brief Parse the container statement.
3490 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003491 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003492 * @param[in,out] data Data to read from, always moved to currently handled character.
3493 * @param[in,out] siblings Siblings to add to.
3494 *
3495 * @return LY_ERR values.
3496 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003497static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003498parse_container(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003499{
3500 LY_ERR ret = 0;
3501 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003502 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003503 enum yang_keyword kw;
3504 struct lysp_node *iter;
3505 struct lysp_node_container *cont;
3506
3507 /* create structure */
3508 cont = calloc(1, sizeof *cont);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003509 LY_CHECK_ERR_RET(!cont, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003510 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003511 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003512
3513 /* insert into siblings */
3514 if (!*siblings) {
3515 *siblings = (struct lysp_node *)cont;
3516 } else {
3517 for (iter = *siblings; iter->next; iter = iter->next);
3518 iter->next = (struct lysp_node *)cont;
3519 }
3520
3521 /* get name */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003522 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003523 INSERT_WORD(ctx, buf, cont->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003524
3525 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01003526 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003527 switch (kw) {
3528 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003529 LY_CHECK_RET(parse_config(ctx, data, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003530 break;
3531 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003532 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 +02003533 break;
3534 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003535 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 +02003536 break;
3537 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003538 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 +02003539 break;
3540 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003541 LY_CHECK_RET(parse_status(ctx, data, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003542 break;
3543 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003544 LY_CHECK_RET(parse_when(ctx, data, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003545 break;
3546 case YANG_PRESENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003547 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 +02003548 break;
3549
3550 case YANG_ANYDATA:
Radek Krejci10113652018-11-14 16:56:50 +01003551 YANG_CHECK_STMTVER_RET(ctx, "anydata", "container");
3552 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003553 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003554 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003555 break;
3556 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003557 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003558 break;
3559 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003560 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003561 break;
3562 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003563 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003564 break;
3565 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003566 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003567 break;
3568 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003569 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003570 break;
3571 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003572 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003573 break;
3574
3575 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01003576 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)cont, data, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003577 break;
3578 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003579 LY_CHECK_RET(parse_restrs(ctx, data, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003580 break;
3581 case YANG_ACTION:
Radek Krejci10113652018-11-14 16:56:50 +01003582 YANG_CHECK_STMTVER_RET(ctx, "action", "container");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003583 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003584 break;
3585 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003586 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003587 break;
3588 case YANG_NOTIFICATION:
Radek Krejci10113652018-11-14 16:56:50 +01003589 YANG_CHECK_STMTVER_RET(ctx, "notification", "container");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003590 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003591 break;
3592 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003593 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003594 break;
3595 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003596 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003597 return LY_EVALID;
3598 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003599 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003600 return ret;
3601}
3602
Michal Vaskoea5abea2018-09-18 13:10:54 +02003603/**
3604 * @brief Parse the list statement.
3605 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003606 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003607 * @param[in,out] data Data to read from, always moved to currently handled character.
3608 * @param[in,out] siblings Siblings to add to.
3609 *
3610 * @return LY_ERR values.
3611 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003612static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003613parse_list(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003614{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003615 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003616 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003617 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003618 enum yang_keyword kw;
3619 struct lysp_node *iter;
3620 struct lysp_node_list *list;
3621
3622 /* create structure */
3623 list = calloc(1, sizeof *list);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003624 LY_CHECK_ERR_RET(!list, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003625 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003626 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003627
3628 /* insert into siblings */
3629 if (!*siblings) {
3630 *siblings = (struct lysp_node *)list;
3631 } else {
3632 for (iter = *siblings; iter->next; iter = iter->next);
3633 iter->next = (struct lysp_node *)list;
3634 }
3635
3636 /* get name */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003637 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003638 INSERT_WORD(ctx, buf, list->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003639
3640 /* parse substatements */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003641 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003642 switch (kw) {
3643 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003644 LY_CHECK_RET(parse_config(ctx, data, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003645 break;
3646 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003647 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 +02003648 break;
3649 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003650 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 +02003651 break;
3652 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003653 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 +02003654 break;
3655 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003656 LY_CHECK_RET(parse_status(ctx, data, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003657 break;
3658 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003659 LY_CHECK_RET(parse_when(ctx, data, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003660 break;
3661 case YANG_KEY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003662 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 +02003663 break;
3664 case YANG_MAX_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003665 LY_CHECK_RET(parse_maxelements(ctx, data, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003666 break;
3667 case YANG_MIN_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003668 LY_CHECK_RET(parse_minelements(ctx, data, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003669 break;
3670 case YANG_ORDERED_BY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003671 LY_CHECK_RET(parse_orderedby(ctx, data, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003672 break;
3673 case YANG_UNIQUE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003674 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 +02003675 break;
3676
3677 case YANG_ANYDATA:
Radek Krejci10113652018-11-14 16:56:50 +01003678 YANG_CHECK_STMTVER_RET(ctx, "anydata", "list");
3679 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003680 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003681 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003682 break;
3683 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003684 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003685 break;
3686 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003687 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003688 break;
3689 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003690 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003691 break;
3692 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003693 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003694 break;
3695 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003696 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003697 break;
3698 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003699 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003700 break;
3701
3702 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01003703 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)list, data, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003704 break;
3705 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003706 LY_CHECK_RET(parse_restrs(ctx, data, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003707 break;
3708 case YANG_ACTION:
Radek Krejci10113652018-11-14 16:56:50 +01003709 YANG_CHECK_STMTVER_RET(ctx, "action", "list");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003710 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003711 break;
3712 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003713 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003714 break;
3715 case YANG_NOTIFICATION:
Radek Krejci10113652018-11-14 16:56:50 +01003716 YANG_CHECK_STMTVER_RET(ctx, "notification", "list");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003717 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003718 break;
3719 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003720 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003721 break;
3722 default:
Radek Krejci10113652018-11-14 16:56:50 +01003723 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003724 return LY_EVALID;
3725 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003726 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003727 LY_CHECK_RET(ret);
3728checks:
3729 if (list->max && list->min > list->max) {
3730 LOGVAL_YANG(ctx, LYVE_SEMANTICS,
3731 "Invalid combination of min-elements and max-elements: min value %u is bigger than the max value %u.",
3732 list->min, list->max);
3733 return LY_EVALID;
3734 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003735
3736 return ret;
3737}
3738
Michal Vaskoea5abea2018-09-18 13:10:54 +02003739/**
3740 * @brief Parse the yin-element statement.
3741 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003742 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003743 * @param[in,out] data Data to read from, always moved to currently handled character.
3744 * @param[in,out] flags Flags to write to.
3745 * @param[in,out] exts Extension instances to add to.
3746 *
3747 * @return LY_ERR values.
3748 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003749static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003750parse_yinelement(struct ly_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003751{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003752 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003753 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003754 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003755 enum yang_keyword kw;
3756
3757 if (*flags & LYS_YINELEM_MASK) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02003758 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003759 return LY_EVALID;
3760 }
3761
3762 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003763 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003764
3765 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
3766 *flags |= LYS_YINELEM_TRUE;
3767 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
3768 *flags |= LYS_YINELEM_FALSE;
3769 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02003770 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003771 free(buf);
3772 return LY_EVALID;
3773 }
3774 free(buf);
3775
Radek Krejci6d6556c2018-11-08 09:37:45 +01003776 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003777 switch (kw) {
3778 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003779 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_YINELEM, 0, exts));
3780 LY_CHECK_RET(ret); break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003781 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003782 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003783 return LY_EVALID;
3784 }
3785 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003786 return ret;
3787}
3788
Michal Vaskoea5abea2018-09-18 13:10:54 +02003789/**
3790 * @brief Parse the yin-element statement.
3791 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003792 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003793 * @param[in,out] data Data to read from, always moved to currently handled character.
3794 * @param[in,out] argument Value to write to.
3795 * @param[in,out] flags Flags to write to.
3796 * @param[in,out] exts Extension instances to add to.
3797 *
3798 * @return LY_ERR values.
3799 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003800static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003801parse_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 +02003802{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003803 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003804 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003805 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003806 enum yang_keyword kw;
3807
3808 if (*argument) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02003809 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003810 return LY_EVALID;
3811 }
3812
3813 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003814 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003815 INSERT_WORD(ctx, buf, *argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003816
Radek Krejci6d6556c2018-11-08 09:37:45 +01003817 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003818 switch (kw) {
3819 case YANG_YIN_ELEMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003820 LY_CHECK_RET(parse_yinelement(ctx, data, flags, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003821 break;
3822 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003823 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_ARGUMENT, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003824 break;
3825 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003826 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003827 return LY_EVALID;
3828 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003829 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003830 return ret;
3831}
3832
Michal Vaskoea5abea2018-09-18 13:10:54 +02003833/**
3834 * @brief Parse the extension statement.
3835 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003836 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003837 * @param[in,out] data Data to read from, always moved to currently handled character.
3838 * @param[in,out] extensions Extensions to add to.
3839 *
3840 * @return LY_ERR values.
3841 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003842static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003843parse_extension(struct ly_parser_ctx *ctx, const char **data, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003844{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003845 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003846 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003847 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003848 enum yang_keyword kw;
3849 struct lysp_ext *ex;
3850
Radek Krejci2c4e7172018-10-19 15:56:26 +02003851 LY_ARRAY_NEW_RET(ctx->ctx, *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003852
3853 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003854 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003855 INSERT_WORD(ctx, buf, ex->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003856
Radek Krejci6d6556c2018-11-08 09:37:45 +01003857 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003858 switch (kw) {
3859 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003860 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 +02003861 break;
3862 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003863 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 +02003864 break;
3865 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003866 LY_CHECK_RET(parse_status(ctx, data, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003867 break;
3868 case YANG_ARGUMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003869 LY_CHECK_RET(parse_argument(ctx, data, &ex->argument, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003870 break;
3871 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003872 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003873 break;
3874 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003875 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003876 return LY_EVALID;
3877 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003878 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003879 return ret;
3880}
3881
Michal Vaskoea5abea2018-09-18 13:10:54 +02003882/**
3883 * @brief Parse the deviate statement.
3884 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003885 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003886 * @param[in,out] data Data to read from, always moved to currently handled character.
3887 * @param[in,out] deviates Deviates to add to.
3888 *
3889 * @return LY_ERR values.
3890 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003891static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003892parse_deviate(struct ly_parser_ctx *ctx, const char **data, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003893{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003894 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003895 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003896 size_t word_len, dev_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003897 enum yang_keyword kw;
3898 struct lysp_deviate *iter, *d;
3899 struct lysp_deviate_add *d_add = NULL;
3900 struct lysp_deviate_rpl *d_rpl = NULL;
3901 struct lysp_deviate_del *d_del = NULL;
3902 const char **d_units, ***d_uniques, ***d_dflts;
3903 struct lysp_restr **d_musts;
3904 uint16_t *d_flags;
3905 uint32_t *d_min, *d_max;
3906
3907 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003908 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003909
3910 if ((word_len == 13) && !strncmp(word, "not-supported", word_len)) {
3911 dev_mod = LYS_DEV_NOT_SUPPORTED;
3912 } else if ((word_len == 3) && !strncmp(word, "add", word_len)) {
3913 dev_mod = LYS_DEV_ADD;
3914 } else if ((word_len == 7) && !strncmp(word, "replace", word_len)) {
3915 dev_mod = LYS_DEV_REPLACE;
3916 } else if ((word_len == 6) && !strncmp(word, "delete", word_len)) {
3917 dev_mod = LYS_DEV_DELETE;
3918 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02003919 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003920 free(buf);
3921 return LY_EVALID;
3922 }
3923 free(buf);
3924
3925 /* create structure */
3926 switch (dev_mod) {
3927 case LYS_DEV_NOT_SUPPORTED:
3928 d = calloc(1, sizeof *d);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003929 LY_CHECK_ERR_RET(!d, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003930 break;
3931 case LYS_DEV_ADD:
3932 d_add = calloc(1, sizeof *d_add);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003933 LY_CHECK_ERR_RET(!d_add, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003934 d = (struct lysp_deviate *)d_add;
3935 d_units = &d_add->units;
3936 d_uniques = &d_add->uniques;
3937 d_dflts = &d_add->dflts;
3938 d_musts = &d_add->musts;
3939 d_flags = &d_add->flags;
3940 d_min = &d_add->min;
3941 d_max = &d_add->max;
3942 break;
3943 case LYS_DEV_REPLACE:
3944 d_rpl = calloc(1, sizeof *d_rpl);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003945 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003946 d = (struct lysp_deviate *)d_rpl;
3947 d_units = &d_rpl->units;
3948 d_flags = &d_rpl->flags;
3949 d_min = &d_rpl->min;
3950 d_max = &d_rpl->max;
3951 break;
3952 case LYS_DEV_DELETE:
3953 d_del = calloc(1, sizeof *d_del);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003954 LY_CHECK_ERR_RET(!d_del, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003955 d = (struct lysp_deviate *)d_del;
3956 d_units = &d_del->units;
3957 d_uniques = &d_del->uniques;
3958 d_dflts = &d_del->dflts;
3959 d_musts = &d_del->musts;
3960 d_flags = &d_del->flags;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003961 break;
3962 default:
3963 assert(0);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003964 LOGINT_RET(ctx->ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003965 }
3966 d->mod = dev_mod;
3967
3968 /* insert into siblings */
3969 if (!*deviates) {
3970 *deviates = d;
3971 } else {
3972 for (iter = *deviates; iter->next; iter = iter->next);
3973 iter->next = d;
3974 }
3975
Radek Krejci6d6556c2018-11-08 09:37:45 +01003976 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003977 switch (kw) {
3978 case YANG_CONFIG:
3979 switch (dev_mod) {
3980 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003981 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003982 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003983 return LY_EVALID;
3984 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003985 LY_CHECK_RET(parse_config(ctx, data, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003986 break;
3987 }
3988 break;
3989 case YANG_DEFAULT:
3990 switch (dev_mod) {
3991 case LYS_DEV_NOT_SUPPORTED:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003992 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003993 return LY_EVALID;
3994 case LYS_DEV_REPLACE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003995 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 +02003996 break;
3997 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003998 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 +02003999 break;
4000 }
4001 break;
4002 case YANG_MANDATORY:
4003 switch (dev_mod) {
4004 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004005 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004006 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004007 return LY_EVALID;
4008 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004009 LY_CHECK_RET(parse_mandatory(ctx, data, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004010 break;
4011 }
4012 break;
4013 case YANG_MAX_ELEMENTS:
4014 switch (dev_mod) {
4015 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004016 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004017 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004018 return LY_EVALID;
4019 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004020 LY_CHECK_RET(parse_maxelements(ctx, data, d_max, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004021 break;
4022 }
4023 break;
4024 case YANG_MIN_ELEMENTS:
4025 switch (dev_mod) {
4026 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004027 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004028 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004029 return LY_EVALID;
4030 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004031 LY_CHECK_RET(parse_minelements(ctx, data, d_min, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004032 break;
4033 }
4034 break;
4035 case YANG_MUST:
4036 switch (dev_mod) {
4037 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004038 case LYS_DEV_REPLACE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004039 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004040 return LY_EVALID;
4041 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004042 LY_CHECK_RET(parse_restrs(ctx, data, kw, d_musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004043 break;
4044 }
4045 break;
4046 case YANG_TYPE:
4047 switch (dev_mod) {
4048 case LYS_DEV_NOT_SUPPORTED:
4049 case LYS_DEV_ADD:
4050 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004051 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004052 return LY_EVALID;
4053 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004054 if (d_rpl->type) {
4055 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
4056 return LY_EVALID;
4057 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004058 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004059 LY_CHECK_ERR_RET(!d_rpl->type, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004060 LY_CHECK_RET(parse_type(ctx, data, d_rpl->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004061 break;
4062 }
4063 break;
4064 case YANG_UNIQUE:
4065 switch (dev_mod) {
4066 case LYS_DEV_NOT_SUPPORTED:
4067 case LYS_DEV_REPLACE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004068 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004069 return LY_EVALID;
4070 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004071 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 +02004072 break;
4073 }
4074 break;
4075 case YANG_UNITS:
4076 switch (dev_mod) {
4077 case LYS_DEV_NOT_SUPPORTED:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004078 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004079 return LY_EVALID;
4080 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004081 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 +02004082 break;
4083 }
4084 break;
4085 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004086 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004087 break;
4088 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004089 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004090 return LY_EVALID;
4091 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004092 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004093 return ret;
4094}
4095
Michal Vaskoea5abea2018-09-18 13:10:54 +02004096/**
4097 * @brief Parse the deviation statement.
4098 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004099 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004100 * @param[in,out] data Data to read from, always moved to currently handled character.
4101 * @param[in,out] deviations Deviations to add to.
4102 *
4103 * @return LY_ERR values.
4104 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004105static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02004106parse_deviation(struct ly_parser_ctx *ctx, const char **data, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004107{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004108 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004109 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004110 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004111 enum yang_keyword kw;
4112 struct lysp_deviation *dev;
4113
Radek Krejci2c4e7172018-10-19 15:56:26 +02004114 LY_ARRAY_NEW_RET(ctx->ctx, *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004115
4116 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004117 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02004118 INSERT_WORD(ctx, buf, dev->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004119
Radek Krejci6d6556c2018-11-08 09:37:45 +01004120 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004121 switch (kw) {
4122 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004123 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 +02004124 break;
4125 case YANG_DEVIATE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004126 LY_CHECK_RET(parse_deviate(ctx, data, &dev->deviates));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004127 break;
4128 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004129 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 +02004130 break;
4131 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004132 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004133 break;
4134 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004135 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004136 return LY_EVALID;
4137 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004138 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004139 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01004140checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004141 /* mandatory substatements */
4142 if (!dev->deviates) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004143 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004144 return LY_EVALID;
4145 }
4146
4147 return ret;
4148}
4149
Michal Vaskoea5abea2018-09-18 13:10:54 +02004150/**
4151 * @brief Parse the feature statement.
4152 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004153 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004154 * @param[in,out] data Data to read from, always moved to currently handled character.
4155 * @param[in,out] features Features to add to.
4156 *
4157 * @return LY_ERR values.
4158 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004159static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02004160parse_feature(struct ly_parser_ctx *ctx, const char **data, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004161{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004162 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004163 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004164 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004165 enum yang_keyword kw;
4166 struct lysp_feature *feat;
4167
Radek Krejci2c4e7172018-10-19 15:56:26 +02004168 LY_ARRAY_NEW_RET(ctx->ctx, *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004169
4170 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004171 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02004172 INSERT_WORD(ctx, buf, feat->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004173
Radek Krejci6d6556c2018-11-08 09:37:45 +01004174 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004175 switch (kw) {
4176 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004177 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 +02004178 break;
4179 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004180 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 +02004181 break;
4182 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004183 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 +02004184 break;
4185 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004186 LY_CHECK_RET(parse_status(ctx, data, &feat->flags, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004187 break;
4188 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004189 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004190 break;
4191 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004192 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004193 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004194 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004195 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004196 return ret;
4197}
4198
Michal Vaskoea5abea2018-09-18 13:10:54 +02004199/**
4200 * @brief Parse the identity statement.
4201 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004202 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004203 * @param[in,out] data Data to read from, always moved to currently handled character.
4204 * @param[in,out] identities Identities to add to.
4205 *
4206 * @return LY_ERR values.
4207 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004208static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02004209parse_identity(struct ly_parser_ctx *ctx, const char **data, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004210{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004211 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004212 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004213 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004214 enum yang_keyword kw;
4215 struct lysp_ident *ident;
4216
Radek Krejci2c4e7172018-10-19 15:56:26 +02004217 LY_ARRAY_NEW_RET(ctx->ctx, *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004218
4219 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004220 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02004221 INSERT_WORD(ctx, buf, ident->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004222
Radek Krejci6d6556c2018-11-08 09:37:45 +01004223 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004224 switch (kw) {
4225 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004226 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 +02004227 break;
4228 case YANG_IF_FEATURE:
Radek Krejci10113652018-11-14 16:56:50 +01004229 YANG_CHECK_STMTVER_RET(ctx, "if-feature", "identity");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004230 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 +02004231 break;
4232 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004233 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 +02004234 break;
4235 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004236 LY_CHECK_RET(parse_status(ctx, data, &ident->flags, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004237 break;
4238 case YANG_BASE:
Radek Krejci10113652018-11-14 16:56:50 +01004239 if (ident->bases && ctx->mod->version < 2) {
4240 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Identity can be derived from multiple base identities only in YANG 1.1 modules");
4241 return LY_EVALID;
4242 }
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004243 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 +02004244 break;
4245 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004246 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004247 break;
4248 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004249 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004250 return LY_EVALID;
4251 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004252 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004253 return ret;
4254}
4255
Michal Vaskoea5abea2018-09-18 13:10:54 +02004256/**
4257 * @brief Parse the module or submodule statement.
4258 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004259 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004260 * @param[in,out] data Data to read from, always moved to currently handled character.
4261 * @param[in,out] mod Module to write to.
4262 *
4263 * @return LY_ERR values.
4264 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004265static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02004266parse_sub_module(struct ly_parser_ctx *ctx, const char **data, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004267{
4268 LY_ERR ret = 0;
4269 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004270 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004271 enum yang_keyword kw, prev_kw = 0;
4272 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Radek Krejcie9e987e2018-10-31 12:50:27 +01004273 struct lysp_module *dup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004274
4275 /* (sub)module name */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004276 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02004277 INSERT_WORD(ctx, buf, mod->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004278
Radek Krejci6d6556c2018-11-08 09:37:45 +01004279 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004280
Radek Krejcie3846472018-10-15 15:24:51 +02004281#define CHECK_ORDER(SECTION) \
4282 if (mod_stmt > SECTION) {LOGVAL_YANG(ctx, LY_VCODE_INORD, ly_stmt2str(kw), ly_stmt2str(prev_kw)); return LY_EVALID;}mod_stmt = SECTION
4283
Michal Vasko7fbc8162018-09-17 10:35:16 +02004284 switch (kw) {
4285 /* module header */
4286 case YANG_NAMESPACE:
4287 case YANG_PREFIX:
4288 if (mod->submodule) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004289 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004290 return LY_EVALID;
4291 }
Radek Krejcie3846472018-10-15 15:24:51 +02004292 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4293 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004294 case YANG_BELONGS_TO:
Radek Krejcie3846472018-10-15 15:24:51 +02004295 if (!mod->submodule) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004296 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004297 return LY_EVALID;
4298 }
Radek Krejcie3846472018-10-15 15:24:51 +02004299 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4300 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004301 case YANG_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004302 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004303 break;
4304 /* linkage */
4305 case YANG_INCLUDE:
4306 case YANG_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004307 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004308 break;
4309 /* meta */
4310 case YANG_ORGANIZATION:
4311 case YANG_CONTACT:
4312 case YANG_DESCRIPTION:
4313 case YANG_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004314 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004315 break;
4316
4317 /* revision */
4318 case YANG_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004319 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004320 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004321 /* body */
4322 case YANG_ANYDATA:
4323 case YANG_ANYXML:
4324 case YANG_AUGMENT:
4325 case YANG_CHOICE:
4326 case YANG_CONTAINER:
4327 case YANG_DEVIATION:
4328 case YANG_EXTENSION:
4329 case YANG_FEATURE:
4330 case YANG_GROUPING:
4331 case YANG_IDENTITY:
4332 case YANG_LEAF:
4333 case YANG_LEAF_LIST:
4334 case YANG_LIST:
4335 case YANG_NOTIFICATION:
4336 case YANG_RPC:
4337 case YANG_TYPEDEF:
4338 case YANG_USES:
4339 case YANG_CUSTOM:
4340 mod_stmt = Y_MOD_BODY;
4341 break;
4342 default:
4343 /* error handled in the next switch */
4344 break;
4345 }
Radek Krejcie3846472018-10-15 15:24:51 +02004346#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004347
Radek Krejcie3846472018-10-15 15:24:51 +02004348 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004349 switch (kw) {
4350 /* module header */
4351 case YANG_YANG_VERSION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004352 LY_CHECK_RET(parse_yangversion(ctx, data, mod));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004353 break;
4354 case YANG_NAMESPACE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004355 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_NAMESPACE, 0, &mod->ns, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004356 break;
4357 case YANG_PREFIX:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004358 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_PREFIX, 0, &mod->prefix, Y_IDENTIF_ARG, &mod->exts));
Radek Krejci70853c52018-10-15 14:46:16 +02004359 LY_CHECK_RET(lysp_check_prefix(ctx, mod, &mod->prefix), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004360 break;
4361 case YANG_BELONGS_TO:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004362 LY_CHECK_RET(parse_belongsto(ctx, data, &mod->belongsto, &mod->prefix, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004363 break;
4364
4365 /* linkage */
4366 case YANG_INCLUDE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004367 LY_CHECK_RET(parse_include(ctx, data, mod));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004368 break;
4369 case YANG_IMPORT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004370 LY_CHECK_RET(parse_import(ctx, data, mod));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004371 break;
4372
4373 /* meta */
4374 case YANG_ORGANIZATION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004375 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_ORGANIZATION, 0, &mod->org, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004376 break;
4377 case YANG_CONTACT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004378 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_CONTACT, 0, &mod->contact, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004379 break;
4380 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004381 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &mod->dsc, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004382 break;
4383 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004384 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &mod->ref, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004385 break;
4386
4387 /* revision */
4388 case YANG_REVISION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004389 LY_CHECK_RET(parse_revision(ctx, data, &mod->revs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004390 break;
4391
4392 /* body */
4393 case YANG_ANYDATA:
Radek Krejci10113652018-11-14 16:56:50 +01004394 YANG_CHECK_STMTVER_RET(ctx, "anydata", mod->submodule ? "submodule" : "module");
4395 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004396 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004397 LY_CHECK_RET(parse_any(ctx, data, kw, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004398 break;
4399 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004400 LY_CHECK_RET(parse_choice(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004401 break;
4402 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004403 LY_CHECK_RET(parse_container(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004404 break;
4405 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004406 LY_CHECK_RET(parse_leaf(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004407 break;
4408 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004409 LY_CHECK_RET(parse_leaflist(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004410 break;
4411 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004412 LY_CHECK_RET(parse_list(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004413 break;
4414 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004415 LY_CHECK_RET(parse_uses(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004416 break;
4417
4418 case YANG_AUGMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004419 LY_CHECK_RET(parse_augment(ctx, data, NULL, &mod->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004420 break;
4421 case YANG_DEVIATION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004422 LY_CHECK_RET(parse_deviation(ctx, data, &mod->deviations));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004423 break;
4424 case YANG_EXTENSION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004425 LY_CHECK_RET(parse_extension(ctx, data, &mod->extensions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004426 break;
4427 case YANG_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004428 LY_CHECK_RET(parse_feature(ctx, data, &mod->features));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004429 break;
4430 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004431 LY_CHECK_RET(parse_grouping(ctx, data, NULL, &mod->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004432 break;
4433 case YANG_IDENTITY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004434 LY_CHECK_RET(parse_identity(ctx, data, &mod->identities));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004435 break;
4436 case YANG_NOTIFICATION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004437 LY_CHECK_RET(parse_notif(ctx, data, NULL, &mod->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004438 break;
4439 case YANG_RPC:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004440 LY_CHECK_RET(parse_action(ctx, data, NULL, &mod->rpcs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004441 break;
4442 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01004443 LY_CHECK_RET(parse_typedef(ctx, NULL, data, &mod->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004444 break;
4445 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004446 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004447 break;
4448
4449 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004450 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), mod->submodule ? "submodule" : "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004451 return LY_EVALID;
4452 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004453 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004454 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01004455checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004456 /* mandatory substatements */
4457 if (mod->submodule) {
4458 if (!mod->belongsto) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004459 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004460 return LY_EVALID;
4461 }
4462 } else {
4463 if (!mod->ns) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004464 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004465 return LY_EVALID;
4466 } else if (!mod->prefix) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004467 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004468 return LY_EVALID;
4469 }
4470 }
4471
Radek Krejcie9e987e2018-10-31 12:50:27 +01004472 /* submodules share the namespace with the module names, so there must not be
4473 * a submodule of the same name in the context, no need for revision matching */
4474 dup = ly_ctx_get_submodule(ctx->ctx, NULL, mod->name, NULL);
4475 if (dup && (!mod->submodule || strcmp(dup->belongsto, mod->belongsto))) {
4476 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Name collision between %s of name \"%s\".",
4477 mod->submodule ? "submodules" : "module and submodule", mod->name);
4478 return LY_EVALID;
4479 }
4480
Michal Vasko7fbc8162018-09-17 10:35:16 +02004481 return ret;
4482}
4483
Radek Krejcid4557c62018-09-17 11:42:09 +02004484LY_ERR
Radek Krejcibbe09a92018-11-08 09:36:54 +01004485yang_parse(struct ly_parser_ctx *context, const char *data, struct lysp_module **mod_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004486{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004487 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004488 char *word, *buf;
Radek Krejciefd22f62018-09-27 11:47:58 +02004489 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004490 enum yang_keyword kw;
Radek Krejci0c2cf322018-10-13 08:02:30 +02004491 struct lysp_module *mod = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004492
4493 /* "module"/"submodule" */
Radek Krejcibbe09a92018-11-08 09:36:54 +01004494 ret = get_keyword(context, &data, &kw, &word, &word_len);
4495 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004496
4497 if ((kw != YANG_MODULE) && (kw != YANG_SUBMODULE)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +01004498 LOGVAL_YANG(context, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".",
Radek Krejcic59bc972018-09-17 16:13:06 +02004499 ly_stmt2str(kw));
Radek Krejcibbe09a92018-11-08 09:36:54 +01004500 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004501 }
4502
4503 mod = calloc(1, sizeof *mod);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004504 LY_CHECK_ERR_GOTO(!mod, LOGMEM(context->ctx), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004505 if (kw == YANG_SUBMODULE) {
4506 mod->submodule = 1;
4507 }
Radek Krejcibbe09a92018-11-08 09:36:54 +01004508 mod->parsing = 1;
4509 mod->ctx = context->ctx;
4510 context->mod = mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004511
4512 /* substatements */
Radek Krejcibbe09a92018-11-08 09:36:54 +01004513 ret = parse_sub_module(context, &data, mod);
4514 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004515
4516 /* read some trailing spaces or new lines */
Radek Krejcibbe09a92018-11-08 09:36:54 +01004517 ret = get_argument(context, &data, Y_MAYBE_STR_ARG, &word, &buf, &word_len);
4518 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004519
4520 if (word) {
Radek Krejcibbe09a92018-11-08 09:36:54 +01004521 LOGVAL_YANG(context, LYVE_SYNTAX, "Invalid character sequence \"%.*s\", expected end-of-file.",
Michal Vasko7fbc8162018-09-17 10:35:16 +02004522 word_len, word);
4523 free(buf);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004524 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004525 }
4526 assert(!buf);
4527
Radek Krejcibbe09a92018-11-08 09:36:54 +01004528 mod->parsing = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004529 *mod_p = mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004530
Radek Krejcibbe09a92018-11-08 09:36:54 +01004531cleanup:
4532 if (ret) {
4533 lysp_module_free(mod);
4534 }
4535
Michal Vasko7fbc8162018-09-17 10:35:16 +02004536 return ret;
4537}