blob: 7e5cc98b3d22a1460b3cef66d68a8aaf040b493a [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 Vasko7fbc8162018-09-17 10:35:16 +0200588 goto str_end;
589 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100590 if (ctx->in->current[1] == '/') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200591 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100592 MOVE_INPUT(ctx, 2);
Michal Vaskob68ea142021-04-26 13:46:00 +0200593 LY_CHECK_GOTO(ret = skip_comment(ctx, 1), error);
Radek Krejci33090f92020-12-17 20:12:46 +0100594 } else if (ctx->in->current[1] == '*') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200595 /* block 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, 2), error);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200598 } else {
599 /* not a comment after all */
Michal Vaskob68ea142021-04-26 13:46:00 +0200600 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 +0200601 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200602 break;
603 case ' ':
604 if (*word_len) {
605 /* word is finished */
606 goto str_end;
607 }
Radek Krejci33090f92020-12-17 20:12:46 +0100608 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200609 break;
610 case '\t':
611 if (*word_len) {
612 /* word is finished */
613 goto str_end;
614 }
615 /* tabs count for 8 spaces */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100616 ctx->indent += Y_TAB_SPACES;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200617
Radek Krejci33090f92020-12-17 20:12:46 +0100618 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200619 break;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200620 case '\r':
621 if (ctx->in->current[1] != '\n') {
622 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
Michal Vaskob68ea142021-04-26 13:46:00 +0200623 ret = LY_EVALID;
624 goto error;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200625 }
626 MOVE_INPUT(ctx, 1);
627 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200628 case '\n':
629 if (*word_len) {
630 /* word is finished */
631 goto str_end;
632 }
Radek Krejcidd713ce2021-01-04 23:12:12 +0100633 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid54412f2020-12-17 20:25:35 +0100634 MOVE_INPUT(ctx, 1);
635
Michal Vasko7fbc8162018-09-17 10:35:16 +0200636 /* reset indent */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200637 ctx->indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200638 break;
639 case ';':
640 case '{':
641 if (*word_len || (arg == Y_MAYBE_STR_ARG)) {
642 /* word is finished */
643 goto str_end;
644 }
645
Radek Krejci33090f92020-12-17 20:12:46 +0100646 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current, "an argument");
Michal Vaskob68ea142021-04-26 13:46:00 +0200647 ret = LY_EVALID;
648 goto error;
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200649 case '}':
650 /* invalid - braces cannot be in unquoted string (opening braces terminates the string and can follow it) */
Radek Krejci33090f92020-12-17 20:12:46 +0100651 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200652 "unquoted string character, optsep, semicolon or opening brace");
Michal Vaskob68ea142021-04-26 13:46:00 +0200653 ret = LY_EVALID;
654 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200655 default:
Michal Vaskob68ea142021-04-26 13:46:00 +0200656 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 +0200657 break;
658 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200659 }
660
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200661 /* unexpected end of loop */
David Sedlákb3077192019-06-19 10:55:37 +0200662 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vaskob68ea142021-04-26 13:46:00 +0200663 ret = LY_EVALID;
664 goto error;
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200665
Michal Vasko7fbc8162018-09-17 10:35:16 +0200666str_end:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200667 /* terminating NULL byte for buf */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200668 if (*word_b) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200669 (*word_b) = ly_realloc(*word_b, (*word_len) + 1);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200670 LY_CHECK_ERR_RET(!(*word_b), LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200671 (*word_b)[*word_len] = '\0';
Michal Vasko7fbc8162018-09-17 10:35:16 +0200672 *word_p = *word_b;
673 }
674
675 return LY_SUCCESS;
Michal Vaskob68ea142021-04-26 13:46:00 +0200676
677error:
678 free(*word_b);
679 *word_b = NULL;
680 return ret;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200681}
682
Michal Vaskoea5abea2018-09-18 13:10:54 +0200683/**
684 * @brief Get another YANG keyword from the raw data.
685 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200686 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200687 * @param[out] kw YANG keyword read.
688 * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances.
689 * @param[out] word_len Length of the keyword in the data. Useful for extension instances.
690 *
691 * @return LY_ERR values.
692 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200693LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100694get_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 +0200695{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200696 uint8_t prefix;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200697 const char *word_start;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200698 size_t len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200699
700 if (word_p) {
701 *word_p = NULL;
702 *word_len = 0;
703 }
704
705 /* first skip "optsep", comments */
Radek Krejci33090f92020-12-17 20:12:46 +0100706 while (ctx->in->current[0]) {
707 switch (ctx->in->current[0]) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200708 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100709 if (ctx->in->current[1] == '/') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200710 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100711 MOVE_INPUT(ctx, 2);
712 LY_CHECK_RET(skip_comment(ctx, 1));
713 } else if (ctx->in->current[1] == '*') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200714 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100715 MOVE_INPUT(ctx, 2);
716 LY_CHECK_RET(skip_comment(ctx, 2));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200717 } else {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200718 /* error - not a comment after all, keyword cannot start with slash */
David Sedlákb3077192019-06-19 10:55:37 +0200719 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '/'.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200720 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200721 }
Radek Krejci13028282019-06-11 14:56:48 +0200722 continue;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200723 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200724 /* skip whitespaces (optsep) */
Radek Krejcidd713ce2021-01-04 23:12:12 +0100725 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200726 ctx->indent = 0;
727 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200728 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200729 /* skip whitespaces (optsep) */
730 ++ctx->indent;
731 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200732 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200733 /* skip whitespaces (optsep) */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100734 ctx->indent += Y_TAB_SPACES;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200735 break;
Michal Vasko50dcbc22021-03-25 12:21:20 +0100736 case '\r':
737 /* possible CRLF endline */
738 if (ctx->in->current[1] == '\n') {
739 break;
740 }
Radek Krejcid43298b2021-03-25 16:17:15 +0100741 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200742 default:
743 /* either a keyword start or an invalid character */
744 goto keyword_start;
745 }
746
Radek Krejci33090f92020-12-17 20:12:46 +0100747 ly_in_skip(ctx->in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200748 }
749
750keyword_start:
Radek Krejci33090f92020-12-17 20:12:46 +0100751 word_start = ctx->in->current;
Radek Krejcid54412f2020-12-17 20:25:35 +0100752 *kw = lysp_match_kw(ctx->in, &ctx->indent);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200753
aPiecek93582ed2021-05-25 14:49:06 +0200754 if (*kw == LY_STMT_SYNTAX_SEMICOLON) {
755 goto success;
756 } else if (*kw == LY_STMT_SYNTAX_LEFT_BRACE) {
757 ctx->depth++;
758 if (ctx->depth > LY_MAX_BLOCK_DEPTH) {
759 LOGERR(ctx->parsed_mod->mod->ctx, LY_EINVAL,
760 "The maximum number of block nestings has been exceeded.");
761 return LY_EINVAL;
762 }
763 goto success;
764 } else if (*kw == LY_STMT_SYNTAX_RIGHT_BRACE) {
765 ctx->depth--;
Radek Krejci626df482018-10-11 15:06:31 +0200766 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200767 }
768
Radek Krejcid6b76452019-09-03 17:03:03 +0200769 if (*kw != LY_STMT_NONE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200770 /* make sure we have the whole keyword */
Radek Krejci33090f92020-12-17 20:12:46 +0100771 switch (ctx->in->current[0]) {
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200772 case '\r':
773 if (ctx->in->current[1] != '\n') {
774 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
775 return LY_EVALID;
776 }
777 MOVE_INPUT(ctx, 1);
778 /* fallthrough */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200779 case '\n':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200780 case '\t':
Radek Krejciabdd8062019-06-11 16:44:19 +0200781 case ' ':
782 /* mandatory "sep" is just checked, not eaten so nothing in the context is updated */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200783 break;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200784 case ':':
785 /* keyword is not actually a keyword, but prefix of an extension.
786 * To avoid repeated check of the prefix syntax, move to the point where the colon was read
787 * and we will be checking the keyword (extension instance) itself */
788 prefix = 1;
Radek Krejci33090f92020-12-17 20:12:46 +0100789 MOVE_INPUT(ctx, 1);
Radek Krejci156ccaf2018-10-15 15:49:17 +0200790 goto extension;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200791 case '{':
792 /* allowed only for input and output statements which can be without arguments */
Michal Vasko69730152020-10-09 16:30:07 +0200793 if ((*kw == LY_STMT_INPUT) || (*kw == LY_STMT_OUTPUT)) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200794 break;
795 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100796 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200797 default:
Radek Krejci33090f92020-12-17 20:12:46 +0100798 MOVE_INPUT(ctx, 1);
799 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start,
Michal Vasko69730152020-10-09 16:30:07 +0200800 "a keyword followed by a separator");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200801 return LY_EVALID;
802 }
803 } else {
804 /* still can be an extension */
805 prefix = 0;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200806
Radek Krejci156ccaf2018-10-15 15:49:17 +0200807extension:
Radek Krejcid54412f2020-12-17 20:25:35 +0100808 while (ctx->in->current[0] && (ctx->in->current[0] != ' ') && (ctx->in->current[0] != '\t') &&
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200809 (ctx->in->current[0] != '\n') && (ctx->in->current[0] != '\r') && (ctx->in->current[0] != '{') &&
810 (ctx->in->current[0] != ';')) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200811 uint32_t c = 0;
812
Radek Krejci33090f92020-12-17 20:12:46 +0100813 LY_CHECK_ERR_RET(ly_getutf8(&ctx->in->current, &c, &len),
814 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[-len]), LY_EVALID);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200815 ++ctx->indent;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200816 /* check character validity */
Michal Vasko63f3d842020-07-08 10:10:14 +0200817 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c,
Radek Krejci33090f92020-12-17 20:12:46 +0100818 ctx->in->current - len == word_start ? 1 : 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200819 }
Radek Krejci33090f92020-12-17 20:12:46 +0100820 if (!ctx->in->current[0]) {
David Sedlákb3077192019-06-19 10:55:37 +0200821 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200822 return LY_EVALID;
823 }
824
825 /* prefix is mandatory for extension instances */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200826 if (prefix != 2) {
Radek Krejci33090f92020-12-17 20:12:46 +0100827 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start, "a keyword");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200828 return LY_EVALID;
829 }
830
Radek Krejcid6b76452019-09-03 17:03:03 +0200831 *kw = LY_STMT_EXTENSION_INSTANCE;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200832 }
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200833
Radek Krejci626df482018-10-11 15:06:31 +0200834success:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200835 if (word_p) {
836 *word_p = (char *)word_start;
Radek Krejci33090f92020-12-17 20:12:46 +0100837 *word_len = ctx->in->current - word_start;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200838 }
839
840 return LY_SUCCESS;
841}
842
Michal Vaskoea5abea2018-09-18 13:10:54 +0200843/**
844 * @brief Parse extension instance substatements.
845 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200846 * @param[in] ctx yang parser context for logging.
Radek Krejci335332a2019-09-05 13:03:35 +0200847 * @param[in] kw Statement keyword value matching @p word value.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200848 * @param[in] word Extension instance substatement name (keyword).
849 * @param[in] word_len Extension instance substatement name length.
850 * @param[in,out] child Children of this extension instance to add to.
851 *
852 * @return LY_ERR values.
853 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200854static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100855parse_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 +0200856 struct lysp_stmt **child)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200857{
858 char *buf;
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100859 LY_ERR ret = LY_SUCCESS;
Radek Krejci335332a2019-09-05 13:03:35 +0200860 enum ly_stmt child_kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200861 struct lysp_stmt *stmt, *par_child;
862
863 stmt = calloc(1, sizeof *stmt);
864 LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM);
865
Radek Krejcibb9b1982019-04-08 14:24:59 +0200866 /* insert into parent statements */
867 if (!*child) {
868 *child = stmt;
869 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +0200870 for (par_child = *child; par_child->next; par_child = par_child->next) {}
Radek Krejcibb9b1982019-04-08 14:24:59 +0200871 par_child->next = stmt;
872 }
873
Michal Vaskofc2cd072021-02-24 13:17:17 +0100874 /* statement */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200875 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), word, word_len, &stmt->stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200876
877 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100878 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, &stmt->flags, &word, &buf, &word_len));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200879 if (word) {
aPieceka24a2252021-05-07 10:52:31 +0200880 INSERT_WORD_RET(ctx, buf, stmt->arg, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200881 }
882
Radek Krejci8df109d2021-04-23 12:19:08 +0200883 stmt->format = LY_VALUE_SCHEMA;
Michal Vaskofc2cd072021-02-24 13:17:17 +0100884 stmt->prefix_data = ctx->parsed_mod;
885 stmt->kw = kw;
886
aPieceka24a2252021-05-07 10:52:31 +0200887 YANG_READ_SUBSTMT_FOR(ctx, child_kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Radek Krejci33090f92020-12-17 20:12:46 +0100888 LY_CHECK_RET(parse_ext_substmt(ctx, child_kw, word, word_len, &stmt->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200889 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200890 return ret;
891}
892
Michal Vaskoea5abea2018-09-18 13:10:54 +0200893/**
894 * @brief Parse extension instance.
895 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200896 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200897 * @param[in] ext_name Extension instance substatement name (keyword).
898 * @param[in] ext_name_len Extension instance substatement name length.
Radek Krejcifc596f92021-02-26 22:40:26 +0100899 * @param[in] insubstmt The statement this extension instance is a substatement of.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200900 * @param[in] insubstmt_index Index of the keyword instance this extension instance is a substatement of.
901 * @param[in,out] exts Extension instances to add to.
902 *
903 * @return LY_ERR values.
904 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200905static LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +0100906parse_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 +0200907 LY_ARRAY_COUNT_TYPE insubstmt_index, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200908{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100909 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200910 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200911 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200912 struct lysp_ext_instance *e;
Radek Krejcid6b76452019-09-03 17:03:03 +0200913 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200914
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200915 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *exts, e, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200916
Michal Vaskofc2cd072021-02-24 13:17:17 +0100917 if (!ly_strnchr(ext_name, ':', ext_name_len)) {
918 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Extension instance \"%*.s\" without the mandatory prefix.", ext_name_len, ext_name);
919 return LY_EVALID;
920 }
921
922 /* store name */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200923 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), ext_name, ext_name_len, &e->name));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200924
925 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100926 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, NULL, &word, &buf, &word_len));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200927 if (word) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200928 INSERT_WORD_RET(ctx, buf, e->argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200929 }
930
Michal Vaskofc2cd072021-02-24 13:17:17 +0100931 /* store the rest of information */
Radek Krejci8df109d2021-04-23 12:19:08 +0200932 e->format = LY_VALUE_SCHEMA;
aPiecek60d9d672021-04-27 15:49:57 +0200933 e->parsed = NULL;
Michal Vaskofc2cd072021-02-24 13:17:17 +0100934 e->prefix_data = ctx->parsed_mod;
Radek Krejciab430862021-03-02 20:13:40 +0100935 e->parent_stmt = insubstmt;
936 e->parent_stmt_index = insubstmt_index;
Michal Vaskofc2cd072021-02-24 13:17:17 +0100937
aPieceka24a2252021-05-07 10:52:31 +0200938 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Radek Krejci33090f92020-12-17 20:12:46 +0100939 LY_CHECK_RET(parse_ext_substmt(ctx, kw, word, word_len, &e->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200940 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200941 return ret;
942}
943
Michal Vaskoea5abea2018-09-18 13:10:54 +0200944/**
945 * @brief Parse a generic text field without specific constraints. Those are contact, organization,
946 * description, etc...
947 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200948 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200949 * @param[in] substmt Type of this substatement.
950 * @param[in] substmt_index Index of this substatement.
951 * @param[in,out] value Place to store the parsed value.
952 * @param[in] arg Type of the YANG keyword argument (of the value).
953 * @param[in,out] exts Extension instances to add to.
954 *
955 * @return LY_ERR values.
956 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200957static LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +0100958parse_text_field(struct lys_yang_parser_ctx *ctx, enum ly_stmt substmt, uint32_t substmt_index,
Radek Krejci0f969882020-08-21 16:56:47 +0200959 const char **value, enum yang_arg arg, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200960{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100961 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200962 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200963 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200964 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200965
966 if (*value) {
Radek Krejci3972b332021-03-02 16:34:31 +0100967 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200968 return LY_EVALID;
969 }
970
971 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +0100972 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200973
974 /* store value and spend buf if allocated */
Radek Krejci011e4aa2020-09-04 15:22:31 +0200975 INSERT_WORD_RET(ctx, buf, *value, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200976
aPieceka24a2252021-05-07 10:52:31 +0200977 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200978 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200979 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +0100980 LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, substmt_index, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200981 break;
982 default:
Radek Krejci3972b332021-03-02 16:34:31 +0100983 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200984 return LY_EVALID;
985 }
986 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200987 return ret;
988}
989
Michal Vaskoea5abea2018-09-18 13:10:54 +0200990/**
991 * @brief Parse the yang-version statement.
992 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200993 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100994 * @param[out] version Storage for the parsed information.
Michal Vasko63f3d842020-07-08 10:10:14 +0200995 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200996 *
997 * @return LY_ERR values.
998 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200999static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001000parse_yangversion(struct lys_yang_parser_ctx *ctx, uint8_t *version, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001001{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001002 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001003 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001004 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001005 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001006
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001007 if (*version) {
David Sedlákb3077192019-06-19 10:55:37 +02001008 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001009 return LY_EVALID;
1010 }
1011
1012 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001013 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001014
Radek Krejci96e48da2020-09-04 13:18:06 +02001015 if ((word_len == 1) && !strncmp(word, "1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001016 *version = LYS_VERSION_1_0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001017 } else if ((word_len == ly_strlen_const("1.1")) && !strncmp(word, "1.1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001018 *version = LYS_VERSION_1_1;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001019 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001020 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001021 free(buf);
1022 return LY_EVALID;
1023 }
1024 free(buf);
1025
aPieceka24a2252021-05-07 10:52:31 +02001026 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001027 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001028 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001029 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_YANG_VERSION, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001030 break;
1031 default:
David Sedlákb3077192019-06-19 10:55:37 +02001032 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001033 return LY_EVALID;
1034 }
1035 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001036 return ret;
1037}
1038
Michal Vaskoea5abea2018-09-18 13:10:54 +02001039/**
1040 * @brief Parse the belongs-to statement.
1041 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001042 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001043 * @param[in,out] prefix Place to store the parsed belongs-to prefix value.
1044 * @param[in,out] exts Extension instances to add to.
1045 *
1046 * @return LY_ERR values.
1047 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001048static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001049parse_belongsto(struct lys_yang_parser_ctx *ctx, const char **prefix, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001050{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001051 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001052 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001053 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001054 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001055
Michal Vaskoc3781c32020-10-06 14:04:08 +02001056 if (*prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001057 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001058 return LY_EVALID;
1059 }
1060
Michal Vaskoc3781c32020-10-06 14:04:08 +02001061 /* get value, it must match the main module */
Radek Krejci33090f92020-12-17 20:12:46 +01001062 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001063 if (ly_strncmp(ctx->parsed_mod->mod->name, word, word_len)) {
Michal Vaskoc3781c32020-10-06 14:04:08 +02001064 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 +02001065 (int)word_len, word, ctx->parsed_mod->mod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02001066 free(buf);
1067 return LY_EVALID;
1068 }
1069 free(buf);
Radek Krejcif09e4e82019-06-14 15:08:11 +02001070
aPieceka24a2252021-05-07 10:52:31 +02001071 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001072 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001073 case LY_STMT_PREFIX:
Radek Krejcifc596f92021-02-26 22:40:26 +01001074 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_PREFIX, 0, prefix, Y_IDENTIF_ARG, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001075 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001076 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001077 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_BELONGS_TO, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001078 break;
1079 default:
David Sedlákb3077192019-06-19 10:55:37 +02001080 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001081 return LY_EVALID;
1082 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001083 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001084 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001085checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001086 /* mandatory substatements */
1087 if (!*prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001088 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001089 return LY_EVALID;
1090 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001091 return ret;
1092}
1093
Michal Vaskoea5abea2018-09-18 13:10:54 +02001094/**
1095 * @brief Parse the revision-date statement.
1096 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001097 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001098 * @param[in,out] rev Array to store the parsed value in.
1099 * @param[in,out] exts Extension instances to add to.
1100 *
1101 * @return LY_ERR values.
1102 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001103static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001104parse_revisiondate(struct lys_yang_parser_ctx *ctx, char *rev, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001105{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001106 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001107 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001108 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001109 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001110
1111 if (rev[0]) {
David Sedlákb3077192019-06-19 10:55:37 +02001112 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001113 return LY_EVALID;
1114 }
1115
1116 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001117 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001118
1119 /* check value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001120 if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision-date")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001121 free(buf);
1122 return LY_EVALID;
1123 }
1124
1125 /* store value and spend buf if allocated */
1126 strncpy(rev, word, word_len);
1127 free(buf);
1128
aPieceka24a2252021-05-07 10:52:31 +02001129 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001130 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001131 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001132 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_REVISION_DATE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001133 break;
1134 default:
David Sedlákb3077192019-06-19 10:55:37 +02001135 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001136 return LY_EVALID;
1137 }
1138 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001139 return ret;
1140}
1141
Michal Vaskoea5abea2018-09-18 13:10:54 +02001142/**
1143 * @brief Parse the include statement.
1144 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001145 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001146 * @param[in] module_name Name of the module to check name collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001147 * @param[in,out] includes Parsed includes to add to.
1148 *
1149 * @return LY_ERR values.
1150 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001151static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001152parse_include(struct lys_yang_parser_ctx *ctx, const char *module_name, struct lysp_include **includes)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001153{
Radek Krejcid33273d2018-10-25 14:55:52 +02001154 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001155 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001156 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001157 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001158 struct lysp_include *inc;
1159
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001160 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *includes, inc, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001161
1162 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001163 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001164
Radek Krejci011e4aa2020-09-04 15:22:31 +02001165 INSERT_WORD_RET(ctx, buf, inc->name, word, word_len);
Radek Krejci086c7132018-10-26 15:29:04 +02001166
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001167 /* submodules share the namespace with the module names, so there must not be
1168 * a module of the same name in the context, no need for revision matching */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001169 if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(PARSER_CTX(ctx), inc->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01001170 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", inc->name);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001171 return LY_EVALID;
1172 }
1173
aPieceka24a2252021-05-07 10:52:31 +02001174 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001175 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001176 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001177 PARSER_CHECK_STMTVER2_RET(ctx, "description", "include");
Radek Krejcifc596f92021-02-26 22:40:26 +01001178 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 +02001179 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001180 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001181 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "include");
Radek Krejcifc596f92021-02-26 22:40:26 +01001182 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 +02001183 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001184 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001185 LY_CHECK_RET(parse_revisiondate(ctx, inc->rev, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001186 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001187 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001188 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_INCLUDE, 0, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001189 break;
1190 default:
David Sedlákb3077192019-06-19 10:55:37 +02001191 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001192 return LY_EVALID;
1193 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001194 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001195 return ret;
1196}
1197
Michal Vaskoea5abea2018-09-18 13:10:54 +02001198/**
1199 * @brief Parse the import statement.
1200 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001201 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001202 * @param[in] module_prefix Prefix of the module to check prefix collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001203 * @param[in,out] imports Parsed imports to add to.
1204 *
1205 * @return LY_ERR values.
1206 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001207static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001208parse_import(struct lys_yang_parser_ctx *ctx, const char *module_prefix, struct lysp_import **imports)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001209{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001210 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001211 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001212 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001213 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001214 struct lysp_import *imp;
1215
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001216 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *imports, imp, LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001217
1218 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001219 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02001220 INSERT_WORD_RET(ctx, buf, imp->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001221
aPieceka24a2252021-05-07 10:52:31 +02001222 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001223 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001224 case LY_STMT_PREFIX:
Radek Krejcifc596f92021-02-26 22:40:26 +01001225 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 +01001226 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 +02001227 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001228 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001229 PARSER_CHECK_STMTVER2_RET(ctx, "description", "import");
Radek Krejcifc596f92021-02-26 22:40:26 +01001230 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 +02001231 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001232 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001233 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "import");
Radek Krejcifc596f92021-02-26 22:40:26 +01001234 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 +02001235 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001236 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001237 LY_CHECK_RET(parse_revisiondate(ctx, imp->rev, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001238 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001239 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001240 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_IMPORT, 0, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001241 break;
1242 default:
David Sedlákb3077192019-06-19 10:55:37 +02001243 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001244 return LY_EVALID;
1245 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001246 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001247 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001248checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001249 /* mandatory substatements */
David Sedlákb3077192019-06-19 10:55:37 +02001250 LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001251
1252 return ret;
1253}
1254
Michal Vaskoea5abea2018-09-18 13:10:54 +02001255/**
1256 * @brief Parse the revision statement.
1257 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001258 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001259 * @param[in,out] revs Parsed revisions to add to.
1260 *
1261 * @return LY_ERR values.
1262 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001263static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001264parse_revision(struct lys_yang_parser_ctx *ctx, struct lysp_revision **revs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001265{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001266 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001267 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001268 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001269 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001270 struct lysp_revision *rev;
1271
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001272 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *revs, rev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001273
1274 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001275 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001276
1277 /* check value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001278 if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision")) {
David Sedlák68ef3dc2019-07-22 13:40:19 +02001279 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001280 return LY_EVALID;
1281 }
1282
Radek Krejcib7db73a2018-10-24 14:18:40 +02001283 strncpy(rev->date, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001284 free(buf);
1285
aPieceka24a2252021-05-07 10:52:31 +02001286 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001287 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001288 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001289 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 +02001290 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001291 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001292 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 +02001293 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001294 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001295 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_REVISION, 0, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001296 break;
1297 default:
David Sedlákb3077192019-06-19 10:55:37 +02001298 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001299 return LY_EVALID;
1300 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001301 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001302 return ret;
1303}
1304
Michal Vaskoea5abea2018-09-18 13:10:54 +02001305/**
1306 * @brief Parse a generic text field that can have more instances such as base.
1307 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001308 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001309 * @param[in] substmt Type of this substatement.
1310 * @param[in,out] texts Parsed values to add to.
1311 * @param[in] arg Type of the expected argument.
1312 * @param[in,out] exts Extension instances to add to.
1313 *
1314 * @return LY_ERR values.
1315 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001316static LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +01001317parse_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 +02001318 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001319{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001320 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001321 char *buf, *word;
Radek Krejci151a5b72018-10-19 14:21:44 +02001322 const char **item;
1323 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001324 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001325
1326 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001327 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *texts, item, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001328
1329 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001330 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001331
Radek Krejci011e4aa2020-09-04 15:22:31 +02001332 INSERT_WORD_RET(ctx, buf, *item, word, word_len);
aPieceka24a2252021-05-07 10:52:31 +02001333 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001334 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001335 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001336 LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, LY_ARRAY_COUNT(*texts) - 1, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001337 break;
1338 default:
Radek Krejci3972b332021-03-02 16:34:31 +01001339 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001340 return LY_EVALID;
1341 }
1342 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001343 return ret;
1344}
1345
Michal Vaskoea5abea2018-09-18 13:10:54 +02001346/**
Michal Vasko7f45cf22020-10-01 12:49:44 +02001347 * @brief Parse a generic text field that can have more instances such as base.
1348 *
1349 * @param[in] ctx yang parser context for logging.
Michal Vasko7f45cf22020-10-01 12:49:44 +02001350 * @param[in] substmt Type of this substatement.
1351 * @param[in,out] qnames Parsed qnames to add to.
1352 * @param[in] arg Type of the expected argument.
1353 * @param[in,out] exts Extension instances to add to.
1354 *
1355 * @return LY_ERR values.
1356 */
1357static LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +01001358parse_qnames(struct lys_yang_parser_ctx *ctx, enum ly_stmt substmt, struct lysp_qname **qnames,
Michal Vasko7f45cf22020-10-01 12:49:44 +02001359 enum yang_arg arg, struct lysp_ext_instance **exts)
1360{
1361 LY_ERR ret = LY_SUCCESS;
1362 char *buf, *word;
1363 struct lysp_qname *item;
1364 size_t word_len;
1365 enum ly_stmt kw;
1366
1367 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001368 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *qnames, item, LY_EMEM);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001369
1370 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001371 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001372
1373 INSERT_WORD_RET(ctx, buf, item->str, word, word_len);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001374 item->mod = ctx->parsed_mod;
aPieceka24a2252021-05-07 10:52:31 +02001375 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02001376 switch (kw) {
1377 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001378 LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, LY_ARRAY_COUNT(*qnames) - 1, exts));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001379 break;
1380 default:
Radek Krejci3972b332021-03-02 16:34:31 +01001381 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(substmt));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001382 return LY_EVALID;
1383 }
1384 }
1385 return ret;
1386}
1387
1388/**
Michal Vaskoea5abea2018-09-18 13:10:54 +02001389 * @brief Parse the config statement.
1390 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001391 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001392 * @param[in,out] flags Flags to add to.
1393 * @param[in,out] exts Extension instances to add to.
1394 *
1395 * @return LY_ERR values.
1396 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001397static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001398parse_config(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001399{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001400 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001401 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001402 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001403 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001404
1405 if (*flags & LYS_CONFIG_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001406 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001407 return LY_EVALID;
1408 }
1409
1410 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001411 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001412
Radek Krejcif13b87b2020-12-01 22:02:17 +01001413 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001414 *flags |= LYS_CONFIG_W;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001415 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001416 *flags |= LYS_CONFIG_R;
1417 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001418 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001419 free(buf);
1420 return LY_EVALID;
1421 }
1422 free(buf);
1423
aPieceka24a2252021-05-07 10:52:31 +02001424 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001425 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001426 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001427 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_CONFIG, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001428 break;
1429 default:
David Sedlákb3077192019-06-19 10:55:37 +02001430 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001431 return LY_EVALID;
1432 }
1433 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001434 return ret;
1435}
1436
Michal Vaskoea5abea2018-09-18 13:10:54 +02001437/**
1438 * @brief Parse the mandatory statement.
1439 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001440 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001441 * @param[in,out] flags Flags to add to.
1442 * @param[in,out] exts Extension instances to add to.
1443 *
1444 * @return LY_ERR values.
1445 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001446static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001447parse_mandatory(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001448{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001449 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001450 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001451 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001452 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001453
1454 if (*flags & LYS_MAND_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001455 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001456 return LY_EVALID;
1457 }
1458
1459 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001460 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001461
Radek Krejcif13b87b2020-12-01 22:02:17 +01001462 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001463 *flags |= LYS_MAND_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001464 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001465 *flags |= LYS_MAND_FALSE;
1466 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001467 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001468 free(buf);
1469 return LY_EVALID;
1470 }
1471 free(buf);
1472
aPieceka24a2252021-05-07 10:52:31 +02001473 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001474 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001475 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001476 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MANDATORY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001477 break;
1478 default:
David Sedlákb3077192019-06-19 10:55:37 +02001479 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001480 return LY_EVALID;
1481 }
1482 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001483 return ret;
1484}
1485
Michal Vaskoea5abea2018-09-18 13:10:54 +02001486/**
1487 * @brief Parse a restriction such as range or length.
1488 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001489 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001490 * @param[in] restr_kw Type of this particular restriction.
1491 * @param[in,out] exts Extension instances to add to.
1492 *
1493 * @return LY_ERR values.
1494 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001495static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001496parse_restr(struct lys_yang_parser_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr *restr)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001497{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001498 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001499 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001500 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001501 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001502
1503 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001504 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001505
Michal Vaskob36053d2020-03-26 15:49:30 +01001506 CHECK_NONEMPTY(ctx, word_len, ly_stmt2str(restr_kw));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001507 INSERT_WORD_RET(ctx, buf, restr->arg.str, word, word_len);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001508 restr->arg.mod = ctx->parsed_mod;
aPieceka24a2252021-05-07 10:52:31 +02001509 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001510 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001511 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001512 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 +02001513 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001514 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001515 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 +02001516 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001517 case LY_STMT_ERROR_APP_TAG:
Radek Krejcifc596f92021-02-26 22:40:26 +01001518 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 +02001519 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001520 case LY_STMT_ERROR_MESSAGE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001521 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 +02001522 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001523 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001524 LY_CHECK_RET(parse_ext(ctx, word, word_len, restr_kw, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001525 break;
1526 default:
David Sedlákb3077192019-06-19 10:55:37 +02001527 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001528 return LY_EVALID;
1529 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001530 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001531 return ret;
1532}
1533
Michal Vaskoea5abea2018-09-18 13:10:54 +02001534/**
1535 * @brief Parse a restriction that can have more instances such as must.
1536 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001537 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001538 * @param[in] restr_kw Type of this particular restriction.
1539 * @param[in,out] restrs Restrictions to add to.
1540 *
1541 * @return LY_ERR values.
1542 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001543static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001544parse_restrs(struct lys_yang_parser_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr **restrs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001545{
1546 struct lysp_restr *restr;
1547
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001548 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *restrs, restr, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01001549 return parse_restr(ctx, restr_kw, restr);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001550}
1551
Michal Vaskoea5abea2018-09-18 13:10:54 +02001552/**
1553 * @brief Parse the status statement.
1554 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001555 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001556 * @param[in,out] flags Flags to add to.
1557 * @param[in,out] exts Extension instances to add to.
1558 *
1559 * @return LY_ERR values.
1560 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001561static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001562parse_status(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001563{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001564 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001565 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001566 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001567 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001568
1569 if (*flags & LYS_STATUS_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001570 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001571 return LY_EVALID;
1572 }
1573
1574 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001575 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001576
Radek Krejcif13b87b2020-12-01 22:02:17 +01001577 if ((word_len == ly_strlen_const("current")) && !strncmp(word, "current", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001578 *flags |= LYS_STATUS_CURR;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001579 } else if ((word_len == ly_strlen_const("deprecated")) && !strncmp(word, "deprecated", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001580 *flags |= LYS_STATUS_DEPRC;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001581 } else if ((word_len == ly_strlen_const("obsolete")) && !strncmp(word, "obsolete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001582 *flags |= LYS_STATUS_OBSLT;
1583 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001584 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001585 free(buf);
1586 return LY_EVALID;
1587 }
1588 free(buf);
1589
aPieceka24a2252021-05-07 10:52:31 +02001590 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001591 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001592 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001593 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_STATUS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001594 break;
1595 default:
David Sedlákb3077192019-06-19 10:55:37 +02001596 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001597 return LY_EVALID;
1598 }
1599 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001600 return ret;
1601}
1602
Michal Vaskoea5abea2018-09-18 13:10:54 +02001603/**
1604 * @brief Parse the when statement.
1605 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001606 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001607 * @param[in,out] when_p When pointer to parse to.
1608 *
1609 * @return LY_ERR values.
1610 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001611LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001612parse_when(struct lys_yang_parser_ctx *ctx, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001613{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001614 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001615 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001616 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001617 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001618 struct lysp_when *when;
1619
1620 if (*when_p) {
David Sedlákb3077192019-06-19 10:55:37 +02001621 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001622 return LY_EVALID;
1623 }
1624
1625 when = calloc(1, sizeof *when);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001626 LY_CHECK_ERR_RET(!when, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001627 *when_p = when;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001628
1629 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001630 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01001631 CHECK_NONEMPTY(ctx, word_len, "when");
Radek Krejci011e4aa2020-09-04 15:22:31 +02001632 INSERT_WORD_RET(ctx, buf, when->cond, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001633
aPieceka24a2252021-05-07 10:52:31 +02001634 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001635 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001636 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001637 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 +02001638 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001639 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001640 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 +02001641 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001642 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001643 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_WHEN, 0, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001644 break;
1645 default:
David Sedlákb3077192019-06-19 10:55:37 +02001646 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001647 return LY_EVALID;
1648 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001649 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001650 return ret;
1651}
1652
Michal Vaskoea5abea2018-09-18 13:10:54 +02001653/**
1654 * @brief Parse the anydata or anyxml statement.
1655 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001656 * @param[in] ctx yang parser context for logging.
Radek Krejci39b7fc22021-02-26 23:29:18 +01001657 * @param[in] any_kw Type of this particular keyword.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001658 * @param[in,out] siblings Siblings to add to.
1659 *
1660 * @return LY_ERR values.
1661 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02001662LY_ERR
Radek Krejci39b7fc22021-02-26 23:29:18 +01001663parse_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 +02001664{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001665 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001666 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001667 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001668 struct lysp_node_anydata *any;
Radek Krejci39b7fc22021-02-26 23:29:18 +01001669 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001670
David Sedlák60adc092019-08-06 15:57:02 +02001671 /* create new structure and insert into siblings */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001672 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, any, next, LY_EMEM);
David Sedlák60adc092019-08-06 15:57:02 +02001673
Radek Krejci39b7fc22021-02-26 23:29:18 +01001674 any->nodetype = any_kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001675 any->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001676
Michal Vasko7fbc8162018-09-17 10:35:16 +02001677 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01001678 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02001679 INSERT_WORD_RET(ctx, buf, any->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001680
1681 /* parse substatements */
aPieceka24a2252021-05-07 10:52:31 +02001682 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001683 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001684 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01001685 LY_CHECK_RET(parse_config(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001686 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001687 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001688 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 +02001689 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001690 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001691 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &any->iffeatures, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001692 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001693 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01001694 LY_CHECK_RET(parse_mandatory(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001695 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001696 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01001697 LY_CHECK_RET(parse_restrs(ctx, kw, &any->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001698 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001699 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001700 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 +02001701 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001702 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001703 LY_CHECK_RET(parse_status(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001704 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001705 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01001706 LY_CHECK_RET(parse_when(ctx, &any->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001707 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001708 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001709 LY_CHECK_RET(parse_ext(ctx, word, word_len, any_kw, 0, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001710 break;
1711 default:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001712 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(any_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001713 return LY_EVALID;
1714 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001715 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001716 return ret;
1717}
1718
Michal Vaskoea5abea2018-09-18 13:10:54 +02001719/**
1720 * @brief Parse the value or position statement. Substatement of type enum statement.
1721 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001722 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001723 * @param[in] val_kw Type of this particular keyword.
1724 * @param[in,out] value Value to write to.
1725 * @param[in,out] flags Flags to write to.
1726 * @param[in,out] exts Extension instances to add to.
1727 *
1728 * @return LY_ERR values.
1729 */
David Sedlákd6ce6d72019-07-16 17:30:18 +02001730LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001731parse_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 +02001732 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001733{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001734 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001735 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001736 size_t word_len;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02001737 long int num = 0;
1738 unsigned long int unum = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001739 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001740
1741 if (*flags & LYS_SET_VALUE) {
David Sedlákb3077192019-06-19 10:55:37 +02001742 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001743 return LY_EVALID;
1744 }
1745 *flags |= LYS_SET_VALUE;
1746
1747 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001748 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001749
Radek Krejcid6b76452019-09-03 17:03:03 +02001750 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 +02001751 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001752 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001753 }
1754
1755 errno = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001756 if (val_kw == LY_STMT_VALUE) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001757 num = strtol(word, &ptr, LY_BASE_DEC);
Michal Vasko69730152020-10-09 16:30:07 +02001758 if ((num < INT64_C(-2147483648)) || (num > INT64_C(2147483647))) {
David Sedlákb3077192019-06-19 10:55:37 +02001759 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001760 goto error;
1761 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001762 } else {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001763 unum = strtoul(word, &ptr, LY_BASE_DEC);
Radek Krejci8b764662018-11-14 14:15:13 +01001764 if (unum > UINT64_C(4294967295)) {
David Sedlákb3077192019-06-19 10:55:37 +02001765 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001766 goto error;
1767 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001768 }
1769 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001770 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001771 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001772 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001773 }
1774 if (errno == ERANGE) {
David Sedlákb3077192019-06-19 10:55:37 +02001775 LOGVAL_PARSER(ctx, LY_VCODE_OOB, 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 }
Radek Krejcid6b76452019-09-03 17:03:03 +02001778 if (val_kw == LY_STMT_VALUE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001779 *value = num;
1780 } else {
1781 *value = unum;
1782 }
1783 free(buf);
1784
aPieceka24a2252021-05-07 10:52:31 +02001785 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001786 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001787 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001788 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 +02001789 break;
1790 default:
David Sedlákb3077192019-06-19 10:55:37 +02001791 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001792 return LY_EVALID;
1793 }
1794 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001795 return ret;
Radek Krejci8b764662018-11-14 14:15:13 +01001796
1797error:
1798 free(buf);
1799 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001800}
1801
Michal Vaskoea5abea2018-09-18 13:10:54 +02001802/**
1803 * @brief Parse the enum or bit statement. Substatement of type statement.
1804 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001805 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001806 * @param[in] enum_kw Type of this particular keyword.
1807 * @param[in,out] enums Enums or bits to add to.
1808 *
1809 * @return LY_ERR values.
1810 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001811static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001812parse_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 +02001813{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001814 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001815 char *buf, *word;
David Sedlák6544c182019-07-12 13:17:33 +02001816 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001817 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001818 struct lysp_type_enum *enm;
1819
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001820 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *enums, enm, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001821
1822 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001823 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 +02001824 if (enum_kw == LY_STMT_ENUM) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001825 ret = lysp_check_enum_name((struct lys_parser_ctx *)ctx, (const char *)word, word_len);
David Sedlák6544c182019-07-12 13:17:33 +02001826 LY_CHECK_ERR_RET(ret, free(buf), ret);
Radek Krejci335332a2019-09-05 13:03:35 +02001827 } /* else nothing specific for YANG_BIT */
Radek Krejci8b764662018-11-14 14:15:13 +01001828
Radek Krejci011e4aa2020-09-04 15:22:31 +02001829 INSERT_WORD_RET(ctx, buf, enm->name, word, word_len);
Radek Krejci8b764662018-11-14 14:15:13 +01001830 CHECK_UNIQUENESS(ctx, *enums, name, ly_stmt2str(enum_kw), enm->name);
1831
aPieceka24a2252021-05-07 10:52:31 +02001832 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001833 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001834 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001835 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 +02001836 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001837 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02001838 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", ly_stmt2str(enum_kw));
Radek Krejcifc596f92021-02-26 22:40:26 +01001839 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &enm->iffeatures, 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_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001842 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 +02001843 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001844 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001845 LY_CHECK_RET(parse_status(ctx, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001846 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001847 case LY_STMT_VALUE:
1848 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 +02001849 ly_stmt2str(enum_kw)), LY_EVALID);
Radek Krejci33090f92020-12-17 20:12:46 +01001850 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, &enm->value, &enm->flags, &enm->exts));
David Sedlák9fb515f2019-07-11 10:33:58 +02001851 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001852 case LY_STMT_POSITION:
1853 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 +02001854 ly_stmt2str(enum_kw)), LY_EVALID);
Radek Krejci33090f92020-12-17 20:12:46 +01001855 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, &enm->value, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001856 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001857 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001858 LY_CHECK_RET(parse_ext(ctx, word, word_len, enum_kw, 0, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001859 break;
1860 default:
David Sedlákb3077192019-06-19 10:55:37 +02001861 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001862 return LY_EVALID;
1863 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001864 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001865 return ret;
1866}
1867
Michal Vaskoea5abea2018-09-18 13:10:54 +02001868/**
1869 * @brief Parse the fraction-digits statement. Substatement of type statement.
1870 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001871 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001872 * @param[in,out] fracdig Value to write to.
1873 * @param[in,out] exts Extension instances to add to.
1874 *
1875 * @return LY_ERR values.
1876 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001877static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001878parse_type_fracdigits(struct lys_yang_parser_ctx *ctx, uint8_t *fracdig, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001879{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001880 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001881 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001882 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001883 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02001884 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001885
1886 if (*fracdig) {
David Sedlákb3077192019-06-19 10:55:37 +02001887 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001888 return LY_EVALID;
1889 }
1890
1891 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001892 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001893
1894 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
David Sedlákb3077192019-06-19 10:55:37 +02001895 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001896 free(buf);
1897 return LY_EVALID;
1898 }
1899
1900 errno = 0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001901 num = strtoul(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001902 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001903 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001904 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001905 free(buf);
1906 return LY_EVALID;
1907 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001908 if ((errno == ERANGE) || (num > LY_TYPE_DEC64_FD_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02001909 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001910 free(buf);
1911 return LY_EVALID;
1912 }
1913 *fracdig = num;
1914 free(buf);
1915
aPieceka24a2252021-05-07 10:52:31 +02001916 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001917 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001918 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001919 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_FRACTION_DIGITS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001920 break;
1921 default:
David Sedlákb3077192019-06-19 10:55:37 +02001922 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001923 return LY_EVALID;
1924 }
1925 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001926 return ret;
1927}
1928
Michal Vaskoea5abea2018-09-18 13:10:54 +02001929/**
1930 * @brief Parse the require-instance statement. Substatement of type statement.
1931 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001932 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001933 * @param[in,out] reqinst Value to write to.
1934 * @param[in,out] flags Flags to write to.
1935 * @param[in,out] exts Extension instances to add to.
1936 *
1937 * @return LY_ERR values.
1938 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001939static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001940parse_type_reqinstance(struct lys_yang_parser_ctx *ctx, uint8_t *reqinst, uint16_t *flags,
Radek Krejci0f969882020-08-21 16:56:47 +02001941 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001942{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001943 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001944 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001945 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001946 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001947
1948 if (*flags & LYS_SET_REQINST) {
David Sedlákb3077192019-06-19 10:55:37 +02001949 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001950 return LY_EVALID;
1951 }
1952 *flags |= LYS_SET_REQINST;
1953
1954 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001955 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001956
Radek Krejcif13b87b2020-12-01 22:02:17 +01001957 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001958 *reqinst = 1;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001959 } else if ((word_len != ly_strlen_const("false")) || strncmp(word, "false", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001960 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001961 free(buf);
1962 return LY_EVALID;
1963 }
1964 free(buf);
1965
aPieceka24a2252021-05-07 10:52:31 +02001966 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001967 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001968 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001969 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_REQUIRE_INSTANCE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001970 break;
1971 default:
David Sedlákb3077192019-06-19 10:55:37 +02001972 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001973 return LY_EVALID;
1974 }
1975 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001976 return ret;
1977}
1978
Michal Vaskoea5abea2018-09-18 13:10:54 +02001979/**
1980 * @brief Parse the modifier statement. Substatement of type pattern statement.
1981 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001982 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001983 * @param[in,out] pat Value to write to.
1984 * @param[in,out] exts Extension instances to add to.
1985 *
1986 * @return LY_ERR values.
1987 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001988static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001989parse_type_pattern_modifier(struct lys_yang_parser_ctx *ctx, const char **pat, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001990{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001991 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001992 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001993 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001994 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001995
Radek Krejcif13b87b2020-12-01 22:02:17 +01001996 if ((*pat)[0] == LYSP_RESTR_PATTERN_NACK) {
David Sedlákb3077192019-06-19 10:55:37 +02001997 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001998 return LY_EVALID;
1999 }
2000
2001 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002002 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002003
Radek Krejcif13b87b2020-12-01 22:02:17 +01002004 if ((word_len != ly_strlen_const("invert-match")) || strncmp(word, "invert-match", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02002005 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002006 free(buf);
2007 return LY_EVALID;
2008 }
2009 free(buf);
2010
2011 /* replace the value in the dictionary */
2012 buf = malloc(strlen(*pat) + 1);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002013 LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002014 strcpy(buf, *pat);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002015 lydict_remove(PARSER_CTX(ctx), *pat);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002016
Radek Krejcif13b87b2020-12-01 22:02:17 +01002017 assert(buf[0] == LYSP_RESTR_PATTERN_ACK);
2018 buf[0] = LYSP_RESTR_PATTERN_NACK;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002019 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, pat));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002020
aPieceka24a2252021-05-07 10:52:31 +02002021 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002022 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002023 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002024 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MODIFIER, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002025 break;
2026 default:
David Sedlákb3077192019-06-19 10:55:37 +02002027 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002028 return LY_EVALID;
2029 }
2030 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002031 return ret;
2032}
2033
Michal Vaskoea5abea2018-09-18 13:10:54 +02002034/**
2035 * @brief Parse the pattern statement. Substatement of type statement.
2036 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002037 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002038 * @param[in,out] patterns Restrictions to add to.
2039 *
2040 * @return LY_ERR values.
2041 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002042static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002043parse_type_pattern(struct lys_yang_parser_ctx *ctx, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002044{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002045 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002046 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002047 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002048 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002049 struct lysp_restr *restr;
2050
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002051 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *patterns, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002052
2053 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002054 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002055
2056 /* add special meaning first byte */
2057 if (buf) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01002058 buf = ly_realloc(buf, word_len + 2);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002059 word = buf;
2060 } else {
2061 buf = malloc(word_len + 2);
2062 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002063 LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci86d106e2018-10-18 09:53:19 +02002064 memmove(buf + 1, word, word_len);
Radek Krejcif13b87b2020-12-01 22:02:17 +01002065 buf[0] = LYSP_RESTR_PATTERN_ACK; /* pattern's default regular-match flag */
Radek Krejci86d106e2018-10-18 09:53:19 +02002066 buf[word_len + 1] = '\0'; /* terminating NULL byte */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002067 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, &restr->arg.str));
2068 restr->arg.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002069
aPieceka24a2252021-05-07 10:52:31 +02002070 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002071 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002072 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002073 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 +02002074 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002075 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002076 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 +02002077 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002078 case LY_STMT_ERROR_APP_TAG:
Radek Krejcifc596f92021-02-26 22:40:26 +01002079 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 +02002080 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002081 case LY_STMT_ERROR_MESSAGE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002082 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 +02002083 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002084 case LY_STMT_MODIFIER:
Radek Krejci335332a2019-09-05 13:03:35 +02002085 PARSER_CHECK_STMTVER2_RET(ctx, "modifier", "pattern");
Radek Krejci33090f92020-12-17 20:12:46 +01002086 LY_CHECK_RET(parse_type_pattern_modifier(ctx, &restr->arg.str, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002087 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002088 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002089 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_PATTERN, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002090 break;
2091 default:
David Sedlákb3077192019-06-19 10:55:37 +02002092 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002093 return LY_EVALID;
2094 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002095 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002096 return ret;
2097}
2098
Michal Vaskoea5abea2018-09-18 13:10:54 +02002099/**
2100 * @brief Parse the type statement.
2101 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002102 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002103 * @param[in,out] type Type to wrote to.
2104 *
2105 * @return LY_ERR values.
2106 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002107static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002108parse_type(struct lys_yang_parser_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002109{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002110 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002111 char *buf, *word;
Michal Vasko004d3152020-06-11 19:59:22 +02002112 const char *str_path = NULL;
Radek Krejciefd22f62018-09-27 11:47:58 +02002113 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002114 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002115 struct lysp_type *nest_type;
2116
2117 if (type->name) {
David Sedlákb3077192019-06-19 10:55:37 +02002118 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002119 return LY_EVALID;
2120 }
2121
2122 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002123 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002124 INSERT_WORD_RET(ctx, buf, type->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002125
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002126 /* set module */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002127 type->pmod = ctx->parsed_mod;
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002128
aPieceka24a2252021-05-07 10:52:31 +02002129 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002130 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002131 case LY_STMT_BASE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002132 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 +01002133 type->flags |= LYS_SET_BASE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002134 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002135 case LY_STMT_BIT:
Radek Krejci33090f92020-12-17 20:12:46 +01002136 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->bits));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002137 type->flags |= LYS_SET_BIT;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002138 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002139 case LY_STMT_ENUM:
Radek Krejci33090f92020-12-17 20:12:46 +01002140 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->enums));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002141 type->flags |= LYS_SET_ENUM;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002142 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002143 case LY_STMT_FRACTION_DIGITS:
Radek Krejci33090f92020-12-17 20:12:46 +01002144 LY_CHECK_RET(parse_type_fracdigits(ctx, &type->fraction_digits, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002145 type->flags |= LYS_SET_FRDIGITS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002146 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002147 case LY_STMT_LENGTH:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002148 if (type->length) {
David Sedlákb3077192019-06-19 10:55:37 +02002149 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002150 return LY_EVALID;
2151 }
2152 type->length = calloc(1, sizeof *type->length);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002153 LY_CHECK_ERR_RET(!type->length, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002154
Radek Krejci33090f92020-12-17 20:12:46 +01002155 LY_CHECK_RET(parse_restr(ctx, kw, type->length));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002156 type->flags |= LYS_SET_LENGTH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002157 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002158 case LY_STMT_PATH:
Michal Vasko004d3152020-06-11 19:59:22 +02002159 if (type->path) {
Radek Krejci3972b332021-03-02 16:34:31 +01002160 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(LY_STMT_PATH));
Michal Vasko004d3152020-06-11 19:59:22 +02002161 return LY_EVALID;
2162 }
2163
aPiecek4bb1e372021-05-07 11:01:00 +02002164 /* Usually, in the parser_yang.c, the result of the parsing is stored directly in the
2165 * corresponding structure, so in case of failure, the lysp_module_free function will take
2166 * care of removing the parsed value from the dictionary. But in this case, it is not possible
2167 * to rely on lysp_module_free because the result of the parsing is stored in a local variable.
2168 */
2169 LY_CHECK_ERR_RET(ret = parse_text_field(ctx, LY_STMT_PATH, 0, &str_path, Y_STR_ARG, &type->exts),
2170 lydict_remove(PARSER_CTX(ctx), str_path), ret);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002171 ret = ly_path_parse(PARSER_CTX(ctx), NULL, str_path, 0, LY_PATH_BEGIN_EITHER, LY_PATH_LREF_TRUE,
Michal Vasko69730152020-10-09 16:30:07 +02002172 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path);
aPiecek4bb1e372021-05-07 11:01:00 +02002173 /* Moreover, even if successful, the string is removed from the dictionary. */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002174 lydict_remove(PARSER_CTX(ctx), str_path);
Michal Vasko004d3152020-06-11 19:59:22 +02002175 LY_CHECK_RET(ret);
Radek Krejcid505e3d2018-11-13 09:04:17 +01002176 type->flags |= LYS_SET_PATH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002177 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002178 case LY_STMT_PATTERN:
Radek Krejci33090f92020-12-17 20:12:46 +01002179 LY_CHECK_RET(parse_type_pattern(ctx, &type->patterns));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002180 type->flags |= LYS_SET_PATTERN;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002181 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002182 case LY_STMT_RANGE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002183 if (type->range) {
David Sedlákb3077192019-06-19 10:55:37 +02002184 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002185 return LY_EVALID;
2186 }
2187 type->range = calloc(1, sizeof *type->range);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002188 LY_CHECK_ERR_RET(!type->range, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002189
Radek Krejci33090f92020-12-17 20:12:46 +01002190 LY_CHECK_RET(parse_restr(ctx, kw, type->range));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002191 type->flags |= LYS_SET_RANGE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002192 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002193 case LY_STMT_REQUIRE_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002194 LY_CHECK_RET(parse_type_reqinstance(ctx, &type->require_instance, &type->flags, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002195 /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002196 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002197 case LY_STMT_TYPE:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002198 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), type->types, nest_type, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01002199 LY_CHECK_RET(parse_type(ctx, nest_type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002200 type->flags |= LYS_SET_TYPE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002201 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002202 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002203 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_TYPE, 0, &type->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002204 break;
2205 default:
David Sedlákb3077192019-06-19 10:55:37 +02002206 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002207 return LY_EVALID;
2208 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002209 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002210 return ret;
2211}
2212
Michal Vaskoea5abea2018-09-18 13:10:54 +02002213/**
2214 * @brief Parse the leaf statement.
2215 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002216 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002217 * @param[in,out] siblings Siblings to add to.
2218 *
2219 * @return LY_ERR values.
2220 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002221LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002222parse_leaf(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002223{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002224 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002225 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002226 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002227 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002228 struct lysp_node_leaf *leaf;
2229
David Sedlák60adc092019-08-06 15:57:02 +02002230 /* create new leaf structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002231 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, leaf, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002232 leaf->nodetype = LYS_LEAF;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002233 leaf->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002234
Michal Vasko7fbc8162018-09-17 10:35:16 +02002235 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002236 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002237 INSERT_WORD_RET(ctx, buf, leaf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002238
2239 /* parse substatements */
aPieceka24a2252021-05-07 10:52:31 +02002240 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002241 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002242 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002243 LY_CHECK_RET(parse_config(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002244 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002245 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01002246 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 +02002247 leaf->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002248 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002249 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002250 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 +02002251 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002252 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002253 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &leaf->iffeatures, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002254 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002255 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002256 LY_CHECK_RET(parse_mandatory(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002257 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002258 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002259 LY_CHECK_RET(parse_restrs(ctx, kw, &leaf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002260 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002261 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002262 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 +02002263 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002264 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002265 LY_CHECK_RET(parse_status(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002266 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002267 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002268 LY_CHECK_RET(parse_type(ctx, &leaf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002269 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002270 case LY_STMT_UNITS:
Radek Krejcifc596f92021-02-26 22:40:26 +01002271 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 +02002272 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002273 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002274 LY_CHECK_RET(parse_when(ctx, &leaf->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002275 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002276 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002277 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_LEAF, 0, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002278 break;
2279 default:
David Sedlákb3077192019-06-19 10:55:37 +02002280 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002281 return LY_EVALID;
2282 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002283 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002284 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002285checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002286 /* mandatory substatements */
2287 if (!leaf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002288 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002289 return LY_EVALID;
2290 }
2291
2292 return ret;
2293}
2294
Michal Vaskoea5abea2018-09-18 13:10:54 +02002295/**
2296 * @brief Parse the max-elements statement.
2297 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002298 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002299 * @param[in,out] max Value to write to.
2300 * @param[in,out] flags Flags to write to.
2301 * @param[in,out] exts Extension instances to add to.
2302 *
2303 * @return LY_ERR values.
2304 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002305LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002306parse_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 +02002307{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002308 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002309 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002310 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002311 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002312 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002313
2314 if (*flags & LYS_SET_MAX) {
David Sedlákb3077192019-06-19 10:55:37 +02002315 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002316 return LY_EVALID;
2317 }
2318 *flags |= LYS_SET_MAX;
2319
2320 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002321 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002322
2323 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
David Sedlákb3077192019-06-19 10:55:37 +02002324 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002325 free(buf);
2326 return LY_EVALID;
2327 }
2328
Radek Krejci7f9b6512019-09-18 13:11:09 +02002329 if (ly_strncmp("unbounded", word, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002330 errno = 0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002331 num = strtoul(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002332 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002333 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002334 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002335 free(buf);
2336 return LY_EVALID;
2337 }
2338 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002339 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002340 free(buf);
2341 return LY_EVALID;
2342 }
2343
2344 *max = num;
Radek Krejcid6315102021-02-02 15:26:34 +01002345 } else {
2346 /* unbounded */
2347 *max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002348 }
2349 free(buf);
2350
aPieceka24a2252021-05-07 10:52:31 +02002351 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002352 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002353 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002354 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MAX_ELEMENTS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002355 break;
2356 default:
David Sedlákb3077192019-06-19 10:55:37 +02002357 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002358 return LY_EVALID;
2359 }
2360 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002361 return ret;
2362}
2363
Michal Vaskoea5abea2018-09-18 13:10:54 +02002364/**
2365 * @brief Parse the min-elements statement.
2366 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002367 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002368 * @param[in,out] min Value to write to.
2369 * @param[in,out] flags Flags to write to.
2370 * @param[in,out] exts Extension instances to add to.
2371 *
2372 * @return LY_ERR values.
2373 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002374LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002375parse_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 +02002376{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002377 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002378 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002379 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002380 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002381 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002382
2383 if (*flags & LYS_SET_MIN) {
David Sedlákb3077192019-06-19 10:55:37 +02002384 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002385 return LY_EVALID;
2386 }
2387 *flags |= LYS_SET_MIN;
2388
2389 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002390 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002391
2392 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
David Sedlákb3077192019-06-19 10:55:37 +02002393 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002394 free(buf);
2395 return LY_EVALID;
2396 }
2397
2398 errno = 0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002399 num = strtoul(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002400 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002401 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002402 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002403 free(buf);
2404 return LY_EVALID;
2405 }
2406 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002407 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002408 free(buf);
2409 return LY_EVALID;
2410 }
2411 *min = num;
2412 free(buf);
2413
aPieceka24a2252021-05-07 10:52:31 +02002414 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002415 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002416 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002417 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MIN_ELEMENTS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002418 break;
2419 default:
David Sedlákb3077192019-06-19 10:55:37 +02002420 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002421 return LY_EVALID;
2422 }
2423 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002424 return ret;
2425}
2426
Michal Vaskoea5abea2018-09-18 13:10:54 +02002427/**
2428 * @brief Parse the ordered-by statement.
2429 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002430 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002431 * @param[in,out] flags Flags to write to.
2432 * @param[in,out] exts Extension instances to add to.
2433 *
2434 * @return LY_ERR values.
2435 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002436static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002437parse_orderedby(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002438{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002439 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002440 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002441 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002442 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002443
2444 if (*flags & LYS_ORDBY_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02002445 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002446 return LY_EVALID;
2447 }
2448
2449 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002450 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002451
Radek Krejcif13b87b2020-12-01 22:02:17 +01002452 if ((word_len == ly_strlen_const("system")) && !strncmp(word, "system", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002453 *flags |= LYS_ORDBY_SYSTEM;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002454 } else if ((word_len == ly_strlen_const("user")) && !strncmp(word, "user", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002455 *flags |= LYS_ORDBY_USER;
2456 } else {
David Sedlákb3077192019-06-19 10:55:37 +02002457 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002458 free(buf);
2459 return LY_EVALID;
2460 }
2461 free(buf);
2462
aPieceka24a2252021-05-07 10:52:31 +02002463 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002464 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002465 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002466 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_ORDERED_BY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002467 break;
2468 default:
David Sedlákb3077192019-06-19 10:55:37 +02002469 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002470 return LY_EVALID;
2471 }
2472 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002473 return ret;
2474}
2475
Michal Vaskoea5abea2018-09-18 13:10:54 +02002476/**
2477 * @brief Parse the leaf-list statement.
2478 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002479 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002480 * @param[in,out] siblings Siblings to add to.
2481 *
2482 * @return LY_ERR values.
2483 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002484LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002485parse_leaflist(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002486{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002487 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002488 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002489 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002490 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002491 struct lysp_node_leaflist *llist;
2492
David Sedlák60adc092019-08-06 15:57:02 +02002493 /* create new leaf-list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002494 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, llist, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002495 llist->nodetype = LYS_LEAFLIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002496 llist->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002497
Michal Vasko7fbc8162018-09-17 10:35:16 +02002498 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002499 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002500 INSERT_WORD_RET(ctx, buf, llist->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002501
2502 /* parse substatements */
aPieceka24a2252021-05-07 10:52:31 +02002503 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002504 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002505 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002506 LY_CHECK_RET(parse_config(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002507 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002508 case LY_STMT_DEFAULT:
Radek Krejci335332a2019-09-05 13:03:35 +02002509 PARSER_CHECK_STMTVER2_RET(ctx, "default", "leaf-list");
Radek Krejcifc596f92021-02-26 22:40:26 +01002510 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, &llist->dflts, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002511 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002512 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002513 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 +02002514 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002515 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002516 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &llist->iffeatures, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002517 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002518 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002519 LY_CHECK_RET(parse_maxelements(ctx, &llist->max, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002520 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002521 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002522 LY_CHECK_RET(parse_minelements(ctx, &llist->min, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002523 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002524 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002525 LY_CHECK_RET(parse_restrs(ctx, kw, &llist->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002526 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002527 case LY_STMT_ORDERED_BY:
Radek Krejci33090f92020-12-17 20:12:46 +01002528 LY_CHECK_RET(parse_orderedby(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002529 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002530 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002531 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 +02002532 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002533 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002534 LY_CHECK_RET(parse_status(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002535 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002536 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002537 LY_CHECK_RET(parse_type(ctx, &llist->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002538 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002539 case LY_STMT_UNITS:
Radek Krejcifc596f92021-02-26 22:40:26 +01002540 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 +02002541 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002542 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002543 LY_CHECK_RET(parse_when(ctx, &llist->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002544 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002545 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002546 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_LEAF_LIST, 0, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002547 break;
2548 default:
David Sedlákb3077192019-06-19 10:55:37 +02002549 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002550 return LY_EVALID;
2551 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002552 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002553 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002554checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002555 /* mandatory substatements */
2556 if (!llist->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002557 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002558 return LY_EVALID;
2559 }
2560
2561 return ret;
2562}
2563
Michal Vaskoea5abea2018-09-18 13:10:54 +02002564/**
2565 * @brief Parse the refine statement.
2566 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002567 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002568 * @param[in,out] refines Refines to add to.
2569 *
2570 * @return LY_ERR values.
2571 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002572static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002573parse_refine(struct lys_yang_parser_ctx *ctx, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002574{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002575 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002576 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002577 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002578 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002579 struct lysp_refine *rf;
2580
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002581 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002582
2583 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002584 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01002585 CHECK_NONEMPTY(ctx, word_len, "refine");
Radek Krejci011e4aa2020-09-04 15:22:31 +02002586 INSERT_WORD_RET(ctx, buf, rf->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002587
aPieceka24a2252021-05-07 10:52:31 +02002588 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002589 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002590 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002591 LY_CHECK_RET(parse_config(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002592 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002593 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01002594 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, &rf->dflts, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002595 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002596 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002597 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 +02002598 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002599 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02002600 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "refine");
Radek Krejcifc596f92021-02-26 22:40:26 +01002601 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &rf->iffeatures, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002602 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002603 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002604 LY_CHECK_RET(parse_maxelements(ctx, &rf->max, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002605 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002606 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002607 LY_CHECK_RET(parse_minelements(ctx, &rf->min, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002608 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002609 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002610 LY_CHECK_RET(parse_restrs(ctx, kw, &rf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002611 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002612 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002613 LY_CHECK_RET(parse_mandatory(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002614 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002615 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002616 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 +02002617 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002618 case LY_STMT_PRESENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002619 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 +02002620 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002621 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002622 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_REFINE, 0, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002623 break;
2624 default:
David Sedlákb3077192019-06-19 10:55:37 +02002625 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002626 return LY_EVALID;
2627 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002628 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002629 return ret;
2630}
2631
Michal Vaskoea5abea2018-09-18 13:10:54 +02002632/**
2633 * @brief Parse the typedef statement.
2634 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002635 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002636 * @param[in,out] typedefs Typedefs to add to.
2637 *
2638 * @return LY_ERR values.
2639 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002640static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002641parse_typedef(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002642{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002643 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002644 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002645 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002646 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002647 struct lysp_tpdf *tpdf;
2648
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002649 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002650
2651 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002652 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002653 INSERT_WORD_RET(ctx, buf, tpdf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002654
2655 /* parse substatements */
aPieceka24a2252021-05-07 10:52:31 +02002656 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002657 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002658 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01002659 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 +02002660 tpdf->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002661 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002662 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002663 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 +02002664 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002665 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002666 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 +02002667 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002668 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002669 LY_CHECK_RET(parse_status(ctx, &tpdf->flags, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002670 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002671 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002672 LY_CHECK_RET(parse_type(ctx, &tpdf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002673 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002674 case LY_STMT_UNITS:
Radek Krejcifc596f92021-02-26 22:40:26 +01002675 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 +02002676 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002677 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002678 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_TYPEDEF, 0, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002679 break;
2680 default:
David Sedlákb3077192019-06-19 10:55:37 +02002681 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002682 return LY_EVALID;
2683 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002684 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002685 LY_CHECK_RET(ret);
Radek Krejcibbe09a92018-11-08 09:36:54 +01002686checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002687 /* mandatory substatements */
2688 if (!tpdf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002689 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002690 return LY_EVALID;
2691 }
2692
Radek Krejcibbe09a92018-11-08 09:36:54 +01002693 /* store data for collision check */
Michal Vasko7f45cf22020-10-01 12:49:44 +02002694 if (parent && !(parent->nodetype & (LYS_GROUPING | LYS_RPC | LYS_ACTION | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF))) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02002695 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, parent, 0, NULL));
Radek Krejcibbe09a92018-11-08 09:36:54 +01002696 }
2697
Michal Vasko7fbc8162018-09-17 10:35:16 +02002698 return ret;
2699}
2700
Michal Vaskoea5abea2018-09-18 13:10:54 +02002701/**
2702 * @brief Parse the input or output statement.
2703 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002704 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002705 * @param[in] kw Type of this particular keyword
2706 * @param[in,out] inout_p Input/output pointer to write to.
2707 *
2708 * @return LY_ERR values.
2709 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002710static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002711parse_inout(struct lys_yang_parser_ctx *ctx, enum ly_stmt inout_kw, struct lysp_node *parent,
Radek Krejci2a9fc652021-01-22 17:44:34 +01002712 struct lysp_node_action_inout *inout_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002713{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002714 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002715 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002716 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002717 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002718 ly_bool input = &((struct lysp_node_action *)parent)->input == inout_p ? 1 : 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002719
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002720 if (inout_p->nodetype) {
David Sedlákb3077192019-06-19 10:55:37 +02002721 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002722 return LY_EVALID;
2723 }
2724
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002725 /* initiate structure */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002726 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), input ? "input" : "output", 0, &inout_p->name));
2727 inout_p->nodetype = input ? LYS_INPUT : LYS_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002728 inout_p->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002729
2730 /* parse substatements */
aPieceka24a2252021-05-07 10:52:31 +02002731 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002732 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002733 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002734 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", ly_stmt2str(inout_kw));
Radek Krejci0f969882020-08-21 16:56:47 +02002735 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002736 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002737 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002738 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002739 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01002740 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002741 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002742 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002743 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002744 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002745 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01002746 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002747 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002748 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002749 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002750 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002751 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002752 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002753 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002754 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01002755 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002756 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002757 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002758 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)inout_p, &inout_p->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002759 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002760 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002761 PARSER_CHECK_STMTVER2_RET(ctx, "must", ly_stmt2str(inout_kw));
Radek Krejci33090f92020-12-17 20:12:46 +01002762 LY_CHECK_RET(parse_restrs(ctx, kw, &inout_p->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002763 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002764 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002765 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)inout_p, &inout_p->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002766 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002767 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002768 LY_CHECK_RET(parse_ext(ctx, word, word_len, inout_kw, 0, &inout_p->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002769 break;
2770 default:
David Sedlákb3077192019-06-19 10:55:37 +02002771 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002772 return LY_EVALID;
2773 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002774 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002775 LY_CHECK_RET(ret);
Michal Vaskob83af8a2020-01-06 09:49:22 +01002776
Radek Krejci7fc68292019-06-12 13:51:09 +02002777checks:
Radek Krejci01180ac2021-01-27 08:48:22 +01002778 if (!inout_p->child) {
Michal Vaskob83af8a2020-01-06 09:49:22 +01002779 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "data-def-stmt", ly_stmt2str(inout_kw));
2780 return LY_EVALID;
2781 }
2782
Michal Vasko7fbc8162018-09-17 10:35:16 +02002783 return ret;
2784}
2785
Michal Vaskoea5abea2018-09-18 13:10:54 +02002786/**
2787 * @brief Parse the action statement.
2788 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002789 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002790 * @param[in,out] actions Actions to add to.
2791 *
2792 * @return LY_ERR values.
2793 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002794LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002795parse_action(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002796{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002797 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002798 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002799 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002800 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002801 struct lysp_node_action *act;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002802
Radek Krejci2a9fc652021-01-22 17:44:34 +01002803 LY_LIST_NEW_RET(PARSER_CTX(ctx), actions, act, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002804
2805 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002806 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002807 INSERT_WORD_RET(ctx, buf, act->name, word, word_len);
Michal Vasko1bf09392020-03-27 12:38:10 +01002808 act->nodetype = parent ? LYS_ACTION : LYS_RPC;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002809 act->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002810
aPieceka24a2252021-05-07 10:52:31 +02002811 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002812 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002813 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002814 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 +02002815 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002816 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002817 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &act->iffeatures, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002818 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002819 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002820 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 +02002821 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002822 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002823 LY_CHECK_RET(parse_status(ctx, &act->flags, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002824 break;
2825
Radek Krejcid6b76452019-09-03 17:03:03 +02002826 case LY_STMT_INPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002827 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->input));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002828 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002829 case LY_STMT_OUTPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002830 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->output));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002831 break;
2832
Radek Krejcid6b76452019-09-03 17:03:03 +02002833 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002834 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)act, &act->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002835 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002836 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002837 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)act, &act->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002838 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002839 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002840 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 +02002841 break;
2842 default:
David Sedlákb3077192019-06-19 10:55:37 +02002843 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), parent ? "action" : "rpc");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002844 return LY_EVALID;
2845 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002846 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002847 LY_CHECK_RET(ret);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002848
Radek Krejci2a9fc652021-01-22 17:44:34 +01002849checks:
Michal Vasko7f45cf22020-10-01 12:49:44 +02002850 /* always initialize inout, they are technically present (needed for later deviations/refines) */
2851 if (!act->input.nodetype) {
2852 act->input.nodetype = LYS_INPUT;
2853 act->input.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002854 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "input", 0, &act->input.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002855 }
2856 if (!act->output.nodetype) {
2857 act->output.nodetype = LYS_OUTPUT;
2858 act->output.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002859 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "output", 0, &act->output.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002860 }
2861
Michal Vasko7fbc8162018-09-17 10:35:16 +02002862 return ret;
2863}
2864
Michal Vaskoea5abea2018-09-18 13:10:54 +02002865/**
2866 * @brief Parse the notification statement.
2867 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002868 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002869 * @param[in,out] notifs Notifications to add to.
2870 *
2871 * @return LY_ERR values.
2872 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002873LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002874parse_notif(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002875{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002876 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002877 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002878 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002879 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002880 struct lysp_node_notif *notif;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002881
Radek Krejci2a9fc652021-01-22 17:44:34 +01002882 LY_LIST_NEW_RET(PARSER_CTX(ctx), notifs, notif, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002883
2884 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002885 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002886 INSERT_WORD_RET(ctx, buf, notif->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002887 notif->nodetype = LYS_NOTIF;
2888 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002889
aPieceka24a2252021-05-07 10:52:31 +02002890 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002891 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002892 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002893 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 +02002894 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002895 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002896 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &notif->iffeatures, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002897 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002898 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002899 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 +02002900 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002901 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002902 LY_CHECK_RET(parse_status(ctx, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002903 break;
2904
Radek Krejcid6b76452019-09-03 17:03:03 +02002905 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002906 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "notification");
Radek Krejci0f969882020-08-21 16:56:47 +02002907 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002908 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002909 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002910 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002911 case LY_STMT_CHOICE:
Michal Vasko5ae1e1a2021-02-08 09:53:26 +01002912 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002913 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002914 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002915 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002916 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002917 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01002918 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002919 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002920 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002921 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002922 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002923 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002924 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002925 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002926 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01002927 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002928 break;
2929
Radek Krejcid6b76452019-09-03 17:03:03 +02002930 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002931 PARSER_CHECK_STMTVER2_RET(ctx, "must", "notification");
Radek Krejci33090f92020-12-17 20:12:46 +01002932 LY_CHECK_RET(parse_restrs(ctx, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002933 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002934 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002935 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)notif, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002936 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002937 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002938 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002939 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002940 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002941 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_NOTIFICATION, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002942 break;
2943 default:
David Sedlákb3077192019-06-19 10:55:37 +02002944 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002945 return LY_EVALID;
2946 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002947 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002948
Michal Vasko7fbc8162018-09-17 10:35:16 +02002949 return ret;
2950}
2951
Michal Vaskoea5abea2018-09-18 13:10:54 +02002952/**
2953 * @brief Parse the grouping statement.
2954 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002955 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002956 * @param[in,out] groupings Groupings to add to.
2957 *
2958 * @return LY_ERR values.
2959 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002960LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002961parse_grouping(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002962{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002963 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002964 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002965 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002966 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002967 struct lysp_node_grp *grp;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002968
Radek Krejci2a9fc652021-01-22 17:44:34 +01002969 LY_LIST_NEW_RET(PARSER_CTX(ctx), groupings, grp, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002970
2971 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002972 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002973 INSERT_WORD_RET(ctx, buf, grp->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002974 grp->nodetype = LYS_GROUPING;
2975 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002976
aPieceka24a2252021-05-07 10:52:31 +02002977 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002978 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002979 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002980 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 +02002981 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002982 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002983 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 +02002984 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002985 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002986 LY_CHECK_RET(parse_status(ctx, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002987 break;
2988
Radek Krejcid6b76452019-09-03 17:03:03 +02002989 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002990 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "grouping");
Radek Krejci0f969882020-08-21 16:56:47 +02002991 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002992 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002993 LY_CHECK_RET(parse_any(ctx, kw, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002994 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002995 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01002996 LY_CHECK_RET(parse_choice(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002997 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002998 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002999 LY_CHECK_RET(parse_container(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003000 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003001 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01003002 LY_CHECK_RET(parse_leaf(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003003 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003004 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003005 LY_CHECK_RET(parse_leaflist(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003006 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003007 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003008 LY_CHECK_RET(parse_list(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003009 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003010 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01003011 LY_CHECK_RET(parse_uses(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003012 break;
3013
Radek Krejcid6b76452019-09-03 17:03:03 +02003014 case LY_STMT_TYPEDEF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01003015 LY_CHECK_RET(parse_typedef(ctx, &grp->node, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003016 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003017 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003018 PARSER_CHECK_STMTVER2_RET(ctx, "action", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003019 LY_CHECK_RET(parse_action(ctx, &grp->node, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003020 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003021 case LY_STMT_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01003022 LY_CHECK_RET(parse_grouping(ctx, &grp->node, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003023 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003024 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003025 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003026 LY_CHECK_RET(parse_notif(ctx, &grp->node, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003027 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003028 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003029 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_GROUPING, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003030 break;
3031 default:
David Sedlákb3077192019-06-19 10:55:37 +02003032 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003033 return LY_EVALID;
3034 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003035 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003036
Michal Vasko7fbc8162018-09-17 10:35:16 +02003037 return ret;
3038}
3039
Michal Vaskoea5abea2018-09-18 13:10:54 +02003040/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02003041 * @brief Parse the augment statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003042 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003043 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003044 * @param[in,out] augments Augments to add to.
3045 *
3046 * @return LY_ERR values.
3047 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003048LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01003049parse_augment(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003050{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003051 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003052 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003053 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003054 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003055 struct lysp_node_augment *aug;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003056
Radek Krejci2a9fc652021-01-22 17:44:34 +01003057 LY_LIST_NEW_RET(PARSER_CTX(ctx), augments, aug, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003058
3059 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003060 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01003061 CHECK_NONEMPTY(ctx, word_len, "augment");
Radek Krejci011e4aa2020-09-04 15:22:31 +02003062 INSERT_WORD_RET(ctx, buf, aug->nodeid, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003063 aug->nodetype = LYS_AUGMENT;
3064 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003065
aPieceka24a2252021-05-07 10:52:31 +02003066 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003067 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003068 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003069 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 +02003070 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003071 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003072 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &aug->iffeatures, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003073 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003074 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003075 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 +02003076 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003077 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003078 LY_CHECK_RET(parse_status(ctx, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003079 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003080 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003081 LY_CHECK_RET(parse_when(ctx, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003082 break;
3083
Radek Krejcid6b76452019-09-03 17:03:03 +02003084 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003085 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "augment");
Radek Krejci0f969882020-08-21 16:56:47 +02003086 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003087 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003088 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003089 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003090 case LY_STMT_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003091 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003092 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003093 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003094 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003095 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003096 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003097 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003098 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003099 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003100 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003101 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003102 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003103 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003104 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003105 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003106 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003107 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003108 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003109 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003110 break;
3111
Radek Krejcid6b76452019-09-03 17:03:03 +02003112 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003113 PARSER_CHECK_STMTVER2_RET(ctx, "action", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003114 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003115 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003116 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003117 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003118 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003119 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003120 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003121 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_AUGMENT, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003122 break;
3123 default:
David Sedlákb3077192019-06-19 10:55:37 +02003124 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003125 return LY_EVALID;
3126 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003127 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003128
Michal Vasko7fbc8162018-09-17 10:35:16 +02003129 return ret;
3130}
3131
Michal Vaskoea5abea2018-09-18 13:10:54 +02003132/**
3133 * @brief Parse the uses statement.
3134 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003135 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003136 * @param[in,out] siblings Siblings to add to.
3137 *
3138 * @return LY_ERR values.
3139 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003140LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003141parse_uses(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003142{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003143 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003144 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003145 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003146 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003147 struct lysp_node_uses *uses;
3148
David Sedlák60adc092019-08-06 15:57:02 +02003149 /* create uses structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003150 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, uses, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003151 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003152 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003153
Michal Vasko7fbc8162018-09-17 10:35:16 +02003154 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003155 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003156 INSERT_WORD_RET(ctx, buf, uses->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003157
3158 /* parse substatements */
aPieceka24a2252021-05-07 10:52:31 +02003159 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003160 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003161 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003162 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 +02003163 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003164 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003165 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &uses->iffeatures, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003166 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003167 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003168 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 +02003169 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003170 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003171 LY_CHECK_RET(parse_status(ctx, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003172 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003173 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003174 LY_CHECK_RET(parse_when(ctx, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003175 break;
3176
Radek Krejcid6b76452019-09-03 17:03:03 +02003177 case LY_STMT_REFINE:
Radek Krejci33090f92020-12-17 20:12:46 +01003178 LY_CHECK_RET(parse_refine(ctx, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003179 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003180 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003181 LY_CHECK_RET(parse_augment(ctx, (struct lysp_node *)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003182 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003183 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003184 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_USES, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003185 break;
3186 default:
David Sedlákb3077192019-06-19 10:55:37 +02003187 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003188 return LY_EVALID;
3189 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003190 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003191
Michal Vasko7fbc8162018-09-17 10:35:16 +02003192 return ret;
3193}
3194
Michal Vaskoea5abea2018-09-18 13:10:54 +02003195/**
3196 * @brief Parse the case statement.
3197 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003198 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003199 * @param[in,out] siblings Siblings to add to.
3200 *
3201 * @return LY_ERR values.
3202 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003203LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003204parse_case(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003205{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003206 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003207 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003208 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003209 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003210 struct lysp_node_case *cas;
3211
David Sedlák60adc092019-08-06 15:57:02 +02003212 /* create new case structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003213 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cas, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003214 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003215 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003216
Michal Vasko7fbc8162018-09-17 10:35:16 +02003217 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003218 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003219 INSERT_WORD_RET(ctx, buf, cas->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003220
3221 /* parse substatements */
aPieceka24a2252021-05-07 10:52:31 +02003222 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003223 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003224 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003225 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 +02003226 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003227 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003228 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &cas->iffeatures, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003229 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003230 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003231 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 +02003232 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003233 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003234 LY_CHECK_RET(parse_status(ctx, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003235 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003236 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003237 LY_CHECK_RET(parse_when(ctx, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003238 break;
3239
Radek Krejcid6b76452019-09-03 17:03:03 +02003240 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003241 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "case");
Radek Krejci0f969882020-08-21 16:56:47 +02003242 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003243 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003244 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003245 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003246 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003247 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003248 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003249 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003250 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003251 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003252 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003253 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003254 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003255 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003256 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003257 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003258 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003259 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003260 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003261 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003262 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003263 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003264 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003265 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_CASE, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003266 break;
3267 default:
David Sedlákb3077192019-06-19 10:55:37 +02003268 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003269 return LY_EVALID;
3270 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003271 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003272 return ret;
3273}
3274
Michal Vaskoea5abea2018-09-18 13:10:54 +02003275/**
3276 * @brief Parse the choice statement.
3277 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003278 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003279 * @param[in,out] siblings Siblings to add to.
3280 *
3281 * @return LY_ERR values.
3282 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003283LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003284parse_choice(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003285{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003286 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003287 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003288 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003289 enum ly_stmt kw;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003290 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003291
David Sedlák60adc092019-08-06 15:57:02 +02003292 /* create new choice structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003293 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, choice, next, LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003294 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003295 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003296
Michal Vasko7fbc8162018-09-17 10:35:16 +02003297 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003298 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003299 INSERT_WORD_RET(ctx, buf, choice->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003300
3301 /* parse substatements */
aPieceka24a2252021-05-07 10:52:31 +02003302 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003303 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003304 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003305 LY_CHECK_RET(parse_config(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003306 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003307 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003308 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 +02003309 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003310 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003311 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &choice->iffeatures, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003312 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003313 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01003314 LY_CHECK_RET(parse_mandatory(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003315 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003316 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003317 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 +02003318 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003319 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003320 LY_CHECK_RET(parse_status(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003321 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003322 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003323 LY_CHECK_RET(parse_when(ctx, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003324 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003325 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01003326 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 +02003327 &choice->exts));
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003328 choice->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003329 break;
3330
Radek Krejcid6b76452019-09-03 17:03:03 +02003331 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003332 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "choice");
Radek Krejci0f969882020-08-21 16:56:47 +02003333 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003334 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003335 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003336 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003337 case LY_STMT_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003338 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003339 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003340 case LY_STMT_CHOICE:
Radek Krejci335332a2019-09-05 13:03:35 +02003341 PARSER_CHECK_STMTVER2_RET(ctx, "choice", "choice");
Radek Krejci33090f92020-12-17 20:12:46 +01003342 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003343 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003344 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003345 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003346 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003347 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003348 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003349 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003350 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003351 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003352 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003353 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003354 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003355 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003356 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003357 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_CHOICE, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003358 break;
3359 default:
David Sedlákb3077192019-06-19 10:55:37 +02003360 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003361 return LY_EVALID;
3362 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003363 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003364 return ret;
3365}
3366
Michal Vaskoea5abea2018-09-18 13:10:54 +02003367/**
3368 * @brief Parse the container statement.
3369 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003370 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003371 * @param[in,out] siblings Siblings to add to.
3372 *
3373 * @return LY_ERR values.
3374 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003375LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003376parse_container(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003377{
3378 LY_ERR ret = 0;
3379 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003380 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003381 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003382 struct lysp_node_container *cont;
3383
David Sedlák60adc092019-08-06 15:57:02 +02003384 /* create new container structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003385 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cont, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003386 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003387 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003388
Michal Vasko7fbc8162018-09-17 10:35:16 +02003389 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003390 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003391 INSERT_WORD_RET(ctx, buf, cont->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003392
3393 /* parse substatements */
aPieceka24a2252021-05-07 10:52:31 +02003394 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003395 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003396 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003397 LY_CHECK_RET(parse_config(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003398 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003399 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003400 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 +02003401 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003402 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003403 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &cont->iffeatures, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003404 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003405 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003406 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 +02003407 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003408 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003409 LY_CHECK_RET(parse_status(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003410 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003411 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003412 LY_CHECK_RET(parse_when(ctx, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003413 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003414 case LY_STMT_PRESENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003415 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 +02003416 break;
3417
Radek Krejcid6b76452019-09-03 17:03:03 +02003418 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003419 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "container");
Radek Krejci0f969882020-08-21 16:56:47 +02003420 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003421 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003422 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003423 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003424 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003425 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003426 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003427 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003428 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003429 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003430 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003431 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003432 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003433 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003434 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003435 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003436 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003437 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003438 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003439 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003440 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003441 break;
3442
Radek Krejcid6b76452019-09-03 17:03:03 +02003443 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003444 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)cont, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003445 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003446 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003447 LY_CHECK_RET(parse_restrs(ctx, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003448 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003449 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003450 PARSER_CHECK_STMTVER2_RET(ctx, "action", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003451 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003452 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003453 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003454 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003455 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003456 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003457 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003458 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003459 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003460 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003461 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_CONTAINER, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003462 break;
3463 default:
David Sedlákb3077192019-06-19 10:55:37 +02003464 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003465 return LY_EVALID;
3466 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003467 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003468
Michal Vasko7fbc8162018-09-17 10:35:16 +02003469 return ret;
3470}
3471
Michal Vaskoea5abea2018-09-18 13:10:54 +02003472/**
3473 * @brief Parse the list statement.
3474 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003475 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003476 * @param[in,out] siblings Siblings to add to.
3477 *
3478 * @return LY_ERR values.
3479 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003480LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003481parse_list(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003482{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003483 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003484 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003485 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003486 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003487 struct lysp_node_list *list;
3488
David Sedlák60adc092019-08-06 15:57:02 +02003489 /* create new list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003490 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, list, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003491 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003492 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003493
Michal Vasko7fbc8162018-09-17 10:35:16 +02003494 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003495 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003496 INSERT_WORD_RET(ctx, buf, list->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003497
3498 /* parse substatements */
aPieceka24a2252021-05-07 10:52:31 +02003499 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003500 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003501 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003502 LY_CHECK_RET(parse_config(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003503 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003504 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003505 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 +02003506 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003507 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003508 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &list->iffeatures, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003509 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003510 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003511 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 +02003512 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003513 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003514 LY_CHECK_RET(parse_status(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003515 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003516 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003517 LY_CHECK_RET(parse_when(ctx, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003518 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003519 case LY_STMT_KEY:
Radek Krejcifc596f92021-02-26 22:40:26 +01003520 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 +02003521 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003522 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003523 LY_CHECK_RET(parse_maxelements(ctx, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003524 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003525 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003526 LY_CHECK_RET(parse_minelements(ctx, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003527 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003528 case LY_STMT_ORDERED_BY:
Radek Krejci33090f92020-12-17 20:12:46 +01003529 LY_CHECK_RET(parse_orderedby(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003530 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003531 case LY_STMT_UNIQUE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003532 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_UNIQUE, &list->uniques, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003533 break;
3534
Radek Krejcid6b76452019-09-03 17:03:03 +02003535 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003536 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "list");
Radek Krejci0f969882020-08-21 16:56:47 +02003537 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003538 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003539 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003540 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003541 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003542 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003543 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003544 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003545 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003546 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003547 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003548 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003549 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003550 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003551 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003552 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003553 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003554 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003555 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003556 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003557 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003558 break;
3559
Radek Krejcid6b76452019-09-03 17:03:03 +02003560 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003561 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)list, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003562 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003563 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003564 LY_CHECK_RET(parse_restrs(ctx, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003565 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003566 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003567 PARSER_CHECK_STMTVER2_RET(ctx, "action", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003568 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003569 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003570 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003571 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003572 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003573 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003574 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003575 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003576 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003577 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003578 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_LIST, 0, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003579 break;
3580 default:
David Sedlákb3077192019-06-19 10:55:37 +02003581 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003582 return LY_EVALID;
3583 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003584 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003585
Michal Vasko7fbc8162018-09-17 10:35:16 +02003586 return ret;
3587}
3588
Michal Vaskoea5abea2018-09-18 13:10:54 +02003589/**
3590 * @brief Parse the yin-element statement.
3591 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003592 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003593 * @param[in,out] flags Flags to write to.
3594 * @param[in,out] exts Extension instances to add to.
3595 *
3596 * @return LY_ERR values.
3597 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003598static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003599parse_yinelement(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003600{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003601 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003602 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003603 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003604 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003605
3606 if (*flags & LYS_YINELEM_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02003607 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003608 return LY_EVALID;
3609 }
3610
3611 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003612 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003613
Radek Krejcif13b87b2020-12-01 22:02:17 +01003614 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003615 *flags |= LYS_YINELEM_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003616 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003617 *flags |= LYS_YINELEM_FALSE;
3618 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003619 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003620 free(buf);
3621 return LY_EVALID;
3622 }
3623 free(buf);
3624
aPieceka24a2252021-05-07 10:52:31 +02003625 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003626 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003627 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003628 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_YIN_ELEMENT, 0, exts));
Michal Vaskod989ba02020-08-24 10:59:24 +02003629 LY_CHECK_RET(ret);
3630 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003631 default:
David Sedlákb3077192019-06-19 10:55:37 +02003632 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003633 return LY_EVALID;
3634 }
3635 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003636 return ret;
3637}
3638
Michal Vaskoea5abea2018-09-18 13:10:54 +02003639/**
David Sedlák2444f8f2019-07-09 11:02:47 +02003640 * @brief Parse the argument statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003641 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003642 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003643 * @param[in,out] argument Value to write to.
3644 * @param[in,out] flags Flags to write to.
3645 * @param[in,out] exts Extension instances to add to.
3646 *
3647 * @return LY_ERR values.
3648 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003649static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003650parse_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 +02003651{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003652 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003653 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003654 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003655 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003656
3657 if (*argument) {
David Sedlákb3077192019-06-19 10:55:37 +02003658 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003659 return LY_EVALID;
3660 }
3661
3662 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003663 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003664 INSERT_WORD_RET(ctx, buf, *argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003665
aPieceka24a2252021-05-07 10:52:31 +02003666 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003667 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003668 case LY_STMT_YIN_ELEMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003669 LY_CHECK_RET(parse_yinelement(ctx, flags, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003670 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003671 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003672 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_ARGUMENT, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003673 break;
3674 default:
David Sedlákb3077192019-06-19 10:55:37 +02003675 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003676 return LY_EVALID;
3677 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003678 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003679 return ret;
3680}
3681
Michal Vaskoea5abea2018-09-18 13:10:54 +02003682/**
3683 * @brief Parse the extension statement.
3684 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003685 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003686 * @param[in,out] extensions Extensions to add to.
3687 *
3688 * @return LY_ERR values.
3689 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003690static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003691parse_extension(struct lys_yang_parser_ctx *ctx, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003692{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003693 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003694 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003695 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003696 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003697 struct lysp_ext *ex;
3698
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003699 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003700
3701 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003702 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003703 INSERT_WORD_RET(ctx, buf, ex->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003704
aPieceka24a2252021-05-07 10:52:31 +02003705 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003706 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003707 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003708 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 +02003709 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003710 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003711 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 +02003712 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003713 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003714 LY_CHECK_RET(parse_status(ctx, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003715 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003716 case LY_STMT_ARGUMENT:
Radek Krejci9f87b0c2021-03-05 14:45:26 +01003717 LY_CHECK_RET(parse_argument(ctx, &ex->argname, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003718 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003719 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003720 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_EXTENSION, 0, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003721 break;
3722 default:
David Sedlákb3077192019-06-19 10:55:37 +02003723 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003724 return LY_EVALID;
3725 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003726 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003727 return ret;
3728}
3729
Michal Vaskoea5abea2018-09-18 13:10:54 +02003730/**
3731 * @brief Parse the deviate statement.
3732 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003733 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003734 * @param[in,out] deviates Deviates to add to.
3735 *
3736 * @return LY_ERR values.
3737 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003738LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003739parse_deviate(struct lys_yang_parser_ctx *ctx, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003740{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003741 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003742 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003743 size_t word_len, dev_mod;
Radek Krejcid6b76452019-09-03 17:03:03 +02003744 enum ly_stmt kw;
David Sedlák60adc092019-08-06 15:57:02 +02003745 struct lysp_deviate *d;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003746 struct lysp_deviate_add *d_add = NULL;
3747 struct lysp_deviate_rpl *d_rpl = NULL;
3748 struct lysp_deviate_del *d_del = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003749 const char **d_units = NULL;
3750 struct lysp_qname **d_uniques = NULL, **d_dflts = NULL;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02003751 struct lysp_restr **d_musts = NULL;
3752 uint16_t *d_flags = 0;
3753 uint32_t *d_min = 0, *d_max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003754
3755 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003756 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003757
Radek Krejcif13b87b2020-12-01 22:02:17 +01003758 if ((word_len == ly_strlen_const("not-supported")) && !strncmp(word, "not-supported", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003759 dev_mod = LYS_DEV_NOT_SUPPORTED;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003760 } else if ((word_len == ly_strlen_const("add")) && !strncmp(word, "add", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003761 dev_mod = LYS_DEV_ADD;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003762 } else if ((word_len == ly_strlen_const("replace")) && !strncmp(word, "replace", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003763 dev_mod = LYS_DEV_REPLACE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003764 } else if ((word_len == ly_strlen_const("delete")) && !strncmp(word, "delete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003765 dev_mod = LYS_DEV_DELETE;
3766 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003767 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003768 free(buf);
3769 return LY_EVALID;
3770 }
3771 free(buf);
3772
3773 /* create structure */
3774 switch (dev_mod) {
3775 case LYS_DEV_NOT_SUPPORTED:
3776 d = calloc(1, sizeof *d);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003777 LY_CHECK_ERR_RET(!d, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003778 break;
3779 case LYS_DEV_ADD:
3780 d_add = calloc(1, sizeof *d_add);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003781 LY_CHECK_ERR_RET(!d_add, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003782 d = (struct lysp_deviate *)d_add;
3783 d_units = &d_add->units;
3784 d_uniques = &d_add->uniques;
3785 d_dflts = &d_add->dflts;
3786 d_musts = &d_add->musts;
3787 d_flags = &d_add->flags;
3788 d_min = &d_add->min;
3789 d_max = &d_add->max;
3790 break;
3791 case LYS_DEV_REPLACE:
3792 d_rpl = calloc(1, sizeof *d_rpl);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003793 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003794 d = (struct lysp_deviate *)d_rpl;
3795 d_units = &d_rpl->units;
3796 d_flags = &d_rpl->flags;
3797 d_min = &d_rpl->min;
3798 d_max = &d_rpl->max;
3799 break;
3800 case LYS_DEV_DELETE:
3801 d_del = calloc(1, sizeof *d_del);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003802 LY_CHECK_ERR_RET(!d_del, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003803 d = (struct lysp_deviate *)d_del;
3804 d_units = &d_del->units;
3805 d_uniques = &d_del->uniques;
3806 d_dflts = &d_del->dflts;
3807 d_musts = &d_del->musts;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003808 break;
3809 default:
3810 assert(0);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003811 LOGINT_RET(PARSER_CTX(ctx));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003812 }
3813 d->mod = dev_mod;
3814
3815 /* insert into siblings */
David Sedlák60adc092019-08-06 15:57:02 +02003816 LY_LIST_INSERT(deviates, d, next);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003817
aPieceka24a2252021-05-07 10:52:31 +02003818 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003819 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003820 case LY_STMT_CONFIG:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003821 switch (dev_mod) {
3822 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003823 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003824 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003825 return LY_EVALID;
3826 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003827 LY_CHECK_RET(parse_config(ctx, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003828 break;
3829 }
3830 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003831 case LY_STMT_DEFAULT:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003832 switch (dev_mod) {
3833 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003834 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003835 return LY_EVALID;
3836 case LYS_DEV_REPLACE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003837 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 +02003838 d_rpl->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003839 break;
3840 default:
Radek Krejcifc596f92021-02-26 22:40:26 +01003841 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, d_dflts, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003842 break;
3843 }
3844 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003845 case LY_STMT_MANDATORY:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003846 switch (dev_mod) {
3847 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003848 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003849 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003850 return LY_EVALID;
3851 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003852 LY_CHECK_RET(parse_mandatory(ctx, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003853 break;
3854 }
3855 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003856 case LY_STMT_MAX_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003857 switch (dev_mod) {
3858 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003859 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003860 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003861 return LY_EVALID;
3862 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003863 LY_CHECK_RET(parse_maxelements(ctx, d_max, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003864 break;
3865 }
3866 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003867 case LY_STMT_MIN_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003868 switch (dev_mod) {
3869 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003870 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003871 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003872 return LY_EVALID;
3873 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003874 LY_CHECK_RET(parse_minelements(ctx, d_min, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003875 break;
3876 }
3877 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003878 case LY_STMT_MUST:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003879 switch (dev_mod) {
3880 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003881 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003882 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003883 return LY_EVALID;
3884 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003885 LY_CHECK_RET(parse_restrs(ctx, kw, d_musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003886 break;
3887 }
3888 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003889 case LY_STMT_TYPE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003890 switch (dev_mod) {
3891 case LYS_DEV_NOT_SUPPORTED:
3892 case LYS_DEV_ADD:
3893 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003894 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003895 return LY_EVALID;
3896 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003897 if (d_rpl->type) {
David Sedlákb3077192019-06-19 10:55:37 +02003898 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003899 return LY_EVALID;
3900 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003901 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003902 LY_CHECK_ERR_RET(!d_rpl->type, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01003903 LY_CHECK_RET(parse_type(ctx, d_rpl->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003904 break;
3905 }
3906 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003907 case LY_STMT_UNIQUE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003908 switch (dev_mod) {
3909 case LYS_DEV_NOT_SUPPORTED:
3910 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003911 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003912 return LY_EVALID;
3913 default:
Radek Krejcifc596f92021-02-26 22:40:26 +01003914 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_UNIQUE, d_uniques, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003915 break;
3916 }
3917 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003918 case LY_STMT_UNITS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003919 switch (dev_mod) {
3920 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003921 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003922 return LY_EVALID;
3923 default:
Radek Krejcifc596f92021-02-26 22:40:26 +01003924 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 +02003925 break;
3926 }
3927 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003928 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003929 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_DEVIATE, 0, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003930 break;
3931 default:
David Sedlákb3077192019-06-19 10:55:37 +02003932 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003933 return LY_EVALID;
3934 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003935 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003936 return ret;
3937}
3938
Michal Vaskoea5abea2018-09-18 13:10:54 +02003939/**
3940 * @brief Parse the deviation statement.
3941 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003942 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003943 * @param[in,out] deviations Deviations to add to.
3944 *
3945 * @return LY_ERR values.
3946 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003947LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003948parse_deviation(struct lys_yang_parser_ctx *ctx, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003949{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003950 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003951 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003952 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003953 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003954 struct lysp_deviation *dev;
3955
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003956 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003957
3958 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003959 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01003960 CHECK_NONEMPTY(ctx, word_len, "deviation");
Radek Krejci011e4aa2020-09-04 15:22:31 +02003961 INSERT_WORD_RET(ctx, buf, dev->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003962
aPieceka24a2252021-05-07 10:52:31 +02003963 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003964 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003965 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003966 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 +02003967 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003968 case LY_STMT_DEVIATE:
Radek Krejci33090f92020-12-17 20:12:46 +01003969 LY_CHECK_RET(parse_deviate(ctx, &dev->deviates));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003970 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003971 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003972 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 +02003973 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003974 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003975 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_DEVIATION, 0, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003976 break;
3977 default:
David Sedlákb3077192019-06-19 10:55:37 +02003978 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003979 return LY_EVALID;
3980 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003981 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003982 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01003983checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003984 /* mandatory substatements */
3985 if (!dev->deviates) {
David Sedlákb3077192019-06-19 10:55:37 +02003986 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003987 return LY_EVALID;
3988 }
3989
3990 return ret;
3991}
3992
Michal Vaskoea5abea2018-09-18 13:10:54 +02003993/**
3994 * @brief Parse the feature statement.
3995 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003996 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003997 * @param[in,out] features Features to add to.
3998 *
3999 * @return LY_ERR values.
4000 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004001LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004002parse_feature(struct lys_yang_parser_ctx *ctx, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004003{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004004 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004005 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004006 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004007 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004008 struct lysp_feature *feat;
4009
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004010 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004011
4012 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01004013 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004014 INSERT_WORD_RET(ctx, buf, feat->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004015
aPieceka24a2252021-05-07 10:52:31 +02004016 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004017 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004018 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004019 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 +02004020 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004021 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004022 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &feat->iffeatures, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004023 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004024 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004025 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 +02004026 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004027 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01004028 LY_CHECK_RET(parse_status(ctx, &feat->flags, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004029 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004030 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01004031 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_FEATURE, 0, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004032 break;
4033 default:
David Sedlákb3077192019-06-19 10:55:37 +02004034 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004035 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004036 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004037 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004038 return ret;
4039}
4040
Michal Vaskoea5abea2018-09-18 13:10:54 +02004041/**
4042 * @brief Parse the identity statement.
4043 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004044 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004045 * @param[in,out] identities Identities to add to.
4046 *
4047 * @return LY_ERR values.
4048 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004049LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004050parse_identity(struct lys_yang_parser_ctx *ctx, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004051{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004052 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004053 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004054 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004055 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004056 struct lysp_ident *ident;
4057
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004058 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004059
4060 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01004061 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004062 INSERT_WORD_RET(ctx, buf, ident->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004063
aPieceka24a2252021-05-07 10:52:31 +02004064 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004065 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004066 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004067 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 +02004068 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004069 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02004070 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "identity");
Radek Krejcifc596f92021-02-26 22:40:26 +01004071 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &ident->iffeatures, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004072 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004073 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004074 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 +02004075 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004076 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01004077 LY_CHECK_RET(parse_status(ctx, &ident->flags, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004078 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004079 case LY_STMT_BASE:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004080 if (ident->bases && (ctx->parsed_mod->version < LYS_VERSION_1_1)) {
David Sedlákb3077192019-06-19 10:55:37 +02004081 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 +01004082 return LY_EVALID;
4083 }
Radek Krejcifc596f92021-02-26 22:40:26 +01004084 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 +02004085 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004086 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01004087 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_IDENTITY, 0, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004088 break;
4089 default:
David Sedlákb3077192019-06-19 10:55:37 +02004090 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004091 return LY_EVALID;
4092 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004093 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004094 return ret;
4095}
4096
Michal Vaskoea5abea2018-09-18 13:10:54 +02004097/**
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004098 * @brief Parse module substatements.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004099 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004100 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004101 * @param[in,out] mod Module to write to.
4102 *
4103 * @return LY_ERR values.
4104 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004105LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004106parse_module(struct lys_yang_parser_ctx *ctx, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004107{
4108 LY_ERR ret = 0;
4109 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004110 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004111 enum ly_stmt kw, prev_kw = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004112 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Michal Vasko8dc31992021-02-22 10:30:47 +01004113 const struct lysp_submodule *dup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004114
Michal Vaskoc3781c32020-10-06 14:04:08 +02004115 mod->is_submod = 0;
4116
4117 /* module name */
Radek Krejci33090f92020-12-17 20:12:46 +01004118 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004119 INSERT_WORD_RET(ctx, buf, mod->mod->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004120
aPieceka24a2252021-05-07 10:52:31 +02004121 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004122
Radek Krejcie3846472018-10-15 15:24:51 +02004123#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004124 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 +02004125
Michal Vasko7fbc8162018-09-17 10:35:16 +02004126 switch (kw) {
4127 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004128 case LY_STMT_NAMESPACE:
4129 case LY_STMT_PREFIX:
Radek Krejcie3846472018-10-15 15:24:51 +02004130 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4131 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004132 case LY_STMT_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004133 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004134 break;
4135 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004136 case LY_STMT_INCLUDE:
4137 case LY_STMT_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004138 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004139 break;
4140 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004141 case LY_STMT_ORGANIZATION:
4142 case LY_STMT_CONTACT:
4143 case LY_STMT_DESCRIPTION:
4144 case LY_STMT_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004145 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004146 break;
4147
4148 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004149 case LY_STMT_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004150 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004151 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004152 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004153 case LY_STMT_ANYDATA:
4154 case LY_STMT_ANYXML:
4155 case LY_STMT_AUGMENT:
4156 case LY_STMT_CHOICE:
4157 case LY_STMT_CONTAINER:
4158 case LY_STMT_DEVIATION:
4159 case LY_STMT_EXTENSION:
4160 case LY_STMT_FEATURE:
4161 case LY_STMT_GROUPING:
4162 case LY_STMT_IDENTITY:
4163 case LY_STMT_LEAF:
4164 case LY_STMT_LEAF_LIST:
4165 case LY_STMT_LIST:
4166 case LY_STMT_NOTIFICATION:
4167 case LY_STMT_RPC:
4168 case LY_STMT_TYPEDEF:
4169 case LY_STMT_USES:
4170 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004171 mod_stmt = Y_MOD_BODY;
4172 break;
4173 default:
4174 /* error handled in the next switch */
4175 break;
4176 }
Radek Krejcie3846472018-10-15 15:24:51 +02004177#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004178
Radek Krejcie3846472018-10-15 15:24:51 +02004179 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004180 switch (kw) {
4181 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004182 case LY_STMT_YANG_VERSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004183 LY_CHECK_RET(parse_yangversion(ctx, &mod->version, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004184 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004185 case LY_STMT_NAMESPACE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004186 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 +02004187 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004188 case LY_STMT_PREFIX:
Radek Krejcifc596f92021-02-26 22:40:26 +01004189 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 +02004190 break;
4191
4192 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004193 case LY_STMT_INCLUDE:
Radek Krejci33090f92020-12-17 20:12:46 +01004194 LY_CHECK_RET(parse_include(ctx, mod->mod->name, &mod->includes));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004195 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004196 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004197 LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, &mod->imports));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004198 break;
4199
4200 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004201 case LY_STMT_ORGANIZATION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004202 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 +02004203 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004204 case LY_STMT_CONTACT:
Radek Krejcifc596f92021-02-26 22:40:26 +01004205 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 +02004206 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004207 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004208 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 +02004209 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004210 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004211 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 +02004212 break;
4213
4214 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004215 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004216 LY_CHECK_RET(parse_revision(ctx, &mod->revs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004217 break;
4218
4219 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004220 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004221 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "module");
Radek Krejci0f969882020-08-21 16:56:47 +02004222 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004223 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004224 LY_CHECK_RET(parse_any(ctx, kw, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004225 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004226 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004227 LY_CHECK_RET(parse_choice(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004228 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004229 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004230 LY_CHECK_RET(parse_container(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004231 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004232 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004233 LY_CHECK_RET(parse_leaf(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004234 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004235 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004236 LY_CHECK_RET(parse_leaflist(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004237 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004238 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004239 LY_CHECK_RET(parse_list(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004240 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004241 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004242 LY_CHECK_RET(parse_uses(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004243 break;
4244
Radek Krejcid6b76452019-09-03 17:03:03 +02004245 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004246 LY_CHECK_RET(parse_augment(ctx, NULL, &mod->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004247 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004248 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004249 LY_CHECK_RET(parse_deviation(ctx, &mod->deviations));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004250 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004251 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004252 LY_CHECK_RET(parse_extension(ctx, &mod->extensions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004253 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004254 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004255 LY_CHECK_RET(parse_feature(ctx, &mod->features));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004256 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004257 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004258 LY_CHECK_RET(parse_grouping(ctx, NULL, &mod->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004259 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004260 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004261 LY_CHECK_RET(parse_identity(ctx, &mod->identities));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004262 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004263 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004264 LY_CHECK_RET(parse_notif(ctx, NULL, &mod->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004265 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004266 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004267 LY_CHECK_RET(parse_action(ctx, NULL, &mod->rpcs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004268 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004269 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004270 LY_CHECK_RET(parse_typedef(ctx, NULL, &mod->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004271 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004272 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01004273 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MODULE, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004274 break;
4275
4276 default:
David Sedlákb3077192019-06-19 10:55:37 +02004277 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004278 return LY_EVALID;
4279 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004280 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004281 LY_CHECK_RET(ret);
Radek Krejcie86bf772018-12-14 11:39:53 +01004282
Radek Krejci6d6556c2018-11-08 09:37:45 +01004283checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004284 /* mandatory substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004285 if (!mod->mod->ns) {
David Sedlákb3077192019-06-19 10:55:37 +02004286 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004287 return LY_EVALID;
4288 } else if (!mod->mod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004289 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004290 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004291 }
4292
Radek Krejcie9e987e2018-10-31 12:50:27 +01004293 /* submodules share the namespace with the module names, so there must not be
4294 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01004295 dup = ly_ctx_get_submodule_latest(PARSER_CTX(ctx), mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004296 if (dup) {
Radek Krejci854e1552020-12-21 15:05:23 +01004297 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004298 return LY_EVALID;
4299 }
4300
4301 return ret;
4302}
4303
4304/**
4305 * @brief Parse submodule substatements.
4306 *
4307 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004308 * @param[out] submod Parsed submodule structure.
4309 *
4310 * @return LY_ERR values.
4311 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004312LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004313parse_submodule(struct lys_yang_parser_ctx *ctx, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004314{
4315 LY_ERR ret = 0;
4316 char *buf, *word;
4317 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004318 enum ly_stmt kw, prev_kw = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004319 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Michal Vasko8dc31992021-02-22 10:30:47 +01004320 const struct lysp_submodule *dup;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004321
Michal Vaskoc3781c32020-10-06 14:04:08 +02004322 submod->is_submod = 1;
4323
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004324 /* submodule name */
Radek Krejci33090f92020-12-17 20:12:46 +01004325 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004326 INSERT_WORD_RET(ctx, buf, submod->name, word, word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004327
aPieceka24a2252021-05-07 10:52:31 +02004328 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004329
4330#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004331 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 +01004332
4333 switch (kw) {
4334 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004335 case LY_STMT_BELONGS_TO:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004336 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4337 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004338 case LY_STMT_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004339 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4340 break;
4341 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004342 case LY_STMT_INCLUDE:
4343 case LY_STMT_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004344 CHECK_ORDER(Y_MOD_LINKAGE);
4345 break;
4346 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004347 case LY_STMT_ORGANIZATION:
4348 case LY_STMT_CONTACT:
4349 case LY_STMT_DESCRIPTION:
4350 case LY_STMT_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004351 CHECK_ORDER(Y_MOD_META);
4352 break;
4353
4354 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004355 case LY_STMT_REVISION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004356 CHECK_ORDER(Y_MOD_REVISION);
4357 break;
4358 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004359 case LY_STMT_ANYDATA:
4360 case LY_STMT_ANYXML:
4361 case LY_STMT_AUGMENT:
4362 case LY_STMT_CHOICE:
4363 case LY_STMT_CONTAINER:
4364 case LY_STMT_DEVIATION:
4365 case LY_STMT_EXTENSION:
4366 case LY_STMT_FEATURE:
4367 case LY_STMT_GROUPING:
4368 case LY_STMT_IDENTITY:
4369 case LY_STMT_LEAF:
4370 case LY_STMT_LEAF_LIST:
4371 case LY_STMT_LIST:
4372 case LY_STMT_NOTIFICATION:
4373 case LY_STMT_RPC:
4374 case LY_STMT_TYPEDEF:
4375 case LY_STMT_USES:
4376 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004377 mod_stmt = Y_MOD_BODY;
4378 break;
4379 default:
4380 /* error handled in the next switch */
4381 break;
4382 }
4383#undef CHECK_ORDER
4384
4385 prev_kw = kw;
4386 switch (kw) {
4387 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004388 case LY_STMT_YANG_VERSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004389 LY_CHECK_RET(parse_yangversion(ctx, &submod->version, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004390 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004391 case LY_STMT_BELONGS_TO:
Radek Krejci33090f92020-12-17 20:12:46 +01004392 LY_CHECK_RET(parse_belongsto(ctx, &submod->prefix, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004393 break;
4394
4395 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004396 case LY_STMT_INCLUDE:
Radek Krejcieeee95c2021-01-19 10:57:22 +01004397 if (submod->version == LYS_VERSION_1_1) {
4398 LOGWRN(PARSER_CTX(ctx), "YANG version 1.1 expects all includes in main module, includes in submodules (%s) are not necessary.",
4399 submod->name);
4400 }
Radek Krejci33090f92020-12-17 20:12:46 +01004401 LY_CHECK_RET(parse_include(ctx, submod->name, &submod->includes));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004402 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004403 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004404 LY_CHECK_RET(parse_import(ctx, submod->prefix, &submod->imports));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004405 break;
4406
4407 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004408 case LY_STMT_ORGANIZATION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004409 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 +01004410 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004411 case LY_STMT_CONTACT:
Radek Krejcifc596f92021-02-26 22:40:26 +01004412 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 +01004413 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004414 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004415 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 +01004416 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004417 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004418 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 +01004419 break;
4420
4421 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004422 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004423 LY_CHECK_RET(parse_revision(ctx, &submod->revs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004424 break;
4425
4426 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004427 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004428 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "submodule");
Radek Krejci0f969882020-08-21 16:56:47 +02004429 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004430 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004431 LY_CHECK_RET(parse_any(ctx, kw, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004432 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004433 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004434 LY_CHECK_RET(parse_choice(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004435 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004436 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004437 LY_CHECK_RET(parse_container(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004438 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004439 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004440 LY_CHECK_RET(parse_leaf(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004441 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004442 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004443 LY_CHECK_RET(parse_leaflist(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004444 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004445 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004446 LY_CHECK_RET(parse_list(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004447 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004448 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004449 LY_CHECK_RET(parse_uses(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004450 break;
4451
Radek Krejcid6b76452019-09-03 17:03:03 +02004452 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004453 LY_CHECK_RET(parse_augment(ctx, NULL, &submod->augments));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004454 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004455 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004456 LY_CHECK_RET(parse_deviation(ctx, &submod->deviations));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004457 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004458 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004459 LY_CHECK_RET(parse_extension(ctx, &submod->extensions));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004460 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004461 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004462 LY_CHECK_RET(parse_feature(ctx, &submod->features));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004463 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004464 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004465 LY_CHECK_RET(parse_grouping(ctx, NULL, &submod->groupings));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004466 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004467 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004468 LY_CHECK_RET(parse_identity(ctx, &submod->identities));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004469 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004470 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004471 LY_CHECK_RET(parse_notif(ctx, NULL, &submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004472 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004473 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004474 LY_CHECK_RET(parse_action(ctx, NULL, &submod->rpcs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004475 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004476 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004477 LY_CHECK_RET(parse_typedef(ctx, NULL, &submod->typedefs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004478 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004479 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01004480 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_SUBMODULE, 0, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004481 break;
4482
4483 default:
David Sedlákb3077192019-06-19 10:55:37 +02004484 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004485 return LY_EVALID;
4486 }
4487 }
4488 LY_CHECK_RET(ret);
4489
4490checks:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004491 /* mandatory substatements */
Michal Vaskoc3781c32020-10-06 14:04:08 +02004492 if (!submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004493 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004494 return LY_EVALID;
4495 }
4496
4497 /* submodules share the namespace with the module names, so there must not be
4498 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01004499 dup = ly_ctx_get_submodule_latest(PARSER_CTX(ctx), submod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02004500 /* main modules may have different revisions */
4501 if (dup && strcmp(dup->mod->name, submod->mod->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01004502 LOGVAL_PARSER(ctx, LY_VCODE_NAME_COL, "submodules", dup->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +01004503 return LY_EVALID;
4504 }
4505
Michal Vasko7fbc8162018-09-17 10:35:16 +02004506 return ret;
4507}
4508
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004509/**
4510 * @brief Skip any redundant characters, namely whitespaces and comments.
4511 *
4512 * @param[in] ctx Yang parser context.
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004513 * @return LY_SUCCESS on success.
4514 * @return LY_EVALID on invalid comment.
4515 */
4516static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004517skip_redundant_chars(struct lys_yang_parser_ctx *ctx)
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004518{
4519 /* read some trailing spaces, new lines, or comments */
Radek Krejci33090f92020-12-17 20:12:46 +01004520 while (ctx->in->current[0]) {
4521 if (!strncmp(ctx->in->current, "//", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004522 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004523 ly_in_skip(ctx->in, 2);
4524 LY_CHECK_RET(skip_comment(ctx, 1));
4525 } else if (!strncmp(ctx->in->current, "/*", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004526 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004527 ly_in_skip(ctx->in, 2);
4528 LY_CHECK_RET(skip_comment(ctx, 2));
4529 } else if (isspace(ctx->in->current[0])) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004530 /* whitespace */
Radek Krejcidd713ce2021-01-04 23:12:12 +01004531 if (ctx->in->current[0] == '\n') {
4532 LY_IN_NEW_LINE(ctx->in);
4533 }
Radek Krejci33090f92020-12-17 20:12:46 +01004534 ly_in_skip(ctx->in, 1);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004535 } else {
4536 break;
4537 }
4538 }
4539
4540 return LY_SUCCESS;
4541}
4542
Radek Krejcid4557c62018-09-17 11:42:09 +02004543LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01004544yang_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 +02004545 struct ly_in *in, struct lysp_submodule **submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004546{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004547 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004548 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004549 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004550 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004551 struct lysp_submodule *mod_p = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004552
David Sedlák1b623122019-08-05 15:27:49 +02004553 /* create context */
4554 *context = calloc(1, sizeof **context);
4555 LY_CHECK_ERR_RET(!(*context), LOGMEM(ly_ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004556 (*context)->format = LYS_IN_YANG;
Michal Vasko405cc9e2020-12-01 12:01:27 +01004557 (*context)->unres = main_ctx->unres;
Radek Krejci33090f92020-12-17 20:12:46 +01004558 (*context)->in = in;
David Sedlák1b623122019-08-05 15:27:49 +02004559
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004560 mod_p = calloc(1, sizeof *mod_p);
4561 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ly_ctx); ret = LY_EMEM, cleanup);
4562 mod_p->mod = main_ctx->parsed_mod->mod;
4563 mod_p->parsing = 1;
4564 (*context)->parsed_mod = (struct lysp_module *)mod_p;
4565
Radek Krejciddace2c2021-01-08 11:30:56 +01004566 LOG_LOCINIT(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004567
David Sedlák1b623122019-08-05 15:27:49 +02004568 /* map the typedefs and groupings list from main context to the submodule's context */
4569 memcpy(&(*context)->tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
4570 memcpy(&(*context)->grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes);
4571
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004572 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004573 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004574 LY_CHECK_GOTO(ret, cleanup);
4575
Michal Vasko7fbc8162018-09-17 10:35:16 +02004576 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004577 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004578 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004579
Radek Krejcid6b76452019-09-03 17:03:03 +02004580 if (kw == LY_STMT_MODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004581 LOGERR(ly_ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004582 ret = LY_EINVAL;
4583 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004584 } else if (kw != LY_STMT_SUBMODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004585 LOGVAL_PARSER(*context, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004586 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004587 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004588 }
4589
Michal Vasko7fbc8162018-09-17 10:35:16 +02004590 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004591 ret = parse_submodule(*context, mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004592 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004593
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004594 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004595 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004596 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004597 if (in->current[0]) {
4598 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_SUBMOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004599 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004600 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004601 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004602
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004603 mod_p->parsing = 0;
4604 *submod = mod_p;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004605
Radek Krejcibbe09a92018-11-08 09:36:54 +01004606cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01004607 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004608 if (ret) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004609 lysp_module_free((struct lysp_module *)mod_p);
Michal Vaskob36053d2020-03-26 15:49:30 +01004610 yang_parser_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004611 *context = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004612 }
4613
4614 return ret;
4615}
4616
4617LY_ERR
Michal Vasko405cc9e2020-12-01 12:01:27 +01004618yang_parse_module(struct lys_yang_parser_ctx **context, struct ly_in *in, struct lys_module *mod, struct lys_glob_unres *unres)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004619{
4620 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004621 char *word;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004622 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004623 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004624 struct lysp_module *mod_p = NULL;
4625
David Sedlák1b623122019-08-05 15:27:49 +02004626 /* create context */
4627 *context = calloc(1, sizeof **context);
4628 LY_CHECK_ERR_RET(!(*context), LOGMEM(mod->ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004629 (*context)->format = LYS_IN_YANG;
Michal Vasko405cc9e2020-12-01 12:01:27 +01004630 (*context)->unres = unres;
Radek Krejci33090f92020-12-17 20:12:46 +01004631 (*context)->in = in;
David Sedlák1b623122019-08-05 15:27:49 +02004632
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004633 mod_p = calloc(1, sizeof *mod_p);
4634 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(mod->ctx), cleanup);
4635 mod_p->mod = mod;
4636 mod_p->parsing = 1;
4637 (*context)->parsed_mod = mod_p;
4638
Radek Krejciddace2c2021-01-08 11:30:56 +01004639 LOG_LOCINIT(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004640
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004641 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004642 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004643 LY_CHECK_GOTO(ret, cleanup);
4644
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004645 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004646 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004647 LY_CHECK_GOTO(ret, cleanup);
4648
Radek Krejcid6b76452019-09-03 17:03:03 +02004649 if (kw == LY_STMT_SUBMODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004650 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 +01004651 ret = LY_EINVAL;
4652 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004653 } else if (kw != LY_STMT_MODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004654 LOGVAL_PARSER((*context), LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004655 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004656 goto cleanup;
4657 }
4658
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004659 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004660 ret = parse_module(*context, mod_p);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004661 LY_CHECK_GOTO(ret, cleanup);
4662
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004663 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004664 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004665 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004666 if (in->current[0]) {
4667 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_MOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004668 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004669 goto cleanup;
4670 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004671
4672 mod_p->parsing = 0;
4673 mod->parsed = mod_p;
4674
4675cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01004676 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004677 if (ret) {
4678 lysp_module_free(mod_p);
Michal Vaskob36053d2020-03-26 15:49:30 +01004679 yang_parser_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004680 *context = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004681 }
4682
Michal Vasko7fbc8162018-09-17 10:35:16 +02004683 return ret;
4684}