blob: 805ef4f5318de314fea14da5fac46f3b203ec896 [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 Krejcie53a8dc2018-10-17 12:52:40 +02001039 LYSP_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 Krejcie53a8dc2018-10-17 12:52:40 +02001302 LYSP_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 Krejcie53a8dc2018-10-17 12:52:40 +02001354 LYSP_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 Krejcie53a8dc2018-10-17 12:52:40 +02001416 LYSP_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 Krejciefd22f62018-09-27 11:47:58 +02001472 size_t count, word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001473 enum yang_keyword kw;
1474
1475 /* allocate new pointer */
1476 for (count = 1; (*texts) && (*texts)[count - 1]; ++count);
Radek Krejcie7f84ae2018-10-15 16:42:29 +02001477 *texts = realloc(*texts, (1 + count) * sizeof **texts);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001478 LY_CHECK_ERR_RET(!*texts, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001479
1480 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001481 ret = get_argument(ctx, data, arg, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001482 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001483
Radek Krejci44ceedc2018-10-02 15:54:31 +02001484 INSERT_WORD(ctx, buf, (*texts)[count - 1], word, word_len);
Radek Krejcie7f84ae2018-10-15 16:42:29 +02001485 (*texts)[count] = NULL; /* NULL-termination of the array */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001486 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001487 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001488
1489 switch (kw) {
1490 case YANG_CUSTOM:
1491 ret = parse_ext(ctx, data, word, word_len, substmt, count - 1, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02001492 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001493 break;
1494 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001495 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001496 return LY_EVALID;
1497 }
1498 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001499 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001500
1501 return ret;
1502}
1503
Michal Vaskoea5abea2018-09-18 13:10:54 +02001504/**
1505 * @brief Parse the config statement.
1506 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001507 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001508 * @param[in,out] data Data to read from, always moved to currently handled character.
1509 * @param[in,out] flags Flags to add to.
1510 * @param[in,out] exts Extension instances to add to.
1511 *
1512 * @return LY_ERR values.
1513 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001514static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001515parse_config(struct ly_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001516{
1517 LY_ERR ret = 0;
1518 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001519 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001520 enum yang_keyword kw;
1521
1522 if (*flags & LYS_CONFIG_MASK) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001523 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001524 return LY_EVALID;
1525 }
1526
1527 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001528 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001529 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001530
1531 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1532 *flags |= LYS_CONFIG_W;
1533 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1534 *flags |= LYS_CONFIG_R;
1535 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001536 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001537 free(buf);
1538 return LY_EVALID;
1539 }
1540 free(buf);
1541
1542 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001543 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001544
1545 switch (kw) {
1546 case YANG_CUSTOM:
1547 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_CONFIG, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02001548 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001549 break;
1550 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001551 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001552 return LY_EVALID;
1553 }
1554 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001555 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001556
1557 return ret;
1558}
1559
Michal Vaskoea5abea2018-09-18 13:10:54 +02001560/**
1561 * @brief Parse the mandatory statement.
1562 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001563 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001564 * @param[in,out] data Data to read from, always moved to currently handled character.
1565 * @param[in,out] flags Flags to add to.
1566 * @param[in,out] exts Extension instances to add to.
1567 *
1568 * @return LY_ERR values.
1569 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001570static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001571parse_mandatory(struct ly_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001572{
1573 LY_ERR ret = 0;
1574 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001575 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001576 enum yang_keyword kw;
1577
1578 if (*flags & LYS_MAND_MASK) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001579 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001580 return LY_EVALID;
1581 }
1582
1583 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001584 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001585 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001586
1587 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1588 *flags |= LYS_MAND_TRUE;
1589 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1590 *flags |= LYS_MAND_FALSE;
1591 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001592 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001593 free(buf);
1594 return LY_EVALID;
1595 }
1596 free(buf);
1597
1598 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001599 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001600
1601 switch (kw) {
1602 case YANG_CUSTOM:
1603 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MANDATORY, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02001604 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001605 break;
1606 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001607 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001608 return LY_EVALID;
1609 }
1610 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001611 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001612
1613 return ret;
1614}
1615
Michal Vaskoea5abea2018-09-18 13:10:54 +02001616/**
1617 * @brief Parse a restriction such as range or length.
1618 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001619 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001620 * @param[in,out] data Data to read from, always moved to currently handled character.
1621 * @param[in] restr_kw Type of this particular restriction.
1622 * @param[in,out] exts Extension instances to add to.
1623 *
1624 * @return LY_ERR values.
1625 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001626static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001627parse_restr(struct ly_parser_ctx *ctx, const char **data, enum yang_keyword restr_kw, struct lysp_restr *restr)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001628{
1629 LY_ERR ret = 0;
1630 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001631 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001632 enum yang_keyword kw;
1633
1634 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001635 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001636 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001637
Radek Krejci44ceedc2018-10-02 15:54:31 +02001638 INSERT_WORD(ctx, buf, restr->arg, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001639 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001640 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001641
1642 switch (kw) {
1643 case YANG_DESCRIPTION:
1644 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts);
1645 break;
1646 case YANG_REFERENCE:
1647 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts);
1648 break;
1649 case YANG_ERROR_APP_TAG:
1650 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRTAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts);
1651 break;
1652 case YANG_ERROR_MESSAGE:
1653 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRMSG, 0, &restr->emsg, Y_STR_ARG, &restr->exts);
1654 break;
1655 case YANG_CUSTOM:
1656 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts);
1657 break;
1658 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001659 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001660 return LY_EVALID;
1661 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001662 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001663 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001664 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001665
1666 return ret;
1667}
1668
Michal Vaskoea5abea2018-09-18 13:10:54 +02001669/**
1670 * @brief Parse a restriction that can have more instances such as must.
1671 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001672 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001673 * @param[in,out] data Data to read from, always moved to currently handled character.
1674 * @param[in] restr_kw Type of this particular restriction.
1675 * @param[in,out] restrs Restrictions to add to.
1676 *
1677 * @return LY_ERR values.
1678 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001679static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001680parse_restrs(struct ly_parser_ctx *ctx, const char **data, enum yang_keyword restr_kw, struct lysp_restr **restrs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001681{
1682 struct lysp_restr *restr;
1683
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001684 LYSP_ARRAY_NEW_RET(ctx->ctx, *restrs, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001685
1686 return parse_restr(ctx, data, restr_kw, restr);
1687}
1688
Michal Vaskoea5abea2018-09-18 13:10:54 +02001689/**
1690 * @brief Parse the status statement.
1691 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001692 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001693 * @param[in,out] data Data to read from, always moved to currently handled character.
1694 * @param[in,out] flags Flags to add to.
1695 * @param[in,out] exts Extension instances to add to.
1696 *
1697 * @return LY_ERR values.
1698 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001699static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001700parse_status(struct ly_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001701{
1702 LY_ERR ret = 0;
1703 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001704 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001705 enum yang_keyword kw;
1706
1707 if (*flags & LYS_STATUS_MASK) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001708 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001709 return LY_EVALID;
1710 }
1711
1712 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001713 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001714 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001715
1716 if ((word_len == 7) && !strncmp(word, "current", word_len)) {
1717 *flags |= LYS_STATUS_CURR;
1718 } else if ((word_len == 10) && !strncmp(word, "deprecated", word_len)) {
1719 *flags |= LYS_STATUS_DEPRC;
1720 } else if ((word_len == 8) && !strncmp(word, "obsolete", word_len)) {
1721 *flags |= LYS_STATUS_OBSLT;
1722 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001723 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001724 free(buf);
1725 return LY_EVALID;
1726 }
1727 free(buf);
1728
1729 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001730 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001731
1732 switch (kw) {
1733 case YANG_CUSTOM:
1734 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_STATUS, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02001735 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001736 break;
1737 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001738 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001739 return LY_EVALID;
1740 }
1741 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001742 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001743
1744 return ret;
1745}
1746
Michal Vaskoea5abea2018-09-18 13:10:54 +02001747/**
1748 * @brief Parse the when statement.
1749 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001750 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001751 * @param[in,out] data Data to read from, always moved to currently handled character.
1752 * @param[in,out] when_p When pointer to parse to.
1753 *
1754 * @return LY_ERR values.
1755 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001756static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001757parse_when(struct ly_parser_ctx *ctx, const char **data, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001758{
1759 LY_ERR ret = 0;
1760 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001761 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001762 enum yang_keyword kw;
1763 struct lysp_when *when;
1764
1765 if (*when_p) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001766 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001767 return LY_EVALID;
1768 }
1769
1770 when = calloc(1, sizeof *when);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001771 LY_CHECK_ERR_RET(!when, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001772 *when_p = when;
1773
1774 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001775 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001776 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001777
Radek Krejci44ceedc2018-10-02 15:54:31 +02001778 INSERT_WORD(ctx, buf, when->cond, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001779 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001780 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001781
1782 switch (kw) {
1783 case YANG_DESCRIPTION:
1784 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &when->dsc, Y_STR_ARG, &when->exts);
1785 break;
1786 case YANG_REFERENCE:
1787 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &when->ref, Y_STR_ARG, &when->exts);
1788 break;
1789 case YANG_CUSTOM:
1790 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &when->exts);
1791 break;
1792 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001793 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001794 return LY_EVALID;
1795 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001796 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001797 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001798 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001799
1800 return ret;
1801}
1802
Michal Vaskoea5abea2018-09-18 13:10:54 +02001803/**
1804 * @brief Parse the anydata or anyxml statement.
1805 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001806 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001807 * @param[in,out] data Data to read from, always moved to currently handled character.
1808 * @param[in] kw Type of this particular keyword.
1809 * @param[in,out] siblings Siblings to add to.
1810 *
1811 * @return LY_ERR values.
1812 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001813static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001814parse_any(struct ly_parser_ctx *ctx, const char **data, enum yang_keyword kw, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001815{
1816 LY_ERR ret = 0;
1817 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001818 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001819 struct lysp_node *iter;
1820 struct lysp_node_anydata *any;
1821
1822 /* create structure */
1823 any = calloc(1, sizeof *any);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001824 LY_CHECK_ERR_RET(!any, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001825 any->nodetype = kw == YANG_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
1826
1827 /* insert into siblings */
1828 if (!*siblings) {
1829 *siblings = (struct lysp_node *)any;
1830 } else {
1831 for (iter = *siblings; iter->next; iter = iter->next);
1832 iter->next = (struct lysp_node *)any;
1833 }
1834
1835 /* get name */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001836 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001837 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001838
Radek Krejci44ceedc2018-10-02 15:54:31 +02001839 INSERT_WORD(ctx, buf, any->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001840
1841 /* parse substatements */
1842 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001843 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001844
1845 switch (kw) {
1846 case YANG_CONFIG:
1847 ret = parse_config(ctx, data, &any->flags, &any->exts);
1848 break;
1849 case YANG_DESCRIPTION:
1850 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &any->dsc, Y_STR_ARG, &any->exts);
1851 break;
1852 case YANG_IF_FEATURE:
1853 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &any->iffeatures, Y_STR_ARG, &any->exts);
1854 break;
1855 case YANG_MANDATORY:
1856 ret = parse_mandatory(ctx, data, &any->flags, &any->exts);
1857 break;
1858 case YANG_MUST:
1859 ret = parse_restrs(ctx, data, kw, &any->musts);
1860 break;
1861 case YANG_REFERENCE:
1862 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &any->ref, Y_STR_ARG, &any->exts);
1863 break;
1864 case YANG_STATUS:
1865 ret = parse_status(ctx, data, &any->flags, &any->exts);
1866 break;
1867 case YANG_WHEN:
1868 ret = parse_when(ctx, data, &any->when);
1869 break;
1870 case YANG_CUSTOM:
1871 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &any->exts);
1872 break;
1873 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001874 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
Radek Krejcic59bc972018-09-17 16:13:06 +02001875 (any->nodetype & LYS_ANYDATA) == LYS_ANYDATA ? ly_stmt2str(YANG_ANYDATA) : ly_stmt2str(YANG_ANYXML));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001876 return LY_EVALID;
1877 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001878 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001879 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001880 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001881
1882 return ret;
1883}
1884
Michal Vaskoea5abea2018-09-18 13:10:54 +02001885/**
1886 * @brief Parse the value or position statement. Substatement of type enum statement.
1887 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001888 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001889 * @param[in,out] data Data to read from, always moved to currently handled character.
1890 * @param[in] val_kw Type of this particular keyword.
1891 * @param[in,out] value Value to write to.
1892 * @param[in,out] flags Flags to write to.
1893 * @param[in,out] exts Extension instances to add to.
1894 *
1895 * @return LY_ERR values.
1896 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001897static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001898parse_type_enum_value_pos(struct ly_parser_ctx *ctx, const char **data, enum yang_keyword val_kw, int64_t *value, uint16_t *flags,
Michal Vasko7fbc8162018-09-17 10:35:16 +02001899 struct lysp_ext_instance **exts)
1900{
1901 LY_ERR ret = 0;
1902 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001903 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001904 long int num;
1905 unsigned long int unum;
1906 enum yang_keyword kw;
1907
1908 if (*flags & LYS_SET_VALUE) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001909 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001910 return LY_EVALID;
1911 }
1912 *flags |= LYS_SET_VALUE;
1913
1914 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001915 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001916 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001917
1918 if (!word_len || (word[0] == '+') || ((word[0] == '0') && (word_len > 1)) || ((val_kw == YANG_VALUE) && !strncmp(word, "-0", 2))) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001919 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001920 free(buf);
1921 return LY_EVALID;
1922 }
1923
1924 errno = 0;
1925 if (val_kw == YANG_VALUE) {
1926 num = strtol(word, &ptr, 10);
1927 } else {
1928 unum = strtoul(word, &ptr, 10);
1929 }
1930 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001931 if ((size_t)(ptr - word) != word_len) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001932 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001933 free(buf);
1934 return LY_EVALID;
1935 }
1936 if (errno == ERANGE) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001937 LOGVAL_YANG(ctx, LY_VCODE_OOB, word_len, word, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001938 free(buf);
1939 return LY_EVALID;
1940 }
1941 if (val_kw == YANG_VALUE) {
1942 *value = num;
1943 } else {
1944 *value = unum;
1945 }
1946 free(buf);
1947
1948 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001949 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001950
1951 switch (kw) {
1952 case YANG_CUSTOM:
1953 ret = parse_ext(ctx, data, word, word_len, val_kw == YANG_VALUE ? LYEXT_SUBSTMT_VALUE : LYEXT_SUBSTMT_POSITION, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02001954 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001955 break;
1956 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001957 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001958 return LY_EVALID;
1959 }
1960 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001961 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001962
1963 return ret;
1964}
1965
Michal Vaskoea5abea2018-09-18 13:10:54 +02001966/**
1967 * @brief Parse the enum or bit statement. Substatement of type statement.
1968 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001969 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001970 * @param[in,out] data Data to read from, always moved to currently handled character.
1971 * @param[in] enum_kw Type of this particular keyword.
1972 * @param[in,out] enums Enums or bits to add to.
1973 *
1974 * @return LY_ERR values.
1975 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001976static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001977parse_type_enum(struct ly_parser_ctx *ctx, const char **data, enum yang_keyword enum_kw, struct lysp_type_enum **enums)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001978{
1979 LY_ERR ret = 0;
1980 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001981 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001982 enum yang_keyword kw;
1983 struct lysp_type_enum *enm;
1984
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001985 LYSP_ARRAY_NEW_RET(ctx->ctx, *enums, enm, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001986
1987 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001988 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001989 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001990
Radek Krejci44ceedc2018-10-02 15:54:31 +02001991 INSERT_WORD(ctx, buf, enm->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001992 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001993 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001994
1995 switch (kw) {
1996 case YANG_DESCRIPTION:
1997 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &enm->dsc, Y_STR_ARG, &enm->exts);
1998 break;
1999 case YANG_IF_FEATURE:
2000 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &enm->iffeatures, Y_STR_ARG, &enm->exts);
2001 break;
2002 case YANG_REFERENCE:
2003 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &enm->ref, Y_STR_ARG, &enm->exts);
2004 break;
2005 case YANG_STATUS:
2006 ret = parse_status(ctx, data, &enm->flags, &enm->exts);
2007 break;
2008 case YANG_VALUE:
2009 case YANG_POSITION:
2010 ret = parse_type_enum_value_pos(ctx, data, kw, &enm->value, &enm->flags, &enm->exts);
2011 break;
2012 case YANG_CUSTOM:
2013 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &enm->exts);
2014 break;
2015 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002016 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002017 return LY_EVALID;
2018 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002019 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002020 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002021 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002022
2023 return ret;
2024}
2025
Michal Vaskoea5abea2018-09-18 13:10:54 +02002026/**
2027 * @brief Parse the fraction-digits statement. Substatement of type statement.
2028 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002029 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002030 * @param[in,out] data Data to read from, always moved to currently handled character.
2031 * @param[in,out] fracdig Value to write to.
2032 * @param[in,out] exts Extension instances to add to.
2033 *
2034 * @return LY_ERR values.
2035 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002036static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002037parse_type_fracdigits(struct ly_parser_ctx *ctx, const char **data, uint8_t *fracdig, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002038{
2039 LY_ERR ret = 0;
2040 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002041 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002042 unsigned long int num;
2043 enum yang_keyword kw;
2044
2045 if (*fracdig) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002046 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002047 return LY_EVALID;
2048 }
2049
2050 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002051 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002052 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002053
2054 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002055 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002056 free(buf);
2057 return LY_EVALID;
2058 }
2059
2060 errno = 0;
2061 num = strtoul(word, &ptr, 10);
2062 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002063 if ((size_t)(ptr - word) != word_len) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002064 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002065 free(buf);
2066 return LY_EVALID;
2067 }
2068 if ((errno == ERANGE) || (num > 18)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002069 LOGVAL_YANG(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002070 free(buf);
2071 return LY_EVALID;
2072 }
2073 *fracdig = num;
2074 free(buf);
2075
2076 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002077 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002078
2079 switch (kw) {
2080 case YANG_CUSTOM:
2081 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_FRACDIGITS, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02002082 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002083 break;
2084 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002085 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002086 return LY_EVALID;
2087 }
2088 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002089 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002090
2091 return ret;
2092}
2093
Michal Vaskoea5abea2018-09-18 13:10:54 +02002094/**
2095 * @brief Parse the require-instance statement. Substatement of type statement.
2096 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002097 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002098 * @param[in,out] data Data to read from, always moved to currently handled character.
2099 * @param[in,out] reqinst Value to write to.
2100 * @param[in,out] flags Flags to write to.
2101 * @param[in,out] exts Extension instances to add to.
2102 *
2103 * @return LY_ERR values.
2104 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002105static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002106parse_type_reqinstance(struct ly_parser_ctx *ctx, const char **data, uint8_t *reqinst, uint16_t *flags,
Michal Vasko7fbc8162018-09-17 10:35:16 +02002107 struct lysp_ext_instance **exts)
2108{
2109 LY_ERR ret = 0;
2110 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002111 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002112 enum yang_keyword kw;
2113
2114 if (*flags & LYS_SET_REQINST) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002115 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002116 return LY_EVALID;
2117 }
2118 *flags |= LYS_SET_REQINST;
2119
2120 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002121 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002122 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002123
2124 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
2125 *reqinst = 1;
2126 } else if ((word_len != 5) || strncmp(word, "false", word_len)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002127 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002128 free(buf);
2129 return LY_EVALID;
2130 }
2131 free(buf);
2132
2133 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002134 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002135
2136 switch (kw) {
2137 case YANG_CUSTOM:
2138 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_REQINSTANCE, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02002139 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002140 break;
2141 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002142 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002143 return LY_EVALID;
2144 }
2145 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002146 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002147
2148 return ret;
2149}
2150
Michal Vaskoea5abea2018-09-18 13:10:54 +02002151/**
2152 * @brief Parse the modifier statement. Substatement of type pattern statement.
2153 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002154 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002155 * @param[in,out] data Data to read from, always moved to currently handled character.
2156 * @param[in,out] pat Value to write to.
2157 * @param[in,out] exts Extension instances to add to.
2158 *
2159 * @return LY_ERR values.
2160 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002161static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002162parse_type_pattern_modifier(struct ly_parser_ctx *ctx, const char **data, const char **pat, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002163{
2164 LY_ERR ret = 0;
2165 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002166 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002167 enum yang_keyword kw;
2168
2169 if ((*pat)[0] == 0x15) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002170 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002171 return LY_EVALID;
2172 }
2173
2174 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002175 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002176 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002177
2178 if ((word_len != 12) || strncmp(word, "invert-match", word_len)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002179 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002180 free(buf);
2181 return LY_EVALID;
2182 }
2183 free(buf);
2184
2185 /* replace the value in the dictionary */
2186 buf = malloc(strlen(*pat) + 1);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002187 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002188 strcpy(buf, *pat);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002189 lydict_remove(ctx->ctx, *pat);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002190
2191 assert(buf[0] == 0x06);
2192 buf[0] = 0x15;
Radek Krejci44ceedc2018-10-02 15:54:31 +02002193 *pat = lydict_insert_zc(ctx->ctx, buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002194
2195 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002196 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002197
2198 switch (kw) {
2199 case YANG_CUSTOM:
2200 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MODIFIER, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02002201 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002202 break;
2203 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002204 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002205 return LY_EVALID;
2206 }
2207 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002208 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002209
2210 return ret;
2211}
2212
Michal Vaskoea5abea2018-09-18 13:10:54 +02002213/**
2214 * @brief Parse the pattern statement. Substatement of type statement.
2215 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002216 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002217 * @param[in,out] data Data to read from, always moved to currently handled character.
2218 * @param[in,out] patterns Restrictions to add to.
2219 *
2220 * @return LY_ERR values.
2221 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002222static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002223parse_type_pattern(struct ly_parser_ctx *ctx, const char **data, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002224{
2225 LY_ERR ret = 0;
2226 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002227 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002228 enum yang_keyword kw;
2229 struct lysp_restr *restr;
2230
Radek Krejcie53a8dc2018-10-17 12:52:40 +02002231 LYSP_ARRAY_NEW_RET(ctx->ctx, *patterns, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002232
2233 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002234 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002235 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002236
2237 /* add special meaning first byte */
2238 if (buf) {
2239 buf = realloc(buf, word_len + 2);
2240 word = buf;
2241 } else {
2242 buf = malloc(word_len + 2);
2243 }
Radek Krejci44ceedc2018-10-02 15:54:31 +02002244 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejci86d106e2018-10-18 09:53:19 +02002245 memmove(buf + 1, word, word_len);
2246 buf[0] = 0x06; /* pattern's default regular-match flag */
2247 buf[word_len + 1] = '\0'; /* terminating NULL byte */
2248 restr->arg = lydict_insert_zc(ctx->ctx, buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002249
2250 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002251 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002252
2253 switch (kw) {
2254 case YANG_DESCRIPTION:
2255 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts);
2256 break;
2257 case YANG_REFERENCE:
2258 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts);
2259 break;
2260 case YANG_ERROR_APP_TAG:
2261 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRTAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts);
2262 break;
2263 case YANG_ERROR_MESSAGE:
2264 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRMSG, 0, &restr->emsg, Y_STR_ARG, &restr->exts);
2265 break;
2266 case YANG_MODIFIER:
2267 ret = parse_type_pattern_modifier(ctx, data, &restr->arg, &restr->exts);
2268 break;
2269 case YANG_CUSTOM:
2270 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts);
2271 break;
2272 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002273 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002274 return LY_EVALID;
2275 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002276 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002277 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002278 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002279
2280 return ret;
2281}
2282
Michal Vaskoea5abea2018-09-18 13:10:54 +02002283/**
2284 * @brief Parse the type statement.
2285 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002286 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002287 * @param[in,out] data Data to read from, always moved to currently handled character.
2288 * @param[in,out] type Type to wrote to.
2289 *
2290 * @return LY_ERR values.
2291 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002292static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002293parse_type(struct ly_parser_ctx *ctx, const char **data, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002294{
2295 LY_ERR ret = 0;
2296 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002297 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002298 enum yang_keyword kw;
2299 struct lysp_type *nest_type;
2300
2301 if (type->name) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002302 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002303 return LY_EVALID;
2304 }
2305
2306 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002307 ret = get_argument(ctx, data, Y_PREF_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002308 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002309
Radek Krejci44ceedc2018-10-02 15:54:31 +02002310 INSERT_WORD(ctx, buf, type->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002311 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002312 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002313
2314 switch (kw) {
2315 case YANG_BASE:
2316 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_BASE, &type->bases, Y_PREF_IDENTIF_ARG, &type->exts);
2317 break;
2318 case YANG_BIT:
2319 ret = parse_type_enum(ctx, data, kw, &type->bits);
2320 break;
2321 case YANG_ENUM:
2322 ret = parse_type_enum(ctx, data, kw, &type->enums);
2323 break;
2324 case YANG_FRACTION_DIGITS:
2325 ret = parse_type_fracdigits(ctx, data, &type->fraction_digits, &type->exts);
2326 break;
2327 case YANG_LENGTH:
2328 if (type->length) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002329 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002330 return LY_EVALID;
2331 }
2332 type->length = calloc(1, sizeof *type->length);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002333 LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002334
2335 ret = parse_restr(ctx, data, kw, type->length);
2336 break;
2337 case YANG_PATH:
2338 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_PATH, 0, &type->path, Y_STR_ARG, &type->exts);
2339 break;
2340 case YANG_PATTERN:
2341 ret = parse_type_pattern(ctx, data, &type->patterns);
2342 break;
2343 case YANG_RANGE:
2344 if (type->range) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002345 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002346 return LY_EVALID;
2347 }
2348 type->range = calloc(1, sizeof *type->range);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002349 LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx->ctx), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002350
2351 ret = parse_restr(ctx, data, kw, type->range);
2352 break;
2353 case YANG_REQUIRE_INSTANCE:
2354 ret = parse_type_reqinstance(ctx, data, &type->require_instance, &type->flags, &type->exts);
2355 break;
2356 case YANG_TYPE:
2357 {
Radek Krejcie53a8dc2018-10-17 12:52:40 +02002358 LYSP_ARRAY_NEW_RET(ctx->ctx, type->types, nest_type, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002359 }
2360 ret = parse_type(ctx, data, nest_type);
2361 break;
2362 case YANG_CUSTOM:
2363 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &type->exts);
2364 break;
2365 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002366 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002367 return LY_EVALID;
2368 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002369 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002370 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002371 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002372
2373 return ret;
2374}
2375
Michal Vaskoea5abea2018-09-18 13:10:54 +02002376/**
2377 * @brief Parse the leaf statement.
2378 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002379 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002380 * @param[in,out] data Data to read from, always moved to currently handled character.
2381 * @param[in,out] siblings Siblings to add to.
2382 *
2383 * @return LY_ERR values.
2384 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002385static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002386parse_leaf(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002387{
2388 LY_ERR ret = 0;
2389 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002390 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002391 enum yang_keyword kw;
2392 struct lysp_node *iter;
2393 struct lysp_node_leaf *leaf;
2394
2395 /* create structure */
2396 leaf = calloc(1, sizeof *leaf);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002397 LY_CHECK_ERR_RET(!leaf, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002398 leaf->nodetype = LYS_LEAF;
2399
2400 /* insert into siblings */
2401 if (!*siblings) {
2402 *siblings = (struct lysp_node *)leaf;
2403 } else {
2404 for (iter = *siblings; iter->next; iter = iter->next);
2405 iter->next = (struct lysp_node *)leaf;
2406 }
2407
2408 /* get name */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002409 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002410 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002411
Radek Krejci44ceedc2018-10-02 15:54:31 +02002412 INSERT_WORD(ctx, buf, leaf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002413
2414 /* parse substatements */
2415 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002416 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002417
2418 switch (kw) {
2419 case YANG_CONFIG:
2420 ret = parse_config(ctx, data, &leaf->flags, &leaf->exts);
2421 break;
2422 case YANG_DEFAULT:
2423 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &leaf->dflt, Y_STR_ARG, &leaf->exts);
2424 break;
2425 case YANG_DESCRIPTION:
2426 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &leaf->dsc, Y_STR_ARG, &leaf->exts);
2427 break;
2428 case YANG_IF_FEATURE:
2429 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &leaf->iffeatures, Y_STR_ARG, &leaf->exts);
2430 break;
2431 case YANG_MANDATORY:
2432 ret = parse_mandatory(ctx, data, &leaf->flags, &leaf->exts);
2433 break;
2434 case YANG_MUST:
2435 ret = parse_restrs(ctx, data, kw, &leaf->musts);
2436 break;
2437 case YANG_REFERENCE:
2438 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &leaf->ref, Y_STR_ARG, &leaf->exts);
2439 break;
2440 case YANG_STATUS:
2441 ret = parse_status(ctx, data, &leaf->flags, &leaf->exts);
2442 break;
2443 case YANG_TYPE:
2444 ret = parse_type(ctx, data, &leaf->type);
2445 break;
2446 case YANG_UNITS:
2447 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, &leaf->units, Y_STR_ARG, &leaf->exts);
2448 break;
2449 case YANG_WHEN:
2450 ret = parse_when(ctx, data, &leaf->when);
2451 break;
2452 case YANG_CUSTOM:
2453 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &leaf->exts);
2454 break;
2455 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002456 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002457 return LY_EVALID;
2458 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002459 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002460 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002461 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002462
2463 /* mandatory substatements */
2464 if (!leaf->type.name) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002465 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002466 return LY_EVALID;
2467 }
2468
2469 return ret;
2470}
2471
Michal Vaskoea5abea2018-09-18 13:10:54 +02002472/**
2473 * @brief Parse the max-elements statement.
2474 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002475 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002476 * @param[in,out] data Data to read from, always moved to currently handled character.
2477 * @param[in,out] max Value to write to.
2478 * @param[in,out] flags Flags to write to.
2479 * @param[in,out] exts Extension instances to add to.
2480 *
2481 * @return LY_ERR values.
2482 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002483static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002484parse_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 +02002485{
2486 LY_ERR ret = 0;
2487 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002488 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002489 unsigned long int num;
2490 enum yang_keyword kw;
2491
2492 if (*flags & LYS_SET_MAX) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002493 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002494 return LY_EVALID;
2495 }
2496 *flags |= LYS_SET_MAX;
2497
2498 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002499 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002500 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002501
2502 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002503 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002504 free(buf);
2505 return LY_EVALID;
2506 }
2507
2508 if (strncmp(word, "unbounded", word_len)) {
2509 errno = 0;
2510 num = strtoul(word, &ptr, 10);
2511 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002512 if ((size_t)(ptr - word) != word_len) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002513 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002514 free(buf);
2515 return LY_EVALID;
2516 }
2517 if ((errno == ERANGE) || (num > UINT32_MAX)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002518 LOGVAL_YANG(ctx, LY_VCODE_OOB, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002519 free(buf);
2520 return LY_EVALID;
2521 }
2522
2523 *max = num;
2524 }
2525 free(buf);
2526
2527 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002528 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002529
2530 switch (kw) {
2531 case YANG_CUSTOM:
2532 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MAX, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02002533 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002534 break;
2535 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002536 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002537 return LY_EVALID;
2538 }
2539 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002540 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002541
2542 return ret;
2543}
2544
Michal Vaskoea5abea2018-09-18 13:10:54 +02002545/**
2546 * @brief Parse the min-elements statement.
2547 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002548 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002549 * @param[in,out] data Data to read from, always moved to currently handled character.
2550 * @param[in,out] min Value to write to.
2551 * @param[in,out] flags Flags to write to.
2552 * @param[in,out] exts Extension instances to add to.
2553 *
2554 * @return LY_ERR values.
2555 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002556static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002557parse_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 +02002558{
2559 LY_ERR ret = 0;
2560 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002561 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002562 unsigned long int num;
2563 enum yang_keyword kw;
2564
2565 if (*flags & LYS_SET_MIN) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002566 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002567 return LY_EVALID;
2568 }
2569 *flags |= LYS_SET_MIN;
2570
2571 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002572 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002573 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002574
2575 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002576 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002577 free(buf);
2578 return LY_EVALID;
2579 }
2580
2581 errno = 0;
2582 num = strtoul(word, &ptr, 10);
2583 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002584 if ((size_t)(ptr - word) != word_len) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002585 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002586 free(buf);
2587 return LY_EVALID;
2588 }
2589 if ((errno == ERANGE) || (num > UINT32_MAX)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002590 LOGVAL_YANG(ctx, LY_VCODE_OOB, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002591 free(buf);
2592 return LY_EVALID;
2593 }
2594 *min = num;
2595 free(buf);
2596
2597 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002598 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002599
2600 switch (kw) {
2601 case YANG_CUSTOM:
2602 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MIN, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02002603 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002604 break;
2605 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002606 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002607 return LY_EVALID;
2608 }
2609 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002610 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002611
2612 return ret;
2613}
2614
Michal Vaskoea5abea2018-09-18 13:10:54 +02002615/**
2616 * @brief Parse the ordered-by statement.
2617 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002618 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002619 * @param[in,out] data Data to read from, always moved to currently handled character.
2620 * @param[in,out] flags Flags to write to.
2621 * @param[in,out] exts Extension instances to add to.
2622 *
2623 * @return LY_ERR values.
2624 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002625static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002626parse_orderedby(struct ly_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002627{
2628 LY_ERR ret = 0;
2629 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002630 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002631 enum yang_keyword kw;
2632
2633 if (*flags & LYS_ORDBY_MASK) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002634 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002635 return LY_EVALID;
2636 }
2637
2638 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002639 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002640 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002641
2642 if ((word_len == 6) && !strncmp(word, "system", word_len)) {
2643 *flags |= LYS_ORDBY_SYSTEM;
2644 } else if ((word_len == 4) && !strncmp(word, "user", word_len)) {
2645 *flags |= LYS_ORDBY_USER;
2646 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002647 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002648 free(buf);
2649 return LY_EVALID;
2650 }
2651 free(buf);
2652
2653 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002654 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002655
2656 switch (kw) {
2657 case YANG_CUSTOM:
2658 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_ORDEREDBY, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02002659 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002660 break;
2661 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002662 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002663 return LY_EVALID;
2664 }
2665 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002666 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002667
2668 return ret;
2669}
2670
Michal Vaskoea5abea2018-09-18 13:10:54 +02002671/**
2672 * @brief Parse the leaf-list statement.
2673 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002674 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002675 * @param[in,out] data Data to read from, always moved to currently handled character.
2676 * @param[in,out] siblings Siblings to add to.
2677 *
2678 * @return LY_ERR values.
2679 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002680static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002681parse_leaflist(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002682{
2683 LY_ERR ret = 0;
2684 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002685 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002686 enum yang_keyword kw;
2687 struct lysp_node *iter;
2688 struct lysp_node_leaflist *llist;
2689
2690 /* create structure */
2691 llist = calloc(1, sizeof *llist);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002692 LY_CHECK_ERR_RET(!llist, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002693 llist->nodetype = LYS_LEAFLIST;
2694
2695 /* insert into siblings */
2696 if (!*siblings) {
2697 *siblings = (struct lysp_node *)llist;
2698 } else {
2699 for (iter = *siblings; iter->next; iter = iter->next);
2700 iter->next = (struct lysp_node *)llist;
2701 }
2702
2703 /* get name */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002704 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002705 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002706
Radek Krejci44ceedc2018-10-02 15:54:31 +02002707 INSERT_WORD(ctx, buf, llist->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002708
2709 /* parse substatements */
2710 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002711 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002712
2713 switch (kw) {
2714 case YANG_CONFIG:
2715 ret = parse_config(ctx, data, &llist->flags, &llist->exts);
2716 break;
2717 case YANG_DEFAULT:
2718 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_DEFAULT, &llist->dflts, Y_STR_ARG, &llist->exts);
2719 break;
2720 case YANG_DESCRIPTION:
2721 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &llist->dsc, Y_STR_ARG, &llist->exts);
2722 break;
2723 case YANG_IF_FEATURE:
2724 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &llist->iffeatures, Y_STR_ARG, &llist->exts);
2725 break;
2726 case YANG_MAX_ELEMENTS:
2727 ret = parse_maxelements(ctx, data, &llist->max, &llist->flags, &llist->exts);
2728 break;
2729 case YANG_MIN_ELEMENTS:
2730 ret = parse_minelements(ctx, data, &llist->min, &llist->flags, &llist->exts);
2731 break;
2732 case YANG_MUST:
2733 ret = parse_restrs(ctx, data, kw, &llist->musts);
2734 break;
2735 case YANG_ORDERED_BY:
2736 ret = parse_orderedby(ctx, data, &llist->flags, &llist->exts);
2737 break;
2738 case YANG_REFERENCE:
2739 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &llist->ref, Y_STR_ARG, &llist->exts);
2740 break;
2741 case YANG_STATUS:
2742 ret = parse_status(ctx, data, &llist->flags, &llist->exts);
2743 break;
2744 case YANG_TYPE:
2745 ret = parse_type(ctx, data, &llist->type);
2746 break;
2747 case YANG_UNITS:
2748 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, &llist->units, Y_STR_ARG, &llist->exts);
2749 break;
2750 case YANG_WHEN:
2751 ret = parse_when(ctx, data, &llist->when);
2752 break;
2753 case YANG_CUSTOM:
2754 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &llist->exts);
2755 break;
2756 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002757 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002758 return LY_EVALID;
2759 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002760 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002761 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002762 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002763
2764 /* mandatory substatements */
2765 if (!llist->type.name) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002766 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002767 return LY_EVALID;
2768 }
2769
2770 return ret;
2771}
2772
Michal Vaskoea5abea2018-09-18 13:10:54 +02002773/**
2774 * @brief Parse the refine statement.
2775 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002776 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002777 * @param[in,out] data Data to read from, always moved to currently handled character.
2778 * @param[in,out] refines Refines to add to.
2779 *
2780 * @return LY_ERR values.
2781 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002782static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002783parse_refine(struct ly_parser_ctx *ctx, const char **data, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002784{
2785 LY_ERR ret = 0;
2786 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002787 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002788 enum yang_keyword kw;
2789 struct lysp_refine *rf;
2790
Radek Krejcie53a8dc2018-10-17 12:52:40 +02002791 LYSP_ARRAY_NEW_RET(ctx->ctx, *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002792
2793 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002794 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002795 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002796
Radek Krejci44ceedc2018-10-02 15:54:31 +02002797 INSERT_WORD(ctx, buf, rf->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002798 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002799 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002800
2801 switch (kw) {
2802 case YANG_CONFIG:
2803 ret = parse_config(ctx, data, &rf->flags, &rf->exts);
2804 break;
2805 case YANG_DEFAULT:
2806 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_DEFAULT, &rf->dflts, Y_STR_ARG, &rf->exts);
2807 break;
2808 case YANG_DESCRIPTION:
2809 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &rf->dsc, Y_STR_ARG, &rf->exts);
2810 break;
2811 case YANG_IF_FEATURE:
2812 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &rf->iffeatures, Y_STR_ARG, &rf->exts);
2813 break;
2814 case YANG_MAX_ELEMENTS:
2815 ret = parse_maxelements(ctx, data, &rf->max, &rf->flags, &rf->exts);
2816 break;
2817 case YANG_MIN_ELEMENTS:
2818 ret = parse_minelements(ctx, data, &rf->min, &rf->flags, &rf->exts);
2819 break;
2820 case YANG_MUST:
2821 ret = parse_restrs(ctx, data, kw, &rf->musts);
2822 break;
2823 case YANG_MANDATORY:
2824 ret = parse_mandatory(ctx, data, &rf->flags, &rf->exts);
2825 break;
2826 case YANG_REFERENCE:
2827 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &rf->ref, Y_STR_ARG, &rf->exts);
2828 break;
2829 case YANG_PRESENCE:
2830 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_PRESENCE, 0, &rf->presence, Y_STR_ARG, &rf->exts);
2831 break;
2832 case YANG_CUSTOM:
2833 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rf->exts);
2834 break;
2835 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002836 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002837 return LY_EVALID;
2838 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002839 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002840 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002841 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002842
2843 return ret;
2844}
2845
Michal Vaskoea5abea2018-09-18 13:10:54 +02002846/**
2847 * @brief Parse the typedef statement.
2848 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002849 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002850 * @param[in,out] data Data to read from, always moved to currently handled character.
2851 * @param[in,out] typedefs Typedefs to add to.
2852 *
2853 * @return LY_ERR values.
2854 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002855static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002856parse_typedef(struct ly_parser_ctx *ctx, const char **data, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002857{
2858 LY_ERR ret = 0;
2859 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002860 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002861 enum yang_keyword kw;
2862 struct lysp_tpdf *tpdf;
2863
Radek Krejcie53a8dc2018-10-17 12:52:40 +02002864 LYSP_ARRAY_NEW_RET(ctx->ctx, *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002865
2866 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002867 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002868 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002869
Radek Krejci44ceedc2018-10-02 15:54:31 +02002870 INSERT_WORD(ctx, buf, tpdf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002871
2872 /* parse substatements */
2873 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002874 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002875
2876 switch (kw) {
2877 case YANG_DEFAULT:
2878 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &tpdf->dflt, Y_STR_ARG, &tpdf->exts);
2879 break;
2880 case YANG_DESCRIPTION:
2881 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &tpdf->dsc, Y_STR_ARG, &tpdf->exts);
2882 break;
2883 case YANG_REFERENCE:
2884 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &tpdf->ref, Y_STR_ARG, &tpdf->exts);
2885 break;
2886 case YANG_STATUS:
2887 ret = parse_status(ctx, data, &tpdf->flags, &tpdf->exts);
2888 break;
2889 case YANG_TYPE:
2890 ret = parse_type(ctx, data, &tpdf->type);
2891 break;
2892 case YANG_UNITS:
2893 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, &tpdf->units, Y_STR_ARG, &tpdf->exts);
2894 break;
2895 case YANG_CUSTOM:
2896 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &tpdf->exts);
2897 break;
2898 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002899 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002900 return LY_EVALID;
2901 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002902 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002903 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002904 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002905
2906 /* mandatory substatements */
2907 if (!tpdf->type.name) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002908 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002909 return LY_EVALID;
2910 }
2911
2912 return ret;
2913}
2914
Michal Vaskoea5abea2018-09-18 13:10:54 +02002915/**
2916 * @brief Parse the input or output statement.
2917 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002918 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002919 * @param[in,out] data Data to read from, always moved to currently handled character.
2920 * @param[in] kw Type of this particular keyword
2921 * @param[in,out] inout_p Input/output pointer to write to.
2922 *
2923 * @return LY_ERR values.
2924 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002925static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002926parse_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 +02002927{
2928 LY_ERR ret = 0;
2929 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002930 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002931 struct lysp_action_inout *inout;
2932
2933 if (*inout_p) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002934 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002935 return LY_EVALID;
2936 }
2937
2938 /* create structure */
2939 inout = calloc(1, sizeof *inout);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002940 LY_CHECK_ERR_RET(!inout, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002941 *inout_p = inout;
2942
2943 /* parse substatements */
2944 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002945 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002946
2947 switch (kw) {
2948 case YANG_ANYDATA:
2949 case YANG_ANYXML:
2950 ret = parse_any(ctx, data, kw, &inout->data);
2951 break;
2952 case YANG_CHOICE:
2953 ret = parse_choice(ctx, data, &inout->data);
2954 break;
2955 case YANG_CONTAINER:
2956 ret = parse_container(ctx, data, &inout->data);
2957 break;
2958 case YANG_LEAF:
2959 ret = parse_leaf(ctx, data, &inout->data);
2960 break;
2961 case YANG_LEAF_LIST:
2962 ret = parse_leaflist(ctx, data, &inout->data);
2963 break;
2964 case YANG_LIST:
2965 ret = parse_list(ctx, data, &inout->data);
2966 break;
2967 case YANG_USES:
2968 ret = parse_uses(ctx, data, &inout->data);
2969 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002970 case YANG_TYPEDEF:
2971 ret = parse_typedef(ctx, data, &inout->typedefs);
2972 break;
2973 case YANG_MUST:
2974 ret = parse_restrs(ctx, data, kw, &inout->musts);
2975 break;
2976 case YANG_GROUPING:
2977 ret = parse_grouping(ctx, data, &inout->groupings);
2978 break;
2979 case YANG_CUSTOM:
2980 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inout->exts);
2981 break;
2982 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002983 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "input/output");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002984 return LY_EVALID;
2985 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002986 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002987 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002988 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002989
2990 return ret;
2991}
2992
Michal Vaskoea5abea2018-09-18 13:10:54 +02002993/**
2994 * @brief Parse the action statement.
2995 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002996 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002997 * @param[in,out] data Data to read from, always moved to currently handled character.
2998 * @param[in,out] actions Actions to add to.
2999 *
3000 * @return LY_ERR values.
3001 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003002static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003003parse_action(struct ly_parser_ctx *ctx, const char **data, struct lysp_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003004{
3005 LY_ERR ret = 0;
3006 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003007 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003008 enum yang_keyword kw;
3009 struct lysp_action *act;
3010
Radek Krejcie53a8dc2018-10-17 12:52:40 +02003011 LYSP_ARRAY_NEW_RET(ctx->ctx, *actions, act, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003012
3013 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003014 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003015 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003016
Radek Krejci44ceedc2018-10-02 15:54:31 +02003017 INSERT_WORD(ctx, buf, act->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003018 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003019 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003020
3021 switch (kw) {
3022 case YANG_DESCRIPTION:
3023 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &act->dsc, Y_STR_ARG, &act->exts);
3024 break;
3025 case YANG_IF_FEATURE:
3026 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &act->iffeatures, Y_STR_ARG, &act->exts);
3027 break;
3028 case YANG_REFERENCE:
3029 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &act->ref, Y_STR_ARG, &act->exts);
3030 break;
3031 case YANG_STATUS:
3032 ret = parse_status(ctx, data, &act->flags, &act->exts);
3033 break;
3034
3035 case YANG_INPUT:
3036 ret = parse_inout(ctx, data, kw, &act->input);
3037 break;
3038 case YANG_OUTPUT:
3039 ret = parse_inout(ctx, data, kw, &act->output);
3040 break;
3041
3042 case YANG_TYPEDEF:
3043 ret = parse_typedef(ctx, data, &act->typedefs);
3044 break;
3045 case YANG_GROUPING:
3046 ret = parse_grouping(ctx, data, &act->groupings);
3047 break;
3048 case YANG_CUSTOM:
3049 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &act->exts);
3050 break;
3051 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003052 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "action");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003053 return LY_EVALID;
3054 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003055 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003056 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003057 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003058
3059 return ret;
3060}
3061
Michal Vaskoea5abea2018-09-18 13:10:54 +02003062/**
3063 * @brief Parse the notification statement.
3064 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003065 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003066 * @param[in,out] data Data to read from, always moved to currently handled character.
3067 * @param[in,out] notifs Notifications to add to.
3068 *
3069 * @return LY_ERR values.
3070 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003071static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003072parse_notif(struct ly_parser_ctx *ctx, const char **data, struct lysp_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003073{
3074 LY_ERR ret = 0;
3075 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003076 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003077 enum yang_keyword kw;
3078 struct lysp_notif *notif;
3079
Radek Krejcie53a8dc2018-10-17 12:52:40 +02003080 LYSP_ARRAY_NEW_RET(ctx->ctx, *notifs, notif, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003081
3082 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003083 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003084 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003085
Radek Krejci44ceedc2018-10-02 15:54:31 +02003086 INSERT_WORD(ctx, buf, notif->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003087 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003088 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003089
3090 switch (kw) {
3091 case YANG_DESCRIPTION:
3092 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &notif->dsc, Y_STR_ARG, &notif->exts);
3093 break;
3094 case YANG_IF_FEATURE:
3095 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &notif->iffeatures, Y_STR_ARG, &notif->exts);
3096 break;
3097 case YANG_REFERENCE:
3098 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &notif->ref, Y_STR_ARG, &notif->exts);
3099 break;
3100 case YANG_STATUS:
3101 ret = parse_status(ctx, data, &notif->flags, &notif->exts);
3102 break;
3103
3104 case YANG_ANYDATA:
3105 case YANG_ANYXML:
3106 ret = parse_any(ctx, data, kw, &notif->data);
3107 break;
3108 case YANG_CHOICE:
3109 ret = parse_case(ctx, data, &notif->data);
3110 break;
3111 case YANG_CONTAINER:
3112 ret = parse_container(ctx, data, &notif->data);
3113 break;
3114 case YANG_LEAF:
3115 ret = parse_leaf(ctx, data, &notif->data);
3116 break;
3117 case YANG_LEAF_LIST:
3118 ret = parse_leaflist(ctx, data, &notif->data);
3119 break;
3120 case YANG_LIST:
3121 ret = parse_list(ctx, data, &notif->data);
3122 break;
3123 case YANG_USES:
3124 ret = parse_uses(ctx, data, &notif->data);
3125 break;
3126
3127 case YANG_MUST:
3128 ret = parse_restrs(ctx, data, kw, &notif->musts);
3129 break;
3130 case YANG_TYPEDEF:
3131 ret = parse_typedef(ctx, data, &notif->typedefs);
3132 break;
3133 case YANG_GROUPING:
3134 ret = parse_grouping(ctx, data, &notif->groupings);
3135 break;
3136 case YANG_CUSTOM:
3137 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &notif->exts);
3138 break;
3139 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003140 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003141 return LY_EVALID;
3142 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003143 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003144 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003145 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003146
3147 return ret;
3148}
3149
Michal Vaskoea5abea2018-09-18 13:10:54 +02003150/**
3151 * @brief Parse the grouping statement.
3152 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003153 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003154 * @param[in,out] data Data to read from, always moved to currently handled character.
3155 * @param[in,out] groupings Groupings to add to.
3156 *
3157 * @return LY_ERR values.
3158 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003159static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003160parse_grouping(struct ly_parser_ctx *ctx, const char **data, struct lysp_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003161{
3162 LY_ERR ret = 0;
3163 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003164 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003165 enum yang_keyword kw;
3166 struct lysp_grp *grp;
3167
Radek Krejcie53a8dc2018-10-17 12:52:40 +02003168 LYSP_ARRAY_NEW_RET(ctx->ctx, *groupings, grp, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003169
3170 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003171 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003172 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003173
Radek Krejci44ceedc2018-10-02 15:54:31 +02003174 INSERT_WORD(ctx, buf, grp->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003175
3176 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003177 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003178
3179 switch (kw) {
3180 case YANG_DESCRIPTION:
3181 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &grp->dsc, Y_STR_ARG, &grp->exts);
3182 break;
3183 case YANG_REFERENCE:
3184 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &grp->ref, Y_STR_ARG, &grp->exts);
3185 break;
3186 case YANG_STATUS:
3187 ret = parse_status(ctx, data, &grp->flags, &grp->exts);
3188 break;
3189
3190 case YANG_ANYDATA:
3191 case YANG_ANYXML:
3192 ret = parse_any(ctx, data, kw, &grp->data);
3193 break;
3194 case YANG_CHOICE:
3195 ret = parse_choice(ctx, data, &grp->data);
3196 break;
3197 case YANG_CONTAINER:
3198 ret = parse_container(ctx, data, &grp->data);
3199 break;
3200 case YANG_LEAF:
3201 ret = parse_leaf(ctx, data, &grp->data);
3202 break;
3203 case YANG_LEAF_LIST:
3204 ret = parse_leaflist(ctx, data, &grp->data);
3205 break;
3206 case YANG_LIST:
3207 ret = parse_list(ctx, data, &grp->data);
3208 break;
3209 case YANG_USES:
3210 ret = parse_uses(ctx, data, &grp->data);
3211 break;
3212
3213 case YANG_TYPEDEF:
3214 ret = parse_typedef(ctx, data, &grp->typedefs);
3215 break;
3216 case YANG_ACTION:
3217 ret = parse_action(ctx, data, &grp->actions);
3218 break;
3219 case YANG_GROUPING:
3220 ret = parse_grouping(ctx, data, &grp->groupings);
3221 break;
3222 case YANG_NOTIFICATION:
3223 ret = parse_notif(ctx, data, &grp->notifs);
3224 break;
3225 case YANG_CUSTOM:
3226 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &grp->exts);
3227 break;
3228 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003229 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003230 return LY_EVALID;
3231 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003232 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003233 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003234 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003235
3236 return ret;
3237}
3238
Michal Vaskoea5abea2018-09-18 13:10:54 +02003239/**
3240 * @brief Parse the refine statement.
3241 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003242 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003243 * @param[in,out] data Data to read from, always moved to currently handled character.
3244 * @param[in,out] augments Augments to add to.
3245 *
3246 * @return LY_ERR values.
3247 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003248static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003249parse_augment(struct ly_parser_ctx *ctx, const char **data, struct lysp_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003250{
3251 LY_ERR ret = 0;
3252 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003253 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003254 enum yang_keyword kw;
3255 struct lysp_augment *aug;
3256
Radek Krejcie53a8dc2018-10-17 12:52:40 +02003257 LYSP_ARRAY_NEW_RET(ctx->ctx, *augments, aug, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003258
3259 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003260 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003261 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003262
Radek Krejci44ceedc2018-10-02 15:54:31 +02003263 INSERT_WORD(ctx, buf, aug->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003264 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003265 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003266
3267 switch (kw) {
3268 case YANG_DESCRIPTION:
3269 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &aug->dsc, Y_STR_ARG, &aug->exts);
3270 break;
3271 case YANG_IF_FEATURE:
3272 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &aug->iffeatures, Y_STR_ARG, &aug->exts);
3273 break;
3274 case YANG_REFERENCE:
3275 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &aug->ref, Y_STR_ARG, &aug->exts);
3276 break;
3277 case YANG_STATUS:
3278 ret = parse_status(ctx, data, &aug->flags, &aug->exts);
3279 break;
3280 case YANG_WHEN:
3281 ret = parse_when(ctx, data, &aug->when);
3282 break;
3283
3284 case YANG_ANYDATA:
3285 case YANG_ANYXML:
3286 ret = parse_any(ctx, data, kw, &aug->child);
3287 break;
3288 case YANG_CASE:
3289 ret = parse_case(ctx, data, &aug->child);
3290 break;
3291 case YANG_CHOICE:
3292 ret = parse_choice(ctx, data, &aug->child);
3293 break;
3294 case YANG_CONTAINER:
3295 ret = parse_container(ctx, data, &aug->child);
3296 break;
3297 case YANG_LEAF:
3298 ret = parse_leaf(ctx, data, &aug->child);
3299 break;
3300 case YANG_LEAF_LIST:
3301 ret = parse_leaflist(ctx, data, &aug->child);
3302 break;
3303 case YANG_LIST:
3304 ret = parse_list(ctx, data, &aug->child);
3305 break;
3306 case YANG_USES:
3307 ret = parse_uses(ctx, data, &aug->child);
3308 break;
3309
3310 case YANG_ACTION:
3311 ret = parse_action(ctx, data, &aug->actions);
3312 break;
3313 case YANG_NOTIFICATION:
3314 ret = parse_notif(ctx, data, &aug->notifs);
3315 break;
3316 case YANG_CUSTOM:
3317 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &aug->exts);
3318 break;
3319 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003320 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003321 return LY_EVALID;
3322 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003323 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003324 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003325 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003326
3327 return ret;
3328}
3329
Michal Vaskoea5abea2018-09-18 13:10:54 +02003330/**
3331 * @brief Parse the uses statement.
3332 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003333 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003334 * @param[in,out] data Data to read from, always moved to currently handled character.
3335 * @param[in,out] siblings Siblings to add to.
3336 *
3337 * @return LY_ERR values.
3338 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003339static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003340parse_uses(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003341{
3342 LY_ERR ret = 0;
3343 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003344 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003345 enum yang_keyword kw;
3346 struct lysp_node *iter;
3347 struct lysp_node_uses *uses;
3348
3349 /* create structure */
3350 uses = calloc(1, sizeof *uses);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003351 LY_CHECK_ERR_RET(!uses, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003352 uses->nodetype = LYS_USES;
3353
3354 /* insert into siblings */
3355 if (!*siblings) {
3356 *siblings = (struct lysp_node *)uses;
3357 } else {
3358 for (iter = *siblings; iter->next; iter = iter->next);
3359 iter->next = (struct lysp_node *)uses;
3360 }
3361
3362 /* get name */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003363 ret = get_argument(ctx, data, Y_PREF_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003364 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003365
Radek Krejci44ceedc2018-10-02 15:54:31 +02003366 INSERT_WORD(ctx, buf, uses->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003367
3368 /* parse substatements */
3369 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003370 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003371
3372 switch (kw) {
3373 case YANG_DESCRIPTION:
3374 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &uses->dsc, Y_STR_ARG, &uses->exts);
3375 break;
3376 case YANG_IF_FEATURE:
3377 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &uses->iffeatures, Y_STR_ARG, &uses->exts);
3378 break;
3379 case YANG_REFERENCE:
3380 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &uses->ref, Y_STR_ARG, &uses->exts);
3381 break;
3382 case YANG_STATUS:
3383 ret = parse_status(ctx, data, &uses->flags, &uses->exts);
3384 break;
3385 case YANG_WHEN:
3386 ret = parse_when(ctx, data, &uses->when);
3387 break;
3388
3389 case YANG_REFINE:
3390 ret = parse_refine(ctx, data, &uses->refines);
3391 break;
3392 case YANG_AUGMENT:
3393 ret = parse_augment(ctx, data, &uses->augments);
3394 break;
3395 case YANG_CUSTOM:
3396 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &uses->exts);
3397 break;
3398 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003399 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003400 return LY_EVALID;
3401 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003402 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003403 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003404 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003405
3406 return ret;
3407}
3408
Michal Vaskoea5abea2018-09-18 13:10:54 +02003409/**
3410 * @brief Parse the case statement.
3411 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003412 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003413 * @param[in,out] data Data to read from, always moved to currently handled character.
3414 * @param[in,out] siblings Siblings to add to.
3415 *
3416 * @return LY_ERR values.
3417 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003418static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003419parse_case(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003420{
3421 LY_ERR ret = 0;
3422 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003423 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003424 enum yang_keyword kw;
3425 struct lysp_node *iter;
3426 struct lysp_node_case *cas;
3427
3428 /* create structure */
3429 cas = calloc(1, sizeof *cas);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003430 LY_CHECK_ERR_RET(!cas, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003431 cas->nodetype = LYS_CASE;
3432
3433 /* insert into siblings */
3434 if (!*siblings) {
3435 *siblings = (struct lysp_node *)cas;
3436 } else {
3437 for (iter = *siblings; iter->next; iter = iter->next);
3438 iter->next = (struct lysp_node *)cas;
3439 }
3440
3441 /* get name */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003442 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003443 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003444
Radek Krejci44ceedc2018-10-02 15:54:31 +02003445 INSERT_WORD(ctx, buf, cas->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003446
3447 /* parse substatements */
3448 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003449 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003450
3451 switch (kw) {
3452 case YANG_DESCRIPTION:
3453 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &cas->dsc, Y_STR_ARG, &cas->exts);
3454 break;
3455 case YANG_IF_FEATURE:
3456 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &cas->iffeatures, Y_STR_ARG, &cas->exts);
3457 break;
3458 case YANG_REFERENCE:
3459 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &cas->ref, Y_STR_ARG, &cas->exts);
3460 break;
3461 case YANG_STATUS:
3462 ret = parse_status(ctx, data, &cas->flags, &cas->exts);
3463 break;
3464 case YANG_WHEN:
3465 ret = parse_when(ctx, data, &cas->when);
3466 break;
3467
3468 case YANG_ANYDATA:
3469 case YANG_ANYXML:
3470 ret = parse_any(ctx, data, kw, &cas->child);
3471 break;
3472 case YANG_CHOICE:
3473 ret = parse_case(ctx, data, &cas->child);
3474 break;
3475 case YANG_CONTAINER:
3476 ret = parse_container(ctx, data, &cas->child);
3477 break;
3478 case YANG_LEAF:
3479 ret = parse_leaf(ctx, data, &cas->child);
3480 break;
3481 case YANG_LEAF_LIST:
3482 ret = parse_leaflist(ctx, data, &cas->child);
3483 break;
3484 case YANG_LIST:
3485 ret = parse_list(ctx, data, &cas->child);
3486 break;
3487 case YANG_USES:
3488 ret = parse_uses(ctx, data, &cas->child);
3489 break;
3490 case YANG_CUSTOM:
3491 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cas->exts);
3492 break;
3493 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003494 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003495 return LY_EVALID;
3496 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003497 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003498 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003499 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003500
3501 return ret;
3502}
3503
Michal Vaskoea5abea2018-09-18 13:10:54 +02003504/**
3505 * @brief Parse the choice statement.
3506 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003507 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003508 * @param[in,out] data Data to read from, always moved to currently handled character.
3509 * @param[in,out] siblings Siblings to add to.
3510 *
3511 * @return LY_ERR values.
3512 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003513static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003514parse_choice(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003515{
3516 LY_ERR ret = 0;
3517 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003518 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003519 enum yang_keyword kw;
3520 struct lysp_node *iter;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003521 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003522
3523 /* create structure */
Radek Krejci44ceedc2018-10-02 15:54:31 +02003524 choice = calloc(1, sizeof *choice);
3525 LY_CHECK_ERR_RET(!choice, LOGMEM(ctx->ctx), LY_EMEM);
3526 choice->nodetype = LYS_CHOICE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003527
3528 /* insert into siblings */
3529 if (!*siblings) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02003530 *siblings = (struct lysp_node *)choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003531 } else {
3532 for (iter = *siblings; iter->next; iter = iter->next);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003533 iter->next = (struct lysp_node *)choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003534 }
3535
3536 /* get name */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003537 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003538 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003539
Radek Krejci44ceedc2018-10-02 15:54:31 +02003540 INSERT_WORD(ctx, buf, choice->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003541
3542 /* parse substatements */
3543 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003544 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003545
3546 switch (kw) {
3547 case YANG_CONFIG:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003548 ret = parse_config(ctx, data, &choice->flags, &choice->exts);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003549 break;
3550 case YANG_DESCRIPTION:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003551 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &choice->dsc, Y_STR_ARG, &choice->exts);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003552 break;
3553 case YANG_IF_FEATURE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003554 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &choice->iffeatures, Y_STR_ARG, &choice->exts);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003555 break;
3556 case YANG_MANDATORY:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003557 ret = parse_mandatory(ctx, data, &choice->flags, &choice->exts);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003558 break;
3559 case YANG_REFERENCE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003560 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &choice->ref, Y_STR_ARG, &choice->exts);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003561 break;
3562 case YANG_STATUS:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003563 ret = parse_status(ctx, data, &choice->flags, &choice->exts);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003564 break;
3565 case YANG_WHEN:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003566 ret = parse_when(ctx, data, &choice->when);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003567 break;
3568 case YANG_DEFAULT:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003569 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &choice->dflt, Y_IDENTIF_ARG, &choice->exts);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003570 break;
3571
3572 case YANG_ANYDATA:
3573 case YANG_ANYXML:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003574 ret = parse_any(ctx, data, kw, &choice->child);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003575 break;
3576 case YANG_CASE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003577 ret = parse_case(ctx, data, &choice->child);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003578 break;
3579 case YANG_CHOICE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003580 ret = parse_choice(ctx, data, &choice->child);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003581 break;
3582 case YANG_CONTAINER:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003583 ret = parse_container(ctx, data, &choice->child);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003584 break;
3585 case YANG_LEAF:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003586 ret = parse_leaf(ctx, data, &choice->child);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003587 break;
3588 case YANG_LEAF_LIST:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003589 ret = parse_leaflist(ctx, data, &choice->child);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003590 break;
3591 case YANG_LIST:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003592 ret = parse_list(ctx, data, &choice->child);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003593 break;
3594 case YANG_CUSTOM:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003595 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &choice->exts);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003596 break;
3597 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003598 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003599 return LY_EVALID;
3600 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003601 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003602 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003603 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003604
3605 return ret;
3606}
3607
Michal Vaskoea5abea2018-09-18 13:10:54 +02003608/**
3609 * @brief Parse the container statement.
3610 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003611 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003612 * @param[in,out] data Data to read from, always moved to currently handled character.
3613 * @param[in,out] siblings Siblings to add to.
3614 *
3615 * @return LY_ERR values.
3616 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003617static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003618parse_container(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003619{
3620 LY_ERR ret = 0;
3621 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003622 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003623 enum yang_keyword kw;
3624 struct lysp_node *iter;
3625 struct lysp_node_container *cont;
3626
3627 /* create structure */
3628 cont = calloc(1, sizeof *cont);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003629 LY_CHECK_ERR_RET(!cont, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003630 cont->nodetype = LYS_CONTAINER;
3631
3632 /* insert into siblings */
3633 if (!*siblings) {
3634 *siblings = (struct lysp_node *)cont;
3635 } else {
3636 for (iter = *siblings; iter->next; iter = iter->next);
3637 iter->next = (struct lysp_node *)cont;
3638 }
3639
3640 /* get name */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003641 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003642 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003643
Radek Krejci44ceedc2018-10-02 15:54:31 +02003644 INSERT_WORD(ctx, buf, cont->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003645
3646 /* parse substatements */
3647 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003648 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003649
3650 switch (kw) {
3651 case YANG_CONFIG:
3652 ret = parse_config(ctx, data, &cont->flags, &cont->exts);
3653 break;
3654 case YANG_DESCRIPTION:
3655 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &cont->dsc, Y_STR_ARG, &cont->exts);
3656 break;
3657 case YANG_IF_FEATURE:
3658 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &cont->iffeatures, Y_STR_ARG, &cont->exts);
3659 break;
3660 case YANG_REFERENCE:
3661 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &cont->ref, Y_STR_ARG, &cont->exts);
3662 break;
3663 case YANG_STATUS:
3664 ret = parse_status(ctx, data, &cont->flags, &cont->exts);
3665 break;
3666 case YANG_WHEN:
3667 ret = parse_when(ctx, data, &cont->when);
3668 break;
3669 case YANG_PRESENCE:
3670 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_PRESENCE, 0, &cont->presence, Y_STR_ARG, &cont->exts);
3671 break;
3672
3673 case YANG_ANYDATA:
3674 case YANG_ANYXML:
3675 ret = parse_any(ctx, data, kw, &cont->child);
3676 break;
3677 case YANG_CHOICE:
3678 ret = parse_choice(ctx, data, &cont->child);
3679 break;
3680 case YANG_CONTAINER:
3681 ret = parse_container(ctx, data, &cont->child);
3682 break;
3683 case YANG_LEAF:
3684 ret = parse_leaf(ctx, data, &cont->child);
3685 break;
3686 case YANG_LEAF_LIST:
3687 ret = parse_leaflist(ctx, data, &cont->child);
3688 break;
3689 case YANG_LIST:
3690 ret = parse_list(ctx, data, &cont->child);
3691 break;
3692 case YANG_USES:
3693 ret = parse_uses(ctx, data, &cont->child);
3694 break;
3695
3696 case YANG_TYPEDEF:
3697 ret = parse_typedef(ctx, data, &cont->typedefs);
3698 break;
3699 case YANG_MUST:
3700 ret = parse_restrs(ctx, data, kw, &cont->musts);
3701 break;
3702 case YANG_ACTION:
3703 ret = parse_action(ctx, data, &cont->actions);
3704 break;
3705 case YANG_GROUPING:
3706 ret = parse_grouping(ctx, data, &cont->groupings);
3707 break;
3708 case YANG_NOTIFICATION:
3709 ret = parse_notif(ctx, data, &cont->notifs);
3710 break;
3711 case YANG_CUSTOM:
3712 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cont->exts);
3713 break;
3714 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003715 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003716 return LY_EVALID;
3717 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003718 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003719 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003720 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003721
3722 return ret;
3723}
3724
Michal Vaskoea5abea2018-09-18 13:10:54 +02003725/**
3726 * @brief Parse the list statement.
3727 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003728 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003729 * @param[in,out] data Data to read from, always moved to currently handled character.
3730 * @param[in,out] siblings Siblings to add to.
3731 *
3732 * @return LY_ERR values.
3733 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003734static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003735parse_list(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003736{
3737 LY_ERR ret = 0;
3738 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003739 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003740 enum yang_keyword kw;
3741 struct lysp_node *iter;
3742 struct lysp_node_list *list;
3743
3744 /* create structure */
3745 list = calloc(1, sizeof *list);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003746 LY_CHECK_ERR_RET(!list, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003747 list->nodetype = LYS_LIST;
3748
3749 /* insert into siblings */
3750 if (!*siblings) {
3751 *siblings = (struct lysp_node *)list;
3752 } else {
3753 for (iter = *siblings; iter->next; iter = iter->next);
3754 iter->next = (struct lysp_node *)list;
3755 }
3756
3757 /* get name */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003758 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003759 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003760
Radek Krejci44ceedc2018-10-02 15:54:31 +02003761 INSERT_WORD(ctx, buf, list->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003762
3763 /* parse substatements */
3764 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003765 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003766
3767 switch (kw) {
3768 case YANG_CONFIG:
3769 ret = parse_config(ctx, data, &list->flags, &list->exts);
3770 break;
3771 case YANG_DESCRIPTION:
3772 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &list->dsc, Y_STR_ARG, &list->exts);
3773 break;
3774 case YANG_IF_FEATURE:
3775 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &list->iffeatures, Y_STR_ARG, &list->exts);
3776 break;
3777 case YANG_REFERENCE:
3778 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &list->ref, Y_STR_ARG, &list->exts);
3779 break;
3780 case YANG_STATUS:
3781 ret = parse_status(ctx, data, &list->flags, &list->exts);
3782 break;
3783 case YANG_WHEN:
3784 ret = parse_when(ctx, data, &list->when);
3785 break;
3786 case YANG_KEY:
3787 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_KEY, 0, &list->key, Y_STR_ARG, &list->exts);
3788 break;
3789 case YANG_MAX_ELEMENTS:
3790 ret = parse_maxelements(ctx, data, &list->max, &list->flags, &list->exts);
3791 break;
3792 case YANG_MIN_ELEMENTS:
3793 ret = parse_minelements(ctx, data, &list->min, &list->flags, &list->exts);
3794 break;
3795 case YANG_ORDERED_BY:
3796 ret = parse_orderedby(ctx, data, &list->flags, &list->exts);
3797 break;
3798 case YANG_UNIQUE:
3799 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_UNIQUE, &list->uniques, Y_STR_ARG, &list->exts);
3800 break;
3801
3802 case YANG_ANYDATA:
3803 case YANG_ANYXML:
3804 ret = parse_any(ctx, data, kw, &list->child);
3805 break;
3806 case YANG_CHOICE:
3807 ret = parse_choice(ctx, data, &list->child);
3808 break;
3809 case YANG_CONTAINER:
3810 ret = parse_container(ctx, data, &list->child);
3811 break;
3812 case YANG_LEAF:
3813 ret = parse_leaf(ctx, data, &list->child);
3814 break;
3815 case YANG_LEAF_LIST:
3816 ret = parse_leaflist(ctx, data, &list->child);
3817 break;
3818 case YANG_LIST:
3819 ret = parse_list(ctx, data, &list->child);
3820 break;
3821 case YANG_USES:
3822 ret = parse_uses(ctx, data, &list->child);
3823 break;
3824
3825 case YANG_TYPEDEF:
3826 ret = parse_typedef(ctx, data, &list->typedefs);
3827 break;
3828 case YANG_MUST:
3829 ret = parse_restrs(ctx, data, kw, &list->musts);
3830 break;
3831 case YANG_ACTION:
3832 ret = parse_action(ctx, data, &list->actions);
3833 break;
3834 case YANG_GROUPING:
3835 ret = parse_grouping(ctx, data, &list->groupings);
3836 break;
3837 case YANG_NOTIFICATION:
3838 ret = parse_notif(ctx, data, &list->notifs);
3839 break;
3840 case YANG_CUSTOM:
3841 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &list->exts);
3842 break;
3843 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003844 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003845 return LY_EVALID;
3846 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003847 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003848 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003849 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003850
3851 return ret;
3852}
3853
Michal Vaskoea5abea2018-09-18 13:10:54 +02003854/**
3855 * @brief Parse the yin-element statement.
3856 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003857 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003858 * @param[in,out] data Data to read from, always moved to currently handled character.
3859 * @param[in,out] flags Flags to write to.
3860 * @param[in,out] exts Extension instances to add to.
3861 *
3862 * @return LY_ERR values.
3863 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003864static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003865parse_yinelement(struct ly_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003866{
3867 LY_ERR ret = 0;
3868 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003869 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003870 enum yang_keyword kw;
3871
3872 if (*flags & LYS_YINELEM_MASK) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02003873 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003874 return LY_EVALID;
3875 }
3876
3877 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003878 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003879 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003880
3881 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
3882 *flags |= LYS_YINELEM_TRUE;
3883 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
3884 *flags |= LYS_YINELEM_FALSE;
3885 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02003886 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003887 free(buf);
3888 return LY_EVALID;
3889 }
3890 free(buf);
3891
3892 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003893 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003894
3895 switch (kw) {
3896 case YANG_CUSTOM:
3897 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_YINELEM, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02003898 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003899 break;
3900 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003901 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003902 return LY_EVALID;
3903 }
3904 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003905 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003906
3907 return ret;
3908}
3909
Michal Vaskoea5abea2018-09-18 13:10:54 +02003910/**
3911 * @brief Parse the yin-element statement.
3912 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003913 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003914 * @param[in,out] data Data to read from, always moved to currently handled character.
3915 * @param[in,out] argument Value to write to.
3916 * @param[in,out] flags Flags to write to.
3917 * @param[in,out] exts Extension instances to add to.
3918 *
3919 * @return LY_ERR values.
3920 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003921static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003922parse_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 +02003923{
3924 LY_ERR ret = 0;
3925 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003926 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003927 enum yang_keyword kw;
3928
3929 if (*argument) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02003930 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003931 return LY_EVALID;
3932 }
3933
3934 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003935 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003936 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003937
Radek Krejci44ceedc2018-10-02 15:54:31 +02003938 INSERT_WORD(ctx, buf, *argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003939 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003940 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003941
3942 switch (kw) {
3943 case YANG_YIN_ELEMENT:
3944 ret = parse_yinelement(ctx, data, flags, exts);
3945 break;
3946 case YANG_CUSTOM:
3947 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_ARGUMENT, 0, exts);
3948 break;
3949 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003950 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003951 return LY_EVALID;
3952 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003953 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003954 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003955 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003956
3957 return ret;
3958}
3959
Michal Vaskoea5abea2018-09-18 13:10:54 +02003960/**
3961 * @brief Parse the extension statement.
3962 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003963 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003964 * @param[in,out] data Data to read from, always moved to currently handled character.
3965 * @param[in,out] extensions Extensions to add to.
3966 *
3967 * @return LY_ERR values.
3968 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003969static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003970parse_extension(struct ly_parser_ctx *ctx, const char **data, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003971{
3972 LY_ERR ret = 0;
3973 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003974 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003975 enum yang_keyword kw;
3976 struct lysp_ext *ex;
3977
Radek Krejcie53a8dc2018-10-17 12:52:40 +02003978 LYSP_ARRAY_NEW_RET(ctx->ctx, *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003979
3980 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003981 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003982 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003983
Radek Krejci44ceedc2018-10-02 15:54:31 +02003984 INSERT_WORD(ctx, buf, ex->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003985 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003986 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003987
3988 switch (kw) {
3989 case YANG_DESCRIPTION:
3990 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &ex->dsc, Y_STR_ARG, &ex->exts);
3991 break;
3992 case YANG_REFERENCE:
3993 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &ex->ref, Y_STR_ARG, &ex->exts);
3994 break;
3995 case YANG_STATUS:
3996 ret = parse_status(ctx, data, &ex->flags, &ex->exts);
3997 break;
3998 case YANG_ARGUMENT:
3999 ret = parse_argument(ctx, data, &ex->argument, &ex->flags, &ex->exts);
4000 break;
4001 case YANG_CUSTOM:
4002 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ex->exts);
4003 break;
4004 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004005 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004006 return LY_EVALID;
4007 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004008 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004009 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004010 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004011
4012 return ret;
4013}
4014
Michal Vaskoea5abea2018-09-18 13:10:54 +02004015/**
4016 * @brief Parse the deviate statement.
4017 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004018 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004019 * @param[in,out] data Data to read from, always moved to currently handled character.
4020 * @param[in,out] deviates Deviates to add to.
4021 *
4022 * @return LY_ERR values.
4023 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004024static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02004025parse_deviate(struct ly_parser_ctx *ctx, const char **data, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004026{
4027 LY_ERR ret = 0;
4028 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004029 size_t word_len, dev_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004030 enum yang_keyword kw;
4031 struct lysp_deviate *iter, *d;
4032 struct lysp_deviate_add *d_add = NULL;
4033 struct lysp_deviate_rpl *d_rpl = NULL;
4034 struct lysp_deviate_del *d_del = NULL;
4035 const char **d_units, ***d_uniques, ***d_dflts;
4036 struct lysp_restr **d_musts;
4037 uint16_t *d_flags;
4038 uint32_t *d_min, *d_max;
4039
4040 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02004041 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02004042 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004043
4044 if ((word_len == 13) && !strncmp(word, "not-supported", word_len)) {
4045 dev_mod = LYS_DEV_NOT_SUPPORTED;
4046 } else if ((word_len == 3) && !strncmp(word, "add", word_len)) {
4047 dev_mod = LYS_DEV_ADD;
4048 } else if ((word_len == 7) && !strncmp(word, "replace", word_len)) {
4049 dev_mod = LYS_DEV_REPLACE;
4050 } else if ((word_len == 6) && !strncmp(word, "delete", word_len)) {
4051 dev_mod = LYS_DEV_DELETE;
4052 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004053 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004054 free(buf);
4055 return LY_EVALID;
4056 }
4057 free(buf);
4058
4059 /* create structure */
4060 switch (dev_mod) {
4061 case LYS_DEV_NOT_SUPPORTED:
4062 d = calloc(1, sizeof *d);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004063 LY_CHECK_ERR_RET(!d, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004064 break;
4065 case LYS_DEV_ADD:
4066 d_add = calloc(1, sizeof *d_add);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004067 LY_CHECK_ERR_RET(!d_add, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004068 d = (struct lysp_deviate *)d_add;
4069 d_units = &d_add->units;
4070 d_uniques = &d_add->uniques;
4071 d_dflts = &d_add->dflts;
4072 d_musts = &d_add->musts;
4073 d_flags = &d_add->flags;
4074 d_min = &d_add->min;
4075 d_max = &d_add->max;
4076 break;
4077 case LYS_DEV_REPLACE:
4078 d_rpl = calloc(1, sizeof *d_rpl);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004079 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004080 d = (struct lysp_deviate *)d_rpl;
4081 d_units = &d_rpl->units;
4082 d_flags = &d_rpl->flags;
4083 d_min = &d_rpl->min;
4084 d_max = &d_rpl->max;
4085 break;
4086 case LYS_DEV_DELETE:
4087 d_del = calloc(1, sizeof *d_del);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004088 LY_CHECK_ERR_RET(!d_del, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004089 d = (struct lysp_deviate *)d_del;
4090 d_units = &d_del->units;
4091 d_uniques = &d_del->uniques;
4092 d_dflts = &d_del->dflts;
4093 d_musts = &d_del->musts;
4094 d_flags = &d_del->flags;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004095 break;
4096 default:
4097 assert(0);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004098 LOGINT_RET(ctx->ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004099 }
4100 d->mod = dev_mod;
4101
4102 /* insert into siblings */
4103 if (!*deviates) {
4104 *deviates = d;
4105 } else {
4106 for (iter = *deviates; iter->next; iter = iter->next);
4107 iter->next = d;
4108 }
4109
4110 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02004111 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004112
4113 switch (kw) {
4114 case YANG_CONFIG:
4115 switch (dev_mod) {
4116 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004117 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004118 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004119 return LY_EVALID;
4120 default:
4121 ret = parse_config(ctx, data, d_flags, &d->exts);
4122 break;
4123 }
4124 break;
4125 case YANG_DEFAULT:
4126 switch (dev_mod) {
4127 case LYS_DEV_NOT_SUPPORTED:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004128 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004129 return LY_EVALID;
4130 case LYS_DEV_REPLACE:
4131 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &d_rpl->dflt, Y_STR_ARG, &d->exts);
4132 break;
4133 default:
4134 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_DEFAULT, d_dflts, Y_STR_ARG, &d->exts);
4135 break;
4136 }
4137 break;
4138 case YANG_MANDATORY:
4139 switch (dev_mod) {
4140 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004141 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004142 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004143 return LY_EVALID;
4144 default:
4145 ret = parse_mandatory(ctx, data, d_flags, &d->exts);
4146 break;
4147 }
4148 break;
4149 case YANG_MAX_ELEMENTS:
4150 switch (dev_mod) {
4151 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004152 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004153 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004154 return LY_EVALID;
4155 default:
4156 ret = parse_maxelements(ctx, data, d_max, d_flags, &d->exts);
4157 break;
4158 }
4159 break;
4160 case YANG_MIN_ELEMENTS:
4161 switch (dev_mod) {
4162 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004163 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004164 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004165 return LY_EVALID;
4166 default:
4167 ret = parse_minelements(ctx, data, d_min, d_flags, &d->exts);
4168 break;
4169 }
4170 break;
4171 case YANG_MUST:
4172 switch (dev_mod) {
4173 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004174 case LYS_DEV_REPLACE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004175 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004176 return LY_EVALID;
4177 default:
4178 ret = parse_restrs(ctx, data, kw, d_musts);
4179 break;
4180 }
4181 break;
4182 case YANG_TYPE:
4183 switch (dev_mod) {
4184 case LYS_DEV_NOT_SUPPORTED:
4185 case LYS_DEV_ADD:
4186 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004187 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004188 return LY_EVALID;
4189 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004190 if (d_rpl->type) {
4191 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
4192 return LY_EVALID;
4193 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004194 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004195 LY_CHECK_ERR_RET(!d_rpl->type, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004196 ret = parse_type(ctx, data, d_rpl->type);
4197 break;
4198 }
4199 break;
4200 case YANG_UNIQUE:
4201 switch (dev_mod) {
4202 case LYS_DEV_NOT_SUPPORTED:
4203 case LYS_DEV_REPLACE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004204 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004205 return LY_EVALID;
4206 default:
4207 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_UNIQUE, d_uniques, Y_STR_ARG, &d->exts);
4208 break;
4209 }
4210 break;
4211 case YANG_UNITS:
4212 switch (dev_mod) {
4213 case LYS_DEV_NOT_SUPPORTED:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004214 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004215 return LY_EVALID;
4216 default:
4217 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, d_units, Y_STR_ARG, &d->exts);
4218 break;
4219 }
4220 break;
4221 case YANG_CUSTOM:
4222 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &d->exts);
4223 break;
4224 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004225 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004226 return LY_EVALID;
4227 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004228 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004229 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004230 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004231
4232 return ret;
4233}
4234
Michal Vaskoea5abea2018-09-18 13:10:54 +02004235/**
4236 * @brief Parse the deviation statement.
4237 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004238 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004239 * @param[in,out] data Data to read from, always moved to currently handled character.
4240 * @param[in,out] deviations Deviations to add to.
4241 *
4242 * @return LY_ERR values.
4243 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004244static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02004245parse_deviation(struct ly_parser_ctx *ctx, const char **data, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004246{
4247 LY_ERR ret = 0;
4248 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004249 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004250 enum yang_keyword kw;
4251 struct lysp_deviation *dev;
4252
Radek Krejcie53a8dc2018-10-17 12:52:40 +02004253 LYSP_ARRAY_NEW_RET(ctx->ctx, *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004254
4255 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02004256 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02004257 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004258
Radek Krejci44ceedc2018-10-02 15:54:31 +02004259 INSERT_WORD(ctx, buf, dev->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004260 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02004261 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004262
4263 switch (kw) {
4264 case YANG_DESCRIPTION:
4265 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &dev->dsc, Y_STR_ARG, &dev->exts);
4266 break;
4267 case YANG_DEVIATE:
4268 ret = parse_deviate(ctx, data, &dev->deviates);
4269 break;
4270 case YANG_REFERENCE:
4271 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &dev->ref, Y_STR_ARG, &dev->exts);
4272 break;
4273 case YANG_CUSTOM:
4274 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &dev->exts);
4275 break;
4276 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004277 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004278 return LY_EVALID;
4279 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004280 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004281 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004282 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004283
4284 /* mandatory substatements */
4285 if (!dev->deviates) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004286 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004287 return LY_EVALID;
4288 }
4289
4290 return ret;
4291}
4292
Michal Vaskoea5abea2018-09-18 13:10:54 +02004293/**
4294 * @brief Parse the feature statement.
4295 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004296 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004297 * @param[in,out] data Data to read from, always moved to currently handled character.
4298 * @param[in,out] features Features to add to.
4299 *
4300 * @return LY_ERR values.
4301 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004302static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02004303parse_feature(struct ly_parser_ctx *ctx, const char **data, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004304{
4305 LY_ERR ret = 0;
4306 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004307 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004308 enum yang_keyword kw;
4309 struct lysp_feature *feat;
4310
Radek Krejcie53a8dc2018-10-17 12:52:40 +02004311 LYSP_ARRAY_NEW_RET(ctx->ctx, *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004312
4313 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02004314 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02004315 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004316
Radek Krejci44ceedc2018-10-02 15:54:31 +02004317 INSERT_WORD(ctx, buf, feat->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004318 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02004319 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004320
4321 switch (kw) {
4322 case YANG_DESCRIPTION:
4323 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &feat->dsc, Y_STR_ARG, &feat->exts);
4324 break;
4325 case YANG_IF_FEATURE:
4326 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &feat->iffeatures, Y_STR_ARG, &feat->exts);
4327 break;
4328 case YANG_REFERENCE:
4329 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &feat->ref, Y_STR_ARG, &feat->exts);
4330 break;
4331 case YANG_STATUS:
4332 ret = parse_status(ctx, data, &feat->flags, &feat->exts);
4333 break;
4334 case YANG_CUSTOM:
4335 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &feat->exts);
4336 break;
4337 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004338 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004339 return LY_EVALID;
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 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004343 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004344
4345 return ret;
4346}
4347
Michal Vaskoea5abea2018-09-18 13:10:54 +02004348/**
4349 * @brief Parse the identity statement.
4350 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004351 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004352 * @param[in,out] data Data to read from, always moved to currently handled character.
4353 * @param[in,out] identities Identities to add to.
4354 *
4355 * @return LY_ERR values.
4356 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004357static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02004358parse_identity(struct ly_parser_ctx *ctx, const char **data, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004359{
4360 LY_ERR ret = 0;
4361 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004362 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004363 enum yang_keyword kw;
4364 struct lysp_ident *ident;
4365
Radek Krejcie53a8dc2018-10-17 12:52:40 +02004366 LYSP_ARRAY_NEW_RET(ctx->ctx, *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004367
4368 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02004369 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02004370 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004371
Radek Krejci44ceedc2018-10-02 15:54:31 +02004372 INSERT_WORD(ctx, buf, ident->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004373 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02004374 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004375
4376 switch (kw) {
4377 case YANG_DESCRIPTION:
4378 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &ident->dsc, Y_STR_ARG, &ident->exts);
4379 break;
4380 case YANG_IF_FEATURE:
4381 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &ident->iffeatures, Y_STR_ARG, &ident->exts);
4382 break;
4383 case YANG_REFERENCE:
4384 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &ident->ref, Y_STR_ARG, &ident->exts);
4385 break;
4386 case YANG_STATUS:
4387 ret = parse_status(ctx, data, &ident->flags, &ident->exts);
4388 break;
4389 case YANG_BASE:
4390 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_BASE, &ident->bases, Y_PREF_IDENTIF_ARG, &ident->exts);
4391 break;
4392 case YANG_CUSTOM:
4393 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ident->exts);
4394 break;
4395 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004396 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004397 return LY_EVALID;
4398 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004399 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004400 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004401
4402 return ret;
4403}
4404
Michal Vaskoea5abea2018-09-18 13:10:54 +02004405/**
4406 * @brief Parse the module or submodule statement.
4407 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004408 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004409 * @param[in,out] data Data to read from, always moved to currently handled character.
4410 * @param[in,out] mod Module to write to.
4411 *
4412 * @return LY_ERR values.
4413 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004414static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02004415parse_sub_module(struct ly_parser_ctx *ctx, const char **data, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004416{
4417 LY_ERR ret = 0;
4418 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004419 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004420 enum yang_keyword kw, prev_kw = 0;
4421 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
4422
4423 /* (sub)module name */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02004424 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02004425 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004426
Radek Krejci44ceedc2018-10-02 15:54:31 +02004427 INSERT_WORD(ctx, buf, mod->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004428 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02004429 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004430
Radek Krejcie3846472018-10-15 15:24:51 +02004431#define CHECK_ORDER(SECTION) \
4432 if (mod_stmt > SECTION) {LOGVAL_YANG(ctx, LY_VCODE_INORD, ly_stmt2str(kw), ly_stmt2str(prev_kw)); return LY_EVALID;}mod_stmt = SECTION
4433
Michal Vasko7fbc8162018-09-17 10:35:16 +02004434 switch (kw) {
4435 /* module header */
4436 case YANG_NAMESPACE:
4437 case YANG_PREFIX:
4438 if (mod->submodule) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004439 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004440 return LY_EVALID;
4441 }
Radek Krejcie3846472018-10-15 15:24:51 +02004442 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4443 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004444 case YANG_BELONGS_TO:
Radek Krejcie3846472018-10-15 15:24:51 +02004445 if (!mod->submodule) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004446 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004447 return LY_EVALID;
4448 }
Radek Krejcie3846472018-10-15 15:24:51 +02004449 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4450 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004451 case YANG_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004452 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004453 break;
4454 /* linkage */
4455 case YANG_INCLUDE:
4456 case YANG_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004457 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004458 break;
4459 /* meta */
4460 case YANG_ORGANIZATION:
4461 case YANG_CONTACT:
4462 case YANG_DESCRIPTION:
4463 case YANG_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004464 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004465 break;
4466
4467 /* revision */
4468 case YANG_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004469 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004470 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004471 /* body */
4472 case YANG_ANYDATA:
4473 case YANG_ANYXML:
4474 case YANG_AUGMENT:
4475 case YANG_CHOICE:
4476 case YANG_CONTAINER:
4477 case YANG_DEVIATION:
4478 case YANG_EXTENSION:
4479 case YANG_FEATURE:
4480 case YANG_GROUPING:
4481 case YANG_IDENTITY:
4482 case YANG_LEAF:
4483 case YANG_LEAF_LIST:
4484 case YANG_LIST:
4485 case YANG_NOTIFICATION:
4486 case YANG_RPC:
4487 case YANG_TYPEDEF:
4488 case YANG_USES:
4489 case YANG_CUSTOM:
4490 mod_stmt = Y_MOD_BODY;
4491 break;
4492 default:
4493 /* error handled in the next switch */
4494 break;
4495 }
Radek Krejcie3846472018-10-15 15:24:51 +02004496#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004497
Radek Krejcie3846472018-10-15 15:24:51 +02004498 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004499 switch (kw) {
4500 /* module header */
4501 case YANG_YANG_VERSION:
4502 ret = parse_yangversion(ctx, data, mod);
4503 break;
4504 case YANG_NAMESPACE:
4505 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_NAMESPACE, 0, &mod->ns, Y_STR_ARG, &mod->exts);
4506 break;
4507 case YANG_PREFIX:
4508 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_PREFIX, 0, &mod->prefix, Y_IDENTIF_ARG, &mod->exts);
Radek Krejci70853c52018-10-15 14:46:16 +02004509 LY_CHECK_RET(lysp_check_prefix(ctx, mod, &mod->prefix), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004510 break;
4511 case YANG_BELONGS_TO:
4512 ret = parse_belongsto(ctx, data, &mod->belongsto, &mod->prefix, &mod->exts);
4513 break;
4514
4515 /* linkage */
4516 case YANG_INCLUDE:
4517 ret = parse_include(ctx, data, &mod->includes);
4518 break;
4519 case YANG_IMPORT:
Radek Krejci70853c52018-10-15 14:46:16 +02004520 ret = parse_import(ctx, data, mod);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004521 break;
4522
4523 /* meta */
4524 case YANG_ORGANIZATION:
4525 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_ORGANIZATION, 0, &mod->org, Y_STR_ARG, &mod->exts);
4526 break;
4527 case YANG_CONTACT:
4528 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_CONTACT, 0, &mod->contact, Y_STR_ARG, &mod->exts);
4529 break;
4530 case YANG_DESCRIPTION:
4531 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &mod->dsc, Y_STR_ARG, &mod->exts);
4532 break;
4533 case YANG_REFERENCE:
4534 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &mod->ref, Y_STR_ARG, &mod->exts);
4535 break;
4536
4537 /* revision */
4538 case YANG_REVISION:
4539 ret = parse_revision(ctx, data, &mod->revs);
4540 break;
4541
4542 /* body */
4543 case YANG_ANYDATA:
4544 case YANG_ANYXML:
4545 ret = parse_any(ctx, data, kw, &mod->data);
4546 break;
4547 case YANG_CHOICE:
4548 ret = parse_choice(ctx, data, &mod->data);
4549 break;
4550 case YANG_CONTAINER:
4551 ret = parse_container(ctx, data, &mod->data);
4552 break;
4553 case YANG_LEAF:
4554 ret = parse_leaf(ctx, data, &mod->data);
4555 break;
4556 case YANG_LEAF_LIST:
4557 ret = parse_leaflist(ctx, data, &mod->data);
4558 break;
4559 case YANG_LIST:
4560 ret = parse_list(ctx, data, &mod->data);
4561 break;
4562 case YANG_USES:
4563 ret = parse_uses(ctx, data, &mod->data);
4564 break;
4565
4566 case YANG_AUGMENT:
4567 ret = parse_augment(ctx, data, &mod->augments);
4568 break;
4569 case YANG_DEVIATION:
4570 ret = parse_deviation(ctx, data, &mod->deviations);
4571 break;
4572 case YANG_EXTENSION:
4573 ret = parse_extension(ctx, data, &mod->extensions);
4574 break;
4575 case YANG_FEATURE:
4576 ret = parse_feature(ctx, data, &mod->features);
4577 break;
4578 case YANG_GROUPING:
4579 ret = parse_grouping(ctx, data, &mod->groupings);
4580 break;
4581 case YANG_IDENTITY:
4582 ret = parse_identity(ctx, data, &mod->identities);
4583 break;
4584 case YANG_NOTIFICATION:
4585 ret = parse_notif(ctx, data, &mod->notifs);
4586 break;
4587 case YANG_RPC:
4588 ret = parse_action(ctx, data, &mod->rpcs);
4589 break;
4590 case YANG_TYPEDEF:
4591 ret = parse_typedef(ctx, data, &mod->typedefs);
4592 break;
4593 case YANG_CUSTOM:
4594 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &mod->exts);
4595 break;
4596
4597 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004598 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), mod->submodule ? "submodule" : "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004599 return LY_EVALID;
4600 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004601 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004602 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004603 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004604
4605 /* mandatory substatements */
4606 if (mod->submodule) {
4607 if (!mod->belongsto) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004608 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004609 return LY_EVALID;
4610 }
4611 } else {
4612 if (!mod->ns) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004613 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004614 return LY_EVALID;
4615 } else if (!mod->prefix) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004616 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004617 return LY_EVALID;
4618 }
4619 }
4620
4621 return ret;
4622}
4623
Radek Krejcid4557c62018-09-17 11:42:09 +02004624LY_ERR
Michal Vasko7fbc8162018-09-17 10:35:16 +02004625yang_parse(struct ly_ctx *ctx, const char *data, struct lysp_module **mod_p)
4626{
4627 LY_ERR ret = 0;
4628 char *word, *buf;
Radek Krejciefd22f62018-09-27 11:47:58 +02004629 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004630 enum yang_keyword kw;
Radek Krejci0c2cf322018-10-13 08:02:30 +02004631 struct lysp_module *mod = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +02004632 struct ly_parser_ctx context = {0};
Michal Vasko7fbc8162018-09-17 10:35:16 +02004633
Radek Krejci44ceedc2018-10-02 15:54:31 +02004634 context.ctx = ctx;
4635 context.line = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004636
4637 /* "module"/"submodule" */
Radek Krejci44ceedc2018-10-02 15:54:31 +02004638 ret = get_keyword(&context, &data, &kw, &word, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02004639 LY_CHECK_GOTO(ret, error);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004640
4641 if ((kw != YANG_MODULE) && (kw != YANG_SUBMODULE)) {
Radek Krejcifc62d7e2018-10-11 12:56:42 +02004642 LOGVAL_YANG(&context, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".",
Radek Krejcic59bc972018-09-17 16:13:06 +02004643 ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004644 goto error;
4645 }
4646
4647 mod = calloc(1, sizeof *mod);
4648 LY_CHECK_ERR_GOTO(!mod, LOGMEM(ctx), error);
4649 if (kw == YANG_SUBMODULE) {
4650 mod->submodule = 1;
4651 }
Radek Krejci9fcacc12018-10-11 15:59:11 +02004652 mod->ctx = ctx;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004653
4654 /* substatements */
Radek Krejci44ceedc2018-10-02 15:54:31 +02004655 ret = parse_sub_module(&context, &data, mod);
Radek Krejcic59bc972018-09-17 16:13:06 +02004656 LY_CHECK_GOTO(ret, error);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004657
4658 /* read some trailing spaces or new lines */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02004659 ret = get_argument(&context, &data, Y_MAYBE_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02004660 LY_CHECK_GOTO(ret, error);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004661
4662 if (word) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004663 LOGVAL_YANG(&context, LYVE_SYNTAX, "Invalid character sequence \"%.*s\", expected end-of-file.",
Michal Vasko7fbc8162018-09-17 10:35:16 +02004664 word_len, word);
4665 free(buf);
4666 goto error;
4667 }
4668 assert(!buf);
4669
Radek Krejci86d106e2018-10-18 09:53:19 +02004670 /* make sure that the newest revision is at position 0 */
4671 lysp_sort_revisions(mod->revs);
4672
Michal Vasko7fbc8162018-09-17 10:35:16 +02004673 *mod_p = mod;
4674 return ret;
4675
4676error:
Radek Krejci9fcacc12018-10-11 15:59:11 +02004677 lysp_module_free(mod);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004678 return ret;
4679}