blob: 5cf82768a9f7c6acfaa1065d4d033b3cda5f8664 [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 Krejci156ccaf2018-10-15 15:49:17 +0200908 case ':':
909 /* keyword is not actually a keyword, but prefix of an extension.
910 * To avoid repeated check of the prefix syntax, move to the point where the colon was read
911 * and we will be checking the keyword (extension instance) itself */
912 prefix = 1;
913 MOVE_INPUT(ctx, data, 1);
914 goto extension;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200915 case '{':
916 /* allowed only for input and output statements which can be without arguments */
917 if (*kw == YANG_INPUT || *kw == YANG_OUTPUT) {
918 break;
919 }
920 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200921 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200922 MOVE_INPUT(ctx, data, 1);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200923 LOGVAL_YANG(ctx, LY_VCODE_INSTREXP, (int)(*data - word_start), word_start,
924 "a keyword followed by a separator");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200925 return LY_EVALID;
926 }
927 } else {
928 /* still can be an extension */
929 prefix = 0;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200930extension:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200931 while (**data && (**data != ' ') && (**data != '\t') && (**data != '\n') && (**data != '{') && (**data != ';')) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200932 LY_CHECK_ERR_RET(ly_getutf8(data, &c, &len),
933 LOGVAL_YANG(ctx, LY_VCODE_INCHAR, (*data)[-len]), LY_EVALID);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200934 ++ctx->indent;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200935 /* check character validity */
936 LY_CHECK_RET(check_identifierchar(ctx, c, *data - len == word_start ? 1 : 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200937 }
938 if (!**data) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200939 LOGVAL_YANG(ctx, LY_VCODE_EOF);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200940 return LY_EVALID;
941 }
942
943 /* prefix is mandatory for extension instances */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200944 if (prefix != 2) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200945 LOGVAL_YANG(ctx, LY_VCODE_INSTREXP, (int)(*data - word_start), word_start, "a keyword");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200946 return LY_EVALID;
947 }
948
949 *kw = YANG_CUSTOM;
950 }
Radek Krejci626df482018-10-11 15:06:31 +0200951success:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200952 if (word_p) {
953 *word_p = (char *)word_start;
954 *word_len = *data - word_start;
955 }
956
957 return LY_SUCCESS;
958}
959
Michal Vaskoea5abea2018-09-18 13:10:54 +0200960/**
961 * @brief Parse extension instance substatements.
962 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200963 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200964 * @param[in,out] data Data to read from, always moved to currently handled character.
965 * @param[in] word Extension instance substatement name (keyword).
966 * @param[in] word_len Extension instance substatement name length.
967 * @param[in,out] child Children of this extension instance to add to.
968 *
969 * @return LY_ERR values.
970 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200971static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +0200972parse_ext_substmt(struct ly_parser_ctx *ctx, const char **data, char *word, size_t word_len,
Michal Vasko7fbc8162018-09-17 10:35:16 +0200973 struct lysp_stmt **child)
974{
975 char *buf;
976 LY_ERR ret = 0;
977 enum yang_keyword kw;
978 struct lysp_stmt *stmt, *par_child;
979
980 stmt = calloc(1, sizeof *stmt);
981 LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM);
982
Radek Krejci44ceedc2018-10-02 15:54:31 +0200983 stmt->stmt = lydict_insert(ctx->ctx, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200984
985 /* get optional argument */
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200986 ret = get_argument(ctx, data, Y_MAYBE_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +0200987 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200988
Radek Krejci0ae092d2018-09-20 16:43:19 +0200989 if (word) {
990 if (buf) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200991 stmt->arg = lydict_insert_zc(ctx->ctx, word);
Radek Krejci0ae092d2018-09-20 16:43:19 +0200992 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200993 stmt->arg = lydict_insert(ctx->ctx, word, word_len);
Radek Krejci0ae092d2018-09-20 16:43:19 +0200994 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200995 }
996
997 /* insert into parent statements */
998 if (!*child) {
999 *child = stmt;
1000 } else {
1001 for (par_child = *child; par_child->next; par_child = par_child->next);
1002 par_child->next = stmt;
1003 }
1004
1005 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001006 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001007
1008 ret = parse_ext_substmt(ctx, data, word, word_len, &stmt->child);
Radek Krejcic59bc972018-09-17 16:13:06 +02001009 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001010 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001011 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001012
1013 return ret;
1014}
1015
Michal Vaskoea5abea2018-09-18 13:10:54 +02001016/**
1017 * @brief Parse extension instance.
1018 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001019 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001020 * @param[in,out] data Data to read from, always moved to currently handled character.
1021 * @param[in] ext_name Extension instance substatement name (keyword).
1022 * @param[in] ext_name_len Extension instance substatement name length.
1023 * @param[in] insubstmt Type of the keyword this extension instance is a substatement of.
1024 * @param[in] insubstmt_index Index of the keyword instance this extension instance is a substatement of.
1025 * @param[in,out] exts Extension instances to add to.
1026 *
1027 * @return LY_ERR values.
1028 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001029static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001030parse_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 +02001031 uint32_t insubstmt_index, struct lysp_ext_instance **exts)
1032{
1033 LY_ERR ret = 0;
1034 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001035 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001036 struct lysp_ext_instance *e;
1037 enum yang_keyword kw;
1038
1039 LYSP_ARRAY_NEW_RET(ctx, exts, e, LY_EMEM);
1040
1041 /* store name and insubstmt info */
Radek Krejci44ceedc2018-10-02 15:54:31 +02001042 e->name = lydict_insert(ctx->ctx, ext_name, ext_name_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001043 e->insubstmt = insubstmt;
1044 e->insubstmt_index = insubstmt_index;
1045
1046 /* get optional argument */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001047 ret = get_argument(ctx, data, Y_MAYBE_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001048 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001049
Radek Krejci0ae092d2018-09-20 16:43:19 +02001050 if (word) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001051 INSERT_WORD(ctx, buf, e->argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001052 }
1053
1054 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001055 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001056
1057 ret = parse_ext_substmt(ctx, data, word, word_len, &e->child);
Radek Krejcic59bc972018-09-17 16:13:06 +02001058 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001059 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001060 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001061
1062 return ret;
1063}
1064
Michal Vaskoea5abea2018-09-18 13:10:54 +02001065/**
1066 * @brief Parse a generic text field without specific constraints. Those are contact, organization,
1067 * description, etc...
1068 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001069 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001070 * @param[in,out] data Data to read from, always moved to currently handled character.
1071 * @param[in] substmt Type of this substatement.
1072 * @param[in] substmt_index Index of this substatement.
1073 * @param[in,out] value Place to store the parsed value.
1074 * @param[in] arg Type of the YANG keyword argument (of the value).
1075 * @param[in,out] exts Extension instances to add to.
1076 *
1077 * @return LY_ERR values.
1078 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001079static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001080parse_text_field(struct ly_parser_ctx *ctx, const char **data, LYEXT_SUBSTMT substmt, uint32_t substmt_index,
Michal Vasko7fbc8162018-09-17 10:35:16 +02001081 const char **value, enum yang_arg arg, struct lysp_ext_instance **exts)
1082{
1083 LY_ERR ret = 0;
1084 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001085 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001086 enum yang_keyword kw;
1087
1088 if (*value) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001089 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001090 return LY_EVALID;
1091 }
1092
1093 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001094 ret = get_argument(ctx, data, arg, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001095 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001096
1097 /* store value and spend buf if allocated */
Radek Krejci44ceedc2018-10-02 15:54:31 +02001098 INSERT_WORD(ctx, buf, *value, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001099
1100 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001101 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001102
1103 switch (kw) {
1104 case YANG_CUSTOM:
1105 ret = parse_ext(ctx, data, word, word_len, substmt, substmt_index, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02001106 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001107 break;
1108 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001109 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001110 return LY_EVALID;
1111 }
1112 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001113 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001114
1115 return ret;
1116}
1117
Michal Vaskoea5abea2018-09-18 13:10:54 +02001118/**
1119 * @brief Parse the yang-version statement.
1120 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001121 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001122 * @param[in,out] data Data to read from, always moved to currently handled character.
1123 * @param[in] mod Module to store the parsed information in.
1124 *
1125 * @return LY_ERR values.
1126 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001127static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001128parse_yangversion(struct ly_parser_ctx *ctx, const char **data, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001129{
1130 LY_ERR ret = 0;
1131 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001132 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001133 enum yang_keyword kw;
1134
1135 if (mod->version) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001136 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001137 return LY_EVALID;
1138 }
1139
1140 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001141 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001142 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001143
1144 if ((word_len == 3) && !strncmp(word, "1.0", word_len)) {
1145 mod->version = LYS_VERSION_1_0;
1146 } else if ((word_len == 3) && !strncmp(word, "1.1", word_len)) {
1147 mod->version = LYS_VERSION_1_1;
1148 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001149 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001150 free(buf);
1151 return LY_EVALID;
1152 }
1153 free(buf);
1154
1155 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001156 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001157
1158 switch (kw) {
1159 case YANG_CUSTOM:
1160 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_VERSION, 0, &mod->exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02001161 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001162 break;
1163 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001164 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001165 return LY_EVALID;
1166 }
1167 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001168 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001169
1170 return ret;
1171}
1172
Michal Vaskoea5abea2018-09-18 13:10:54 +02001173/**
1174 * @brief Parse the belongs-to statement.
1175 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001176 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001177 * @param[in,out] data Data to read from, always moved to currently handled character.
1178 * @param[in,out] belongsto Place to store the parsed value.
1179 * @param[in,out] prefix Place to store the parsed belongs-to prefix value.
1180 * @param[in,out] exts Extension instances to add to.
1181 *
1182 * @return LY_ERR values.
1183 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001184static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001185parse_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 +02001186{
1187 LY_ERR ret = 0;
1188 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001189 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001190 enum yang_keyword kw;
1191
1192 if (*belongsto) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001193 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001194 return LY_EVALID;
1195 }
1196
1197 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001198 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001199 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001200
Radek Krejci44ceedc2018-10-02 15:54:31 +02001201 INSERT_WORD(ctx, buf, *belongsto, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001202 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001203 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001204
1205 switch (kw) {
1206 case YANG_PREFIX:
1207 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_PREFIX, 0, prefix, Y_IDENTIF_ARG, exts);
1208 break;
1209 case YANG_CUSTOM:
1210 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_BELONGSTO, 0, exts);
1211 break;
1212 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001213 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001214 return LY_EVALID;
1215 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001216 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001217 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001218 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001219
1220 /* mandatory substatements */
1221 if (!*prefix) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001222 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001223 return LY_EVALID;
1224 }
1225
1226 return ret;
1227}
1228
Michal Vaskoea5abea2018-09-18 13:10:54 +02001229/**
1230 * @brief Parse the revision-date statement.
1231 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001232 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001233 * @param[in,out] data Data to read from, always moved to currently handled character.
1234 * @param[in,out] rev Array to store the parsed value in.
1235 * @param[in,out] exts Extension instances to add to.
1236 *
1237 * @return LY_ERR values.
1238 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001239static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001240parse_revisiondate(struct ly_parser_ctx *ctx, const char **data, char *rev, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001241{
1242 LY_ERR ret = 0;
1243 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001244 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001245 enum yang_keyword kw;
1246
1247 if (rev[0]) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001248 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001249 return LY_EVALID;
1250 }
1251
1252 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001253 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001254 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001255
1256 /* check value */
Radek Krejci44ceedc2018-10-02 15:54:31 +02001257 if (lysp_check_date(ctx->ctx, word, word_len, "revision-date")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001258 free(buf);
1259 return LY_EVALID;
1260 }
1261
1262 /* store value and spend buf if allocated */
1263 strncpy(rev, word, word_len);
1264 free(buf);
1265
1266 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001267 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001268
1269 switch (kw) {
1270 case YANG_CUSTOM:
1271 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_REVISIONDATE, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02001272 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001273 break;
1274 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001275 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001276 return LY_EVALID;
1277 }
1278 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001279 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001280
1281 return ret;
1282}
1283
Michal Vaskoea5abea2018-09-18 13:10:54 +02001284/**
1285 * @brief Parse the include statement.
1286 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001287 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001288 * @param[in,out] data Data to read from, always moved to currently handled character.
1289 * @param[in,out] includes Parsed includes to add to.
1290 *
1291 * @return LY_ERR values.
1292 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001293static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001294parse_include(struct ly_parser_ctx *ctx, const char **data, struct lysp_include **includes)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001295{
1296 LY_ERR ret = 0;
1297 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001298 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001299 enum yang_keyword kw;
1300 struct lysp_include *inc;
1301
1302 LYSP_ARRAY_NEW_RET(ctx, includes, inc, LY_EMEM);
1303
1304 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001305 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001306 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001307
Radek Krejci44ceedc2018-10-02 15:54:31 +02001308 INSERT_WORD(ctx, buf, inc->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001309 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001310 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001311
1312 switch (kw) {
1313 case YANG_DESCRIPTION:
1314 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &inc->dsc, Y_STR_ARG, &inc->exts);
1315 break;
1316 case YANG_REFERENCE:
1317 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &inc->ref, Y_STR_ARG, &inc->exts);
1318 break;
1319 case YANG_REVISION_DATE:
1320 ret = parse_revisiondate(ctx, data, inc->rev, &inc->exts);
1321 break;
1322 case YANG_CUSTOM:
1323 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inc->exts);
1324 break;
1325 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001326 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001327 return LY_EVALID;
1328 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001329 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001330 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001331 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001332
1333 return ret;
1334}
1335
Michal Vaskoea5abea2018-09-18 13:10:54 +02001336/**
1337 * @brief Parse the import statement.
1338 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001339 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001340 * @param[in,out] data Data to read from, always moved to currently handled character.
1341 * @param[in,out] imports Parsed imports to add to.
1342 *
1343 * @return LY_ERR values.
1344 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001345static LY_ERR
Radek Krejci70853c52018-10-15 14:46:16 +02001346parse_import(struct ly_parser_ctx *ctx, const char **data, struct lysp_module *module)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001347{
1348 LY_ERR ret = 0;
1349 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001350 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001351 enum yang_keyword kw;
1352 struct lysp_import *imp;
1353
Radek Krejci70853c52018-10-15 14:46:16 +02001354 LYSP_ARRAY_NEW_RET(ctx, &module->imports, imp, LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001355
1356 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001357 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001358 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001359
Radek Krejci44ceedc2018-10-02 15:54:31 +02001360 INSERT_WORD(ctx, buf, imp->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001361 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001362 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001363
1364 switch (kw) {
1365 case YANG_PREFIX:
1366 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_PREFIX, 0, &imp->prefix, Y_IDENTIF_ARG, &imp->exts);
Radek Krejci70853c52018-10-15 14:46:16 +02001367 LY_CHECK_RET(lysp_check_prefix(ctx, module, &imp->prefix), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001368 break;
1369 case YANG_DESCRIPTION:
1370 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &imp->dsc, Y_STR_ARG, &imp->exts);
1371 break;
1372 case YANG_REFERENCE:
1373 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &imp->ref, Y_STR_ARG, &imp->exts);
1374 break;
1375 case YANG_REVISION_DATE:
1376 ret = parse_revisiondate(ctx, data, imp->rev, &imp->exts);
1377 break;
1378 case YANG_CUSTOM:
1379 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &imp->exts);
1380 break;
1381 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001382 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001383 return LY_EVALID;
1384 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001385 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001386 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001387 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001388
1389 /* mandatory substatements */
1390 if (!imp->prefix) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001391 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "prefix", "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001392 return LY_EVALID;
1393 }
1394
1395 return ret;
1396}
1397
Michal Vaskoea5abea2018-09-18 13:10:54 +02001398/**
1399 * @brief Parse the revision statement.
1400 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001401 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001402 * @param[in,out] data Data to read from, always moved to currently handled character.
1403 * @param[in,out] revs Parsed revisions to add to.
1404 *
1405 * @return LY_ERR values.
1406 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001407static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001408parse_revision(struct ly_parser_ctx *ctx, const char **data, struct lysp_revision **revs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001409{
1410 LY_ERR ret = 0;
1411 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001412 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001413 enum yang_keyword kw;
1414 struct lysp_revision *rev;
1415
1416 LYSP_ARRAY_NEW_RET(ctx, revs, rev, LY_EMEM);
1417
1418 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001419 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001420 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001421
1422 /* check value */
Radek Krejci44ceedc2018-10-02 15:54:31 +02001423 if (lysp_check_date(ctx->ctx, word, word_len, "revision")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001424 return LY_EVALID;
1425 }
1426
1427 strncpy(rev->rev, word, word_len);
1428 free(buf);
1429
1430 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001431 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001432
1433 switch (kw) {
1434 case YANG_DESCRIPTION:
1435 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &rev->dsc, Y_STR_ARG, &rev->exts);
1436 break;
1437 case YANG_REFERENCE:
1438 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &rev->ref, Y_STR_ARG, &rev->exts);
1439 break;
1440 case YANG_CUSTOM:
1441 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rev->exts);
1442 break;
1443 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001444 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001445 return LY_EVALID;
1446 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001447 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001448 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001449 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001450
1451 return ret;
1452}
1453
Michal Vaskoea5abea2018-09-18 13:10:54 +02001454/**
1455 * @brief Parse a generic text field that can have more instances such as base.
1456 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001457 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001458 * @param[in,out] data Data to read from, always moved to currently handled character.
1459 * @param[in] substmt Type of this substatement.
1460 * @param[in,out] texts Parsed values to add to.
1461 * @param[in] arg Type of the expected argument.
1462 * @param[in,out] exts Extension instances to add to.
1463 *
1464 * @return LY_ERR values.
1465 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001466static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001467parse_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 +02001468 struct lysp_ext_instance **exts)
1469{
1470 LY_ERR ret = 0;
1471 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001472 size_t count, word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001473 enum yang_keyword kw;
1474
1475 /* allocate new pointer */
1476 for (count = 1; (*texts) && (*texts)[count - 1]; ++count);
Radek Krejcie7f84ae2018-10-15 16:42:29 +02001477 *texts = realloc(*texts, (1 + count) * sizeof **texts);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001478 LY_CHECK_ERR_RET(!*texts, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001479
1480 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001481 ret = get_argument(ctx, data, arg, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001482 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001483
Radek Krejci44ceedc2018-10-02 15:54:31 +02001484 INSERT_WORD(ctx, buf, (*texts)[count - 1], word, word_len);
Radek Krejcie7f84ae2018-10-15 16:42:29 +02001485 (*texts)[count] = NULL; /* NULL-termination of the array */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001486 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001487 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001488
1489 switch (kw) {
1490 case YANG_CUSTOM:
1491 ret = parse_ext(ctx, data, word, word_len, substmt, count - 1, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02001492 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001493 break;
1494 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001495 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001496 return LY_EVALID;
1497 }
1498 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001499 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001500
1501 return ret;
1502}
1503
Michal Vaskoea5abea2018-09-18 13:10:54 +02001504/**
1505 * @brief Parse the config statement.
1506 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001507 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001508 * @param[in,out] data Data to read from, always moved to currently handled character.
1509 * @param[in,out] flags Flags to add to.
1510 * @param[in,out] exts Extension instances to add to.
1511 *
1512 * @return LY_ERR values.
1513 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001514static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001515parse_config(struct ly_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001516{
1517 LY_ERR ret = 0;
1518 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001519 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001520 enum yang_keyword kw;
1521
1522 if (*flags & LYS_CONFIG_MASK) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001523 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001524 return LY_EVALID;
1525 }
1526
1527 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001528 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001529 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001530
1531 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1532 *flags |= LYS_CONFIG_W;
1533 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1534 *flags |= LYS_CONFIG_R;
1535 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001536 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001537 free(buf);
1538 return LY_EVALID;
1539 }
1540 free(buf);
1541
1542 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001543 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001544
1545 switch (kw) {
1546 case YANG_CUSTOM:
1547 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_CONFIG, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02001548 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001549 break;
1550 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001551 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001552 return LY_EVALID;
1553 }
1554 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001555 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001556
1557 return ret;
1558}
1559
Michal Vaskoea5abea2018-09-18 13:10:54 +02001560/**
1561 * @brief Parse the mandatory statement.
1562 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001563 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001564 * @param[in,out] data Data to read from, always moved to currently handled character.
1565 * @param[in,out] flags Flags to add to.
1566 * @param[in,out] exts Extension instances to add to.
1567 *
1568 * @return LY_ERR values.
1569 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001570static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001571parse_mandatory(struct ly_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001572{
1573 LY_ERR ret = 0;
1574 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001575 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001576 enum yang_keyword kw;
1577
1578 if (*flags & LYS_MAND_MASK) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001579 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001580 return LY_EVALID;
1581 }
1582
1583 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001584 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001585 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001586
1587 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1588 *flags |= LYS_MAND_TRUE;
1589 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1590 *flags |= LYS_MAND_FALSE;
1591 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001592 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001593 free(buf);
1594 return LY_EVALID;
1595 }
1596 free(buf);
1597
1598 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001599 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001600
1601 switch (kw) {
1602 case YANG_CUSTOM:
1603 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MANDATORY, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02001604 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001605 break;
1606 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001607 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001608 return LY_EVALID;
1609 }
1610 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001611 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001612
1613 return ret;
1614}
1615
Michal Vaskoea5abea2018-09-18 13:10:54 +02001616/**
1617 * @brief Parse a restriction such as range or length.
1618 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001619 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001620 * @param[in,out] data Data to read from, always moved to currently handled character.
1621 * @param[in] restr_kw Type of this particular restriction.
1622 * @param[in,out] exts Extension instances to add to.
1623 *
1624 * @return LY_ERR values.
1625 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001626static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001627parse_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 +02001628{
1629 LY_ERR ret = 0;
1630 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001631 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001632 enum yang_keyword kw;
1633
1634 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001635 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001636 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001637
Radek Krejci44ceedc2018-10-02 15:54:31 +02001638 INSERT_WORD(ctx, buf, restr->arg, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001639 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001640 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001641
1642 switch (kw) {
1643 case YANG_DESCRIPTION:
1644 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts);
1645 break;
1646 case YANG_REFERENCE:
1647 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts);
1648 break;
1649 case YANG_ERROR_APP_TAG:
1650 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRTAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts);
1651 break;
1652 case YANG_ERROR_MESSAGE:
1653 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRMSG, 0, &restr->emsg, Y_STR_ARG, &restr->exts);
1654 break;
1655 case YANG_CUSTOM:
1656 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts);
1657 break;
1658 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001659 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001660 return LY_EVALID;
1661 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001662 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001663 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001664 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001665
1666 return ret;
1667}
1668
Michal Vaskoea5abea2018-09-18 13:10:54 +02001669/**
1670 * @brief Parse a restriction that can have more instances such as must.
1671 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001672 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001673 * @param[in,out] data Data to read from, always moved to currently handled character.
1674 * @param[in] restr_kw Type of this particular restriction.
1675 * @param[in,out] restrs Restrictions to add to.
1676 *
1677 * @return LY_ERR values.
1678 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001679static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001680parse_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 +02001681{
1682 struct lysp_restr *restr;
1683
1684 LYSP_ARRAY_NEW_RET(ctx, restrs, restr, LY_EMEM);
1685
1686 return parse_restr(ctx, data, restr_kw, restr);
1687}
1688
Michal Vaskoea5abea2018-09-18 13:10:54 +02001689/**
1690 * @brief Parse the status statement.
1691 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001692 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001693 * @param[in,out] data Data to read from, always moved to currently handled character.
1694 * @param[in,out] flags Flags to add to.
1695 * @param[in,out] exts Extension instances to add to.
1696 *
1697 * @return LY_ERR values.
1698 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001699static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001700parse_status(struct ly_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001701{
1702 LY_ERR ret = 0;
1703 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001704 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001705 enum yang_keyword kw;
1706
1707 if (*flags & LYS_STATUS_MASK) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001708 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001709 return LY_EVALID;
1710 }
1711
1712 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001713 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001714 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001715
1716 if ((word_len == 7) && !strncmp(word, "current", word_len)) {
1717 *flags |= LYS_STATUS_CURR;
1718 } else if ((word_len == 10) && !strncmp(word, "deprecated", word_len)) {
1719 *flags |= LYS_STATUS_DEPRC;
1720 } else if ((word_len == 8) && !strncmp(word, "obsolete", word_len)) {
1721 *flags |= LYS_STATUS_OBSLT;
1722 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001723 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001724 free(buf);
1725 return LY_EVALID;
1726 }
1727 free(buf);
1728
1729 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001730 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001731
1732 switch (kw) {
1733 case YANG_CUSTOM:
1734 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_STATUS, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02001735 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001736 break;
1737 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001738 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001739 return LY_EVALID;
1740 }
1741 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001742 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001743
1744 return ret;
1745}
1746
Michal Vaskoea5abea2018-09-18 13:10:54 +02001747/**
1748 * @brief Parse the when statement.
1749 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001750 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001751 * @param[in,out] data Data to read from, always moved to currently handled character.
1752 * @param[in,out] when_p When pointer to parse to.
1753 *
1754 * @return LY_ERR values.
1755 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001756static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001757parse_when(struct ly_parser_ctx *ctx, const char **data, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001758{
1759 LY_ERR ret = 0;
1760 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001761 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001762 enum yang_keyword kw;
1763 struct lysp_when *when;
1764
1765 if (*when_p) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001766 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001767 return LY_EVALID;
1768 }
1769
1770 when = calloc(1, sizeof *when);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001771 LY_CHECK_ERR_RET(!when, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001772 *when_p = when;
1773
1774 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001775 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001776 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001777
Radek Krejci44ceedc2018-10-02 15:54:31 +02001778 INSERT_WORD(ctx, buf, when->cond, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001779 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001780 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001781
1782 switch (kw) {
1783 case YANG_DESCRIPTION:
1784 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &when->dsc, Y_STR_ARG, &when->exts);
1785 break;
1786 case YANG_REFERENCE:
1787 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &when->ref, Y_STR_ARG, &when->exts);
1788 break;
1789 case YANG_CUSTOM:
1790 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &when->exts);
1791 break;
1792 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001793 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001794 return LY_EVALID;
1795 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001796 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001797 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001798 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001799
1800 return ret;
1801}
1802
Michal Vaskoea5abea2018-09-18 13:10:54 +02001803/**
1804 * @brief Parse the anydata or anyxml statement.
1805 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001806 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001807 * @param[in,out] data Data to read from, always moved to currently handled character.
1808 * @param[in] kw Type of this particular keyword.
1809 * @param[in,out] siblings Siblings to add to.
1810 *
1811 * @return LY_ERR values.
1812 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001813static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001814parse_any(struct ly_parser_ctx *ctx, const char **data, enum yang_keyword kw, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001815{
1816 LY_ERR ret = 0;
1817 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001818 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001819 struct lysp_node *iter;
1820 struct lysp_node_anydata *any;
1821
1822 /* create structure */
1823 any = calloc(1, sizeof *any);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001824 LY_CHECK_ERR_RET(!any, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001825 any->nodetype = kw == YANG_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
1826
1827 /* insert into siblings */
1828 if (!*siblings) {
1829 *siblings = (struct lysp_node *)any;
1830 } else {
1831 for (iter = *siblings; iter->next; iter = iter->next);
1832 iter->next = (struct lysp_node *)any;
1833 }
1834
1835 /* get name */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001836 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001837 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001838
Radek Krejci44ceedc2018-10-02 15:54:31 +02001839 INSERT_WORD(ctx, buf, any->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001840
1841 /* parse substatements */
1842 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001843 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001844
1845 switch (kw) {
1846 case YANG_CONFIG:
1847 ret = parse_config(ctx, data, &any->flags, &any->exts);
1848 break;
1849 case YANG_DESCRIPTION:
1850 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &any->dsc, Y_STR_ARG, &any->exts);
1851 break;
1852 case YANG_IF_FEATURE:
1853 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &any->iffeatures, Y_STR_ARG, &any->exts);
1854 break;
1855 case YANG_MANDATORY:
1856 ret = parse_mandatory(ctx, data, &any->flags, &any->exts);
1857 break;
1858 case YANG_MUST:
1859 ret = parse_restrs(ctx, data, kw, &any->musts);
1860 break;
1861 case YANG_REFERENCE:
1862 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &any->ref, Y_STR_ARG, &any->exts);
1863 break;
1864 case YANG_STATUS:
1865 ret = parse_status(ctx, data, &any->flags, &any->exts);
1866 break;
1867 case YANG_WHEN:
1868 ret = parse_when(ctx, data, &any->when);
1869 break;
1870 case YANG_CUSTOM:
1871 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &any->exts);
1872 break;
1873 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001874 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
Radek Krejcic59bc972018-09-17 16:13:06 +02001875 (any->nodetype & LYS_ANYDATA) == LYS_ANYDATA ? ly_stmt2str(YANG_ANYDATA) : ly_stmt2str(YANG_ANYXML));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001876 return LY_EVALID;
1877 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001878 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001879 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001880 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001881
1882 return ret;
1883}
1884
Michal Vaskoea5abea2018-09-18 13:10:54 +02001885/**
1886 * @brief Parse the value or position statement. Substatement of type enum statement.
1887 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001888 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001889 * @param[in,out] data Data to read from, always moved to currently handled character.
1890 * @param[in] val_kw Type of this particular keyword.
1891 * @param[in,out] value Value to write to.
1892 * @param[in,out] flags Flags to write to.
1893 * @param[in,out] exts Extension instances to add to.
1894 *
1895 * @return LY_ERR values.
1896 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001897static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001898parse_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 +02001899 struct lysp_ext_instance **exts)
1900{
1901 LY_ERR ret = 0;
1902 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001903 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001904 long int num;
1905 unsigned long int unum;
1906 enum yang_keyword kw;
1907
1908 if (*flags & LYS_SET_VALUE) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001909 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001910 return LY_EVALID;
1911 }
1912 *flags |= LYS_SET_VALUE;
1913
1914 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001915 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001916 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001917
1918 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 +02001919 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001920 free(buf);
1921 return LY_EVALID;
1922 }
1923
1924 errno = 0;
1925 if (val_kw == YANG_VALUE) {
1926 num = strtol(word, &ptr, 10);
1927 } else {
1928 unum = strtoul(word, &ptr, 10);
1929 }
1930 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001931 if ((size_t)(ptr - word) != word_len) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001932 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001933 free(buf);
1934 return LY_EVALID;
1935 }
1936 if (errno == ERANGE) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001937 LOGVAL_YANG(ctx, LY_VCODE_OOB, word_len, word, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001938 free(buf);
1939 return LY_EVALID;
1940 }
1941 if (val_kw == YANG_VALUE) {
1942 *value = num;
1943 } else {
1944 *value = unum;
1945 }
1946 free(buf);
1947
1948 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001949 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001950
1951 switch (kw) {
1952 case YANG_CUSTOM:
1953 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 +02001954 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001955 break;
1956 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001957 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001958 return LY_EVALID;
1959 }
1960 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001961 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001962
1963 return ret;
1964}
1965
Michal Vaskoea5abea2018-09-18 13:10:54 +02001966/**
1967 * @brief Parse the enum or bit statement. Substatement of type statement.
1968 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001969 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001970 * @param[in,out] data Data to read from, always moved to currently handled character.
1971 * @param[in] enum_kw Type of this particular keyword.
1972 * @param[in,out] enums Enums or bits to add to.
1973 *
1974 * @return LY_ERR values.
1975 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001976static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001977parse_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 +02001978{
1979 LY_ERR ret = 0;
1980 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001981 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001982 enum yang_keyword kw;
1983 struct lysp_type_enum *enm;
1984
1985 LYSP_ARRAY_NEW_RET(ctx, enums, enm, LY_EMEM);
1986
1987 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001988 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001989 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001990
Radek Krejci44ceedc2018-10-02 15:54:31 +02001991 INSERT_WORD(ctx, buf, enm->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001992 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001993 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001994
1995 switch (kw) {
1996 case YANG_DESCRIPTION:
1997 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &enm->dsc, Y_STR_ARG, &enm->exts);
1998 break;
1999 case YANG_IF_FEATURE:
2000 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &enm->iffeatures, Y_STR_ARG, &enm->exts);
2001 break;
2002 case YANG_REFERENCE:
2003 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &enm->ref, Y_STR_ARG, &enm->exts);
2004 break;
2005 case YANG_STATUS:
2006 ret = parse_status(ctx, data, &enm->flags, &enm->exts);
2007 break;
2008 case YANG_VALUE:
2009 case YANG_POSITION:
2010 ret = parse_type_enum_value_pos(ctx, data, kw, &enm->value, &enm->flags, &enm->exts);
2011 break;
2012 case YANG_CUSTOM:
2013 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &enm->exts);
2014 break;
2015 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002016 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002017 return LY_EVALID;
2018 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002019 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002020 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002021 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002022
2023 return ret;
2024}
2025
Michal Vaskoea5abea2018-09-18 13:10:54 +02002026/**
2027 * @brief Parse the fraction-digits statement. Substatement of type statement.
2028 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002029 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002030 * @param[in,out] data Data to read from, always moved to currently handled character.
2031 * @param[in,out] fracdig Value to write to.
2032 * @param[in,out] exts Extension instances to add to.
2033 *
2034 * @return LY_ERR values.
2035 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002036static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002037parse_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 +02002038{
2039 LY_ERR ret = 0;
2040 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002041 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002042 unsigned long int num;
2043 enum yang_keyword kw;
2044
2045 if (*fracdig) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002046 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002047 return LY_EVALID;
2048 }
2049
2050 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002051 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002052 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002053
2054 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
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
2060 errno = 0;
2061 num = strtoul(word, &ptr, 10);
2062 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002063 if ((size_t)(ptr - word) != word_len) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002064 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002065 free(buf);
2066 return LY_EVALID;
2067 }
2068 if ((errno == ERANGE) || (num > 18)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002069 LOGVAL_YANG(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002070 free(buf);
2071 return LY_EVALID;
2072 }
2073 *fracdig = num;
2074 free(buf);
2075
2076 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002077 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002078
2079 switch (kw) {
2080 case YANG_CUSTOM:
2081 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_FRACDIGITS, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02002082 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002083 break;
2084 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002085 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002086 return LY_EVALID;
2087 }
2088 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002089 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002090
2091 return ret;
2092}
2093
Michal Vaskoea5abea2018-09-18 13:10:54 +02002094/**
2095 * @brief Parse the require-instance statement. Substatement of type statement.
2096 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002097 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002098 * @param[in,out] data Data to read from, always moved to currently handled character.
2099 * @param[in,out] reqinst Value to write to.
2100 * @param[in,out] flags Flags to write to.
2101 * @param[in,out] exts Extension instances to add to.
2102 *
2103 * @return LY_ERR values.
2104 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002105static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002106parse_type_reqinstance(struct ly_parser_ctx *ctx, const char **data, uint8_t *reqinst, uint16_t *flags,
Michal Vasko7fbc8162018-09-17 10:35:16 +02002107 struct lysp_ext_instance **exts)
2108{
2109 LY_ERR ret = 0;
2110 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002111 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002112 enum yang_keyword kw;
2113
2114 if (*flags & LYS_SET_REQINST) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002115 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002116 return LY_EVALID;
2117 }
2118 *flags |= LYS_SET_REQINST;
2119
2120 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002121 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002122 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002123
2124 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
2125 *reqinst = 1;
2126 } else if ((word_len != 5) || strncmp(word, "false", word_len)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002127 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002128 free(buf);
2129 return LY_EVALID;
2130 }
2131 free(buf);
2132
2133 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002134 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002135
2136 switch (kw) {
2137 case YANG_CUSTOM:
2138 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_REQINSTANCE, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02002139 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002140 break;
2141 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002142 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002143 return LY_EVALID;
2144 }
2145 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002146 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002147
2148 return ret;
2149}
2150
Michal Vaskoea5abea2018-09-18 13:10:54 +02002151/**
2152 * @brief Parse the modifier statement. Substatement of type pattern statement.
2153 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002154 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002155 * @param[in,out] data Data to read from, always moved to currently handled character.
2156 * @param[in,out] pat Value to write to.
2157 * @param[in,out] exts Extension instances to add to.
2158 *
2159 * @return LY_ERR values.
2160 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002161static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002162parse_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 +02002163{
2164 LY_ERR ret = 0;
2165 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002166 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002167 enum yang_keyword kw;
2168
2169 if ((*pat)[0] == 0x15) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002170 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002171 return LY_EVALID;
2172 }
2173
2174 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002175 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002176 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002177
2178 if ((word_len != 12) || strncmp(word, "invert-match", word_len)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002179 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002180 free(buf);
2181 return LY_EVALID;
2182 }
2183 free(buf);
2184
2185 /* replace the value in the dictionary */
2186 buf = malloc(strlen(*pat) + 1);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002187 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002188 strcpy(buf, *pat);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002189 lydict_remove(ctx->ctx, *pat);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002190
2191 assert(buf[0] == 0x06);
2192 buf[0] = 0x15;
Radek Krejci44ceedc2018-10-02 15:54:31 +02002193 *pat = lydict_insert_zc(ctx->ctx, buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002194
2195 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002196 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002197
2198 switch (kw) {
2199 case YANG_CUSTOM:
2200 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MODIFIER, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02002201 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002202 break;
2203 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002204 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002205 return LY_EVALID;
2206 }
2207 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002208 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002209
2210 return ret;
2211}
2212
Michal Vaskoea5abea2018-09-18 13:10:54 +02002213/**
2214 * @brief Parse the pattern statement. Substatement of type statement.
2215 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002216 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002217 * @param[in,out] data Data to read from, always moved to currently handled character.
2218 * @param[in,out] patterns Restrictions to add to.
2219 *
2220 * @return LY_ERR values.
2221 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002222static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002223parse_type_pattern(struct ly_parser_ctx *ctx, const char **data, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002224{
2225 LY_ERR ret = 0;
2226 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002227 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002228 enum yang_keyword kw;
2229 struct lysp_restr *restr;
2230
2231 LYSP_ARRAY_NEW_RET(ctx, patterns, restr, LY_EMEM);
2232
2233 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002234 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002235 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002236
2237 /* add special meaning first byte */
2238 if (buf) {
2239 buf = realloc(buf, word_len + 2);
2240 word = buf;
2241 } else {
2242 buf = malloc(word_len + 2);
2243 }
Radek Krejci44ceedc2018-10-02 15:54:31 +02002244 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002245 memmove(buf + 1, word, word_len + 1);
2246 word[0] = 0x06;
Radek Krejci44ceedc2018-10-02 15:54:31 +02002247 restr->arg = lydict_insert_zc(ctx->ctx, word);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002248
2249 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002250 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002251
2252 switch (kw) {
2253 case YANG_DESCRIPTION:
2254 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts);
2255 break;
2256 case YANG_REFERENCE:
2257 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts);
2258 break;
2259 case YANG_ERROR_APP_TAG:
2260 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRTAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts);
2261 break;
2262 case YANG_ERROR_MESSAGE:
2263 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRMSG, 0, &restr->emsg, Y_STR_ARG, &restr->exts);
2264 break;
2265 case YANG_MODIFIER:
2266 ret = parse_type_pattern_modifier(ctx, data, &restr->arg, &restr->exts);
2267 break;
2268 case YANG_CUSTOM:
2269 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts);
2270 break;
2271 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002272 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002273 return LY_EVALID;
2274 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002275 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002276 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002277 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002278
2279 return ret;
2280}
2281
Michal Vaskoea5abea2018-09-18 13:10:54 +02002282/**
2283 * @brief Parse the type statement.
2284 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002285 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002286 * @param[in,out] data Data to read from, always moved to currently handled character.
2287 * @param[in,out] type Type to wrote to.
2288 *
2289 * @return LY_ERR values.
2290 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002291static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002292parse_type(struct ly_parser_ctx *ctx, const char **data, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002293{
2294 LY_ERR ret = 0;
2295 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002296 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002297 enum yang_keyword kw;
2298 struct lysp_type *nest_type;
2299
2300 if (type->name) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002301 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002302 return LY_EVALID;
2303 }
2304
2305 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002306 ret = get_argument(ctx, data, Y_PREF_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002307 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002308
Radek Krejci44ceedc2018-10-02 15:54:31 +02002309 INSERT_WORD(ctx, buf, type->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002310 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002311 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002312
2313 switch (kw) {
2314 case YANG_BASE:
2315 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_BASE, &type->bases, Y_PREF_IDENTIF_ARG, &type->exts);
2316 break;
2317 case YANG_BIT:
2318 ret = parse_type_enum(ctx, data, kw, &type->bits);
2319 break;
2320 case YANG_ENUM:
2321 ret = parse_type_enum(ctx, data, kw, &type->enums);
2322 break;
2323 case YANG_FRACTION_DIGITS:
2324 ret = parse_type_fracdigits(ctx, data, &type->fraction_digits, &type->exts);
2325 break;
2326 case YANG_LENGTH:
2327 if (type->length) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002328 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002329 return LY_EVALID;
2330 }
2331 type->length = calloc(1, sizeof *type->length);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002332 LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002333
2334 ret = parse_restr(ctx, data, kw, type->length);
2335 break;
2336 case YANG_PATH:
2337 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_PATH, 0, &type->path, Y_STR_ARG, &type->exts);
2338 break;
2339 case YANG_PATTERN:
2340 ret = parse_type_pattern(ctx, data, &type->patterns);
2341 break;
2342 case YANG_RANGE:
2343 if (type->range) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002344 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002345 return LY_EVALID;
2346 }
2347 type->range = calloc(1, sizeof *type->range);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002348 LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx->ctx), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002349
2350 ret = parse_restr(ctx, data, kw, type->range);
2351 break;
2352 case YANG_REQUIRE_INSTANCE:
2353 ret = parse_type_reqinstance(ctx, data, &type->require_instance, &type->flags, &type->exts);
2354 break;
2355 case YANG_TYPE:
2356 {
2357 LYSP_ARRAY_NEW_RET(ctx, &type->types, nest_type, LY_EMEM);
2358 }
2359 ret = parse_type(ctx, data, nest_type);
2360 break;
2361 case YANG_CUSTOM:
2362 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &type->exts);
2363 break;
2364 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002365 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002366 return LY_EVALID;
2367 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002368 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002369 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002370 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002371
2372 return ret;
2373}
2374
Michal Vaskoea5abea2018-09-18 13:10:54 +02002375/**
2376 * @brief Parse the leaf statement.
2377 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002378 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002379 * @param[in,out] data Data to read from, always moved to currently handled character.
2380 * @param[in,out] siblings Siblings to add to.
2381 *
2382 * @return LY_ERR values.
2383 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002384static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002385parse_leaf(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002386{
2387 LY_ERR ret = 0;
2388 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002389 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002390 enum yang_keyword kw;
2391 struct lysp_node *iter;
2392 struct lysp_node_leaf *leaf;
2393
2394 /* create structure */
2395 leaf = calloc(1, sizeof *leaf);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002396 LY_CHECK_ERR_RET(!leaf, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002397 leaf->nodetype = LYS_LEAF;
2398
2399 /* insert into siblings */
2400 if (!*siblings) {
2401 *siblings = (struct lysp_node *)leaf;
2402 } else {
2403 for (iter = *siblings; iter->next; iter = iter->next);
2404 iter->next = (struct lysp_node *)leaf;
2405 }
2406
2407 /* get name */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002408 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002409 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002410
Radek Krejci44ceedc2018-10-02 15:54:31 +02002411 INSERT_WORD(ctx, buf, leaf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002412
2413 /* parse substatements */
2414 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002415 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002416
2417 switch (kw) {
2418 case YANG_CONFIG:
2419 ret = parse_config(ctx, data, &leaf->flags, &leaf->exts);
2420 break;
2421 case YANG_DEFAULT:
2422 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &leaf->dflt, Y_STR_ARG, &leaf->exts);
2423 break;
2424 case YANG_DESCRIPTION:
2425 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &leaf->dsc, Y_STR_ARG, &leaf->exts);
2426 break;
2427 case YANG_IF_FEATURE:
2428 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &leaf->iffeatures, Y_STR_ARG, &leaf->exts);
2429 break;
2430 case YANG_MANDATORY:
2431 ret = parse_mandatory(ctx, data, &leaf->flags, &leaf->exts);
2432 break;
2433 case YANG_MUST:
2434 ret = parse_restrs(ctx, data, kw, &leaf->musts);
2435 break;
2436 case YANG_REFERENCE:
2437 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &leaf->ref, Y_STR_ARG, &leaf->exts);
2438 break;
2439 case YANG_STATUS:
2440 ret = parse_status(ctx, data, &leaf->flags, &leaf->exts);
2441 break;
2442 case YANG_TYPE:
2443 ret = parse_type(ctx, data, &leaf->type);
2444 break;
2445 case YANG_UNITS:
2446 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, &leaf->units, Y_STR_ARG, &leaf->exts);
2447 break;
2448 case YANG_WHEN:
2449 ret = parse_when(ctx, data, &leaf->when);
2450 break;
2451 case YANG_CUSTOM:
2452 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &leaf->exts);
2453 break;
2454 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002455 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002456 return LY_EVALID;
2457 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002458 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002459 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002460 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002461
2462 /* mandatory substatements */
2463 if (!leaf->type.name) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002464 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002465 return LY_EVALID;
2466 }
2467
2468 return ret;
2469}
2470
Michal Vaskoea5abea2018-09-18 13:10:54 +02002471/**
2472 * @brief Parse the max-elements statement.
2473 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002474 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002475 * @param[in,out] data Data to read from, always moved to currently handled character.
2476 * @param[in,out] max Value to write to.
2477 * @param[in,out] flags Flags to write to.
2478 * @param[in,out] exts Extension instances to add to.
2479 *
2480 * @return LY_ERR values.
2481 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002482static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002483parse_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 +02002484{
2485 LY_ERR ret = 0;
2486 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002487 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002488 unsigned long int num;
2489 enum yang_keyword kw;
2490
2491 if (*flags & LYS_SET_MAX) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002492 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002493 return LY_EVALID;
2494 }
2495 *flags |= LYS_SET_MAX;
2496
2497 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002498 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002499 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002500
2501 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002502 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002503 free(buf);
2504 return LY_EVALID;
2505 }
2506
2507 if (strncmp(word, "unbounded", word_len)) {
2508 errno = 0;
2509 num = strtoul(word, &ptr, 10);
2510 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002511 if ((size_t)(ptr - word) != word_len) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002512 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002513 free(buf);
2514 return LY_EVALID;
2515 }
2516 if ((errno == ERANGE) || (num > UINT32_MAX)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002517 LOGVAL_YANG(ctx, LY_VCODE_OOB, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002518 free(buf);
2519 return LY_EVALID;
2520 }
2521
2522 *max = num;
2523 }
2524 free(buf);
2525
2526 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002527 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002528
2529 switch (kw) {
2530 case YANG_CUSTOM:
2531 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MAX, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02002532 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002533 break;
2534 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002535 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002536 return LY_EVALID;
2537 }
2538 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002539 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002540
2541 return ret;
2542}
2543
Michal Vaskoea5abea2018-09-18 13:10:54 +02002544/**
2545 * @brief Parse the min-elements statement.
2546 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002547 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002548 * @param[in,out] data Data to read from, always moved to currently handled character.
2549 * @param[in,out] min Value to write to.
2550 * @param[in,out] flags Flags to write to.
2551 * @param[in,out] exts Extension instances to add to.
2552 *
2553 * @return LY_ERR values.
2554 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002555static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002556parse_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 +02002557{
2558 LY_ERR ret = 0;
2559 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002560 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002561 unsigned long int num;
2562 enum yang_keyword kw;
2563
2564 if (*flags & LYS_SET_MIN) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002565 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002566 return LY_EVALID;
2567 }
2568 *flags |= LYS_SET_MIN;
2569
2570 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002571 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002572 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002573
2574 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
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
2580 errno = 0;
2581 num = strtoul(word, &ptr, 10);
2582 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002583 if ((size_t)(ptr - word) != word_len) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002584 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002585 free(buf);
2586 return LY_EVALID;
2587 }
2588 if ((errno == ERANGE) || (num > UINT32_MAX)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002589 LOGVAL_YANG(ctx, LY_VCODE_OOB, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002590 free(buf);
2591 return LY_EVALID;
2592 }
2593 *min = num;
2594 free(buf);
2595
2596 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002597 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002598
2599 switch (kw) {
2600 case YANG_CUSTOM:
2601 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MIN, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02002602 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002603 break;
2604 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002605 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002606 return LY_EVALID;
2607 }
2608 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002609 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002610
2611 return ret;
2612}
2613
Michal Vaskoea5abea2018-09-18 13:10:54 +02002614/**
2615 * @brief Parse the ordered-by statement.
2616 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002617 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002618 * @param[in,out] data Data to read from, always moved to currently handled character.
2619 * @param[in,out] flags Flags to write to.
2620 * @param[in,out] exts Extension instances to add to.
2621 *
2622 * @return LY_ERR values.
2623 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002624static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002625parse_orderedby(struct ly_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002626{
2627 LY_ERR ret = 0;
2628 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002629 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002630 enum yang_keyword kw;
2631
2632 if (*flags & LYS_ORDBY_MASK) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002633 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002634 return LY_EVALID;
2635 }
2636
2637 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002638 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002639 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002640
2641 if ((word_len == 6) && !strncmp(word, "system", word_len)) {
2642 *flags |= LYS_ORDBY_SYSTEM;
2643 } else if ((word_len == 4) && !strncmp(word, "user", word_len)) {
2644 *flags |= LYS_ORDBY_USER;
2645 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002646 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002647 free(buf);
2648 return LY_EVALID;
2649 }
2650 free(buf);
2651
2652 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002653 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002654
2655 switch (kw) {
2656 case YANG_CUSTOM:
2657 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_ORDEREDBY, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02002658 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002659 break;
2660 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002661 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002662 return LY_EVALID;
2663 }
2664 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002665 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002666
2667 return ret;
2668}
2669
Michal Vaskoea5abea2018-09-18 13:10:54 +02002670/**
2671 * @brief Parse the leaf-list statement.
2672 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002673 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002674 * @param[in,out] data Data to read from, always moved to currently handled character.
2675 * @param[in,out] siblings Siblings to add to.
2676 *
2677 * @return LY_ERR values.
2678 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002679static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002680parse_leaflist(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002681{
2682 LY_ERR ret = 0;
2683 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002684 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002685 enum yang_keyword kw;
2686 struct lysp_node *iter;
2687 struct lysp_node_leaflist *llist;
2688
2689 /* create structure */
2690 llist = calloc(1, sizeof *llist);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002691 LY_CHECK_ERR_RET(!llist, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002692 llist->nodetype = LYS_LEAFLIST;
2693
2694 /* insert into siblings */
2695 if (!*siblings) {
2696 *siblings = (struct lysp_node *)llist;
2697 } else {
2698 for (iter = *siblings; iter->next; iter = iter->next);
2699 iter->next = (struct lysp_node *)llist;
2700 }
2701
2702 /* get name */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002703 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002704 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002705
Radek Krejci44ceedc2018-10-02 15:54:31 +02002706 INSERT_WORD(ctx, buf, llist->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002707
2708 /* parse substatements */
2709 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002710 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002711
2712 switch (kw) {
2713 case YANG_CONFIG:
2714 ret = parse_config(ctx, data, &llist->flags, &llist->exts);
2715 break;
2716 case YANG_DEFAULT:
2717 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_DEFAULT, &llist->dflts, Y_STR_ARG, &llist->exts);
2718 break;
2719 case YANG_DESCRIPTION:
2720 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &llist->dsc, Y_STR_ARG, &llist->exts);
2721 break;
2722 case YANG_IF_FEATURE:
2723 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &llist->iffeatures, Y_STR_ARG, &llist->exts);
2724 break;
2725 case YANG_MAX_ELEMENTS:
2726 ret = parse_maxelements(ctx, data, &llist->max, &llist->flags, &llist->exts);
2727 break;
2728 case YANG_MIN_ELEMENTS:
2729 ret = parse_minelements(ctx, data, &llist->min, &llist->flags, &llist->exts);
2730 break;
2731 case YANG_MUST:
2732 ret = parse_restrs(ctx, data, kw, &llist->musts);
2733 break;
2734 case YANG_ORDERED_BY:
2735 ret = parse_orderedby(ctx, data, &llist->flags, &llist->exts);
2736 break;
2737 case YANG_REFERENCE:
2738 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &llist->ref, Y_STR_ARG, &llist->exts);
2739 break;
2740 case YANG_STATUS:
2741 ret = parse_status(ctx, data, &llist->flags, &llist->exts);
2742 break;
2743 case YANG_TYPE:
2744 ret = parse_type(ctx, data, &llist->type);
2745 break;
2746 case YANG_UNITS:
2747 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, &llist->units, Y_STR_ARG, &llist->exts);
2748 break;
2749 case YANG_WHEN:
2750 ret = parse_when(ctx, data, &llist->when);
2751 break;
2752 case YANG_CUSTOM:
2753 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &llist->exts);
2754 break;
2755 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002756 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002757 return LY_EVALID;
2758 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002759 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002760 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002761 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002762
2763 /* mandatory substatements */
2764 if (!llist->type.name) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002765 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002766 return LY_EVALID;
2767 }
2768
2769 return ret;
2770}
2771
Michal Vaskoea5abea2018-09-18 13:10:54 +02002772/**
2773 * @brief Parse the refine statement.
2774 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002775 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002776 * @param[in,out] data Data to read from, always moved to currently handled character.
2777 * @param[in,out] refines Refines to add to.
2778 *
2779 * @return LY_ERR values.
2780 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002781static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002782parse_refine(struct ly_parser_ctx *ctx, const char **data, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002783{
2784 LY_ERR ret = 0;
2785 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002786 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002787 enum yang_keyword kw;
2788 struct lysp_refine *rf;
2789
2790 LYSP_ARRAY_NEW_RET(ctx, refines, rf, LY_EMEM);
2791
2792 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002793 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002794 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002795
Radek Krejci44ceedc2018-10-02 15:54:31 +02002796 INSERT_WORD(ctx, buf, rf->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002797 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002798 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002799
2800 switch (kw) {
2801 case YANG_CONFIG:
2802 ret = parse_config(ctx, data, &rf->flags, &rf->exts);
2803 break;
2804 case YANG_DEFAULT:
2805 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_DEFAULT, &rf->dflts, Y_STR_ARG, &rf->exts);
2806 break;
2807 case YANG_DESCRIPTION:
2808 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &rf->dsc, Y_STR_ARG, &rf->exts);
2809 break;
2810 case YANG_IF_FEATURE:
2811 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &rf->iffeatures, Y_STR_ARG, &rf->exts);
2812 break;
2813 case YANG_MAX_ELEMENTS:
2814 ret = parse_maxelements(ctx, data, &rf->max, &rf->flags, &rf->exts);
2815 break;
2816 case YANG_MIN_ELEMENTS:
2817 ret = parse_minelements(ctx, data, &rf->min, &rf->flags, &rf->exts);
2818 break;
2819 case YANG_MUST:
2820 ret = parse_restrs(ctx, data, kw, &rf->musts);
2821 break;
2822 case YANG_MANDATORY:
2823 ret = parse_mandatory(ctx, data, &rf->flags, &rf->exts);
2824 break;
2825 case YANG_REFERENCE:
2826 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &rf->ref, Y_STR_ARG, &rf->exts);
2827 break;
2828 case YANG_PRESENCE:
2829 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_PRESENCE, 0, &rf->presence, Y_STR_ARG, &rf->exts);
2830 break;
2831 case YANG_CUSTOM:
2832 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rf->exts);
2833 break;
2834 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002835 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002836 return LY_EVALID;
2837 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002838 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002839 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002840 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002841
2842 return ret;
2843}
2844
Michal Vaskoea5abea2018-09-18 13:10:54 +02002845/**
2846 * @brief Parse the typedef statement.
2847 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002848 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002849 * @param[in,out] data Data to read from, always moved to currently handled character.
2850 * @param[in,out] typedefs Typedefs to add to.
2851 *
2852 * @return LY_ERR values.
2853 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002854static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002855parse_typedef(struct ly_parser_ctx *ctx, const char **data, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002856{
2857 LY_ERR ret = 0;
2858 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002859 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002860 enum yang_keyword kw;
2861 struct lysp_tpdf *tpdf;
2862
2863 LYSP_ARRAY_NEW_RET(ctx, typedefs, tpdf, LY_EMEM);
2864
2865 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002866 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002867 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002868
Radek Krejci44ceedc2018-10-02 15:54:31 +02002869 INSERT_WORD(ctx, buf, tpdf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002870
2871 /* parse substatements */
2872 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002873 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002874
2875 switch (kw) {
2876 case YANG_DEFAULT:
2877 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &tpdf->dflt, Y_STR_ARG, &tpdf->exts);
2878 break;
2879 case YANG_DESCRIPTION:
2880 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &tpdf->dsc, Y_STR_ARG, &tpdf->exts);
2881 break;
2882 case YANG_REFERENCE:
2883 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &tpdf->ref, Y_STR_ARG, &tpdf->exts);
2884 break;
2885 case YANG_STATUS:
2886 ret = parse_status(ctx, data, &tpdf->flags, &tpdf->exts);
2887 break;
2888 case YANG_TYPE:
2889 ret = parse_type(ctx, data, &tpdf->type);
2890 break;
2891 case YANG_UNITS:
2892 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, &tpdf->units, Y_STR_ARG, &tpdf->exts);
2893 break;
2894 case YANG_CUSTOM:
2895 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &tpdf->exts);
2896 break;
2897 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002898 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002899 return LY_EVALID;
2900 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002901 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002902 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002903 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002904
2905 /* mandatory substatements */
2906 if (!tpdf->type.name) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002907 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002908 return LY_EVALID;
2909 }
2910
2911 return ret;
2912}
2913
Michal Vaskoea5abea2018-09-18 13:10:54 +02002914/**
2915 * @brief Parse the input or output statement.
2916 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002917 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002918 * @param[in,out] data Data to read from, always moved to currently handled character.
2919 * @param[in] kw Type of this particular keyword
2920 * @param[in,out] inout_p Input/output pointer to write to.
2921 *
2922 * @return LY_ERR values.
2923 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002924static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002925parse_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 +02002926{
2927 LY_ERR ret = 0;
2928 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002929 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002930 struct lysp_action_inout *inout;
2931
2932 if (*inout_p) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002933 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002934 return LY_EVALID;
2935 }
2936
2937 /* create structure */
2938 inout = calloc(1, sizeof *inout);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002939 LY_CHECK_ERR_RET(!inout, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002940 *inout_p = inout;
2941
2942 /* parse substatements */
2943 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002944 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002945
2946 switch (kw) {
2947 case YANG_ANYDATA:
2948 case YANG_ANYXML:
2949 ret = parse_any(ctx, data, kw, &inout->data);
2950 break;
2951 case YANG_CHOICE:
2952 ret = parse_choice(ctx, data, &inout->data);
2953 break;
2954 case YANG_CONTAINER:
2955 ret = parse_container(ctx, data, &inout->data);
2956 break;
2957 case YANG_LEAF:
2958 ret = parse_leaf(ctx, data, &inout->data);
2959 break;
2960 case YANG_LEAF_LIST:
2961 ret = parse_leaflist(ctx, data, &inout->data);
2962 break;
2963 case YANG_LIST:
2964 ret = parse_list(ctx, data, &inout->data);
2965 break;
2966 case YANG_USES:
2967 ret = parse_uses(ctx, data, &inout->data);
2968 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002969 case YANG_TYPEDEF:
2970 ret = parse_typedef(ctx, data, &inout->typedefs);
2971 break;
2972 case YANG_MUST:
2973 ret = parse_restrs(ctx, data, kw, &inout->musts);
2974 break;
2975 case YANG_GROUPING:
2976 ret = parse_grouping(ctx, data, &inout->groupings);
2977 break;
2978 case YANG_CUSTOM:
2979 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inout->exts);
2980 break;
2981 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002982 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "input/output");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002983 return LY_EVALID;
2984 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002985 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002986 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002987 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002988
2989 return ret;
2990}
2991
Michal Vaskoea5abea2018-09-18 13:10:54 +02002992/**
2993 * @brief Parse the action statement.
2994 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002995 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002996 * @param[in,out] data Data to read from, always moved to currently handled character.
2997 * @param[in,out] actions Actions to add to.
2998 *
2999 * @return LY_ERR values.
3000 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003001static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003002parse_action(struct ly_parser_ctx *ctx, const char **data, struct lysp_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003003{
3004 LY_ERR ret = 0;
3005 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003006 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003007 enum yang_keyword kw;
3008 struct lysp_action *act;
3009
3010 LYSP_ARRAY_NEW_RET(ctx, actions, act, LY_EMEM);
3011
3012 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003013 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003014 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003015
Radek Krejci44ceedc2018-10-02 15:54:31 +02003016 INSERT_WORD(ctx, buf, act->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003017 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003018 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003019
3020 switch (kw) {
3021 case YANG_DESCRIPTION:
3022 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &act->dsc, Y_STR_ARG, &act->exts);
3023 break;
3024 case YANG_IF_FEATURE:
3025 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &act->iffeatures, Y_STR_ARG, &act->exts);
3026 break;
3027 case YANG_REFERENCE:
3028 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &act->ref, Y_STR_ARG, &act->exts);
3029 break;
3030 case YANG_STATUS:
3031 ret = parse_status(ctx, data, &act->flags, &act->exts);
3032 break;
3033
3034 case YANG_INPUT:
3035 ret = parse_inout(ctx, data, kw, &act->input);
3036 break;
3037 case YANG_OUTPUT:
3038 ret = parse_inout(ctx, data, kw, &act->output);
3039 break;
3040
3041 case YANG_TYPEDEF:
3042 ret = parse_typedef(ctx, data, &act->typedefs);
3043 break;
3044 case YANG_GROUPING:
3045 ret = parse_grouping(ctx, data, &act->groupings);
3046 break;
3047 case YANG_CUSTOM:
3048 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &act->exts);
3049 break;
3050 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003051 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "action");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003052 return LY_EVALID;
3053 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003054 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003055 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003056 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003057
3058 return ret;
3059}
3060
Michal Vaskoea5abea2018-09-18 13:10:54 +02003061/**
3062 * @brief Parse the notification statement.
3063 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003064 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003065 * @param[in,out] data Data to read from, always moved to currently handled character.
3066 * @param[in,out] notifs Notifications to add to.
3067 *
3068 * @return LY_ERR values.
3069 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003070static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003071parse_notif(struct ly_parser_ctx *ctx, const char **data, struct lysp_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003072{
3073 LY_ERR ret = 0;
3074 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003075 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003076 enum yang_keyword kw;
3077 struct lysp_notif *notif;
3078
3079 LYSP_ARRAY_NEW_RET(ctx, notifs, notif, LY_EMEM);
3080
3081 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003082 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003083 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003084
Radek Krejci44ceedc2018-10-02 15:54:31 +02003085 INSERT_WORD(ctx, buf, notif->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003086 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003087 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003088
3089 switch (kw) {
3090 case YANG_DESCRIPTION:
3091 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &notif->dsc, Y_STR_ARG, &notif->exts);
3092 break;
3093 case YANG_IF_FEATURE:
3094 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &notif->iffeatures, Y_STR_ARG, &notif->exts);
3095 break;
3096 case YANG_REFERENCE:
3097 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &notif->ref, Y_STR_ARG, &notif->exts);
3098 break;
3099 case YANG_STATUS:
3100 ret = parse_status(ctx, data, &notif->flags, &notif->exts);
3101 break;
3102
3103 case YANG_ANYDATA:
3104 case YANG_ANYXML:
3105 ret = parse_any(ctx, data, kw, &notif->data);
3106 break;
3107 case YANG_CHOICE:
3108 ret = parse_case(ctx, data, &notif->data);
3109 break;
3110 case YANG_CONTAINER:
3111 ret = parse_container(ctx, data, &notif->data);
3112 break;
3113 case YANG_LEAF:
3114 ret = parse_leaf(ctx, data, &notif->data);
3115 break;
3116 case YANG_LEAF_LIST:
3117 ret = parse_leaflist(ctx, data, &notif->data);
3118 break;
3119 case YANG_LIST:
3120 ret = parse_list(ctx, data, &notif->data);
3121 break;
3122 case YANG_USES:
3123 ret = parse_uses(ctx, data, &notif->data);
3124 break;
3125
3126 case YANG_MUST:
3127 ret = parse_restrs(ctx, data, kw, &notif->musts);
3128 break;
3129 case YANG_TYPEDEF:
3130 ret = parse_typedef(ctx, data, &notif->typedefs);
3131 break;
3132 case YANG_GROUPING:
3133 ret = parse_grouping(ctx, data, &notif->groupings);
3134 break;
3135 case YANG_CUSTOM:
3136 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &notif->exts);
3137 break;
3138 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003139 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003140 return LY_EVALID;
3141 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003142 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003143 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003144 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003145
3146 return ret;
3147}
3148
Michal Vaskoea5abea2018-09-18 13:10:54 +02003149/**
3150 * @brief Parse the grouping statement.
3151 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003152 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003153 * @param[in,out] data Data to read from, always moved to currently handled character.
3154 * @param[in,out] groupings Groupings to add to.
3155 *
3156 * @return LY_ERR values.
3157 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003158static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003159parse_grouping(struct ly_parser_ctx *ctx, const char **data, struct lysp_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003160{
3161 LY_ERR ret = 0;
3162 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003163 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003164 enum yang_keyword kw;
3165 struct lysp_grp *grp;
3166
3167 LYSP_ARRAY_NEW_RET(ctx, groupings, grp, LY_EMEM);
3168
3169 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003170 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003171 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003172
Radek Krejci44ceedc2018-10-02 15:54:31 +02003173 INSERT_WORD(ctx, buf, grp->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003174
3175 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003176 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003177
3178 switch (kw) {
3179 case YANG_DESCRIPTION:
3180 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &grp->dsc, Y_STR_ARG, &grp->exts);
3181 break;
3182 case YANG_REFERENCE:
3183 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &grp->ref, Y_STR_ARG, &grp->exts);
3184 break;
3185 case YANG_STATUS:
3186 ret = parse_status(ctx, data, &grp->flags, &grp->exts);
3187 break;
3188
3189 case YANG_ANYDATA:
3190 case YANG_ANYXML:
3191 ret = parse_any(ctx, data, kw, &grp->data);
3192 break;
3193 case YANG_CHOICE:
3194 ret = parse_choice(ctx, data, &grp->data);
3195 break;
3196 case YANG_CONTAINER:
3197 ret = parse_container(ctx, data, &grp->data);
3198 break;
3199 case YANG_LEAF:
3200 ret = parse_leaf(ctx, data, &grp->data);
3201 break;
3202 case YANG_LEAF_LIST:
3203 ret = parse_leaflist(ctx, data, &grp->data);
3204 break;
3205 case YANG_LIST:
3206 ret = parse_list(ctx, data, &grp->data);
3207 break;
3208 case YANG_USES:
3209 ret = parse_uses(ctx, data, &grp->data);
3210 break;
3211
3212 case YANG_TYPEDEF:
3213 ret = parse_typedef(ctx, data, &grp->typedefs);
3214 break;
3215 case YANG_ACTION:
3216 ret = parse_action(ctx, data, &grp->actions);
3217 break;
3218 case YANG_GROUPING:
3219 ret = parse_grouping(ctx, data, &grp->groupings);
3220 break;
3221 case YANG_NOTIFICATION:
3222 ret = parse_notif(ctx, data, &grp->notifs);
3223 break;
3224 case YANG_CUSTOM:
3225 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &grp->exts);
3226 break;
3227 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003228 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003229 return LY_EVALID;
3230 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003231 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003232 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003233 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003234
3235 return ret;
3236}
3237
Michal Vaskoea5abea2018-09-18 13:10:54 +02003238/**
3239 * @brief Parse the refine statement.
3240 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003241 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003242 * @param[in,out] data Data to read from, always moved to currently handled character.
3243 * @param[in,out] augments Augments to add to.
3244 *
3245 * @return LY_ERR values.
3246 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003247static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003248parse_augment(struct ly_parser_ctx *ctx, const char **data, struct lysp_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003249{
3250 LY_ERR ret = 0;
3251 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003252 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003253 enum yang_keyword kw;
3254 struct lysp_augment *aug;
3255
3256 LYSP_ARRAY_NEW_RET(ctx, augments, aug, LY_EMEM);
3257
3258 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003259 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003260 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003261
Radek Krejci44ceedc2018-10-02 15:54:31 +02003262 INSERT_WORD(ctx, buf, aug->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003263 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003264 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003265
3266 switch (kw) {
3267 case YANG_DESCRIPTION:
3268 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &aug->dsc, Y_STR_ARG, &aug->exts);
3269 break;
3270 case YANG_IF_FEATURE:
3271 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &aug->iffeatures, Y_STR_ARG, &aug->exts);
3272 break;
3273 case YANG_REFERENCE:
3274 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &aug->ref, Y_STR_ARG, &aug->exts);
3275 break;
3276 case YANG_STATUS:
3277 ret = parse_status(ctx, data, &aug->flags, &aug->exts);
3278 break;
3279 case YANG_WHEN:
3280 ret = parse_when(ctx, data, &aug->when);
3281 break;
3282
3283 case YANG_ANYDATA:
3284 case YANG_ANYXML:
3285 ret = parse_any(ctx, data, kw, &aug->child);
3286 break;
3287 case YANG_CASE:
3288 ret = parse_case(ctx, data, &aug->child);
3289 break;
3290 case YANG_CHOICE:
3291 ret = parse_choice(ctx, data, &aug->child);
3292 break;
3293 case YANG_CONTAINER:
3294 ret = parse_container(ctx, data, &aug->child);
3295 break;
3296 case YANG_LEAF:
3297 ret = parse_leaf(ctx, data, &aug->child);
3298 break;
3299 case YANG_LEAF_LIST:
3300 ret = parse_leaflist(ctx, data, &aug->child);
3301 break;
3302 case YANG_LIST:
3303 ret = parse_list(ctx, data, &aug->child);
3304 break;
3305 case YANG_USES:
3306 ret = parse_uses(ctx, data, &aug->child);
3307 break;
3308
3309 case YANG_ACTION:
3310 ret = parse_action(ctx, data, &aug->actions);
3311 break;
3312 case YANG_NOTIFICATION:
3313 ret = parse_notif(ctx, data, &aug->notifs);
3314 break;
3315 case YANG_CUSTOM:
3316 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &aug->exts);
3317 break;
3318 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003319 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003320 return LY_EVALID;
3321 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003322 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003323 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003324 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003325
3326 return ret;
3327}
3328
Michal Vaskoea5abea2018-09-18 13:10:54 +02003329/**
3330 * @brief Parse the uses statement.
3331 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003332 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003333 * @param[in,out] data Data to read from, always moved to currently handled character.
3334 * @param[in,out] siblings Siblings to add to.
3335 *
3336 * @return LY_ERR values.
3337 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003338static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003339parse_uses(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003340{
3341 LY_ERR ret = 0;
3342 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003343 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003344 enum yang_keyword kw;
3345 struct lysp_node *iter;
3346 struct lysp_node_uses *uses;
3347
3348 /* create structure */
3349 uses = calloc(1, sizeof *uses);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003350 LY_CHECK_ERR_RET(!uses, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003351 uses->nodetype = LYS_USES;
3352
3353 /* insert into siblings */
3354 if (!*siblings) {
3355 *siblings = (struct lysp_node *)uses;
3356 } else {
3357 for (iter = *siblings; iter->next; iter = iter->next);
3358 iter->next = (struct lysp_node *)uses;
3359 }
3360
3361 /* get name */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003362 ret = get_argument(ctx, data, Y_PREF_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003363 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003364
Radek Krejci44ceedc2018-10-02 15:54:31 +02003365 INSERT_WORD(ctx, buf, uses->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003366
3367 /* parse substatements */
3368 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003369 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003370
3371 switch (kw) {
3372 case YANG_DESCRIPTION:
3373 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &uses->dsc, Y_STR_ARG, &uses->exts);
3374 break;
3375 case YANG_IF_FEATURE:
3376 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &uses->iffeatures, Y_STR_ARG, &uses->exts);
3377 break;
3378 case YANG_REFERENCE:
3379 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &uses->ref, Y_STR_ARG, &uses->exts);
3380 break;
3381 case YANG_STATUS:
3382 ret = parse_status(ctx, data, &uses->flags, &uses->exts);
3383 break;
3384 case YANG_WHEN:
3385 ret = parse_when(ctx, data, &uses->when);
3386 break;
3387
3388 case YANG_REFINE:
3389 ret = parse_refine(ctx, data, &uses->refines);
3390 break;
3391 case YANG_AUGMENT:
3392 ret = parse_augment(ctx, data, &uses->augments);
3393 break;
3394 case YANG_CUSTOM:
3395 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &uses->exts);
3396 break;
3397 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003398 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003399 return LY_EVALID;
3400 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003401 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003402 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003403 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003404
3405 return ret;
3406}
3407
Michal Vaskoea5abea2018-09-18 13:10:54 +02003408/**
3409 * @brief Parse the case statement.
3410 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003411 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003412 * @param[in,out] data Data to read from, always moved to currently handled character.
3413 * @param[in,out] siblings Siblings to add to.
3414 *
3415 * @return LY_ERR values.
3416 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003417static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003418parse_case(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003419{
3420 LY_ERR ret = 0;
3421 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003422 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003423 enum yang_keyword kw;
3424 struct lysp_node *iter;
3425 struct lysp_node_case *cas;
3426
3427 /* create structure */
3428 cas = calloc(1, sizeof *cas);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003429 LY_CHECK_ERR_RET(!cas, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003430 cas->nodetype = LYS_CASE;
3431
3432 /* insert into siblings */
3433 if (!*siblings) {
3434 *siblings = (struct lysp_node *)cas;
3435 } else {
3436 for (iter = *siblings; iter->next; iter = iter->next);
3437 iter->next = (struct lysp_node *)cas;
3438 }
3439
3440 /* get name */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003441 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003442 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003443
Radek Krejci44ceedc2018-10-02 15:54:31 +02003444 INSERT_WORD(ctx, buf, cas->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003445
3446 /* parse substatements */
3447 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003448 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003449
3450 switch (kw) {
3451 case YANG_DESCRIPTION:
3452 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &cas->dsc, Y_STR_ARG, &cas->exts);
3453 break;
3454 case YANG_IF_FEATURE:
3455 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &cas->iffeatures, Y_STR_ARG, &cas->exts);
3456 break;
3457 case YANG_REFERENCE:
3458 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &cas->ref, Y_STR_ARG, &cas->exts);
3459 break;
3460 case YANG_STATUS:
3461 ret = parse_status(ctx, data, &cas->flags, &cas->exts);
3462 break;
3463 case YANG_WHEN:
3464 ret = parse_when(ctx, data, &cas->when);
3465 break;
3466
3467 case YANG_ANYDATA:
3468 case YANG_ANYXML:
3469 ret = parse_any(ctx, data, kw, &cas->child);
3470 break;
3471 case YANG_CHOICE:
3472 ret = parse_case(ctx, data, &cas->child);
3473 break;
3474 case YANG_CONTAINER:
3475 ret = parse_container(ctx, data, &cas->child);
3476 break;
3477 case YANG_LEAF:
3478 ret = parse_leaf(ctx, data, &cas->child);
3479 break;
3480 case YANG_LEAF_LIST:
3481 ret = parse_leaflist(ctx, data, &cas->child);
3482 break;
3483 case YANG_LIST:
3484 ret = parse_list(ctx, data, &cas->child);
3485 break;
3486 case YANG_USES:
3487 ret = parse_uses(ctx, data, &cas->child);
3488 break;
3489 case YANG_CUSTOM:
3490 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cas->exts);
3491 break;
3492 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003493 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003494 return LY_EVALID;
3495 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003496 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003497 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003498 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003499
3500 return ret;
3501}
3502
Michal Vaskoea5abea2018-09-18 13:10:54 +02003503/**
3504 * @brief Parse the choice statement.
3505 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003506 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003507 * @param[in,out] data Data to read from, always moved to currently handled character.
3508 * @param[in,out] siblings Siblings to add to.
3509 *
3510 * @return LY_ERR values.
3511 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003512static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003513parse_choice(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003514{
3515 LY_ERR ret = 0;
3516 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003517 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003518 enum yang_keyword kw;
3519 struct lysp_node *iter;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003520 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003521
3522 /* create structure */
Radek Krejci44ceedc2018-10-02 15:54:31 +02003523 choice = calloc(1, sizeof *choice);
3524 LY_CHECK_ERR_RET(!choice, LOGMEM(ctx->ctx), LY_EMEM);
3525 choice->nodetype = LYS_CHOICE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003526
3527 /* insert into siblings */
3528 if (!*siblings) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02003529 *siblings = (struct lysp_node *)choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003530 } else {
3531 for (iter = *siblings; iter->next; iter = iter->next);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003532 iter->next = (struct lysp_node *)choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003533 }
3534
3535 /* get name */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003536 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003537 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003538
Radek Krejci44ceedc2018-10-02 15:54:31 +02003539 INSERT_WORD(ctx, buf, choice->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003540
3541 /* parse substatements */
3542 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003543 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003544
3545 switch (kw) {
3546 case YANG_CONFIG:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003547 ret = parse_config(ctx, data, &choice->flags, &choice->exts);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003548 break;
3549 case YANG_DESCRIPTION:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003550 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &choice->dsc, Y_STR_ARG, &choice->exts);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003551 break;
3552 case YANG_IF_FEATURE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003553 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &choice->iffeatures, Y_STR_ARG, &choice->exts);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003554 break;
3555 case YANG_MANDATORY:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003556 ret = parse_mandatory(ctx, data, &choice->flags, &choice->exts);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003557 break;
3558 case YANG_REFERENCE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003559 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &choice->ref, Y_STR_ARG, &choice->exts);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003560 break;
3561 case YANG_STATUS:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003562 ret = parse_status(ctx, data, &choice->flags, &choice->exts);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003563 break;
3564 case YANG_WHEN:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003565 ret = parse_when(ctx, data, &choice->when);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003566 break;
3567 case YANG_DEFAULT:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003568 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &choice->dflt, Y_IDENTIF_ARG, &choice->exts);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003569 break;
3570
3571 case YANG_ANYDATA:
3572 case YANG_ANYXML:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003573 ret = parse_any(ctx, data, kw, &choice->child);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003574 break;
3575 case YANG_CASE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003576 ret = parse_case(ctx, data, &choice->child);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003577 break;
3578 case YANG_CHOICE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003579 ret = parse_choice(ctx, data, &choice->child);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003580 break;
3581 case YANG_CONTAINER:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003582 ret = parse_container(ctx, data, &choice->child);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003583 break;
3584 case YANG_LEAF:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003585 ret = parse_leaf(ctx, data, &choice->child);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003586 break;
3587 case YANG_LEAF_LIST:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003588 ret = parse_leaflist(ctx, data, &choice->child);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003589 break;
3590 case YANG_LIST:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003591 ret = parse_list(ctx, data, &choice->child);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003592 break;
3593 case YANG_CUSTOM:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003594 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &choice->exts);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003595 break;
3596 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003597 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003598 return LY_EVALID;
3599 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003600 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003601 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003602 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003603
3604 return ret;
3605}
3606
Michal Vaskoea5abea2018-09-18 13:10:54 +02003607/**
3608 * @brief Parse the container statement.
3609 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003610 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003611 * @param[in,out] data Data to read from, always moved to currently handled character.
3612 * @param[in,out] siblings Siblings to add to.
3613 *
3614 * @return LY_ERR values.
3615 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003616static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003617parse_container(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003618{
3619 LY_ERR ret = 0;
3620 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003621 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003622 enum yang_keyword kw;
3623 struct lysp_node *iter;
3624 struct lysp_node_container *cont;
3625
3626 /* create structure */
3627 cont = calloc(1, sizeof *cont);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003628 LY_CHECK_ERR_RET(!cont, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003629 cont->nodetype = LYS_CONTAINER;
3630
3631 /* insert into siblings */
3632 if (!*siblings) {
3633 *siblings = (struct lysp_node *)cont;
3634 } else {
3635 for (iter = *siblings; iter->next; iter = iter->next);
3636 iter->next = (struct lysp_node *)cont;
3637 }
3638
3639 /* get name */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003640 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003641 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003642
Radek Krejci44ceedc2018-10-02 15:54:31 +02003643 INSERT_WORD(ctx, buf, cont->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003644
3645 /* parse substatements */
3646 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003647 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003648
3649 switch (kw) {
3650 case YANG_CONFIG:
3651 ret = parse_config(ctx, data, &cont->flags, &cont->exts);
3652 break;
3653 case YANG_DESCRIPTION:
3654 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &cont->dsc, Y_STR_ARG, &cont->exts);
3655 break;
3656 case YANG_IF_FEATURE:
3657 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &cont->iffeatures, Y_STR_ARG, &cont->exts);
3658 break;
3659 case YANG_REFERENCE:
3660 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &cont->ref, Y_STR_ARG, &cont->exts);
3661 break;
3662 case YANG_STATUS:
3663 ret = parse_status(ctx, data, &cont->flags, &cont->exts);
3664 break;
3665 case YANG_WHEN:
3666 ret = parse_when(ctx, data, &cont->when);
3667 break;
3668 case YANG_PRESENCE:
3669 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_PRESENCE, 0, &cont->presence, Y_STR_ARG, &cont->exts);
3670 break;
3671
3672 case YANG_ANYDATA:
3673 case YANG_ANYXML:
3674 ret = parse_any(ctx, data, kw, &cont->child);
3675 break;
3676 case YANG_CHOICE:
3677 ret = parse_choice(ctx, data, &cont->child);
3678 break;
3679 case YANG_CONTAINER:
3680 ret = parse_container(ctx, data, &cont->child);
3681 break;
3682 case YANG_LEAF:
3683 ret = parse_leaf(ctx, data, &cont->child);
3684 break;
3685 case YANG_LEAF_LIST:
3686 ret = parse_leaflist(ctx, data, &cont->child);
3687 break;
3688 case YANG_LIST:
3689 ret = parse_list(ctx, data, &cont->child);
3690 break;
3691 case YANG_USES:
3692 ret = parse_uses(ctx, data, &cont->child);
3693 break;
3694
3695 case YANG_TYPEDEF:
3696 ret = parse_typedef(ctx, data, &cont->typedefs);
3697 break;
3698 case YANG_MUST:
3699 ret = parse_restrs(ctx, data, kw, &cont->musts);
3700 break;
3701 case YANG_ACTION:
3702 ret = parse_action(ctx, data, &cont->actions);
3703 break;
3704 case YANG_GROUPING:
3705 ret = parse_grouping(ctx, data, &cont->groupings);
3706 break;
3707 case YANG_NOTIFICATION:
3708 ret = parse_notif(ctx, data, &cont->notifs);
3709 break;
3710 case YANG_CUSTOM:
3711 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cont->exts);
3712 break;
3713 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003714 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003715 return LY_EVALID;
3716 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003717 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003718 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003719 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003720
3721 return ret;
3722}
3723
Michal Vaskoea5abea2018-09-18 13:10:54 +02003724/**
3725 * @brief Parse the list statement.
3726 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003727 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003728 * @param[in,out] data Data to read from, always moved to currently handled character.
3729 * @param[in,out] siblings Siblings to add to.
3730 *
3731 * @return LY_ERR values.
3732 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003733static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003734parse_list(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003735{
3736 LY_ERR ret = 0;
3737 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003738 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003739 enum yang_keyword kw;
3740 struct lysp_node *iter;
3741 struct lysp_node_list *list;
3742
3743 /* create structure */
3744 list = calloc(1, sizeof *list);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003745 LY_CHECK_ERR_RET(!list, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003746 list->nodetype = LYS_LIST;
3747
3748 /* insert into siblings */
3749 if (!*siblings) {
3750 *siblings = (struct lysp_node *)list;
3751 } else {
3752 for (iter = *siblings; iter->next; iter = iter->next);
3753 iter->next = (struct lysp_node *)list;
3754 }
3755
3756 /* get name */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003757 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003758 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003759
Radek Krejci44ceedc2018-10-02 15:54:31 +02003760 INSERT_WORD(ctx, buf, list->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003761
3762 /* parse substatements */
3763 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003764 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003765
3766 switch (kw) {
3767 case YANG_CONFIG:
3768 ret = parse_config(ctx, data, &list->flags, &list->exts);
3769 break;
3770 case YANG_DESCRIPTION:
3771 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &list->dsc, Y_STR_ARG, &list->exts);
3772 break;
3773 case YANG_IF_FEATURE:
3774 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &list->iffeatures, Y_STR_ARG, &list->exts);
3775 break;
3776 case YANG_REFERENCE:
3777 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &list->ref, Y_STR_ARG, &list->exts);
3778 break;
3779 case YANG_STATUS:
3780 ret = parse_status(ctx, data, &list->flags, &list->exts);
3781 break;
3782 case YANG_WHEN:
3783 ret = parse_when(ctx, data, &list->when);
3784 break;
3785 case YANG_KEY:
3786 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_KEY, 0, &list->key, Y_STR_ARG, &list->exts);
3787 break;
3788 case YANG_MAX_ELEMENTS:
3789 ret = parse_maxelements(ctx, data, &list->max, &list->flags, &list->exts);
3790 break;
3791 case YANG_MIN_ELEMENTS:
3792 ret = parse_minelements(ctx, data, &list->min, &list->flags, &list->exts);
3793 break;
3794 case YANG_ORDERED_BY:
3795 ret = parse_orderedby(ctx, data, &list->flags, &list->exts);
3796 break;
3797 case YANG_UNIQUE:
3798 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_UNIQUE, &list->uniques, Y_STR_ARG, &list->exts);
3799 break;
3800
3801 case YANG_ANYDATA:
3802 case YANG_ANYXML:
3803 ret = parse_any(ctx, data, kw, &list->child);
3804 break;
3805 case YANG_CHOICE:
3806 ret = parse_choice(ctx, data, &list->child);
3807 break;
3808 case YANG_CONTAINER:
3809 ret = parse_container(ctx, data, &list->child);
3810 break;
3811 case YANG_LEAF:
3812 ret = parse_leaf(ctx, data, &list->child);
3813 break;
3814 case YANG_LEAF_LIST:
3815 ret = parse_leaflist(ctx, data, &list->child);
3816 break;
3817 case YANG_LIST:
3818 ret = parse_list(ctx, data, &list->child);
3819 break;
3820 case YANG_USES:
3821 ret = parse_uses(ctx, data, &list->child);
3822 break;
3823
3824 case YANG_TYPEDEF:
3825 ret = parse_typedef(ctx, data, &list->typedefs);
3826 break;
3827 case YANG_MUST:
3828 ret = parse_restrs(ctx, data, kw, &list->musts);
3829 break;
3830 case YANG_ACTION:
3831 ret = parse_action(ctx, data, &list->actions);
3832 break;
3833 case YANG_GROUPING:
3834 ret = parse_grouping(ctx, data, &list->groupings);
3835 break;
3836 case YANG_NOTIFICATION:
3837 ret = parse_notif(ctx, data, &list->notifs);
3838 break;
3839 case YANG_CUSTOM:
3840 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &list->exts);
3841 break;
3842 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003843 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003844 return LY_EVALID;
3845 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003846 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003847 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003848 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003849
3850 return ret;
3851}
3852
Michal Vaskoea5abea2018-09-18 13:10:54 +02003853/**
3854 * @brief Parse the yin-element statement.
3855 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003856 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003857 * @param[in,out] data Data to read from, always moved to currently handled character.
3858 * @param[in,out] flags Flags to write to.
3859 * @param[in,out] exts Extension instances to add to.
3860 *
3861 * @return LY_ERR values.
3862 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003863static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003864parse_yinelement(struct ly_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003865{
3866 LY_ERR ret = 0;
3867 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003868 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003869 enum yang_keyword kw;
3870
3871 if (*flags & LYS_YINELEM_MASK) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02003872 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003873 return LY_EVALID;
3874 }
3875
3876 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003877 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003878 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003879
3880 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
3881 *flags |= LYS_YINELEM_TRUE;
3882 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
3883 *flags |= LYS_YINELEM_FALSE;
3884 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02003885 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003886 free(buf);
3887 return LY_EVALID;
3888 }
3889 free(buf);
3890
3891 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003892 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003893
3894 switch (kw) {
3895 case YANG_CUSTOM:
3896 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_YINELEM, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02003897 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003898 break;
3899 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003900 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003901 return LY_EVALID;
3902 }
3903 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003904 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003905
3906 return ret;
3907}
3908
Michal Vaskoea5abea2018-09-18 13:10:54 +02003909/**
3910 * @brief Parse the yin-element statement.
3911 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003912 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003913 * @param[in,out] data Data to read from, always moved to currently handled character.
3914 * @param[in,out] argument Value to write to.
3915 * @param[in,out] flags Flags to write to.
3916 * @param[in,out] exts Extension instances to add to.
3917 *
3918 * @return LY_ERR values.
3919 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003920static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003921parse_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 +02003922{
3923 LY_ERR ret = 0;
3924 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003925 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003926 enum yang_keyword kw;
3927
3928 if (*argument) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02003929 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003930 return LY_EVALID;
3931 }
3932
3933 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003934 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003935 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003936
Radek Krejci44ceedc2018-10-02 15:54:31 +02003937 INSERT_WORD(ctx, buf, *argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003938 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003939 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003940
3941 switch (kw) {
3942 case YANG_YIN_ELEMENT:
3943 ret = parse_yinelement(ctx, data, flags, exts);
3944 break;
3945 case YANG_CUSTOM:
3946 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_ARGUMENT, 0, exts);
3947 break;
3948 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003949 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003950 return LY_EVALID;
3951 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003952 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003953 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003954 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003955
3956 return ret;
3957}
3958
Michal Vaskoea5abea2018-09-18 13:10:54 +02003959/**
3960 * @brief Parse the extension statement.
3961 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003962 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003963 * @param[in,out] data Data to read from, always moved to currently handled character.
3964 * @param[in,out] extensions Extensions to add to.
3965 *
3966 * @return LY_ERR values.
3967 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003968static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003969parse_extension(struct ly_parser_ctx *ctx, const char **data, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003970{
3971 LY_ERR ret = 0;
3972 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003973 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003974 enum yang_keyword kw;
3975 struct lysp_ext *ex;
3976
3977 LYSP_ARRAY_NEW_RET(ctx, extensions, ex, LY_EMEM);
3978
3979 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003980 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003981 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003982
Radek Krejci44ceedc2018-10-02 15:54:31 +02003983 INSERT_WORD(ctx, buf, ex->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003984 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003985 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003986
3987 switch (kw) {
3988 case YANG_DESCRIPTION:
3989 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &ex->dsc, Y_STR_ARG, &ex->exts);
3990 break;
3991 case YANG_REFERENCE:
3992 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &ex->ref, Y_STR_ARG, &ex->exts);
3993 break;
3994 case YANG_STATUS:
3995 ret = parse_status(ctx, data, &ex->flags, &ex->exts);
3996 break;
3997 case YANG_ARGUMENT:
3998 ret = parse_argument(ctx, data, &ex->argument, &ex->flags, &ex->exts);
3999 break;
4000 case YANG_CUSTOM:
4001 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ex->exts);
4002 break;
4003 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004004 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004005 return LY_EVALID;
4006 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004007 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004008 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004009 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004010
4011 return ret;
4012}
4013
Michal Vaskoea5abea2018-09-18 13:10:54 +02004014/**
4015 * @brief Parse the deviate statement.
4016 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004017 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004018 * @param[in,out] data Data to read from, always moved to currently handled character.
4019 * @param[in,out] deviates Deviates to add to.
4020 *
4021 * @return LY_ERR values.
4022 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004023static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02004024parse_deviate(struct ly_parser_ctx *ctx, const char **data, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004025{
4026 LY_ERR ret = 0;
4027 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004028 size_t word_len, dev_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004029 enum yang_keyword kw;
4030 struct lysp_deviate *iter, *d;
4031 struct lysp_deviate_add *d_add = NULL;
4032 struct lysp_deviate_rpl *d_rpl = NULL;
4033 struct lysp_deviate_del *d_del = NULL;
4034 const char **d_units, ***d_uniques, ***d_dflts;
4035 struct lysp_restr **d_musts;
4036 uint16_t *d_flags;
4037 uint32_t *d_min, *d_max;
4038
4039 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02004040 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02004041 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004042
4043 if ((word_len == 13) && !strncmp(word, "not-supported", word_len)) {
4044 dev_mod = LYS_DEV_NOT_SUPPORTED;
4045 } else if ((word_len == 3) && !strncmp(word, "add", word_len)) {
4046 dev_mod = LYS_DEV_ADD;
4047 } else if ((word_len == 7) && !strncmp(word, "replace", word_len)) {
4048 dev_mod = LYS_DEV_REPLACE;
4049 } else if ((word_len == 6) && !strncmp(word, "delete", word_len)) {
4050 dev_mod = LYS_DEV_DELETE;
4051 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004052 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004053 free(buf);
4054 return LY_EVALID;
4055 }
4056 free(buf);
4057
4058 /* create structure */
4059 switch (dev_mod) {
4060 case LYS_DEV_NOT_SUPPORTED:
4061 d = calloc(1, sizeof *d);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004062 LY_CHECK_ERR_RET(!d, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004063 break;
4064 case LYS_DEV_ADD:
4065 d_add = calloc(1, sizeof *d_add);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004066 LY_CHECK_ERR_RET(!d_add, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004067 d = (struct lysp_deviate *)d_add;
4068 d_units = &d_add->units;
4069 d_uniques = &d_add->uniques;
4070 d_dflts = &d_add->dflts;
4071 d_musts = &d_add->musts;
4072 d_flags = &d_add->flags;
4073 d_min = &d_add->min;
4074 d_max = &d_add->max;
4075 break;
4076 case LYS_DEV_REPLACE:
4077 d_rpl = calloc(1, sizeof *d_rpl);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004078 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004079 d = (struct lysp_deviate *)d_rpl;
4080 d_units = &d_rpl->units;
4081 d_flags = &d_rpl->flags;
4082 d_min = &d_rpl->min;
4083 d_max = &d_rpl->max;
4084 break;
4085 case LYS_DEV_DELETE:
4086 d_del = calloc(1, sizeof *d_del);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004087 LY_CHECK_ERR_RET(!d_del, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004088 d = (struct lysp_deviate *)d_del;
4089 d_units = &d_del->units;
4090 d_uniques = &d_del->uniques;
4091 d_dflts = &d_del->dflts;
4092 d_musts = &d_del->musts;
4093 d_flags = &d_del->flags;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004094 break;
4095 default:
4096 assert(0);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004097 LOGINT_RET(ctx->ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004098 }
4099 d->mod = dev_mod;
4100
4101 /* insert into siblings */
4102 if (!*deviates) {
4103 *deviates = d;
4104 } else {
4105 for (iter = *deviates; iter->next; iter = iter->next);
4106 iter->next = d;
4107 }
4108
4109 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02004110 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004111
4112 switch (kw) {
4113 case YANG_CONFIG:
4114 switch (dev_mod) {
4115 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004116 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004117 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004118 return LY_EVALID;
4119 default:
4120 ret = parse_config(ctx, data, d_flags, &d->exts);
4121 break;
4122 }
4123 break;
4124 case YANG_DEFAULT:
4125 switch (dev_mod) {
4126 case LYS_DEV_NOT_SUPPORTED:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004127 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004128 return LY_EVALID;
4129 case LYS_DEV_REPLACE:
4130 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &d_rpl->dflt, Y_STR_ARG, &d->exts);
4131 break;
4132 default:
4133 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_DEFAULT, d_dflts, Y_STR_ARG, &d->exts);
4134 break;
4135 }
4136 break;
4137 case YANG_MANDATORY:
4138 switch (dev_mod) {
4139 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004140 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004141 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004142 return LY_EVALID;
4143 default:
4144 ret = parse_mandatory(ctx, data, d_flags, &d->exts);
4145 break;
4146 }
4147 break;
4148 case YANG_MAX_ELEMENTS:
4149 switch (dev_mod) {
4150 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004151 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004152 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004153 return LY_EVALID;
4154 default:
4155 ret = parse_maxelements(ctx, data, d_max, d_flags, &d->exts);
4156 break;
4157 }
4158 break;
4159 case YANG_MIN_ELEMENTS:
4160 switch (dev_mod) {
4161 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004162 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004163 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004164 return LY_EVALID;
4165 default:
4166 ret = parse_minelements(ctx, data, d_min, d_flags, &d->exts);
4167 break;
4168 }
4169 break;
4170 case YANG_MUST:
4171 switch (dev_mod) {
4172 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004173 case LYS_DEV_REPLACE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004174 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004175 return LY_EVALID;
4176 default:
4177 ret = parse_restrs(ctx, data, kw, d_musts);
4178 break;
4179 }
4180 break;
4181 case YANG_TYPE:
4182 switch (dev_mod) {
4183 case LYS_DEV_NOT_SUPPORTED:
4184 case LYS_DEV_ADD:
4185 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004186 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004187 return LY_EVALID;
4188 default:
4189 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004190 LY_CHECK_ERR_RET(!d_rpl->type, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004191 ret = parse_type(ctx, data, d_rpl->type);
4192 break;
4193 }
4194 break;
4195 case YANG_UNIQUE:
4196 switch (dev_mod) {
4197 case LYS_DEV_NOT_SUPPORTED:
4198 case LYS_DEV_REPLACE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004199 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004200 return LY_EVALID;
4201 default:
4202 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_UNIQUE, d_uniques, Y_STR_ARG, &d->exts);
4203 break;
4204 }
4205 break;
4206 case YANG_UNITS:
4207 switch (dev_mod) {
4208 case LYS_DEV_NOT_SUPPORTED:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004209 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004210 return LY_EVALID;
4211 default:
4212 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, d_units, Y_STR_ARG, &d->exts);
4213 break;
4214 }
4215 break;
4216 case YANG_CUSTOM:
4217 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &d->exts);
4218 break;
4219 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004220 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004221 return LY_EVALID;
4222 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004223 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004224 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004225 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004226
4227 return ret;
4228}
4229
Michal Vaskoea5abea2018-09-18 13:10:54 +02004230/**
4231 * @brief Parse the deviation statement.
4232 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004233 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004234 * @param[in,out] data Data to read from, always moved to currently handled character.
4235 * @param[in,out] deviations Deviations to add to.
4236 *
4237 * @return LY_ERR values.
4238 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004239static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02004240parse_deviation(struct ly_parser_ctx *ctx, const char **data, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004241{
4242 LY_ERR ret = 0;
4243 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004244 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004245 enum yang_keyword kw;
4246 struct lysp_deviation *dev;
4247
4248 LYSP_ARRAY_NEW_RET(ctx, deviations, dev, LY_EMEM);
4249
4250 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02004251 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02004252 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004253
Radek Krejci44ceedc2018-10-02 15:54:31 +02004254 INSERT_WORD(ctx, buf, dev->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004255 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02004256 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004257
4258 switch (kw) {
4259 case YANG_DESCRIPTION:
4260 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &dev->dsc, Y_STR_ARG, &dev->exts);
4261 break;
4262 case YANG_DEVIATE:
4263 ret = parse_deviate(ctx, data, &dev->deviates);
4264 break;
4265 case YANG_REFERENCE:
4266 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &dev->ref, Y_STR_ARG, &dev->exts);
4267 break;
4268 case YANG_CUSTOM:
4269 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &dev->exts);
4270 break;
4271 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004272 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004273 return LY_EVALID;
4274 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004275 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004276 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004277 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004278
4279 /* mandatory substatements */
4280 if (!dev->deviates) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004281 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004282 return LY_EVALID;
4283 }
4284
4285 return ret;
4286}
4287
Michal Vaskoea5abea2018-09-18 13:10:54 +02004288/**
4289 * @brief Parse the feature statement.
4290 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004291 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004292 * @param[in,out] data Data to read from, always moved to currently handled character.
4293 * @param[in,out] features Features to add to.
4294 *
4295 * @return LY_ERR values.
4296 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004297static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02004298parse_feature(struct ly_parser_ctx *ctx, const char **data, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004299{
4300 LY_ERR ret = 0;
4301 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004302 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004303 enum yang_keyword kw;
4304 struct lysp_feature *feat;
4305
4306 LYSP_ARRAY_NEW_RET(ctx, features, feat, LY_EMEM);
4307
4308 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02004309 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02004310 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004311
Radek Krejci44ceedc2018-10-02 15:54:31 +02004312 INSERT_WORD(ctx, buf, feat->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004313 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02004314 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004315
4316 switch (kw) {
4317 case YANG_DESCRIPTION:
4318 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &feat->dsc, Y_STR_ARG, &feat->exts);
4319 break;
4320 case YANG_IF_FEATURE:
4321 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &feat->iffeatures, Y_STR_ARG, &feat->exts);
4322 break;
4323 case YANG_REFERENCE:
4324 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &feat->ref, Y_STR_ARG, &feat->exts);
4325 break;
4326 case YANG_STATUS:
4327 ret = parse_status(ctx, data, &feat->flags, &feat->exts);
4328 break;
4329 case YANG_CUSTOM:
4330 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &feat->exts);
4331 break;
4332 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004333 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "feature");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004334 return LY_EMEM;
4335 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004336 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004337 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004338 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004339
4340 return ret;
4341}
4342
Michal Vaskoea5abea2018-09-18 13:10:54 +02004343/**
4344 * @brief Parse the identity statement.
4345 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004346 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004347 * @param[in,out] data Data to read from, always moved to currently handled character.
4348 * @param[in,out] identities Identities to add to.
4349 *
4350 * @return LY_ERR values.
4351 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004352static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02004353parse_identity(struct ly_parser_ctx *ctx, const char **data, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004354{
4355 LY_ERR ret = 0;
4356 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004357 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004358 enum yang_keyword kw;
4359 struct lysp_ident *ident;
4360
4361 LYSP_ARRAY_NEW_RET(ctx, identities, ident, LY_EMEM);
4362
4363 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02004364 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02004365 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004366
Radek Krejci44ceedc2018-10-02 15:54:31 +02004367 INSERT_WORD(ctx, buf, ident->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004368 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02004369 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004370
4371 switch (kw) {
4372 case YANG_DESCRIPTION:
4373 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &ident->dsc, Y_STR_ARG, &ident->exts);
4374 break;
4375 case YANG_IF_FEATURE:
4376 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &ident->iffeatures, Y_STR_ARG, &ident->exts);
4377 break;
4378 case YANG_REFERENCE:
4379 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &ident->ref, Y_STR_ARG, &ident->exts);
4380 break;
4381 case YANG_STATUS:
4382 ret = parse_status(ctx, data, &ident->flags, &ident->exts);
4383 break;
4384 case YANG_BASE:
4385 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_BASE, &ident->bases, Y_PREF_IDENTIF_ARG, &ident->exts);
4386 break;
4387 case YANG_CUSTOM:
4388 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ident->exts);
4389 break;
4390 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004391 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004392 return LY_EVALID;
4393 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004394 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004395 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004396
4397 return ret;
4398}
4399
Michal Vaskoea5abea2018-09-18 13:10:54 +02004400/**
4401 * @brief Parse the module or submodule statement.
4402 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004403 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004404 * @param[in,out] data Data to read from, always moved to currently handled character.
4405 * @param[in,out] mod Module to write to.
4406 *
4407 * @return LY_ERR values.
4408 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004409static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02004410parse_sub_module(struct ly_parser_ctx *ctx, const char **data, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004411{
4412 LY_ERR ret = 0;
4413 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004414 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004415 enum yang_keyword kw, prev_kw = 0;
4416 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
4417
4418 /* (sub)module name */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02004419 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02004420 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004421
Radek Krejci44ceedc2018-10-02 15:54:31 +02004422 INSERT_WORD(ctx, buf, mod->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004423 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02004424 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004425
Radek Krejcie3846472018-10-15 15:24:51 +02004426#define CHECK_ORDER(SECTION) \
4427 if (mod_stmt > SECTION) {LOGVAL_YANG(ctx, LY_VCODE_INORD, ly_stmt2str(kw), ly_stmt2str(prev_kw)); return LY_EVALID;}mod_stmt = SECTION
4428
Michal Vasko7fbc8162018-09-17 10:35:16 +02004429 switch (kw) {
4430 /* module header */
4431 case YANG_NAMESPACE:
4432 case YANG_PREFIX:
4433 if (mod->submodule) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004434 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004435 return LY_EVALID;
4436 }
Radek Krejcie3846472018-10-15 15:24:51 +02004437 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4438 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004439 case YANG_BELONGS_TO:
Radek Krejcie3846472018-10-15 15:24:51 +02004440 if (!mod->submodule) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004441 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004442 return LY_EVALID;
4443 }
Radek Krejcie3846472018-10-15 15:24:51 +02004444 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4445 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004446 case YANG_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004447 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004448 break;
4449 /* linkage */
4450 case YANG_INCLUDE:
4451 case YANG_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004452 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004453 break;
4454 /* meta */
4455 case YANG_ORGANIZATION:
4456 case YANG_CONTACT:
4457 case YANG_DESCRIPTION:
4458 case YANG_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004459 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004460 break;
4461
4462 /* revision */
4463 case YANG_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004464 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004465 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004466 /* body */
4467 case YANG_ANYDATA:
4468 case YANG_ANYXML:
4469 case YANG_AUGMENT:
4470 case YANG_CHOICE:
4471 case YANG_CONTAINER:
4472 case YANG_DEVIATION:
4473 case YANG_EXTENSION:
4474 case YANG_FEATURE:
4475 case YANG_GROUPING:
4476 case YANG_IDENTITY:
4477 case YANG_LEAF:
4478 case YANG_LEAF_LIST:
4479 case YANG_LIST:
4480 case YANG_NOTIFICATION:
4481 case YANG_RPC:
4482 case YANG_TYPEDEF:
4483 case YANG_USES:
4484 case YANG_CUSTOM:
4485 mod_stmt = Y_MOD_BODY;
4486 break;
4487 default:
4488 /* error handled in the next switch */
4489 break;
4490 }
Radek Krejcie3846472018-10-15 15:24:51 +02004491#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004492
Radek Krejcie3846472018-10-15 15:24:51 +02004493 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004494 switch (kw) {
4495 /* module header */
4496 case YANG_YANG_VERSION:
4497 ret = parse_yangversion(ctx, data, mod);
4498 break;
4499 case YANG_NAMESPACE:
4500 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_NAMESPACE, 0, &mod->ns, Y_STR_ARG, &mod->exts);
4501 break;
4502 case YANG_PREFIX:
4503 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_PREFIX, 0, &mod->prefix, Y_IDENTIF_ARG, &mod->exts);
Radek Krejci70853c52018-10-15 14:46:16 +02004504 LY_CHECK_RET(lysp_check_prefix(ctx, mod, &mod->prefix), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004505 break;
4506 case YANG_BELONGS_TO:
4507 ret = parse_belongsto(ctx, data, &mod->belongsto, &mod->prefix, &mod->exts);
4508 break;
4509
4510 /* linkage */
4511 case YANG_INCLUDE:
4512 ret = parse_include(ctx, data, &mod->includes);
4513 break;
4514 case YANG_IMPORT:
Radek Krejci70853c52018-10-15 14:46:16 +02004515 ret = parse_import(ctx, data, mod);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004516 break;
4517
4518 /* meta */
4519 case YANG_ORGANIZATION:
4520 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_ORGANIZATION, 0, &mod->org, Y_STR_ARG, &mod->exts);
4521 break;
4522 case YANG_CONTACT:
4523 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_CONTACT, 0, &mod->contact, Y_STR_ARG, &mod->exts);
4524 break;
4525 case YANG_DESCRIPTION:
4526 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &mod->dsc, Y_STR_ARG, &mod->exts);
4527 break;
4528 case YANG_REFERENCE:
4529 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &mod->ref, Y_STR_ARG, &mod->exts);
4530 break;
4531
4532 /* revision */
4533 case YANG_REVISION:
4534 ret = parse_revision(ctx, data, &mod->revs);
4535 break;
4536
4537 /* body */
4538 case YANG_ANYDATA:
4539 case YANG_ANYXML:
4540 ret = parse_any(ctx, data, kw, &mod->data);
4541 break;
4542 case YANG_CHOICE:
4543 ret = parse_choice(ctx, data, &mod->data);
4544 break;
4545 case YANG_CONTAINER:
4546 ret = parse_container(ctx, data, &mod->data);
4547 break;
4548 case YANG_LEAF:
4549 ret = parse_leaf(ctx, data, &mod->data);
4550 break;
4551 case YANG_LEAF_LIST:
4552 ret = parse_leaflist(ctx, data, &mod->data);
4553 break;
4554 case YANG_LIST:
4555 ret = parse_list(ctx, data, &mod->data);
4556 break;
4557 case YANG_USES:
4558 ret = parse_uses(ctx, data, &mod->data);
4559 break;
4560
4561 case YANG_AUGMENT:
4562 ret = parse_augment(ctx, data, &mod->augments);
4563 break;
4564 case YANG_DEVIATION:
4565 ret = parse_deviation(ctx, data, &mod->deviations);
4566 break;
4567 case YANG_EXTENSION:
4568 ret = parse_extension(ctx, data, &mod->extensions);
4569 break;
4570 case YANG_FEATURE:
4571 ret = parse_feature(ctx, data, &mod->features);
4572 break;
4573 case YANG_GROUPING:
4574 ret = parse_grouping(ctx, data, &mod->groupings);
4575 break;
4576 case YANG_IDENTITY:
4577 ret = parse_identity(ctx, data, &mod->identities);
4578 break;
4579 case YANG_NOTIFICATION:
4580 ret = parse_notif(ctx, data, &mod->notifs);
4581 break;
4582 case YANG_RPC:
4583 ret = parse_action(ctx, data, &mod->rpcs);
4584 break;
4585 case YANG_TYPEDEF:
4586 ret = parse_typedef(ctx, data, &mod->typedefs);
4587 break;
4588 case YANG_CUSTOM:
4589 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &mod->exts);
4590 break;
4591
4592 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004593 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), mod->submodule ? "submodule" : "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004594 return LY_EVALID;
4595 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004596 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004597 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004598 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004599
4600 /* mandatory substatements */
4601 if (mod->submodule) {
4602 if (!mod->belongsto) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004603 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004604 return LY_EVALID;
4605 }
4606 } else {
4607 if (!mod->ns) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004608 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004609 return LY_EVALID;
4610 } else if (!mod->prefix) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004611 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004612 return LY_EVALID;
4613 }
4614 }
4615
4616 return ret;
4617}
4618
Radek Krejcid4557c62018-09-17 11:42:09 +02004619LY_ERR
Michal Vasko7fbc8162018-09-17 10:35:16 +02004620yang_parse(struct ly_ctx *ctx, const char *data, struct lysp_module **mod_p)
4621{
4622 LY_ERR ret = 0;
4623 char *word, *buf;
Radek Krejciefd22f62018-09-27 11:47:58 +02004624 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004625 enum yang_keyword kw;
Radek Krejci0c2cf322018-10-13 08:02:30 +02004626 struct lysp_module *mod = NULL;
Radek Krejci44ceedc2018-10-02 15:54:31 +02004627 struct ly_parser_ctx context;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004628
Radek Krejci44ceedc2018-10-02 15:54:31 +02004629 context.ctx = ctx;
4630 context.line = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004631
4632 /* "module"/"submodule" */
Radek Krejci44ceedc2018-10-02 15:54:31 +02004633 ret = get_keyword(&context, &data, &kw, &word, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02004634 LY_CHECK_GOTO(ret, error);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004635
4636 if ((kw != YANG_MODULE) && (kw != YANG_SUBMODULE)) {
Radek Krejcifc62d7e2018-10-11 12:56:42 +02004637 LOGVAL_YANG(&context, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".",
Radek Krejcic59bc972018-09-17 16:13:06 +02004638 ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004639 goto error;
4640 }
4641
4642 mod = calloc(1, sizeof *mod);
4643 LY_CHECK_ERR_GOTO(!mod, LOGMEM(ctx), error);
4644 if (kw == YANG_SUBMODULE) {
4645 mod->submodule = 1;
4646 }
Radek Krejci9fcacc12018-10-11 15:59:11 +02004647 mod->ctx = ctx;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004648
4649 /* substatements */
Radek Krejci44ceedc2018-10-02 15:54:31 +02004650 ret = parse_sub_module(&context, &data, mod);
Radek Krejcic59bc972018-09-17 16:13:06 +02004651 LY_CHECK_GOTO(ret, error);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004652
4653 /* read some trailing spaces or new lines */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02004654 ret = get_argument(&context, &data, Y_MAYBE_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02004655 LY_CHECK_GOTO(ret, error);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004656
4657 if (word) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004658 LOGVAL_YANG(&context, LYVE_SYNTAX, "Invalid character sequence \"%.*s\", expected end-of-file.",
Michal Vasko7fbc8162018-09-17 10:35:16 +02004659 word_len, word);
4660 free(buf);
4661 goto error;
4662 }
4663 assert(!buf);
4664
4665 *mod_p = mod;
4666 return ret;
4667
4668error:
Radek Krejci9fcacc12018-10-11 15:59:11 +02004669 lysp_module_free(mod);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004670 return ret;
4671}