blob: dbd38805d92d57506fb380d2773120aa14f3aba4 [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 Vasko63f3d842020-07-08 10:10:14 +020014#include "parser_internal.h"
Michal Vasko7fbc8162018-09-17 10:35:16 +020015
Radek Krejcie7b95092019-05-15 11:03:07 +020016#include <assert.h>
17#include <ctype.h>
18#include <errno.h>
19#include <stdint.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23
Radek Krejci535ea9f2020-05-29 16:01:05 +020024#include "common.h"
Michal Vasko7fbc8162018-09-17 10:35:16 +020025#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020026#include "dict.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020027#include "in_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020028#include "log.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020029#include "parser_schema.h"
Michal Vasko69730152020-10-09 16:30:07 +020030#include "path.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020031#include "set.h"
32#include "tree.h"
33#include "tree_schema.h"
Radek Krejci70853c52018-10-15 14:46:16 +020034#include "tree_schema_internal.h"
Radek Krejci44ceedc2018-10-02 15:54:31 +020035
Radek Krejciceaf2122019-01-02 15:03:26 +010036/**
37 * @brief Insert WORD into the libyang context's dictionary and store as TARGET.
38 * @param[in] CTX yang parser context to access libyang context.
39 * @param[in] BUF buffer in case the word is not a constant and can be inserted directly (zero-copy)
40 * @param[out] TARGET variable where to store the pointer to the inserted value.
41 * @param[in] WORD string to store.
42 * @param[in] LEN length of the string in WORD to store.
43 */
Radek Krejci011e4aa2020-09-04 15:22:31 +020044#define INSERT_WORD_RET(CTX, BUF, TARGET, WORD, LEN) \
Michal Vasko5d24f6c2020-10-13 13:49:06 +020045 if (BUF) {LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(CTX), WORD, &(TARGET)));}\
46 else {LY_CHECK_RET(lydict_insert(PARSER_CTX(CTX), LEN ? WORD : "", LEN, &(TARGET)));}
Radek Krejci44ceedc2018-10-02 15:54:31 +020047
Radek Krejciceaf2122019-01-02 15:03:26 +010048/**
Michal Vasko63f3d842020-07-08 10:10:14 +020049 * @brief Read from the IN structure COUNT items. Also updates the indent value in yang parser context
Radek Krejciceaf2122019-01-02 15:03:26 +010050 * @param[in] CTX yang parser context to update its indent value.
Michal Vasko63f3d842020-07-08 10:10:14 +020051 * @param[in,out] IN input structure
Radek Krejciceaf2122019-01-02 15:03:26 +010052 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
53 */
Michal Vasko63f3d842020-07-08 10:10:14 +020054#define MOVE_INPUT(CTX, IN, COUNT) ly_in_skip(IN, COUNT);(CTX)->indent+=COUNT
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020055
Michal Vaskoea5abea2018-09-18 13:10:54 +020056/**
57 * @brief Loop through all substatements providing, return if there are none.
58 *
Radek Krejci44ceedc2018-10-02 15:54:31 +020059 * @param[in] CTX yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +020060 * @param[in] IN Input structure to read from.
Michal Vaskoea5abea2018-09-18 13:10:54 +020061 * @param[out] KW YANG keyword read.
62 * @param[out] WORD Pointer to the keyword itself.
63 * @param[out] WORD_LEN Length of the keyword.
64 * @param[out] ERR Variable for error storing.
65 *
66 * @return In case there are no substatements or a fatal error encountered.
67 */
Michal Vasko63f3d842020-07-08 10:10:14 +020068#define YANG_READ_SUBSTMT_FOR(CTX, IN, KW, WORD, WORD_LEN, ERR, CHECKGOTO) \
69 LY_CHECK_RET(get_keyword(CTX, IN, &KW, &WORD, &WORD_LEN)); \
Radek Krejcid6b76452019-09-03 17:03:03 +020070 if (KW == LY_STMT_SYNTAX_SEMICOLON) { \
Radek Krejci6d6556c2018-11-08 09:37:45 +010071 CHECKGOTO; \
Radek Krejci6d9b9b52018-11-02 12:43:39 +010072 return LY_SUCCESS; \
Michal Vasko7fbc8162018-09-17 10:35:16 +020073 } \
Radek Krejcid6b76452019-09-03 17:03:03 +020074 if (KW != LY_STMT_SYNTAX_LEFT_BRACE) { \
David Sedlákb3077192019-06-19 10:55:37 +020075 LOGVAL_PARSER(CTX, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\", expected \";\" or \"{\".", ly_stmt2str(KW)); \
Michal Vasko7fbc8162018-09-17 10:35:16 +020076 return LY_EVALID; \
77 } \
Michal Vasko63f3d842020-07-08 10:10:14 +020078 for (ERR = get_keyword(CTX, IN, &KW, &WORD, &WORD_LEN); \
Radek Krejcid6b76452019-09-03 17:03:03 +020079 !ERR && (KW != LY_STMT_SYNTAX_RIGHT_BRACE); \
Michal Vasko63f3d842020-07-08 10:10:14 +020080 ERR = get_keyword(CTX, IN, &KW, &WORD, &WORD_LEN))
Michal Vasko7fbc8162018-09-17 10:35:16 +020081
Michal Vasko63f3d842020-07-08 10:10:14 +020082LY_ERR parse_container(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent,
Radek Krejci0f969882020-08-21 16:56:47 +020083 struct lysp_node **siblings);
Michal Vasko63f3d842020-07-08 10:10:14 +020084LY_ERR parse_uses(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings);
85LY_ERR parse_choice(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings);
86LY_ERR parse_case(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings);
87LY_ERR parse_list(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings);
88LY_ERR parse_grouping(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_grp **groupings);
Michal Vasko7fbc8162018-09-17 10:35:16 +020089
Michal Vaskoea5abea2018-09-18 13:10:54 +020090/**
91 * @brief Add another character to dynamic buffer, a low-level function.
92 *
Radek Krejci44ceedc2018-10-02 15:54:31 +020093 * Enlarge if needed. Updates \p input as well as \p buf_used.
Michal Vaskoea5abea2018-09-18 13:10:54 +020094 *
Radek Krejci404251e2018-10-09 12:06:44 +020095 * @param[in] ctx libyang context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +020096 * @param[in,out] in Input structure.
Radek Krejci44ceedc2018-10-02 15:54:31 +020097 * @param[in] len Number of bytes to get from the input string and copy into the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +020098 * @param[in,out] buf Buffer to use, can be moved by realloc().
99 * @param[in,out] buf_len Current size of the buffer.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200100 * @param[in,out] buf_used Currently used characters of the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200101 *
102 * @return LY_ERR values.
103 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200104LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200105buf_add_char(struct ly_ctx *ctx, struct ly_in *in, size_t len, char **buf, size_t *buf_len, size_t *buf_used)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200106{
Radek Krejci44ceedc2018-10-02 15:54:31 +0200107 if (*buf_len <= (*buf_used) + len) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200108 *buf_len += 16;
109 *buf = ly_realloc(*buf, *buf_len);
110 LY_CHECK_ERR_RET(!*buf, LOGMEM(ctx), LY_EMEM);
111 }
Radek Krejcic0917392019-04-10 13:04:04 +0200112 if (*buf_used) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200113 ly_in_read(in, &(*buf)[*buf_used], len);
Radek Krejcic0917392019-04-10 13:04:04 +0200114 } else {
Michal Vasko63f3d842020-07-08 10:10:14 +0200115 ly_in_read(in, *buf, len);
Radek Krejcic0917392019-04-10 13:04:04 +0200116 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200117
Radek Krejci44ceedc2018-10-02 15:54:31 +0200118 (*buf_used) += len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200119 return LY_SUCCESS;
120}
121
Michal Vaskoea5abea2018-09-18 13:10:54 +0200122/**
Radek Krejci44ceedc2018-10-02 15:54:31 +0200123 * @brief Store a single UTF8 character. It depends whether in a dynamically-allocated buffer or just as a pointer to the data.
124 *
125 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200126 * @param[in,out] in Input structure.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200127 * @param[in] arg Type of the input string to select method of checking character validity.
128 * @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 +0200129 * 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 +0200130 * @param[in,out] word_len Current length of the word pointed to by \p word_p.
131 * @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 +0200132 * @param[in,out] buf_len Current length of \p word_b.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200133 * @param[in] need_buf Flag if the dynamically allocated buffer is required.
David Sedlák40bb13b2019-07-10 14:34:18 +0200134 * @param[in,out] prefix Storage for internally used flag in case of possible prefixed identifiers:
135 * 0 - colon not yet found (no prefix)
136 * 1 - \p c is the colon character
137 * 2 - prefix already processed, now processing the identifier
Michal Vaskoea5abea2018-09-18 13:10:54 +0200138 *
139 * @return LY_ERR values.
140 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200141LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200142buf_store_char(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum yang_arg arg, char **word_p, size_t *word_len,
Radek Krejci857189e2020-09-01 13:26:36 +0200143 char **word_b, size_t *buf_len, ly_bool need_buf, uint8_t *prefix)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200144{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200145 uint32_t c;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200146 size_t len;
147
Radek Krejcif29b7c32019-04-09 16:17:49 +0200148 /* check valid combination of input paremeters - if need_buf specified, word_b must be provided */
149 assert(!need_buf || (need_buf && word_b));
150
Radek Krejci44ceedc2018-10-02 15:54:31 +0200151 /* get UTF8 code point (and number of bytes coding the character) */
Michal Vasko63f3d842020-07-08 10:10:14 +0200152 LY_CHECK_ERR_RET(ly_getutf8(&in->current, &c, &len),
Michal Vasko69730152020-10-09 16:30:07 +0200153 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, in->current[-len]), LY_EVALID);
Michal Vasko63f3d842020-07-08 10:10:14 +0200154 in->current -= len;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200155 if (c == '\n') {
156 ctx->indent = 0;
157 } else {
158 /* note - even the multibyte character is count as 1 */
159 ++ctx->indent;
160 }
161
Radek Krejci44ceedc2018-10-02 15:54:31 +0200162 /* check character validity */
163 switch (arg) {
164 case Y_IDENTIF_ARG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100165 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !(*word_len), NULL));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200166 break;
167 case Y_PREF_IDENTIF_ARG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100168 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !(*word_len), prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200169 break;
170 case Y_STR_ARG:
171 case Y_MAYBE_STR_ARG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100172 LY_CHECK_RET(lysp_check_stringchar((struct lys_parser_ctx *)ctx, c));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200173 break;
174 }
175
Michal Vasko7fbc8162018-09-17 10:35:16 +0200176 if (word_b && *word_b) {
177 /* add another character into buffer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200178 if (buf_add_char(PARSER_CTX(ctx), in, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200179 return LY_EMEM;
180 }
181
182 /* in case of realloc */
183 *word_p = *word_b;
Radek Krejcif29b7c32019-04-09 16:17:49 +0200184 } else if (word_b && need_buf) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200185 /* first time we need a buffer, copy everything read up to now */
186 if (*word_len) {
187 *word_b = malloc(*word_len);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200188 LY_CHECK_ERR_RET(!*word_b, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200189 *buf_len = *word_len;
190 memcpy(*word_b, *word_p, *word_len);
191 }
192
193 /* add this new character into buffer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200194 if (buf_add_char(PARSER_CTX(ctx), in, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200195 return LY_EMEM;
196 }
197
198 /* in case of realloc */
199 *word_p = *word_b;
200 } else {
201 /* just remember the first character pointer */
202 if (!*word_p) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200203 *word_p = (char *)in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200204 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200205 /* ... and update the word's length */
206 (*word_len) += len;
Michal Vasko63f3d842020-07-08 10:10:14 +0200207 ly_in_skip(in, len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200208 }
209
210 return LY_SUCCESS;
211}
212
Michal Vaskoea5abea2018-09-18 13:10:54 +0200213/**
214 * @brief Skip YANG comment in data.
215 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200216 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200217 * @param[in,out] in Input structure.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200218 * @param[in] comment Type of the comment to process:
219 * 1 for a one-line comment,
220 * 2 for a block comment.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200221 * @return LY_ERR values.
222 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200223LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200224skip_comment(struct lys_yang_parser_ctx *ctx, struct ly_in *in, uint8_t comment)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200225{
Radek Krejci80dd33e2018-09-26 15:57:18 +0200226 /* internal statuses: 0 - comment ended,
227 * 1 - in line comment,
228 * 2 - in block comment,
229 * 3 - in block comment with last read character '*'
230 */
Michal Vasko63f3d842020-07-08 10:10:14 +0200231 while (in->current[0] && comment) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200232 switch (comment) {
233 case 1:
Michal Vasko63f3d842020-07-08 10:10:14 +0200234 if (in->current[0] == '\n') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200235 comment = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200236 ++ctx->line;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200237 }
238 break;
239 case 2:
Michal Vasko63f3d842020-07-08 10:10:14 +0200240 if (in->current[0] == '*') {
Radek Krejci15c80ca2018-10-09 11:01:31 +0200241 comment = 3;
Michal Vasko63f3d842020-07-08 10:10:14 +0200242 } else if (in->current[0] == '\n') {
Radek Krejci15c80ca2018-10-09 11:01:31 +0200243 ++ctx->line;
244 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200245 break;
246 case 3:
Michal Vasko63f3d842020-07-08 10:10:14 +0200247 if (in->current[0] == '/') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200248 comment = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200249 } else if (in->current[0] != '*') {
250 if (in->current[0] == '\n') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200251 ++ctx->line;
252 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200253 comment = 2;
254 }
255 break;
256 default:
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200257 LOGINT_RET(PARSER_CTX(ctx));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200258 }
259
Michal Vasko63f3d842020-07-08 10:10:14 +0200260 if (in->current[0] == '\n') {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200261 ctx->indent = 0;
262 } else {
263 ++ctx->indent;
264 }
Michal Vasko63f3d842020-07-08 10:10:14 +0200265 ++in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200266 }
267
Michal Vasko63f3d842020-07-08 10:10:14 +0200268 if (!in->current[0] && (comment > 1)) {
David Sedlákb3077192019-06-19 10:55:37 +0200269 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Unexpected end-of-input, non-terminated comment.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200270 return LY_EVALID;
271 }
272
273 return LY_SUCCESS;
274}
275
Michal Vaskoea5abea2018-09-18 13:10:54 +0200276/**
277 * @brief Read a quoted string from data.
278 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200279 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200280 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200281 * @param[in] arg Type of YANG keyword argument expected.
282 * @param[out] word_p Pointer to the read quoted string.
283 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read quoted string. If not needed,
284 * set to NULL. Otherwise equal to \p word_p.
285 * @param[out] word_len Length of the read quoted string.
286 * @param[out] buf_len Length of the dynamically-allocated buffer \p word_b.
287 * @param[in] indent Current indent (number of YANG spaces). Needed for correct multi-line string
288 * indenation in the final quoted string.
289 *
290 * @return LY_ERR values.
291 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200292static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200293read_qstring(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum yang_arg arg, char **word_p, char **word_b,
Radek Krejci0f969882020-08-21 16:56:47 +0200294 size_t *word_len, size_t *buf_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200295{
296 /* string: 0 - string ended, 1 - string with ', 2 - string with ", 3 - string with " with last character \,
297 * 4 - string finished, now skipping whitespaces looking for +,
298 * 5 - string continues after +, skipping whitespaces */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200299 uint8_t string;
300 uint64_t block_indent = 0, current_indent = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200301 ly_bool need_buf = 0;
302 uint8_t prefix = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200303 const char *c;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200304 uint64_t trailing_ws = 0; /* current number of stored trailing whitespace characters */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200305
Michal Vasko63f3d842020-07-08 10:10:14 +0200306 if (in->current[0] == '\"') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200307 string = 2;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200308 current_indent = block_indent = ctx->indent + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200309 } else {
Michal Vasko63f3d842020-07-08 10:10:14 +0200310 assert(in->current[0] == '\'');
Michal Vasko7fbc8162018-09-17 10:35:16 +0200311 string = 1;
312 }
Michal Vasko63f3d842020-07-08 10:10:14 +0200313 MOVE_INPUT(ctx, in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200314
Michal Vasko63f3d842020-07-08 10:10:14 +0200315 while (in->current[0] && string) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200316 switch (string) {
317 case 1:
Michal Vasko63f3d842020-07-08 10:10:14 +0200318 switch (in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200319 case '\'':
320 /* string may be finished, but check for + */
321 string = 4;
Michal Vasko63f3d842020-07-08 10:10:14 +0200322 MOVE_INPUT(ctx, in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200323 break;
324 default:
325 /* check and store character */
Michal Vasko63f3d842020-07-08 10:10:14 +0200326 LY_CHECK_RET(buf_store_char(ctx, in, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200327 break;
328 }
329 break;
330 case 2:
Michal Vasko63f3d842020-07-08 10:10:14 +0200331 switch (in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200332 case '\"':
333 /* string may be finished, but check for + */
334 string = 4;
Michal Vasko63f3d842020-07-08 10:10:14 +0200335 MOVE_INPUT(ctx, in, 1);
Radek Krejciff13cd12019-10-25 15:34:24 +0200336 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200337 break;
338 case '\\':
339 /* special character following */
340 string = 3;
Radek Krejciff13cd12019-10-25 15:34:24 +0200341
342 /* the backslash sequence is substituted, so we will need a buffer to store the result */
343 need_buf = 1;
344
345 /* move forward to the escaped character */
Michal Vasko63f3d842020-07-08 10:10:14 +0200346 ++in->current;
Radek Krejciff13cd12019-10-25 15:34:24 +0200347
348 /* note that the trailing whitespaces are supposed to be trimmed before substitution of
349 * backslash-escaped characters (RFC 7950, 6.1.3), so we have to zero the trailing whitespaces counter */
350 trailing_ws = 0;
351
352 /* since the backslash-escaped character is handled as first non-whitespace character, stop eating indentation */
353 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200354 break;
355 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200356 if (current_indent < block_indent) {
357 ++current_indent;
Michal Vasko63f3d842020-07-08 10:10:14 +0200358 MOVE_INPUT(ctx, in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200359 } else {
Michal Vasko90edde42019-11-25 15:25:07 +0100360 /* check and store whitespace character */
Michal Vasko63f3d842020-07-08 10:10:14 +0200361 LY_CHECK_RET(buf_store_char(ctx, in, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko90edde42019-11-25 15:25:07 +0100362 trailing_ws++;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200363 }
364 break;
365 case '\t':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200366 if (current_indent < block_indent) {
367 assert(need_buf);
368 current_indent += 8;
369 ctx->indent += 8;
Michal Vaskod989ba02020-08-24 10:59:24 +0200370 for ( ; current_indent > block_indent; --current_indent, --ctx->indent) {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200371 /* store leftover spaces from the tab */
Michal Vasko63f3d842020-07-08 10:10:14 +0200372 c = in->current;
373 in->current = " ";
374 LY_CHECK_RET(buf_store_char(ctx, in, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
375 in->current = c;
Michal Vasko90edde42019-11-25 15:25:07 +0100376 trailing_ws++;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200377 }
Michal Vasko63f3d842020-07-08 10:10:14 +0200378 ++in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200379 } else {
Michal Vasko90edde42019-11-25 15:25:07 +0100380 /* check and store whitespace character */
Michal Vasko63f3d842020-07-08 10:10:14 +0200381 LY_CHECK_RET(buf_store_char(ctx, in, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko90edde42019-11-25 15:25:07 +0100382 trailing_ws++;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200383 /* additional characters for indentation - only 1 was count in buf_store_char */
384 ctx->indent += 7;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200385 }
386 break;
387 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200388 if (block_indent) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200389 /* we will be removing the indents so we need our own buffer */
390 need_buf = 1;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200391
392 /* remove trailing tabs and spaces */
Radek Krejciff13cd12019-10-25 15:34:24 +0200393 (*word_len) = *word_len - trailing_ws;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200394
Radek Krejciff13cd12019-10-25 15:34:24 +0200395 /* restart indentation */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200396 current_indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200397 }
398
399 /* check and store character */
Michal Vasko63f3d842020-07-08 10:10:14 +0200400 LY_CHECK_RET(buf_store_char(ctx, in, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200401
402 /* maintain line number */
403 ++ctx->line;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200404
405 /* reset context indentation counter for possible string after this one */
406 ctx->indent = 0;
Radek Krejciff13cd12019-10-25 15:34:24 +0200407 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200408 break;
409 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200410 /* first non-whitespace character, stop eating indentation */
411 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200412
413 /* check and store character */
Michal Vasko63f3d842020-07-08 10:10:14 +0200414 LY_CHECK_RET(buf_store_char(ctx, in, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Radek Krejciff13cd12019-10-25 15:34:24 +0200415 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200416 break;
417 }
418 break;
419 case 3:
420 /* string encoded characters */
Michal Vasko63f3d842020-07-08 10:10:14 +0200421 c = in->current;
422 switch (in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200423 case 'n':
Michal Vasko63f3d842020-07-08 10:10:14 +0200424 in->current = "\n";
Michal Vasko7fbc8162018-09-17 10:35:16 +0200425 break;
426 case 't':
Michal Vasko63f3d842020-07-08 10:10:14 +0200427 in->current = "\t";
Michal Vasko7fbc8162018-09-17 10:35:16 +0200428 break;
429 case '\"':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200430 case '\\':
Michal Vasko63f3d842020-07-08 10:10:14 +0200431 /* ok as is */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200432 break;
433 default:
Michal Vasko63f3d842020-07-08 10:10:14 +0200434 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Double-quoted string unknown special character '\\%c'.",
Michal Vasko69730152020-10-09 16:30:07 +0200435 in->current[0]);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200436 return LY_EVALID;
437 }
438
439 /* check and store character */
Michal Vasko63f3d842020-07-08 10:10:14 +0200440 LY_CHECK_RET(buf_store_char(ctx, in, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200441
442 string = 2;
Michal Vasko63f3d842020-07-08 10:10:14 +0200443 in->current = c + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200444 break;
445 case 4:
Michal Vasko63f3d842020-07-08 10:10:14 +0200446 switch (in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200447 case '+':
448 /* string continues */
449 string = 5;
Radek Krejciefd22f62018-09-27 11:47:58 +0200450 need_buf = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200451 break;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200452 case '\n':
453 ++ctx->line;
Radek Krejci0f969882020-08-21 16:56:47 +0200454 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200455 case ' ':
456 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200457 /* just skip */
458 break;
459 default:
460 /* string is finished */
461 goto string_end;
462 }
Michal Vasko63f3d842020-07-08 10:10:14 +0200463 MOVE_INPUT(ctx, in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200464 break;
465 case 5:
Michal Vasko63f3d842020-07-08 10:10:14 +0200466 switch (in->current[0]) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200467 case '\n':
468 ++ctx->line;
Radek Krejci0f969882020-08-21 16:56:47 +0200469 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200470 case ' ':
471 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200472 /* skip */
473 break;
474 case '\'':
475 string = 1;
476 break;
477 case '\"':
478 string = 2;
479 break;
480 default:
481 /* it must be quoted again */
David Sedlákb3077192019-06-19 10:55:37 +0200482 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Both string parts divided by '+' must be quoted.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200483 return LY_EVALID;
484 }
Michal Vasko63f3d842020-07-08 10:10:14 +0200485 MOVE_INPUT(ctx, in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200486 break;
487 default:
488 return LY_EINT;
489 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200490 }
491
492string_end:
Michal Vasko69730152020-10-09 16:30:07 +0200493 if ((arg <= Y_PREF_IDENTIF_ARG) && !(*word_len)) {
Radek Krejci4e199f52019-05-28 09:09:28 +0200494 /* empty identifier */
David Sedlákb3077192019-06-19 10:55:37 +0200495 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Statement argument is required.");
Radek Krejci4e199f52019-05-28 09:09:28 +0200496 return LY_EVALID;
497 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200498 return LY_SUCCESS;
499}
500
Michal Vaskoea5abea2018-09-18 13:10:54 +0200501/**
502 * @brief Get another YANG string from the raw data.
503 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200504 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200505 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200506 * @param[in] arg Type of YANG keyword argument expected.
Radek Krejcid3ca0632019-04-16 16:54:54 +0200507 * @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 +0200508 * @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 +0200509 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read string. If not needed,
510 * set to NULL. Otherwise equal to \p word_p.
511 * @param[out] word_len Length of the read string.
512 *
513 * @return LY_ERR values.
Michal Vasko7fbc8162018-09-17 10:35:16 +0200514 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200515LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200516get_argument(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum yang_arg arg, uint16_t *flags, char **word_p,
Radek Krejci0f969882020-08-21 16:56:47 +0200517 char **word_b, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200518{
Radek Krejci44ceedc2018-10-02 15:54:31 +0200519 size_t buf_len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200520 uint8_t prefix = 0;
Michal Vasko69730152020-10-09 16:30:07 +0200521
Michal Vasko7fbc8162018-09-17 10:35:16 +0200522 /* word buffer - dynamically allocated */
523 *word_b = NULL;
524
525 /* word pointer - just a pointer to data */
526 *word_p = NULL;
527
528 *word_len = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200529 while (in->current[0]) {
530 switch (in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200531 case '\'':
532 case '\"':
533 if (*word_len) {
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200534 /* invalid - quotes cannot be in unquoted string and only optsep, ; or { can follow it */
Michal Vasko63f3d842020-07-08 10:10:14 +0200535 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200536 "unquoted string character, optsep, semicolon or opening brace");
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200537 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200538 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200539 if (flags) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200540 (*flags) |= in->current[0] == '\'' ? LYS_SINGLEQUOTED : LYS_DOUBLEQUOTED;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200541 }
Michal Vasko63f3d842020-07-08 10:10:14 +0200542 LY_CHECK_RET(read_qstring(ctx, in, arg, word_p, word_b, word_len, &buf_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200543 goto str_end;
544 case '/':
Michal Vasko63f3d842020-07-08 10:10:14 +0200545 if (in->current[1] == '/') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200546 /* one-line comment */
Michal Vasko63f3d842020-07-08 10:10:14 +0200547 MOVE_INPUT(ctx, in, 2);
548 LY_CHECK_RET(skip_comment(ctx, in, 1));
549 } else if (in->current[1] == '*') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200550 /* block comment */
Michal Vasko63f3d842020-07-08 10:10:14 +0200551 MOVE_INPUT(ctx, in, 2);
552 LY_CHECK_RET(skip_comment(ctx, in, 2));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200553 } else {
554 /* not a comment after all */
Michal Vasko63f3d842020-07-08 10:10:14 +0200555 LY_CHECK_RET(buf_store_char(ctx, in, arg, word_p, word_len, word_b, &buf_len, 0, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200556 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200557 break;
558 case ' ':
559 if (*word_len) {
560 /* word is finished */
561 goto str_end;
562 }
Michal Vasko63f3d842020-07-08 10:10:14 +0200563 MOVE_INPUT(ctx, in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200564 break;
565 case '\t':
566 if (*word_len) {
567 /* word is finished */
568 goto str_end;
569 }
570 /* tabs count for 8 spaces */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200571 ctx->indent += 8;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200572
Michal Vasko63f3d842020-07-08 10:10:14 +0200573 ++in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200574 break;
575 case '\n':
576 if (*word_len) {
577 /* word is finished */
578 goto str_end;
579 }
580 /* reset indent */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200581 ctx->indent = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200582
583 /* track line numbers */
584 ++ctx->line;
585
Michal Vasko63f3d842020-07-08 10:10:14 +0200586 ++in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200587 break;
588 case ';':
589 case '{':
590 if (*word_len || (arg == Y_MAYBE_STR_ARG)) {
591 /* word is finished */
592 goto str_end;
593 }
594
Michal Vasko63f3d842020-07-08 10:10:14 +0200595 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, in->current, "an argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200596 return LY_EVALID;
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200597 case '}':
598 /* invalid - braces cannot be in unquoted string (opening braces terminates the string and can follow it) */
Michal Vasko63f3d842020-07-08 10:10:14 +0200599 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200600 "unquoted string character, optsep, semicolon or opening brace");
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200601 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200602 default:
Michal Vasko63f3d842020-07-08 10:10:14 +0200603 LY_CHECK_RET(buf_store_char(ctx, in, arg, word_p, word_len, word_b, &buf_len, 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200604 break;
605 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200606 }
607
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200608 /* unexpected end of loop */
David Sedlákb3077192019-06-19 10:55:37 +0200609 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200610 return LY_EVALID;
611
Michal Vasko7fbc8162018-09-17 10:35:16 +0200612str_end:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200613 /* terminating NULL byte for buf */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200614 if (*word_b) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200615 (*word_b) = ly_realloc(*word_b, (*word_len) + 1);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200616 LY_CHECK_ERR_RET(!(*word_b), LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200617 (*word_b)[*word_len] = '\0';
Michal Vasko7fbc8162018-09-17 10:35:16 +0200618 *word_p = *word_b;
619 }
620
621 return LY_SUCCESS;
622}
623
Michal Vaskoea5abea2018-09-18 13:10:54 +0200624/**
625 * @brief Get another YANG keyword from the raw data.
626 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200627 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200628 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200629 * @param[out] kw YANG keyword read.
630 * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances.
631 * @param[out] word_len Length of the keyword in the data. Useful for extension instances.
632 *
633 * @return LY_ERR values.
634 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200635LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200636get_keyword(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum ly_stmt *kw, char **word_p, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200637{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200638 uint8_t prefix;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200639 const char *word_start;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200640 size_t len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200641
642 if (word_p) {
643 *word_p = NULL;
644 *word_len = 0;
645 }
646
647 /* first skip "optsep", comments */
Michal Vasko63f3d842020-07-08 10:10:14 +0200648 while (in->current[0]) {
649 switch (in->current[0]) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200650 case '/':
Michal Vasko63f3d842020-07-08 10:10:14 +0200651 if (in->current[1] == '/') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200652 /* one-line comment */
Michal Vasko63f3d842020-07-08 10:10:14 +0200653 MOVE_INPUT(ctx, in, 2);
654 LY_CHECK_RET(skip_comment(ctx, in, 1));
655 } else if (in->current[1] == '*') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200656 /* block comment */
Michal Vasko63f3d842020-07-08 10:10:14 +0200657 MOVE_INPUT(ctx, in, 2);
658 LY_CHECK_RET(skip_comment(ctx, in, 2));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200659 } else {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200660 /* error - not a comment after all, keyword cannot start with slash */
David Sedlákb3077192019-06-19 10:55:37 +0200661 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '/'.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200662 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200663 }
Radek Krejci13028282019-06-11 14:56:48 +0200664 continue;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200665 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200666 /* skip whitespaces (optsep) */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200667 ++ctx->line;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200668 ctx->indent = 0;
669 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200670 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200671 /* skip whitespaces (optsep) */
672 ++ctx->indent;
673 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200674 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200675 /* skip whitespaces (optsep) */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200676 ctx->indent += 8;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200677 break;
678 default:
679 /* either a keyword start or an invalid character */
680 goto keyword_start;
681 }
682
Michal Vasko63f3d842020-07-08 10:10:14 +0200683 ly_in_skip(in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200684 }
685
686keyword_start:
Michal Vasko63f3d842020-07-08 10:10:14 +0200687 word_start = in->current;
688 *kw = lysp_match_kw(ctx, in);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200689
Michal Vasko69730152020-10-09 16:30:07 +0200690 if ((*kw == LY_STMT_SYNTAX_SEMICOLON) || (*kw == LY_STMT_SYNTAX_LEFT_BRACE) || (*kw == LY_STMT_SYNTAX_RIGHT_BRACE)) {
Radek Krejci626df482018-10-11 15:06:31 +0200691 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200692 }
693
Radek Krejcid6b76452019-09-03 17:03:03 +0200694 if (*kw != LY_STMT_NONE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200695 /* make sure we have the whole keyword */
Michal Vasko63f3d842020-07-08 10:10:14 +0200696 switch (in->current[0]) {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200697 case '\n':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200698 case '\t':
Radek Krejciabdd8062019-06-11 16:44:19 +0200699 case ' ':
700 /* mandatory "sep" is just checked, not eaten so nothing in the context is updated */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200701 break;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200702 case ':':
703 /* keyword is not actually a keyword, but prefix of an extension.
704 * To avoid repeated check of the prefix syntax, move to the point where the colon was read
705 * and we will be checking the keyword (extension instance) itself */
706 prefix = 1;
Michal Vasko63f3d842020-07-08 10:10:14 +0200707 MOVE_INPUT(ctx, in, 1);
Radek Krejci156ccaf2018-10-15 15:49:17 +0200708 goto extension;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200709 case '{':
710 /* allowed only for input and output statements which can be without arguments */
Michal Vasko69730152020-10-09 16:30:07 +0200711 if ((*kw == LY_STMT_INPUT) || (*kw == LY_STMT_OUTPUT)) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200712 break;
713 }
Radek Krejci0f969882020-08-21 16:56:47 +0200714 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200715 default:
Michal Vasko63f3d842020-07-08 10:10:14 +0200716 MOVE_INPUT(ctx, in, 1);
717 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(in->current - word_start), word_start,
Michal Vasko69730152020-10-09 16:30:07 +0200718 "a keyword followed by a separator");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200719 return LY_EVALID;
720 }
721 } else {
722 /* still can be an extension */
723 prefix = 0;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200724extension:
Michal Vasko69730152020-10-09 16:30:07 +0200725 while (in->current[0] && (in->current[0] != ' ') && (in->current[0] != '\t') && (in->current[0] != '\n') &&
726 (in->current[0] != '{') && (in->current[0] != ';')) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200727 uint32_t c = 0;
728
Michal Vasko63f3d842020-07-08 10:10:14 +0200729 LY_CHECK_ERR_RET(ly_getutf8(&in->current, &c, &len),
Michal Vasko69730152020-10-09 16:30:07 +0200730 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, in->current[-len]), LY_EVALID);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200731 ++ctx->indent;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200732 /* check character validity */
Michal Vasko63f3d842020-07-08 10:10:14 +0200733 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c,
Michal Vasko69730152020-10-09 16:30:07 +0200734 in->current - len == word_start ? 1 : 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200735 }
Michal Vasko63f3d842020-07-08 10:10:14 +0200736 if (!in->current[0]) {
David Sedlákb3077192019-06-19 10:55:37 +0200737 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200738 return LY_EVALID;
739 }
740
741 /* prefix is mandatory for extension instances */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200742 if (prefix != 2) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200743 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(in->current - word_start), word_start, "a keyword");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200744 return LY_EVALID;
745 }
746
Radek Krejcid6b76452019-09-03 17:03:03 +0200747 *kw = LY_STMT_EXTENSION_INSTANCE;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200748 }
Radek Krejci626df482018-10-11 15:06:31 +0200749success:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200750 if (word_p) {
751 *word_p = (char *)word_start;
Michal Vasko63f3d842020-07-08 10:10:14 +0200752 *word_len = in->current - word_start;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200753 }
754
755 return LY_SUCCESS;
756}
757
Michal Vaskoea5abea2018-09-18 13:10:54 +0200758/**
759 * @brief Parse extension instance substatements.
760 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200761 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200762 * @param[in,out] in Input structure.
Radek Krejci335332a2019-09-05 13:03:35 +0200763 * @param[in] kw Statement keyword value matching @p word value.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200764 * @param[in] word Extension instance substatement name (keyword).
765 * @param[in] word_len Extension instance substatement name length.
766 * @param[in,out] child Children of this extension instance to add to.
767 *
768 * @return LY_ERR values.
769 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200770static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200771parse_ext_substmt(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum ly_stmt kw, char *word, size_t word_len,
Radek Krejci0f969882020-08-21 16:56:47 +0200772 struct lysp_stmt **child)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200773{
774 char *buf;
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100775 LY_ERR ret = LY_SUCCESS;
Radek Krejci335332a2019-09-05 13:03:35 +0200776 enum ly_stmt child_kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200777 struct lysp_stmt *stmt, *par_child;
778
779 stmt = calloc(1, sizeof *stmt);
780 LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM);
781
Radek Krejcibb9b1982019-04-08 14:24:59 +0200782 /* insert into parent statements */
783 if (!*child) {
784 *child = stmt;
785 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +0200786 for (par_child = *child; par_child->next; par_child = par_child->next) {}
Radek Krejcibb9b1982019-04-08 14:24:59 +0200787 par_child->next = stmt;
788 }
789
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200790 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), word, word_len, &stmt->stmt));
Radek Krejci335332a2019-09-05 13:03:35 +0200791 stmt->kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200792
793 /* get optional argument */
Michal Vasko63f3d842020-07-08 10:10:14 +0200794 LY_CHECK_RET(get_argument(ctx, in, Y_MAYBE_STR_ARG, &stmt->flags, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200795
Radek Krejci0ae092d2018-09-20 16:43:19 +0200796 if (word) {
797 if (buf) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200798 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), word, &stmt->arg));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200799 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200800 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), word, word_len, &stmt->arg));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200801 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200802 }
803
Michal Vasko63f3d842020-07-08 10:10:14 +0200804 YANG_READ_SUBSTMT_FOR(ctx, in, child_kw, word, word_len, ret, ) {
805 LY_CHECK_RET(parse_ext_substmt(ctx, in, child_kw, word, word_len, &stmt->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200806 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200807 return ret;
808}
809
Michal Vaskoea5abea2018-09-18 13:10:54 +0200810/**
811 * @brief Parse extension instance.
812 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200813 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200814 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200815 * @param[in] ext_name Extension instance substatement name (keyword).
816 * @param[in] ext_name_len Extension instance substatement name length.
817 * @param[in] insubstmt Type of the keyword this extension instance is a substatement of.
818 * @param[in] insubstmt_index Index of the keyword instance this extension instance is a substatement of.
819 * @param[in,out] exts Extension instances to add to.
820 *
821 * @return LY_ERR values.
822 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200823static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200824parse_ext(struct lys_yang_parser_ctx *ctx, struct ly_in *in, const char *ext_name, size_t ext_name_len, LYEXT_SUBSTMT insubstmt,
Radek Krejci0f969882020-08-21 16:56:47 +0200825 LY_ARRAY_COUNT_TYPE insubstmt_index, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200826{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100827 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200828 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200829 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200830 struct lysp_ext_instance *e;
Radek Krejcid6b76452019-09-03 17:03:03 +0200831 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200832
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200833 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *exts, e, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200834
835 /* store name and insubstmt info */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200836 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), ext_name, ext_name_len, &e->name));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200837 e->insubstmt = insubstmt;
838 e->insubstmt_index = insubstmt_index;
839
840 /* get optional argument */
Michal Vasko63f3d842020-07-08 10:10:14 +0200841 LY_CHECK_RET(get_argument(ctx, in, Y_MAYBE_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200842
Radek Krejci0ae092d2018-09-20 16:43:19 +0200843 if (word) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200844 INSERT_WORD_RET(ctx, buf, e->argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200845 }
846
Michal Vaskod989ba02020-08-24 10:59:24 +0200847 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200848 LY_CHECK_RET(parse_ext_substmt(ctx, in, kw, word, word_len, &e->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200849 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200850 return ret;
851}
852
Michal Vaskoea5abea2018-09-18 13:10:54 +0200853/**
854 * @brief Parse a generic text field without specific constraints. Those are contact, organization,
855 * description, etc...
856 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200857 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200858 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200859 * @param[in] substmt Type of this substatement.
860 * @param[in] substmt_index Index of this substatement.
861 * @param[in,out] value Place to store the parsed value.
862 * @param[in] arg Type of the YANG keyword argument (of the value).
863 * @param[in,out] exts Extension instances to add to.
864 *
865 * @return LY_ERR values.
866 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200867static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200868parse_text_field(struct lys_yang_parser_ctx *ctx, struct ly_in *in, LYEXT_SUBSTMT substmt, uint32_t substmt_index,
Radek Krejci0f969882020-08-21 16:56:47 +0200869 const char **value, enum yang_arg arg, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200870{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100871 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200872 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200873 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200874 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200875
876 if (*value) {
David Sedlákb3077192019-06-19 10:55:37 +0200877 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200878 return LY_EVALID;
879 }
880
881 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +0200882 LY_CHECK_RET(get_argument(ctx, in, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200883
884 /* store value and spend buf if allocated */
Radek Krejci011e4aa2020-09-04 15:22:31 +0200885 INSERT_WORD_RET(ctx, buf, *value, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200886
Michal Vaskod989ba02020-08-24 10:59:24 +0200887 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200888 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200889 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +0200890 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, substmt, substmt_index, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200891 break;
892 default:
David Sedlákb3077192019-06-19 10:55:37 +0200893 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200894 return LY_EVALID;
895 }
896 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200897 return ret;
898}
899
Michal Vaskoea5abea2018-09-18 13:10:54 +0200900/**
901 * @brief Parse the yang-version statement.
902 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200903 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200904 * @param[in,out] in Input structure.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100905 * @param[out] version Storage for the parsed information.
Michal Vasko63f3d842020-07-08 10:10:14 +0200906 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200907 *
908 * @return LY_ERR values.
909 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200910static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200911parse_yangversion(struct lys_yang_parser_ctx *ctx, struct ly_in *in, uint8_t *version, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200912{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100913 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200914 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200915 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200916 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200917
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100918 if (*version) {
David Sedlákb3077192019-06-19 10:55:37 +0200919 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200920 return LY_EVALID;
921 }
922
923 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +0200924 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200925
Radek Krejci96e48da2020-09-04 13:18:06 +0200926 if ((word_len == 1) && !strncmp(word, "1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100927 *version = LYS_VERSION_1_0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200928 } else if ((word_len == 3) && !strncmp(word, "1.1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100929 *version = LYS_VERSION_1_1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200930 } else {
David Sedlákb3077192019-06-19 10:55:37 +0200931 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200932 free(buf);
933 return LY_EVALID;
934 }
935 free(buf);
936
Michal Vaskod989ba02020-08-24 10:59:24 +0200937 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200938 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200939 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +0200940 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_VERSION, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200941 break;
942 default:
David Sedlákb3077192019-06-19 10:55:37 +0200943 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200944 return LY_EVALID;
945 }
946 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200947 return ret;
948}
949
Michal Vaskoea5abea2018-09-18 13:10:54 +0200950/**
951 * @brief Parse the belongs-to statement.
952 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200953 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200954 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200955 * @param[in,out] prefix Place to store the parsed belongs-to prefix value.
956 * @param[in,out] exts Extension instances to add to.
957 *
958 * @return LY_ERR values.
959 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200960static LY_ERR
Michal Vaskoc3781c32020-10-06 14:04:08 +0200961parse_belongsto(struct lys_yang_parser_ctx *ctx, struct ly_in *in, const char **prefix, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200962{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100963 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200964 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200965 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200966 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200967
Michal Vaskoc3781c32020-10-06 14:04:08 +0200968 if (*prefix) {
David Sedlákb3077192019-06-19 10:55:37 +0200969 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200970 return LY_EVALID;
971 }
972
Michal Vaskoc3781c32020-10-06 14:04:08 +0200973 /* get value, it must match the main module */
Michal Vasko63f3d842020-07-08 10:10:14 +0200974 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200975 if (ly_strncmp(ctx->parsed_mod->mod->name, word, word_len)) {
Michal Vaskoc3781c32020-10-06 14:04:08 +0200976 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Submodule \"belongs-to\" value \"%.*s\" does not match its module name \"%s\".",
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200977 (int)word_len, word, ctx->parsed_mod->mod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +0200978 free(buf);
979 return LY_EVALID;
980 }
981 free(buf);
Radek Krejcif09e4e82019-06-14 15:08:11 +0200982
Michal Vasko63f3d842020-07-08 10:10:14 +0200983 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200984 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200985 case LY_STMT_PREFIX:
Michal Vasko63f3d842020-07-08 10:10:14 +0200986 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_PREFIX, 0, prefix, Y_IDENTIF_ARG, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200987 break;
Radek Krejcid6b76452019-09-03 17:03:03 +0200988 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +0200989 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_BELONGSTO, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200990 break;
991 default:
David Sedlákb3077192019-06-19 10:55:37 +0200992 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200993 return LY_EVALID;
994 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200995 }
Radek Krejcic59bc972018-09-17 16:13:06 +0200996 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +0100997checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200998 /* mandatory substatements */
999 if (!*prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001000 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001001 return LY_EVALID;
1002 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001003 return ret;
1004}
1005
Michal Vaskoea5abea2018-09-18 13:10:54 +02001006/**
1007 * @brief Parse the revision-date statement.
1008 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001009 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001010 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001011 * @param[in,out] rev Array to store the parsed value in.
1012 * @param[in,out] exts Extension instances to add to.
1013 *
1014 * @return LY_ERR values.
1015 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001016static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001017parse_revisiondate(struct lys_yang_parser_ctx *ctx, struct ly_in *in, char *rev, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001018{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001019 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001020 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001021 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001022 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001023
1024 if (rev[0]) {
David Sedlákb3077192019-06-19 10:55:37 +02001025 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001026 return LY_EVALID;
1027 }
1028
1029 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001030 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001031
1032 /* check value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001033 if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision-date")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001034 free(buf);
1035 return LY_EVALID;
1036 }
1037
1038 /* store value and spend buf if allocated */
1039 strncpy(rev, word, word_len);
1040 free(buf);
1041
Michal Vaskod989ba02020-08-24 10:59:24 +02001042 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001043 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001044 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001045 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_REVISIONDATE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001046 break;
1047 default:
David Sedlákb3077192019-06-19 10:55:37 +02001048 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001049 return LY_EVALID;
1050 }
1051 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001052 return ret;
1053}
1054
Michal Vaskoea5abea2018-09-18 13:10:54 +02001055/**
1056 * @brief Parse the include statement.
1057 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001058 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001059 * @param[in] module_name Name of the module to check name collisions.
Michal Vasko63f3d842020-07-08 10:10:14 +02001060 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001061 * @param[in,out] includes Parsed includes to add to.
1062 *
1063 * @return LY_ERR values.
1064 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001065static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001066parse_include(struct lys_yang_parser_ctx *ctx, const char *module_name, struct ly_in *in, struct lysp_include **includes)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001067{
Radek Krejcid33273d2018-10-25 14:55:52 +02001068 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001069 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001070 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001071 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001072 struct lysp_include *inc;
1073
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001074 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *includes, inc, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001075
1076 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001077 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001078
Radek Krejci011e4aa2020-09-04 15:22:31 +02001079 INSERT_WORD_RET(ctx, buf, inc->name, word, word_len);
Radek Krejci086c7132018-10-26 15:29:04 +02001080
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001081 /* submodules share the namespace with the module names, so there must not be
1082 * a module of the same name in the context, no need for revision matching */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001083 if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(PARSER_CTX(ctx), inc->name)) {
David Sedlákb3077192019-06-19 10:55:37 +02001084 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Name collision between module and submodule of name \"%s\".", inc->name);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001085 return LY_EVALID;
1086 }
1087
Michal Vaskod989ba02020-08-24 10:59:24 +02001088 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001089 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001090 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001091 PARSER_CHECK_STMTVER2_RET(ctx, "description", "include");
Michal Vasko63f3d842020-07-08 10:10:14 +02001092 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &inc->dsc, Y_STR_ARG, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001093 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001094 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001095 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "include");
Michal Vasko63f3d842020-07-08 10:10:14 +02001096 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &inc->ref, Y_STR_ARG, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001097 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001098 case LY_STMT_REVISION_DATE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001099 LY_CHECK_RET(parse_revisiondate(ctx, in, inc->rev, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001100 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001101 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001102 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001103 break;
1104 default:
David Sedlákb3077192019-06-19 10:55:37 +02001105 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001106 return LY_EVALID;
1107 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001108 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001109 return ret;
1110}
1111
Michal Vaskoea5abea2018-09-18 13:10:54 +02001112/**
1113 * @brief Parse the import statement.
1114 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001115 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001116 * @param[in] module_prefix Prefix of the module to check prefix collisions.
Michal Vasko63f3d842020-07-08 10:10:14 +02001117 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001118 * @param[in,out] imports Parsed imports to add to.
1119 *
1120 * @return LY_ERR values.
1121 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001122static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001123parse_import(struct lys_yang_parser_ctx *ctx, const char *module_prefix, struct ly_in *in, struct lysp_import **imports)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001124{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001125 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001126 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001127 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001128 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001129 struct lysp_import *imp;
1130
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001131 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *imports, imp, LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001132
1133 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001134 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02001135 INSERT_WORD_RET(ctx, buf, imp->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001136
Michal Vasko63f3d842020-07-08 10:10:14 +02001137 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001138 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001139 case LY_STMT_PREFIX:
Michal Vasko63f3d842020-07-08 10:10:14 +02001140 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_PREFIX, 0, &imp->prefix, Y_IDENTIF_ARG, &imp->exts));
Michal Vaskob36053d2020-03-26 15:49:30 +01001141 LY_CHECK_RET(lysp_check_prefix((struct lys_parser_ctx *)ctx, *imports, module_prefix, &imp->prefix), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001142 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001143 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001144 PARSER_CHECK_STMTVER2_RET(ctx, "description", "import");
Michal Vasko63f3d842020-07-08 10:10:14 +02001145 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &imp->dsc, Y_STR_ARG, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001146 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001147 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001148 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "import");
Michal Vasko63f3d842020-07-08 10:10:14 +02001149 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &imp->ref, Y_STR_ARG, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001150 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001151 case LY_STMT_REVISION_DATE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001152 LY_CHECK_RET(parse_revisiondate(ctx, in, imp->rev, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001153 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001154 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001155 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001156 break;
1157 default:
David Sedlákb3077192019-06-19 10:55:37 +02001158 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001159 return LY_EVALID;
1160 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001161 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001162 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001163checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001164 /* mandatory substatements */
David Sedlákb3077192019-06-19 10:55:37 +02001165 LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001166
1167 return ret;
1168}
1169
Michal Vaskoea5abea2018-09-18 13:10:54 +02001170/**
1171 * @brief Parse the revision statement.
1172 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001173 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001174 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001175 * @param[in,out] revs Parsed revisions to add to.
1176 *
1177 * @return LY_ERR values.
1178 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001179static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001180parse_revision(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_revision **revs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001181{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001182 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001183 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001184 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001185 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001186 struct lysp_revision *rev;
1187
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001188 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *revs, rev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001189
1190 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001191 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001192
1193 /* check value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001194 if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision")) {
David Sedlák68ef3dc2019-07-22 13:40:19 +02001195 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001196 return LY_EVALID;
1197 }
1198
Radek Krejcib7db73a2018-10-24 14:18:40 +02001199 strncpy(rev->date, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001200 free(buf);
1201
Michal Vaskod989ba02020-08-24 10:59:24 +02001202 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001203 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001204 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02001205 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &rev->dsc, Y_STR_ARG, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001206 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001207 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001208 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &rev->ref, Y_STR_ARG, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001209 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001210 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001211 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001212 break;
1213 default:
David Sedlákb3077192019-06-19 10:55:37 +02001214 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001215 return LY_EVALID;
1216 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001217 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001218 return ret;
1219}
1220
Michal Vaskoea5abea2018-09-18 13:10:54 +02001221/**
1222 * @brief Parse a generic text field that can have more instances such as base.
1223 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001224 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001225 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001226 * @param[in] substmt Type of this substatement.
1227 * @param[in,out] texts Parsed values to add to.
1228 * @param[in] arg Type of the expected argument.
1229 * @param[in,out] exts Extension instances to add to.
1230 *
1231 * @return LY_ERR values.
1232 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001233static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001234parse_text_fields(struct lys_yang_parser_ctx *ctx, struct ly_in *in, LYEXT_SUBSTMT substmt, const char ***texts, enum yang_arg arg,
Radek Krejci0f969882020-08-21 16:56:47 +02001235 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001236{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001237 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001238 char *buf, *word;
Radek Krejci151a5b72018-10-19 14:21:44 +02001239 const char **item;
1240 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001241 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001242
1243 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001244 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *texts, item, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001245
1246 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001247 LY_CHECK_RET(get_argument(ctx, in, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001248
Radek Krejci011e4aa2020-09-04 15:22:31 +02001249 INSERT_WORD_RET(ctx, buf, *item, word, word_len);
Michal Vaskod989ba02020-08-24 10:59:24 +02001250 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001251 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001252 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001253 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, substmt, LY_ARRAY_COUNT(*texts) - 1, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001254 break;
1255 default:
David Sedlákb3077192019-06-19 10:55:37 +02001256 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001257 return LY_EVALID;
1258 }
1259 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001260 return ret;
1261}
1262
Michal Vaskoea5abea2018-09-18 13:10:54 +02001263/**
Michal Vasko7f45cf22020-10-01 12:49:44 +02001264 * @brief Parse a generic text field that can have more instances such as base.
1265 *
1266 * @param[in] ctx yang parser context for logging.
1267 * @param[in,out] in Input structure.
1268 * @param[in] substmt Type of this substatement.
1269 * @param[in,out] qnames Parsed qnames to add to.
1270 * @param[in] arg Type of the expected argument.
1271 * @param[in,out] exts Extension instances to add to.
1272 *
1273 * @return LY_ERR values.
1274 */
1275static LY_ERR
1276parse_qnames(struct lys_yang_parser_ctx *ctx, struct ly_in *in, LYEXT_SUBSTMT substmt, struct lysp_qname **qnames,
1277 enum yang_arg arg, struct lysp_ext_instance **exts)
1278{
1279 LY_ERR ret = LY_SUCCESS;
1280 char *buf, *word;
1281 struct lysp_qname *item;
1282 size_t word_len;
1283 enum ly_stmt kw;
1284
1285 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001286 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *qnames, item, LY_EMEM);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001287
1288 /* get value */
1289 LY_CHECK_RET(get_argument(ctx, in, arg, NULL, &word, &buf, &word_len));
1290
1291 INSERT_WORD_RET(ctx, buf, item->str, word, word_len);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001292 item->mod = ctx->parsed_mod;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001293 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
1294 switch (kw) {
1295 case LY_STMT_EXTENSION_INSTANCE:
1296 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, substmt, LY_ARRAY_COUNT(*qnames) - 1, exts));
1297 break;
1298 default:
1299 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
1300 return LY_EVALID;
1301 }
1302 }
1303 return ret;
1304}
1305
1306/**
Michal Vaskoea5abea2018-09-18 13:10:54 +02001307 * @brief Parse the config statement.
1308 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001309 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001310 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001311 * @param[in,out] flags Flags to add to.
1312 * @param[in,out] exts Extension instances to add to.
1313 *
1314 * @return LY_ERR values.
1315 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001316static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001317parse_config(struct lys_yang_parser_ctx *ctx, struct ly_in *in, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001318{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001319 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001320 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001321 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001322 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001323
1324 if (*flags & LYS_CONFIG_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001325 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001326 return LY_EVALID;
1327 }
1328
1329 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001330 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001331
1332 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1333 *flags |= LYS_CONFIG_W;
1334 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1335 *flags |= LYS_CONFIG_R;
1336 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001337 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001338 free(buf);
1339 return LY_EVALID;
1340 }
1341 free(buf);
1342
Michal Vaskod989ba02020-08-24 10:59:24 +02001343 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001344 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001345 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001346 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_CONFIG, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001347 break;
1348 default:
David Sedlákb3077192019-06-19 10:55:37 +02001349 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001350 return LY_EVALID;
1351 }
1352 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001353 return ret;
1354}
1355
Michal Vaskoea5abea2018-09-18 13:10:54 +02001356/**
1357 * @brief Parse the mandatory statement.
1358 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001359 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001360 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001361 * @param[in,out] flags Flags to add to.
1362 * @param[in,out] exts Extension instances to add to.
1363 *
1364 * @return LY_ERR values.
1365 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001366static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001367parse_mandatory(struct lys_yang_parser_ctx *ctx, struct ly_in *in, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001368{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001369 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001370 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001371 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001372 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001373
1374 if (*flags & LYS_MAND_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001375 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001376 return LY_EVALID;
1377 }
1378
1379 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001380 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001381
1382 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1383 *flags |= LYS_MAND_TRUE;
1384 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1385 *flags |= LYS_MAND_FALSE;
1386 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001387 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001388 free(buf);
1389 return LY_EVALID;
1390 }
1391 free(buf);
1392
Michal Vaskod989ba02020-08-24 10:59:24 +02001393 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001394 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001395 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001396 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_MANDATORY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001397 break;
1398 default:
David Sedlákb3077192019-06-19 10:55:37 +02001399 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001400 return LY_EVALID;
1401 }
1402 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001403 return ret;
1404}
1405
Michal Vaskoea5abea2018-09-18 13:10:54 +02001406/**
1407 * @brief Parse a restriction such as range or length.
1408 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001409 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001410 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001411 * @param[in] restr_kw Type of this particular restriction.
1412 * @param[in,out] exts Extension instances to add to.
1413 *
1414 * @return LY_ERR values.
1415 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001416static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001417parse_restr(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum ly_stmt restr_kw, struct lysp_restr *restr)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001418{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001419 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001420 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001421 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001422 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001423
1424 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001425 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001426
Michal Vaskob36053d2020-03-26 15:49:30 +01001427 CHECK_NONEMPTY(ctx, word_len, ly_stmt2str(restr_kw));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001428 INSERT_WORD_RET(ctx, buf, restr->arg.str, word, word_len);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001429 restr->arg.mod = ctx->parsed_mod;
Michal Vaskod989ba02020-08-24 10:59:24 +02001430 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001431 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001432 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02001433 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001434 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001435 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001436 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001437 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001438 case LY_STMT_ERROR_APP_TAG:
Michal Vasko63f3d842020-07-08 10:10:14 +02001439 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_ERRTAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001440 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001441 case LY_STMT_ERROR_MESSAGE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001442 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_ERRMSG, 0, &restr->emsg, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001443 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001444 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001445 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001446 break;
1447 default:
David Sedlákb3077192019-06-19 10:55:37 +02001448 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001449 return LY_EVALID;
1450 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001451 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001452 return ret;
1453}
1454
Michal Vaskoea5abea2018-09-18 13:10:54 +02001455/**
1456 * @brief Parse a restriction that can have more instances such as must.
1457 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001458 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001459 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001460 * @param[in] restr_kw Type of this particular restriction.
1461 * @param[in,out] restrs Restrictions to add to.
1462 *
1463 * @return LY_ERR values.
1464 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001465static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001466parse_restrs(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum ly_stmt restr_kw, struct lysp_restr **restrs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001467{
1468 struct lysp_restr *restr;
1469
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001470 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *restrs, restr, LY_EMEM);
Michal Vasko63f3d842020-07-08 10:10:14 +02001471 return parse_restr(ctx, in, restr_kw, restr);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001472}
1473
Michal Vaskoea5abea2018-09-18 13:10:54 +02001474/**
1475 * @brief Parse the status statement.
1476 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001477 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001478 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001479 * @param[in,out] flags Flags to add to.
1480 * @param[in,out] exts Extension instances to add to.
1481 *
1482 * @return LY_ERR values.
1483 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001484static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001485parse_status(struct lys_yang_parser_ctx *ctx, struct ly_in *in, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001486{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001487 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001488 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001489 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001490 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001491
1492 if (*flags & LYS_STATUS_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001493 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001494 return LY_EVALID;
1495 }
1496
1497 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001498 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001499
1500 if ((word_len == 7) && !strncmp(word, "current", word_len)) {
1501 *flags |= LYS_STATUS_CURR;
1502 } else if ((word_len == 10) && !strncmp(word, "deprecated", word_len)) {
1503 *flags |= LYS_STATUS_DEPRC;
1504 } else if ((word_len == 8) && !strncmp(word, "obsolete", word_len)) {
1505 *flags |= LYS_STATUS_OBSLT;
1506 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001507 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001508 free(buf);
1509 return LY_EVALID;
1510 }
1511 free(buf);
1512
Michal Vaskod989ba02020-08-24 10:59:24 +02001513 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001514 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001515 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001516 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_STATUS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001517 break;
1518 default:
David Sedlákb3077192019-06-19 10:55:37 +02001519 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001520 return LY_EVALID;
1521 }
1522 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001523 return ret;
1524}
1525
Michal Vaskoea5abea2018-09-18 13:10:54 +02001526/**
1527 * @brief Parse the when statement.
1528 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001529 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001530 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001531 * @param[in,out] when_p When pointer to parse to.
1532 *
1533 * @return LY_ERR values.
1534 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001535LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001536parse_when(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001537{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001538 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001539 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001540 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001541 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001542 struct lysp_when *when;
1543
1544 if (*when_p) {
David Sedlákb3077192019-06-19 10:55:37 +02001545 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001546 return LY_EVALID;
1547 }
1548
1549 when = calloc(1, sizeof *when);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001550 LY_CHECK_ERR_RET(!when, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001551 *when_p = when;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001552
1553 /* get value */
Radek Krejci011e4aa2020-09-04 15:22:31 +02001554 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01001555 CHECK_NONEMPTY(ctx, word_len, "when");
Radek Krejci011e4aa2020-09-04 15:22:31 +02001556 INSERT_WORD_RET(ctx, buf, when->cond, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001557
Michal Vaskod989ba02020-08-24 10:59:24 +02001558 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001559 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001560 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02001561 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &when->dsc, Y_STR_ARG, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001562 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001563 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001564 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &when->ref, Y_STR_ARG, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001565 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001566 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001567 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001568 break;
1569 default:
David Sedlákb3077192019-06-19 10:55:37 +02001570 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001571 return LY_EVALID;
1572 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001573 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001574 return ret;
1575}
1576
Michal Vaskoea5abea2018-09-18 13:10:54 +02001577/**
1578 * @brief Parse the anydata or anyxml statement.
1579 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001580 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001581 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001582 * @param[in] kw Type of this particular keyword.
1583 * @param[in,out] siblings Siblings to add to.
1584 *
1585 * @return LY_ERR values.
1586 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02001587LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001588parse_any(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum ly_stmt kw, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001589{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001590 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001591 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001592 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001593 struct lysp_node_anydata *any;
1594
David Sedlák60adc092019-08-06 15:57:02 +02001595 /* create new structure and insert into siblings */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001596 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, any, next, LY_EMEM);
David Sedlák60adc092019-08-06 15:57:02 +02001597
Radek Krejcid6b76452019-09-03 17:03:03 +02001598 any->nodetype = kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001599 any->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001600
Michal Vasko7fbc8162018-09-17 10:35:16 +02001601 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02001602 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02001603 INSERT_WORD_RET(ctx, buf, any->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001604
1605 /* parse substatements */
Michal Vaskod989ba02020-08-24 10:59:24 +02001606 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001607 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001608 case LY_STMT_CONFIG:
Michal Vasko63f3d842020-07-08 10:10:14 +02001609 LY_CHECK_RET(parse_config(ctx, in, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001610 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001611 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02001612 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &any->dsc, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001613 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001614 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02001615 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &any->iffeatures, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001616 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001617 case LY_STMT_MANDATORY:
Michal Vasko63f3d842020-07-08 10:10:14 +02001618 LY_CHECK_RET(parse_mandatory(ctx, in, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001619 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001620 case LY_STMT_MUST:
Michal Vasko63f3d842020-07-08 10:10:14 +02001621 LY_CHECK_RET(parse_restrs(ctx, in, kw, &any->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001622 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001623 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001624 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &any->ref, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001625 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001626 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02001627 LY_CHECK_RET(parse_status(ctx, in, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001628 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001629 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02001630 LY_CHECK_RET(parse_when(ctx, in, &any->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001631 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001632 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001633 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001634 break;
1635 default:
David Sedlákb3077192019-06-19 10:55:37 +02001636 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
Radek Krejci0f969882020-08-21 16:56:47 +02001637 (any->nodetype & LYS_ANYDATA) == LYS_ANYDATA ? ly_stmt2str(LY_STMT_ANYDATA) : ly_stmt2str(LY_STMT_ANYXML));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001638 return LY_EVALID;
1639 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001640 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001641 return ret;
1642}
1643
Michal Vaskoea5abea2018-09-18 13:10:54 +02001644/**
1645 * @brief Parse the value or position statement. Substatement of type enum statement.
1646 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001647 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001648 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001649 * @param[in] val_kw Type of this particular keyword.
1650 * @param[in,out] value Value to write to.
1651 * @param[in,out] flags Flags to write to.
1652 * @param[in,out] exts Extension instances to add to.
1653 *
1654 * @return LY_ERR values.
1655 */
David Sedlákd6ce6d72019-07-16 17:30:18 +02001656LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001657parse_type_enum_value_pos(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum ly_stmt val_kw, int64_t *value, uint16_t *flags,
Radek Krejci0f969882020-08-21 16:56:47 +02001658 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001659{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001660 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001661 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001662 size_t word_len;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02001663 long int num = 0;
1664 unsigned long int unum = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001665 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001666
1667 if (*flags & LYS_SET_VALUE) {
David Sedlákb3077192019-06-19 10:55:37 +02001668 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001669 return LY_EVALID;
1670 }
1671 *flags |= LYS_SET_VALUE;
1672
1673 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001674 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001675
Radek Krejcid6b76452019-09-03 17:03:03 +02001676 if (!word_len || (word[0] == '+') || ((word[0] == '0') && (word_len > 1)) || ((val_kw == LY_STMT_POSITION) && !strncmp(word, "-0", 2))) {
David Sedlákb3077192019-06-19 10:55:37 +02001677 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001678 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001679 }
1680
1681 errno = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001682 if (val_kw == LY_STMT_VALUE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001683 num = strtol(word, &ptr, 10);
Michal Vasko69730152020-10-09 16:30:07 +02001684 if ((num < INT64_C(-2147483648)) || (num > INT64_C(2147483647))) {
David Sedlákb3077192019-06-19 10:55:37 +02001685 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001686 goto error;
1687 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001688 } else {
1689 unum = strtoul(word, &ptr, 10);
Radek Krejci8b764662018-11-14 14:15:13 +01001690 if (unum > UINT64_C(4294967295)) {
David Sedlákb3077192019-06-19 10:55:37 +02001691 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001692 goto error;
1693 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001694 }
1695 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001696 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001697 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001698 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001699 }
1700 if (errno == ERANGE) {
David Sedlákb3077192019-06-19 10:55:37 +02001701 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001702 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001703 }
Radek Krejcid6b76452019-09-03 17:03:03 +02001704 if (val_kw == LY_STMT_VALUE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001705 *value = num;
1706 } else {
1707 *value = unum;
1708 }
1709 free(buf);
1710
Michal Vaskod989ba02020-08-24 10:59:24 +02001711 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001712 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001713 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001714 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, val_kw == LY_STMT_VALUE ? LYEXT_SUBSTMT_VALUE : LYEXT_SUBSTMT_POSITION, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001715 break;
1716 default:
David Sedlákb3077192019-06-19 10:55:37 +02001717 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001718 return LY_EVALID;
1719 }
1720 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001721 return ret;
Radek Krejci8b764662018-11-14 14:15:13 +01001722
1723error:
1724 free(buf);
1725 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001726}
1727
Michal Vaskoea5abea2018-09-18 13:10:54 +02001728/**
1729 * @brief Parse the enum or bit statement. Substatement of type statement.
1730 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001731 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001732 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001733 * @param[in] enum_kw Type of this particular keyword.
1734 * @param[in,out] enums Enums or bits to add to.
1735 *
1736 * @return LY_ERR values.
1737 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001738static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001739parse_type_enum(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum ly_stmt enum_kw, struct lysp_type_enum **enums)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001740{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001741 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001742 char *buf, *word;
David Sedlák6544c182019-07-12 13:17:33 +02001743 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001744 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001745 struct lysp_type_enum *enm;
1746
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001747 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *enums, enm, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001748
1749 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001750 LY_CHECK_RET(get_argument(ctx, in, enum_kw == LY_STMT_ENUM ? Y_STR_ARG : Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejcid6b76452019-09-03 17:03:03 +02001751 if (enum_kw == LY_STMT_ENUM) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001752 ret = lysp_check_enum_name((struct lys_parser_ctx *)ctx, (const char *)word, word_len);
David Sedlák6544c182019-07-12 13:17:33 +02001753 LY_CHECK_ERR_RET(ret, free(buf), ret);
Radek Krejci335332a2019-09-05 13:03:35 +02001754 } /* else nothing specific for YANG_BIT */
Radek Krejci8b764662018-11-14 14:15:13 +01001755
Radek Krejci011e4aa2020-09-04 15:22:31 +02001756 INSERT_WORD_RET(ctx, buf, enm->name, word, word_len);
Radek Krejci8b764662018-11-14 14:15:13 +01001757 CHECK_UNIQUENESS(ctx, *enums, name, ly_stmt2str(enum_kw), enm->name);
1758
Michal Vaskod989ba02020-08-24 10:59:24 +02001759 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001760 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001761 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02001762 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &enm->dsc, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001763 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001764 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02001765 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", ly_stmt2str(enum_kw));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001766 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &enm->iffeatures, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001767 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001768 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001769 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &enm->ref, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001770 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001771 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02001772 LY_CHECK_RET(parse_status(ctx, in, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001773 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001774 case LY_STMT_VALUE:
1775 LY_CHECK_ERR_RET(enum_kw == LY_STMT_BIT, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
Michal Vasko69730152020-10-09 16:30:07 +02001776 ly_stmt2str(enum_kw)), LY_EVALID);
Michal Vasko63f3d842020-07-08 10:10:14 +02001777 LY_CHECK_RET(parse_type_enum_value_pos(ctx, in, kw, &enm->value, &enm->flags, &enm->exts));
David Sedlák9fb515f2019-07-11 10:33:58 +02001778 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001779 case LY_STMT_POSITION:
1780 LY_CHECK_ERR_RET(enum_kw == LY_STMT_ENUM, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
Michal Vasko69730152020-10-09 16:30:07 +02001781 ly_stmt2str(enum_kw)), LY_EVALID);
Michal Vasko63f3d842020-07-08 10:10:14 +02001782 LY_CHECK_RET(parse_type_enum_value_pos(ctx, in, kw, &enm->value, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001783 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001784 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001785 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001786 break;
1787 default:
David Sedlákb3077192019-06-19 10:55:37 +02001788 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001789 return LY_EVALID;
1790 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001791 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001792 return ret;
1793}
1794
Michal Vaskoea5abea2018-09-18 13:10:54 +02001795/**
1796 * @brief Parse the fraction-digits statement. Substatement of type statement.
1797 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001798 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001799 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001800 * @param[in,out] fracdig Value to write to.
1801 * @param[in,out] exts Extension instances to add to.
1802 *
1803 * @return LY_ERR values.
1804 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001805static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001806parse_type_fracdigits(struct lys_yang_parser_ctx *ctx, struct ly_in *in, uint8_t *fracdig, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001807{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001808 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001809 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001810 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001811 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02001812 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001813
1814 if (*fracdig) {
David Sedlákb3077192019-06-19 10:55:37 +02001815 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001816 return LY_EVALID;
1817 }
1818
1819 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001820 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001821
1822 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
David Sedlákb3077192019-06-19 10:55:37 +02001823 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001824 free(buf);
1825 return LY_EVALID;
1826 }
1827
1828 errno = 0;
1829 num = strtoul(word, &ptr, 10);
1830 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001831 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001832 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001833 free(buf);
1834 return LY_EVALID;
1835 }
1836 if ((errno == ERANGE) || (num > 18)) {
David Sedlákb3077192019-06-19 10:55:37 +02001837 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001838 free(buf);
1839 return LY_EVALID;
1840 }
1841 *fracdig = num;
1842 free(buf);
1843
Michal Vaskod989ba02020-08-24 10:59:24 +02001844 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001845 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001846 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001847 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_FRACDIGITS, 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), "fraction-digits");
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 require-instance statement. Substatement of type statement.
1859 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001860 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001861 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001862 * @param[in,out] reqinst Value to write to.
1863 * @param[in,out] flags Flags to write to.
1864 * @param[in,out] exts Extension instances to add to.
1865 *
1866 * @return LY_ERR values.
1867 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001868static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001869parse_type_reqinstance(struct lys_yang_parser_ctx *ctx, struct ly_in *in, uint8_t *reqinst, uint16_t *flags,
Radek Krejci0f969882020-08-21 16:56:47 +02001870 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001871{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001872 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001873 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001874 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001875 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001876
1877 if (*flags & LYS_SET_REQINST) {
David Sedlákb3077192019-06-19 10:55:37 +02001878 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001879 return LY_EVALID;
1880 }
1881 *flags |= LYS_SET_REQINST;
1882
1883 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001884 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001885
1886 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1887 *reqinst = 1;
1888 } else if ((word_len != 5) || strncmp(word, "false", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001889 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001890 free(buf);
1891 return LY_EVALID;
1892 }
1893 free(buf);
1894
Michal Vaskod989ba02020-08-24 10:59:24 +02001895 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001896 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001897 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001898 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_REQINSTANCE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001899 break;
1900 default:
David Sedlákb3077192019-06-19 10:55:37 +02001901 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001902 return LY_EVALID;
1903 }
1904 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001905 return ret;
1906}
1907
Michal Vaskoea5abea2018-09-18 13:10:54 +02001908/**
1909 * @brief Parse the modifier statement. Substatement of type pattern statement.
1910 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001911 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001912 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001913 * @param[in,out] pat Value to write to.
1914 * @param[in,out] exts Extension instances to add to.
1915 *
1916 * @return LY_ERR values.
1917 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001918static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001919parse_type_pattern_modifier(struct lys_yang_parser_ctx *ctx, struct ly_in *in, const char **pat,
Radek Krejci0f969882020-08-21 16:56:47 +02001920 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001921{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001922 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001923 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001924 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001925 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001926
1927 if ((*pat)[0] == 0x15) {
David Sedlákb3077192019-06-19 10:55:37 +02001928 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001929 return LY_EVALID;
1930 }
1931
1932 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001933 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001934
1935 if ((word_len != 12) || strncmp(word, "invert-match", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001936 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001937 free(buf);
1938 return LY_EVALID;
1939 }
1940 free(buf);
1941
1942 /* replace the value in the dictionary */
1943 buf = malloc(strlen(*pat) + 1);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001944 LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001945 strcpy(buf, *pat);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001946 lydict_remove(PARSER_CTX(ctx), *pat);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001947
1948 assert(buf[0] == 0x06);
1949 buf[0] = 0x15;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001950 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, pat));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001951
Michal Vaskod989ba02020-08-24 10:59:24 +02001952 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001953 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001954 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001955 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_MODIFIER, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001956 break;
1957 default:
David Sedlákb3077192019-06-19 10:55:37 +02001958 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001959 return LY_EVALID;
1960 }
1961 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001962 return ret;
1963}
1964
Michal Vaskoea5abea2018-09-18 13:10:54 +02001965/**
1966 * @brief Parse the pattern statement. Substatement of type statement.
1967 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001968 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001969 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001970 * @param[in,out] patterns Restrictions to add to.
1971 *
1972 * @return LY_ERR values.
1973 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001974static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001975parse_type_pattern(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001976{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001977 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001978 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001979 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001980 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001981 struct lysp_restr *restr;
1982
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001983 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *patterns, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001984
1985 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001986 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001987
1988 /* add special meaning first byte */
1989 if (buf) {
1990 buf = realloc(buf, word_len + 2);
1991 word = buf;
1992 } else {
1993 buf = malloc(word_len + 2);
1994 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001995 LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci86d106e2018-10-18 09:53:19 +02001996 memmove(buf + 1, word, word_len);
1997 buf[0] = 0x06; /* pattern's default regular-match flag */
1998 buf[word_len + 1] = '\0'; /* terminating NULL byte */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001999 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, &restr->arg.str));
2000 restr->arg.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002001
Michal Vaskod989ba02020-08-24 10:59:24 +02002002 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002003 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002004 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002005 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002006 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002007 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002008 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002009 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002010 case LY_STMT_ERROR_APP_TAG:
Michal Vasko63f3d842020-07-08 10:10:14 +02002011 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_ERRTAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002012 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002013 case LY_STMT_ERROR_MESSAGE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002014 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_ERRMSG, 0, &restr->emsg, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002015 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002016 case LY_STMT_MODIFIER:
Radek Krejci335332a2019-09-05 13:03:35 +02002017 PARSER_CHECK_STMTVER2_RET(ctx, "modifier", "pattern");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002018 LY_CHECK_RET(parse_type_pattern_modifier(ctx, in, &restr->arg.str, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002019 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002020 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002021 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002022 break;
2023 default:
David Sedlákb3077192019-06-19 10:55:37 +02002024 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002025 return LY_EVALID;
2026 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002027 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002028 return ret;
2029}
2030
Michal Vaskoea5abea2018-09-18 13:10:54 +02002031/**
2032 * @brief Parse the type statement.
2033 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002034 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002035 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002036 * @param[in,out] type Type to wrote to.
2037 *
2038 * @return LY_ERR values.
2039 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002040static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002041parse_type(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002042{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002043 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002044 char *buf, *word;
Michal Vasko004d3152020-06-11 19:59:22 +02002045 const char *str_path = NULL;
Radek Krejciefd22f62018-09-27 11:47:58 +02002046 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002047 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002048 struct lysp_type *nest_type;
2049
2050 if (type->name) {
David Sedlákb3077192019-06-19 10:55:37 +02002051 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002052 return LY_EVALID;
2053 }
2054
2055 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002056 LY_CHECK_RET(get_argument(ctx, in, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002057 INSERT_WORD_RET(ctx, buf, type->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002058
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002059 /* set module */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002060 type->pmod = ctx->parsed_mod;
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002061
Michal Vaskod989ba02020-08-24 10:59:24 +02002062 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002063 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002064 case LY_STMT_BASE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002065 LY_CHECK_RET(parse_text_fields(ctx, in, LYEXT_SUBSTMT_BASE, &type->bases, Y_PREF_IDENTIF_ARG, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002066 type->flags |= LYS_SET_BASE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002067 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002068 case LY_STMT_BIT:
Michal Vasko63f3d842020-07-08 10:10:14 +02002069 LY_CHECK_RET(parse_type_enum(ctx, in, kw, &type->bits));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002070 type->flags |= LYS_SET_BIT;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002071 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002072 case LY_STMT_ENUM:
Michal Vasko63f3d842020-07-08 10:10:14 +02002073 LY_CHECK_RET(parse_type_enum(ctx, in, kw, &type->enums));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002074 type->flags |= LYS_SET_ENUM;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002075 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002076 case LY_STMT_FRACTION_DIGITS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002077 LY_CHECK_RET(parse_type_fracdigits(ctx, in, &type->fraction_digits, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002078 type->flags |= LYS_SET_FRDIGITS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002079 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002080 case LY_STMT_LENGTH:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002081 if (type->length) {
David Sedlákb3077192019-06-19 10:55:37 +02002082 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002083 return LY_EVALID;
2084 }
2085 type->length = calloc(1, sizeof *type->length);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002086 LY_CHECK_ERR_RET(!type->length, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002087
Michal Vasko63f3d842020-07-08 10:10:14 +02002088 LY_CHECK_RET(parse_restr(ctx, in, kw, type->length));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002089 type->flags |= LYS_SET_LENGTH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002090 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002091 case LY_STMT_PATH:
Michal Vasko004d3152020-06-11 19:59:22 +02002092 if (type->path) {
2093 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyext_substmt2str(LYEXT_SUBSTMT_PATH));
2094 return LY_EVALID;
2095 }
2096
Michal Vasko63f3d842020-07-08 10:10:14 +02002097 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_PATH, 0, &str_path, Y_STR_ARG, &type->exts));
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002098 ret = ly_path_parse(PARSER_CTX(ctx), NULL, str_path, 0, LY_PATH_BEGIN_EITHER, LY_PATH_LREF_TRUE,
Michal Vasko69730152020-10-09 16:30:07 +02002099 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002100 lydict_remove(PARSER_CTX(ctx), str_path);
Michal Vasko004d3152020-06-11 19:59:22 +02002101 LY_CHECK_RET(ret);
Radek Krejcid505e3d2018-11-13 09:04:17 +01002102 type->flags |= LYS_SET_PATH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002103 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002104 case LY_STMT_PATTERN:
Michal Vasko63f3d842020-07-08 10:10:14 +02002105 LY_CHECK_RET(parse_type_pattern(ctx, in, &type->patterns));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002106 type->flags |= LYS_SET_PATTERN;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002107 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002108 case LY_STMT_RANGE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002109 if (type->range) {
David Sedlákb3077192019-06-19 10:55:37 +02002110 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002111 return LY_EVALID;
2112 }
2113 type->range = calloc(1, sizeof *type->range);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002114 LY_CHECK_ERR_RET(!type->range, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002115
Michal Vasko63f3d842020-07-08 10:10:14 +02002116 LY_CHECK_RET(parse_restr(ctx, in, kw, type->range));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002117 type->flags |= LYS_SET_RANGE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002118 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002119 case LY_STMT_REQUIRE_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002120 LY_CHECK_RET(parse_type_reqinstance(ctx, in, &type->require_instance, &type->flags, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002121 /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002122 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002123 case LY_STMT_TYPE:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002124 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), type->types, nest_type, LY_EMEM);
Michal Vasko63f3d842020-07-08 10:10:14 +02002125 LY_CHECK_RET(parse_type(ctx, in, nest_type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002126 type->flags |= LYS_SET_TYPE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002127 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002128 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002129 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &type->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002130 break;
2131 default:
David Sedlákb3077192019-06-19 10:55:37 +02002132 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002133 return LY_EVALID;
2134 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002135 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002136 return ret;
2137}
2138
Michal Vaskoea5abea2018-09-18 13:10:54 +02002139/**
2140 * @brief Parse the leaf statement.
2141 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002142 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002143 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002144 * @param[in,out] siblings Siblings to add to.
2145 *
2146 * @return LY_ERR values.
2147 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002148LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002149parse_leaf(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002150{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002151 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002152 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002153 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002154 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002155 struct lysp_node_leaf *leaf;
2156
David Sedlák60adc092019-08-06 15:57:02 +02002157 /* create new leaf structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002158 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, leaf, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002159 leaf->nodetype = LYS_LEAF;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002160 leaf->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002161
Michal Vasko7fbc8162018-09-17 10:35:16 +02002162 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02002163 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002164 INSERT_WORD_RET(ctx, buf, leaf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002165
2166 /* parse substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02002167 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002168 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002169 case LY_STMT_CONFIG:
Michal Vasko63f3d842020-07-08 10:10:14 +02002170 LY_CHECK_RET(parse_config(ctx, in, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002171 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002172 case LY_STMT_DEFAULT:
Michal Vasko7f45cf22020-10-01 12:49:44 +02002173 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DEFAULT, 0, &leaf->dflt.str, Y_STR_ARG, &leaf->exts));
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002174 leaf->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002175 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002176 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002177 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &leaf->dsc, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002178 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002179 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02002180 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &leaf->iffeatures, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002181 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002182 case LY_STMT_MANDATORY:
Michal Vasko63f3d842020-07-08 10:10:14 +02002183 LY_CHECK_RET(parse_mandatory(ctx, in, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002184 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002185 case LY_STMT_MUST:
Michal Vasko63f3d842020-07-08 10:10:14 +02002186 LY_CHECK_RET(parse_restrs(ctx, in, kw, &leaf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002187 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002188 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002189 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &leaf->ref, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002190 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002191 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002192 LY_CHECK_RET(parse_status(ctx, in, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002193 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002194 case LY_STMT_TYPE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002195 LY_CHECK_RET(parse_type(ctx, in, &leaf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002196 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002197 case LY_STMT_UNITS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002198 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_UNITS, 0, &leaf->units, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002199 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002200 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02002201 LY_CHECK_RET(parse_when(ctx, in, &leaf->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002202 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002203 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002204 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002205 break;
2206 default:
David Sedlákb3077192019-06-19 10:55:37 +02002207 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002208 return LY_EVALID;
2209 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002210 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002211 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002212checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002213 /* mandatory substatements */
2214 if (!leaf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002215 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002216 return LY_EVALID;
2217 }
2218
2219 return ret;
2220}
2221
Michal Vaskoea5abea2018-09-18 13:10:54 +02002222/**
2223 * @brief Parse the max-elements statement.
2224 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002225 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002226 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002227 * @param[in,out] max Value to write to.
2228 * @param[in,out] flags Flags to write to.
2229 * @param[in,out] exts Extension instances to add to.
2230 *
2231 * @return LY_ERR values.
2232 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002233LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002234parse_maxelements(struct lys_yang_parser_ctx *ctx, struct ly_in *in, uint32_t *max, uint16_t *flags,
Radek Krejci0f969882020-08-21 16:56:47 +02002235 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002236{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002237 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002238 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002239 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002240 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002241 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002242
2243 if (*flags & LYS_SET_MAX) {
David Sedlákb3077192019-06-19 10:55:37 +02002244 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002245 return LY_EVALID;
2246 }
2247 *flags |= LYS_SET_MAX;
2248
2249 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002250 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002251
2252 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
David Sedlákb3077192019-06-19 10:55:37 +02002253 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002254 free(buf);
2255 return LY_EVALID;
2256 }
2257
Radek Krejci7f9b6512019-09-18 13:11:09 +02002258 if (ly_strncmp("unbounded", word, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002259 errno = 0;
2260 num = strtoul(word, &ptr, 10);
2261 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002262 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002263 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002264 free(buf);
2265 return LY_EVALID;
2266 }
2267 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002268 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002269 free(buf);
2270 return LY_EVALID;
2271 }
2272
2273 *max = num;
2274 }
2275 free(buf);
2276
Michal Vaskod989ba02020-08-24 10:59:24 +02002277 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002278 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002279 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002280 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_MAX, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002281 break;
2282 default:
David Sedlákb3077192019-06-19 10:55:37 +02002283 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002284 return LY_EVALID;
2285 }
2286 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002287 return ret;
2288}
2289
Michal Vaskoea5abea2018-09-18 13:10:54 +02002290/**
2291 * @brief Parse the min-elements statement.
2292 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002293 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002294 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002295 * @param[in,out] min Value to write to.
2296 * @param[in,out] flags Flags to write to.
2297 * @param[in,out] exts Extension instances to add to.
2298 *
2299 * @return LY_ERR values.
2300 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002301LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002302parse_minelements(struct lys_yang_parser_ctx *ctx, struct ly_in *in, uint32_t *min, uint16_t *flags,
Radek Krejci0f969882020-08-21 16:56:47 +02002303 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002304{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002305 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002306 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002307 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002308 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002309 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002310
2311 if (*flags & LYS_SET_MIN) {
David Sedlákb3077192019-06-19 10:55:37 +02002312 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002313 return LY_EVALID;
2314 }
2315 *flags |= LYS_SET_MIN;
2316
2317 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002318 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002319
2320 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
David Sedlákb3077192019-06-19 10:55:37 +02002321 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002322 free(buf);
2323 return LY_EVALID;
2324 }
2325
2326 errno = 0;
2327 num = strtoul(word, &ptr, 10);
2328 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002329 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002330 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002331 free(buf);
2332 return LY_EVALID;
2333 }
2334 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002335 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002336 free(buf);
2337 return LY_EVALID;
2338 }
2339 *min = num;
2340 free(buf);
2341
Michal Vaskod989ba02020-08-24 10:59:24 +02002342 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002343 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002344 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002345 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_MIN, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002346 break;
2347 default:
David Sedlákb3077192019-06-19 10:55:37 +02002348 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002349 return LY_EVALID;
2350 }
2351 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002352 return ret;
2353}
2354
Michal Vaskoea5abea2018-09-18 13:10:54 +02002355/**
2356 * @brief Parse the ordered-by statement.
2357 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002358 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002359 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002360 * @param[in,out] flags Flags to write to.
2361 * @param[in,out] exts Extension instances to add to.
2362 *
2363 * @return LY_ERR values.
2364 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002365static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002366parse_orderedby(struct lys_yang_parser_ctx *ctx, struct ly_in *in, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002367{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002368 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002369 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002370 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002371 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002372
2373 if (*flags & LYS_ORDBY_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02002374 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002375 return LY_EVALID;
2376 }
2377
2378 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002379 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002380
2381 if ((word_len == 6) && !strncmp(word, "system", word_len)) {
2382 *flags |= LYS_ORDBY_SYSTEM;
2383 } else if ((word_len == 4) && !strncmp(word, "user", word_len)) {
2384 *flags |= LYS_ORDBY_USER;
2385 } else {
David Sedlákb3077192019-06-19 10:55:37 +02002386 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002387 free(buf);
2388 return LY_EVALID;
2389 }
2390 free(buf);
2391
Michal Vaskod989ba02020-08-24 10:59:24 +02002392 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002393 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002394 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002395 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_ORDEREDBY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002396 break;
2397 default:
David Sedlákb3077192019-06-19 10:55:37 +02002398 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002399 return LY_EVALID;
2400 }
2401 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002402 return ret;
2403}
2404
Michal Vaskoea5abea2018-09-18 13:10:54 +02002405/**
2406 * @brief Parse the leaf-list statement.
2407 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002408 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002409 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002410 * @param[in,out] siblings Siblings to add to.
2411 *
2412 * @return LY_ERR values.
2413 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002414LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002415parse_leaflist(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002416{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002417 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002418 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002419 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002420 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002421 struct lysp_node_leaflist *llist;
2422
David Sedlák60adc092019-08-06 15:57:02 +02002423 /* create new leaf-list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002424 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, llist, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002425 llist->nodetype = LYS_LEAFLIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002426 llist->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002427
Michal Vasko7fbc8162018-09-17 10:35:16 +02002428 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02002429 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002430 INSERT_WORD_RET(ctx, buf, llist->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002431
2432 /* parse substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02002433 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002434 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002435 case LY_STMT_CONFIG:
Michal Vasko63f3d842020-07-08 10:10:14 +02002436 LY_CHECK_RET(parse_config(ctx, in, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002437 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002438 case LY_STMT_DEFAULT:
Radek Krejci335332a2019-09-05 13:03:35 +02002439 PARSER_CHECK_STMTVER2_RET(ctx, "default", "leaf-list");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002440 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_DEFAULT, &llist->dflts, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002441 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002442 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002443 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &llist->dsc, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002444 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002445 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02002446 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &llist->iffeatures, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002447 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002448 case LY_STMT_MAX_ELEMENTS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002449 LY_CHECK_RET(parse_maxelements(ctx, in, &llist->max, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002450 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002451 case LY_STMT_MIN_ELEMENTS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002452 LY_CHECK_RET(parse_minelements(ctx, in, &llist->min, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002453 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002454 case LY_STMT_MUST:
Michal Vasko63f3d842020-07-08 10:10:14 +02002455 LY_CHECK_RET(parse_restrs(ctx, in, kw, &llist->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002456 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002457 case LY_STMT_ORDERED_BY:
Michal Vasko63f3d842020-07-08 10:10:14 +02002458 LY_CHECK_RET(parse_orderedby(ctx, in, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002459 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002460 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002461 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &llist->ref, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002462 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002463 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002464 LY_CHECK_RET(parse_status(ctx, in, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002465 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002466 case LY_STMT_TYPE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002467 LY_CHECK_RET(parse_type(ctx, in, &llist->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002468 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002469 case LY_STMT_UNITS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002470 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_UNITS, 0, &llist->units, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002471 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002472 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02002473 LY_CHECK_RET(parse_when(ctx, in, &llist->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002474 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002475 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002476 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002477 break;
2478 default:
David Sedlákb3077192019-06-19 10:55:37 +02002479 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002480 return LY_EVALID;
2481 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002482 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002483 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002484checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002485 /* mandatory substatements */
2486 if (!llist->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002487 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002488 return LY_EVALID;
2489 }
2490
2491 return ret;
2492}
2493
Michal Vaskoea5abea2018-09-18 13:10:54 +02002494/**
2495 * @brief Parse the refine statement.
2496 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002497 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002498 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002499 * @param[in,out] refines Refines to add to.
2500 *
2501 * @return LY_ERR values.
2502 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002503static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002504parse_refine(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002505{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002506 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002507 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002508 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002509 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002510 struct lysp_refine *rf;
2511
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002512 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002513
2514 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002515 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01002516 CHECK_NONEMPTY(ctx, word_len, "refine");
Radek Krejci011e4aa2020-09-04 15:22:31 +02002517 INSERT_WORD_RET(ctx, buf, rf->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002518
Michal Vaskod989ba02020-08-24 10:59:24 +02002519 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002520 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002521 case LY_STMT_CONFIG:
Michal Vasko63f3d842020-07-08 10:10:14 +02002522 LY_CHECK_RET(parse_config(ctx, in, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002523 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002524 case LY_STMT_DEFAULT:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002525 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_DEFAULT, &rf->dflts, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002526 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002527 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002528 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &rf->dsc, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002529 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002530 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02002531 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "refine");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002532 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &rf->iffeatures, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002533 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002534 case LY_STMT_MAX_ELEMENTS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002535 LY_CHECK_RET(parse_maxelements(ctx, in, &rf->max, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002536 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002537 case LY_STMT_MIN_ELEMENTS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002538 LY_CHECK_RET(parse_minelements(ctx, in, &rf->min, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002539 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002540 case LY_STMT_MUST:
Michal Vasko63f3d842020-07-08 10:10:14 +02002541 LY_CHECK_RET(parse_restrs(ctx, in, kw, &rf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002542 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002543 case LY_STMT_MANDATORY:
Michal Vasko63f3d842020-07-08 10:10:14 +02002544 LY_CHECK_RET(parse_mandatory(ctx, in, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002545 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002546 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002547 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &rf->ref, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002548 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002549 case LY_STMT_PRESENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002550 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_PRESENCE, 0, &rf->presence, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002551 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002552 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002553 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002554 break;
2555 default:
David Sedlákb3077192019-06-19 10:55:37 +02002556 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002557 return LY_EVALID;
2558 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002559 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002560 return ret;
2561}
2562
Michal Vaskoea5abea2018-09-18 13:10:54 +02002563/**
2564 * @brief Parse the typedef statement.
2565 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002566 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002567 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002568 * @param[in,out] typedefs Typedefs to add to.
2569 *
2570 * @return LY_ERR values.
2571 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002572static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002573parse_typedef(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct ly_in *in, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002574{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002575 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002576 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002577 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002578 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002579 struct lysp_tpdf *tpdf;
2580
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002581 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002582
2583 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002584 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002585 INSERT_WORD_RET(ctx, buf, tpdf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002586
2587 /* parse substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02002588 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002589 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002590 case LY_STMT_DEFAULT:
Michal Vasko7f45cf22020-10-01 12:49:44 +02002591 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DEFAULT, 0, &tpdf->dflt.str, Y_STR_ARG, &tpdf->exts));
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002592 tpdf->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002593 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002594 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002595 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &tpdf->dsc, Y_STR_ARG, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002596 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002597 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002598 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &tpdf->ref, Y_STR_ARG, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002599 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002600 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002601 LY_CHECK_RET(parse_status(ctx, in, &tpdf->flags, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002602 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002603 case LY_STMT_TYPE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002604 LY_CHECK_RET(parse_type(ctx, in, &tpdf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002605 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002606 case LY_STMT_UNITS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002607 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_UNITS, 0, &tpdf->units, Y_STR_ARG, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002608 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002609 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002610 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002611 break;
2612 default:
David Sedlákb3077192019-06-19 10:55:37 +02002613 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002614 return LY_EVALID;
2615 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002616 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002617 LY_CHECK_RET(ret);
Radek Krejcibbe09a92018-11-08 09:36:54 +01002618checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002619 /* mandatory substatements */
2620 if (!tpdf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002621 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002622 return LY_EVALID;
2623 }
2624
Radek Krejcibbe09a92018-11-08 09:36:54 +01002625 /* store data for collision check */
Michal Vasko7f45cf22020-10-01 12:49:44 +02002626 if (parent && !(parent->nodetype & (LYS_GROUPING | LYS_RPC | LYS_ACTION | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF))) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02002627 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, parent, 0, NULL));
Radek Krejcibbe09a92018-11-08 09:36:54 +01002628 }
2629
Michal Vasko7fbc8162018-09-17 10:35:16 +02002630 return ret;
2631}
2632
Michal Vaskoea5abea2018-09-18 13:10:54 +02002633/**
2634 * @brief Parse the input or output statement.
2635 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002636 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002637 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002638 * @param[in] kw Type of this particular keyword
2639 * @param[in,out] inout_p Input/output pointer to write to.
2640 *
2641 * @return LY_ERR values.
2642 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002643static LY_ERR
Michal Vasko7f45cf22020-10-01 12:49:44 +02002644parse_inout(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum ly_stmt inout_kw, struct lysp_node *parent,
2645 struct lysp_action_inout *inout_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002646{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002647 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002648 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002649 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002650 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002651
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002652 if (inout_p->nodetype) {
David Sedlákb3077192019-06-19 10:55:37 +02002653 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002654 return LY_EVALID;
2655 }
2656
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002657 /* initiate structure */
Michal Vasko22df3f02020-08-24 13:29:22 +02002658 inout_p->nodetype = &((struct lysp_action *)parent)->input == inout_p ? LYS_INPUT : LYS_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002659 inout_p->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002660
2661 /* parse substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02002662 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002663 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002664 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002665 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", ly_stmt2str(inout_kw));
Radek Krejci0f969882020-08-21 16:56:47 +02002666 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002667 case LY_STMT_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +02002668 LY_CHECK_RET(parse_any(ctx, in, kw, (struct lysp_node *)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002669 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002670 case LY_STMT_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02002671 LY_CHECK_RET(parse_choice(ctx, in, (struct lysp_node *)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002672 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002673 case LY_STMT_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02002674 LY_CHECK_RET(parse_container(ctx, in, (struct lysp_node *)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002675 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002676 case LY_STMT_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02002677 LY_CHECK_RET(parse_leaf(ctx, in, (struct lysp_node *)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002678 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002679 case LY_STMT_LEAF_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02002680 LY_CHECK_RET(parse_leaflist(ctx, in, (struct lysp_node *)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002681 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002682 case LY_STMT_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02002683 LY_CHECK_RET(parse_list(ctx, in, (struct lysp_node *)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002684 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002685 case LY_STMT_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +02002686 LY_CHECK_RET(parse_uses(ctx, in, (struct lysp_node *)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002687 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002688 case LY_STMT_TYPEDEF:
Michal Vasko22df3f02020-08-24 13:29:22 +02002689 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)inout_p, in, &inout_p->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002690 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002691 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002692 PARSER_CHECK_STMTVER2_RET(ctx, "must", ly_stmt2str(inout_kw));
Michal Vasko63f3d842020-07-08 10:10:14 +02002693 LY_CHECK_RET(parse_restrs(ctx, in, kw, &inout_p->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002694 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002695 case LY_STMT_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02002696 LY_CHECK_RET(parse_grouping(ctx, in, (struct lysp_node *)inout_p, &inout_p->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002697 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002698 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002699 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inout_p->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002700 break;
2701 default:
David Sedlákb3077192019-06-19 10:55:37 +02002702 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002703 return LY_EVALID;
2704 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002705 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002706 LY_CHECK_RET(ret);
Michal Vaskob83af8a2020-01-06 09:49:22 +01002707
Radek Krejci7fc68292019-06-12 13:51:09 +02002708checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002709 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01002710 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, inout_p->groupings, NULL, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02002711
Michal Vaskob83af8a2020-01-06 09:49:22 +01002712 if (!inout_p->data) {
2713 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "data-def-stmt", ly_stmt2str(inout_kw));
2714 return LY_EVALID;
2715 }
2716
Michal Vasko7fbc8162018-09-17 10:35:16 +02002717 return ret;
2718}
2719
Michal Vaskoea5abea2018-09-18 13:10:54 +02002720/**
2721 * @brief Parse the action statement.
2722 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002723 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002724 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002725 * @param[in,out] actions Actions to add to.
2726 *
2727 * @return LY_ERR values.
2728 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002729LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002730parse_action(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002731{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002732 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002733 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002734 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002735 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002736 struct lysp_action *act;
2737
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002738 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *actions, act, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002739
2740 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002741 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002742 INSERT_WORD_RET(ctx, buf, act->name, word, word_len);
Michal Vasko1bf09392020-03-27 12:38:10 +01002743 act->nodetype = parent ? LYS_ACTION : LYS_RPC;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002744 act->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002745
Michal Vasko63f3d842020-07-08 10:10:14 +02002746 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002747 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002748 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002749 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &act->dsc, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002750 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002751 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02002752 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &act->iffeatures, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002753 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002754 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002755 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &act->ref, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002756 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002757 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002758 LY_CHECK_RET(parse_status(ctx, in, &act->flags, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002759 break;
2760
Radek Krejcid6b76452019-09-03 17:03:03 +02002761 case LY_STMT_INPUT:
Michal Vasko22df3f02020-08-24 13:29:22 +02002762 LY_CHECK_RET(parse_inout(ctx, in, kw, (struct lysp_node *)act, &act->input));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002763 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002764 case LY_STMT_OUTPUT:
Michal Vasko22df3f02020-08-24 13:29:22 +02002765 LY_CHECK_RET(parse_inout(ctx, in, kw, (struct lysp_node *)act, &act->output));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002766 break;
2767
Radek Krejcid6b76452019-09-03 17:03:03 +02002768 case LY_STMT_TYPEDEF:
Michal Vasko22df3f02020-08-24 13:29:22 +02002769 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)act, in, &act->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002770 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002771 case LY_STMT_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02002772 LY_CHECK_RET(parse_grouping(ctx, in, (struct lysp_node *)act, &act->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002773 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002774 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002775 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002776 break;
2777 default:
David Sedlákb3077192019-06-19 10:55:37 +02002778 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), parent ? "action" : "rpc");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002779 return LY_EVALID;
2780 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002781 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002782 LY_CHECK_RET(ret);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002783
2784 /* always initialize inout, they are technically present (needed for later deviations/refines) */
2785 if (!act->input.nodetype) {
2786 act->input.nodetype = LYS_INPUT;
2787 act->input.parent = (struct lysp_node *)act;
2788 }
2789 if (!act->output.nodetype) {
2790 act->output.nodetype = LYS_OUTPUT;
2791 act->output.parent = (struct lysp_node *)act;
2792 }
2793
Radek Krejci7fc68292019-06-12 13:51:09 +02002794checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002795 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01002796 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, act->groupings, NULL, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02002797
Michal Vasko7fbc8162018-09-17 10:35:16 +02002798 return ret;
2799}
2800
Michal Vaskoea5abea2018-09-18 13:10:54 +02002801/**
2802 * @brief Parse the notification statement.
2803 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002804 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002805 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002806 * @param[in,out] notifs Notifications to add to.
2807 *
2808 * @return LY_ERR values.
2809 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002810LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002811parse_notif(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002812{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002813 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002814 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002815 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002816 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002817 struct lysp_notif *notif;
2818
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002819 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *notifs, notif, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002820
2821 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002822 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002823 INSERT_WORD_RET(ctx, buf, notif->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002824 notif->nodetype = LYS_NOTIF;
2825 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002826
Michal Vasko63f3d842020-07-08 10:10:14 +02002827 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002828 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002829 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002830 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &notif->dsc, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002831 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002832 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02002833 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &notif->iffeatures, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002834 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002835 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002836 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &notif->ref, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002837 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002838 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002839 LY_CHECK_RET(parse_status(ctx, in, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002840 break;
2841
Radek Krejcid6b76452019-09-03 17:03:03 +02002842 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002843 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "notification");
Radek Krejci0f969882020-08-21 16:56:47 +02002844 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002845 case LY_STMT_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +02002846 LY_CHECK_RET(parse_any(ctx, in, kw, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002847 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002848 case LY_STMT_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02002849 LY_CHECK_RET(parse_case(ctx, in, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002850 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002851 case LY_STMT_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02002852 LY_CHECK_RET(parse_container(ctx, in, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002853 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002854 case LY_STMT_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02002855 LY_CHECK_RET(parse_leaf(ctx, in, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002856 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002857 case LY_STMT_LEAF_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02002858 LY_CHECK_RET(parse_leaflist(ctx, in, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002859 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002860 case LY_STMT_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02002861 LY_CHECK_RET(parse_list(ctx, in, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002862 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002863 case LY_STMT_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +02002864 LY_CHECK_RET(parse_uses(ctx, in, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002865 break;
2866
Radek Krejcid6b76452019-09-03 17:03:03 +02002867 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002868 PARSER_CHECK_STMTVER2_RET(ctx, "must", "notification");
Michal Vasko63f3d842020-07-08 10:10:14 +02002869 LY_CHECK_RET(parse_restrs(ctx, in, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002870 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002871 case LY_STMT_TYPEDEF:
Michal Vasko22df3f02020-08-24 13:29:22 +02002872 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)notif, in, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002873 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002874 case LY_STMT_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02002875 LY_CHECK_RET(parse_grouping(ctx, in, (struct lysp_node *)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002876 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002877 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002878 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002879 break;
2880 default:
David Sedlákb3077192019-06-19 10:55:37 +02002881 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002882 return LY_EVALID;
2883 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002884 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002885 LY_CHECK_RET(ret);
2886checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002887 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01002888 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, notif->groupings, NULL, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02002889
Michal Vasko7fbc8162018-09-17 10:35:16 +02002890 return ret;
2891}
2892
Michal Vaskoea5abea2018-09-18 13:10:54 +02002893/**
2894 * @brief Parse the grouping statement.
2895 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002896 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002897 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002898 * @param[in,out] groupings Groupings to add to.
2899 *
2900 * @return LY_ERR values.
2901 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002902LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002903parse_grouping(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002904{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002905 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002906 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002907 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002908 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002909 struct lysp_grp *grp;
2910
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002911 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *groupings, grp, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002912
2913 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002914 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002915 INSERT_WORD_RET(ctx, buf, grp->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002916 grp->nodetype = LYS_GROUPING;
2917 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002918
Michal Vasko63f3d842020-07-08 10:10:14 +02002919 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002920 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002921 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002922 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &grp->dsc, Y_STR_ARG, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002923 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002924 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002925 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &grp->ref, Y_STR_ARG, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002926 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002927 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002928 LY_CHECK_RET(parse_status(ctx, in, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002929 break;
2930
Radek Krejcid6b76452019-09-03 17:03:03 +02002931 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002932 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "grouping");
Radek Krejci0f969882020-08-21 16:56:47 +02002933 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002934 case LY_STMT_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +02002935 LY_CHECK_RET(parse_any(ctx, in, kw, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002936 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002937 case LY_STMT_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02002938 LY_CHECK_RET(parse_choice(ctx, in, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002939 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002940 case LY_STMT_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02002941 LY_CHECK_RET(parse_container(ctx, in, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002942 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002943 case LY_STMT_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02002944 LY_CHECK_RET(parse_leaf(ctx, in, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002945 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002946 case LY_STMT_LEAF_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02002947 LY_CHECK_RET(parse_leaflist(ctx, in, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002948 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002949 case LY_STMT_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02002950 LY_CHECK_RET(parse_list(ctx, in, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002951 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002952 case LY_STMT_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +02002953 LY_CHECK_RET(parse_uses(ctx, in, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002954 break;
2955
Radek Krejcid6b76452019-09-03 17:03:03 +02002956 case LY_STMT_TYPEDEF:
Michal Vasko22df3f02020-08-24 13:29:22 +02002957 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)grp, in, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002958 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002959 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02002960 PARSER_CHECK_STMTVER2_RET(ctx, "action", "grouping");
Michal Vasko22df3f02020-08-24 13:29:22 +02002961 LY_CHECK_RET(parse_action(ctx, in, (struct lysp_node *)grp, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002962 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002963 case LY_STMT_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02002964 LY_CHECK_RET(parse_grouping(ctx, in, (struct lysp_node *)grp, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002965 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002966 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02002967 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "grouping");
Michal Vasko22df3f02020-08-24 13:29:22 +02002968 LY_CHECK_RET(parse_notif(ctx, in, (struct lysp_node *)grp, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002969 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002970 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002971 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002972 break;
2973 default:
David Sedlákb3077192019-06-19 10:55:37 +02002974 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002975 return LY_EVALID;
2976 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002977 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002978 LY_CHECK_RET(ret);
2979checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002980 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01002981 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, grp->groupings, NULL, grp->actions, grp->notifs));
Radek Krejci7fc68292019-06-12 13:51:09 +02002982
Michal Vasko7fbc8162018-09-17 10:35:16 +02002983 return ret;
2984}
2985
Michal Vaskoea5abea2018-09-18 13:10:54 +02002986/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02002987 * @brief Parse the augment statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002988 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002989 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002990 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002991 * @param[in,out] augments Augments to add to.
2992 *
2993 * @return LY_ERR values.
2994 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002995LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002996parse_augment(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002997{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002998 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002999 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003000 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003001 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003002 struct lysp_augment *aug;
3003
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003004 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *augments, aug, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003005
3006 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02003007 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01003008 CHECK_NONEMPTY(ctx, word_len, "augment");
Radek Krejci011e4aa2020-09-04 15:22:31 +02003009 INSERT_WORD_RET(ctx, buf, aug->nodeid, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003010 aug->nodetype = LYS_AUGMENT;
3011 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003012
Michal Vasko63f3d842020-07-08 10:10:14 +02003013 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003014 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003015 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003016 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &aug->dsc, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003017 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003018 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003019 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &aug->iffeatures, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003020 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003021 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003022 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &aug->ref, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003023 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003024 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003025 LY_CHECK_RET(parse_status(ctx, in, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003026 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003027 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02003028 LY_CHECK_RET(parse_when(ctx, in, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003029 break;
3030
Radek Krejcid6b76452019-09-03 17:03:03 +02003031 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003032 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "augment");
Radek Krejci0f969882020-08-21 16:56:47 +02003033 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003034 case LY_STMT_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +02003035 LY_CHECK_RET(parse_any(ctx, in, kw, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003036 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003037 case LY_STMT_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02003038 LY_CHECK_RET(parse_case(ctx, in, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003039 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003040 case LY_STMT_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02003041 LY_CHECK_RET(parse_choice(ctx, in, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003042 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003043 case LY_STMT_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02003044 LY_CHECK_RET(parse_container(ctx, in, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003045 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003046 case LY_STMT_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02003047 LY_CHECK_RET(parse_leaf(ctx, in, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003048 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003049 case LY_STMT_LEAF_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003050 LY_CHECK_RET(parse_leaflist(ctx, in, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003051 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003052 case LY_STMT_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003053 LY_CHECK_RET(parse_list(ctx, in, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003054 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003055 case LY_STMT_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +02003056 LY_CHECK_RET(parse_uses(ctx, in, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003057 break;
3058
Radek Krejcid6b76452019-09-03 17:03:03 +02003059 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003060 PARSER_CHECK_STMTVER2_RET(ctx, "action", "augment");
Michal Vasko22df3f02020-08-24 13:29:22 +02003061 LY_CHECK_RET(parse_action(ctx, in, (struct lysp_node *)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003062 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003063 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003064 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "augment");
Michal Vasko22df3f02020-08-24 13:29:22 +02003065 LY_CHECK_RET(parse_notif(ctx, in, (struct lysp_node *)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003066 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003067 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003068 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003069 break;
3070 default:
David Sedlákb3077192019-06-19 10:55:37 +02003071 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003072 return LY_EVALID;
3073 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003074 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003075 LY_CHECK_RET(ret);
3076checks:
3077 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01003078 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, NULL, NULL, aug->actions, aug->notifs));
Radek Krejci7fc68292019-06-12 13:51:09 +02003079
Michal Vasko7fbc8162018-09-17 10:35:16 +02003080 return ret;
3081}
3082
Michal Vaskoea5abea2018-09-18 13:10:54 +02003083/**
3084 * @brief Parse the uses statement.
3085 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003086 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003087 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003088 * @param[in,out] siblings Siblings to add to.
3089 *
3090 * @return LY_ERR values.
3091 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003092LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003093parse_uses(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003094{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003095 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003096 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003097 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003098 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003099 struct lysp_node_uses *uses;
3100
David Sedlák60adc092019-08-06 15:57:02 +02003101 /* create uses structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003102 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, uses, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003103 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003104 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003105
Michal Vasko7fbc8162018-09-17 10:35:16 +02003106 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02003107 LY_CHECK_RET(get_argument(ctx, in, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003108 INSERT_WORD_RET(ctx, buf, uses->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003109
3110 /* parse substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02003111 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003112 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003113 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003114 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &uses->dsc, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003115 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003116 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003117 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &uses->iffeatures, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003118 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003119 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003120 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &uses->ref, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003121 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003122 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003123 LY_CHECK_RET(parse_status(ctx, in, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003124 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003125 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02003126 LY_CHECK_RET(parse_when(ctx, in, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003127 break;
3128
Radek Krejcid6b76452019-09-03 17:03:03 +02003129 case LY_STMT_REFINE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003130 LY_CHECK_RET(parse_refine(ctx, in, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003131 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003132 case LY_STMT_AUGMENT:
Michal Vasko22df3f02020-08-24 13:29:22 +02003133 LY_CHECK_RET(parse_augment(ctx, in, (struct lysp_node *)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003134 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003135 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003136 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003137 break;
3138 default:
David Sedlákb3077192019-06-19 10:55:37 +02003139 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003140 return LY_EVALID;
3141 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003142 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003143checks:
3144 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01003145 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, NULL, uses->augments, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02003146
Michal Vasko7fbc8162018-09-17 10:35:16 +02003147 return ret;
3148}
3149
Michal Vaskoea5abea2018-09-18 13:10:54 +02003150/**
3151 * @brief Parse the case statement.
3152 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003153 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003154 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003155 * @param[in,out] siblings Siblings to add to.
3156 *
3157 * @return LY_ERR values.
3158 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003159LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003160parse_case(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003161{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003162 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003163 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003164 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003165 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003166 struct lysp_node_case *cas;
3167
David Sedlák60adc092019-08-06 15:57:02 +02003168 /* create new case structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003169 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cas, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003170 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003171 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003172
Michal Vasko7fbc8162018-09-17 10:35:16 +02003173 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02003174 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003175 INSERT_WORD_RET(ctx, buf, cas->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003176
3177 /* parse substatements */
Michal Vaskod989ba02020-08-24 10:59:24 +02003178 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003179 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003180 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003181 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &cas->dsc, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003182 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003183 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003184 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &cas->iffeatures, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003185 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003186 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003187 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &cas->ref, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003188 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003189 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003190 LY_CHECK_RET(parse_status(ctx, in, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003191 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003192 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02003193 LY_CHECK_RET(parse_when(ctx, in, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003194 break;
3195
Radek Krejcid6b76452019-09-03 17:03:03 +02003196 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003197 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "case");
Radek Krejci0f969882020-08-21 16:56:47 +02003198 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003199 case LY_STMT_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +02003200 LY_CHECK_RET(parse_any(ctx, in, kw, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003201 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003202 case LY_STMT_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02003203 LY_CHECK_RET(parse_choice(ctx, in, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003204 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003205 case LY_STMT_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02003206 LY_CHECK_RET(parse_container(ctx, in, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003207 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003208 case LY_STMT_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02003209 LY_CHECK_RET(parse_leaf(ctx, in, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003210 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003211 case LY_STMT_LEAF_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003212 LY_CHECK_RET(parse_leaflist(ctx, in, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003213 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003214 case LY_STMT_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003215 LY_CHECK_RET(parse_list(ctx, in, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003216 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003217 case LY_STMT_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +02003218 LY_CHECK_RET(parse_uses(ctx, in, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003219 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003220 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003221 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003222 break;
3223 default:
David Sedlákb3077192019-06-19 10:55:37 +02003224 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003225 return LY_EVALID;
3226 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003227 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003228 return ret;
3229}
3230
Michal Vaskoea5abea2018-09-18 13:10:54 +02003231/**
3232 * @brief Parse the choice statement.
3233 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003234 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003235 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003236 * @param[in,out] siblings Siblings to add to.
3237 *
3238 * @return LY_ERR values.
3239 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003240LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003241parse_choice(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003242{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003243 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003244 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003245 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003246 enum ly_stmt kw;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003247 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003248
David Sedlák60adc092019-08-06 15:57:02 +02003249 /* create new choice structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003250 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, choice, next, LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003251 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003252 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003253
Michal Vasko7fbc8162018-09-17 10:35:16 +02003254 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02003255 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003256 INSERT_WORD_RET(ctx, buf, choice->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003257
3258 /* parse substatements */
Michal Vasko7f45cf22020-10-01 12:49:44 +02003259 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003260 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003261 case LY_STMT_CONFIG:
Michal Vasko63f3d842020-07-08 10:10:14 +02003262 LY_CHECK_RET(parse_config(ctx, in, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003263 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003264 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003265 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &choice->dsc, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003266 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003267 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003268 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &choice->iffeatures, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003269 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003270 case LY_STMT_MANDATORY:
Michal Vasko63f3d842020-07-08 10:10:14 +02003271 LY_CHECK_RET(parse_mandatory(ctx, in, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003272 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003273 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003274 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &choice->ref, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003275 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003276 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003277 LY_CHECK_RET(parse_status(ctx, in, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003278 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003279 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02003280 LY_CHECK_RET(parse_when(ctx, in, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003281 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003282 case LY_STMT_DEFAULT:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003283 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DEFAULT, 0, &choice->dflt.str, Y_PREF_IDENTIF_ARG,
3284 &choice->exts));
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003285 choice->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003286 break;
3287
Radek Krejcid6b76452019-09-03 17:03:03 +02003288 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003289 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "choice");
Radek Krejci0f969882020-08-21 16:56:47 +02003290 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003291 case LY_STMT_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +02003292 LY_CHECK_RET(parse_any(ctx, in, kw, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003293 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003294 case LY_STMT_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02003295 LY_CHECK_RET(parse_case(ctx, in, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003296 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003297 case LY_STMT_CHOICE:
Radek Krejci335332a2019-09-05 13:03:35 +02003298 PARSER_CHECK_STMTVER2_RET(ctx, "choice", "choice");
Michal Vasko22df3f02020-08-24 13:29:22 +02003299 LY_CHECK_RET(parse_choice(ctx, in, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003300 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003301 case LY_STMT_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02003302 LY_CHECK_RET(parse_container(ctx, in, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003303 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003304 case LY_STMT_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02003305 LY_CHECK_RET(parse_leaf(ctx, in, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003306 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003307 case LY_STMT_LEAF_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003308 LY_CHECK_RET(parse_leaflist(ctx, in, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003309 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003310 case LY_STMT_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003311 LY_CHECK_RET(parse_list(ctx, in, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003312 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003313 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003314 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003315 break;
3316 default:
David Sedlákb3077192019-06-19 10:55:37 +02003317 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003318 return LY_EVALID;
3319 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003320 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003321 return ret;
3322}
3323
Michal Vaskoea5abea2018-09-18 13:10:54 +02003324/**
3325 * @brief Parse the container statement.
3326 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003327 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003328 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003329 * @param[in,out] siblings Siblings to add to.
3330 *
3331 * @return LY_ERR values.
3332 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003333LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003334parse_container(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003335{
3336 LY_ERR ret = 0;
3337 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003338 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003339 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003340 struct lysp_node_container *cont;
3341
David Sedlák60adc092019-08-06 15:57:02 +02003342 /* create new container structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003343 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cont, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003344 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003345 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003346
Michal Vasko7fbc8162018-09-17 10:35:16 +02003347 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02003348 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003349 INSERT_WORD_RET(ctx, buf, cont->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003350
3351 /* parse substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02003352 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003353 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003354 case LY_STMT_CONFIG:
Michal Vasko63f3d842020-07-08 10:10:14 +02003355 LY_CHECK_RET(parse_config(ctx, in, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003356 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003357 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003358 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &cont->dsc, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003359 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003360 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003361 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &cont->iffeatures, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003362 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003363 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003364 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &cont->ref, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003365 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003366 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003367 LY_CHECK_RET(parse_status(ctx, in, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003368 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003369 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02003370 LY_CHECK_RET(parse_when(ctx, in, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003371 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003372 case LY_STMT_PRESENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003373 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_PRESENCE, 0, &cont->presence, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003374 break;
3375
Radek Krejcid6b76452019-09-03 17:03:03 +02003376 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003377 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "container");
Radek Krejci0f969882020-08-21 16:56:47 +02003378 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003379 case LY_STMT_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +02003380 LY_CHECK_RET(parse_any(ctx, in, kw, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003381 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003382 case LY_STMT_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02003383 LY_CHECK_RET(parse_choice(ctx, in, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003384 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003385 case LY_STMT_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02003386 LY_CHECK_RET(parse_container(ctx, in, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003387 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003388 case LY_STMT_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02003389 LY_CHECK_RET(parse_leaf(ctx, in, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003390 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003391 case LY_STMT_LEAF_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003392 LY_CHECK_RET(parse_leaflist(ctx, in, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003393 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003394 case LY_STMT_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003395 LY_CHECK_RET(parse_list(ctx, in, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003396 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003397 case LY_STMT_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +02003398 LY_CHECK_RET(parse_uses(ctx, in, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003399 break;
3400
Radek Krejcid6b76452019-09-03 17:03:03 +02003401 case LY_STMT_TYPEDEF:
Michal Vasko22df3f02020-08-24 13:29:22 +02003402 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)cont, in, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003403 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003404 case LY_STMT_MUST:
Michal Vasko63f3d842020-07-08 10:10:14 +02003405 LY_CHECK_RET(parse_restrs(ctx, in, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003406 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003407 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003408 PARSER_CHECK_STMTVER2_RET(ctx, "action", "container");
Michal Vasko22df3f02020-08-24 13:29:22 +02003409 LY_CHECK_RET(parse_action(ctx, in, (struct lysp_node *)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003410 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003411 case LY_STMT_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02003412 LY_CHECK_RET(parse_grouping(ctx, in, (struct lysp_node *)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003413 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003414 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003415 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "container");
Michal Vasko22df3f02020-08-24 13:29:22 +02003416 LY_CHECK_RET(parse_notif(ctx, in, (struct lysp_node *)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003417 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003418 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003419 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003420 break;
3421 default:
David Sedlákb3077192019-06-19 10:55:37 +02003422 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003423 return LY_EVALID;
3424 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003425 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003426checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01003427 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01003428 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, cont->groupings, NULL, cont->actions, cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003429 return ret;
3430}
3431
Michal Vaskoea5abea2018-09-18 13:10:54 +02003432/**
3433 * @brief Parse the list statement.
3434 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003435 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003436 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003437 * @param[in,out] siblings Siblings to add to.
3438 *
3439 * @return LY_ERR values.
3440 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003441LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003442parse_list(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003443{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003444 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003445 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003446 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003447 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003448 struct lysp_node_list *list;
3449
David Sedlák60adc092019-08-06 15:57:02 +02003450 /* create new list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003451 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, list, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003452 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003453 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003454
Michal Vasko7fbc8162018-09-17 10:35:16 +02003455 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02003456 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003457 INSERT_WORD_RET(ctx, buf, list->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003458
3459 /* parse substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02003460 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003461 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003462 case LY_STMT_CONFIG:
Michal Vasko63f3d842020-07-08 10:10:14 +02003463 LY_CHECK_RET(parse_config(ctx, in, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003464 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003465 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003466 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &list->dsc, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003467 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003468 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003469 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &list->iffeatures, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003470 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003471 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003472 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &list->ref, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003473 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003474 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003475 LY_CHECK_RET(parse_status(ctx, in, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003476 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003477 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02003478 LY_CHECK_RET(parse_when(ctx, in, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003479 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003480 case LY_STMT_KEY:
Michal Vasko63f3d842020-07-08 10:10:14 +02003481 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_KEY, 0, &list->key, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003482 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003483 case LY_STMT_MAX_ELEMENTS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003484 LY_CHECK_RET(parse_maxelements(ctx, in, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003485 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003486 case LY_STMT_MIN_ELEMENTS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003487 LY_CHECK_RET(parse_minelements(ctx, in, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003488 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003489 case LY_STMT_ORDERED_BY:
Michal Vasko63f3d842020-07-08 10:10:14 +02003490 LY_CHECK_RET(parse_orderedby(ctx, in, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003491 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003492 case LY_STMT_UNIQUE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003493 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_UNIQUE, &list->uniques, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003494 break;
3495
Radek Krejcid6b76452019-09-03 17:03:03 +02003496 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003497 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "list");
Radek Krejci0f969882020-08-21 16:56:47 +02003498 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003499 case LY_STMT_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +02003500 LY_CHECK_RET(parse_any(ctx, in, kw, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003501 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003502 case LY_STMT_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02003503 LY_CHECK_RET(parse_choice(ctx, in, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003504 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003505 case LY_STMT_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02003506 LY_CHECK_RET(parse_container(ctx, in, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003507 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003508 case LY_STMT_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02003509 LY_CHECK_RET(parse_leaf(ctx, in, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003510 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003511 case LY_STMT_LEAF_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003512 LY_CHECK_RET(parse_leaflist(ctx, in, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003513 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003514 case LY_STMT_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003515 LY_CHECK_RET(parse_list(ctx, in, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003516 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003517 case LY_STMT_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +02003518 LY_CHECK_RET(parse_uses(ctx, in, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003519 break;
3520
Radek Krejcid6b76452019-09-03 17:03:03 +02003521 case LY_STMT_TYPEDEF:
Michal Vasko22df3f02020-08-24 13:29:22 +02003522 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)list, in, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003523 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003524 case LY_STMT_MUST:
Michal Vasko63f3d842020-07-08 10:10:14 +02003525 LY_CHECK_RET(parse_restrs(ctx, in, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003526 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003527 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003528 PARSER_CHECK_STMTVER2_RET(ctx, "action", "list");
Michal Vasko22df3f02020-08-24 13:29:22 +02003529 LY_CHECK_RET(parse_action(ctx, in, (struct lysp_node *)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003530 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003531 case LY_STMT_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02003532 LY_CHECK_RET(parse_grouping(ctx, in, (struct lysp_node *)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003533 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003534 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003535 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "list");
Michal Vasko22df3f02020-08-24 13:29:22 +02003536 LY_CHECK_RET(parse_notif(ctx, in, (struct lysp_node *)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003537 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003538 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003539 LY_CHECK_RET(parse_ext(ctx, in, 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 */
Michal Vaskob36053d2020-03-26 15:49:30 +01003549 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, list->groupings, NULL, list->actions, list->notifs));
Radek Krejci7fc68292019-06-12 13:51:09 +02003550
Michal Vasko7fbc8162018-09-17 10:35:16 +02003551 return ret;
3552}
3553
Michal Vaskoea5abea2018-09-18 13:10:54 +02003554/**
3555 * @brief Parse the yin-element statement.
3556 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003557 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003558 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003559 * @param[in,out] flags Flags to write to.
3560 * @param[in,out] exts Extension instances to add to.
3561 *
3562 * @return LY_ERR values.
3563 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003564static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003565parse_yinelement(struct lys_yang_parser_ctx *ctx, struct ly_in *in, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003566{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003567 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003568 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003569 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003570 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003571
3572 if (*flags & LYS_YINELEM_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02003573 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003574 return LY_EVALID;
3575 }
3576
3577 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02003578 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003579
3580 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
3581 *flags |= LYS_YINELEM_TRUE;
3582 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
3583 *flags |= LYS_YINELEM_FALSE;
3584 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003585 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003586 free(buf);
3587 return LY_EVALID;
3588 }
3589 free(buf);
3590
Michal Vaskod989ba02020-08-24 10:59:24 +02003591 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003592 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003593 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003594 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_YINELEM, 0, exts));
Michal Vaskod989ba02020-08-24 10:59:24 +02003595 LY_CHECK_RET(ret);
3596 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003597 default:
David Sedlákb3077192019-06-19 10:55:37 +02003598 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003599 return LY_EVALID;
3600 }
3601 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003602 return ret;
3603}
3604
Michal Vaskoea5abea2018-09-18 13:10:54 +02003605/**
David Sedlák2444f8f2019-07-09 11:02:47 +02003606 * @brief Parse the argument statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003607 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003608 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003609 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003610 * @param[in,out] argument Value to write to.
3611 * @param[in,out] flags Flags to write to.
3612 * @param[in,out] exts Extension instances to add to.
3613 *
3614 * @return LY_ERR values.
3615 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003616static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003617parse_argument(struct lys_yang_parser_ctx *ctx, struct ly_in *in, const char **argument, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003618{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003619 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003620 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003621 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003622 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003623
3624 if (*argument) {
David Sedlákb3077192019-06-19 10:55:37 +02003625 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003626 return LY_EVALID;
3627 }
3628
3629 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02003630 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003631 INSERT_WORD_RET(ctx, buf, *argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003632
Michal Vaskod989ba02020-08-24 10:59:24 +02003633 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003634 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003635 case LY_STMT_YIN_ELEMENT:
Michal Vasko63f3d842020-07-08 10:10:14 +02003636 LY_CHECK_RET(parse_yinelement(ctx, in, flags, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003637 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003638 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003639 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_ARGUMENT, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003640 break;
3641 default:
David Sedlákb3077192019-06-19 10:55:37 +02003642 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003643 return LY_EVALID;
3644 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003645 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003646 return ret;
3647}
3648
Michal Vaskoea5abea2018-09-18 13:10:54 +02003649/**
3650 * @brief Parse the extension statement.
3651 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003652 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003653 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003654 * @param[in,out] extensions Extensions to add to.
3655 *
3656 * @return LY_ERR values.
3657 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003658static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003659parse_extension(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003660{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003661 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003662 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003663 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003664 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003665 struct lysp_ext *ex;
3666
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003667 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003668
3669 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02003670 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003671 INSERT_WORD_RET(ctx, buf, ex->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003672
Michal Vaskod989ba02020-08-24 10:59:24 +02003673 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003674 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003675 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003676 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &ex->dsc, Y_STR_ARG, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003677 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003678 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003679 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &ex->ref, Y_STR_ARG, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003680 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003681 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003682 LY_CHECK_RET(parse_status(ctx, in, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003683 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003684 case LY_STMT_ARGUMENT:
Michal Vasko63f3d842020-07-08 10:10:14 +02003685 LY_CHECK_RET(parse_argument(ctx, in, &ex->argument, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003686 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003687 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003688 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003689 break;
3690 default:
David Sedlákb3077192019-06-19 10:55:37 +02003691 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003692 return LY_EVALID;
3693 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003694 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003695 return ret;
3696}
3697
Michal Vaskoea5abea2018-09-18 13:10:54 +02003698/**
3699 * @brief Parse the deviate statement.
3700 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003701 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003702 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003703 * @param[in,out] deviates Deviates to add to.
3704 *
3705 * @return LY_ERR values.
3706 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003707LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003708parse_deviate(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003709{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003710 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003711 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003712 size_t word_len, dev_mod;
Radek Krejcid6b76452019-09-03 17:03:03 +02003713 enum ly_stmt kw;
David Sedlák60adc092019-08-06 15:57:02 +02003714 struct lysp_deviate *d;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003715 struct lysp_deviate_add *d_add = NULL;
3716 struct lysp_deviate_rpl *d_rpl = NULL;
3717 struct lysp_deviate_del *d_del = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003718 const char **d_units = NULL;
3719 struct lysp_qname **d_uniques = NULL, **d_dflts = NULL;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02003720 struct lysp_restr **d_musts = NULL;
3721 uint16_t *d_flags = 0;
3722 uint32_t *d_min = 0, *d_max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003723
3724 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02003725 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003726
3727 if ((word_len == 13) && !strncmp(word, "not-supported", word_len)) {
3728 dev_mod = LYS_DEV_NOT_SUPPORTED;
3729 } else if ((word_len == 3) && !strncmp(word, "add", word_len)) {
3730 dev_mod = LYS_DEV_ADD;
3731 } else if ((word_len == 7) && !strncmp(word, "replace", word_len)) {
3732 dev_mod = LYS_DEV_REPLACE;
3733 } else if ((word_len == 6) && !strncmp(word, "delete", word_len)) {
3734 dev_mod = LYS_DEV_DELETE;
3735 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003736 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003737 free(buf);
3738 return LY_EVALID;
3739 }
3740 free(buf);
3741
3742 /* create structure */
3743 switch (dev_mod) {
3744 case LYS_DEV_NOT_SUPPORTED:
3745 d = calloc(1, sizeof *d);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003746 LY_CHECK_ERR_RET(!d, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003747 break;
3748 case LYS_DEV_ADD:
3749 d_add = calloc(1, sizeof *d_add);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003750 LY_CHECK_ERR_RET(!d_add, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003751 d = (struct lysp_deviate *)d_add;
3752 d_units = &d_add->units;
3753 d_uniques = &d_add->uniques;
3754 d_dflts = &d_add->dflts;
3755 d_musts = &d_add->musts;
3756 d_flags = &d_add->flags;
3757 d_min = &d_add->min;
3758 d_max = &d_add->max;
3759 break;
3760 case LYS_DEV_REPLACE:
3761 d_rpl = calloc(1, sizeof *d_rpl);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003762 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003763 d = (struct lysp_deviate *)d_rpl;
3764 d_units = &d_rpl->units;
3765 d_flags = &d_rpl->flags;
3766 d_min = &d_rpl->min;
3767 d_max = &d_rpl->max;
3768 break;
3769 case LYS_DEV_DELETE:
3770 d_del = calloc(1, sizeof *d_del);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003771 LY_CHECK_ERR_RET(!d_del, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003772 d = (struct lysp_deviate *)d_del;
3773 d_units = &d_del->units;
3774 d_uniques = &d_del->uniques;
3775 d_dflts = &d_del->dflts;
3776 d_musts = &d_del->musts;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003777 break;
3778 default:
3779 assert(0);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003780 LOGINT_RET(PARSER_CTX(ctx));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003781 }
3782 d->mod = dev_mod;
3783
3784 /* insert into siblings */
David Sedlák60adc092019-08-06 15:57:02 +02003785 LY_LIST_INSERT(deviates, d, next);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003786
Michal Vaskod989ba02020-08-24 10:59:24 +02003787 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003788 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003789 case LY_STMT_CONFIG:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003790 switch (dev_mod) {
3791 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003792 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003793 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003794 return LY_EVALID;
3795 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003796 LY_CHECK_RET(parse_config(ctx, in, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003797 break;
3798 }
3799 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003800 case LY_STMT_DEFAULT:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003801 switch (dev_mod) {
3802 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003803 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003804 return LY_EVALID;
3805 case LYS_DEV_REPLACE:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003806 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DEFAULT, 0, &d_rpl->dflt.str, Y_STR_ARG, &d->exts));
3807 d_rpl->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003808 break;
3809 default:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003810 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_DEFAULT, d_dflts, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003811 break;
3812 }
3813 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003814 case LY_STMT_MANDATORY:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003815 switch (dev_mod) {
3816 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003817 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003818 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003819 return LY_EVALID;
3820 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003821 LY_CHECK_RET(parse_mandatory(ctx, in, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003822 break;
3823 }
3824 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003825 case LY_STMT_MAX_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003826 switch (dev_mod) {
3827 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003828 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003829 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003830 return LY_EVALID;
3831 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003832 LY_CHECK_RET(parse_maxelements(ctx, in, d_max, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003833 break;
3834 }
3835 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003836 case LY_STMT_MIN_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003837 switch (dev_mod) {
3838 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003839 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003840 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003841 return LY_EVALID;
3842 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003843 LY_CHECK_RET(parse_minelements(ctx, in, d_min, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003844 break;
3845 }
3846 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003847 case LY_STMT_MUST:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003848 switch (dev_mod) {
3849 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003850 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003851 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003852 return LY_EVALID;
3853 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003854 LY_CHECK_RET(parse_restrs(ctx, in, kw, d_musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003855 break;
3856 }
3857 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003858 case LY_STMT_TYPE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003859 switch (dev_mod) {
3860 case LYS_DEV_NOT_SUPPORTED:
3861 case LYS_DEV_ADD:
3862 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003863 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003864 return LY_EVALID;
3865 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003866 if (d_rpl->type) {
David Sedlákb3077192019-06-19 10:55:37 +02003867 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003868 return LY_EVALID;
3869 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003870 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003871 LY_CHECK_ERR_RET(!d_rpl->type, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko63f3d842020-07-08 10:10:14 +02003872 LY_CHECK_RET(parse_type(ctx, in, d_rpl->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003873 break;
3874 }
3875 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003876 case LY_STMT_UNIQUE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003877 switch (dev_mod) {
3878 case LYS_DEV_NOT_SUPPORTED:
3879 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003880 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003881 return LY_EVALID;
3882 default:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003883 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_UNIQUE, d_uniques, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003884 break;
3885 }
3886 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003887 case LY_STMT_UNITS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003888 switch (dev_mod) {
3889 case LYS_DEV_NOT_SUPPORTED:
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:
Michal Vasko63f3d842020-07-08 10:10:14 +02003893 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_UNITS, 0, d_units, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003894 break;
3895 }
3896 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003897 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003898 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003899 break;
3900 default:
David Sedlákb3077192019-06-19 10:55:37 +02003901 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003902 return LY_EVALID;
3903 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003904 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003905 return ret;
3906}
3907
Michal Vaskoea5abea2018-09-18 13:10:54 +02003908/**
3909 * @brief Parse the deviation statement.
3910 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003911 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003912 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003913 * @param[in,out] deviations Deviations to add to.
3914 *
3915 * @return LY_ERR values.
3916 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003917LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003918parse_deviation(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003919{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003920 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003921 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003922 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003923 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003924 struct lysp_deviation *dev;
3925
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003926 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003927
3928 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02003929 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01003930 CHECK_NONEMPTY(ctx, word_len, "deviation");
Radek Krejci011e4aa2020-09-04 15:22:31 +02003931 INSERT_WORD_RET(ctx, buf, dev->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003932
Michal Vasko63f3d842020-07-08 10:10:14 +02003933 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003934 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003935 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003936 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &dev->dsc, Y_STR_ARG, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003937 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003938 case LY_STMT_DEVIATE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003939 LY_CHECK_RET(parse_deviate(ctx, in, &dev->deviates));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003940 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003941 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003942 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &dev->ref, Y_STR_ARG, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003943 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003944 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003945 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003946 break;
3947 default:
David Sedlákb3077192019-06-19 10:55:37 +02003948 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003949 return LY_EVALID;
3950 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003951 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003952 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01003953checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003954 /* mandatory substatements */
3955 if (!dev->deviates) {
David Sedlákb3077192019-06-19 10:55:37 +02003956 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003957 return LY_EVALID;
3958 }
3959
3960 return ret;
3961}
3962
Michal Vaskoea5abea2018-09-18 13:10:54 +02003963/**
3964 * @brief Parse the feature statement.
3965 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003966 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003967 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003968 * @param[in,out] features Features to add to.
3969 *
3970 * @return LY_ERR values.
3971 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003972LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003973parse_feature(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003974{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003975 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003976 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003977 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003978 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003979 struct lysp_feature *feat;
3980
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003981 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003982
3983 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02003984 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003985 INSERT_WORD_RET(ctx, buf, feat->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01003986
Michal Vaskod989ba02020-08-24 10:59:24 +02003987 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003988 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003989 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003990 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &feat->dsc, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003991 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003992 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003993 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &feat->iffeatures, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003994 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003995 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003996 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &feat->ref, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003997 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003998 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003999 LY_CHECK_RET(parse_status(ctx, in, &feat->flags, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004000 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004001 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004002 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004003 break;
4004 default:
David Sedlákb3077192019-06-19 10:55:37 +02004005 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004006 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004007 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004008 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004009 return ret;
4010}
4011
Michal Vaskoea5abea2018-09-18 13:10:54 +02004012/**
4013 * @brief Parse the identity statement.
4014 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004015 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02004016 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004017 * @param[in,out] identities Identities to add to.
4018 *
4019 * @return LY_ERR values.
4020 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004021LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02004022parse_identity(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004023{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004024 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004025 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004026 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004027 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004028 struct lysp_ident *ident;
4029
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004030 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004031
4032 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02004033 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004034 INSERT_WORD_RET(ctx, buf, ident->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004035
Michal Vaskod989ba02020-08-24 10:59:24 +02004036 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004037 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004038 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004039 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &ident->dsc, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004040 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004041 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02004042 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "identity");
Michal Vasko7f45cf22020-10-01 12:49:44 +02004043 LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &ident->iffeatures, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004044 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004045 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004046 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &ident->ref, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004047 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004048 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02004049 LY_CHECK_RET(parse_status(ctx, in, &ident->flags, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004050 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004051 case LY_STMT_BASE:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004052 if (ident->bases && (ctx->parsed_mod->version < LYS_VERSION_1_1)) {
David Sedlákb3077192019-06-19 10:55:37 +02004053 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 +01004054 return LY_EVALID;
4055 }
Michal Vasko63f3d842020-07-08 10:10:14 +02004056 LY_CHECK_RET(parse_text_fields(ctx, in, LYEXT_SUBSTMT_BASE, &ident->bases, Y_PREF_IDENTIF_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004057 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004058 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004059 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004060 break;
4061 default:
David Sedlákb3077192019-06-19 10:55:37 +02004062 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004063 return LY_EVALID;
4064 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004065 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004066 return ret;
4067}
4068
Michal Vaskoea5abea2018-09-18 13:10:54 +02004069/**
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004070 * @brief Parse module substatements.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004071 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004072 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02004073 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004074 * @param[in,out] mod Module to write to.
4075 *
4076 * @return LY_ERR values.
4077 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004078LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02004079parse_module(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004080{
4081 LY_ERR ret = 0;
4082 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004083 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004084 enum ly_stmt kw, prev_kw = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004085 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004086 struct lysp_submodule *dup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004087
Michal Vaskoc3781c32020-10-06 14:04:08 +02004088 mod->is_submod = 0;
4089
4090 /* module name */
Michal Vasko63f3d842020-07-08 10:10:14 +02004091 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004092 INSERT_WORD_RET(ctx, buf, mod->mod->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004093
Michal Vasko63f3d842020-07-08 10:10:14 +02004094 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004095
Radek Krejcie3846472018-10-15 15:24:51 +02004096#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004097 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 +02004098
Michal Vasko7fbc8162018-09-17 10:35:16 +02004099 switch (kw) {
4100 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004101 case LY_STMT_NAMESPACE:
4102 case LY_STMT_PREFIX:
Radek Krejcie3846472018-10-15 15:24:51 +02004103 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4104 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004105 case LY_STMT_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004106 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004107 break;
4108 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004109 case LY_STMT_INCLUDE:
4110 case LY_STMT_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004111 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004112 break;
4113 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004114 case LY_STMT_ORGANIZATION:
4115 case LY_STMT_CONTACT:
4116 case LY_STMT_DESCRIPTION:
4117 case LY_STMT_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004118 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004119 break;
4120
4121 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004122 case LY_STMT_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004123 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004124 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004125 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004126 case LY_STMT_ANYDATA:
4127 case LY_STMT_ANYXML:
4128 case LY_STMT_AUGMENT:
4129 case LY_STMT_CHOICE:
4130 case LY_STMT_CONTAINER:
4131 case LY_STMT_DEVIATION:
4132 case LY_STMT_EXTENSION:
4133 case LY_STMT_FEATURE:
4134 case LY_STMT_GROUPING:
4135 case LY_STMT_IDENTITY:
4136 case LY_STMT_LEAF:
4137 case LY_STMT_LEAF_LIST:
4138 case LY_STMT_LIST:
4139 case LY_STMT_NOTIFICATION:
4140 case LY_STMT_RPC:
4141 case LY_STMT_TYPEDEF:
4142 case LY_STMT_USES:
4143 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004144 mod_stmt = Y_MOD_BODY;
4145 break;
4146 default:
4147 /* error handled in the next switch */
4148 break;
4149 }
Radek Krejcie3846472018-10-15 15:24:51 +02004150#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004151
Radek Krejcie3846472018-10-15 15:24:51 +02004152 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004153 switch (kw) {
4154 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004155 case LY_STMT_YANG_VERSION:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004156 LY_CHECK_RET(parse_yangversion(ctx, in, &mod->version, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004157 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004158 case LY_STMT_NAMESPACE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004159 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_NAMESPACE, 0, &mod->mod->ns, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004160 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004161 case LY_STMT_PREFIX:
Michal Vasko63f3d842020-07-08 10:10:14 +02004162 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_PREFIX, 0, &mod->mod->prefix, Y_IDENTIF_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004163 break;
4164
4165 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004166 case LY_STMT_INCLUDE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004167 LY_CHECK_RET(parse_include(ctx, mod->mod->name, in, &mod->includes));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004168 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004169 case LY_STMT_IMPORT:
Michal Vasko63f3d842020-07-08 10:10:14 +02004170 LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, in, &mod->imports));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004171 break;
4172
4173 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004174 case LY_STMT_ORGANIZATION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004175 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_ORGANIZATION, 0, &mod->mod->org, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004176 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004177 case LY_STMT_CONTACT:
Michal Vasko63f3d842020-07-08 10:10:14 +02004178 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_CONTACT, 0, &mod->mod->contact, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004179 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004180 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004181 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &mod->mod->dsc, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004182 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004183 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004184 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &mod->mod->ref, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004185 break;
4186
4187 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004188 case LY_STMT_REVISION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004189 LY_CHECK_RET(parse_revision(ctx, in, &mod->revs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004190 break;
4191
4192 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004193 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004194 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "module");
Radek Krejci0f969882020-08-21 16:56:47 +02004195 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004196 case LY_STMT_ANYXML:
Michal Vasko63f3d842020-07-08 10:10:14 +02004197 LY_CHECK_RET(parse_any(ctx, in, kw, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004198 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004199 case LY_STMT_CHOICE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004200 LY_CHECK_RET(parse_choice(ctx, in, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004201 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004202 case LY_STMT_CONTAINER:
Michal Vasko63f3d842020-07-08 10:10:14 +02004203 LY_CHECK_RET(parse_container(ctx, in, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004204 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004205 case LY_STMT_LEAF:
Michal Vasko63f3d842020-07-08 10:10:14 +02004206 LY_CHECK_RET(parse_leaf(ctx, in, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004207 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004208 case LY_STMT_LEAF_LIST:
Michal Vasko63f3d842020-07-08 10:10:14 +02004209 LY_CHECK_RET(parse_leaflist(ctx, in, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004210 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004211 case LY_STMT_LIST:
Michal Vasko63f3d842020-07-08 10:10:14 +02004212 LY_CHECK_RET(parse_list(ctx, in, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004213 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004214 case LY_STMT_USES:
Michal Vasko63f3d842020-07-08 10:10:14 +02004215 LY_CHECK_RET(parse_uses(ctx, in, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004216 break;
4217
Radek Krejcid6b76452019-09-03 17:03:03 +02004218 case LY_STMT_AUGMENT:
Michal Vasko63f3d842020-07-08 10:10:14 +02004219 LY_CHECK_RET(parse_augment(ctx, in, NULL, &mod->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004220 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004221 case LY_STMT_DEVIATION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004222 LY_CHECK_RET(parse_deviation(ctx, in, &mod->deviations));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004223 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004224 case LY_STMT_EXTENSION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004225 LY_CHECK_RET(parse_extension(ctx, in, &mod->extensions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004226 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004227 case LY_STMT_FEATURE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004228 LY_CHECK_RET(parse_feature(ctx, in, &mod->features));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004229 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004230 case LY_STMT_GROUPING:
Michal Vasko63f3d842020-07-08 10:10:14 +02004231 LY_CHECK_RET(parse_grouping(ctx, in, NULL, &mod->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004232 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004233 case LY_STMT_IDENTITY:
Michal Vasko63f3d842020-07-08 10:10:14 +02004234 LY_CHECK_RET(parse_identity(ctx, in, &mod->identities));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004235 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004236 case LY_STMT_NOTIFICATION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004237 LY_CHECK_RET(parse_notif(ctx, in, NULL, &mod->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004238 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004239 case LY_STMT_RPC:
Michal Vasko63f3d842020-07-08 10:10:14 +02004240 LY_CHECK_RET(parse_action(ctx, in, NULL, &mod->rpcs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004241 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004242 case LY_STMT_TYPEDEF:
Michal Vasko63f3d842020-07-08 10:10:14 +02004243 LY_CHECK_RET(parse_typedef(ctx, NULL, in, &mod->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004244 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004245 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004246 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004247 break;
4248
4249 default:
David Sedlákb3077192019-06-19 10:55:37 +02004250 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004251 return LY_EVALID;
4252 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004253 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004254 LY_CHECK_RET(ret);
Radek Krejcie86bf772018-12-14 11:39:53 +01004255
Radek Krejci6d6556c2018-11-08 09:37:45 +01004256checks:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004257 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01004258 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, mod->groupings, mod->augments, mod->rpcs, mod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004259
Michal Vasko7fbc8162018-09-17 10:35:16 +02004260 /* mandatory substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004261 if (!mod->mod->ns) {
David Sedlákb3077192019-06-19 10:55:37 +02004262 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004263 return LY_EVALID;
4264 } else if (!mod->mod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004265 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004266 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004267 }
4268
Radek Krejcie9e987e2018-10-31 12:50:27 +01004269 /* submodules share the namespace with the module names, so there must not be
4270 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004271 dup = ly_ctx_get_submodule(PARSER_CTX(ctx), NULL, mod->mod->name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004272 if (dup) {
David Sedlákb3077192019-06-19 10:55:37 +02004273 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 +01004274 return LY_EVALID;
4275 }
4276
4277 return ret;
4278}
4279
4280/**
4281 * @brief Parse submodule substatements.
4282 *
4283 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02004284 * @param[in,out] in Input structure.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004285 * @param[out] submod Parsed submodule structure.
4286 *
4287 * @return LY_ERR values.
4288 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004289LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02004290parse_submodule(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004291{
4292 LY_ERR ret = 0;
4293 char *buf, *word;
4294 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004295 enum ly_stmt kw, prev_kw = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004296 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
4297 struct lysp_submodule *dup;
4298
Michal Vaskoc3781c32020-10-06 14:04:08 +02004299 submod->is_submod = 1;
4300
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004301 /* submodule name */
Michal Vasko63f3d842020-07-08 10:10:14 +02004302 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004303 INSERT_WORD_RET(ctx, buf, submod->name, word, word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004304
Michal Vasko63f3d842020-07-08 10:10:14 +02004305 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004306
4307#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004308 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 +01004309
4310 switch (kw) {
4311 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004312 case LY_STMT_BELONGS_TO:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004313 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4314 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004315 case LY_STMT_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004316 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4317 break;
4318 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004319 case LY_STMT_INCLUDE:
4320 case LY_STMT_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004321 CHECK_ORDER(Y_MOD_LINKAGE);
4322 break;
4323 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004324 case LY_STMT_ORGANIZATION:
4325 case LY_STMT_CONTACT:
4326 case LY_STMT_DESCRIPTION:
4327 case LY_STMT_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004328 CHECK_ORDER(Y_MOD_META);
4329 break;
4330
4331 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004332 case LY_STMT_REVISION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004333 CHECK_ORDER(Y_MOD_REVISION);
4334 break;
4335 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004336 case LY_STMT_ANYDATA:
4337 case LY_STMT_ANYXML:
4338 case LY_STMT_AUGMENT:
4339 case LY_STMT_CHOICE:
4340 case LY_STMT_CONTAINER:
4341 case LY_STMT_DEVIATION:
4342 case LY_STMT_EXTENSION:
4343 case LY_STMT_FEATURE:
4344 case LY_STMT_GROUPING:
4345 case LY_STMT_IDENTITY:
4346 case LY_STMT_LEAF:
4347 case LY_STMT_LEAF_LIST:
4348 case LY_STMT_LIST:
4349 case LY_STMT_NOTIFICATION:
4350 case LY_STMT_RPC:
4351 case LY_STMT_TYPEDEF:
4352 case LY_STMT_USES:
4353 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004354 mod_stmt = Y_MOD_BODY;
4355 break;
4356 default:
4357 /* error handled in the next switch */
4358 break;
4359 }
4360#undef CHECK_ORDER
4361
4362 prev_kw = kw;
4363 switch (kw) {
4364 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004365 case LY_STMT_YANG_VERSION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004366 LY_CHECK_RET(parse_yangversion(ctx, in, &submod->version, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004367 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004368 case LY_STMT_BELONGS_TO:
Michal Vaskoc3781c32020-10-06 14:04:08 +02004369 LY_CHECK_RET(parse_belongsto(ctx, in, &submod->prefix, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004370 break;
4371
4372 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004373 case LY_STMT_INCLUDE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004374 LY_CHECK_RET(parse_include(ctx, submod->name, in, &submod->includes));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004375 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004376 case LY_STMT_IMPORT:
Michal Vasko63f3d842020-07-08 10:10:14 +02004377 LY_CHECK_RET(parse_import(ctx, submod->prefix, in, &submod->imports));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004378 break;
4379
4380 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004381 case LY_STMT_ORGANIZATION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004382 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_ORGANIZATION, 0, &submod->org, Y_STR_ARG, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004383 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004384 case LY_STMT_CONTACT:
Michal Vasko63f3d842020-07-08 10:10:14 +02004385 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_CONTACT, 0, &submod->contact, Y_STR_ARG, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004386 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004387 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004388 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &submod->dsc, Y_STR_ARG, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004389 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004390 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004391 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &submod->ref, Y_STR_ARG, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004392 break;
4393
4394 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004395 case LY_STMT_REVISION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004396 LY_CHECK_RET(parse_revision(ctx, in, &submod->revs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004397 break;
4398
4399 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004400 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004401 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "submodule");
Radek Krejci0f969882020-08-21 16:56:47 +02004402 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004403 case LY_STMT_ANYXML:
Michal Vasko63f3d842020-07-08 10:10:14 +02004404 LY_CHECK_RET(parse_any(ctx, in, kw, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004405 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004406 case LY_STMT_CHOICE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004407 LY_CHECK_RET(parse_choice(ctx, in, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004408 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004409 case LY_STMT_CONTAINER:
Michal Vasko63f3d842020-07-08 10:10:14 +02004410 LY_CHECK_RET(parse_container(ctx, in, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004411 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004412 case LY_STMT_LEAF:
Michal Vasko63f3d842020-07-08 10:10:14 +02004413 LY_CHECK_RET(parse_leaf(ctx, in, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004414 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004415 case LY_STMT_LEAF_LIST:
Michal Vasko63f3d842020-07-08 10:10:14 +02004416 LY_CHECK_RET(parse_leaflist(ctx, in, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004417 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004418 case LY_STMT_LIST:
Michal Vasko63f3d842020-07-08 10:10:14 +02004419 LY_CHECK_RET(parse_list(ctx, in, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004420 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004421 case LY_STMT_USES:
Michal Vasko63f3d842020-07-08 10:10:14 +02004422 LY_CHECK_RET(parse_uses(ctx, in, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004423 break;
4424
Radek Krejcid6b76452019-09-03 17:03:03 +02004425 case LY_STMT_AUGMENT:
Michal Vasko63f3d842020-07-08 10:10:14 +02004426 LY_CHECK_RET(parse_augment(ctx, in, NULL, &submod->augments));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004427 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004428 case LY_STMT_DEVIATION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004429 LY_CHECK_RET(parse_deviation(ctx, in, &submod->deviations));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004430 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004431 case LY_STMT_EXTENSION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004432 LY_CHECK_RET(parse_extension(ctx, in, &submod->extensions));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004433 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004434 case LY_STMT_FEATURE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004435 LY_CHECK_RET(parse_feature(ctx, in, &submod->features));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004436 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004437 case LY_STMT_GROUPING:
Michal Vasko63f3d842020-07-08 10:10:14 +02004438 LY_CHECK_RET(parse_grouping(ctx, in, NULL, &submod->groupings));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004439 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004440 case LY_STMT_IDENTITY:
Michal Vasko63f3d842020-07-08 10:10:14 +02004441 LY_CHECK_RET(parse_identity(ctx, in, &submod->identities));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004442 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004443 case LY_STMT_NOTIFICATION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004444 LY_CHECK_RET(parse_notif(ctx, in, NULL, &submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004445 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004446 case LY_STMT_RPC:
Michal Vasko63f3d842020-07-08 10:10:14 +02004447 LY_CHECK_RET(parse_action(ctx, in, NULL, &submod->rpcs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004448 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004449 case LY_STMT_TYPEDEF:
Michal Vasko63f3d842020-07-08 10:10:14 +02004450 LY_CHECK_RET(parse_typedef(ctx, NULL, in, &submod->typedefs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004451 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004452 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004453 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004454 break;
4455
4456 default:
David Sedlákb3077192019-06-19 10:55:37 +02004457 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004458 return LY_EVALID;
4459 }
4460 }
4461 LY_CHECK_RET(ret);
4462
4463checks:
4464 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01004465 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, submod->groupings, submod->augments,
Michal Vasko69730152020-10-09 16:30:07 +02004466 submod->rpcs, submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004467
4468 /* mandatory substatements */
Michal Vaskoc3781c32020-10-06 14:04:08 +02004469 if (!submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004470 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004471 return LY_EVALID;
4472 }
4473
4474 /* submodules share the namespace with the module names, so there must not be
4475 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004476 dup = ly_ctx_get_submodule(PARSER_CTX(ctx), NULL, submod->name, NULL);
Michal Vaskoc3781c32020-10-06 14:04:08 +02004477 /* main modules may have different revisions */
4478 if (dup && strcmp(dup->mod->name, submod->mod->name)) {
David Sedlákb3077192019-06-19 10:55:37 +02004479 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Name collision between submodules of name \"%s\".", dup->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +01004480 return LY_EVALID;
4481 }
4482
Michal Vasko7fbc8162018-09-17 10:35:16 +02004483 return ret;
4484}
4485
Radek Krejcid4557c62018-09-17 11:42:09 +02004486LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01004487yang_parse_submodule(struct lys_yang_parser_ctx **context, struct ly_ctx *ly_ctx, struct lys_parser_ctx *main_ctx,
Radek Krejci0f969882020-08-21 16:56:47 +02004488 struct ly_in *in, struct lysp_submodule **submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004489{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004490 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004491 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004492 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004493 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004494 struct lysp_submodule *mod_p = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004495
David Sedlák1b623122019-08-05 15:27:49 +02004496 /* create context */
4497 *context = calloc(1, sizeof **context);
4498 LY_CHECK_ERR_RET(!(*context), LOGMEM(ly_ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004499 (*context)->format = LYS_IN_YANG;
Radek Krejci99435242019-09-05 16:19:15 +02004500 (*context)->pos_type = LY_VLOG_LINE;
David Sedlák1b623122019-08-05 15:27:49 +02004501 (*context)->line = 1;
4502
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004503 mod_p = calloc(1, sizeof *mod_p);
4504 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ly_ctx); ret = LY_EMEM, cleanup);
4505 mod_p->mod = main_ctx->parsed_mod->mod;
4506 mod_p->parsing = 1;
4507 (*context)->parsed_mod = (struct lysp_module *)mod_p;
4508
David Sedlák1b623122019-08-05 15:27:49 +02004509 /* map the typedefs and groupings list from main context to the submodule's context */
4510 memcpy(&(*context)->tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
4511 memcpy(&(*context)->grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes);
4512
Michal Vasko7fbc8162018-09-17 10:35:16 +02004513 /* "module"/"submodule" */
Michal Vasko63f3d842020-07-08 10:10:14 +02004514 ret = get_keyword(*context, in, &kw, &word, &word_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004515 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004516
Radek Krejcid6b76452019-09-03 17:03:03 +02004517 if (kw == LY_STMT_MODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004518 LOGERR(ly_ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004519 ret = LY_EINVAL;
4520 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004521 } else if (kw != LY_STMT_SUBMODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004522 LOGVAL_PARSER(*context, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004523 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004524 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004525 }
4526
Michal Vasko7fbc8162018-09-17 10:35:16 +02004527 /* substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02004528 ret = parse_submodule(*context, in, mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004529 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004530
4531 /* read some trailing spaces or new lines */
Michal Vasko63f3d842020-07-08 10:10:14 +02004532 while (isspace(in->current[0])) {
4533 ++in->current;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004534 }
Michal Vasko63f3d842020-07-08 10:10:14 +02004535 if (in->current[0]) {
4536 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_SUBMOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004537 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004538 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004539 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004540
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004541 mod_p->parsing = 0;
4542 *submod = mod_p;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004543
Radek Krejcibbe09a92018-11-08 09:36:54 +01004544cleanup:
4545 if (ret) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004546 lysp_module_free((struct lysp_module *)mod_p);
Michal Vaskob36053d2020-03-26 15:49:30 +01004547 yang_parser_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004548 *context = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004549 }
4550
4551 return ret;
4552}
4553
4554LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02004555yang_parse_module(struct lys_yang_parser_ctx **context, struct ly_in *in, struct lys_module *mod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004556{
4557 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004558 char *word;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004559 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004560 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004561 struct lysp_module *mod_p = NULL;
4562
David Sedlák1b623122019-08-05 15:27:49 +02004563 /* create context */
4564 *context = calloc(1, sizeof **context);
4565 LY_CHECK_ERR_RET(!(*context), LOGMEM(mod->ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004566 (*context)->format = LYS_IN_YANG;
Radek Krejci335332a2019-09-05 13:03:35 +02004567 (*context)->pos_type = LY_VLOG_LINE;
David Sedlák1b623122019-08-05 15:27:49 +02004568 (*context)->line = 1;
4569
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004570 mod_p = calloc(1, sizeof *mod_p);
4571 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(mod->ctx), cleanup);
4572 mod_p->mod = mod;
4573 mod_p->parsing = 1;
4574 (*context)->parsed_mod = mod_p;
4575
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004576 /* "module"/"submodule" */
Michal Vasko63f3d842020-07-08 10:10:14 +02004577 ret = get_keyword(*context, in, &kw, &word, &word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004578 LY_CHECK_GOTO(ret, cleanup);
4579
Radek Krejcid6b76452019-09-03 17:03:03 +02004580 if (kw == LY_STMT_SUBMODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004581 LOGERR(mod->ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004582 ret = LY_EINVAL;
4583 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004584 } else if (kw != LY_STMT_MODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004585 LOGVAL_PARSER((*context), LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004586 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004587 goto cleanup;
4588 }
4589
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004590 /* substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02004591 ret = parse_module(*context, in, mod_p);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004592 LY_CHECK_GOTO(ret, cleanup);
4593
4594 /* read some trailing spaces or new lines */
Michal Vasko63f3d842020-07-08 10:10:14 +02004595 while (isspace(in->current[0])) {
4596 ++in->current;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004597 }
Michal Vasko63f3d842020-07-08 10:10:14 +02004598 if (in->current[0]) {
4599 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_MOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004600 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004601 goto cleanup;
4602 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004603
4604 mod_p->parsing = 0;
4605 mod->parsed = mod_p;
4606
4607cleanup:
4608 if (ret) {
4609 lysp_module_free(mod_p);
Michal Vaskob36053d2020-03-26 15:49:30 +01004610 yang_parser_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004611 *context = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004612 }
4613
Michal Vasko7fbc8162018-09-17 10:35:16 +02004614 return ret;
4615}