blob: 2d2d667901abd138a55851d10b4caba533ca5158 [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 Krejci2b610482019-01-02 13:36:09 +010058#define MOVE_INPUT(CTX, DATA, COUNT) (*(DATA))+=COUNT;(CTX)->indent+=COUNT
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020059
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;
Radek Krejcie86bf772018-12-14 11:39:53 +01002825 unsigned int u;
2826 struct lysp_node *child;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002827
2828 if (*inout_p) {
Radek Krejci10113652018-11-14 16:56:50 +01002829 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002830 return LY_EVALID;
2831 }
2832
2833 /* create structure */
2834 inout = calloc(1, sizeof *inout);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002835 LY_CHECK_ERR_RET(!inout, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002836 *inout_p = inout;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002837 inout->nodetype = LYS_INOUT;
2838 inout->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002839
2840 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01002841 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002842 switch (kw) {
2843 case YANG_ANYDATA:
Radek Krejci10113652018-11-14 16:56:50 +01002844 YANG_CHECK_STMTVER_RET(ctx, "anydata", ly_stmt2str(inout_kw));
2845 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002846 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002847 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)inout, &inout->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002848 break;
2849 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002850 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)inout, &inout->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002851 break;
2852 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002853 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)inout, &inout->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002854 break;
2855 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002856 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)inout, &inout->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002857 break;
2858 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002859 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)inout, &inout->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002860 break;
2861 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002862 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)inout, &inout->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002863 break;
2864 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002865 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)inout, &inout->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002866 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002867 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01002868 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)inout, data, &inout->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002869 break;
2870 case YANG_MUST:
Radek Krejci10113652018-11-14 16:56:50 +01002871 YANG_CHECK_STMTVER_RET(ctx, "must", ly_stmt2str(inout_kw));
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002872 LY_CHECK_RET(parse_restrs(ctx, data, kw, &inout->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002873 break;
2874 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002875 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)inout, &inout->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002876 break;
2877 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002878 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inout->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002879 break;
2880 default:
Radek Krejci10113652018-11-14 16:56:50 +01002881 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002882 return LY_EVALID;
2883 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002884 }
Radek Krejcie86bf772018-12-14 11:39:53 +01002885 /* finalize parent pointers to the reallocated items */
2886 LY_ARRAY_FOR(inout->groupings, u) {
2887 LY_LIST_FOR(inout->groupings[u].data, child) {
2888 child->parent = (struct lysp_node*)&inout->groupings[u];
2889 }
2890 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002891 return ret;
2892}
2893
Michal Vaskoea5abea2018-09-18 13:10:54 +02002894/**
2895 * @brief Parse the action statement.
2896 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002897 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002898 * @param[in,out] data Data to read from, always moved to currently handled character.
2899 * @param[in,out] actions Actions to add to.
2900 *
2901 * @return LY_ERR values.
2902 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002903static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002904parse_action(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002905{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002906 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002907 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002908 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002909 enum yang_keyword kw;
2910 struct lysp_action *act;
Radek Krejcie86bf772018-12-14 11:39:53 +01002911 struct lysp_node *child;
2912 unsigned int u;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002913
Radek Krejci2c4e7172018-10-19 15:56:26 +02002914 LY_ARRAY_NEW_RET(ctx->ctx, *actions, act, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002915
2916 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002917 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002918 INSERT_WORD(ctx, buf, act->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002919 act->nodetype = LYS_ACTION;
2920 act->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002921
Radek Krejci6d6556c2018-11-08 09:37:45 +01002922 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002923 switch (kw) {
2924 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002925 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 +02002926 break;
2927 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002928 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 +02002929 break;
2930 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002931 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 +02002932 break;
2933 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002934 LY_CHECK_RET(parse_status(ctx, data, &act->flags, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002935 break;
2936
2937 case YANG_INPUT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002938 LY_CHECK_RET(parse_inout(ctx, data, kw, (struct lysp_node*)act, &act->input));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002939 break;
2940 case YANG_OUTPUT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002941 LY_CHECK_RET(parse_inout(ctx, data, kw, (struct lysp_node*)act, &act->output));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002942 break;
2943
2944 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01002945 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)act, data, &act->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002946 break;
2947 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002948 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)act, &act->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002949 break;
2950 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002951 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002952 break;
2953 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002954 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "action");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002955 return LY_EVALID;
2956 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002957 }
Radek Krejcie86bf772018-12-14 11:39:53 +01002958 /* finalize parent pointers to the reallocated items */
2959 LY_ARRAY_FOR(act->groupings, u) {
2960 LY_LIST_FOR(act->groupings[u].data, child) {
2961 child->parent = (struct lysp_node*)&act->groupings[u];
2962 }
2963 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002964 return ret;
2965}
2966
Michal Vaskoea5abea2018-09-18 13:10:54 +02002967/**
2968 * @brief Parse the notification statement.
2969 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002970 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002971 * @param[in,out] data Data to read from, always moved to currently handled character.
2972 * @param[in,out] notifs Notifications to add to.
2973 *
2974 * @return LY_ERR values.
2975 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002976static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002977parse_notif(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002978{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002979 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002980 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002981 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002982 enum yang_keyword kw;
2983 struct lysp_notif *notif;
Radek Krejcie86bf772018-12-14 11:39:53 +01002984 struct lysp_node *child;
2985 unsigned int u;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002986
Radek Krejci2c4e7172018-10-19 15:56:26 +02002987 LY_ARRAY_NEW_RET(ctx->ctx, *notifs, notif, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002988
2989 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002990 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002991 INSERT_WORD(ctx, buf, notif->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002992 notif->nodetype = LYS_NOTIF;
2993 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002994
Radek Krejci6d6556c2018-11-08 09:37:45 +01002995 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002996 switch (kw) {
2997 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002998 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 +02002999 break;
3000 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003001 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 +02003002 break;
3003 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003004 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 +02003005 break;
3006 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003007 LY_CHECK_RET(parse_status(ctx, data, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003008 break;
3009
3010 case YANG_ANYDATA:
Radek Krejci10113652018-11-14 16:56:50 +01003011 YANG_CHECK_STMTVER_RET(ctx, "anydata", "notification");
3012 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003013 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003014 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003015 break;
3016 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003017 LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003018 break;
3019 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003020 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003021 break;
3022 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003023 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003024 break;
3025 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003026 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003027 break;
3028 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003029 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003030 break;
3031 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003032 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003033 break;
3034
3035 case YANG_MUST:
Radek Krejci10113652018-11-14 16:56:50 +01003036 YANG_CHECK_STMTVER_RET(ctx, "must", "notification");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003037 LY_CHECK_RET(parse_restrs(ctx, data, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003038 break;
3039 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01003040 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)notif, data, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003041 break;
3042 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003043 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003044 break;
3045 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003046 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003047 break;
3048 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003049 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003050 return LY_EVALID;
3051 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003052 }
Radek Krejcie86bf772018-12-14 11:39:53 +01003053 /* finalize parent pointers to the reallocated items */
3054 LY_ARRAY_FOR(notif->groupings, u) {
3055 LY_LIST_FOR(notif->groupings[u].data, child) {
3056 child->parent = (struct lysp_node*)&notif->groupings[u];
3057 }
3058 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003059 return ret;
3060}
3061
Michal Vaskoea5abea2018-09-18 13:10:54 +02003062/**
3063 * @brief Parse the grouping statement.
3064 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003065 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003066 * @param[in,out] data Data to read from, always moved to currently handled character.
3067 * @param[in,out] groupings Groupings to add to.
3068 *
3069 * @return LY_ERR values.
3070 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003071static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003072parse_grouping(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003073{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003074 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003075 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003076 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003077 enum yang_keyword kw;
3078 struct lysp_grp *grp;
Radek Krejcie86bf772018-12-14 11:39:53 +01003079 struct lysp_node *child;
3080 unsigned int u;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003081
Radek Krejci2c4e7172018-10-19 15:56:26 +02003082 LY_ARRAY_NEW_RET(ctx->ctx, *groupings, grp, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003083
3084 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003085 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003086 INSERT_WORD(ctx, buf, grp->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003087 grp->nodetype = LYS_GROUPING;
3088 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003089
Radek Krejci6d6556c2018-11-08 09:37:45 +01003090 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003091 switch (kw) {
3092 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003093 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 +02003094 break;
3095 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003096 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 +02003097 break;
3098 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003099 LY_CHECK_RET(parse_status(ctx, data, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003100 break;
3101
3102 case YANG_ANYDATA:
Radek Krejci10113652018-11-14 16:56:50 +01003103 YANG_CHECK_STMTVER_RET(ctx, "anydata", "grouping");
3104 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003105 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003106 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003107 break;
3108 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003109 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003110 break;
3111 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003112 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003113 break;
3114 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003115 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003116 break;
3117 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003118 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003119 break;
3120 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003121 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003122 break;
3123 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003124 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003125 break;
3126
3127 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01003128 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)grp, data, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003129 break;
3130 case YANG_ACTION:
Radek Krejci10113652018-11-14 16:56:50 +01003131 YANG_CHECK_STMTVER_RET(ctx, "action", "grouping");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003132 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)grp, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003133 break;
3134 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003135 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)grp, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003136 break;
3137 case YANG_NOTIFICATION:
Radek Krejci10113652018-11-14 16:56:50 +01003138 YANG_CHECK_STMTVER_RET(ctx, "notification", "grouping");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003139 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)grp, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003140 break;
3141 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003142 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003143 break;
3144 default:
Radek Krejci10113652018-11-14 16:56:50 +01003145 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003146 return LY_EVALID;
3147 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003148 }
Radek Krejcie86bf772018-12-14 11:39:53 +01003149 /* finalize parent pointers to the reallocated items */
3150 LY_ARRAY_FOR(grp->groupings, u) {
3151 LY_LIST_FOR(grp->groupings[u].data, child) {
3152 child->parent = (struct lysp_node*)&grp->groupings[u];
3153 }
3154 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003155 return ret;
3156}
3157
Michal Vaskoea5abea2018-09-18 13:10:54 +02003158/**
3159 * @brief Parse the refine statement.
3160 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003161 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003162 * @param[in,out] data Data to read from, always moved to currently handled character.
3163 * @param[in,out] augments Augments to add to.
3164 *
3165 * @return LY_ERR values.
3166 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003167static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003168parse_augment(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003169{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003170 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003171 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003172 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003173 enum yang_keyword kw;
3174 struct lysp_augment *aug;
3175
Radek Krejci2c4e7172018-10-19 15:56:26 +02003176 LY_ARRAY_NEW_RET(ctx->ctx, *augments, aug, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003177
3178 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003179 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003180 INSERT_WORD(ctx, buf, aug->nodeid, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003181 aug->nodetype = LYS_AUGMENT;
3182 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003183
Radek Krejci6d6556c2018-11-08 09:37:45 +01003184 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003185 switch (kw) {
3186 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003187 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 +02003188 break;
3189 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003190 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 +02003191 break;
3192 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003193 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 +02003194 break;
3195 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003196 LY_CHECK_RET(parse_status(ctx, data, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003197 break;
3198 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003199 LY_CHECK_RET(parse_when(ctx, data, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003200 break;
3201
3202 case YANG_ANYDATA:
Radek Krejci10113652018-11-14 16:56:50 +01003203 YANG_CHECK_STMTVER_RET(ctx, "anydata", "augment");
3204 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003205 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003206 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003207 break;
3208 case YANG_CASE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003209 LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003210 break;
3211 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003212 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003213 break;
3214 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003215 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003216 break;
3217 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003218 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003219 break;
3220 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003221 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003222 break;
3223 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003224 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003225 break;
3226 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003227 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003228 break;
3229
3230 case YANG_ACTION:
Radek Krejci10113652018-11-14 16:56:50 +01003231 YANG_CHECK_STMTVER_RET(ctx, "action", "augment");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003232 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003233 break;
3234 case YANG_NOTIFICATION:
Radek Krejci10113652018-11-14 16:56:50 +01003235 YANG_CHECK_STMTVER_RET(ctx, "notification", "augment");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003236 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003237 break;
3238 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003239 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003240 break;
3241 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003242 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003243 return LY_EVALID;
3244 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003245 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003246 return ret;
3247}
3248
Michal Vaskoea5abea2018-09-18 13:10:54 +02003249/**
3250 * @brief Parse the uses statement.
3251 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003252 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003253 * @param[in,out] data Data to read from, always moved to currently handled character.
3254 * @param[in,out] siblings Siblings to add to.
3255 *
3256 * @return LY_ERR values.
3257 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003258static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003259parse_uses(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003260{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003261 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003262 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003263 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003264 enum yang_keyword kw;
3265 struct lysp_node *iter;
3266 struct lysp_node_uses *uses;
3267
3268 /* create structure */
3269 uses = calloc(1, sizeof *uses);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003270 LY_CHECK_ERR_RET(!uses, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003271 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003272 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003273
3274 /* insert into siblings */
3275 if (!*siblings) {
3276 *siblings = (struct lysp_node *)uses;
3277 } else {
3278 for (iter = *siblings; iter->next; iter = iter->next);
3279 iter->next = (struct lysp_node *)uses;
3280 }
3281
3282 /* get name */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003283 LY_CHECK_RET(get_argument(ctx, data, Y_PREF_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003284 INSERT_WORD(ctx, buf, uses->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003285
3286 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01003287 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003288 switch (kw) {
3289 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003290 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 +02003291 break;
3292 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003293 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 +02003294 break;
3295 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003296 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 +02003297 break;
3298 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003299 LY_CHECK_RET(parse_status(ctx, data, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003300 break;
3301 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003302 LY_CHECK_RET(parse_when(ctx, data, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003303 break;
3304
3305 case YANG_REFINE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003306 LY_CHECK_RET(parse_refine(ctx, data, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003307 break;
3308 case YANG_AUGMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003309 LY_CHECK_RET(parse_augment(ctx, data, (struct lysp_node*)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003310 break;
3311 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003312 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003313 break;
3314 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003315 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003316 return LY_EVALID;
3317 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003318 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003319 return ret;
3320}
3321
Michal Vaskoea5abea2018-09-18 13:10:54 +02003322/**
3323 * @brief Parse the case statement.
3324 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003325 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003326 * @param[in,out] data Data to read from, always moved to currently handled character.
3327 * @param[in,out] siblings Siblings to add to.
3328 *
3329 * @return LY_ERR values.
3330 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003331static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003332parse_case(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003333{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003334 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003335 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003336 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003337 enum yang_keyword kw;
3338 struct lysp_node *iter;
3339 struct lysp_node_case *cas;
3340
3341 /* create structure */
3342 cas = calloc(1, sizeof *cas);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003343 LY_CHECK_ERR_RET(!cas, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003344 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003345 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003346
3347 /* insert into siblings */
3348 if (!*siblings) {
3349 *siblings = (struct lysp_node *)cas;
3350 } else {
3351 for (iter = *siblings; iter->next; iter = iter->next);
3352 iter->next = (struct lysp_node *)cas;
3353 }
3354
3355 /* get name */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003356 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003357 INSERT_WORD(ctx, buf, cas->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003358
3359 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01003360 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003361 switch (kw) {
3362 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003363 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 +02003364 break;
3365 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003366 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 +02003367 break;
3368 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003369 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 +02003370 break;
3371 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003372 LY_CHECK_RET(parse_status(ctx, data, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003373 break;
3374 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003375 LY_CHECK_RET(parse_when(ctx, data, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003376 break;
3377
3378 case YANG_ANYDATA:
Radek Krejci10113652018-11-14 16:56:50 +01003379 YANG_CHECK_STMTVER_RET(ctx, "anydata", "case");
3380 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003381 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003382 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003383 break;
3384 case YANG_CHOICE:
Radek Krejcia9026eb2018-12-12 16:04:47 +01003385 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003386 break;
3387 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003388 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003389 break;
3390 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003391 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003392 break;
3393 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003394 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003395 break;
3396 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003397 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003398 break;
3399 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003400 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003401 break;
3402 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003403 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003404 break;
3405 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003406 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003407 return LY_EVALID;
3408 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003409 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003410 return ret;
3411}
3412
Michal Vaskoea5abea2018-09-18 13:10:54 +02003413/**
3414 * @brief Parse the choice statement.
3415 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003416 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003417 * @param[in,out] data Data to read from, always moved to currently handled character.
3418 * @param[in,out] siblings Siblings to add to.
3419 *
3420 * @return LY_ERR values.
3421 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003422static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003423parse_choice(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003424{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003425 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003426 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003427 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003428 enum yang_keyword kw;
3429 struct lysp_node *iter;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003430 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003431
3432 /* create structure */
Radek Krejci44ceedc2018-10-02 15:54:31 +02003433 choice = calloc(1, sizeof *choice);
3434 LY_CHECK_ERR_RET(!choice, LOGMEM(ctx->ctx), LY_EMEM);
3435 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003436 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003437
3438 /* insert into siblings */
3439 if (!*siblings) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02003440 *siblings = (struct lysp_node *)choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003441 } else {
3442 for (iter = *siblings; iter->next; iter = iter->next);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003443 iter->next = (struct lysp_node *)choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003444 }
3445
3446 /* get name */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003447 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003448 INSERT_WORD(ctx, buf, choice->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003449
3450 /* parse substatements */
Radek Krejcia9026eb2018-12-12 16:04:47 +01003451 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003452 switch (kw) {
3453 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003454 LY_CHECK_RET(parse_config(ctx, data, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003455 break;
3456 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003457 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 +02003458 break;
3459 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003460 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 +02003461 break;
3462 case YANG_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003463 LY_CHECK_RET(parse_mandatory(ctx, data, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003464 break;
3465 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003466 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 +02003467 break;
3468 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003469 LY_CHECK_RET(parse_status(ctx, data, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003470 break;
3471 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003472 LY_CHECK_RET(parse_when(ctx, data, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003473 break;
3474 case YANG_DEFAULT:
Radek Krejcia9026eb2018-12-12 16:04:47 +01003475 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 +02003476 break;
3477
3478 case YANG_ANYDATA:
Radek Krejci10113652018-11-14 16:56:50 +01003479 YANG_CHECK_STMTVER_RET(ctx, "anydata", "choice");
3480 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003481 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003482 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003483 break;
3484 case YANG_CASE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003485 LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003486 break;
3487 case YANG_CHOICE:
Radek Krejci10113652018-11-14 16:56:50 +01003488 YANG_CHECK_STMTVER_RET(ctx, "choice", "choice");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003489 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003490 break;
3491 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003492 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003493 break;
3494 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003495 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003496 break;
3497 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003498 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003499 break;
3500 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003501 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003502 break;
3503 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003504 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003505 break;
3506 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003507 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003508 return LY_EVALID;
3509 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003510 }
Radek Krejcia9026eb2018-12-12 16:04:47 +01003511 LY_CHECK_RET(ret);
3512checks:
3513 if ((choice->flags & LYS_MAND_TRUE) && choice->dflt) {
3514 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMSCOMB, "mandatory", "default", "choice");
3515 return LY_EVALID;
3516 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003517 return ret;
3518}
3519
Michal Vaskoea5abea2018-09-18 13:10:54 +02003520/**
3521 * @brief Parse the container statement.
3522 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003523 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003524 * @param[in,out] data Data to read from, always moved to currently handled character.
3525 * @param[in,out] siblings Siblings to add to.
3526 *
3527 * @return LY_ERR values.
3528 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003529static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003530parse_container(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003531{
3532 LY_ERR ret = 0;
3533 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003534 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003535 enum yang_keyword kw;
3536 struct lysp_node *iter;
3537 struct lysp_node_container *cont;
Radek Krejcie86bf772018-12-14 11:39:53 +01003538 unsigned int u;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003539
3540 /* create structure */
3541 cont = calloc(1, sizeof *cont);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003542 LY_CHECK_ERR_RET(!cont, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003543 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003544 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003545
3546 /* insert into siblings */
3547 if (!*siblings) {
3548 *siblings = (struct lysp_node *)cont;
3549 } else {
3550 for (iter = *siblings; iter->next; iter = iter->next);
3551 iter->next = (struct lysp_node *)cont;
3552 }
3553
3554 /* get name */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003555 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003556 INSERT_WORD(ctx, buf, cont->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003557
3558 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01003559 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003560 switch (kw) {
3561 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003562 LY_CHECK_RET(parse_config(ctx, data, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003563 break;
3564 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003565 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 +02003566 break;
3567 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003568 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 +02003569 break;
3570 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003571 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 +02003572 break;
3573 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003574 LY_CHECK_RET(parse_status(ctx, data, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003575 break;
3576 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003577 LY_CHECK_RET(parse_when(ctx, data, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003578 break;
3579 case YANG_PRESENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003580 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 +02003581 break;
3582
3583 case YANG_ANYDATA:
Radek Krejci10113652018-11-14 16:56:50 +01003584 YANG_CHECK_STMTVER_RET(ctx, "anydata", "container");
3585 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003586 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003587 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003588 break;
3589 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003590 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003591 break;
3592 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003593 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003594 break;
3595 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003596 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003597 break;
3598 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003599 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003600 break;
3601 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003602 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003603 break;
3604 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003605 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003606 break;
3607
3608 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01003609 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)cont, data, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003610 break;
3611 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003612 LY_CHECK_RET(parse_restrs(ctx, data, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003613 break;
3614 case YANG_ACTION:
Radek Krejci10113652018-11-14 16:56:50 +01003615 YANG_CHECK_STMTVER_RET(ctx, "action", "container");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003616 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003617 break;
3618 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003619 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003620 break;
3621 case YANG_NOTIFICATION:
Radek Krejci10113652018-11-14 16:56:50 +01003622 YANG_CHECK_STMTVER_RET(ctx, "notification", "container");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003623 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003624 break;
3625 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003626 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003627 break;
3628 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003629 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003630 return LY_EVALID;
3631 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003632 }
Radek Krejcie86bf772018-12-14 11:39:53 +01003633 /* finalize parent pointers to the reallocated items */
3634 LY_ARRAY_FOR(cont->groupings, u) {
3635 LY_LIST_FOR(cont->groupings[u].data, iter) {
3636 iter->parent = (struct lysp_node*)&cont->groupings[u];
3637 }
3638 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003639 return ret;
3640}
3641
Michal Vaskoea5abea2018-09-18 13:10:54 +02003642/**
3643 * @brief Parse the list statement.
3644 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003645 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003646 * @param[in,out] data Data to read from, always moved to currently handled character.
3647 * @param[in,out] siblings Siblings to add to.
3648 *
3649 * @return LY_ERR values.
3650 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003651static LY_ERR
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003652parse_list(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003653{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003654 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003655 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003656 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003657 enum yang_keyword kw;
3658 struct lysp_node *iter;
3659 struct lysp_node_list *list;
Radek Krejcie86bf772018-12-14 11:39:53 +01003660 unsigned int u;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003661
3662 /* create structure */
3663 list = calloc(1, sizeof *list);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003664 LY_CHECK_ERR_RET(!list, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003665 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003666 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003667
3668 /* insert into siblings */
3669 if (!*siblings) {
3670 *siblings = (struct lysp_node *)list;
3671 } else {
3672 for (iter = *siblings; iter->next; iter = iter->next);
3673 iter->next = (struct lysp_node *)list;
3674 }
3675
3676 /* get name */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003677 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003678 INSERT_WORD(ctx, buf, list->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003679
3680 /* parse substatements */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003681 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003682 switch (kw) {
3683 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003684 LY_CHECK_RET(parse_config(ctx, data, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003685 break;
3686 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003687 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 +02003688 break;
3689 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003690 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 +02003691 break;
3692 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003693 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 +02003694 break;
3695 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003696 LY_CHECK_RET(parse_status(ctx, data, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003697 break;
3698 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003699 LY_CHECK_RET(parse_when(ctx, data, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003700 break;
3701 case YANG_KEY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003702 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 +02003703 break;
3704 case YANG_MAX_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003705 LY_CHECK_RET(parse_maxelements(ctx, data, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003706 break;
3707 case YANG_MIN_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003708 LY_CHECK_RET(parse_minelements(ctx, data, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003709 break;
3710 case YANG_ORDERED_BY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003711 LY_CHECK_RET(parse_orderedby(ctx, data, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003712 break;
3713 case YANG_UNIQUE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003714 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 +02003715 break;
3716
3717 case YANG_ANYDATA:
Radek Krejci10113652018-11-14 16:56:50 +01003718 YANG_CHECK_STMTVER_RET(ctx, "anydata", "list");
3719 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003720 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003721 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003722 break;
3723 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003724 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003725 break;
3726 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003727 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003728 break;
3729 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003730 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003731 break;
3732 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003733 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003734 break;
3735 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003736 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003737 break;
3738 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003739 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003740 break;
3741
3742 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01003743 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)list, data, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003744 break;
3745 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003746 LY_CHECK_RET(parse_restrs(ctx, data, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003747 break;
3748 case YANG_ACTION:
Radek Krejci10113652018-11-14 16:56:50 +01003749 YANG_CHECK_STMTVER_RET(ctx, "action", "list");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003750 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003751 break;
3752 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003753 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003754 break;
3755 case YANG_NOTIFICATION:
Radek Krejci10113652018-11-14 16:56:50 +01003756 YANG_CHECK_STMTVER_RET(ctx, "notification", "list");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003757 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003758 break;
3759 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003760 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003761 break;
3762 default:
Radek Krejci10113652018-11-14 16:56:50 +01003763 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003764 return LY_EVALID;
3765 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003766 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003767 LY_CHECK_RET(ret);
Radek Krejcie86bf772018-12-14 11:39:53 +01003768 /* finalize parent pointers to the reallocated items */
3769 LY_ARRAY_FOR(list->groupings, u) {
3770 LY_LIST_FOR(list->groupings[u].data, iter) {
3771 iter->parent = (struct lysp_node*)&list->groupings[u];
3772 }
3773 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003774checks:
3775 if (list->max && list->min > list->max) {
3776 LOGVAL_YANG(ctx, LYVE_SEMANTICS,
3777 "Invalid combination of min-elements and max-elements: min value %u is bigger than the max value %u.",
3778 list->min, list->max);
3779 return LY_EVALID;
3780 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003781
3782 return ret;
3783}
3784
Michal Vaskoea5abea2018-09-18 13:10:54 +02003785/**
3786 * @brief Parse the yin-element statement.
3787 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003788 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003789 * @param[in,out] data Data to read from, always moved to currently handled character.
3790 * @param[in,out] flags Flags to write to.
3791 * @param[in,out] exts Extension instances to add to.
3792 *
3793 * @return LY_ERR values.
3794 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003795static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003796parse_yinelement(struct ly_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003797{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003798 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003799 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003800 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003801 enum yang_keyword kw;
3802
3803 if (*flags & LYS_YINELEM_MASK) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02003804 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003805 return LY_EVALID;
3806 }
3807
3808 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003809 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003810
3811 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
3812 *flags |= LYS_YINELEM_TRUE;
3813 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
3814 *flags |= LYS_YINELEM_FALSE;
3815 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02003816 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003817 free(buf);
3818 return LY_EVALID;
3819 }
3820 free(buf);
3821
Radek Krejci6d6556c2018-11-08 09:37:45 +01003822 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003823 switch (kw) {
3824 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003825 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_YINELEM, 0, exts));
3826 LY_CHECK_RET(ret); break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003827 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003828 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003829 return LY_EVALID;
3830 }
3831 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003832 return ret;
3833}
3834
Michal Vaskoea5abea2018-09-18 13:10:54 +02003835/**
3836 * @brief Parse the yin-element statement.
3837 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003838 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003839 * @param[in,out] data Data to read from, always moved to currently handled character.
3840 * @param[in,out] argument Value to write to.
3841 * @param[in,out] flags Flags to write to.
3842 * @param[in,out] exts Extension instances to add to.
3843 *
3844 * @return LY_ERR values.
3845 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003846static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003847parse_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 +02003848{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003849 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003850 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003851 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003852 enum yang_keyword kw;
3853
3854 if (*argument) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02003855 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003856 return LY_EVALID;
3857 }
3858
3859 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003860 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003861 INSERT_WORD(ctx, buf, *argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003862
Radek Krejci6d6556c2018-11-08 09:37:45 +01003863 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003864 switch (kw) {
3865 case YANG_YIN_ELEMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003866 LY_CHECK_RET(parse_yinelement(ctx, data, flags, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003867 break;
3868 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003869 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_ARGUMENT, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003870 break;
3871 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003872 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003873 return LY_EVALID;
3874 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003875 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003876 return ret;
3877}
3878
Michal Vaskoea5abea2018-09-18 13:10:54 +02003879/**
3880 * @brief Parse the extension statement.
3881 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003882 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003883 * @param[in,out] data Data to read from, always moved to currently handled character.
3884 * @param[in,out] extensions Extensions to add to.
3885 *
3886 * @return LY_ERR values.
3887 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003888static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003889parse_extension(struct ly_parser_ctx *ctx, const char **data, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003890{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003891 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003892 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003893 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003894 enum yang_keyword kw;
3895 struct lysp_ext *ex;
3896
Radek Krejci2c4e7172018-10-19 15:56:26 +02003897 LY_ARRAY_NEW_RET(ctx->ctx, *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003898
3899 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003900 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003901 INSERT_WORD(ctx, buf, ex->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003902
Radek Krejci6d6556c2018-11-08 09:37:45 +01003903 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003904 switch (kw) {
3905 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003906 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 +02003907 break;
3908 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003909 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 +02003910 break;
3911 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003912 LY_CHECK_RET(parse_status(ctx, data, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003913 break;
3914 case YANG_ARGUMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003915 LY_CHECK_RET(parse_argument(ctx, data, &ex->argument, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003916 break;
3917 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003918 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003919 break;
3920 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003921 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003922 return LY_EVALID;
3923 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003924 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003925 return ret;
3926}
3927
Michal Vaskoea5abea2018-09-18 13:10:54 +02003928/**
3929 * @brief Parse the deviate statement.
3930 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003931 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003932 * @param[in,out] data Data to read from, always moved to currently handled character.
3933 * @param[in,out] deviates Deviates to add to.
3934 *
3935 * @return LY_ERR values.
3936 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003937static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003938parse_deviate(struct ly_parser_ctx *ctx, const char **data, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003939{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003940 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003941 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003942 size_t word_len, dev_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003943 enum yang_keyword kw;
3944 struct lysp_deviate *iter, *d;
3945 struct lysp_deviate_add *d_add = NULL;
3946 struct lysp_deviate_rpl *d_rpl = NULL;
3947 struct lysp_deviate_del *d_del = NULL;
3948 const char **d_units, ***d_uniques, ***d_dflts;
3949 struct lysp_restr **d_musts;
3950 uint16_t *d_flags;
3951 uint32_t *d_min, *d_max;
3952
3953 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003954 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003955
3956 if ((word_len == 13) && !strncmp(word, "not-supported", word_len)) {
3957 dev_mod = LYS_DEV_NOT_SUPPORTED;
3958 } else if ((word_len == 3) && !strncmp(word, "add", word_len)) {
3959 dev_mod = LYS_DEV_ADD;
3960 } else if ((word_len == 7) && !strncmp(word, "replace", word_len)) {
3961 dev_mod = LYS_DEV_REPLACE;
3962 } else if ((word_len == 6) && !strncmp(word, "delete", word_len)) {
3963 dev_mod = LYS_DEV_DELETE;
3964 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02003965 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003966 free(buf);
3967 return LY_EVALID;
3968 }
3969 free(buf);
3970
3971 /* create structure */
3972 switch (dev_mod) {
3973 case LYS_DEV_NOT_SUPPORTED:
3974 d = calloc(1, sizeof *d);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003975 LY_CHECK_ERR_RET(!d, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003976 break;
3977 case LYS_DEV_ADD:
3978 d_add = calloc(1, sizeof *d_add);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003979 LY_CHECK_ERR_RET(!d_add, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003980 d = (struct lysp_deviate *)d_add;
3981 d_units = &d_add->units;
3982 d_uniques = &d_add->uniques;
3983 d_dflts = &d_add->dflts;
3984 d_musts = &d_add->musts;
3985 d_flags = &d_add->flags;
3986 d_min = &d_add->min;
3987 d_max = &d_add->max;
3988 break;
3989 case LYS_DEV_REPLACE:
3990 d_rpl = calloc(1, sizeof *d_rpl);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003991 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003992 d = (struct lysp_deviate *)d_rpl;
3993 d_units = &d_rpl->units;
3994 d_flags = &d_rpl->flags;
3995 d_min = &d_rpl->min;
3996 d_max = &d_rpl->max;
3997 break;
3998 case LYS_DEV_DELETE:
3999 d_del = calloc(1, sizeof *d_del);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004000 LY_CHECK_ERR_RET(!d_del, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004001 d = (struct lysp_deviate *)d_del;
4002 d_units = &d_del->units;
4003 d_uniques = &d_del->uniques;
4004 d_dflts = &d_del->dflts;
4005 d_musts = &d_del->musts;
4006 d_flags = &d_del->flags;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004007 break;
4008 default:
4009 assert(0);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004010 LOGINT_RET(ctx->ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004011 }
4012 d->mod = dev_mod;
4013
4014 /* insert into siblings */
4015 if (!*deviates) {
4016 *deviates = d;
4017 } else {
4018 for (iter = *deviates; iter->next; iter = iter->next);
4019 iter->next = d;
4020 }
4021
Radek Krejci6d6556c2018-11-08 09:37:45 +01004022 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004023 switch (kw) {
4024 case YANG_CONFIG:
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_config(ctx, data, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004032 break;
4033 }
4034 break;
4035 case YANG_DEFAULT:
4036 switch (dev_mod) {
4037 case LYS_DEV_NOT_SUPPORTED:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004038 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004039 return LY_EVALID;
4040 case LYS_DEV_REPLACE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004041 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 +02004042 break;
4043 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004044 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 +02004045 break;
4046 }
4047 break;
4048 case YANG_MANDATORY:
4049 switch (dev_mod) {
4050 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004051 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004052 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004053 return LY_EVALID;
4054 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004055 LY_CHECK_RET(parse_mandatory(ctx, data, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004056 break;
4057 }
4058 break;
4059 case YANG_MAX_ELEMENTS:
4060 switch (dev_mod) {
4061 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004062 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004063 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004064 return LY_EVALID;
4065 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004066 LY_CHECK_RET(parse_maxelements(ctx, data, d_max, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004067 break;
4068 }
4069 break;
4070 case YANG_MIN_ELEMENTS:
4071 switch (dev_mod) {
4072 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004073 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004074 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004075 return LY_EVALID;
4076 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004077 LY_CHECK_RET(parse_minelements(ctx, data, d_min, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004078 break;
4079 }
4080 break;
4081 case YANG_MUST:
4082 switch (dev_mod) {
4083 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004084 case LYS_DEV_REPLACE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004085 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004086 return LY_EVALID;
4087 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004088 LY_CHECK_RET(parse_restrs(ctx, data, kw, d_musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004089 break;
4090 }
4091 break;
4092 case YANG_TYPE:
4093 switch (dev_mod) {
4094 case LYS_DEV_NOT_SUPPORTED:
4095 case LYS_DEV_ADD:
4096 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004097 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004098 return LY_EVALID;
4099 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004100 if (d_rpl->type) {
4101 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
4102 return LY_EVALID;
4103 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004104 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004105 LY_CHECK_ERR_RET(!d_rpl->type, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004106 LY_CHECK_RET(parse_type(ctx, data, d_rpl->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004107 break;
4108 }
4109 break;
4110 case YANG_UNIQUE:
4111 switch (dev_mod) {
4112 case LYS_DEV_NOT_SUPPORTED:
4113 case LYS_DEV_REPLACE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004114 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004115 return LY_EVALID;
4116 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004117 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 +02004118 break;
4119 }
4120 break;
4121 case YANG_UNITS:
4122 switch (dev_mod) {
4123 case LYS_DEV_NOT_SUPPORTED:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004124 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004125 return LY_EVALID;
4126 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004127 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 +02004128 break;
4129 }
4130 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, &d->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), "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004136 return LY_EVALID;
4137 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004138 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004139 return ret;
4140}
4141
Michal Vaskoea5abea2018-09-18 13:10:54 +02004142/**
4143 * @brief Parse the deviation statement.
4144 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004145 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004146 * @param[in,out] data Data to read from, always moved to currently handled character.
4147 * @param[in,out] deviations Deviations to add to.
4148 *
4149 * @return LY_ERR values.
4150 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004151static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02004152parse_deviation(struct ly_parser_ctx *ctx, const char **data, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004153{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004154 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004155 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004156 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004157 enum yang_keyword kw;
4158 struct lysp_deviation *dev;
4159
Radek Krejci2c4e7172018-10-19 15:56:26 +02004160 LY_ARRAY_NEW_RET(ctx->ctx, *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004161
4162 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004163 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02004164 INSERT_WORD(ctx, buf, dev->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004165
Radek Krejci6d6556c2018-11-08 09:37:45 +01004166 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004167 switch (kw) {
4168 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004169 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 +02004170 break;
4171 case YANG_DEVIATE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004172 LY_CHECK_RET(parse_deviate(ctx, data, &dev->deviates));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004173 break;
4174 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004175 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 +02004176 break;
4177 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004178 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004179 break;
4180 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004181 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004182 return LY_EVALID;
4183 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004184 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004185 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01004186checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004187 /* mandatory substatements */
4188 if (!dev->deviates) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004189 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004190 return LY_EVALID;
4191 }
4192
4193 return ret;
4194}
4195
Michal Vaskoea5abea2018-09-18 13:10:54 +02004196/**
4197 * @brief Parse the feature statement.
4198 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004199 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004200 * @param[in,out] data Data to read from, always moved to currently handled character.
4201 * @param[in,out] features Features to add to.
4202 *
4203 * @return LY_ERR values.
4204 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004205static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02004206parse_feature(struct ly_parser_ctx *ctx, const char **data, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004207{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004208 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004209 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004210 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004211 enum yang_keyword kw;
4212 struct lysp_feature *feat;
4213
Radek Krejci2c4e7172018-10-19 15:56:26 +02004214 LY_ARRAY_NEW_RET(ctx->ctx, *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004215
4216 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004217 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02004218 INSERT_WORD(ctx, buf, feat->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004219
Radek Krejci6d6556c2018-11-08 09:37:45 +01004220 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004221 switch (kw) {
4222 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004223 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 +02004224 break;
4225 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004226 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 +02004227 break;
4228 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004229 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 +02004230 break;
4231 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004232 LY_CHECK_RET(parse_status(ctx, data, &feat->flags, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004233 break;
4234 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004235 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004236 break;
4237 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004238 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004239 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004240 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004241 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004242 return ret;
4243}
4244
Michal Vaskoea5abea2018-09-18 13:10:54 +02004245/**
4246 * @brief Parse the identity statement.
4247 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004248 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004249 * @param[in,out] data Data to read from, always moved to currently handled character.
4250 * @param[in,out] identities Identities to add to.
4251 *
4252 * @return LY_ERR values.
4253 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004254static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02004255parse_identity(struct ly_parser_ctx *ctx, const char **data, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004256{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004257 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004258 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004259 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004260 enum yang_keyword kw;
4261 struct lysp_ident *ident;
4262
Radek Krejci2c4e7172018-10-19 15:56:26 +02004263 LY_ARRAY_NEW_RET(ctx->ctx, *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004264
4265 /* get value */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004266 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02004267 INSERT_WORD(ctx, buf, ident->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004268
Radek Krejci6d6556c2018-11-08 09:37:45 +01004269 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004270 switch (kw) {
4271 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004272 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 +02004273 break;
4274 case YANG_IF_FEATURE:
Radek Krejci10113652018-11-14 16:56:50 +01004275 YANG_CHECK_STMTVER_RET(ctx, "if-feature", "identity");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004276 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 +02004277 break;
4278 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004279 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 +02004280 break;
4281 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004282 LY_CHECK_RET(parse_status(ctx, data, &ident->flags, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004283 break;
4284 case YANG_BASE:
Radek Krejci10113652018-11-14 16:56:50 +01004285 if (ident->bases && ctx->mod->version < 2) {
4286 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Identity can be derived from multiple base identities only in YANG 1.1 modules");
4287 return LY_EVALID;
4288 }
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004289 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 +02004290 break;
4291 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004292 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004293 break;
4294 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004295 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004296 return LY_EVALID;
4297 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004298 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004299 return ret;
4300}
4301
Michal Vaskoea5abea2018-09-18 13:10:54 +02004302/**
4303 * @brief Parse the module or submodule statement.
4304 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004305 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004306 * @param[in,out] data Data to read from, always moved to currently handled character.
4307 * @param[in,out] mod Module to write to.
4308 *
4309 * @return LY_ERR values.
4310 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004311static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02004312parse_sub_module(struct ly_parser_ctx *ctx, const char **data, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004313{
4314 LY_ERR ret = 0;
4315 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004316 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004317 enum yang_keyword kw, prev_kw = 0;
4318 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Radek Krejcie9e987e2018-10-31 12:50:27 +01004319 struct lysp_module *dup;
Radek Krejcie86bf772018-12-14 11:39:53 +01004320 struct lysp_node *child;
4321 unsigned int u;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004322
4323 /* (sub)module name */
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004324 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02004325 INSERT_WORD(ctx, buf, mod->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004326
Radek Krejci6d6556c2018-11-08 09:37:45 +01004327 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004328
Radek Krejcie3846472018-10-15 15:24:51 +02004329#define CHECK_ORDER(SECTION) \
4330 if (mod_stmt > SECTION) {LOGVAL_YANG(ctx, LY_VCODE_INORD, ly_stmt2str(kw), ly_stmt2str(prev_kw)); return LY_EVALID;}mod_stmt = SECTION
4331
Michal Vasko7fbc8162018-09-17 10:35:16 +02004332 switch (kw) {
4333 /* module header */
4334 case YANG_NAMESPACE:
4335 case YANG_PREFIX:
4336 if (mod->submodule) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004337 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004338 return LY_EVALID;
4339 }
Radek Krejcie3846472018-10-15 15:24:51 +02004340 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4341 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004342 case YANG_BELONGS_TO:
Radek Krejcie3846472018-10-15 15:24:51 +02004343 if (!mod->submodule) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004344 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004345 return LY_EVALID;
4346 }
Radek Krejcie3846472018-10-15 15:24:51 +02004347 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4348 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004349 case YANG_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004350 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004351 break;
4352 /* linkage */
4353 case YANG_INCLUDE:
4354 case YANG_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004355 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004356 break;
4357 /* meta */
4358 case YANG_ORGANIZATION:
4359 case YANG_CONTACT:
4360 case YANG_DESCRIPTION:
4361 case YANG_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004362 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004363 break;
4364
4365 /* revision */
4366 case YANG_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004367 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004368 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004369 /* body */
4370 case YANG_ANYDATA:
4371 case YANG_ANYXML:
4372 case YANG_AUGMENT:
4373 case YANG_CHOICE:
4374 case YANG_CONTAINER:
4375 case YANG_DEVIATION:
4376 case YANG_EXTENSION:
4377 case YANG_FEATURE:
4378 case YANG_GROUPING:
4379 case YANG_IDENTITY:
4380 case YANG_LEAF:
4381 case YANG_LEAF_LIST:
4382 case YANG_LIST:
4383 case YANG_NOTIFICATION:
4384 case YANG_RPC:
4385 case YANG_TYPEDEF:
4386 case YANG_USES:
4387 case YANG_CUSTOM:
4388 mod_stmt = Y_MOD_BODY;
4389 break;
4390 default:
4391 /* error handled in the next switch */
4392 break;
4393 }
Radek Krejcie3846472018-10-15 15:24:51 +02004394#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004395
Radek Krejcie3846472018-10-15 15:24:51 +02004396 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004397 switch (kw) {
4398 /* module header */
4399 case YANG_YANG_VERSION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004400 LY_CHECK_RET(parse_yangversion(ctx, data, mod));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004401 break;
4402 case YANG_NAMESPACE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004403 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 +02004404 break;
4405 case YANG_PREFIX:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004406 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 +02004407 LY_CHECK_RET(lysp_check_prefix(ctx, mod, &mod->prefix), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004408 break;
4409 case YANG_BELONGS_TO:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004410 LY_CHECK_RET(parse_belongsto(ctx, data, &mod->belongsto, &mod->prefix, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004411 break;
4412
4413 /* linkage */
4414 case YANG_INCLUDE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004415 LY_CHECK_RET(parse_include(ctx, data, mod));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004416 break;
4417 case YANG_IMPORT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004418 LY_CHECK_RET(parse_import(ctx, data, mod));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004419 break;
4420
4421 /* meta */
4422 case YANG_ORGANIZATION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004423 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 +02004424 break;
4425 case YANG_CONTACT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004426 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 +02004427 break;
4428 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004429 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 +02004430 break;
4431 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004432 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 +02004433 break;
4434
4435 /* revision */
4436 case YANG_REVISION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004437 LY_CHECK_RET(parse_revision(ctx, data, &mod->revs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004438 break;
4439
4440 /* body */
4441 case YANG_ANYDATA:
Radek Krejci10113652018-11-14 16:56:50 +01004442 YANG_CHECK_STMTVER_RET(ctx, "anydata", mod->submodule ? "submodule" : "module");
4443 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004444 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004445 LY_CHECK_RET(parse_any(ctx, data, kw, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004446 break;
4447 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004448 LY_CHECK_RET(parse_choice(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004449 break;
4450 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004451 LY_CHECK_RET(parse_container(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004452 break;
4453 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004454 LY_CHECK_RET(parse_leaf(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004455 break;
4456 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004457 LY_CHECK_RET(parse_leaflist(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004458 break;
4459 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004460 LY_CHECK_RET(parse_list(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004461 break;
4462 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004463 LY_CHECK_RET(parse_uses(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004464 break;
4465
4466 case YANG_AUGMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004467 LY_CHECK_RET(parse_augment(ctx, data, NULL, &mod->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004468 break;
4469 case YANG_DEVIATION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004470 LY_CHECK_RET(parse_deviation(ctx, data, &mod->deviations));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004471 break;
4472 case YANG_EXTENSION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004473 LY_CHECK_RET(parse_extension(ctx, data, &mod->extensions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004474 break;
4475 case YANG_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004476 LY_CHECK_RET(parse_feature(ctx, data, &mod->features));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004477 break;
4478 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004479 LY_CHECK_RET(parse_grouping(ctx, data, NULL, &mod->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004480 break;
4481 case YANG_IDENTITY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004482 LY_CHECK_RET(parse_identity(ctx, data, &mod->identities));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004483 break;
4484 case YANG_NOTIFICATION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004485 LY_CHECK_RET(parse_notif(ctx, data, NULL, &mod->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004486 break;
4487 case YANG_RPC:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004488 LY_CHECK_RET(parse_action(ctx, data, NULL, &mod->rpcs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004489 break;
4490 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01004491 LY_CHECK_RET(parse_typedef(ctx, NULL, data, &mod->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004492 break;
4493 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004494 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004495 break;
4496
4497 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004498 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), mod->submodule ? "submodule" : "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004499 return LY_EVALID;
4500 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004501 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004502 LY_CHECK_RET(ret);
Radek Krejcie86bf772018-12-14 11:39:53 +01004503
4504 /* finalize parent pointers to the reallocated items */
4505 LY_ARRAY_FOR(mod->groupings, u) {
4506 LY_LIST_FOR(mod->groupings[u].data, child) {
4507 child->parent = (struct lysp_node*)&mod->groupings[u];
4508 }
4509 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004510 /* TODO the same finalization for rpcs and notifications, do also in the relevant nodes */
Radek Krejcie86bf772018-12-14 11:39:53 +01004511
Radek Krejci6d6556c2018-11-08 09:37:45 +01004512checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004513 /* mandatory substatements */
4514 if (mod->submodule) {
4515 if (!mod->belongsto) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004516 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004517 return LY_EVALID;
4518 }
4519 } else {
4520 if (!mod->ns) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004521 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004522 return LY_EVALID;
4523 } else if (!mod->prefix) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004524 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004525 return LY_EVALID;
4526 }
4527 }
4528
Radek Krejcie9e987e2018-10-31 12:50:27 +01004529 /* submodules share the namespace with the module names, so there must not be
4530 * a submodule of the same name in the context, no need for revision matching */
4531 dup = ly_ctx_get_submodule(ctx->ctx, NULL, mod->name, NULL);
4532 if (dup && (!mod->submodule || strcmp(dup->belongsto, mod->belongsto))) {
4533 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Name collision between %s of name \"%s\".",
4534 mod->submodule ? "submodules" : "module and submodule", mod->name);
4535 return LY_EVALID;
4536 }
4537
Michal Vasko7fbc8162018-09-17 10:35:16 +02004538 return ret;
4539}
4540
Radek Krejcid4557c62018-09-17 11:42:09 +02004541LY_ERR
Radek Krejcibbe09a92018-11-08 09:36:54 +01004542yang_parse(struct ly_parser_ctx *context, const char *data, struct lysp_module **mod_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004543{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004544 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004545 char *word, *buf;
Radek Krejciefd22f62018-09-27 11:47:58 +02004546 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004547 enum yang_keyword kw;
Radek Krejci0c2cf322018-10-13 08:02:30 +02004548 struct lysp_module *mod = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004549
4550 /* "module"/"submodule" */
Radek Krejcibbe09a92018-11-08 09:36:54 +01004551 ret = get_keyword(context, &data, &kw, &word, &word_len);
4552 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004553
4554 if ((kw != YANG_MODULE) && (kw != YANG_SUBMODULE)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +01004555 LOGVAL_YANG(context, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".",
Radek Krejcic59bc972018-09-17 16:13:06 +02004556 ly_stmt2str(kw));
Radek Krejcibbe09a92018-11-08 09:36:54 +01004557 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004558 }
4559
4560 mod = calloc(1, sizeof *mod);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004561 LY_CHECK_ERR_GOTO(!mod, LOGMEM(context->ctx), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004562 if (kw == YANG_SUBMODULE) {
4563 mod->submodule = 1;
4564 }
Radek Krejcibbe09a92018-11-08 09:36:54 +01004565 mod->parsing = 1;
4566 mod->ctx = context->ctx;
4567 context->mod = mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004568
4569 /* substatements */
Radek Krejcibbe09a92018-11-08 09:36:54 +01004570 ret = parse_sub_module(context, &data, mod);
4571 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004572
4573 /* read some trailing spaces or new lines */
Radek Krejcibbe09a92018-11-08 09:36:54 +01004574 ret = get_argument(context, &data, Y_MAYBE_STR_ARG, &word, &buf, &word_len);
4575 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004576
4577 if (word) {
Radek Krejcibbe09a92018-11-08 09:36:54 +01004578 LOGVAL_YANG(context, LYVE_SYNTAX, "Invalid character sequence \"%.*s\", expected end-of-file.",
Michal Vasko7fbc8162018-09-17 10:35:16 +02004579 word_len, word);
4580 free(buf);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004581 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004582 }
4583 assert(!buf);
4584
Radek Krejcibbe09a92018-11-08 09:36:54 +01004585 mod->parsing = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004586 *mod_p = mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004587
Radek Krejcibbe09a92018-11-08 09:36:54 +01004588cleanup:
4589 if (ret) {
4590 lysp_module_free(mod);
4591 }
4592
Michal Vasko7fbc8162018-09-17 10:35:16 +02004593 return ret;
4594}