blob: 69482e0827f5b9cbd4e33c0fa3d04a14afa0f7b0 [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
44/* These 2 macros checks YANG's identifier grammar rule */
45#define is_yangidentstartchar(c) ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_')
46#define is_yangidentchar(c) ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || \
47 c == '_' || c == '-' || c == '.')
48
Radek Krejci9fcacc12018-10-11 15:59:11 +020049#define INSERT_WORD(CTX, BUF, TARGET, WORD, LEN) \
50 if (BUF) {(TARGET) = lydict_insert_zc((CTX)->ctx, WORD);}\
51 else {(TARGET) = lydict_insert((CTX)->ctx, WORD, LEN);}
Radek Krejci44ceedc2018-10-02 15:54:31 +020052
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020053#define MOVE_INPUT(CTX, DATA, COUNT) (*(data))+=COUNT;(CTX)->indent+=COUNT
54
Radek Krejci44ceedc2018-10-02 15:54:31 +020055#define LOGVAL_YANG(CTX, ...) LOGVAL((CTX)->ctx, LY_VLOG_LINE, &(CTX)->line, __VA_ARGS__)
56
Michal Vaskoea5abea2018-09-18 13:10:54 +020057/**
58 * @brief Loop through all substatements providing, return if there are none.
59 *
Radek Krejci44ceedc2018-10-02 15:54:31 +020060 * @param[in] CTX yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +020061 * @param[in] DATA Raw data to read from.
62 * @param[out] KW YANG keyword read.
63 * @param[out] WORD Pointer to the keyword itself.
64 * @param[out] WORD_LEN Length of the keyword.
65 * @param[out] ERR Variable for error storing.
66 *
67 * @return In case there are no substatements or a fatal error encountered.
68 */
Michal Vasko7fbc8162018-09-17 10:35:16 +020069#define YANG_READ_SUBSTMT_FOR(CTX, DATA, KW, WORD, WORD_LEN, ERR) \
70 ERR = get_keyword(CTX, DATA, &KW, &WORD, &WORD_LEN); \
Radek Krejcic59bc972018-09-17 16:13:06 +020071 LY_CHECK_RET(ERR); \
Michal Vasko7fbc8162018-09-17 10:35:16 +020072 \
73 if (KW == YANG_SEMICOLON) { \
74 return ERR; \
75 } \
76 if (KW != YANG_LEFT_BRACE) { \
Radek Krejci44ceedc2018-10-02 15:54:31 +020077 LOGVAL_YANG(CTX, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\", expected \";\" or \"{\".", ly_stmt2str(KW)); \
Michal Vasko7fbc8162018-09-17 10:35:16 +020078 return LY_EVALID; \
79 } \
80 for (ERR = get_keyword(CTX, DATA, &KW, &WORD, &WORD_LEN); \
81 !ERR && (KW != YANG_RIGHT_BRACE); \
82 ERR = get_keyword(CTX, DATA, &KW, &WORD, &WORD_LEN))
83
Radek Krejci44ceedc2018-10-02 15:54:31 +020084static LY_ERR parse_container(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings);
85static LY_ERR parse_uses(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings);
86static LY_ERR parse_choice(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings);
87static LY_ERR parse_case(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings);
88static LY_ERR parse_list(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings);
89static LY_ERR parse_grouping(struct ly_parser_ctx *ctx, const char **data, struct lysp_grp **groupings);
Michal Vasko7fbc8162018-09-17 10:35:16 +020090
Michal Vaskoea5abea2018-09-18 13:10:54 +020091/**
92 * @brief Add another character to dynamic buffer, a low-level function.
93 *
Radek Krejci44ceedc2018-10-02 15:54:31 +020094 * Enlarge if needed. Updates \p input as well as \p buf_used.
Michal Vaskoea5abea2018-09-18 13:10:54 +020095 *
Radek Krejci404251e2018-10-09 12:06:44 +020096 * @param[in] ctx libyang context for logging.
Radek Krejci44ceedc2018-10-02 15:54:31 +020097 * @param[in, out] input Input string to process.
98 * @param[in] len Number of bytes to get from the input string and copy into the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +020099 * @param[in,out] buf Buffer to use, can be moved by realloc().
100 * @param[in,out] buf_len Current size of the buffer.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200101 * @param[in,out] buf_used Currently used characters of the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200102 *
103 * @return LY_ERR values.
104 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200105static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +0200106buf_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 +0200107{
Radek Krejci44ceedc2018-10-02 15:54:31 +0200108 if (*buf_len <= (*buf_used) + len) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200109 *buf_len += 16;
110 *buf = ly_realloc(*buf, *buf_len);
111 LY_CHECK_ERR_RET(!*buf, LOGMEM(ctx), LY_EMEM);
112 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200113 memcpy(&(*buf)[*buf_used], *input, len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200114
Radek Krejci44ceedc2018-10-02 15:54:31 +0200115 (*buf_used) += len;
116 (*input) += len;
117
Michal Vasko7fbc8162018-09-17 10:35:16 +0200118 return LY_SUCCESS;
119}
120
Michal Vaskoea5abea2018-09-18 13:10:54 +0200121/**
Radek Krejci44ceedc2018-10-02 15:54:31 +0200122 * @brief Check that \p c is valid UTF8 code point for YANG string.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200123 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200124 * @param[in] ctx yang parser context for logging.
125 * @param[in] c UTF8 code point of a character to check.
126 * @return LY_ERR values.
127 */
128static LY_ERR
129check_stringchar(struct ly_parser_ctx *ctx, unsigned int c)
130{
131 if (!is_yangutf8char(c)) {
132 LOGVAL_YANG(ctx, LY_VCODE_INCHAR, c);
133 return LY_EVALID;
134 }
135 return LY_SUCCESS;
136}
137
138/**
139 * @brief Check that \p c is valid UTF8 code point for YANG identifier.
140 *
141 * @param[in] ctx yang parser context for logging.
142 * @param[in] c UTF8 code point of a character to check.
143 * @param[in] first Flag to check the first character of an identifier, which is more restricted.
Radek Krejcidcc7b322018-10-11 14:24:02 +0200144 * @param[in,out] prefix Storage for internally used flag in case of possible prefixed identifiers:
145 * 0 - colon not yet found (no prefix)
146 * 1 - \p c is the colon character
147 * 2 - prefix already processed, now processing the identifier
148 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200149 * If the identifier cannot be prefixed, NULL is expected.
150 * @return LY_ERR values.
151 */
152static LY_ERR
153check_identifierchar(struct ly_parser_ctx *ctx, unsigned int c, int first, int *prefix)
154{
155 if (first || (prefix && (*prefix) == 1)) {
156 if (!is_yangidentstartchar(c)) {
157 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '%c'.", c);
158 return LY_EVALID;
159 }
Radek Krejci9fcacc12018-10-11 15:59:11 +0200160 if (prefix) {
161 if (first) {
162 (*prefix) = 0;
163 } else {
164 (*prefix) = 2;
165 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200166 }
167 } else if (c == ':' && prefix && (*prefix) == 0) {
168 (*prefix) = 1;
169 } else if (!is_yangidentchar(c)) {
170 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Invalid identifier character '%c'.", c);
171 return LY_EVALID;
172 }
173
174 return LY_SUCCESS;
175}
176
177/**
178 * @brief Store a single UTF8 character. It depends whether in a dynamically-allocated buffer or just as a pointer to the data.
179 *
180 * @param[in] ctx yang parser context for logging.
181 * @param[in,out] input Pointer to the string where to get the character to store. Automatically moved to the next character
182 * when function returns.
183 * @param[in] arg Type of the input string to select method of checking character validity.
184 * @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 +0200185 * 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 +0200186 * @param[in,out] word_len Current length of the word pointed to by \p word_p.
187 * @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 +0200188 * @param[in,out] buf_len Current length of \p word_b.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200189 * @param[in] need_buf Flag if the dynamically allocated buffer is required.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200190 *
191 * @return LY_ERR values.
192 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200193static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +0200194buf_store_char(struct ly_parser_ctx *ctx, const char **input, enum yang_arg arg,
195 char **word_p, size_t *word_len, char **word_b, size_t *buf_len, int need_buf)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200196{
Radek Krejci404251e2018-10-09 12:06:44 +0200197 int prefix = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200198 unsigned int c;
199 size_t len;
200
201 /* get UTF8 code point (and number of bytes coding the character) */
202 LY_CHECK_ERR_RET(ly_getutf8(input, &c, &len),
203 LOGVAL_YANG(ctx, LY_VCODE_INCHAR, (*input)[-len]), LY_EVALID);
204 (*input) -= len;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200205 if (c == '\n') {
206 ctx->indent = 0;
207 } else {
208 /* note - even the multibyte character is count as 1 */
209 ++ctx->indent;
210 }
211
Radek Krejci44ceedc2018-10-02 15:54:31 +0200212 /* check character validity */
213 switch (arg) {
214 case Y_IDENTIF_ARG:
215 LY_CHECK_RET(check_identifierchar(ctx, c, !(*word_len), NULL));
216 break;
217 case Y_PREF_IDENTIF_ARG:
218 LY_CHECK_RET(check_identifierchar(ctx, c, !(*word_len), &prefix));
219 break;
220 case Y_STR_ARG:
221 case Y_MAYBE_STR_ARG:
222 LY_CHECK_RET(check_stringchar(ctx, c));
223 break;
224 }
225
Michal Vasko7fbc8162018-09-17 10:35:16 +0200226 if (word_b && *word_b) {
227 /* add another character into buffer */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200228 if (buf_add_char(ctx->ctx, input, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200229 return LY_EMEM;
230 }
231
232 /* in case of realloc */
233 *word_p = *word_b;
234 } else if (need_buf) {
235 /* first time we need a buffer, copy everything read up to now */
236 if (*word_len) {
237 *word_b = malloc(*word_len);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200238 LY_CHECK_ERR_RET(!*word_b, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200239 *buf_len = *word_len;
240 memcpy(*word_b, *word_p, *word_len);
241 }
242
243 /* add this new character into buffer */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200244 if (buf_add_char(ctx->ctx, input, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200245 return LY_EMEM;
246 }
247
248 /* in case of realloc */
249 *word_p = *word_b;
250 } else {
251 /* just remember the first character pointer */
252 if (!*word_p) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200253 *word_p = (char *)(*input);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200254 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200255 /* ... and update the word's length */
256 (*word_len) += len;
257 (*input) += len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200258 }
259
260 return LY_SUCCESS;
261}
262
Michal Vaskoea5abea2018-09-18 13:10:54 +0200263/**
264 * @brief Skip YANG comment in data.
265 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200266 * @param[in] ctx yang parser context for logging.
267 * @param[in,out] data Data to read from, automatically moved after the comment.
268 * @param[in] comment Type of the comment to process:
269 * 1 for a one-line comment,
270 * 2 for a block comment.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200271 *
272 * @return LY_ERR values.
273 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200274static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +0200275skip_comment(struct ly_parser_ctx *ctx, const char **data, int comment)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200276{
Radek Krejci80dd33e2018-09-26 15:57:18 +0200277 /* internal statuses: 0 - comment ended,
278 * 1 - in line comment,
279 * 2 - in block comment,
280 * 3 - in block comment with last read character '*'
281 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200282 while (**data && comment) {
283 switch (comment) {
284 case 1:
285 if (**data == '\n') {
286 comment = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200287 ++ctx->line;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200288 }
289 break;
290 case 2:
291 if (**data == '*') {
Radek Krejci15c80ca2018-10-09 11:01:31 +0200292 comment = 3;
293 } else if (**data == '\n') {
294 ++ctx->line;
295 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200296 break;
297 case 3:
298 if (**data == '/') {
299 comment = 0;
300 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200301 if (**data == '\n') {
302 ++ctx->line;
303 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200304 comment = 2;
305 }
306 break;
307 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200308 LOGINT_RET(ctx->ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200309 }
310
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200311 if (**data == '\n') {
312 ctx->indent = 0;
313 } else {
314 ++ctx->indent;
315 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200316 ++(*data);
317 }
318
319 if (!**data && (comment > 1)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200320 LOGVAL_YANG(ctx, LYVE_SYNTAX, "Unexpected end-of-file, non-terminated comment.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200321 return LY_EVALID;
322 }
323
324 return LY_SUCCESS;
325}
326
Michal Vaskoea5abea2018-09-18 13:10:54 +0200327/**
328 * @brief Read a quoted string from data.
329 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200330 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200331 * @param[in,out] data Data to read from, always moved to currently handled character.
332 * @param[in] arg Type of YANG keyword argument expected.
333 * @param[out] word_p Pointer to the read quoted string.
334 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read quoted string. If not needed,
335 * set to NULL. Otherwise equal to \p word_p.
336 * @param[out] word_len Length of the read quoted string.
337 * @param[out] buf_len Length of the dynamically-allocated buffer \p word_b.
338 * @param[in] indent Current indent (number of YANG spaces). Needed for correct multi-line string
339 * indenation in the final quoted string.
340 *
341 * @return LY_ERR values.
342 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200343static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +0200344read_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 +0200345 size_t *buf_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200346{
347 /* string: 0 - string ended, 1 - string with ', 2 - string with ", 3 - string with " with last character \,
348 * 4 - string finished, now skipping whitespaces looking for +,
349 * 5 - string continues after +, skipping whitespaces */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200350 unsigned int string, block_indent = 0, current_indent = 0, need_buf = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200351 const char *c;
352
353 if (**data == '\"') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200354 string = 2;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200355 current_indent = block_indent = ctx->indent + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200356 } else {
357 assert(**data == '\'');
358 string = 1;
359 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200360 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200361
362 while (**data && string) {
363 switch (string) {
364 case 1:
365 switch (**data) {
366 case '\'':
367 /* string may be finished, but check for + */
368 string = 4;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200369 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200370 break;
371 default:
372 /* check and store character */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200373 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 +0200374 break;
375 }
376 break;
377 case 2:
378 switch (**data) {
379 case '\"':
380 /* string may be finished, but check for + */
381 string = 4;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200382 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200383 break;
384 case '\\':
385 /* special character following */
386 string = 3;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200387 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200388 break;
389 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200390 if (current_indent < block_indent) {
391 ++current_indent;
392 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200393 } else {
394 /* check and store character */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200395 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 +0200396 }
397 break;
398 case '\t':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200399 if (current_indent < block_indent) {
400 assert(need_buf);
401 current_indent += 8;
402 ctx->indent += 8;
403 for (; current_indent > block_indent; --current_indent, --ctx->indent) {
404 /* store leftover spaces from the tab */
405 c = " ";
406 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 +0200407 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200408 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200409 } else {
410 /* check and store character */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200411 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 +0200412 /* additional characters for indentation - only 1 was count in buf_store_char */
413 ctx->indent += 7;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200414 }
415 break;
416 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200417 if (block_indent) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200418 /* we will be removing the indents so we need our own buffer */
419 need_buf = 1;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200420
421 /* remove trailing tabs and spaces */
422 while ((*word_len) && ((*word_p)[(*word_len) - 1] == '\t' || (*word_p)[(*word_len) - 1] == ' ')) {
423 --(*word_len);
424 }
425
426 /* start indentation */
427 current_indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200428 }
429
430 /* check and store character */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200431 LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, buf_len, need_buf));
432
433 /* maintain line number */
434 ++ctx->line;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200435
436 /* reset context indentation counter for possible string after this one */
437 ctx->indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200438 break;
439 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200440 /* first non-whitespace character, stop eating indentation */
441 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200442
443 /* check and store character */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200444 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 +0200445 break;
446 }
447 break;
448 case 3:
449 /* string encoded characters */
450 switch (**data) {
451 case 'n':
452 c = "\n";
453 break;
454 case 't':
455 c = "\t";
456 break;
457 case '\"':
458 c = *data;
459 break;
460 case '\\':
461 c = *data;
462 break;
463 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200464 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Double-quoted string unknown special character '\\%c'.", **data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200465 return LY_EVALID;
466 }
467
468 /* check and store character */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200469 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 +0200470
471 string = 2;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200472 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200473 break;
474 case 4:
475 switch (**data) {
476 case '+':
477 /* string continues */
478 string = 5;
Radek Krejciefd22f62018-09-27 11:47:58 +0200479 need_buf = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200480 break;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200481 case '\n':
482 ++ctx->line;
483 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200484 case ' ':
485 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200486 /* just skip */
487 break;
488 default:
489 /* string is finished */
490 goto string_end;
491 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200492 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200493 break;
494 case 5:
495 switch (**data) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200496 case '\n':
497 ++ctx->line;
498 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200499 case ' ':
500 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200501 /* skip */
502 break;
503 case '\'':
504 string = 1;
505 break;
506 case '\"':
507 string = 2;
508 break;
509 default:
510 /* it must be quoted again */
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200511 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Both string parts divided by '+' must be quoted.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200512 return LY_EVALID;
513 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200514 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200515 break;
516 default:
517 return LY_EINT;
518 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200519 }
520
521string_end:
522 return LY_SUCCESS;
523}
524
Michal Vaskoea5abea2018-09-18 13:10:54 +0200525/**
526 * @brief Get another YANG string from the raw data.
527 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200528 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200529 * @param[in,out] data Data to read from, always moved to currently handled character.
530 * @param[in] arg Type of YANG keyword argument expected.
Michal Vasko2ca70f52018-09-27 11:04:51 +0200531 * @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 +0200532 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read string. If not needed,
533 * set to NULL. Otherwise equal to \p word_p.
534 * @param[out] word_len Length of the read string.
535 *
536 * @return LY_ERR values.
Michal Vasko7fbc8162018-09-17 10:35:16 +0200537 */
538static LY_ERR
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200539get_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 +0200540{
Radek Krejci44ceedc2018-10-02 15:54:31 +0200541 size_t buf_len = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200542 LY_ERR ret;
543
544 /* word buffer - dynamically allocated */
545 *word_b = NULL;
546
547 /* word pointer - just a pointer to data */
548 *word_p = NULL;
549
550 *word_len = 0;
551 while (**data) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200552 switch (**data) {
553 case '\'':
554 case '\"':
555 if (*word_len) {
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200556 /* invalid - quotes cannot be in unquoted string and only optsep, ; or { can follow it */
557 LOGVAL_YANG(ctx, LY_VCODE_INSTREXP, 1, *data,
558 "unquoted string character, optsep, semicolon or opening brace");
559 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200560 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200561 ret = read_qstring(ctx, data, arg, word_p, word_b, word_len, &buf_len);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200562 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200563 goto str_end;
564 case '/':
Radek Krejci44ceedc2018-10-02 15:54:31 +0200565 if ((*data)[1] == '/') {
566 /* one-line comment */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200567 MOVE_INPUT(ctx, data, 2);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200568 ret = skip_comment(ctx, data, 1);
569 } else if ((*data)[1] == '*') {
570 /* block comment */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200571 MOVE_INPUT(ctx, data, 2);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200572 ret = skip_comment(ctx, data, 2);
573 } else {
574 /* not a comment after all */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200575 ret = buf_store_char(ctx, data, arg, word_p, word_len, word_b, &buf_len, 0);
576 }
577 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200578 break;
579 case ' ':
580 if (*word_len) {
581 /* word is finished */
582 goto str_end;
583 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200584 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200585 break;
586 case '\t':
587 if (*word_len) {
588 /* word is finished */
589 goto str_end;
590 }
591 /* tabs count for 8 spaces */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200592 ctx->indent += 8;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200593
594 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200595 break;
596 case '\n':
597 if (*word_len) {
598 /* word is finished */
599 goto str_end;
600 }
601 /* reset indent */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200602 ctx->indent = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200603
604 /* track line numbers */
605 ++ctx->line;
606
607 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200608 break;
609 case ';':
610 case '{':
611 if (*word_len || (arg == Y_MAYBE_STR_ARG)) {
612 /* word is finished */
613 goto str_end;
614 }
615
Radek Krejci44ceedc2018-10-02 15:54:31 +0200616 LOGVAL_YANG(ctx, LY_VCODE_INSTREXP, 1, *data, "an argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200617 return LY_EVALID;
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200618 case '}':
619 /* invalid - braces cannot be in unquoted string (opening braces terminates the string and can follow it) */
620 LOGVAL_YANG(ctx, LY_VCODE_INSTREXP, 1, *data,
621 "unquoted string character, optsep, semicolon or opening brace");
622 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200623 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200624 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 +0200625 break;
626 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200627 }
628
629str_end:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200630 /* terminating NULL byte for buf */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200631 if (*word_b) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200632 (*word_b) = ly_realloc(*word_b, (*word_len) + 1);
633 LY_CHECK_ERR_RET(!(*word_b), LOGMEM(ctx->ctx), LY_EMEM);
634 (*word_b)[*word_len] = '\0';
Michal Vasko7fbc8162018-09-17 10:35:16 +0200635 *word_p = *word_b;
636 }
637
638 return LY_SUCCESS;
639}
640
Michal Vaskoea5abea2018-09-18 13:10:54 +0200641/**
642 * @brief Get another YANG keyword from the raw data.
643 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200644 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200645 * @param[in,out] data Data to read from, always moved to currently handled character.
646 * @param[out] kw YANG keyword read.
647 * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances.
648 * @param[out] word_len Length of the keyword in the data. Useful for extension instances.
649 *
650 * @return LY_ERR values.
651 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200652static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +0200653get_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 +0200654{
655 LY_ERR ret;
656 int prefix;
657 const char *word_start;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200658 unsigned int c;
659 size_t len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200660
661 if (word_p) {
662 *word_p = NULL;
663 *word_len = 0;
664 }
665
666 /* first skip "optsep", comments */
667 while (**data) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200668 switch (**data) {
669 case '/':
670 if ((*data)[1] == '/') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200671 /* one-line comment */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200672 MOVE_INPUT(ctx, data, 2);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200673 ret = skip_comment(ctx, data, 1);
674 if (ret) {
675 return ret;
676 }
Radek Krejcidcc7b322018-10-11 14:24:02 +0200677 } else if ((*data)[1] == '*') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200678 /* block comment */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200679 MOVE_INPUT(ctx, data, 2);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200680 ret = skip_comment(ctx, data, 2);
681 if (ret) {
682 return ret;
683 }
684 } else {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200685 /* error - not a comment after all, keyword cannot start with slash */
686 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '/'.");
687 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200688 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200689 break;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200690 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200691 /* skip whitespaces (optsep) */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200692 ++ctx->line;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200693 ctx->indent = 0;
694 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200695 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200696 /* skip whitespaces (optsep) */
697 ++ctx->indent;
698 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200699 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200700 /* skip whitespaces (optsep) */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200701 ctx->indent += 8;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200702 break;
703 default:
704 /* either a keyword start or an invalid character */
705 goto keyword_start;
706 }
707
708 ++(*data);
709 }
710
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200711#define IF_KW(STR, LEN, STMT) if (!strncmp(*(data), STR, LEN)) {MOVE_INPUT(ctx, data, LEN);*kw=STMT;}
712#define IF_KW_PREFIX(STR, LEN) if (!strncmp(*(data), STR, LEN)) {MOVE_INPUT(ctx, data, LEN);
713#define IF_KW_PREFIX_END }
714
Michal Vasko7fbc8162018-09-17 10:35:16 +0200715keyword_start:
716 word_start = *data;
717 *kw = YANG_NONE;
718
719 /* read the keyword itself */
720 switch (**data) {
721 case 'a':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200722 MOVE_INPUT(ctx, data, 1);
723 IF_KW("rgument", 7, YANG_ARGUMENT)
724 else IF_KW("ugment", 6, YANG_AUGMENT)
725 else IF_KW("ction", 5, YANG_ACTION)
726 else IF_KW_PREFIX("ny", 2)
727 IF_KW("data", 4, YANG_ANYDATA)
728 else IF_KW("xml", 3, YANG_ANYXML)
729 IF_KW_PREFIX_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200730 break;
731 case 'b':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200732 MOVE_INPUT(ctx, data, 1);
733 IF_KW("ase", 3, YANG_BASE)
734 else IF_KW("elongs-to", 9, YANG_BELONGS_TO)
735 else IF_KW("it", 2, YANG_BIT)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200736 break;
737 case 'c':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200738 MOVE_INPUT(ctx, data, 1);
739 IF_KW("ase", 3, YANG_CASE)
740 else IF_KW("hoice", 5, YANG_CHOICE)
741 else IF_KW_PREFIX("on", 2)
742 IF_KW("fig", 3, YANG_CONFIG)
743 else IF_KW_PREFIX("ta", 2)
744 IF_KW("ct", 2, YANG_CONTACT)
745 else IF_KW("iner", 4, YANG_CONTAINER)
746 IF_KW_PREFIX_END
747 IF_KW_PREFIX_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200748 break;
749 case 'd':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200750 MOVE_INPUT(ctx, data, 1);
751 IF_KW_PREFIX("e", 1)
752 IF_KW("fault", 5, YANG_DEFAULT)
753 else IF_KW("scription", 9, YANG_DESCRIPTION)
754 else IF_KW_PREFIX("viat", 4)
755 IF_KW("e", 1, YANG_DEVIATE)
756 else IF_KW("ion", 3, YANG_DEVIATION)
757 IF_KW_PREFIX_END
758 IF_KW_PREFIX_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200759 break;
760 case 'e':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200761 MOVE_INPUT(ctx, data, 1);
762 IF_KW("num", 3, YANG_ENUM)
763 else IF_KW_PREFIX("rror-", 5)
764 IF_KW("app-tag", 7, YANG_ERROR_APP_TAG)
765 else IF_KW("message", 7, YANG_ERROR_MESSAGE)
766 IF_KW_PREFIX_END
767 else IF_KW("xtension", 8, YANG_EXTENSION)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200768 break;
769 case 'f':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200770 MOVE_INPUT(ctx, data, 1);
771 IF_KW("eature", 6, YANG_FEATURE)
772 else IF_KW("raction-digits", 14, YANG_FRACTION_DIGITS)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200773 break;
774 case 'g':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200775 MOVE_INPUT(ctx, data, 1);
776 IF_KW("rouping", 7, YANG_GROUPING)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200777 break;
778 case 'i':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200779 MOVE_INPUT(ctx, data, 1);
780 IF_KW("dentity", 7, YANG_IDENTITY)
781 else IF_KW("f-feature", 9, YANG_IF_FEATURE)
782 else IF_KW("mport", 5, YANG_IMPORT)
783 else IF_KW_PREFIX("n", 1)
784 IF_KW("clude", 5, YANG_INCLUDE)
785 else IF_KW("put", 3, YANG_INPUT)
786 IF_KW_PREFIX_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200787 break;
788 case 'k':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200789 MOVE_INPUT(ctx, data, 1);
790 IF_KW("ey", 2, YANG_KEY)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200791 break;
792 case 'l':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200793 MOVE_INPUT(ctx, data, 1);
794 IF_KW_PREFIX("e", 1)
795 IF_KW("af-list", 7, YANG_LEAF_LIST)
796 else IF_KW("af", 2, YANG_LEAF)
797 else IF_KW("ngth", 4, YANG_LENGTH)
798 IF_KW_PREFIX_END
799 else IF_KW("ist", 3, YANG_LIST)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200800 break;
801 case 'm':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200802 MOVE_INPUT(ctx, data, 1);
803 IF_KW_PREFIX("a", 1)
804 IF_KW("ndatory", 7, YANG_MANDATORY)
805 else IF_KW("x-elements", 10, YANG_MAX_ELEMENTS)
806 IF_KW_PREFIX_END
807 else IF_KW("in-elements", 11, YANG_MIN_ELEMENTS)
808 else IF_KW("ust", 3, YANG_MUST)
809 else IF_KW_PREFIX("od", 2)
810 IF_KW("ule", 3, YANG_MODULE)
811 else IF_KW("ifier", 5, YANG_MODIFIER)
812 IF_KW_PREFIX_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200813 break;
814 case 'n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200815 MOVE_INPUT(ctx, data, 1);
816 IF_KW("amespace", 8, YANG_NAMESPACE)
817 else IF_KW("otification", 11, YANG_NOTIFICATION)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200818 break;
819 case 'o':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200820 MOVE_INPUT(ctx, data, 1);
821 IF_KW_PREFIX("r", 1)
822 IF_KW("dered-by", 8, YANG_ORDERED_BY)
823 else IF_KW("ganization", 10, YANG_ORGANIZATION)
824 IF_KW_PREFIX_END
825 else IF_KW("utput", 5, YANG_OUTPUT)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200826 break;
827 case 'p':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200828 MOVE_INPUT(ctx, data, 1);
829 IF_KW("ath", 3, YANG_PATH)
830 else IF_KW("attern", 6, YANG_PATTERN)
831 else IF_KW("osition", 7, YANG_POSITION)
832 else IF_KW_PREFIX("re", 2)
833 IF_KW("fix", 3, YANG_PREFIX)
834 else IF_KW("sence", 5, YANG_PRESENCE)
835 IF_KW_PREFIX_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200836 break;
837 case 'r':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200838 MOVE_INPUT(ctx, data, 1);
839 IF_KW("ange", 4, YANG_RANGE)
840 else IF_KW_PREFIX("e", 1)
841 IF_KW_PREFIX("f", 1)
842 IF_KW("erence", 6, YANG_REFERENCE)
843 else IF_KW("ine", 3, YANG_REFINE)
844 IF_KW_PREFIX_END
845 else IF_KW("quire-instance", 14, YANG_REQUIRE_INSTANCE)
846 else IF_KW("vision-date", 11, YANG_REVISION_DATE)
847 else IF_KW("vision", 6, YANG_REVISION)
848 IF_KW_PREFIX_END
849 else IF_KW("pc", 2, YANG_RPC)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200850 break;
851 case 's':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200852 MOVE_INPUT(ctx, data, 1);
853 IF_KW("tatus", 5, YANG_STATUS)
854 else IF_KW("ubmodule", 8, YANG_SUBMODULE)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200855 break;
856 case 't':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200857 MOVE_INPUT(ctx, data, 1);
858 IF_KW("ypedef", 6, YANG_TYPEDEF)
859 else IF_KW("ype", 3, YANG_TYPE)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200860 break;
861 case 'u':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200862 MOVE_INPUT(ctx, data, 1);
863 IF_KW_PREFIX("ni", 2)
864 IF_KW("que", 3, YANG_UNIQUE)
865 else IF_KW("ts", 2, YANG_UNITS)
866 IF_KW_PREFIX_END
867 else IF_KW("ses", 3, YANG_USES)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200868 break;
869 case 'v':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200870 MOVE_INPUT(ctx, data, 1);
871 IF_KW("alue", 4, YANG_VALUE)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200872 break;
873 case 'w':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200874 MOVE_INPUT(ctx, data, 1);
875 IF_KW("hen", 3, YANG_WHEN)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200876 break;
877 case 'y':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200878 MOVE_INPUT(ctx, data, 1);
879 IF_KW("ang-version", 11, YANG_YANG_VERSION)
880 else IF_KW("in-element", 10, YANG_YIN_ELEMENT)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200881 break;
882 case ';':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200883 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200884 *kw = YANG_SEMICOLON;
Radek Krejci626df482018-10-11 15:06:31 +0200885 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200886 case '{':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200887 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200888 *kw = YANG_LEFT_BRACE;
Radek Krejci626df482018-10-11 15:06:31 +0200889 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200890 case '}':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200891 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200892 *kw = YANG_RIGHT_BRACE;
Radek Krejci626df482018-10-11 15:06:31 +0200893 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200894 default:
895 break;
896 }
897
898 if (*kw != YANG_NONE) {
899 /* make sure we have the whole keyword */
900 switch (**data) {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200901 case '\n':
902 ++ctx->line;
903 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200904 case ' ':
905 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200906 /* mandatory "sep" */
907 break;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200908 case '{':
909 /* allowed only for input and output statements which can be without arguments */
910 if (*kw == YANG_INPUT || *kw == YANG_OUTPUT) {
911 break;
912 }
913 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200914 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200915 MOVE_INPUT(ctx, data, 1);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200916 LOGVAL_YANG(ctx, LY_VCODE_INSTREXP, (int)(*data - word_start), word_start,
917 "a keyword followed by a separator");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200918 return LY_EVALID;
919 }
920 } else {
921 /* still can be an extension */
922 prefix = 0;
923 while (**data && (**data != ' ') && (**data != '\t') && (**data != '\n') && (**data != '{') && (**data != ';')) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200924 LY_CHECK_ERR_RET(ly_getutf8(data, &c, &len),
925 LOGVAL_YANG(ctx, LY_VCODE_INCHAR, (*data)[-len]), LY_EVALID);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200926 ++ctx->indent;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200927 /* check character validity */
928 LY_CHECK_RET(check_identifierchar(ctx, c, *data - len == word_start ? 1 : 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200929 }
930 if (!**data) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200931 LOGVAL_YANG(ctx, LY_VCODE_EOF);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200932 return LY_EVALID;
933 }
934
935 /* prefix is mandatory for extension instances */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200936 if (prefix != 2) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200937 LOGVAL_YANG(ctx, LY_VCODE_INSTREXP, (int)(*data - word_start), word_start, "a keyword");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200938 return LY_EVALID;
939 }
940
941 *kw = YANG_CUSTOM;
942 }
Radek Krejci626df482018-10-11 15:06:31 +0200943success:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200944 if (word_p) {
945 *word_p = (char *)word_start;
946 *word_len = *data - word_start;
947 }
948
949 return LY_SUCCESS;
950}
951
Michal Vaskoea5abea2018-09-18 13:10:54 +0200952/**
953 * @brief Parse extension instance substatements.
954 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200955 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200956 * @param[in,out] data Data to read from, always moved to currently handled character.
957 * @param[in] word Extension instance substatement name (keyword).
958 * @param[in] word_len Extension instance substatement name length.
959 * @param[in,out] child Children of this extension instance to add to.
960 *
961 * @return LY_ERR values.
962 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200963static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +0200964parse_ext_substmt(struct ly_parser_ctx *ctx, const char **data, char *word, size_t word_len,
Michal Vasko7fbc8162018-09-17 10:35:16 +0200965 struct lysp_stmt **child)
966{
967 char *buf;
968 LY_ERR ret = 0;
969 enum yang_keyword kw;
970 struct lysp_stmt *stmt, *par_child;
971
972 stmt = calloc(1, sizeof *stmt);
973 LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM);
974
Radek Krejci44ceedc2018-10-02 15:54:31 +0200975 stmt->stmt = lydict_insert(ctx->ctx, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200976
977 /* get optional argument */
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200978 ret = get_argument(ctx, data, Y_MAYBE_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +0200979 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200980
Radek Krejci0ae092d2018-09-20 16:43:19 +0200981 if (word) {
982 if (buf) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200983 stmt->arg = lydict_insert_zc(ctx->ctx, word);
Radek Krejci0ae092d2018-09-20 16:43:19 +0200984 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200985 stmt->arg = lydict_insert(ctx->ctx, word, word_len);
Radek Krejci0ae092d2018-09-20 16:43:19 +0200986 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200987 }
988
989 /* insert into parent statements */
990 if (!*child) {
991 *child = stmt;
992 } else {
993 for (par_child = *child; par_child->next; par_child = par_child->next);
994 par_child->next = stmt;
995 }
996
997 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +0200998 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200999
1000 ret = parse_ext_substmt(ctx, data, word, word_len, &stmt->child);
Radek Krejcic59bc972018-09-17 16:13:06 +02001001 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001002 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001003 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001004
1005 return ret;
1006}
1007
Michal Vaskoea5abea2018-09-18 13:10:54 +02001008/**
1009 * @brief Parse extension instance.
1010 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001011 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001012 * @param[in,out] data Data to read from, always moved to currently handled character.
1013 * @param[in] ext_name Extension instance substatement name (keyword).
1014 * @param[in] ext_name_len Extension instance substatement name length.
1015 * @param[in] insubstmt Type of the keyword this extension instance is a substatement of.
1016 * @param[in] insubstmt_index Index of the keyword instance this extension instance is a substatement of.
1017 * @param[in,out] exts Extension instances to add to.
1018 *
1019 * @return LY_ERR values.
1020 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001021static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001022parse_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 +02001023 uint32_t insubstmt_index, struct lysp_ext_instance **exts)
1024{
1025 LY_ERR ret = 0;
1026 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001027 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001028 struct lysp_ext_instance *e;
1029 enum yang_keyword kw;
1030
1031 LYSP_ARRAY_NEW_RET(ctx, exts, e, LY_EMEM);
1032
1033 /* store name and insubstmt info */
Radek Krejci44ceedc2018-10-02 15:54:31 +02001034 e->name = lydict_insert(ctx->ctx, ext_name, ext_name_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001035 e->insubstmt = insubstmt;
1036 e->insubstmt_index = insubstmt_index;
1037
1038 /* get optional argument */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001039 ret = get_argument(ctx, data, Y_MAYBE_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001040 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001041
Radek Krejci0ae092d2018-09-20 16:43:19 +02001042 if (word) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001043 INSERT_WORD(ctx, buf, e->argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001044 }
1045
1046 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001047 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001048
1049 ret = parse_ext_substmt(ctx, data, word, word_len, &e->child);
Radek Krejcic59bc972018-09-17 16:13:06 +02001050 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001051 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001052 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001053
1054 return ret;
1055}
1056
Michal Vaskoea5abea2018-09-18 13:10:54 +02001057/**
1058 * @brief Parse a generic text field without specific constraints. Those are contact, organization,
1059 * description, etc...
1060 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001061 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001062 * @param[in,out] data Data to read from, always moved to currently handled character.
1063 * @param[in] substmt Type of this substatement.
1064 * @param[in] substmt_index Index of this substatement.
1065 * @param[in,out] value Place to store the parsed value.
1066 * @param[in] arg Type of the YANG keyword argument (of the value).
1067 * @param[in,out] exts Extension instances to add to.
1068 *
1069 * @return LY_ERR values.
1070 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001071static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001072parse_text_field(struct ly_parser_ctx *ctx, const char **data, LYEXT_SUBSTMT substmt, uint32_t substmt_index,
Michal Vasko7fbc8162018-09-17 10:35:16 +02001073 const char **value, enum yang_arg arg, struct lysp_ext_instance **exts)
1074{
1075 LY_ERR ret = 0;
1076 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001077 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001078 enum yang_keyword kw;
1079
1080 if (*value) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001081 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001082 return LY_EVALID;
1083 }
1084
1085 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001086 ret = get_argument(ctx, data, arg, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001087 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001088
1089 /* store value and spend buf if allocated */
Radek Krejci44ceedc2018-10-02 15:54:31 +02001090 INSERT_WORD(ctx, buf, *value, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001091
1092 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001093 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001094
1095 switch (kw) {
1096 case YANG_CUSTOM:
1097 ret = parse_ext(ctx, data, word, word_len, substmt, substmt_index, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02001098 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001099 break;
1100 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001101 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001102 return LY_EVALID;
1103 }
1104 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001105 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001106
1107 return ret;
1108}
1109
Michal Vaskoea5abea2018-09-18 13:10:54 +02001110/**
1111 * @brief Parse the yang-version statement.
1112 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001113 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001114 * @param[in,out] data Data to read from, always moved to currently handled character.
1115 * @param[in] mod Module to store the parsed information in.
1116 *
1117 * @return LY_ERR values.
1118 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001119static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001120parse_yangversion(struct ly_parser_ctx *ctx, const char **data, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001121{
1122 LY_ERR ret = 0;
1123 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001124 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001125 enum yang_keyword kw;
1126
1127 if (mod->version) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001128 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001129 return LY_EVALID;
1130 }
1131
1132 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001133 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001134 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001135
1136 if ((word_len == 3) && !strncmp(word, "1.0", word_len)) {
1137 mod->version = LYS_VERSION_1_0;
1138 } else if ((word_len == 3) && !strncmp(word, "1.1", word_len)) {
1139 mod->version = LYS_VERSION_1_1;
1140 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001141 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001142 free(buf);
1143 return LY_EVALID;
1144 }
1145 free(buf);
1146
1147 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001148 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001149
1150 switch (kw) {
1151 case YANG_CUSTOM:
1152 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_VERSION, 0, &mod->exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02001153 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001154 break;
1155 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001156 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001157 return LY_EVALID;
1158 }
1159 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001160 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001161
1162 return ret;
1163}
1164
Michal Vaskoea5abea2018-09-18 13:10:54 +02001165/**
1166 * @brief Parse the belongs-to statement.
1167 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001168 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001169 * @param[in,out] data Data to read from, always moved to currently handled character.
1170 * @param[in,out] belongsto Place to store the parsed value.
1171 * @param[in,out] prefix Place to store the parsed belongs-to prefix value.
1172 * @param[in,out] exts Extension instances to add to.
1173 *
1174 * @return LY_ERR values.
1175 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001176static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001177parse_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 +02001178{
1179 LY_ERR ret = 0;
1180 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001181 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001182 enum yang_keyword kw;
1183
1184 if (*belongsto) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001185 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001186 return LY_EVALID;
1187 }
1188
1189 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001190 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001191 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001192
Radek Krejci44ceedc2018-10-02 15:54:31 +02001193 INSERT_WORD(ctx, buf, *belongsto, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001194 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001195 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001196
1197 switch (kw) {
1198 case YANG_PREFIX:
1199 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_PREFIX, 0, prefix, Y_IDENTIF_ARG, exts);
1200 break;
1201 case YANG_CUSTOM:
1202 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_BELONGSTO, 0, exts);
1203 break;
1204 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001205 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001206 return LY_EVALID;
1207 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001208 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001209 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001210 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001211
1212 /* mandatory substatements */
1213 if (!*prefix) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001214 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001215 return LY_EVALID;
1216 }
1217
1218 return ret;
1219}
1220
Michal Vaskoea5abea2018-09-18 13:10:54 +02001221/**
1222 * @brief Parse the revision-date statement.
1223 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001224 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001225 * @param[in,out] data Data to read from, always moved to currently handled character.
1226 * @param[in,out] rev Array to store the parsed value in.
1227 * @param[in,out] exts Extension instances to add to.
1228 *
1229 * @return LY_ERR values.
1230 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001231static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001232parse_revisiondate(struct ly_parser_ctx *ctx, const char **data, char *rev, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001233{
1234 LY_ERR ret = 0;
1235 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001236 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001237 enum yang_keyword kw;
1238
1239 if (rev[0]) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001240 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001241 return LY_EVALID;
1242 }
1243
1244 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001245 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001246 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001247
1248 /* check value */
Radek Krejci44ceedc2018-10-02 15:54:31 +02001249 if (lysp_check_date(ctx->ctx, word, word_len, "revision-date")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001250 free(buf);
1251 return LY_EVALID;
1252 }
1253
1254 /* store value and spend buf if allocated */
1255 strncpy(rev, word, word_len);
1256 free(buf);
1257
1258 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001259 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001260
1261 switch (kw) {
1262 case YANG_CUSTOM:
1263 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_REVISIONDATE, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02001264 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001265 break;
1266 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001267 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001268 return LY_EVALID;
1269 }
1270 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001271 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001272
1273 return ret;
1274}
1275
Michal Vaskoea5abea2018-09-18 13:10:54 +02001276/**
1277 * @brief Parse the include statement.
1278 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001279 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001280 * @param[in,out] data Data to read from, always moved to currently handled character.
1281 * @param[in,out] includes Parsed includes to add to.
1282 *
1283 * @return LY_ERR values.
1284 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001285static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001286parse_include(struct ly_parser_ctx *ctx, const char **data, struct lysp_include **includes)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001287{
1288 LY_ERR ret = 0;
1289 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001290 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001291 enum yang_keyword kw;
1292 struct lysp_include *inc;
1293
1294 LYSP_ARRAY_NEW_RET(ctx, includes, inc, LY_EMEM);
1295
1296 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001297 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001298 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001299
Radek Krejci44ceedc2018-10-02 15:54:31 +02001300 INSERT_WORD(ctx, buf, inc->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001301 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001302 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001303
1304 switch (kw) {
1305 case YANG_DESCRIPTION:
1306 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &inc->dsc, Y_STR_ARG, &inc->exts);
1307 break;
1308 case YANG_REFERENCE:
1309 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &inc->ref, Y_STR_ARG, &inc->exts);
1310 break;
1311 case YANG_REVISION_DATE:
1312 ret = parse_revisiondate(ctx, data, inc->rev, &inc->exts);
1313 break;
1314 case YANG_CUSTOM:
1315 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inc->exts);
1316 break;
1317 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001318 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001319 return LY_EVALID;
1320 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001321 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001322 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001323 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001324
1325 return ret;
1326}
1327
Michal Vaskoea5abea2018-09-18 13:10:54 +02001328/**
1329 * @brief Parse the import statement.
1330 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001331 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001332 * @param[in,out] data Data to read from, always moved to currently handled character.
1333 * @param[in,out] imports Parsed imports to add to.
1334 *
1335 * @return LY_ERR values.
1336 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001337static LY_ERR
Radek Krejci70853c52018-10-15 14:46:16 +02001338parse_import(struct ly_parser_ctx *ctx, const char **data, struct lysp_module *module)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001339{
1340 LY_ERR ret = 0;
1341 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001342 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001343 enum yang_keyword kw;
1344 struct lysp_import *imp;
1345
Radek Krejci70853c52018-10-15 14:46:16 +02001346 LYSP_ARRAY_NEW_RET(ctx, &module->imports, imp, LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001347
1348 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001349 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001350 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001351
Radek Krejci44ceedc2018-10-02 15:54:31 +02001352 INSERT_WORD(ctx, buf, imp->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001353 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001354 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001355
1356 switch (kw) {
1357 case YANG_PREFIX:
1358 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_PREFIX, 0, &imp->prefix, Y_IDENTIF_ARG, &imp->exts);
Radek Krejci70853c52018-10-15 14:46:16 +02001359 LY_CHECK_RET(lysp_check_prefix(ctx, module, &imp->prefix), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001360 break;
1361 case YANG_DESCRIPTION:
1362 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &imp->dsc, Y_STR_ARG, &imp->exts);
1363 break;
1364 case YANG_REFERENCE:
1365 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &imp->ref, Y_STR_ARG, &imp->exts);
1366 break;
1367 case YANG_REVISION_DATE:
1368 ret = parse_revisiondate(ctx, data, imp->rev, &imp->exts);
1369 break;
1370 case YANG_CUSTOM:
1371 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &imp->exts);
1372 break;
1373 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001374 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001375 return LY_EVALID;
1376 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001377 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001378 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001379 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001380
1381 /* mandatory substatements */
1382 if (!imp->prefix) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001383 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "prefix", "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001384 return LY_EVALID;
1385 }
1386
1387 return ret;
1388}
1389
Michal Vaskoea5abea2018-09-18 13:10:54 +02001390/**
1391 * @brief Parse the revision statement.
1392 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001393 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001394 * @param[in,out] data Data to read from, always moved to currently handled character.
1395 * @param[in,out] revs Parsed revisions to add to.
1396 *
1397 * @return LY_ERR values.
1398 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001399static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001400parse_revision(struct ly_parser_ctx *ctx, const char **data, struct lysp_revision **revs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001401{
1402 LY_ERR ret = 0;
1403 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001404 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001405 enum yang_keyword kw;
1406 struct lysp_revision *rev;
1407
1408 LYSP_ARRAY_NEW_RET(ctx, revs, rev, LY_EMEM);
1409
1410 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001411 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001412 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001413
1414 /* check value */
Radek Krejci44ceedc2018-10-02 15:54:31 +02001415 if (lysp_check_date(ctx->ctx, word, word_len, "revision")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001416 return LY_EVALID;
1417 }
1418
1419 strncpy(rev->rev, word, word_len);
1420 free(buf);
1421
1422 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001423 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001424
1425 switch (kw) {
1426 case YANG_DESCRIPTION:
1427 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &rev->dsc, Y_STR_ARG, &rev->exts);
1428 break;
1429 case YANG_REFERENCE:
1430 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &rev->ref, Y_STR_ARG, &rev->exts);
1431 break;
1432 case YANG_CUSTOM:
1433 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rev->exts);
1434 break;
1435 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001436 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001437 return LY_EVALID;
1438 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001439 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001440 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001441 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001442
1443 return ret;
1444}
1445
Michal Vaskoea5abea2018-09-18 13:10:54 +02001446/**
1447 * @brief Parse a generic text field that can have more instances such as base.
1448 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001449 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001450 * @param[in,out] data Data to read from, always moved to currently handled character.
1451 * @param[in] substmt Type of this substatement.
1452 * @param[in,out] texts Parsed values to add to.
1453 * @param[in] arg Type of the expected argument.
1454 * @param[in,out] exts Extension instances to add to.
1455 *
1456 * @return LY_ERR values.
1457 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001458static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001459parse_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 +02001460 struct lysp_ext_instance **exts)
1461{
1462 LY_ERR ret = 0;
1463 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001464 size_t count, word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001465 enum yang_keyword kw;
1466
1467 /* allocate new pointer */
1468 for (count = 1; (*texts) && (*texts)[count - 1]; ++count);
1469 *texts = realloc(*texts, count * sizeof **texts);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001470 LY_CHECK_ERR_RET(!*texts, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001471
1472 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001473 ret = get_argument(ctx, data, arg, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001474 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001475
Radek Krejci44ceedc2018-10-02 15:54:31 +02001476 INSERT_WORD(ctx, buf, (*texts)[count - 1], word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001477 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001478 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001479
1480 switch (kw) {
1481 case YANG_CUSTOM:
1482 ret = parse_ext(ctx, data, word, word_len, substmt, count - 1, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02001483 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001484 break;
1485 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001486 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001487 return LY_EVALID;
1488 }
1489 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001490 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001491
1492 return ret;
1493}
1494
Michal Vaskoea5abea2018-09-18 13:10:54 +02001495/**
1496 * @brief Parse the config statement.
1497 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001498 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001499 * @param[in,out] data Data to read from, always moved to currently handled character.
1500 * @param[in,out] flags Flags to add to.
1501 * @param[in,out] exts Extension instances to add to.
1502 *
1503 * @return LY_ERR values.
1504 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001505static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001506parse_config(struct ly_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001507{
1508 LY_ERR ret = 0;
1509 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001510 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001511 enum yang_keyword kw;
1512
1513 if (*flags & LYS_CONFIG_MASK) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001514 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001515 return LY_EVALID;
1516 }
1517
1518 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001519 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001520 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001521
1522 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1523 *flags |= LYS_CONFIG_W;
1524 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1525 *flags |= LYS_CONFIG_R;
1526 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001527 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001528 free(buf);
1529 return LY_EVALID;
1530 }
1531 free(buf);
1532
1533 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001534 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001535
1536 switch (kw) {
1537 case YANG_CUSTOM:
1538 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_CONFIG, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02001539 LY_CHECK_RET(ret);
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), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001543 return LY_EVALID;
1544 }
1545 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001546 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001547
1548 return ret;
1549}
1550
Michal Vaskoea5abea2018-09-18 13:10:54 +02001551/**
1552 * @brief Parse the mandatory statement.
1553 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001554 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001555 * @param[in,out] data Data to read from, always moved to currently handled character.
1556 * @param[in,out] flags Flags to add to.
1557 * @param[in,out] exts Extension instances to add to.
1558 *
1559 * @return LY_ERR values.
1560 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001561static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001562parse_mandatory(struct ly_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001563{
1564 LY_ERR ret = 0;
1565 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001566 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001567 enum yang_keyword kw;
1568
1569 if (*flags & LYS_MAND_MASK) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001570 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001571 return LY_EVALID;
1572 }
1573
1574 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001575 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001576 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001577
1578 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1579 *flags |= LYS_MAND_TRUE;
1580 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1581 *flags |= LYS_MAND_FALSE;
1582 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001583 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001584 free(buf);
1585 return LY_EVALID;
1586 }
1587 free(buf);
1588
1589 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001590 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001591
1592 switch (kw) {
1593 case YANG_CUSTOM:
1594 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MANDATORY, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02001595 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001596 break;
1597 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001598 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001599 return LY_EVALID;
1600 }
1601 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001602 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001603
1604 return ret;
1605}
1606
Michal Vaskoea5abea2018-09-18 13:10:54 +02001607/**
1608 * @brief Parse a restriction such as range or length.
1609 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001610 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001611 * @param[in,out] data Data to read from, always moved to currently handled character.
1612 * @param[in] restr_kw Type of this particular restriction.
1613 * @param[in,out] exts Extension instances to add to.
1614 *
1615 * @return LY_ERR values.
1616 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001617static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001618parse_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 +02001619{
1620 LY_ERR ret = 0;
1621 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001622 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001623 enum yang_keyword kw;
1624
1625 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001626 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001627 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001628
Radek Krejci44ceedc2018-10-02 15:54:31 +02001629 INSERT_WORD(ctx, buf, restr->arg, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001630 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001631 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001632
1633 switch (kw) {
1634 case YANG_DESCRIPTION:
1635 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts);
1636 break;
1637 case YANG_REFERENCE:
1638 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts);
1639 break;
1640 case YANG_ERROR_APP_TAG:
1641 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRTAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts);
1642 break;
1643 case YANG_ERROR_MESSAGE:
1644 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRMSG, 0, &restr->emsg, Y_STR_ARG, &restr->exts);
1645 break;
1646 case YANG_CUSTOM:
1647 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts);
1648 break;
1649 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001650 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001651 return LY_EVALID;
1652 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001653 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001654 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001655 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001656
1657 return ret;
1658}
1659
Michal Vaskoea5abea2018-09-18 13:10:54 +02001660/**
1661 * @brief Parse a restriction that can have more instances such as must.
1662 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001663 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001664 * @param[in,out] data Data to read from, always moved to currently handled character.
1665 * @param[in] restr_kw Type of this particular restriction.
1666 * @param[in,out] restrs Restrictions to add to.
1667 *
1668 * @return LY_ERR values.
1669 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001670static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001671parse_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 +02001672{
1673 struct lysp_restr *restr;
1674
1675 LYSP_ARRAY_NEW_RET(ctx, restrs, restr, LY_EMEM);
1676
1677 return parse_restr(ctx, data, restr_kw, restr);
1678}
1679
Michal Vaskoea5abea2018-09-18 13:10:54 +02001680/**
1681 * @brief Parse the status statement.
1682 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001683 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001684 * @param[in,out] data Data to read from, always moved to currently handled character.
1685 * @param[in,out] flags Flags to add to.
1686 * @param[in,out] exts Extension instances to add to.
1687 *
1688 * @return LY_ERR values.
1689 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001690static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001691parse_status(struct ly_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001692{
1693 LY_ERR ret = 0;
1694 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001695 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001696 enum yang_keyword kw;
1697
1698 if (*flags & LYS_STATUS_MASK) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001699 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001700 return LY_EVALID;
1701 }
1702
1703 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001704 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001705 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001706
1707 if ((word_len == 7) && !strncmp(word, "current", word_len)) {
1708 *flags |= LYS_STATUS_CURR;
1709 } else if ((word_len == 10) && !strncmp(word, "deprecated", word_len)) {
1710 *flags |= LYS_STATUS_DEPRC;
1711 } else if ((word_len == 8) && !strncmp(word, "obsolete", word_len)) {
1712 *flags |= LYS_STATUS_OBSLT;
1713 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001714 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001715 free(buf);
1716 return LY_EVALID;
1717 }
1718 free(buf);
1719
1720 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001721 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001722
1723 switch (kw) {
1724 case YANG_CUSTOM:
1725 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_STATUS, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02001726 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001727 break;
1728 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001729 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001730 return LY_EVALID;
1731 }
1732 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001733 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001734
1735 return ret;
1736}
1737
Michal Vaskoea5abea2018-09-18 13:10:54 +02001738/**
1739 * @brief Parse the when statement.
1740 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001741 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001742 * @param[in,out] data Data to read from, always moved to currently handled character.
1743 * @param[in,out] when_p When pointer to parse to.
1744 *
1745 * @return LY_ERR values.
1746 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001747static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001748parse_when(struct ly_parser_ctx *ctx, const char **data, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001749{
1750 LY_ERR ret = 0;
1751 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001752 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001753 enum yang_keyword kw;
1754 struct lysp_when *when;
1755
1756 if (*when_p) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001757 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001758 return LY_EVALID;
1759 }
1760
1761 when = calloc(1, sizeof *when);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001762 LY_CHECK_ERR_RET(!when, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001763 *when_p = when;
1764
1765 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001766 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001767 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001768
Radek Krejci44ceedc2018-10-02 15:54:31 +02001769 INSERT_WORD(ctx, buf, when->cond, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001770 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001771 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001772
1773 switch (kw) {
1774 case YANG_DESCRIPTION:
1775 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &when->dsc, Y_STR_ARG, &when->exts);
1776 break;
1777 case YANG_REFERENCE:
1778 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &when->ref, Y_STR_ARG, &when->exts);
1779 break;
1780 case YANG_CUSTOM:
1781 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &when->exts);
1782 break;
1783 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001784 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001785 return LY_EVALID;
1786 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001787 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001788 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001789 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001790
1791 return ret;
1792}
1793
Michal Vaskoea5abea2018-09-18 13:10:54 +02001794/**
1795 * @brief Parse the anydata or anyxml statement.
1796 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001797 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001798 * @param[in,out] data Data to read from, always moved to currently handled character.
1799 * @param[in] kw Type of this particular keyword.
1800 * @param[in,out] siblings Siblings to add to.
1801 *
1802 * @return LY_ERR values.
1803 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001804static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001805parse_any(struct ly_parser_ctx *ctx, const char **data, enum yang_keyword kw, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001806{
1807 LY_ERR ret = 0;
1808 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001809 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001810 struct lysp_node *iter;
1811 struct lysp_node_anydata *any;
1812
1813 /* create structure */
1814 any = calloc(1, sizeof *any);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001815 LY_CHECK_ERR_RET(!any, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001816 any->nodetype = kw == YANG_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
1817
1818 /* insert into siblings */
1819 if (!*siblings) {
1820 *siblings = (struct lysp_node *)any;
1821 } else {
1822 for (iter = *siblings; iter->next; iter = iter->next);
1823 iter->next = (struct lysp_node *)any;
1824 }
1825
1826 /* get name */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001827 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001828 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001829
Radek Krejci44ceedc2018-10-02 15:54:31 +02001830 INSERT_WORD(ctx, buf, any->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001831
1832 /* parse substatements */
1833 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001834 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001835
1836 switch (kw) {
1837 case YANG_CONFIG:
1838 ret = parse_config(ctx, data, &any->flags, &any->exts);
1839 break;
1840 case YANG_DESCRIPTION:
1841 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &any->dsc, Y_STR_ARG, &any->exts);
1842 break;
1843 case YANG_IF_FEATURE:
1844 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &any->iffeatures, Y_STR_ARG, &any->exts);
1845 break;
1846 case YANG_MANDATORY:
1847 ret = parse_mandatory(ctx, data, &any->flags, &any->exts);
1848 break;
1849 case YANG_MUST:
1850 ret = parse_restrs(ctx, data, kw, &any->musts);
1851 break;
1852 case YANG_REFERENCE:
1853 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &any->ref, Y_STR_ARG, &any->exts);
1854 break;
1855 case YANG_STATUS:
1856 ret = parse_status(ctx, data, &any->flags, &any->exts);
1857 break;
1858 case YANG_WHEN:
1859 ret = parse_when(ctx, data, &any->when);
1860 break;
1861 case YANG_CUSTOM:
1862 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &any->exts);
1863 break;
1864 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001865 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
Radek Krejcic59bc972018-09-17 16:13:06 +02001866 (any->nodetype & LYS_ANYDATA) == LYS_ANYDATA ? ly_stmt2str(YANG_ANYDATA) : ly_stmt2str(YANG_ANYXML));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001867 return LY_EVALID;
1868 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001869 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001870 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001871 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001872
1873 return ret;
1874}
1875
Michal Vaskoea5abea2018-09-18 13:10:54 +02001876/**
1877 * @brief Parse the value or position statement. Substatement of type enum statement.
1878 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001879 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001880 * @param[in,out] data Data to read from, always moved to currently handled character.
1881 * @param[in] val_kw Type of this particular keyword.
1882 * @param[in,out] value Value to write to.
1883 * @param[in,out] flags Flags to write to.
1884 * @param[in,out] exts Extension instances to add to.
1885 *
1886 * @return LY_ERR values.
1887 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001888static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001889parse_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 +02001890 struct lysp_ext_instance **exts)
1891{
1892 LY_ERR ret = 0;
1893 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001894 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001895 long int num;
1896 unsigned long int unum;
1897 enum yang_keyword kw;
1898
1899 if (*flags & LYS_SET_VALUE) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001900 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001901 return LY_EVALID;
1902 }
1903 *flags |= LYS_SET_VALUE;
1904
1905 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001906 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001907 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001908
1909 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 +02001910 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001911 free(buf);
1912 return LY_EVALID;
1913 }
1914
1915 errno = 0;
1916 if (val_kw == YANG_VALUE) {
1917 num = strtol(word, &ptr, 10);
1918 } else {
1919 unum = strtoul(word, &ptr, 10);
1920 }
1921 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001922 if ((size_t)(ptr - word) != word_len) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001923 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001924 free(buf);
1925 return LY_EVALID;
1926 }
1927 if (errno == ERANGE) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001928 LOGVAL_YANG(ctx, LY_VCODE_OOB, word_len, word, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001929 free(buf);
1930 return LY_EVALID;
1931 }
1932 if (val_kw == YANG_VALUE) {
1933 *value = num;
1934 } else {
1935 *value = unum;
1936 }
1937 free(buf);
1938
1939 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001940 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001941
1942 switch (kw) {
1943 case YANG_CUSTOM:
1944 ret = parse_ext(ctx, data, word, word_len, val_kw == YANG_VALUE ? LYEXT_SUBSTMT_VALUE : LYEXT_SUBSTMT_POSITION, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02001945 LY_CHECK_RET(ret);
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(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001949 return LY_EVALID;
1950 }
1951 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001952 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001953
1954 return ret;
1955}
1956
Michal Vaskoea5abea2018-09-18 13:10:54 +02001957/**
1958 * @brief Parse the enum or bit statement. Substatement of type statement.
1959 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001960 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001961 * @param[in,out] data Data to read from, always moved to currently handled character.
1962 * @param[in] enum_kw Type of this particular keyword.
1963 * @param[in,out] enums Enums or bits to add to.
1964 *
1965 * @return LY_ERR values.
1966 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001967static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001968parse_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 +02001969{
1970 LY_ERR ret = 0;
1971 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001972 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001973 enum yang_keyword kw;
1974 struct lysp_type_enum *enm;
1975
1976 LYSP_ARRAY_NEW_RET(ctx, enums, enm, LY_EMEM);
1977
1978 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001979 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001980 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001981
Radek Krejci44ceedc2018-10-02 15:54:31 +02001982 INSERT_WORD(ctx, buf, enm->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001983 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001984 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001985
1986 switch (kw) {
1987 case YANG_DESCRIPTION:
1988 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &enm->dsc, Y_STR_ARG, &enm->exts);
1989 break;
1990 case YANG_IF_FEATURE:
1991 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &enm->iffeatures, Y_STR_ARG, &enm->exts);
1992 break;
1993 case YANG_REFERENCE:
1994 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &enm->ref, Y_STR_ARG, &enm->exts);
1995 break;
1996 case YANG_STATUS:
1997 ret = parse_status(ctx, data, &enm->flags, &enm->exts);
1998 break;
1999 case YANG_VALUE:
2000 case YANG_POSITION:
2001 ret = parse_type_enum_value_pos(ctx, data, kw, &enm->value, &enm->flags, &enm->exts);
2002 break;
2003 case YANG_CUSTOM:
2004 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &enm->exts);
2005 break;
2006 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002007 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002008 return LY_EVALID;
2009 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002010 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002011 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002012 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002013
2014 return ret;
2015}
2016
Michal Vaskoea5abea2018-09-18 13:10:54 +02002017/**
2018 * @brief Parse the fraction-digits 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] fracdig Value to write to.
2023 * @param[in,out] exts Extension instances to add to.
2024 *
2025 * @return LY_ERR values.
2026 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002027static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002028parse_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 +02002029{
2030 LY_ERR ret = 0;
2031 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002032 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002033 unsigned long int num;
2034 enum yang_keyword kw;
2035
2036 if (*fracdig) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002037 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002038 return LY_EVALID;
2039 }
2040
2041 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002042 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002043 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002044
2045 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002046 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002047 free(buf);
2048 return LY_EVALID;
2049 }
2050
2051 errno = 0;
2052 num = strtoul(word, &ptr, 10);
2053 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002054 if ((size_t)(ptr - word) != word_len) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002055 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002056 free(buf);
2057 return LY_EVALID;
2058 }
2059 if ((errno == ERANGE) || (num > 18)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002060 LOGVAL_YANG(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002061 free(buf);
2062 return LY_EVALID;
2063 }
2064 *fracdig = num;
2065 free(buf);
2066
2067 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002068 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002069
2070 switch (kw) {
2071 case YANG_CUSTOM:
2072 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_FRACDIGITS, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02002073 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002074 break;
2075 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002076 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002077 return LY_EVALID;
2078 }
2079 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002080 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002081
2082 return ret;
2083}
2084
Michal Vaskoea5abea2018-09-18 13:10:54 +02002085/**
2086 * @brief Parse the require-instance statement. Substatement of type statement.
2087 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002088 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002089 * @param[in,out] data Data to read from, always moved to currently handled character.
2090 * @param[in,out] reqinst Value to write to.
2091 * @param[in,out] flags Flags to write to.
2092 * @param[in,out] exts Extension instances to add to.
2093 *
2094 * @return LY_ERR values.
2095 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002096static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002097parse_type_reqinstance(struct ly_parser_ctx *ctx, const char **data, uint8_t *reqinst, uint16_t *flags,
Michal Vasko7fbc8162018-09-17 10:35:16 +02002098 struct lysp_ext_instance **exts)
2099{
2100 LY_ERR ret = 0;
2101 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002102 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002103 enum yang_keyword kw;
2104
2105 if (*flags & LYS_SET_REQINST) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002106 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002107 return LY_EVALID;
2108 }
2109 *flags |= LYS_SET_REQINST;
2110
2111 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002112 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002113 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002114
2115 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
2116 *reqinst = 1;
2117 } else if ((word_len != 5) || strncmp(word, "false", word_len)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002118 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002119 free(buf);
2120 return LY_EVALID;
2121 }
2122 free(buf);
2123
2124 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002125 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002126
2127 switch (kw) {
2128 case YANG_CUSTOM:
2129 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_REQINSTANCE, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02002130 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002131 break;
2132 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002133 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002134 return LY_EVALID;
2135 }
2136 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002137 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002138
2139 return ret;
2140}
2141
Michal Vaskoea5abea2018-09-18 13:10:54 +02002142/**
2143 * @brief Parse the modifier statement. Substatement of type pattern statement.
2144 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002145 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002146 * @param[in,out] data Data to read from, always moved to currently handled character.
2147 * @param[in,out] pat Value to write to.
2148 * @param[in,out] exts Extension instances to add to.
2149 *
2150 * @return LY_ERR values.
2151 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002152static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002153parse_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 +02002154{
2155 LY_ERR ret = 0;
2156 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002157 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002158 enum yang_keyword kw;
2159
2160 if ((*pat)[0] == 0x15) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002161 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002162 return LY_EVALID;
2163 }
2164
2165 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002166 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002167 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002168
2169 if ((word_len != 12) || strncmp(word, "invert-match", word_len)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002170 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002171 free(buf);
2172 return LY_EVALID;
2173 }
2174 free(buf);
2175
2176 /* replace the value in the dictionary */
2177 buf = malloc(strlen(*pat) + 1);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002178 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002179 strcpy(buf, *pat);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002180 lydict_remove(ctx->ctx, *pat);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002181
2182 assert(buf[0] == 0x06);
2183 buf[0] = 0x15;
Radek Krejci44ceedc2018-10-02 15:54:31 +02002184 *pat = lydict_insert_zc(ctx->ctx, buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002185
2186 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002187 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002188
2189 switch (kw) {
2190 case YANG_CUSTOM:
2191 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MODIFIER, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02002192 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002193 break;
2194 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002195 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002196 return LY_EVALID;
2197 }
2198 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002199 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002200
2201 return ret;
2202}
2203
Michal Vaskoea5abea2018-09-18 13:10:54 +02002204/**
2205 * @brief Parse the pattern statement. Substatement of type statement.
2206 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002207 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002208 * @param[in,out] data Data to read from, always moved to currently handled character.
2209 * @param[in,out] patterns Restrictions to add to.
2210 *
2211 * @return LY_ERR values.
2212 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002213static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002214parse_type_pattern(struct ly_parser_ctx *ctx, const char **data, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002215{
2216 LY_ERR ret = 0;
2217 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002218 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002219 enum yang_keyword kw;
2220 struct lysp_restr *restr;
2221
2222 LYSP_ARRAY_NEW_RET(ctx, patterns, restr, LY_EMEM);
2223
2224 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002225 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002226 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002227
2228 /* add special meaning first byte */
2229 if (buf) {
2230 buf = realloc(buf, word_len + 2);
2231 word = buf;
2232 } else {
2233 buf = malloc(word_len + 2);
2234 }
Radek Krejci44ceedc2018-10-02 15:54:31 +02002235 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002236 memmove(buf + 1, word, word_len + 1);
2237 word[0] = 0x06;
Radek Krejci44ceedc2018-10-02 15:54:31 +02002238 restr->arg = lydict_insert_zc(ctx->ctx, word);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002239
2240 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002241 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002242
2243 switch (kw) {
2244 case YANG_DESCRIPTION:
2245 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts);
2246 break;
2247 case YANG_REFERENCE:
2248 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts);
2249 break;
2250 case YANG_ERROR_APP_TAG:
2251 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRTAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts);
2252 break;
2253 case YANG_ERROR_MESSAGE:
2254 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRMSG, 0, &restr->emsg, Y_STR_ARG, &restr->exts);
2255 break;
2256 case YANG_MODIFIER:
2257 ret = parse_type_pattern_modifier(ctx, data, &restr->arg, &restr->exts);
2258 break;
2259 case YANG_CUSTOM:
2260 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts);
2261 break;
2262 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002263 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002264 return LY_EVALID;
2265 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002266 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002267 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002268 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002269
2270 return ret;
2271}
2272
Michal Vaskoea5abea2018-09-18 13:10:54 +02002273/**
2274 * @brief Parse the type statement.
2275 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002276 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002277 * @param[in,out] data Data to read from, always moved to currently handled character.
2278 * @param[in,out] type Type to wrote to.
2279 *
2280 * @return LY_ERR values.
2281 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002282static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002283parse_type(struct ly_parser_ctx *ctx, const char **data, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002284{
2285 LY_ERR ret = 0;
2286 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002287 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002288 enum yang_keyword kw;
2289 struct lysp_type *nest_type;
2290
2291 if (type->name) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002292 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002293 return LY_EVALID;
2294 }
2295
2296 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002297 ret = get_argument(ctx, data, Y_PREF_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002298 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002299
Radek Krejci44ceedc2018-10-02 15:54:31 +02002300 INSERT_WORD(ctx, buf, type->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002301 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002302 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002303
2304 switch (kw) {
2305 case YANG_BASE:
2306 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_BASE, &type->bases, Y_PREF_IDENTIF_ARG, &type->exts);
2307 break;
2308 case YANG_BIT:
2309 ret = parse_type_enum(ctx, data, kw, &type->bits);
2310 break;
2311 case YANG_ENUM:
2312 ret = parse_type_enum(ctx, data, kw, &type->enums);
2313 break;
2314 case YANG_FRACTION_DIGITS:
2315 ret = parse_type_fracdigits(ctx, data, &type->fraction_digits, &type->exts);
2316 break;
2317 case YANG_LENGTH:
2318 if (type->length) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002319 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002320 return LY_EVALID;
2321 }
2322 type->length = calloc(1, sizeof *type->length);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002323 LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002324
2325 ret = parse_restr(ctx, data, kw, type->length);
2326 break;
2327 case YANG_PATH:
2328 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_PATH, 0, &type->path, Y_STR_ARG, &type->exts);
2329 break;
2330 case YANG_PATTERN:
2331 ret = parse_type_pattern(ctx, data, &type->patterns);
2332 break;
2333 case YANG_RANGE:
2334 if (type->range) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002335 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002336 return LY_EVALID;
2337 }
2338 type->range = calloc(1, sizeof *type->range);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002339 LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx->ctx), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002340
2341 ret = parse_restr(ctx, data, kw, type->range);
2342 break;
2343 case YANG_REQUIRE_INSTANCE:
2344 ret = parse_type_reqinstance(ctx, data, &type->require_instance, &type->flags, &type->exts);
2345 break;
2346 case YANG_TYPE:
2347 {
2348 LYSP_ARRAY_NEW_RET(ctx, &type->types, nest_type, LY_EMEM);
2349 }
2350 ret = parse_type(ctx, data, nest_type);
2351 break;
2352 case YANG_CUSTOM:
2353 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &type->exts);
2354 break;
2355 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002356 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002357 return LY_EVALID;
2358 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002359 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002360 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002361 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002362
2363 return ret;
2364}
2365
Michal Vaskoea5abea2018-09-18 13:10:54 +02002366/**
2367 * @brief Parse the leaf statement.
2368 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002369 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002370 * @param[in,out] data Data to read from, always moved to currently handled character.
2371 * @param[in,out] siblings Siblings to add to.
2372 *
2373 * @return LY_ERR values.
2374 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002375static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002376parse_leaf(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002377{
2378 LY_ERR ret = 0;
2379 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002380 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002381 enum yang_keyword kw;
2382 struct lysp_node *iter;
2383 struct lysp_node_leaf *leaf;
2384
2385 /* create structure */
2386 leaf = calloc(1, sizeof *leaf);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002387 LY_CHECK_ERR_RET(!leaf, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002388 leaf->nodetype = LYS_LEAF;
2389
2390 /* insert into siblings */
2391 if (!*siblings) {
2392 *siblings = (struct lysp_node *)leaf;
2393 } else {
2394 for (iter = *siblings; iter->next; iter = iter->next);
2395 iter->next = (struct lysp_node *)leaf;
2396 }
2397
2398 /* get name */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002399 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002400 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002401
Radek Krejci44ceedc2018-10-02 15:54:31 +02002402 INSERT_WORD(ctx, buf, leaf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002403
2404 /* parse substatements */
2405 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002406 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002407
2408 switch (kw) {
2409 case YANG_CONFIG:
2410 ret = parse_config(ctx, data, &leaf->flags, &leaf->exts);
2411 break;
2412 case YANG_DEFAULT:
2413 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &leaf->dflt, Y_STR_ARG, &leaf->exts);
2414 break;
2415 case YANG_DESCRIPTION:
2416 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &leaf->dsc, Y_STR_ARG, &leaf->exts);
2417 break;
2418 case YANG_IF_FEATURE:
2419 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &leaf->iffeatures, Y_STR_ARG, &leaf->exts);
2420 break;
2421 case YANG_MANDATORY:
2422 ret = parse_mandatory(ctx, data, &leaf->flags, &leaf->exts);
2423 break;
2424 case YANG_MUST:
2425 ret = parse_restrs(ctx, data, kw, &leaf->musts);
2426 break;
2427 case YANG_REFERENCE:
2428 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &leaf->ref, Y_STR_ARG, &leaf->exts);
2429 break;
2430 case YANG_STATUS:
2431 ret = parse_status(ctx, data, &leaf->flags, &leaf->exts);
2432 break;
2433 case YANG_TYPE:
2434 ret = parse_type(ctx, data, &leaf->type);
2435 break;
2436 case YANG_UNITS:
2437 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, &leaf->units, Y_STR_ARG, &leaf->exts);
2438 break;
2439 case YANG_WHEN:
2440 ret = parse_when(ctx, data, &leaf->when);
2441 break;
2442 case YANG_CUSTOM:
2443 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &leaf->exts);
2444 break;
2445 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002446 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002447 return LY_EVALID;
2448 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002449 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002450 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002451 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002452
2453 /* mandatory substatements */
2454 if (!leaf->type.name) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002455 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002456 return LY_EVALID;
2457 }
2458
2459 return ret;
2460}
2461
Michal Vaskoea5abea2018-09-18 13:10:54 +02002462/**
2463 * @brief Parse the max-elements statement.
2464 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002465 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002466 * @param[in,out] data Data to read from, always moved to currently handled character.
2467 * @param[in,out] max Value to write to.
2468 * @param[in,out] flags Flags to write to.
2469 * @param[in,out] exts Extension instances to add to.
2470 *
2471 * @return LY_ERR values.
2472 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002473static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002474parse_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 +02002475{
2476 LY_ERR ret = 0;
2477 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002478 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002479 unsigned long int num;
2480 enum yang_keyword kw;
2481
2482 if (*flags & LYS_SET_MAX) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002483 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002484 return LY_EVALID;
2485 }
2486 *flags |= LYS_SET_MAX;
2487
2488 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002489 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002490 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002491
2492 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002493 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002494 free(buf);
2495 return LY_EVALID;
2496 }
2497
2498 if (strncmp(word, "unbounded", word_len)) {
2499 errno = 0;
2500 num = strtoul(word, &ptr, 10);
2501 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002502 if ((size_t)(ptr - word) != word_len) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002503 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002504 free(buf);
2505 return LY_EVALID;
2506 }
2507 if ((errno == ERANGE) || (num > UINT32_MAX)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002508 LOGVAL_YANG(ctx, LY_VCODE_OOB, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002509 free(buf);
2510 return LY_EVALID;
2511 }
2512
2513 *max = num;
2514 }
2515 free(buf);
2516
2517 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002518 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002519
2520 switch (kw) {
2521 case YANG_CUSTOM:
2522 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MAX, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02002523 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002524 break;
2525 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002526 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002527 return LY_EVALID;
2528 }
2529 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002530 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002531
2532 return ret;
2533}
2534
Michal Vaskoea5abea2018-09-18 13:10:54 +02002535/**
2536 * @brief Parse the min-elements statement.
2537 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002538 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002539 * @param[in,out] data Data to read from, always moved to currently handled character.
2540 * @param[in,out] min Value to write to.
2541 * @param[in,out] flags Flags to write to.
2542 * @param[in,out] exts Extension instances to add to.
2543 *
2544 * @return LY_ERR values.
2545 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002546static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002547parse_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 +02002548{
2549 LY_ERR ret = 0;
2550 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002551 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002552 unsigned long int num;
2553 enum yang_keyword kw;
2554
2555 if (*flags & LYS_SET_MIN) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002556 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002557 return LY_EVALID;
2558 }
2559 *flags |= LYS_SET_MIN;
2560
2561 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002562 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002563 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002564
2565 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002566 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002567 free(buf);
2568 return LY_EVALID;
2569 }
2570
2571 errno = 0;
2572 num = strtoul(word, &ptr, 10);
2573 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002574 if ((size_t)(ptr - word) != word_len) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002575 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002576 free(buf);
2577 return LY_EVALID;
2578 }
2579 if ((errno == ERANGE) || (num > UINT32_MAX)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002580 LOGVAL_YANG(ctx, LY_VCODE_OOB, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002581 free(buf);
2582 return LY_EVALID;
2583 }
2584 *min = num;
2585 free(buf);
2586
2587 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002588 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002589
2590 switch (kw) {
2591 case YANG_CUSTOM:
2592 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MIN, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02002593 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002594 break;
2595 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002596 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002597 return LY_EVALID;
2598 }
2599 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002600 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002601
2602 return ret;
2603}
2604
Michal Vaskoea5abea2018-09-18 13:10:54 +02002605/**
2606 * @brief Parse the ordered-by statement.
2607 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002608 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002609 * @param[in,out] data Data to read from, always moved to currently handled character.
2610 * @param[in,out] flags Flags to write to.
2611 * @param[in,out] exts Extension instances to add to.
2612 *
2613 * @return LY_ERR values.
2614 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002615static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002616parse_orderedby(struct ly_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002617{
2618 LY_ERR ret = 0;
2619 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002620 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002621 enum yang_keyword kw;
2622
2623 if (*flags & LYS_ORDBY_MASK) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002624 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002625 return LY_EVALID;
2626 }
2627
2628 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002629 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002630 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002631
2632 if ((word_len == 6) && !strncmp(word, "system", word_len)) {
2633 *flags |= LYS_ORDBY_SYSTEM;
2634 } else if ((word_len == 4) && !strncmp(word, "user", word_len)) {
2635 *flags |= LYS_ORDBY_USER;
2636 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002637 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002638 free(buf);
2639 return LY_EVALID;
2640 }
2641 free(buf);
2642
2643 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002644 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002645
2646 switch (kw) {
2647 case YANG_CUSTOM:
2648 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_ORDEREDBY, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02002649 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002650 break;
2651 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002652 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002653 return LY_EVALID;
2654 }
2655 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002656 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002657
2658 return ret;
2659}
2660
Michal Vaskoea5abea2018-09-18 13:10:54 +02002661/**
2662 * @brief Parse the leaf-list statement.
2663 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002664 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002665 * @param[in,out] data Data to read from, always moved to currently handled character.
2666 * @param[in,out] siblings Siblings to add to.
2667 *
2668 * @return LY_ERR values.
2669 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002670static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002671parse_leaflist(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002672{
2673 LY_ERR ret = 0;
2674 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002675 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002676 enum yang_keyword kw;
2677 struct lysp_node *iter;
2678 struct lysp_node_leaflist *llist;
2679
2680 /* create structure */
2681 llist = calloc(1, sizeof *llist);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002682 LY_CHECK_ERR_RET(!llist, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002683 llist->nodetype = LYS_LEAFLIST;
2684
2685 /* insert into siblings */
2686 if (!*siblings) {
2687 *siblings = (struct lysp_node *)llist;
2688 } else {
2689 for (iter = *siblings; iter->next; iter = iter->next);
2690 iter->next = (struct lysp_node *)llist;
2691 }
2692
2693 /* get name */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002694 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002695 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002696
Radek Krejci44ceedc2018-10-02 15:54:31 +02002697 INSERT_WORD(ctx, buf, llist->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002698
2699 /* parse substatements */
2700 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002701 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002702
2703 switch (kw) {
2704 case YANG_CONFIG:
2705 ret = parse_config(ctx, data, &llist->flags, &llist->exts);
2706 break;
2707 case YANG_DEFAULT:
2708 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_DEFAULT, &llist->dflts, Y_STR_ARG, &llist->exts);
2709 break;
2710 case YANG_DESCRIPTION:
2711 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &llist->dsc, Y_STR_ARG, &llist->exts);
2712 break;
2713 case YANG_IF_FEATURE:
2714 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &llist->iffeatures, Y_STR_ARG, &llist->exts);
2715 break;
2716 case YANG_MAX_ELEMENTS:
2717 ret = parse_maxelements(ctx, data, &llist->max, &llist->flags, &llist->exts);
2718 break;
2719 case YANG_MIN_ELEMENTS:
2720 ret = parse_minelements(ctx, data, &llist->min, &llist->flags, &llist->exts);
2721 break;
2722 case YANG_MUST:
2723 ret = parse_restrs(ctx, data, kw, &llist->musts);
2724 break;
2725 case YANG_ORDERED_BY:
2726 ret = parse_orderedby(ctx, data, &llist->flags, &llist->exts);
2727 break;
2728 case YANG_REFERENCE:
2729 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &llist->ref, Y_STR_ARG, &llist->exts);
2730 break;
2731 case YANG_STATUS:
2732 ret = parse_status(ctx, data, &llist->flags, &llist->exts);
2733 break;
2734 case YANG_TYPE:
2735 ret = parse_type(ctx, data, &llist->type);
2736 break;
2737 case YANG_UNITS:
2738 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, &llist->units, Y_STR_ARG, &llist->exts);
2739 break;
2740 case YANG_WHEN:
2741 ret = parse_when(ctx, data, &llist->when);
2742 break;
2743 case YANG_CUSTOM:
2744 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &llist->exts);
2745 break;
2746 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002747 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002748 return LY_EVALID;
2749 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002750 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002751 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002752 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002753
2754 /* mandatory substatements */
2755 if (!llist->type.name) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002756 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002757 return LY_EVALID;
2758 }
2759
2760 return ret;
2761}
2762
Michal Vaskoea5abea2018-09-18 13:10:54 +02002763/**
2764 * @brief Parse the refine statement.
2765 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002766 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002767 * @param[in,out] data Data to read from, always moved to currently handled character.
2768 * @param[in,out] refines Refines to add to.
2769 *
2770 * @return LY_ERR values.
2771 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002772static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002773parse_refine(struct ly_parser_ctx *ctx, const char **data, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002774{
2775 LY_ERR ret = 0;
2776 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002777 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002778 enum yang_keyword kw;
2779 struct lysp_refine *rf;
2780
2781 LYSP_ARRAY_NEW_RET(ctx, refines, rf, LY_EMEM);
2782
2783 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002784 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002785 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002786
Radek Krejci44ceedc2018-10-02 15:54:31 +02002787 INSERT_WORD(ctx, buf, rf->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002788 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002789 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002790
2791 switch (kw) {
2792 case YANG_CONFIG:
2793 ret = parse_config(ctx, data, &rf->flags, &rf->exts);
2794 break;
2795 case YANG_DEFAULT:
2796 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_DEFAULT, &rf->dflts, Y_STR_ARG, &rf->exts);
2797 break;
2798 case YANG_DESCRIPTION:
2799 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &rf->dsc, Y_STR_ARG, &rf->exts);
2800 break;
2801 case YANG_IF_FEATURE:
2802 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &rf->iffeatures, Y_STR_ARG, &rf->exts);
2803 break;
2804 case YANG_MAX_ELEMENTS:
2805 ret = parse_maxelements(ctx, data, &rf->max, &rf->flags, &rf->exts);
2806 break;
2807 case YANG_MIN_ELEMENTS:
2808 ret = parse_minelements(ctx, data, &rf->min, &rf->flags, &rf->exts);
2809 break;
2810 case YANG_MUST:
2811 ret = parse_restrs(ctx, data, kw, &rf->musts);
2812 break;
2813 case YANG_MANDATORY:
2814 ret = parse_mandatory(ctx, data, &rf->flags, &rf->exts);
2815 break;
2816 case YANG_REFERENCE:
2817 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &rf->ref, Y_STR_ARG, &rf->exts);
2818 break;
2819 case YANG_PRESENCE:
2820 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_PRESENCE, 0, &rf->presence, Y_STR_ARG, &rf->exts);
2821 break;
2822 case YANG_CUSTOM:
2823 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rf->exts);
2824 break;
2825 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002826 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002827 return LY_EVALID;
2828 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002829 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002830 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002831 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002832
2833 return ret;
2834}
2835
Michal Vaskoea5abea2018-09-18 13:10:54 +02002836/**
2837 * @brief Parse the typedef statement.
2838 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002839 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002840 * @param[in,out] data Data to read from, always moved to currently handled character.
2841 * @param[in,out] typedefs Typedefs to add to.
2842 *
2843 * @return LY_ERR values.
2844 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002845static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002846parse_typedef(struct ly_parser_ctx *ctx, const char **data, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002847{
2848 LY_ERR ret = 0;
2849 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002850 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002851 enum yang_keyword kw;
2852 struct lysp_tpdf *tpdf;
2853
2854 LYSP_ARRAY_NEW_RET(ctx, typedefs, tpdf, LY_EMEM);
2855
2856 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002857 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002858 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002859
Radek Krejci44ceedc2018-10-02 15:54:31 +02002860 INSERT_WORD(ctx, buf, tpdf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002861
2862 /* parse substatements */
2863 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002864 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002865
2866 switch (kw) {
2867 case YANG_DEFAULT:
2868 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &tpdf->dflt, Y_STR_ARG, &tpdf->exts);
2869 break;
2870 case YANG_DESCRIPTION:
2871 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &tpdf->dsc, Y_STR_ARG, &tpdf->exts);
2872 break;
2873 case YANG_REFERENCE:
2874 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &tpdf->ref, Y_STR_ARG, &tpdf->exts);
2875 break;
2876 case YANG_STATUS:
2877 ret = parse_status(ctx, data, &tpdf->flags, &tpdf->exts);
2878 break;
2879 case YANG_TYPE:
2880 ret = parse_type(ctx, data, &tpdf->type);
2881 break;
2882 case YANG_UNITS:
2883 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, &tpdf->units, Y_STR_ARG, &tpdf->exts);
2884 break;
2885 case YANG_CUSTOM:
2886 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &tpdf->exts);
2887 break;
2888 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002889 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002890 return LY_EVALID;
2891 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002892 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002893 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002894 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002895
2896 /* mandatory substatements */
2897 if (!tpdf->type.name) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002898 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002899 return LY_EVALID;
2900 }
2901
2902 return ret;
2903}
2904
Michal Vaskoea5abea2018-09-18 13:10:54 +02002905/**
2906 * @brief Parse the input or output statement.
2907 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002908 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002909 * @param[in,out] data Data to read from, always moved to currently handled character.
2910 * @param[in] kw Type of this particular keyword
2911 * @param[in,out] inout_p Input/output pointer to write to.
2912 *
2913 * @return LY_ERR values.
2914 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002915static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002916parse_inout(struct ly_parser_ctx *ctx, const char **data, enum yang_keyword kw, struct lysp_action_inout **inout_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002917{
2918 LY_ERR ret = 0;
2919 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002920 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002921 struct lysp_action_inout *inout;
2922
2923 if (*inout_p) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002924 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002925 return LY_EVALID;
2926 }
2927
2928 /* create structure */
2929 inout = calloc(1, sizeof *inout);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002930 LY_CHECK_ERR_RET(!inout, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002931 *inout_p = inout;
2932
2933 /* parse substatements */
2934 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002935 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002936
2937 switch (kw) {
2938 case YANG_ANYDATA:
2939 case YANG_ANYXML:
2940 ret = parse_any(ctx, data, kw, &inout->data);
2941 break;
2942 case YANG_CHOICE:
2943 ret = parse_choice(ctx, data, &inout->data);
2944 break;
2945 case YANG_CONTAINER:
2946 ret = parse_container(ctx, data, &inout->data);
2947 break;
2948 case YANG_LEAF:
2949 ret = parse_leaf(ctx, data, &inout->data);
2950 break;
2951 case YANG_LEAF_LIST:
2952 ret = parse_leaflist(ctx, data, &inout->data);
2953 break;
2954 case YANG_LIST:
2955 ret = parse_list(ctx, data, &inout->data);
2956 break;
2957 case YANG_USES:
2958 ret = parse_uses(ctx, data, &inout->data);
2959 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002960 case YANG_TYPEDEF:
2961 ret = parse_typedef(ctx, data, &inout->typedefs);
2962 break;
2963 case YANG_MUST:
2964 ret = parse_restrs(ctx, data, kw, &inout->musts);
2965 break;
2966 case YANG_GROUPING:
2967 ret = parse_grouping(ctx, data, &inout->groupings);
2968 break;
2969 case YANG_CUSTOM:
2970 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inout->exts);
2971 break;
2972 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002973 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "input/output");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002974 return LY_EVALID;
2975 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002976 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002977 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002978 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002979
2980 return ret;
2981}
2982
Michal Vaskoea5abea2018-09-18 13:10:54 +02002983/**
2984 * @brief Parse the action statement.
2985 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002986 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002987 * @param[in,out] data Data to read from, always moved to currently handled character.
2988 * @param[in,out] actions Actions to add to.
2989 *
2990 * @return LY_ERR values.
2991 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002992static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002993parse_action(struct ly_parser_ctx *ctx, const char **data, struct lysp_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002994{
2995 LY_ERR ret = 0;
2996 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002997 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002998 enum yang_keyword kw;
2999 struct lysp_action *act;
3000
3001 LYSP_ARRAY_NEW_RET(ctx, actions, act, LY_EMEM);
3002
3003 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003004 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003005 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003006
Radek Krejci44ceedc2018-10-02 15:54:31 +02003007 INSERT_WORD(ctx, buf, act->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003008 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003009 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003010
3011 switch (kw) {
3012 case YANG_DESCRIPTION:
3013 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &act->dsc, Y_STR_ARG, &act->exts);
3014 break;
3015 case YANG_IF_FEATURE:
3016 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &act->iffeatures, Y_STR_ARG, &act->exts);
3017 break;
3018 case YANG_REFERENCE:
3019 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &act->ref, Y_STR_ARG, &act->exts);
3020 break;
3021 case YANG_STATUS:
3022 ret = parse_status(ctx, data, &act->flags, &act->exts);
3023 break;
3024
3025 case YANG_INPUT:
3026 ret = parse_inout(ctx, data, kw, &act->input);
3027 break;
3028 case YANG_OUTPUT:
3029 ret = parse_inout(ctx, data, kw, &act->output);
3030 break;
3031
3032 case YANG_TYPEDEF:
3033 ret = parse_typedef(ctx, data, &act->typedefs);
3034 break;
3035 case YANG_GROUPING:
3036 ret = parse_grouping(ctx, data, &act->groupings);
3037 break;
3038 case YANG_CUSTOM:
3039 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &act->exts);
3040 break;
3041 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003042 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "action");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003043 return LY_EVALID;
3044 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003045 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003046 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003047 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003048
3049 return ret;
3050}
3051
Michal Vaskoea5abea2018-09-18 13:10:54 +02003052/**
3053 * @brief Parse the notification statement.
3054 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003055 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003056 * @param[in,out] data Data to read from, always moved to currently handled character.
3057 * @param[in,out] notifs Notifications to add to.
3058 *
3059 * @return LY_ERR values.
3060 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003061static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003062parse_notif(struct ly_parser_ctx *ctx, const char **data, struct lysp_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003063{
3064 LY_ERR ret = 0;
3065 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003066 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003067 enum yang_keyword kw;
3068 struct lysp_notif *notif;
3069
3070 LYSP_ARRAY_NEW_RET(ctx, notifs, notif, LY_EMEM);
3071
3072 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003073 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003074 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003075
Radek Krejci44ceedc2018-10-02 15:54:31 +02003076 INSERT_WORD(ctx, buf, notif->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003077 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003078 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003079
3080 switch (kw) {
3081 case YANG_DESCRIPTION:
3082 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &notif->dsc, Y_STR_ARG, &notif->exts);
3083 break;
3084 case YANG_IF_FEATURE:
3085 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &notif->iffeatures, Y_STR_ARG, &notif->exts);
3086 break;
3087 case YANG_REFERENCE:
3088 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &notif->ref, Y_STR_ARG, &notif->exts);
3089 break;
3090 case YANG_STATUS:
3091 ret = parse_status(ctx, data, &notif->flags, &notif->exts);
3092 break;
3093
3094 case YANG_ANYDATA:
3095 case YANG_ANYXML:
3096 ret = parse_any(ctx, data, kw, &notif->data);
3097 break;
3098 case YANG_CHOICE:
3099 ret = parse_case(ctx, data, &notif->data);
3100 break;
3101 case YANG_CONTAINER:
3102 ret = parse_container(ctx, data, &notif->data);
3103 break;
3104 case YANG_LEAF:
3105 ret = parse_leaf(ctx, data, &notif->data);
3106 break;
3107 case YANG_LEAF_LIST:
3108 ret = parse_leaflist(ctx, data, &notif->data);
3109 break;
3110 case YANG_LIST:
3111 ret = parse_list(ctx, data, &notif->data);
3112 break;
3113 case YANG_USES:
3114 ret = parse_uses(ctx, data, &notif->data);
3115 break;
3116
3117 case YANG_MUST:
3118 ret = parse_restrs(ctx, data, kw, &notif->musts);
3119 break;
3120 case YANG_TYPEDEF:
3121 ret = parse_typedef(ctx, data, &notif->typedefs);
3122 break;
3123 case YANG_GROUPING:
3124 ret = parse_grouping(ctx, data, &notif->groupings);
3125 break;
3126 case YANG_CUSTOM:
3127 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &notif->exts);
3128 break;
3129 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003130 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003131 return LY_EVALID;
3132 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003133 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003134 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003135 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003136
3137 return ret;
3138}
3139
Michal Vaskoea5abea2018-09-18 13:10:54 +02003140/**
3141 * @brief Parse the grouping statement.
3142 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003143 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003144 * @param[in,out] data Data to read from, always moved to currently handled character.
3145 * @param[in,out] groupings Groupings to add to.
3146 *
3147 * @return LY_ERR values.
3148 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003149static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003150parse_grouping(struct ly_parser_ctx *ctx, const char **data, struct lysp_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003151{
3152 LY_ERR ret = 0;
3153 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003154 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003155 enum yang_keyword kw;
3156 struct lysp_grp *grp;
3157
3158 LYSP_ARRAY_NEW_RET(ctx, groupings, grp, LY_EMEM);
3159
3160 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003161 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003162 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003163
Radek Krejci44ceedc2018-10-02 15:54:31 +02003164 INSERT_WORD(ctx, buf, grp->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003165
3166 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003167 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003168
3169 switch (kw) {
3170 case YANG_DESCRIPTION:
3171 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &grp->dsc, Y_STR_ARG, &grp->exts);
3172 break;
3173 case YANG_REFERENCE:
3174 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &grp->ref, Y_STR_ARG, &grp->exts);
3175 break;
3176 case YANG_STATUS:
3177 ret = parse_status(ctx, data, &grp->flags, &grp->exts);
3178 break;
3179
3180 case YANG_ANYDATA:
3181 case YANG_ANYXML:
3182 ret = parse_any(ctx, data, kw, &grp->data);
3183 break;
3184 case YANG_CHOICE:
3185 ret = parse_choice(ctx, data, &grp->data);
3186 break;
3187 case YANG_CONTAINER:
3188 ret = parse_container(ctx, data, &grp->data);
3189 break;
3190 case YANG_LEAF:
3191 ret = parse_leaf(ctx, data, &grp->data);
3192 break;
3193 case YANG_LEAF_LIST:
3194 ret = parse_leaflist(ctx, data, &grp->data);
3195 break;
3196 case YANG_LIST:
3197 ret = parse_list(ctx, data, &grp->data);
3198 break;
3199 case YANG_USES:
3200 ret = parse_uses(ctx, data, &grp->data);
3201 break;
3202
3203 case YANG_TYPEDEF:
3204 ret = parse_typedef(ctx, data, &grp->typedefs);
3205 break;
3206 case YANG_ACTION:
3207 ret = parse_action(ctx, data, &grp->actions);
3208 break;
3209 case YANG_GROUPING:
3210 ret = parse_grouping(ctx, data, &grp->groupings);
3211 break;
3212 case YANG_NOTIFICATION:
3213 ret = parse_notif(ctx, data, &grp->notifs);
3214 break;
3215 case YANG_CUSTOM:
3216 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &grp->exts);
3217 break;
3218 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003219 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003220 return LY_EVALID;
3221 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003222 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003223 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003224 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003225
3226 return ret;
3227}
3228
Michal Vaskoea5abea2018-09-18 13:10:54 +02003229/**
3230 * @brief Parse the refine statement.
3231 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003232 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003233 * @param[in,out] data Data to read from, always moved to currently handled character.
3234 * @param[in,out] augments Augments to add to.
3235 *
3236 * @return LY_ERR values.
3237 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003238static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003239parse_augment(struct ly_parser_ctx *ctx, const char **data, struct lysp_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003240{
3241 LY_ERR ret = 0;
3242 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003243 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003244 enum yang_keyword kw;
3245 struct lysp_augment *aug;
3246
3247 LYSP_ARRAY_NEW_RET(ctx, augments, aug, LY_EMEM);
3248
3249 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003250 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003251 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003252
Radek Krejci44ceedc2018-10-02 15:54:31 +02003253 INSERT_WORD(ctx, buf, aug->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003254 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003255 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003256
3257 switch (kw) {
3258 case YANG_DESCRIPTION:
3259 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &aug->dsc, Y_STR_ARG, &aug->exts);
3260 break;
3261 case YANG_IF_FEATURE:
3262 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &aug->iffeatures, Y_STR_ARG, &aug->exts);
3263 break;
3264 case YANG_REFERENCE:
3265 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &aug->ref, Y_STR_ARG, &aug->exts);
3266 break;
3267 case YANG_STATUS:
3268 ret = parse_status(ctx, data, &aug->flags, &aug->exts);
3269 break;
3270 case YANG_WHEN:
3271 ret = parse_when(ctx, data, &aug->when);
3272 break;
3273
3274 case YANG_ANYDATA:
3275 case YANG_ANYXML:
3276 ret = parse_any(ctx, data, kw, &aug->child);
3277 break;
3278 case YANG_CASE:
3279 ret = parse_case(ctx, data, &aug->child);
3280 break;
3281 case YANG_CHOICE:
3282 ret = parse_choice(ctx, data, &aug->child);
3283 break;
3284 case YANG_CONTAINER:
3285 ret = parse_container(ctx, data, &aug->child);
3286 break;
3287 case YANG_LEAF:
3288 ret = parse_leaf(ctx, data, &aug->child);
3289 break;
3290 case YANG_LEAF_LIST:
3291 ret = parse_leaflist(ctx, data, &aug->child);
3292 break;
3293 case YANG_LIST:
3294 ret = parse_list(ctx, data, &aug->child);
3295 break;
3296 case YANG_USES:
3297 ret = parse_uses(ctx, data, &aug->child);
3298 break;
3299
3300 case YANG_ACTION:
3301 ret = parse_action(ctx, data, &aug->actions);
3302 break;
3303 case YANG_NOTIFICATION:
3304 ret = parse_notif(ctx, data, &aug->notifs);
3305 break;
3306 case YANG_CUSTOM:
3307 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &aug->exts);
3308 break;
3309 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003310 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003311 return LY_EVALID;
3312 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003313 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003314 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003315 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003316
3317 return ret;
3318}
3319
Michal Vaskoea5abea2018-09-18 13:10:54 +02003320/**
3321 * @brief Parse the uses statement.
3322 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003323 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003324 * @param[in,out] data Data to read from, always moved to currently handled character.
3325 * @param[in,out] siblings Siblings to add to.
3326 *
3327 * @return LY_ERR values.
3328 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003329static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003330parse_uses(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003331{
3332 LY_ERR ret = 0;
3333 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003334 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003335 enum yang_keyword kw;
3336 struct lysp_node *iter;
3337 struct lysp_node_uses *uses;
3338
3339 /* create structure */
3340 uses = calloc(1, sizeof *uses);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003341 LY_CHECK_ERR_RET(!uses, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003342 uses->nodetype = LYS_USES;
3343
3344 /* insert into siblings */
3345 if (!*siblings) {
3346 *siblings = (struct lysp_node *)uses;
3347 } else {
3348 for (iter = *siblings; iter->next; iter = iter->next);
3349 iter->next = (struct lysp_node *)uses;
3350 }
3351
3352 /* get name */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003353 ret = get_argument(ctx, data, Y_PREF_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003354 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003355
Radek Krejci44ceedc2018-10-02 15:54:31 +02003356 INSERT_WORD(ctx, buf, uses->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003357
3358 /* parse substatements */
3359 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003360 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003361
3362 switch (kw) {
3363 case YANG_DESCRIPTION:
3364 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &uses->dsc, Y_STR_ARG, &uses->exts);
3365 break;
3366 case YANG_IF_FEATURE:
3367 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &uses->iffeatures, Y_STR_ARG, &uses->exts);
3368 break;
3369 case YANG_REFERENCE:
3370 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &uses->ref, Y_STR_ARG, &uses->exts);
3371 break;
3372 case YANG_STATUS:
3373 ret = parse_status(ctx, data, &uses->flags, &uses->exts);
3374 break;
3375 case YANG_WHEN:
3376 ret = parse_when(ctx, data, &uses->when);
3377 break;
3378
3379 case YANG_REFINE:
3380 ret = parse_refine(ctx, data, &uses->refines);
3381 break;
3382 case YANG_AUGMENT:
3383 ret = parse_augment(ctx, data, &uses->augments);
3384 break;
3385 case YANG_CUSTOM:
3386 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &uses->exts);
3387 break;
3388 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003389 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003390 return LY_EVALID;
3391 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003392 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003393 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003394 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003395
3396 return ret;
3397}
3398
Michal Vaskoea5abea2018-09-18 13:10:54 +02003399/**
3400 * @brief Parse the case statement.
3401 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003402 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003403 * @param[in,out] data Data to read from, always moved to currently handled character.
3404 * @param[in,out] siblings Siblings to add to.
3405 *
3406 * @return LY_ERR values.
3407 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003408static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003409parse_case(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003410{
3411 LY_ERR ret = 0;
3412 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003413 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003414 enum yang_keyword kw;
3415 struct lysp_node *iter;
3416 struct lysp_node_case *cas;
3417
3418 /* create structure */
3419 cas = calloc(1, sizeof *cas);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003420 LY_CHECK_ERR_RET(!cas, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003421 cas->nodetype = LYS_CASE;
3422
3423 /* insert into siblings */
3424 if (!*siblings) {
3425 *siblings = (struct lysp_node *)cas;
3426 } else {
3427 for (iter = *siblings; iter->next; iter = iter->next);
3428 iter->next = (struct lysp_node *)cas;
3429 }
3430
3431 /* get name */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003432 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003433 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003434
Radek Krejci44ceedc2018-10-02 15:54:31 +02003435 INSERT_WORD(ctx, buf, cas->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003436
3437 /* parse substatements */
3438 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003439 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003440
3441 switch (kw) {
3442 case YANG_DESCRIPTION:
3443 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &cas->dsc, Y_STR_ARG, &cas->exts);
3444 break;
3445 case YANG_IF_FEATURE:
3446 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &cas->iffeatures, Y_STR_ARG, &cas->exts);
3447 break;
3448 case YANG_REFERENCE:
3449 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &cas->ref, Y_STR_ARG, &cas->exts);
3450 break;
3451 case YANG_STATUS:
3452 ret = parse_status(ctx, data, &cas->flags, &cas->exts);
3453 break;
3454 case YANG_WHEN:
3455 ret = parse_when(ctx, data, &cas->when);
3456 break;
3457
3458 case YANG_ANYDATA:
3459 case YANG_ANYXML:
3460 ret = parse_any(ctx, data, kw, &cas->child);
3461 break;
3462 case YANG_CHOICE:
3463 ret = parse_case(ctx, data, &cas->child);
3464 break;
3465 case YANG_CONTAINER:
3466 ret = parse_container(ctx, data, &cas->child);
3467 break;
3468 case YANG_LEAF:
3469 ret = parse_leaf(ctx, data, &cas->child);
3470 break;
3471 case YANG_LEAF_LIST:
3472 ret = parse_leaflist(ctx, data, &cas->child);
3473 break;
3474 case YANG_LIST:
3475 ret = parse_list(ctx, data, &cas->child);
3476 break;
3477 case YANG_USES:
3478 ret = parse_uses(ctx, data, &cas->child);
3479 break;
3480 case YANG_CUSTOM:
3481 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cas->exts);
3482 break;
3483 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003484 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003485 return LY_EVALID;
3486 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003487 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003488 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003489 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003490
3491 return ret;
3492}
3493
Michal Vaskoea5abea2018-09-18 13:10:54 +02003494/**
3495 * @brief Parse the choice statement.
3496 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003497 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003498 * @param[in,out] data Data to read from, always moved to currently handled character.
3499 * @param[in,out] siblings Siblings to add to.
3500 *
3501 * @return LY_ERR values.
3502 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003503static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003504parse_choice(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003505{
3506 LY_ERR ret = 0;
3507 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003508 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003509 enum yang_keyword kw;
3510 struct lysp_node *iter;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003511 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003512
3513 /* create structure */
Radek Krejci44ceedc2018-10-02 15:54:31 +02003514 choice = calloc(1, sizeof *choice);
3515 LY_CHECK_ERR_RET(!choice, LOGMEM(ctx->ctx), LY_EMEM);
3516 choice->nodetype = LYS_CHOICE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003517
3518 /* insert into siblings */
3519 if (!*siblings) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02003520 *siblings = (struct lysp_node *)choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003521 } else {
3522 for (iter = *siblings; iter->next; iter = iter->next);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003523 iter->next = (struct lysp_node *)choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003524 }
3525
3526 /* get name */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003527 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003528 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003529
Radek Krejci44ceedc2018-10-02 15:54:31 +02003530 INSERT_WORD(ctx, buf, choice->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003531
3532 /* parse substatements */
3533 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003534 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003535
3536 switch (kw) {
3537 case YANG_CONFIG:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003538 ret = parse_config(ctx, data, &choice->flags, &choice->exts);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003539 break;
3540 case YANG_DESCRIPTION:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003541 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &choice->dsc, Y_STR_ARG, &choice->exts);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003542 break;
3543 case YANG_IF_FEATURE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003544 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &choice->iffeatures, Y_STR_ARG, &choice->exts);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003545 break;
3546 case YANG_MANDATORY:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003547 ret = parse_mandatory(ctx, data, &choice->flags, &choice->exts);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003548 break;
3549 case YANG_REFERENCE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003550 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &choice->ref, Y_STR_ARG, &choice->exts);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003551 break;
3552 case YANG_STATUS:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003553 ret = parse_status(ctx, data, &choice->flags, &choice->exts);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003554 break;
3555 case YANG_WHEN:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003556 ret = parse_when(ctx, data, &choice->when);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003557 break;
3558 case YANG_DEFAULT:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003559 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &choice->dflt, Y_IDENTIF_ARG, &choice->exts);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003560 break;
3561
3562 case YANG_ANYDATA:
3563 case YANG_ANYXML:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003564 ret = parse_any(ctx, data, kw, &choice->child);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003565 break;
3566 case YANG_CASE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003567 ret = parse_case(ctx, data, &choice->child);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003568 break;
3569 case YANG_CHOICE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003570 ret = parse_choice(ctx, data, &choice->child);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003571 break;
3572 case YANG_CONTAINER:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003573 ret = parse_container(ctx, data, &choice->child);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003574 break;
3575 case YANG_LEAF:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003576 ret = parse_leaf(ctx, data, &choice->child);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003577 break;
3578 case YANG_LEAF_LIST:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003579 ret = parse_leaflist(ctx, data, &choice->child);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003580 break;
3581 case YANG_LIST:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003582 ret = parse_list(ctx, data, &choice->child);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003583 break;
3584 case YANG_CUSTOM:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003585 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &choice->exts);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003586 break;
3587 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003588 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003589 return LY_EVALID;
3590 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003591 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003592 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003593 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003594
3595 return ret;
3596}
3597
Michal Vaskoea5abea2018-09-18 13:10:54 +02003598/**
3599 * @brief Parse the container statement.
3600 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003601 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003602 * @param[in,out] data Data to read from, always moved to currently handled character.
3603 * @param[in,out] siblings Siblings to add to.
3604 *
3605 * @return LY_ERR values.
3606 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003607static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003608parse_container(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003609{
3610 LY_ERR ret = 0;
3611 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003612 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003613 enum yang_keyword kw;
3614 struct lysp_node *iter;
3615 struct lysp_node_container *cont;
3616
3617 /* create structure */
3618 cont = calloc(1, sizeof *cont);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003619 LY_CHECK_ERR_RET(!cont, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003620 cont->nodetype = LYS_CONTAINER;
3621
3622 /* insert into siblings */
3623 if (!*siblings) {
3624 *siblings = (struct lysp_node *)cont;
3625 } else {
3626 for (iter = *siblings; iter->next; iter = iter->next);
3627 iter->next = (struct lysp_node *)cont;
3628 }
3629
3630 /* get name */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003631 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003632 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003633
Radek Krejci44ceedc2018-10-02 15:54:31 +02003634 INSERT_WORD(ctx, buf, cont->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003635
3636 /* parse substatements */
3637 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003638 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003639
3640 switch (kw) {
3641 case YANG_CONFIG:
3642 ret = parse_config(ctx, data, &cont->flags, &cont->exts);
3643 break;
3644 case YANG_DESCRIPTION:
3645 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &cont->dsc, Y_STR_ARG, &cont->exts);
3646 break;
3647 case YANG_IF_FEATURE:
3648 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &cont->iffeatures, Y_STR_ARG, &cont->exts);
3649 break;
3650 case YANG_REFERENCE:
3651 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &cont->ref, Y_STR_ARG, &cont->exts);
3652 break;
3653 case YANG_STATUS:
3654 ret = parse_status(ctx, data, &cont->flags, &cont->exts);
3655 break;
3656 case YANG_WHEN:
3657 ret = parse_when(ctx, data, &cont->when);
3658 break;
3659 case YANG_PRESENCE:
3660 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_PRESENCE, 0, &cont->presence, Y_STR_ARG, &cont->exts);
3661 break;
3662
3663 case YANG_ANYDATA:
3664 case YANG_ANYXML:
3665 ret = parse_any(ctx, data, kw, &cont->child);
3666 break;
3667 case YANG_CHOICE:
3668 ret = parse_choice(ctx, data, &cont->child);
3669 break;
3670 case YANG_CONTAINER:
3671 ret = parse_container(ctx, data, &cont->child);
3672 break;
3673 case YANG_LEAF:
3674 ret = parse_leaf(ctx, data, &cont->child);
3675 break;
3676 case YANG_LEAF_LIST:
3677 ret = parse_leaflist(ctx, data, &cont->child);
3678 break;
3679 case YANG_LIST:
3680 ret = parse_list(ctx, data, &cont->child);
3681 break;
3682 case YANG_USES:
3683 ret = parse_uses(ctx, data, &cont->child);
3684 break;
3685
3686 case YANG_TYPEDEF:
3687 ret = parse_typedef(ctx, data, &cont->typedefs);
3688 break;
3689 case YANG_MUST:
3690 ret = parse_restrs(ctx, data, kw, &cont->musts);
3691 break;
3692 case YANG_ACTION:
3693 ret = parse_action(ctx, data, &cont->actions);
3694 break;
3695 case YANG_GROUPING:
3696 ret = parse_grouping(ctx, data, &cont->groupings);
3697 break;
3698 case YANG_NOTIFICATION:
3699 ret = parse_notif(ctx, data, &cont->notifs);
3700 break;
3701 case YANG_CUSTOM:
3702 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cont->exts);
3703 break;
3704 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003705 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003706 return LY_EVALID;
3707 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003708 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003709 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003710 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003711
3712 return ret;
3713}
3714
Michal Vaskoea5abea2018-09-18 13:10:54 +02003715/**
3716 * @brief Parse the list statement.
3717 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003718 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003719 * @param[in,out] data Data to read from, always moved to currently handled character.
3720 * @param[in,out] siblings Siblings to add to.
3721 *
3722 * @return LY_ERR values.
3723 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003724static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003725parse_list(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003726{
3727 LY_ERR ret = 0;
3728 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003729 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003730 enum yang_keyword kw;
3731 struct lysp_node *iter;
3732 struct lysp_node_list *list;
3733
3734 /* create structure */
3735 list = calloc(1, sizeof *list);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003736 LY_CHECK_ERR_RET(!list, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003737 list->nodetype = LYS_LIST;
3738
3739 /* insert into siblings */
3740 if (!*siblings) {
3741 *siblings = (struct lysp_node *)list;
3742 } else {
3743 for (iter = *siblings; iter->next; iter = iter->next);
3744 iter->next = (struct lysp_node *)list;
3745 }
3746
3747 /* get name */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003748 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003749 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003750
Radek Krejci44ceedc2018-10-02 15:54:31 +02003751 INSERT_WORD(ctx, buf, list->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003752
3753 /* parse substatements */
3754 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003755 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003756
3757 switch (kw) {
3758 case YANG_CONFIG:
3759 ret = parse_config(ctx, data, &list->flags, &list->exts);
3760 break;
3761 case YANG_DESCRIPTION:
3762 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &list->dsc, Y_STR_ARG, &list->exts);
3763 break;
3764 case YANG_IF_FEATURE:
3765 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &list->iffeatures, Y_STR_ARG, &list->exts);
3766 break;
3767 case YANG_REFERENCE:
3768 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &list->ref, Y_STR_ARG, &list->exts);
3769 break;
3770 case YANG_STATUS:
3771 ret = parse_status(ctx, data, &list->flags, &list->exts);
3772 break;
3773 case YANG_WHEN:
3774 ret = parse_when(ctx, data, &list->when);
3775 break;
3776 case YANG_KEY:
3777 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_KEY, 0, &list->key, Y_STR_ARG, &list->exts);
3778 break;
3779 case YANG_MAX_ELEMENTS:
3780 ret = parse_maxelements(ctx, data, &list->max, &list->flags, &list->exts);
3781 break;
3782 case YANG_MIN_ELEMENTS:
3783 ret = parse_minelements(ctx, data, &list->min, &list->flags, &list->exts);
3784 break;
3785 case YANG_ORDERED_BY:
3786 ret = parse_orderedby(ctx, data, &list->flags, &list->exts);
3787 break;
3788 case YANG_UNIQUE:
3789 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_UNIQUE, &list->uniques, Y_STR_ARG, &list->exts);
3790 break;
3791
3792 case YANG_ANYDATA:
3793 case YANG_ANYXML:
3794 ret = parse_any(ctx, data, kw, &list->child);
3795 break;
3796 case YANG_CHOICE:
3797 ret = parse_choice(ctx, data, &list->child);
3798 break;
3799 case YANG_CONTAINER:
3800 ret = parse_container(ctx, data, &list->child);
3801 break;
3802 case YANG_LEAF:
3803 ret = parse_leaf(ctx, data, &list->child);
3804 break;
3805 case YANG_LEAF_LIST:
3806 ret = parse_leaflist(ctx, data, &list->child);
3807 break;
3808 case YANG_LIST:
3809 ret = parse_list(ctx, data, &list->child);
3810 break;
3811 case YANG_USES:
3812 ret = parse_uses(ctx, data, &list->child);
3813 break;
3814
3815 case YANG_TYPEDEF:
3816 ret = parse_typedef(ctx, data, &list->typedefs);
3817 break;
3818 case YANG_MUST:
3819 ret = parse_restrs(ctx, data, kw, &list->musts);
3820 break;
3821 case YANG_ACTION:
3822 ret = parse_action(ctx, data, &list->actions);
3823 break;
3824 case YANG_GROUPING:
3825 ret = parse_grouping(ctx, data, &list->groupings);
3826 break;
3827 case YANG_NOTIFICATION:
3828 ret = parse_notif(ctx, data, &list->notifs);
3829 break;
3830 case YANG_CUSTOM:
3831 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &list->exts);
3832 break;
3833 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003834 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003835 return LY_EVALID;
3836 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003837 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003838 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003839 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003840
3841 return ret;
3842}
3843
Michal Vaskoea5abea2018-09-18 13:10:54 +02003844/**
3845 * @brief Parse the yin-element statement.
3846 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003847 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003848 * @param[in,out] data Data to read from, always moved to currently handled character.
3849 * @param[in,out] flags Flags to write to.
3850 * @param[in,out] exts Extension instances to add to.
3851 *
3852 * @return LY_ERR values.
3853 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003854static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003855parse_yinelement(struct ly_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003856{
3857 LY_ERR ret = 0;
3858 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003859 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003860 enum yang_keyword kw;
3861
3862 if (*flags & LYS_YINELEM_MASK) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02003863 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003864 return LY_EVALID;
3865 }
3866
3867 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003868 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003869 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003870
3871 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
3872 *flags |= LYS_YINELEM_TRUE;
3873 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
3874 *flags |= LYS_YINELEM_FALSE;
3875 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02003876 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003877 free(buf);
3878 return LY_EVALID;
3879 }
3880 free(buf);
3881
3882 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003883 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003884
3885 switch (kw) {
3886 case YANG_CUSTOM:
3887 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_YINELEM, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02003888 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003889 break;
3890 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003891 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003892 return LY_EVALID;
3893 }
3894 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003895 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003896
3897 return ret;
3898}
3899
Michal Vaskoea5abea2018-09-18 13:10:54 +02003900/**
3901 * @brief Parse the yin-element statement.
3902 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003903 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003904 * @param[in,out] data Data to read from, always moved to currently handled character.
3905 * @param[in,out] argument Value to write to.
3906 * @param[in,out] flags Flags to write to.
3907 * @param[in,out] exts Extension instances to add to.
3908 *
3909 * @return LY_ERR values.
3910 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003911static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003912parse_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 +02003913{
3914 LY_ERR ret = 0;
3915 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003916 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003917 enum yang_keyword kw;
3918
3919 if (*argument) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02003920 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003921 return LY_EVALID;
3922 }
3923
3924 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003925 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003926 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003927
Radek Krejci44ceedc2018-10-02 15:54:31 +02003928 INSERT_WORD(ctx, buf, *argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003929 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003930 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003931
3932 switch (kw) {
3933 case YANG_YIN_ELEMENT:
3934 ret = parse_yinelement(ctx, data, flags, exts);
3935 break;
3936 case YANG_CUSTOM:
3937 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_ARGUMENT, 0, exts);
3938 break;
3939 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003940 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003941 return LY_EVALID;
3942 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003943 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003944 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003945 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003946
3947 return ret;
3948}
3949
Michal Vaskoea5abea2018-09-18 13:10:54 +02003950/**
3951 * @brief Parse the extension statement.
3952 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003953 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003954 * @param[in,out] data Data to read from, always moved to currently handled character.
3955 * @param[in,out] extensions Extensions to add to.
3956 *
3957 * @return LY_ERR values.
3958 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003959static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003960parse_extension(struct ly_parser_ctx *ctx, const char **data, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003961{
3962 LY_ERR ret = 0;
3963 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003964 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003965 enum yang_keyword kw;
3966 struct lysp_ext *ex;
3967
3968 LYSP_ARRAY_NEW_RET(ctx, extensions, ex, LY_EMEM);
3969
3970 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003971 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003972 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003973
Radek Krejci44ceedc2018-10-02 15:54:31 +02003974 INSERT_WORD(ctx, buf, ex->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003975 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003976 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003977
3978 switch (kw) {
3979 case YANG_DESCRIPTION:
3980 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &ex->dsc, Y_STR_ARG, &ex->exts);
3981 break;
3982 case YANG_REFERENCE:
3983 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &ex->ref, Y_STR_ARG, &ex->exts);
3984 break;
3985 case YANG_STATUS:
3986 ret = parse_status(ctx, data, &ex->flags, &ex->exts);
3987 break;
3988 case YANG_ARGUMENT:
3989 ret = parse_argument(ctx, data, &ex->argument, &ex->flags, &ex->exts);
3990 break;
3991 case YANG_CUSTOM:
3992 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ex->exts);
3993 break;
3994 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003995 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003996 return LY_EVALID;
3997 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003998 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003999 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004000 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004001
4002 return ret;
4003}
4004
Michal Vaskoea5abea2018-09-18 13:10:54 +02004005/**
4006 * @brief Parse the deviate statement.
4007 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004008 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004009 * @param[in,out] data Data to read from, always moved to currently handled character.
4010 * @param[in,out] deviates Deviates to add to.
4011 *
4012 * @return LY_ERR values.
4013 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004014static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02004015parse_deviate(struct ly_parser_ctx *ctx, const char **data, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004016{
4017 LY_ERR ret = 0;
4018 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004019 size_t word_len, dev_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004020 enum yang_keyword kw;
4021 struct lysp_deviate *iter, *d;
4022 struct lysp_deviate_add *d_add = NULL;
4023 struct lysp_deviate_rpl *d_rpl = NULL;
4024 struct lysp_deviate_del *d_del = NULL;
4025 const char **d_units, ***d_uniques, ***d_dflts;
4026 struct lysp_restr **d_musts;
4027 uint16_t *d_flags;
4028 uint32_t *d_min, *d_max;
4029
4030 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02004031 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02004032 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004033
4034 if ((word_len == 13) && !strncmp(word, "not-supported", word_len)) {
4035 dev_mod = LYS_DEV_NOT_SUPPORTED;
4036 } else if ((word_len == 3) && !strncmp(word, "add", word_len)) {
4037 dev_mod = LYS_DEV_ADD;
4038 } else if ((word_len == 7) && !strncmp(word, "replace", word_len)) {
4039 dev_mod = LYS_DEV_REPLACE;
4040 } else if ((word_len == 6) && !strncmp(word, "delete", word_len)) {
4041 dev_mod = LYS_DEV_DELETE;
4042 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004043 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004044 free(buf);
4045 return LY_EVALID;
4046 }
4047 free(buf);
4048
4049 /* create structure */
4050 switch (dev_mod) {
4051 case LYS_DEV_NOT_SUPPORTED:
4052 d = calloc(1, sizeof *d);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004053 LY_CHECK_ERR_RET(!d, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004054 break;
4055 case LYS_DEV_ADD:
4056 d_add = calloc(1, sizeof *d_add);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004057 LY_CHECK_ERR_RET(!d_add, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004058 d = (struct lysp_deviate *)d_add;
4059 d_units = &d_add->units;
4060 d_uniques = &d_add->uniques;
4061 d_dflts = &d_add->dflts;
4062 d_musts = &d_add->musts;
4063 d_flags = &d_add->flags;
4064 d_min = &d_add->min;
4065 d_max = &d_add->max;
4066 break;
4067 case LYS_DEV_REPLACE:
4068 d_rpl = calloc(1, sizeof *d_rpl);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004069 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004070 d = (struct lysp_deviate *)d_rpl;
4071 d_units = &d_rpl->units;
4072 d_flags = &d_rpl->flags;
4073 d_min = &d_rpl->min;
4074 d_max = &d_rpl->max;
4075 break;
4076 case LYS_DEV_DELETE:
4077 d_del = calloc(1, sizeof *d_del);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004078 LY_CHECK_ERR_RET(!d_del, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004079 d = (struct lysp_deviate *)d_del;
4080 d_units = &d_del->units;
4081 d_uniques = &d_del->uniques;
4082 d_dflts = &d_del->dflts;
4083 d_musts = &d_del->musts;
4084 d_flags = &d_del->flags;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004085 break;
4086 default:
4087 assert(0);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004088 LOGINT_RET(ctx->ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004089 }
4090 d->mod = dev_mod;
4091
4092 /* insert into siblings */
4093 if (!*deviates) {
4094 *deviates = d;
4095 } else {
4096 for (iter = *deviates; iter->next; iter = iter->next);
4097 iter->next = d;
4098 }
4099
4100 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02004101 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004102
4103 switch (kw) {
4104 case YANG_CONFIG:
4105 switch (dev_mod) {
4106 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004107 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004108 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004109 return LY_EVALID;
4110 default:
4111 ret = parse_config(ctx, data, d_flags, &d->exts);
4112 break;
4113 }
4114 break;
4115 case YANG_DEFAULT:
4116 switch (dev_mod) {
4117 case LYS_DEV_NOT_SUPPORTED:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004118 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004119 return LY_EVALID;
4120 case LYS_DEV_REPLACE:
4121 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &d_rpl->dflt, Y_STR_ARG, &d->exts);
4122 break;
4123 default:
4124 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_DEFAULT, d_dflts, Y_STR_ARG, &d->exts);
4125 break;
4126 }
4127 break;
4128 case YANG_MANDATORY:
4129 switch (dev_mod) {
4130 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004131 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004132 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004133 return LY_EVALID;
4134 default:
4135 ret = parse_mandatory(ctx, data, d_flags, &d->exts);
4136 break;
4137 }
4138 break;
4139 case YANG_MAX_ELEMENTS:
4140 switch (dev_mod) {
4141 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004142 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004143 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004144 return LY_EVALID;
4145 default:
4146 ret = parse_maxelements(ctx, data, d_max, d_flags, &d->exts);
4147 break;
4148 }
4149 break;
4150 case YANG_MIN_ELEMENTS:
4151 switch (dev_mod) {
4152 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004153 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004154 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004155 return LY_EVALID;
4156 default:
4157 ret = parse_minelements(ctx, data, d_min, d_flags, &d->exts);
4158 break;
4159 }
4160 break;
4161 case YANG_MUST:
4162 switch (dev_mod) {
4163 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004164 case LYS_DEV_REPLACE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004165 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004166 return LY_EVALID;
4167 default:
4168 ret = parse_restrs(ctx, data, kw, d_musts);
4169 break;
4170 }
4171 break;
4172 case YANG_TYPE:
4173 switch (dev_mod) {
4174 case LYS_DEV_NOT_SUPPORTED:
4175 case LYS_DEV_ADD:
4176 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004177 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004178 return LY_EVALID;
4179 default:
4180 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004181 LY_CHECK_ERR_RET(!d_rpl->type, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004182 ret = parse_type(ctx, data, d_rpl->type);
4183 break;
4184 }
4185 break;
4186 case YANG_UNIQUE:
4187 switch (dev_mod) {
4188 case LYS_DEV_NOT_SUPPORTED:
4189 case LYS_DEV_REPLACE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004190 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004191 return LY_EVALID;
4192 default:
4193 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_UNIQUE, d_uniques, Y_STR_ARG, &d->exts);
4194 break;
4195 }
4196 break;
4197 case YANG_UNITS:
4198 switch (dev_mod) {
4199 case LYS_DEV_NOT_SUPPORTED:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004200 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004201 return LY_EVALID;
4202 default:
4203 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, d_units, Y_STR_ARG, &d->exts);
4204 break;
4205 }
4206 break;
4207 case YANG_CUSTOM:
4208 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &d->exts);
4209 break;
4210 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004211 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004212 return LY_EVALID;
4213 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004214 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004215 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004216 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004217
4218 return ret;
4219}
4220
Michal Vaskoea5abea2018-09-18 13:10:54 +02004221/**
4222 * @brief Parse the deviation statement.
4223 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004224 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004225 * @param[in,out] data Data to read from, always moved to currently handled character.
4226 * @param[in,out] deviations Deviations to add to.
4227 *
4228 * @return LY_ERR values.
4229 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004230static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02004231parse_deviation(struct ly_parser_ctx *ctx, const char **data, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004232{
4233 LY_ERR ret = 0;
4234 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004235 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004236 enum yang_keyword kw;
4237 struct lysp_deviation *dev;
4238
4239 LYSP_ARRAY_NEW_RET(ctx, deviations, dev, LY_EMEM);
4240
4241 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02004242 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02004243 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004244
Radek Krejci44ceedc2018-10-02 15:54:31 +02004245 INSERT_WORD(ctx, buf, dev->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004246 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02004247 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004248
4249 switch (kw) {
4250 case YANG_DESCRIPTION:
4251 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &dev->dsc, Y_STR_ARG, &dev->exts);
4252 break;
4253 case YANG_DEVIATE:
4254 ret = parse_deviate(ctx, data, &dev->deviates);
4255 break;
4256 case YANG_REFERENCE:
4257 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &dev->ref, Y_STR_ARG, &dev->exts);
4258 break;
4259 case YANG_CUSTOM:
4260 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &dev->exts);
4261 break;
4262 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004263 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004264 return LY_EVALID;
4265 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004266 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004267 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004268 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004269
4270 /* mandatory substatements */
4271 if (!dev->deviates) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004272 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004273 return LY_EVALID;
4274 }
4275
4276 return ret;
4277}
4278
Michal Vaskoea5abea2018-09-18 13:10:54 +02004279/**
4280 * @brief Parse the feature statement.
4281 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004282 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004283 * @param[in,out] data Data to read from, always moved to currently handled character.
4284 * @param[in,out] features Features to add to.
4285 *
4286 * @return LY_ERR values.
4287 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004288static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02004289parse_feature(struct ly_parser_ctx *ctx, const char **data, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004290{
4291 LY_ERR ret = 0;
4292 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004293 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004294 enum yang_keyword kw;
4295 struct lysp_feature *feat;
4296
4297 LYSP_ARRAY_NEW_RET(ctx, features, feat, LY_EMEM);
4298
4299 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02004300 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02004301 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004302
Radek Krejci44ceedc2018-10-02 15:54:31 +02004303 INSERT_WORD(ctx, buf, feat->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004304 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02004305 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004306
4307 switch (kw) {
4308 case YANG_DESCRIPTION:
4309 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &feat->dsc, Y_STR_ARG, &feat->exts);
4310 break;
4311 case YANG_IF_FEATURE:
4312 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &feat->iffeatures, Y_STR_ARG, &feat->exts);
4313 break;
4314 case YANG_REFERENCE:
4315 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &feat->ref, Y_STR_ARG, &feat->exts);
4316 break;
4317 case YANG_STATUS:
4318 ret = parse_status(ctx, data, &feat->flags, &feat->exts);
4319 break;
4320 case YANG_CUSTOM:
4321 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &feat->exts);
4322 break;
4323 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004324 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "feature");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004325 return LY_EMEM;
4326 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004327 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004328 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004329 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004330
4331 return ret;
4332}
4333
Michal Vaskoea5abea2018-09-18 13:10:54 +02004334/**
4335 * @brief Parse the identity statement.
4336 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004337 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004338 * @param[in,out] data Data to read from, always moved to currently handled character.
4339 * @param[in,out] identities Identities to add to.
4340 *
4341 * @return LY_ERR values.
4342 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004343static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02004344parse_identity(struct ly_parser_ctx *ctx, const char **data, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004345{
4346 LY_ERR ret = 0;
4347 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004348 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004349 enum yang_keyword kw;
4350 struct lysp_ident *ident;
4351
4352 LYSP_ARRAY_NEW_RET(ctx, identities, ident, LY_EMEM);
4353
4354 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02004355 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02004356 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004357
Radek Krejci44ceedc2018-10-02 15:54:31 +02004358 INSERT_WORD(ctx, buf, ident->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004359 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02004360 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004361
4362 switch (kw) {
4363 case YANG_DESCRIPTION:
4364 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &ident->dsc, Y_STR_ARG, &ident->exts);
4365 break;
4366 case YANG_IF_FEATURE:
4367 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &ident->iffeatures, Y_STR_ARG, &ident->exts);
4368 break;
4369 case YANG_REFERENCE:
4370 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &ident->ref, Y_STR_ARG, &ident->exts);
4371 break;
4372 case YANG_STATUS:
4373 ret = parse_status(ctx, data, &ident->flags, &ident->exts);
4374 break;
4375 case YANG_BASE:
4376 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_BASE, &ident->bases, Y_PREF_IDENTIF_ARG, &ident->exts);
4377 break;
4378 case YANG_CUSTOM:
4379 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ident->exts);
4380 break;
4381 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004382 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004383 return LY_EVALID;
4384 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004385 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004386 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004387 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004388
4389 return ret;
4390}
4391
Michal Vaskoea5abea2018-09-18 13:10:54 +02004392/**
4393 * @brief Parse the module or submodule statement.
4394 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004395 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004396 * @param[in,out] data Data to read from, always moved to currently handled character.
4397 * @param[in,out] mod Module to write to.
4398 *
4399 * @return LY_ERR values.
4400 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004401static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02004402parse_sub_module(struct ly_parser_ctx *ctx, const char **data, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004403{
4404 LY_ERR ret = 0;
4405 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004406 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004407 enum yang_keyword kw, prev_kw = 0;
4408 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
4409
4410 /* (sub)module name */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02004411 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02004412 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004413
Radek Krejci44ceedc2018-10-02 15:54:31 +02004414 INSERT_WORD(ctx, buf, mod->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004415 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02004416 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004417
Radek Krejcie3846472018-10-15 15:24:51 +02004418#define CHECK_ORDER(SECTION) \
4419 if (mod_stmt > SECTION) {LOGVAL_YANG(ctx, LY_VCODE_INORD, ly_stmt2str(kw), ly_stmt2str(prev_kw)); return LY_EVALID;}mod_stmt = SECTION
4420
Michal Vasko7fbc8162018-09-17 10:35:16 +02004421 switch (kw) {
4422 /* module header */
4423 case YANG_NAMESPACE:
4424 case YANG_PREFIX:
4425 if (mod->submodule) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004426 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004427 return LY_EVALID;
4428 }
Radek Krejcie3846472018-10-15 15:24:51 +02004429 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4430 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004431 case YANG_BELONGS_TO:
Radek Krejcie3846472018-10-15 15:24:51 +02004432 if (!mod->submodule) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004433 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004434 return LY_EVALID;
4435 }
Radek Krejcie3846472018-10-15 15:24:51 +02004436 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4437 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004438 case YANG_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004439 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004440 break;
4441 /* linkage */
4442 case YANG_INCLUDE:
4443 case YANG_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004444 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004445 break;
4446 /* meta */
4447 case YANG_ORGANIZATION:
4448 case YANG_CONTACT:
4449 case YANG_DESCRIPTION:
4450 case YANG_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004451 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004452 break;
4453
4454 /* revision */
4455 case YANG_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004456 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004457 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004458 /* body */
4459 case YANG_ANYDATA:
4460 case YANG_ANYXML:
4461 case YANG_AUGMENT:
4462 case YANG_CHOICE:
4463 case YANG_CONTAINER:
4464 case YANG_DEVIATION:
4465 case YANG_EXTENSION:
4466 case YANG_FEATURE:
4467 case YANG_GROUPING:
4468 case YANG_IDENTITY:
4469 case YANG_LEAF:
4470 case YANG_LEAF_LIST:
4471 case YANG_LIST:
4472 case YANG_NOTIFICATION:
4473 case YANG_RPC:
4474 case YANG_TYPEDEF:
4475 case YANG_USES:
4476 case YANG_CUSTOM:
4477 mod_stmt = Y_MOD_BODY;
4478 break;
4479 default:
4480 /* error handled in the next switch */
4481 break;
4482 }
Radek Krejcie3846472018-10-15 15:24:51 +02004483#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004484
Radek Krejcie3846472018-10-15 15:24:51 +02004485 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004486 switch (kw) {
4487 /* module header */
4488 case YANG_YANG_VERSION:
4489 ret = parse_yangversion(ctx, data, mod);
4490 break;
4491 case YANG_NAMESPACE:
4492 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_NAMESPACE, 0, &mod->ns, Y_STR_ARG, &mod->exts);
4493 break;
4494 case YANG_PREFIX:
4495 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_PREFIX, 0, &mod->prefix, Y_IDENTIF_ARG, &mod->exts);
Radek Krejci70853c52018-10-15 14:46:16 +02004496 LY_CHECK_RET(lysp_check_prefix(ctx, mod, &mod->prefix), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004497 break;
4498 case YANG_BELONGS_TO:
4499 ret = parse_belongsto(ctx, data, &mod->belongsto, &mod->prefix, &mod->exts);
4500 break;
4501
4502 /* linkage */
4503 case YANG_INCLUDE:
4504 ret = parse_include(ctx, data, &mod->includes);
4505 break;
4506 case YANG_IMPORT:
Radek Krejci70853c52018-10-15 14:46:16 +02004507 ret = parse_import(ctx, data, mod);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004508 break;
4509
4510 /* meta */
4511 case YANG_ORGANIZATION:
4512 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_ORGANIZATION, 0, &mod->org, Y_STR_ARG, &mod->exts);
4513 break;
4514 case YANG_CONTACT:
4515 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_CONTACT, 0, &mod->contact, Y_STR_ARG, &mod->exts);
4516 break;
4517 case YANG_DESCRIPTION:
4518 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &mod->dsc, Y_STR_ARG, &mod->exts);
4519 break;
4520 case YANG_REFERENCE:
4521 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &mod->ref, Y_STR_ARG, &mod->exts);
4522 break;
4523
4524 /* revision */
4525 case YANG_REVISION:
4526 ret = parse_revision(ctx, data, &mod->revs);
4527 break;
4528
4529 /* body */
4530 case YANG_ANYDATA:
4531 case YANG_ANYXML:
4532 ret = parse_any(ctx, data, kw, &mod->data);
4533 break;
4534 case YANG_CHOICE:
4535 ret = parse_choice(ctx, data, &mod->data);
4536 break;
4537 case YANG_CONTAINER:
4538 ret = parse_container(ctx, data, &mod->data);
4539 break;
4540 case YANG_LEAF:
4541 ret = parse_leaf(ctx, data, &mod->data);
4542 break;
4543 case YANG_LEAF_LIST:
4544 ret = parse_leaflist(ctx, data, &mod->data);
4545 break;
4546 case YANG_LIST:
4547 ret = parse_list(ctx, data, &mod->data);
4548 break;
4549 case YANG_USES:
4550 ret = parse_uses(ctx, data, &mod->data);
4551 break;
4552
4553 case YANG_AUGMENT:
4554 ret = parse_augment(ctx, data, &mod->augments);
4555 break;
4556 case YANG_DEVIATION:
4557 ret = parse_deviation(ctx, data, &mod->deviations);
4558 break;
4559 case YANG_EXTENSION:
4560 ret = parse_extension(ctx, data, &mod->extensions);
4561 break;
4562 case YANG_FEATURE:
4563 ret = parse_feature(ctx, data, &mod->features);
4564 break;
4565 case YANG_GROUPING:
4566 ret = parse_grouping(ctx, data, &mod->groupings);
4567 break;
4568 case YANG_IDENTITY:
4569 ret = parse_identity(ctx, data, &mod->identities);
4570 break;
4571 case YANG_NOTIFICATION:
4572 ret = parse_notif(ctx, data, &mod->notifs);
4573 break;
4574 case YANG_RPC:
4575 ret = parse_action(ctx, data, &mod->rpcs);
4576 break;
4577 case YANG_TYPEDEF:
4578 ret = parse_typedef(ctx, data, &mod->typedefs);
4579 break;
4580 case YANG_CUSTOM:
4581 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &mod->exts);
4582 break;
4583
4584 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004585 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), mod->submodule ? "submodule" : "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004586 return LY_EVALID;
4587 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004588 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004589 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004590 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004591
4592 /* mandatory substatements */
4593 if (mod->submodule) {
4594 if (!mod->belongsto) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004595 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004596 return LY_EVALID;
4597 }
4598 } else {
4599 if (!mod->ns) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004600 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004601 return LY_EVALID;
4602 } else if (!mod->prefix) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004603 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004604 return LY_EVALID;
4605 }
4606 }
4607
4608 return ret;
4609}
4610
Radek Krejcid4557c62018-09-17 11:42:09 +02004611LY_ERR
Michal Vasko7fbc8162018-09-17 10:35:16 +02004612yang_parse(struct ly_ctx *ctx, const char *data, struct lysp_module **mod_p)
4613{
4614 LY_ERR ret = 0;
4615 char *word, *buf;
Radek Krejciefd22f62018-09-27 11:47:58 +02004616 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004617 enum yang_keyword kw;
Radek Krejci0c2cf322018-10-13 08:02:30 +02004618 struct lysp_module *mod = NULL;
Radek Krejci44ceedc2018-10-02 15:54:31 +02004619 struct ly_parser_ctx context;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004620
Radek Krejci44ceedc2018-10-02 15:54:31 +02004621 context.ctx = ctx;
4622 context.line = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004623
4624 /* "module"/"submodule" */
Radek Krejci44ceedc2018-10-02 15:54:31 +02004625 ret = get_keyword(&context, &data, &kw, &word, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02004626 LY_CHECK_GOTO(ret, error);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004627
4628 if ((kw != YANG_MODULE) && (kw != YANG_SUBMODULE)) {
Radek Krejcifc62d7e2018-10-11 12:56:42 +02004629 LOGVAL_YANG(&context, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".",
Radek Krejcic59bc972018-09-17 16:13:06 +02004630 ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004631 goto error;
4632 }
4633
4634 mod = calloc(1, sizeof *mod);
4635 LY_CHECK_ERR_GOTO(!mod, LOGMEM(ctx), error);
4636 if (kw == YANG_SUBMODULE) {
4637 mod->submodule = 1;
4638 }
Radek Krejci9fcacc12018-10-11 15:59:11 +02004639 mod->ctx = ctx;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004640
4641 /* substatements */
Radek Krejci44ceedc2018-10-02 15:54:31 +02004642 ret = parse_sub_module(&context, &data, mod);
Radek Krejcic59bc972018-09-17 16:13:06 +02004643 LY_CHECK_GOTO(ret, error);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004644
4645 /* read some trailing spaces or new lines */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02004646 ret = get_argument(&context, &data, Y_MAYBE_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02004647 LY_CHECK_GOTO(ret, error);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004648
4649 if (word) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004650 LOGVAL_YANG(&context, LYVE_SYNTAX, "Invalid character sequence \"%.*s\", expected end-of-file.",
Michal Vasko7fbc8162018-09-17 10:35:16 +02004651 word_len, word);
4652 free(buf);
4653 goto error;
4654 }
4655 assert(!buf);
4656
4657 *mod_p = mod;
4658 return ret;
4659
4660error:
Radek Krejci9fcacc12018-10-11 15:59:11 +02004661 lysp_module_free(mod);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004662 return ret;
4663}