blob: 434073d08767c0a3d11159fca13856e09bcc8040 [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
Radek Krejci2c4e7172018-10-19 15:56:26 +02001039 LY_ARRAY_NEW_RET(ctx->ctx, *exts, e, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001040
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
Radek Krejci2c4e7172018-10-19 15:56:26 +02001302 LY_ARRAY_NEW_RET(ctx->ctx, *includes, inc, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001303
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 Krejci2c4e7172018-10-19 15:56:26 +02001354 LY_ARRAY_NEW_RET(ctx->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
Radek Krejci2c4e7172018-10-19 15:56:26 +02001416 LY_ARRAY_NEW_RET(ctx->ctx, *revs, rev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001417
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 Krejci151a5b72018-10-19 14:21:44 +02001472 const char **item;
1473 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001474 enum yang_keyword kw;
1475
1476 /* allocate new pointer */
Radek Krejci2c4e7172018-10-19 15:56:26 +02001477 LY_ARRAY_NEW_RET(ctx->ctx, *texts, item, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001478
1479 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001480 ret = get_argument(ctx, data, arg, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001481 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001482
Radek Krejci151a5b72018-10-19 14:21:44 +02001483 INSERT_WORD(ctx, buf, *item, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001484 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001485 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001486
1487 switch (kw) {
1488 case YANG_CUSTOM:
Radek Krejci151a5b72018-10-19 14:21:44 +02001489 ret = parse_ext(ctx, data, word, word_len, substmt, LY_ARRAY_SIZE(*texts) - 1, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02001490 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001491 break;
1492 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001493 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001494 return LY_EVALID;
1495 }
1496 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001497 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001498
1499 return ret;
1500}
1501
Michal Vaskoea5abea2018-09-18 13:10:54 +02001502/**
1503 * @brief Parse the config statement.
1504 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001505 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001506 * @param[in,out] data Data to read from, always moved to currently handled character.
1507 * @param[in,out] flags Flags to add to.
1508 * @param[in,out] exts Extension instances to add to.
1509 *
1510 * @return LY_ERR values.
1511 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001512static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001513parse_config(struct ly_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001514{
1515 LY_ERR ret = 0;
1516 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001517 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001518 enum yang_keyword kw;
1519
1520 if (*flags & LYS_CONFIG_MASK) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001521 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001522 return LY_EVALID;
1523 }
1524
1525 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001526 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001527 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001528
1529 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1530 *flags |= LYS_CONFIG_W;
1531 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1532 *flags |= LYS_CONFIG_R;
1533 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001534 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001535 free(buf);
1536 return LY_EVALID;
1537 }
1538 free(buf);
1539
1540 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001541 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001542
1543 switch (kw) {
1544 case YANG_CUSTOM:
1545 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_CONFIG, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02001546 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001547 break;
1548 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001549 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001550 return LY_EVALID;
1551 }
1552 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001553 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001554
1555 return ret;
1556}
1557
Michal Vaskoea5abea2018-09-18 13:10:54 +02001558/**
1559 * @brief Parse the mandatory statement.
1560 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001561 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001562 * @param[in,out] data Data to read from, always moved to currently handled character.
1563 * @param[in,out] flags Flags to add to.
1564 * @param[in,out] exts Extension instances to add to.
1565 *
1566 * @return LY_ERR values.
1567 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001568static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001569parse_mandatory(struct ly_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001570{
1571 LY_ERR ret = 0;
1572 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001573 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001574 enum yang_keyword kw;
1575
1576 if (*flags & LYS_MAND_MASK) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001577 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001578 return LY_EVALID;
1579 }
1580
1581 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001582 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001583 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001584
1585 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1586 *flags |= LYS_MAND_TRUE;
1587 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1588 *flags |= LYS_MAND_FALSE;
1589 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001590 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001591 free(buf);
1592 return LY_EVALID;
1593 }
1594 free(buf);
1595
1596 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001597 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001598
1599 switch (kw) {
1600 case YANG_CUSTOM:
1601 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MANDATORY, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02001602 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001603 break;
1604 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001605 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001606 return LY_EVALID;
1607 }
1608 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001609 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001610
1611 return ret;
1612}
1613
Michal Vaskoea5abea2018-09-18 13:10:54 +02001614/**
1615 * @brief Parse a restriction such as range or length.
1616 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001617 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001618 * @param[in,out] data Data to read from, always moved to currently handled character.
1619 * @param[in] restr_kw Type of this particular restriction.
1620 * @param[in,out] exts Extension instances to add to.
1621 *
1622 * @return LY_ERR values.
1623 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001624static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001625parse_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 +02001626{
1627 LY_ERR ret = 0;
1628 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001629 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001630 enum yang_keyword kw;
1631
1632 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001633 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001634 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001635
Radek Krejci44ceedc2018-10-02 15:54:31 +02001636 INSERT_WORD(ctx, buf, restr->arg, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001637 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001638 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001639
1640 switch (kw) {
1641 case YANG_DESCRIPTION:
1642 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts);
1643 break;
1644 case YANG_REFERENCE:
1645 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts);
1646 break;
1647 case YANG_ERROR_APP_TAG:
1648 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRTAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts);
1649 break;
1650 case YANG_ERROR_MESSAGE:
1651 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRMSG, 0, &restr->emsg, Y_STR_ARG, &restr->exts);
1652 break;
1653 case YANG_CUSTOM:
1654 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts);
1655 break;
1656 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001657 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001658 return LY_EVALID;
1659 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001660 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001661 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001662 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001663
1664 return ret;
1665}
1666
Michal Vaskoea5abea2018-09-18 13:10:54 +02001667/**
1668 * @brief Parse a restriction that can have more instances such as must.
1669 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001670 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001671 * @param[in,out] data Data to read from, always moved to currently handled character.
1672 * @param[in] restr_kw Type of this particular restriction.
1673 * @param[in,out] restrs Restrictions to add to.
1674 *
1675 * @return LY_ERR values.
1676 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001677static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001678parse_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 +02001679{
1680 struct lysp_restr *restr;
1681
Radek Krejci2c4e7172018-10-19 15:56:26 +02001682 LY_ARRAY_NEW_RET(ctx->ctx, *restrs, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001683
1684 return parse_restr(ctx, data, restr_kw, restr);
1685}
1686
Michal Vaskoea5abea2018-09-18 13:10:54 +02001687/**
1688 * @brief Parse the status statement.
1689 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001690 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001691 * @param[in,out] data Data to read from, always moved to currently handled character.
1692 * @param[in,out] flags Flags to add to.
1693 * @param[in,out] exts Extension instances to add to.
1694 *
1695 * @return LY_ERR values.
1696 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001697static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001698parse_status(struct ly_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001699{
1700 LY_ERR ret = 0;
1701 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001702 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001703 enum yang_keyword kw;
1704
1705 if (*flags & LYS_STATUS_MASK) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001706 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001707 return LY_EVALID;
1708 }
1709
1710 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001711 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001712 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001713
1714 if ((word_len == 7) && !strncmp(word, "current", word_len)) {
1715 *flags |= LYS_STATUS_CURR;
1716 } else if ((word_len == 10) && !strncmp(word, "deprecated", word_len)) {
1717 *flags |= LYS_STATUS_DEPRC;
1718 } else if ((word_len == 8) && !strncmp(word, "obsolete", word_len)) {
1719 *flags |= LYS_STATUS_OBSLT;
1720 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001721 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001722 free(buf);
1723 return LY_EVALID;
1724 }
1725 free(buf);
1726
1727 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001728 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001729
1730 switch (kw) {
1731 case YANG_CUSTOM:
1732 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_STATUS, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02001733 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001734 break;
1735 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001736 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001737 return LY_EVALID;
1738 }
1739 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001740 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001741
1742 return ret;
1743}
1744
Michal Vaskoea5abea2018-09-18 13:10:54 +02001745/**
1746 * @brief Parse the when statement.
1747 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001748 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001749 * @param[in,out] data Data to read from, always moved to currently handled character.
1750 * @param[in,out] when_p When pointer to parse to.
1751 *
1752 * @return LY_ERR values.
1753 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001754static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001755parse_when(struct ly_parser_ctx *ctx, const char **data, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001756{
1757 LY_ERR ret = 0;
1758 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001759 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001760 enum yang_keyword kw;
1761 struct lysp_when *when;
1762
1763 if (*when_p) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001764 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001765 return LY_EVALID;
1766 }
1767
1768 when = calloc(1, sizeof *when);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001769 LY_CHECK_ERR_RET(!when, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001770 *when_p = when;
1771
1772 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001773 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001774 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001775
Radek Krejci44ceedc2018-10-02 15:54:31 +02001776 INSERT_WORD(ctx, buf, when->cond, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001777 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001778 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001779
1780 switch (kw) {
1781 case YANG_DESCRIPTION:
1782 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &when->dsc, Y_STR_ARG, &when->exts);
1783 break;
1784 case YANG_REFERENCE:
1785 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &when->ref, Y_STR_ARG, &when->exts);
1786 break;
1787 case YANG_CUSTOM:
1788 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &when->exts);
1789 break;
1790 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001791 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001792 return LY_EVALID;
1793 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001794 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001795 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001796 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001797
1798 return ret;
1799}
1800
Michal Vaskoea5abea2018-09-18 13:10:54 +02001801/**
1802 * @brief Parse the anydata or anyxml statement.
1803 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001804 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001805 * @param[in,out] data Data to read from, always moved to currently handled character.
1806 * @param[in] kw Type of this particular keyword.
1807 * @param[in,out] siblings Siblings to add to.
1808 *
1809 * @return LY_ERR values.
1810 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001811static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001812parse_any(struct ly_parser_ctx *ctx, const char **data, enum yang_keyword kw, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001813{
1814 LY_ERR ret = 0;
1815 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001816 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001817 struct lysp_node *iter;
1818 struct lysp_node_anydata *any;
1819
1820 /* create structure */
1821 any = calloc(1, sizeof *any);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001822 LY_CHECK_ERR_RET(!any, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001823 any->nodetype = kw == YANG_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
1824
1825 /* insert into siblings */
1826 if (!*siblings) {
1827 *siblings = (struct lysp_node *)any;
1828 } else {
1829 for (iter = *siblings; iter->next; iter = iter->next);
1830 iter->next = (struct lysp_node *)any;
1831 }
1832
1833 /* get name */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001834 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001835 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001836
Radek Krejci44ceedc2018-10-02 15:54:31 +02001837 INSERT_WORD(ctx, buf, any->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001838
1839 /* parse substatements */
1840 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001841 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001842
1843 switch (kw) {
1844 case YANG_CONFIG:
1845 ret = parse_config(ctx, data, &any->flags, &any->exts);
1846 break;
1847 case YANG_DESCRIPTION:
1848 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &any->dsc, Y_STR_ARG, &any->exts);
1849 break;
1850 case YANG_IF_FEATURE:
1851 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &any->iffeatures, Y_STR_ARG, &any->exts);
1852 break;
1853 case YANG_MANDATORY:
1854 ret = parse_mandatory(ctx, data, &any->flags, &any->exts);
1855 break;
1856 case YANG_MUST:
1857 ret = parse_restrs(ctx, data, kw, &any->musts);
1858 break;
1859 case YANG_REFERENCE:
1860 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &any->ref, Y_STR_ARG, &any->exts);
1861 break;
1862 case YANG_STATUS:
1863 ret = parse_status(ctx, data, &any->flags, &any->exts);
1864 break;
1865 case YANG_WHEN:
1866 ret = parse_when(ctx, data, &any->when);
1867 break;
1868 case YANG_CUSTOM:
1869 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &any->exts);
1870 break;
1871 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001872 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
Radek Krejcic59bc972018-09-17 16:13:06 +02001873 (any->nodetype & LYS_ANYDATA) == LYS_ANYDATA ? ly_stmt2str(YANG_ANYDATA) : ly_stmt2str(YANG_ANYXML));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001874 return LY_EVALID;
1875 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001876 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001877 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001878 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001879
1880 return ret;
1881}
1882
Michal Vaskoea5abea2018-09-18 13:10:54 +02001883/**
1884 * @brief Parse the value or position statement. Substatement of type enum statement.
1885 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001886 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001887 * @param[in,out] data Data to read from, always moved to currently handled character.
1888 * @param[in] val_kw Type of this particular keyword.
1889 * @param[in,out] value Value to write to.
1890 * @param[in,out] flags Flags to write to.
1891 * @param[in,out] exts Extension instances to add to.
1892 *
1893 * @return LY_ERR values.
1894 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001895static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001896parse_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 +02001897 struct lysp_ext_instance **exts)
1898{
1899 LY_ERR ret = 0;
1900 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001901 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001902 long int num;
1903 unsigned long int unum;
1904 enum yang_keyword kw;
1905
1906 if (*flags & LYS_SET_VALUE) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001907 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001908 return LY_EVALID;
1909 }
1910 *flags |= LYS_SET_VALUE;
1911
1912 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001913 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001914 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001915
1916 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 +02001917 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001918 free(buf);
1919 return LY_EVALID;
1920 }
1921
1922 errno = 0;
1923 if (val_kw == YANG_VALUE) {
1924 num = strtol(word, &ptr, 10);
1925 } else {
1926 unum = strtoul(word, &ptr, 10);
1927 }
1928 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001929 if ((size_t)(ptr - word) != word_len) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001930 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001931 free(buf);
1932 return LY_EVALID;
1933 }
1934 if (errno == ERANGE) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001935 LOGVAL_YANG(ctx, LY_VCODE_OOB, word_len, word, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001936 free(buf);
1937 return LY_EVALID;
1938 }
1939 if (val_kw == YANG_VALUE) {
1940 *value = num;
1941 } else {
1942 *value = unum;
1943 }
1944 free(buf);
1945
1946 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001947 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001948
1949 switch (kw) {
1950 case YANG_CUSTOM:
1951 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 +02001952 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001953 break;
1954 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001955 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001956 return LY_EVALID;
1957 }
1958 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001959 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001960
1961 return ret;
1962}
1963
Michal Vaskoea5abea2018-09-18 13:10:54 +02001964/**
1965 * @brief Parse the enum or bit statement. Substatement of type statement.
1966 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001967 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001968 * @param[in,out] data Data to read from, always moved to currently handled character.
1969 * @param[in] enum_kw Type of this particular keyword.
1970 * @param[in,out] enums Enums or bits to add to.
1971 *
1972 * @return LY_ERR values.
1973 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001974static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001975parse_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 +02001976{
1977 LY_ERR ret = 0;
1978 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001979 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001980 enum yang_keyword kw;
1981 struct lysp_type_enum *enm;
1982
Radek Krejci2c4e7172018-10-19 15:56:26 +02001983 LY_ARRAY_NEW_RET(ctx->ctx, *enums, enm, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001984
1985 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001986 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001987 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001988
Radek Krejci44ceedc2018-10-02 15:54:31 +02001989 INSERT_WORD(ctx, buf, enm->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001990 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001991 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001992
1993 switch (kw) {
1994 case YANG_DESCRIPTION:
1995 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &enm->dsc, Y_STR_ARG, &enm->exts);
1996 break;
1997 case YANG_IF_FEATURE:
1998 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &enm->iffeatures, Y_STR_ARG, &enm->exts);
1999 break;
2000 case YANG_REFERENCE:
2001 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &enm->ref, Y_STR_ARG, &enm->exts);
2002 break;
2003 case YANG_STATUS:
2004 ret = parse_status(ctx, data, &enm->flags, &enm->exts);
2005 break;
2006 case YANG_VALUE:
2007 case YANG_POSITION:
2008 ret = parse_type_enum_value_pos(ctx, data, kw, &enm->value, &enm->flags, &enm->exts);
2009 break;
2010 case YANG_CUSTOM:
2011 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &enm->exts);
2012 break;
2013 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002014 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002015 return LY_EVALID;
2016 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002017 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002018 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002019 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002020
2021 return ret;
2022}
2023
Michal Vaskoea5abea2018-09-18 13:10:54 +02002024/**
2025 * @brief Parse the fraction-digits statement. Substatement of type statement.
2026 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002027 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002028 * @param[in,out] data Data to read from, always moved to currently handled character.
2029 * @param[in,out] fracdig Value to write to.
2030 * @param[in,out] exts Extension instances to add to.
2031 *
2032 * @return LY_ERR values.
2033 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002034static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002035parse_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 +02002036{
2037 LY_ERR ret = 0;
2038 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002039 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002040 unsigned long int num;
2041 enum yang_keyword kw;
2042
2043 if (*fracdig) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002044 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002045 return LY_EVALID;
2046 }
2047
2048 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002049 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002050 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002051
2052 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002053 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002054 free(buf);
2055 return LY_EVALID;
2056 }
2057
2058 errno = 0;
2059 num = strtoul(word, &ptr, 10);
2060 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002061 if ((size_t)(ptr - word) != word_len) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002062 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002063 free(buf);
2064 return LY_EVALID;
2065 }
2066 if ((errno == ERANGE) || (num > 18)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002067 LOGVAL_YANG(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002068 free(buf);
2069 return LY_EVALID;
2070 }
2071 *fracdig = num;
2072 free(buf);
2073
2074 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002075 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002076
2077 switch (kw) {
2078 case YANG_CUSTOM:
2079 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_FRACDIGITS, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02002080 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002081 break;
2082 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002083 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002084 return LY_EVALID;
2085 }
2086 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002087 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002088
2089 return ret;
2090}
2091
Michal Vaskoea5abea2018-09-18 13:10:54 +02002092/**
2093 * @brief Parse the require-instance statement. Substatement of type statement.
2094 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002095 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002096 * @param[in,out] data Data to read from, always moved to currently handled character.
2097 * @param[in,out] reqinst Value to write to.
2098 * @param[in,out] flags Flags to write to.
2099 * @param[in,out] exts Extension instances to add to.
2100 *
2101 * @return LY_ERR values.
2102 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002103static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002104parse_type_reqinstance(struct ly_parser_ctx *ctx, const char **data, uint8_t *reqinst, uint16_t *flags,
Michal Vasko7fbc8162018-09-17 10:35:16 +02002105 struct lysp_ext_instance **exts)
2106{
2107 LY_ERR ret = 0;
2108 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002109 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002110 enum yang_keyword kw;
2111
2112 if (*flags & LYS_SET_REQINST) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002113 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002114 return LY_EVALID;
2115 }
2116 *flags |= LYS_SET_REQINST;
2117
2118 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002119 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002120 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002121
2122 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
2123 *reqinst = 1;
2124 } else if ((word_len != 5) || strncmp(word, "false", word_len)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002125 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002126 free(buf);
2127 return LY_EVALID;
2128 }
2129 free(buf);
2130
2131 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002132 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002133
2134 switch (kw) {
2135 case YANG_CUSTOM:
2136 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_REQINSTANCE, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02002137 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002138 break;
2139 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002140 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002141 return LY_EVALID;
2142 }
2143 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002144 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002145
2146 return ret;
2147}
2148
Michal Vaskoea5abea2018-09-18 13:10:54 +02002149/**
2150 * @brief Parse the modifier statement. Substatement of type pattern statement.
2151 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002152 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002153 * @param[in,out] data Data to read from, always moved to currently handled character.
2154 * @param[in,out] pat Value to write to.
2155 * @param[in,out] exts Extension instances to add to.
2156 *
2157 * @return LY_ERR values.
2158 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002159static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002160parse_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 +02002161{
2162 LY_ERR ret = 0;
2163 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002164 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002165 enum yang_keyword kw;
2166
2167 if ((*pat)[0] == 0x15) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002168 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002169 return LY_EVALID;
2170 }
2171
2172 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002173 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002174 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002175
2176 if ((word_len != 12) || strncmp(word, "invert-match", word_len)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002177 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002178 free(buf);
2179 return LY_EVALID;
2180 }
2181 free(buf);
2182
2183 /* replace the value in the dictionary */
2184 buf = malloc(strlen(*pat) + 1);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002185 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002186 strcpy(buf, *pat);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002187 lydict_remove(ctx->ctx, *pat);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002188
2189 assert(buf[0] == 0x06);
2190 buf[0] = 0x15;
Radek Krejci44ceedc2018-10-02 15:54:31 +02002191 *pat = lydict_insert_zc(ctx->ctx, buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002192
2193 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002194 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002195
2196 switch (kw) {
2197 case YANG_CUSTOM:
2198 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MODIFIER, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02002199 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002200 break;
2201 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002202 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002203 return LY_EVALID;
2204 }
2205 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002206 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002207
2208 return ret;
2209}
2210
Michal Vaskoea5abea2018-09-18 13:10:54 +02002211/**
2212 * @brief Parse the pattern statement. Substatement of type statement.
2213 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002214 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002215 * @param[in,out] data Data to read from, always moved to currently handled character.
2216 * @param[in,out] patterns Restrictions to add to.
2217 *
2218 * @return LY_ERR values.
2219 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002220static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002221parse_type_pattern(struct ly_parser_ctx *ctx, const char **data, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002222{
2223 LY_ERR ret = 0;
2224 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002225 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002226 enum yang_keyword kw;
2227 struct lysp_restr *restr;
2228
Radek Krejci2c4e7172018-10-19 15:56:26 +02002229 LY_ARRAY_NEW_RET(ctx->ctx, *patterns, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002230
2231 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002232 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002233 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002234
2235 /* add special meaning first byte */
2236 if (buf) {
2237 buf = realloc(buf, word_len + 2);
2238 word = buf;
2239 } else {
2240 buf = malloc(word_len + 2);
2241 }
Radek Krejci44ceedc2018-10-02 15:54:31 +02002242 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejci86d106e2018-10-18 09:53:19 +02002243 memmove(buf + 1, word, word_len);
2244 buf[0] = 0x06; /* pattern's default regular-match flag */
2245 buf[word_len + 1] = '\0'; /* terminating NULL byte */
2246 restr->arg = lydict_insert_zc(ctx->ctx, buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002247
2248 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002249 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002250
2251 switch (kw) {
2252 case YANG_DESCRIPTION:
2253 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts);
2254 break;
2255 case YANG_REFERENCE:
2256 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts);
2257 break;
2258 case YANG_ERROR_APP_TAG:
2259 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRTAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts);
2260 break;
2261 case YANG_ERROR_MESSAGE:
2262 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRMSG, 0, &restr->emsg, Y_STR_ARG, &restr->exts);
2263 break;
2264 case YANG_MODIFIER:
2265 ret = parse_type_pattern_modifier(ctx, data, &restr->arg, &restr->exts);
2266 break;
2267 case YANG_CUSTOM:
2268 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts);
2269 break;
2270 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002271 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002272 return LY_EVALID;
2273 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002274 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002275 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002276 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002277
2278 return ret;
2279}
2280
Michal Vaskoea5abea2018-09-18 13:10:54 +02002281/**
2282 * @brief Parse the type statement.
2283 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002284 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002285 * @param[in,out] data Data to read from, always moved to currently handled character.
2286 * @param[in,out] type Type to wrote to.
2287 *
2288 * @return LY_ERR values.
2289 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002290static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002291parse_type(struct ly_parser_ctx *ctx, const char **data, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002292{
2293 LY_ERR ret = 0;
2294 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002295 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002296 enum yang_keyword kw;
2297 struct lysp_type *nest_type;
2298
2299 if (type->name) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002300 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002301 return LY_EVALID;
2302 }
2303
2304 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002305 ret = get_argument(ctx, data, Y_PREF_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002306 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002307
Radek Krejci44ceedc2018-10-02 15:54:31 +02002308 INSERT_WORD(ctx, buf, type->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002309 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002310 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002311
2312 switch (kw) {
2313 case YANG_BASE:
2314 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_BASE, &type->bases, Y_PREF_IDENTIF_ARG, &type->exts);
2315 break;
2316 case YANG_BIT:
2317 ret = parse_type_enum(ctx, data, kw, &type->bits);
2318 break;
2319 case YANG_ENUM:
2320 ret = parse_type_enum(ctx, data, kw, &type->enums);
2321 break;
2322 case YANG_FRACTION_DIGITS:
2323 ret = parse_type_fracdigits(ctx, data, &type->fraction_digits, &type->exts);
2324 break;
2325 case YANG_LENGTH:
2326 if (type->length) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002327 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002328 return LY_EVALID;
2329 }
2330 type->length = calloc(1, sizeof *type->length);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002331 LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002332
2333 ret = parse_restr(ctx, data, kw, type->length);
2334 break;
2335 case YANG_PATH:
2336 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_PATH, 0, &type->path, Y_STR_ARG, &type->exts);
2337 break;
2338 case YANG_PATTERN:
2339 ret = parse_type_pattern(ctx, data, &type->patterns);
2340 break;
2341 case YANG_RANGE:
2342 if (type->range) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002343 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002344 return LY_EVALID;
2345 }
2346 type->range = calloc(1, sizeof *type->range);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002347 LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx->ctx), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002348
2349 ret = parse_restr(ctx, data, kw, type->range);
2350 break;
2351 case YANG_REQUIRE_INSTANCE:
2352 ret = parse_type_reqinstance(ctx, data, &type->require_instance, &type->flags, &type->exts);
2353 break;
2354 case YANG_TYPE:
2355 {
Radek Krejci2c4e7172018-10-19 15:56:26 +02002356 LY_ARRAY_NEW_RET(ctx->ctx, type->types, nest_type, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002357 }
2358 ret = parse_type(ctx, data, nest_type);
2359 break;
2360 case YANG_CUSTOM:
2361 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &type->exts);
2362 break;
2363 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002364 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002365 return LY_EVALID;
2366 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002367 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002368 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002369 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002370
2371 return ret;
2372}
2373
Michal Vaskoea5abea2018-09-18 13:10:54 +02002374/**
2375 * @brief Parse the leaf statement.
2376 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002377 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002378 * @param[in,out] data Data to read from, always moved to currently handled character.
2379 * @param[in,out] siblings Siblings to add to.
2380 *
2381 * @return LY_ERR values.
2382 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002383static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002384parse_leaf(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002385{
2386 LY_ERR ret = 0;
2387 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002388 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002389 enum yang_keyword kw;
2390 struct lysp_node *iter;
2391 struct lysp_node_leaf *leaf;
2392
2393 /* create structure */
2394 leaf = calloc(1, sizeof *leaf);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002395 LY_CHECK_ERR_RET(!leaf, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002396 leaf->nodetype = LYS_LEAF;
2397
2398 /* insert into siblings */
2399 if (!*siblings) {
2400 *siblings = (struct lysp_node *)leaf;
2401 } else {
2402 for (iter = *siblings; iter->next; iter = iter->next);
2403 iter->next = (struct lysp_node *)leaf;
2404 }
2405
2406 /* get name */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002407 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002408 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002409
Radek Krejci44ceedc2018-10-02 15:54:31 +02002410 INSERT_WORD(ctx, buf, leaf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002411
2412 /* parse substatements */
2413 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002414 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002415
2416 switch (kw) {
2417 case YANG_CONFIG:
2418 ret = parse_config(ctx, data, &leaf->flags, &leaf->exts);
2419 break;
2420 case YANG_DEFAULT:
2421 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &leaf->dflt, Y_STR_ARG, &leaf->exts);
2422 break;
2423 case YANG_DESCRIPTION:
2424 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &leaf->dsc, Y_STR_ARG, &leaf->exts);
2425 break;
2426 case YANG_IF_FEATURE:
2427 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &leaf->iffeatures, Y_STR_ARG, &leaf->exts);
2428 break;
2429 case YANG_MANDATORY:
2430 ret = parse_mandatory(ctx, data, &leaf->flags, &leaf->exts);
2431 break;
2432 case YANG_MUST:
2433 ret = parse_restrs(ctx, data, kw, &leaf->musts);
2434 break;
2435 case YANG_REFERENCE:
2436 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &leaf->ref, Y_STR_ARG, &leaf->exts);
2437 break;
2438 case YANG_STATUS:
2439 ret = parse_status(ctx, data, &leaf->flags, &leaf->exts);
2440 break;
2441 case YANG_TYPE:
2442 ret = parse_type(ctx, data, &leaf->type);
2443 break;
2444 case YANG_UNITS:
2445 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, &leaf->units, Y_STR_ARG, &leaf->exts);
2446 break;
2447 case YANG_WHEN:
2448 ret = parse_when(ctx, data, &leaf->when);
2449 break;
2450 case YANG_CUSTOM:
2451 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &leaf->exts);
2452 break;
2453 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002454 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002455 return LY_EVALID;
2456 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002457 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002458 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002459 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002460
2461 /* mandatory substatements */
2462 if (!leaf->type.name) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002463 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002464 return LY_EVALID;
2465 }
2466
2467 return ret;
2468}
2469
Michal Vaskoea5abea2018-09-18 13:10:54 +02002470/**
2471 * @brief Parse the max-elements statement.
2472 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002473 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002474 * @param[in,out] data Data to read from, always moved to currently handled character.
2475 * @param[in,out] max Value to write to.
2476 * @param[in,out] flags Flags to write to.
2477 * @param[in,out] exts Extension instances to add to.
2478 *
2479 * @return LY_ERR values.
2480 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002481static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002482parse_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 +02002483{
2484 LY_ERR ret = 0;
2485 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002486 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002487 unsigned long int num;
2488 enum yang_keyword kw;
2489
2490 if (*flags & LYS_SET_MAX) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002491 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002492 return LY_EVALID;
2493 }
2494 *flags |= LYS_SET_MAX;
2495
2496 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002497 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002498 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002499
2500 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002501 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002502 free(buf);
2503 return LY_EVALID;
2504 }
2505
2506 if (strncmp(word, "unbounded", word_len)) {
2507 errno = 0;
2508 num = strtoul(word, &ptr, 10);
2509 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002510 if ((size_t)(ptr - word) != word_len) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002511 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002512 free(buf);
2513 return LY_EVALID;
2514 }
2515 if ((errno == ERANGE) || (num > UINT32_MAX)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002516 LOGVAL_YANG(ctx, LY_VCODE_OOB, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002517 free(buf);
2518 return LY_EVALID;
2519 }
2520
2521 *max = num;
2522 }
2523 free(buf);
2524
2525 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002526 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002527
2528 switch (kw) {
2529 case YANG_CUSTOM:
2530 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MAX, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02002531 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002532 break;
2533 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002534 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002535 return LY_EVALID;
2536 }
2537 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002538 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002539
2540 return ret;
2541}
2542
Michal Vaskoea5abea2018-09-18 13:10:54 +02002543/**
2544 * @brief Parse the min-elements statement.
2545 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002546 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002547 * @param[in,out] data Data to read from, always moved to currently handled character.
2548 * @param[in,out] min Value to write to.
2549 * @param[in,out] flags Flags to write to.
2550 * @param[in,out] exts Extension instances to add to.
2551 *
2552 * @return LY_ERR values.
2553 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002554static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002555parse_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 +02002556{
2557 LY_ERR ret = 0;
2558 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002559 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002560 unsigned long int num;
2561 enum yang_keyword kw;
2562
2563 if (*flags & LYS_SET_MIN) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002564 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002565 return LY_EVALID;
2566 }
2567 *flags |= LYS_SET_MIN;
2568
2569 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002570 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002571 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002572
2573 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002574 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002575 free(buf);
2576 return LY_EVALID;
2577 }
2578
2579 errno = 0;
2580 num = strtoul(word, &ptr, 10);
2581 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002582 if ((size_t)(ptr - word) != word_len) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002583 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002584 free(buf);
2585 return LY_EVALID;
2586 }
2587 if ((errno == ERANGE) || (num > UINT32_MAX)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002588 LOGVAL_YANG(ctx, LY_VCODE_OOB, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002589 free(buf);
2590 return LY_EVALID;
2591 }
2592 *min = num;
2593 free(buf);
2594
2595 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002596 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002597
2598 switch (kw) {
2599 case YANG_CUSTOM:
2600 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MIN, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02002601 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002602 break;
2603 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002604 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002605 return LY_EVALID;
2606 }
2607 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002608 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002609
2610 return ret;
2611}
2612
Michal Vaskoea5abea2018-09-18 13:10:54 +02002613/**
2614 * @brief Parse the ordered-by statement.
2615 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002616 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002617 * @param[in,out] data Data to read from, always moved to currently handled character.
2618 * @param[in,out] flags Flags to write to.
2619 * @param[in,out] exts Extension instances to add to.
2620 *
2621 * @return LY_ERR values.
2622 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002623static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002624parse_orderedby(struct ly_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002625{
2626 LY_ERR ret = 0;
2627 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002628 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002629 enum yang_keyword kw;
2630
2631 if (*flags & LYS_ORDBY_MASK) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002632 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002633 return LY_EVALID;
2634 }
2635
2636 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002637 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002638 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002639
2640 if ((word_len == 6) && !strncmp(word, "system", word_len)) {
2641 *flags |= LYS_ORDBY_SYSTEM;
2642 } else if ((word_len == 4) && !strncmp(word, "user", word_len)) {
2643 *flags |= LYS_ORDBY_USER;
2644 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002645 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002646 free(buf);
2647 return LY_EVALID;
2648 }
2649 free(buf);
2650
2651 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002652 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002653
2654 switch (kw) {
2655 case YANG_CUSTOM:
2656 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_ORDEREDBY, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02002657 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002658 break;
2659 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002660 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002661 return LY_EVALID;
2662 }
2663 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002664 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002665
2666 return ret;
2667}
2668
Michal Vaskoea5abea2018-09-18 13:10:54 +02002669/**
2670 * @brief Parse the leaf-list statement.
2671 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002672 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002673 * @param[in,out] data Data to read from, always moved to currently handled character.
2674 * @param[in,out] siblings Siblings to add to.
2675 *
2676 * @return LY_ERR values.
2677 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002678static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002679parse_leaflist(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002680{
2681 LY_ERR ret = 0;
2682 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002683 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002684 enum yang_keyword kw;
2685 struct lysp_node *iter;
2686 struct lysp_node_leaflist *llist;
2687
2688 /* create structure */
2689 llist = calloc(1, sizeof *llist);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002690 LY_CHECK_ERR_RET(!llist, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002691 llist->nodetype = LYS_LEAFLIST;
2692
2693 /* insert into siblings */
2694 if (!*siblings) {
2695 *siblings = (struct lysp_node *)llist;
2696 } else {
2697 for (iter = *siblings; iter->next; iter = iter->next);
2698 iter->next = (struct lysp_node *)llist;
2699 }
2700
2701 /* get name */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002702 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002703 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002704
Radek Krejci44ceedc2018-10-02 15:54:31 +02002705 INSERT_WORD(ctx, buf, llist->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002706
2707 /* parse substatements */
2708 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002709 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002710
2711 switch (kw) {
2712 case YANG_CONFIG:
2713 ret = parse_config(ctx, data, &llist->flags, &llist->exts);
2714 break;
2715 case YANG_DEFAULT:
2716 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_DEFAULT, &llist->dflts, Y_STR_ARG, &llist->exts);
2717 break;
2718 case YANG_DESCRIPTION:
2719 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &llist->dsc, Y_STR_ARG, &llist->exts);
2720 break;
2721 case YANG_IF_FEATURE:
2722 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &llist->iffeatures, Y_STR_ARG, &llist->exts);
2723 break;
2724 case YANG_MAX_ELEMENTS:
2725 ret = parse_maxelements(ctx, data, &llist->max, &llist->flags, &llist->exts);
2726 break;
2727 case YANG_MIN_ELEMENTS:
2728 ret = parse_minelements(ctx, data, &llist->min, &llist->flags, &llist->exts);
2729 break;
2730 case YANG_MUST:
2731 ret = parse_restrs(ctx, data, kw, &llist->musts);
2732 break;
2733 case YANG_ORDERED_BY:
2734 ret = parse_orderedby(ctx, data, &llist->flags, &llist->exts);
2735 break;
2736 case YANG_REFERENCE:
2737 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &llist->ref, Y_STR_ARG, &llist->exts);
2738 break;
2739 case YANG_STATUS:
2740 ret = parse_status(ctx, data, &llist->flags, &llist->exts);
2741 break;
2742 case YANG_TYPE:
2743 ret = parse_type(ctx, data, &llist->type);
2744 break;
2745 case YANG_UNITS:
2746 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, &llist->units, Y_STR_ARG, &llist->exts);
2747 break;
2748 case YANG_WHEN:
2749 ret = parse_when(ctx, data, &llist->when);
2750 break;
2751 case YANG_CUSTOM:
2752 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &llist->exts);
2753 break;
2754 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002755 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002756 return LY_EVALID;
2757 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002758 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002759 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002760 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002761
2762 /* mandatory substatements */
2763 if (!llist->type.name) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002764 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002765 return LY_EVALID;
2766 }
2767
2768 return ret;
2769}
2770
Michal Vaskoea5abea2018-09-18 13:10:54 +02002771/**
2772 * @brief Parse the refine statement.
2773 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002774 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002775 * @param[in,out] data Data to read from, always moved to currently handled character.
2776 * @param[in,out] refines Refines to add to.
2777 *
2778 * @return LY_ERR values.
2779 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002780static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002781parse_refine(struct ly_parser_ctx *ctx, const char **data, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002782{
2783 LY_ERR ret = 0;
2784 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002785 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002786 enum yang_keyword kw;
2787 struct lysp_refine *rf;
2788
Radek Krejci2c4e7172018-10-19 15:56:26 +02002789 LY_ARRAY_NEW_RET(ctx->ctx, *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002790
2791 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002792 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002793 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002794
Radek Krejci44ceedc2018-10-02 15:54:31 +02002795 INSERT_WORD(ctx, buf, rf->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002796 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002797 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002798
2799 switch (kw) {
2800 case YANG_CONFIG:
2801 ret = parse_config(ctx, data, &rf->flags, &rf->exts);
2802 break;
2803 case YANG_DEFAULT:
2804 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_DEFAULT, &rf->dflts, Y_STR_ARG, &rf->exts);
2805 break;
2806 case YANG_DESCRIPTION:
2807 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &rf->dsc, Y_STR_ARG, &rf->exts);
2808 break;
2809 case YANG_IF_FEATURE:
2810 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &rf->iffeatures, Y_STR_ARG, &rf->exts);
2811 break;
2812 case YANG_MAX_ELEMENTS:
2813 ret = parse_maxelements(ctx, data, &rf->max, &rf->flags, &rf->exts);
2814 break;
2815 case YANG_MIN_ELEMENTS:
2816 ret = parse_minelements(ctx, data, &rf->min, &rf->flags, &rf->exts);
2817 break;
2818 case YANG_MUST:
2819 ret = parse_restrs(ctx, data, kw, &rf->musts);
2820 break;
2821 case YANG_MANDATORY:
2822 ret = parse_mandatory(ctx, data, &rf->flags, &rf->exts);
2823 break;
2824 case YANG_REFERENCE:
2825 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &rf->ref, Y_STR_ARG, &rf->exts);
2826 break;
2827 case YANG_PRESENCE:
2828 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_PRESENCE, 0, &rf->presence, Y_STR_ARG, &rf->exts);
2829 break;
2830 case YANG_CUSTOM:
2831 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rf->exts);
2832 break;
2833 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002834 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002835 return LY_EVALID;
2836 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002837 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002838 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002839 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002840
2841 return ret;
2842}
2843
Michal Vaskoea5abea2018-09-18 13:10:54 +02002844/**
2845 * @brief Parse the typedef statement.
2846 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002847 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002848 * @param[in,out] data Data to read from, always moved to currently handled character.
2849 * @param[in,out] typedefs Typedefs to add to.
2850 *
2851 * @return LY_ERR values.
2852 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002853static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002854parse_typedef(struct ly_parser_ctx *ctx, const char **data, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002855{
2856 LY_ERR ret = 0;
2857 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002858 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002859 enum yang_keyword kw;
2860 struct lysp_tpdf *tpdf;
2861
Radek Krejci2c4e7172018-10-19 15:56:26 +02002862 LY_ARRAY_NEW_RET(ctx->ctx, *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002863
2864 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002865 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002866 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002867
Radek Krejci44ceedc2018-10-02 15:54:31 +02002868 INSERT_WORD(ctx, buf, tpdf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002869
2870 /* parse substatements */
2871 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002872 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002873
2874 switch (kw) {
2875 case YANG_DEFAULT:
2876 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &tpdf->dflt, Y_STR_ARG, &tpdf->exts);
2877 break;
2878 case YANG_DESCRIPTION:
2879 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &tpdf->dsc, Y_STR_ARG, &tpdf->exts);
2880 break;
2881 case YANG_REFERENCE:
2882 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &tpdf->ref, Y_STR_ARG, &tpdf->exts);
2883 break;
2884 case YANG_STATUS:
2885 ret = parse_status(ctx, data, &tpdf->flags, &tpdf->exts);
2886 break;
2887 case YANG_TYPE:
2888 ret = parse_type(ctx, data, &tpdf->type);
2889 break;
2890 case YANG_UNITS:
2891 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, &tpdf->units, Y_STR_ARG, &tpdf->exts);
2892 break;
2893 case YANG_CUSTOM:
2894 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &tpdf->exts);
2895 break;
2896 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002897 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002898 return LY_EVALID;
2899 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002900 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002901 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002902 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002903
2904 /* mandatory substatements */
2905 if (!tpdf->type.name) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002906 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002907 return LY_EVALID;
2908 }
2909
2910 return ret;
2911}
2912
Michal Vaskoea5abea2018-09-18 13:10:54 +02002913/**
2914 * @brief Parse the input or output statement.
2915 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002916 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002917 * @param[in,out] data Data to read from, always moved to currently handled character.
2918 * @param[in] kw Type of this particular keyword
2919 * @param[in,out] inout_p Input/output pointer to write to.
2920 *
2921 * @return LY_ERR values.
2922 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002923static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002924parse_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 +02002925{
2926 LY_ERR ret = 0;
2927 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002928 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002929 struct lysp_action_inout *inout;
2930
2931 if (*inout_p) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002932 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002933 return LY_EVALID;
2934 }
2935
2936 /* create structure */
2937 inout = calloc(1, sizeof *inout);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002938 LY_CHECK_ERR_RET(!inout, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002939 *inout_p = inout;
2940
2941 /* parse substatements */
2942 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002943 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002944
2945 switch (kw) {
2946 case YANG_ANYDATA:
2947 case YANG_ANYXML:
2948 ret = parse_any(ctx, data, kw, &inout->data);
2949 break;
2950 case YANG_CHOICE:
2951 ret = parse_choice(ctx, data, &inout->data);
2952 break;
2953 case YANG_CONTAINER:
2954 ret = parse_container(ctx, data, &inout->data);
2955 break;
2956 case YANG_LEAF:
2957 ret = parse_leaf(ctx, data, &inout->data);
2958 break;
2959 case YANG_LEAF_LIST:
2960 ret = parse_leaflist(ctx, data, &inout->data);
2961 break;
2962 case YANG_LIST:
2963 ret = parse_list(ctx, data, &inout->data);
2964 break;
2965 case YANG_USES:
2966 ret = parse_uses(ctx, data, &inout->data);
2967 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002968 case YANG_TYPEDEF:
2969 ret = parse_typedef(ctx, data, &inout->typedefs);
2970 break;
2971 case YANG_MUST:
2972 ret = parse_restrs(ctx, data, kw, &inout->musts);
2973 break;
2974 case YANG_GROUPING:
2975 ret = parse_grouping(ctx, data, &inout->groupings);
2976 break;
2977 case YANG_CUSTOM:
2978 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inout->exts);
2979 break;
2980 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002981 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "input/output");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002982 return LY_EVALID;
2983 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002984 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002985 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002986 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002987
2988 return ret;
2989}
2990
Michal Vaskoea5abea2018-09-18 13:10:54 +02002991/**
2992 * @brief Parse the action statement.
2993 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002994 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002995 * @param[in,out] data Data to read from, always moved to currently handled character.
2996 * @param[in,out] actions Actions to add to.
2997 *
2998 * @return LY_ERR values.
2999 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003000static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003001parse_action(struct ly_parser_ctx *ctx, const char **data, struct lysp_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003002{
3003 LY_ERR ret = 0;
3004 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003005 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003006 enum yang_keyword kw;
3007 struct lysp_action *act;
3008
Radek Krejci2c4e7172018-10-19 15:56:26 +02003009 LY_ARRAY_NEW_RET(ctx->ctx, *actions, act, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003010
3011 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003012 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003013 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003014
Radek Krejci44ceedc2018-10-02 15:54:31 +02003015 INSERT_WORD(ctx, buf, act->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003016 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003017 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003018
3019 switch (kw) {
3020 case YANG_DESCRIPTION:
3021 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &act->dsc, Y_STR_ARG, &act->exts);
3022 break;
3023 case YANG_IF_FEATURE:
3024 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &act->iffeatures, Y_STR_ARG, &act->exts);
3025 break;
3026 case YANG_REFERENCE:
3027 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &act->ref, Y_STR_ARG, &act->exts);
3028 break;
3029 case YANG_STATUS:
3030 ret = parse_status(ctx, data, &act->flags, &act->exts);
3031 break;
3032
3033 case YANG_INPUT:
3034 ret = parse_inout(ctx, data, kw, &act->input);
3035 break;
3036 case YANG_OUTPUT:
3037 ret = parse_inout(ctx, data, kw, &act->output);
3038 break;
3039
3040 case YANG_TYPEDEF:
3041 ret = parse_typedef(ctx, data, &act->typedefs);
3042 break;
3043 case YANG_GROUPING:
3044 ret = parse_grouping(ctx, data, &act->groupings);
3045 break;
3046 case YANG_CUSTOM:
3047 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &act->exts);
3048 break;
3049 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003050 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "action");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003051 return LY_EVALID;
3052 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003053 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003054 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003055 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003056
3057 return ret;
3058}
3059
Michal Vaskoea5abea2018-09-18 13:10:54 +02003060/**
3061 * @brief Parse the notification statement.
3062 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003063 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003064 * @param[in,out] data Data to read from, always moved to currently handled character.
3065 * @param[in,out] notifs Notifications to add to.
3066 *
3067 * @return LY_ERR values.
3068 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003069static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003070parse_notif(struct ly_parser_ctx *ctx, const char **data, struct lysp_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003071{
3072 LY_ERR ret = 0;
3073 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003074 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003075 enum yang_keyword kw;
3076 struct lysp_notif *notif;
3077
Radek Krejci2c4e7172018-10-19 15:56:26 +02003078 LY_ARRAY_NEW_RET(ctx->ctx, *notifs, notif, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003079
3080 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003081 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003082 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003083
Radek Krejci44ceedc2018-10-02 15:54:31 +02003084 INSERT_WORD(ctx, buf, notif->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003085 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003086 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003087
3088 switch (kw) {
3089 case YANG_DESCRIPTION:
3090 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &notif->dsc, Y_STR_ARG, &notif->exts);
3091 break;
3092 case YANG_IF_FEATURE:
3093 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &notif->iffeatures, Y_STR_ARG, &notif->exts);
3094 break;
3095 case YANG_REFERENCE:
3096 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &notif->ref, Y_STR_ARG, &notif->exts);
3097 break;
3098 case YANG_STATUS:
3099 ret = parse_status(ctx, data, &notif->flags, &notif->exts);
3100 break;
3101
3102 case YANG_ANYDATA:
3103 case YANG_ANYXML:
3104 ret = parse_any(ctx, data, kw, &notif->data);
3105 break;
3106 case YANG_CHOICE:
3107 ret = parse_case(ctx, data, &notif->data);
3108 break;
3109 case YANG_CONTAINER:
3110 ret = parse_container(ctx, data, &notif->data);
3111 break;
3112 case YANG_LEAF:
3113 ret = parse_leaf(ctx, data, &notif->data);
3114 break;
3115 case YANG_LEAF_LIST:
3116 ret = parse_leaflist(ctx, data, &notif->data);
3117 break;
3118 case YANG_LIST:
3119 ret = parse_list(ctx, data, &notif->data);
3120 break;
3121 case YANG_USES:
3122 ret = parse_uses(ctx, data, &notif->data);
3123 break;
3124
3125 case YANG_MUST:
3126 ret = parse_restrs(ctx, data, kw, &notif->musts);
3127 break;
3128 case YANG_TYPEDEF:
3129 ret = parse_typedef(ctx, data, &notif->typedefs);
3130 break;
3131 case YANG_GROUPING:
3132 ret = parse_grouping(ctx, data, &notif->groupings);
3133 break;
3134 case YANG_CUSTOM:
3135 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &notif->exts);
3136 break;
3137 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003138 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003139 return LY_EVALID;
3140 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003141 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003142 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003143 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003144
3145 return ret;
3146}
3147
Michal Vaskoea5abea2018-09-18 13:10:54 +02003148/**
3149 * @brief Parse the grouping statement.
3150 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003151 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003152 * @param[in,out] data Data to read from, always moved to currently handled character.
3153 * @param[in,out] groupings Groupings to add to.
3154 *
3155 * @return LY_ERR values.
3156 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003157static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003158parse_grouping(struct ly_parser_ctx *ctx, const char **data, struct lysp_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003159{
3160 LY_ERR ret = 0;
3161 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003162 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003163 enum yang_keyword kw;
3164 struct lysp_grp *grp;
3165
Radek Krejci2c4e7172018-10-19 15:56:26 +02003166 LY_ARRAY_NEW_RET(ctx->ctx, *groupings, grp, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003167
3168 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003169 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003170 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003171
Radek Krejci44ceedc2018-10-02 15:54:31 +02003172 INSERT_WORD(ctx, buf, grp->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003173
3174 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003175 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003176
3177 switch (kw) {
3178 case YANG_DESCRIPTION:
3179 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &grp->dsc, Y_STR_ARG, &grp->exts);
3180 break;
3181 case YANG_REFERENCE:
3182 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &grp->ref, Y_STR_ARG, &grp->exts);
3183 break;
3184 case YANG_STATUS:
3185 ret = parse_status(ctx, data, &grp->flags, &grp->exts);
3186 break;
3187
3188 case YANG_ANYDATA:
3189 case YANG_ANYXML:
3190 ret = parse_any(ctx, data, kw, &grp->data);
3191 break;
3192 case YANG_CHOICE:
3193 ret = parse_choice(ctx, data, &grp->data);
3194 break;
3195 case YANG_CONTAINER:
3196 ret = parse_container(ctx, data, &grp->data);
3197 break;
3198 case YANG_LEAF:
3199 ret = parse_leaf(ctx, data, &grp->data);
3200 break;
3201 case YANG_LEAF_LIST:
3202 ret = parse_leaflist(ctx, data, &grp->data);
3203 break;
3204 case YANG_LIST:
3205 ret = parse_list(ctx, data, &grp->data);
3206 break;
3207 case YANG_USES:
3208 ret = parse_uses(ctx, data, &grp->data);
3209 break;
3210
3211 case YANG_TYPEDEF:
3212 ret = parse_typedef(ctx, data, &grp->typedefs);
3213 break;
3214 case YANG_ACTION:
3215 ret = parse_action(ctx, data, &grp->actions);
3216 break;
3217 case YANG_GROUPING:
3218 ret = parse_grouping(ctx, data, &grp->groupings);
3219 break;
3220 case YANG_NOTIFICATION:
3221 ret = parse_notif(ctx, data, &grp->notifs);
3222 break;
3223 case YANG_CUSTOM:
3224 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &grp->exts);
3225 break;
3226 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003227 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003228 return LY_EVALID;
3229 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003230 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003231 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003232 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003233
3234 return ret;
3235}
3236
Michal Vaskoea5abea2018-09-18 13:10:54 +02003237/**
3238 * @brief Parse the refine statement.
3239 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003240 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003241 * @param[in,out] data Data to read from, always moved to currently handled character.
3242 * @param[in,out] augments Augments to add to.
3243 *
3244 * @return LY_ERR values.
3245 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003246static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003247parse_augment(struct ly_parser_ctx *ctx, const char **data, struct lysp_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003248{
3249 LY_ERR ret = 0;
3250 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003251 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003252 enum yang_keyword kw;
3253 struct lysp_augment *aug;
3254
Radek Krejci2c4e7172018-10-19 15:56:26 +02003255 LY_ARRAY_NEW_RET(ctx->ctx, *augments, aug, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003256
3257 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003258 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003259 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003260
Radek Krejci44ceedc2018-10-02 15:54:31 +02003261 INSERT_WORD(ctx, buf, aug->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003262 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003263 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003264
3265 switch (kw) {
3266 case YANG_DESCRIPTION:
3267 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &aug->dsc, Y_STR_ARG, &aug->exts);
3268 break;
3269 case YANG_IF_FEATURE:
3270 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &aug->iffeatures, Y_STR_ARG, &aug->exts);
3271 break;
3272 case YANG_REFERENCE:
3273 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &aug->ref, Y_STR_ARG, &aug->exts);
3274 break;
3275 case YANG_STATUS:
3276 ret = parse_status(ctx, data, &aug->flags, &aug->exts);
3277 break;
3278 case YANG_WHEN:
3279 ret = parse_when(ctx, data, &aug->when);
3280 break;
3281
3282 case YANG_ANYDATA:
3283 case YANG_ANYXML:
3284 ret = parse_any(ctx, data, kw, &aug->child);
3285 break;
3286 case YANG_CASE:
3287 ret = parse_case(ctx, data, &aug->child);
3288 break;
3289 case YANG_CHOICE:
3290 ret = parse_choice(ctx, data, &aug->child);
3291 break;
3292 case YANG_CONTAINER:
3293 ret = parse_container(ctx, data, &aug->child);
3294 break;
3295 case YANG_LEAF:
3296 ret = parse_leaf(ctx, data, &aug->child);
3297 break;
3298 case YANG_LEAF_LIST:
3299 ret = parse_leaflist(ctx, data, &aug->child);
3300 break;
3301 case YANG_LIST:
3302 ret = parse_list(ctx, data, &aug->child);
3303 break;
3304 case YANG_USES:
3305 ret = parse_uses(ctx, data, &aug->child);
3306 break;
3307
3308 case YANG_ACTION:
3309 ret = parse_action(ctx, data, &aug->actions);
3310 break;
3311 case YANG_NOTIFICATION:
3312 ret = parse_notif(ctx, data, &aug->notifs);
3313 break;
3314 case YANG_CUSTOM:
3315 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &aug->exts);
3316 break;
3317 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003318 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003319 return LY_EVALID;
3320 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003321 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003322 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003323 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003324
3325 return ret;
3326}
3327
Michal Vaskoea5abea2018-09-18 13:10:54 +02003328/**
3329 * @brief Parse the uses statement.
3330 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003331 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003332 * @param[in,out] data Data to read from, always moved to currently handled character.
3333 * @param[in,out] siblings Siblings to add to.
3334 *
3335 * @return LY_ERR values.
3336 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003337static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003338parse_uses(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003339{
3340 LY_ERR ret = 0;
3341 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003342 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003343 enum yang_keyword kw;
3344 struct lysp_node *iter;
3345 struct lysp_node_uses *uses;
3346
3347 /* create structure */
3348 uses = calloc(1, sizeof *uses);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003349 LY_CHECK_ERR_RET(!uses, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003350 uses->nodetype = LYS_USES;
3351
3352 /* insert into siblings */
3353 if (!*siblings) {
3354 *siblings = (struct lysp_node *)uses;
3355 } else {
3356 for (iter = *siblings; iter->next; iter = iter->next);
3357 iter->next = (struct lysp_node *)uses;
3358 }
3359
3360 /* get name */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003361 ret = get_argument(ctx, data, Y_PREF_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003362 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003363
Radek Krejci44ceedc2018-10-02 15:54:31 +02003364 INSERT_WORD(ctx, buf, uses->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003365
3366 /* parse substatements */
3367 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003368 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003369
3370 switch (kw) {
3371 case YANG_DESCRIPTION:
3372 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &uses->dsc, Y_STR_ARG, &uses->exts);
3373 break;
3374 case YANG_IF_FEATURE:
3375 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &uses->iffeatures, Y_STR_ARG, &uses->exts);
3376 break;
3377 case YANG_REFERENCE:
3378 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &uses->ref, Y_STR_ARG, &uses->exts);
3379 break;
3380 case YANG_STATUS:
3381 ret = parse_status(ctx, data, &uses->flags, &uses->exts);
3382 break;
3383 case YANG_WHEN:
3384 ret = parse_when(ctx, data, &uses->when);
3385 break;
3386
3387 case YANG_REFINE:
3388 ret = parse_refine(ctx, data, &uses->refines);
3389 break;
3390 case YANG_AUGMENT:
3391 ret = parse_augment(ctx, data, &uses->augments);
3392 break;
3393 case YANG_CUSTOM:
3394 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &uses->exts);
3395 break;
3396 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003397 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003398 return LY_EVALID;
3399 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003400 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003401 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003402 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003403
3404 return ret;
3405}
3406
Michal Vaskoea5abea2018-09-18 13:10:54 +02003407/**
3408 * @brief Parse the case statement.
3409 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003410 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003411 * @param[in,out] data Data to read from, always moved to currently handled character.
3412 * @param[in,out] siblings Siblings to add to.
3413 *
3414 * @return LY_ERR values.
3415 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003416static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003417parse_case(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003418{
3419 LY_ERR ret = 0;
3420 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003421 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003422 enum yang_keyword kw;
3423 struct lysp_node *iter;
3424 struct lysp_node_case *cas;
3425
3426 /* create structure */
3427 cas = calloc(1, sizeof *cas);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003428 LY_CHECK_ERR_RET(!cas, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003429 cas->nodetype = LYS_CASE;
3430
3431 /* insert into siblings */
3432 if (!*siblings) {
3433 *siblings = (struct lysp_node *)cas;
3434 } else {
3435 for (iter = *siblings; iter->next; iter = iter->next);
3436 iter->next = (struct lysp_node *)cas;
3437 }
3438
3439 /* get name */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003440 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003441 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003442
Radek Krejci44ceedc2018-10-02 15:54:31 +02003443 INSERT_WORD(ctx, buf, cas->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003444
3445 /* parse substatements */
3446 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003447 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003448
3449 switch (kw) {
3450 case YANG_DESCRIPTION:
3451 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &cas->dsc, Y_STR_ARG, &cas->exts);
3452 break;
3453 case YANG_IF_FEATURE:
3454 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &cas->iffeatures, Y_STR_ARG, &cas->exts);
3455 break;
3456 case YANG_REFERENCE:
3457 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &cas->ref, Y_STR_ARG, &cas->exts);
3458 break;
3459 case YANG_STATUS:
3460 ret = parse_status(ctx, data, &cas->flags, &cas->exts);
3461 break;
3462 case YANG_WHEN:
3463 ret = parse_when(ctx, data, &cas->when);
3464 break;
3465
3466 case YANG_ANYDATA:
3467 case YANG_ANYXML:
3468 ret = parse_any(ctx, data, kw, &cas->child);
3469 break;
3470 case YANG_CHOICE:
3471 ret = parse_case(ctx, data, &cas->child);
3472 break;
3473 case YANG_CONTAINER:
3474 ret = parse_container(ctx, data, &cas->child);
3475 break;
3476 case YANG_LEAF:
3477 ret = parse_leaf(ctx, data, &cas->child);
3478 break;
3479 case YANG_LEAF_LIST:
3480 ret = parse_leaflist(ctx, data, &cas->child);
3481 break;
3482 case YANG_LIST:
3483 ret = parse_list(ctx, data, &cas->child);
3484 break;
3485 case YANG_USES:
3486 ret = parse_uses(ctx, data, &cas->child);
3487 break;
3488 case YANG_CUSTOM:
3489 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cas->exts);
3490 break;
3491 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003492 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003493 return LY_EVALID;
3494 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003495 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003496 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003497 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003498
3499 return ret;
3500}
3501
Michal Vaskoea5abea2018-09-18 13:10:54 +02003502/**
3503 * @brief Parse the choice statement.
3504 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003505 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003506 * @param[in,out] data Data to read from, always moved to currently handled character.
3507 * @param[in,out] siblings Siblings to add to.
3508 *
3509 * @return LY_ERR values.
3510 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003511static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003512parse_choice(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003513{
3514 LY_ERR ret = 0;
3515 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003516 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003517 enum yang_keyword kw;
3518 struct lysp_node *iter;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003519 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003520
3521 /* create structure */
Radek Krejci44ceedc2018-10-02 15:54:31 +02003522 choice = calloc(1, sizeof *choice);
3523 LY_CHECK_ERR_RET(!choice, LOGMEM(ctx->ctx), LY_EMEM);
3524 choice->nodetype = LYS_CHOICE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003525
3526 /* insert into siblings */
3527 if (!*siblings) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02003528 *siblings = (struct lysp_node *)choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003529 } else {
3530 for (iter = *siblings; iter->next; iter = iter->next);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003531 iter->next = (struct lysp_node *)choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003532 }
3533
3534 /* get name */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003535 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003536 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003537
Radek Krejci44ceedc2018-10-02 15:54:31 +02003538 INSERT_WORD(ctx, buf, choice->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003539
3540 /* parse substatements */
3541 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003542 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003543
3544 switch (kw) {
3545 case YANG_CONFIG:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003546 ret = parse_config(ctx, data, &choice->flags, &choice->exts);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003547 break;
3548 case YANG_DESCRIPTION:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003549 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &choice->dsc, Y_STR_ARG, &choice->exts);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003550 break;
3551 case YANG_IF_FEATURE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003552 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &choice->iffeatures, Y_STR_ARG, &choice->exts);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003553 break;
3554 case YANG_MANDATORY:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003555 ret = parse_mandatory(ctx, data, &choice->flags, &choice->exts);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003556 break;
3557 case YANG_REFERENCE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003558 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &choice->ref, Y_STR_ARG, &choice->exts);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003559 break;
3560 case YANG_STATUS:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003561 ret = parse_status(ctx, data, &choice->flags, &choice->exts);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003562 break;
3563 case YANG_WHEN:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003564 ret = parse_when(ctx, data, &choice->when);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003565 break;
3566 case YANG_DEFAULT:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003567 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &choice->dflt, Y_IDENTIF_ARG, &choice->exts);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003568 break;
3569
3570 case YANG_ANYDATA:
3571 case YANG_ANYXML:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003572 ret = parse_any(ctx, data, kw, &choice->child);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003573 break;
3574 case YANG_CASE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003575 ret = parse_case(ctx, data, &choice->child);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003576 break;
3577 case YANG_CHOICE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003578 ret = parse_choice(ctx, data, &choice->child);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003579 break;
3580 case YANG_CONTAINER:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003581 ret = parse_container(ctx, data, &choice->child);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003582 break;
3583 case YANG_LEAF:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003584 ret = parse_leaf(ctx, data, &choice->child);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003585 break;
3586 case YANG_LEAF_LIST:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003587 ret = parse_leaflist(ctx, data, &choice->child);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003588 break;
3589 case YANG_LIST:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003590 ret = parse_list(ctx, data, &choice->child);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003591 break;
3592 case YANG_CUSTOM:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003593 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &choice->exts);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003594 break;
3595 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003596 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003597 return LY_EVALID;
3598 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003599 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003600 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003601 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003602
3603 return ret;
3604}
3605
Michal Vaskoea5abea2018-09-18 13:10:54 +02003606/**
3607 * @brief Parse the container statement.
3608 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003609 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003610 * @param[in,out] data Data to read from, always moved to currently handled character.
3611 * @param[in,out] siblings Siblings to add to.
3612 *
3613 * @return LY_ERR values.
3614 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003615static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003616parse_container(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003617{
3618 LY_ERR ret = 0;
3619 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003620 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003621 enum yang_keyword kw;
3622 struct lysp_node *iter;
3623 struct lysp_node_container *cont;
3624
3625 /* create structure */
3626 cont = calloc(1, sizeof *cont);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003627 LY_CHECK_ERR_RET(!cont, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003628 cont->nodetype = LYS_CONTAINER;
3629
3630 /* insert into siblings */
3631 if (!*siblings) {
3632 *siblings = (struct lysp_node *)cont;
3633 } else {
3634 for (iter = *siblings; iter->next; iter = iter->next);
3635 iter->next = (struct lysp_node *)cont;
3636 }
3637
3638 /* get name */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003639 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003640 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003641
Radek Krejci44ceedc2018-10-02 15:54:31 +02003642 INSERT_WORD(ctx, buf, cont->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003643
3644 /* parse substatements */
3645 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003646 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003647
3648 switch (kw) {
3649 case YANG_CONFIG:
3650 ret = parse_config(ctx, data, &cont->flags, &cont->exts);
3651 break;
3652 case YANG_DESCRIPTION:
3653 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &cont->dsc, Y_STR_ARG, &cont->exts);
3654 break;
3655 case YANG_IF_FEATURE:
3656 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &cont->iffeatures, Y_STR_ARG, &cont->exts);
3657 break;
3658 case YANG_REFERENCE:
3659 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &cont->ref, Y_STR_ARG, &cont->exts);
3660 break;
3661 case YANG_STATUS:
3662 ret = parse_status(ctx, data, &cont->flags, &cont->exts);
3663 break;
3664 case YANG_WHEN:
3665 ret = parse_when(ctx, data, &cont->when);
3666 break;
3667 case YANG_PRESENCE:
3668 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_PRESENCE, 0, &cont->presence, Y_STR_ARG, &cont->exts);
3669 break;
3670
3671 case YANG_ANYDATA:
3672 case YANG_ANYXML:
3673 ret = parse_any(ctx, data, kw, &cont->child);
3674 break;
3675 case YANG_CHOICE:
3676 ret = parse_choice(ctx, data, &cont->child);
3677 break;
3678 case YANG_CONTAINER:
3679 ret = parse_container(ctx, data, &cont->child);
3680 break;
3681 case YANG_LEAF:
3682 ret = parse_leaf(ctx, data, &cont->child);
3683 break;
3684 case YANG_LEAF_LIST:
3685 ret = parse_leaflist(ctx, data, &cont->child);
3686 break;
3687 case YANG_LIST:
3688 ret = parse_list(ctx, data, &cont->child);
3689 break;
3690 case YANG_USES:
3691 ret = parse_uses(ctx, data, &cont->child);
3692 break;
3693
3694 case YANG_TYPEDEF:
3695 ret = parse_typedef(ctx, data, &cont->typedefs);
3696 break;
3697 case YANG_MUST:
3698 ret = parse_restrs(ctx, data, kw, &cont->musts);
3699 break;
3700 case YANG_ACTION:
3701 ret = parse_action(ctx, data, &cont->actions);
3702 break;
3703 case YANG_GROUPING:
3704 ret = parse_grouping(ctx, data, &cont->groupings);
3705 break;
3706 case YANG_NOTIFICATION:
3707 ret = parse_notif(ctx, data, &cont->notifs);
3708 break;
3709 case YANG_CUSTOM:
3710 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cont->exts);
3711 break;
3712 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003713 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003714 return LY_EVALID;
3715 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003716 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003717 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003718 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003719
3720 return ret;
3721}
3722
Michal Vaskoea5abea2018-09-18 13:10:54 +02003723/**
3724 * @brief Parse the list statement.
3725 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003726 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003727 * @param[in,out] data Data to read from, always moved to currently handled character.
3728 * @param[in,out] siblings Siblings to add to.
3729 *
3730 * @return LY_ERR values.
3731 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003732static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003733parse_list(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003734{
3735 LY_ERR ret = 0;
3736 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003737 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003738 enum yang_keyword kw;
3739 struct lysp_node *iter;
3740 struct lysp_node_list *list;
3741
3742 /* create structure */
3743 list = calloc(1, sizeof *list);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003744 LY_CHECK_ERR_RET(!list, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003745 list->nodetype = LYS_LIST;
3746
3747 /* insert into siblings */
3748 if (!*siblings) {
3749 *siblings = (struct lysp_node *)list;
3750 } else {
3751 for (iter = *siblings; iter->next; iter = iter->next);
3752 iter->next = (struct lysp_node *)list;
3753 }
3754
3755 /* get name */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003756 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003757 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003758
Radek Krejci44ceedc2018-10-02 15:54:31 +02003759 INSERT_WORD(ctx, buf, list->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003760
3761 /* parse substatements */
3762 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003763 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003764
3765 switch (kw) {
3766 case YANG_CONFIG:
3767 ret = parse_config(ctx, data, &list->flags, &list->exts);
3768 break;
3769 case YANG_DESCRIPTION:
3770 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &list->dsc, Y_STR_ARG, &list->exts);
3771 break;
3772 case YANG_IF_FEATURE:
3773 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &list->iffeatures, Y_STR_ARG, &list->exts);
3774 break;
3775 case YANG_REFERENCE:
3776 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &list->ref, Y_STR_ARG, &list->exts);
3777 break;
3778 case YANG_STATUS:
3779 ret = parse_status(ctx, data, &list->flags, &list->exts);
3780 break;
3781 case YANG_WHEN:
3782 ret = parse_when(ctx, data, &list->when);
3783 break;
3784 case YANG_KEY:
3785 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_KEY, 0, &list->key, Y_STR_ARG, &list->exts);
3786 break;
3787 case YANG_MAX_ELEMENTS:
3788 ret = parse_maxelements(ctx, data, &list->max, &list->flags, &list->exts);
3789 break;
3790 case YANG_MIN_ELEMENTS:
3791 ret = parse_minelements(ctx, data, &list->min, &list->flags, &list->exts);
3792 break;
3793 case YANG_ORDERED_BY:
3794 ret = parse_orderedby(ctx, data, &list->flags, &list->exts);
3795 break;
3796 case YANG_UNIQUE:
3797 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_UNIQUE, &list->uniques, Y_STR_ARG, &list->exts);
3798 break;
3799
3800 case YANG_ANYDATA:
3801 case YANG_ANYXML:
3802 ret = parse_any(ctx, data, kw, &list->child);
3803 break;
3804 case YANG_CHOICE:
3805 ret = parse_choice(ctx, data, &list->child);
3806 break;
3807 case YANG_CONTAINER:
3808 ret = parse_container(ctx, data, &list->child);
3809 break;
3810 case YANG_LEAF:
3811 ret = parse_leaf(ctx, data, &list->child);
3812 break;
3813 case YANG_LEAF_LIST:
3814 ret = parse_leaflist(ctx, data, &list->child);
3815 break;
3816 case YANG_LIST:
3817 ret = parse_list(ctx, data, &list->child);
3818 break;
3819 case YANG_USES:
3820 ret = parse_uses(ctx, data, &list->child);
3821 break;
3822
3823 case YANG_TYPEDEF:
3824 ret = parse_typedef(ctx, data, &list->typedefs);
3825 break;
3826 case YANG_MUST:
3827 ret = parse_restrs(ctx, data, kw, &list->musts);
3828 break;
3829 case YANG_ACTION:
3830 ret = parse_action(ctx, data, &list->actions);
3831 break;
3832 case YANG_GROUPING:
3833 ret = parse_grouping(ctx, data, &list->groupings);
3834 break;
3835 case YANG_NOTIFICATION:
3836 ret = parse_notif(ctx, data, &list->notifs);
3837 break;
3838 case YANG_CUSTOM:
3839 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &list->exts);
3840 break;
3841 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003842 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003843 return LY_EVALID;
3844 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003845 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003846 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003847 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003848
3849 return ret;
3850}
3851
Michal Vaskoea5abea2018-09-18 13:10:54 +02003852/**
3853 * @brief Parse the yin-element statement.
3854 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003855 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003856 * @param[in,out] data Data to read from, always moved to currently handled character.
3857 * @param[in,out] flags Flags to write to.
3858 * @param[in,out] exts Extension instances to add to.
3859 *
3860 * @return LY_ERR values.
3861 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003862static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003863parse_yinelement(struct ly_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003864{
3865 LY_ERR ret = 0;
3866 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003867 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003868 enum yang_keyword kw;
3869
3870 if (*flags & LYS_YINELEM_MASK) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02003871 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003872 return LY_EVALID;
3873 }
3874
3875 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003876 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003877 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003878
3879 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
3880 *flags |= LYS_YINELEM_TRUE;
3881 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
3882 *flags |= LYS_YINELEM_FALSE;
3883 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02003884 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003885 free(buf);
3886 return LY_EVALID;
3887 }
3888 free(buf);
3889
3890 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003891 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003892
3893 switch (kw) {
3894 case YANG_CUSTOM:
3895 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_YINELEM, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02003896 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003897 break;
3898 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003899 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003900 return LY_EVALID;
3901 }
3902 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003903 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003904
3905 return ret;
3906}
3907
Michal Vaskoea5abea2018-09-18 13:10:54 +02003908/**
3909 * @brief Parse the yin-element statement.
3910 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003911 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003912 * @param[in,out] data Data to read from, always moved to currently handled character.
3913 * @param[in,out] argument Value to write to.
3914 * @param[in,out] flags Flags to write to.
3915 * @param[in,out] exts Extension instances to add to.
3916 *
3917 * @return LY_ERR values.
3918 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003919static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003920parse_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 +02003921{
3922 LY_ERR ret = 0;
3923 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003924 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003925 enum yang_keyword kw;
3926
3927 if (*argument) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02003928 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003929 return LY_EVALID;
3930 }
3931
3932 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003933 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003934 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003935
Radek Krejci44ceedc2018-10-02 15:54:31 +02003936 INSERT_WORD(ctx, buf, *argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003937 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003938 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003939
3940 switch (kw) {
3941 case YANG_YIN_ELEMENT:
3942 ret = parse_yinelement(ctx, data, flags, exts);
3943 break;
3944 case YANG_CUSTOM:
3945 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_ARGUMENT, 0, exts);
3946 break;
3947 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003948 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003949 return LY_EVALID;
3950 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003951 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003952 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003953 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003954
3955 return ret;
3956}
3957
Michal Vaskoea5abea2018-09-18 13:10:54 +02003958/**
3959 * @brief Parse the extension statement.
3960 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003961 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003962 * @param[in,out] data Data to read from, always moved to currently handled character.
3963 * @param[in,out] extensions Extensions to add to.
3964 *
3965 * @return LY_ERR values.
3966 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003967static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003968parse_extension(struct ly_parser_ctx *ctx, const char **data, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003969{
3970 LY_ERR ret = 0;
3971 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003972 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003973 enum yang_keyword kw;
3974 struct lysp_ext *ex;
3975
Radek Krejci2c4e7172018-10-19 15:56:26 +02003976 LY_ARRAY_NEW_RET(ctx->ctx, *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003977
3978 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003979 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003980 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003981
Radek Krejci44ceedc2018-10-02 15:54:31 +02003982 INSERT_WORD(ctx, buf, ex->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003983 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003984 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003985
3986 switch (kw) {
3987 case YANG_DESCRIPTION:
3988 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &ex->dsc, Y_STR_ARG, &ex->exts);
3989 break;
3990 case YANG_REFERENCE:
3991 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &ex->ref, Y_STR_ARG, &ex->exts);
3992 break;
3993 case YANG_STATUS:
3994 ret = parse_status(ctx, data, &ex->flags, &ex->exts);
3995 break;
3996 case YANG_ARGUMENT:
3997 ret = parse_argument(ctx, data, &ex->argument, &ex->flags, &ex->exts);
3998 break;
3999 case YANG_CUSTOM:
4000 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ex->exts);
4001 break;
4002 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004003 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004004 return LY_EVALID;
4005 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004006 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004007 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004008 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004009
4010 return ret;
4011}
4012
Michal Vaskoea5abea2018-09-18 13:10:54 +02004013/**
4014 * @brief Parse the deviate statement.
4015 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004016 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004017 * @param[in,out] data Data to read from, always moved to currently handled character.
4018 * @param[in,out] deviates Deviates to add to.
4019 *
4020 * @return LY_ERR values.
4021 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004022static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02004023parse_deviate(struct ly_parser_ctx *ctx, const char **data, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004024{
4025 LY_ERR ret = 0;
4026 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004027 size_t word_len, dev_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004028 enum yang_keyword kw;
4029 struct lysp_deviate *iter, *d;
4030 struct lysp_deviate_add *d_add = NULL;
4031 struct lysp_deviate_rpl *d_rpl = NULL;
4032 struct lysp_deviate_del *d_del = NULL;
4033 const char **d_units, ***d_uniques, ***d_dflts;
4034 struct lysp_restr **d_musts;
4035 uint16_t *d_flags;
4036 uint32_t *d_min, *d_max;
4037
4038 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02004039 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02004040 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004041
4042 if ((word_len == 13) && !strncmp(word, "not-supported", word_len)) {
4043 dev_mod = LYS_DEV_NOT_SUPPORTED;
4044 } else if ((word_len == 3) && !strncmp(word, "add", word_len)) {
4045 dev_mod = LYS_DEV_ADD;
4046 } else if ((word_len == 7) && !strncmp(word, "replace", word_len)) {
4047 dev_mod = LYS_DEV_REPLACE;
4048 } else if ((word_len == 6) && !strncmp(word, "delete", word_len)) {
4049 dev_mod = LYS_DEV_DELETE;
4050 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004051 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004052 free(buf);
4053 return LY_EVALID;
4054 }
4055 free(buf);
4056
4057 /* create structure */
4058 switch (dev_mod) {
4059 case LYS_DEV_NOT_SUPPORTED:
4060 d = calloc(1, sizeof *d);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004061 LY_CHECK_ERR_RET(!d, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004062 break;
4063 case LYS_DEV_ADD:
4064 d_add = calloc(1, sizeof *d_add);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004065 LY_CHECK_ERR_RET(!d_add, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004066 d = (struct lysp_deviate *)d_add;
4067 d_units = &d_add->units;
4068 d_uniques = &d_add->uniques;
4069 d_dflts = &d_add->dflts;
4070 d_musts = &d_add->musts;
4071 d_flags = &d_add->flags;
4072 d_min = &d_add->min;
4073 d_max = &d_add->max;
4074 break;
4075 case LYS_DEV_REPLACE:
4076 d_rpl = calloc(1, sizeof *d_rpl);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004077 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004078 d = (struct lysp_deviate *)d_rpl;
4079 d_units = &d_rpl->units;
4080 d_flags = &d_rpl->flags;
4081 d_min = &d_rpl->min;
4082 d_max = &d_rpl->max;
4083 break;
4084 case LYS_DEV_DELETE:
4085 d_del = calloc(1, sizeof *d_del);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004086 LY_CHECK_ERR_RET(!d_del, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004087 d = (struct lysp_deviate *)d_del;
4088 d_units = &d_del->units;
4089 d_uniques = &d_del->uniques;
4090 d_dflts = &d_del->dflts;
4091 d_musts = &d_del->musts;
4092 d_flags = &d_del->flags;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004093 break;
4094 default:
4095 assert(0);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004096 LOGINT_RET(ctx->ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004097 }
4098 d->mod = dev_mod;
4099
4100 /* insert into siblings */
4101 if (!*deviates) {
4102 *deviates = d;
4103 } else {
4104 for (iter = *deviates; iter->next; iter = iter->next);
4105 iter->next = d;
4106 }
4107
4108 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02004109 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004110
4111 switch (kw) {
4112 case YANG_CONFIG:
4113 switch (dev_mod) {
4114 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004115 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004116 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004117 return LY_EVALID;
4118 default:
4119 ret = parse_config(ctx, data, d_flags, &d->exts);
4120 break;
4121 }
4122 break;
4123 case YANG_DEFAULT:
4124 switch (dev_mod) {
4125 case LYS_DEV_NOT_SUPPORTED:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004126 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004127 return LY_EVALID;
4128 case LYS_DEV_REPLACE:
4129 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &d_rpl->dflt, Y_STR_ARG, &d->exts);
4130 break;
4131 default:
4132 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_DEFAULT, d_dflts, Y_STR_ARG, &d->exts);
4133 break;
4134 }
4135 break;
4136 case YANG_MANDATORY:
4137 switch (dev_mod) {
4138 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004139 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004140 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004141 return LY_EVALID;
4142 default:
4143 ret = parse_mandatory(ctx, data, d_flags, &d->exts);
4144 break;
4145 }
4146 break;
4147 case YANG_MAX_ELEMENTS:
4148 switch (dev_mod) {
4149 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004150 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004151 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004152 return LY_EVALID;
4153 default:
4154 ret = parse_maxelements(ctx, data, d_max, d_flags, &d->exts);
4155 break;
4156 }
4157 break;
4158 case YANG_MIN_ELEMENTS:
4159 switch (dev_mod) {
4160 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004161 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004162 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004163 return LY_EVALID;
4164 default:
4165 ret = parse_minelements(ctx, data, d_min, d_flags, &d->exts);
4166 break;
4167 }
4168 break;
4169 case YANG_MUST:
4170 switch (dev_mod) {
4171 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004172 case LYS_DEV_REPLACE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004173 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004174 return LY_EVALID;
4175 default:
4176 ret = parse_restrs(ctx, data, kw, d_musts);
4177 break;
4178 }
4179 break;
4180 case YANG_TYPE:
4181 switch (dev_mod) {
4182 case LYS_DEV_NOT_SUPPORTED:
4183 case LYS_DEV_ADD:
4184 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004185 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004186 return LY_EVALID;
4187 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004188 if (d_rpl->type) {
4189 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
4190 return LY_EVALID;
4191 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004192 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004193 LY_CHECK_ERR_RET(!d_rpl->type, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004194 ret = parse_type(ctx, data, d_rpl->type);
4195 break;
4196 }
4197 break;
4198 case YANG_UNIQUE:
4199 switch (dev_mod) {
4200 case LYS_DEV_NOT_SUPPORTED:
4201 case LYS_DEV_REPLACE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004202 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004203 return LY_EVALID;
4204 default:
4205 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_UNIQUE, d_uniques, Y_STR_ARG, &d->exts);
4206 break;
4207 }
4208 break;
4209 case YANG_UNITS:
4210 switch (dev_mod) {
4211 case LYS_DEV_NOT_SUPPORTED:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004212 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004213 return LY_EVALID;
4214 default:
4215 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, d_units, Y_STR_ARG, &d->exts);
4216 break;
4217 }
4218 break;
4219 case YANG_CUSTOM:
4220 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &d->exts);
4221 break;
4222 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004223 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004224 return LY_EVALID;
4225 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004226 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004227 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004228 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004229
4230 return ret;
4231}
4232
Michal Vaskoea5abea2018-09-18 13:10:54 +02004233/**
4234 * @brief Parse the deviation statement.
4235 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004236 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004237 * @param[in,out] data Data to read from, always moved to currently handled character.
4238 * @param[in,out] deviations Deviations to add to.
4239 *
4240 * @return LY_ERR values.
4241 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004242static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02004243parse_deviation(struct ly_parser_ctx *ctx, const char **data, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004244{
4245 LY_ERR ret = 0;
4246 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004247 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004248 enum yang_keyword kw;
4249 struct lysp_deviation *dev;
4250
Radek Krejci2c4e7172018-10-19 15:56:26 +02004251 LY_ARRAY_NEW_RET(ctx->ctx, *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004252
4253 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02004254 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02004255 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004256
Radek Krejci44ceedc2018-10-02 15:54:31 +02004257 INSERT_WORD(ctx, buf, dev->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004258 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02004259 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004260
4261 switch (kw) {
4262 case YANG_DESCRIPTION:
4263 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &dev->dsc, Y_STR_ARG, &dev->exts);
4264 break;
4265 case YANG_DEVIATE:
4266 ret = parse_deviate(ctx, data, &dev->deviates);
4267 break;
4268 case YANG_REFERENCE:
4269 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &dev->ref, Y_STR_ARG, &dev->exts);
4270 break;
4271 case YANG_CUSTOM:
4272 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &dev->exts);
4273 break;
4274 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004275 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004276 return LY_EVALID;
4277 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004278 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004279 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004280 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004281
4282 /* mandatory substatements */
4283 if (!dev->deviates) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004284 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004285 return LY_EVALID;
4286 }
4287
4288 return ret;
4289}
4290
Michal Vaskoea5abea2018-09-18 13:10:54 +02004291/**
4292 * @brief Parse the feature statement.
4293 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004294 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004295 * @param[in,out] data Data to read from, always moved to currently handled character.
4296 * @param[in,out] features Features to add to.
4297 *
4298 * @return LY_ERR values.
4299 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004300static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02004301parse_feature(struct ly_parser_ctx *ctx, const char **data, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004302{
4303 LY_ERR ret = 0;
4304 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004305 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004306 enum yang_keyword kw;
4307 struct lysp_feature *feat;
4308
Radek Krejci2c4e7172018-10-19 15:56:26 +02004309 LY_ARRAY_NEW_RET(ctx->ctx, *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004310
4311 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02004312 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02004313 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004314
Radek Krejci44ceedc2018-10-02 15:54:31 +02004315 INSERT_WORD(ctx, buf, feat->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004316 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02004317 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004318
4319 switch (kw) {
4320 case YANG_DESCRIPTION:
4321 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &feat->dsc, Y_STR_ARG, &feat->exts);
4322 break;
4323 case YANG_IF_FEATURE:
4324 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &feat->iffeatures, Y_STR_ARG, &feat->exts);
4325 break;
4326 case YANG_REFERENCE:
4327 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &feat->ref, Y_STR_ARG, &feat->exts);
4328 break;
4329 case YANG_STATUS:
4330 ret = parse_status(ctx, data, &feat->flags, &feat->exts);
4331 break;
4332 case YANG_CUSTOM:
4333 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &feat->exts);
4334 break;
4335 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004336 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004337 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004338 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004339 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004340 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004341 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004342
4343 return ret;
4344}
4345
Michal Vaskoea5abea2018-09-18 13:10:54 +02004346/**
4347 * @brief Parse the identity statement.
4348 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004349 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004350 * @param[in,out] data Data to read from, always moved to currently handled character.
4351 * @param[in,out] identities Identities to add to.
4352 *
4353 * @return LY_ERR values.
4354 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004355static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02004356parse_identity(struct ly_parser_ctx *ctx, const char **data, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004357{
4358 LY_ERR ret = 0;
4359 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004360 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004361 enum yang_keyword kw;
4362 struct lysp_ident *ident;
4363
Radek Krejci2c4e7172018-10-19 15:56:26 +02004364 LY_ARRAY_NEW_RET(ctx->ctx, *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004365
4366 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02004367 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02004368 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004369
Radek Krejci44ceedc2018-10-02 15:54:31 +02004370 INSERT_WORD(ctx, buf, ident->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004371 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02004372 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004373
4374 switch (kw) {
4375 case YANG_DESCRIPTION:
4376 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &ident->dsc, Y_STR_ARG, &ident->exts);
4377 break;
4378 case YANG_IF_FEATURE:
4379 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &ident->iffeatures, Y_STR_ARG, &ident->exts);
4380 break;
4381 case YANG_REFERENCE:
4382 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &ident->ref, Y_STR_ARG, &ident->exts);
4383 break;
4384 case YANG_STATUS:
4385 ret = parse_status(ctx, data, &ident->flags, &ident->exts);
4386 break;
4387 case YANG_BASE:
4388 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_BASE, &ident->bases, Y_PREF_IDENTIF_ARG, &ident->exts);
4389 break;
4390 case YANG_CUSTOM:
4391 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ident->exts);
4392 break;
4393 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004394 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004395 return LY_EVALID;
4396 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004397 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004398 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004399
4400 return ret;
4401}
4402
Michal Vaskoea5abea2018-09-18 13:10:54 +02004403/**
4404 * @brief Parse the module or submodule statement.
4405 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004406 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004407 * @param[in,out] data Data to read from, always moved to currently handled character.
4408 * @param[in,out] mod Module to write to.
4409 *
4410 * @return LY_ERR values.
4411 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004412static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02004413parse_sub_module(struct ly_parser_ctx *ctx, const char **data, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004414{
4415 LY_ERR ret = 0;
4416 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004417 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004418 enum yang_keyword kw, prev_kw = 0;
4419 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
4420
4421 /* (sub)module name */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02004422 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02004423 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004424
Radek Krejci44ceedc2018-10-02 15:54:31 +02004425 INSERT_WORD(ctx, buf, mod->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004426 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02004427 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004428
Radek Krejcie3846472018-10-15 15:24:51 +02004429#define CHECK_ORDER(SECTION) \
4430 if (mod_stmt > SECTION) {LOGVAL_YANG(ctx, LY_VCODE_INORD, ly_stmt2str(kw), ly_stmt2str(prev_kw)); return LY_EVALID;}mod_stmt = SECTION
4431
Michal Vasko7fbc8162018-09-17 10:35:16 +02004432 switch (kw) {
4433 /* module header */
4434 case YANG_NAMESPACE:
4435 case YANG_PREFIX:
4436 if (mod->submodule) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004437 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004438 return LY_EVALID;
4439 }
Radek Krejcie3846472018-10-15 15:24:51 +02004440 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4441 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004442 case YANG_BELONGS_TO:
Radek Krejcie3846472018-10-15 15:24:51 +02004443 if (!mod->submodule) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004444 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004445 return LY_EVALID;
4446 }
Radek Krejcie3846472018-10-15 15:24:51 +02004447 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4448 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004449 case YANG_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004450 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004451 break;
4452 /* linkage */
4453 case YANG_INCLUDE:
4454 case YANG_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004455 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004456 break;
4457 /* meta */
4458 case YANG_ORGANIZATION:
4459 case YANG_CONTACT:
4460 case YANG_DESCRIPTION:
4461 case YANG_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004462 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004463 break;
4464
4465 /* revision */
4466 case YANG_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004467 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004468 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004469 /* body */
4470 case YANG_ANYDATA:
4471 case YANG_ANYXML:
4472 case YANG_AUGMENT:
4473 case YANG_CHOICE:
4474 case YANG_CONTAINER:
4475 case YANG_DEVIATION:
4476 case YANG_EXTENSION:
4477 case YANG_FEATURE:
4478 case YANG_GROUPING:
4479 case YANG_IDENTITY:
4480 case YANG_LEAF:
4481 case YANG_LEAF_LIST:
4482 case YANG_LIST:
4483 case YANG_NOTIFICATION:
4484 case YANG_RPC:
4485 case YANG_TYPEDEF:
4486 case YANG_USES:
4487 case YANG_CUSTOM:
4488 mod_stmt = Y_MOD_BODY;
4489 break;
4490 default:
4491 /* error handled in the next switch */
4492 break;
4493 }
Radek Krejcie3846472018-10-15 15:24:51 +02004494#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004495
Radek Krejcie3846472018-10-15 15:24:51 +02004496 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004497 switch (kw) {
4498 /* module header */
4499 case YANG_YANG_VERSION:
4500 ret = parse_yangversion(ctx, data, mod);
4501 break;
4502 case YANG_NAMESPACE:
4503 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_NAMESPACE, 0, &mod->ns, Y_STR_ARG, &mod->exts);
4504 break;
4505 case YANG_PREFIX:
4506 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_PREFIX, 0, &mod->prefix, Y_IDENTIF_ARG, &mod->exts);
Radek Krejci70853c52018-10-15 14:46:16 +02004507 LY_CHECK_RET(lysp_check_prefix(ctx, mod, &mod->prefix), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004508 break;
4509 case YANG_BELONGS_TO:
4510 ret = parse_belongsto(ctx, data, &mod->belongsto, &mod->prefix, &mod->exts);
4511 break;
4512
4513 /* linkage */
4514 case YANG_INCLUDE:
4515 ret = parse_include(ctx, data, &mod->includes);
4516 break;
4517 case YANG_IMPORT:
Radek Krejci70853c52018-10-15 14:46:16 +02004518 ret = parse_import(ctx, data, mod);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004519 break;
4520
4521 /* meta */
4522 case YANG_ORGANIZATION:
4523 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_ORGANIZATION, 0, &mod->org, Y_STR_ARG, &mod->exts);
4524 break;
4525 case YANG_CONTACT:
4526 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_CONTACT, 0, &mod->contact, Y_STR_ARG, &mod->exts);
4527 break;
4528 case YANG_DESCRIPTION:
4529 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &mod->dsc, Y_STR_ARG, &mod->exts);
4530 break;
4531 case YANG_REFERENCE:
4532 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &mod->ref, Y_STR_ARG, &mod->exts);
4533 break;
4534
4535 /* revision */
4536 case YANG_REVISION:
4537 ret = parse_revision(ctx, data, &mod->revs);
4538 break;
4539
4540 /* body */
4541 case YANG_ANYDATA:
4542 case YANG_ANYXML:
4543 ret = parse_any(ctx, data, kw, &mod->data);
4544 break;
4545 case YANG_CHOICE:
4546 ret = parse_choice(ctx, data, &mod->data);
4547 break;
4548 case YANG_CONTAINER:
4549 ret = parse_container(ctx, data, &mod->data);
4550 break;
4551 case YANG_LEAF:
4552 ret = parse_leaf(ctx, data, &mod->data);
4553 break;
4554 case YANG_LEAF_LIST:
4555 ret = parse_leaflist(ctx, data, &mod->data);
4556 break;
4557 case YANG_LIST:
4558 ret = parse_list(ctx, data, &mod->data);
4559 break;
4560 case YANG_USES:
4561 ret = parse_uses(ctx, data, &mod->data);
4562 break;
4563
4564 case YANG_AUGMENT:
4565 ret = parse_augment(ctx, data, &mod->augments);
4566 break;
4567 case YANG_DEVIATION:
4568 ret = parse_deviation(ctx, data, &mod->deviations);
4569 break;
4570 case YANG_EXTENSION:
4571 ret = parse_extension(ctx, data, &mod->extensions);
4572 break;
4573 case YANG_FEATURE:
4574 ret = parse_feature(ctx, data, &mod->features);
4575 break;
4576 case YANG_GROUPING:
4577 ret = parse_grouping(ctx, data, &mod->groupings);
4578 break;
4579 case YANG_IDENTITY:
4580 ret = parse_identity(ctx, data, &mod->identities);
4581 break;
4582 case YANG_NOTIFICATION:
4583 ret = parse_notif(ctx, data, &mod->notifs);
4584 break;
4585 case YANG_RPC:
4586 ret = parse_action(ctx, data, &mod->rpcs);
4587 break;
4588 case YANG_TYPEDEF:
4589 ret = parse_typedef(ctx, data, &mod->typedefs);
4590 break;
4591 case YANG_CUSTOM:
4592 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &mod->exts);
4593 break;
4594
4595 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004596 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), mod->submodule ? "submodule" : "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004597 return LY_EVALID;
4598 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004599 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004600 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004601 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004602
4603 /* mandatory substatements */
4604 if (mod->submodule) {
4605 if (!mod->belongsto) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004606 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004607 return LY_EVALID;
4608 }
4609 } else {
4610 if (!mod->ns) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004611 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004612 return LY_EVALID;
4613 } else if (!mod->prefix) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004614 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004615 return LY_EVALID;
4616 }
4617 }
4618
4619 return ret;
4620}
4621
Radek Krejcid4557c62018-09-17 11:42:09 +02004622LY_ERR
Michal Vasko7fbc8162018-09-17 10:35:16 +02004623yang_parse(struct ly_ctx *ctx, const char *data, struct lysp_module **mod_p)
4624{
4625 LY_ERR ret = 0;
4626 char *word, *buf;
Radek Krejciefd22f62018-09-27 11:47:58 +02004627 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004628 enum yang_keyword kw;
Radek Krejci0c2cf322018-10-13 08:02:30 +02004629 struct lysp_module *mod = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +02004630 struct ly_parser_ctx context = {0};
Michal Vasko7fbc8162018-09-17 10:35:16 +02004631
Radek Krejci44ceedc2018-10-02 15:54:31 +02004632 context.ctx = ctx;
4633 context.line = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004634
4635 /* "module"/"submodule" */
Radek Krejci44ceedc2018-10-02 15:54:31 +02004636 ret = get_keyword(&context, &data, &kw, &word, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02004637 LY_CHECK_GOTO(ret, error);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004638
4639 if ((kw != YANG_MODULE) && (kw != YANG_SUBMODULE)) {
Radek Krejcifc62d7e2018-10-11 12:56:42 +02004640 LOGVAL_YANG(&context, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".",
Radek Krejcic59bc972018-09-17 16:13:06 +02004641 ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004642 goto error;
4643 }
4644
4645 mod = calloc(1, sizeof *mod);
4646 LY_CHECK_ERR_GOTO(!mod, LOGMEM(ctx), error);
4647 if (kw == YANG_SUBMODULE) {
4648 mod->submodule = 1;
4649 }
Radek Krejci9fcacc12018-10-11 15:59:11 +02004650 mod->ctx = ctx;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004651
4652 /* substatements */
Radek Krejci44ceedc2018-10-02 15:54:31 +02004653 ret = parse_sub_module(&context, &data, mod);
Radek Krejcic59bc972018-09-17 16:13:06 +02004654 LY_CHECK_GOTO(ret, error);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004655
4656 /* read some trailing spaces or new lines */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02004657 ret = get_argument(&context, &data, Y_MAYBE_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02004658 LY_CHECK_GOTO(ret, error);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004659
4660 if (word) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004661 LOGVAL_YANG(&context, LYVE_SYNTAX, "Invalid character sequence \"%.*s\", expected end-of-file.",
Michal Vasko7fbc8162018-09-17 10:35:16 +02004662 word_len, word);
4663 free(buf);
4664 goto error;
4665 }
4666 assert(!buf);
4667
Radek Krejci86d106e2018-10-18 09:53:19 +02004668 /* make sure that the newest revision is at position 0 */
4669 lysp_sort_revisions(mod->revs);
4670
Michal Vasko7fbc8162018-09-17 10:35:16 +02004671 *mod_p = mod;
4672 return ret;
4673
4674error:
Radek Krejci9fcacc12018-10-11 15:59:11 +02004675 lysp_module_free(mod);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004676 return ret;
4677}