blob: f904680024311dc94868ef8a0a197b94989bb0b4 [file] [log] [blame]
Michal Vasko7fbc8162018-09-17 10:35:16 +02001/**
2 * @file parser_yang.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief YANG parser
5 *
6 * Copyright (c) 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14#include <sys/types.h>
15#include <sys/stat.h>
16#include <unistd.h>
17#include <fcntl.h>
18#include <stdio.h>
19#include <stdlib.h>
20#include <stdint.h>
21#include <errno.h>
22#include <ctype.h>
23#include <string.h>
24#include <dirent.h>
25#include <assert.h>
26
27#include "common.h"
28#include "context.h"
29#include "libyang.h"
Radek Krejci70853c52018-10-15 14:46:16 +020030#include "tree_schema_internal.h"
Radek Krejci44ceedc2018-10-02 15:54:31 +020031
32/* Macro to check YANG's yang-char grammar rule */
33#define is_yangutf8char(c) ((c >= 0x20 && c <= 0xd77) || c == 0x09 || c == 0x0a || c == 0x0d || \
34 (c >= 0xe000 && c <= 0xfdcf) || (c >= 0xfdf0 && c <= 0xfffd) || \
35 (c >= 0x10000 && c <= 0x1fffd) || (c >= 0x20000 && c <= 0x2fffd) || \
36 (c >= 0x30000 && c <= 0x3fffd) || (c >= 0x40000 && c <= 0x2fffd) || \
37 (c >= 0x50000 && c <= 0x5fffd) || (c >= 0x60000 && c <= 0x6fffd) || \
38 (c >= 0x70000 && c <= 0x7fffd) || (c >= 0x80000 && c <= 0x8fffd) || \
39 (c >= 0x90000 && c <= 0x9fffd) || (c >= 0xa0000 && c <= 0xafffd) || \
40 (c >= 0xb0000 && c <= 0xbfffd) || (c >= 0xc0000 && c <= 0xcfffd) || \
41 (c >= 0xd0000 && c <= 0xdfffd) || (c >= 0xe0000 && c <= 0xefffd) || \
42 (c >= 0xf0000 && c <= 0xffffd) || (c >= 0x100000 && c <= 0x10fffd))
43
44/* These 2 macros checks YANG's identifier grammar rule */
45#define is_yangidentstartchar(c) ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_')
46#define is_yangidentchar(c) ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || \
47 c == '_' || c == '-' || c == '.')
48
Radek Krejci9fcacc12018-10-11 15:59:11 +020049#define INSERT_WORD(CTX, BUF, TARGET, WORD, LEN) \
50 if (BUF) {(TARGET) = lydict_insert_zc((CTX)->ctx, WORD);}\
51 else {(TARGET) = lydict_insert((CTX)->ctx, WORD, LEN);}
Radek Krejci44ceedc2018-10-02 15:54:31 +020052
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020053#define MOVE_INPUT(CTX, DATA, COUNT) (*(data))+=COUNT;(CTX)->indent+=COUNT
54
Radek Krejci44ceedc2018-10-02 15:54:31 +020055#define LOGVAL_YANG(CTX, ...) LOGVAL((CTX)->ctx, LY_VLOG_LINE, &(CTX)->line, __VA_ARGS__)
56
Michal Vaskoea5abea2018-09-18 13:10:54 +020057/**
58 * @brief Loop through all substatements providing, return if there are none.
59 *
Radek Krejci44ceedc2018-10-02 15:54:31 +020060 * @param[in] CTX yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +020061 * @param[in] DATA Raw data to read from.
62 * @param[out] KW YANG keyword read.
63 * @param[out] WORD Pointer to the keyword itself.
64 * @param[out] WORD_LEN Length of the keyword.
65 * @param[out] ERR Variable for error storing.
66 *
67 * @return In case there are no substatements or a fatal error encountered.
68 */
Michal Vasko7fbc8162018-09-17 10:35:16 +020069#define YANG_READ_SUBSTMT_FOR(CTX, DATA, KW, WORD, WORD_LEN, ERR) \
70 ERR = get_keyword(CTX, DATA, &KW, &WORD, &WORD_LEN); \
Radek Krejcic59bc972018-09-17 16:13:06 +020071 LY_CHECK_RET(ERR); \
Michal Vasko7fbc8162018-09-17 10:35:16 +020072 \
73 if (KW == YANG_SEMICOLON) { \
74 return ERR; \
75 } \
76 if (KW != YANG_LEFT_BRACE) { \
Radek Krejci44ceedc2018-10-02 15:54:31 +020077 LOGVAL_YANG(CTX, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\", expected \";\" or \"{\".", ly_stmt2str(KW)); \
Michal Vasko7fbc8162018-09-17 10:35:16 +020078 return LY_EVALID; \
79 } \
80 for (ERR = get_keyword(CTX, DATA, &KW, &WORD, &WORD_LEN); \
81 !ERR && (KW != YANG_RIGHT_BRACE); \
82 ERR = get_keyword(CTX, DATA, &KW, &WORD, &WORD_LEN))
83
Radek Krejci44ceedc2018-10-02 15:54:31 +020084static LY_ERR parse_container(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings);
85static LY_ERR parse_uses(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings);
86static LY_ERR parse_choice(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings);
87static LY_ERR parse_case(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings);
88static LY_ERR parse_list(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings);
89static LY_ERR parse_grouping(struct ly_parser_ctx *ctx, const char **data, struct lysp_grp **groupings);
Michal Vasko7fbc8162018-09-17 10:35:16 +020090
Michal Vaskoea5abea2018-09-18 13:10:54 +020091/**
92 * @brief Add another character to dynamic buffer, a low-level function.
93 *
Radek Krejci44ceedc2018-10-02 15:54:31 +020094 * Enlarge if needed. Updates \p input as well as \p buf_used.
Michal Vaskoea5abea2018-09-18 13:10:54 +020095 *
Radek Krejci404251e2018-10-09 12:06:44 +020096 * @param[in] ctx libyang context for logging.
Radek Krejci44ceedc2018-10-02 15:54:31 +020097 * @param[in, out] input Input string to process.
98 * @param[in] len Number of bytes to get from the input string and copy into the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +020099 * @param[in,out] buf Buffer to use, can be moved by realloc().
100 * @param[in,out] buf_len Current size of the buffer.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200101 * @param[in,out] buf_used Currently used characters of the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200102 *
103 * @return LY_ERR values.
104 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200105static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +0200106buf_add_char(struct ly_ctx *ctx, const char **input, size_t len, char **buf, size_t *buf_len, size_t *buf_used)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200107{
Radek Krejci44ceedc2018-10-02 15:54:31 +0200108 if (*buf_len <= (*buf_used) + len) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200109 *buf_len += 16;
110 *buf = ly_realloc(*buf, *buf_len);
111 LY_CHECK_ERR_RET(!*buf, LOGMEM(ctx), LY_EMEM);
112 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200113 memcpy(&(*buf)[*buf_used], *input, len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200114
Radek Krejci44ceedc2018-10-02 15:54:31 +0200115 (*buf_used) += len;
116 (*input) += len;
117
Michal Vasko7fbc8162018-09-17 10:35:16 +0200118 return LY_SUCCESS;
119}
120
Michal Vaskoea5abea2018-09-18 13:10:54 +0200121/**
Radek Krejci44ceedc2018-10-02 15:54:31 +0200122 * @brief Check that \p c is valid UTF8 code point for YANG string.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200123 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200124 * @param[in] ctx yang parser context for logging.
125 * @param[in] c UTF8 code point of a character to check.
126 * @return LY_ERR values.
127 */
128static LY_ERR
129check_stringchar(struct ly_parser_ctx *ctx, unsigned int c)
130{
131 if (!is_yangutf8char(c)) {
132 LOGVAL_YANG(ctx, LY_VCODE_INCHAR, c);
133 return LY_EVALID;
134 }
135 return LY_SUCCESS;
136}
137
138/**
139 * @brief Check that \p c is valid UTF8 code point for YANG identifier.
140 *
141 * @param[in] ctx yang parser context for logging.
142 * @param[in] c UTF8 code point of a character to check.
143 * @param[in] first Flag to check the first character of an identifier, which is more restricted.
Radek Krejcidcc7b322018-10-11 14:24:02 +0200144 * @param[in,out] prefix Storage for internally used flag in case of possible prefixed identifiers:
145 * 0 - colon not yet found (no prefix)
146 * 1 - \p c is the colon character
147 * 2 - prefix already processed, now processing the identifier
148 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200149 * If the identifier cannot be prefixed, NULL is expected.
150 * @return LY_ERR values.
151 */
152static LY_ERR
153check_identifierchar(struct ly_parser_ctx *ctx, unsigned int c, int first, int *prefix)
154{
155 if (first || (prefix && (*prefix) == 1)) {
156 if (!is_yangidentstartchar(c)) {
157 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '%c'.", c);
158 return LY_EVALID;
159 }
Radek Krejci9fcacc12018-10-11 15:59:11 +0200160 if (prefix) {
161 if (first) {
162 (*prefix) = 0;
163 } else {
164 (*prefix) = 2;
165 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200166 }
167 } else if (c == ':' && prefix && (*prefix) == 0) {
168 (*prefix) = 1;
169 } else if (!is_yangidentchar(c)) {
170 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Invalid identifier character '%c'.", c);
171 return LY_EVALID;
172 }
173
174 return LY_SUCCESS;
175}
176
177/**
178 * @brief Store a single UTF8 character. It depends whether in a dynamically-allocated buffer or just as a pointer to the data.
179 *
180 * @param[in] ctx yang parser context for logging.
181 * @param[in,out] input Pointer to the string where to get the character to store. Automatically moved to the next character
182 * when function returns.
183 * @param[in] arg Type of the input string to select method of checking character validity.
184 * @param[in,out] word_p Word pointer. If buffer (\p word_b) was not yet needed, it is just a pointer to the first
Michal Vaskoea5abea2018-09-18 13:10:54 +0200185 * stored character. If buffer was needed (\p word_b is non-NULL or \p need_buf is set), it is pointing to the buffer.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200186 * @param[in,out] word_len Current length of the word pointed to by \p word_p.
187 * @param[in,out] word_b Word buffer. Is kept NULL as long as it is not requested (word is a substring of the data).
Michal Vaskoea5abea2018-09-18 13:10:54 +0200188 * @param[in,out] buf_len Current length of \p word_b.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200189 * @param[in] need_buf Flag if the dynamically allocated buffer is required.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200190 *
191 * @return LY_ERR values.
192 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200193static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +0200194buf_store_char(struct ly_parser_ctx *ctx, const char **input, enum yang_arg arg,
195 char **word_p, size_t *word_len, char **word_b, size_t *buf_len, int need_buf)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200196{
Radek Krejci404251e2018-10-09 12:06:44 +0200197 int prefix = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200198 unsigned int c;
199 size_t len;
200
201 /* get UTF8 code point (and number of bytes coding the character) */
202 LY_CHECK_ERR_RET(ly_getutf8(input, &c, &len),
203 LOGVAL_YANG(ctx, LY_VCODE_INCHAR, (*input)[-len]), LY_EVALID);
204 (*input) -= len;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200205 if (c == '\n') {
206 ctx->indent = 0;
207 } else {
208 /* note - even the multibyte character is count as 1 */
209 ++ctx->indent;
210 }
211
Radek Krejci44ceedc2018-10-02 15:54:31 +0200212 /* check character validity */
213 switch (arg) {
214 case Y_IDENTIF_ARG:
215 LY_CHECK_RET(check_identifierchar(ctx, c, !(*word_len), NULL));
216 break;
217 case Y_PREF_IDENTIF_ARG:
218 LY_CHECK_RET(check_identifierchar(ctx, c, !(*word_len), &prefix));
219 break;
220 case Y_STR_ARG:
221 case Y_MAYBE_STR_ARG:
222 LY_CHECK_RET(check_stringchar(ctx, c));
223 break;
224 }
225
Michal Vasko7fbc8162018-09-17 10:35:16 +0200226 if (word_b && *word_b) {
227 /* add another character into buffer */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200228 if (buf_add_char(ctx->ctx, input, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200229 return LY_EMEM;
230 }
231
232 /* in case of realloc */
233 *word_p = *word_b;
234 } else if (need_buf) {
235 /* first time we need a buffer, copy everything read up to now */
236 if (*word_len) {
237 *word_b = malloc(*word_len);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200238 LY_CHECK_ERR_RET(!*word_b, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200239 *buf_len = *word_len;
240 memcpy(*word_b, *word_p, *word_len);
241 }
242
243 /* add this new character into buffer */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200244 if (buf_add_char(ctx->ctx, input, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200245 return LY_EMEM;
246 }
247
248 /* in case of realloc */
249 *word_p = *word_b;
250 } else {
251 /* just remember the first character pointer */
252 if (!*word_p) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200253 *word_p = (char *)(*input);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200254 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200255 /* ... and update the word's length */
256 (*word_len) += len;
257 (*input) += len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200258 }
259
260 return LY_SUCCESS;
261}
262
Michal Vaskoea5abea2018-09-18 13:10:54 +0200263/**
264 * @brief Skip YANG comment in data.
265 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200266 * @param[in] ctx yang parser context for logging.
267 * @param[in,out] data Data to read from, automatically moved after the comment.
268 * @param[in] comment Type of the comment to process:
269 * 1 for a one-line comment,
270 * 2 for a block comment.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200271 *
272 * @return LY_ERR values.
273 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200274static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +0200275skip_comment(struct ly_parser_ctx *ctx, const char **data, int comment)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200276{
Radek Krejci80dd33e2018-09-26 15:57:18 +0200277 /* internal statuses: 0 - comment ended,
278 * 1 - in line comment,
279 * 2 - in block comment,
280 * 3 - in block comment with last read character '*'
281 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200282 while (**data && comment) {
283 switch (comment) {
284 case 1:
285 if (**data == '\n') {
286 comment = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200287 ++ctx->line;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200288 }
289 break;
290 case 2:
291 if (**data == '*') {
Radek Krejci15c80ca2018-10-09 11:01:31 +0200292 comment = 3;
293 } else if (**data == '\n') {
294 ++ctx->line;
295 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200296 break;
297 case 3:
298 if (**data == '/') {
299 comment = 0;
300 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200301 if (**data == '\n') {
302 ++ctx->line;
303 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200304 comment = 2;
305 }
306 break;
307 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200308 LOGINT_RET(ctx->ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200309 }
310
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200311 if (**data == '\n') {
312 ctx->indent = 0;
313 } else {
314 ++ctx->indent;
315 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200316 ++(*data);
317 }
318
319 if (!**data && (comment > 1)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200320 LOGVAL_YANG(ctx, LYVE_SYNTAX, "Unexpected end-of-file, non-terminated comment.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200321 return LY_EVALID;
322 }
323
324 return LY_SUCCESS;
325}
326
Michal Vaskoea5abea2018-09-18 13:10:54 +0200327/**
328 * @brief Read a quoted string from data.
329 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200330 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200331 * @param[in,out] data Data to read from, always moved to currently handled character.
332 * @param[in] arg Type of YANG keyword argument expected.
333 * @param[out] word_p Pointer to the read quoted string.
334 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read quoted string. If not needed,
335 * set to NULL. Otherwise equal to \p word_p.
336 * @param[out] word_len Length of the read quoted string.
337 * @param[out] buf_len Length of the dynamically-allocated buffer \p word_b.
338 * @param[in] indent Current indent (number of YANG spaces). Needed for correct multi-line string
339 * indenation in the final quoted string.
340 *
341 * @return LY_ERR values.
342 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200343static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +0200344read_qstring(struct ly_parser_ctx *ctx, const char **data, enum yang_arg arg, char **word_p, char **word_b, size_t *word_len,
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200345 size_t *buf_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200346{
347 /* string: 0 - string ended, 1 - string with ', 2 - string with ", 3 - string with " with last character \,
348 * 4 - string finished, now skipping whitespaces looking for +,
349 * 5 - string continues after +, skipping whitespaces */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200350 unsigned int string, block_indent = 0, current_indent = 0, need_buf = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200351 const char *c;
352
353 if (**data == '\"') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200354 string = 2;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200355 current_indent = block_indent = ctx->indent + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200356 } else {
357 assert(**data == '\'');
358 string = 1;
359 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200360 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200361
362 while (**data && string) {
363 switch (string) {
364 case 1:
365 switch (**data) {
366 case '\'':
367 /* string may be finished, but check for + */
368 string = 4;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200369 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200370 break;
371 default:
372 /* check and store character */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200373 LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, buf_len, need_buf));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200374 break;
375 }
376 break;
377 case 2:
378 switch (**data) {
379 case '\"':
380 /* string may be finished, but check for + */
381 string = 4;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200382 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200383 break;
384 case '\\':
385 /* special character following */
386 string = 3;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200387 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200388 break;
389 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200390 if (current_indent < block_indent) {
391 ++current_indent;
392 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200393 } else {
394 /* check and store character */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200395 LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, buf_len, need_buf));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200396 }
397 break;
398 case '\t':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200399 if (current_indent < block_indent) {
400 assert(need_buf);
401 current_indent += 8;
402 ctx->indent += 8;
403 for (; current_indent > block_indent; --current_indent, --ctx->indent) {
404 /* store leftover spaces from the tab */
405 c = " ";
406 LY_CHECK_RET(buf_store_char(ctx, &c, arg, word_p, word_len, word_b, buf_len, need_buf));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200407 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200408 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200409 } else {
410 /* check and store character */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200411 LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, buf_len, need_buf));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200412 /* additional characters for indentation - only 1 was count in buf_store_char */
413 ctx->indent += 7;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200414 }
415 break;
416 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200417 if (block_indent) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200418 /* we will be removing the indents so we need our own buffer */
419 need_buf = 1;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200420
421 /* remove trailing tabs and spaces */
422 while ((*word_len) && ((*word_p)[(*word_len) - 1] == '\t' || (*word_p)[(*word_len) - 1] == ' ')) {
423 --(*word_len);
424 }
425
426 /* start indentation */
427 current_indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200428 }
429
430 /* check and store character */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200431 LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, buf_len, need_buf));
432
433 /* maintain line number */
434 ++ctx->line;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200435
436 /* reset context indentation counter for possible string after this one */
437 ctx->indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200438 break;
439 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200440 /* first non-whitespace character, stop eating indentation */
441 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200442
443 /* check and store character */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200444 LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, buf_len, need_buf));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200445 break;
446 }
447 break;
448 case 3:
449 /* string encoded characters */
450 switch (**data) {
451 case 'n':
452 c = "\n";
453 break;
454 case 't':
455 c = "\t";
456 break;
457 case '\"':
458 c = *data;
459 break;
460 case '\\':
461 c = *data;
462 break;
463 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200464 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Double-quoted string unknown special character '\\%c'.", **data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200465 return LY_EVALID;
466 }
467
468 /* check and store character */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200469 LY_CHECK_RET(buf_store_char(ctx, &c, arg, word_p, word_len, word_b, buf_len, need_buf));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200470
471 string = 2;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200472 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200473 break;
474 case 4:
475 switch (**data) {
476 case '+':
477 /* string continues */
478 string = 5;
Radek Krejciefd22f62018-09-27 11:47:58 +0200479 need_buf = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200480 break;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200481 case '\n':
482 ++ctx->line;
483 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200484 case ' ':
485 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200486 /* just skip */
487 break;
488 default:
489 /* string is finished */
490 goto string_end;
491 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200492 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200493 break;
494 case 5:
495 switch (**data) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200496 case '\n':
497 ++ctx->line;
498 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200499 case ' ':
500 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200501 /* skip */
502 break;
503 case '\'':
504 string = 1;
505 break;
506 case '\"':
507 string = 2;
508 break;
509 default:
510 /* it must be quoted again */
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200511 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Both string parts divided by '+' must be quoted.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200512 return LY_EVALID;
513 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200514 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200515 break;
516 default:
517 return LY_EINT;
518 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200519 }
520
521string_end:
522 return LY_SUCCESS;
523}
524
Michal Vaskoea5abea2018-09-18 13:10:54 +0200525/**
526 * @brief Get another YANG string from the raw data.
527 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200528 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200529 * @param[in,out] data Data to read from, always moved to currently handled character.
530 * @param[in] arg Type of YANG keyword argument expected.
Michal Vasko2ca70f52018-09-27 11:04:51 +0200531 * @param[out] word_p Pointer to the read string. Can return NULL if \p arg is #Y_MAYBE_STR_ARG.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200532 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read string. If not needed,
533 * set to NULL. Otherwise equal to \p word_p.
534 * @param[out] word_len Length of the read string.
535 *
536 * @return LY_ERR values.
Michal Vasko7fbc8162018-09-17 10:35:16 +0200537 */
538static LY_ERR
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200539get_argument(struct ly_parser_ctx *ctx, const char **data, enum yang_arg arg, char **word_p, char **word_b, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200540{
Radek Krejci44ceedc2018-10-02 15:54:31 +0200541 size_t buf_len = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200542 LY_ERR ret;
543
544 /* word buffer - dynamically allocated */
545 *word_b = NULL;
546
547 /* word pointer - just a pointer to data */
548 *word_p = NULL;
549
550 *word_len = 0;
551 while (**data) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200552 switch (**data) {
553 case '\'':
554 case '\"':
555 if (*word_len) {
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200556 /* invalid - quotes cannot be in unquoted string and only optsep, ; or { can follow it */
557 LOGVAL_YANG(ctx, LY_VCODE_INSTREXP, 1, *data,
558 "unquoted string character, optsep, semicolon or opening brace");
559 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200560 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200561 ret = read_qstring(ctx, data, arg, word_p, word_b, word_len, &buf_len);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200562 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200563 goto str_end;
564 case '/':
Radek Krejci44ceedc2018-10-02 15:54:31 +0200565 if ((*data)[1] == '/') {
566 /* one-line comment */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200567 MOVE_INPUT(ctx, data, 2);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200568 ret = skip_comment(ctx, data, 1);
569 } else if ((*data)[1] == '*') {
570 /* block comment */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200571 MOVE_INPUT(ctx, data, 2);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200572 ret = skip_comment(ctx, data, 2);
573 } else {
574 /* not a comment after all */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200575 ret = buf_store_char(ctx, data, arg, word_p, word_len, word_b, &buf_len, 0);
576 }
577 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200578 break;
579 case ' ':
580 if (*word_len) {
581 /* word is finished */
582 goto str_end;
583 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200584 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200585 break;
586 case '\t':
587 if (*word_len) {
588 /* word is finished */
589 goto str_end;
590 }
591 /* tabs count for 8 spaces */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200592 ctx->indent += 8;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200593
594 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200595 break;
596 case '\n':
597 if (*word_len) {
598 /* word is finished */
599 goto str_end;
600 }
601 /* reset indent */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200602 ctx->indent = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200603
604 /* track line numbers */
605 ++ctx->line;
606
607 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200608 break;
609 case ';':
610 case '{':
611 if (*word_len || (arg == Y_MAYBE_STR_ARG)) {
612 /* word is finished */
613 goto str_end;
614 }
615
Radek Krejci44ceedc2018-10-02 15:54:31 +0200616 LOGVAL_YANG(ctx, LY_VCODE_INSTREXP, 1, *data, "an argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200617 return LY_EVALID;
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200618 case '}':
619 /* invalid - braces cannot be in unquoted string (opening braces terminates the string and can follow it) */
620 LOGVAL_YANG(ctx, LY_VCODE_INSTREXP, 1, *data,
621 "unquoted string character, optsep, semicolon or opening brace");
622 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200623 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200624 LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, &buf_len, 0));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200625 break;
626 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200627 }
628
629str_end:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200630 /* terminating NULL byte for buf */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200631 if (*word_b) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200632 (*word_b) = ly_realloc(*word_b, (*word_len) + 1);
633 LY_CHECK_ERR_RET(!(*word_b), LOGMEM(ctx->ctx), LY_EMEM);
634 (*word_b)[*word_len] = '\0';
Michal Vasko7fbc8162018-09-17 10:35:16 +0200635 *word_p = *word_b;
636 }
637
638 return LY_SUCCESS;
639}
640
Michal Vaskoea5abea2018-09-18 13:10:54 +0200641/**
642 * @brief Get another YANG keyword from the raw data.
643 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200644 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200645 * @param[in,out] data Data to read from, always moved to currently handled character.
646 * @param[out] kw YANG keyword read.
647 * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances.
648 * @param[out] word_len Length of the keyword in the data. Useful for extension instances.
649 *
650 * @return LY_ERR values.
651 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200652static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +0200653get_keyword(struct ly_parser_ctx *ctx, const char **data, enum yang_keyword *kw, char **word_p, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200654{
655 LY_ERR ret;
656 int prefix;
657 const char *word_start;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200658 unsigned int c;
659 size_t len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200660
661 if (word_p) {
662 *word_p = NULL;
663 *word_len = 0;
664 }
665
666 /* first skip "optsep", comments */
667 while (**data) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200668 switch (**data) {
669 case '/':
670 if ((*data)[1] == '/') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200671 /* one-line comment */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200672 MOVE_INPUT(ctx, data, 2);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200673 ret = skip_comment(ctx, data, 1);
674 if (ret) {
675 return ret;
676 }
Radek Krejcidcc7b322018-10-11 14:24:02 +0200677 } else if ((*data)[1] == '*') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200678 /* block comment */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200679 MOVE_INPUT(ctx, data, 2);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200680 ret = skip_comment(ctx, data, 2);
681 if (ret) {
682 return ret;
683 }
684 } else {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200685 /* error - not a comment after all, keyword cannot start with slash */
686 LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '/'.");
687 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200688 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200689 break;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200690 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200691 /* skip whitespaces (optsep) */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200692 ++ctx->line;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200693 ctx->indent = 0;
694 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200695 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200696 /* skip whitespaces (optsep) */
697 ++ctx->indent;
698 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200699 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200700 /* skip whitespaces (optsep) */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200701 ctx->indent += 8;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200702 break;
703 default:
704 /* either a keyword start or an invalid character */
705 goto keyword_start;
706 }
707
708 ++(*data);
709 }
710
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200711#define IF_KW(STR, LEN, STMT) if (!strncmp(*(data), STR, LEN)) {MOVE_INPUT(ctx, data, LEN);*kw=STMT;}
712#define IF_KW_PREFIX(STR, LEN) if (!strncmp(*(data), STR, LEN)) {MOVE_INPUT(ctx, data, LEN);
713#define IF_KW_PREFIX_END }
714
Michal Vasko7fbc8162018-09-17 10:35:16 +0200715keyword_start:
716 word_start = *data;
717 *kw = YANG_NONE;
718
719 /* read the keyword itself */
720 switch (**data) {
721 case 'a':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200722 MOVE_INPUT(ctx, data, 1);
723 IF_KW("rgument", 7, YANG_ARGUMENT)
724 else IF_KW("ugment", 6, YANG_AUGMENT)
725 else IF_KW("ction", 5, YANG_ACTION)
726 else IF_KW_PREFIX("ny", 2)
727 IF_KW("data", 4, YANG_ANYDATA)
728 else IF_KW("xml", 3, YANG_ANYXML)
729 IF_KW_PREFIX_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200730 break;
731 case 'b':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200732 MOVE_INPUT(ctx, data, 1);
733 IF_KW("ase", 3, YANG_BASE)
734 else IF_KW("elongs-to", 9, YANG_BELONGS_TO)
735 else IF_KW("it", 2, YANG_BIT)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200736 break;
737 case 'c':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200738 MOVE_INPUT(ctx, data, 1);
739 IF_KW("ase", 3, YANG_CASE)
740 else IF_KW("hoice", 5, YANG_CHOICE)
741 else IF_KW_PREFIX("on", 2)
742 IF_KW("fig", 3, YANG_CONFIG)
743 else IF_KW_PREFIX("ta", 2)
744 IF_KW("ct", 2, YANG_CONTACT)
745 else IF_KW("iner", 4, YANG_CONTAINER)
746 IF_KW_PREFIX_END
747 IF_KW_PREFIX_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200748 break;
749 case 'd':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200750 MOVE_INPUT(ctx, data, 1);
751 IF_KW_PREFIX("e", 1)
752 IF_KW("fault", 5, YANG_DEFAULT)
753 else IF_KW("scription", 9, YANG_DESCRIPTION)
754 else IF_KW_PREFIX("viat", 4)
755 IF_KW("e", 1, YANG_DEVIATE)
756 else IF_KW("ion", 3, YANG_DEVIATION)
757 IF_KW_PREFIX_END
758 IF_KW_PREFIX_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200759 break;
760 case 'e':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200761 MOVE_INPUT(ctx, data, 1);
762 IF_KW("num", 3, YANG_ENUM)
763 else IF_KW_PREFIX("rror-", 5)
764 IF_KW("app-tag", 7, YANG_ERROR_APP_TAG)
765 else IF_KW("message", 7, YANG_ERROR_MESSAGE)
766 IF_KW_PREFIX_END
767 else IF_KW("xtension", 8, YANG_EXTENSION)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200768 break;
769 case 'f':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200770 MOVE_INPUT(ctx, data, 1);
771 IF_KW("eature", 6, YANG_FEATURE)
772 else IF_KW("raction-digits", 14, YANG_FRACTION_DIGITS)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200773 break;
774 case 'g':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200775 MOVE_INPUT(ctx, data, 1);
776 IF_KW("rouping", 7, YANG_GROUPING)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200777 break;
778 case 'i':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200779 MOVE_INPUT(ctx, data, 1);
780 IF_KW("dentity", 7, YANG_IDENTITY)
781 else IF_KW("f-feature", 9, YANG_IF_FEATURE)
782 else IF_KW("mport", 5, YANG_IMPORT)
783 else IF_KW_PREFIX("n", 1)
784 IF_KW("clude", 5, YANG_INCLUDE)
785 else IF_KW("put", 3, YANG_INPUT)
786 IF_KW_PREFIX_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200787 break;
788 case 'k':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200789 MOVE_INPUT(ctx, data, 1);
790 IF_KW("ey", 2, YANG_KEY)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200791 break;
792 case 'l':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200793 MOVE_INPUT(ctx, data, 1);
794 IF_KW_PREFIX("e", 1)
795 IF_KW("af-list", 7, YANG_LEAF_LIST)
796 else IF_KW("af", 2, YANG_LEAF)
797 else IF_KW("ngth", 4, YANG_LENGTH)
798 IF_KW_PREFIX_END
799 else IF_KW("ist", 3, YANG_LIST)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200800 break;
801 case 'm':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200802 MOVE_INPUT(ctx, data, 1);
803 IF_KW_PREFIX("a", 1)
804 IF_KW("ndatory", 7, YANG_MANDATORY)
805 else IF_KW("x-elements", 10, YANG_MAX_ELEMENTS)
806 IF_KW_PREFIX_END
807 else IF_KW("in-elements", 11, YANG_MIN_ELEMENTS)
808 else IF_KW("ust", 3, YANG_MUST)
809 else IF_KW_PREFIX("od", 2)
810 IF_KW("ule", 3, YANG_MODULE)
811 else IF_KW("ifier", 5, YANG_MODIFIER)
812 IF_KW_PREFIX_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200813 break;
814 case 'n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200815 MOVE_INPUT(ctx, data, 1);
816 IF_KW("amespace", 8, YANG_NAMESPACE)
817 else IF_KW("otification", 11, YANG_NOTIFICATION)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200818 break;
819 case 'o':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200820 MOVE_INPUT(ctx, data, 1);
821 IF_KW_PREFIX("r", 1)
822 IF_KW("dered-by", 8, YANG_ORDERED_BY)
823 else IF_KW("ganization", 10, YANG_ORGANIZATION)
824 IF_KW_PREFIX_END
825 else IF_KW("utput", 5, YANG_OUTPUT)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200826 break;
827 case 'p':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200828 MOVE_INPUT(ctx, data, 1);
829 IF_KW("ath", 3, YANG_PATH)
830 else IF_KW("attern", 6, YANG_PATTERN)
831 else IF_KW("osition", 7, YANG_POSITION)
832 else IF_KW_PREFIX("re", 2)
833 IF_KW("fix", 3, YANG_PREFIX)
834 else IF_KW("sence", 5, YANG_PRESENCE)
835 IF_KW_PREFIX_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200836 break;
837 case 'r':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200838 MOVE_INPUT(ctx, data, 1);
839 IF_KW("ange", 4, YANG_RANGE)
840 else IF_KW_PREFIX("e", 1)
841 IF_KW_PREFIX("f", 1)
842 IF_KW("erence", 6, YANG_REFERENCE)
843 else IF_KW("ine", 3, YANG_REFINE)
844 IF_KW_PREFIX_END
845 else IF_KW("quire-instance", 14, YANG_REQUIRE_INSTANCE)
846 else IF_KW("vision-date", 11, YANG_REVISION_DATE)
847 else IF_KW("vision", 6, YANG_REVISION)
848 IF_KW_PREFIX_END
849 else IF_KW("pc", 2, YANG_RPC)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200850 break;
851 case 's':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200852 MOVE_INPUT(ctx, data, 1);
853 IF_KW("tatus", 5, YANG_STATUS)
854 else IF_KW("ubmodule", 8, YANG_SUBMODULE)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200855 break;
856 case 't':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200857 MOVE_INPUT(ctx, data, 1);
858 IF_KW("ypedef", 6, YANG_TYPEDEF)
859 else IF_KW("ype", 3, YANG_TYPE)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200860 break;
861 case 'u':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200862 MOVE_INPUT(ctx, data, 1);
863 IF_KW_PREFIX("ni", 2)
864 IF_KW("que", 3, YANG_UNIQUE)
865 else IF_KW("ts", 2, YANG_UNITS)
866 IF_KW_PREFIX_END
867 else IF_KW("ses", 3, YANG_USES)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200868 break;
869 case 'v':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200870 MOVE_INPUT(ctx, data, 1);
871 IF_KW("alue", 4, YANG_VALUE)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200872 break;
873 case 'w':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200874 MOVE_INPUT(ctx, data, 1);
875 IF_KW("hen", 3, YANG_WHEN)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200876 break;
877 case 'y':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200878 MOVE_INPUT(ctx, data, 1);
879 IF_KW("ang-version", 11, YANG_YANG_VERSION)
880 else IF_KW("in-element", 10, YANG_YIN_ELEMENT)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200881 break;
882 case ';':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200883 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200884 *kw = YANG_SEMICOLON;
Radek Krejci626df482018-10-11 15:06:31 +0200885 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200886 case '{':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200887 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200888 *kw = YANG_LEFT_BRACE;
Radek Krejci626df482018-10-11 15:06:31 +0200889 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200890 case '}':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200891 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200892 *kw = YANG_RIGHT_BRACE;
Radek Krejci626df482018-10-11 15:06:31 +0200893 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200894 default:
895 break;
896 }
897
898 if (*kw != YANG_NONE) {
899 /* make sure we have the whole keyword */
900 switch (**data) {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200901 case '\n':
902 ++ctx->line;
903 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200904 case ' ':
905 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200906 /* mandatory "sep" */
907 break;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200908 case ':':
909 /* keyword is not actually a keyword, but prefix of an extension.
910 * To avoid repeated check of the prefix syntax, move to the point where the colon was read
911 * and we will be checking the keyword (extension instance) itself */
912 prefix = 1;
913 MOVE_INPUT(ctx, data, 1);
914 goto extension;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200915 case '{':
916 /* allowed only for input and output statements which can be without arguments */
917 if (*kw == YANG_INPUT || *kw == YANG_OUTPUT) {
918 break;
919 }
920 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200921 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200922 MOVE_INPUT(ctx, data, 1);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200923 LOGVAL_YANG(ctx, LY_VCODE_INSTREXP, (int)(*data - word_start), word_start,
924 "a keyword followed by a separator");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200925 return LY_EVALID;
926 }
927 } else {
928 /* still can be an extension */
929 prefix = 0;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200930extension:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200931 while (**data && (**data != ' ') && (**data != '\t') && (**data != '\n') && (**data != '{') && (**data != ';')) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200932 LY_CHECK_ERR_RET(ly_getutf8(data, &c, &len),
933 LOGVAL_YANG(ctx, LY_VCODE_INCHAR, (*data)[-len]), LY_EVALID);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200934 ++ctx->indent;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200935 /* check character validity */
936 LY_CHECK_RET(check_identifierchar(ctx, c, *data - len == word_start ? 1 : 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200937 }
938 if (!**data) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200939 LOGVAL_YANG(ctx, LY_VCODE_EOF);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200940 return LY_EVALID;
941 }
942
943 /* prefix is mandatory for extension instances */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200944 if (prefix != 2) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200945 LOGVAL_YANG(ctx, LY_VCODE_INSTREXP, (int)(*data - word_start), word_start, "a keyword");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200946 return LY_EVALID;
947 }
948
949 *kw = YANG_CUSTOM;
950 }
Radek Krejci626df482018-10-11 15:06:31 +0200951success:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200952 if (word_p) {
953 *word_p = (char *)word_start;
954 *word_len = *data - word_start;
955 }
956
957 return LY_SUCCESS;
958}
959
Michal Vaskoea5abea2018-09-18 13:10:54 +0200960/**
961 * @brief Parse extension instance substatements.
962 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200963 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200964 * @param[in,out] data Data to read from, always moved to currently handled character.
965 * @param[in] word Extension instance substatement name (keyword).
966 * @param[in] word_len Extension instance substatement name length.
967 * @param[in,out] child Children of this extension instance to add to.
968 *
969 * @return LY_ERR values.
970 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200971static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +0200972parse_ext_substmt(struct ly_parser_ctx *ctx, const char **data, char *word, size_t word_len,
Michal Vasko7fbc8162018-09-17 10:35:16 +0200973 struct lysp_stmt **child)
974{
975 char *buf;
976 LY_ERR ret = 0;
977 enum yang_keyword kw;
978 struct lysp_stmt *stmt, *par_child;
979
980 stmt = calloc(1, sizeof *stmt);
981 LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM);
982
Radek Krejci44ceedc2018-10-02 15:54:31 +0200983 stmt->stmt = lydict_insert(ctx->ctx, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200984
985 /* get optional argument */
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200986 ret = get_argument(ctx, data, Y_MAYBE_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +0200987 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200988
Radek Krejci0ae092d2018-09-20 16:43:19 +0200989 if (word) {
990 if (buf) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200991 stmt->arg = lydict_insert_zc(ctx->ctx, word);
Radek Krejci0ae092d2018-09-20 16:43:19 +0200992 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200993 stmt->arg = lydict_insert(ctx->ctx, word, word_len);
Radek Krejci0ae092d2018-09-20 16:43:19 +0200994 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200995 }
996
997 /* insert into parent statements */
998 if (!*child) {
999 *child = stmt;
1000 } else {
1001 for (par_child = *child; par_child->next; par_child = par_child->next);
1002 par_child->next = stmt;
1003 }
1004
1005 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001006 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001007
1008 ret = parse_ext_substmt(ctx, data, word, word_len, &stmt->child);
Radek Krejcic59bc972018-09-17 16:13:06 +02001009 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001010 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001011 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001012
1013 return ret;
1014}
1015
Michal Vaskoea5abea2018-09-18 13:10:54 +02001016/**
1017 * @brief Parse extension instance.
1018 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001019 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001020 * @param[in,out] data Data to read from, always moved to currently handled character.
1021 * @param[in] ext_name Extension instance substatement name (keyword).
1022 * @param[in] ext_name_len Extension instance substatement name length.
1023 * @param[in] insubstmt Type of the keyword this extension instance is a substatement of.
1024 * @param[in] insubstmt_index Index of the keyword instance this extension instance is a substatement of.
1025 * @param[in,out] exts Extension instances to add to.
1026 *
1027 * @return LY_ERR values.
1028 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001029static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001030parse_ext(struct ly_parser_ctx *ctx, const char **data, const char *ext_name, int ext_name_len, LYEXT_SUBSTMT insubstmt,
Michal Vasko7fbc8162018-09-17 10:35:16 +02001031 uint32_t insubstmt_index, struct lysp_ext_instance **exts)
1032{
1033 LY_ERR ret = 0;
1034 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001035 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001036 struct lysp_ext_instance *e;
1037 enum yang_keyword kw;
1038
1039 LYSP_ARRAY_NEW_RET(ctx, exts, e, LY_EMEM);
1040
1041 /* store name and insubstmt info */
Radek Krejci44ceedc2018-10-02 15:54:31 +02001042 e->name = lydict_insert(ctx->ctx, ext_name, ext_name_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001043 e->insubstmt = insubstmt;
1044 e->insubstmt_index = insubstmt_index;
1045
1046 /* get optional argument */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001047 ret = get_argument(ctx, data, Y_MAYBE_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001048 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001049
Radek Krejci0ae092d2018-09-20 16:43:19 +02001050 if (word) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001051 INSERT_WORD(ctx, buf, e->argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001052 }
1053
1054 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001055 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001056
1057 ret = parse_ext_substmt(ctx, data, word, word_len, &e->child);
Radek Krejcic59bc972018-09-17 16:13:06 +02001058 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001059 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001060 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001061
1062 return ret;
1063}
1064
Michal Vaskoea5abea2018-09-18 13:10:54 +02001065/**
1066 * @brief Parse a generic text field without specific constraints. Those are contact, organization,
1067 * description, etc...
1068 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001069 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001070 * @param[in,out] data Data to read from, always moved to currently handled character.
1071 * @param[in] substmt Type of this substatement.
1072 * @param[in] substmt_index Index of this substatement.
1073 * @param[in,out] value Place to store the parsed value.
1074 * @param[in] arg Type of the YANG keyword argument (of the value).
1075 * @param[in,out] exts Extension instances to add to.
1076 *
1077 * @return LY_ERR values.
1078 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001079static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001080parse_text_field(struct ly_parser_ctx *ctx, const char **data, LYEXT_SUBSTMT substmt, uint32_t substmt_index,
Michal Vasko7fbc8162018-09-17 10:35:16 +02001081 const char **value, enum yang_arg arg, struct lysp_ext_instance **exts)
1082{
1083 LY_ERR ret = 0;
1084 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001085 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001086 enum yang_keyword kw;
1087
1088 if (*value) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001089 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001090 return LY_EVALID;
1091 }
1092
1093 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001094 ret = get_argument(ctx, data, arg, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001095 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001096
1097 /* store value and spend buf if allocated */
Radek Krejci44ceedc2018-10-02 15:54:31 +02001098 INSERT_WORD(ctx, buf, *value, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001099
1100 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001101 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001102
1103 switch (kw) {
1104 case YANG_CUSTOM:
1105 ret = parse_ext(ctx, data, word, word_len, substmt, substmt_index, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02001106 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001107 break;
1108 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001109 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001110 return LY_EVALID;
1111 }
1112 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001113 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001114
1115 return ret;
1116}
1117
Michal Vaskoea5abea2018-09-18 13:10:54 +02001118/**
1119 * @brief Parse the yang-version statement.
1120 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001121 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001122 * @param[in,out] data Data to read from, always moved to currently handled character.
1123 * @param[in] mod Module to store the parsed information in.
1124 *
1125 * @return LY_ERR values.
1126 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001127static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001128parse_yangversion(struct ly_parser_ctx *ctx, const char **data, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001129{
1130 LY_ERR ret = 0;
1131 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001132 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001133 enum yang_keyword kw;
1134
1135 if (mod->version) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001136 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001137 return LY_EVALID;
1138 }
1139
1140 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001141 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001142 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001143
1144 if ((word_len == 3) && !strncmp(word, "1.0", word_len)) {
1145 mod->version = LYS_VERSION_1_0;
1146 } else if ((word_len == 3) && !strncmp(word, "1.1", word_len)) {
1147 mod->version = LYS_VERSION_1_1;
1148 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001149 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001150 free(buf);
1151 return LY_EVALID;
1152 }
1153 free(buf);
1154
1155 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001156 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001157
1158 switch (kw) {
1159 case YANG_CUSTOM:
1160 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_VERSION, 0, &mod->exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02001161 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001162 break;
1163 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001164 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001165 return LY_EVALID;
1166 }
1167 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001168 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001169
1170 return ret;
1171}
1172
Michal Vaskoea5abea2018-09-18 13:10:54 +02001173/**
1174 * @brief Parse the belongs-to statement.
1175 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001176 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001177 * @param[in,out] data Data to read from, always moved to currently handled character.
1178 * @param[in,out] belongsto Place to store the parsed value.
1179 * @param[in,out] prefix Place to store the parsed belongs-to prefix value.
1180 * @param[in,out] exts Extension instances to add to.
1181 *
1182 * @return LY_ERR values.
1183 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001184static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001185parse_belongsto(struct ly_parser_ctx *ctx, const char **data, const char **belongsto, const char **prefix, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001186{
1187 LY_ERR ret = 0;
1188 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001189 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001190 enum yang_keyword kw;
1191
1192 if (*belongsto) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001193 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001194 return LY_EVALID;
1195 }
1196
1197 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001198 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001199 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001200
Radek Krejci44ceedc2018-10-02 15:54:31 +02001201 INSERT_WORD(ctx, buf, *belongsto, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001202 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001203 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001204
1205 switch (kw) {
1206 case YANG_PREFIX:
1207 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_PREFIX, 0, prefix, Y_IDENTIF_ARG, exts);
1208 break;
1209 case YANG_CUSTOM:
1210 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_BELONGSTO, 0, exts);
1211 break;
1212 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001213 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001214 return LY_EVALID;
1215 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001216 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001217 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001218 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001219
1220 /* mandatory substatements */
1221 if (!*prefix) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001222 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001223 return LY_EVALID;
1224 }
1225
1226 return ret;
1227}
1228
Michal Vaskoea5abea2018-09-18 13:10:54 +02001229/**
1230 * @brief Parse the revision-date statement.
1231 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001232 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001233 * @param[in,out] data Data to read from, always moved to currently handled character.
1234 * @param[in,out] rev Array to store the parsed value in.
1235 * @param[in,out] exts Extension instances to add to.
1236 *
1237 * @return LY_ERR values.
1238 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001239static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001240parse_revisiondate(struct ly_parser_ctx *ctx, const char **data, char *rev, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001241{
1242 LY_ERR ret = 0;
1243 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001244 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001245 enum yang_keyword kw;
1246
1247 if (rev[0]) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001248 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001249 return LY_EVALID;
1250 }
1251
1252 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001253 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001254 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001255
1256 /* check value */
Radek Krejci44ceedc2018-10-02 15:54:31 +02001257 if (lysp_check_date(ctx->ctx, word, word_len, "revision-date")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001258 free(buf);
1259 return LY_EVALID;
1260 }
1261
1262 /* store value and spend buf if allocated */
1263 strncpy(rev, word, word_len);
1264 free(buf);
1265
1266 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001267 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001268
1269 switch (kw) {
1270 case YANG_CUSTOM:
1271 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_REVISIONDATE, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02001272 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001273 break;
1274 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001275 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001276 return LY_EVALID;
1277 }
1278 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001279 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001280
1281 return ret;
1282}
1283
Michal Vaskoea5abea2018-09-18 13:10:54 +02001284/**
1285 * @brief Parse the include statement.
1286 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001287 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001288 * @param[in,out] data Data to read from, always moved to currently handled character.
1289 * @param[in,out] includes Parsed includes to add to.
1290 *
1291 * @return LY_ERR values.
1292 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001293static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001294parse_include(struct ly_parser_ctx *ctx, const char **data, struct lysp_include **includes)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001295{
1296 LY_ERR ret = 0;
1297 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001298 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001299 enum yang_keyword kw;
1300 struct lysp_include *inc;
1301
1302 LYSP_ARRAY_NEW_RET(ctx, includes, inc, LY_EMEM);
1303
1304 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001305 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001306 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001307
Radek Krejci44ceedc2018-10-02 15:54:31 +02001308 INSERT_WORD(ctx, buf, inc->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001309 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001310 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001311
1312 switch (kw) {
1313 case YANG_DESCRIPTION:
1314 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &inc->dsc, Y_STR_ARG, &inc->exts);
1315 break;
1316 case YANG_REFERENCE:
1317 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &inc->ref, Y_STR_ARG, &inc->exts);
1318 break;
1319 case YANG_REVISION_DATE:
1320 ret = parse_revisiondate(ctx, data, inc->rev, &inc->exts);
1321 break;
1322 case YANG_CUSTOM:
1323 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inc->exts);
1324 break;
1325 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001326 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001327 return LY_EVALID;
1328 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001329 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001330 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001331 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001332
1333 return ret;
1334}
1335
Michal Vaskoea5abea2018-09-18 13:10:54 +02001336/**
1337 * @brief Parse the import statement.
1338 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001339 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001340 * @param[in,out] data Data to read from, always moved to currently handled character.
1341 * @param[in,out] imports Parsed imports to add to.
1342 *
1343 * @return LY_ERR values.
1344 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001345static LY_ERR
Radek Krejci70853c52018-10-15 14:46:16 +02001346parse_import(struct ly_parser_ctx *ctx, const char **data, struct lysp_module *module)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001347{
1348 LY_ERR ret = 0;
1349 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001350 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001351 enum yang_keyword kw;
1352 struct lysp_import *imp;
1353
Radek Krejci70853c52018-10-15 14:46:16 +02001354 LYSP_ARRAY_NEW_RET(ctx, &module->imports, imp, LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001355
1356 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001357 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001358 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001359
Radek Krejci44ceedc2018-10-02 15:54:31 +02001360 INSERT_WORD(ctx, buf, imp->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001361 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001362 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001363
1364 switch (kw) {
1365 case YANG_PREFIX:
1366 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_PREFIX, 0, &imp->prefix, Y_IDENTIF_ARG, &imp->exts);
Radek Krejci70853c52018-10-15 14:46:16 +02001367 LY_CHECK_RET(lysp_check_prefix(ctx, module, &imp->prefix), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001368 break;
1369 case YANG_DESCRIPTION:
1370 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &imp->dsc, Y_STR_ARG, &imp->exts);
1371 break;
1372 case YANG_REFERENCE:
1373 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &imp->ref, Y_STR_ARG, &imp->exts);
1374 break;
1375 case YANG_REVISION_DATE:
1376 ret = parse_revisiondate(ctx, data, imp->rev, &imp->exts);
1377 break;
1378 case YANG_CUSTOM:
1379 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &imp->exts);
1380 break;
1381 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001382 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001383 return LY_EVALID;
1384 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001385 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001386 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001387 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001388
1389 /* mandatory substatements */
1390 if (!imp->prefix) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001391 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "prefix", "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001392 return LY_EVALID;
1393 }
1394
1395 return ret;
1396}
1397
Michal Vaskoea5abea2018-09-18 13:10:54 +02001398/**
1399 * @brief Parse the revision statement.
1400 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001401 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001402 * @param[in,out] data Data to read from, always moved to currently handled character.
1403 * @param[in,out] revs Parsed revisions to add to.
1404 *
1405 * @return LY_ERR values.
1406 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001407static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001408parse_revision(struct ly_parser_ctx *ctx, const char **data, struct lysp_revision **revs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001409{
1410 LY_ERR ret = 0;
1411 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001412 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001413 enum yang_keyword kw;
1414 struct lysp_revision *rev;
1415
1416 LYSP_ARRAY_NEW_RET(ctx, revs, rev, LY_EMEM);
1417
1418 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001419 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001420 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001421
1422 /* check value */
Radek Krejci44ceedc2018-10-02 15:54:31 +02001423 if (lysp_check_date(ctx->ctx, word, word_len, "revision")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001424 return LY_EVALID;
1425 }
1426
1427 strncpy(rev->rev, word, word_len);
1428 free(buf);
1429
1430 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001431 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001432
1433 switch (kw) {
1434 case YANG_DESCRIPTION:
1435 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &rev->dsc, Y_STR_ARG, &rev->exts);
1436 break;
1437 case YANG_REFERENCE:
1438 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &rev->ref, Y_STR_ARG, &rev->exts);
1439 break;
1440 case YANG_CUSTOM:
1441 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rev->exts);
1442 break;
1443 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001444 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001445 return LY_EVALID;
1446 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001447 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001448 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001449 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001450
1451 return ret;
1452}
1453
Michal Vaskoea5abea2018-09-18 13:10:54 +02001454/**
1455 * @brief Parse a generic text field that can have more instances such as base.
1456 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001457 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001458 * @param[in,out] data Data to read from, always moved to currently handled character.
1459 * @param[in] substmt Type of this substatement.
1460 * @param[in,out] texts Parsed values to add to.
1461 * @param[in] arg Type of the expected argument.
1462 * @param[in,out] exts Extension instances to add to.
1463 *
1464 * @return LY_ERR values.
1465 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001466static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001467parse_text_fields(struct ly_parser_ctx *ctx, const char **data, LYEXT_SUBSTMT substmt, const char ***texts, enum yang_arg arg,
Michal Vasko7fbc8162018-09-17 10:35:16 +02001468 struct lysp_ext_instance **exts)
1469{
1470 LY_ERR ret = 0;
1471 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001472 size_t count, word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001473 enum yang_keyword kw;
1474
1475 /* allocate new pointer */
1476 for (count = 1; (*texts) && (*texts)[count - 1]; ++count);
1477 *texts = realloc(*texts, 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);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001485 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001486 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001487
1488 switch (kw) {
1489 case YANG_CUSTOM:
1490 ret = parse_ext(ctx, data, word, word_len, substmt, count - 1, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02001491 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001492 break;
1493 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001494 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001495 return LY_EVALID;
1496 }
1497 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001498 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001499
1500 return ret;
1501}
1502
Michal Vaskoea5abea2018-09-18 13:10:54 +02001503/**
1504 * @brief Parse the config statement.
1505 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001506 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001507 * @param[in,out] data Data to read from, always moved to currently handled character.
1508 * @param[in,out] flags Flags to add to.
1509 * @param[in,out] exts Extension instances to add to.
1510 *
1511 * @return LY_ERR values.
1512 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001513static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001514parse_config(struct ly_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001515{
1516 LY_ERR ret = 0;
1517 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001518 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001519 enum yang_keyword kw;
1520
1521 if (*flags & LYS_CONFIG_MASK) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001522 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001523 return LY_EVALID;
1524 }
1525
1526 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001527 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001528 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001529
1530 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1531 *flags |= LYS_CONFIG_W;
1532 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1533 *flags |= LYS_CONFIG_R;
1534 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001535 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001536 free(buf);
1537 return LY_EVALID;
1538 }
1539 free(buf);
1540
1541 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001542 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001543
1544 switch (kw) {
1545 case YANG_CUSTOM:
1546 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_CONFIG, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02001547 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001548 break;
1549 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001550 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001551 return LY_EVALID;
1552 }
1553 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001554 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001555
1556 return ret;
1557}
1558
Michal Vaskoea5abea2018-09-18 13:10:54 +02001559/**
1560 * @brief Parse the mandatory statement.
1561 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001562 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001563 * @param[in,out] data Data to read from, always moved to currently handled character.
1564 * @param[in,out] flags Flags to add to.
1565 * @param[in,out] exts Extension instances to add to.
1566 *
1567 * @return LY_ERR values.
1568 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001569static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001570parse_mandatory(struct ly_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001571{
1572 LY_ERR ret = 0;
1573 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001574 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001575 enum yang_keyword kw;
1576
1577 if (*flags & LYS_MAND_MASK) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001578 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001579 return LY_EVALID;
1580 }
1581
1582 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001583 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001584 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001585
1586 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1587 *flags |= LYS_MAND_TRUE;
1588 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1589 *flags |= LYS_MAND_FALSE;
1590 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001591 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001592 free(buf);
1593 return LY_EVALID;
1594 }
1595 free(buf);
1596
1597 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001598 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001599
1600 switch (kw) {
1601 case YANG_CUSTOM:
1602 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MANDATORY, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02001603 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001604 break;
1605 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001606 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001607 return LY_EVALID;
1608 }
1609 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001610 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001611
1612 return ret;
1613}
1614
Michal Vaskoea5abea2018-09-18 13:10:54 +02001615/**
1616 * @brief Parse a restriction such as range or length.
1617 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001618 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001619 * @param[in,out] data Data to read from, always moved to currently handled character.
1620 * @param[in] restr_kw Type of this particular restriction.
1621 * @param[in,out] exts Extension instances to add to.
1622 *
1623 * @return LY_ERR values.
1624 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001625static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001626parse_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 +02001627{
1628 LY_ERR ret = 0;
1629 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001630 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001631 enum yang_keyword kw;
1632
1633 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001634 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001635 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001636
Radek Krejci44ceedc2018-10-02 15:54:31 +02001637 INSERT_WORD(ctx, buf, restr->arg, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001638 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001639 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001640
1641 switch (kw) {
1642 case YANG_DESCRIPTION:
1643 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts);
1644 break;
1645 case YANG_REFERENCE:
1646 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts);
1647 break;
1648 case YANG_ERROR_APP_TAG:
1649 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRTAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts);
1650 break;
1651 case YANG_ERROR_MESSAGE:
1652 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRMSG, 0, &restr->emsg, Y_STR_ARG, &restr->exts);
1653 break;
1654 case YANG_CUSTOM:
1655 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts);
1656 break;
1657 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001658 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001659 return LY_EVALID;
1660 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001661 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001662 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001663 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001664
1665 return ret;
1666}
1667
Michal Vaskoea5abea2018-09-18 13:10:54 +02001668/**
1669 * @brief Parse a restriction that can have more instances such as must.
1670 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001671 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001672 * @param[in,out] data Data to read from, always moved to currently handled character.
1673 * @param[in] restr_kw Type of this particular restriction.
1674 * @param[in,out] restrs Restrictions to add to.
1675 *
1676 * @return LY_ERR values.
1677 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001678static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001679parse_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 +02001680{
1681 struct lysp_restr *restr;
1682
1683 LYSP_ARRAY_NEW_RET(ctx, restrs, restr, LY_EMEM);
1684
1685 return parse_restr(ctx, data, restr_kw, restr);
1686}
1687
Michal Vaskoea5abea2018-09-18 13:10:54 +02001688/**
1689 * @brief Parse the status statement.
1690 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001691 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001692 * @param[in,out] data Data to read from, always moved to currently handled character.
1693 * @param[in,out] flags Flags to add to.
1694 * @param[in,out] exts Extension instances to add to.
1695 *
1696 * @return LY_ERR values.
1697 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001698static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001699parse_status(struct ly_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001700{
1701 LY_ERR ret = 0;
1702 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001703 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001704 enum yang_keyword kw;
1705
1706 if (*flags & LYS_STATUS_MASK) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001707 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001708 return LY_EVALID;
1709 }
1710
1711 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001712 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001713 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001714
1715 if ((word_len == 7) && !strncmp(word, "current", word_len)) {
1716 *flags |= LYS_STATUS_CURR;
1717 } else if ((word_len == 10) && !strncmp(word, "deprecated", word_len)) {
1718 *flags |= LYS_STATUS_DEPRC;
1719 } else if ((word_len == 8) && !strncmp(word, "obsolete", word_len)) {
1720 *flags |= LYS_STATUS_OBSLT;
1721 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001722 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001723 free(buf);
1724 return LY_EVALID;
1725 }
1726 free(buf);
1727
1728 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001729 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001730
1731 switch (kw) {
1732 case YANG_CUSTOM:
1733 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_STATUS, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02001734 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001735 break;
1736 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001737 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001738 return LY_EVALID;
1739 }
1740 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001741 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001742
1743 return ret;
1744}
1745
Michal Vaskoea5abea2018-09-18 13:10:54 +02001746/**
1747 * @brief Parse the when statement.
1748 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001749 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001750 * @param[in,out] data Data to read from, always moved to currently handled character.
1751 * @param[in,out] when_p When pointer to parse to.
1752 *
1753 * @return LY_ERR values.
1754 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001755static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001756parse_when(struct ly_parser_ctx *ctx, const char **data, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001757{
1758 LY_ERR ret = 0;
1759 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001760 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001761 enum yang_keyword kw;
1762 struct lysp_when *when;
1763
1764 if (*when_p) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001765 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001766 return LY_EVALID;
1767 }
1768
1769 when = calloc(1, sizeof *when);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001770 LY_CHECK_ERR_RET(!when, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001771 *when_p = when;
1772
1773 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001774 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001775 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001776
Radek Krejci44ceedc2018-10-02 15:54:31 +02001777 INSERT_WORD(ctx, buf, when->cond, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001778 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001779 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001780
1781 switch (kw) {
1782 case YANG_DESCRIPTION:
1783 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &when->dsc, Y_STR_ARG, &when->exts);
1784 break;
1785 case YANG_REFERENCE:
1786 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &when->ref, Y_STR_ARG, &when->exts);
1787 break;
1788 case YANG_CUSTOM:
1789 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &when->exts);
1790 break;
1791 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001792 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001793 return LY_EVALID;
1794 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001795 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001796 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001797 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001798
1799 return ret;
1800}
1801
Michal Vaskoea5abea2018-09-18 13:10:54 +02001802/**
1803 * @brief Parse the anydata or anyxml statement.
1804 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001805 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001806 * @param[in,out] data Data to read from, always moved to currently handled character.
1807 * @param[in] kw Type of this particular keyword.
1808 * @param[in,out] siblings Siblings to add to.
1809 *
1810 * @return LY_ERR values.
1811 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001812static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001813parse_any(struct ly_parser_ctx *ctx, const char **data, enum yang_keyword kw, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001814{
1815 LY_ERR ret = 0;
1816 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001817 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001818 struct lysp_node *iter;
1819 struct lysp_node_anydata *any;
1820
1821 /* create structure */
1822 any = calloc(1, sizeof *any);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001823 LY_CHECK_ERR_RET(!any, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001824 any->nodetype = kw == YANG_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
1825
1826 /* insert into siblings */
1827 if (!*siblings) {
1828 *siblings = (struct lysp_node *)any;
1829 } else {
1830 for (iter = *siblings; iter->next; iter = iter->next);
1831 iter->next = (struct lysp_node *)any;
1832 }
1833
1834 /* get name */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001835 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001836 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001837
Radek Krejci44ceedc2018-10-02 15:54:31 +02001838 INSERT_WORD(ctx, buf, any->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001839
1840 /* parse substatements */
1841 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001842 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001843
1844 switch (kw) {
1845 case YANG_CONFIG:
1846 ret = parse_config(ctx, data, &any->flags, &any->exts);
1847 break;
1848 case YANG_DESCRIPTION:
1849 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &any->dsc, Y_STR_ARG, &any->exts);
1850 break;
1851 case YANG_IF_FEATURE:
1852 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &any->iffeatures, Y_STR_ARG, &any->exts);
1853 break;
1854 case YANG_MANDATORY:
1855 ret = parse_mandatory(ctx, data, &any->flags, &any->exts);
1856 break;
1857 case YANG_MUST:
1858 ret = parse_restrs(ctx, data, kw, &any->musts);
1859 break;
1860 case YANG_REFERENCE:
1861 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &any->ref, Y_STR_ARG, &any->exts);
1862 break;
1863 case YANG_STATUS:
1864 ret = parse_status(ctx, data, &any->flags, &any->exts);
1865 break;
1866 case YANG_WHEN:
1867 ret = parse_when(ctx, data, &any->when);
1868 break;
1869 case YANG_CUSTOM:
1870 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &any->exts);
1871 break;
1872 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001873 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
Radek Krejcic59bc972018-09-17 16:13:06 +02001874 (any->nodetype & LYS_ANYDATA) == LYS_ANYDATA ? ly_stmt2str(YANG_ANYDATA) : ly_stmt2str(YANG_ANYXML));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001875 return LY_EVALID;
1876 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001877 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001878 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001879 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001880
1881 return ret;
1882}
1883
Michal Vaskoea5abea2018-09-18 13:10:54 +02001884/**
1885 * @brief Parse the value or position statement. Substatement of type enum statement.
1886 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001887 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001888 * @param[in,out] data Data to read from, always moved to currently handled character.
1889 * @param[in] val_kw Type of this particular keyword.
1890 * @param[in,out] value Value to write to.
1891 * @param[in,out] flags Flags to write to.
1892 * @param[in,out] exts Extension instances to add to.
1893 *
1894 * @return LY_ERR values.
1895 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001896static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001897parse_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 +02001898 struct lysp_ext_instance **exts)
1899{
1900 LY_ERR ret = 0;
1901 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001902 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001903 long int num;
1904 unsigned long int unum;
1905 enum yang_keyword kw;
1906
1907 if (*flags & LYS_SET_VALUE) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001908 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001909 return LY_EVALID;
1910 }
1911 *flags |= LYS_SET_VALUE;
1912
1913 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001914 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001915 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001916
1917 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 +02001918 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001919 free(buf);
1920 return LY_EVALID;
1921 }
1922
1923 errno = 0;
1924 if (val_kw == YANG_VALUE) {
1925 num = strtol(word, &ptr, 10);
1926 } else {
1927 unum = strtoul(word, &ptr, 10);
1928 }
1929 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001930 if ((size_t)(ptr - word) != word_len) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001931 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001932 free(buf);
1933 return LY_EVALID;
1934 }
1935 if (errno == ERANGE) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001936 LOGVAL_YANG(ctx, LY_VCODE_OOB, word_len, word, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001937 free(buf);
1938 return LY_EVALID;
1939 }
1940 if (val_kw == YANG_VALUE) {
1941 *value = num;
1942 } else {
1943 *value = unum;
1944 }
1945 free(buf);
1946
1947 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001948 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001949
1950 switch (kw) {
1951 case YANG_CUSTOM:
1952 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 +02001953 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001954 break;
1955 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02001956 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001957 return LY_EVALID;
1958 }
1959 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001960 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001961
1962 return ret;
1963}
1964
Michal Vaskoea5abea2018-09-18 13:10:54 +02001965/**
1966 * @brief Parse the enum or bit statement. Substatement of type statement.
1967 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001968 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001969 * @param[in,out] data Data to read from, always moved to currently handled character.
1970 * @param[in] enum_kw Type of this particular keyword.
1971 * @param[in,out] enums Enums or bits to add to.
1972 *
1973 * @return LY_ERR values.
1974 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001975static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02001976parse_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 +02001977{
1978 LY_ERR ret = 0;
1979 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001980 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001981 enum yang_keyword kw;
1982 struct lysp_type_enum *enm;
1983
1984 LYSP_ARRAY_NEW_RET(ctx, enums, enm, LY_EMEM);
1985
1986 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02001987 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001988 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001989
Radek Krejci44ceedc2018-10-02 15:54:31 +02001990 INSERT_WORD(ctx, buf, enm->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001991 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001992 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001993
1994 switch (kw) {
1995 case YANG_DESCRIPTION:
1996 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &enm->dsc, Y_STR_ARG, &enm->exts);
1997 break;
1998 case YANG_IF_FEATURE:
1999 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &enm->iffeatures, Y_STR_ARG, &enm->exts);
2000 break;
2001 case YANG_REFERENCE:
2002 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &enm->ref, Y_STR_ARG, &enm->exts);
2003 break;
2004 case YANG_STATUS:
2005 ret = parse_status(ctx, data, &enm->flags, &enm->exts);
2006 break;
2007 case YANG_VALUE:
2008 case YANG_POSITION:
2009 ret = parse_type_enum_value_pos(ctx, data, kw, &enm->value, &enm->flags, &enm->exts);
2010 break;
2011 case YANG_CUSTOM:
2012 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &enm->exts);
2013 break;
2014 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002015 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002016 return LY_EVALID;
2017 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002018 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002019 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002020 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002021
2022 return ret;
2023}
2024
Michal Vaskoea5abea2018-09-18 13:10:54 +02002025/**
2026 * @brief Parse the fraction-digits statement. Substatement of type statement.
2027 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002028 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002029 * @param[in,out] data Data to read from, always moved to currently handled character.
2030 * @param[in,out] fracdig Value to write to.
2031 * @param[in,out] exts Extension instances to add to.
2032 *
2033 * @return LY_ERR values.
2034 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002035static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002036parse_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 +02002037{
2038 LY_ERR ret = 0;
2039 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002040 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002041 unsigned long int num;
2042 enum yang_keyword kw;
2043
2044 if (*fracdig) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002045 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002046 return LY_EVALID;
2047 }
2048
2049 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002050 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002051 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002052
2053 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002054 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002055 free(buf);
2056 return LY_EVALID;
2057 }
2058
2059 errno = 0;
2060 num = strtoul(word, &ptr, 10);
2061 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002062 if ((size_t)(ptr - word) != word_len) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002063 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002064 free(buf);
2065 return LY_EVALID;
2066 }
2067 if ((errno == ERANGE) || (num > 18)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002068 LOGVAL_YANG(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002069 free(buf);
2070 return LY_EVALID;
2071 }
2072 *fracdig = num;
2073 free(buf);
2074
2075 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002076 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002077
2078 switch (kw) {
2079 case YANG_CUSTOM:
2080 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_FRACDIGITS, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02002081 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002082 break;
2083 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002084 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002085 return LY_EVALID;
2086 }
2087 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002088 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002089
2090 return ret;
2091}
2092
Michal Vaskoea5abea2018-09-18 13:10:54 +02002093/**
2094 * @brief Parse the require-instance statement. Substatement of type statement.
2095 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002096 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002097 * @param[in,out] data Data to read from, always moved to currently handled character.
2098 * @param[in,out] reqinst Value to write to.
2099 * @param[in,out] flags Flags to write to.
2100 * @param[in,out] exts Extension instances to add to.
2101 *
2102 * @return LY_ERR values.
2103 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002104static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002105parse_type_reqinstance(struct ly_parser_ctx *ctx, const char **data, uint8_t *reqinst, uint16_t *flags,
Michal Vasko7fbc8162018-09-17 10:35:16 +02002106 struct lysp_ext_instance **exts)
2107{
2108 LY_ERR ret = 0;
2109 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002110 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002111 enum yang_keyword kw;
2112
2113 if (*flags & LYS_SET_REQINST) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002114 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002115 return LY_EVALID;
2116 }
2117 *flags |= LYS_SET_REQINST;
2118
2119 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002120 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002121 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002122
2123 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
2124 *reqinst = 1;
2125 } else if ((word_len != 5) || strncmp(word, "false", word_len)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002126 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002127 free(buf);
2128 return LY_EVALID;
2129 }
2130 free(buf);
2131
2132 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002133 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002134
2135 switch (kw) {
2136 case YANG_CUSTOM:
2137 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_REQINSTANCE, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02002138 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002139 break;
2140 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002141 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002142 return LY_EVALID;
2143 }
2144 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002145 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002146
2147 return ret;
2148}
2149
Michal Vaskoea5abea2018-09-18 13:10:54 +02002150/**
2151 * @brief Parse the modifier statement. Substatement of type pattern statement.
2152 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002153 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002154 * @param[in,out] data Data to read from, always moved to currently handled character.
2155 * @param[in,out] pat Value to write to.
2156 * @param[in,out] exts Extension instances to add to.
2157 *
2158 * @return LY_ERR values.
2159 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002160static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002161parse_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 +02002162{
2163 LY_ERR ret = 0;
2164 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002165 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002166 enum yang_keyword kw;
2167
2168 if ((*pat)[0] == 0x15) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002169 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002170 return LY_EVALID;
2171 }
2172
2173 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002174 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002175 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002176
2177 if ((word_len != 12) || strncmp(word, "invert-match", word_len)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002178 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002179 free(buf);
2180 return LY_EVALID;
2181 }
2182 free(buf);
2183
2184 /* replace the value in the dictionary */
2185 buf = malloc(strlen(*pat) + 1);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002186 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002187 strcpy(buf, *pat);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002188 lydict_remove(ctx->ctx, *pat);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002189
2190 assert(buf[0] == 0x06);
2191 buf[0] = 0x15;
Radek Krejci44ceedc2018-10-02 15:54:31 +02002192 *pat = lydict_insert_zc(ctx->ctx, buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002193
2194 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002195 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002196
2197 switch (kw) {
2198 case YANG_CUSTOM:
2199 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MODIFIER, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02002200 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002201 break;
2202 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002203 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002204 return LY_EVALID;
2205 }
2206 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002207 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002208
2209 return ret;
2210}
2211
Michal Vaskoea5abea2018-09-18 13:10:54 +02002212/**
2213 * @brief Parse the pattern statement. Substatement of type statement.
2214 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002215 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002216 * @param[in,out] data Data to read from, always moved to currently handled character.
2217 * @param[in,out] patterns Restrictions to add to.
2218 *
2219 * @return LY_ERR values.
2220 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002221static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002222parse_type_pattern(struct ly_parser_ctx *ctx, const char **data, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002223{
2224 LY_ERR ret = 0;
2225 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002226 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002227 enum yang_keyword kw;
2228 struct lysp_restr *restr;
2229
2230 LYSP_ARRAY_NEW_RET(ctx, patterns, restr, LY_EMEM);
2231
2232 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002233 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002234 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002235
2236 /* add special meaning first byte */
2237 if (buf) {
2238 buf = realloc(buf, word_len + 2);
2239 word = buf;
2240 } else {
2241 buf = malloc(word_len + 2);
2242 }
Radek Krejci44ceedc2018-10-02 15:54:31 +02002243 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002244 memmove(buf + 1, word, word_len + 1);
2245 word[0] = 0x06;
Radek Krejci44ceedc2018-10-02 15:54:31 +02002246 restr->arg = lydict_insert_zc(ctx->ctx, word);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002247
2248 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002249 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002250
2251 switch (kw) {
2252 case YANG_DESCRIPTION:
2253 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts);
2254 break;
2255 case YANG_REFERENCE:
2256 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts);
2257 break;
2258 case YANG_ERROR_APP_TAG:
2259 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRTAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts);
2260 break;
2261 case YANG_ERROR_MESSAGE:
2262 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRMSG, 0, &restr->emsg, Y_STR_ARG, &restr->exts);
2263 break;
2264 case YANG_MODIFIER:
2265 ret = parse_type_pattern_modifier(ctx, data, &restr->arg, &restr->exts);
2266 break;
2267 case YANG_CUSTOM:
2268 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts);
2269 break;
2270 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002271 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002272 return LY_EVALID;
2273 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002274 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002275 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002276 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002277
2278 return ret;
2279}
2280
Michal Vaskoea5abea2018-09-18 13:10:54 +02002281/**
2282 * @brief Parse the type statement.
2283 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002284 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002285 * @param[in,out] data Data to read from, always moved to currently handled character.
2286 * @param[in,out] type Type to wrote to.
2287 *
2288 * @return LY_ERR values.
2289 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002290static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002291parse_type(struct ly_parser_ctx *ctx, const char **data, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002292{
2293 LY_ERR ret = 0;
2294 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002295 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002296 enum yang_keyword kw;
2297 struct lysp_type *nest_type;
2298
2299 if (type->name) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002300 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002301 return LY_EVALID;
2302 }
2303
2304 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002305 ret = get_argument(ctx, data, Y_PREF_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002306 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002307
Radek Krejci44ceedc2018-10-02 15:54:31 +02002308 INSERT_WORD(ctx, buf, type->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002309 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002310 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002311
2312 switch (kw) {
2313 case YANG_BASE:
2314 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_BASE, &type->bases, Y_PREF_IDENTIF_ARG, &type->exts);
2315 break;
2316 case YANG_BIT:
2317 ret = parse_type_enum(ctx, data, kw, &type->bits);
2318 break;
2319 case YANG_ENUM:
2320 ret = parse_type_enum(ctx, data, kw, &type->enums);
2321 break;
2322 case YANG_FRACTION_DIGITS:
2323 ret = parse_type_fracdigits(ctx, data, &type->fraction_digits, &type->exts);
2324 break;
2325 case YANG_LENGTH:
2326 if (type->length) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002327 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002328 return LY_EVALID;
2329 }
2330 type->length = calloc(1, sizeof *type->length);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002331 LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002332
2333 ret = parse_restr(ctx, data, kw, type->length);
2334 break;
2335 case YANG_PATH:
2336 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_PATH, 0, &type->path, Y_STR_ARG, &type->exts);
2337 break;
2338 case YANG_PATTERN:
2339 ret = parse_type_pattern(ctx, data, &type->patterns);
2340 break;
2341 case YANG_RANGE:
2342 if (type->range) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002343 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002344 return LY_EVALID;
2345 }
2346 type->range = calloc(1, sizeof *type->range);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002347 LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx->ctx), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002348
2349 ret = parse_restr(ctx, data, kw, type->range);
2350 break;
2351 case YANG_REQUIRE_INSTANCE:
2352 ret = parse_type_reqinstance(ctx, data, &type->require_instance, &type->flags, &type->exts);
2353 break;
2354 case YANG_TYPE:
2355 {
2356 LYSP_ARRAY_NEW_RET(ctx, &type->types, nest_type, LY_EMEM);
2357 }
2358 ret = parse_type(ctx, data, nest_type);
2359 break;
2360 case YANG_CUSTOM:
2361 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &type->exts);
2362 break;
2363 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002364 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002365 return LY_EVALID;
2366 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002367 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002368 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002369 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002370
2371 return ret;
2372}
2373
Michal Vaskoea5abea2018-09-18 13:10:54 +02002374/**
2375 * @brief Parse the leaf statement.
2376 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002377 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002378 * @param[in,out] data Data to read from, always moved to currently handled character.
2379 * @param[in,out] siblings Siblings to add to.
2380 *
2381 * @return LY_ERR values.
2382 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002383static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002384parse_leaf(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002385{
2386 LY_ERR ret = 0;
2387 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002388 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002389 enum yang_keyword kw;
2390 struct lysp_node *iter;
2391 struct lysp_node_leaf *leaf;
2392
2393 /* create structure */
2394 leaf = calloc(1, sizeof *leaf);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002395 LY_CHECK_ERR_RET(!leaf, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002396 leaf->nodetype = LYS_LEAF;
2397
2398 /* insert into siblings */
2399 if (!*siblings) {
2400 *siblings = (struct lysp_node *)leaf;
2401 } else {
2402 for (iter = *siblings; iter->next; iter = iter->next);
2403 iter->next = (struct lysp_node *)leaf;
2404 }
2405
2406 /* get name */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002407 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002408 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002409
Radek Krejci44ceedc2018-10-02 15:54:31 +02002410 INSERT_WORD(ctx, buf, leaf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002411
2412 /* parse substatements */
2413 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002414 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002415
2416 switch (kw) {
2417 case YANG_CONFIG:
2418 ret = parse_config(ctx, data, &leaf->flags, &leaf->exts);
2419 break;
2420 case YANG_DEFAULT:
2421 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &leaf->dflt, Y_STR_ARG, &leaf->exts);
2422 break;
2423 case YANG_DESCRIPTION:
2424 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &leaf->dsc, Y_STR_ARG, &leaf->exts);
2425 break;
2426 case YANG_IF_FEATURE:
2427 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &leaf->iffeatures, Y_STR_ARG, &leaf->exts);
2428 break;
2429 case YANG_MANDATORY:
2430 ret = parse_mandatory(ctx, data, &leaf->flags, &leaf->exts);
2431 break;
2432 case YANG_MUST:
2433 ret = parse_restrs(ctx, data, kw, &leaf->musts);
2434 break;
2435 case YANG_REFERENCE:
2436 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &leaf->ref, Y_STR_ARG, &leaf->exts);
2437 break;
2438 case YANG_STATUS:
2439 ret = parse_status(ctx, data, &leaf->flags, &leaf->exts);
2440 break;
2441 case YANG_TYPE:
2442 ret = parse_type(ctx, data, &leaf->type);
2443 break;
2444 case YANG_UNITS:
2445 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, &leaf->units, Y_STR_ARG, &leaf->exts);
2446 break;
2447 case YANG_WHEN:
2448 ret = parse_when(ctx, data, &leaf->when);
2449 break;
2450 case YANG_CUSTOM:
2451 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &leaf->exts);
2452 break;
2453 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002454 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002455 return LY_EVALID;
2456 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002457 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002458 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002459 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002460
2461 /* mandatory substatements */
2462 if (!leaf->type.name) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002463 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002464 return LY_EVALID;
2465 }
2466
2467 return ret;
2468}
2469
Michal Vaskoea5abea2018-09-18 13:10:54 +02002470/**
2471 * @brief Parse the max-elements statement.
2472 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002473 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002474 * @param[in,out] data Data to read from, always moved to currently handled character.
2475 * @param[in,out] max Value to write to.
2476 * @param[in,out] flags Flags to write to.
2477 * @param[in,out] exts Extension instances to add to.
2478 *
2479 * @return LY_ERR values.
2480 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002481static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002482parse_maxelements(struct ly_parser_ctx *ctx, const char **data, uint32_t *max, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002483{
2484 LY_ERR ret = 0;
2485 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002486 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002487 unsigned long int num;
2488 enum yang_keyword kw;
2489
2490 if (*flags & LYS_SET_MAX) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002491 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002492 return LY_EVALID;
2493 }
2494 *flags |= LYS_SET_MAX;
2495
2496 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002497 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002498 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002499
2500 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002501 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002502 free(buf);
2503 return LY_EVALID;
2504 }
2505
2506 if (strncmp(word, "unbounded", word_len)) {
2507 errno = 0;
2508 num = strtoul(word, &ptr, 10);
2509 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002510 if ((size_t)(ptr - word) != word_len) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002511 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002512 free(buf);
2513 return LY_EVALID;
2514 }
2515 if ((errno == ERANGE) || (num > UINT32_MAX)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002516 LOGVAL_YANG(ctx, LY_VCODE_OOB, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002517 free(buf);
2518 return LY_EVALID;
2519 }
2520
2521 *max = num;
2522 }
2523 free(buf);
2524
2525 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002526 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002527
2528 switch (kw) {
2529 case YANG_CUSTOM:
2530 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MAX, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02002531 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002532 break;
2533 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002534 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002535 return LY_EVALID;
2536 }
2537 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002538 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002539
2540 return ret;
2541}
2542
Michal Vaskoea5abea2018-09-18 13:10:54 +02002543/**
2544 * @brief Parse the min-elements statement.
2545 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002546 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002547 * @param[in,out] data Data to read from, always moved to currently handled character.
2548 * @param[in,out] min Value to write to.
2549 * @param[in,out] flags Flags to write to.
2550 * @param[in,out] exts Extension instances to add to.
2551 *
2552 * @return LY_ERR values.
2553 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002554static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002555parse_minelements(struct ly_parser_ctx *ctx, const char **data, uint32_t *min, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002556{
2557 LY_ERR ret = 0;
2558 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002559 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002560 unsigned long int num;
2561 enum yang_keyword kw;
2562
2563 if (*flags & LYS_SET_MIN) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002564 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002565 return LY_EVALID;
2566 }
2567 *flags |= LYS_SET_MIN;
2568
2569 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002570 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002571 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002572
2573 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002574 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002575 free(buf);
2576 return LY_EVALID;
2577 }
2578
2579 errno = 0;
2580 num = strtoul(word, &ptr, 10);
2581 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002582 if ((size_t)(ptr - word) != word_len) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002583 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002584 free(buf);
2585 return LY_EVALID;
2586 }
2587 if ((errno == ERANGE) || (num > UINT32_MAX)) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002588 LOGVAL_YANG(ctx, LY_VCODE_OOB, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002589 free(buf);
2590 return LY_EVALID;
2591 }
2592 *min = num;
2593 free(buf);
2594
2595 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002596 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002597
2598 switch (kw) {
2599 case YANG_CUSTOM:
2600 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MIN, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02002601 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002602 break;
2603 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002604 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002605 return LY_EVALID;
2606 }
2607 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002608 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002609
2610 return ret;
2611}
2612
Michal Vaskoea5abea2018-09-18 13:10:54 +02002613/**
2614 * @brief Parse the ordered-by statement.
2615 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002616 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002617 * @param[in,out] data Data to read from, always moved to currently handled character.
2618 * @param[in,out] flags Flags to write to.
2619 * @param[in,out] exts Extension instances to add to.
2620 *
2621 * @return LY_ERR values.
2622 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002623static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002624parse_orderedby(struct ly_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002625{
2626 LY_ERR ret = 0;
2627 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002628 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002629 enum yang_keyword kw;
2630
2631 if (*flags & LYS_ORDBY_MASK) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002632 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002633 return LY_EVALID;
2634 }
2635
2636 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002637 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002638 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002639
2640 if ((word_len == 6) && !strncmp(word, "system", word_len)) {
2641 *flags |= LYS_ORDBY_SYSTEM;
2642 } else if ((word_len == 4) && !strncmp(word, "user", word_len)) {
2643 *flags |= LYS_ORDBY_USER;
2644 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002645 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002646 free(buf);
2647 return LY_EVALID;
2648 }
2649 free(buf);
2650
2651 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002652 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002653
2654 switch (kw) {
2655 case YANG_CUSTOM:
2656 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_ORDEREDBY, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02002657 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002658 break;
2659 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002660 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002661 return LY_EVALID;
2662 }
2663 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002664 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002665
2666 return ret;
2667}
2668
Michal Vaskoea5abea2018-09-18 13:10:54 +02002669/**
2670 * @brief Parse the leaf-list statement.
2671 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002672 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002673 * @param[in,out] data Data to read from, always moved to currently handled character.
2674 * @param[in,out] siblings Siblings to add to.
2675 *
2676 * @return LY_ERR values.
2677 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002678static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002679parse_leaflist(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002680{
2681 LY_ERR ret = 0;
2682 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002683 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002684 enum yang_keyword kw;
2685 struct lysp_node *iter;
2686 struct lysp_node_leaflist *llist;
2687
2688 /* create structure */
2689 llist = calloc(1, sizeof *llist);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002690 LY_CHECK_ERR_RET(!llist, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002691 llist->nodetype = LYS_LEAFLIST;
2692
2693 /* insert into siblings */
2694 if (!*siblings) {
2695 *siblings = (struct lysp_node *)llist;
2696 } else {
2697 for (iter = *siblings; iter->next; iter = iter->next);
2698 iter->next = (struct lysp_node *)llist;
2699 }
2700
2701 /* get name */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002702 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002703 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002704
Radek Krejci44ceedc2018-10-02 15:54:31 +02002705 INSERT_WORD(ctx, buf, llist->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002706
2707 /* parse substatements */
2708 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002709 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002710
2711 switch (kw) {
2712 case YANG_CONFIG:
2713 ret = parse_config(ctx, data, &llist->flags, &llist->exts);
2714 break;
2715 case YANG_DEFAULT:
2716 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_DEFAULT, &llist->dflts, Y_STR_ARG, &llist->exts);
2717 break;
2718 case YANG_DESCRIPTION:
2719 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &llist->dsc, Y_STR_ARG, &llist->exts);
2720 break;
2721 case YANG_IF_FEATURE:
2722 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &llist->iffeatures, Y_STR_ARG, &llist->exts);
2723 break;
2724 case YANG_MAX_ELEMENTS:
2725 ret = parse_maxelements(ctx, data, &llist->max, &llist->flags, &llist->exts);
2726 break;
2727 case YANG_MIN_ELEMENTS:
2728 ret = parse_minelements(ctx, data, &llist->min, &llist->flags, &llist->exts);
2729 break;
2730 case YANG_MUST:
2731 ret = parse_restrs(ctx, data, kw, &llist->musts);
2732 break;
2733 case YANG_ORDERED_BY:
2734 ret = parse_orderedby(ctx, data, &llist->flags, &llist->exts);
2735 break;
2736 case YANG_REFERENCE:
2737 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &llist->ref, Y_STR_ARG, &llist->exts);
2738 break;
2739 case YANG_STATUS:
2740 ret = parse_status(ctx, data, &llist->flags, &llist->exts);
2741 break;
2742 case YANG_TYPE:
2743 ret = parse_type(ctx, data, &llist->type);
2744 break;
2745 case YANG_UNITS:
2746 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, &llist->units, Y_STR_ARG, &llist->exts);
2747 break;
2748 case YANG_WHEN:
2749 ret = parse_when(ctx, data, &llist->when);
2750 break;
2751 case YANG_CUSTOM:
2752 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &llist->exts);
2753 break;
2754 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002755 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002756 return LY_EVALID;
2757 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002758 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002759 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002760 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002761
2762 /* mandatory substatements */
2763 if (!llist->type.name) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002764 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002765 return LY_EVALID;
2766 }
2767
2768 return ret;
2769}
2770
Michal Vaskoea5abea2018-09-18 13:10:54 +02002771/**
2772 * @brief Parse the refine statement.
2773 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002774 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002775 * @param[in,out] data Data to read from, always moved to currently handled character.
2776 * @param[in,out] refines Refines to add to.
2777 *
2778 * @return LY_ERR values.
2779 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002780static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002781parse_refine(struct ly_parser_ctx *ctx, const char **data, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002782{
2783 LY_ERR ret = 0;
2784 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002785 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002786 enum yang_keyword kw;
2787 struct lysp_refine *rf;
2788
2789 LYSP_ARRAY_NEW_RET(ctx, refines, rf, LY_EMEM);
2790
2791 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002792 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002793 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002794
Radek Krejci44ceedc2018-10-02 15:54:31 +02002795 INSERT_WORD(ctx, buf, rf->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002796 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002797 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002798
2799 switch (kw) {
2800 case YANG_CONFIG:
2801 ret = parse_config(ctx, data, &rf->flags, &rf->exts);
2802 break;
2803 case YANG_DEFAULT:
2804 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_DEFAULT, &rf->dflts, Y_STR_ARG, &rf->exts);
2805 break;
2806 case YANG_DESCRIPTION:
2807 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &rf->dsc, Y_STR_ARG, &rf->exts);
2808 break;
2809 case YANG_IF_FEATURE:
2810 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &rf->iffeatures, Y_STR_ARG, &rf->exts);
2811 break;
2812 case YANG_MAX_ELEMENTS:
2813 ret = parse_maxelements(ctx, data, &rf->max, &rf->flags, &rf->exts);
2814 break;
2815 case YANG_MIN_ELEMENTS:
2816 ret = parse_minelements(ctx, data, &rf->min, &rf->flags, &rf->exts);
2817 break;
2818 case YANG_MUST:
2819 ret = parse_restrs(ctx, data, kw, &rf->musts);
2820 break;
2821 case YANG_MANDATORY:
2822 ret = parse_mandatory(ctx, data, &rf->flags, &rf->exts);
2823 break;
2824 case YANG_REFERENCE:
2825 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &rf->ref, Y_STR_ARG, &rf->exts);
2826 break;
2827 case YANG_PRESENCE:
2828 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_PRESENCE, 0, &rf->presence, Y_STR_ARG, &rf->exts);
2829 break;
2830 case YANG_CUSTOM:
2831 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rf->exts);
2832 break;
2833 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002834 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002835 return LY_EVALID;
2836 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002837 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002838 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002839 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002840
2841 return ret;
2842}
2843
Michal Vaskoea5abea2018-09-18 13:10:54 +02002844/**
2845 * @brief Parse the typedef statement.
2846 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002847 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002848 * @param[in,out] data Data to read from, always moved to currently handled character.
2849 * @param[in,out] typedefs Typedefs to add to.
2850 *
2851 * @return LY_ERR values.
2852 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002853static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002854parse_typedef(struct ly_parser_ctx *ctx, const char **data, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002855{
2856 LY_ERR ret = 0;
2857 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002858 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002859 enum yang_keyword kw;
2860 struct lysp_tpdf *tpdf;
2861
2862 LYSP_ARRAY_NEW_RET(ctx, typedefs, tpdf, LY_EMEM);
2863
2864 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02002865 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002866 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002867
Radek Krejci44ceedc2018-10-02 15:54:31 +02002868 INSERT_WORD(ctx, buf, tpdf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002869
2870 /* parse substatements */
2871 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002872 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002873
2874 switch (kw) {
2875 case YANG_DEFAULT:
2876 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &tpdf->dflt, Y_STR_ARG, &tpdf->exts);
2877 break;
2878 case YANG_DESCRIPTION:
2879 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &tpdf->dsc, Y_STR_ARG, &tpdf->exts);
2880 break;
2881 case YANG_REFERENCE:
2882 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &tpdf->ref, Y_STR_ARG, &tpdf->exts);
2883 break;
2884 case YANG_STATUS:
2885 ret = parse_status(ctx, data, &tpdf->flags, &tpdf->exts);
2886 break;
2887 case YANG_TYPE:
2888 ret = parse_type(ctx, data, &tpdf->type);
2889 break;
2890 case YANG_UNITS:
2891 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, &tpdf->units, Y_STR_ARG, &tpdf->exts);
2892 break;
2893 case YANG_CUSTOM:
2894 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &tpdf->exts);
2895 break;
2896 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002897 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002898 return LY_EVALID;
2899 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002900 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002901 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002902 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002903
2904 /* mandatory substatements */
2905 if (!tpdf->type.name) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002906 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002907 return LY_EVALID;
2908 }
2909
2910 return ret;
2911}
2912
Michal Vaskoea5abea2018-09-18 13:10:54 +02002913/**
2914 * @brief Parse the input or output statement.
2915 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002916 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002917 * @param[in,out] data Data to read from, always moved to currently handled character.
2918 * @param[in] kw Type of this particular keyword
2919 * @param[in,out] inout_p Input/output pointer to write to.
2920 *
2921 * @return LY_ERR values.
2922 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002923static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02002924parse_inout(struct ly_parser_ctx *ctx, const char **data, enum yang_keyword kw, struct lysp_action_inout **inout_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002925{
2926 LY_ERR ret = 0;
2927 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002928 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002929 struct lysp_action_inout *inout;
2930
2931 if (*inout_p) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002932 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002933 return LY_EVALID;
2934 }
2935
2936 /* create structure */
2937 inout = calloc(1, sizeof *inout);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002938 LY_CHECK_ERR_RET(!inout, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002939 *inout_p = inout;
2940
2941 /* parse substatements */
2942 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002943 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002944
2945 switch (kw) {
2946 case YANG_ANYDATA:
2947 case YANG_ANYXML:
2948 ret = parse_any(ctx, data, kw, &inout->data);
2949 break;
2950 case YANG_CHOICE:
2951 ret = parse_choice(ctx, data, &inout->data);
2952 break;
2953 case YANG_CONTAINER:
2954 ret = parse_container(ctx, data, &inout->data);
2955 break;
2956 case YANG_LEAF:
2957 ret = parse_leaf(ctx, data, &inout->data);
2958 break;
2959 case YANG_LEAF_LIST:
2960 ret = parse_leaflist(ctx, data, &inout->data);
2961 break;
2962 case YANG_LIST:
2963 ret = parse_list(ctx, data, &inout->data);
2964 break;
2965 case YANG_USES:
2966 ret = parse_uses(ctx, data, &inout->data);
2967 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002968 case YANG_TYPEDEF:
2969 ret = parse_typedef(ctx, data, &inout->typedefs);
2970 break;
2971 case YANG_MUST:
2972 ret = parse_restrs(ctx, data, kw, &inout->musts);
2973 break;
2974 case YANG_GROUPING:
2975 ret = parse_grouping(ctx, data, &inout->groupings);
2976 break;
2977 case YANG_CUSTOM:
2978 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inout->exts);
2979 break;
2980 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02002981 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "input/output");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002982 return LY_EVALID;
2983 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002984 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002985 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002986 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002987
2988 return ret;
2989}
2990
Michal Vaskoea5abea2018-09-18 13:10:54 +02002991/**
2992 * @brief Parse the action statement.
2993 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002994 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002995 * @param[in,out] data Data to read from, always moved to currently handled character.
2996 * @param[in,out] actions Actions to add to.
2997 *
2998 * @return LY_ERR values.
2999 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003000static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003001parse_action(struct ly_parser_ctx *ctx, const char **data, struct lysp_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003002{
3003 LY_ERR ret = 0;
3004 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003005 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003006 enum yang_keyword kw;
3007 struct lysp_action *act;
3008
3009 LYSP_ARRAY_NEW_RET(ctx, actions, act, LY_EMEM);
3010
3011 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003012 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003013 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003014
Radek Krejci44ceedc2018-10-02 15:54:31 +02003015 INSERT_WORD(ctx, buf, act->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003016 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003017 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003018
3019 switch (kw) {
3020 case YANG_DESCRIPTION:
3021 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &act->dsc, Y_STR_ARG, &act->exts);
3022 break;
3023 case YANG_IF_FEATURE:
3024 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &act->iffeatures, Y_STR_ARG, &act->exts);
3025 break;
3026 case YANG_REFERENCE:
3027 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &act->ref, Y_STR_ARG, &act->exts);
3028 break;
3029 case YANG_STATUS:
3030 ret = parse_status(ctx, data, &act->flags, &act->exts);
3031 break;
3032
3033 case YANG_INPUT:
3034 ret = parse_inout(ctx, data, kw, &act->input);
3035 break;
3036 case YANG_OUTPUT:
3037 ret = parse_inout(ctx, data, kw, &act->output);
3038 break;
3039
3040 case YANG_TYPEDEF:
3041 ret = parse_typedef(ctx, data, &act->typedefs);
3042 break;
3043 case YANG_GROUPING:
3044 ret = parse_grouping(ctx, data, &act->groupings);
3045 break;
3046 case YANG_CUSTOM:
3047 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &act->exts);
3048 break;
3049 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003050 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "action");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003051 return LY_EVALID;
3052 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003053 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003054 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003055 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003056
3057 return ret;
3058}
3059
Michal Vaskoea5abea2018-09-18 13:10:54 +02003060/**
3061 * @brief Parse the notification statement.
3062 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003063 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003064 * @param[in,out] data Data to read from, always moved to currently handled character.
3065 * @param[in,out] notifs Notifications to add to.
3066 *
3067 * @return LY_ERR values.
3068 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003069static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003070parse_notif(struct ly_parser_ctx *ctx, const char **data, struct lysp_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003071{
3072 LY_ERR ret = 0;
3073 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003074 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003075 enum yang_keyword kw;
3076 struct lysp_notif *notif;
3077
3078 LYSP_ARRAY_NEW_RET(ctx, notifs, notif, LY_EMEM);
3079
3080 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003081 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003082 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003083
Radek Krejci44ceedc2018-10-02 15:54:31 +02003084 INSERT_WORD(ctx, buf, notif->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003085 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003086 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003087
3088 switch (kw) {
3089 case YANG_DESCRIPTION:
3090 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &notif->dsc, Y_STR_ARG, &notif->exts);
3091 break;
3092 case YANG_IF_FEATURE:
3093 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &notif->iffeatures, Y_STR_ARG, &notif->exts);
3094 break;
3095 case YANG_REFERENCE:
3096 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &notif->ref, Y_STR_ARG, &notif->exts);
3097 break;
3098 case YANG_STATUS:
3099 ret = parse_status(ctx, data, &notif->flags, &notif->exts);
3100 break;
3101
3102 case YANG_ANYDATA:
3103 case YANG_ANYXML:
3104 ret = parse_any(ctx, data, kw, &notif->data);
3105 break;
3106 case YANG_CHOICE:
3107 ret = parse_case(ctx, data, &notif->data);
3108 break;
3109 case YANG_CONTAINER:
3110 ret = parse_container(ctx, data, &notif->data);
3111 break;
3112 case YANG_LEAF:
3113 ret = parse_leaf(ctx, data, &notif->data);
3114 break;
3115 case YANG_LEAF_LIST:
3116 ret = parse_leaflist(ctx, data, &notif->data);
3117 break;
3118 case YANG_LIST:
3119 ret = parse_list(ctx, data, &notif->data);
3120 break;
3121 case YANG_USES:
3122 ret = parse_uses(ctx, data, &notif->data);
3123 break;
3124
3125 case YANG_MUST:
3126 ret = parse_restrs(ctx, data, kw, &notif->musts);
3127 break;
3128 case YANG_TYPEDEF:
3129 ret = parse_typedef(ctx, data, &notif->typedefs);
3130 break;
3131 case YANG_GROUPING:
3132 ret = parse_grouping(ctx, data, &notif->groupings);
3133 break;
3134 case YANG_CUSTOM:
3135 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &notif->exts);
3136 break;
3137 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003138 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003139 return LY_EVALID;
3140 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003141 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003142 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003143 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003144
3145 return ret;
3146}
3147
Michal Vaskoea5abea2018-09-18 13:10:54 +02003148/**
3149 * @brief Parse the grouping statement.
3150 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003151 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003152 * @param[in,out] data Data to read from, always moved to currently handled character.
3153 * @param[in,out] groupings Groupings to add to.
3154 *
3155 * @return LY_ERR values.
3156 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003157static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003158parse_grouping(struct ly_parser_ctx *ctx, const char **data, struct lysp_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003159{
3160 LY_ERR ret = 0;
3161 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003162 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003163 enum yang_keyword kw;
3164 struct lysp_grp *grp;
3165
3166 LYSP_ARRAY_NEW_RET(ctx, groupings, grp, LY_EMEM);
3167
3168 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003169 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003170 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003171
Radek Krejci44ceedc2018-10-02 15:54:31 +02003172 INSERT_WORD(ctx, buf, grp->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003173
3174 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003175 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003176
3177 switch (kw) {
3178 case YANG_DESCRIPTION:
3179 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &grp->dsc, Y_STR_ARG, &grp->exts);
3180 break;
3181 case YANG_REFERENCE:
3182 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &grp->ref, Y_STR_ARG, &grp->exts);
3183 break;
3184 case YANG_STATUS:
3185 ret = parse_status(ctx, data, &grp->flags, &grp->exts);
3186 break;
3187
3188 case YANG_ANYDATA:
3189 case YANG_ANYXML:
3190 ret = parse_any(ctx, data, kw, &grp->data);
3191 break;
3192 case YANG_CHOICE:
3193 ret = parse_choice(ctx, data, &grp->data);
3194 break;
3195 case YANG_CONTAINER:
3196 ret = parse_container(ctx, data, &grp->data);
3197 break;
3198 case YANG_LEAF:
3199 ret = parse_leaf(ctx, data, &grp->data);
3200 break;
3201 case YANG_LEAF_LIST:
3202 ret = parse_leaflist(ctx, data, &grp->data);
3203 break;
3204 case YANG_LIST:
3205 ret = parse_list(ctx, data, &grp->data);
3206 break;
3207 case YANG_USES:
3208 ret = parse_uses(ctx, data, &grp->data);
3209 break;
3210
3211 case YANG_TYPEDEF:
3212 ret = parse_typedef(ctx, data, &grp->typedefs);
3213 break;
3214 case YANG_ACTION:
3215 ret = parse_action(ctx, data, &grp->actions);
3216 break;
3217 case YANG_GROUPING:
3218 ret = parse_grouping(ctx, data, &grp->groupings);
3219 break;
3220 case YANG_NOTIFICATION:
3221 ret = parse_notif(ctx, data, &grp->notifs);
3222 break;
3223 case YANG_CUSTOM:
3224 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &grp->exts);
3225 break;
3226 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003227 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003228 return LY_EVALID;
3229 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003230 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003231 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003232 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003233
3234 return ret;
3235}
3236
Michal Vaskoea5abea2018-09-18 13:10:54 +02003237/**
3238 * @brief Parse the refine statement.
3239 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003240 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003241 * @param[in,out] data Data to read from, always moved to currently handled character.
3242 * @param[in,out] augments Augments to add to.
3243 *
3244 * @return LY_ERR values.
3245 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003246static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003247parse_augment(struct ly_parser_ctx *ctx, const char **data, struct lysp_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003248{
3249 LY_ERR ret = 0;
3250 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003251 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003252 enum yang_keyword kw;
3253 struct lysp_augment *aug;
3254
3255 LYSP_ARRAY_NEW_RET(ctx, augments, aug, LY_EMEM);
3256
3257 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003258 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003259 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003260
Radek Krejci44ceedc2018-10-02 15:54:31 +02003261 INSERT_WORD(ctx, buf, aug->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003262 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003263 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003264
3265 switch (kw) {
3266 case YANG_DESCRIPTION:
3267 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &aug->dsc, Y_STR_ARG, &aug->exts);
3268 break;
3269 case YANG_IF_FEATURE:
3270 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &aug->iffeatures, Y_STR_ARG, &aug->exts);
3271 break;
3272 case YANG_REFERENCE:
3273 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &aug->ref, Y_STR_ARG, &aug->exts);
3274 break;
3275 case YANG_STATUS:
3276 ret = parse_status(ctx, data, &aug->flags, &aug->exts);
3277 break;
3278 case YANG_WHEN:
3279 ret = parse_when(ctx, data, &aug->when);
3280 break;
3281
3282 case YANG_ANYDATA:
3283 case YANG_ANYXML:
3284 ret = parse_any(ctx, data, kw, &aug->child);
3285 break;
3286 case YANG_CASE:
3287 ret = parse_case(ctx, data, &aug->child);
3288 break;
3289 case YANG_CHOICE:
3290 ret = parse_choice(ctx, data, &aug->child);
3291 break;
3292 case YANG_CONTAINER:
3293 ret = parse_container(ctx, data, &aug->child);
3294 break;
3295 case YANG_LEAF:
3296 ret = parse_leaf(ctx, data, &aug->child);
3297 break;
3298 case YANG_LEAF_LIST:
3299 ret = parse_leaflist(ctx, data, &aug->child);
3300 break;
3301 case YANG_LIST:
3302 ret = parse_list(ctx, data, &aug->child);
3303 break;
3304 case YANG_USES:
3305 ret = parse_uses(ctx, data, &aug->child);
3306 break;
3307
3308 case YANG_ACTION:
3309 ret = parse_action(ctx, data, &aug->actions);
3310 break;
3311 case YANG_NOTIFICATION:
3312 ret = parse_notif(ctx, data, &aug->notifs);
3313 break;
3314 case YANG_CUSTOM:
3315 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &aug->exts);
3316 break;
3317 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003318 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003319 return LY_EVALID;
3320 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003321 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003322 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003323 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003324
3325 return ret;
3326}
3327
Michal Vaskoea5abea2018-09-18 13:10:54 +02003328/**
3329 * @brief Parse the uses statement.
3330 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003331 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003332 * @param[in,out] data Data to read from, always moved to currently handled character.
3333 * @param[in,out] siblings Siblings to add to.
3334 *
3335 * @return LY_ERR values.
3336 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003337static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003338parse_uses(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003339{
3340 LY_ERR ret = 0;
3341 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003342 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003343 enum yang_keyword kw;
3344 struct lysp_node *iter;
3345 struct lysp_node_uses *uses;
3346
3347 /* create structure */
3348 uses = calloc(1, sizeof *uses);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003349 LY_CHECK_ERR_RET(!uses, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003350 uses->nodetype = LYS_USES;
3351
3352 /* insert into siblings */
3353 if (!*siblings) {
3354 *siblings = (struct lysp_node *)uses;
3355 } else {
3356 for (iter = *siblings; iter->next; iter = iter->next);
3357 iter->next = (struct lysp_node *)uses;
3358 }
3359
3360 /* get name */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003361 ret = get_argument(ctx, data, Y_PREF_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003362 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003363
Radek Krejci44ceedc2018-10-02 15:54:31 +02003364 INSERT_WORD(ctx, buf, uses->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003365
3366 /* parse substatements */
3367 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003368 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003369
3370 switch (kw) {
3371 case YANG_DESCRIPTION:
3372 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &uses->dsc, Y_STR_ARG, &uses->exts);
3373 break;
3374 case YANG_IF_FEATURE:
3375 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &uses->iffeatures, Y_STR_ARG, &uses->exts);
3376 break;
3377 case YANG_REFERENCE:
3378 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &uses->ref, Y_STR_ARG, &uses->exts);
3379 break;
3380 case YANG_STATUS:
3381 ret = parse_status(ctx, data, &uses->flags, &uses->exts);
3382 break;
3383 case YANG_WHEN:
3384 ret = parse_when(ctx, data, &uses->when);
3385 break;
3386
3387 case YANG_REFINE:
3388 ret = parse_refine(ctx, data, &uses->refines);
3389 break;
3390 case YANG_AUGMENT:
3391 ret = parse_augment(ctx, data, &uses->augments);
3392 break;
3393 case YANG_CUSTOM:
3394 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &uses->exts);
3395 break;
3396 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003397 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003398 return LY_EVALID;
3399 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003400 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003401 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003402 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003403
3404 return ret;
3405}
3406
Michal Vaskoea5abea2018-09-18 13:10:54 +02003407/**
3408 * @brief Parse the case statement.
3409 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003410 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003411 * @param[in,out] data Data to read from, always moved to currently handled character.
3412 * @param[in,out] siblings Siblings to add to.
3413 *
3414 * @return LY_ERR values.
3415 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003416static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003417parse_case(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003418{
3419 LY_ERR ret = 0;
3420 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003421 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003422 enum yang_keyword kw;
3423 struct lysp_node *iter;
3424 struct lysp_node_case *cas;
3425
3426 /* create structure */
3427 cas = calloc(1, sizeof *cas);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003428 LY_CHECK_ERR_RET(!cas, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003429 cas->nodetype = LYS_CASE;
3430
3431 /* insert into siblings */
3432 if (!*siblings) {
3433 *siblings = (struct lysp_node *)cas;
3434 } else {
3435 for (iter = *siblings; iter->next; iter = iter->next);
3436 iter->next = (struct lysp_node *)cas;
3437 }
3438
3439 /* get name */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003440 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003441 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003442
Radek Krejci44ceedc2018-10-02 15:54:31 +02003443 INSERT_WORD(ctx, buf, cas->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003444
3445 /* parse substatements */
3446 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003447 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003448
3449 switch (kw) {
3450 case YANG_DESCRIPTION:
3451 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &cas->dsc, Y_STR_ARG, &cas->exts);
3452 break;
3453 case YANG_IF_FEATURE:
3454 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &cas->iffeatures, Y_STR_ARG, &cas->exts);
3455 break;
3456 case YANG_REFERENCE:
3457 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &cas->ref, Y_STR_ARG, &cas->exts);
3458 break;
3459 case YANG_STATUS:
3460 ret = parse_status(ctx, data, &cas->flags, &cas->exts);
3461 break;
3462 case YANG_WHEN:
3463 ret = parse_when(ctx, data, &cas->when);
3464 break;
3465
3466 case YANG_ANYDATA:
3467 case YANG_ANYXML:
3468 ret = parse_any(ctx, data, kw, &cas->child);
3469 break;
3470 case YANG_CHOICE:
3471 ret = parse_case(ctx, data, &cas->child);
3472 break;
3473 case YANG_CONTAINER:
3474 ret = parse_container(ctx, data, &cas->child);
3475 break;
3476 case YANG_LEAF:
3477 ret = parse_leaf(ctx, data, &cas->child);
3478 break;
3479 case YANG_LEAF_LIST:
3480 ret = parse_leaflist(ctx, data, &cas->child);
3481 break;
3482 case YANG_LIST:
3483 ret = parse_list(ctx, data, &cas->child);
3484 break;
3485 case YANG_USES:
3486 ret = parse_uses(ctx, data, &cas->child);
3487 break;
3488 case YANG_CUSTOM:
3489 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cas->exts);
3490 break;
3491 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003492 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003493 return LY_EVALID;
3494 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003495 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003496 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003497 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003498
3499 return ret;
3500}
3501
Michal Vaskoea5abea2018-09-18 13:10:54 +02003502/**
3503 * @brief Parse the choice statement.
3504 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003505 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003506 * @param[in,out] data Data to read from, always moved to currently handled character.
3507 * @param[in,out] siblings Siblings to add to.
3508 *
3509 * @return LY_ERR values.
3510 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003511static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003512parse_choice(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003513{
3514 LY_ERR ret = 0;
3515 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003516 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003517 enum yang_keyword kw;
3518 struct lysp_node *iter;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003519 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003520
3521 /* create structure */
Radek Krejci44ceedc2018-10-02 15:54:31 +02003522 choice = calloc(1, sizeof *choice);
3523 LY_CHECK_ERR_RET(!choice, LOGMEM(ctx->ctx), LY_EMEM);
3524 choice->nodetype = LYS_CHOICE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003525
3526 /* insert into siblings */
3527 if (!*siblings) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02003528 *siblings = (struct lysp_node *)choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003529 } else {
3530 for (iter = *siblings; iter->next; iter = iter->next);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003531 iter->next = (struct lysp_node *)choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003532 }
3533
3534 /* get name */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003535 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003536 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003537
Radek Krejci44ceedc2018-10-02 15:54:31 +02003538 INSERT_WORD(ctx, buf, choice->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003539
3540 /* parse substatements */
3541 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003542 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003543
3544 switch (kw) {
3545 case YANG_CONFIG:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003546 ret = parse_config(ctx, data, &choice->flags, &choice->exts);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003547 break;
3548 case YANG_DESCRIPTION:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003549 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &choice->dsc, Y_STR_ARG, &choice->exts);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003550 break;
3551 case YANG_IF_FEATURE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003552 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &choice->iffeatures, Y_STR_ARG, &choice->exts);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003553 break;
3554 case YANG_MANDATORY:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003555 ret = parse_mandatory(ctx, data, &choice->flags, &choice->exts);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003556 break;
3557 case YANG_REFERENCE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003558 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &choice->ref, Y_STR_ARG, &choice->exts);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003559 break;
3560 case YANG_STATUS:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003561 ret = parse_status(ctx, data, &choice->flags, &choice->exts);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003562 break;
3563 case YANG_WHEN:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003564 ret = parse_when(ctx, data, &choice->when);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003565 break;
3566 case YANG_DEFAULT:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003567 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &choice->dflt, Y_IDENTIF_ARG, &choice->exts);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003568 break;
3569
3570 case YANG_ANYDATA:
3571 case YANG_ANYXML:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003572 ret = parse_any(ctx, data, kw, &choice->child);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003573 break;
3574 case YANG_CASE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003575 ret = parse_case(ctx, data, &choice->child);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003576 break;
3577 case YANG_CHOICE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003578 ret = parse_choice(ctx, data, &choice->child);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003579 break;
3580 case YANG_CONTAINER:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003581 ret = parse_container(ctx, data, &choice->child);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003582 break;
3583 case YANG_LEAF:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003584 ret = parse_leaf(ctx, data, &choice->child);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003585 break;
3586 case YANG_LEAF_LIST:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003587 ret = parse_leaflist(ctx, data, &choice->child);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003588 break;
3589 case YANG_LIST:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003590 ret = parse_list(ctx, data, &choice->child);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003591 break;
3592 case YANG_CUSTOM:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003593 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &choice->exts);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003594 break;
3595 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003596 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003597 return LY_EVALID;
3598 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003599 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003600 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003601 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003602
3603 return ret;
3604}
3605
Michal Vaskoea5abea2018-09-18 13:10:54 +02003606/**
3607 * @brief Parse the container statement.
3608 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003609 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003610 * @param[in,out] data Data to read from, always moved to currently handled character.
3611 * @param[in,out] siblings Siblings to add to.
3612 *
3613 * @return LY_ERR values.
3614 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003615static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003616parse_container(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003617{
3618 LY_ERR ret = 0;
3619 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003620 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003621 enum yang_keyword kw;
3622 struct lysp_node *iter;
3623 struct lysp_node_container *cont;
3624
3625 /* create structure */
3626 cont = calloc(1, sizeof *cont);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003627 LY_CHECK_ERR_RET(!cont, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003628 cont->nodetype = LYS_CONTAINER;
3629
3630 /* insert into siblings */
3631 if (!*siblings) {
3632 *siblings = (struct lysp_node *)cont;
3633 } else {
3634 for (iter = *siblings; iter->next; iter = iter->next);
3635 iter->next = (struct lysp_node *)cont;
3636 }
3637
3638 /* get name */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003639 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003640 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003641
Radek Krejci44ceedc2018-10-02 15:54:31 +02003642 INSERT_WORD(ctx, buf, cont->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003643
3644 /* parse substatements */
3645 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003646 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003647
3648 switch (kw) {
3649 case YANG_CONFIG:
3650 ret = parse_config(ctx, data, &cont->flags, &cont->exts);
3651 break;
3652 case YANG_DESCRIPTION:
3653 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &cont->dsc, Y_STR_ARG, &cont->exts);
3654 break;
3655 case YANG_IF_FEATURE:
3656 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &cont->iffeatures, Y_STR_ARG, &cont->exts);
3657 break;
3658 case YANG_REFERENCE:
3659 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &cont->ref, Y_STR_ARG, &cont->exts);
3660 break;
3661 case YANG_STATUS:
3662 ret = parse_status(ctx, data, &cont->flags, &cont->exts);
3663 break;
3664 case YANG_WHEN:
3665 ret = parse_when(ctx, data, &cont->when);
3666 break;
3667 case YANG_PRESENCE:
3668 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_PRESENCE, 0, &cont->presence, Y_STR_ARG, &cont->exts);
3669 break;
3670
3671 case YANG_ANYDATA:
3672 case YANG_ANYXML:
3673 ret = parse_any(ctx, data, kw, &cont->child);
3674 break;
3675 case YANG_CHOICE:
3676 ret = parse_choice(ctx, data, &cont->child);
3677 break;
3678 case YANG_CONTAINER:
3679 ret = parse_container(ctx, data, &cont->child);
3680 break;
3681 case YANG_LEAF:
3682 ret = parse_leaf(ctx, data, &cont->child);
3683 break;
3684 case YANG_LEAF_LIST:
3685 ret = parse_leaflist(ctx, data, &cont->child);
3686 break;
3687 case YANG_LIST:
3688 ret = parse_list(ctx, data, &cont->child);
3689 break;
3690 case YANG_USES:
3691 ret = parse_uses(ctx, data, &cont->child);
3692 break;
3693
3694 case YANG_TYPEDEF:
3695 ret = parse_typedef(ctx, data, &cont->typedefs);
3696 break;
3697 case YANG_MUST:
3698 ret = parse_restrs(ctx, data, kw, &cont->musts);
3699 break;
3700 case YANG_ACTION:
3701 ret = parse_action(ctx, data, &cont->actions);
3702 break;
3703 case YANG_GROUPING:
3704 ret = parse_grouping(ctx, data, &cont->groupings);
3705 break;
3706 case YANG_NOTIFICATION:
3707 ret = parse_notif(ctx, data, &cont->notifs);
3708 break;
3709 case YANG_CUSTOM:
3710 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cont->exts);
3711 break;
3712 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003713 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003714 return LY_EVALID;
3715 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003716 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003717 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003718 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003719
3720 return ret;
3721}
3722
Michal Vaskoea5abea2018-09-18 13:10:54 +02003723/**
3724 * @brief Parse the list statement.
3725 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003726 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003727 * @param[in,out] data Data to read from, always moved to currently handled character.
3728 * @param[in,out] siblings Siblings to add to.
3729 *
3730 * @return LY_ERR values.
3731 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003732static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003733parse_list(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003734{
3735 LY_ERR ret = 0;
3736 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003737 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003738 enum yang_keyword kw;
3739 struct lysp_node *iter;
3740 struct lysp_node_list *list;
3741
3742 /* create structure */
3743 list = calloc(1, sizeof *list);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003744 LY_CHECK_ERR_RET(!list, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003745 list->nodetype = LYS_LIST;
3746
3747 /* insert into siblings */
3748 if (!*siblings) {
3749 *siblings = (struct lysp_node *)list;
3750 } else {
3751 for (iter = *siblings; iter->next; iter = iter->next);
3752 iter->next = (struct lysp_node *)list;
3753 }
3754
3755 /* get name */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003756 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003757 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003758
Radek Krejci44ceedc2018-10-02 15:54:31 +02003759 INSERT_WORD(ctx, buf, list->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003760
3761 /* parse substatements */
3762 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003763 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003764
3765 switch (kw) {
3766 case YANG_CONFIG:
3767 ret = parse_config(ctx, data, &list->flags, &list->exts);
3768 break;
3769 case YANG_DESCRIPTION:
3770 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &list->dsc, Y_STR_ARG, &list->exts);
3771 break;
3772 case YANG_IF_FEATURE:
3773 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &list->iffeatures, Y_STR_ARG, &list->exts);
3774 break;
3775 case YANG_REFERENCE:
3776 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &list->ref, Y_STR_ARG, &list->exts);
3777 break;
3778 case YANG_STATUS:
3779 ret = parse_status(ctx, data, &list->flags, &list->exts);
3780 break;
3781 case YANG_WHEN:
3782 ret = parse_when(ctx, data, &list->when);
3783 break;
3784 case YANG_KEY:
3785 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_KEY, 0, &list->key, Y_STR_ARG, &list->exts);
3786 break;
3787 case YANG_MAX_ELEMENTS:
3788 ret = parse_maxelements(ctx, data, &list->max, &list->flags, &list->exts);
3789 break;
3790 case YANG_MIN_ELEMENTS:
3791 ret = parse_minelements(ctx, data, &list->min, &list->flags, &list->exts);
3792 break;
3793 case YANG_ORDERED_BY:
3794 ret = parse_orderedby(ctx, data, &list->flags, &list->exts);
3795 break;
3796 case YANG_UNIQUE:
3797 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_UNIQUE, &list->uniques, Y_STR_ARG, &list->exts);
3798 break;
3799
3800 case YANG_ANYDATA:
3801 case YANG_ANYXML:
3802 ret = parse_any(ctx, data, kw, &list->child);
3803 break;
3804 case YANG_CHOICE:
3805 ret = parse_choice(ctx, data, &list->child);
3806 break;
3807 case YANG_CONTAINER:
3808 ret = parse_container(ctx, data, &list->child);
3809 break;
3810 case YANG_LEAF:
3811 ret = parse_leaf(ctx, data, &list->child);
3812 break;
3813 case YANG_LEAF_LIST:
3814 ret = parse_leaflist(ctx, data, &list->child);
3815 break;
3816 case YANG_LIST:
3817 ret = parse_list(ctx, data, &list->child);
3818 break;
3819 case YANG_USES:
3820 ret = parse_uses(ctx, data, &list->child);
3821 break;
3822
3823 case YANG_TYPEDEF:
3824 ret = parse_typedef(ctx, data, &list->typedefs);
3825 break;
3826 case YANG_MUST:
3827 ret = parse_restrs(ctx, data, kw, &list->musts);
3828 break;
3829 case YANG_ACTION:
3830 ret = parse_action(ctx, data, &list->actions);
3831 break;
3832 case YANG_GROUPING:
3833 ret = parse_grouping(ctx, data, &list->groupings);
3834 break;
3835 case YANG_NOTIFICATION:
3836 ret = parse_notif(ctx, data, &list->notifs);
3837 break;
3838 case YANG_CUSTOM:
3839 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &list->exts);
3840 break;
3841 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003842 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003843 return LY_EVALID;
3844 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003845 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003846 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003847 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003848
3849 return ret;
3850}
3851
Michal Vaskoea5abea2018-09-18 13:10:54 +02003852/**
3853 * @brief Parse the yin-element statement.
3854 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003855 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003856 * @param[in,out] data Data to read from, always moved to currently handled character.
3857 * @param[in,out] flags Flags to write to.
3858 * @param[in,out] exts Extension instances to add to.
3859 *
3860 * @return LY_ERR values.
3861 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003862static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003863parse_yinelement(struct ly_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003864{
3865 LY_ERR ret = 0;
3866 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003867 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003868 enum yang_keyword kw;
3869
3870 if (*flags & LYS_YINELEM_MASK) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02003871 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003872 return LY_EVALID;
3873 }
3874
3875 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003876 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003877 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003878
3879 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
3880 *flags |= LYS_YINELEM_TRUE;
3881 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
3882 *flags |= LYS_YINELEM_FALSE;
3883 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02003884 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003885 free(buf);
3886 return LY_EVALID;
3887 }
3888 free(buf);
3889
3890 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003891 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003892
3893 switch (kw) {
3894 case YANG_CUSTOM:
3895 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_YINELEM, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02003896 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003897 break;
3898 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003899 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003900 return LY_EVALID;
3901 }
3902 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003903 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003904
3905 return ret;
3906}
3907
Michal Vaskoea5abea2018-09-18 13:10:54 +02003908/**
3909 * @brief Parse the yin-element statement.
3910 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003911 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003912 * @param[in,out] data Data to read from, always moved to currently handled character.
3913 * @param[in,out] argument Value to write to.
3914 * @param[in,out] flags Flags to write to.
3915 * @param[in,out] exts Extension instances to add to.
3916 *
3917 * @return LY_ERR values.
3918 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003919static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003920parse_argument(struct ly_parser_ctx *ctx, const char **data, const char **argument, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003921{
3922 LY_ERR ret = 0;
3923 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003924 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003925 enum yang_keyword kw;
3926
3927 if (*argument) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02003928 LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003929 return LY_EVALID;
3930 }
3931
3932 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003933 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003934 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003935
Radek Krejci44ceedc2018-10-02 15:54:31 +02003936 INSERT_WORD(ctx, buf, *argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003937 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003938 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003939
3940 switch (kw) {
3941 case YANG_YIN_ELEMENT:
3942 ret = parse_yinelement(ctx, data, flags, exts);
3943 break;
3944 case YANG_CUSTOM:
3945 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_ARGUMENT, 0, exts);
3946 break;
3947 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02003948 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003949 return LY_EVALID;
3950 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003951 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003952 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003953 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003954
3955 return ret;
3956}
3957
Michal Vaskoea5abea2018-09-18 13:10:54 +02003958/**
3959 * @brief Parse the extension statement.
3960 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003961 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003962 * @param[in,out] data Data to read from, always moved to currently handled character.
3963 * @param[in,out] extensions Extensions to add to.
3964 *
3965 * @return LY_ERR values.
3966 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003967static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02003968parse_extension(struct ly_parser_ctx *ctx, const char **data, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003969{
3970 LY_ERR ret = 0;
3971 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003972 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003973 enum yang_keyword kw;
3974 struct lysp_ext *ex;
3975
3976 LYSP_ARRAY_NEW_RET(ctx, extensions, ex, LY_EMEM);
3977
3978 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02003979 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003980 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003981
Radek Krejci44ceedc2018-10-02 15:54:31 +02003982 INSERT_WORD(ctx, buf, ex->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003983 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003984 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003985
3986 switch (kw) {
3987 case YANG_DESCRIPTION:
3988 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &ex->dsc, Y_STR_ARG, &ex->exts);
3989 break;
3990 case YANG_REFERENCE:
3991 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &ex->ref, Y_STR_ARG, &ex->exts);
3992 break;
3993 case YANG_STATUS:
3994 ret = parse_status(ctx, data, &ex->flags, &ex->exts);
3995 break;
3996 case YANG_ARGUMENT:
3997 ret = parse_argument(ctx, data, &ex->argument, &ex->flags, &ex->exts);
3998 break;
3999 case YANG_CUSTOM:
4000 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ex->exts);
4001 break;
4002 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004003 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004004 return LY_EVALID;
4005 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004006 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004007 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004008 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004009
4010 return ret;
4011}
4012
Michal Vaskoea5abea2018-09-18 13:10:54 +02004013/**
4014 * @brief Parse the deviate statement.
4015 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004016 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004017 * @param[in,out] data Data to read from, always moved to currently handled character.
4018 * @param[in,out] deviates Deviates to add to.
4019 *
4020 * @return LY_ERR values.
4021 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004022static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02004023parse_deviate(struct ly_parser_ctx *ctx, const char **data, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004024{
4025 LY_ERR ret = 0;
4026 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004027 size_t word_len, dev_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004028 enum yang_keyword kw;
4029 struct lysp_deviate *iter, *d;
4030 struct lysp_deviate_add *d_add = NULL;
4031 struct lysp_deviate_rpl *d_rpl = NULL;
4032 struct lysp_deviate_del *d_del = NULL;
4033 const char **d_units, ***d_uniques, ***d_dflts;
4034 struct lysp_restr **d_musts;
4035 uint16_t *d_flags;
4036 uint32_t *d_min, *d_max;
4037
4038 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02004039 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02004040 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004041
4042 if ((word_len == 13) && !strncmp(word, "not-supported", word_len)) {
4043 dev_mod = LYS_DEV_NOT_SUPPORTED;
4044 } else if ((word_len == 3) && !strncmp(word, "add", word_len)) {
4045 dev_mod = LYS_DEV_ADD;
4046 } else if ((word_len == 7) && !strncmp(word, "replace", word_len)) {
4047 dev_mod = LYS_DEV_REPLACE;
4048 } else if ((word_len == 6) && !strncmp(word, "delete", word_len)) {
4049 dev_mod = LYS_DEV_DELETE;
4050 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004051 LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004052 free(buf);
4053 return LY_EVALID;
4054 }
4055 free(buf);
4056
4057 /* create structure */
4058 switch (dev_mod) {
4059 case LYS_DEV_NOT_SUPPORTED:
4060 d = calloc(1, sizeof *d);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004061 LY_CHECK_ERR_RET(!d, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004062 break;
4063 case LYS_DEV_ADD:
4064 d_add = calloc(1, sizeof *d_add);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004065 LY_CHECK_ERR_RET(!d_add, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004066 d = (struct lysp_deviate *)d_add;
4067 d_units = &d_add->units;
4068 d_uniques = &d_add->uniques;
4069 d_dflts = &d_add->dflts;
4070 d_musts = &d_add->musts;
4071 d_flags = &d_add->flags;
4072 d_min = &d_add->min;
4073 d_max = &d_add->max;
4074 break;
4075 case LYS_DEV_REPLACE:
4076 d_rpl = calloc(1, sizeof *d_rpl);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004077 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004078 d = (struct lysp_deviate *)d_rpl;
4079 d_units = &d_rpl->units;
4080 d_flags = &d_rpl->flags;
4081 d_min = &d_rpl->min;
4082 d_max = &d_rpl->max;
4083 break;
4084 case LYS_DEV_DELETE:
4085 d_del = calloc(1, sizeof *d_del);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004086 LY_CHECK_ERR_RET(!d_del, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004087 d = (struct lysp_deviate *)d_del;
4088 d_units = &d_del->units;
4089 d_uniques = &d_del->uniques;
4090 d_dflts = &d_del->dflts;
4091 d_musts = &d_del->musts;
4092 d_flags = &d_del->flags;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004093 break;
4094 default:
4095 assert(0);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004096 LOGINT_RET(ctx->ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004097 }
4098 d->mod = dev_mod;
4099
4100 /* insert into siblings */
4101 if (!*deviates) {
4102 *deviates = d;
4103 } else {
4104 for (iter = *deviates; iter->next; iter = iter->next);
4105 iter->next = d;
4106 }
4107
4108 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02004109 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004110
4111 switch (kw) {
4112 case YANG_CONFIG:
4113 switch (dev_mod) {
4114 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004115 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004116 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004117 return LY_EVALID;
4118 default:
4119 ret = parse_config(ctx, data, d_flags, &d->exts);
4120 break;
4121 }
4122 break;
4123 case YANG_DEFAULT:
4124 switch (dev_mod) {
4125 case LYS_DEV_NOT_SUPPORTED:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004126 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004127 return LY_EVALID;
4128 case LYS_DEV_REPLACE:
4129 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &d_rpl->dflt, Y_STR_ARG, &d->exts);
4130 break;
4131 default:
4132 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_DEFAULT, d_dflts, Y_STR_ARG, &d->exts);
4133 break;
4134 }
4135 break;
4136 case YANG_MANDATORY:
4137 switch (dev_mod) {
4138 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004139 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004140 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004141 return LY_EVALID;
4142 default:
4143 ret = parse_mandatory(ctx, data, d_flags, &d->exts);
4144 break;
4145 }
4146 break;
4147 case YANG_MAX_ELEMENTS:
4148 switch (dev_mod) {
4149 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004150 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004151 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004152 return LY_EVALID;
4153 default:
4154 ret = parse_maxelements(ctx, data, d_max, d_flags, &d->exts);
4155 break;
4156 }
4157 break;
4158 case YANG_MIN_ELEMENTS:
4159 switch (dev_mod) {
4160 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004161 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004162 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004163 return LY_EVALID;
4164 default:
4165 ret = parse_minelements(ctx, data, d_min, d_flags, &d->exts);
4166 break;
4167 }
4168 break;
4169 case YANG_MUST:
4170 switch (dev_mod) {
4171 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004172 case LYS_DEV_REPLACE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004173 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004174 return LY_EVALID;
4175 default:
4176 ret = parse_restrs(ctx, data, kw, d_musts);
4177 break;
4178 }
4179 break;
4180 case YANG_TYPE:
4181 switch (dev_mod) {
4182 case LYS_DEV_NOT_SUPPORTED:
4183 case LYS_DEV_ADD:
4184 case LYS_DEV_DELETE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004185 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004186 return LY_EVALID;
4187 default:
4188 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Radek Krejci44ceedc2018-10-02 15:54:31 +02004189 LY_CHECK_ERR_RET(!d_rpl->type, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004190 ret = parse_type(ctx, data, d_rpl->type);
4191 break;
4192 }
4193 break;
4194 case YANG_UNIQUE:
4195 switch (dev_mod) {
4196 case LYS_DEV_NOT_SUPPORTED:
4197 case LYS_DEV_REPLACE:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004198 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004199 return LY_EVALID;
4200 default:
4201 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_UNIQUE, d_uniques, Y_STR_ARG, &d->exts);
4202 break;
4203 }
4204 break;
4205 case YANG_UNITS:
4206 switch (dev_mod) {
4207 case LYS_DEV_NOT_SUPPORTED:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004208 LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004209 return LY_EVALID;
4210 default:
4211 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, d_units, Y_STR_ARG, &d->exts);
4212 break;
4213 }
4214 break;
4215 case YANG_CUSTOM:
4216 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &d->exts);
4217 break;
4218 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004219 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004220 return LY_EVALID;
4221 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004222 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004223 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004224 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004225
4226 return ret;
4227}
4228
Michal Vaskoea5abea2018-09-18 13:10:54 +02004229/**
4230 * @brief Parse the deviation statement.
4231 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004232 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004233 * @param[in,out] data Data to read from, always moved to currently handled character.
4234 * @param[in,out] deviations Deviations to add to.
4235 *
4236 * @return LY_ERR values.
4237 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004238static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02004239parse_deviation(struct ly_parser_ctx *ctx, const char **data, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004240{
4241 LY_ERR ret = 0;
4242 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004243 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004244 enum yang_keyword kw;
4245 struct lysp_deviation *dev;
4246
4247 LYSP_ARRAY_NEW_RET(ctx, deviations, dev, LY_EMEM);
4248
4249 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02004250 ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02004251 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004252
Radek Krejci44ceedc2018-10-02 15:54:31 +02004253 INSERT_WORD(ctx, buf, dev->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004254 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02004255 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004256
4257 switch (kw) {
4258 case YANG_DESCRIPTION:
4259 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &dev->dsc, Y_STR_ARG, &dev->exts);
4260 break;
4261 case YANG_DEVIATE:
4262 ret = parse_deviate(ctx, data, &dev->deviates);
4263 break;
4264 case YANG_REFERENCE:
4265 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &dev->ref, Y_STR_ARG, &dev->exts);
4266 break;
4267 case YANG_CUSTOM:
4268 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &dev->exts);
4269 break;
4270 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004271 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004272 return LY_EVALID;
4273 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004274 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004275 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004276 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004277
4278 /* mandatory substatements */
4279 if (!dev->deviates) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004280 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004281 return LY_EVALID;
4282 }
4283
4284 return ret;
4285}
4286
Michal Vaskoea5abea2018-09-18 13:10:54 +02004287/**
4288 * @brief Parse the feature statement.
4289 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004290 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004291 * @param[in,out] data Data to read from, always moved to currently handled character.
4292 * @param[in,out] features Features to add to.
4293 *
4294 * @return LY_ERR values.
4295 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004296static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02004297parse_feature(struct ly_parser_ctx *ctx, const char **data, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004298{
4299 LY_ERR ret = 0;
4300 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004301 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004302 enum yang_keyword kw;
4303 struct lysp_feature *feat;
4304
4305 LYSP_ARRAY_NEW_RET(ctx, features, feat, LY_EMEM);
4306
4307 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02004308 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02004309 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004310
Radek Krejci44ceedc2018-10-02 15:54:31 +02004311 INSERT_WORD(ctx, buf, feat->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004312 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02004313 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004314
4315 switch (kw) {
4316 case YANG_DESCRIPTION:
4317 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &feat->dsc, Y_STR_ARG, &feat->exts);
4318 break;
4319 case YANG_IF_FEATURE:
4320 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &feat->iffeatures, Y_STR_ARG, &feat->exts);
4321 break;
4322 case YANG_REFERENCE:
4323 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &feat->ref, Y_STR_ARG, &feat->exts);
4324 break;
4325 case YANG_STATUS:
4326 ret = parse_status(ctx, data, &feat->flags, &feat->exts);
4327 break;
4328 case YANG_CUSTOM:
4329 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &feat->exts);
4330 break;
4331 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004332 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "feature");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004333 return LY_EMEM;
4334 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004335 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004336 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004337 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004338
4339 return ret;
4340}
4341
Michal Vaskoea5abea2018-09-18 13:10:54 +02004342/**
4343 * @brief Parse the identity statement.
4344 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004345 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004346 * @param[in,out] data Data to read from, always moved to currently handled character.
4347 * @param[in,out] identities Identities to add to.
4348 *
4349 * @return LY_ERR values.
4350 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004351static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02004352parse_identity(struct ly_parser_ctx *ctx, const char **data, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004353{
4354 LY_ERR ret = 0;
4355 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004356 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004357 enum yang_keyword kw;
4358 struct lysp_ident *ident;
4359
4360 LYSP_ARRAY_NEW_RET(ctx, identities, ident, LY_EMEM);
4361
4362 /* get value */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02004363 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02004364 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004365
Radek Krejci44ceedc2018-10-02 15:54:31 +02004366 INSERT_WORD(ctx, buf, ident->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004367 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02004368 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004369
4370 switch (kw) {
4371 case YANG_DESCRIPTION:
4372 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &ident->dsc, Y_STR_ARG, &ident->exts);
4373 break;
4374 case YANG_IF_FEATURE:
4375 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &ident->iffeatures, Y_STR_ARG, &ident->exts);
4376 break;
4377 case YANG_REFERENCE:
4378 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &ident->ref, Y_STR_ARG, &ident->exts);
4379 break;
4380 case YANG_STATUS:
4381 ret = parse_status(ctx, data, &ident->flags, &ident->exts);
4382 break;
4383 case YANG_BASE:
4384 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_BASE, &ident->bases, Y_PREF_IDENTIF_ARG, &ident->exts);
4385 break;
4386 case YANG_CUSTOM:
4387 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ident->exts);
4388 break;
4389 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004390 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004391 return LY_EVALID;
4392 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004393 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004394 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004395 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004396
4397 return ret;
4398}
4399
Michal Vaskoea5abea2018-09-18 13:10:54 +02004400/**
4401 * @brief Parse the module or submodule statement.
4402 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004403 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004404 * @param[in,out] data Data to read from, always moved to currently handled character.
4405 * @param[in,out] mod Module to write to.
4406 *
4407 * @return LY_ERR values.
4408 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004409static LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +02004410parse_sub_module(struct ly_parser_ctx *ctx, const char **data, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004411{
4412 LY_ERR ret = 0;
4413 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004414 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004415 enum yang_keyword kw, prev_kw = 0;
4416 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
4417
4418 /* (sub)module name */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02004419 ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02004420 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004421
Radek Krejci44ceedc2018-10-02 15:54:31 +02004422 INSERT_WORD(ctx, buf, mod->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004423 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02004424 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004425
Radek Krejcie3846472018-10-15 15:24:51 +02004426#define CHECK_ORDER(SECTION) \
4427 if (mod_stmt > SECTION) {LOGVAL_YANG(ctx, LY_VCODE_INORD, ly_stmt2str(kw), ly_stmt2str(prev_kw)); return LY_EVALID;}mod_stmt = SECTION
4428
Michal Vasko7fbc8162018-09-17 10:35:16 +02004429 switch (kw) {
4430 /* module header */
4431 case YANG_NAMESPACE:
4432 case YANG_PREFIX:
4433 if (mod->submodule) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004434 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004435 return LY_EVALID;
4436 }
Radek Krejcie3846472018-10-15 15:24:51 +02004437 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4438 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004439 case YANG_BELONGS_TO:
Radek Krejcie3846472018-10-15 15:24:51 +02004440 if (!mod->submodule) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004441 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004442 return LY_EVALID;
4443 }
Radek Krejcie3846472018-10-15 15:24:51 +02004444 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4445 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004446 case YANG_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004447 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004448 break;
4449 /* linkage */
4450 case YANG_INCLUDE:
4451 case YANG_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004452 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004453 break;
4454 /* meta */
4455 case YANG_ORGANIZATION:
4456 case YANG_CONTACT:
4457 case YANG_DESCRIPTION:
4458 case YANG_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004459 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004460 break;
4461
4462 /* revision */
4463 case YANG_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004464 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004465 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004466 /* body */
4467 case YANG_ANYDATA:
4468 case YANG_ANYXML:
4469 case YANG_AUGMENT:
4470 case YANG_CHOICE:
4471 case YANG_CONTAINER:
4472 case YANG_DEVIATION:
4473 case YANG_EXTENSION:
4474 case YANG_FEATURE:
4475 case YANG_GROUPING:
4476 case YANG_IDENTITY:
4477 case YANG_LEAF:
4478 case YANG_LEAF_LIST:
4479 case YANG_LIST:
4480 case YANG_NOTIFICATION:
4481 case YANG_RPC:
4482 case YANG_TYPEDEF:
4483 case YANG_USES:
4484 case YANG_CUSTOM:
4485 mod_stmt = Y_MOD_BODY;
4486 break;
4487 default:
4488 /* error handled in the next switch */
4489 break;
4490 }
Radek Krejcie3846472018-10-15 15:24:51 +02004491#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004492
Radek Krejcie3846472018-10-15 15:24:51 +02004493 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004494 switch (kw) {
4495 /* module header */
4496 case YANG_YANG_VERSION:
4497 ret = parse_yangversion(ctx, data, mod);
4498 break;
4499 case YANG_NAMESPACE:
4500 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_NAMESPACE, 0, &mod->ns, Y_STR_ARG, &mod->exts);
4501 break;
4502 case YANG_PREFIX:
4503 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_PREFIX, 0, &mod->prefix, Y_IDENTIF_ARG, &mod->exts);
Radek Krejci70853c52018-10-15 14:46:16 +02004504 LY_CHECK_RET(lysp_check_prefix(ctx, mod, &mod->prefix), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004505 break;
4506 case YANG_BELONGS_TO:
4507 ret = parse_belongsto(ctx, data, &mod->belongsto, &mod->prefix, &mod->exts);
4508 break;
4509
4510 /* linkage */
4511 case YANG_INCLUDE:
4512 ret = parse_include(ctx, data, &mod->includes);
4513 break;
4514 case YANG_IMPORT:
Radek Krejci70853c52018-10-15 14:46:16 +02004515 ret = parse_import(ctx, data, mod);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004516 break;
4517
4518 /* meta */
4519 case YANG_ORGANIZATION:
4520 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_ORGANIZATION, 0, &mod->org, Y_STR_ARG, &mod->exts);
4521 break;
4522 case YANG_CONTACT:
4523 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_CONTACT, 0, &mod->contact, Y_STR_ARG, &mod->exts);
4524 break;
4525 case YANG_DESCRIPTION:
4526 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &mod->dsc, Y_STR_ARG, &mod->exts);
4527 break;
4528 case YANG_REFERENCE:
4529 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &mod->ref, Y_STR_ARG, &mod->exts);
4530 break;
4531
4532 /* revision */
4533 case YANG_REVISION:
4534 ret = parse_revision(ctx, data, &mod->revs);
4535 break;
4536
4537 /* body */
4538 case YANG_ANYDATA:
4539 case YANG_ANYXML:
4540 ret = parse_any(ctx, data, kw, &mod->data);
4541 break;
4542 case YANG_CHOICE:
4543 ret = parse_choice(ctx, data, &mod->data);
4544 break;
4545 case YANG_CONTAINER:
4546 ret = parse_container(ctx, data, &mod->data);
4547 break;
4548 case YANG_LEAF:
4549 ret = parse_leaf(ctx, data, &mod->data);
4550 break;
4551 case YANG_LEAF_LIST:
4552 ret = parse_leaflist(ctx, data, &mod->data);
4553 break;
4554 case YANG_LIST:
4555 ret = parse_list(ctx, data, &mod->data);
4556 break;
4557 case YANG_USES:
4558 ret = parse_uses(ctx, data, &mod->data);
4559 break;
4560
4561 case YANG_AUGMENT:
4562 ret = parse_augment(ctx, data, &mod->augments);
4563 break;
4564 case YANG_DEVIATION:
4565 ret = parse_deviation(ctx, data, &mod->deviations);
4566 break;
4567 case YANG_EXTENSION:
4568 ret = parse_extension(ctx, data, &mod->extensions);
4569 break;
4570 case YANG_FEATURE:
4571 ret = parse_feature(ctx, data, &mod->features);
4572 break;
4573 case YANG_GROUPING:
4574 ret = parse_grouping(ctx, data, &mod->groupings);
4575 break;
4576 case YANG_IDENTITY:
4577 ret = parse_identity(ctx, data, &mod->identities);
4578 break;
4579 case YANG_NOTIFICATION:
4580 ret = parse_notif(ctx, data, &mod->notifs);
4581 break;
4582 case YANG_RPC:
4583 ret = parse_action(ctx, data, &mod->rpcs);
4584 break;
4585 case YANG_TYPEDEF:
4586 ret = parse_typedef(ctx, data, &mod->typedefs);
4587 break;
4588 case YANG_CUSTOM:
4589 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &mod->exts);
4590 break;
4591
4592 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +02004593 LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), mod->submodule ? "submodule" : "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004594 return LY_EVALID;
4595 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004596 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004597 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004598 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004599
4600 /* mandatory substatements */
4601 if (mod->submodule) {
4602 if (!mod->belongsto) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004603 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004604 return LY_EVALID;
4605 }
4606 } else {
4607 if (!mod->ns) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004608 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004609 return LY_EVALID;
4610 } else if (!mod->prefix) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004611 LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004612 return LY_EVALID;
4613 }
4614 }
4615
4616 return ret;
4617}
4618
Radek Krejcid4557c62018-09-17 11:42:09 +02004619LY_ERR
Michal Vasko7fbc8162018-09-17 10:35:16 +02004620yang_parse(struct ly_ctx *ctx, const char *data, struct lysp_module **mod_p)
4621{
4622 LY_ERR ret = 0;
4623 char *word, *buf;
Radek Krejciefd22f62018-09-27 11:47:58 +02004624 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004625 enum yang_keyword kw;
Radek Krejci0c2cf322018-10-13 08:02:30 +02004626 struct lysp_module *mod = NULL;
Radek Krejci44ceedc2018-10-02 15:54:31 +02004627 struct ly_parser_ctx context;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004628
Radek Krejci44ceedc2018-10-02 15:54:31 +02004629 context.ctx = ctx;
4630 context.line = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004631
4632 /* "module"/"submodule" */
Radek Krejci44ceedc2018-10-02 15:54:31 +02004633 ret = get_keyword(&context, &data, &kw, &word, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02004634 LY_CHECK_GOTO(ret, error);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004635
4636 if ((kw != YANG_MODULE) && (kw != YANG_SUBMODULE)) {
Radek Krejcifc62d7e2018-10-11 12:56:42 +02004637 LOGVAL_YANG(&context, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".",
Radek Krejcic59bc972018-09-17 16:13:06 +02004638 ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004639 goto error;
4640 }
4641
4642 mod = calloc(1, sizeof *mod);
4643 LY_CHECK_ERR_GOTO(!mod, LOGMEM(ctx), error);
4644 if (kw == YANG_SUBMODULE) {
4645 mod->submodule = 1;
4646 }
Radek Krejci9fcacc12018-10-11 15:59:11 +02004647 mod->ctx = ctx;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004648
4649 /* substatements */
Radek Krejci44ceedc2018-10-02 15:54:31 +02004650 ret = parse_sub_module(&context, &data, mod);
Radek Krejcic59bc972018-09-17 16:13:06 +02004651 LY_CHECK_GOTO(ret, error);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004652
4653 /* read some trailing spaces or new lines */
Radek Krejcifc62d7e2018-10-11 12:56:42 +02004654 ret = get_argument(&context, &data, Y_MAYBE_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02004655 LY_CHECK_GOTO(ret, error);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004656
4657 if (word) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02004658 LOGVAL_YANG(&context, LYVE_SYNTAX, "Invalid character sequence \"%.*s\", expected end-of-file.",
Michal Vasko7fbc8162018-09-17 10:35:16 +02004659 word_len, word);
4660 free(buf);
4661 goto error;
4662 }
4663 assert(!buf);
4664
4665 *mod_p = mod;
4666 return ret;
4667
4668error:
Radek Krejci9fcacc12018-10-11 15:59:11 +02004669 lysp_module_free(mod);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004670 return ret;
4671}