blob: aa587562b5a7fa2aec97ce7bdfcd6bc9d38ce41b [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 */
Michal Vasko7fbc8162018-09-17 10:35:16 +020014
15#include "common.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020016
17#include <assert.h>
18#include <ctype.h>
19#include <errno.h>
20#include <stdint.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24
Michal Vasko7fbc8162018-09-17 10:35:16 +020025#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020026#include "dict.h"
27#include "extensions.h"
28#include "log.h"
29#include "set.h"
30#include "tree.h"
31#include "tree_schema.h"
Radek Krejci70853c52018-10-15 14:46:16 +020032#include "tree_schema_internal.h"
Radek Krejci44ceedc2018-10-02 15:54:31 +020033
Radek Krejciceaf2122019-01-02 15:03:26 +010034/**
Radek Krejciceaf2122019-01-02 15:03:26 +010035 * @brief Insert WORD into the libyang context's dictionary and store as TARGET.
36 * @param[in] CTX yang parser context to access libyang context.
37 * @param[in] BUF buffer in case the word is not a constant and can be inserted directly (zero-copy)
38 * @param[out] TARGET variable where to store the pointer to the inserted value.
39 * @param[in] WORD string to store.
40 * @param[in] LEN length of the string in WORD to store.
41 */
Radek Krejci9fcacc12018-10-11 15:59:11 +020042#define INSERT_WORD(CTX, BUF, TARGET, WORD, LEN) \
43 if (BUF) {(TARGET) = lydict_insert_zc((CTX)->ctx, WORD);}\
Radek Krejcif09e4e82019-06-14 15:08:11 +020044 else {(TARGET) = lydict_insert((CTX)->ctx, LEN ? WORD : "", LEN);}
Radek Krejci44ceedc2018-10-02 15:54:31 +020045
Radek Krejciceaf2122019-01-02 15:03:26 +010046/**
47 * @brief Move the DATA pointer by COUNT items. Also updates the indent value in yang parser context
48 * @param[in] CTX yang parser context to update its indent value.
49 * @param[in,out] DATA pointer to move
50 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
51 */
Radek Krejci2b610482019-01-02 13:36:09 +010052#define MOVE_INPUT(CTX, DATA, COUNT) (*(DATA))+=COUNT;(CTX)->indent+=COUNT
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020053
Michal Vaskoea5abea2018-09-18 13:10:54 +020054/**
55 * @brief Loop through all substatements providing, return if there are none.
56 *
Radek Krejci44ceedc2018-10-02 15:54:31 +020057 * @param[in] CTX yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +020058 * @param[in] DATA Raw data to read from.
59 * @param[out] KW YANG keyword read.
60 * @param[out] WORD Pointer to the keyword itself.
61 * @param[out] WORD_LEN Length of the keyword.
62 * @param[out] ERR Variable for error storing.
63 *
64 * @return In case there are no substatements or a fatal error encountered.
65 */
Radek Krejci6d6556c2018-11-08 09:37:45 +010066#define YANG_READ_SUBSTMT_FOR(CTX, DATA, KW, WORD, WORD_LEN, ERR, CHECKGOTO) \
Radek Krejci6d9b9b52018-11-02 12:43:39 +010067 LY_CHECK_RET(get_keyword(CTX, DATA, &KW, &WORD, &WORD_LEN)); \
Michal Vasko7fbc8162018-09-17 10:35:16 +020068 if (KW == YANG_SEMICOLON) { \
Radek Krejci6d6556c2018-11-08 09:37:45 +010069 CHECKGOTO; \
Radek Krejci6d9b9b52018-11-02 12:43:39 +010070 return LY_SUCCESS; \
Michal Vasko7fbc8162018-09-17 10:35:16 +020071 } \
72 if (KW != YANG_LEFT_BRACE) { \
David Sedlákb3077192019-06-19 10:55:37 +020073 LOGVAL_PARSER(CTX, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\", expected \";\" or \"{\".", ly_stmt2str(KW)); \
Michal Vasko7fbc8162018-09-17 10:35:16 +020074 return LY_EVALID; \
75 } \
76 for (ERR = get_keyword(CTX, DATA, &KW, &WORD, &WORD_LEN); \
77 !ERR && (KW != YANG_RIGHT_BRACE); \
78 ERR = get_keyword(CTX, DATA, &KW, &WORD, &WORD_LEN))
79
Radek Krejciceaf2122019-01-02 15:03:26 +010080/**
81 * @brief Check module version is at least 2 (YANG 1.1) because of the keyword presence.
82 * Logs error message and returns LY_EVALID in case of module in YANG version 1.0.
83 * @param[in] CTX yang parser context to get current module and for logging.
84 * @param[in] KW keyword allowed only in YANG version 1.1 (or later) - for logging.
85 * @param[in] PARENT parent statement where the KW is present - for logging.
86 */
87#define YANG_CHECK_STMTVER2_RET(CTX, KW, PARENT) \
David Sedlákb3077192019-06-19 10:55:37 +020088 if ((CTX)->mod_version < 2) {LOGVAL_PARSER((CTX), LY_VCODE_INCHILDSTMT2, KW, PARENT); return LY_EVALID;}
Radek Krejci10113652018-11-14 16:56:50 +010089
Radek Krejci2d7a47b2019-05-16 13:34:10 +020090LY_ERR parse_container(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
91LY_ERR parse_uses(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
92LY_ERR parse_choice(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
93LY_ERR parse_case(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
94LY_ERR parse_list(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
95LY_ERR parse_grouping(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_grp **groupings);
Michal Vasko7fbc8162018-09-17 10:35:16 +020096
Michal Vaskoea5abea2018-09-18 13:10:54 +020097/**
98 * @brief Add another character to dynamic buffer, a low-level function.
99 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200100 * Enlarge if needed. Updates \p input as well as \p buf_used.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200101 *
Radek Krejci404251e2018-10-09 12:06:44 +0200102 * @param[in] ctx libyang context for logging.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200103 * @param[in, out] input Input string to process.
104 * @param[in] len Number of bytes to get from the input string and copy into the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200105 * @param[in,out] buf Buffer to use, can be moved by realloc().
106 * @param[in,out] buf_len Current size of the buffer.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200107 * @param[in,out] buf_used Currently used characters of the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200108 *
109 * @return LY_ERR values.
110 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200111LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +0200112buf_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 +0200113{
Radek Krejci44ceedc2018-10-02 15:54:31 +0200114 if (*buf_len <= (*buf_used) + len) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200115 *buf_len += 16;
116 *buf = ly_realloc(*buf, *buf_len);
117 LY_CHECK_ERR_RET(!*buf, LOGMEM(ctx), LY_EMEM);
118 }
Radek Krejcic0917392019-04-10 13:04:04 +0200119 if (*buf_used) {
120 memcpy(&(*buf)[*buf_used], *input, len);
121 } else {
122 memcpy(*buf, *input, len);
123 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200124
Radek Krejci44ceedc2018-10-02 15:54:31 +0200125 (*buf_used) += len;
126 (*input) += len;
127
Michal Vasko7fbc8162018-09-17 10:35:16 +0200128 return LY_SUCCESS;
129}
130
Michal Vaskoea5abea2018-09-18 13:10:54 +0200131/**
Radek Krejci44ceedc2018-10-02 15:54:31 +0200132 * @brief Store a single UTF8 character. It depends whether in a dynamically-allocated buffer or just as a pointer to the data.
133 *
134 * @param[in] ctx yang parser context for logging.
135 * @param[in,out] input Pointer to the string where to get the character to store. Automatically moved to the next character
136 * when function returns.
137 * @param[in] arg Type of the input string to select method of checking character validity.
138 * @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 +0200139 * 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 +0200140 * @param[in,out] word_len Current length of the word pointed to by \p word_p.
141 * @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 +0200142 * @param[in,out] buf_len Current length of \p word_b.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200143 * @param[in] need_buf Flag if the dynamically allocated buffer is required.
David Sedlák40bb13b2019-07-10 14:34:18 +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
Michal Vaskoea5abea2018-09-18 13:10:54 +0200148 *
149 * @return LY_ERR values.
150 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200151LY_ERR
David Sedlák40bb13b2019-07-10 14:34:18 +0200152buf_store_char(struct lys_parser_ctx *ctx, const char **input, enum yang_arg arg, char **word_p,
153 size_t *word_len, char **word_b, size_t *buf_len, int need_buf, int *prefix)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200154{
Radek Krejci44ceedc2018-10-02 15:54:31 +0200155 unsigned int c;
156 size_t len;
157
Radek Krejcif29b7c32019-04-09 16:17:49 +0200158 /* check valid combination of input paremeters - if need_buf specified, word_b must be provided */
159 assert(!need_buf || (need_buf && word_b));
160
Radek Krejci44ceedc2018-10-02 15:54:31 +0200161 /* get UTF8 code point (and number of bytes coding the character) */
162 LY_CHECK_ERR_RET(ly_getutf8(input, &c, &len),
David Sedlákb3077192019-06-19 10:55:37 +0200163 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, (*input)[-len]), LY_EVALID);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200164 (*input) -= len;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200165 if (c == '\n') {
166 ctx->indent = 0;
167 } else {
168 /* note - even the multibyte character is count as 1 */
169 ++ctx->indent;
170 }
171
Radek Krejci44ceedc2018-10-02 15:54:31 +0200172 /* check character validity */
173 switch (arg) {
174 case Y_IDENTIF_ARG:
David Sedlák4a650532019-07-10 11:55:18 +0200175 LY_CHECK_RET(lysp_check_identifierchar(ctx, c, !(*word_len), NULL));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200176 break;
177 case Y_PREF_IDENTIF_ARG:
David Sedlák40bb13b2019-07-10 14:34:18 +0200178 LY_CHECK_RET(lysp_check_identifierchar(ctx, c, !(*word_len), prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200179 break;
180 case Y_STR_ARG:
181 case Y_MAYBE_STR_ARG:
David Sedlák4a650532019-07-10 11:55:18 +0200182 LY_CHECK_RET(lysp_check_stringchar(ctx, c));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200183 break;
184 }
185
Michal Vasko7fbc8162018-09-17 10:35:16 +0200186 if (word_b && *word_b) {
187 /* add another character into buffer */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200188 if (buf_add_char(ctx->ctx, input, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200189 return LY_EMEM;
190 }
191
192 /* in case of realloc */
193 *word_p = *word_b;
Radek Krejcif29b7c32019-04-09 16:17:49 +0200194 } else if (word_b && need_buf) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200195 /* first time we need a buffer, copy everything read up to now */
196 if (*word_len) {
197 *word_b = malloc(*word_len);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200198 LY_CHECK_ERR_RET(!*word_b, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200199 *buf_len = *word_len;
200 memcpy(*word_b, *word_p, *word_len);
201 }
202
203 /* add this new character into buffer */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200204 if (buf_add_char(ctx->ctx, input, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200205 return LY_EMEM;
206 }
207
208 /* in case of realloc */
209 *word_p = *word_b;
210 } else {
211 /* just remember the first character pointer */
212 if (!*word_p) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200213 *word_p = (char *)(*input);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200214 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200215 /* ... and update the word's length */
216 (*word_len) += len;
217 (*input) += len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200218 }
219
220 return LY_SUCCESS;
221}
222
Michal Vaskoea5abea2018-09-18 13:10:54 +0200223/**
224 * @brief Skip YANG comment in data.
225 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200226 * @param[in] ctx yang parser context for logging.
227 * @param[in,out] data Data to read from, automatically moved after the comment.
228 * @param[in] comment Type of the comment to process:
229 * 1 for a one-line comment,
230 * 2 for a block comment.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200231 *
232 * @return LY_ERR values.
233 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200234LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200235skip_comment(struct lys_parser_ctx *ctx, const char **data, int comment)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200236{
Radek Krejci80dd33e2018-09-26 15:57:18 +0200237 /* internal statuses: 0 - comment ended,
238 * 1 - in line comment,
239 * 2 - in block comment,
240 * 3 - in block comment with last read character '*'
241 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200242 while (**data && comment) {
243 switch (comment) {
244 case 1:
245 if (**data == '\n') {
246 comment = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200247 ++ctx->line;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200248 }
249 break;
250 case 2:
251 if (**data == '*') {
Radek Krejci15c80ca2018-10-09 11:01:31 +0200252 comment = 3;
253 } else if (**data == '\n') {
254 ++ctx->line;
255 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200256 break;
257 case 3:
258 if (**data == '/') {
259 comment = 0;
Radek Krejci5b930492019-06-11 14:54:08 +0200260 } else if (**data != '*') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200261 if (**data == '\n') {
262 ++ctx->line;
263 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200264 comment = 2;
265 }
266 break;
267 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200268 LOGINT_RET(ctx->ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200269 }
270
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200271 if (**data == '\n') {
272 ctx->indent = 0;
273 } else {
274 ++ctx->indent;
275 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200276 ++(*data);
277 }
278
279 if (!**data && (comment > 1)) {
David Sedlákb3077192019-06-19 10:55:37 +0200280 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Unexpected end-of-input, non-terminated comment.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200281 return LY_EVALID;
282 }
283
284 return LY_SUCCESS;
285}
286
Michal Vaskoea5abea2018-09-18 13:10:54 +0200287/**
288 * @brief Read a quoted string from data.
289 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200290 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200291 * @param[in,out] data Data to read from, always moved to currently handled character.
292 * @param[in] arg Type of YANG keyword argument expected.
293 * @param[out] word_p Pointer to the read quoted string.
294 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read quoted string. If not needed,
295 * set to NULL. Otherwise equal to \p word_p.
296 * @param[out] word_len Length of the read quoted string.
297 * @param[out] buf_len Length of the dynamically-allocated buffer \p word_b.
298 * @param[in] indent Current indent (number of YANG spaces). Needed for correct multi-line string
299 * indenation in the final quoted string.
300 *
301 * @return LY_ERR values.
302 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200303static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200304read_qstring(struct lys_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 +0200305 size_t *buf_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200306{
307 /* string: 0 - string ended, 1 - string with ', 2 - string with ", 3 - string with " with last character \,
308 * 4 - string finished, now skipping whitespaces looking for +,
309 * 5 - string continues after +, skipping whitespaces */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200310 unsigned int string, block_indent = 0, current_indent = 0, need_buf = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200311 const char *c;
David Sedlák40bb13b2019-07-10 14:34:18 +0200312 int prefix = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200313
314 if (**data == '\"') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200315 string = 2;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200316 current_indent = block_indent = ctx->indent + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200317 } else {
318 assert(**data == '\'');
319 string = 1;
320 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200321 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200322
323 while (**data && string) {
324 switch (string) {
325 case 1:
326 switch (**data) {
327 case '\'':
328 /* string may be finished, but check for + */
329 string = 4;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200330 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200331 break;
332 default:
333 /* check and store character */
David Sedlák40bb13b2019-07-10 14:34:18 +0200334 LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200335 break;
336 }
337 break;
338 case 2:
339 switch (**data) {
340 case '\"':
341 /* string may be finished, but check for + */
342 string = 4;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200343 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200344 break;
345 case '\\':
346 /* special character following */
347 string = 3;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200348 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200349 break;
350 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200351 if (current_indent < block_indent) {
352 ++current_indent;
353 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200354 } else {
355 /* check and store character */
David Sedlák40bb13b2019-07-10 14:34:18 +0200356 LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200357 }
358 break;
359 case '\t':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200360 if (current_indent < block_indent) {
361 assert(need_buf);
362 current_indent += 8;
363 ctx->indent += 8;
364 for (; current_indent > block_indent; --current_indent, --ctx->indent) {
365 /* store leftover spaces from the tab */
366 c = " ";
David Sedlák40bb13b2019-07-10 14:34:18 +0200367 LY_CHECK_RET(buf_store_char(ctx, &c, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200368 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200369 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200370 } else {
371 /* check and store character */
David Sedlák40bb13b2019-07-10 14:34:18 +0200372 LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200373 /* additional characters for indentation - only 1 was count in buf_store_char */
374 ctx->indent += 7;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200375 }
376 break;
377 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200378 if (block_indent) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200379 /* we will be removing the indents so we need our own buffer */
380 need_buf = 1;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200381
382 /* remove trailing tabs and spaces */
383 while ((*word_len) && ((*word_p)[(*word_len) - 1] == '\t' || (*word_p)[(*word_len) - 1] == ' ')) {
384 --(*word_len);
385 }
386
387 /* start indentation */
388 current_indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200389 }
390
391 /* check and store character */
David Sedlák40bb13b2019-07-10 14:34:18 +0200392 LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200393
394 /* maintain line number */
395 ++ctx->line;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200396
397 /* reset context indentation counter for possible string after this one */
398 ctx->indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200399 break;
400 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200401 /* first non-whitespace character, stop eating indentation */
402 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200403
404 /* check and store character */
David Sedlák40bb13b2019-07-10 14:34:18 +0200405 LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200406 break;
407 }
408 break;
409 case 3:
410 /* string encoded characters */
411 switch (**data) {
412 case 'n':
413 c = "\n";
414 break;
415 case 't':
416 c = "\t";
417 break;
418 case '\"':
419 c = *data;
420 break;
421 case '\\':
422 c = *data;
423 break;
424 default:
David Sedlákb3077192019-06-19 10:55:37 +0200425 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Double-quoted string unknown special character '\\%c'.", **data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200426 return LY_EVALID;
427 }
428
429 /* check and store character */
David Sedlák40bb13b2019-07-10 14:34:18 +0200430 LY_CHECK_RET(buf_store_char(ctx, &c, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200431
432 string = 2;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200433 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200434 break;
435 case 4:
436 switch (**data) {
437 case '+':
438 /* string continues */
439 string = 5;
Radek Krejciefd22f62018-09-27 11:47:58 +0200440 need_buf = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200441 break;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200442 case '\n':
443 ++ctx->line;
444 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200445 case ' ':
446 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200447 /* just skip */
448 break;
449 default:
450 /* string is finished */
451 goto string_end;
452 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200453 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200454 break;
455 case 5:
456 switch (**data) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200457 case '\n':
458 ++ctx->line;
459 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200460 case ' ':
461 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200462 /* skip */
463 break;
464 case '\'':
465 string = 1;
466 break;
467 case '\"':
468 string = 2;
469 break;
470 default:
471 /* it must be quoted again */
David Sedlákb3077192019-06-19 10:55:37 +0200472 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Both string parts divided by '+' must be quoted.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200473 return LY_EVALID;
474 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200475 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200476 break;
477 default:
478 return LY_EINT;
479 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200480 }
481
482string_end:
Radek Krejci4e199f52019-05-28 09:09:28 +0200483 if (arg <= Y_PREF_IDENTIF_ARG && !(*word_len)) {
484 /* empty identifier */
David Sedlákb3077192019-06-19 10:55:37 +0200485 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Statement argument is required.");
Radek Krejci4e199f52019-05-28 09:09:28 +0200486 return LY_EVALID;
487 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200488 return LY_SUCCESS;
489}
490
Michal Vaskoea5abea2018-09-18 13:10:54 +0200491/**
492 * @brief Get another YANG string from the raw data.
493 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200494 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200495 * @param[in,out] data Data to read from, always moved to currently handled character.
496 * @param[in] arg Type of YANG keyword argument expected.
Radek Krejcid3ca0632019-04-16 16:54:54 +0200497 * @param[out] flags optional output argument to get flag of the argument's quoting (LYS_*QOUTED - see [schema node flags](@ref snodeflags))
Michal Vasko2ca70f52018-09-27 11:04:51 +0200498 * @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 +0200499 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read string. If not needed,
500 * set to NULL. Otherwise equal to \p word_p.
501 * @param[out] word_len Length of the read string.
502 *
503 * @return LY_ERR values.
Michal Vasko7fbc8162018-09-17 10:35:16 +0200504 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200505LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200506get_argument(struct lys_parser_ctx *ctx, const char **data, enum yang_arg arg,
Radek Krejcid3ca0632019-04-16 16:54:54 +0200507 uint16_t *flags, char **word_p, char **word_b, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200508{
Radek Krejci44ceedc2018-10-02 15:54:31 +0200509 size_t buf_len = 0;
David Sedlák40bb13b2019-07-10 14:34:18 +0200510 int prefix = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200511 /* word buffer - dynamically allocated */
512 *word_b = NULL;
513
514 /* word pointer - just a pointer to data */
515 *word_p = NULL;
516
517 *word_len = 0;
518 while (**data) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200519 switch (**data) {
520 case '\'':
521 case '\"':
522 if (*word_len) {
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200523 /* invalid - quotes cannot be in unquoted string and only optsep, ; or { can follow it */
David Sedlákb3077192019-06-19 10:55:37 +0200524 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, *data,
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200525 "unquoted string character, optsep, semicolon or opening brace");
526 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200527 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200528 if (flags) {
529 (*flags) |= (**data) == '\'' ? LYS_SINGLEQUOTED : LYS_DOUBLEQUOTED;
530 }
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100531 LY_CHECK_RET(read_qstring(ctx, data, arg, word_p, word_b, word_len, &buf_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200532 goto str_end;
533 case '/':
Radek Krejci44ceedc2018-10-02 15:54:31 +0200534 if ((*data)[1] == '/') {
535 /* one-line comment */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200536 MOVE_INPUT(ctx, data, 2);
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100537 LY_CHECK_RET(skip_comment(ctx, data, 1));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200538 } else if ((*data)[1] == '*') {
539 /* block comment */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200540 MOVE_INPUT(ctx, data, 2);
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100541 LY_CHECK_RET(skip_comment(ctx, data, 2));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200542 } else {
543 /* not a comment after all */
David Sedlák40bb13b2019-07-10 14:34:18 +0200544 LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, &buf_len, 0, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200545 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200546 break;
547 case ' ':
548 if (*word_len) {
549 /* word is finished */
550 goto str_end;
551 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200552 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200553 break;
554 case '\t':
555 if (*word_len) {
556 /* word is finished */
557 goto str_end;
558 }
559 /* tabs count for 8 spaces */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200560 ctx->indent += 8;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200561
562 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200563 break;
564 case '\n':
565 if (*word_len) {
566 /* word is finished */
567 goto str_end;
568 }
569 /* reset indent */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200570 ctx->indent = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200571
572 /* track line numbers */
573 ++ctx->line;
574
575 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200576 break;
577 case ';':
578 case '{':
579 if (*word_len || (arg == Y_MAYBE_STR_ARG)) {
580 /* word is finished */
581 goto str_end;
582 }
583
David Sedlákb3077192019-06-19 10:55:37 +0200584 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, *data, "an argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200585 return LY_EVALID;
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200586 case '}':
587 /* invalid - braces cannot be in unquoted string (opening braces terminates the string and can follow it) */
David Sedlákb3077192019-06-19 10:55:37 +0200588 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, *data,
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200589 "unquoted string character, optsep, semicolon or opening brace");
590 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200591 default:
David Sedlák40bb13b2019-07-10 14:34:18 +0200592 LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, &buf_len, 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200593 break;
594 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200595 }
596
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200597 /* unexpected end of loop */
David Sedlákb3077192019-06-19 10:55:37 +0200598 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200599 return LY_EVALID;
600
Michal Vasko7fbc8162018-09-17 10:35:16 +0200601str_end:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200602 /* terminating NULL byte for buf */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200603 if (*word_b) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200604 (*word_b) = ly_realloc(*word_b, (*word_len) + 1);
605 LY_CHECK_ERR_RET(!(*word_b), LOGMEM(ctx->ctx), LY_EMEM);
606 (*word_b)[*word_len] = '\0';
Michal Vasko7fbc8162018-09-17 10:35:16 +0200607 *word_p = *word_b;
608 }
609
610 return LY_SUCCESS;
611}
612
Michal Vaskoea5abea2018-09-18 13:10:54 +0200613/**
614 * @brief Get another YANG keyword from the raw data.
615 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200616 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200617 * @param[in,out] data Data to read from, always moved to currently handled character.
618 * @param[out] kw YANG keyword read.
619 * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances.
620 * @param[out] word_len Length of the keyword in the data. Useful for extension instances.
621 *
622 * @return LY_ERR values.
623 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200624LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200625get_keyword(struct lys_parser_ctx *ctx, const char **data, enum yang_keyword *kw, char **word_p, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200626{
Michal Vasko7fbc8162018-09-17 10:35:16 +0200627 int prefix;
628 const char *word_start;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200629 unsigned int c;
630 size_t len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200631
632 if (word_p) {
633 *word_p = NULL;
634 *word_len = 0;
635 }
636
637 /* first skip "optsep", comments */
638 while (**data) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200639 switch (**data) {
640 case '/':
641 if ((*data)[1] == '/') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200642 /* one-line comment */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200643 MOVE_INPUT(ctx, data, 2);
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100644 LY_CHECK_RET(skip_comment(ctx, data, 1));
Radek Krejcidcc7b322018-10-11 14:24:02 +0200645 } else if ((*data)[1] == '*') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200646 /* block comment */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200647 MOVE_INPUT(ctx, data, 2);
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100648 LY_CHECK_RET(skip_comment(ctx, data, 2));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200649 } else {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200650 /* error - not a comment after all, keyword cannot start with slash */
David Sedlákb3077192019-06-19 10:55:37 +0200651 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '/'.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200652 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200653 }
Radek Krejci13028282019-06-11 14:56:48 +0200654 continue;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200655 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200656 /* skip whitespaces (optsep) */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200657 ++ctx->line;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200658 ctx->indent = 0;
659 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200660 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200661 /* skip whitespaces (optsep) */
662 ++ctx->indent;
663 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200664 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200665 /* skip whitespaces (optsep) */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200666 ctx->indent += 8;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200667 break;
668 default:
669 /* either a keyword start or an invalid character */
670 goto keyword_start;
671 }
672
673 ++(*data);
674 }
675
676keyword_start:
677 word_start = *data;
David Sedlák5f8f0332019-06-18 16:34:30 +0200678 *kw = lysp_match_kw(ctx, data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200679
David Sedlák1bccdfa2019-06-17 15:55:27 +0200680 if (*kw == YANG_SEMICOLON || *kw == YANG_LEFT_BRACE || *kw == YANG_RIGHT_BRACE) {
Radek Krejci626df482018-10-11 15:06:31 +0200681 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200682 }
683
684 if (*kw != YANG_NONE) {
685 /* make sure we have the whole keyword */
686 switch (**data) {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200687 case '\n':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200688 case '\t':
Radek Krejciabdd8062019-06-11 16:44:19 +0200689 case ' ':
690 /* mandatory "sep" is just checked, not eaten so nothing in the context is updated */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200691 break;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200692 case ':':
693 /* keyword is not actually a keyword, but prefix of an extension.
694 * To avoid repeated check of the prefix syntax, move to the point where the colon was read
695 * and we will be checking the keyword (extension instance) itself */
696 prefix = 1;
697 MOVE_INPUT(ctx, data, 1);
698 goto extension;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200699 case '{':
700 /* allowed only for input and output statements which can be without arguments */
701 if (*kw == YANG_INPUT || *kw == YANG_OUTPUT) {
702 break;
703 }
704 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200705 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200706 MOVE_INPUT(ctx, data, 1);
David Sedlákb3077192019-06-19 10:55:37 +0200707 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(*data - word_start), word_start,
Radek Krejci44ceedc2018-10-02 15:54:31 +0200708 "a keyword followed by a separator");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200709 return LY_EVALID;
710 }
711 } else {
712 /* still can be an extension */
713 prefix = 0;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200714extension:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200715 while (**data && (**data != ' ') && (**data != '\t') && (**data != '\n') && (**data != '{') && (**data != ';')) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200716 LY_CHECK_ERR_RET(ly_getutf8(data, &c, &len),
David Sedlákb3077192019-06-19 10:55:37 +0200717 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, (*data)[-len]), LY_EVALID);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200718 ++ctx->indent;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200719 /* check character validity */
David Sedlák4a650532019-07-10 11:55:18 +0200720 LY_CHECK_RET(lysp_check_identifierchar(ctx, c, *data - len == word_start ? 1 : 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200721 }
722 if (!**data) {
David Sedlákb3077192019-06-19 10:55:37 +0200723 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200724 return LY_EVALID;
725 }
726
727 /* prefix is mandatory for extension instances */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200728 if (prefix != 2) {
David Sedlákb3077192019-06-19 10:55:37 +0200729 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(*data - word_start), word_start, "a keyword");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200730 return LY_EVALID;
731 }
732
733 *kw = YANG_CUSTOM;
734 }
Radek Krejci626df482018-10-11 15:06:31 +0200735success:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200736 if (word_p) {
737 *word_p = (char *)word_start;
738 *word_len = *data - word_start;
739 }
740
741 return LY_SUCCESS;
742}
743
Michal Vaskoea5abea2018-09-18 13:10:54 +0200744/**
745 * @brief Parse extension instance substatements.
746 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200747 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200748 * @param[in,out] data Data to read from, always moved to currently handled character.
749 * @param[in] word Extension instance substatement name (keyword).
750 * @param[in] word_len Extension instance substatement name length.
751 * @param[in,out] child Children of this extension instance to add to.
752 *
753 * @return LY_ERR values.
754 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200755static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200756parse_ext_substmt(struct lys_parser_ctx *ctx, const char **data, char *word, size_t word_len,
Michal Vasko7fbc8162018-09-17 10:35:16 +0200757 struct lysp_stmt **child)
758{
759 char *buf;
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100760 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200761 enum yang_keyword kw;
762 struct lysp_stmt *stmt, *par_child;
763
764 stmt = calloc(1, sizeof *stmt);
765 LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM);
766
Radek Krejcibb9b1982019-04-08 14:24:59 +0200767 /* insert into parent statements */
768 if (!*child) {
769 *child = stmt;
770 } else {
771 for (par_child = *child; par_child->next; par_child = par_child->next);
772 par_child->next = stmt;
773 }
774
Radek Krejci44ceedc2018-10-02 15:54:31 +0200775 stmt->stmt = lydict_insert(ctx->ctx, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200776
777 /* get optional argument */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200778 LY_CHECK_RET(get_argument(ctx, data, Y_MAYBE_STR_ARG, &stmt->flags, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200779
Radek Krejci0ae092d2018-09-20 16:43:19 +0200780 if (word) {
781 if (buf) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200782 stmt->arg = lydict_insert_zc(ctx->ctx, word);
Radek Krejci0ae092d2018-09-20 16:43:19 +0200783 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200784 stmt->arg = lydict_insert(ctx->ctx, word, word_len);
Radek Krejci0ae092d2018-09-20 16:43:19 +0200785 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200786 }
787
Radek Krejci6d6556c2018-11-08 09:37:45 +0100788 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, ) {
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100789 LY_CHECK_RET(parse_ext_substmt(ctx, data, word, word_len, &stmt->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200790 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200791 return ret;
792}
793
Michal Vaskoea5abea2018-09-18 13:10:54 +0200794/**
795 * @brief Parse extension instance.
796 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200797 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200798 * @param[in,out] data Data to read from, always moved to currently handled character.
799 * @param[in] ext_name Extension instance substatement name (keyword).
800 * @param[in] ext_name_len Extension instance substatement name length.
801 * @param[in] insubstmt Type of the keyword this extension instance is a substatement of.
802 * @param[in] insubstmt_index Index of the keyword instance this extension instance is a substatement of.
803 * @param[in,out] exts Extension instances to add to.
804 *
805 * @return LY_ERR values.
806 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200807static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200808parse_ext(struct lys_parser_ctx *ctx, const char **data, const char *ext_name, int ext_name_len, LYEXT_SUBSTMT insubstmt,
Michal Vasko7fbc8162018-09-17 10:35:16 +0200809 uint32_t insubstmt_index, struct lysp_ext_instance **exts)
810{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100811 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200812 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200813 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200814 struct lysp_ext_instance *e;
815 enum yang_keyword kw;
816
Radek Krejci2c4e7172018-10-19 15:56:26 +0200817 LY_ARRAY_NEW_RET(ctx->ctx, *exts, e, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200818
819 /* store name and insubstmt info */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200820 e->name = lydict_insert(ctx->ctx, ext_name, ext_name_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200821 e->insubstmt = insubstmt;
822 e->insubstmt_index = insubstmt_index;
823
824 /* get optional argument */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200825 LY_CHECK_RET(get_argument(ctx, data, Y_MAYBE_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200826
Radek Krejci0ae092d2018-09-20 16:43:19 +0200827 if (word) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200828 INSERT_WORD(ctx, buf, e->argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200829 }
830
Radek Krejci6d6556c2018-11-08 09:37:45 +0100831 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100832 LY_CHECK_RET(parse_ext_substmt(ctx, data, word, word_len, &e->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200833 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200834 return ret;
835}
836
Michal Vaskoea5abea2018-09-18 13:10:54 +0200837/**
838 * @brief Parse a generic text field without specific constraints. Those are contact, organization,
839 * description, etc...
840 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200841 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200842 * @param[in,out] data Data to read from, always moved to currently handled character.
843 * @param[in] substmt Type of this substatement.
844 * @param[in] substmt_index Index of this substatement.
845 * @param[in,out] value Place to store the parsed value.
846 * @param[in] arg Type of the YANG keyword argument (of the value).
847 * @param[in,out] exts Extension instances to add to.
848 *
849 * @return LY_ERR values.
850 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200851static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200852parse_text_field(struct lys_parser_ctx *ctx, const char **data, LYEXT_SUBSTMT substmt, uint32_t substmt_index,
Michal Vasko7fbc8162018-09-17 10:35:16 +0200853 const char **value, enum yang_arg arg, struct lysp_ext_instance **exts)
854{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100855 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200856 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200857 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200858 enum yang_keyword kw;
859
860 if (*value) {
David Sedlákb3077192019-06-19 10:55:37 +0200861 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200862 return LY_EVALID;
863 }
864
865 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200866 LY_CHECK_RET(get_argument(ctx, data, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200867
868 /* store value and spend buf if allocated */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200869 INSERT_WORD(ctx, buf, *value, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200870
Radek Krejci6d6556c2018-11-08 09:37:45 +0100871 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200872 switch (kw) {
873 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100874 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, substmt, substmt_index, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200875 break;
876 default:
David Sedlákb3077192019-06-19 10:55:37 +0200877 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200878 return LY_EVALID;
879 }
880 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200881 return ret;
882}
883
Michal Vaskoea5abea2018-09-18 13:10:54 +0200884/**
885 * @brief Parse the yang-version statement.
886 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200887 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200888 * @param[in,out] data Data to read from, always moved to currently handled character.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100889 * @param[out] version Storage for the parsed information.
890 * @param[in, out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200891 *
892 * @return LY_ERR values.
893 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200894static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200895parse_yangversion(struct lys_parser_ctx *ctx, const char **data, uint8_t *version, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200896{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100897 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200898 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200899 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200900 enum yang_keyword kw;
901
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100902 if (*version) {
David Sedlákb3077192019-06-19 10:55:37 +0200903 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200904 return LY_EVALID;
905 }
906
907 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200908 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200909
910 if ((word_len == 3) && !strncmp(word, "1.0", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100911 *version = LYS_VERSION_1_0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200912 } else if ((word_len == 3) && !strncmp(word, "1.1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100913 *version = LYS_VERSION_1_1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200914 } else {
David Sedlákb3077192019-06-19 10:55:37 +0200915 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200916 free(buf);
917 return LY_EVALID;
918 }
919 free(buf);
920
Radek Krejci6d6556c2018-11-08 09:37:45 +0100921 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200922 switch (kw) {
923 case YANG_CUSTOM:
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100924 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_VERSION, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200925 break;
926 default:
David Sedlákb3077192019-06-19 10:55:37 +0200927 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200928 return LY_EVALID;
929 }
930 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200931 return ret;
932}
933
Michal Vaskoea5abea2018-09-18 13:10:54 +0200934/**
935 * @brief Parse the belongs-to statement.
936 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200937 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200938 * @param[in,out] data Data to read from, always moved to currently handled character.
939 * @param[in,out] belongsto Place to store the parsed value.
940 * @param[in,out] prefix Place to store the parsed belongs-to prefix value.
941 * @param[in,out] exts Extension instances to add to.
942 *
943 * @return LY_ERR values.
944 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200945static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200946parse_belongsto(struct lys_parser_ctx *ctx, const char **data, const char **belongsto, const char **prefix, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200947{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100948 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200949 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200950 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200951 enum yang_keyword kw;
952
953 if (*belongsto) {
David Sedlákb3077192019-06-19 10:55:37 +0200954 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200955 return LY_EVALID;
956 }
957
958 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200959 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200960
Radek Krejci44ceedc2018-10-02 15:54:31 +0200961 INSERT_WORD(ctx, buf, *belongsto, word, word_len);
Radek Krejcif09e4e82019-06-14 15:08:11 +0200962
Radek Krejci6d6556c2018-11-08 09:37:45 +0100963 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200964 switch (kw) {
965 case YANG_PREFIX:
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100966 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_PREFIX, 0, prefix, Y_IDENTIF_ARG, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200967 break;
968 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100969 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_BELONGSTO, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200970 break;
971 default:
David Sedlákb3077192019-06-19 10:55:37 +0200972 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200973 return LY_EVALID;
974 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200975 }
Radek Krejcic59bc972018-09-17 16:13:06 +0200976 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +0100977checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200978 /* mandatory substatements */
979 if (!*prefix) {
David Sedlákb3077192019-06-19 10:55:37 +0200980 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200981 return LY_EVALID;
982 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200983 return ret;
984}
985
Michal Vaskoea5abea2018-09-18 13:10:54 +0200986/**
987 * @brief Parse the revision-date statement.
988 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200989 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200990 * @param[in,out] data Data to read from, always moved to currently handled character.
991 * @param[in,out] rev Array to store the parsed value in.
992 * @param[in,out] exts Extension instances to add to.
993 *
994 * @return LY_ERR values.
995 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200996static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200997parse_revisiondate(struct lys_parser_ctx *ctx, const char **data, char *rev, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200998{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100999 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001000 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001001 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001002 enum yang_keyword kw;
1003
1004 if (rev[0]) {
David Sedlákb3077192019-06-19 10:55:37 +02001005 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001006 return LY_EVALID;
1007 }
1008
1009 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001010 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001011
1012 /* check value */
Radek Krejcibbe09a92018-11-08 09:36:54 +01001013 if (lysp_check_date(ctx, word, word_len, "revision-date")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001014 free(buf);
1015 return LY_EVALID;
1016 }
1017
1018 /* store value and spend buf if allocated */
1019 strncpy(rev, word, word_len);
1020 free(buf);
1021
Radek Krejci6d6556c2018-11-08 09:37:45 +01001022 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001023 switch (kw) {
1024 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001025 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_REVISIONDATE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001026 break;
1027 default:
David Sedlákb3077192019-06-19 10:55:37 +02001028 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001029 return LY_EVALID;
1030 }
1031 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001032 return ret;
1033}
1034
Michal Vaskoea5abea2018-09-18 13:10:54 +02001035/**
1036 * @brief Parse the include statement.
1037 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001038 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001039 * @param[in] module_name Name of the module to check name collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001040 * @param[in,out] data Data to read from, always moved to currently handled character.
1041 * @param[in,out] includes Parsed includes to add to.
1042 *
1043 * @return LY_ERR values.
1044 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001045static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001046parse_include(struct lys_parser_ctx *ctx, const char *module_name, const char **data, struct lysp_include **includes)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001047{
Radek Krejcid33273d2018-10-25 14:55:52 +02001048 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001049 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001050 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001051 enum yang_keyword kw;
1052 struct lysp_include *inc;
1053
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001054 LY_ARRAY_NEW_RET(ctx->ctx, *includes, inc, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001055
1056 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001057 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001058
Radek Krejci086c7132018-10-26 15:29:04 +02001059 INSERT_WORD(ctx, buf, inc->name, word, word_len);
1060
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001061 /* submodules share the namespace with the module names, so there must not be
1062 * a module of the same name in the context, no need for revision matching */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001063 if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(ctx->ctx, inc->name)) {
David Sedlákb3077192019-06-19 10:55:37 +02001064 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Name collision between module and submodule of name \"%s\".", inc->name);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001065 return LY_EVALID;
1066 }
1067
Radek Krejci6d6556c2018-11-08 09:37:45 +01001068 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001069 switch (kw) {
1070 case YANG_DESCRIPTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01001071 YANG_CHECK_STMTVER2_RET(ctx, "description", "include");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001072 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &inc->dsc, Y_STR_ARG, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001073 break;
1074 case YANG_REFERENCE:
Radek Krejciceaf2122019-01-02 15:03:26 +01001075 YANG_CHECK_STMTVER2_RET(ctx, "reference", "include");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001076 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &inc->ref, Y_STR_ARG, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001077 break;
1078 case YANG_REVISION_DATE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001079 LY_CHECK_RET(parse_revisiondate(ctx, data, inc->rev, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001080 break;
1081 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001082 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001083 break;
1084 default:
David Sedlákb3077192019-06-19 10:55:37 +02001085 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001086 return LY_EVALID;
1087 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001088 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001089 return ret;
1090}
1091
Michal Vaskoea5abea2018-09-18 13:10:54 +02001092/**
1093 * @brief Parse the import statement.
1094 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001095 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001096 * @param[in] module_prefix Prefix of the module to check prefix collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001097 * @param[in,out] data Data to read from, always moved to currently handled character.
1098 * @param[in,out] imports Parsed imports to add to.
1099 *
1100 * @return LY_ERR values.
1101 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001102static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001103parse_import(struct lys_parser_ctx *ctx, const char *module_prefix, const char **data, struct lysp_import **imports)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001104{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001105 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001106 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001107 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001108 enum yang_keyword kw;
1109 struct lysp_import *imp;
1110
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001111 LY_ARRAY_NEW_RET(ctx->ctx, *imports, imp, LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001112
1113 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001114 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02001115 INSERT_WORD(ctx, buf, imp->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001116
Radek Krejci6d6556c2018-11-08 09:37:45 +01001117 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001118 switch (kw) {
1119 case YANG_PREFIX:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001120 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_PREFIX, 0, &imp->prefix, Y_IDENTIF_ARG, &imp->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001121 LY_CHECK_RET(lysp_check_prefix(ctx, *imports, module_prefix, &imp->prefix), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001122 break;
1123 case YANG_DESCRIPTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01001124 YANG_CHECK_STMTVER2_RET(ctx, "description", "import");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001125 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &imp->dsc, Y_STR_ARG, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001126 break;
1127 case YANG_REFERENCE:
Radek Krejciceaf2122019-01-02 15:03:26 +01001128 YANG_CHECK_STMTVER2_RET(ctx, "reference", "import");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001129 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &imp->ref, Y_STR_ARG, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001130 break;
1131 case YANG_REVISION_DATE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001132 LY_CHECK_RET(parse_revisiondate(ctx, data, imp->rev, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001133 break;
1134 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001135 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001136 break;
1137 default:
David Sedlákb3077192019-06-19 10:55:37 +02001138 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001139 return LY_EVALID;
1140 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001141 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001142 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001143checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001144 /* mandatory substatements */
David Sedlákb3077192019-06-19 10:55:37 +02001145 LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001146
1147 return ret;
1148}
1149
Michal Vaskoea5abea2018-09-18 13:10:54 +02001150/**
1151 * @brief Parse the revision statement.
1152 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001153 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001154 * @param[in,out] data Data to read from, always moved to currently handled character.
1155 * @param[in,out] revs Parsed revisions to add to.
1156 *
1157 * @return LY_ERR values.
1158 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001159static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001160parse_revision(struct lys_parser_ctx *ctx, const char **data, struct lysp_revision **revs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001161{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001162 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001163 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001164 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001165 enum yang_keyword kw;
1166 struct lysp_revision *rev;
1167
Radek Krejci2c4e7172018-10-19 15:56:26 +02001168 LY_ARRAY_NEW_RET(ctx->ctx, *revs, rev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001169
1170 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001171 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001172
1173 /* check value */
Radek Krejcibbe09a92018-11-08 09:36:54 +01001174 if (lysp_check_date(ctx, word, word_len, "revision")) {
David Sedlák68ef3dc2019-07-22 13:40:19 +02001175 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001176 return LY_EVALID;
1177 }
1178
Radek Krejcib7db73a2018-10-24 14:18:40 +02001179 strncpy(rev->date, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001180 free(buf);
1181
Radek Krejci6d6556c2018-11-08 09:37:45 +01001182 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001183 switch (kw) {
1184 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001185 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &rev->dsc, Y_STR_ARG, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001186 break;
1187 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001188 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &rev->ref, Y_STR_ARG, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001189 break;
1190 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001191 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001192 break;
1193 default:
David Sedlákb3077192019-06-19 10:55:37 +02001194 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001195 return LY_EVALID;
1196 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001197 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001198 return ret;
1199}
1200
Michal Vaskoea5abea2018-09-18 13:10:54 +02001201/**
1202 * @brief Parse a generic text field that can have more instances such as base.
1203 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001204 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001205 * @param[in,out] data Data to read from, always moved to currently handled character.
1206 * @param[in] substmt Type of this substatement.
1207 * @param[in,out] texts Parsed values to add to.
1208 * @param[in] arg Type of the expected argument.
1209 * @param[in,out] exts Extension instances to add to.
1210 *
1211 * @return LY_ERR values.
1212 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001213static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001214parse_text_fields(struct lys_parser_ctx *ctx, const char **data, LYEXT_SUBSTMT substmt, const char ***texts, enum yang_arg arg,
Michal Vasko7fbc8162018-09-17 10:35:16 +02001215 struct lysp_ext_instance **exts)
1216{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001217 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001218 char *buf, *word;
Radek Krejci151a5b72018-10-19 14:21:44 +02001219 const char **item;
1220 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001221 enum yang_keyword kw;
1222
1223 /* allocate new pointer */
Radek Krejci2c4e7172018-10-19 15:56:26 +02001224 LY_ARRAY_NEW_RET(ctx->ctx, *texts, item, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001225
1226 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001227 LY_CHECK_RET(get_argument(ctx, data, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001228
Radek Krejci151a5b72018-10-19 14:21:44 +02001229 INSERT_WORD(ctx, buf, *item, word, word_len);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001230 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001231 switch (kw) {
1232 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001233 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, substmt, LY_ARRAY_SIZE(*texts) - 1, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001234 break;
1235 default:
David Sedlákb3077192019-06-19 10:55:37 +02001236 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001237 return LY_EVALID;
1238 }
1239 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001240 return ret;
1241}
1242
Michal Vaskoea5abea2018-09-18 13:10:54 +02001243/**
1244 * @brief Parse the config statement.
1245 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001246 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001247 * @param[in,out] data Data to read from, always moved to currently handled character.
1248 * @param[in,out] flags Flags to add to.
1249 * @param[in,out] exts Extension instances to add to.
1250 *
1251 * @return LY_ERR values.
1252 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001253static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001254parse_config(struct lys_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001255{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001256 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001257 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001258 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001259 enum yang_keyword kw;
1260
1261 if (*flags & LYS_CONFIG_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001262 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001263 return LY_EVALID;
1264 }
1265
1266 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001267 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001268
1269 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1270 *flags |= LYS_CONFIG_W;
1271 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1272 *flags |= LYS_CONFIG_R;
1273 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001274 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001275 free(buf);
1276 return LY_EVALID;
1277 }
1278 free(buf);
1279
Radek Krejci6d6556c2018-11-08 09:37:45 +01001280 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001281 switch (kw) {
1282 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001283 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_CONFIG, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001284 break;
1285 default:
David Sedlákb3077192019-06-19 10:55:37 +02001286 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001287 return LY_EVALID;
1288 }
1289 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001290 return ret;
1291}
1292
Michal Vaskoea5abea2018-09-18 13:10:54 +02001293/**
1294 * @brief Parse the mandatory statement.
1295 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001296 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001297 * @param[in,out] data Data to read from, always moved to currently handled character.
1298 * @param[in,out] flags Flags to add to.
1299 * @param[in,out] exts Extension instances to add to.
1300 *
1301 * @return LY_ERR values.
1302 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001303static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001304parse_mandatory(struct lys_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001305{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001306 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001307 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001308 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001309 enum yang_keyword kw;
1310
1311 if (*flags & LYS_MAND_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001312 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001313 return LY_EVALID;
1314 }
1315
1316 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001317 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001318
1319 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1320 *flags |= LYS_MAND_TRUE;
1321 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1322 *flags |= LYS_MAND_FALSE;
1323 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001324 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001325 free(buf);
1326 return LY_EVALID;
1327 }
1328 free(buf);
1329
Radek Krejci6d6556c2018-11-08 09:37:45 +01001330 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001331 switch (kw) {
1332 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001333 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MANDATORY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001334 break;
1335 default:
David Sedlákb3077192019-06-19 10:55:37 +02001336 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001337 return LY_EVALID;
1338 }
1339 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001340 return ret;
1341}
1342
Michal Vaskoea5abea2018-09-18 13:10:54 +02001343/**
1344 * @brief Parse a restriction such as range or length.
1345 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001346 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001347 * @param[in,out] data Data to read from, always moved to currently handled character.
1348 * @param[in] restr_kw Type of this particular restriction.
1349 * @param[in,out] exts Extension instances to add to.
1350 *
1351 * @return LY_ERR values.
1352 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001353static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001354parse_restr(struct lys_parser_ctx *ctx, const char **data, enum yang_keyword restr_kw, struct lysp_restr *restr)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001355{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001356 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001357 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001358 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001359 enum yang_keyword kw;
1360
1361 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001362 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001363
David Sedlákb9b892c2019-07-12 14:44:02 +02001364 YANG_CHECK_NONEMPTY(ctx, word_len, ly_stmt2str(restr_kw));
Radek Krejci44ceedc2018-10-02 15:54:31 +02001365 INSERT_WORD(ctx, buf, restr->arg, word, word_len);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001366 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001367 switch (kw) {
1368 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001369 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001370 break;
1371 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001372 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001373 break;
1374 case YANG_ERROR_APP_TAG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001375 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRTAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001376 break;
1377 case YANG_ERROR_MESSAGE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001378 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRMSG, 0, &restr->emsg, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001379 break;
1380 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001381 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001382 break;
1383 default:
David Sedlákb3077192019-06-19 10:55:37 +02001384 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001385 return LY_EVALID;
1386 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001387 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001388 return ret;
1389}
1390
Michal Vaskoea5abea2018-09-18 13:10:54 +02001391/**
1392 * @brief Parse a restriction that can have more instances such as must.
1393 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001394 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001395 * @param[in,out] data Data to read from, always moved to currently handled character.
1396 * @param[in] restr_kw Type of this particular restriction.
1397 * @param[in,out] restrs Restrictions to add to.
1398 *
1399 * @return LY_ERR values.
1400 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001401static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001402parse_restrs(struct lys_parser_ctx *ctx, const char **data, enum yang_keyword restr_kw, struct lysp_restr **restrs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001403{
1404 struct lysp_restr *restr;
1405
Radek Krejci2c4e7172018-10-19 15:56:26 +02001406 LY_ARRAY_NEW_RET(ctx->ctx, *restrs, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001407 return parse_restr(ctx, data, restr_kw, restr);
1408}
1409
Michal Vaskoea5abea2018-09-18 13:10:54 +02001410/**
1411 * @brief Parse the status statement.
1412 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001413 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001414 * @param[in,out] data Data to read from, always moved to currently handled character.
1415 * @param[in,out] flags Flags to add to.
1416 * @param[in,out] exts Extension instances to add to.
1417 *
1418 * @return LY_ERR values.
1419 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001420static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001421parse_status(struct lys_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001422{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001423 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001424 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001425 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001426 enum yang_keyword kw;
1427
1428 if (*flags & LYS_STATUS_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001429 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001430 return LY_EVALID;
1431 }
1432
1433 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001434 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001435
1436 if ((word_len == 7) && !strncmp(word, "current", word_len)) {
1437 *flags |= LYS_STATUS_CURR;
1438 } else if ((word_len == 10) && !strncmp(word, "deprecated", word_len)) {
1439 *flags |= LYS_STATUS_DEPRC;
1440 } else if ((word_len == 8) && !strncmp(word, "obsolete", word_len)) {
1441 *flags |= LYS_STATUS_OBSLT;
1442 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001443 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001444 free(buf);
1445 return LY_EVALID;
1446 }
1447 free(buf);
1448
Radek Krejci6d6556c2018-11-08 09:37:45 +01001449 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001450 switch (kw) {
1451 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001452 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_STATUS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001453 break;
1454 default:
David Sedlákb3077192019-06-19 10:55:37 +02001455 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001456 return LY_EVALID;
1457 }
1458 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001459 return ret;
1460}
1461
Michal Vaskoea5abea2018-09-18 13:10:54 +02001462/**
1463 * @brief Parse the when statement.
1464 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001465 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001466 * @param[in,out] data Data to read from, always moved to currently handled character.
1467 * @param[in,out] when_p When pointer to parse to.
1468 *
1469 * @return LY_ERR values.
1470 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001471LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001472parse_when(struct lys_parser_ctx *ctx, const char **data, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001473{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001474 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001475 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001476 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001477 enum yang_keyword kw;
1478 struct lysp_when *when;
1479
1480 if (*when_p) {
David Sedlákb3077192019-06-19 10:55:37 +02001481 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001482 return LY_EVALID;
1483 }
1484
1485 when = calloc(1, sizeof *when);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001486 LY_CHECK_ERR_RET(!when, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001487
1488 /* get value */
Radek Krejci2f54df52019-06-21 10:59:19 +02001489 LY_CHECK_ERR_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len), free(when), LY_EMEM);
David Sedlákb9b892c2019-07-12 14:44:02 +02001490 YANG_CHECK_NONEMPTY(ctx, word_len, "when");
Radek Krejci44ceedc2018-10-02 15:54:31 +02001491 INSERT_WORD(ctx, buf, when->cond, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001492
Radek Krejcif09e4e82019-06-14 15:08:11 +02001493 *when_p = when;
1494
Radek Krejci6d6556c2018-11-08 09:37:45 +01001495 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001496 switch (kw) {
1497 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001498 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &when->dsc, Y_STR_ARG, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001499 break;
1500 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001501 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &when->ref, Y_STR_ARG, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001502 break;
1503 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001504 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001505 break;
1506 default:
David Sedlákb3077192019-06-19 10:55:37 +02001507 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001508 return LY_EVALID;
1509 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001510 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001511 return ret;
1512}
1513
Michal Vaskoea5abea2018-09-18 13:10:54 +02001514/**
1515 * @brief Parse the anydata or anyxml statement.
1516 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001517 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001518 * @param[in,out] data Data to read from, always moved to currently handled character.
1519 * @param[in] kw Type of this particular keyword.
1520 * @param[in,out] siblings Siblings to add to.
1521 *
1522 * @return LY_ERR values.
1523 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02001524LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001525parse_any(struct lys_parser_ctx *ctx, const char **data, enum yang_keyword kw, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001526{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001527 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001528 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001529 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001530 struct lysp_node *iter;
1531 struct lysp_node_anydata *any;
1532
1533 /* create structure */
1534 any = calloc(1, sizeof *any);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001535 LY_CHECK_ERR_RET(!any, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001536 any->nodetype = kw == YANG_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001537 any->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001538
1539 /* insert into siblings */
1540 if (!*siblings) {
1541 *siblings = (struct lysp_node *)any;
1542 } else {
1543 for (iter = *siblings; iter->next; iter = iter->next);
1544 iter->next = (struct lysp_node *)any;
1545 }
1546
1547 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001548 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02001549 INSERT_WORD(ctx, buf, any->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001550
1551 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01001552 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001553 switch (kw) {
1554 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001555 LY_CHECK_RET(parse_config(ctx, data, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001556 break;
1557 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001558 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &any->dsc, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001559 break;
1560 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001561 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &any->iffeatures, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001562 break;
1563 case YANG_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001564 LY_CHECK_RET(parse_mandatory(ctx, data, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001565 break;
1566 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001567 LY_CHECK_RET(parse_restrs(ctx, data, kw, &any->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001568 break;
1569 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001570 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &any->ref, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001571 break;
1572 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001573 LY_CHECK_RET(parse_status(ctx, data, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001574 break;
1575 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001576 LY_CHECK_RET(parse_when(ctx, data, &any->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001577 break;
1578 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001579 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001580 break;
1581 default:
David Sedlákb3077192019-06-19 10:55:37 +02001582 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
Radek Krejcic59bc972018-09-17 16:13:06 +02001583 (any->nodetype & LYS_ANYDATA) == LYS_ANYDATA ? ly_stmt2str(YANG_ANYDATA) : ly_stmt2str(YANG_ANYXML));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001584 return LY_EVALID;
1585 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001586 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001587 return ret;
1588}
1589
Michal Vaskoea5abea2018-09-18 13:10:54 +02001590/**
1591 * @brief Parse the value or position statement. Substatement of type enum statement.
1592 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001593 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001594 * @param[in,out] data Data to read from, always moved to currently handled character.
1595 * @param[in] val_kw Type of this particular keyword.
1596 * @param[in,out] value Value to write to.
1597 * @param[in,out] flags Flags to write to.
1598 * @param[in,out] exts Extension instances to add to.
1599 *
1600 * @return LY_ERR values.
1601 */
David Sedlákd6ce6d72019-07-16 17:30:18 +02001602LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001603parse_type_enum_value_pos(struct lys_parser_ctx *ctx, const char **data, enum yang_keyword val_kw, int64_t *value, uint16_t *flags,
Michal Vasko7fbc8162018-09-17 10:35:16 +02001604 struct lysp_ext_instance **exts)
1605{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001606 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001607 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001608 size_t word_len;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02001609 long int num = 0;
1610 unsigned long int unum = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001611 enum yang_keyword kw;
1612
1613 if (*flags & LYS_SET_VALUE) {
David Sedlákb3077192019-06-19 10:55:37 +02001614 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001615 return LY_EVALID;
1616 }
1617 *flags |= LYS_SET_VALUE;
1618
1619 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001620 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001621
David Sedlákd6ce6d72019-07-16 17:30:18 +02001622 if (!word_len || (word[0] == '+') || ((word[0] == '0') && (word_len > 1)) || ((val_kw == YANG_POSITION) && !strncmp(word, "-0", 2))) {
David Sedlákb3077192019-06-19 10:55:37 +02001623 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001624 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001625 }
1626
1627 errno = 0;
1628 if (val_kw == YANG_VALUE) {
1629 num = strtol(word, &ptr, 10);
Radek Krejci8b764662018-11-14 14:15:13 +01001630 if (num < INT64_C(-2147483648) || num > INT64_C(2147483647)) {
David Sedlákb3077192019-06-19 10:55:37 +02001631 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001632 goto error;
1633 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001634 } else {
1635 unum = strtoul(word, &ptr, 10);
Radek Krejci8b764662018-11-14 14:15:13 +01001636 if (unum > UINT64_C(4294967295)) {
David Sedlákb3077192019-06-19 10:55:37 +02001637 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001638 goto error;
1639 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001640 }
1641 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001642 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001643 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001644 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001645 }
1646 if (errno == ERANGE) {
David Sedlákb3077192019-06-19 10:55:37 +02001647 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001648 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001649 }
1650 if (val_kw == YANG_VALUE) {
1651 *value = num;
1652 } else {
1653 *value = unum;
1654 }
1655 free(buf);
1656
Radek Krejci6d6556c2018-11-08 09:37:45 +01001657 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001658 switch (kw) {
1659 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001660 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, val_kw == YANG_VALUE ? LYEXT_SUBSTMT_VALUE : LYEXT_SUBSTMT_POSITION, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001661 break;
1662 default:
David Sedlákb3077192019-06-19 10:55:37 +02001663 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001664 return LY_EVALID;
1665 }
1666 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001667 return ret;
Radek Krejci8b764662018-11-14 14:15:13 +01001668
1669error:
1670 free(buf);
1671 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001672}
1673
Michal Vaskoea5abea2018-09-18 13:10:54 +02001674/**
1675 * @brief Parse the enum or bit statement. Substatement of type statement.
1676 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001677 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001678 * @param[in,out] data Data to read from, always moved to currently handled character.
1679 * @param[in] enum_kw Type of this particular keyword.
1680 * @param[in,out] enums Enums or bits to add to.
1681 *
1682 * @return LY_ERR values.
1683 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001684static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001685parse_type_enum(struct lys_parser_ctx *ctx, const char **data, enum yang_keyword enum_kw, struct lysp_type_enum **enums)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001686{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001687 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001688 char *buf, *word;
David Sedlák6544c182019-07-12 13:17:33 +02001689 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001690 enum yang_keyword kw;
1691 struct lysp_type_enum *enm;
1692
Radek Krejci2c4e7172018-10-19 15:56:26 +02001693 LY_ARRAY_NEW_RET(ctx->ctx, *enums, enm, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001694
1695 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001696 LY_CHECK_RET(get_argument(ctx, data, enum_kw == YANG_ENUM ? Y_STR_ARG : Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci8b764662018-11-14 14:15:13 +01001697 if (enum_kw == YANG_ENUM) {
David Sedlák07869a52019-07-12 14:28:19 +02001698 ret = lysp_check_enum_name(ctx, (const char *)word, word_len);
David Sedlák6544c182019-07-12 13:17:33 +02001699 LY_CHECK_ERR_RET(ret, free(buf), ret);
Radek Krejci8b764662018-11-14 14:15:13 +01001700 } else { /* YANG_BIT */
1701
1702 }
Radek Krejcif09e4e82019-06-14 15:08:11 +02001703 if (enum_kw == YANG_ENUM) {
David Sedlákb9b892c2019-07-12 14:44:02 +02001704 YANG_CHECK_NONEMPTY(ctx, word_len, "enum");
Radek Krejcif09e4e82019-06-14 15:08:11 +02001705 }
Radek Krejci44ceedc2018-10-02 15:54:31 +02001706 INSERT_WORD(ctx, buf, enm->name, word, word_len);
Radek Krejci8b764662018-11-14 14:15:13 +01001707 CHECK_UNIQUENESS(ctx, *enums, name, ly_stmt2str(enum_kw), enm->name);
1708
Radek Krejci6d6556c2018-11-08 09:37:45 +01001709 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001710 switch (kw) {
1711 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001712 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &enm->dsc, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001713 break;
1714 case YANG_IF_FEATURE:
Radek Krejciceaf2122019-01-02 15:03:26 +01001715 YANG_CHECK_STMTVER2_RET(ctx, "if-feature", ly_stmt2str(enum_kw));
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001716 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &enm->iffeatures, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001717 break;
1718 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001719 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &enm->ref, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001720 break;
1721 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001722 LY_CHECK_RET(parse_status(ctx, data, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001723 break;
1724 case YANG_VALUE:
David Sedlák9fb515f2019-07-11 10:33:58 +02001725 LY_CHECK_ERR_RET(enum_kw == YANG_BIT, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
1726 ly_stmt2str(enum_kw)), LY_EVALID);
1727 LY_CHECK_RET(parse_type_enum_value_pos(ctx, data, kw, &enm->value, &enm->flags, &enm->exts));
1728 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001729 case YANG_POSITION:
David Sedlák9fb515f2019-07-11 10:33:58 +02001730 LY_CHECK_ERR_RET(enum_kw == YANG_ENUM, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
1731 ly_stmt2str(enum_kw)), LY_EVALID);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001732 LY_CHECK_RET(parse_type_enum_value_pos(ctx, data, kw, &enm->value, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001733 break;
1734 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001735 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001736 break;
1737 default:
David Sedlákb3077192019-06-19 10:55:37 +02001738 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001739 return LY_EVALID;
1740 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001741 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001742 return ret;
1743}
1744
Michal Vaskoea5abea2018-09-18 13:10:54 +02001745/**
1746 * @brief Parse the fraction-digits statement. Substatement of type statement.
1747 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001748 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001749 * @param[in,out] data Data to read from, always moved to currently handled character.
1750 * @param[in,out] fracdig Value to write to.
1751 * @param[in,out] exts Extension instances to add to.
1752 *
1753 * @return LY_ERR values.
1754 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001755static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001756parse_type_fracdigits(struct lys_parser_ctx *ctx, const char **data, uint8_t *fracdig, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001757{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001758 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001759 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001760 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001761 unsigned long int num;
1762 enum yang_keyword kw;
1763
1764 if (*fracdig) {
David Sedlákb3077192019-06-19 10:55:37 +02001765 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001766 return LY_EVALID;
1767 }
1768
1769 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001770 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001771
1772 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
David Sedlákb3077192019-06-19 10:55:37 +02001773 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001774 free(buf);
1775 return LY_EVALID;
1776 }
1777
1778 errno = 0;
1779 num = strtoul(word, &ptr, 10);
1780 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001781 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001782 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001783 free(buf);
1784 return LY_EVALID;
1785 }
1786 if ((errno == ERANGE) || (num > 18)) {
David Sedlákb3077192019-06-19 10:55:37 +02001787 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001788 free(buf);
1789 return LY_EVALID;
1790 }
1791 *fracdig = num;
1792 free(buf);
1793
Radek Krejci6d6556c2018-11-08 09:37:45 +01001794 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001795 switch (kw) {
1796 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001797 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_FRACDIGITS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001798 break;
1799 default:
David Sedlákb3077192019-06-19 10:55:37 +02001800 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001801 return LY_EVALID;
1802 }
1803 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001804 return ret;
1805}
1806
Michal Vaskoea5abea2018-09-18 13:10:54 +02001807/**
1808 * @brief Parse the require-instance statement. Substatement of type statement.
1809 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001810 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001811 * @param[in,out] data Data to read from, always moved to currently handled character.
1812 * @param[in,out] reqinst Value to write to.
1813 * @param[in,out] flags Flags to write to.
1814 * @param[in,out] exts Extension instances to add to.
1815 *
1816 * @return LY_ERR values.
1817 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001818static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001819parse_type_reqinstance(struct lys_parser_ctx *ctx, const char **data, uint8_t *reqinst, uint16_t *flags,
Michal Vasko7fbc8162018-09-17 10:35:16 +02001820 struct lysp_ext_instance **exts)
1821{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001822 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001823 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001824 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001825 enum yang_keyword kw;
1826
1827 if (*flags & LYS_SET_REQINST) {
David Sedlákb3077192019-06-19 10:55:37 +02001828 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001829 return LY_EVALID;
1830 }
1831 *flags |= LYS_SET_REQINST;
1832
1833 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001834 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001835
1836 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1837 *reqinst = 1;
1838 } else if ((word_len != 5) || strncmp(word, "false", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001839 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001840 free(buf);
1841 return LY_EVALID;
1842 }
1843 free(buf);
1844
Radek Krejci6d6556c2018-11-08 09:37:45 +01001845 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001846 switch (kw) {
1847 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001848 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_REQINSTANCE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001849 break;
1850 default:
David Sedlákb3077192019-06-19 10:55:37 +02001851 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001852 return LY_EVALID;
1853 }
1854 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001855 return ret;
1856}
1857
Michal Vaskoea5abea2018-09-18 13:10:54 +02001858/**
1859 * @brief Parse the modifier statement. Substatement of type pattern statement.
1860 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001861 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001862 * @param[in,out] data Data to read from, always moved to currently handled character.
1863 * @param[in,out] pat Value to write to.
1864 * @param[in,out] exts Extension instances to add to.
1865 *
1866 * @return LY_ERR values.
1867 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001868static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001869parse_type_pattern_modifier(struct lys_parser_ctx *ctx, const char **data, const char **pat, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001870{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001871 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001872 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001873 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001874 enum yang_keyword kw;
1875
1876 if ((*pat)[0] == 0x15) {
David Sedlákb3077192019-06-19 10:55:37 +02001877 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001878 return LY_EVALID;
1879 }
1880
1881 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001882 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001883
1884 if ((word_len != 12) || strncmp(word, "invert-match", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001885 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001886 free(buf);
1887 return LY_EVALID;
1888 }
1889 free(buf);
1890
1891 /* replace the value in the dictionary */
1892 buf = malloc(strlen(*pat) + 1);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001893 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001894 strcpy(buf, *pat);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001895 lydict_remove(ctx->ctx, *pat);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001896
1897 assert(buf[0] == 0x06);
1898 buf[0] = 0x15;
Radek Krejci44ceedc2018-10-02 15:54:31 +02001899 *pat = lydict_insert_zc(ctx->ctx, buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001900
Radek Krejci6d6556c2018-11-08 09:37:45 +01001901 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001902 switch (kw) {
1903 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001904 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MODIFIER, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001905 break;
1906 default:
David Sedlákb3077192019-06-19 10:55:37 +02001907 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001908 return LY_EVALID;
1909 }
1910 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001911 return ret;
1912}
1913
Michal Vaskoea5abea2018-09-18 13:10:54 +02001914/**
1915 * @brief Parse the pattern statement. Substatement of type statement.
1916 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001917 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001918 * @param[in,out] data Data to read from, always moved to currently handled character.
1919 * @param[in,out] patterns Restrictions to add to.
1920 *
1921 * @return LY_ERR values.
1922 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001923static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001924parse_type_pattern(struct lys_parser_ctx *ctx, const char **data, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001925{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001926 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001927 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001928 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001929 enum yang_keyword kw;
1930 struct lysp_restr *restr;
1931
Radek Krejci2c4e7172018-10-19 15:56:26 +02001932 LY_ARRAY_NEW_RET(ctx->ctx, *patterns, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001933
1934 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001935 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001936
1937 /* add special meaning first byte */
1938 if (buf) {
1939 buf = realloc(buf, word_len + 2);
1940 word = buf;
1941 } else {
1942 buf = malloc(word_len + 2);
1943 }
Radek Krejci44ceedc2018-10-02 15:54:31 +02001944 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejci86d106e2018-10-18 09:53:19 +02001945 memmove(buf + 1, word, word_len);
1946 buf[0] = 0x06; /* pattern's default regular-match flag */
1947 buf[word_len + 1] = '\0'; /* terminating NULL byte */
1948 restr->arg = lydict_insert_zc(ctx->ctx, buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001949
Radek Krejci6d6556c2018-11-08 09:37:45 +01001950 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001951 switch (kw) {
1952 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001953 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001954 break;
1955 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001956 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001957 break;
1958 case YANG_ERROR_APP_TAG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001959 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRTAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001960 break;
1961 case YANG_ERROR_MESSAGE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001962 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRMSG, 0, &restr->emsg, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001963 break;
1964 case YANG_MODIFIER:
Radek Krejciceaf2122019-01-02 15:03:26 +01001965 YANG_CHECK_STMTVER2_RET(ctx, "modifier", "pattern");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001966 LY_CHECK_RET(parse_type_pattern_modifier(ctx, data, &restr->arg, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001967 break;
1968 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001969 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001970 break;
1971 default:
David Sedlákb3077192019-06-19 10:55:37 +02001972 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001973 return LY_EVALID;
1974 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001975 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001976 return ret;
1977}
1978
Michal Vaskoea5abea2018-09-18 13:10:54 +02001979/**
1980 * @brief Parse the type statement.
1981 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001982 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001983 * @param[in,out] data Data to read from, always moved to currently handled character.
1984 * @param[in,out] type Type to wrote to.
1985 *
1986 * @return LY_ERR values.
1987 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001988static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001989parse_type(struct lys_parser_ctx *ctx, const char **data, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001990{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001991 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001992 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001993 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001994 enum yang_keyword kw;
1995 struct lysp_type *nest_type;
1996
1997 if (type->name) {
David Sedlákb3077192019-06-19 10:55:37 +02001998 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001999 return LY_EVALID;
2000 }
2001
2002 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002003 LY_CHECK_RET(get_argument(ctx, data, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002004 INSERT_WORD(ctx, buf, type->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002005
Radek Krejci6d6556c2018-11-08 09:37:45 +01002006 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002007 switch (kw) {
2008 case YANG_BASE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002009 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_BASE, &type->bases, Y_PREF_IDENTIF_ARG, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002010 type->flags |= LYS_SET_BASE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002011 break;
2012 case YANG_BIT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002013 LY_CHECK_RET(parse_type_enum(ctx, data, kw, &type->bits));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002014 type->flags |= LYS_SET_BIT;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002015 break;
2016 case YANG_ENUM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002017 LY_CHECK_RET(parse_type_enum(ctx, data, kw, &type->enums));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002018 type->flags |= LYS_SET_ENUM;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002019 break;
2020 case YANG_FRACTION_DIGITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002021 LY_CHECK_RET(parse_type_fracdigits(ctx, data, &type->fraction_digits, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002022 type->flags |= LYS_SET_FRDIGITS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002023 break;
2024 case YANG_LENGTH:
2025 if (type->length) {
David Sedlákb3077192019-06-19 10:55:37 +02002026 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002027 return LY_EVALID;
2028 }
2029 type->length = calloc(1, sizeof *type->length);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002030 LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002031
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002032 LY_CHECK_RET(parse_restr(ctx, data, kw, type->length));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002033 type->flags |= LYS_SET_LENGTH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002034 break;
2035 case YANG_PATH:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002036 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_PATH, 0, &type->path, Y_STR_ARG, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002037 type->flags |= LYS_SET_PATH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002038 break;
2039 case YANG_PATTERN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002040 LY_CHECK_RET(parse_type_pattern(ctx, data, &type->patterns));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002041 type->flags |= LYS_SET_PATTERN;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002042 break;
2043 case YANG_RANGE:
2044 if (type->range) {
David Sedlákb3077192019-06-19 10:55:37 +02002045 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002046 return LY_EVALID;
2047 }
2048 type->range = calloc(1, sizeof *type->range);
David Sedlák7a8b2472019-07-11 15:08:34 +02002049 LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002050
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002051 LY_CHECK_RET(parse_restr(ctx, data, kw, type->range));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002052 type->flags |= LYS_SET_RANGE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002053 break;
2054 case YANG_REQUIRE_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002055 LY_CHECK_RET(parse_type_reqinstance(ctx, data, &type->require_instance, &type->flags, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002056 /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002057 break;
2058 case YANG_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002059 LY_ARRAY_NEW_RET(ctx->ctx, type->types, nest_type, LY_EMEM);
2060 LY_CHECK_RET(parse_type(ctx, data, nest_type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002061 type->flags |= LYS_SET_TYPE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002062 break;
2063 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002064 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &type->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002065 break;
2066 default:
David Sedlákb3077192019-06-19 10:55:37 +02002067 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002068 return LY_EVALID;
2069 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002070 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002071 return ret;
2072}
2073
Michal Vaskoea5abea2018-09-18 13:10:54 +02002074/**
2075 * @brief Parse the leaf statement.
2076 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002077 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002078 * @param[in,out] data Data to read from, always moved to currently handled character.
2079 * @param[in,out] siblings Siblings to add to.
2080 *
2081 * @return LY_ERR values.
2082 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002083LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002084parse_leaf(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002085{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002086 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002087 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002088 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002089 enum yang_keyword kw;
2090 struct lysp_node *iter;
2091 struct lysp_node_leaf *leaf;
2092
2093 /* create structure */
2094 leaf = calloc(1, sizeof *leaf);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002095 LY_CHECK_ERR_RET(!leaf, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002096 leaf->nodetype = LYS_LEAF;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002097 leaf->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002098
2099 /* insert into siblings */
2100 if (!*siblings) {
2101 *siblings = (struct lysp_node *)leaf;
2102 } else {
2103 for (iter = *siblings; iter->next; iter = iter->next);
2104 iter->next = (struct lysp_node *)leaf;
2105 }
2106
2107 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002108 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002109 INSERT_WORD(ctx, buf, leaf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002110
2111 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01002112 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002113 switch (kw) {
2114 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002115 LY_CHECK_RET(parse_config(ctx, data, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002116 break;
2117 case YANG_DEFAULT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002118 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &leaf->dflt, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002119 break;
2120 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002121 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &leaf->dsc, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002122 break;
2123 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002124 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &leaf->iffeatures, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002125 break;
2126 case YANG_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002127 LY_CHECK_RET(parse_mandatory(ctx, data, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002128 break;
2129 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002130 LY_CHECK_RET(parse_restrs(ctx, data, kw, &leaf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002131 break;
2132 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002133 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &leaf->ref, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002134 break;
2135 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002136 LY_CHECK_RET(parse_status(ctx, data, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002137 break;
2138 case YANG_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002139 LY_CHECK_RET(parse_type(ctx, data, &leaf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002140 break;
2141 case YANG_UNITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002142 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, &leaf->units, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002143 break;
2144 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002145 LY_CHECK_RET(parse_when(ctx, data, &leaf->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002146 break;
2147 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002148 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002149 break;
2150 default:
David Sedlákb3077192019-06-19 10:55:37 +02002151 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002152 return LY_EVALID;
2153 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002154 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002155 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002156checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002157 /* mandatory substatements */
2158 if (!leaf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002159 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002160 return LY_EVALID;
2161 }
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002162 if ((leaf->flags & LYS_MAND_TRUE) && (leaf->dflt)) {
David Sedlákb3077192019-06-19 10:55:37 +02002163 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMSCOMB, "mandatory", "default", "leaf");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002164 return LY_EVALID;
2165 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002166
2167 return ret;
2168}
2169
Michal Vaskoea5abea2018-09-18 13:10:54 +02002170/**
2171 * @brief Parse the max-elements statement.
2172 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002173 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002174 * @param[in,out] data Data to read from, always moved to currently handled character.
2175 * @param[in,out] max Value to write to.
2176 * @param[in,out] flags Flags to write to.
2177 * @param[in,out] exts Extension instances to add to.
2178 *
2179 * @return LY_ERR values.
2180 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002181LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002182parse_maxelements(struct lys_parser_ctx *ctx, const char **data, uint32_t *max, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002183{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002184 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002185 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002186 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002187 unsigned long int num;
2188 enum yang_keyword kw;
2189
2190 if (*flags & LYS_SET_MAX) {
David Sedlákb3077192019-06-19 10:55:37 +02002191 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002192 return LY_EVALID;
2193 }
2194 *flags |= LYS_SET_MAX;
2195
2196 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002197 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002198
2199 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
David Sedlákb3077192019-06-19 10:55:37 +02002200 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002201 free(buf);
2202 return LY_EVALID;
2203 }
2204
2205 if (strncmp(word, "unbounded", word_len)) {
2206 errno = 0;
2207 num = strtoul(word, &ptr, 10);
2208 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002209 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002210 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002211 free(buf);
2212 return LY_EVALID;
2213 }
2214 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002215 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002216 free(buf);
2217 return LY_EVALID;
2218 }
2219
2220 *max = num;
2221 }
2222 free(buf);
2223
Radek Krejci6d6556c2018-11-08 09:37:45 +01002224 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002225 switch (kw) {
2226 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002227 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MAX, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002228 break;
2229 default:
David Sedlákb3077192019-06-19 10:55:37 +02002230 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002231 return LY_EVALID;
2232 }
2233 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002234 return ret;
2235}
2236
Michal Vaskoea5abea2018-09-18 13:10:54 +02002237/**
2238 * @brief Parse the min-elements statement.
2239 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002240 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002241 * @param[in,out] data Data to read from, always moved to currently handled character.
2242 * @param[in,out] min Value to write to.
2243 * @param[in,out] flags Flags to write to.
2244 * @param[in,out] exts Extension instances to add to.
2245 *
2246 * @return LY_ERR values.
2247 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002248LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002249parse_minelements(struct lys_parser_ctx *ctx, const char **data, uint32_t *min, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002250{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002251 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002252 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002253 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002254 unsigned long int num;
2255 enum yang_keyword kw;
2256
2257 if (*flags & LYS_SET_MIN) {
David Sedlákb3077192019-06-19 10:55:37 +02002258 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002259 return LY_EVALID;
2260 }
2261 *flags |= LYS_SET_MIN;
2262
2263 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002264 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002265
2266 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
David Sedlákb3077192019-06-19 10:55:37 +02002267 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002268 free(buf);
2269 return LY_EVALID;
2270 }
2271
2272 errno = 0;
2273 num = strtoul(word, &ptr, 10);
2274 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002275 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002276 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002277 free(buf);
2278 return LY_EVALID;
2279 }
2280 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002281 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002282 free(buf);
2283 return LY_EVALID;
2284 }
2285 *min = num;
2286 free(buf);
2287
Radek Krejci6d6556c2018-11-08 09:37:45 +01002288 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002289 switch (kw) {
2290 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002291 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MIN, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002292 break;
2293 default:
David Sedlákb3077192019-06-19 10:55:37 +02002294 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002295 return LY_EVALID;
2296 }
2297 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002298 return ret;
2299}
2300
Michal Vaskoea5abea2018-09-18 13:10:54 +02002301/**
2302 * @brief Parse the ordered-by statement.
2303 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002304 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002305 * @param[in,out] data Data to read from, always moved to currently handled character.
2306 * @param[in,out] flags Flags to write to.
2307 * @param[in,out] exts Extension instances to add to.
2308 *
2309 * @return LY_ERR values.
2310 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002311static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002312parse_orderedby(struct lys_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002313{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002314 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002315 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002316 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002317 enum yang_keyword kw;
2318
2319 if (*flags & LYS_ORDBY_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02002320 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002321 return LY_EVALID;
2322 }
2323
2324 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002325 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002326
2327 if ((word_len == 6) && !strncmp(word, "system", word_len)) {
2328 *flags |= LYS_ORDBY_SYSTEM;
2329 } else if ((word_len == 4) && !strncmp(word, "user", word_len)) {
2330 *flags |= LYS_ORDBY_USER;
2331 } else {
David Sedlákb3077192019-06-19 10:55:37 +02002332 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002333 free(buf);
2334 return LY_EVALID;
2335 }
2336 free(buf);
2337
Radek Krejci6d6556c2018-11-08 09:37:45 +01002338 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002339 switch (kw) {
2340 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002341 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_ORDEREDBY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002342 break;
2343 default:
David Sedlákb3077192019-06-19 10:55:37 +02002344 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002345 return LY_EVALID;
2346 }
2347 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002348 return ret;
2349}
2350
Michal Vaskoea5abea2018-09-18 13:10:54 +02002351/**
2352 * @brief Parse the leaf-list statement.
2353 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002354 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002355 * @param[in,out] data Data to read from, always moved to currently handled character.
2356 * @param[in,out] siblings Siblings to add to.
2357 *
2358 * @return LY_ERR values.
2359 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002360LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002361parse_leaflist(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002362{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002363 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002364 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002365 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002366 enum yang_keyword kw;
2367 struct lysp_node *iter;
2368 struct lysp_node_leaflist *llist;
2369
2370 /* create structure */
2371 llist = calloc(1, sizeof *llist);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002372 LY_CHECK_ERR_RET(!llist, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002373 llist->nodetype = LYS_LEAFLIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002374 llist->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002375
2376 /* insert into siblings */
2377 if (!*siblings) {
2378 *siblings = (struct lysp_node *)llist;
2379 } else {
2380 for (iter = *siblings; iter->next; iter = iter->next);
2381 iter->next = (struct lysp_node *)llist;
2382 }
2383
2384 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002385 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002386 INSERT_WORD(ctx, buf, llist->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002387
2388 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01002389 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002390 switch (kw) {
2391 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002392 LY_CHECK_RET(parse_config(ctx, data, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002393 break;
2394 case YANG_DEFAULT:
Radek Krejciceaf2122019-01-02 15:03:26 +01002395 YANG_CHECK_STMTVER2_RET(ctx, "default", "leaf-list");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002396 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_DEFAULT, &llist->dflts, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002397 break;
2398 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002399 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &llist->dsc, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002400 break;
2401 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002402 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &llist->iffeatures, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002403 break;
2404 case YANG_MAX_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002405 LY_CHECK_RET(parse_maxelements(ctx, data, &llist->max, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002406 break;
2407 case YANG_MIN_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002408 LY_CHECK_RET(parse_minelements(ctx, data, &llist->min, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002409 break;
2410 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002411 LY_CHECK_RET(parse_restrs(ctx, data, kw, &llist->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002412 break;
2413 case YANG_ORDERED_BY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002414 LY_CHECK_RET(parse_orderedby(ctx, data, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002415 break;
2416 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002417 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &llist->ref, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002418 break;
2419 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002420 LY_CHECK_RET(parse_status(ctx, data, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002421 break;
2422 case YANG_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002423 LY_CHECK_RET(parse_type(ctx, data, &llist->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002424 break;
2425 case YANG_UNITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002426 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, &llist->units, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002427 break;
2428 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002429 LY_CHECK_RET(parse_when(ctx, data, &llist->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002430 break;
2431 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002432 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002433 break;
2434 default:
David Sedlákb3077192019-06-19 10:55:37 +02002435 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002436 return LY_EVALID;
2437 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002438 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002439 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002440checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002441 /* mandatory substatements */
2442 if (!llist->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002443 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002444 return LY_EVALID;
2445 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01002446 if ((llist->min) && (llist->dflts)) {
David Sedlákb3077192019-06-19 10:55:37 +02002447 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMSCOMB, "min-elements", "default", "leaf-list");
Radek Krejci0e5d8382018-11-28 16:37:53 +01002448 return LY_EVALID;
2449 }
Radek Krejcidf6cad12018-11-28 17:10:55 +01002450 if (llist->max && llist->min > llist->max) {
David Sedlákb3077192019-06-19 10:55:37 +02002451 LOGVAL_PARSER(ctx, LYVE_SEMANTICS,
Radek Krejcidf6cad12018-11-28 17:10:55 +01002452 "Invalid combination of min-elements and max-elements: min value %u is bigger than the max value %u.",
2453 llist->min, llist->max);
2454 return LY_EVALID;
2455 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002456
2457 return ret;
2458}
2459
Michal Vaskoea5abea2018-09-18 13:10:54 +02002460/**
2461 * @brief Parse the refine statement.
2462 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002463 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002464 * @param[in,out] data Data to read from, always moved to currently handled character.
2465 * @param[in,out] refines Refines to add to.
2466 *
2467 * @return LY_ERR values.
2468 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002469static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002470parse_refine(struct lys_parser_ctx *ctx, const char **data, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002471{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002472 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002473 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002474 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002475 enum yang_keyword kw;
2476 struct lysp_refine *rf;
2477
Radek Krejci2c4e7172018-10-19 15:56:26 +02002478 LY_ARRAY_NEW_RET(ctx->ctx, *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002479
2480 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002481 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
David Sedlákb9b892c2019-07-12 14:44:02 +02002482 YANG_CHECK_NONEMPTY(ctx, word_len, "refine");
Radek Krejci44ceedc2018-10-02 15:54:31 +02002483 INSERT_WORD(ctx, buf, rf->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002484
Radek Krejci6d6556c2018-11-08 09:37:45 +01002485 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002486 switch (kw) {
2487 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002488 LY_CHECK_RET(parse_config(ctx, data, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002489 break;
2490 case YANG_DEFAULT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002491 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_DEFAULT, &rf->dflts, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002492 break;
2493 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002494 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &rf->dsc, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002495 break;
2496 case YANG_IF_FEATURE:
Radek Krejciceaf2122019-01-02 15:03:26 +01002497 YANG_CHECK_STMTVER2_RET(ctx, "if-feature", "refine");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002498 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &rf->iffeatures, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002499 break;
2500 case YANG_MAX_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002501 LY_CHECK_RET(parse_maxelements(ctx, data, &rf->max, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002502 break;
2503 case YANG_MIN_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002504 LY_CHECK_RET(parse_minelements(ctx, data, &rf->min, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002505 break;
2506 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002507 LY_CHECK_RET(parse_restrs(ctx, data, kw, &rf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002508 break;
2509 case YANG_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002510 LY_CHECK_RET(parse_mandatory(ctx, data, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002511 break;
2512 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002513 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &rf->ref, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002514 break;
2515 case YANG_PRESENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002516 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_PRESENCE, 0, &rf->presence, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002517 break;
2518 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002519 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002520 break;
2521 default:
David Sedlákb3077192019-06-19 10:55:37 +02002522 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002523 return LY_EVALID;
2524 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002525 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002526 return ret;
2527}
2528
Michal Vaskoea5abea2018-09-18 13:10:54 +02002529/**
2530 * @brief Parse the typedef statement.
2531 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002532 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002533 * @param[in,out] data Data to read from, always moved to currently handled character.
2534 * @param[in,out] typedefs Typedefs to add to.
2535 *
2536 * @return LY_ERR values.
2537 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002538static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002539parse_typedef(struct lys_parser_ctx *ctx, struct lysp_node *parent, const char **data, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002540{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002541 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002542 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002543 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002544 enum yang_keyword kw;
2545 struct lysp_tpdf *tpdf;
2546
Radek Krejci2c4e7172018-10-19 15:56:26 +02002547 LY_ARRAY_NEW_RET(ctx->ctx, *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002548
2549 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002550 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002551 INSERT_WORD(ctx, buf, tpdf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002552
2553 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01002554 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002555 switch (kw) {
2556 case YANG_DEFAULT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002557 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &tpdf->dflt, Y_STR_ARG, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002558 break;
2559 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002560 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &tpdf->dsc, Y_STR_ARG, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002561 break;
2562 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002563 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &tpdf->ref, Y_STR_ARG, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002564 break;
2565 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002566 LY_CHECK_RET(parse_status(ctx, data, &tpdf->flags, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002567 break;
2568 case YANG_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002569 LY_CHECK_RET(parse_type(ctx, data, &tpdf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002570 break;
2571 case YANG_UNITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002572 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, &tpdf->units, Y_STR_ARG, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002573 break;
2574 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002575 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002576 break;
2577 default:
David Sedlákb3077192019-06-19 10:55:37 +02002578 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002579 return LY_EVALID;
2580 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002581 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002582 LY_CHECK_RET(ret);
Radek Krejcibbe09a92018-11-08 09:36:54 +01002583checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002584 /* mandatory substatements */
2585 if (!tpdf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002586 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002587 return LY_EVALID;
2588 }
2589
Radek Krejcibbe09a92018-11-08 09:36:54 +01002590 /* store data for collision check */
Radek Krejci7fc68292019-06-12 13:51:09 +02002591 if (parent && !(parent->nodetype & (LYS_GROUPING | LYS_ACTION | LYS_INOUT | LYS_NOTIF))) {
Radek Krejcibbe09a92018-11-08 09:36:54 +01002592 ly_set_add(&ctx->tpdfs_nodes, parent, 0);
2593 }
2594
Michal Vasko7fbc8162018-09-17 10:35:16 +02002595 return ret;
2596}
2597
Michal Vaskoea5abea2018-09-18 13:10:54 +02002598/**
2599 * @brief Parse the input or output statement.
2600 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002601 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002602 * @param[in,out] data Data to read from, always moved to currently handled character.
2603 * @param[in] kw Type of this particular keyword
2604 * @param[in,out] inout_p Input/output pointer to write to.
2605 *
2606 * @return LY_ERR values.
2607 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002608static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002609parse_inout(struct lys_parser_ctx *ctx, const char **data, enum yang_keyword inout_kw, struct lysp_node *parent, struct lysp_action_inout *inout_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002610{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002611 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002612 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002613 size_t word_len;
Radek Krejci10113652018-11-14 16:56:50 +01002614 enum yang_keyword kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002615
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002616 if (inout_p->nodetype) {
David Sedlákb3077192019-06-19 10:55:37 +02002617 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002618 return LY_EVALID;
2619 }
2620
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002621 /* initiate structure */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002622 inout_p->nodetype = &((struct lysp_action*)parent)->input == inout_p ? LYS_INPUT : LYS_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002623 inout_p->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002624
2625 /* parse substatements */
Radek Krejci7fc68292019-06-12 13:51:09 +02002626 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002627 switch (kw) {
2628 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01002629 YANG_CHECK_STMTVER2_RET(ctx, "anydata", ly_stmt2str(inout_kw));
Radek Krejci10113652018-11-14 16:56:50 +01002630 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002631 case YANG_ANYXML:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002632 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002633 break;
2634 case YANG_CHOICE:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002635 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002636 break;
2637 case YANG_CONTAINER:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002638 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002639 break;
2640 case YANG_LEAF:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002641 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002642 break;
2643 case YANG_LEAF_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002644 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002645 break;
2646 case YANG_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002647 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002648 break;
2649 case YANG_USES:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002650 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002651 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002652 case YANG_TYPEDEF:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002653 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)inout_p, data, &inout_p->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002654 break;
2655 case YANG_MUST:
Radek Krejciceaf2122019-01-02 15:03:26 +01002656 YANG_CHECK_STMTVER2_RET(ctx, "must", ly_stmt2str(inout_kw));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002657 LY_CHECK_RET(parse_restrs(ctx, data, kw, &inout_p->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002658 break;
2659 case YANG_GROUPING:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002660 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)inout_p, &inout_p->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002661 break;
2662 case YANG_CUSTOM:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002663 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inout_p->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002664 break;
2665 default:
David Sedlákb3077192019-06-19 10:55:37 +02002666 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002667 return LY_EVALID;
2668 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002669 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002670 LY_CHECK_RET(ret);
2671checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002672 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02002673 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, inout_p->groupings, NULL, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02002674
Michal Vasko7fbc8162018-09-17 10:35:16 +02002675 return ret;
2676}
2677
Michal Vaskoea5abea2018-09-18 13:10:54 +02002678/**
2679 * @brief Parse the action statement.
2680 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002681 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002682 * @param[in,out] data Data to read from, always moved to currently handled character.
2683 * @param[in,out] actions Actions to add to.
2684 *
2685 * @return LY_ERR values.
2686 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002687LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002688parse_action(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002689{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002690 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002691 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002692 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002693 enum yang_keyword kw;
2694 struct lysp_action *act;
2695
Radek Krejci2c4e7172018-10-19 15:56:26 +02002696 LY_ARRAY_NEW_RET(ctx->ctx, *actions, act, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002697
2698 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002699 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002700 INSERT_WORD(ctx, buf, act->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002701 act->nodetype = LYS_ACTION;
2702 act->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002703
Radek Krejci7fc68292019-06-12 13:51:09 +02002704 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002705 switch (kw) {
2706 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002707 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &act->dsc, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002708 break;
2709 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002710 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &act->iffeatures, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002711 break;
2712 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002713 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &act->ref, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002714 break;
2715 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002716 LY_CHECK_RET(parse_status(ctx, data, &act->flags, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002717 break;
2718
2719 case YANG_INPUT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002720 LY_CHECK_RET(parse_inout(ctx, data, kw, (struct lysp_node*)act, &act->input));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002721 break;
2722 case YANG_OUTPUT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002723 LY_CHECK_RET(parse_inout(ctx, data, kw, (struct lysp_node*)act, &act->output));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002724 break;
2725
2726 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01002727 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)act, data, &act->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002728 break;
2729 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002730 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)act, &act->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002731 break;
2732 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002733 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002734 break;
2735 default:
David Sedlákb3077192019-06-19 10:55:37 +02002736 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), parent ? "action" : "rpc");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002737 return LY_EVALID;
2738 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002739 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002740 LY_CHECK_RET(ret);
2741checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002742 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02002743 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, act->groupings, NULL, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02002744
Michal Vasko7fbc8162018-09-17 10:35:16 +02002745 return ret;
2746}
2747
Michal Vaskoea5abea2018-09-18 13:10:54 +02002748/**
2749 * @brief Parse the notification statement.
2750 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002751 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002752 * @param[in,out] data Data to read from, always moved to currently handled character.
2753 * @param[in,out] notifs Notifications to add to.
2754 *
2755 * @return LY_ERR values.
2756 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002757LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002758parse_notif(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002759{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002760 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002761 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002762 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002763 enum yang_keyword kw;
2764 struct lysp_notif *notif;
2765
Radek Krejci2c4e7172018-10-19 15:56:26 +02002766 LY_ARRAY_NEW_RET(ctx->ctx, *notifs, notif, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002767
2768 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002769 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002770 INSERT_WORD(ctx, buf, notif->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002771 notif->nodetype = LYS_NOTIF;
2772 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002773
Radek Krejci7fc68292019-06-12 13:51:09 +02002774 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002775 switch (kw) {
2776 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002777 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &notif->dsc, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002778 break;
2779 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002780 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &notif->iffeatures, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002781 break;
2782 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002783 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &notif->ref, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002784 break;
2785 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002786 LY_CHECK_RET(parse_status(ctx, data, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002787 break;
2788
2789 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01002790 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "notification");
Radek Krejci10113652018-11-14 16:56:50 +01002791 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002792 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002793 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002794 break;
2795 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002796 LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002797 break;
2798 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002799 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002800 break;
2801 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002802 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002803 break;
2804 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002805 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002806 break;
2807 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002808 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002809 break;
2810 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002811 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002812 break;
2813
2814 case YANG_MUST:
Radek Krejciceaf2122019-01-02 15:03:26 +01002815 YANG_CHECK_STMTVER2_RET(ctx, "must", "notification");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002816 LY_CHECK_RET(parse_restrs(ctx, data, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002817 break;
2818 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01002819 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)notif, data, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002820 break;
2821 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002822 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002823 break;
2824 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002825 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002826 break;
2827 default:
David Sedlákb3077192019-06-19 10:55:37 +02002828 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002829 return LY_EVALID;
2830 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002831 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002832 LY_CHECK_RET(ret);
2833checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002834 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02002835 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, notif->groupings, NULL, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02002836
Michal Vasko7fbc8162018-09-17 10:35:16 +02002837 return ret;
2838}
2839
Michal Vaskoea5abea2018-09-18 13:10:54 +02002840/**
2841 * @brief Parse the grouping statement.
2842 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002843 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002844 * @param[in,out] data Data to read from, always moved to currently handled character.
2845 * @param[in,out] groupings Groupings to add to.
2846 *
2847 * @return LY_ERR values.
2848 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002849LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002850parse_grouping(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002851{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002852 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002853 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002854 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002855 enum yang_keyword kw;
2856 struct lysp_grp *grp;
2857
Radek Krejci2c4e7172018-10-19 15:56:26 +02002858 LY_ARRAY_NEW_RET(ctx->ctx, *groupings, grp, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002859
2860 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002861 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002862 INSERT_WORD(ctx, buf, grp->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002863 grp->nodetype = LYS_GROUPING;
2864 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002865
Radek Krejci7fc68292019-06-12 13:51:09 +02002866 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002867 switch (kw) {
2868 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002869 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &grp->dsc, Y_STR_ARG, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002870 break;
2871 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002872 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &grp->ref, Y_STR_ARG, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002873 break;
2874 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002875 LY_CHECK_RET(parse_status(ctx, data, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002876 break;
2877
2878 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01002879 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "grouping");
Radek Krejci10113652018-11-14 16:56:50 +01002880 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002881 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002882 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002883 break;
2884 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002885 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002886 break;
2887 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002888 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002889 break;
2890 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002891 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002892 break;
2893 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002894 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002895 break;
2896 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002897 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002898 break;
2899 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002900 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002901 break;
2902
2903 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01002904 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)grp, data, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002905 break;
2906 case YANG_ACTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01002907 YANG_CHECK_STMTVER2_RET(ctx, "action", "grouping");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002908 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)grp, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002909 break;
2910 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002911 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)grp, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002912 break;
2913 case YANG_NOTIFICATION:
Radek Krejciceaf2122019-01-02 15:03:26 +01002914 YANG_CHECK_STMTVER2_RET(ctx, "notification", "grouping");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002915 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)grp, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002916 break;
2917 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002918 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002919 break;
2920 default:
David Sedlákb3077192019-06-19 10:55:37 +02002921 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002922 return LY_EVALID;
2923 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002924 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002925 LY_CHECK_RET(ret);
2926checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002927 /* finalize parent pointers to the reallocated items */
David Sedlák0d6de5a2019-07-22 13:25:44 +02002928 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, grp->groupings, NULL, grp->actions, grp->notifs));
Radek Krejci7fc68292019-06-12 13:51:09 +02002929
Michal Vasko7fbc8162018-09-17 10:35:16 +02002930 return ret;
2931}
2932
Michal Vaskoea5abea2018-09-18 13:10:54 +02002933/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02002934 * @brief Parse the augment statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002935 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002936 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002937 * @param[in,out] data Data to read from, always moved to currently handled character.
2938 * @param[in,out] augments Augments to add to.
2939 *
2940 * @return LY_ERR values.
2941 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002942LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002943parse_augment(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002944{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002945 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002946 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002947 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002948 enum yang_keyword kw;
2949 struct lysp_augment *aug;
2950
Radek Krejci2c4e7172018-10-19 15:56:26 +02002951 LY_ARRAY_NEW_RET(ctx->ctx, *augments, aug, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002952
2953 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002954 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
David Sedlákb9b892c2019-07-12 14:44:02 +02002955 YANG_CHECK_NONEMPTY(ctx, word_len, "augment");
Radek Krejci44ceedc2018-10-02 15:54:31 +02002956 INSERT_WORD(ctx, buf, aug->nodeid, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002957 aug->nodetype = LYS_AUGMENT;
2958 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002959
Radek Krejci7fc68292019-06-12 13:51:09 +02002960 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002961 switch (kw) {
2962 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002963 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &aug->dsc, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002964 break;
2965 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002966 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &aug->iffeatures, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002967 break;
2968 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002969 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &aug->ref, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002970 break;
2971 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002972 LY_CHECK_RET(parse_status(ctx, data, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002973 break;
2974 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002975 LY_CHECK_RET(parse_when(ctx, data, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002976 break;
2977
2978 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01002979 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "augment");
Radek Krejci10113652018-11-14 16:56:50 +01002980 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002981 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002982 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002983 break;
2984 case YANG_CASE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002985 LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002986 break;
2987 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002988 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002989 break;
2990 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002991 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002992 break;
2993 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002994 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002995 break;
2996 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002997 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002998 break;
2999 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003000 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003001 break;
3002 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003003 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003004 break;
3005
3006 case YANG_ACTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003007 YANG_CHECK_STMTVER2_RET(ctx, "action", "augment");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003008 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003009 break;
3010 case YANG_NOTIFICATION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003011 YANG_CHECK_STMTVER2_RET(ctx, "notification", "augment");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003012 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003013 break;
3014 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003015 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003016 break;
3017 default:
David Sedlákb3077192019-06-19 10:55:37 +02003018 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003019 return LY_EVALID;
3020 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003021 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003022 LY_CHECK_RET(ret);
3023checks:
3024 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02003025 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, NULL, NULL, aug->actions, aug->notifs));
Radek Krejci7fc68292019-06-12 13:51:09 +02003026
Michal Vasko7fbc8162018-09-17 10:35:16 +02003027 return ret;
3028}
3029
Michal Vaskoea5abea2018-09-18 13:10:54 +02003030/**
3031 * @brief Parse the uses statement.
3032 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003033 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003034 * @param[in,out] data Data to read from, always moved to currently handled character.
3035 * @param[in,out] siblings Siblings to add to.
3036 *
3037 * @return LY_ERR values.
3038 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003039LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003040parse_uses(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003041{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003042 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003043 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003044 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003045 enum yang_keyword kw;
3046 struct lysp_node *iter;
3047 struct lysp_node_uses *uses;
3048
3049 /* create structure */
3050 uses = calloc(1, sizeof *uses);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003051 LY_CHECK_ERR_RET(!uses, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003052 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003053 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003054
3055 /* insert into siblings */
3056 if (!*siblings) {
3057 *siblings = (struct lysp_node *)uses;
3058 } else {
3059 for (iter = *siblings; iter->next; iter = iter->next);
3060 iter->next = (struct lysp_node *)uses;
3061 }
3062
3063 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003064 LY_CHECK_RET(get_argument(ctx, data, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003065 INSERT_WORD(ctx, buf, uses->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003066
3067 /* parse substatements */
Radek Krejci7fc68292019-06-12 13:51:09 +02003068 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003069 switch (kw) {
3070 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003071 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &uses->dsc, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003072 break;
3073 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003074 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &uses->iffeatures, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003075 break;
3076 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003077 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &uses->ref, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003078 break;
3079 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003080 LY_CHECK_RET(parse_status(ctx, data, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003081 break;
3082 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003083 LY_CHECK_RET(parse_when(ctx, data, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003084 break;
3085
3086 case YANG_REFINE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003087 LY_CHECK_RET(parse_refine(ctx, data, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003088 break;
3089 case YANG_AUGMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003090 LY_CHECK_RET(parse_augment(ctx, data, (struct lysp_node*)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003091 break;
3092 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003093 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003094 break;
3095 default:
David Sedlákb3077192019-06-19 10:55:37 +02003096 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003097 return LY_EVALID;
3098 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003099 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003100checks:
3101 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02003102 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, NULL, uses->augments, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02003103
Michal Vasko7fbc8162018-09-17 10:35:16 +02003104 return ret;
3105}
3106
Michal Vaskoea5abea2018-09-18 13:10:54 +02003107/**
3108 * @brief Parse the case statement.
3109 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003110 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003111 * @param[in,out] data Data to read from, always moved to currently handled character.
3112 * @param[in,out] siblings Siblings to add to.
3113 *
3114 * @return LY_ERR values.
3115 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003116LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003117parse_case(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003118{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003119 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003120 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003121 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003122 enum yang_keyword kw;
3123 struct lysp_node *iter;
3124 struct lysp_node_case *cas;
3125
3126 /* create structure */
3127 cas = calloc(1, sizeof *cas);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003128 LY_CHECK_ERR_RET(!cas, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003129 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003130 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003131
3132 /* insert into siblings */
3133 if (!*siblings) {
3134 *siblings = (struct lysp_node *)cas;
3135 } else {
3136 for (iter = *siblings; iter->next; iter = iter->next);
3137 iter->next = (struct lysp_node *)cas;
3138 }
3139
3140 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003141 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003142 INSERT_WORD(ctx, buf, cas->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003143
3144 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01003145 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003146 switch (kw) {
3147 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003148 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &cas->dsc, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003149 break;
3150 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003151 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &cas->iffeatures, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003152 break;
3153 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003154 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &cas->ref, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003155 break;
3156 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003157 LY_CHECK_RET(parse_status(ctx, data, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003158 break;
3159 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003160 LY_CHECK_RET(parse_when(ctx, data, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003161 break;
3162
3163 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01003164 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "case");
Radek Krejci10113652018-11-14 16:56:50 +01003165 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003166 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003167 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003168 break;
3169 case YANG_CHOICE:
Radek Krejcia9026eb2018-12-12 16:04:47 +01003170 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003171 break;
3172 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003173 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003174 break;
3175 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003176 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003177 break;
3178 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003179 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003180 break;
3181 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003182 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003183 break;
3184 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003185 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003186 break;
3187 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003188 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003189 break;
3190 default:
David Sedlákb3077192019-06-19 10:55:37 +02003191 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003192 return LY_EVALID;
3193 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003194 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003195 return ret;
3196}
3197
Michal Vaskoea5abea2018-09-18 13:10:54 +02003198/**
3199 * @brief Parse the choice statement.
3200 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003201 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003202 * @param[in,out] data Data to read from, always moved to currently handled character.
3203 * @param[in,out] siblings Siblings to add to.
3204 *
3205 * @return LY_ERR values.
3206 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003207LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003208parse_choice(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003209{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003210 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003211 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003212 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003213 enum yang_keyword kw;
3214 struct lysp_node *iter;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003215 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003216
3217 /* create structure */
Radek Krejci44ceedc2018-10-02 15:54:31 +02003218 choice = calloc(1, sizeof *choice);
3219 LY_CHECK_ERR_RET(!choice, LOGMEM(ctx->ctx), LY_EMEM);
3220 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003221 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003222
3223 /* insert into siblings */
3224 if (!*siblings) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02003225 *siblings = (struct lysp_node *)choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003226 } else {
3227 for (iter = *siblings; iter->next; iter = iter->next);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003228 iter->next = (struct lysp_node *)choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003229 }
3230
3231 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003232 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003233 INSERT_WORD(ctx, buf, choice->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003234
3235 /* parse substatements */
Radek Krejcia9026eb2018-12-12 16:04:47 +01003236 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003237 switch (kw) {
3238 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003239 LY_CHECK_RET(parse_config(ctx, data, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003240 break;
3241 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003242 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &choice->dsc, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003243 break;
3244 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003245 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &choice->iffeatures, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003246 break;
3247 case YANG_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003248 LY_CHECK_RET(parse_mandatory(ctx, data, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003249 break;
3250 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003251 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &choice->ref, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003252 break;
3253 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003254 LY_CHECK_RET(parse_status(ctx, data, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003255 break;
3256 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003257 LY_CHECK_RET(parse_when(ctx, data, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003258 break;
3259 case YANG_DEFAULT:
Radek Krejcia9026eb2018-12-12 16:04:47 +01003260 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &choice->dflt, Y_PREF_IDENTIF_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003261 break;
3262
3263 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01003264 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "choice");
Radek Krejci10113652018-11-14 16:56:50 +01003265 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003266 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003267 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003268 break;
3269 case YANG_CASE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003270 LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003271 break;
3272 case YANG_CHOICE:
Radek Krejciceaf2122019-01-02 15:03:26 +01003273 YANG_CHECK_STMTVER2_RET(ctx, "choice", "choice");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003274 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003275 break;
3276 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003277 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003278 break;
3279 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003280 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003281 break;
3282 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003283 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003284 break;
3285 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003286 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003287 break;
3288 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003289 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003290 break;
3291 default:
David Sedlákb3077192019-06-19 10:55:37 +02003292 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003293 return LY_EVALID;
3294 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003295 }
Radek Krejcia9026eb2018-12-12 16:04:47 +01003296 LY_CHECK_RET(ret);
3297checks:
3298 if ((choice->flags & LYS_MAND_TRUE) && choice->dflt) {
David Sedlákb3077192019-06-19 10:55:37 +02003299 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMSCOMB, "mandatory", "default", "choice");
Radek Krejcia9026eb2018-12-12 16:04:47 +01003300 return LY_EVALID;
3301 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003302 return ret;
3303}
3304
Michal Vaskoea5abea2018-09-18 13:10:54 +02003305/**
3306 * @brief Parse the container statement.
3307 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003308 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003309 * @param[in,out] data Data to read from, always moved to currently handled character.
3310 * @param[in,out] siblings Siblings to add to.
3311 *
3312 * @return LY_ERR values.
3313 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003314LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003315parse_container(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003316{
3317 LY_ERR ret = 0;
3318 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003319 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003320 enum yang_keyword kw;
3321 struct lysp_node *iter;
3322 struct lysp_node_container *cont;
3323
3324 /* create structure */
3325 cont = calloc(1, sizeof *cont);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003326 LY_CHECK_ERR_RET(!cont, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003327 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003328 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003329
3330 /* insert into siblings */
3331 if (!*siblings) {
3332 *siblings = (struct lysp_node *)cont;
3333 } else {
3334 for (iter = *siblings; iter->next; iter = iter->next);
3335 iter->next = (struct lysp_node *)cont;
3336 }
3337
3338 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003339 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003340 INSERT_WORD(ctx, buf, cont->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003341
3342 /* parse substatements */
Radek Krejci7fc68292019-06-12 13:51:09 +02003343 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003344 switch (kw) {
3345 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003346 LY_CHECK_RET(parse_config(ctx, data, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003347 break;
3348 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003349 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &cont->dsc, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003350 break;
3351 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003352 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &cont->iffeatures, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003353 break;
3354 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003355 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &cont->ref, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003356 break;
3357 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003358 LY_CHECK_RET(parse_status(ctx, data, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003359 break;
3360 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003361 LY_CHECK_RET(parse_when(ctx, data, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003362 break;
3363 case YANG_PRESENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003364 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_PRESENCE, 0, &cont->presence, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003365 break;
3366
3367 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01003368 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "container");
Radek Krejci10113652018-11-14 16:56:50 +01003369 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003370 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003371 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003372 break;
3373 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003374 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003375 break;
3376 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003377 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003378 break;
3379 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003380 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003381 break;
3382 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003383 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003384 break;
3385 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003386 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003387 break;
3388 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003389 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003390 break;
3391
3392 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01003393 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)cont, data, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003394 break;
3395 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003396 LY_CHECK_RET(parse_restrs(ctx, data, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003397 break;
3398 case YANG_ACTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003399 YANG_CHECK_STMTVER2_RET(ctx, "action", "container");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003400 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003401 break;
3402 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003403 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003404 break;
3405 case YANG_NOTIFICATION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003406 YANG_CHECK_STMTVER2_RET(ctx, "notification", "container");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003407 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003408 break;
3409 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003410 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003411 break;
3412 default:
David Sedlákb3077192019-06-19 10:55:37 +02003413 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003414 return LY_EVALID;
3415 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003416 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003417checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01003418 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02003419 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, cont->groupings, NULL, cont->actions, cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003420 return ret;
3421}
3422
Michal Vaskoea5abea2018-09-18 13:10:54 +02003423/**
3424 * @brief Parse the list statement.
3425 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003426 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003427 * @param[in,out] data Data to read from, always moved to currently handled character.
3428 * @param[in,out] siblings Siblings to add to.
3429 *
3430 * @return LY_ERR values.
3431 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003432LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003433parse_list(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003434{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003435 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003436 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003437 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003438 enum yang_keyword kw;
3439 struct lysp_node *iter;
3440 struct lysp_node_list *list;
3441
3442 /* create structure */
3443 list = calloc(1, sizeof *list);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003444 LY_CHECK_ERR_RET(!list, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003445 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003446 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003447
3448 /* insert into siblings */
3449 if (!*siblings) {
3450 *siblings = (struct lysp_node *)list;
3451 } else {
3452 for (iter = *siblings; iter->next; iter = iter->next);
3453 iter->next = (struct lysp_node *)list;
3454 }
3455
3456 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003457 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003458 INSERT_WORD(ctx, buf, list->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003459
3460 /* parse substatements */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003461 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003462 switch (kw) {
3463 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003464 LY_CHECK_RET(parse_config(ctx, data, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003465 break;
3466 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003467 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &list->dsc, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003468 break;
3469 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003470 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &list->iffeatures, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003471 break;
3472 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003473 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &list->ref, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003474 break;
3475 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003476 LY_CHECK_RET(parse_status(ctx, data, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003477 break;
3478 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003479 LY_CHECK_RET(parse_when(ctx, data, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003480 break;
3481 case YANG_KEY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003482 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_KEY, 0, &list->key, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003483 break;
3484 case YANG_MAX_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003485 LY_CHECK_RET(parse_maxelements(ctx, data, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003486 break;
3487 case YANG_MIN_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003488 LY_CHECK_RET(parse_minelements(ctx, data, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003489 break;
3490 case YANG_ORDERED_BY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003491 LY_CHECK_RET(parse_orderedby(ctx, data, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003492 break;
3493 case YANG_UNIQUE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003494 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_UNIQUE, &list->uniques, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003495 break;
3496
3497 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01003498 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "list");
Radek Krejci10113652018-11-14 16:56:50 +01003499 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003500 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003501 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003502 break;
3503 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003504 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003505 break;
3506 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003507 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003508 break;
3509 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003510 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003511 break;
3512 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003513 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003514 break;
3515 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003516 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003517 break;
3518 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003519 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003520 break;
3521
3522 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01003523 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)list, data, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003524 break;
3525 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003526 LY_CHECK_RET(parse_restrs(ctx, data, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003527 break;
3528 case YANG_ACTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003529 YANG_CHECK_STMTVER2_RET(ctx, "action", "list");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003530 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003531 break;
3532 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003533 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003534 break;
3535 case YANG_NOTIFICATION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003536 YANG_CHECK_STMTVER2_RET(ctx, "notification", "list");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003537 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003538 break;
3539 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003540 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003541 break;
3542 default:
David Sedlákb3077192019-06-19 10:55:37 +02003543 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003544 return LY_EVALID;
3545 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003546 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003547 LY_CHECK_RET(ret);
3548checks:
Radek Krejci7fc68292019-06-12 13:51:09 +02003549 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02003550 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, list->groupings, NULL, list->actions, list->notifs));
Radek Krejci7fc68292019-06-12 13:51:09 +02003551
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003552 if (list->max && list->min > list->max) {
David Sedlákb3077192019-06-19 10:55:37 +02003553 LOGVAL_PARSER(ctx, LYVE_SEMANTICS,
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003554 "Invalid combination of min-elements and max-elements: min value %u is bigger than the max value %u.",
3555 list->min, list->max);
3556 return LY_EVALID;
3557 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003558
3559 return ret;
3560}
3561
Michal Vaskoea5abea2018-09-18 13:10:54 +02003562/**
3563 * @brief Parse the yin-element statement.
3564 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003565 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003566 * @param[in,out] data Data to read from, always moved to currently handled character.
3567 * @param[in,out] flags Flags to write to.
3568 * @param[in,out] exts Extension instances to add to.
3569 *
3570 * @return LY_ERR values.
3571 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003572static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003573parse_yinelement(struct lys_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003574{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003575 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003576 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003577 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003578 enum yang_keyword kw;
3579
3580 if (*flags & LYS_YINELEM_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02003581 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003582 return LY_EVALID;
3583 }
3584
3585 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003586 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003587
3588 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
3589 *flags |= LYS_YINELEM_TRUE;
3590 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
3591 *flags |= LYS_YINELEM_FALSE;
3592 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003593 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003594 free(buf);
3595 return LY_EVALID;
3596 }
3597 free(buf);
3598
Radek Krejci6d6556c2018-11-08 09:37:45 +01003599 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003600 switch (kw) {
3601 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003602 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_YINELEM, 0, exts));
3603 LY_CHECK_RET(ret); break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003604 default:
David Sedlákb3077192019-06-19 10:55:37 +02003605 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003606 return LY_EVALID;
3607 }
3608 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003609 return ret;
3610}
3611
Michal Vaskoea5abea2018-09-18 13:10:54 +02003612/**
David Sedlák2444f8f2019-07-09 11:02:47 +02003613 * @brief Parse the argument statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003614 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003615 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003616 * @param[in,out] data Data to read from, always moved to currently handled character.
3617 * @param[in,out] argument Value to write to.
3618 * @param[in,out] flags Flags to write to.
3619 * @param[in,out] exts Extension instances to add to.
3620 *
3621 * @return LY_ERR values.
3622 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003623static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003624parse_argument(struct lys_parser_ctx *ctx, const char **data, const char **argument, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003625{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003626 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003627 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003628 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003629 enum yang_keyword kw;
3630
3631 if (*argument) {
David Sedlákb3077192019-06-19 10:55:37 +02003632 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003633 return LY_EVALID;
3634 }
3635
3636 /* get value */
David Sedlák2444f8f2019-07-09 11:02:47 +02003637 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003638 INSERT_WORD(ctx, buf, *argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003639
Radek Krejci6d6556c2018-11-08 09:37:45 +01003640 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003641 switch (kw) {
3642 case YANG_YIN_ELEMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003643 LY_CHECK_RET(parse_yinelement(ctx, data, flags, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003644 break;
3645 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003646 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_ARGUMENT, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003647 break;
3648 default:
David Sedlákb3077192019-06-19 10:55:37 +02003649 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003650 return LY_EVALID;
3651 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003652 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003653 return ret;
3654}
3655
Michal Vaskoea5abea2018-09-18 13:10:54 +02003656/**
3657 * @brief Parse the extension statement.
3658 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003659 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003660 * @param[in,out] data Data to read from, always moved to currently handled character.
3661 * @param[in,out] extensions Extensions to add to.
3662 *
3663 * @return LY_ERR values.
3664 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003665static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003666parse_extension(struct lys_parser_ctx *ctx, const char **data, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003667{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003668 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003669 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003670 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003671 enum yang_keyword kw;
3672 struct lysp_ext *ex;
3673
Radek Krejci2c4e7172018-10-19 15:56:26 +02003674 LY_ARRAY_NEW_RET(ctx->ctx, *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003675
3676 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003677 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003678 INSERT_WORD(ctx, buf, ex->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003679
Radek Krejci6d6556c2018-11-08 09:37:45 +01003680 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003681 switch (kw) {
3682 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003683 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &ex->dsc, Y_STR_ARG, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003684 break;
3685 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003686 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &ex->ref, Y_STR_ARG, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003687 break;
3688 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003689 LY_CHECK_RET(parse_status(ctx, data, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003690 break;
3691 case YANG_ARGUMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003692 LY_CHECK_RET(parse_argument(ctx, data, &ex->argument, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003693 break;
3694 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003695 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003696 break;
3697 default:
David Sedlákb3077192019-06-19 10:55:37 +02003698 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003699 return LY_EVALID;
3700 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003701 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003702 return ret;
3703}
3704
Michal Vaskoea5abea2018-09-18 13:10:54 +02003705/**
3706 * @brief Parse the deviate statement.
3707 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003708 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003709 * @param[in,out] data Data to read from, always moved to currently handled character.
3710 * @param[in,out] deviates Deviates to add to.
3711 *
3712 * @return LY_ERR values.
3713 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003714LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003715parse_deviate(struct lys_parser_ctx *ctx, const char **data, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003716{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003717 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003718 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003719 size_t word_len, dev_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003720 enum yang_keyword kw;
3721 struct lysp_deviate *iter, *d;
3722 struct lysp_deviate_add *d_add = NULL;
3723 struct lysp_deviate_rpl *d_rpl = NULL;
3724 struct lysp_deviate_del *d_del = NULL;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02003725 const char **d_units = NULL, ***d_uniques = NULL, ***d_dflts = NULL;
3726 struct lysp_restr **d_musts = NULL;
3727 uint16_t *d_flags = 0;
3728 uint32_t *d_min = 0, *d_max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003729
3730 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003731 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003732
3733 if ((word_len == 13) && !strncmp(word, "not-supported", word_len)) {
3734 dev_mod = LYS_DEV_NOT_SUPPORTED;
3735 } else if ((word_len == 3) && !strncmp(word, "add", word_len)) {
3736 dev_mod = LYS_DEV_ADD;
3737 } else if ((word_len == 7) && !strncmp(word, "replace", word_len)) {
3738 dev_mod = LYS_DEV_REPLACE;
3739 } else if ((word_len == 6) && !strncmp(word, "delete", word_len)) {
3740 dev_mod = LYS_DEV_DELETE;
3741 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003742 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003743 free(buf);
3744 return LY_EVALID;
3745 }
3746 free(buf);
3747
3748 /* create structure */
3749 switch (dev_mod) {
3750 case LYS_DEV_NOT_SUPPORTED:
3751 d = calloc(1, sizeof *d);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003752 LY_CHECK_ERR_RET(!d, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003753 break;
3754 case LYS_DEV_ADD:
3755 d_add = calloc(1, sizeof *d_add);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003756 LY_CHECK_ERR_RET(!d_add, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003757 d = (struct lysp_deviate *)d_add;
3758 d_units = &d_add->units;
3759 d_uniques = &d_add->uniques;
3760 d_dflts = &d_add->dflts;
3761 d_musts = &d_add->musts;
3762 d_flags = &d_add->flags;
3763 d_min = &d_add->min;
3764 d_max = &d_add->max;
3765 break;
3766 case LYS_DEV_REPLACE:
3767 d_rpl = calloc(1, sizeof *d_rpl);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003768 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003769 d = (struct lysp_deviate *)d_rpl;
3770 d_units = &d_rpl->units;
3771 d_flags = &d_rpl->flags;
3772 d_min = &d_rpl->min;
3773 d_max = &d_rpl->max;
3774 break;
3775 case LYS_DEV_DELETE:
3776 d_del = calloc(1, sizeof *d_del);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003777 LY_CHECK_ERR_RET(!d_del, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003778 d = (struct lysp_deviate *)d_del;
3779 d_units = &d_del->units;
3780 d_uniques = &d_del->uniques;
3781 d_dflts = &d_del->dflts;
3782 d_musts = &d_del->musts;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003783 break;
3784 default:
3785 assert(0);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003786 LOGINT_RET(ctx->ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003787 }
3788 d->mod = dev_mod;
3789
3790 /* insert into siblings */
3791 if (!*deviates) {
3792 *deviates = d;
3793 } else {
3794 for (iter = *deviates; iter->next; iter = iter->next);
3795 iter->next = d;
3796 }
3797
Radek Krejci6d6556c2018-11-08 09:37:45 +01003798 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003799 switch (kw) {
3800 case YANG_CONFIG:
3801 switch (dev_mod) {
3802 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003803 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003804 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003805 return LY_EVALID;
3806 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003807 LY_CHECK_RET(parse_config(ctx, data, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003808 break;
3809 }
3810 break;
3811 case YANG_DEFAULT:
3812 switch (dev_mod) {
3813 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003814 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003815 return LY_EVALID;
3816 case LYS_DEV_REPLACE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003817 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &d_rpl->dflt, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003818 break;
3819 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003820 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_DEFAULT, d_dflts, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003821 break;
3822 }
3823 break;
3824 case YANG_MANDATORY:
3825 switch (dev_mod) {
3826 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003827 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003828 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003829 return LY_EVALID;
3830 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003831 LY_CHECK_RET(parse_mandatory(ctx, data, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003832 break;
3833 }
3834 break;
3835 case YANG_MAX_ELEMENTS:
3836 switch (dev_mod) {
3837 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003838 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003839 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003840 return LY_EVALID;
3841 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003842 LY_CHECK_RET(parse_maxelements(ctx, data, d_max, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003843 break;
3844 }
3845 break;
3846 case YANG_MIN_ELEMENTS:
3847 switch (dev_mod) {
3848 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003849 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003850 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003851 return LY_EVALID;
3852 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003853 LY_CHECK_RET(parse_minelements(ctx, data, d_min, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003854 break;
3855 }
3856 break;
3857 case YANG_MUST:
3858 switch (dev_mod) {
3859 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003860 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003861 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003862 return LY_EVALID;
3863 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003864 LY_CHECK_RET(parse_restrs(ctx, data, kw, d_musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003865 break;
3866 }
3867 break;
3868 case YANG_TYPE:
3869 switch (dev_mod) {
3870 case LYS_DEV_NOT_SUPPORTED:
3871 case LYS_DEV_ADD:
3872 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003873 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003874 return LY_EVALID;
3875 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003876 if (d_rpl->type) {
David Sedlákb3077192019-06-19 10:55:37 +02003877 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003878 return LY_EVALID;
3879 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003880 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003881 LY_CHECK_ERR_RET(!d_rpl->type, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003882 LY_CHECK_RET(parse_type(ctx, data, d_rpl->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003883 break;
3884 }
3885 break;
3886 case YANG_UNIQUE:
3887 switch (dev_mod) {
3888 case LYS_DEV_NOT_SUPPORTED:
3889 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003890 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003891 return LY_EVALID;
3892 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003893 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_UNIQUE, d_uniques, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003894 break;
3895 }
3896 break;
3897 case YANG_UNITS:
3898 switch (dev_mod) {
3899 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003900 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003901 return LY_EVALID;
3902 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003903 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, d_units, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003904 break;
3905 }
3906 break;
3907 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003908 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003909 break;
3910 default:
David Sedlákb3077192019-06-19 10:55:37 +02003911 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003912 return LY_EVALID;
3913 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003914 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003915 return ret;
3916}
3917
Michal Vaskoea5abea2018-09-18 13:10:54 +02003918/**
3919 * @brief Parse the deviation statement.
3920 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003921 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003922 * @param[in,out] data Data to read from, always moved to currently handled character.
3923 * @param[in,out] deviations Deviations to add to.
3924 *
3925 * @return LY_ERR values.
3926 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003927LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003928parse_deviation(struct lys_parser_ctx *ctx, const char **data, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003929{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003930 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003931 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003932 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003933 enum yang_keyword kw;
3934 struct lysp_deviation *dev;
3935
Radek Krejci2c4e7172018-10-19 15:56:26 +02003936 LY_ARRAY_NEW_RET(ctx->ctx, *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003937
3938 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003939 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
David Sedlákb9b892c2019-07-12 14:44:02 +02003940 YANG_CHECK_NONEMPTY(ctx, word_len, "deviation");
Radek Krejci44ceedc2018-10-02 15:54:31 +02003941 INSERT_WORD(ctx, buf, dev->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003942
Radek Krejci6d6556c2018-11-08 09:37:45 +01003943 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003944 switch (kw) {
3945 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003946 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &dev->dsc, Y_STR_ARG, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003947 break;
3948 case YANG_DEVIATE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003949 LY_CHECK_RET(parse_deviate(ctx, data, &dev->deviates));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003950 break;
3951 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003952 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &dev->ref, Y_STR_ARG, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003953 break;
3954 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003955 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003956 break;
3957 default:
David Sedlákb3077192019-06-19 10:55:37 +02003958 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003959 return LY_EVALID;
3960 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003961 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003962 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01003963checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003964 /* mandatory substatements */
3965 if (!dev->deviates) {
David Sedlákb3077192019-06-19 10:55:37 +02003966 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003967 return LY_EVALID;
3968 }
3969
3970 return ret;
3971}
3972
Michal Vaskoea5abea2018-09-18 13:10:54 +02003973/**
3974 * @brief Parse the feature statement.
3975 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003976 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003977 * @param[in,out] data Data to read from, always moved to currently handled character.
3978 * @param[in,out] features Features to add to.
3979 *
3980 * @return LY_ERR values.
3981 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003982LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003983parse_feature(struct lys_parser_ctx *ctx, const char **data, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003984{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003985 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003986 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003987 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003988 enum yang_keyword kw;
3989 struct lysp_feature *feat;
3990
Radek Krejci2c4e7172018-10-19 15:56:26 +02003991 LY_ARRAY_NEW_RET(ctx->ctx, *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003992
3993 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003994 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003995 INSERT_WORD(ctx, buf, feat->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01003996
Radek Krejci6d6556c2018-11-08 09:37:45 +01003997 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003998 switch (kw) {
3999 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004000 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &feat->dsc, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004001 break;
4002 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004003 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &feat->iffeatures, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004004 break;
4005 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004006 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &feat->ref, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004007 break;
4008 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004009 LY_CHECK_RET(parse_status(ctx, data, &feat->flags, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004010 break;
4011 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004012 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004013 break;
4014 default:
David Sedlákb3077192019-06-19 10:55:37 +02004015 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004016 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004017 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004018 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004019 return ret;
4020}
4021
Michal Vaskoea5abea2018-09-18 13:10:54 +02004022/**
4023 * @brief Parse the identity statement.
4024 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004025 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004026 * @param[in,out] data Data to read from, always moved to currently handled character.
4027 * @param[in,out] identities Identities to add to.
4028 *
4029 * @return LY_ERR values.
4030 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004031LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02004032parse_identity(struct lys_parser_ctx *ctx, const char **data, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004033{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004034 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004035 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004036 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004037 enum yang_keyword kw;
4038 struct lysp_ident *ident;
4039
Radek Krejci2c4e7172018-10-19 15:56:26 +02004040 LY_ARRAY_NEW_RET(ctx->ctx, *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004041
4042 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02004043 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02004044 INSERT_WORD(ctx, buf, ident->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004045
Radek Krejci6d6556c2018-11-08 09:37:45 +01004046 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004047 switch (kw) {
4048 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004049 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &ident->dsc, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004050 break;
4051 case YANG_IF_FEATURE:
Radek Krejciceaf2122019-01-02 15:03:26 +01004052 YANG_CHECK_STMTVER2_RET(ctx, "if-feature", "identity");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004053 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &ident->iffeatures, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004054 break;
4055 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004056 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &ident->ref, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004057 break;
4058 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004059 LY_CHECK_RET(parse_status(ctx, data, &ident->flags, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004060 break;
4061 case YANG_BASE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004062 if (ident->bases && ctx->mod_version < 2) {
David Sedlákb3077192019-06-19 10:55:37 +02004063 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Identity can be derived from multiple base identities only in YANG 1.1 modules");
Radek Krejci10113652018-11-14 16:56:50 +01004064 return LY_EVALID;
4065 }
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004066 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_BASE, &ident->bases, Y_PREF_IDENTIF_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004067 break;
4068 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004069 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004070 break;
4071 default:
David Sedlákb3077192019-06-19 10:55:37 +02004072 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004073 return LY_EVALID;
4074 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004075 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004076 return ret;
4077}
4078
Michal Vaskoea5abea2018-09-18 13:10:54 +02004079/**
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004080 * @brief Parse module substatements.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004081 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004082 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004083 * @param[in,out] data Data to read from, always moved to currently handled character.
4084 * @param[in,out] mod Module to write to.
4085 *
4086 * @return LY_ERR values.
4087 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004088LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02004089parse_module(struct lys_parser_ctx *ctx, const char **data, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004090{
4091 LY_ERR ret = 0;
4092 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004093 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004094 enum yang_keyword kw, prev_kw = 0;
4095 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004096 struct lysp_submodule *dup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004097
4098 /* (sub)module name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02004099 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004100 INSERT_WORD(ctx, buf, mod->mod->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004101
Radek Krejci6d6556c2018-11-08 09:37:45 +01004102 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004103
Radek Krejcie3846472018-10-15 15:24:51 +02004104#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004105 if (mod_stmt > SECTION) {LOGVAL_PARSER(ctx, LY_VCODE_INORD, ly_stmt2str(kw), ly_stmt2str(prev_kw)); return LY_EVALID;}mod_stmt = SECTION
Radek Krejcie3846472018-10-15 15:24:51 +02004106
Michal Vasko7fbc8162018-09-17 10:35:16 +02004107 switch (kw) {
4108 /* module header */
4109 case YANG_NAMESPACE:
4110 case YANG_PREFIX:
Radek Krejcie3846472018-10-15 15:24:51 +02004111 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4112 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004113 case YANG_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004114 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004115 break;
4116 /* linkage */
4117 case YANG_INCLUDE:
4118 case YANG_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004119 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004120 break;
4121 /* meta */
4122 case YANG_ORGANIZATION:
4123 case YANG_CONTACT:
4124 case YANG_DESCRIPTION:
4125 case YANG_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004126 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004127 break;
4128
4129 /* revision */
4130 case YANG_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004131 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004132 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004133 /* body */
4134 case YANG_ANYDATA:
4135 case YANG_ANYXML:
4136 case YANG_AUGMENT:
4137 case YANG_CHOICE:
4138 case YANG_CONTAINER:
4139 case YANG_DEVIATION:
4140 case YANG_EXTENSION:
4141 case YANG_FEATURE:
4142 case YANG_GROUPING:
4143 case YANG_IDENTITY:
4144 case YANG_LEAF:
4145 case YANG_LEAF_LIST:
4146 case YANG_LIST:
4147 case YANG_NOTIFICATION:
4148 case YANG_RPC:
4149 case YANG_TYPEDEF:
4150 case YANG_USES:
4151 case YANG_CUSTOM:
4152 mod_stmt = Y_MOD_BODY;
4153 break;
4154 default:
4155 /* error handled in the next switch */
4156 break;
4157 }
Radek Krejcie3846472018-10-15 15:24:51 +02004158#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004159
Radek Krejcie3846472018-10-15 15:24:51 +02004160 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004161 switch (kw) {
4162 /* module header */
4163 case YANG_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004164 LY_CHECK_RET(parse_yangversion(ctx, data, &mod->mod->version, &mod->exts));
4165 ctx->mod_version = mod->mod->version;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004166 break;
4167 case YANG_NAMESPACE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004168 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_NAMESPACE, 0, &mod->mod->ns, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004169 break;
4170 case YANG_PREFIX:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004171 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_PREFIX, 0, &mod->mod->prefix, Y_IDENTIF_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004172 break;
4173
4174 /* linkage */
4175 case YANG_INCLUDE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004176 LY_CHECK_RET(parse_include(ctx, mod->mod->name, data, &mod->includes));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004177 break;
4178 case YANG_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004179 LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, data, &mod->imports));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004180 break;
4181
4182 /* meta */
4183 case YANG_ORGANIZATION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004184 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_ORGANIZATION, 0, &mod->mod->org, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004185 break;
4186 case YANG_CONTACT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004187 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_CONTACT, 0, &mod->mod->contact, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004188 break;
4189 case YANG_DESCRIPTION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004190 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &mod->mod->dsc, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004191 break;
4192 case YANG_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004193 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &mod->mod->ref, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004194 break;
4195
4196 /* revision */
4197 case YANG_REVISION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004198 LY_CHECK_RET(parse_revision(ctx, data, &mod->revs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004199 break;
4200
4201 /* body */
4202 case YANG_ANYDATA:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004203 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "module");
Radek Krejci10113652018-11-14 16:56:50 +01004204 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004205 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004206 LY_CHECK_RET(parse_any(ctx, data, kw, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004207 break;
4208 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004209 LY_CHECK_RET(parse_choice(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004210 break;
4211 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004212 LY_CHECK_RET(parse_container(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004213 break;
4214 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004215 LY_CHECK_RET(parse_leaf(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004216 break;
4217 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004218 LY_CHECK_RET(parse_leaflist(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004219 break;
4220 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004221 LY_CHECK_RET(parse_list(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004222 break;
4223 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004224 LY_CHECK_RET(parse_uses(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004225 break;
4226
4227 case YANG_AUGMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004228 LY_CHECK_RET(parse_augment(ctx, data, NULL, &mod->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004229 break;
4230 case YANG_DEVIATION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004231 LY_CHECK_RET(parse_deviation(ctx, data, &mod->deviations));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004232 break;
4233 case YANG_EXTENSION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004234 LY_CHECK_RET(parse_extension(ctx, data, &mod->extensions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004235 break;
4236 case YANG_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004237 LY_CHECK_RET(parse_feature(ctx, data, &mod->features));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004238 break;
4239 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004240 LY_CHECK_RET(parse_grouping(ctx, data, NULL, &mod->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004241 break;
4242 case YANG_IDENTITY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004243 LY_CHECK_RET(parse_identity(ctx, data, &mod->identities));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004244 break;
4245 case YANG_NOTIFICATION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004246 LY_CHECK_RET(parse_notif(ctx, data, NULL, &mod->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004247 break;
4248 case YANG_RPC:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004249 LY_CHECK_RET(parse_action(ctx, data, NULL, &mod->rpcs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004250 break;
4251 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01004252 LY_CHECK_RET(parse_typedef(ctx, NULL, data, &mod->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004253 break;
4254 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004255 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004256 break;
4257
4258 default:
David Sedlákb3077192019-06-19 10:55:37 +02004259 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004260 return LY_EVALID;
4261 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004262 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004263 LY_CHECK_RET(ret);
Radek Krejcie86bf772018-12-14 11:39:53 +01004264
Radek Krejci6d6556c2018-11-08 09:37:45 +01004265checks:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004266 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02004267 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, mod->groupings, mod->augments, mod->rpcs, mod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004268
Michal Vasko7fbc8162018-09-17 10:35:16 +02004269 /* mandatory substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004270 if (!mod->mod->ns) {
David Sedlákb3077192019-06-19 10:55:37 +02004271 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004272 return LY_EVALID;
4273 } else if (!mod->mod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004274 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004275 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004276 }
4277
Radek Krejcie9e987e2018-10-31 12:50:27 +01004278 /* submodules share the namespace with the module names, so there must not be
4279 * a submodule of the same name in the context, no need for revision matching */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004280 dup = ly_ctx_get_submodule(ctx->ctx, NULL, mod->mod->name, NULL);
4281 if (dup) {
David Sedlákb3077192019-06-19 10:55:37 +02004282 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Name collision between module and submodule of name \"%s\".", mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004283 return LY_EVALID;
4284 }
4285
4286 return ret;
4287}
4288
4289/**
4290 * @brief Parse submodule substatements.
4291 *
4292 * @param[in] ctx yang parser context for logging.
4293 * @param[in,out] data Data to read from, always moved to currently handled character.
4294 * @param[out] submod Parsed submodule structure.
4295 *
4296 * @return LY_ERR values.
4297 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004298LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02004299parse_submodule(struct lys_parser_ctx *ctx, const char **data, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004300{
4301 LY_ERR ret = 0;
4302 char *buf, *word;
4303 size_t word_len;
4304 enum yang_keyword kw, prev_kw = 0;
4305 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
4306 struct lysp_submodule *dup;
4307
4308 /* submodule name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02004309 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004310 INSERT_WORD(ctx, buf, submod->name, word, word_len);
4311
4312 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
4313
4314#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004315 if (mod_stmt > SECTION) {LOGVAL_PARSER(ctx, LY_VCODE_INORD, ly_stmt2str(kw), ly_stmt2str(prev_kw)); return LY_EVALID;}mod_stmt = SECTION
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004316
4317 switch (kw) {
4318 /* module header */
4319 case YANG_BELONGS_TO:
4320 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4321 break;
4322 case YANG_YANG_VERSION:
4323 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4324 break;
4325 /* linkage */
4326 case YANG_INCLUDE:
4327 case YANG_IMPORT:
4328 CHECK_ORDER(Y_MOD_LINKAGE);
4329 break;
4330 /* meta */
4331 case YANG_ORGANIZATION:
4332 case YANG_CONTACT:
4333 case YANG_DESCRIPTION:
4334 case YANG_REFERENCE:
4335 CHECK_ORDER(Y_MOD_META);
4336 break;
4337
4338 /* revision */
4339 case YANG_REVISION:
4340 CHECK_ORDER(Y_MOD_REVISION);
4341 break;
4342 /* body */
4343 case YANG_ANYDATA:
4344 case YANG_ANYXML:
4345 case YANG_AUGMENT:
4346 case YANG_CHOICE:
4347 case YANG_CONTAINER:
4348 case YANG_DEVIATION:
4349 case YANG_EXTENSION:
4350 case YANG_FEATURE:
4351 case YANG_GROUPING:
4352 case YANG_IDENTITY:
4353 case YANG_LEAF:
4354 case YANG_LEAF_LIST:
4355 case YANG_LIST:
4356 case YANG_NOTIFICATION:
4357 case YANG_RPC:
4358 case YANG_TYPEDEF:
4359 case YANG_USES:
4360 case YANG_CUSTOM:
4361 mod_stmt = Y_MOD_BODY;
4362 break;
4363 default:
4364 /* error handled in the next switch */
4365 break;
4366 }
4367#undef CHECK_ORDER
4368
4369 prev_kw = kw;
4370 switch (kw) {
4371 /* module header */
4372 case YANG_YANG_VERSION:
4373 LY_CHECK_RET(parse_yangversion(ctx, data, &submod->version, &submod->exts));
4374 ctx->mod_version = submod->version;
4375 break;
4376 case YANG_BELONGS_TO:
4377 LY_CHECK_RET(parse_belongsto(ctx, data, &submod->belongsto, &submod->prefix, &submod->exts));
4378 break;
4379
4380 /* linkage */
4381 case YANG_INCLUDE:
4382 LY_CHECK_RET(parse_include(ctx, submod->name, data, &submod->includes));
4383 break;
4384 case YANG_IMPORT:
4385 LY_CHECK_RET(parse_import(ctx, submod->prefix, data, &submod->imports));
4386 break;
4387
4388 /* meta */
4389 case YANG_ORGANIZATION:
4390 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_ORGANIZATION, 0, &submod->org, Y_STR_ARG, &submod->exts));
4391 break;
4392 case YANG_CONTACT:
4393 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_CONTACT, 0, &submod->contact, Y_STR_ARG, &submod->exts));
4394 break;
4395 case YANG_DESCRIPTION:
4396 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &submod->dsc, Y_STR_ARG, &submod->exts));
4397 break;
4398 case YANG_REFERENCE:
4399 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &submod->ref, Y_STR_ARG, &submod->exts));
4400 break;
4401
4402 /* revision */
4403 case YANG_REVISION:
4404 LY_CHECK_RET(parse_revision(ctx, data, &submod->revs));
4405 break;
4406
4407 /* body */
4408 case YANG_ANYDATA:
4409 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "submodule");
4410 /* fall through */
4411 case YANG_ANYXML:
4412 LY_CHECK_RET(parse_any(ctx, data, kw, NULL, &submod->data));
4413 break;
4414 case YANG_CHOICE:
4415 LY_CHECK_RET(parse_choice(ctx, data, NULL, &submod->data));
4416 break;
4417 case YANG_CONTAINER:
4418 LY_CHECK_RET(parse_container(ctx, data, NULL, &submod->data));
4419 break;
4420 case YANG_LEAF:
4421 LY_CHECK_RET(parse_leaf(ctx, data, NULL, &submod->data));
4422 break;
4423 case YANG_LEAF_LIST:
4424 LY_CHECK_RET(parse_leaflist(ctx, data, NULL, &submod->data));
4425 break;
4426 case YANG_LIST:
4427 LY_CHECK_RET(parse_list(ctx, data, NULL, &submod->data));
4428 break;
4429 case YANG_USES:
4430 LY_CHECK_RET(parse_uses(ctx, data, NULL, &submod->data));
4431 break;
4432
4433 case YANG_AUGMENT:
4434 LY_CHECK_RET(parse_augment(ctx, data, NULL, &submod->augments));
4435 break;
4436 case YANG_DEVIATION:
4437 LY_CHECK_RET(parse_deviation(ctx, data, &submod->deviations));
4438 break;
4439 case YANG_EXTENSION:
4440 LY_CHECK_RET(parse_extension(ctx, data, &submod->extensions));
4441 break;
4442 case YANG_FEATURE:
4443 LY_CHECK_RET(parse_feature(ctx, data, &submod->features));
4444 break;
4445 case YANG_GROUPING:
4446 LY_CHECK_RET(parse_grouping(ctx, data, NULL, &submod->groupings));
4447 break;
4448 case YANG_IDENTITY:
4449 LY_CHECK_RET(parse_identity(ctx, data, &submod->identities));
4450 break;
4451 case YANG_NOTIFICATION:
4452 LY_CHECK_RET(parse_notif(ctx, data, NULL, &submod->notifs));
4453 break;
4454 case YANG_RPC:
4455 LY_CHECK_RET(parse_action(ctx, data, NULL, &submod->rpcs));
4456 break;
4457 case YANG_TYPEDEF:
4458 LY_CHECK_RET(parse_typedef(ctx, NULL, data, &submod->typedefs));
4459 break;
4460 case YANG_CUSTOM:
4461 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &submod->exts));
4462 break;
4463
4464 default:
David Sedlákb3077192019-06-19 10:55:37 +02004465 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004466 return LY_EVALID;
4467 }
4468 }
4469 LY_CHECK_RET(ret);
4470
4471checks:
4472 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02004473 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, submod->groupings, submod->augments, submod->rpcs, submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004474
4475 /* mandatory substatements */
4476 if (!submod->belongsto) {
David Sedlákb3077192019-06-19 10:55:37 +02004477 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004478 return LY_EVALID;
4479 }
4480
4481 /* submodules share the namespace with the module names, so there must not be
4482 * a submodule of the same name in the context, no need for revision matching */
4483 dup = ly_ctx_get_submodule(ctx->ctx, NULL, submod->name, NULL);
4484 if (dup && strcmp(dup->belongsto, submod->belongsto)) {
David Sedlákb3077192019-06-19 10:55:37 +02004485 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Name collision between submodules of name \"%s\".", dup->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +01004486 return LY_EVALID;
4487 }
4488
Michal Vasko7fbc8162018-09-17 10:35:16 +02004489 return ret;
4490}
4491
Radek Krejcid4557c62018-09-17 11:42:09 +02004492LY_ERR
David Sedlák1b623122019-08-05 15:27:49 +02004493yang_parse_submodule(struct lys_parser_ctx **context, struct ly_ctx *ly_ctx, struct lys_parser_ctx *main_ctx, const char *data, struct lysp_submodule **submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004494{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004495 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004496 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004497 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004498 enum yang_keyword kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004499 struct lysp_submodule *mod_p = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004500
David Sedlák1b623122019-08-05 15:27:49 +02004501 /* create context */
4502 *context = calloc(1, sizeof **context);
4503 LY_CHECK_ERR_RET(!(*context), LOGMEM(ly_ctx), LY_EMEM);
4504 (*context)->ctx = ly_ctx;
4505 (*context)->line = 1;
4506
4507 /* map the typedefs and groupings list from main context to the submodule's context */
4508 memcpy(&(*context)->tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
4509 memcpy(&(*context)->grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes);
4510
Michal Vasko7fbc8162018-09-17 10:35:16 +02004511 /* "module"/"submodule" */
David Sedlák1b623122019-08-05 15:27:49 +02004512 ret = get_keyword(*context, &data, &kw, &word, &word_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004513 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004514
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004515 if (kw == YANG_MODULE) {
David Sedlák1b623122019-08-05 15:27:49 +02004516 LOGERR((*context)->ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004517 ret = LY_EINVAL;
4518 goto cleanup;
4519 } else if (kw != YANG_SUBMODULE) {
David Sedlák1b623122019-08-05 15:27:49 +02004520 LOGVAL_PARSER(*context, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".",
Radek Krejcic59bc972018-09-17 16:13:06 +02004521 ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004522 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004523 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004524 }
4525
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004526 mod_p = calloc(1, sizeof *mod_p);
David Sedlák1b623122019-08-05 15:27:49 +02004527 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM((*context)->ctx), cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004528 mod_p->parsing = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004529
4530 /* substatements */
David Sedlák1b623122019-08-05 15:27:49 +02004531 ret = parse_submodule(*context, &data, mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004532 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004533
4534 /* read some trailing spaces or new lines */
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004535 while(*data && isspace(*data)) {
4536 data++;
4537 }
4538 if (*data) {
David Sedlák1b623122019-08-05 15:27:49 +02004539 LOGVAL_PARSER(*context, LYVE_SYNTAX, "Trailing garbage \"%.*s%s\" after submodule, expected end-of-input.",
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004540 15, data, strlen(data) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004541 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004542 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004543 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004544
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004545 mod_p->parsing = 0;
4546 *submod = mod_p;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004547
Radek Krejcibbe09a92018-11-08 09:36:54 +01004548cleanup:
4549 if (ret) {
David Sedlák1b623122019-08-05 15:27:49 +02004550 lysp_submodule_free((*context)->ctx, mod_p);
4551 lys_parser_ctx_free(*context);
4552 *context = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004553 }
4554
4555 return ret;
4556}
4557
4558LY_ERR
David Sedlák1b623122019-08-05 15:27:49 +02004559yang_parse_module(struct lys_parser_ctx **context, const char *data, struct lys_module *mod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004560{
4561 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004562 char *word;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004563 size_t word_len;
4564 enum yang_keyword kw;
4565 struct lysp_module *mod_p = NULL;
4566
David Sedlák1b623122019-08-05 15:27:49 +02004567 /* create context */
4568 *context = calloc(1, sizeof **context);
4569 LY_CHECK_ERR_RET(!(*context), LOGMEM(mod->ctx), LY_EMEM);
4570 (*context)->ctx = mod->ctx;
4571 (*context)->line = 1;
4572
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004573 /* "module"/"submodule" */
David Sedlák1b623122019-08-05 15:27:49 +02004574 ret = get_keyword(*context, &data, &kw, &word, &word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004575 LY_CHECK_GOTO(ret, cleanup);
4576
4577 if (kw == YANG_SUBMODULE) {
David Sedlák1b623122019-08-05 15:27:49 +02004578 LOGERR((*context)->ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004579 ret = LY_EINVAL;
4580 goto cleanup;
4581 } else if (kw != YANG_MODULE) {
David Sedlák1b623122019-08-05 15:27:49 +02004582 LOGVAL_PARSER((*context), LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004583 ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004584 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004585 goto cleanup;
4586 }
4587
4588 mod_p = calloc(1, sizeof *mod_p);
David Sedlák1b623122019-08-05 15:27:49 +02004589 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM((*context)->ctx), cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004590 mod_p->mod = mod;
4591 mod_p->parsing = 1;
4592
4593 /* substatements */
David Sedlák1b623122019-08-05 15:27:49 +02004594 ret = parse_module(*context, &data, mod_p);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004595 LY_CHECK_GOTO(ret, cleanup);
4596
4597 /* read some trailing spaces or new lines */
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004598 while(*data && isspace(*data)) {
4599 data++;
4600 }
4601 if (*data) {
David Sedlák1b623122019-08-05 15:27:49 +02004602 LOGVAL_PARSER(*context, LYVE_SYNTAX, "Trailing garbage \"%.*s%s\" after module, expected end-of-input.",
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004603 15, data, strlen(data) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004604 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004605 goto cleanup;
4606 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004607
4608 mod_p->parsing = 0;
4609 mod->parsed = mod_p;
4610
4611cleanup:
4612 if (ret) {
4613 lysp_module_free(mod_p);
David Sedlák1b623122019-08-05 15:27:49 +02004614 lys_parser_ctx_free(*context);
4615 *context = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004616 }
4617
Michal Vasko7fbc8162018-09-17 10:35:16 +02004618 return ret;
4619}