blob: cb5ae57b11aee20d71df6c518553e3c594bb6c56 [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")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001175 return LY_EVALID;
1176 }
1177
Radek Krejcib7db73a2018-10-24 14:18:40 +02001178 strncpy(rev->date, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001179 free(buf);
1180
Radek Krejci6d6556c2018-11-08 09:37:45 +01001181 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001182 switch (kw) {
1183 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001184 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 +02001185 break;
1186 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001187 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 +02001188 break;
1189 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001190 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001191 break;
1192 default:
David Sedlákb3077192019-06-19 10:55:37 +02001193 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001194 return LY_EVALID;
1195 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001196 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001197 return ret;
1198}
1199
Michal Vaskoea5abea2018-09-18 13:10:54 +02001200/**
1201 * @brief Parse a generic text field that can have more instances such as base.
1202 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001203 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001204 * @param[in,out] data Data to read from, always moved to currently handled character.
1205 * @param[in] substmt Type of this substatement.
1206 * @param[in,out] texts Parsed values to add to.
1207 * @param[in] arg Type of the expected argument.
1208 * @param[in,out] exts Extension instances to add to.
1209 *
1210 * @return LY_ERR values.
1211 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001212static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001213parse_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 +02001214 struct lysp_ext_instance **exts)
1215{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001216 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001217 char *buf, *word;
Radek Krejci151a5b72018-10-19 14:21:44 +02001218 const char **item;
1219 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001220 enum yang_keyword kw;
1221
1222 /* allocate new pointer */
Radek Krejci2c4e7172018-10-19 15:56:26 +02001223 LY_ARRAY_NEW_RET(ctx->ctx, *texts, item, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001224
1225 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001226 LY_CHECK_RET(get_argument(ctx, data, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001227
Radek Krejci151a5b72018-10-19 14:21:44 +02001228 INSERT_WORD(ctx, buf, *item, word, word_len);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001229 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001230 switch (kw) {
1231 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001232 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, substmt, LY_ARRAY_SIZE(*texts) - 1, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001233 break;
1234 default:
David Sedlákb3077192019-06-19 10:55:37 +02001235 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001236 return LY_EVALID;
1237 }
1238 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001239 return ret;
1240}
1241
Michal Vaskoea5abea2018-09-18 13:10:54 +02001242/**
1243 * @brief Parse the config statement.
1244 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001245 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001246 * @param[in,out] data Data to read from, always moved to currently handled character.
1247 * @param[in,out] flags Flags to add to.
1248 * @param[in,out] exts Extension instances to add to.
1249 *
1250 * @return LY_ERR values.
1251 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001252static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001253parse_config(struct lys_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001254{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001255 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001256 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001257 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001258 enum yang_keyword kw;
1259
1260 if (*flags & LYS_CONFIG_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001261 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001262 return LY_EVALID;
1263 }
1264
1265 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001266 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001267
1268 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1269 *flags |= LYS_CONFIG_W;
1270 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1271 *flags |= LYS_CONFIG_R;
1272 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001273 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001274 free(buf);
1275 return LY_EVALID;
1276 }
1277 free(buf);
1278
Radek Krejci6d6556c2018-11-08 09:37:45 +01001279 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001280 switch (kw) {
1281 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001282 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_CONFIG, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001283 break;
1284 default:
David Sedlákb3077192019-06-19 10:55:37 +02001285 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001286 return LY_EVALID;
1287 }
1288 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001289 return ret;
1290}
1291
Michal Vaskoea5abea2018-09-18 13:10:54 +02001292/**
1293 * @brief Parse the mandatory statement.
1294 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001295 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001296 * @param[in,out] data Data to read from, always moved to currently handled character.
1297 * @param[in,out] flags Flags to add to.
1298 * @param[in,out] exts Extension instances to add to.
1299 *
1300 * @return LY_ERR values.
1301 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001302static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001303parse_mandatory(struct lys_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001304{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001305 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001306 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001307 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001308 enum yang_keyword kw;
1309
1310 if (*flags & LYS_MAND_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001311 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001312 return LY_EVALID;
1313 }
1314
1315 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001316 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001317
1318 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1319 *flags |= LYS_MAND_TRUE;
1320 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1321 *flags |= LYS_MAND_FALSE;
1322 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001323 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001324 free(buf);
1325 return LY_EVALID;
1326 }
1327 free(buf);
1328
Radek Krejci6d6556c2018-11-08 09:37:45 +01001329 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001330 switch (kw) {
1331 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001332 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MANDATORY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001333 break;
1334 default:
David Sedlákb3077192019-06-19 10:55:37 +02001335 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001336 return LY_EVALID;
1337 }
1338 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001339 return ret;
1340}
1341
Michal Vaskoea5abea2018-09-18 13:10:54 +02001342/**
1343 * @brief Parse a restriction such as range or length.
1344 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001345 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001346 * @param[in,out] data Data to read from, always moved to currently handled character.
1347 * @param[in] restr_kw Type of this particular restriction.
1348 * @param[in,out] exts Extension instances to add to.
1349 *
1350 * @return LY_ERR values.
1351 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001352static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001353parse_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 +02001354{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001355 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001356 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001357 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001358 enum yang_keyword kw;
1359
1360 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001361 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001362
David Sedlákb9b892c2019-07-12 14:44:02 +02001363 YANG_CHECK_NONEMPTY(ctx, word_len, ly_stmt2str(restr_kw));
Radek Krejci44ceedc2018-10-02 15:54:31 +02001364 INSERT_WORD(ctx, buf, restr->arg, word, word_len);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001365 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001366 switch (kw) {
1367 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001368 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 +02001369 break;
1370 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001371 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 +02001372 break;
1373 case YANG_ERROR_APP_TAG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001374 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 +02001375 break;
1376 case YANG_ERROR_MESSAGE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001377 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 +02001378 break;
1379 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001380 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001381 break;
1382 default:
David Sedlákb3077192019-06-19 10:55:37 +02001383 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001384 return LY_EVALID;
1385 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001386 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001387 return ret;
1388}
1389
Michal Vaskoea5abea2018-09-18 13:10:54 +02001390/**
1391 * @brief Parse a restriction that can have more instances such as must.
1392 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001393 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001394 * @param[in,out] data Data to read from, always moved to currently handled character.
1395 * @param[in] restr_kw Type of this particular restriction.
1396 * @param[in,out] restrs Restrictions to add to.
1397 *
1398 * @return LY_ERR values.
1399 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001400static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001401parse_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 +02001402{
1403 struct lysp_restr *restr;
1404
Radek Krejci2c4e7172018-10-19 15:56:26 +02001405 LY_ARRAY_NEW_RET(ctx->ctx, *restrs, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001406 return parse_restr(ctx, data, restr_kw, restr);
1407}
1408
Michal Vaskoea5abea2018-09-18 13:10:54 +02001409/**
1410 * @brief Parse the status statement.
1411 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001412 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001413 * @param[in,out] data Data to read from, always moved to currently handled character.
1414 * @param[in,out] flags Flags to add to.
1415 * @param[in,out] exts Extension instances to add to.
1416 *
1417 * @return LY_ERR values.
1418 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001419static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001420parse_status(struct lys_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001421{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001422 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001423 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001424 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001425 enum yang_keyword kw;
1426
1427 if (*flags & LYS_STATUS_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001428 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001429 return LY_EVALID;
1430 }
1431
1432 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001433 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001434
1435 if ((word_len == 7) && !strncmp(word, "current", word_len)) {
1436 *flags |= LYS_STATUS_CURR;
1437 } else if ((word_len == 10) && !strncmp(word, "deprecated", word_len)) {
1438 *flags |= LYS_STATUS_DEPRC;
1439 } else if ((word_len == 8) && !strncmp(word, "obsolete", word_len)) {
1440 *flags |= LYS_STATUS_OBSLT;
1441 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001442 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001443 free(buf);
1444 return LY_EVALID;
1445 }
1446 free(buf);
1447
Radek Krejci6d6556c2018-11-08 09:37:45 +01001448 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001449 switch (kw) {
1450 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001451 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_STATUS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001452 break;
1453 default:
David Sedlákb3077192019-06-19 10:55:37 +02001454 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001455 return LY_EVALID;
1456 }
1457 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001458 return ret;
1459}
1460
Michal Vaskoea5abea2018-09-18 13:10:54 +02001461/**
1462 * @brief Parse the when statement.
1463 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001464 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001465 * @param[in,out] data Data to read from, always moved to currently handled character.
1466 * @param[in,out] when_p When pointer to parse to.
1467 *
1468 * @return LY_ERR values.
1469 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001470LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001471parse_when(struct lys_parser_ctx *ctx, const char **data, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001472{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001473 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001474 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001475 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001476 enum yang_keyword kw;
1477 struct lysp_when *when;
1478
1479 if (*when_p) {
David Sedlákb3077192019-06-19 10:55:37 +02001480 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001481 return LY_EVALID;
1482 }
1483
1484 when = calloc(1, sizeof *when);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001485 LY_CHECK_ERR_RET(!when, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001486
1487 /* get value */
Radek Krejci2f54df52019-06-21 10:59:19 +02001488 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 +02001489 YANG_CHECK_NONEMPTY(ctx, word_len, "when");
Radek Krejci44ceedc2018-10-02 15:54:31 +02001490 INSERT_WORD(ctx, buf, when->cond, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001491
Radek Krejcif09e4e82019-06-14 15:08:11 +02001492 *when_p = when;
1493
Radek Krejci6d6556c2018-11-08 09:37:45 +01001494 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001495 switch (kw) {
1496 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001497 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 +02001498 break;
1499 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001500 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 +02001501 break;
1502 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001503 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001504 break;
1505 default:
David Sedlákb3077192019-06-19 10:55:37 +02001506 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001507 return LY_EVALID;
1508 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001509 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001510 return ret;
1511}
1512
Michal Vaskoea5abea2018-09-18 13:10:54 +02001513/**
1514 * @brief Parse the anydata or anyxml statement.
1515 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001516 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001517 * @param[in,out] data Data to read from, always moved to currently handled character.
1518 * @param[in] kw Type of this particular keyword.
1519 * @param[in,out] siblings Siblings to add to.
1520 *
1521 * @return LY_ERR values.
1522 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02001523LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001524parse_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 +02001525{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001526 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001527 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001528 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001529 struct lysp_node *iter;
1530 struct lysp_node_anydata *any;
1531
1532 /* create structure */
1533 any = calloc(1, sizeof *any);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001534 LY_CHECK_ERR_RET(!any, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001535 any->nodetype = kw == YANG_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001536 any->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001537
1538 /* insert into siblings */
1539 if (!*siblings) {
1540 *siblings = (struct lysp_node *)any;
1541 } else {
1542 for (iter = *siblings; iter->next; iter = iter->next);
1543 iter->next = (struct lysp_node *)any;
1544 }
1545
1546 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001547 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02001548 INSERT_WORD(ctx, buf, any->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001549
1550 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01001551 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001552 switch (kw) {
1553 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001554 LY_CHECK_RET(parse_config(ctx, data, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001555 break;
1556 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001557 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 +02001558 break;
1559 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001560 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 +02001561 break;
1562 case YANG_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001563 LY_CHECK_RET(parse_mandatory(ctx, data, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001564 break;
1565 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001566 LY_CHECK_RET(parse_restrs(ctx, data, kw, &any->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001567 break;
1568 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001569 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 +02001570 break;
1571 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001572 LY_CHECK_RET(parse_status(ctx, data, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001573 break;
1574 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001575 LY_CHECK_RET(parse_when(ctx, data, &any->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001576 break;
1577 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001578 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001579 break;
1580 default:
David Sedlákb3077192019-06-19 10:55:37 +02001581 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
Radek Krejcic59bc972018-09-17 16:13:06 +02001582 (any->nodetype & LYS_ANYDATA) == LYS_ANYDATA ? ly_stmt2str(YANG_ANYDATA) : ly_stmt2str(YANG_ANYXML));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001583 return LY_EVALID;
1584 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001585 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001586 return ret;
1587}
1588
Michal Vaskoea5abea2018-09-18 13:10:54 +02001589/**
1590 * @brief Parse the value or position statement. Substatement of type enum statement.
1591 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001592 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001593 * @param[in,out] data Data to read from, always moved to currently handled character.
1594 * @param[in] val_kw Type of this particular keyword.
1595 * @param[in,out] value Value to write to.
1596 * @param[in,out] flags Flags to write to.
1597 * @param[in,out] exts Extension instances to add to.
1598 *
1599 * @return LY_ERR values.
1600 */
David Sedlákd6ce6d72019-07-16 17:30:18 +02001601LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001602parse_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 +02001603 struct lysp_ext_instance **exts)
1604{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001605 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001606 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001607 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001608 long int num;
1609 unsigned long int unum;
1610 enum yang_keyword kw;
1611
1612 if (*flags & LYS_SET_VALUE) {
David Sedlákb3077192019-06-19 10:55:37 +02001613 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001614 return LY_EVALID;
1615 }
1616 *flags |= LYS_SET_VALUE;
1617
1618 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001619 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001620
David Sedlákd6ce6d72019-07-16 17:30:18 +02001621 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 +02001622 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001623 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001624 }
1625
1626 errno = 0;
1627 if (val_kw == YANG_VALUE) {
1628 num = strtol(word, &ptr, 10);
Radek Krejci8b764662018-11-14 14:15:13 +01001629 if (num < INT64_C(-2147483648) || num > INT64_C(2147483647)) {
David Sedlákb3077192019-06-19 10:55:37 +02001630 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001631 goto error;
1632 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001633 } else {
1634 unum = strtoul(word, &ptr, 10);
Radek Krejci8b764662018-11-14 14:15:13 +01001635 if (unum > UINT64_C(4294967295)) {
David Sedlákb3077192019-06-19 10:55:37 +02001636 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001637 goto error;
1638 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001639 }
1640 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001641 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001642 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001643 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001644 }
1645 if (errno == ERANGE) {
David Sedlákb3077192019-06-19 10:55:37 +02001646 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001647 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001648 }
1649 if (val_kw == YANG_VALUE) {
1650 *value = num;
1651 } else {
1652 *value = unum;
1653 }
1654 free(buf);
1655
Radek Krejci6d6556c2018-11-08 09:37:45 +01001656 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001657 switch (kw) {
1658 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001659 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 +02001660 break;
1661 default:
David Sedlákb3077192019-06-19 10:55:37 +02001662 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001663 return LY_EVALID;
1664 }
1665 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001666 return ret;
Radek Krejci8b764662018-11-14 14:15:13 +01001667
1668error:
1669 free(buf);
1670 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001671}
1672
Michal Vaskoea5abea2018-09-18 13:10:54 +02001673/**
1674 * @brief Parse the enum or bit statement. Substatement of type statement.
1675 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001676 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001677 * @param[in,out] data Data to read from, always moved to currently handled character.
1678 * @param[in] enum_kw Type of this particular keyword.
1679 * @param[in,out] enums Enums or bits to add to.
1680 *
1681 * @return LY_ERR values.
1682 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001683static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001684parse_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 +02001685{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001686 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001687 char *buf, *word;
David Sedlák6544c182019-07-12 13:17:33 +02001688 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001689 enum yang_keyword kw;
1690 struct lysp_type_enum *enm;
1691
Radek Krejci2c4e7172018-10-19 15:56:26 +02001692 LY_ARRAY_NEW_RET(ctx->ctx, *enums, enm, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001693
1694 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001695 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 +01001696 if (enum_kw == YANG_ENUM) {
David Sedlák07869a52019-07-12 14:28:19 +02001697 ret = lysp_check_enum_name(ctx, (const char *)word, word_len);
David Sedlák6544c182019-07-12 13:17:33 +02001698 LY_CHECK_ERR_RET(ret, free(buf), ret);
Radek Krejci8b764662018-11-14 14:15:13 +01001699 } else { /* YANG_BIT */
1700
1701 }
Radek Krejcif09e4e82019-06-14 15:08:11 +02001702 if (enum_kw == YANG_ENUM) {
David Sedlákb9b892c2019-07-12 14:44:02 +02001703 YANG_CHECK_NONEMPTY(ctx, word_len, "enum");
Radek Krejcif09e4e82019-06-14 15:08:11 +02001704 }
Radek Krejci44ceedc2018-10-02 15:54:31 +02001705 INSERT_WORD(ctx, buf, enm->name, word, word_len);
Radek Krejci8b764662018-11-14 14:15:13 +01001706 CHECK_UNIQUENESS(ctx, *enums, name, ly_stmt2str(enum_kw), enm->name);
1707
Radek Krejci6d6556c2018-11-08 09:37:45 +01001708 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001709 switch (kw) {
1710 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001711 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 +02001712 break;
1713 case YANG_IF_FEATURE:
Radek Krejciceaf2122019-01-02 15:03:26 +01001714 YANG_CHECK_STMTVER2_RET(ctx, "if-feature", ly_stmt2str(enum_kw));
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001715 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 +02001716 break;
1717 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001718 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 +02001719 break;
1720 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001721 LY_CHECK_RET(parse_status(ctx, data, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001722 break;
1723 case YANG_VALUE:
David Sedlák9fb515f2019-07-11 10:33:58 +02001724 LY_CHECK_ERR_RET(enum_kw == YANG_BIT, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
1725 ly_stmt2str(enum_kw)), LY_EVALID);
1726 LY_CHECK_RET(parse_type_enum_value_pos(ctx, data, kw, &enm->value, &enm->flags, &enm->exts));
1727 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001728 case YANG_POSITION:
David Sedlák9fb515f2019-07-11 10:33:58 +02001729 LY_CHECK_ERR_RET(enum_kw == YANG_ENUM, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
1730 ly_stmt2str(enum_kw)), LY_EVALID);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001731 LY_CHECK_RET(parse_type_enum_value_pos(ctx, data, kw, &enm->value, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001732 break;
1733 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001734 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001735 break;
1736 default:
David Sedlákb3077192019-06-19 10:55:37 +02001737 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001738 return LY_EVALID;
1739 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001740 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001741 return ret;
1742}
1743
Michal Vaskoea5abea2018-09-18 13:10:54 +02001744/**
1745 * @brief Parse the fraction-digits statement. Substatement of type statement.
1746 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001747 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001748 * @param[in,out] data Data to read from, always moved to currently handled character.
1749 * @param[in,out] fracdig Value to write to.
1750 * @param[in,out] exts Extension instances to add to.
1751 *
1752 * @return LY_ERR values.
1753 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001754static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001755parse_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 +02001756{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001757 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001758 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001759 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001760 unsigned long int num;
1761 enum yang_keyword kw;
1762
1763 if (*fracdig) {
David Sedlákb3077192019-06-19 10:55:37 +02001764 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001765 return LY_EVALID;
1766 }
1767
1768 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001769 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001770
1771 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
David Sedlákb3077192019-06-19 10:55:37 +02001772 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001773 free(buf);
1774 return LY_EVALID;
1775 }
1776
1777 errno = 0;
1778 num = strtoul(word, &ptr, 10);
1779 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001780 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001781 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001782 free(buf);
1783 return LY_EVALID;
1784 }
1785 if ((errno == ERANGE) || (num > 18)) {
David Sedlákb3077192019-06-19 10:55:37 +02001786 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001787 free(buf);
1788 return LY_EVALID;
1789 }
1790 *fracdig = num;
1791 free(buf);
1792
Radek Krejci6d6556c2018-11-08 09:37:45 +01001793 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001794 switch (kw) {
1795 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001796 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_FRACDIGITS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001797 break;
1798 default:
David Sedlákb3077192019-06-19 10:55:37 +02001799 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001800 return LY_EVALID;
1801 }
1802 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001803 return ret;
1804}
1805
Michal Vaskoea5abea2018-09-18 13:10:54 +02001806/**
1807 * @brief Parse the require-instance statement. Substatement of type statement.
1808 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001809 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001810 * @param[in,out] data Data to read from, always moved to currently handled character.
1811 * @param[in,out] reqinst Value to write to.
1812 * @param[in,out] flags Flags to write to.
1813 * @param[in,out] exts Extension instances to add to.
1814 *
1815 * @return LY_ERR values.
1816 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001817static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001818parse_type_reqinstance(struct lys_parser_ctx *ctx, const char **data, uint8_t *reqinst, uint16_t *flags,
Michal Vasko7fbc8162018-09-17 10:35:16 +02001819 struct lysp_ext_instance **exts)
1820{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001821 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001822 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001823 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001824 enum yang_keyword kw;
1825
1826 if (*flags & LYS_SET_REQINST) {
David Sedlákb3077192019-06-19 10:55:37 +02001827 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001828 return LY_EVALID;
1829 }
1830 *flags |= LYS_SET_REQINST;
1831
1832 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001833 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001834
1835 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1836 *reqinst = 1;
1837 } else if ((word_len != 5) || strncmp(word, "false", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001838 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001839 free(buf);
1840 return LY_EVALID;
1841 }
1842 free(buf);
1843
Radek Krejci6d6556c2018-11-08 09:37:45 +01001844 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001845 switch (kw) {
1846 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001847 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_REQINSTANCE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001848 break;
1849 default:
David Sedlákb3077192019-06-19 10:55:37 +02001850 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001851 return LY_EVALID;
1852 }
1853 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001854 return ret;
1855}
1856
Michal Vaskoea5abea2018-09-18 13:10:54 +02001857/**
1858 * @brief Parse the modifier statement. Substatement of type pattern statement.
1859 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001860 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001861 * @param[in,out] data Data to read from, always moved to currently handled character.
1862 * @param[in,out] pat Value to write to.
1863 * @param[in,out] exts Extension instances to add to.
1864 *
1865 * @return LY_ERR values.
1866 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001867static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001868parse_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 +02001869{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001870 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001871 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001872 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001873 enum yang_keyword kw;
1874
1875 if ((*pat)[0] == 0x15) {
David Sedlákb3077192019-06-19 10:55:37 +02001876 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001877 return LY_EVALID;
1878 }
1879
1880 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001881 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001882
1883 if ((word_len != 12) || strncmp(word, "invert-match", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001884 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001885 free(buf);
1886 return LY_EVALID;
1887 }
1888 free(buf);
1889
1890 /* replace the value in the dictionary */
1891 buf = malloc(strlen(*pat) + 1);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001892 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001893 strcpy(buf, *pat);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001894 lydict_remove(ctx->ctx, *pat);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001895
1896 assert(buf[0] == 0x06);
1897 buf[0] = 0x15;
Radek Krejci44ceedc2018-10-02 15:54:31 +02001898 *pat = lydict_insert_zc(ctx->ctx, buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001899
Radek Krejci6d6556c2018-11-08 09:37:45 +01001900 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001901 switch (kw) {
1902 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001903 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MODIFIER, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001904 break;
1905 default:
David Sedlákb3077192019-06-19 10:55:37 +02001906 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001907 return LY_EVALID;
1908 }
1909 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001910 return ret;
1911}
1912
Michal Vaskoea5abea2018-09-18 13:10:54 +02001913/**
1914 * @brief Parse the pattern statement. Substatement of type statement.
1915 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001916 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001917 * @param[in,out] data Data to read from, always moved to currently handled character.
1918 * @param[in,out] patterns Restrictions to add to.
1919 *
1920 * @return LY_ERR values.
1921 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001922static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001923parse_type_pattern(struct lys_parser_ctx *ctx, const char **data, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001924{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001925 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001926 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001927 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001928 enum yang_keyword kw;
1929 struct lysp_restr *restr;
1930
Radek Krejci2c4e7172018-10-19 15:56:26 +02001931 LY_ARRAY_NEW_RET(ctx->ctx, *patterns, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001932
1933 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001934 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001935
1936 /* add special meaning first byte */
1937 if (buf) {
1938 buf = realloc(buf, word_len + 2);
1939 word = buf;
1940 } else {
1941 buf = malloc(word_len + 2);
1942 }
Radek Krejci44ceedc2018-10-02 15:54:31 +02001943 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejci86d106e2018-10-18 09:53:19 +02001944 memmove(buf + 1, word, word_len);
1945 buf[0] = 0x06; /* pattern's default regular-match flag */
1946 buf[word_len + 1] = '\0'; /* terminating NULL byte */
1947 restr->arg = lydict_insert_zc(ctx->ctx, buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001948
Radek Krejci6d6556c2018-11-08 09:37:45 +01001949 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001950 switch (kw) {
1951 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001952 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 +02001953 break;
1954 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001955 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 +02001956 break;
1957 case YANG_ERROR_APP_TAG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001958 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 +02001959 break;
1960 case YANG_ERROR_MESSAGE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001961 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 +02001962 break;
1963 case YANG_MODIFIER:
Radek Krejciceaf2122019-01-02 15:03:26 +01001964 YANG_CHECK_STMTVER2_RET(ctx, "modifier", "pattern");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001965 LY_CHECK_RET(parse_type_pattern_modifier(ctx, data, &restr->arg, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001966 break;
1967 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001968 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001969 break;
1970 default:
David Sedlákb3077192019-06-19 10:55:37 +02001971 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001972 return LY_EVALID;
1973 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001974 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001975 return ret;
1976}
1977
Michal Vaskoea5abea2018-09-18 13:10:54 +02001978/**
1979 * @brief Parse the type statement.
1980 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001981 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001982 * @param[in,out] data Data to read from, always moved to currently handled character.
1983 * @param[in,out] type Type to wrote to.
1984 *
1985 * @return LY_ERR values.
1986 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001987static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02001988parse_type(struct lys_parser_ctx *ctx, const char **data, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001989{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001990 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001991 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001992 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001993 enum yang_keyword kw;
1994 struct lysp_type *nest_type;
1995
1996 if (type->name) {
David Sedlákb3077192019-06-19 10:55:37 +02001997 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001998 return LY_EVALID;
1999 }
2000
2001 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002002 LY_CHECK_RET(get_argument(ctx, data, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002003 INSERT_WORD(ctx, buf, type->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002004
Radek Krejci6d6556c2018-11-08 09:37:45 +01002005 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002006 switch (kw) {
2007 case YANG_BASE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002008 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 +01002009 type->flags |= LYS_SET_BASE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002010 break;
2011 case YANG_BIT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002012 LY_CHECK_RET(parse_type_enum(ctx, data, kw, &type->bits));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002013 type->flags |= LYS_SET_BIT;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002014 break;
2015 case YANG_ENUM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002016 LY_CHECK_RET(parse_type_enum(ctx, data, kw, &type->enums));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002017 type->flags |= LYS_SET_ENUM;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002018 break;
2019 case YANG_FRACTION_DIGITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002020 LY_CHECK_RET(parse_type_fracdigits(ctx, data, &type->fraction_digits, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002021 type->flags |= LYS_SET_FRDIGITS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002022 break;
2023 case YANG_LENGTH:
2024 if (type->length) {
David Sedlákb3077192019-06-19 10:55:37 +02002025 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002026 return LY_EVALID;
2027 }
2028 type->length = calloc(1, sizeof *type->length);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002029 LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002030
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002031 LY_CHECK_RET(parse_restr(ctx, data, kw, type->length));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002032 type->flags |= LYS_SET_LENGTH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002033 break;
2034 case YANG_PATH:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002035 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 +01002036 type->flags |= LYS_SET_PATH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002037 break;
2038 case YANG_PATTERN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002039 LY_CHECK_RET(parse_type_pattern(ctx, data, &type->patterns));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002040 type->flags |= LYS_SET_PATTERN;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002041 break;
2042 case YANG_RANGE:
2043 if (type->range) {
David Sedlákb3077192019-06-19 10:55:37 +02002044 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002045 return LY_EVALID;
2046 }
2047 type->range = calloc(1, sizeof *type->range);
David Sedlák7a8b2472019-07-11 15:08:34 +02002048 LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002049
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002050 LY_CHECK_RET(parse_restr(ctx, data, kw, type->range));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002051 type->flags |= LYS_SET_RANGE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002052 break;
2053 case YANG_REQUIRE_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002054 LY_CHECK_RET(parse_type_reqinstance(ctx, data, &type->require_instance, &type->flags, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002055 /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002056 break;
2057 case YANG_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002058 LY_ARRAY_NEW_RET(ctx->ctx, type->types, nest_type, LY_EMEM);
2059 LY_CHECK_RET(parse_type(ctx, data, nest_type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002060 type->flags |= LYS_SET_TYPE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002061 break;
2062 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002063 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &type->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002064 break;
2065 default:
David Sedlákb3077192019-06-19 10:55:37 +02002066 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002067 return LY_EVALID;
2068 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002069 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002070 return ret;
2071}
2072
Michal Vaskoea5abea2018-09-18 13:10:54 +02002073/**
2074 * @brief Parse the leaf statement.
2075 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002076 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002077 * @param[in,out] data Data to read from, always moved to currently handled character.
2078 * @param[in,out] siblings Siblings to add to.
2079 *
2080 * @return LY_ERR values.
2081 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002082LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002083parse_leaf(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002084{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002085 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002086 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002087 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002088 enum yang_keyword kw;
2089 struct lysp_node *iter;
2090 struct lysp_node_leaf *leaf;
2091
2092 /* create structure */
2093 leaf = calloc(1, sizeof *leaf);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002094 LY_CHECK_ERR_RET(!leaf, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002095 leaf->nodetype = LYS_LEAF;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002096 leaf->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002097
2098 /* insert into siblings */
2099 if (!*siblings) {
2100 *siblings = (struct lysp_node *)leaf;
2101 } else {
2102 for (iter = *siblings; iter->next; iter = iter->next);
2103 iter->next = (struct lysp_node *)leaf;
2104 }
2105
2106 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002107 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002108 INSERT_WORD(ctx, buf, leaf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002109
2110 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01002111 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002112 switch (kw) {
2113 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002114 LY_CHECK_RET(parse_config(ctx, data, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002115 break;
2116 case YANG_DEFAULT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002117 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 +02002118 break;
2119 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002120 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 +02002121 break;
2122 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002123 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 +02002124 break;
2125 case YANG_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002126 LY_CHECK_RET(parse_mandatory(ctx, data, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002127 break;
2128 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002129 LY_CHECK_RET(parse_restrs(ctx, data, kw, &leaf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002130 break;
2131 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002132 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 +02002133 break;
2134 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002135 LY_CHECK_RET(parse_status(ctx, data, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002136 break;
2137 case YANG_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002138 LY_CHECK_RET(parse_type(ctx, data, &leaf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002139 break;
2140 case YANG_UNITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002141 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 +02002142 break;
2143 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002144 LY_CHECK_RET(parse_when(ctx, data, &leaf->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002145 break;
2146 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002147 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002148 break;
2149 default:
David Sedlákb3077192019-06-19 10:55:37 +02002150 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002151 return LY_EVALID;
2152 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002153 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002154 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002155checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002156 /* mandatory substatements */
2157 if (!leaf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002158 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002159 return LY_EVALID;
2160 }
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002161 if ((leaf->flags & LYS_MAND_TRUE) && (leaf->dflt)) {
David Sedlákb3077192019-06-19 10:55:37 +02002162 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMSCOMB, "mandatory", "default", "leaf");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002163 return LY_EVALID;
2164 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002165
2166 return ret;
2167}
2168
Michal Vaskoea5abea2018-09-18 13:10:54 +02002169/**
2170 * @brief Parse the max-elements statement.
2171 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002172 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002173 * @param[in,out] data Data to read from, always moved to currently handled character.
2174 * @param[in,out] max Value to write to.
2175 * @param[in,out] flags Flags to write to.
2176 * @param[in,out] exts Extension instances to add to.
2177 *
2178 * @return LY_ERR values.
2179 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002180LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002181parse_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 +02002182{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002183 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002184 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002185 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002186 unsigned long int num;
2187 enum yang_keyword kw;
2188
2189 if (*flags & LYS_SET_MAX) {
David Sedlákb3077192019-06-19 10:55:37 +02002190 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002191 return LY_EVALID;
2192 }
2193 *flags |= LYS_SET_MAX;
2194
2195 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002196 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002197
2198 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
David Sedlákb3077192019-06-19 10:55:37 +02002199 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002200 free(buf);
2201 return LY_EVALID;
2202 }
2203
2204 if (strncmp(word, "unbounded", word_len)) {
2205 errno = 0;
2206 num = strtoul(word, &ptr, 10);
2207 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002208 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002209 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002210 free(buf);
2211 return LY_EVALID;
2212 }
2213 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002214 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002215 free(buf);
2216 return LY_EVALID;
2217 }
2218
2219 *max = num;
2220 }
2221 free(buf);
2222
Radek Krejci6d6556c2018-11-08 09:37:45 +01002223 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002224 switch (kw) {
2225 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002226 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MAX, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002227 break;
2228 default:
David Sedlákb3077192019-06-19 10:55:37 +02002229 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002230 return LY_EVALID;
2231 }
2232 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002233 return ret;
2234}
2235
Michal Vaskoea5abea2018-09-18 13:10:54 +02002236/**
2237 * @brief Parse the min-elements statement.
2238 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002239 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002240 * @param[in,out] data Data to read from, always moved to currently handled character.
2241 * @param[in,out] min Value to write to.
2242 * @param[in,out] flags Flags to write to.
2243 * @param[in,out] exts Extension instances to add to.
2244 *
2245 * @return LY_ERR values.
2246 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002247LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002248parse_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 +02002249{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002250 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002251 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002252 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002253 unsigned long int num;
2254 enum yang_keyword kw;
2255
2256 if (*flags & LYS_SET_MIN) {
David Sedlákb3077192019-06-19 10:55:37 +02002257 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002258 return LY_EVALID;
2259 }
2260 *flags |= LYS_SET_MIN;
2261
2262 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002263 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002264
2265 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
David Sedlákb3077192019-06-19 10:55:37 +02002266 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002267 free(buf);
2268 return LY_EVALID;
2269 }
2270
2271 errno = 0;
2272 num = strtoul(word, &ptr, 10);
2273 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002274 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002275 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002276 free(buf);
2277 return LY_EVALID;
2278 }
2279 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002280 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002281 free(buf);
2282 return LY_EVALID;
2283 }
2284 *min = num;
2285 free(buf);
2286
Radek Krejci6d6556c2018-11-08 09:37:45 +01002287 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002288 switch (kw) {
2289 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002290 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MIN, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002291 break;
2292 default:
David Sedlákb3077192019-06-19 10:55:37 +02002293 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002294 return LY_EVALID;
2295 }
2296 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002297 return ret;
2298}
2299
Michal Vaskoea5abea2018-09-18 13:10:54 +02002300/**
2301 * @brief Parse the ordered-by statement.
2302 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002303 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002304 * @param[in,out] data Data to read from, always moved to currently handled character.
2305 * @param[in,out] flags Flags to write to.
2306 * @param[in,out] exts Extension instances to add to.
2307 *
2308 * @return LY_ERR values.
2309 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002310static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002311parse_orderedby(struct lys_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002312{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002313 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002314 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002315 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002316 enum yang_keyword kw;
2317
2318 if (*flags & LYS_ORDBY_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02002319 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002320 return LY_EVALID;
2321 }
2322
2323 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002324 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002325
2326 if ((word_len == 6) && !strncmp(word, "system", word_len)) {
2327 *flags |= LYS_ORDBY_SYSTEM;
2328 } else if ((word_len == 4) && !strncmp(word, "user", word_len)) {
2329 *flags |= LYS_ORDBY_USER;
2330 } else {
David Sedlákb3077192019-06-19 10:55:37 +02002331 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002332 free(buf);
2333 return LY_EVALID;
2334 }
2335 free(buf);
2336
Radek Krejci6d6556c2018-11-08 09:37:45 +01002337 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002338 switch (kw) {
2339 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002340 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_ORDEREDBY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002341 break;
2342 default:
David Sedlákb3077192019-06-19 10:55:37 +02002343 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002344 return LY_EVALID;
2345 }
2346 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002347 return ret;
2348}
2349
Michal Vaskoea5abea2018-09-18 13:10:54 +02002350/**
2351 * @brief Parse the leaf-list statement.
2352 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002353 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002354 * @param[in,out] data Data to read from, always moved to currently handled character.
2355 * @param[in,out] siblings Siblings to add to.
2356 *
2357 * @return LY_ERR values.
2358 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002359LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002360parse_leaflist(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002361{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002362 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002363 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002364 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002365 enum yang_keyword kw;
2366 struct lysp_node *iter;
2367 struct lysp_node_leaflist *llist;
2368
2369 /* create structure */
2370 llist = calloc(1, sizeof *llist);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002371 LY_CHECK_ERR_RET(!llist, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002372 llist->nodetype = LYS_LEAFLIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002373 llist->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002374
2375 /* insert into siblings */
2376 if (!*siblings) {
2377 *siblings = (struct lysp_node *)llist;
2378 } else {
2379 for (iter = *siblings; iter->next; iter = iter->next);
2380 iter->next = (struct lysp_node *)llist;
2381 }
2382
2383 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002384 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002385 INSERT_WORD(ctx, buf, llist->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002386
2387 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01002388 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002389 switch (kw) {
2390 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002391 LY_CHECK_RET(parse_config(ctx, data, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002392 break;
2393 case YANG_DEFAULT:
Radek Krejciceaf2122019-01-02 15:03:26 +01002394 YANG_CHECK_STMTVER2_RET(ctx, "default", "leaf-list");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002395 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 +02002396 break;
2397 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002398 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 +02002399 break;
2400 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002401 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 +02002402 break;
2403 case YANG_MAX_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002404 LY_CHECK_RET(parse_maxelements(ctx, data, &llist->max, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002405 break;
2406 case YANG_MIN_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002407 LY_CHECK_RET(parse_minelements(ctx, data, &llist->min, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002408 break;
2409 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002410 LY_CHECK_RET(parse_restrs(ctx, data, kw, &llist->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002411 break;
2412 case YANG_ORDERED_BY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002413 LY_CHECK_RET(parse_orderedby(ctx, data, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002414 break;
2415 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002416 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 +02002417 break;
2418 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002419 LY_CHECK_RET(parse_status(ctx, data, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002420 break;
2421 case YANG_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002422 LY_CHECK_RET(parse_type(ctx, data, &llist->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002423 break;
2424 case YANG_UNITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002425 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 +02002426 break;
2427 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002428 LY_CHECK_RET(parse_when(ctx, data, &llist->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002429 break;
2430 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002431 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002432 break;
2433 default:
David Sedlákb3077192019-06-19 10:55:37 +02002434 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002435 return LY_EVALID;
2436 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002437 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002438 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002439checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002440 /* mandatory substatements */
2441 if (!llist->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002442 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002443 return LY_EVALID;
2444 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01002445 if ((llist->min) && (llist->dflts)) {
David Sedlákb3077192019-06-19 10:55:37 +02002446 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMSCOMB, "min-elements", "default", "leaf-list");
Radek Krejci0e5d8382018-11-28 16:37:53 +01002447 return LY_EVALID;
2448 }
Radek Krejcidf6cad12018-11-28 17:10:55 +01002449 if (llist->max && llist->min > llist->max) {
David Sedlákb3077192019-06-19 10:55:37 +02002450 LOGVAL_PARSER(ctx, LYVE_SEMANTICS,
Radek Krejcidf6cad12018-11-28 17:10:55 +01002451 "Invalid combination of min-elements and max-elements: min value %u is bigger than the max value %u.",
2452 llist->min, llist->max);
2453 return LY_EVALID;
2454 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002455
2456 return ret;
2457}
2458
Michal Vaskoea5abea2018-09-18 13:10:54 +02002459/**
2460 * @brief Parse the refine statement.
2461 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002462 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002463 * @param[in,out] data Data to read from, always moved to currently handled character.
2464 * @param[in,out] refines Refines to add to.
2465 *
2466 * @return LY_ERR values.
2467 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002468static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002469parse_refine(struct lys_parser_ctx *ctx, const char **data, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002470{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002471 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002472 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002473 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002474 enum yang_keyword kw;
2475 struct lysp_refine *rf;
2476
Radek Krejci2c4e7172018-10-19 15:56:26 +02002477 LY_ARRAY_NEW_RET(ctx->ctx, *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002478
2479 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002480 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
David Sedlákb9b892c2019-07-12 14:44:02 +02002481 YANG_CHECK_NONEMPTY(ctx, word_len, "refine");
Radek Krejci44ceedc2018-10-02 15:54:31 +02002482 INSERT_WORD(ctx, buf, rf->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002483
Radek Krejci6d6556c2018-11-08 09:37:45 +01002484 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002485 switch (kw) {
2486 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002487 LY_CHECK_RET(parse_config(ctx, data, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002488 break;
2489 case YANG_DEFAULT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002490 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 +02002491 break;
2492 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002493 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 +02002494 break;
2495 case YANG_IF_FEATURE:
Radek Krejciceaf2122019-01-02 15:03:26 +01002496 YANG_CHECK_STMTVER2_RET(ctx, "if-feature", "refine");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002497 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 +02002498 break;
2499 case YANG_MAX_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002500 LY_CHECK_RET(parse_maxelements(ctx, data, &rf->max, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002501 break;
2502 case YANG_MIN_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002503 LY_CHECK_RET(parse_minelements(ctx, data, &rf->min, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002504 break;
2505 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002506 LY_CHECK_RET(parse_restrs(ctx, data, kw, &rf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002507 break;
2508 case YANG_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002509 LY_CHECK_RET(parse_mandatory(ctx, data, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002510 break;
2511 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002512 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 +02002513 break;
2514 case YANG_PRESENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002515 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 +02002516 break;
2517 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002518 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002519 break;
2520 default:
David Sedlákb3077192019-06-19 10:55:37 +02002521 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002522 return LY_EVALID;
2523 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002524 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002525 return ret;
2526}
2527
Michal Vaskoea5abea2018-09-18 13:10:54 +02002528/**
2529 * @brief Parse the typedef statement.
2530 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002531 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002532 * @param[in,out] data Data to read from, always moved to currently handled character.
2533 * @param[in,out] typedefs Typedefs to add to.
2534 *
2535 * @return LY_ERR values.
2536 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002537static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002538parse_typedef(struct lys_parser_ctx *ctx, struct lysp_node *parent, const char **data, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002539{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002540 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002541 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002542 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002543 enum yang_keyword kw;
2544 struct lysp_tpdf *tpdf;
2545
Radek Krejci2c4e7172018-10-19 15:56:26 +02002546 LY_ARRAY_NEW_RET(ctx->ctx, *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002547
2548 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002549 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002550 INSERT_WORD(ctx, buf, tpdf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002551
2552 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01002553 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002554 switch (kw) {
2555 case YANG_DEFAULT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002556 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 +02002557 break;
2558 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002559 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 +02002560 break;
2561 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002562 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 +02002563 break;
2564 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002565 LY_CHECK_RET(parse_status(ctx, data, &tpdf->flags, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002566 break;
2567 case YANG_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002568 LY_CHECK_RET(parse_type(ctx, data, &tpdf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002569 break;
2570 case YANG_UNITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002571 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 +02002572 break;
2573 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002574 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002575 break;
2576 default:
David Sedlákb3077192019-06-19 10:55:37 +02002577 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002578 return LY_EVALID;
2579 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002580 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002581 LY_CHECK_RET(ret);
Radek Krejcibbe09a92018-11-08 09:36:54 +01002582checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002583 /* mandatory substatements */
2584 if (!tpdf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002585 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002586 return LY_EVALID;
2587 }
2588
Radek Krejcibbe09a92018-11-08 09:36:54 +01002589 /* store data for collision check */
Radek Krejci7fc68292019-06-12 13:51:09 +02002590 if (parent && !(parent->nodetype & (LYS_GROUPING | LYS_ACTION | LYS_INOUT | LYS_NOTIF))) {
Radek Krejcibbe09a92018-11-08 09:36:54 +01002591 ly_set_add(&ctx->tpdfs_nodes, parent, 0);
2592 }
2593
Michal Vasko7fbc8162018-09-17 10:35:16 +02002594 return ret;
2595}
2596
Michal Vaskoea5abea2018-09-18 13:10:54 +02002597/**
2598 * @brief Parse the input or output statement.
2599 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002600 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002601 * @param[in,out] data Data to read from, always moved to currently handled character.
2602 * @param[in] kw Type of this particular keyword
2603 * @param[in,out] inout_p Input/output pointer to write to.
2604 *
2605 * @return LY_ERR values.
2606 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002607static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002608parse_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 +02002609{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002610 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002611 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002612 size_t word_len;
Radek Krejci10113652018-11-14 16:56:50 +01002613 enum yang_keyword kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002614
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002615 if (inout_p->nodetype) {
David Sedlákb3077192019-06-19 10:55:37 +02002616 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002617 return LY_EVALID;
2618 }
2619
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002620 /* initiate structure */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002621 inout_p->nodetype = &((struct lysp_action*)parent)->input == inout_p ? LYS_INPUT : LYS_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002622 inout_p->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002623
2624 /* parse substatements */
Radek Krejci7fc68292019-06-12 13:51:09 +02002625 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002626 switch (kw) {
2627 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01002628 YANG_CHECK_STMTVER2_RET(ctx, "anydata", ly_stmt2str(inout_kw));
Radek Krejci10113652018-11-14 16:56:50 +01002629 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002630 case YANG_ANYXML:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002631 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002632 break;
2633 case YANG_CHOICE:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002634 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002635 break;
2636 case YANG_CONTAINER:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002637 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002638 break;
2639 case YANG_LEAF:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002640 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002641 break;
2642 case YANG_LEAF_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002643 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002644 break;
2645 case YANG_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002646 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002647 break;
2648 case YANG_USES:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002649 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002650 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002651 case YANG_TYPEDEF:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002652 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)inout_p, data, &inout_p->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002653 break;
2654 case YANG_MUST:
Radek Krejciceaf2122019-01-02 15:03:26 +01002655 YANG_CHECK_STMTVER2_RET(ctx, "must", ly_stmt2str(inout_kw));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002656 LY_CHECK_RET(parse_restrs(ctx, data, kw, &inout_p->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002657 break;
2658 case YANG_GROUPING:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002659 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)inout_p, &inout_p->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002660 break;
2661 case YANG_CUSTOM:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002662 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inout_p->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002663 break;
2664 default:
David Sedlákb3077192019-06-19 10:55:37 +02002665 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002666 return LY_EVALID;
2667 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002668 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002669 LY_CHECK_RET(ret);
2670checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002671 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02002672 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, inout_p->groupings, NULL, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02002673
Michal Vasko7fbc8162018-09-17 10:35:16 +02002674 return ret;
2675}
2676
Michal Vaskoea5abea2018-09-18 13:10:54 +02002677/**
2678 * @brief Parse the action statement.
2679 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002680 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002681 * @param[in,out] data Data to read from, always moved to currently handled character.
2682 * @param[in,out] actions Actions to add to.
2683 *
2684 * @return LY_ERR values.
2685 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002686LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002687parse_action(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002688{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002689 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002690 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002691 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002692 enum yang_keyword kw;
2693 struct lysp_action *act;
2694
Radek Krejci2c4e7172018-10-19 15:56:26 +02002695 LY_ARRAY_NEW_RET(ctx->ctx, *actions, act, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002696
2697 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002698 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002699 INSERT_WORD(ctx, buf, act->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002700 act->nodetype = LYS_ACTION;
2701 act->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002702
Radek Krejci7fc68292019-06-12 13:51:09 +02002703 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002704 switch (kw) {
2705 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002706 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 +02002707 break;
2708 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002709 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 +02002710 break;
2711 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002712 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 +02002713 break;
2714 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002715 LY_CHECK_RET(parse_status(ctx, data, &act->flags, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002716 break;
2717
2718 case YANG_INPUT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002719 LY_CHECK_RET(parse_inout(ctx, data, kw, (struct lysp_node*)act, &act->input));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002720 break;
2721 case YANG_OUTPUT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002722 LY_CHECK_RET(parse_inout(ctx, data, kw, (struct lysp_node*)act, &act->output));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002723 break;
2724
2725 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01002726 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)act, data, &act->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002727 break;
2728 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002729 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)act, &act->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002730 break;
2731 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002732 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002733 break;
2734 default:
David Sedlákb3077192019-06-19 10:55:37 +02002735 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), parent ? "action" : "rpc");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002736 return LY_EVALID;
2737 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002738 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002739 LY_CHECK_RET(ret);
2740checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002741 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02002742 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, act->groupings, NULL, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02002743
Michal Vasko7fbc8162018-09-17 10:35:16 +02002744 return ret;
2745}
2746
Michal Vaskoea5abea2018-09-18 13:10:54 +02002747/**
2748 * @brief Parse the notification statement.
2749 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002750 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002751 * @param[in,out] data Data to read from, always moved to currently handled character.
2752 * @param[in,out] notifs Notifications to add to.
2753 *
2754 * @return LY_ERR values.
2755 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002756LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002757parse_notif(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002758{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002759 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002760 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002761 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002762 enum yang_keyword kw;
2763 struct lysp_notif *notif;
2764
Radek Krejci2c4e7172018-10-19 15:56:26 +02002765 LY_ARRAY_NEW_RET(ctx->ctx, *notifs, notif, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002766
2767 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002768 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002769 INSERT_WORD(ctx, buf, notif->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002770 notif->nodetype = LYS_NOTIF;
2771 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002772
Radek Krejci7fc68292019-06-12 13:51:09 +02002773 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002774 switch (kw) {
2775 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002776 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 +02002777 break;
2778 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002779 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 +02002780 break;
2781 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002782 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 +02002783 break;
2784 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002785 LY_CHECK_RET(parse_status(ctx, data, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002786 break;
2787
2788 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01002789 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "notification");
Radek Krejci10113652018-11-14 16:56:50 +01002790 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002791 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002792 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002793 break;
2794 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002795 LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002796 break;
2797 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002798 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002799 break;
2800 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002801 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002802 break;
2803 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002804 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002805 break;
2806 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002807 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002808 break;
2809 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002810 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002811 break;
2812
2813 case YANG_MUST:
Radek Krejciceaf2122019-01-02 15:03:26 +01002814 YANG_CHECK_STMTVER2_RET(ctx, "must", "notification");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002815 LY_CHECK_RET(parse_restrs(ctx, data, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002816 break;
2817 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01002818 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)notif, data, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002819 break;
2820 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002821 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002822 break;
2823 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002824 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002825 break;
2826 default:
David Sedlákb3077192019-06-19 10:55:37 +02002827 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002828 return LY_EVALID;
2829 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002830 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002831 LY_CHECK_RET(ret);
2832checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002833 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02002834 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, notif->groupings, NULL, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02002835
Michal Vasko7fbc8162018-09-17 10:35:16 +02002836 return ret;
2837}
2838
Michal Vaskoea5abea2018-09-18 13:10:54 +02002839/**
2840 * @brief Parse the grouping statement.
2841 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002842 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002843 * @param[in,out] data Data to read from, always moved to currently handled character.
2844 * @param[in,out] groupings Groupings to add to.
2845 *
2846 * @return LY_ERR values.
2847 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002848LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002849parse_grouping(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002850{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002851 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002852 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002853 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002854 enum yang_keyword kw;
2855 struct lysp_grp *grp;
2856
Radek Krejci2c4e7172018-10-19 15:56:26 +02002857 LY_ARRAY_NEW_RET(ctx->ctx, *groupings, grp, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002858
2859 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002860 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002861 INSERT_WORD(ctx, buf, grp->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002862 grp->nodetype = LYS_GROUPING;
2863 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002864
Radek Krejci7fc68292019-06-12 13:51:09 +02002865 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002866 switch (kw) {
2867 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002868 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 +02002869 break;
2870 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002871 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 +02002872 break;
2873 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002874 LY_CHECK_RET(parse_status(ctx, data, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002875 break;
2876
2877 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01002878 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "grouping");
Radek Krejci10113652018-11-14 16:56:50 +01002879 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002880 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002881 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002882 break;
2883 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002884 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002885 break;
2886 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002887 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002888 break;
2889 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002890 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002891 break;
2892 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002893 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002894 break;
2895 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002896 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002897 break;
2898 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002899 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002900 break;
2901
2902 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01002903 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)grp, data, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002904 break;
2905 case YANG_ACTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01002906 YANG_CHECK_STMTVER2_RET(ctx, "action", "grouping");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002907 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)grp, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002908 break;
2909 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002910 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)grp, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002911 break;
2912 case YANG_NOTIFICATION:
Radek Krejciceaf2122019-01-02 15:03:26 +01002913 YANG_CHECK_STMTVER2_RET(ctx, "notification", "grouping");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002914 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)grp, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002915 break;
2916 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002917 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002918 break;
2919 default:
David Sedlákb3077192019-06-19 10:55:37 +02002920 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002921 return LY_EVALID;
2922 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002923 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002924 LY_CHECK_RET(ret);
2925checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002926 /* finalize parent pointers to the reallocated items */
Radek Krejci7fc68292019-06-12 13:51:09 +02002927 LY_CHECK_RET(parse_finalize_reallocated(ctx, grp->groupings, NULL, grp->actions, grp->notifs));
2928
Michal Vasko7fbc8162018-09-17 10:35:16 +02002929 return ret;
2930}
2931
Michal Vaskoea5abea2018-09-18 13:10:54 +02002932/**
2933 * @brief Parse the refine statement.
2934 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002935 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002936 * @param[in,out] data Data to read from, always moved to currently handled character.
2937 * @param[in,out] augments Augments to add to.
2938 *
2939 * @return LY_ERR values.
2940 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002941LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02002942parse_augment(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002943{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002944 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002945 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002946 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002947 enum yang_keyword kw;
2948 struct lysp_augment *aug;
2949
Radek Krejci2c4e7172018-10-19 15:56:26 +02002950 LY_ARRAY_NEW_RET(ctx->ctx, *augments, aug, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002951
2952 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002953 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
David Sedlákb9b892c2019-07-12 14:44:02 +02002954 YANG_CHECK_NONEMPTY(ctx, word_len, "augment");
Radek Krejci44ceedc2018-10-02 15:54:31 +02002955 INSERT_WORD(ctx, buf, aug->nodeid, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002956 aug->nodetype = LYS_AUGMENT;
2957 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002958
Radek Krejci7fc68292019-06-12 13:51:09 +02002959 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002960 switch (kw) {
2961 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002962 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 +02002963 break;
2964 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002965 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 +02002966 break;
2967 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002968 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 +02002969 break;
2970 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002971 LY_CHECK_RET(parse_status(ctx, data, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002972 break;
2973 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002974 LY_CHECK_RET(parse_when(ctx, data, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002975 break;
2976
2977 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01002978 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "augment");
Radek Krejci10113652018-11-14 16:56:50 +01002979 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002980 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002981 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002982 break;
2983 case YANG_CASE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002984 LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002985 break;
2986 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002987 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002988 break;
2989 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002990 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002991 break;
2992 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002993 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002994 break;
2995 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002996 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002997 break;
2998 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002999 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003000 break;
3001 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003002 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003003 break;
3004
3005 case YANG_ACTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003006 YANG_CHECK_STMTVER2_RET(ctx, "action", "augment");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003007 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003008 break;
3009 case YANG_NOTIFICATION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003010 YANG_CHECK_STMTVER2_RET(ctx, "notification", "augment");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003011 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003012 break;
3013 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003014 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003015 break;
3016 default:
David Sedlákb3077192019-06-19 10:55:37 +02003017 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003018 return LY_EVALID;
3019 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003020 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003021 LY_CHECK_RET(ret);
3022checks:
3023 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02003024 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, NULL, NULL, aug->actions, aug->notifs));
Radek Krejci7fc68292019-06-12 13:51:09 +02003025
Michal Vasko7fbc8162018-09-17 10:35:16 +02003026 return ret;
3027}
3028
Michal Vaskoea5abea2018-09-18 13:10:54 +02003029/**
3030 * @brief Parse the uses statement.
3031 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003032 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003033 * @param[in,out] data Data to read from, always moved to currently handled character.
3034 * @param[in,out] siblings Siblings to add to.
3035 *
3036 * @return LY_ERR values.
3037 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003038LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003039parse_uses(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003040{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003041 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003042 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003043 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003044 enum yang_keyword kw;
3045 struct lysp_node *iter;
3046 struct lysp_node_uses *uses;
3047
3048 /* create structure */
3049 uses = calloc(1, sizeof *uses);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003050 LY_CHECK_ERR_RET(!uses, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003051 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003052 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003053
3054 /* insert into siblings */
3055 if (!*siblings) {
3056 *siblings = (struct lysp_node *)uses;
3057 } else {
3058 for (iter = *siblings; iter->next; iter = iter->next);
3059 iter->next = (struct lysp_node *)uses;
3060 }
3061
3062 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003063 LY_CHECK_RET(get_argument(ctx, data, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003064 INSERT_WORD(ctx, buf, uses->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003065
3066 /* parse substatements */
Radek Krejci7fc68292019-06-12 13:51:09 +02003067 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003068 switch (kw) {
3069 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003070 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 +02003071 break;
3072 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003073 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 +02003074 break;
3075 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003076 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 +02003077 break;
3078 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003079 LY_CHECK_RET(parse_status(ctx, data, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003080 break;
3081 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003082 LY_CHECK_RET(parse_when(ctx, data, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003083 break;
3084
3085 case YANG_REFINE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003086 LY_CHECK_RET(parse_refine(ctx, data, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003087 break;
3088 case YANG_AUGMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003089 LY_CHECK_RET(parse_augment(ctx, data, (struct lysp_node*)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003090 break;
3091 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003092 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003093 break;
3094 default:
David Sedlákb3077192019-06-19 10:55:37 +02003095 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003096 return LY_EVALID;
3097 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003098 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003099checks:
3100 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02003101 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, NULL, uses->augments, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02003102
Michal Vasko7fbc8162018-09-17 10:35:16 +02003103 return ret;
3104}
3105
Michal Vaskoea5abea2018-09-18 13:10:54 +02003106/**
3107 * @brief Parse the case statement.
3108 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003109 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003110 * @param[in,out] data Data to read from, always moved to currently handled character.
3111 * @param[in,out] siblings Siblings to add to.
3112 *
3113 * @return LY_ERR values.
3114 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003115LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003116parse_case(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003117{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003118 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003119 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003120 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003121 enum yang_keyword kw;
3122 struct lysp_node *iter;
3123 struct lysp_node_case *cas;
3124
3125 /* create structure */
3126 cas = calloc(1, sizeof *cas);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003127 LY_CHECK_ERR_RET(!cas, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003128 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003129 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003130
3131 /* insert into siblings */
3132 if (!*siblings) {
3133 *siblings = (struct lysp_node *)cas;
3134 } else {
3135 for (iter = *siblings; iter->next; iter = iter->next);
3136 iter->next = (struct lysp_node *)cas;
3137 }
3138
3139 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003140 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003141 INSERT_WORD(ctx, buf, cas->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003142
3143 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01003144 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003145 switch (kw) {
3146 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003147 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 +02003148 break;
3149 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003150 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 +02003151 break;
3152 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003153 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 +02003154 break;
3155 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003156 LY_CHECK_RET(parse_status(ctx, data, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003157 break;
3158 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003159 LY_CHECK_RET(parse_when(ctx, data, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003160 break;
3161
3162 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01003163 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "case");
Radek Krejci10113652018-11-14 16:56:50 +01003164 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003165 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003166 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003167 break;
3168 case YANG_CHOICE:
Radek Krejcia9026eb2018-12-12 16:04:47 +01003169 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003170 break;
3171 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003172 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003173 break;
3174 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003175 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003176 break;
3177 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003178 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003179 break;
3180 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003181 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003182 break;
3183 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003184 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003185 break;
3186 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003187 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003188 break;
3189 default:
David Sedlákb3077192019-06-19 10:55:37 +02003190 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003191 return LY_EVALID;
3192 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003193 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003194 return ret;
3195}
3196
Michal Vaskoea5abea2018-09-18 13:10:54 +02003197/**
3198 * @brief Parse the choice statement.
3199 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003200 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003201 * @param[in,out] data Data to read from, always moved to currently handled character.
3202 * @param[in,out] siblings Siblings to add to.
3203 *
3204 * @return LY_ERR values.
3205 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003206LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003207parse_choice(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003208{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003209 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003210 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003211 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003212 enum yang_keyword kw;
3213 struct lysp_node *iter;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003214 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003215
3216 /* create structure */
Radek Krejci44ceedc2018-10-02 15:54:31 +02003217 choice = calloc(1, sizeof *choice);
3218 LY_CHECK_ERR_RET(!choice, LOGMEM(ctx->ctx), LY_EMEM);
3219 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003220 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003221
3222 /* insert into siblings */
3223 if (!*siblings) {
Radek Krejci44ceedc2018-10-02 15:54:31 +02003224 *siblings = (struct lysp_node *)choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003225 } else {
3226 for (iter = *siblings; iter->next; iter = iter->next);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003227 iter->next = (struct lysp_node *)choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003228 }
3229
3230 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003231 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003232 INSERT_WORD(ctx, buf, choice->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003233
3234 /* parse substatements */
Radek Krejcia9026eb2018-12-12 16:04:47 +01003235 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003236 switch (kw) {
3237 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003238 LY_CHECK_RET(parse_config(ctx, data, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003239 break;
3240 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003241 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 +02003242 break;
3243 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003244 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 +02003245 break;
3246 case YANG_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003247 LY_CHECK_RET(parse_mandatory(ctx, data, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003248 break;
3249 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003250 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 +02003251 break;
3252 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003253 LY_CHECK_RET(parse_status(ctx, data, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003254 break;
3255 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003256 LY_CHECK_RET(parse_when(ctx, data, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003257 break;
3258 case YANG_DEFAULT:
Radek Krejcia9026eb2018-12-12 16:04:47 +01003259 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 +02003260 break;
3261
3262 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01003263 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "choice");
Radek Krejci10113652018-11-14 16:56:50 +01003264 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003265 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003266 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003267 break;
3268 case YANG_CASE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003269 LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003270 break;
3271 case YANG_CHOICE:
Radek Krejciceaf2122019-01-02 15:03:26 +01003272 YANG_CHECK_STMTVER2_RET(ctx, "choice", "choice");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003273 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003274 break;
3275 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003276 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003277 break;
3278 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003279 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003280 break;
3281 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003282 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003283 break;
3284 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003285 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003286 break;
3287 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003288 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003289 break;
3290 default:
David Sedlákb3077192019-06-19 10:55:37 +02003291 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003292 return LY_EVALID;
3293 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003294 }
Radek Krejcia9026eb2018-12-12 16:04:47 +01003295 LY_CHECK_RET(ret);
3296checks:
3297 if ((choice->flags & LYS_MAND_TRUE) && choice->dflt) {
David Sedlákb3077192019-06-19 10:55:37 +02003298 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMSCOMB, "mandatory", "default", "choice");
Radek Krejcia9026eb2018-12-12 16:04:47 +01003299 return LY_EVALID;
3300 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003301 return ret;
3302}
3303
Michal Vaskoea5abea2018-09-18 13:10:54 +02003304/**
3305 * @brief Parse the container statement.
3306 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003307 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003308 * @param[in,out] data Data to read from, always moved to currently handled character.
3309 * @param[in,out] siblings Siblings to add to.
3310 *
3311 * @return LY_ERR values.
3312 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003313LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003314parse_container(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003315{
3316 LY_ERR ret = 0;
3317 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003318 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003319 enum yang_keyword kw;
3320 struct lysp_node *iter;
3321 struct lysp_node_container *cont;
3322
3323 /* create structure */
3324 cont = calloc(1, sizeof *cont);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003325 LY_CHECK_ERR_RET(!cont, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003326 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003327 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003328
3329 /* insert into siblings */
3330 if (!*siblings) {
3331 *siblings = (struct lysp_node *)cont;
3332 } else {
3333 for (iter = *siblings; iter->next; iter = iter->next);
3334 iter->next = (struct lysp_node *)cont;
3335 }
3336
3337 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003338 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003339 INSERT_WORD(ctx, buf, cont->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003340
3341 /* parse substatements */
Radek Krejci7fc68292019-06-12 13:51:09 +02003342 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003343 switch (kw) {
3344 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003345 LY_CHECK_RET(parse_config(ctx, data, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003346 break;
3347 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003348 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 +02003349 break;
3350 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003351 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 +02003352 break;
3353 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003354 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 +02003355 break;
3356 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003357 LY_CHECK_RET(parse_status(ctx, data, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003358 break;
3359 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003360 LY_CHECK_RET(parse_when(ctx, data, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003361 break;
3362 case YANG_PRESENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003363 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 +02003364 break;
3365
3366 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01003367 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "container");
Radek Krejci10113652018-11-14 16:56:50 +01003368 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003369 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003370 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003371 break;
3372 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003373 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003374 break;
3375 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003376 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003377 break;
3378 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003379 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003380 break;
3381 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003382 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003383 break;
3384 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003385 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003386 break;
3387 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003388 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003389 break;
3390
3391 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01003392 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)cont, data, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003393 break;
3394 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003395 LY_CHECK_RET(parse_restrs(ctx, data, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003396 break;
3397 case YANG_ACTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003398 YANG_CHECK_STMTVER2_RET(ctx, "action", "container");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003399 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003400 break;
3401 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003402 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003403 break;
3404 case YANG_NOTIFICATION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003405 YANG_CHECK_STMTVER2_RET(ctx, "notification", "container");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003406 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003407 break;
3408 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003409 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003410 break;
3411 default:
David Sedlákb3077192019-06-19 10:55:37 +02003412 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003413 return LY_EVALID;
3414 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003415 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003416checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01003417 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02003418 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, cont->groupings, NULL, cont->actions, cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003419 return ret;
3420}
3421
Michal Vaskoea5abea2018-09-18 13:10:54 +02003422/**
3423 * @brief Parse the list statement.
3424 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003425 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003426 * @param[in,out] data Data to read from, always moved to currently handled character.
3427 * @param[in,out] siblings Siblings to add to.
3428 *
3429 * @return LY_ERR values.
3430 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003431LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003432parse_list(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003433{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003434 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003435 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003436 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003437 enum yang_keyword kw;
3438 struct lysp_node *iter;
3439 struct lysp_node_list *list;
3440
3441 /* create structure */
3442 list = calloc(1, sizeof *list);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003443 LY_CHECK_ERR_RET(!list, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003444 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003445 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003446
3447 /* insert into siblings */
3448 if (!*siblings) {
3449 *siblings = (struct lysp_node *)list;
3450 } else {
3451 for (iter = *siblings; iter->next; iter = iter->next);
3452 iter->next = (struct lysp_node *)list;
3453 }
3454
3455 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003456 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003457 INSERT_WORD(ctx, buf, list->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003458
3459 /* parse substatements */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003460 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003461 switch (kw) {
3462 case YANG_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003463 LY_CHECK_RET(parse_config(ctx, data, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003464 break;
3465 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003466 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 +02003467 break;
3468 case YANG_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003469 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 +02003470 break;
3471 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003472 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 +02003473 break;
3474 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003475 LY_CHECK_RET(parse_status(ctx, data, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003476 break;
3477 case YANG_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003478 LY_CHECK_RET(parse_when(ctx, data, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003479 break;
3480 case YANG_KEY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003481 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 +02003482 break;
3483 case YANG_MAX_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003484 LY_CHECK_RET(parse_maxelements(ctx, data, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003485 break;
3486 case YANG_MIN_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003487 LY_CHECK_RET(parse_minelements(ctx, data, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003488 break;
3489 case YANG_ORDERED_BY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003490 LY_CHECK_RET(parse_orderedby(ctx, data, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003491 break;
3492 case YANG_UNIQUE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003493 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 +02003494 break;
3495
3496 case YANG_ANYDATA:
Radek Krejciceaf2122019-01-02 15:03:26 +01003497 YANG_CHECK_STMTVER2_RET(ctx, "anydata", "list");
Radek Krejci10113652018-11-14 16:56:50 +01003498 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003499 case YANG_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003500 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003501 break;
3502 case YANG_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003503 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003504 break;
3505 case YANG_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003506 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003507 break;
3508 case YANG_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003509 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003510 break;
3511 case YANG_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003512 LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003513 break;
3514 case YANG_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003515 LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003516 break;
3517 case YANG_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003518 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003519 break;
3520
3521 case YANG_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01003522 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)list, data, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003523 break;
3524 case YANG_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003525 LY_CHECK_RET(parse_restrs(ctx, data, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003526 break;
3527 case YANG_ACTION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003528 YANG_CHECK_STMTVER2_RET(ctx, "action", "list");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003529 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003530 break;
3531 case YANG_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003532 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003533 break;
3534 case YANG_NOTIFICATION:
Radek Krejciceaf2122019-01-02 15:03:26 +01003535 YANG_CHECK_STMTVER2_RET(ctx, "notification", "list");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003536 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003537 break;
3538 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003539 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003540 break;
3541 default:
David Sedlákb3077192019-06-19 10:55:37 +02003542 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003543 return LY_EVALID;
3544 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003545 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003546 LY_CHECK_RET(ret);
3547checks:
Radek Krejci7fc68292019-06-12 13:51:09 +02003548 /* finalize parent pointers to the reallocated items */
David Sedlákd2ebe572019-07-22 12:53:14 +02003549 LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, list->groupings, NULL, list->actions, list->notifs));
Radek Krejci7fc68292019-06-12 13:51:09 +02003550
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003551 if (list->max && list->min > list->max) {
David Sedlákb3077192019-06-19 10:55:37 +02003552 LOGVAL_PARSER(ctx, LYVE_SEMANTICS,
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003553 "Invalid combination of min-elements and max-elements: min value %u is bigger than the max value %u.",
3554 list->min, list->max);
3555 return LY_EVALID;
3556 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003557
3558 return ret;
3559}
3560
Michal Vaskoea5abea2018-09-18 13:10:54 +02003561/**
3562 * @brief Parse the yin-element statement.
3563 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003564 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003565 * @param[in,out] data Data to read from, always moved to currently handled character.
3566 * @param[in,out] flags Flags to write to.
3567 * @param[in,out] exts Extension instances to add to.
3568 *
3569 * @return LY_ERR values.
3570 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003571static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003572parse_yinelement(struct lys_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003573{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003574 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003575 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003576 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003577 enum yang_keyword kw;
3578
3579 if (*flags & LYS_YINELEM_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02003580 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003581 return LY_EVALID;
3582 }
3583
3584 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003585 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003586
3587 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
3588 *flags |= LYS_YINELEM_TRUE;
3589 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
3590 *flags |= LYS_YINELEM_FALSE;
3591 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003592 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003593 free(buf);
3594 return LY_EVALID;
3595 }
3596 free(buf);
3597
Radek Krejci6d6556c2018-11-08 09:37:45 +01003598 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003599 switch (kw) {
3600 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003601 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_YINELEM, 0, exts));
3602 LY_CHECK_RET(ret); break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003603 default:
David Sedlákb3077192019-06-19 10:55:37 +02003604 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003605 return LY_EVALID;
3606 }
3607 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003608 return ret;
3609}
3610
Michal Vaskoea5abea2018-09-18 13:10:54 +02003611/**
David Sedlák2444f8f2019-07-09 11:02:47 +02003612 * @brief Parse the argument statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003613 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003614 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003615 * @param[in,out] data Data to read from, always moved to currently handled character.
3616 * @param[in,out] argument Value to write to.
3617 * @param[in,out] flags Flags to write to.
3618 * @param[in,out] exts Extension instances to add to.
3619 *
3620 * @return LY_ERR values.
3621 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003622static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003623parse_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 +02003624{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003625 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003626 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003627 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003628 enum yang_keyword kw;
3629
3630 if (*argument) {
David Sedlákb3077192019-06-19 10:55:37 +02003631 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003632 return LY_EVALID;
3633 }
3634
3635 /* get value */
David Sedlák2444f8f2019-07-09 11:02:47 +02003636 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003637 INSERT_WORD(ctx, buf, *argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003638
Radek Krejci6d6556c2018-11-08 09:37:45 +01003639 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003640 switch (kw) {
3641 case YANG_YIN_ELEMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003642 LY_CHECK_RET(parse_yinelement(ctx, data, flags, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003643 break;
3644 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003645 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_ARGUMENT, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003646 break;
3647 default:
David Sedlákb3077192019-06-19 10:55:37 +02003648 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003649 return LY_EVALID;
3650 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003651 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003652 return ret;
3653}
3654
Michal Vaskoea5abea2018-09-18 13:10:54 +02003655/**
3656 * @brief Parse the extension statement.
3657 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003658 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003659 * @param[in,out] data Data to read from, always moved to currently handled character.
3660 * @param[in,out] extensions Extensions to add to.
3661 *
3662 * @return LY_ERR values.
3663 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003664static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003665parse_extension(struct lys_parser_ctx *ctx, const char **data, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003666{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003667 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003668 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003669 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003670 enum yang_keyword kw;
3671 struct lysp_ext *ex;
3672
Radek Krejci2c4e7172018-10-19 15:56:26 +02003673 LY_ARRAY_NEW_RET(ctx->ctx, *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003674
3675 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003676 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003677 INSERT_WORD(ctx, buf, ex->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003678
Radek Krejci6d6556c2018-11-08 09:37:45 +01003679 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003680 switch (kw) {
3681 case YANG_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003682 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 +02003683 break;
3684 case YANG_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003685 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 +02003686 break;
3687 case YANG_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003688 LY_CHECK_RET(parse_status(ctx, data, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003689 break;
3690 case YANG_ARGUMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003691 LY_CHECK_RET(parse_argument(ctx, data, &ex->argument, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003692 break;
3693 case YANG_CUSTOM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003694 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003695 break;
3696 default:
David Sedlákb3077192019-06-19 10:55:37 +02003697 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003698 return LY_EVALID;
3699 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003700 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003701 return ret;
3702}
3703
Michal Vaskoea5abea2018-09-18 13:10:54 +02003704/**
3705 * @brief Parse the deviate statement.
3706 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003707 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003708 * @param[in,out] data Data to read from, always moved to currently handled character.
3709 * @param[in,out] deviates Deviates to add to.
3710 *
3711 * @return LY_ERR values.
3712 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003713LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02003714parse_deviate(struct lys_parser_ctx *ctx, const char **data, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003715{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003716 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003717 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003718 size_t word_len, dev_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003719 enum yang_keyword kw;
3720 struct lysp_deviate *iter, *d;
3721 struct lysp_deviate_add *d_add = NULL;
3722 struct lysp_deviate_rpl *d_rpl = NULL;
3723 struct lysp_deviate_del *d_del = NULL;
3724 const char **d_units, ***d_uniques, ***d_dflts;
3725 struct lysp_restr **d_musts;
3726 uint16_t *d_flags;
3727 uint32_t *d_min, *d_max;
3728
3729 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003730 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003731
3732 if ((word_len == 13) && !strncmp(word, "not-supported", word_len)) {
3733 dev_mod = LYS_DEV_NOT_SUPPORTED;
3734 } else if ((word_len == 3) && !strncmp(word, "add", word_len)) {
3735 dev_mod = LYS_DEV_ADD;
3736 } else if ((word_len == 7) && !strncmp(word, "replace", word_len)) {
3737 dev_mod = LYS_DEV_REPLACE;
3738 } else if ((word_len == 6) && !strncmp(word, "delete", word_len)) {
3739 dev_mod = LYS_DEV_DELETE;
3740 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003741 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003742 free(buf);
3743 return LY_EVALID;
3744 }
3745 free(buf);
3746
3747 /* create structure */
3748 switch (dev_mod) {
3749 case LYS_DEV_NOT_SUPPORTED:
3750 d = calloc(1, sizeof *d);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003751 LY_CHECK_ERR_RET(!d, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003752 break;
3753 case LYS_DEV_ADD:
3754 d_add = calloc(1, sizeof *d_add);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003755 LY_CHECK_ERR_RET(!d_add, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003756 d = (struct lysp_deviate *)d_add;
3757 d_units = &d_add->units;
3758 d_uniques = &d_add->uniques;
3759 d_dflts = &d_add->dflts;
3760 d_musts = &d_add->musts;
3761 d_flags = &d_add->flags;
3762 d_min = &d_add->min;
3763 d_max = &d_add->max;
3764 break;
3765 case LYS_DEV_REPLACE:
3766 d_rpl = calloc(1, sizeof *d_rpl);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003767 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003768 d = (struct lysp_deviate *)d_rpl;
3769 d_units = &d_rpl->units;
3770 d_flags = &d_rpl->flags;
3771 d_min = &d_rpl->min;
3772 d_max = &d_rpl->max;
3773 break;
3774 case LYS_DEV_DELETE:
3775 d_del = calloc(1, sizeof *d_del);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003776 LY_CHECK_ERR_RET(!d_del, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003777 d = (struct lysp_deviate *)d_del;
3778 d_units = &d_del->units;
3779 d_uniques = &d_del->uniques;
3780 d_dflts = &d_del->dflts;
3781 d_musts = &d_del->musts;
3782 d_flags = &d_del->flags;
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
Radek Krejcie7b95092019-05-15 11:03:07 +02004493yang_parse_submodule(struct lys_parser_ctx *context, 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
4501 /* "module"/"submodule" */
Radek Krejcibbe09a92018-11-08 09:36:54 +01004502 ret = get_keyword(context, &data, &kw, &word, &word_len);
4503 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004504
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004505 if (kw == YANG_MODULE) {
4506 LOGERR(context->ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
4507 ret = LY_EINVAL;
4508 goto cleanup;
4509 } else if (kw != YANG_SUBMODULE) {
David Sedlákb3077192019-06-19 10:55:37 +02004510 LOGVAL_PARSER(context, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".",
Radek Krejcic59bc972018-09-17 16:13:06 +02004511 ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004512 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004513 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004514 }
4515
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004516 mod_p = calloc(1, sizeof *mod_p);
4517 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(context->ctx), cleanup);
4518 mod_p->parsing = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004519
4520 /* substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004521 ret = parse_submodule(context, &data, mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004522 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004523
4524 /* read some trailing spaces or new lines */
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004525 while(*data && isspace(*data)) {
4526 data++;
4527 }
4528 if (*data) {
David Sedlákb3077192019-06-19 10:55:37 +02004529 LOGVAL_PARSER(context, LYVE_SYNTAX, "Trailing garbage \"%.*s%s\" after submodule, expected end-of-input.",
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004530 15, data, strlen(data) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004531 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004532 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004533 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004534
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004535 mod_p->parsing = 0;
4536 *submod = mod_p;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004537
Radek Krejcibbe09a92018-11-08 09:36:54 +01004538cleanup:
4539 if (ret) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004540 lysp_submodule_free(context->ctx, mod_p);
4541 }
4542
4543 return ret;
4544}
4545
4546LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +02004547yang_parse_module(struct lys_parser_ctx *context, const char *data, struct lys_module *mod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004548{
4549 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004550 char *word;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004551 size_t word_len;
4552 enum yang_keyword kw;
4553 struct lysp_module *mod_p = NULL;
4554
4555 /* "module"/"submodule" */
4556 ret = get_keyword(context, &data, &kw, &word, &word_len);
4557 LY_CHECK_GOTO(ret, cleanup);
4558
4559 if (kw == YANG_SUBMODULE) {
4560 LOGERR(context->ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module.");
4561 ret = LY_EINVAL;
4562 goto cleanup;
4563 } else if (kw != YANG_MODULE) {
David Sedlákb3077192019-06-19 10:55:37 +02004564 LOGVAL_PARSER(context, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004565 ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004566 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004567 goto cleanup;
4568 }
4569
4570 mod_p = calloc(1, sizeof *mod_p);
4571 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(context->ctx), cleanup);
4572 mod_p->mod = mod;
4573 mod_p->parsing = 1;
4574
4575 /* substatements */
4576 ret = parse_module(context, &data, mod_p);
4577 LY_CHECK_GOTO(ret, cleanup);
4578
4579 /* read some trailing spaces or new lines */
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004580 while(*data && isspace(*data)) {
4581 data++;
4582 }
4583 if (*data) {
David Sedlákb3077192019-06-19 10:55:37 +02004584 LOGVAL_PARSER(context, LYVE_SYNTAX, "Trailing garbage \"%.*s%s\" after module, expected end-of-input.",
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004585 15, data, strlen(data) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004586 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004587 goto cleanup;
4588 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004589
4590 mod_p->parsing = 0;
4591 mod->parsed = mod_p;
4592
4593cleanup:
4594 if (ret) {
4595 lysp_module_free(mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004596 }
4597
Michal Vasko7fbc8162018-09-17 10:35:16 +02004598 return ret;
4599}