blob: beedbf15b9008363c892e30ca0f26613773c9ab0 [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"
Radek Krejci859a15a2021-03-05 20:56:59 +010033#include "tree_edit.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020034#include "tree_schema.h"
Radek Krejci70853c52018-10-15 14:46:16 +020035#include "tree_schema_internal.h"
Radek Krejci44ceedc2018-10-02 15:54:31 +020036
Radek Krejci77114102021-03-10 15:21:57 +010037struct lys_glob_unres;
38
Radek Krejciceaf2122019-01-02 15:03:26 +010039/**
40 * @brief Insert WORD into the libyang context's dictionary and store as TARGET.
41 * @param[in] CTX yang parser context to access libyang context.
42 * @param[in] BUF buffer in case the word is not a constant and can be inserted directly (zero-copy)
43 * @param[out] TARGET variable where to store the pointer to the inserted value.
44 * @param[in] WORD string to store.
45 * @param[in] LEN length of the string in WORD to store.
46 */
Radek Krejci011e4aa2020-09-04 15:22:31 +020047#define INSERT_WORD_RET(CTX, BUF, TARGET, WORD, LEN) \
Michal Vasko5d24f6c2020-10-13 13:49:06 +020048 if (BUF) {LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(CTX), WORD, &(TARGET)));}\
49 else {LY_CHECK_RET(lydict_insert(PARSER_CTX(CTX), LEN ? WORD : "", LEN, &(TARGET)));}
Radek Krejci44ceedc2018-10-02 15:54:31 +020050
Radek Krejciceaf2122019-01-02 15:03:26 +010051/**
Michal Vasko63f3d842020-07-08 10:10:14 +020052 * @brief Read from the IN structure COUNT items. Also updates the indent value in yang parser context
Radek Krejciceaf2122019-01-02 15:03:26 +010053 * @param[in] CTX yang parser context to update its indent value.
Radek Krejciceaf2122019-01-02 15:03:26 +010054 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
55 */
Radek Krejcid54412f2020-12-17 20:25:35 +010056#define MOVE_INPUT(CTX, COUNT) ly_in_skip((CTX)->in, COUNT);(CTX)->indent+=COUNT
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020057
Michal Vaskoea5abea2018-09-18 13:10:54 +020058/**
aPieceka24a2252021-05-07 10:52:31 +020059 * @brief Loop through all substatements.
Michal Vaskoea5abea2018-09-18 13:10:54 +020060 *
Radek Krejci44ceedc2018-10-02 15:54:31 +020061 * @param[in] CTX yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +020062 * @param[out] KW YANG keyword read.
63 * @param[out] WORD Pointer to the keyword itself.
64 * @param[out] WORD_LEN Length of the keyword.
65 * @param[out] ERR Variable for error storing.
aPieceka24a2252021-05-07 10:52:31 +020066 * @param[in] SUC_CMD Command is applied if a semicolon is found, so no
67 * substatements are available. It is expected to contain a return or goto command.
68 * @param[in] ERR_CMD Command is applied if an error occurs before loop through
69 * substatements. It is expected to contain return or goto command.
Michal Vaskoea5abea2018-09-18 13:10:54 +020070 *
71 * @return In case there are no substatements or a fatal error encountered.
72 */
aPieceka24a2252021-05-07 10:52:31 +020073#define YANG_READ_SUBSTMT_FOR(CTX, KW, WORD, WORD_LEN, ERR, SUC_CMD, ERR_CMD) \
74 if ((ERR = get_keyword(CTX, &KW, &WORD, &WORD_LEN))) { \
75 ERR_CMD; \
76 } \
Radek Krejcid6b76452019-09-03 17:03:03 +020077 if (KW == LY_STMT_SYNTAX_SEMICOLON) { \
aPieceka24a2252021-05-07 10:52:31 +020078 SUC_CMD; \
Michal Vasko7fbc8162018-09-17 10:35:16 +020079 } \
Radek Krejcid6b76452019-09-03 17:03:03 +020080 if (KW != LY_STMT_SYNTAX_LEFT_BRACE) { \
David Sedlákb3077192019-06-19 10:55:37 +020081 LOGVAL_PARSER(CTX, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\", expected \";\" or \"{\".", ly_stmt2str(KW)); \
aPieceka24a2252021-05-07 10:52:31 +020082 ERR = LY_EVALID; \
83 ERR_CMD; \
Michal Vasko7fbc8162018-09-17 10:35:16 +020084 } \
Radek Krejci33090f92020-12-17 20:12:46 +010085 for (ERR = get_keyword(CTX, &KW, &WORD, &WORD_LEN); \
Radek Krejcid6b76452019-09-03 17:03:03 +020086 !ERR && (KW != LY_STMT_SYNTAX_RIGHT_BRACE); \
Radek Krejci33090f92020-12-17 20:12:46 +010087 ERR = get_keyword(CTX, &KW, &WORD, &WORD_LEN))
Michal Vasko7fbc8162018-09-17 10:35:16 +020088
Radek Krejci33090f92020-12-17 20:12:46 +010089LY_ERR parse_container(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent,
Radek Krejci0f969882020-08-21 16:56:47 +020090 struct lysp_node **siblings);
Radek Krejci33090f92020-12-17 20:12:46 +010091LY_ERR parse_uses(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
92LY_ERR parse_choice(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
93LY_ERR parse_case(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
94LY_ERR parse_list(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
Radek Krejci2a9fc652021-01-22 17:44:34 +010095LY_ERR parse_grouping(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_grp **groupings);
Michal Vasko7fbc8162018-09-17 10:35:16 +020096
Michal Vaskoea5abea2018-09-18 13:10:54 +020097/**
98 * @brief Add another character to dynamic buffer, a low-level function.
99 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200100 * Enlarge if needed. Updates \p input as well as \p buf_used.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200101 *
Radek Krejci404251e2018-10-09 12:06:44 +0200102 * @param[in] ctx libyang context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200103 * @param[in,out] in Input structure.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200104 * @param[in] len Number of bytes to get from the input string and copy into the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200105 * @param[in,out] buf Buffer to use, can be moved by realloc().
106 * @param[in,out] buf_len Current size of the buffer.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200107 * @param[in,out] buf_used Currently used characters of the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200108 *
109 * @return LY_ERR values.
110 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200111LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200112buf_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 +0200113{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100114#define BUF_STEP 16;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200115 if (*buf_len <= (*buf_used) + len) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100116 *buf_len += BUF_STEP;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200117 *buf = ly_realloc(*buf, *buf_len);
118 LY_CHECK_ERR_RET(!*buf, LOGMEM(ctx), LY_EMEM);
119 }
Radek Krejcic0917392019-04-10 13:04:04 +0200120 if (*buf_used) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200121 ly_in_read(in, &(*buf)[*buf_used], len);
Radek Krejcic0917392019-04-10 13:04:04 +0200122 } else {
Michal Vasko63f3d842020-07-08 10:10:14 +0200123 ly_in_read(in, *buf, len);
Radek Krejcic0917392019-04-10 13:04:04 +0200124 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200125
Radek Krejci44ceedc2018-10-02 15:54:31 +0200126 (*buf_used) += len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200127 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100128#undef BUF_STEP
Michal Vasko7fbc8162018-09-17 10:35:16 +0200129}
130
Michal Vaskoea5abea2018-09-18 13:10:54 +0200131/**
Radek Krejci44ceedc2018-10-02 15:54:31 +0200132 * @brief Store a single UTF8 character. It depends whether in a dynamically-allocated buffer or just as a pointer to the data.
133 *
134 * @param[in] ctx yang parser context for logging.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200135 * @param[in] arg Type of the input string to select method of checking character validity.
136 * @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 +0200137 * 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 +0200138 * @param[in,out] word_len Current length of the word pointed to by \p word_p.
139 * @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 +0200140 * @param[in,out] buf_len Current length of \p word_b.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200141 * @param[in] need_buf Flag if the dynamically allocated buffer is required.
David Sedlák40bb13b2019-07-10 14:34:18 +0200142 * @param[in,out] prefix Storage for internally used flag in case of possible prefixed identifiers:
143 * 0 - colon not yet found (no prefix)
144 * 1 - \p c is the colon character
145 * 2 - prefix already processed, now processing the identifier
Michal Vaskoea5abea2018-09-18 13:10:54 +0200146 *
147 * @return LY_ERR values.
148 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200149LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100150buf_store_char(struct lys_yang_parser_ctx *ctx, enum yang_arg arg, char **word_p, size_t *word_len,
Radek Krejci857189e2020-09-01 13:26:36 +0200151 char **word_b, size_t *buf_len, ly_bool need_buf, uint8_t *prefix)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200152{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200153 uint32_t c;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200154 size_t len;
155
Radek Krejcif29b7c32019-04-09 16:17:49 +0200156 /* check valid combination of input paremeters - if need_buf specified, word_b must be provided */
157 assert(!need_buf || (need_buf && word_b));
158
Radek Krejci44ceedc2018-10-02 15:54:31 +0200159 /* get UTF8 code point (and number of bytes coding the character) */
Radek Krejci33090f92020-12-17 20:12:46 +0100160 LY_CHECK_ERR_RET(ly_getutf8(&ctx->in->current, &c, &len),
161 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[-len]), LY_EVALID);
162 ctx->in->current -= len;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200163 if (c == '\n') {
164 ctx->indent = 0;
Radek Krejcidd713ce2021-01-04 23:12:12 +0100165 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200166 } else {
167 /* note - even the multibyte character is count as 1 */
168 ++ctx->indent;
169 }
170
Radek Krejci44ceedc2018-10-02 15:54:31 +0200171 /* check character validity */
172 switch (arg) {
173 case Y_IDENTIF_ARG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100174 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !(*word_len), NULL));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200175 break;
176 case Y_PREF_IDENTIF_ARG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100177 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !(*word_len), prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200178 break;
179 case Y_STR_ARG:
180 case Y_MAYBE_STR_ARG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100181 LY_CHECK_RET(lysp_check_stringchar((struct lys_parser_ctx *)ctx, c));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200182 break;
183 }
184
Michal Vasko7fbc8162018-09-17 10:35:16 +0200185 if (word_b && *word_b) {
186 /* add another character into buffer */
Radek Krejci33090f92020-12-17 20:12:46 +0100187 if (buf_add_char(PARSER_CTX(ctx), ctx->in, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200188 return LY_EMEM;
189 }
190
191 /* in case of realloc */
192 *word_p = *word_b;
Radek Krejcif29b7c32019-04-09 16:17:49 +0200193 } else if (word_b && need_buf) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200194 /* first time we need a buffer, copy everything read up to now */
195 if (*word_len) {
196 *word_b = malloc(*word_len);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200197 LY_CHECK_ERR_RET(!*word_b, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200198 *buf_len = *word_len;
199 memcpy(*word_b, *word_p, *word_len);
200 }
201
202 /* add this new character into buffer */
Radek Krejci33090f92020-12-17 20:12:46 +0100203 if (buf_add_char(PARSER_CTX(ctx), ctx->in, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200204 return LY_EMEM;
205 }
206
207 /* in case of realloc */
208 *word_p = *word_b;
209 } else {
210 /* just remember the first character pointer */
211 if (!*word_p) {
Radek Krejci33090f92020-12-17 20:12:46 +0100212 *word_p = (char *)ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200213 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200214 /* ... and update the word's length */
215 (*word_len) += len;
Radek Krejci33090f92020-12-17 20:12:46 +0100216 ly_in_skip(ctx->in, len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200217 }
218
219 return LY_SUCCESS;
220}
221
Michal Vaskoea5abea2018-09-18 13:10:54 +0200222/**
223 * @brief Skip YANG comment in data.
224 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200225 * @param[in] ctx yang parser context for logging.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200226 * @param[in] comment Type of the comment to process:
227 * 1 for a one-line comment,
228 * 2 for a block comment.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200229 * @return LY_ERR values.
230 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200231LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100232skip_comment(struct lys_yang_parser_ctx *ctx, uint8_t comment)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200233{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100234 /* internal statuses: */
235#define COMMENT_NO 0 /* comment ended */
236#define COMMENT_LINE 1 /* in line comment */
237#define COMMENT_BLOCK 2 /* in block comment */
238#define COMMENT_BLOCK_END 3 /* in block comment with last read character '*' */
239
Radek Krejci33090f92020-12-17 20:12:46 +0100240 while (ctx->in->current[0] && comment) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200241 switch (comment) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100242 case COMMENT_LINE:
Radek Krejci33090f92020-12-17 20:12:46 +0100243 if (ctx->in->current[0] == '\n') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100244 comment = COMMENT_NO;
Radek Krejcid54412f2020-12-17 20:25:35 +0100245 LY_IN_NEW_LINE(ctx->in);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200246 }
247 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100248 case COMMENT_BLOCK:
Radek Krejci33090f92020-12-17 20:12:46 +0100249 if (ctx->in->current[0] == '*') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100250 comment = COMMENT_BLOCK_END;
Radek Krejci33090f92020-12-17 20:12:46 +0100251 } else if (ctx->in->current[0] == '\n') {
Radek Krejcid54412f2020-12-17 20:25:35 +0100252 LY_IN_NEW_LINE(ctx->in);
Radek Krejci15c80ca2018-10-09 11:01:31 +0200253 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200254 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100255 case COMMENT_BLOCK_END:
Radek Krejci33090f92020-12-17 20:12:46 +0100256 if (ctx->in->current[0] == '/') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100257 comment = COMMENT_NO;
Radek Krejci33090f92020-12-17 20:12:46 +0100258 } else if (ctx->in->current[0] != '*') {
259 if (ctx->in->current[0] == '\n') {
Radek Krejcid54412f2020-12-17 20:25:35 +0100260 LY_IN_NEW_LINE(ctx->in);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200261 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100262 comment = COMMENT_BLOCK;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200263 }
264 break;
265 default:
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200266 LOGINT_RET(PARSER_CTX(ctx));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200267 }
268
Radek Krejci33090f92020-12-17 20:12:46 +0100269 if (ctx->in->current[0] == '\n') {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200270 ctx->indent = 0;
271 } else {
272 ++ctx->indent;
273 }
Radek Krejci33090f92020-12-17 20:12:46 +0100274 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200275 }
276
Radek Krejci33090f92020-12-17 20:12:46 +0100277 if (!ctx->in->current[0] && (comment >= COMMENT_BLOCK)) {
David Sedlákb3077192019-06-19 10:55:37 +0200278 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Unexpected end-of-input, non-terminated comment.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200279 return LY_EVALID;
280 }
281
282 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100283
284#undef COMMENT_NO
285#undef COMMENT_LINE
286#undef COMMENT_BLOCK
287#undef COMMENT_BLOCK_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200288}
289
Michal Vaskoea5abea2018-09-18 13:10:54 +0200290/**
291 * @brief Read a quoted string from data.
292 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200293 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200294 * @param[in] arg Type of YANG keyword argument expected.
295 * @param[out] word_p Pointer to the read quoted string.
296 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read quoted string. If not needed,
297 * set to NULL. Otherwise equal to \p word_p.
298 * @param[out] word_len Length of the read quoted string.
299 * @param[out] buf_len Length of the dynamically-allocated buffer \p word_b.
300 * @param[in] indent Current indent (number of YANG spaces). Needed for correct multi-line string
301 * indenation in the final quoted string.
302 *
303 * @return LY_ERR values.
304 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200305static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100306read_qstring(struct lys_yang_parser_ctx *ctx, enum yang_arg arg, char **word_p, char **word_b,
Radek Krejci0f969882020-08-21 16:56:47 +0200307 size_t *word_len, size_t *buf_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200308{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100309 /* string parsing status: */
310#define STRING_ENDED 0 /* string ended */
311#define STRING_SINGLE_QUOTED 1 /* string with ' */
312#define STRING_DOUBLE_QUOTED 2 /* string with " */
313#define STRING_DOUBLE_QUOTED_ESCAPED 3 /* string with " with last character \ */
314#define STRING_PAUSED_NEXTSTRING 4 /* string finished, now skipping whitespaces looking for + */
315#define STRING_PAUSED_CONTINUE 5 /* string continues after +, skipping whitespaces */
316
Radek Krejci1deb5be2020-08-26 16:43:36 +0200317 uint8_t string;
318 uint64_t block_indent = 0, current_indent = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200319 ly_bool need_buf = 0;
320 uint8_t prefix = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200321 const char *c;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200322 uint64_t trailing_ws = 0; /* current number of stored trailing whitespace characters */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200323
Radek Krejci33090f92020-12-17 20:12:46 +0100324 if (ctx->in->current[0] == '\"') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100325 string = STRING_DOUBLE_QUOTED;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200326 current_indent = block_indent = ctx->indent + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200327 } else {
Radek Krejci33090f92020-12-17 20:12:46 +0100328 assert(ctx->in->current[0] == '\'');
Radek Krejcif13b87b2020-12-01 22:02:17 +0100329 string = STRING_SINGLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200330 }
Radek Krejci33090f92020-12-17 20:12:46 +0100331 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200332
Radek Krejci33090f92020-12-17 20:12:46 +0100333 while (ctx->in->current[0] && string) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200334 switch (string) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100335 case STRING_SINGLE_QUOTED:
Radek Krejci33090f92020-12-17 20:12:46 +0100336 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200337 case '\'':
338 /* string may be finished, but check for + */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100339 string = STRING_PAUSED_NEXTSTRING;
Radek Krejci33090f92020-12-17 20:12:46 +0100340 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200341 break;
342 default:
343 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100344 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200345 break;
346 }
347 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100348 case STRING_DOUBLE_QUOTED:
Radek Krejci33090f92020-12-17 20:12:46 +0100349 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200350 case '\"':
351 /* string may be finished, but check for + */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100352 string = STRING_PAUSED_NEXTSTRING;
Radek Krejci33090f92020-12-17 20:12:46 +0100353 MOVE_INPUT(ctx, 1);
Radek Krejciff13cd12019-10-25 15:34:24 +0200354 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200355 break;
356 case '\\':
357 /* special character following */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100358 string = STRING_DOUBLE_QUOTED_ESCAPED;
Radek Krejciff13cd12019-10-25 15:34:24 +0200359
360 /* the backslash sequence is substituted, so we will need a buffer to store the result */
361 need_buf = 1;
362
363 /* move forward to the escaped character */
Radek Krejci33090f92020-12-17 20:12:46 +0100364 ++ctx->in->current;
Radek Krejciff13cd12019-10-25 15:34:24 +0200365
366 /* note that the trailing whitespaces are supposed to be trimmed before substitution of
367 * backslash-escaped characters (RFC 7950, 6.1.3), so we have to zero the trailing whitespaces counter */
368 trailing_ws = 0;
369
370 /* since the backslash-escaped character is handled as first non-whitespace character, stop eating indentation */
371 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200372 break;
373 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200374 if (current_indent < block_indent) {
375 ++current_indent;
Radek Krejci33090f92020-12-17 20:12:46 +0100376 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200377 } else {
Michal Vasko90edde42019-11-25 15:25:07 +0100378 /* check and store whitespace character */
Radek Krejci33090f92020-12-17 20:12:46 +0100379 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko90edde42019-11-25 15:25:07 +0100380 trailing_ws++;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200381 }
382 break;
383 case '\t':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200384 if (current_indent < block_indent) {
385 assert(need_buf);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100386 current_indent += Y_TAB_SPACES;
387 ctx->indent += Y_TAB_SPACES;
Michal Vaskod989ba02020-08-24 10:59:24 +0200388 for ( ; current_indent > block_indent; --current_indent, --ctx->indent) {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200389 /* store leftover spaces from the tab */
Radek Krejci33090f92020-12-17 20:12:46 +0100390 c = ctx->in->current;
391 ctx->in->current = " ";
392 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
393 ctx->in->current = c;
Michal Vasko90edde42019-11-25 15:25:07 +0100394 trailing_ws++;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200395 }
Radek Krejci33090f92020-12-17 20:12:46 +0100396 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200397 } else {
Michal Vasko90edde42019-11-25 15:25:07 +0100398 /* check and store whitespace character */
Radek Krejci33090f92020-12-17 20:12:46 +0100399 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko90edde42019-11-25 15:25:07 +0100400 trailing_ws++;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200401 /* additional characters for indentation - only 1 was count in buf_store_char */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100402 ctx->indent += Y_TAB_SPACES - 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200403 }
404 break;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200405 case '\r':
406 if (ctx->in->current[1] != '\n') {
407 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
408 return LY_EVALID;
409 }
410 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200411 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200412 if (block_indent) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200413 /* we will be removing the indents so we need our own buffer */
414 need_buf = 1;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200415
416 /* remove trailing tabs and spaces */
Radek Krejciff13cd12019-10-25 15:34:24 +0200417 (*word_len) = *word_len - trailing_ws;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200418
Radek Krejciff13cd12019-10-25 15:34:24 +0200419 /* restart indentation */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200420 current_indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200421 }
422
423 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100424 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200425
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200426 /* reset context indentation counter for possible string after this one */
427 ctx->indent = 0;
Radek Krejciff13cd12019-10-25 15:34:24 +0200428 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200429 break;
430 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200431 /* first non-whitespace character, stop eating indentation */
432 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200433
434 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100435 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Radek Krejciff13cd12019-10-25 15:34:24 +0200436 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200437 break;
438 }
439 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100440 case STRING_DOUBLE_QUOTED_ESCAPED:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200441 /* string encoded characters */
Radek Krejci33090f92020-12-17 20:12:46 +0100442 c = ctx->in->current;
443 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200444 case 'n':
Radek Krejci33090f92020-12-17 20:12:46 +0100445 ctx->in->current = "\n";
Radek Krejcidd713ce2021-01-04 23:12:12 +0100446 /* fix false newline count in buf_store_char() */
447 ctx->in->line--;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200448 break;
449 case 't':
Radek Krejci33090f92020-12-17 20:12:46 +0100450 ctx->in->current = "\t";
Michal Vasko7fbc8162018-09-17 10:35:16 +0200451 break;
452 case '\"':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200453 case '\\':
Michal Vasko63f3d842020-07-08 10:10:14 +0200454 /* ok as is */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200455 break;
456 default:
Michal Vasko63f3d842020-07-08 10:10:14 +0200457 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Double-quoted string unknown special character '\\%c'.",
Radek Krejci33090f92020-12-17 20:12:46 +0100458 ctx->in->current[0]);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200459 return LY_EVALID;
460 }
461
462 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100463 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200464
Radek Krejcif13b87b2020-12-01 22:02:17 +0100465 string = STRING_DOUBLE_QUOTED;
Radek Krejci33090f92020-12-17 20:12:46 +0100466 ctx->in->current = c + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200467 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100468 case STRING_PAUSED_NEXTSTRING:
Radek Krejci33090f92020-12-17 20:12:46 +0100469 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200470 case '+':
471 /* string continues */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100472 string = STRING_PAUSED_CONTINUE;
Radek Krejciefd22f62018-09-27 11:47:58 +0200473 need_buf = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200474 break;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200475 case '\r':
476 if (ctx->in->current[1] != '\n') {
477 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
478 return LY_EVALID;
479 }
480 MOVE_INPUT(ctx, 1);
481 /* fallthrough */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200482 case '\n':
Radek Krejcidd713ce2021-01-04 23:12:12 +0100483 LY_IN_NEW_LINE(ctx->in);
Radek Krejcicb3e6472021-01-06 08:19:01 +0100484 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200485 case ' ':
486 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200487 /* just skip */
488 break;
489 default:
490 /* string is finished */
491 goto string_end;
492 }
Radek Krejci33090f92020-12-17 20:12:46 +0100493 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200494 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100495 case STRING_PAUSED_CONTINUE:
Radek Krejci33090f92020-12-17 20:12:46 +0100496 switch (ctx->in->current[0]) {
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200497 case '\r':
498 if (ctx->in->current[1] != '\n') {
499 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
500 return LY_EVALID;
501 }
502 MOVE_INPUT(ctx, 1);
503 /* fallthrough */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200504 case '\n':
Radek Krejcidd713ce2021-01-04 23:12:12 +0100505 LY_IN_NEW_LINE(ctx->in);
Radek Krejcicb3e6472021-01-06 08:19:01 +0100506 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200507 case ' ':
508 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200509 /* skip */
510 break;
511 case '\'':
Radek Krejcif13b87b2020-12-01 22:02:17 +0100512 string = STRING_SINGLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200513 break;
514 case '\"':
Radek Krejcif13b87b2020-12-01 22:02:17 +0100515 string = STRING_DOUBLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200516 break;
517 default:
518 /* it must be quoted again */
David Sedlákb3077192019-06-19 10:55:37 +0200519 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Both string parts divided by '+' must be quoted.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200520 return LY_EVALID;
521 }
Radek Krejci33090f92020-12-17 20:12:46 +0100522 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200523 break;
524 default:
525 return LY_EINT;
526 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200527 }
528
529string_end:
Michal Vasko69730152020-10-09 16:30:07 +0200530 if ((arg <= Y_PREF_IDENTIF_ARG) && !(*word_len)) {
Radek Krejci4e199f52019-05-28 09:09:28 +0200531 /* empty identifier */
David Sedlákb3077192019-06-19 10:55:37 +0200532 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Statement argument is required.");
Radek Krejci4e199f52019-05-28 09:09:28 +0200533 return LY_EVALID;
534 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200535 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100536
537#undef STRING_ENDED
538#undef STRING_SINGLE_QUOTED
539#undef STRING_DOUBLE_QUOTED
540#undef STRING_DOUBLE_QUOTED_ESCAPED
541#undef STRING_PAUSED_NEXTSTRING
542#undef STRING_PAUSED_CONTINUE
Michal Vasko7fbc8162018-09-17 10:35:16 +0200543}
544
Michal Vaskoea5abea2018-09-18 13:10:54 +0200545/**
546 * @brief Get another YANG string from the raw data.
547 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200548 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200549 * @param[in] arg Type of YANG keyword argument expected.
Michal Vaskob68ea142021-04-26 13:46:00 +0200550 * @param[out] flags optional output argument to get flag of the argument's quoting (LYS_*QUOTED - see
551 * [schema node flags](@ref snodeflags))
Michal Vasko2ca70f52018-09-27 11:04:51 +0200552 * @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 +0200553 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read string. If not needed,
554 * set to NULL. Otherwise equal to \p word_p.
555 * @param[out] word_len Length of the read string.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200556 * @return LY_ERR values.
Michal Vasko7fbc8162018-09-17 10:35:16 +0200557 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200558LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100559get_argument(struct lys_yang_parser_ctx *ctx, enum yang_arg arg, uint16_t *flags, char **word_p,
Radek Krejci0f969882020-08-21 16:56:47 +0200560 char **word_b, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200561{
Michal Vaskob68ea142021-04-26 13:46:00 +0200562 LY_ERR ret;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200563 size_t buf_len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200564 uint8_t prefix = 0;
Michal Vasko69730152020-10-09 16:30:07 +0200565
Michal Vasko7fbc8162018-09-17 10:35:16 +0200566 /* word buffer - dynamically allocated */
567 *word_b = NULL;
568
569 /* word pointer - just a pointer to data */
570 *word_p = NULL;
571
572 *word_len = 0;
Radek Krejci33090f92020-12-17 20:12:46 +0100573 while (ctx->in->current[0]) {
574 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200575 case '\'':
576 case '\"':
577 if (*word_len) {
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200578 /* invalid - quotes cannot be in unquoted string and only optsep, ; or { can follow it */
Radek Krejci33090f92020-12-17 20:12:46 +0100579 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200580 "unquoted string character, optsep, semicolon or opening brace");
Michal Vaskob68ea142021-04-26 13:46:00 +0200581 ret = LY_EVALID;
582 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200583 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200584 if (flags) {
Radek Krejci33090f92020-12-17 20:12:46 +0100585 (*flags) |= ctx->in->current[0] == '\'' ? LYS_SINGLEQUOTED : LYS_DOUBLEQUOTED;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200586 }
Michal Vaskob68ea142021-04-26 13:46:00 +0200587 LY_CHECK_GOTO(ret = read_qstring(ctx, arg, word_p, word_b, word_len, &buf_len), error);
Michal Vasko55a16b92021-09-15 08:51:35 +0200588 if (!*word_p) {
589 /* do not return NULL word */
590 *word_p = "";
591 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200592 goto str_end;
593 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100594 if (ctx->in->current[1] == '/') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200595 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100596 MOVE_INPUT(ctx, 2);
Michal Vaskob68ea142021-04-26 13:46:00 +0200597 LY_CHECK_GOTO(ret = skip_comment(ctx, 1), error);
Radek Krejci33090f92020-12-17 20:12:46 +0100598 } else if (ctx->in->current[1] == '*') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200599 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100600 MOVE_INPUT(ctx, 2);
Michal Vaskob68ea142021-04-26 13:46:00 +0200601 LY_CHECK_GOTO(ret = skip_comment(ctx, 2), error);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200602 } else {
603 /* not a comment after all */
Michal Vaskob68ea142021-04-26 13:46:00 +0200604 LY_CHECK_GOTO(ret = buf_store_char(ctx, arg, word_p, word_len, word_b, &buf_len, 0, &prefix), error);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200605 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200606 break;
607 case ' ':
608 if (*word_len) {
609 /* word is finished */
610 goto str_end;
611 }
Radek Krejci33090f92020-12-17 20:12:46 +0100612 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200613 break;
614 case '\t':
615 if (*word_len) {
616 /* word is finished */
617 goto str_end;
618 }
619 /* tabs count for 8 spaces */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100620 ctx->indent += Y_TAB_SPACES;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200621
Radek Krejci33090f92020-12-17 20:12:46 +0100622 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200623 break;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200624 case '\r':
625 if (ctx->in->current[1] != '\n') {
626 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
Michal Vaskob68ea142021-04-26 13:46:00 +0200627 ret = LY_EVALID;
628 goto error;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200629 }
630 MOVE_INPUT(ctx, 1);
631 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200632 case '\n':
633 if (*word_len) {
634 /* word is finished */
635 goto str_end;
636 }
Radek Krejcidd713ce2021-01-04 23:12:12 +0100637 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid54412f2020-12-17 20:25:35 +0100638 MOVE_INPUT(ctx, 1);
639
Michal Vasko7fbc8162018-09-17 10:35:16 +0200640 /* reset indent */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200641 ctx->indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200642 break;
643 case ';':
644 case '{':
645 if (*word_len || (arg == Y_MAYBE_STR_ARG)) {
646 /* word is finished */
647 goto str_end;
648 }
649
Radek Krejci33090f92020-12-17 20:12:46 +0100650 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current, "an argument");
Michal Vaskob68ea142021-04-26 13:46:00 +0200651 ret = LY_EVALID;
652 goto error;
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200653 case '}':
654 /* invalid - braces cannot be in unquoted string (opening braces terminates the string and can follow it) */
Radek Krejci33090f92020-12-17 20:12:46 +0100655 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200656 "unquoted string character, optsep, semicolon or opening brace");
Michal Vaskob68ea142021-04-26 13:46:00 +0200657 ret = LY_EVALID;
658 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200659 default:
Michal Vaskob68ea142021-04-26 13:46:00 +0200660 LY_CHECK_GOTO(ret = buf_store_char(ctx, arg, word_p, word_len, word_b, &buf_len, 0, &prefix), error);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200661 break;
662 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200663 }
664
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200665 /* unexpected end of loop */
David Sedlákb3077192019-06-19 10:55:37 +0200666 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vaskob68ea142021-04-26 13:46:00 +0200667 ret = LY_EVALID;
668 goto error;
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200669
Michal Vasko7fbc8162018-09-17 10:35:16 +0200670str_end:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200671 /* terminating NULL byte for buf */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200672 if (*word_b) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200673 (*word_b) = ly_realloc(*word_b, (*word_len) + 1);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200674 LY_CHECK_ERR_RET(!(*word_b), LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200675 (*word_b)[*word_len] = '\0';
Michal Vasko7fbc8162018-09-17 10:35:16 +0200676 *word_p = *word_b;
677 }
678
679 return LY_SUCCESS;
Michal Vaskob68ea142021-04-26 13:46:00 +0200680
681error:
682 free(*word_b);
683 *word_b = NULL;
684 return ret;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200685}
686
Michal Vaskoea5abea2018-09-18 13:10:54 +0200687/**
688 * @brief Get another YANG keyword from the raw data.
689 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200690 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200691 * @param[out] kw YANG keyword read.
692 * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances.
693 * @param[out] word_len Length of the keyword in the data. Useful for extension instances.
694 *
695 * @return LY_ERR values.
696 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200697LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100698get_keyword(struct lys_yang_parser_ctx *ctx, enum ly_stmt *kw, char **word_p, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200699{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200700 uint8_t prefix;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200701 const char *word_start;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200702 size_t len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200703
704 if (word_p) {
705 *word_p = NULL;
706 *word_len = 0;
707 }
708
709 /* first skip "optsep", comments */
Radek Krejci33090f92020-12-17 20:12:46 +0100710 while (ctx->in->current[0]) {
711 switch (ctx->in->current[0]) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200712 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100713 if (ctx->in->current[1] == '/') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200714 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100715 MOVE_INPUT(ctx, 2);
716 LY_CHECK_RET(skip_comment(ctx, 1));
717 } else if (ctx->in->current[1] == '*') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200718 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100719 MOVE_INPUT(ctx, 2);
720 LY_CHECK_RET(skip_comment(ctx, 2));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200721 } else {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200722 /* error - not a comment after all, keyword cannot start with slash */
David Sedlákb3077192019-06-19 10:55:37 +0200723 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '/'.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200724 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200725 }
Radek Krejci13028282019-06-11 14:56:48 +0200726 continue;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200727 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200728 /* skip whitespaces (optsep) */
Radek Krejcidd713ce2021-01-04 23:12:12 +0100729 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200730 ctx->indent = 0;
731 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200732 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200733 /* skip whitespaces (optsep) */
734 ++ctx->indent;
735 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200736 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200737 /* skip whitespaces (optsep) */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100738 ctx->indent += Y_TAB_SPACES;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200739 break;
Michal Vasko50dcbc22021-03-25 12:21:20 +0100740 case '\r':
741 /* possible CRLF endline */
742 if (ctx->in->current[1] == '\n') {
743 break;
744 }
Radek Krejcid43298b2021-03-25 16:17:15 +0100745 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200746 default:
747 /* either a keyword start or an invalid character */
748 goto keyword_start;
749 }
750
Radek Krejci33090f92020-12-17 20:12:46 +0100751 ly_in_skip(ctx->in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200752 }
753
754keyword_start:
Radek Krejci33090f92020-12-17 20:12:46 +0100755 word_start = ctx->in->current;
Radek Krejcid54412f2020-12-17 20:25:35 +0100756 *kw = lysp_match_kw(ctx->in, &ctx->indent);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200757
aPiecek93582ed2021-05-25 14:49:06 +0200758 if (*kw == LY_STMT_SYNTAX_SEMICOLON) {
759 goto success;
760 } else if (*kw == LY_STMT_SYNTAX_LEFT_BRACE) {
761 ctx->depth++;
762 if (ctx->depth > LY_MAX_BLOCK_DEPTH) {
763 LOGERR(ctx->parsed_mod->mod->ctx, LY_EINVAL,
764 "The maximum number of block nestings has been exceeded.");
765 return LY_EINVAL;
766 }
767 goto success;
768 } else if (*kw == LY_STMT_SYNTAX_RIGHT_BRACE) {
769 ctx->depth--;
Radek Krejci626df482018-10-11 15:06:31 +0200770 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200771 }
772
Radek Krejcid6b76452019-09-03 17:03:03 +0200773 if (*kw != LY_STMT_NONE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200774 /* make sure we have the whole keyword */
Radek Krejci33090f92020-12-17 20:12:46 +0100775 switch (ctx->in->current[0]) {
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200776 case '\r':
777 if (ctx->in->current[1] != '\n') {
778 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
779 return LY_EVALID;
780 }
781 MOVE_INPUT(ctx, 1);
782 /* fallthrough */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200783 case '\n':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200784 case '\t':
Radek Krejciabdd8062019-06-11 16:44:19 +0200785 case ' ':
786 /* mandatory "sep" is just checked, not eaten so nothing in the context is updated */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200787 break;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200788 case ':':
789 /* keyword is not actually a keyword, but prefix of an extension.
790 * To avoid repeated check of the prefix syntax, move to the point where the colon was read
791 * and we will be checking the keyword (extension instance) itself */
792 prefix = 1;
Radek Krejci33090f92020-12-17 20:12:46 +0100793 MOVE_INPUT(ctx, 1);
Radek Krejci156ccaf2018-10-15 15:49:17 +0200794 goto extension;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200795 case '{':
796 /* allowed only for input and output statements which can be without arguments */
Michal Vasko69730152020-10-09 16:30:07 +0200797 if ((*kw == LY_STMT_INPUT) || (*kw == LY_STMT_OUTPUT)) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200798 break;
799 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100800 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200801 default:
Radek Krejci33090f92020-12-17 20:12:46 +0100802 MOVE_INPUT(ctx, 1);
803 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start,
Michal Vasko69730152020-10-09 16:30:07 +0200804 "a keyword followed by a separator");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200805 return LY_EVALID;
806 }
807 } else {
808 /* still can be an extension */
809 prefix = 0;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200810
Radek Krejci156ccaf2018-10-15 15:49:17 +0200811extension:
Radek Krejcid54412f2020-12-17 20:25:35 +0100812 while (ctx->in->current[0] && (ctx->in->current[0] != ' ') && (ctx->in->current[0] != '\t') &&
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200813 (ctx->in->current[0] != '\n') && (ctx->in->current[0] != '\r') && (ctx->in->current[0] != '{') &&
814 (ctx->in->current[0] != ';')) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200815 uint32_t c = 0;
816
Radek Krejci33090f92020-12-17 20:12:46 +0100817 LY_CHECK_ERR_RET(ly_getutf8(&ctx->in->current, &c, &len),
818 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[-len]), LY_EVALID);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200819 ++ctx->indent;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200820 /* check character validity */
Michal Vasko63f3d842020-07-08 10:10:14 +0200821 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c,
Radek Krejci33090f92020-12-17 20:12:46 +0100822 ctx->in->current - len == word_start ? 1 : 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200823 }
Radek Krejci33090f92020-12-17 20:12:46 +0100824 if (!ctx->in->current[0]) {
David Sedlákb3077192019-06-19 10:55:37 +0200825 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200826 return LY_EVALID;
827 }
828
829 /* prefix is mandatory for extension instances */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200830 if (prefix != 2) {
Radek Krejci33090f92020-12-17 20:12:46 +0100831 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start, "a keyword");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200832 return LY_EVALID;
833 }
834
Radek Krejcid6b76452019-09-03 17:03:03 +0200835 *kw = LY_STMT_EXTENSION_INSTANCE;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200836 }
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200837
Radek Krejci626df482018-10-11 15:06:31 +0200838success:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200839 if (word_p) {
840 *word_p = (char *)word_start;
Radek Krejci33090f92020-12-17 20:12:46 +0100841 *word_len = ctx->in->current - word_start;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200842 }
843
844 return LY_SUCCESS;
845}
846
Michal Vaskoea5abea2018-09-18 13:10:54 +0200847/**
848 * @brief Parse extension instance substatements.
849 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200850 * @param[in] ctx yang parser context for logging.
Radek Krejci335332a2019-09-05 13:03:35 +0200851 * @param[in] kw Statement keyword value matching @p word value.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200852 * @param[in] word Extension instance substatement name (keyword).
853 * @param[in] word_len Extension instance substatement name length.
854 * @param[in,out] child Children of this extension instance to add to.
855 *
856 * @return LY_ERR values.
857 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200858static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100859parse_ext_substmt(struct lys_yang_parser_ctx *ctx, enum ly_stmt kw, char *word, size_t word_len,
Radek Krejci0f969882020-08-21 16:56:47 +0200860 struct lysp_stmt **child)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200861{
862 char *buf;
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100863 LY_ERR ret = LY_SUCCESS;
Radek Krejci335332a2019-09-05 13:03:35 +0200864 enum ly_stmt child_kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200865 struct lysp_stmt *stmt, *par_child;
866
867 stmt = calloc(1, sizeof *stmt);
868 LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM);
869
Radek Krejcibb9b1982019-04-08 14:24:59 +0200870 /* insert into parent statements */
871 if (!*child) {
872 *child = stmt;
873 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +0200874 for (par_child = *child; par_child->next; par_child = par_child->next) {}
Radek Krejcibb9b1982019-04-08 14:24:59 +0200875 par_child->next = stmt;
876 }
877
Michal Vaskofc2cd072021-02-24 13:17:17 +0100878 /* statement */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200879 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), word, word_len, &stmt->stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200880
881 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100882 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, &stmt->flags, &word, &buf, &word_len));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200883 if (word) {
aPieceka24a2252021-05-07 10:52:31 +0200884 INSERT_WORD_RET(ctx, buf, stmt->arg, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200885 }
886
Radek Krejci8df109d2021-04-23 12:19:08 +0200887 stmt->format = LY_VALUE_SCHEMA;
Michal Vaskofc2cd072021-02-24 13:17:17 +0100888 stmt->prefix_data = ctx->parsed_mod;
889 stmt->kw = kw;
890
aPieceka24a2252021-05-07 10:52:31 +0200891 YANG_READ_SUBSTMT_FOR(ctx, child_kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Radek Krejci33090f92020-12-17 20:12:46 +0100892 LY_CHECK_RET(parse_ext_substmt(ctx, child_kw, word, word_len, &stmt->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200893 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200894 return ret;
895}
896
Michal Vaskoea5abea2018-09-18 13:10:54 +0200897/**
898 * @brief Parse extension instance.
899 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200900 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200901 * @param[in] ext_name Extension instance substatement name (keyword).
902 * @param[in] ext_name_len Extension instance substatement name length.
Radek Krejcifc596f92021-02-26 22:40:26 +0100903 * @param[in] insubstmt The statement this extension instance is a substatement of.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200904 * @param[in] insubstmt_index Index of the keyword instance this extension instance is a substatement of.
905 * @param[in,out] exts Extension instances to add to.
906 *
907 * @return LY_ERR values.
908 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200909static LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +0100910parse_ext(struct lys_yang_parser_ctx *ctx, const char *ext_name, size_t ext_name_len, enum ly_stmt insubstmt,
Radek Krejci0f969882020-08-21 16:56:47 +0200911 LY_ARRAY_COUNT_TYPE insubstmt_index, 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;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200916 struct lysp_ext_instance *e;
Radek Krejcid6b76452019-09-03 17:03:03 +0200917 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200918
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200919 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *exts, e, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200920
Michal Vaskofc2cd072021-02-24 13:17:17 +0100921 if (!ly_strnchr(ext_name, ':', ext_name_len)) {
922 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Extension instance \"%*.s\" without the mandatory prefix.", ext_name_len, ext_name);
923 return LY_EVALID;
924 }
925
926 /* store name */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200927 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), ext_name, ext_name_len, &e->name));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200928
929 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100930 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, NULL, &word, &buf, &word_len));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200931 if (word) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200932 INSERT_WORD_RET(ctx, buf, e->argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200933 }
934
Michal Vaskofc2cd072021-02-24 13:17:17 +0100935 /* store the rest of information */
Radek Krejci8df109d2021-04-23 12:19:08 +0200936 e->format = LY_VALUE_SCHEMA;
aPiecek60d9d672021-04-27 15:49:57 +0200937 e->parsed = NULL;
Michal Vaskofc2cd072021-02-24 13:17:17 +0100938 e->prefix_data = ctx->parsed_mod;
Radek Krejciab430862021-03-02 20:13:40 +0100939 e->parent_stmt = insubstmt;
940 e->parent_stmt_index = insubstmt_index;
Michal Vaskofc2cd072021-02-24 13:17:17 +0100941
aPieceka24a2252021-05-07 10:52:31 +0200942 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Radek Krejci33090f92020-12-17 20:12:46 +0100943 LY_CHECK_RET(parse_ext_substmt(ctx, kw, word, word_len, &e->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200944 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200945 return ret;
946}
947
Michal Vaskoea5abea2018-09-18 13:10:54 +0200948/**
949 * @brief Parse a generic text field without specific constraints. Those are contact, organization,
950 * description, etc...
951 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200952 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200953 * @param[in] substmt Type of this substatement.
954 * @param[in] substmt_index Index of this substatement.
955 * @param[in,out] value Place to store the parsed value.
956 * @param[in] arg Type of the YANG keyword argument (of the value).
957 * @param[in,out] exts Extension instances to add to.
958 *
959 * @return LY_ERR values.
960 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200961static LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +0100962parse_text_field(struct lys_yang_parser_ctx *ctx, enum ly_stmt substmt, uint32_t substmt_index,
Radek Krejci0f969882020-08-21 16:56:47 +0200963 const char **value, enum yang_arg arg, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200964{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100965 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200966 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200967 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200968 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200969
970 if (*value) {
Radek Krejci3972b332021-03-02 16:34:31 +0100971 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200972 return LY_EVALID;
973 }
974
975 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +0100976 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200977
978 /* store value and spend buf if allocated */
Radek Krejci011e4aa2020-09-04 15:22:31 +0200979 INSERT_WORD_RET(ctx, buf, *value, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200980
aPieceka24a2252021-05-07 10:52:31 +0200981 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200982 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200983 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +0100984 LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, substmt_index, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200985 break;
986 default:
Radek Krejci3972b332021-03-02 16:34:31 +0100987 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200988 return LY_EVALID;
989 }
990 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200991 return ret;
992}
993
Michal Vaskoea5abea2018-09-18 13:10:54 +0200994/**
995 * @brief Parse the yang-version statement.
996 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200997 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100998 * @param[out] version Storage for the parsed information.
Michal Vasko63f3d842020-07-08 10:10:14 +0200999 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001000 *
1001 * @return LY_ERR values.
1002 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001003static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001004parse_yangversion(struct lys_yang_parser_ctx *ctx, uint8_t *version, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001005{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001006 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001007 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001008 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001009 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001010
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001011 if (*version) {
David Sedlákb3077192019-06-19 10:55:37 +02001012 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001013 return LY_EVALID;
1014 }
1015
1016 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001017 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001018
Radek Krejci96e48da2020-09-04 13:18:06 +02001019 if ((word_len == 1) && !strncmp(word, "1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001020 *version = LYS_VERSION_1_0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001021 } else if ((word_len == ly_strlen_const("1.1")) && !strncmp(word, "1.1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001022 *version = LYS_VERSION_1_1;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001023 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001024 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001025 free(buf);
1026 return LY_EVALID;
1027 }
1028 free(buf);
1029
aPieceka24a2252021-05-07 10:52:31 +02001030 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001031 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001032 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001033 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_YANG_VERSION, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001034 break;
1035 default:
David Sedlákb3077192019-06-19 10:55:37 +02001036 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001037 return LY_EVALID;
1038 }
1039 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001040 return ret;
1041}
1042
Michal Vaskoea5abea2018-09-18 13:10:54 +02001043/**
1044 * @brief Parse the belongs-to statement.
1045 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001046 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001047 * @param[in,out] prefix Place to store the parsed belongs-to prefix value.
1048 * @param[in,out] exts Extension instances to add to.
1049 *
1050 * @return LY_ERR values.
1051 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001052static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001053parse_belongsto(struct lys_yang_parser_ctx *ctx, const char **prefix, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001054{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001055 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001056 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001057 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001058 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001059
Michal Vaskoc3781c32020-10-06 14:04:08 +02001060 if (*prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001061 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001062 return LY_EVALID;
1063 }
1064
Michal Vaskoc3781c32020-10-06 14:04:08 +02001065 /* get value, it must match the main module */
Radek Krejci33090f92020-12-17 20:12:46 +01001066 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001067 if (ly_strncmp(ctx->parsed_mod->mod->name, word, word_len)) {
Michal Vaskoc3781c32020-10-06 14:04:08 +02001068 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 +02001069 (int)word_len, word, ctx->parsed_mod->mod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02001070 free(buf);
1071 return LY_EVALID;
1072 }
1073 free(buf);
Radek Krejcif09e4e82019-06-14 15:08:11 +02001074
aPieceka24a2252021-05-07 10:52:31 +02001075 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001076 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001077 case LY_STMT_PREFIX:
Radek Krejcifc596f92021-02-26 22:40:26 +01001078 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_PREFIX, 0, prefix, Y_IDENTIF_ARG, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001079 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001080 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001081 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_BELONGS_TO, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001082 break;
1083 default:
David Sedlákb3077192019-06-19 10:55:37 +02001084 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001085 return LY_EVALID;
1086 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001087 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001088 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001089checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001090 /* mandatory substatements */
1091 if (!*prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001092 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001093 return LY_EVALID;
1094 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001095 return ret;
1096}
1097
Michal Vaskoea5abea2018-09-18 13:10:54 +02001098/**
1099 * @brief Parse the revision-date statement.
1100 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001101 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001102 * @param[in,out] rev Array to store the parsed value in.
1103 * @param[in,out] exts Extension instances to add to.
1104 *
1105 * @return LY_ERR values.
1106 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001107static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001108parse_revisiondate(struct lys_yang_parser_ctx *ctx, char *rev, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001109{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001110 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001111 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001112 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001113 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001114
1115 if (rev[0]) {
David Sedlákb3077192019-06-19 10:55:37 +02001116 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001117 return LY_EVALID;
1118 }
1119
1120 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001121 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001122
1123 /* check value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001124 if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision-date")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001125 free(buf);
1126 return LY_EVALID;
1127 }
1128
1129 /* store value and spend buf if allocated */
1130 strncpy(rev, word, word_len);
1131 free(buf);
1132
aPieceka24a2252021-05-07 10:52:31 +02001133 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001134 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001135 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001136 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_REVISION_DATE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001137 break;
1138 default:
David Sedlákb3077192019-06-19 10:55:37 +02001139 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001140 return LY_EVALID;
1141 }
1142 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001143 return ret;
1144}
1145
Michal Vaskoea5abea2018-09-18 13:10:54 +02001146/**
1147 * @brief Parse the include statement.
1148 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001149 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001150 * @param[in] module_name Name of the module to check name collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001151 * @param[in,out] includes Parsed includes to add to.
1152 *
1153 * @return LY_ERR values.
1154 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001155static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001156parse_include(struct lys_yang_parser_ctx *ctx, const char *module_name, struct lysp_include **includes)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001157{
Radek Krejcid33273d2018-10-25 14:55:52 +02001158 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001159 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001160 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001161 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001162 struct lysp_include *inc;
1163
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001164 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *includes, inc, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001165
1166 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001167 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001168
Radek Krejci011e4aa2020-09-04 15:22:31 +02001169 INSERT_WORD_RET(ctx, buf, inc->name, word, word_len);
Radek Krejci086c7132018-10-26 15:29:04 +02001170
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001171 /* submodules share the namespace with the module names, so there must not be
1172 * a module of the same name in the context, no need for revision matching */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001173 if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(PARSER_CTX(ctx), inc->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01001174 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", inc->name);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001175 return LY_EVALID;
1176 }
1177
aPieceka24a2252021-05-07 10:52:31 +02001178 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001179 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001180 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001181 PARSER_CHECK_STMTVER2_RET(ctx, "description", "include");
Radek Krejcifc596f92021-02-26 22:40:26 +01001182 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &inc->dsc, Y_STR_ARG, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001183 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001184 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001185 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "include");
Radek Krejcifc596f92021-02-26 22:40:26 +01001186 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &inc->ref, Y_STR_ARG, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001187 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001188 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001189 LY_CHECK_RET(parse_revisiondate(ctx, inc->rev, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001190 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001191 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001192 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_INCLUDE, 0, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001193 break;
1194 default:
David Sedlákb3077192019-06-19 10:55:37 +02001195 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001196 return LY_EVALID;
1197 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001198 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001199 return ret;
1200}
1201
Michal Vaskoea5abea2018-09-18 13:10:54 +02001202/**
1203 * @brief Parse the import statement.
1204 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001205 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001206 * @param[in] module_prefix Prefix of the module to check prefix collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001207 * @param[in,out] imports Parsed imports to add to.
1208 *
1209 * @return LY_ERR values.
1210 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001211static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001212parse_import(struct lys_yang_parser_ctx *ctx, const char *module_prefix, struct lysp_import **imports)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001213{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001214 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001215 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001216 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001217 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001218 struct lysp_import *imp;
1219
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001220 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *imports, imp, LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001221
1222 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001223 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02001224 INSERT_WORD_RET(ctx, buf, imp->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001225
aPieceka24a2252021-05-07 10:52:31 +02001226 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001227 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001228 case LY_STMT_PREFIX:
Radek Krejcifc596f92021-02-26 22:40:26 +01001229 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_PREFIX, 0, &imp->prefix, Y_IDENTIF_ARG, &imp->exts));
Michal Vaskob36053d2020-03-26 15:49:30 +01001230 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 +02001231 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001232 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001233 PARSER_CHECK_STMTVER2_RET(ctx, "description", "import");
Radek Krejcifc596f92021-02-26 22:40:26 +01001234 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &imp->dsc, Y_STR_ARG, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001235 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001236 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001237 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "import");
Radek Krejcifc596f92021-02-26 22:40:26 +01001238 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &imp->ref, Y_STR_ARG, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001239 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001240 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001241 LY_CHECK_RET(parse_revisiondate(ctx, imp->rev, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001242 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001243 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001244 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_IMPORT, 0, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001245 break;
1246 default:
David Sedlákb3077192019-06-19 10:55:37 +02001247 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001248 return LY_EVALID;
1249 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001250 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001251 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001252checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001253 /* mandatory substatements */
David Sedlákb3077192019-06-19 10:55:37 +02001254 LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001255
1256 return ret;
1257}
1258
Michal Vaskoea5abea2018-09-18 13:10:54 +02001259/**
1260 * @brief Parse the revision statement.
1261 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001262 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001263 * @param[in,out] revs Parsed revisions to add to.
1264 *
1265 * @return LY_ERR values.
1266 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001267static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001268parse_revision(struct lys_yang_parser_ctx *ctx, struct lysp_revision **revs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001269{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001270 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001271 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001272 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001273 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001274 struct lysp_revision *rev;
1275
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001276 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *revs, rev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001277
1278 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001279 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001280
1281 /* check value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001282 if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision")) {
David Sedlák68ef3dc2019-07-22 13:40:19 +02001283 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001284 return LY_EVALID;
1285 }
1286
Radek Krejcib7db73a2018-10-24 14:18:40 +02001287 strncpy(rev->date, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001288 free(buf);
1289
aPieceka24a2252021-05-07 10:52:31 +02001290 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001291 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001292 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001293 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &rev->dsc, Y_STR_ARG, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001294 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001295 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001296 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &rev->ref, Y_STR_ARG, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001297 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001298 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001299 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_REVISION, 0, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001300 break;
1301 default:
David Sedlákb3077192019-06-19 10:55:37 +02001302 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001303 return LY_EVALID;
1304 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001305 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001306 return ret;
1307}
1308
Michal Vaskoea5abea2018-09-18 13:10:54 +02001309/**
1310 * @brief Parse a generic text field that can have more instances such as base.
1311 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001312 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001313 * @param[in] substmt Type of this substatement.
1314 * @param[in,out] texts Parsed values to add to.
1315 * @param[in] arg Type of the expected argument.
1316 * @param[in,out] exts Extension instances to add to.
1317 *
1318 * @return LY_ERR values.
1319 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001320static LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +01001321parse_text_fields(struct lys_yang_parser_ctx *ctx, enum ly_stmt substmt, const char ***texts, enum yang_arg arg,
Radek Krejci0f969882020-08-21 16:56:47 +02001322 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001323{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001324 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001325 char *buf, *word;
Radek Krejci151a5b72018-10-19 14:21:44 +02001326 const char **item;
1327 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001328 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001329
1330 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001331 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *texts, item, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001332
1333 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001334 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001335
Radek Krejci011e4aa2020-09-04 15:22:31 +02001336 INSERT_WORD_RET(ctx, buf, *item, word, word_len);
aPieceka24a2252021-05-07 10:52:31 +02001337 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001338 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001339 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001340 LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, LY_ARRAY_COUNT(*texts) - 1, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001341 break;
1342 default:
Radek Krejci3972b332021-03-02 16:34:31 +01001343 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001344 return LY_EVALID;
1345 }
1346 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001347 return ret;
1348}
1349
Michal Vaskoea5abea2018-09-18 13:10:54 +02001350/**
Michal Vasko7f45cf22020-10-01 12:49:44 +02001351 * @brief Parse a generic text field that can have more instances such as base.
1352 *
1353 * @param[in] ctx yang parser context for logging.
Michal Vasko7f45cf22020-10-01 12:49:44 +02001354 * @param[in] substmt Type of this substatement.
1355 * @param[in,out] qnames Parsed qnames to add to.
1356 * @param[in] arg Type of the expected argument.
1357 * @param[in,out] exts Extension instances to add to.
1358 *
1359 * @return LY_ERR values.
1360 */
1361static LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +01001362parse_qnames(struct lys_yang_parser_ctx *ctx, enum ly_stmt substmt, struct lysp_qname **qnames,
Michal Vasko7f45cf22020-10-01 12:49:44 +02001363 enum yang_arg arg, struct lysp_ext_instance **exts)
1364{
1365 LY_ERR ret = LY_SUCCESS;
1366 char *buf, *word;
1367 struct lysp_qname *item;
1368 size_t word_len;
1369 enum ly_stmt kw;
1370
1371 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001372 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *qnames, item, LY_EMEM);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001373
1374 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001375 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001376
1377 INSERT_WORD_RET(ctx, buf, item->str, word, word_len);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001378 item->mod = ctx->parsed_mod;
aPieceka24a2252021-05-07 10:52:31 +02001379 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02001380 switch (kw) {
1381 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001382 LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, LY_ARRAY_COUNT(*qnames) - 1, exts));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001383 break;
1384 default:
Radek Krejci3972b332021-03-02 16:34:31 +01001385 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(substmt));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001386 return LY_EVALID;
1387 }
1388 }
1389 return ret;
1390}
1391
1392/**
Michal Vaskoea5abea2018-09-18 13:10:54 +02001393 * @brief Parse the config statement.
1394 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001395 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001396 * @param[in,out] flags Flags to add to.
1397 * @param[in,out] exts Extension instances to add to.
1398 *
1399 * @return LY_ERR values.
1400 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001401static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001402parse_config(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001403{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001404 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001405 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001406 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001407 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001408
1409 if (*flags & LYS_CONFIG_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001410 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001411 return LY_EVALID;
1412 }
1413
1414 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001415 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001416
Radek Krejcif13b87b2020-12-01 22:02:17 +01001417 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001418 *flags |= LYS_CONFIG_W;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001419 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001420 *flags |= LYS_CONFIG_R;
1421 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001422 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001423 free(buf);
1424 return LY_EVALID;
1425 }
1426 free(buf);
1427
aPieceka24a2252021-05-07 10:52:31 +02001428 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001429 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001430 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001431 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_CONFIG, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001432 break;
1433 default:
David Sedlákb3077192019-06-19 10:55:37 +02001434 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001435 return LY_EVALID;
1436 }
1437 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001438 return ret;
1439}
1440
Michal Vaskoea5abea2018-09-18 13:10:54 +02001441/**
1442 * @brief Parse the mandatory statement.
1443 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001444 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001445 * @param[in,out] flags Flags to add to.
1446 * @param[in,out] exts Extension instances to add to.
1447 *
1448 * @return LY_ERR values.
1449 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001450static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001451parse_mandatory(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001452{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001453 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001454 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001455 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001456 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001457
1458 if (*flags & LYS_MAND_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001459 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001460 return LY_EVALID;
1461 }
1462
1463 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001464 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001465
Radek Krejcif13b87b2020-12-01 22:02:17 +01001466 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001467 *flags |= LYS_MAND_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001468 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001469 *flags |= LYS_MAND_FALSE;
1470 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001471 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001472 free(buf);
1473 return LY_EVALID;
1474 }
1475 free(buf);
1476
aPieceka24a2252021-05-07 10:52:31 +02001477 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001478 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001479 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001480 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MANDATORY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001481 break;
1482 default:
David Sedlákb3077192019-06-19 10:55:37 +02001483 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001484 return LY_EVALID;
1485 }
1486 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001487 return ret;
1488}
1489
Michal Vaskoea5abea2018-09-18 13:10:54 +02001490/**
1491 * @brief Parse a restriction such as range or length.
1492 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001493 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001494 * @param[in] restr_kw Type of this particular restriction.
1495 * @param[in,out] exts Extension instances to add to.
1496 *
1497 * @return LY_ERR values.
1498 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001499static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001500parse_restr(struct lys_yang_parser_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr *restr)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001501{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001502 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001503 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001504 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001505 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001506
1507 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001508 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001509
Michal Vaskob36053d2020-03-26 15:49:30 +01001510 CHECK_NONEMPTY(ctx, word_len, ly_stmt2str(restr_kw));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001511 INSERT_WORD_RET(ctx, buf, restr->arg.str, word, word_len);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001512 restr->arg.mod = ctx->parsed_mod;
aPieceka24a2252021-05-07 10:52:31 +02001513 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001514 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001515 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001516 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001517 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001518 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001519 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001520 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001521 case LY_STMT_ERROR_APP_TAG:
Radek Krejcifc596f92021-02-26 22:40:26 +01001522 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_ERROR_APP_TAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001523 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001524 case LY_STMT_ERROR_MESSAGE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001525 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_ERROR_MESSAGE, 0, &restr->emsg, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001526 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001527 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001528 LY_CHECK_RET(parse_ext(ctx, word, word_len, restr_kw, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001529 break;
1530 default:
David Sedlákb3077192019-06-19 10:55:37 +02001531 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001532 return LY_EVALID;
1533 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001534 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001535 return ret;
1536}
1537
Michal Vaskoea5abea2018-09-18 13:10:54 +02001538/**
1539 * @brief Parse a restriction that can have more instances such as must.
1540 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001541 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001542 * @param[in] restr_kw Type of this particular restriction.
1543 * @param[in,out] restrs Restrictions to add to.
1544 *
1545 * @return LY_ERR values.
1546 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001547static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001548parse_restrs(struct lys_yang_parser_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr **restrs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001549{
1550 struct lysp_restr *restr;
1551
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001552 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *restrs, restr, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01001553 return parse_restr(ctx, restr_kw, restr);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001554}
1555
Michal Vaskoea5abea2018-09-18 13:10:54 +02001556/**
1557 * @brief Parse the status statement.
1558 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001559 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001560 * @param[in,out] flags Flags to add to.
1561 * @param[in,out] exts Extension instances to add to.
1562 *
1563 * @return LY_ERR values.
1564 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001565static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001566parse_status(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001567{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001568 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001569 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001570 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001571 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001572
1573 if (*flags & LYS_STATUS_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001574 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001575 return LY_EVALID;
1576 }
1577
1578 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001579 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001580
Radek Krejcif13b87b2020-12-01 22:02:17 +01001581 if ((word_len == ly_strlen_const("current")) && !strncmp(word, "current", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001582 *flags |= LYS_STATUS_CURR;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001583 } else if ((word_len == ly_strlen_const("deprecated")) && !strncmp(word, "deprecated", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001584 *flags |= LYS_STATUS_DEPRC;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001585 } else if ((word_len == ly_strlen_const("obsolete")) && !strncmp(word, "obsolete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001586 *flags |= LYS_STATUS_OBSLT;
1587 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001588 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001589 free(buf);
1590 return LY_EVALID;
1591 }
1592 free(buf);
1593
aPieceka24a2252021-05-07 10:52:31 +02001594 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001595 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001596 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001597 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_STATUS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001598 break;
1599 default:
David Sedlákb3077192019-06-19 10:55:37 +02001600 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001601 return LY_EVALID;
1602 }
1603 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001604 return ret;
1605}
1606
Michal Vaskoea5abea2018-09-18 13:10:54 +02001607/**
1608 * @brief Parse the when statement.
1609 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001610 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001611 * @param[in,out] when_p When pointer to parse to.
1612 *
1613 * @return LY_ERR values.
1614 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001615LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001616parse_when(struct lys_yang_parser_ctx *ctx, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001617{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001618 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001619 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001620 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001621 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001622 struct lysp_when *when;
1623
1624 if (*when_p) {
David Sedlákb3077192019-06-19 10:55:37 +02001625 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001626 return LY_EVALID;
1627 }
1628
1629 when = calloc(1, sizeof *when);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001630 LY_CHECK_ERR_RET(!when, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001631 *when_p = when;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001632
1633 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001634 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01001635 CHECK_NONEMPTY(ctx, word_len, "when");
Radek Krejci011e4aa2020-09-04 15:22:31 +02001636 INSERT_WORD_RET(ctx, buf, when->cond, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001637
aPieceka24a2252021-05-07 10:52:31 +02001638 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001639 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001640 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001641 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &when->dsc, Y_STR_ARG, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001642 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001643 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001644 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &when->ref, Y_STR_ARG, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001645 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001646 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001647 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_WHEN, 0, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001648 break;
1649 default:
David Sedlákb3077192019-06-19 10:55:37 +02001650 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001651 return LY_EVALID;
1652 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001653 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001654 return ret;
1655}
1656
Michal Vaskoea5abea2018-09-18 13:10:54 +02001657/**
1658 * @brief Parse the anydata or anyxml statement.
1659 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001660 * @param[in] ctx yang parser context for logging.
Radek Krejci39b7fc22021-02-26 23:29:18 +01001661 * @param[in] any_kw Type of this particular keyword.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001662 * @param[in,out] siblings Siblings to add to.
1663 *
1664 * @return LY_ERR values.
1665 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02001666LY_ERR
Radek Krejci39b7fc22021-02-26 23:29:18 +01001667parse_any(struct lys_yang_parser_ctx *ctx, enum ly_stmt any_kw, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001668{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001669 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001670 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001671 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001672 struct lysp_node_anydata *any;
Radek Krejci39b7fc22021-02-26 23:29:18 +01001673 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001674
David Sedlák60adc092019-08-06 15:57:02 +02001675 /* create new structure and insert into siblings */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001676 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, any, next, LY_EMEM);
David Sedlák60adc092019-08-06 15:57:02 +02001677
Radek Krejci39b7fc22021-02-26 23:29:18 +01001678 any->nodetype = any_kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001679 any->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001680
Michal Vasko7fbc8162018-09-17 10:35:16 +02001681 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01001682 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02001683 INSERT_WORD_RET(ctx, buf, any->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001684
1685 /* parse substatements */
aPieceka24a2252021-05-07 10:52:31 +02001686 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001687 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001688 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01001689 LY_CHECK_RET(parse_config(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001690 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001691 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001692 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &any->dsc, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001693 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001694 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001695 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &any->iffeatures, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001696 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001697 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01001698 LY_CHECK_RET(parse_mandatory(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001699 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001700 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01001701 LY_CHECK_RET(parse_restrs(ctx, kw, &any->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001702 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001703 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001704 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &any->ref, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001705 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001706 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001707 LY_CHECK_RET(parse_status(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001708 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001709 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01001710 LY_CHECK_RET(parse_when(ctx, &any->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001711 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001712 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001713 LY_CHECK_RET(parse_ext(ctx, word, word_len, any_kw, 0, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001714 break;
1715 default:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001716 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(any_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001717 return LY_EVALID;
1718 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001719 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001720 return ret;
1721}
1722
Michal Vaskoea5abea2018-09-18 13:10:54 +02001723/**
1724 * @brief Parse the value or position statement. Substatement of type enum statement.
1725 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001726 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001727 * @param[in] val_kw Type of this particular keyword.
1728 * @param[in,out] value Value to write to.
1729 * @param[in,out] flags Flags to write to.
1730 * @param[in,out] exts Extension instances to add to.
1731 *
1732 * @return LY_ERR values.
1733 */
David Sedlákd6ce6d72019-07-16 17:30:18 +02001734LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001735parse_type_enum_value_pos(struct lys_yang_parser_ctx *ctx, enum ly_stmt val_kw, int64_t *value, uint16_t *flags,
Radek Krejci0f969882020-08-21 16:56:47 +02001736 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001737{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001738 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001739 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001740 size_t word_len;
Michal Vasko73d77ab2021-07-23 12:45:55 +02001741 long long int num = 0;
1742 unsigned long long int unum = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001743 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001744
1745 if (*flags & LYS_SET_VALUE) {
David Sedlákb3077192019-06-19 10:55:37 +02001746 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001747 return LY_EVALID;
1748 }
1749 *flags |= LYS_SET_VALUE;
1750
1751 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001752 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001753
Radek Krejcid6b76452019-09-03 17:03:03 +02001754 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 +02001755 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001756 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001757 }
1758
1759 errno = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001760 if (val_kw == LY_STMT_VALUE) {
Michal Vasko73d77ab2021-07-23 12:45:55 +02001761 num = strtoll(word, &ptr, LY_BASE_DEC);
Michal Vasko69730152020-10-09 16:30:07 +02001762 if ((num < INT64_C(-2147483648)) || (num > INT64_C(2147483647))) {
David Sedlákb3077192019-06-19 10:55:37 +02001763 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001764 goto error;
1765 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001766 } else {
Michal Vasko73d77ab2021-07-23 12:45:55 +02001767 unum = strtoull(word, &ptr, LY_BASE_DEC);
Radek Krejci8b764662018-11-14 14:15:13 +01001768 if (unum > UINT64_C(4294967295)) {
David Sedlákb3077192019-06-19 10:55:37 +02001769 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001770 goto error;
1771 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001772 }
1773 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001774 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001775 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001776 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001777 }
1778 if (errno == ERANGE) {
David Sedlákb3077192019-06-19 10:55:37 +02001779 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001780 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001781 }
Radek Krejcid6b76452019-09-03 17:03:03 +02001782 if (val_kw == LY_STMT_VALUE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001783 *value = num;
1784 } else {
1785 *value = unum;
1786 }
1787 free(buf);
1788
aPieceka24a2252021-05-07 10:52:31 +02001789 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001790 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001791 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001792 LY_CHECK_RET(parse_ext(ctx, word, word_len, val_kw == LY_STMT_VALUE ? LY_STMT_VALUE : LY_STMT_POSITION, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001793 break;
1794 default:
David Sedlákb3077192019-06-19 10:55:37 +02001795 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001796 return LY_EVALID;
1797 }
1798 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001799 return ret;
Radek Krejci8b764662018-11-14 14:15:13 +01001800
1801error:
1802 free(buf);
1803 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001804}
1805
Michal Vaskoea5abea2018-09-18 13:10:54 +02001806/**
1807 * @brief Parse the enum or bit statement. Substatement of type statement.
1808 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001809 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001810 * @param[in] enum_kw Type of this particular keyword.
1811 * @param[in,out] enums Enums or bits to add to.
1812 *
1813 * @return LY_ERR values.
1814 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001815static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001816parse_type_enum(struct lys_yang_parser_ctx *ctx, enum ly_stmt enum_kw, struct lysp_type_enum **enums)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001817{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001818 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001819 char *buf, *word;
David Sedlák6544c182019-07-12 13:17:33 +02001820 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001821 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001822 struct lysp_type_enum *enm;
1823
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001824 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *enums, enm, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001825
1826 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001827 LY_CHECK_RET(get_argument(ctx, enum_kw == LY_STMT_ENUM ? Y_STR_ARG : Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejcid6b76452019-09-03 17:03:03 +02001828 if (enum_kw == LY_STMT_ENUM) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001829 ret = lysp_check_enum_name((struct lys_parser_ctx *)ctx, (const char *)word, word_len);
David Sedlák6544c182019-07-12 13:17:33 +02001830 LY_CHECK_ERR_RET(ret, free(buf), ret);
Radek Krejci335332a2019-09-05 13:03:35 +02001831 } /* else nothing specific for YANG_BIT */
Radek Krejci8b764662018-11-14 14:15:13 +01001832
Radek Krejci011e4aa2020-09-04 15:22:31 +02001833 INSERT_WORD_RET(ctx, buf, enm->name, word, word_len);
Radek Krejci8b764662018-11-14 14:15:13 +01001834 CHECK_UNIQUENESS(ctx, *enums, name, ly_stmt2str(enum_kw), enm->name);
1835
aPieceka24a2252021-05-07 10:52:31 +02001836 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001837 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001838 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001839 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &enm->dsc, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001840 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001841 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02001842 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", ly_stmt2str(enum_kw));
Radek Krejcifc596f92021-02-26 22:40:26 +01001843 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &enm->iffeatures, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001844 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001845 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001846 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &enm->ref, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001847 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001848 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001849 LY_CHECK_RET(parse_status(ctx, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001850 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001851 case LY_STMT_VALUE:
1852 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 +02001853 ly_stmt2str(enum_kw)), LY_EVALID);
Radek Krejci33090f92020-12-17 20:12:46 +01001854 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, &enm->value, &enm->flags, &enm->exts));
David Sedlák9fb515f2019-07-11 10:33:58 +02001855 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001856 case LY_STMT_POSITION:
1857 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 +02001858 ly_stmt2str(enum_kw)), LY_EVALID);
Radek Krejci33090f92020-12-17 20:12:46 +01001859 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, &enm->value, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001860 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001861 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001862 LY_CHECK_RET(parse_ext(ctx, word, word_len, enum_kw, 0, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001863 break;
1864 default:
David Sedlákb3077192019-06-19 10:55:37 +02001865 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001866 return LY_EVALID;
1867 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001868 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001869 return ret;
1870}
1871
Michal Vaskoea5abea2018-09-18 13:10:54 +02001872/**
1873 * @brief Parse the fraction-digits statement. Substatement of type statement.
1874 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001875 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001876 * @param[in,out] fracdig Value to write to.
1877 * @param[in,out] exts Extension instances to add to.
1878 *
1879 * @return LY_ERR values.
1880 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001881static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001882parse_type_fracdigits(struct lys_yang_parser_ctx *ctx, uint8_t *fracdig, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001883{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001884 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001885 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001886 size_t word_len;
Michal Vasko73d77ab2021-07-23 12:45:55 +02001887 unsigned long long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02001888 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001889
1890 if (*fracdig) {
David Sedlákb3077192019-06-19 10:55:37 +02001891 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001892 return LY_EVALID;
1893 }
1894
1895 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001896 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001897
1898 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
David Sedlákb3077192019-06-19 10:55:37 +02001899 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001900 free(buf);
1901 return LY_EVALID;
1902 }
1903
1904 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02001905 num = strtoull(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001906 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001907 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001908 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001909 free(buf);
1910 return LY_EVALID;
1911 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001912 if ((errno == ERANGE) || (num > LY_TYPE_DEC64_FD_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02001913 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001914 free(buf);
1915 return LY_EVALID;
1916 }
1917 *fracdig = num;
1918 free(buf);
1919
aPieceka24a2252021-05-07 10:52:31 +02001920 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001921 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001922 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001923 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_FRACTION_DIGITS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001924 break;
1925 default:
David Sedlákb3077192019-06-19 10:55:37 +02001926 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001927 return LY_EVALID;
1928 }
1929 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001930 return ret;
1931}
1932
Michal Vaskoea5abea2018-09-18 13:10:54 +02001933/**
1934 * @brief Parse the require-instance statement. Substatement of type statement.
1935 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001936 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001937 * @param[in,out] reqinst Value to write to.
1938 * @param[in,out] flags Flags to write to.
1939 * @param[in,out] exts Extension instances to add to.
1940 *
1941 * @return LY_ERR values.
1942 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001943static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001944parse_type_reqinstance(struct lys_yang_parser_ctx *ctx, uint8_t *reqinst, uint16_t *flags,
Radek Krejci0f969882020-08-21 16:56:47 +02001945 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001946{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001947 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001948 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001949 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001950 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001951
1952 if (*flags & LYS_SET_REQINST) {
David Sedlákb3077192019-06-19 10:55:37 +02001953 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001954 return LY_EVALID;
1955 }
1956 *flags |= LYS_SET_REQINST;
1957
1958 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001959 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001960
Radek Krejcif13b87b2020-12-01 22:02:17 +01001961 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001962 *reqinst = 1;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001963 } else if ((word_len != ly_strlen_const("false")) || strncmp(word, "false", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001964 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001965 free(buf);
1966 return LY_EVALID;
1967 }
1968 free(buf);
1969
aPieceka24a2252021-05-07 10:52:31 +02001970 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001971 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001972 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001973 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_REQUIRE_INSTANCE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001974 break;
1975 default:
David Sedlákb3077192019-06-19 10:55:37 +02001976 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001977 return LY_EVALID;
1978 }
1979 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001980 return ret;
1981}
1982
Michal Vaskoea5abea2018-09-18 13:10:54 +02001983/**
1984 * @brief Parse the modifier statement. Substatement of type pattern statement.
1985 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001986 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001987 * @param[in,out] pat Value to write to.
1988 * @param[in,out] exts Extension instances to add to.
1989 *
1990 * @return LY_ERR values.
1991 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001992static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001993parse_type_pattern_modifier(struct lys_yang_parser_ctx *ctx, const char **pat, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001994{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001995 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001996 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001997 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001998 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001999
Radek Krejcif13b87b2020-12-01 22:02:17 +01002000 if ((*pat)[0] == LYSP_RESTR_PATTERN_NACK) {
David Sedlákb3077192019-06-19 10:55:37 +02002001 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002002 return LY_EVALID;
2003 }
2004
2005 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002006 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002007
Radek Krejcif13b87b2020-12-01 22:02:17 +01002008 if ((word_len != ly_strlen_const("invert-match")) || strncmp(word, "invert-match", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02002009 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002010 free(buf);
2011 return LY_EVALID;
2012 }
2013 free(buf);
2014
2015 /* replace the value in the dictionary */
2016 buf = malloc(strlen(*pat) + 1);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002017 LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002018 strcpy(buf, *pat);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002019 lydict_remove(PARSER_CTX(ctx), *pat);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002020
Radek Krejcif13b87b2020-12-01 22:02:17 +01002021 assert(buf[0] == LYSP_RESTR_PATTERN_ACK);
2022 buf[0] = LYSP_RESTR_PATTERN_NACK;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002023 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, pat));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002024
aPieceka24a2252021-05-07 10:52:31 +02002025 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002026 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002027 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002028 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MODIFIER, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002029 break;
2030 default:
David Sedlákb3077192019-06-19 10:55:37 +02002031 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002032 return LY_EVALID;
2033 }
2034 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002035 return ret;
2036}
2037
Michal Vaskoea5abea2018-09-18 13:10:54 +02002038/**
2039 * @brief Parse the pattern statement. Substatement of type statement.
2040 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002041 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002042 * @param[in,out] patterns Restrictions to add to.
2043 *
2044 * @return LY_ERR values.
2045 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002046static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002047parse_type_pattern(struct lys_yang_parser_ctx *ctx, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002048{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002049 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002050 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002051 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002052 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002053 struct lysp_restr *restr;
2054
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002055 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *patterns, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002056
2057 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002058 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002059
Michal Vasko55a16b92021-09-15 08:51:35 +02002060 /* empty pattern is not valid */
2061 if (!strlen(word)) {
2062 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "pattern");
2063 return LY_EVALID;
2064 }
2065
Michal Vasko7fbc8162018-09-17 10:35:16 +02002066 /* add special meaning first byte */
2067 if (buf) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01002068 buf = ly_realloc(buf, word_len + 2);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002069 word = buf;
2070 } else {
2071 buf = malloc(word_len + 2);
2072 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002073 LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko54720192021-06-11 13:55:59 +02002074 if (word_len) {
2075 memmove(buf + 1, word, word_len);
2076 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01002077 buf[0] = LYSP_RESTR_PATTERN_ACK; /* pattern's default regular-match flag */
Radek Krejci86d106e2018-10-18 09:53:19 +02002078 buf[word_len + 1] = '\0'; /* terminating NULL byte */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002079 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, &restr->arg.str));
2080 restr->arg.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002081
aPieceka24a2252021-05-07 10:52:31 +02002082 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002083 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002084 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002085 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002086 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002087 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002088 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002089 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002090 case LY_STMT_ERROR_APP_TAG:
Radek Krejcifc596f92021-02-26 22:40:26 +01002091 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_ERROR_APP_TAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002092 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002093 case LY_STMT_ERROR_MESSAGE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002094 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_ERROR_MESSAGE, 0, &restr->emsg, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002095 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002096 case LY_STMT_MODIFIER:
Radek Krejci335332a2019-09-05 13:03:35 +02002097 PARSER_CHECK_STMTVER2_RET(ctx, "modifier", "pattern");
Radek Krejci33090f92020-12-17 20:12:46 +01002098 LY_CHECK_RET(parse_type_pattern_modifier(ctx, &restr->arg.str, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002099 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002100 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002101 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_PATTERN, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002102 break;
2103 default:
David Sedlákb3077192019-06-19 10:55:37 +02002104 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002105 return LY_EVALID;
2106 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002107 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002108 return ret;
2109}
2110
Michal Vaskoea5abea2018-09-18 13:10:54 +02002111/**
2112 * @brief Parse the type statement.
2113 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002114 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002115 * @param[in,out] type Type to wrote to.
2116 *
2117 * @return LY_ERR values.
2118 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002119static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002120parse_type(struct lys_yang_parser_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002121{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002122 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002123 char *buf, *word;
Michal Vasko004d3152020-06-11 19:59:22 +02002124 const char *str_path = NULL;
Radek Krejciefd22f62018-09-27 11:47:58 +02002125 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002126 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002127 struct lysp_type *nest_type;
2128
2129 if (type->name) {
David Sedlákb3077192019-06-19 10:55:37 +02002130 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002131 return LY_EVALID;
2132 }
2133
2134 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002135 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002136 INSERT_WORD_RET(ctx, buf, type->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002137
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002138 /* set module */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002139 type->pmod = ctx->parsed_mod;
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002140
aPieceka24a2252021-05-07 10:52:31 +02002141 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002142 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002143 case LY_STMT_BASE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002144 LY_CHECK_RET(parse_text_fields(ctx, LY_STMT_BASE, &type->bases, Y_PREF_IDENTIF_ARG, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002145 type->flags |= LYS_SET_BASE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002146 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002147 case LY_STMT_BIT:
Radek Krejci33090f92020-12-17 20:12:46 +01002148 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->bits));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002149 type->flags |= LYS_SET_BIT;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002150 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002151 case LY_STMT_ENUM:
Radek Krejci33090f92020-12-17 20:12:46 +01002152 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->enums));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002153 type->flags |= LYS_SET_ENUM;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002154 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002155 case LY_STMT_FRACTION_DIGITS:
Radek Krejci33090f92020-12-17 20:12:46 +01002156 LY_CHECK_RET(parse_type_fracdigits(ctx, &type->fraction_digits, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002157 type->flags |= LYS_SET_FRDIGITS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002158 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002159 case LY_STMT_LENGTH:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002160 if (type->length) {
David Sedlákb3077192019-06-19 10:55:37 +02002161 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002162 return LY_EVALID;
2163 }
2164 type->length = calloc(1, sizeof *type->length);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002165 LY_CHECK_ERR_RET(!type->length, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002166
Radek Krejci33090f92020-12-17 20:12:46 +01002167 LY_CHECK_RET(parse_restr(ctx, kw, type->length));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002168 type->flags |= LYS_SET_LENGTH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002169 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002170 case LY_STMT_PATH:
Michal Vasko004d3152020-06-11 19:59:22 +02002171 if (type->path) {
Radek Krejci3972b332021-03-02 16:34:31 +01002172 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(LY_STMT_PATH));
Michal Vasko004d3152020-06-11 19:59:22 +02002173 return LY_EVALID;
2174 }
2175
aPiecek4bb1e372021-05-07 11:01:00 +02002176 /* Usually, in the parser_yang.c, the result of the parsing is stored directly in the
2177 * corresponding structure, so in case of failure, the lysp_module_free function will take
2178 * care of removing the parsed value from the dictionary. But in this case, it is not possible
2179 * to rely on lysp_module_free because the result of the parsing is stored in a local variable.
2180 */
2181 LY_CHECK_ERR_RET(ret = parse_text_field(ctx, LY_STMT_PATH, 0, &str_path, Y_STR_ARG, &type->exts),
2182 lydict_remove(PARSER_CTX(ctx), str_path), ret);
Michal Vaskoed725d72021-06-23 12:03:45 +02002183 ret = ly_path_parse(PARSER_CTX(ctx), NULL, str_path, 0, 1, LY_PATH_BEGIN_EITHER,
Michal Vasko69730152020-10-09 16:30:07 +02002184 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path);
aPiecek4bb1e372021-05-07 11:01:00 +02002185 /* Moreover, even if successful, the string is removed from the dictionary. */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002186 lydict_remove(PARSER_CTX(ctx), str_path);
Michal Vasko004d3152020-06-11 19:59:22 +02002187 LY_CHECK_RET(ret);
Radek Krejcid505e3d2018-11-13 09:04:17 +01002188 type->flags |= LYS_SET_PATH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002189 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002190 case LY_STMT_PATTERN:
Radek Krejci33090f92020-12-17 20:12:46 +01002191 LY_CHECK_RET(parse_type_pattern(ctx, &type->patterns));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002192 type->flags |= LYS_SET_PATTERN;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002193 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002194 case LY_STMT_RANGE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002195 if (type->range) {
David Sedlákb3077192019-06-19 10:55:37 +02002196 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002197 return LY_EVALID;
2198 }
2199 type->range = calloc(1, sizeof *type->range);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002200 LY_CHECK_ERR_RET(!type->range, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002201
Radek Krejci33090f92020-12-17 20:12:46 +01002202 LY_CHECK_RET(parse_restr(ctx, kw, type->range));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002203 type->flags |= LYS_SET_RANGE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002204 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002205 case LY_STMT_REQUIRE_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002206 LY_CHECK_RET(parse_type_reqinstance(ctx, &type->require_instance, &type->flags, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002207 /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002208 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002209 case LY_STMT_TYPE:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002210 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), type->types, nest_type, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01002211 LY_CHECK_RET(parse_type(ctx, nest_type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002212 type->flags |= LYS_SET_TYPE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002213 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002214 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002215 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_TYPE, 0, &type->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002216 break;
2217 default:
David Sedlákb3077192019-06-19 10:55:37 +02002218 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002219 return LY_EVALID;
2220 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002221 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002222 return ret;
2223}
2224
Michal Vaskoea5abea2018-09-18 13:10:54 +02002225/**
2226 * @brief Parse the leaf statement.
2227 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002228 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002229 * @param[in,out] siblings Siblings to add to.
2230 *
2231 * @return LY_ERR values.
2232 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002233LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002234parse_leaf(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002235{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002236 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002237 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002238 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002239 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002240 struct lysp_node_leaf *leaf;
2241
David Sedlák60adc092019-08-06 15:57:02 +02002242 /* create new leaf structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002243 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, leaf, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002244 leaf->nodetype = LYS_LEAF;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002245 leaf->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002246
Michal Vasko7fbc8162018-09-17 10:35:16 +02002247 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002248 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002249 INSERT_WORD_RET(ctx, buf, leaf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002250
2251 /* parse substatements */
aPieceka24a2252021-05-07 10:52:31 +02002252 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002253 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002254 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002255 LY_CHECK_RET(parse_config(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002256 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002257 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01002258 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DEFAULT, 0, &leaf->dflt.str, Y_STR_ARG, &leaf->exts));
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002259 leaf->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002260 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002261 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002262 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &leaf->dsc, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002263 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002264 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002265 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &leaf->iffeatures, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002266 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002267 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002268 LY_CHECK_RET(parse_mandatory(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002269 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002270 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002271 LY_CHECK_RET(parse_restrs(ctx, kw, &leaf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002272 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002273 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002274 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &leaf->ref, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002275 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002276 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002277 LY_CHECK_RET(parse_status(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002278 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002279 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002280 LY_CHECK_RET(parse_type(ctx, &leaf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002281 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002282 case LY_STMT_UNITS:
Radek Krejcifc596f92021-02-26 22:40:26 +01002283 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_UNITS, 0, &leaf->units, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002284 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002285 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002286 LY_CHECK_RET(parse_when(ctx, &leaf->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002287 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002288 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002289 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_LEAF, 0, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002290 break;
2291 default:
David Sedlákb3077192019-06-19 10:55:37 +02002292 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002293 return LY_EVALID;
2294 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002295 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002296 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002297checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002298 /* mandatory substatements */
2299 if (!leaf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002300 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002301 return LY_EVALID;
2302 }
2303
2304 return ret;
2305}
2306
Michal Vaskoea5abea2018-09-18 13:10:54 +02002307/**
2308 * @brief Parse the max-elements statement.
2309 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002310 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002311 * @param[in,out] max Value to write to.
2312 * @param[in,out] flags Flags to write to.
2313 * @param[in,out] exts Extension instances to add to.
2314 *
2315 * @return LY_ERR values.
2316 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002317LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002318parse_maxelements(struct lys_yang_parser_ctx *ctx, uint32_t *max, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002319{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002320 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002321 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002322 size_t word_len;
Michal Vasko73d77ab2021-07-23 12:45:55 +02002323 unsigned long long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002324 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002325
2326 if (*flags & LYS_SET_MAX) {
David Sedlákb3077192019-06-19 10:55:37 +02002327 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002328 return LY_EVALID;
2329 }
2330 *flags |= LYS_SET_MAX;
2331
2332 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002333 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002334
2335 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
David Sedlákb3077192019-06-19 10:55:37 +02002336 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002337 free(buf);
2338 return LY_EVALID;
2339 }
2340
Radek Krejci7f9b6512019-09-18 13:11:09 +02002341 if (ly_strncmp("unbounded", word, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002342 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02002343 num = strtoull(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002344 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002345 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002346 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002347 free(buf);
2348 return LY_EVALID;
2349 }
2350 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002351 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002352 free(buf);
2353 return LY_EVALID;
2354 }
2355
2356 *max = num;
Radek Krejcid6315102021-02-02 15:26:34 +01002357 } else {
2358 /* unbounded */
2359 *max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002360 }
2361 free(buf);
2362
aPieceka24a2252021-05-07 10:52:31 +02002363 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002364 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002365 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002366 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MAX_ELEMENTS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002367 break;
2368 default:
David Sedlákb3077192019-06-19 10:55:37 +02002369 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002370 return LY_EVALID;
2371 }
2372 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002373 return ret;
2374}
2375
Michal Vaskoea5abea2018-09-18 13:10:54 +02002376/**
2377 * @brief Parse the min-elements statement.
2378 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002379 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002380 * @param[in,out] min Value to write to.
2381 * @param[in,out] flags Flags to write to.
2382 * @param[in,out] exts Extension instances to add to.
2383 *
2384 * @return LY_ERR values.
2385 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002386LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002387parse_minelements(struct lys_yang_parser_ctx *ctx, uint32_t *min, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002388{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002389 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002390 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002391 size_t word_len;
Michal Vasko73d77ab2021-07-23 12:45:55 +02002392 unsigned long long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002393 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002394
2395 if (*flags & LYS_SET_MIN) {
David Sedlákb3077192019-06-19 10:55:37 +02002396 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002397 return LY_EVALID;
2398 }
2399 *flags |= LYS_SET_MIN;
2400
2401 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002402 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002403
2404 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
David Sedlákb3077192019-06-19 10:55:37 +02002405 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002406 free(buf);
2407 return LY_EVALID;
2408 }
2409
2410 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02002411 num = strtoull(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002412 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002413 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002414 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002415 free(buf);
2416 return LY_EVALID;
2417 }
2418 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002419 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002420 free(buf);
2421 return LY_EVALID;
2422 }
2423 *min = num;
2424 free(buf);
2425
aPieceka24a2252021-05-07 10:52:31 +02002426 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002427 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002428 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002429 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MIN_ELEMENTS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002430 break;
2431 default:
David Sedlákb3077192019-06-19 10:55:37 +02002432 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002433 return LY_EVALID;
2434 }
2435 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002436 return ret;
2437}
2438
Michal Vaskoea5abea2018-09-18 13:10:54 +02002439/**
2440 * @brief Parse the ordered-by statement.
2441 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002442 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002443 * @param[in,out] flags Flags to write to.
2444 * @param[in,out] exts Extension instances to add to.
2445 *
2446 * @return LY_ERR values.
2447 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002448static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002449parse_orderedby(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002450{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002451 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002452 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002453 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002454 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002455
2456 if (*flags & LYS_ORDBY_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02002457 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002458 return LY_EVALID;
2459 }
2460
2461 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002462 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002463
Radek Krejcif13b87b2020-12-01 22:02:17 +01002464 if ((word_len == ly_strlen_const("system")) && !strncmp(word, "system", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002465 *flags |= LYS_ORDBY_SYSTEM;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002466 } else if ((word_len == ly_strlen_const("user")) && !strncmp(word, "user", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002467 *flags |= LYS_ORDBY_USER;
2468 } else {
David Sedlákb3077192019-06-19 10:55:37 +02002469 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002470 free(buf);
2471 return LY_EVALID;
2472 }
2473 free(buf);
2474
aPieceka24a2252021-05-07 10:52:31 +02002475 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002476 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002477 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002478 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_ORDERED_BY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002479 break;
2480 default:
David Sedlákb3077192019-06-19 10:55:37 +02002481 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002482 return LY_EVALID;
2483 }
2484 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002485 return ret;
2486}
2487
Michal Vaskoea5abea2018-09-18 13:10:54 +02002488/**
2489 * @brief Parse the leaf-list statement.
2490 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002491 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002492 * @param[in,out] siblings Siblings to add to.
2493 *
2494 * @return LY_ERR values.
2495 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002496LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002497parse_leaflist(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002498{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002499 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002500 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002501 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002502 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002503 struct lysp_node_leaflist *llist;
2504
David Sedlák60adc092019-08-06 15:57:02 +02002505 /* create new leaf-list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002506 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, llist, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002507 llist->nodetype = LYS_LEAFLIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002508 llist->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002509
Michal Vasko7fbc8162018-09-17 10:35:16 +02002510 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002511 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002512 INSERT_WORD_RET(ctx, buf, llist->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002513
2514 /* parse substatements */
aPieceka24a2252021-05-07 10:52:31 +02002515 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002516 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002517 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002518 LY_CHECK_RET(parse_config(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002519 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002520 case LY_STMT_DEFAULT:
Radek Krejci335332a2019-09-05 13:03:35 +02002521 PARSER_CHECK_STMTVER2_RET(ctx, "default", "leaf-list");
Radek Krejcifc596f92021-02-26 22:40:26 +01002522 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, &llist->dflts, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002523 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002524 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002525 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &llist->dsc, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002526 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002527 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002528 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &llist->iffeatures, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002529 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002530 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002531 LY_CHECK_RET(parse_maxelements(ctx, &llist->max, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002532 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002533 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002534 LY_CHECK_RET(parse_minelements(ctx, &llist->min, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002535 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002536 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002537 LY_CHECK_RET(parse_restrs(ctx, kw, &llist->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002538 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002539 case LY_STMT_ORDERED_BY:
Radek Krejci33090f92020-12-17 20:12:46 +01002540 LY_CHECK_RET(parse_orderedby(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002541 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002542 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002543 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &llist->ref, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002544 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002545 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002546 LY_CHECK_RET(parse_status(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002547 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002548 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002549 LY_CHECK_RET(parse_type(ctx, &llist->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002550 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002551 case LY_STMT_UNITS:
Radek Krejcifc596f92021-02-26 22:40:26 +01002552 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_UNITS, 0, &llist->units, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002553 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002554 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002555 LY_CHECK_RET(parse_when(ctx, &llist->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002556 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002557 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002558 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_LEAF_LIST, 0, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002559 break;
2560 default:
David Sedlákb3077192019-06-19 10:55:37 +02002561 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002562 return LY_EVALID;
2563 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002564 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002565 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002566checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002567 /* mandatory substatements */
2568 if (!llist->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002569 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002570 return LY_EVALID;
2571 }
2572
2573 return ret;
2574}
2575
Michal Vaskoea5abea2018-09-18 13:10:54 +02002576/**
2577 * @brief Parse the refine statement.
2578 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002579 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002580 * @param[in,out] refines Refines to add to.
2581 *
2582 * @return LY_ERR values.
2583 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002584static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002585parse_refine(struct lys_yang_parser_ctx *ctx, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002586{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002587 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002588 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002589 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002590 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002591 struct lysp_refine *rf;
2592
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002593 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002594
2595 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002596 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01002597 CHECK_NONEMPTY(ctx, word_len, "refine");
Radek Krejci011e4aa2020-09-04 15:22:31 +02002598 INSERT_WORD_RET(ctx, buf, rf->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002599
aPieceka24a2252021-05-07 10:52:31 +02002600 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002601 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002602 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002603 LY_CHECK_RET(parse_config(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002604 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002605 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01002606 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, &rf->dflts, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002607 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002608 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002609 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &rf->dsc, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002610 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002611 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02002612 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "refine");
Radek Krejcifc596f92021-02-26 22:40:26 +01002613 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &rf->iffeatures, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002614 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002615 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002616 LY_CHECK_RET(parse_maxelements(ctx, &rf->max, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002617 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002618 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002619 LY_CHECK_RET(parse_minelements(ctx, &rf->min, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002620 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002621 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002622 LY_CHECK_RET(parse_restrs(ctx, kw, &rf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002623 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002624 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002625 LY_CHECK_RET(parse_mandatory(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002626 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002627 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002628 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &rf->ref, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002629 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002630 case LY_STMT_PRESENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002631 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_PRESENCE, 0, &rf->presence, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002632 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002633 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002634 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_REFINE, 0, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002635 break;
2636 default:
David Sedlákb3077192019-06-19 10:55:37 +02002637 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002638 return LY_EVALID;
2639 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002640 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002641 return ret;
2642}
2643
Michal Vaskoea5abea2018-09-18 13:10:54 +02002644/**
2645 * @brief Parse the typedef statement.
2646 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002647 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002648 * @param[in,out] typedefs Typedefs to add to.
2649 *
2650 * @return LY_ERR values.
2651 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002652static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002653parse_typedef(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002654{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002655 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002656 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002657 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002658 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002659 struct lysp_tpdf *tpdf;
2660
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002661 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002662
2663 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002664 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002665 INSERT_WORD_RET(ctx, buf, tpdf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002666
2667 /* parse substatements */
aPieceka24a2252021-05-07 10:52:31 +02002668 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002669 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002670 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01002671 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DEFAULT, 0, &tpdf->dflt.str, Y_STR_ARG, &tpdf->exts));
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002672 tpdf->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002673 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002674 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002675 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &tpdf->dsc, Y_STR_ARG, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002676 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002677 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002678 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &tpdf->ref, Y_STR_ARG, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002679 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002680 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002681 LY_CHECK_RET(parse_status(ctx, &tpdf->flags, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002682 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002683 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002684 LY_CHECK_RET(parse_type(ctx, &tpdf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002685 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002686 case LY_STMT_UNITS:
Radek Krejcifc596f92021-02-26 22:40:26 +01002687 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_UNITS, 0, &tpdf->units, Y_STR_ARG, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002688 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002689 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002690 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_TYPEDEF, 0, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002691 break;
2692 default:
David Sedlákb3077192019-06-19 10:55:37 +02002693 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002694 return LY_EVALID;
2695 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002696 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002697 LY_CHECK_RET(ret);
Radek Krejcibbe09a92018-11-08 09:36:54 +01002698checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002699 /* mandatory substatements */
2700 if (!tpdf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002701 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002702 return LY_EVALID;
2703 }
2704
Radek Krejcibbe09a92018-11-08 09:36:54 +01002705 /* store data for collision check */
aPiecek75b83a02021-06-29 10:34:38 +02002706 if (parent) {
aPiecek8d4e75d2021-06-24 14:47:06 +02002707 assert(ctx->main_ctx);
2708 LY_CHECK_RET(ly_set_add(&ctx->main_ctx->tpdfs_nodes, parent, 0, NULL));
Radek Krejcibbe09a92018-11-08 09:36:54 +01002709 }
2710
Michal Vasko7fbc8162018-09-17 10:35:16 +02002711 return ret;
2712}
2713
Michal Vaskoea5abea2018-09-18 13:10:54 +02002714/**
2715 * @brief Parse the input or output statement.
2716 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002717 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002718 * @param[in] kw Type of this particular keyword
2719 * @param[in,out] inout_p Input/output pointer to write to.
2720 *
2721 * @return LY_ERR values.
2722 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002723static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002724parse_inout(struct lys_yang_parser_ctx *ctx, enum ly_stmt inout_kw, struct lysp_node *parent,
Radek Krejci2a9fc652021-01-22 17:44:34 +01002725 struct lysp_node_action_inout *inout_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002726{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002727 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002728 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002729 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002730 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002731 ly_bool input = &((struct lysp_node_action *)parent)->input == inout_p ? 1 : 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002732
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002733 if (inout_p->nodetype) {
David Sedlákb3077192019-06-19 10:55:37 +02002734 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002735 return LY_EVALID;
2736 }
2737
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002738 /* initiate structure */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002739 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), input ? "input" : "output", 0, &inout_p->name));
2740 inout_p->nodetype = input ? LYS_INPUT : LYS_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002741 inout_p->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002742
2743 /* parse substatements */
aPieceka24a2252021-05-07 10:52:31 +02002744 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002745 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002746 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002747 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", ly_stmt2str(inout_kw));
Radek Krejci0f969882020-08-21 16:56:47 +02002748 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002749 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002750 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002751 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002752 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01002753 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002754 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002755 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002756 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002757 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002758 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01002759 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002760 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002761 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002762 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002763 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002764 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002765 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002766 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002767 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01002768 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002769 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002770 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002771 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)inout_p, &inout_p->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002772 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002773 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002774 PARSER_CHECK_STMTVER2_RET(ctx, "must", ly_stmt2str(inout_kw));
Radek Krejci33090f92020-12-17 20:12:46 +01002775 LY_CHECK_RET(parse_restrs(ctx, kw, &inout_p->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002776 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002777 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002778 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)inout_p, &inout_p->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002779 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002780 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002781 LY_CHECK_RET(parse_ext(ctx, word, word_len, inout_kw, 0, &inout_p->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002782 break;
2783 default:
David Sedlákb3077192019-06-19 10:55:37 +02002784 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002785 return LY_EVALID;
2786 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002787 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002788 LY_CHECK_RET(ret);
Michal Vaskob83af8a2020-01-06 09:49:22 +01002789
Radek Krejci7fc68292019-06-12 13:51:09 +02002790checks:
Radek Krejci01180ac2021-01-27 08:48:22 +01002791 if (!inout_p->child) {
Michal Vaskob83af8a2020-01-06 09:49:22 +01002792 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "data-def-stmt", ly_stmt2str(inout_kw));
2793 return LY_EVALID;
2794 }
2795
Michal Vasko7fbc8162018-09-17 10:35:16 +02002796 return ret;
2797}
2798
Michal Vaskoea5abea2018-09-18 13:10:54 +02002799/**
2800 * @brief Parse the action statement.
2801 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002802 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002803 * @param[in,out] actions Actions to add to.
2804 *
2805 * @return LY_ERR values.
2806 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002807LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002808parse_action(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002809{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002810 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002811 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002812 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002813 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002814 struct lysp_node_action *act;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002815
Radek Krejci2a9fc652021-01-22 17:44:34 +01002816 LY_LIST_NEW_RET(PARSER_CTX(ctx), actions, act, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002817
2818 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002819 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002820 INSERT_WORD_RET(ctx, buf, act->name, word, word_len);
Michal Vasko1bf09392020-03-27 12:38:10 +01002821 act->nodetype = parent ? LYS_ACTION : LYS_RPC;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002822 act->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002823
aPieceka24a2252021-05-07 10:52:31 +02002824 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002825 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002826 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002827 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &act->dsc, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002828 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002829 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002830 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &act->iffeatures, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002831 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002832 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002833 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &act->ref, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002834 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002835 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002836 LY_CHECK_RET(parse_status(ctx, &act->flags, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002837 break;
2838
Radek Krejcid6b76452019-09-03 17:03:03 +02002839 case LY_STMT_INPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002840 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->input));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002841 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002842 case LY_STMT_OUTPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002843 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->output));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002844 break;
2845
Radek Krejcid6b76452019-09-03 17:03:03 +02002846 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002847 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)act, &act->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002848 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002849 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002850 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)act, &act->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002851 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002852 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002853 LY_CHECK_RET(parse_ext(ctx, word, word_len, parent ? LY_STMT_ACTION : LY_STMT_RPC, 0, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002854 break;
2855 default:
David Sedlákb3077192019-06-19 10:55:37 +02002856 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), parent ? "action" : "rpc");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002857 return LY_EVALID;
2858 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002859 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002860 LY_CHECK_RET(ret);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002861
Radek Krejci2a9fc652021-01-22 17:44:34 +01002862checks:
Michal Vasko7f45cf22020-10-01 12:49:44 +02002863 /* always initialize inout, they are technically present (needed for later deviations/refines) */
2864 if (!act->input.nodetype) {
2865 act->input.nodetype = LYS_INPUT;
2866 act->input.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002867 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "input", 0, &act->input.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002868 }
2869 if (!act->output.nodetype) {
2870 act->output.nodetype = LYS_OUTPUT;
2871 act->output.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002872 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "output", 0, &act->output.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002873 }
2874
Michal Vasko7fbc8162018-09-17 10:35:16 +02002875 return ret;
2876}
2877
Michal Vaskoea5abea2018-09-18 13:10:54 +02002878/**
2879 * @brief Parse the notification statement.
2880 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002881 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002882 * @param[in,out] notifs Notifications to add to.
2883 *
2884 * @return LY_ERR values.
2885 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002886LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002887parse_notif(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002888{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002889 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002890 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002891 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002892 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002893 struct lysp_node_notif *notif;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002894
Radek Krejci2a9fc652021-01-22 17:44:34 +01002895 LY_LIST_NEW_RET(PARSER_CTX(ctx), notifs, notif, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002896
2897 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002898 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002899 INSERT_WORD_RET(ctx, buf, notif->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002900 notif->nodetype = LYS_NOTIF;
2901 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002902
aPieceka24a2252021-05-07 10:52:31 +02002903 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002904 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002905 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002906 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &notif->dsc, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002907 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002908 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002909 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &notif->iffeatures, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002910 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002911 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002912 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &notif->ref, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002913 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002914 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002915 LY_CHECK_RET(parse_status(ctx, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002916 break;
2917
Radek Krejcid6b76452019-09-03 17:03:03 +02002918 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002919 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "notification");
Radek Krejci0f969882020-08-21 16:56:47 +02002920 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002921 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002922 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002923 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002924 case LY_STMT_CHOICE:
Michal Vasko5ae1e1a2021-02-08 09:53:26 +01002925 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002926 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002927 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002928 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002929 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002930 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01002931 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002932 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002933 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002934 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002935 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002936 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002937 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002938 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002939 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01002940 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002941 break;
2942
Radek Krejcid6b76452019-09-03 17:03:03 +02002943 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002944 PARSER_CHECK_STMTVER2_RET(ctx, "must", "notification");
Radek Krejci33090f92020-12-17 20:12:46 +01002945 LY_CHECK_RET(parse_restrs(ctx, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002946 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002947 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002948 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)notif, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002949 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002950 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002951 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002952 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002953 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002954 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_NOTIFICATION, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002955 break;
2956 default:
David Sedlákb3077192019-06-19 10:55:37 +02002957 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002958 return LY_EVALID;
2959 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002960 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002961
Michal Vasko7fbc8162018-09-17 10:35:16 +02002962 return ret;
2963}
2964
Michal Vaskoea5abea2018-09-18 13:10:54 +02002965/**
2966 * @brief Parse the grouping statement.
2967 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002968 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002969 * @param[in,out] groupings Groupings to add to.
2970 *
2971 * @return LY_ERR values.
2972 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002973LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002974parse_grouping(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002975{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002976 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002977 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002978 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002979 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002980 struct lysp_node_grp *grp;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002981
Radek Krejci2a9fc652021-01-22 17:44:34 +01002982 LY_LIST_NEW_RET(PARSER_CTX(ctx), groupings, grp, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002983
2984 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002985 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002986 INSERT_WORD_RET(ctx, buf, grp->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002987 grp->nodetype = LYS_GROUPING;
2988 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002989
aPiecek63e080d2021-06-29 13:53:28 +02002990 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002991 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002992 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002993 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &grp->dsc, Y_STR_ARG, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002994 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002995 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002996 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &grp->ref, Y_STR_ARG, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002997 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002998 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002999 LY_CHECK_RET(parse_status(ctx, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003000 break;
3001
Radek Krejcid6b76452019-09-03 17:03:03 +02003002 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003003 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "grouping");
Radek Krejci0f969882020-08-21 16:56:47 +02003004 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003005 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01003006 LY_CHECK_RET(parse_any(ctx, kw, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003007 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003008 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01003009 LY_CHECK_RET(parse_choice(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003010 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003011 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01003012 LY_CHECK_RET(parse_container(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003013 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003014 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01003015 LY_CHECK_RET(parse_leaf(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003016 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003017 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003018 LY_CHECK_RET(parse_leaflist(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003019 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003020 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003021 LY_CHECK_RET(parse_list(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003022 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003023 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01003024 LY_CHECK_RET(parse_uses(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003025 break;
3026
Radek Krejcid6b76452019-09-03 17:03:03 +02003027 case LY_STMT_TYPEDEF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01003028 LY_CHECK_RET(parse_typedef(ctx, &grp->node, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003029 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003030 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003031 PARSER_CHECK_STMTVER2_RET(ctx, "action", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003032 LY_CHECK_RET(parse_action(ctx, &grp->node, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003033 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003034 case LY_STMT_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01003035 LY_CHECK_RET(parse_grouping(ctx, &grp->node, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003036 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003037 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003038 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003039 LY_CHECK_RET(parse_notif(ctx, &grp->node, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003040 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003041 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003042 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_GROUPING, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003043 break;
3044 default:
David Sedlákb3077192019-06-19 10:55:37 +02003045 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003046 return LY_EVALID;
3047 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003048 }
aPiecek63e080d2021-06-29 13:53:28 +02003049 LY_CHECK_RET(ret);
3050checks:
3051 /* store data for collision check */
3052 if (parent) {
3053 assert(ctx->main_ctx);
3054 LY_CHECK_RET(ly_set_add(&ctx->main_ctx->grps_nodes, parent, 0, NULL));
3055 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003056
Michal Vasko7fbc8162018-09-17 10:35:16 +02003057 return ret;
3058}
3059
Michal Vaskoea5abea2018-09-18 13:10:54 +02003060/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02003061 * @brief Parse the augment statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003062 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003063 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003064 * @param[in,out] augments Augments to add to.
3065 *
3066 * @return LY_ERR values.
3067 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003068LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01003069parse_augment(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003070{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003071 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003072 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003073 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003074 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003075 struct lysp_node_augment *aug;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003076
Radek Krejci2a9fc652021-01-22 17:44:34 +01003077 LY_LIST_NEW_RET(PARSER_CTX(ctx), augments, aug, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003078
3079 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003080 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01003081 CHECK_NONEMPTY(ctx, word_len, "augment");
Radek Krejci011e4aa2020-09-04 15:22:31 +02003082 INSERT_WORD_RET(ctx, buf, aug->nodeid, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003083 aug->nodetype = LYS_AUGMENT;
3084 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003085
aPieceka24a2252021-05-07 10:52:31 +02003086 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003087 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003088 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003089 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &aug->dsc, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003090 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003091 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003092 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &aug->iffeatures, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003093 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003094 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003095 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &aug->ref, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003096 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003097 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003098 LY_CHECK_RET(parse_status(ctx, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003099 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003100 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003101 LY_CHECK_RET(parse_when(ctx, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003102 break;
3103
Radek Krejcid6b76452019-09-03 17:03:03 +02003104 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003105 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "augment");
Radek Krejci0f969882020-08-21 16:56:47 +02003106 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003107 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003108 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003109 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003110 case LY_STMT_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003111 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003112 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003113 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003114 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003115 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003116 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003117 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003118 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003119 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003120 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003121 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003122 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003123 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003124 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003125 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003126 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003127 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003128 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003129 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003130 break;
3131
Radek Krejcid6b76452019-09-03 17:03:03 +02003132 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003133 PARSER_CHECK_STMTVER2_RET(ctx, "action", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003134 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003135 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003136 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003137 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003138 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003139 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003140 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003141 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_AUGMENT, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003142 break;
3143 default:
David Sedlákb3077192019-06-19 10:55:37 +02003144 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003145 return LY_EVALID;
3146 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003147 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003148
Michal Vasko7fbc8162018-09-17 10:35:16 +02003149 return ret;
3150}
3151
Michal Vaskoea5abea2018-09-18 13:10:54 +02003152/**
3153 * @brief Parse the uses statement.
3154 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003155 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003156 * @param[in,out] siblings Siblings to add to.
3157 *
3158 * @return LY_ERR values.
3159 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003160LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003161parse_uses(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003162{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003163 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003164 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003165 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003166 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003167 struct lysp_node_uses *uses;
3168
David Sedlák60adc092019-08-06 15:57:02 +02003169 /* create uses structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003170 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, uses, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003171 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003172 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003173
Michal Vasko7fbc8162018-09-17 10:35:16 +02003174 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003175 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003176 INSERT_WORD_RET(ctx, buf, uses->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003177
3178 /* parse substatements */
aPieceka24a2252021-05-07 10:52:31 +02003179 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003180 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003181 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003182 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &uses->dsc, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003183 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003184 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003185 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &uses->iffeatures, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003186 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003187 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003188 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &uses->ref, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003189 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003190 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003191 LY_CHECK_RET(parse_status(ctx, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003192 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003193 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003194 LY_CHECK_RET(parse_when(ctx, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003195 break;
3196
Radek Krejcid6b76452019-09-03 17:03:03 +02003197 case LY_STMT_REFINE:
Radek Krejci33090f92020-12-17 20:12:46 +01003198 LY_CHECK_RET(parse_refine(ctx, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003199 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003200 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003201 LY_CHECK_RET(parse_augment(ctx, (struct lysp_node *)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003202 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003203 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003204 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_USES, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003205 break;
3206 default:
David Sedlákb3077192019-06-19 10:55:37 +02003207 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003208 return LY_EVALID;
3209 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003210 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003211
Michal Vasko7fbc8162018-09-17 10:35:16 +02003212 return ret;
3213}
3214
Michal Vaskoea5abea2018-09-18 13:10:54 +02003215/**
3216 * @brief Parse the case statement.
3217 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003218 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003219 * @param[in,out] siblings Siblings to add to.
3220 *
3221 * @return LY_ERR values.
3222 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003223LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003224parse_case(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003225{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003226 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003227 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003228 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003229 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003230 struct lysp_node_case *cas;
3231
David Sedlák60adc092019-08-06 15:57:02 +02003232 /* create new case structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003233 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cas, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003234 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003235 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003236
Michal Vasko7fbc8162018-09-17 10:35:16 +02003237 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003238 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003239 INSERT_WORD_RET(ctx, buf, cas->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003240
3241 /* parse substatements */
aPieceka24a2252021-05-07 10:52:31 +02003242 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003243 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003244 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003245 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &cas->dsc, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003246 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003247 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003248 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &cas->iffeatures, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003249 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003250 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003251 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &cas->ref, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003252 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003253 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003254 LY_CHECK_RET(parse_status(ctx, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003255 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003256 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003257 LY_CHECK_RET(parse_when(ctx, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003258 break;
3259
Radek Krejcid6b76452019-09-03 17:03:03 +02003260 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003261 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "case");
Radek Krejci0f969882020-08-21 16:56:47 +02003262 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003263 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003264 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003265 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003266 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003267 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003268 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003269 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003270 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003271 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003272 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003273 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003274 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003275 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003276 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003277 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003278 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003279 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003280 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003281 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003282 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003283 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003284 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003285 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_CASE, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003286 break;
3287 default:
David Sedlákb3077192019-06-19 10:55:37 +02003288 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003289 return LY_EVALID;
3290 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003291 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003292 return ret;
3293}
3294
Michal Vaskoea5abea2018-09-18 13:10:54 +02003295/**
3296 * @brief Parse the choice statement.
3297 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003298 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003299 * @param[in,out] siblings Siblings to add to.
3300 *
3301 * @return LY_ERR values.
3302 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003303LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003304parse_choice(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003305{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003306 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003307 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003308 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003309 enum ly_stmt kw;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003310 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003311
David Sedlák60adc092019-08-06 15:57:02 +02003312 /* create new choice structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003313 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, choice, next, LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003314 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003315 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003316
Michal Vasko7fbc8162018-09-17 10:35:16 +02003317 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003318 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003319 INSERT_WORD_RET(ctx, buf, choice->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003320
3321 /* parse substatements */
aPieceka24a2252021-05-07 10:52:31 +02003322 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003323 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003324 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003325 LY_CHECK_RET(parse_config(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003326 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003327 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003328 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &choice->dsc, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003329 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003330 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003331 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &choice->iffeatures, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003332 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003333 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01003334 LY_CHECK_RET(parse_mandatory(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003335 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003336 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003337 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &choice->ref, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003338 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003339 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003340 LY_CHECK_RET(parse_status(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003341 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003342 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003343 LY_CHECK_RET(parse_when(ctx, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003344 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003345 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01003346 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DEFAULT, 0, &choice->dflt.str, Y_PREF_IDENTIF_ARG,
Michal Vasko7f45cf22020-10-01 12:49:44 +02003347 &choice->exts));
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003348 choice->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003349 break;
3350
Radek Krejcid6b76452019-09-03 17:03:03 +02003351 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003352 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "choice");
Radek Krejci0f969882020-08-21 16:56:47 +02003353 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003354 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003355 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003356 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003357 case LY_STMT_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003358 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003359 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003360 case LY_STMT_CHOICE:
Radek Krejci335332a2019-09-05 13:03:35 +02003361 PARSER_CHECK_STMTVER2_RET(ctx, "choice", "choice");
Radek Krejci33090f92020-12-17 20:12:46 +01003362 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003363 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003364 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003365 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003366 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003367 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003368 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003369 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003370 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003371 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003372 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003373 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003374 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003375 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003376 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003377 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_CHOICE, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003378 break;
3379 default:
David Sedlákb3077192019-06-19 10:55:37 +02003380 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003381 return LY_EVALID;
3382 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003383 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003384 return ret;
3385}
3386
Michal Vaskoea5abea2018-09-18 13:10:54 +02003387/**
3388 * @brief Parse the container statement.
3389 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003390 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003391 * @param[in,out] siblings Siblings to add to.
3392 *
3393 * @return LY_ERR values.
3394 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003395LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003396parse_container(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003397{
3398 LY_ERR ret = 0;
3399 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003400 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003401 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003402 struct lysp_node_container *cont;
3403
David Sedlák60adc092019-08-06 15:57:02 +02003404 /* create new container structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003405 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cont, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003406 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003407 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003408
Michal Vasko7fbc8162018-09-17 10:35:16 +02003409 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003410 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003411 INSERT_WORD_RET(ctx, buf, cont->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003412
3413 /* parse substatements */
aPieceka24a2252021-05-07 10:52:31 +02003414 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003415 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003416 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003417 LY_CHECK_RET(parse_config(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003418 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003419 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003420 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &cont->dsc, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003421 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003422 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003423 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &cont->iffeatures, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003424 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003425 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003426 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &cont->ref, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003427 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003428 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003429 LY_CHECK_RET(parse_status(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003430 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003431 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003432 LY_CHECK_RET(parse_when(ctx, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003433 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003434 case LY_STMT_PRESENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003435 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_PRESENCE, 0, &cont->presence, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003436 break;
3437
Radek Krejcid6b76452019-09-03 17:03:03 +02003438 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003439 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "container");
Radek Krejci0f969882020-08-21 16:56:47 +02003440 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003441 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003442 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003443 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003444 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003445 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003446 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003447 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003448 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003449 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003450 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003451 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003452 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003453 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003454 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003455 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003456 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003457 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003458 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003459 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003460 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003461 break;
3462
Radek Krejcid6b76452019-09-03 17:03:03 +02003463 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003464 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)cont, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003465 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003466 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003467 LY_CHECK_RET(parse_restrs(ctx, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003468 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003469 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003470 PARSER_CHECK_STMTVER2_RET(ctx, "action", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003471 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003472 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003473 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003474 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003475 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003476 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003477 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003478 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003479 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003480 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003481 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_CONTAINER, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003482 break;
3483 default:
David Sedlákb3077192019-06-19 10:55:37 +02003484 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003485 return LY_EVALID;
3486 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003487 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003488
Michal Vasko7fbc8162018-09-17 10:35:16 +02003489 return ret;
3490}
3491
Michal Vaskoea5abea2018-09-18 13:10:54 +02003492/**
3493 * @brief Parse the list statement.
3494 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003495 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003496 * @param[in,out] siblings Siblings to add to.
3497 *
3498 * @return LY_ERR values.
3499 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003500LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003501parse_list(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003502{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003503 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003504 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003505 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003506 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003507 struct lysp_node_list *list;
3508
David Sedlák60adc092019-08-06 15:57:02 +02003509 /* create new list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003510 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, list, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003511 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003512 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003513
Michal Vasko7fbc8162018-09-17 10:35:16 +02003514 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003515 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003516 INSERT_WORD_RET(ctx, buf, list->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003517
3518 /* parse substatements */
aPieceka24a2252021-05-07 10:52:31 +02003519 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003520 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003521 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003522 LY_CHECK_RET(parse_config(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003523 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003524 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003525 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &list->dsc, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003526 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003527 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003528 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &list->iffeatures, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003529 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003530 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003531 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &list->ref, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003532 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003533 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003534 LY_CHECK_RET(parse_status(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003535 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003536 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003537 LY_CHECK_RET(parse_when(ctx, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003538 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003539 case LY_STMT_KEY:
Radek Krejcifc596f92021-02-26 22:40:26 +01003540 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_KEY, 0, &list->key, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003541 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003542 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003543 LY_CHECK_RET(parse_maxelements(ctx, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003544 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003545 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003546 LY_CHECK_RET(parse_minelements(ctx, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003547 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003548 case LY_STMT_ORDERED_BY:
Radek Krejci33090f92020-12-17 20:12:46 +01003549 LY_CHECK_RET(parse_orderedby(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003550 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003551 case LY_STMT_UNIQUE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003552 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_UNIQUE, &list->uniques, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003553 break;
3554
Radek Krejcid6b76452019-09-03 17:03:03 +02003555 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003556 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "list");
Radek Krejci0f969882020-08-21 16:56:47 +02003557 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003558 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003559 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003560 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003561 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003562 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003563 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003564 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003565 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003566 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003567 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003568 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003569 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003570 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003571 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003572 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003573 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003574 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003575 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003576 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003577 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003578 break;
3579
Radek Krejcid6b76452019-09-03 17:03:03 +02003580 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003581 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)list, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003582 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003583 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003584 LY_CHECK_RET(parse_restrs(ctx, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003585 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003586 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003587 PARSER_CHECK_STMTVER2_RET(ctx, "action", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003588 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003589 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003590 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003591 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003592 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003593 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003594 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003595 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003596 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003597 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003598 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_LIST, 0, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003599 break;
3600 default:
David Sedlákb3077192019-06-19 10:55:37 +02003601 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003602 return LY_EVALID;
3603 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003604 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003605
Michal Vasko7fbc8162018-09-17 10:35:16 +02003606 return ret;
3607}
3608
Michal Vaskoea5abea2018-09-18 13:10:54 +02003609/**
3610 * @brief Parse the yin-element statement.
3611 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003612 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003613 * @param[in,out] flags Flags to write to.
3614 * @param[in,out] exts Extension instances to add to.
3615 *
3616 * @return LY_ERR values.
3617 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003618static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003619parse_yinelement(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003620{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003621 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003622 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003623 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003624 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003625
3626 if (*flags & LYS_YINELEM_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02003627 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003628 return LY_EVALID;
3629 }
3630
3631 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003632 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003633
Radek Krejcif13b87b2020-12-01 22:02:17 +01003634 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003635 *flags |= LYS_YINELEM_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003636 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003637 *flags |= LYS_YINELEM_FALSE;
3638 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003639 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003640 free(buf);
3641 return LY_EVALID;
3642 }
3643 free(buf);
3644
aPieceka24a2252021-05-07 10:52:31 +02003645 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003646 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003647 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003648 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_YIN_ELEMENT, 0, exts));
Michal Vaskod989ba02020-08-24 10:59:24 +02003649 LY_CHECK_RET(ret);
3650 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003651 default:
David Sedlákb3077192019-06-19 10:55:37 +02003652 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003653 return LY_EVALID;
3654 }
3655 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003656 return ret;
3657}
3658
Michal Vaskoea5abea2018-09-18 13:10:54 +02003659/**
David Sedlák2444f8f2019-07-09 11:02:47 +02003660 * @brief Parse the argument statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003661 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003662 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003663 * @param[in,out] argument Value to write to.
3664 * @param[in,out] flags Flags to write to.
3665 * @param[in,out] exts Extension instances to add to.
3666 *
3667 * @return LY_ERR values.
3668 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003669static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003670parse_argument(struct lys_yang_parser_ctx *ctx, const char **argument, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003671{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003672 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003673 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003674 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003675 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003676
3677 if (*argument) {
David Sedlákb3077192019-06-19 10:55:37 +02003678 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003679 return LY_EVALID;
3680 }
3681
3682 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003683 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003684 INSERT_WORD_RET(ctx, buf, *argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003685
aPieceka24a2252021-05-07 10:52:31 +02003686 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003687 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003688 case LY_STMT_YIN_ELEMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003689 LY_CHECK_RET(parse_yinelement(ctx, flags, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003690 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003691 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003692 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_ARGUMENT, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003693 break;
3694 default:
David Sedlákb3077192019-06-19 10:55:37 +02003695 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003696 return LY_EVALID;
3697 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003698 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003699 return ret;
3700}
3701
Michal Vaskoea5abea2018-09-18 13:10:54 +02003702/**
3703 * @brief Parse the extension statement.
3704 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003705 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003706 * @param[in,out] extensions Extensions to add to.
3707 *
3708 * @return LY_ERR values.
3709 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003710static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003711parse_extension(struct lys_yang_parser_ctx *ctx, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003712{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003713 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003714 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003715 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003716 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003717 struct lysp_ext *ex;
3718
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003719 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003720
3721 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003722 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003723 INSERT_WORD_RET(ctx, buf, ex->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003724
aPieceka24a2252021-05-07 10:52:31 +02003725 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003726 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003727 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003728 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &ex->dsc, Y_STR_ARG, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003729 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003730 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003731 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &ex->ref, Y_STR_ARG, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003732 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003733 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003734 LY_CHECK_RET(parse_status(ctx, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003735 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003736 case LY_STMT_ARGUMENT:
Radek Krejci9f87b0c2021-03-05 14:45:26 +01003737 LY_CHECK_RET(parse_argument(ctx, &ex->argname, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003738 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003739 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003740 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_EXTENSION, 0, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003741 break;
3742 default:
David Sedlákb3077192019-06-19 10:55:37 +02003743 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003744 return LY_EVALID;
3745 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003746 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003747 return ret;
3748}
3749
Michal Vaskoea5abea2018-09-18 13:10:54 +02003750/**
3751 * @brief Parse the deviate statement.
3752 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003753 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003754 * @param[in,out] deviates Deviates to add to.
3755 *
3756 * @return LY_ERR values.
3757 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003758LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003759parse_deviate(struct lys_yang_parser_ctx *ctx, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003760{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003761 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003762 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003763 size_t word_len, dev_mod;
Radek Krejcid6b76452019-09-03 17:03:03 +02003764 enum ly_stmt kw;
David Sedlák60adc092019-08-06 15:57:02 +02003765 struct lysp_deviate *d;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003766 struct lysp_deviate_add *d_add = NULL;
3767 struct lysp_deviate_rpl *d_rpl = NULL;
3768 struct lysp_deviate_del *d_del = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003769 const char **d_units = NULL;
3770 struct lysp_qname **d_uniques = NULL, **d_dflts = NULL;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02003771 struct lysp_restr **d_musts = NULL;
3772 uint16_t *d_flags = 0;
3773 uint32_t *d_min = 0, *d_max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003774
3775 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003776 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003777
Radek Krejcif13b87b2020-12-01 22:02:17 +01003778 if ((word_len == ly_strlen_const("not-supported")) && !strncmp(word, "not-supported", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003779 dev_mod = LYS_DEV_NOT_SUPPORTED;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003780 } else if ((word_len == ly_strlen_const("add")) && !strncmp(word, "add", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003781 dev_mod = LYS_DEV_ADD;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003782 } else if ((word_len == ly_strlen_const("replace")) && !strncmp(word, "replace", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003783 dev_mod = LYS_DEV_REPLACE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003784 } else if ((word_len == ly_strlen_const("delete")) && !strncmp(word, "delete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003785 dev_mod = LYS_DEV_DELETE;
3786 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003787 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003788 free(buf);
3789 return LY_EVALID;
3790 }
3791 free(buf);
3792
3793 /* create structure */
3794 switch (dev_mod) {
3795 case LYS_DEV_NOT_SUPPORTED:
3796 d = calloc(1, sizeof *d);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003797 LY_CHECK_ERR_RET(!d, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003798 break;
3799 case LYS_DEV_ADD:
3800 d_add = calloc(1, sizeof *d_add);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003801 LY_CHECK_ERR_RET(!d_add, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003802 d = (struct lysp_deviate *)d_add;
3803 d_units = &d_add->units;
3804 d_uniques = &d_add->uniques;
3805 d_dflts = &d_add->dflts;
3806 d_musts = &d_add->musts;
3807 d_flags = &d_add->flags;
3808 d_min = &d_add->min;
3809 d_max = &d_add->max;
3810 break;
3811 case LYS_DEV_REPLACE:
3812 d_rpl = calloc(1, sizeof *d_rpl);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003813 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003814 d = (struct lysp_deviate *)d_rpl;
3815 d_units = &d_rpl->units;
3816 d_flags = &d_rpl->flags;
3817 d_min = &d_rpl->min;
3818 d_max = &d_rpl->max;
3819 break;
3820 case LYS_DEV_DELETE:
3821 d_del = calloc(1, sizeof *d_del);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003822 LY_CHECK_ERR_RET(!d_del, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003823 d = (struct lysp_deviate *)d_del;
3824 d_units = &d_del->units;
3825 d_uniques = &d_del->uniques;
3826 d_dflts = &d_del->dflts;
3827 d_musts = &d_del->musts;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003828 break;
3829 default:
3830 assert(0);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003831 LOGINT_RET(PARSER_CTX(ctx));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003832 }
3833 d->mod = dev_mod;
3834
3835 /* insert into siblings */
David Sedlák60adc092019-08-06 15:57:02 +02003836 LY_LIST_INSERT(deviates, d, next);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003837
aPieceka24a2252021-05-07 10:52:31 +02003838 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003839 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003840 case LY_STMT_CONFIG:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003841 switch (dev_mod) {
3842 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003843 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003844 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003845 return LY_EVALID;
3846 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003847 LY_CHECK_RET(parse_config(ctx, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003848 break;
3849 }
3850 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003851 case LY_STMT_DEFAULT:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003852 switch (dev_mod) {
3853 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003854 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003855 return LY_EVALID;
3856 case LYS_DEV_REPLACE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003857 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DEFAULT, 0, &d_rpl->dflt.str, Y_STR_ARG, &d->exts));
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003858 d_rpl->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003859 break;
3860 default:
Radek Krejcifc596f92021-02-26 22:40:26 +01003861 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, d_dflts, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003862 break;
3863 }
3864 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003865 case LY_STMT_MANDATORY:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003866 switch (dev_mod) {
3867 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003868 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003869 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003870 return LY_EVALID;
3871 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003872 LY_CHECK_RET(parse_mandatory(ctx, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003873 break;
3874 }
3875 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003876 case LY_STMT_MAX_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003877 switch (dev_mod) {
3878 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003879 case LYS_DEV_DELETE:
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:
Radek Krejci33090f92020-12-17 20:12:46 +01003883 LY_CHECK_RET(parse_maxelements(ctx, d_max, d_flags, &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_MIN_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003888 switch (dev_mod) {
3889 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003890 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003891 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003892 return LY_EVALID;
3893 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003894 LY_CHECK_RET(parse_minelements(ctx, d_min, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003895 break;
3896 }
3897 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003898 case LY_STMT_MUST:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003899 switch (dev_mod) {
3900 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003901 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003902 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003903 return LY_EVALID;
3904 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003905 LY_CHECK_RET(parse_restrs(ctx, kw, d_musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003906 break;
3907 }
3908 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003909 case LY_STMT_TYPE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003910 switch (dev_mod) {
3911 case LYS_DEV_NOT_SUPPORTED:
3912 case LYS_DEV_ADD:
3913 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003914 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003915 return LY_EVALID;
3916 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003917 if (d_rpl->type) {
David Sedlákb3077192019-06-19 10:55:37 +02003918 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003919 return LY_EVALID;
3920 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003921 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003922 LY_CHECK_ERR_RET(!d_rpl->type, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01003923 LY_CHECK_RET(parse_type(ctx, d_rpl->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003924 break;
3925 }
3926 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003927 case LY_STMT_UNIQUE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003928 switch (dev_mod) {
3929 case LYS_DEV_NOT_SUPPORTED:
3930 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003931 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003932 return LY_EVALID;
3933 default:
Radek Krejcifc596f92021-02-26 22:40:26 +01003934 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_UNIQUE, d_uniques, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003935 break;
3936 }
3937 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003938 case LY_STMT_UNITS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003939 switch (dev_mod) {
3940 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003941 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003942 return LY_EVALID;
3943 default:
Radek Krejcifc596f92021-02-26 22:40:26 +01003944 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_UNITS, 0, d_units, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003945 break;
3946 }
3947 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003948 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003949 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_DEVIATE, 0, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003950 break;
3951 default:
David Sedlákb3077192019-06-19 10:55:37 +02003952 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003953 return LY_EVALID;
3954 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003955 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003956 return ret;
3957}
3958
Michal Vaskoea5abea2018-09-18 13:10:54 +02003959/**
3960 * @brief Parse the deviation statement.
3961 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003962 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003963 * @param[in,out] deviations Deviations to add to.
3964 *
3965 * @return LY_ERR values.
3966 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003967LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003968parse_deviation(struct lys_yang_parser_ctx *ctx, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003969{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003970 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003971 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003972 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003973 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003974 struct lysp_deviation *dev;
3975
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003976 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003977
3978 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003979 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01003980 CHECK_NONEMPTY(ctx, word_len, "deviation");
Radek Krejci011e4aa2020-09-04 15:22:31 +02003981 INSERT_WORD_RET(ctx, buf, dev->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003982
aPieceka24a2252021-05-07 10:52:31 +02003983 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003984 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003985 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003986 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &dev->dsc, Y_STR_ARG, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003987 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003988 case LY_STMT_DEVIATE:
Radek Krejci33090f92020-12-17 20:12:46 +01003989 LY_CHECK_RET(parse_deviate(ctx, &dev->deviates));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003990 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003991 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003992 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &dev->ref, Y_STR_ARG, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003993 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003994 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003995 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_DEVIATION, 0, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003996 break;
3997 default:
David Sedlákb3077192019-06-19 10:55:37 +02003998 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003999 return LY_EVALID;
4000 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004001 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004002 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01004003checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004004 /* mandatory substatements */
4005 if (!dev->deviates) {
David Sedlákb3077192019-06-19 10:55:37 +02004006 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004007 return LY_EVALID;
4008 }
4009
4010 return ret;
4011}
4012
Michal Vaskoea5abea2018-09-18 13:10:54 +02004013/**
4014 * @brief Parse the feature statement.
4015 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004016 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004017 * @param[in,out] features Features to add to.
4018 *
4019 * @return LY_ERR values.
4020 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004021LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004022parse_feature(struct lys_yang_parser_ctx *ctx, struct lysp_feature **features)
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_feature *feat;
4029
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004030 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004031
4032 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01004033 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004034 INSERT_WORD_RET(ctx, buf, feat->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004035
aPieceka24a2252021-05-07 10:52:31 +02004036 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004037 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004038 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004039 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &feat->dsc, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004040 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004041 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004042 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &feat->iffeatures, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004043 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004044 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004045 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &feat->ref, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004046 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004047 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01004048 LY_CHECK_RET(parse_status(ctx, &feat->flags, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004049 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004050 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01004051 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_FEATURE, 0, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004052 break;
4053 default:
David Sedlákb3077192019-06-19 10:55:37 +02004054 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004055 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004056 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004057 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004058 return ret;
4059}
4060
Michal Vaskoea5abea2018-09-18 13:10:54 +02004061/**
4062 * @brief Parse the identity statement.
4063 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004064 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004065 * @param[in,out] identities Identities to add to.
4066 *
4067 * @return LY_ERR values.
4068 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004069LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004070parse_identity(struct lys_yang_parser_ctx *ctx, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004071{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004072 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004073 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004074 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004075 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004076 struct lysp_ident *ident;
4077
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004078 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004079
4080 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01004081 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004082 INSERT_WORD_RET(ctx, buf, ident->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004083
aPieceka24a2252021-05-07 10:52:31 +02004084 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004085 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004086 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004087 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &ident->dsc, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004088 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004089 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02004090 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "identity");
Radek Krejcifc596f92021-02-26 22:40:26 +01004091 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &ident->iffeatures, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004092 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004093 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004094 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &ident->ref, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004095 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004096 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01004097 LY_CHECK_RET(parse_status(ctx, &ident->flags, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004098 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004099 case LY_STMT_BASE:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004100 if (ident->bases && (ctx->parsed_mod->version < LYS_VERSION_1_1)) {
David Sedlákb3077192019-06-19 10:55:37 +02004101 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 +01004102 return LY_EVALID;
4103 }
Radek Krejcifc596f92021-02-26 22:40:26 +01004104 LY_CHECK_RET(parse_text_fields(ctx, LY_STMT_BASE, &ident->bases, Y_PREF_IDENTIF_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004105 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004106 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01004107 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_IDENTITY, 0, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004108 break;
4109 default:
David Sedlákb3077192019-06-19 10:55:37 +02004110 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004111 return LY_EVALID;
4112 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004113 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004114 return ret;
4115}
4116
Michal Vaskoea5abea2018-09-18 13:10:54 +02004117/**
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004118 * @brief Parse module substatements.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004119 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004120 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004121 * @param[in,out] mod Module to write to.
4122 *
4123 * @return LY_ERR values.
4124 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004125LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004126parse_module(struct lys_yang_parser_ctx *ctx, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004127{
4128 LY_ERR ret = 0;
4129 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004130 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004131 enum ly_stmt kw, prev_kw = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004132 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Michal Vasko8dc31992021-02-22 10:30:47 +01004133 const struct lysp_submodule *dup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004134
Michal Vaskoc3781c32020-10-06 14:04:08 +02004135 mod->is_submod = 0;
4136
4137 /* module name */
Radek Krejci33090f92020-12-17 20:12:46 +01004138 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004139 INSERT_WORD_RET(ctx, buf, mod->mod->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004140
aPieceka24a2252021-05-07 10:52:31 +02004141 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004142
Radek Krejcie3846472018-10-15 15:24:51 +02004143#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004144 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 +02004145
Michal Vasko7fbc8162018-09-17 10:35:16 +02004146 switch (kw) {
4147 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004148 case LY_STMT_NAMESPACE:
4149 case LY_STMT_PREFIX:
Radek Krejcie3846472018-10-15 15:24:51 +02004150 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4151 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004152 case LY_STMT_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004153 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004154 break;
4155 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004156 case LY_STMT_INCLUDE:
4157 case LY_STMT_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004158 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004159 break;
4160 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004161 case LY_STMT_ORGANIZATION:
4162 case LY_STMT_CONTACT:
4163 case LY_STMT_DESCRIPTION:
4164 case LY_STMT_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004165 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004166 break;
4167
4168 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004169 case LY_STMT_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004170 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004171 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004172 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004173 case LY_STMT_ANYDATA:
4174 case LY_STMT_ANYXML:
4175 case LY_STMT_AUGMENT:
4176 case LY_STMT_CHOICE:
4177 case LY_STMT_CONTAINER:
4178 case LY_STMT_DEVIATION:
4179 case LY_STMT_EXTENSION:
4180 case LY_STMT_FEATURE:
4181 case LY_STMT_GROUPING:
4182 case LY_STMT_IDENTITY:
4183 case LY_STMT_LEAF:
4184 case LY_STMT_LEAF_LIST:
4185 case LY_STMT_LIST:
4186 case LY_STMT_NOTIFICATION:
4187 case LY_STMT_RPC:
4188 case LY_STMT_TYPEDEF:
4189 case LY_STMT_USES:
4190 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004191 mod_stmt = Y_MOD_BODY;
4192 break;
4193 default:
4194 /* error handled in the next switch */
4195 break;
4196 }
Radek Krejcie3846472018-10-15 15:24:51 +02004197#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004198
Radek Krejcie3846472018-10-15 15:24:51 +02004199 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004200 switch (kw) {
4201 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004202 case LY_STMT_YANG_VERSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004203 LY_CHECK_RET(parse_yangversion(ctx, &mod->version, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004204 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004205 case LY_STMT_NAMESPACE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004206 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_NAMESPACE, 0, &mod->mod->ns, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004207 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004208 case LY_STMT_PREFIX:
Radek Krejcifc596f92021-02-26 22:40:26 +01004209 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_PREFIX, 0, &mod->mod->prefix, Y_IDENTIF_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004210 break;
4211
4212 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004213 case LY_STMT_INCLUDE:
Radek Krejci33090f92020-12-17 20:12:46 +01004214 LY_CHECK_RET(parse_include(ctx, mod->mod->name, &mod->includes));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004215 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004216 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004217 LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, &mod->imports));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004218 break;
4219
4220 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004221 case LY_STMT_ORGANIZATION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004222 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_ORGANIZATION, 0, &mod->mod->org, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004223 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004224 case LY_STMT_CONTACT:
Radek Krejcifc596f92021-02-26 22:40:26 +01004225 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_CONTACT, 0, &mod->mod->contact, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004226 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004227 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004228 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &mod->mod->dsc, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004229 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004230 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004231 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &mod->mod->ref, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004232 break;
4233
4234 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004235 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004236 LY_CHECK_RET(parse_revision(ctx, &mod->revs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004237 break;
4238
4239 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004240 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004241 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "module");
Radek Krejci0f969882020-08-21 16:56:47 +02004242 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004243 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004244 LY_CHECK_RET(parse_any(ctx, kw, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004245 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004246 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004247 LY_CHECK_RET(parse_choice(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004248 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004249 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004250 LY_CHECK_RET(parse_container(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004251 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004252 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004253 LY_CHECK_RET(parse_leaf(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004254 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004255 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004256 LY_CHECK_RET(parse_leaflist(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004257 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004258 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004259 LY_CHECK_RET(parse_list(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004260 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004261 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004262 LY_CHECK_RET(parse_uses(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004263 break;
4264
Radek Krejcid6b76452019-09-03 17:03:03 +02004265 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004266 LY_CHECK_RET(parse_augment(ctx, NULL, &mod->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004267 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004268 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004269 LY_CHECK_RET(parse_deviation(ctx, &mod->deviations));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004270 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004271 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004272 LY_CHECK_RET(parse_extension(ctx, &mod->extensions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004273 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004274 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004275 LY_CHECK_RET(parse_feature(ctx, &mod->features));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004276 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004277 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004278 LY_CHECK_RET(parse_grouping(ctx, NULL, &mod->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004279 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004280 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004281 LY_CHECK_RET(parse_identity(ctx, &mod->identities));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004282 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004283 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004284 LY_CHECK_RET(parse_notif(ctx, NULL, &mod->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004285 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004286 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004287 LY_CHECK_RET(parse_action(ctx, NULL, &mod->rpcs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004288 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004289 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004290 LY_CHECK_RET(parse_typedef(ctx, NULL, &mod->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004291 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004292 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01004293 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MODULE, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004294 break;
4295
4296 default:
David Sedlákb3077192019-06-19 10:55:37 +02004297 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004298 return LY_EVALID;
4299 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004300 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004301 LY_CHECK_RET(ret);
Radek Krejcie86bf772018-12-14 11:39:53 +01004302
Radek Krejci6d6556c2018-11-08 09:37:45 +01004303checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004304 /* mandatory substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004305 if (!mod->mod->ns) {
David Sedlákb3077192019-06-19 10:55:37 +02004306 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004307 return LY_EVALID;
4308 } else if (!mod->mod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004309 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004310 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004311 }
4312
Radek Krejcie9e987e2018-10-31 12:50:27 +01004313 /* submodules share the namespace with the module names, so there must not be
4314 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01004315 dup = ly_ctx_get_submodule_latest(PARSER_CTX(ctx), mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004316 if (dup) {
Radek Krejci854e1552020-12-21 15:05:23 +01004317 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004318 return LY_EVALID;
4319 }
4320
4321 return ret;
4322}
4323
4324/**
4325 * @brief Parse submodule substatements.
4326 *
4327 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004328 * @param[out] submod Parsed submodule structure.
4329 *
4330 * @return LY_ERR values.
4331 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004332LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004333parse_submodule(struct lys_yang_parser_ctx *ctx, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004334{
4335 LY_ERR ret = 0;
4336 char *buf, *word;
4337 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004338 enum ly_stmt kw, prev_kw = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004339 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Michal Vasko8dc31992021-02-22 10:30:47 +01004340 const struct lysp_submodule *dup;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004341
Michal Vaskoc3781c32020-10-06 14:04:08 +02004342 submod->is_submod = 1;
4343
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004344 /* submodule name */
Radek Krejci33090f92020-12-17 20:12:46 +01004345 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004346 INSERT_WORD_RET(ctx, buf, submod->name, word, word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004347
aPieceka24a2252021-05-07 10:52:31 +02004348 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004349
4350#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004351 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 +01004352
4353 switch (kw) {
4354 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004355 case LY_STMT_BELONGS_TO:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004356 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4357 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004358 case LY_STMT_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004359 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4360 break;
4361 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004362 case LY_STMT_INCLUDE:
4363 case LY_STMT_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004364 CHECK_ORDER(Y_MOD_LINKAGE);
4365 break;
4366 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004367 case LY_STMT_ORGANIZATION:
4368 case LY_STMT_CONTACT:
4369 case LY_STMT_DESCRIPTION:
4370 case LY_STMT_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004371 CHECK_ORDER(Y_MOD_META);
4372 break;
4373
4374 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004375 case LY_STMT_REVISION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004376 CHECK_ORDER(Y_MOD_REVISION);
4377 break;
4378 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004379 case LY_STMT_ANYDATA:
4380 case LY_STMT_ANYXML:
4381 case LY_STMT_AUGMENT:
4382 case LY_STMT_CHOICE:
4383 case LY_STMT_CONTAINER:
4384 case LY_STMT_DEVIATION:
4385 case LY_STMT_EXTENSION:
4386 case LY_STMT_FEATURE:
4387 case LY_STMT_GROUPING:
4388 case LY_STMT_IDENTITY:
4389 case LY_STMT_LEAF:
4390 case LY_STMT_LEAF_LIST:
4391 case LY_STMT_LIST:
4392 case LY_STMT_NOTIFICATION:
4393 case LY_STMT_RPC:
4394 case LY_STMT_TYPEDEF:
4395 case LY_STMT_USES:
4396 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004397 mod_stmt = Y_MOD_BODY;
4398 break;
4399 default:
4400 /* error handled in the next switch */
4401 break;
4402 }
4403#undef CHECK_ORDER
4404
4405 prev_kw = kw;
4406 switch (kw) {
4407 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004408 case LY_STMT_YANG_VERSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004409 LY_CHECK_RET(parse_yangversion(ctx, &submod->version, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004410 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004411 case LY_STMT_BELONGS_TO:
Radek Krejci33090f92020-12-17 20:12:46 +01004412 LY_CHECK_RET(parse_belongsto(ctx, &submod->prefix, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004413 break;
4414
4415 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004416 case LY_STMT_INCLUDE:
Radek Krejcieeee95c2021-01-19 10:57:22 +01004417 if (submod->version == LYS_VERSION_1_1) {
4418 LOGWRN(PARSER_CTX(ctx), "YANG version 1.1 expects all includes in main module, includes in submodules (%s) are not necessary.",
4419 submod->name);
4420 }
Radek Krejci33090f92020-12-17 20:12:46 +01004421 LY_CHECK_RET(parse_include(ctx, submod->name, &submod->includes));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004422 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004423 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004424 LY_CHECK_RET(parse_import(ctx, submod->prefix, &submod->imports));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004425 break;
4426
4427 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004428 case LY_STMT_ORGANIZATION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004429 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_ORGANIZATION, 0, &submod->org, Y_STR_ARG, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004430 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004431 case LY_STMT_CONTACT:
Radek Krejcifc596f92021-02-26 22:40:26 +01004432 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_CONTACT, 0, &submod->contact, Y_STR_ARG, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004433 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004434 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004435 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &submod->dsc, Y_STR_ARG, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004436 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004437 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004438 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &submod->ref, Y_STR_ARG, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004439 break;
4440
4441 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004442 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004443 LY_CHECK_RET(parse_revision(ctx, &submod->revs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004444 break;
4445
4446 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004447 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004448 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "submodule");
Radek Krejci0f969882020-08-21 16:56:47 +02004449 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004450 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004451 LY_CHECK_RET(parse_any(ctx, kw, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004452 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004453 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004454 LY_CHECK_RET(parse_choice(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004455 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004456 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004457 LY_CHECK_RET(parse_container(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004458 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004459 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004460 LY_CHECK_RET(parse_leaf(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004461 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004462 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004463 LY_CHECK_RET(parse_leaflist(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004464 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004465 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004466 LY_CHECK_RET(parse_list(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004467 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004468 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004469 LY_CHECK_RET(parse_uses(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004470 break;
4471
Radek Krejcid6b76452019-09-03 17:03:03 +02004472 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004473 LY_CHECK_RET(parse_augment(ctx, NULL, &submod->augments));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004474 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004475 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004476 LY_CHECK_RET(parse_deviation(ctx, &submod->deviations));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004477 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004478 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004479 LY_CHECK_RET(parse_extension(ctx, &submod->extensions));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004480 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004481 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004482 LY_CHECK_RET(parse_feature(ctx, &submod->features));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004483 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004484 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004485 LY_CHECK_RET(parse_grouping(ctx, NULL, &submod->groupings));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004486 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004487 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004488 LY_CHECK_RET(parse_identity(ctx, &submod->identities));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004489 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004490 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004491 LY_CHECK_RET(parse_notif(ctx, NULL, &submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004492 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004493 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004494 LY_CHECK_RET(parse_action(ctx, NULL, &submod->rpcs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004495 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004496 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004497 LY_CHECK_RET(parse_typedef(ctx, NULL, &submod->typedefs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004498 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004499 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01004500 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_SUBMODULE, 0, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004501 break;
4502
4503 default:
David Sedlákb3077192019-06-19 10:55:37 +02004504 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004505 return LY_EVALID;
4506 }
4507 }
4508 LY_CHECK_RET(ret);
4509
4510checks:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004511 /* mandatory substatements */
Michal Vaskoc3781c32020-10-06 14:04:08 +02004512 if (!submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004513 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004514 return LY_EVALID;
4515 }
4516
4517 /* submodules share the namespace with the module names, so there must not be
4518 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01004519 dup = ly_ctx_get_submodule_latest(PARSER_CTX(ctx), submod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02004520 /* main modules may have different revisions */
4521 if (dup && strcmp(dup->mod->name, submod->mod->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01004522 LOGVAL_PARSER(ctx, LY_VCODE_NAME_COL, "submodules", dup->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +01004523 return LY_EVALID;
4524 }
4525
Michal Vasko7fbc8162018-09-17 10:35:16 +02004526 return ret;
4527}
4528
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004529/**
4530 * @brief Skip any redundant characters, namely whitespaces and comments.
4531 *
4532 * @param[in] ctx Yang parser context.
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004533 * @return LY_SUCCESS on success.
4534 * @return LY_EVALID on invalid comment.
4535 */
4536static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004537skip_redundant_chars(struct lys_yang_parser_ctx *ctx)
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004538{
4539 /* read some trailing spaces, new lines, or comments */
Radek Krejci33090f92020-12-17 20:12:46 +01004540 while (ctx->in->current[0]) {
4541 if (!strncmp(ctx->in->current, "//", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004542 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004543 ly_in_skip(ctx->in, 2);
4544 LY_CHECK_RET(skip_comment(ctx, 1));
4545 } else if (!strncmp(ctx->in->current, "/*", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004546 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004547 ly_in_skip(ctx->in, 2);
4548 LY_CHECK_RET(skip_comment(ctx, 2));
4549 } else if (isspace(ctx->in->current[0])) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004550 /* whitespace */
Radek Krejcidd713ce2021-01-04 23:12:12 +01004551 if (ctx->in->current[0] == '\n') {
4552 LY_IN_NEW_LINE(ctx->in);
4553 }
Radek Krejci33090f92020-12-17 20:12:46 +01004554 ly_in_skip(ctx->in, 1);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004555 } else {
4556 break;
4557 }
4558 }
4559
4560 return LY_SUCCESS;
4561}
4562
Radek Krejcid4557c62018-09-17 11:42:09 +02004563LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01004564yang_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 +02004565 struct ly_in *in, struct lysp_submodule **submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004566{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004567 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004568 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004569 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004570 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004571 struct lysp_submodule *mod_p = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004572
aPiecek56be92a2021-07-01 07:18:10 +02004573 assert(context && ly_ctx && main_ctx && in && submod);
4574
David Sedlák1b623122019-08-05 15:27:49 +02004575 /* create context */
4576 *context = calloc(1, sizeof **context);
4577 LY_CHECK_ERR_RET(!(*context), LOGMEM(ly_ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004578 (*context)->format = LYS_IN_YANG;
Radek Krejci33090f92020-12-17 20:12:46 +01004579 (*context)->in = in;
aPiecek56be92a2021-07-01 07:18:10 +02004580 (*context)->main_ctx = main_ctx;
David Sedlák1b623122019-08-05 15:27:49 +02004581
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004582 mod_p = calloc(1, sizeof *mod_p);
4583 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ly_ctx); ret = LY_EMEM, cleanup);
4584 mod_p->mod = main_ctx->parsed_mod->mod;
4585 mod_p->parsing = 1;
4586 (*context)->parsed_mod = (struct lysp_module *)mod_p;
4587
Radek Krejciddace2c2021-01-08 11:30:56 +01004588 LOG_LOCINIT(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004589
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004590 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004591 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004592 LY_CHECK_GOTO(ret, cleanup);
4593
Michal Vasko7fbc8162018-09-17 10:35:16 +02004594 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004595 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004596 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004597
Radek Krejcid6b76452019-09-03 17:03:03 +02004598 if (kw == LY_STMT_MODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004599 LOGERR(ly_ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004600 ret = LY_EINVAL;
4601 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004602 } else if (kw != LY_STMT_SUBMODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004603 LOGVAL_PARSER(*context, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004604 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004605 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004606 }
4607
Michal Vasko7fbc8162018-09-17 10:35:16 +02004608 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004609 ret = parse_submodule(*context, mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004610 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004611
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004612 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004613 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004614 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004615 if (in->current[0]) {
4616 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_SUBMOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004617 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004618 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004619 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004620
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004621 mod_p->parsing = 0;
4622 *submod = mod_p;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004623
Radek Krejcibbe09a92018-11-08 09:36:54 +01004624cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01004625 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004626 if (ret) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004627 lysp_module_free((struct lysp_module *)mod_p);
Michal Vaskob36053d2020-03-26 15:49:30 +01004628 yang_parser_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004629 *context = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004630 }
4631
4632 return ret;
4633}
4634
4635LY_ERR
aPiecekc3e26142021-06-22 14:25:49 +02004636yang_parse_module(struct lys_yang_parser_ctx **context, struct ly_in *in, struct lys_module *mod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004637{
4638 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004639 char *word;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004640 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004641 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004642 struct lysp_module *mod_p = NULL;
4643
David Sedlák1b623122019-08-05 15:27:49 +02004644 /* create context */
4645 *context = calloc(1, sizeof **context);
4646 LY_CHECK_ERR_RET(!(*context), LOGMEM(mod->ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004647 (*context)->format = LYS_IN_YANG;
Radek Krejci33090f92020-12-17 20:12:46 +01004648 (*context)->in = in;
aPiecek8d4e75d2021-06-24 14:47:06 +02004649 (*context)->main_ctx = (struct lys_parser_ctx *)(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004650
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004651 mod_p = calloc(1, sizeof *mod_p);
4652 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(mod->ctx), cleanup);
4653 mod_p->mod = mod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004654 (*context)->parsed_mod = mod_p;
4655
Radek Krejciddace2c2021-01-08 11:30:56 +01004656 LOG_LOCINIT(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004657
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004658 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004659 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004660 LY_CHECK_GOTO(ret, cleanup);
4661
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004662 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004663 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004664 LY_CHECK_GOTO(ret, cleanup);
4665
Radek Krejcid6b76452019-09-03 17:03:03 +02004666 if (kw == LY_STMT_SUBMODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004667 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 +01004668 ret = LY_EINVAL;
4669 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004670 } else if (kw != LY_STMT_MODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004671 LOGVAL_PARSER((*context), LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004672 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004673 goto cleanup;
4674 }
4675
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004676 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004677 ret = parse_module(*context, mod_p);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004678 LY_CHECK_GOTO(ret, cleanup);
4679
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004680 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004681 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004682 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004683 if (in->current[0]) {
4684 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_MOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004685 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004686 goto cleanup;
4687 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004688
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004689 mod->parsed = mod_p;
4690
4691cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01004692 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004693 if (ret) {
4694 lysp_module_free(mod_p);
Michal Vaskob36053d2020-03-26 15:49:30 +01004695 yang_parser_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004696 *context = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004697 }
4698
Michal Vasko7fbc8162018-09-17 10:35:16 +02004699 return ret;
4700}