blob: cb0ff61d95629148ae75171ee7bc1ffcd90a2740 [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 */
Michal Vasko12ef5362022-09-16 15:13:58 +020047#define INSERT_WORD_GOTO(CTX, BUF, TARGET, WORD, LEN, RET, LABEL) \
48 if (BUF) {LY_CHECK_GOTO(RET = lydict_insert_zc(PARSER_CTX(CTX), WORD, &(TARGET)), LABEL);}\
49 else {LY_CHECK_GOTO(RET = lydict_insert(PARSER_CTX(CTX), LEN ? WORD : "", LEN, &(TARGET)), LABEL);}
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.
Michal Vasko12ef5362022-09-16 15:13:58 +020065 * @param[out] RET 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 */
Michal Vasko12ef5362022-09-16 15:13:58 +020073#define YANG_READ_SUBSTMT_FOR(CTX, KW, WORD, WORD_LEN, RET, SUC_CMD, ERR_CMD) \
74 if ((RET = get_keyword(CTX, &KW, &WORD, &WORD_LEN))) { \
aPieceka24a2252021-05-07 10:52:31 +020075 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)); \
Michal Vasko12ef5362022-09-16 15:13:58 +020082 RET = LY_EVALID; \
aPieceka24a2252021-05-07 10:52:31 +020083 ERR_CMD; \
Michal Vasko7fbc8162018-09-17 10:35:16 +020084 } \
Michal Vasko12ef5362022-09-16 15:13:58 +020085 for (RET = get_keyword(CTX, &KW, &WORD, &WORD_LEN); \
86 !RET && (KW != LY_STMT_SYNTAX_RIGHT_BRACE); \
87 RET = get_keyword(CTX, &KW, &WORD, &WORD_LEN))
Michal Vasko7fbc8162018-09-17 10:35:16 +020088
Radek Krejci33090f92020-12-17 20:12:46 +010089LY_ERR parse_container(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent,
Radek Krejci0f969882020-08-21 16:56:47 +020090 struct lysp_node **siblings);
Radek Krejci33090f92020-12-17 20:12:46 +010091LY_ERR parse_uses(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
92LY_ERR parse_choice(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
93LY_ERR parse_case(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
94LY_ERR parse_list(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
Radek Krejci2a9fc652021-01-22 17:44:34 +010095LY_ERR parse_grouping(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_grp **groupings);
Michal Vasko7fbc8162018-09-17 10:35:16 +020096
Michal Vaskoea5abea2018-09-18 13:10:54 +020097/**
98 * @brief Add another character to dynamic buffer, a low-level function.
99 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200100 * Enlarge if needed. Updates \p input as well as \p buf_used.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200101 *
Radek Krejci404251e2018-10-09 12:06:44 +0200102 * @param[in] ctx libyang context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200103 * @param[in,out] in Input structure.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200104 * @param[in] len Number of bytes to get from the input string and copy into the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200105 * @param[in,out] buf Buffer to use, can be moved by realloc().
106 * @param[in,out] buf_len Current size of the buffer.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200107 * @param[in,out] buf_used Currently used characters of the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200108 *
109 * @return LY_ERR values.
110 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200111LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200112buf_add_char(struct ly_ctx *ctx, struct ly_in *in, size_t len, char **buf, size_t *buf_len, size_t *buf_used)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200113{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100114#define BUF_STEP 16;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200115 if (*buf_len <= (*buf_used) + len) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100116 *buf_len += BUF_STEP;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200117 *buf = ly_realloc(*buf, *buf_len);
118 LY_CHECK_ERR_RET(!*buf, LOGMEM(ctx), LY_EMEM);
119 }
Radek Krejcic0917392019-04-10 13:04:04 +0200120 if (*buf_used) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200121 ly_in_read(in, &(*buf)[*buf_used], len);
Radek Krejcic0917392019-04-10 13:04:04 +0200122 } else {
Michal Vasko63f3d842020-07-08 10:10:14 +0200123 ly_in_read(in, *buf, len);
Radek Krejcic0917392019-04-10 13:04:04 +0200124 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200125
Radek Krejci44ceedc2018-10-02 15:54:31 +0200126 (*buf_used) += len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200127 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100128#undef BUF_STEP
Michal Vasko7fbc8162018-09-17 10:35:16 +0200129}
130
Michal Vaskoea5abea2018-09-18 13:10:54 +0200131/**
Radek Krejci44ceedc2018-10-02 15:54:31 +0200132 * @brief Store a single UTF8 character. It depends whether in a dynamically-allocated buffer or just as a pointer to the data.
133 *
134 * @param[in] ctx yang parser context for logging.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200135 * @param[in] arg Type of the input string to select method of checking character validity.
136 * @param[in,out] word_p Word pointer. If buffer (\p word_b) was not yet needed, it is just a pointer to the first
Michal Vaskoea5abea2018-09-18 13:10:54 +0200137 * stored character. If buffer was needed (\p word_b is non-NULL or \p need_buf is set), it is pointing to the buffer.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200138 * @param[in,out] word_len Current length of the word pointed to by \p word_p.
139 * @param[in,out] word_b Word buffer. Is kept NULL as long as it is not requested (word is a substring of the data).
Michal Vaskoea5abea2018-09-18 13:10:54 +0200140 * @param[in,out] buf_len Current length of \p word_b.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200141 * @param[in] need_buf Flag if the dynamically allocated buffer is required.
David Sedlák40bb13b2019-07-10 14:34:18 +0200142 * @param[in,out] prefix Storage for internally used flag in case of possible prefixed identifiers:
143 * 0 - colon not yet found (no prefix)
144 * 1 - \p c is the colon character
145 * 2 - prefix already processed, now processing the identifier
Michal Vaskoea5abea2018-09-18 13:10:54 +0200146 *
147 * @return LY_ERR values.
148 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200149LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100150buf_store_char(struct lys_yang_parser_ctx *ctx, enum yang_arg arg, char **word_p, size_t *word_len,
Radek Krejci857189e2020-09-01 13:26:36 +0200151 char **word_b, size_t *buf_len, ly_bool need_buf, uint8_t *prefix)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200152{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200153 uint32_t c;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200154 size_t len;
155
Radek Krejcif29b7c32019-04-09 16:17:49 +0200156 /* check valid combination of input paremeters - if need_buf specified, word_b must be provided */
157 assert(!need_buf || (need_buf && word_b));
158
Radek Krejci44ceedc2018-10-02 15:54:31 +0200159 /* get UTF8 code point (and number of bytes coding the character) */
Radek Krejci33090f92020-12-17 20:12:46 +0100160 LY_CHECK_ERR_RET(ly_getutf8(&ctx->in->current, &c, &len),
161 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[-len]), LY_EVALID);
162 ctx->in->current -= len;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200163 if (c == '\n') {
164 ctx->indent = 0;
Radek Krejcidd713ce2021-01-04 23:12:12 +0100165 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200166 } else {
167 /* note - even the multibyte character is count as 1 */
168 ++ctx->indent;
169 }
170
Radek Krejci44ceedc2018-10-02 15:54:31 +0200171 /* check character validity */
172 switch (arg) {
173 case Y_IDENTIF_ARG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100174 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !(*word_len), NULL));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200175 break;
176 case Y_PREF_IDENTIF_ARG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100177 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !(*word_len), prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200178 break;
179 case Y_STR_ARG:
180 case Y_MAYBE_STR_ARG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100181 LY_CHECK_RET(lysp_check_stringchar((struct lys_parser_ctx *)ctx, c));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200182 break;
183 }
184
Michal Vasko7fbc8162018-09-17 10:35:16 +0200185 if (word_b && *word_b) {
186 /* add another character into buffer */
Radek Krejci33090f92020-12-17 20:12:46 +0100187 if (buf_add_char(PARSER_CTX(ctx), ctx->in, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200188 return LY_EMEM;
189 }
190
191 /* in case of realloc */
192 *word_p = *word_b;
Radek Krejcif29b7c32019-04-09 16:17:49 +0200193 } else if (word_b && need_buf) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200194 /* first time we need a buffer, copy everything read up to now */
195 if (*word_len) {
196 *word_b = malloc(*word_len);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200197 LY_CHECK_ERR_RET(!*word_b, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200198 *buf_len = *word_len;
199 memcpy(*word_b, *word_p, *word_len);
200 }
201
202 /* add this new character into buffer */
Radek Krejci33090f92020-12-17 20:12:46 +0100203 if (buf_add_char(PARSER_CTX(ctx), ctx->in, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200204 return LY_EMEM;
205 }
206
207 /* in case of realloc */
208 *word_p = *word_b;
209 } else {
210 /* just remember the first character pointer */
211 if (!*word_p) {
Radek Krejci33090f92020-12-17 20:12:46 +0100212 *word_p = (char *)ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200213 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200214 /* ... and update the word's length */
215 (*word_len) += len;
Radek Krejci33090f92020-12-17 20:12:46 +0100216 ly_in_skip(ctx->in, len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200217 }
218
219 return LY_SUCCESS;
220}
221
Michal Vaskoea5abea2018-09-18 13:10:54 +0200222/**
223 * @brief Skip YANG comment in data.
224 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200225 * @param[in] ctx yang parser context for logging.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200226 * @param[in] comment Type of the comment to process:
227 * 1 for a one-line comment,
228 * 2 for a block comment.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200229 * @return LY_ERR values.
230 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200231LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100232skip_comment(struct lys_yang_parser_ctx *ctx, uint8_t comment)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200233{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100234 /* internal statuses: */
235#define COMMENT_NO 0 /* comment ended */
236#define COMMENT_LINE 1 /* in line comment */
237#define COMMENT_BLOCK 2 /* in block comment */
238#define COMMENT_BLOCK_END 3 /* in block comment with last read character '*' */
239
Radek Krejci33090f92020-12-17 20:12:46 +0100240 while (ctx->in->current[0] && comment) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200241 switch (comment) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100242 case COMMENT_LINE:
Radek Krejci33090f92020-12-17 20:12:46 +0100243 if (ctx->in->current[0] == '\n') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100244 comment = COMMENT_NO;
Radek Krejcid54412f2020-12-17 20:25:35 +0100245 LY_IN_NEW_LINE(ctx->in);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200246 }
247 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100248 case COMMENT_BLOCK:
Radek Krejci33090f92020-12-17 20:12:46 +0100249 if (ctx->in->current[0] == '*') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100250 comment = COMMENT_BLOCK_END;
Radek Krejci33090f92020-12-17 20:12:46 +0100251 } else if (ctx->in->current[0] == '\n') {
Radek Krejcid54412f2020-12-17 20:25:35 +0100252 LY_IN_NEW_LINE(ctx->in);
Radek Krejci15c80ca2018-10-09 11:01:31 +0200253 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200254 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100255 case COMMENT_BLOCK_END:
Radek Krejci33090f92020-12-17 20:12:46 +0100256 if (ctx->in->current[0] == '/') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100257 comment = COMMENT_NO;
Radek Krejci33090f92020-12-17 20:12:46 +0100258 } else if (ctx->in->current[0] != '*') {
259 if (ctx->in->current[0] == '\n') {
Radek Krejcid54412f2020-12-17 20:25:35 +0100260 LY_IN_NEW_LINE(ctx->in);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200261 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100262 comment = COMMENT_BLOCK;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200263 }
264 break;
265 default:
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200266 LOGINT_RET(PARSER_CTX(ctx));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200267 }
268
Radek Krejci33090f92020-12-17 20:12:46 +0100269 if (ctx->in->current[0] == '\n') {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200270 ctx->indent = 0;
271 } else {
272 ++ctx->indent;
273 }
Radek Krejci33090f92020-12-17 20:12:46 +0100274 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200275 }
276
Radek Krejci33090f92020-12-17 20:12:46 +0100277 if (!ctx->in->current[0] && (comment >= COMMENT_BLOCK)) {
David Sedlákb3077192019-06-19 10:55:37 +0200278 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Unexpected end-of-input, non-terminated comment.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200279 return LY_EVALID;
280 }
281
282 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100283
284#undef COMMENT_NO
285#undef COMMENT_LINE
286#undef COMMENT_BLOCK
287#undef COMMENT_BLOCK_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200288}
289
Michal Vaskoea5abea2018-09-18 13:10:54 +0200290/**
291 * @brief Read a quoted string from data.
292 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200293 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200294 * @param[in] arg Type of YANG keyword argument expected.
295 * @param[out] word_p Pointer to the read quoted string.
296 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read quoted string. If not needed,
297 * set to NULL. Otherwise equal to \p word_p.
298 * @param[out] word_len Length of the read quoted string.
299 * @param[out] buf_len Length of the dynamically-allocated buffer \p word_b.
300 * @param[in] indent Current indent (number of YANG spaces). Needed for correct multi-line string
301 * indenation in the final quoted string.
302 *
303 * @return LY_ERR values.
304 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200305static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100306read_qstring(struct lys_yang_parser_ctx *ctx, enum yang_arg arg, char **word_p, char **word_b,
Radek Krejci0f969882020-08-21 16:56:47 +0200307 size_t *word_len, size_t *buf_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200308{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100309 /* string parsing status: */
310#define STRING_ENDED 0 /* string ended */
311#define STRING_SINGLE_QUOTED 1 /* string with ' */
312#define STRING_DOUBLE_QUOTED 2 /* string with " */
313#define STRING_DOUBLE_QUOTED_ESCAPED 3 /* string with " with last character \ */
314#define STRING_PAUSED_NEXTSTRING 4 /* string finished, now skipping whitespaces looking for + */
315#define STRING_PAUSED_CONTINUE 5 /* string continues after +, skipping whitespaces */
316
Radek Krejci1deb5be2020-08-26 16:43:36 +0200317 uint8_t string;
318 uint64_t block_indent = 0, current_indent = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200319 ly_bool need_buf = 0;
320 uint8_t prefix = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200321 const char *c;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200322 uint64_t trailing_ws = 0; /* current number of stored trailing whitespace characters */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200323
Radek Krejci33090f92020-12-17 20:12:46 +0100324 if (ctx->in->current[0] == '\"') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100325 string = STRING_DOUBLE_QUOTED;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200326 current_indent = block_indent = ctx->indent + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200327 } else {
Radek Krejci33090f92020-12-17 20:12:46 +0100328 assert(ctx->in->current[0] == '\'');
Radek Krejcif13b87b2020-12-01 22:02:17 +0100329 string = STRING_SINGLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200330 }
Radek Krejci33090f92020-12-17 20:12:46 +0100331 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200332
Radek Krejci33090f92020-12-17 20:12:46 +0100333 while (ctx->in->current[0] && string) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200334 switch (string) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100335 case STRING_SINGLE_QUOTED:
Radek Krejci33090f92020-12-17 20:12:46 +0100336 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200337 case '\'':
338 /* string may be finished, but check for + */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100339 string = STRING_PAUSED_NEXTSTRING;
Radek Krejci33090f92020-12-17 20:12:46 +0100340 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200341 break;
342 default:
343 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100344 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200345 break;
346 }
347 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100348 case STRING_DOUBLE_QUOTED:
Radek Krejci33090f92020-12-17 20:12:46 +0100349 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200350 case '\"':
351 /* string may be finished, but check for + */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100352 string = STRING_PAUSED_NEXTSTRING;
Radek Krejci33090f92020-12-17 20:12:46 +0100353 MOVE_INPUT(ctx, 1);
Radek Krejciff13cd12019-10-25 15:34:24 +0200354 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200355 break;
356 case '\\':
357 /* special character following */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100358 string = STRING_DOUBLE_QUOTED_ESCAPED;
Radek Krejciff13cd12019-10-25 15:34:24 +0200359
360 /* the backslash sequence is substituted, so we will need a buffer to store the result */
361 need_buf = 1;
362
363 /* move forward to the escaped character */
Radek Krejci33090f92020-12-17 20:12:46 +0100364 ++ctx->in->current;
Radek Krejciff13cd12019-10-25 15:34:24 +0200365
366 /* note that the trailing whitespaces are supposed to be trimmed before substitution of
367 * backslash-escaped characters (RFC 7950, 6.1.3), so we have to zero the trailing whitespaces counter */
368 trailing_ws = 0;
369
370 /* since the backslash-escaped character is handled as first non-whitespace character, stop eating indentation */
371 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200372 break;
373 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200374 if (current_indent < block_indent) {
375 ++current_indent;
Radek Krejci33090f92020-12-17 20:12:46 +0100376 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200377 } else {
Michal Vasko90edde42019-11-25 15:25:07 +0100378 /* check and store whitespace character */
Radek Krejci33090f92020-12-17 20:12:46 +0100379 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko90edde42019-11-25 15:25:07 +0100380 trailing_ws++;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200381 }
382 break;
383 case '\t':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200384 if (current_indent < block_indent) {
385 assert(need_buf);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100386 current_indent += Y_TAB_SPACES;
387 ctx->indent += Y_TAB_SPACES;
Michal Vaskod989ba02020-08-24 10:59:24 +0200388 for ( ; current_indent > block_indent; --current_indent, --ctx->indent) {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200389 /* store leftover spaces from the tab */
Radek Krejci33090f92020-12-17 20:12:46 +0100390 c = ctx->in->current;
391 ctx->in->current = " ";
392 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
393 ctx->in->current = c;
Michal Vasko90edde42019-11-25 15:25:07 +0100394 trailing_ws++;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200395 }
Radek Krejci33090f92020-12-17 20:12:46 +0100396 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200397 } else {
Michal Vasko90edde42019-11-25 15:25:07 +0100398 /* check and store whitespace character */
Radek Krejci33090f92020-12-17 20:12:46 +0100399 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko90edde42019-11-25 15:25:07 +0100400 trailing_ws++;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200401 /* additional characters for indentation - only 1 was count in buf_store_char */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100402 ctx->indent += Y_TAB_SPACES - 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200403 }
404 break;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200405 case '\r':
406 if (ctx->in->current[1] != '\n') {
407 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
408 return LY_EVALID;
409 }
410 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200411 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200412 if (block_indent) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200413 /* we will be removing the indents so we need our own buffer */
414 need_buf = 1;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200415
416 /* remove trailing tabs and spaces */
Radek Krejciff13cd12019-10-25 15:34:24 +0200417 (*word_len) = *word_len - trailing_ws;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200418
Radek Krejciff13cd12019-10-25 15:34:24 +0200419 /* restart indentation */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200420 current_indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200421 }
422
423 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100424 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200425
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200426 /* reset context indentation counter for possible string after this one */
427 ctx->indent = 0;
Radek Krejciff13cd12019-10-25 15:34:24 +0200428 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200429 break;
430 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200431 /* first non-whitespace character, stop eating indentation */
432 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200433
434 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100435 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Radek Krejciff13cd12019-10-25 15:34:24 +0200436 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200437 break;
438 }
439 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100440 case STRING_DOUBLE_QUOTED_ESCAPED:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200441 /* string encoded characters */
Radek Krejci33090f92020-12-17 20:12:46 +0100442 c = ctx->in->current;
443 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200444 case 'n':
Radek Krejci33090f92020-12-17 20:12:46 +0100445 ctx->in->current = "\n";
Radek Krejcidd713ce2021-01-04 23:12:12 +0100446 /* fix false newline count in buf_store_char() */
447 ctx->in->line--;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200448 break;
449 case 't':
Radek Krejci33090f92020-12-17 20:12:46 +0100450 ctx->in->current = "\t";
Michal Vasko7fbc8162018-09-17 10:35:16 +0200451 break;
452 case '\"':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200453 case '\\':
Michal Vasko63f3d842020-07-08 10:10:14 +0200454 /* ok as is */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200455 break;
456 default:
Michal Vasko63f3d842020-07-08 10:10:14 +0200457 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Double-quoted string unknown special character '\\%c'.",
Radek Krejci33090f92020-12-17 20:12:46 +0100458 ctx->in->current[0]);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200459 return LY_EVALID;
460 }
461
462 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100463 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200464
Radek Krejcif13b87b2020-12-01 22:02:17 +0100465 string = STRING_DOUBLE_QUOTED;
Radek Krejci33090f92020-12-17 20:12:46 +0100466 ctx->in->current = c + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200467 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100468 case STRING_PAUSED_NEXTSTRING:
Radek Krejci33090f92020-12-17 20:12:46 +0100469 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200470 case '+':
471 /* string continues */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100472 string = STRING_PAUSED_CONTINUE;
Radek Krejciefd22f62018-09-27 11:47:58 +0200473 need_buf = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200474 break;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200475 case '\r':
476 if (ctx->in->current[1] != '\n') {
477 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
478 return LY_EVALID;
479 }
480 MOVE_INPUT(ctx, 1);
481 /* fallthrough */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200482 case '\n':
Radek Krejcidd713ce2021-01-04 23:12:12 +0100483 LY_IN_NEW_LINE(ctx->in);
Radek Krejcicb3e6472021-01-06 08:19:01 +0100484 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200485 case ' ':
486 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200487 /* just skip */
488 break;
489 default:
490 /* string is finished */
491 goto string_end;
492 }
Radek Krejci33090f92020-12-17 20:12:46 +0100493 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200494 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100495 case STRING_PAUSED_CONTINUE:
Radek Krejci33090f92020-12-17 20:12:46 +0100496 switch (ctx->in->current[0]) {
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200497 case '\r':
498 if (ctx->in->current[1] != '\n') {
499 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
500 return LY_EVALID;
501 }
502 MOVE_INPUT(ctx, 1);
503 /* fallthrough */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200504 case '\n':
Radek Krejcidd713ce2021-01-04 23:12:12 +0100505 LY_IN_NEW_LINE(ctx->in);
Radek Krejcicb3e6472021-01-06 08:19:01 +0100506 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200507 case ' ':
508 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200509 /* skip */
510 break;
511 case '\'':
Radek Krejcif13b87b2020-12-01 22:02:17 +0100512 string = STRING_SINGLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200513 break;
514 case '\"':
Radek Krejcif13b87b2020-12-01 22:02:17 +0100515 string = STRING_DOUBLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200516 break;
517 default:
518 /* it must be quoted again */
David Sedlákb3077192019-06-19 10:55:37 +0200519 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Both string parts divided by '+' must be quoted.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200520 return LY_EVALID;
521 }
Radek Krejci33090f92020-12-17 20:12:46 +0100522 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200523 break;
524 default:
525 return LY_EINT;
526 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200527 }
528
529string_end:
Michal Vasko69730152020-10-09 16:30:07 +0200530 if ((arg <= Y_PREF_IDENTIF_ARG) && !(*word_len)) {
Radek Krejci4e199f52019-05-28 09:09:28 +0200531 /* empty identifier */
David Sedlákb3077192019-06-19 10:55:37 +0200532 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Statement argument is required.");
Radek Krejci4e199f52019-05-28 09:09:28 +0200533 return LY_EVALID;
534 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200535 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100536
537#undef STRING_ENDED
538#undef STRING_SINGLE_QUOTED
539#undef STRING_DOUBLE_QUOTED
540#undef STRING_DOUBLE_QUOTED_ESCAPED
541#undef STRING_PAUSED_NEXTSTRING
542#undef STRING_PAUSED_CONTINUE
Michal Vasko7fbc8162018-09-17 10:35:16 +0200543}
544
Michal Vaskoea5abea2018-09-18 13:10:54 +0200545/**
546 * @brief Get another YANG string from the raw data.
547 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200548 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200549 * @param[in] arg Type of YANG keyword argument expected.
Michal Vaskob68ea142021-04-26 13:46:00 +0200550 * @param[out] flags optional output argument to get flag of the argument's quoting (LYS_*QUOTED - see
551 * [schema node flags](@ref snodeflags))
Michal Vasko2ca70f52018-09-27 11:04:51 +0200552 * @param[out] word_p Pointer to the read string. Can return NULL if \p arg is #Y_MAYBE_STR_ARG.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200553 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read string. If not needed,
554 * set to NULL. Otherwise equal to \p word_p.
555 * @param[out] word_len Length of the read string.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200556 * @return LY_ERR values.
Michal Vasko7fbc8162018-09-17 10:35:16 +0200557 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200558LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100559get_argument(struct lys_yang_parser_ctx *ctx, enum yang_arg arg, uint16_t *flags, char **word_p,
Radek Krejci0f969882020-08-21 16:56:47 +0200560 char **word_b, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200561{
Michal Vaskob68ea142021-04-26 13:46:00 +0200562 LY_ERR ret;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200563 size_t buf_len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200564 uint8_t prefix = 0;
Michal Vasko69730152020-10-09 16:30:07 +0200565
Michal Vasko7fbc8162018-09-17 10:35:16 +0200566 /* word buffer - dynamically allocated */
567 *word_b = NULL;
568
569 /* word pointer - just a pointer to data */
570 *word_p = NULL;
571
572 *word_len = 0;
Radek Krejci33090f92020-12-17 20:12:46 +0100573 while (ctx->in->current[0]) {
574 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200575 case '\'':
576 case '\"':
577 if (*word_len) {
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200578 /* invalid - quotes cannot be in unquoted string and only optsep, ; or { can follow it */
Radek Krejci33090f92020-12-17 20:12:46 +0100579 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200580 "unquoted string character, optsep, semicolon or opening brace");
Michal Vaskob68ea142021-04-26 13:46:00 +0200581 ret = LY_EVALID;
582 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200583 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200584 if (flags) {
Radek Krejci33090f92020-12-17 20:12:46 +0100585 (*flags) |= ctx->in->current[0] == '\'' ? LYS_SINGLEQUOTED : LYS_DOUBLEQUOTED;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200586 }
Michal Vaskob68ea142021-04-26 13:46:00 +0200587 LY_CHECK_GOTO(ret = read_qstring(ctx, arg, word_p, word_b, word_len, &buf_len), error);
Michal Vasko55a16b92021-09-15 08:51:35 +0200588 if (!*word_p) {
589 /* do not return NULL word */
590 *word_p = "";
591 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200592 goto str_end;
593 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100594 if (ctx->in->current[1] == '/') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200595 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100596 MOVE_INPUT(ctx, 2);
Michal Vaskob68ea142021-04-26 13:46:00 +0200597 LY_CHECK_GOTO(ret = skip_comment(ctx, 1), error);
Radek Krejci33090f92020-12-17 20:12:46 +0100598 } else if (ctx->in->current[1] == '*') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200599 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100600 MOVE_INPUT(ctx, 2);
Michal Vaskob68ea142021-04-26 13:46:00 +0200601 LY_CHECK_GOTO(ret = skip_comment(ctx, 2), error);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200602 } else {
603 /* not a comment after all */
Michal Vaskob68ea142021-04-26 13:46:00 +0200604 LY_CHECK_GOTO(ret = buf_store_char(ctx, arg, word_p, word_len, word_b, &buf_len, 0, &prefix), error);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200605 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200606 break;
607 case ' ':
608 if (*word_len) {
609 /* word is finished */
610 goto str_end;
611 }
Radek Krejci33090f92020-12-17 20:12:46 +0100612 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200613 break;
614 case '\t':
615 if (*word_len) {
616 /* word is finished */
617 goto str_end;
618 }
619 /* tabs count for 8 spaces */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100620 ctx->indent += Y_TAB_SPACES;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200621
Radek Krejci33090f92020-12-17 20:12:46 +0100622 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200623 break;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200624 case '\r':
625 if (ctx->in->current[1] != '\n') {
626 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
Michal Vaskob68ea142021-04-26 13:46:00 +0200627 ret = LY_EVALID;
628 goto error;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200629 }
630 MOVE_INPUT(ctx, 1);
631 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200632 case '\n':
633 if (*word_len) {
634 /* word is finished */
635 goto str_end;
636 }
Radek Krejcidd713ce2021-01-04 23:12:12 +0100637 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid54412f2020-12-17 20:25:35 +0100638 MOVE_INPUT(ctx, 1);
639
Michal Vasko7fbc8162018-09-17 10:35:16 +0200640 /* reset indent */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200641 ctx->indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200642 break;
643 case ';':
644 case '{':
645 if (*word_len || (arg == Y_MAYBE_STR_ARG)) {
646 /* word is finished */
647 goto str_end;
648 }
649
Radek Krejci33090f92020-12-17 20:12:46 +0100650 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current, "an argument");
Michal Vaskob68ea142021-04-26 13:46:00 +0200651 ret = LY_EVALID;
652 goto error;
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200653 case '}':
654 /* invalid - braces cannot be in unquoted string (opening braces terminates the string and can follow it) */
Radek Krejci33090f92020-12-17 20:12:46 +0100655 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200656 "unquoted string character, optsep, semicolon or opening brace");
Michal Vaskob68ea142021-04-26 13:46:00 +0200657 ret = LY_EVALID;
658 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200659 default:
Michal Vaskob68ea142021-04-26 13:46:00 +0200660 LY_CHECK_GOTO(ret = buf_store_char(ctx, arg, word_p, word_len, word_b, &buf_len, 0, &prefix), error);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200661 break;
662 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200663 }
664
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200665 /* unexpected end of loop */
David Sedlákb3077192019-06-19 10:55:37 +0200666 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vaskob68ea142021-04-26 13:46:00 +0200667 ret = LY_EVALID;
668 goto error;
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200669
Michal Vasko7fbc8162018-09-17 10:35:16 +0200670str_end:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200671 /* terminating NULL byte for buf */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200672 if (*word_b) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200673 (*word_b) = ly_realloc(*word_b, (*word_len) + 1);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200674 LY_CHECK_ERR_RET(!(*word_b), LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200675 (*word_b)[*word_len] = '\0';
Michal Vasko7fbc8162018-09-17 10:35:16 +0200676 *word_p = *word_b;
677 }
678
679 return LY_SUCCESS;
Michal Vaskob68ea142021-04-26 13:46:00 +0200680
681error:
682 free(*word_b);
683 *word_b = NULL;
684 return ret;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200685}
686
Michal Vaskoea5abea2018-09-18 13:10:54 +0200687/**
688 * @brief Get another YANG keyword from the raw data.
689 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200690 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200691 * @param[out] kw YANG keyword read.
692 * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances.
693 * @param[out] word_len Length of the keyword in the data. Useful for extension instances.
694 *
695 * @return LY_ERR values.
696 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200697LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100698get_keyword(struct lys_yang_parser_ctx *ctx, enum ly_stmt *kw, char **word_p, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200699{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200700 uint8_t prefix;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200701 const char *word_start;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200702 size_t len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200703
704 if (word_p) {
705 *word_p = NULL;
706 *word_len = 0;
707 }
708
709 /* first skip "optsep", comments */
Radek Krejci33090f92020-12-17 20:12:46 +0100710 while (ctx->in->current[0]) {
711 switch (ctx->in->current[0]) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200712 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100713 if (ctx->in->current[1] == '/') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200714 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100715 MOVE_INPUT(ctx, 2);
716 LY_CHECK_RET(skip_comment(ctx, 1));
717 } else if (ctx->in->current[1] == '*') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200718 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100719 MOVE_INPUT(ctx, 2);
720 LY_CHECK_RET(skip_comment(ctx, 2));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200721 } else {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200722 /* error - not a comment after all, keyword cannot start with slash */
David Sedlákb3077192019-06-19 10:55:37 +0200723 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '/'.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200724 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200725 }
Radek Krejci13028282019-06-11 14:56:48 +0200726 continue;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200727 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200728 /* skip whitespaces (optsep) */
Radek Krejcidd713ce2021-01-04 23:12:12 +0100729 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200730 ctx->indent = 0;
731 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200732 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200733 /* skip whitespaces (optsep) */
734 ++ctx->indent;
735 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200736 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200737 /* skip whitespaces (optsep) */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100738 ctx->indent += Y_TAB_SPACES;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200739 break;
Michal Vasko50dcbc22021-03-25 12:21:20 +0100740 case '\r':
741 /* possible CRLF endline */
742 if (ctx->in->current[1] == '\n') {
743 break;
744 }
Radek Krejcid43298b2021-03-25 16:17:15 +0100745 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200746 default:
747 /* either a keyword start or an invalid character */
748 goto keyword_start;
749 }
750
Radek Krejci33090f92020-12-17 20:12:46 +0100751 ly_in_skip(ctx->in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200752 }
753
754keyword_start:
Radek Krejci33090f92020-12-17 20:12:46 +0100755 word_start = ctx->in->current;
Radek Krejcid54412f2020-12-17 20:25:35 +0100756 *kw = lysp_match_kw(ctx->in, &ctx->indent);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200757
aPiecek93582ed2021-05-25 14:49:06 +0200758 if (*kw == LY_STMT_SYNTAX_SEMICOLON) {
759 goto success;
760 } else if (*kw == LY_STMT_SYNTAX_LEFT_BRACE) {
761 ctx->depth++;
762 if (ctx->depth > LY_MAX_BLOCK_DEPTH) {
Michal Vasko8a67eff2021-12-07 14:04:47 +0100763 LOGERR(PARSER_CTX(ctx), LY_EINVAL, "The maximum number of block nestings has been exceeded.");
aPiecek93582ed2021-05-25 14:49:06 +0200764 return LY_EINVAL;
765 }
766 goto success;
767 } else if (*kw == LY_STMT_SYNTAX_RIGHT_BRACE) {
768 ctx->depth--;
Radek Krejci626df482018-10-11 15:06:31 +0200769 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200770 }
771
Radek Krejcid6b76452019-09-03 17:03:03 +0200772 if (*kw != LY_STMT_NONE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200773 /* make sure we have the whole keyword */
Radek Krejci33090f92020-12-17 20:12:46 +0100774 switch (ctx->in->current[0]) {
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200775 case '\r':
776 if (ctx->in->current[1] != '\n') {
777 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
778 return LY_EVALID;
779 }
780 MOVE_INPUT(ctx, 1);
781 /* fallthrough */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200782 case '\n':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200783 case '\t':
Radek Krejciabdd8062019-06-11 16:44:19 +0200784 case ' ':
785 /* mandatory "sep" is just checked, not eaten so nothing in the context is updated */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200786 break;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200787 case ':':
788 /* keyword is not actually a keyword, but prefix of an extension.
789 * To avoid repeated check of the prefix syntax, move to the point where the colon was read
790 * and we will be checking the keyword (extension instance) itself */
791 prefix = 1;
Radek Krejci33090f92020-12-17 20:12:46 +0100792 MOVE_INPUT(ctx, 1);
Radek Krejci156ccaf2018-10-15 15:49:17 +0200793 goto extension;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200794 case '{':
795 /* allowed only for input and output statements which can be without arguments */
Michal Vasko69730152020-10-09 16:30:07 +0200796 if ((*kw == LY_STMT_INPUT) || (*kw == LY_STMT_OUTPUT)) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200797 break;
798 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100799 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200800 default:
Radek Krejci33090f92020-12-17 20:12:46 +0100801 MOVE_INPUT(ctx, 1);
802 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start,
Michal Vasko69730152020-10-09 16:30:07 +0200803 "a keyword followed by a separator");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200804 return LY_EVALID;
805 }
806 } else {
807 /* still can be an extension */
808 prefix = 0;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200809
Radek Krejci156ccaf2018-10-15 15:49:17 +0200810extension:
Radek Krejcid54412f2020-12-17 20:25:35 +0100811 while (ctx->in->current[0] && (ctx->in->current[0] != ' ') && (ctx->in->current[0] != '\t') &&
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200812 (ctx->in->current[0] != '\n') && (ctx->in->current[0] != '\r') && (ctx->in->current[0] != '{') &&
813 (ctx->in->current[0] != ';')) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200814 uint32_t c = 0;
815
Radek Krejci33090f92020-12-17 20:12:46 +0100816 LY_CHECK_ERR_RET(ly_getutf8(&ctx->in->current, &c, &len),
817 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[-len]), LY_EVALID);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200818 ++ctx->indent;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200819 /* check character validity */
Michal Vasko63f3d842020-07-08 10:10:14 +0200820 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c,
Radek Krejci33090f92020-12-17 20:12:46 +0100821 ctx->in->current - len == word_start ? 1 : 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200822 }
Radek Krejci33090f92020-12-17 20:12:46 +0100823 if (!ctx->in->current[0]) {
David Sedlákb3077192019-06-19 10:55:37 +0200824 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200825 return LY_EVALID;
826 }
827
828 /* prefix is mandatory for extension instances */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200829 if (prefix != 2) {
Radek Krejci33090f92020-12-17 20:12:46 +0100830 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start, "a keyword");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200831 return LY_EVALID;
832 }
833
Radek Krejcid6b76452019-09-03 17:03:03 +0200834 *kw = LY_STMT_EXTENSION_INSTANCE;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200835 }
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200836
Radek Krejci626df482018-10-11 15:06:31 +0200837success:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200838 if (word_p) {
839 *word_p = (char *)word_start;
Radek Krejci33090f92020-12-17 20:12:46 +0100840 *word_len = ctx->in->current - word_start;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200841 }
842
843 return LY_SUCCESS;
844}
845
Michal Vaskoea5abea2018-09-18 13:10:54 +0200846/**
847 * @brief Parse extension instance substatements.
848 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200849 * @param[in] ctx yang parser context for logging.
Radek Krejci335332a2019-09-05 13:03:35 +0200850 * @param[in] kw Statement keyword value matching @p word value.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200851 * @param[in] word Extension instance substatement name (keyword).
852 * @param[in] word_len Extension instance substatement name length.
853 * @param[in,out] child Children of this extension instance to add to.
854 *
855 * @return LY_ERR values.
856 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200857static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100858parse_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 +0200859 struct lysp_stmt **child)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200860{
861 char *buf;
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100862 LY_ERR ret = LY_SUCCESS;
Radek Krejci335332a2019-09-05 13:03:35 +0200863 enum ly_stmt child_kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200864 struct lysp_stmt *stmt, *par_child;
865
866 stmt = calloc(1, sizeof *stmt);
867 LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM);
868
Radek Krejcibb9b1982019-04-08 14:24:59 +0200869 /* insert into parent statements */
870 if (!*child) {
871 *child = stmt;
872 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +0200873 for (par_child = *child; par_child->next; par_child = par_child->next) {}
Radek Krejcibb9b1982019-04-08 14:24:59 +0200874 par_child->next = stmt;
875 }
876
Michal Vaskofc2cd072021-02-24 13:17:17 +0100877 /* statement */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200878 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), word, word_len, &stmt->stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200879
880 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100881 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, &stmt->flags, &word, &buf, &word_len));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200882 if (word) {
Michal Vasko12ef5362022-09-16 15:13:58 +0200883 INSERT_WORD_GOTO(ctx, buf, stmt->arg, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200884 }
885
Radek Krejci8df109d2021-04-23 12:19:08 +0200886 stmt->format = LY_VALUE_SCHEMA;
Michal Vasko8a67eff2021-12-07 14:04:47 +0100887 stmt->prefix_data = PARSER_CUR_PMOD(ctx);
Michal Vaskofc2cd072021-02-24 13:17:17 +0100888 stmt->kw = kw;
889
aPieceka24a2252021-05-07 10:52:31 +0200890 YANG_READ_SUBSTMT_FOR(ctx, child_kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Radek Krejci33090f92020-12-17 20:12:46 +0100891 LY_CHECK_RET(parse_ext_substmt(ctx, child_kw, word, word_len, &stmt->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200892 }
Michal Vasko12ef5362022-09-16 15:13:58 +0200893
894cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200895 return ret;
896}
897
Michal Vaskoea5abea2018-09-18 13:10:54 +0200898/**
899 * @brief Parse extension instance.
900 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200901 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200902 * @param[in] ext_name Extension instance substatement name (keyword).
903 * @param[in] ext_name_len Extension instance substatement name length.
Radek Krejcifc596f92021-02-26 22:40:26 +0100904 * @param[in] insubstmt The statement this extension instance is a substatement of.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200905 * @param[in] insubstmt_index Index of the keyword instance this extension instance is a substatement of.
906 * @param[in,out] exts Extension instances to add to.
907 *
908 * @return LY_ERR values.
909 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200910static LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +0100911parse_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 +0200912 LY_ARRAY_COUNT_TYPE insubstmt_index, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200913{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100914 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200915 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200916 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200917 struct lysp_ext_instance *e;
Radek Krejcid6b76452019-09-03 17:03:03 +0200918 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200919
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200920 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *exts, e, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200921
Michal Vaskofc2cd072021-02-24 13:17:17 +0100922 if (!ly_strnchr(ext_name, ':', ext_name_len)) {
923 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Extension instance \"%*.s\" without the mandatory prefix.", ext_name_len, ext_name);
924 return LY_EVALID;
925 }
926
927 /* store name */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200928 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), ext_name, ext_name_len, &e->name));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200929
930 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100931 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, NULL, &word, &buf, &word_len));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200932 if (word) {
Michal Vasko12ef5362022-09-16 15:13:58 +0200933 INSERT_WORD_GOTO(ctx, buf, e->argument, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200934 }
935
Michal Vaskofc2cd072021-02-24 13:17:17 +0100936 /* store the rest of information */
Radek Krejci8df109d2021-04-23 12:19:08 +0200937 e->format = LY_VALUE_SCHEMA;
aPiecek60d9d672021-04-27 15:49:57 +0200938 e->parsed = NULL;
Michal Vasko8a67eff2021-12-07 14:04:47 +0100939 e->prefix_data = PARSER_CUR_PMOD(ctx);
Radek Krejciab430862021-03-02 20:13:40 +0100940 e->parent_stmt = insubstmt;
941 e->parent_stmt_index = insubstmt_index;
Michal Vaskofc2cd072021-02-24 13:17:17 +0100942
aPieceka24a2252021-05-07 10:52:31 +0200943 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Radek Krejci33090f92020-12-17 20:12:46 +0100944 LY_CHECK_RET(parse_ext_substmt(ctx, kw, word, word_len, &e->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200945 }
Michal Vasko12ef5362022-09-16 15:13:58 +0200946
947cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200948 return ret;
949}
950
Michal Vaskoea5abea2018-09-18 13:10:54 +0200951/**
952 * @brief Parse a generic text field without specific constraints. Those are contact, organization,
953 * description, etc...
954 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200955 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200956 * @param[in] substmt Type of this substatement.
957 * @param[in] substmt_index Index of this substatement.
958 * @param[in,out] value Place to store the parsed value.
959 * @param[in] arg Type of the YANG keyword argument (of the value).
960 * @param[in,out] exts Extension instances to add to.
961 *
962 * @return LY_ERR values.
963 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200964static LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +0100965parse_text_field(struct lys_yang_parser_ctx *ctx, enum ly_stmt substmt, uint32_t substmt_index,
Radek Krejci0f969882020-08-21 16:56:47 +0200966 const char **value, enum yang_arg arg, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200967{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100968 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200969 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200970 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200971 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200972
973 if (*value) {
Radek Krejci3972b332021-03-02 16:34:31 +0100974 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200975 return LY_EVALID;
976 }
977
978 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +0100979 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200980
981 /* store value and spend buf if allocated */
Michal Vasko12ef5362022-09-16 15:13:58 +0200982 INSERT_WORD_GOTO(ctx, buf, *value, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200983
aPieceka24a2252021-05-07 10:52:31 +0200984 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200985 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200986 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +0100987 LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, substmt_index, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200988 break;
989 default:
Radek Krejci3972b332021-03-02 16:34:31 +0100990 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200991 return LY_EVALID;
992 }
993 }
Michal Vasko12ef5362022-09-16 15:13:58 +0200994
995cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200996 return ret;
997}
998
Michal Vaskoea5abea2018-09-18 13:10:54 +0200999/**
1000 * @brief Parse the yang-version statement.
1001 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001002 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001003 * @param[out] version Storage for the parsed information.
Michal Vasko63f3d842020-07-08 10:10:14 +02001004 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001005 *
1006 * @return LY_ERR values.
1007 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001008static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001009parse_yangversion(struct lys_yang_parser_ctx *ctx, uint8_t *version, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001010{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001011 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001012 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001013 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001014 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001015
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001016 if (*version) {
David Sedlákb3077192019-06-19 10:55:37 +02001017 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001018 return LY_EVALID;
1019 }
1020
1021 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001022 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001023
Radek Krejci96e48da2020-09-04 13:18:06 +02001024 if ((word_len == 1) && !strncmp(word, "1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001025 *version = LYS_VERSION_1_0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001026 } else if ((word_len == ly_strlen_const("1.1")) && !strncmp(word, "1.1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001027 *version = LYS_VERSION_1_1;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001028 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001029 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001030 free(buf);
1031 return LY_EVALID;
1032 }
1033 free(buf);
1034
aPieceka24a2252021-05-07 10:52:31 +02001035 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001036 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001037 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001038 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_YANG_VERSION, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001039 break;
1040 default:
David Sedlákb3077192019-06-19 10:55:37 +02001041 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001042 return LY_EVALID;
1043 }
1044 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001045 return ret;
1046}
1047
Michal Vaskoea5abea2018-09-18 13:10:54 +02001048/**
1049 * @brief Parse the belongs-to statement.
1050 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001051 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001052 * @param[in,out] prefix Place to store the parsed belongs-to prefix value.
1053 * @param[in,out] exts Extension instances to add to.
1054 *
1055 * @return LY_ERR values.
1056 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001057static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001058parse_belongsto(struct lys_yang_parser_ctx *ctx, const char **prefix, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001059{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001060 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001061 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001062 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001063 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001064
Michal Vaskoc3781c32020-10-06 14:04:08 +02001065 if (*prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001066 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001067 return LY_EVALID;
1068 }
1069
Michal Vaskoc3781c32020-10-06 14:04:08 +02001070 /* get value, it must match the main module */
Radek Krejci33090f92020-12-17 20:12:46 +01001071 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko8a67eff2021-12-07 14:04:47 +01001072 if (ly_strncmp(PARSER_CUR_PMOD(ctx)->mod->name, word, word_len)) {
Michal Vaskoc3781c32020-10-06 14:04:08 +02001073 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Submodule \"belongs-to\" value \"%.*s\" does not match its module name \"%s\".",
Michal Vasko8a67eff2021-12-07 14:04:47 +01001074 (int)word_len, word, PARSER_CUR_PMOD(ctx)->mod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02001075 free(buf);
1076 return LY_EVALID;
1077 }
1078 free(buf);
Radek Krejcif09e4e82019-06-14 15:08:11 +02001079
aPieceka24a2252021-05-07 10:52:31 +02001080 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001081 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001082 case LY_STMT_PREFIX:
Radek Krejcifc596f92021-02-26 22:40:26 +01001083 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_PREFIX, 0, prefix, Y_IDENTIF_ARG, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001084 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001085 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001086 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_BELONGS_TO, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001087 break;
1088 default:
David Sedlákb3077192019-06-19 10:55:37 +02001089 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001090 return LY_EVALID;
1091 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001092 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001093 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001094checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001095 /* mandatory substatements */
1096 if (!*prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001097 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001098 return LY_EVALID;
1099 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001100 return ret;
1101}
1102
Michal Vaskoea5abea2018-09-18 13:10:54 +02001103/**
1104 * @brief Parse the revision-date statement.
1105 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001106 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001107 * @param[in,out] rev Array to store the parsed value in.
1108 * @param[in,out] exts Extension instances to add to.
1109 *
1110 * @return LY_ERR values.
1111 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001112static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001113parse_revisiondate(struct lys_yang_parser_ctx *ctx, char *rev, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001114{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001115 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001116 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001117 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001118 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001119
1120 if (rev[0]) {
David Sedlákb3077192019-06-19 10:55:37 +02001121 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001122 return LY_EVALID;
1123 }
1124
1125 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001126 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001127
1128 /* check value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001129 if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision-date")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001130 free(buf);
1131 return LY_EVALID;
1132 }
1133
1134 /* store value and spend buf if allocated */
1135 strncpy(rev, word, word_len);
1136 free(buf);
1137
aPieceka24a2252021-05-07 10:52:31 +02001138 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001139 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001140 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001141 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_REVISION_DATE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001142 break;
1143 default:
David Sedlákb3077192019-06-19 10:55:37 +02001144 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001145 return LY_EVALID;
1146 }
1147 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001148 return ret;
1149}
1150
Michal Vaskoea5abea2018-09-18 13:10:54 +02001151/**
1152 * @brief Parse the include statement.
1153 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001154 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001155 * @param[in] module_name Name of the module to check name collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001156 * @param[in,out] includes Parsed includes to add to.
1157 *
1158 * @return LY_ERR values.
1159 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001160static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001161parse_include(struct lys_yang_parser_ctx *ctx, const char *module_name, struct lysp_include **includes)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001162{
Radek Krejcid33273d2018-10-25 14:55:52 +02001163 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001164 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001165 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001166 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001167 struct lysp_include *inc;
1168
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001169 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *includes, inc, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001170
1171 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001172 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001173
Michal Vasko12ef5362022-09-16 15:13:58 +02001174 INSERT_WORD_GOTO(ctx, buf, inc->name, word, word_len, ret, cleanup);
Radek Krejci086c7132018-10-26 15:29:04 +02001175
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001176 /* submodules share the namespace with the module names, so there must not be
1177 * a module of the same name in the context, no need for revision matching */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001178 if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(PARSER_CTX(ctx), inc->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01001179 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", inc->name);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001180 return LY_EVALID;
1181 }
1182
aPieceka24a2252021-05-07 10:52:31 +02001183 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001184 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001185 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001186 PARSER_CHECK_STMTVER2_RET(ctx, "description", "include");
Radek Krejcifc596f92021-02-26 22:40:26 +01001187 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 +02001188 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001189 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001190 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "include");
Radek Krejcifc596f92021-02-26 22:40:26 +01001191 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 +02001192 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001193 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001194 LY_CHECK_RET(parse_revisiondate(ctx, inc->rev, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001195 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001196 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001197 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_INCLUDE, 0, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001198 break;
1199 default:
David Sedlákb3077192019-06-19 10:55:37 +02001200 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001201 return LY_EVALID;
1202 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001203 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001204
1205cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001206 return ret;
1207}
1208
Michal Vaskoea5abea2018-09-18 13:10:54 +02001209/**
1210 * @brief Parse the import statement.
1211 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001212 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001213 * @param[in] module_prefix Prefix of the module to check prefix collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001214 * @param[in,out] imports Parsed imports to add to.
1215 *
1216 * @return LY_ERR values.
1217 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001218static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001219parse_import(struct lys_yang_parser_ctx *ctx, const char *module_prefix, struct lysp_import **imports)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001220{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001221 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001222 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001223 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001224 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001225 struct lysp_import *imp;
1226
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001227 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *imports, imp, LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001228
1229 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001230 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02001231 INSERT_WORD_GOTO(ctx, buf, imp->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001232
aPieceka24a2252021-05-07 10:52:31 +02001233 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001234 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001235 case LY_STMT_PREFIX:
Radek Krejcifc596f92021-02-26 22:40:26 +01001236 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 +01001237 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 +02001238 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001239 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001240 PARSER_CHECK_STMTVER2_RET(ctx, "description", "import");
Radek Krejcifc596f92021-02-26 22:40:26 +01001241 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 +02001242 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001243 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001244 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "import");
Radek Krejcifc596f92021-02-26 22:40:26 +01001245 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 +02001246 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001247 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001248 LY_CHECK_RET(parse_revisiondate(ctx, imp->rev, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001249 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001250 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001251 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_IMPORT, 0, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001252 break;
1253 default:
David Sedlákb3077192019-06-19 10:55:37 +02001254 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001255 return LY_EVALID;
1256 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001257 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001258 LY_CHECK_RET(ret);
Michal Vasko12ef5362022-09-16 15:13:58 +02001259
Radek Krejci6d6556c2018-11-08 09:37:45 +01001260checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001261 /* mandatory substatements */
David Sedlákb3077192019-06-19 10:55:37 +02001262 LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001263
Michal Vasko12ef5362022-09-16 15:13:58 +02001264cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001265 return ret;
1266}
1267
Michal Vaskoea5abea2018-09-18 13:10:54 +02001268/**
1269 * @brief Parse the revision statement.
1270 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001271 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001272 * @param[in,out] revs Parsed revisions to add to.
1273 *
1274 * @return LY_ERR values.
1275 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001276static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001277parse_revision(struct lys_yang_parser_ctx *ctx, struct lysp_revision **revs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001278{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001279 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001280 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001281 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001282 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001283 struct lysp_revision *rev;
1284
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001285 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *revs, rev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001286
1287 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001288 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001289
1290 /* check value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001291 if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision")) {
David Sedlák68ef3dc2019-07-22 13:40:19 +02001292 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001293 return LY_EVALID;
1294 }
1295
Radek Krejcib7db73a2018-10-24 14:18:40 +02001296 strncpy(rev->date, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001297 free(buf);
1298
aPieceka24a2252021-05-07 10:52:31 +02001299 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001300 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001301 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001302 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 +02001303 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001304 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001305 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 +02001306 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001307 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001308 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_REVISION, 0, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001309 break;
1310 default:
David Sedlákb3077192019-06-19 10:55:37 +02001311 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001312 return LY_EVALID;
1313 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001314 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001315 return ret;
1316}
1317
Michal Vaskoea5abea2018-09-18 13:10:54 +02001318/**
1319 * @brief Parse a generic text field that can have more instances such as base.
1320 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001321 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001322 * @param[in] substmt Type of this substatement.
1323 * @param[in,out] texts Parsed values to add to.
1324 * @param[in] arg Type of the expected argument.
1325 * @param[in,out] exts Extension instances to add to.
1326 *
1327 * @return LY_ERR values.
1328 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001329static LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +01001330parse_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 +02001331 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001332{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001333 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001334 char *buf, *word;
Radek Krejci151a5b72018-10-19 14:21:44 +02001335 const char **item;
1336 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001337 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001338
1339 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001340 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *texts, item, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001341
1342 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001343 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001344
Michal Vasko12ef5362022-09-16 15:13:58 +02001345 INSERT_WORD_GOTO(ctx, buf, *item, word, word_len, ret, cleanup);
aPieceka24a2252021-05-07 10:52:31 +02001346 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001347 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001348 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001349 LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, LY_ARRAY_COUNT(*texts) - 1, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001350 break;
1351 default:
Radek Krejci3972b332021-03-02 16:34:31 +01001352 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001353 return LY_EVALID;
1354 }
1355 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001356
1357cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001358 return ret;
1359}
1360
Michal Vaskoea5abea2018-09-18 13:10:54 +02001361/**
Michal Vasko7f45cf22020-10-01 12:49:44 +02001362 * @brief Parse a generic text field that can have more instances such as base.
1363 *
1364 * @param[in] ctx yang parser context for logging.
Michal Vasko7f45cf22020-10-01 12:49:44 +02001365 * @param[in] substmt Type of this substatement.
1366 * @param[in,out] qnames Parsed qnames to add to.
1367 * @param[in] arg Type of the expected argument.
1368 * @param[in,out] exts Extension instances to add to.
1369 *
1370 * @return LY_ERR values.
1371 */
1372static LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +01001373parse_qnames(struct lys_yang_parser_ctx *ctx, enum ly_stmt substmt, struct lysp_qname **qnames,
Michal Vasko7f45cf22020-10-01 12:49:44 +02001374 enum yang_arg arg, struct lysp_ext_instance **exts)
1375{
1376 LY_ERR ret = LY_SUCCESS;
1377 char *buf, *word;
1378 struct lysp_qname *item;
1379 size_t word_len;
1380 enum ly_stmt kw;
1381
1382 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001383 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *qnames, item, LY_EMEM);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001384
1385 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001386 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001387
Michal Vasko12ef5362022-09-16 15:13:58 +02001388 INSERT_WORD_GOTO(ctx, buf, item->str, word, word_len, ret, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001389 item->mod = PARSER_CUR_PMOD(ctx);
aPieceka24a2252021-05-07 10:52:31 +02001390 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02001391 switch (kw) {
1392 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001393 LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, LY_ARRAY_COUNT(*qnames) - 1, exts));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001394 break;
1395 default:
Radek Krejci3972b332021-03-02 16:34:31 +01001396 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(substmt));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001397 return LY_EVALID;
1398 }
1399 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001400
1401cleanup:
Michal Vasko7f45cf22020-10-01 12:49:44 +02001402 return ret;
1403}
1404
1405/**
Michal Vaskoea5abea2018-09-18 13:10:54 +02001406 * @brief Parse the config statement.
1407 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001408 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001409 * @param[in,out] flags Flags to add to.
1410 * @param[in,out] exts Extension instances to add to.
1411 *
1412 * @return LY_ERR values.
1413 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001414static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001415parse_config(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001416{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001417 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001418 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001419 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001420 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001421
1422 if (*flags & LYS_CONFIG_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001423 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001424 return LY_EVALID;
1425 }
1426
1427 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001428 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001429
Radek Krejcif13b87b2020-12-01 22:02:17 +01001430 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001431 *flags |= LYS_CONFIG_W;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001432 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001433 *flags |= LYS_CONFIG_R;
1434 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001435 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001436 free(buf);
1437 return LY_EVALID;
1438 }
1439 free(buf);
1440
aPieceka24a2252021-05-07 10:52:31 +02001441 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001442 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001443 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001444 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_CONFIG, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001445 break;
1446 default:
David Sedlákb3077192019-06-19 10:55:37 +02001447 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001448 return LY_EVALID;
1449 }
1450 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001451 return ret;
1452}
1453
Michal Vaskoea5abea2018-09-18 13:10:54 +02001454/**
1455 * @brief Parse the mandatory statement.
1456 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001457 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001458 * @param[in,out] flags Flags to add to.
1459 * @param[in,out] exts Extension instances to add to.
1460 *
1461 * @return LY_ERR values.
1462 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001463static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001464parse_mandatory(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001465{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001466 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001467 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001468 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001469 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001470
1471 if (*flags & LYS_MAND_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001472 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001473 return LY_EVALID;
1474 }
1475
1476 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001477 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001478
Radek Krejcif13b87b2020-12-01 22:02:17 +01001479 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001480 *flags |= LYS_MAND_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001481 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001482 *flags |= LYS_MAND_FALSE;
1483 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001484 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001485 free(buf);
1486 return LY_EVALID;
1487 }
1488 free(buf);
1489
aPieceka24a2252021-05-07 10:52:31 +02001490 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001491 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001492 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001493 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MANDATORY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001494 break;
1495 default:
David Sedlákb3077192019-06-19 10:55:37 +02001496 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001497 return LY_EVALID;
1498 }
1499 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001500 return ret;
1501}
1502
Michal Vaskoea5abea2018-09-18 13:10:54 +02001503/**
1504 * @brief Parse a restriction such as range or length.
1505 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001506 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001507 * @param[in] restr_kw Type of this particular restriction.
1508 * @param[in,out] exts Extension instances to add to.
1509 *
1510 * @return LY_ERR values.
1511 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001512static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001513parse_restr(struct lys_yang_parser_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr *restr)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001514{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001515 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001516 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001517 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001518 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001519
1520 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001521 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001522
Michal Vaskob36053d2020-03-26 15:49:30 +01001523 CHECK_NONEMPTY(ctx, word_len, ly_stmt2str(restr_kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02001524 INSERT_WORD_GOTO(ctx, buf, restr->arg.str, word, word_len, ret, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001525 restr->arg.mod = PARSER_CUR_PMOD(ctx);
aPieceka24a2252021-05-07 10:52:31 +02001526 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001527 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001528 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001529 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 +02001530 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001531 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001532 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 +02001533 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001534 case LY_STMT_ERROR_APP_TAG:
Radek Krejcifc596f92021-02-26 22:40:26 +01001535 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 +02001536 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001537 case LY_STMT_ERROR_MESSAGE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001538 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 +02001539 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001540 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001541 LY_CHECK_RET(parse_ext(ctx, word, word_len, restr_kw, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001542 break;
1543 default:
David Sedlákb3077192019-06-19 10:55:37 +02001544 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001545 return LY_EVALID;
1546 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001547 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001548
1549cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001550 return ret;
1551}
1552
Michal Vaskoea5abea2018-09-18 13:10:54 +02001553/**
1554 * @brief Parse a restriction that can have more instances such as must.
1555 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001556 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001557 * @param[in] restr_kw Type of this particular restriction.
1558 * @param[in,out] restrs Restrictions to add to.
1559 *
1560 * @return LY_ERR values.
1561 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001562static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001563parse_restrs(struct lys_yang_parser_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr **restrs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001564{
1565 struct lysp_restr *restr;
1566
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001567 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *restrs, restr, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01001568 return parse_restr(ctx, restr_kw, restr);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001569}
1570
Michal Vaskoea5abea2018-09-18 13:10:54 +02001571/**
1572 * @brief Parse the status statement.
1573 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001574 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001575 * @param[in,out] flags Flags to add to.
1576 * @param[in,out] exts Extension instances to add to.
1577 *
1578 * @return LY_ERR values.
1579 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001580static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001581parse_status(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001582{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001583 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001584 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001585 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001586 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001587
1588 if (*flags & LYS_STATUS_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001589 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001590 return LY_EVALID;
1591 }
1592
1593 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001594 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001595
Radek Krejcif13b87b2020-12-01 22:02:17 +01001596 if ((word_len == ly_strlen_const("current")) && !strncmp(word, "current", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001597 *flags |= LYS_STATUS_CURR;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001598 } else if ((word_len == ly_strlen_const("deprecated")) && !strncmp(word, "deprecated", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001599 *flags |= LYS_STATUS_DEPRC;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001600 } else if ((word_len == ly_strlen_const("obsolete")) && !strncmp(word, "obsolete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001601 *flags |= LYS_STATUS_OBSLT;
1602 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001603 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001604 free(buf);
1605 return LY_EVALID;
1606 }
1607 free(buf);
1608
aPieceka24a2252021-05-07 10:52:31 +02001609 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001610 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001611 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001612 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_STATUS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001613 break;
1614 default:
David Sedlákb3077192019-06-19 10:55:37 +02001615 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001616 return LY_EVALID;
1617 }
1618 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001619 return ret;
1620}
1621
Michal Vaskoea5abea2018-09-18 13:10:54 +02001622/**
1623 * @brief Parse the when statement.
1624 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001625 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001626 * @param[in,out] when_p When pointer to parse to.
1627 *
1628 * @return LY_ERR values.
1629 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001630LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001631parse_when(struct lys_yang_parser_ctx *ctx, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001632{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001633 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001634 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001635 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001636 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001637 struct lysp_when *when;
1638
1639 if (*when_p) {
David Sedlákb3077192019-06-19 10:55:37 +02001640 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001641 return LY_EVALID;
1642 }
1643
1644 when = calloc(1, sizeof *when);
Michal Vasko12ef5362022-09-16 15:13:58 +02001645 LY_CHECK_ERR_GOTO(!when, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001646
1647 /* get value */
Michal Vasko12ef5362022-09-16 15:13:58 +02001648 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +01001649 CHECK_NONEMPTY(ctx, word_len, "when");
Michal Vasko12ef5362022-09-16 15:13:58 +02001650 INSERT_WORD_GOTO(ctx, buf, when->cond, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001651
Michal Vasko12ef5362022-09-16 15:13:58 +02001652 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto cleanup, goto cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001653 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001654 case LY_STMT_DESCRIPTION:
Michal Vasko12ef5362022-09-16 15:13:58 +02001655 LY_CHECK_GOTO(ret = parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &when->dsc, Y_STR_ARG, &when->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001656 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001657 case LY_STMT_REFERENCE:
Michal Vasko12ef5362022-09-16 15:13:58 +02001658 LY_CHECK_GOTO(ret = parse_text_field(ctx, LY_STMT_REFERENCE, 0, &when->ref, Y_STR_ARG, &when->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001659 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001660 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko12ef5362022-09-16 15:13:58 +02001661 LY_CHECK_GOTO(ret = parse_ext(ctx, word, word_len, LY_STMT_WHEN, 0, &when->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001662 break;
1663 default:
David Sedlákb3077192019-06-19 10:55:37 +02001664 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when");
Michal Vasko12ef5362022-09-16 15:13:58 +02001665 ret = LY_EVALID;
1666 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001667 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001668 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001669
1670cleanup:
1671 if (ret) {
1672 lysp_when_free(PARSER_CTX(ctx), when);
1673 free(when);
1674 } else {
1675 *when_p = when;
1676 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001677 return ret;
1678}
1679
Michal Vaskoea5abea2018-09-18 13:10:54 +02001680/**
1681 * @brief Parse the anydata or anyxml statement.
1682 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001683 * @param[in] ctx yang parser context for logging.
Radek Krejci39b7fc22021-02-26 23:29:18 +01001684 * @param[in] any_kw Type of this particular keyword.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001685 * @param[in,out] siblings Siblings to add to.
1686 *
1687 * @return LY_ERR values.
1688 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02001689LY_ERR
Radek Krejci39b7fc22021-02-26 23:29:18 +01001690parse_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 +02001691{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001692 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001693 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001694 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001695 struct lysp_node_anydata *any;
Radek Krejci39b7fc22021-02-26 23:29:18 +01001696 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001697
David Sedlák60adc092019-08-06 15:57:02 +02001698 /* create new structure and insert into siblings */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001699 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, any, next, LY_EMEM);
David Sedlák60adc092019-08-06 15:57:02 +02001700
Radek Krejci39b7fc22021-02-26 23:29:18 +01001701 any->nodetype = any_kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001702 any->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001703
Michal Vasko7fbc8162018-09-17 10:35:16 +02001704 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01001705 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02001706 INSERT_WORD_GOTO(ctx, buf, any->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001707
1708 /* parse substatements */
aPieceka24a2252021-05-07 10:52:31 +02001709 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001710 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001711 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01001712 LY_CHECK_RET(parse_config(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001713 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001714 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001715 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 +02001716 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001717 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001718 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &any->iffeatures, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001719 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001720 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01001721 LY_CHECK_RET(parse_mandatory(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001722 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001723 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01001724 LY_CHECK_RET(parse_restrs(ctx, kw, &any->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001725 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001726 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001727 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 +02001728 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001729 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001730 LY_CHECK_RET(parse_status(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001731 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001732 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01001733 LY_CHECK_RET(parse_when(ctx, &any->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001734 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001735 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001736 LY_CHECK_RET(parse_ext(ctx, word, word_len, any_kw, 0, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001737 break;
1738 default:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001739 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(any_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001740 return LY_EVALID;
1741 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001742 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001743
1744cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001745 return ret;
1746}
1747
Michal Vaskoea5abea2018-09-18 13:10:54 +02001748/**
1749 * @brief Parse the value or position statement. Substatement of type enum statement.
1750 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001751 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001752 * @param[in] val_kw Type of this particular keyword.
1753 * @param[in,out] value Value to write to.
1754 * @param[in,out] flags Flags to write to.
1755 * @param[in,out] exts Extension instances to add to.
1756 *
1757 * @return LY_ERR values.
1758 */
David Sedlákd6ce6d72019-07-16 17:30:18 +02001759LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001760parse_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 +02001761 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001762{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001763 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001764 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001765 size_t word_len;
Michal Vasko73d77ab2021-07-23 12:45:55 +02001766 long long int num = 0;
1767 unsigned long long int unum = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001768 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001769
1770 if (*flags & LYS_SET_VALUE) {
David Sedlákb3077192019-06-19 10:55:37 +02001771 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001772 return LY_EVALID;
1773 }
1774 *flags |= LYS_SET_VALUE;
1775
1776 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001777 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001778
Radek Krejcid6b76452019-09-03 17:03:03 +02001779 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 +02001780 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001781 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001782 }
1783
1784 errno = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001785 if (val_kw == LY_STMT_VALUE) {
Michal Vasko73d77ab2021-07-23 12:45:55 +02001786 num = strtoll(word, &ptr, LY_BASE_DEC);
Michal Vasko69730152020-10-09 16:30:07 +02001787 if ((num < INT64_C(-2147483648)) || (num > INT64_C(2147483647))) {
David Sedlákb3077192019-06-19 10:55:37 +02001788 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001789 goto error;
1790 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001791 } else {
Michal Vasko73d77ab2021-07-23 12:45:55 +02001792 unum = strtoull(word, &ptr, LY_BASE_DEC);
Radek Krejci8b764662018-11-14 14:15:13 +01001793 if (unum > UINT64_C(4294967295)) {
David Sedlákb3077192019-06-19 10:55:37 +02001794 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001795 goto error;
1796 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001797 }
1798 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001799 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001800 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001801 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001802 }
1803 if (errno == ERANGE) {
David Sedlákb3077192019-06-19 10:55:37 +02001804 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001805 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001806 }
Radek Krejcid6b76452019-09-03 17:03:03 +02001807 if (val_kw == LY_STMT_VALUE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001808 *value = num;
1809 } else {
1810 *value = unum;
1811 }
1812 free(buf);
1813
aPieceka24a2252021-05-07 10:52:31 +02001814 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001815 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001816 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001817 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 +02001818 break;
1819 default:
David Sedlákb3077192019-06-19 10:55:37 +02001820 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001821 return LY_EVALID;
1822 }
1823 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001824 return ret;
Radek Krejci8b764662018-11-14 14:15:13 +01001825
1826error:
1827 free(buf);
1828 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001829}
1830
Michal Vaskoea5abea2018-09-18 13:10:54 +02001831/**
1832 * @brief Parse the enum or bit statement. Substatement of type statement.
1833 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001834 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001835 * @param[in] enum_kw Type of this particular keyword.
1836 * @param[in,out] enums Enums or bits to add to.
1837 *
1838 * @return LY_ERR values.
1839 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001840static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001841parse_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 +02001842{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001843 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001844 char *buf, *word;
David Sedlák6544c182019-07-12 13:17:33 +02001845 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001846 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001847 struct lysp_type_enum *enm;
1848
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001849 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *enums, enm, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001850
1851 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001852 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 +02001853 if (enum_kw == LY_STMT_ENUM) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001854 ret = lysp_check_enum_name((struct lys_parser_ctx *)ctx, (const char *)word, word_len);
David Sedlák6544c182019-07-12 13:17:33 +02001855 LY_CHECK_ERR_RET(ret, free(buf), ret);
Radek Krejci335332a2019-09-05 13:03:35 +02001856 } /* else nothing specific for YANG_BIT */
Radek Krejci8b764662018-11-14 14:15:13 +01001857
Michal Vasko12ef5362022-09-16 15:13:58 +02001858 INSERT_WORD_GOTO(ctx, buf, enm->name, word, word_len, ret, cleanup);
Radek Krejci8b764662018-11-14 14:15:13 +01001859 CHECK_UNIQUENESS(ctx, *enums, name, ly_stmt2str(enum_kw), enm->name);
1860
aPieceka24a2252021-05-07 10:52:31 +02001861 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001862 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001863 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001864 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 +02001865 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001866 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02001867 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", ly_stmt2str(enum_kw));
Radek Krejcifc596f92021-02-26 22:40:26 +01001868 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &enm->iffeatures, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001869 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001870 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001871 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 +02001872 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001873 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001874 LY_CHECK_RET(parse_status(ctx, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001875 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001876 case LY_STMT_VALUE:
1877 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 +02001878 ly_stmt2str(enum_kw)), LY_EVALID);
Radek Krejci33090f92020-12-17 20:12:46 +01001879 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, &enm->value, &enm->flags, &enm->exts));
David Sedlák9fb515f2019-07-11 10:33:58 +02001880 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001881 case LY_STMT_POSITION:
1882 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 +02001883 ly_stmt2str(enum_kw)), LY_EVALID);
Radek Krejci33090f92020-12-17 20:12:46 +01001884 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, &enm->value, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001885 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001886 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001887 LY_CHECK_RET(parse_ext(ctx, word, word_len, enum_kw, 0, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001888 break;
1889 default:
David Sedlákb3077192019-06-19 10:55:37 +02001890 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001891 return LY_EVALID;
1892 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001893 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001894
1895cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001896 return ret;
1897}
1898
Michal Vaskoea5abea2018-09-18 13:10:54 +02001899/**
1900 * @brief Parse the fraction-digits statement. Substatement of type statement.
1901 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001902 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001903 * @param[in,out] fracdig Value to write to.
1904 * @param[in,out] exts Extension instances to add to.
1905 *
1906 * @return LY_ERR values.
1907 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001908static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001909parse_type_fracdigits(struct lys_yang_parser_ctx *ctx, uint8_t *fracdig, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001910{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001911 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001912 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001913 size_t word_len;
Michal Vasko73d77ab2021-07-23 12:45:55 +02001914 unsigned long long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02001915 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001916
1917 if (*fracdig) {
David Sedlákb3077192019-06-19 10:55:37 +02001918 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001919 return LY_EVALID;
1920 }
1921
1922 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001923 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001924
1925 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
David Sedlákb3077192019-06-19 10:55:37 +02001926 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001927 free(buf);
1928 return LY_EVALID;
1929 }
1930
1931 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02001932 num = strtoull(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001933 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001934 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001935 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001936 free(buf);
1937 return LY_EVALID;
1938 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001939 if ((errno == ERANGE) || (num > LY_TYPE_DEC64_FD_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02001940 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001941 free(buf);
1942 return LY_EVALID;
1943 }
1944 *fracdig = num;
1945 free(buf);
1946
aPieceka24a2252021-05-07 10:52:31 +02001947 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001948 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001949 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001950 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_FRACTION_DIGITS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001951 break;
1952 default:
David Sedlákb3077192019-06-19 10:55:37 +02001953 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001954 return LY_EVALID;
1955 }
1956 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001957 return ret;
1958}
1959
Michal Vaskoea5abea2018-09-18 13:10:54 +02001960/**
1961 * @brief Parse the require-instance statement. Substatement of type statement.
1962 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001963 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001964 * @param[in,out] reqinst Value to write to.
1965 * @param[in,out] flags Flags to write to.
1966 * @param[in,out] exts Extension instances to add to.
1967 *
1968 * @return LY_ERR values.
1969 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001970static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001971parse_type_reqinstance(struct lys_yang_parser_ctx *ctx, uint8_t *reqinst, uint16_t *flags,
Radek Krejci0f969882020-08-21 16:56:47 +02001972 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001973{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001974 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001975 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001976 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001977 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001978
1979 if (*flags & LYS_SET_REQINST) {
David Sedlákb3077192019-06-19 10:55:37 +02001980 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001981 return LY_EVALID;
1982 }
1983 *flags |= LYS_SET_REQINST;
1984
1985 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001986 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001987
Radek Krejcif13b87b2020-12-01 22:02:17 +01001988 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001989 *reqinst = 1;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001990 } else if ((word_len != ly_strlen_const("false")) || strncmp(word, "false", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001991 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001992 free(buf);
1993 return LY_EVALID;
1994 }
1995 free(buf);
1996
aPieceka24a2252021-05-07 10:52:31 +02001997 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001998 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001999 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002000 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_REQUIRE_INSTANCE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002001 break;
2002 default:
David Sedlákb3077192019-06-19 10:55:37 +02002003 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002004 return LY_EVALID;
2005 }
2006 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002007 return ret;
2008}
2009
Michal Vaskoea5abea2018-09-18 13:10:54 +02002010/**
2011 * @brief Parse the modifier statement. Substatement of type pattern statement.
2012 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002013 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002014 * @param[in,out] pat Value to write to.
2015 * @param[in,out] exts Extension instances to add to.
2016 *
2017 * @return LY_ERR values.
2018 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002019static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002020parse_type_pattern_modifier(struct lys_yang_parser_ctx *ctx, const char **pat, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002021{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002022 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002023 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002024 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002025 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002026
Radek Krejcif13b87b2020-12-01 22:02:17 +01002027 if ((*pat)[0] == LYSP_RESTR_PATTERN_NACK) {
David Sedlákb3077192019-06-19 10:55:37 +02002028 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002029 return LY_EVALID;
2030 }
2031
2032 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002033 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002034
Radek Krejcif13b87b2020-12-01 22:02:17 +01002035 if ((word_len != ly_strlen_const("invert-match")) || strncmp(word, "invert-match", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02002036 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002037 free(buf);
2038 return LY_EVALID;
2039 }
2040 free(buf);
2041
2042 /* replace the value in the dictionary */
2043 buf = malloc(strlen(*pat) + 1);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002044 LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002045 strcpy(buf, *pat);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002046 lydict_remove(PARSER_CTX(ctx), *pat);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002047
Radek Krejcif13b87b2020-12-01 22:02:17 +01002048 assert(buf[0] == LYSP_RESTR_PATTERN_ACK);
2049 buf[0] = LYSP_RESTR_PATTERN_NACK;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002050 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, pat));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002051
aPieceka24a2252021-05-07 10:52:31 +02002052 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002053 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002054 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002055 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MODIFIER, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002056 break;
2057 default:
David Sedlákb3077192019-06-19 10:55:37 +02002058 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002059 return LY_EVALID;
2060 }
2061 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002062 return ret;
2063}
2064
Michal Vaskoea5abea2018-09-18 13:10:54 +02002065/**
2066 * @brief Parse the pattern statement. Substatement of type statement.
2067 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002068 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002069 * @param[in,out] patterns Restrictions to add to.
2070 *
2071 * @return LY_ERR values.
2072 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002073static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002074parse_type_pattern(struct lys_yang_parser_ctx *ctx, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002075{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002076 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002077 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002078 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002079 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002080 struct lysp_restr *restr;
2081
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002082 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *patterns, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002083
2084 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002085 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002086
2087 /* add special meaning first byte */
2088 if (buf) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01002089 buf = ly_realloc(buf, word_len + 2);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002090 word = buf;
2091 } else {
2092 buf = malloc(word_len + 2);
2093 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002094 LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko54720192021-06-11 13:55:59 +02002095 if (word_len) {
2096 memmove(buf + 1, word, word_len);
2097 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01002098 buf[0] = LYSP_RESTR_PATTERN_ACK; /* pattern's default regular-match flag */
Radek Krejci86d106e2018-10-18 09:53:19 +02002099 buf[word_len + 1] = '\0'; /* terminating NULL byte */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002100 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, &restr->arg.str));
Michal Vasko8a67eff2021-12-07 14:04:47 +01002101 restr->arg.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002102
aPieceka24a2252021-05-07 10:52:31 +02002103 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002104 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002105 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002106 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 +02002107 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002108 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002109 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 +02002110 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002111 case LY_STMT_ERROR_APP_TAG:
Radek Krejcifc596f92021-02-26 22:40:26 +01002112 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 +02002113 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002114 case LY_STMT_ERROR_MESSAGE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002115 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 +02002116 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002117 case LY_STMT_MODIFIER:
Radek Krejci335332a2019-09-05 13:03:35 +02002118 PARSER_CHECK_STMTVER2_RET(ctx, "modifier", "pattern");
Radek Krejci33090f92020-12-17 20:12:46 +01002119 LY_CHECK_RET(parse_type_pattern_modifier(ctx, &restr->arg.str, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002120 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002121 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002122 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_PATTERN, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002123 break;
2124 default:
David Sedlákb3077192019-06-19 10:55:37 +02002125 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002126 return LY_EVALID;
2127 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002128 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002129 return ret;
2130}
2131
Michal Vaskoea5abea2018-09-18 13:10:54 +02002132/**
2133 * @brief Parse the type statement.
2134 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002135 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002136 * @param[in,out] type Type to wrote to.
2137 *
2138 * @return LY_ERR values.
2139 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002140static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002141parse_type(struct lys_yang_parser_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002142{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002143 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002144 char *buf, *word;
Michal Vasko004d3152020-06-11 19:59:22 +02002145 const char *str_path = NULL;
Radek Krejciefd22f62018-09-27 11:47:58 +02002146 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002147 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002148 struct lysp_type *nest_type;
2149
2150 if (type->name) {
David Sedlákb3077192019-06-19 10:55:37 +02002151 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002152 return LY_EVALID;
2153 }
2154
2155 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002156 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002157 INSERT_WORD_GOTO(ctx, buf, type->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002158
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002159 /* set module */
Michal Vasko8a67eff2021-12-07 14:04:47 +01002160 type->pmod = PARSER_CUR_PMOD(ctx);
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002161
aPieceka24a2252021-05-07 10:52:31 +02002162 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002163 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002164 case LY_STMT_BASE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002165 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 +01002166 type->flags |= LYS_SET_BASE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002167 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002168 case LY_STMT_BIT:
Radek Krejci33090f92020-12-17 20:12:46 +01002169 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->bits));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002170 type->flags |= LYS_SET_BIT;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002171 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002172 case LY_STMT_ENUM:
Radek Krejci33090f92020-12-17 20:12:46 +01002173 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->enums));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002174 type->flags |= LYS_SET_ENUM;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002175 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002176 case LY_STMT_FRACTION_DIGITS:
Radek Krejci33090f92020-12-17 20:12:46 +01002177 LY_CHECK_RET(parse_type_fracdigits(ctx, &type->fraction_digits, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002178 type->flags |= LYS_SET_FRDIGITS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002179 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002180 case LY_STMT_LENGTH:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002181 if (type->length) {
David Sedlákb3077192019-06-19 10:55:37 +02002182 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002183 return LY_EVALID;
2184 }
2185 type->length = calloc(1, sizeof *type->length);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002186 LY_CHECK_ERR_RET(!type->length, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002187
Radek Krejci33090f92020-12-17 20:12:46 +01002188 LY_CHECK_RET(parse_restr(ctx, kw, type->length));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002189 type->flags |= LYS_SET_LENGTH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002190 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002191 case LY_STMT_PATH:
Michal Vasko004d3152020-06-11 19:59:22 +02002192 if (type->path) {
Radek Krejci3972b332021-03-02 16:34:31 +01002193 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(LY_STMT_PATH));
Michal Vasko004d3152020-06-11 19:59:22 +02002194 return LY_EVALID;
2195 }
2196
aPiecek4bb1e372021-05-07 11:01:00 +02002197 /* Usually, in the parser_yang.c, the result of the parsing is stored directly in the
2198 * corresponding structure, so in case of failure, the lysp_module_free function will take
2199 * care of removing the parsed value from the dictionary. But in this case, it is not possible
2200 * to rely on lysp_module_free because the result of the parsing is stored in a local variable.
2201 */
2202 LY_CHECK_ERR_RET(ret = parse_text_field(ctx, LY_STMT_PATH, 0, &str_path, Y_STR_ARG, &type->exts),
2203 lydict_remove(PARSER_CTX(ctx), str_path), ret);
Michal Vaskoed725d72021-06-23 12:03:45 +02002204 ret = ly_path_parse(PARSER_CTX(ctx), NULL, str_path, 0, 1, LY_PATH_BEGIN_EITHER,
Michal Vasko69730152020-10-09 16:30:07 +02002205 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path);
aPiecek4bb1e372021-05-07 11:01:00 +02002206 /* Moreover, even if successful, the string is removed from the dictionary. */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002207 lydict_remove(PARSER_CTX(ctx), str_path);
Michal Vasko004d3152020-06-11 19:59:22 +02002208 LY_CHECK_RET(ret);
Radek Krejcid505e3d2018-11-13 09:04:17 +01002209 type->flags |= LYS_SET_PATH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002210 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002211 case LY_STMT_PATTERN:
Radek Krejci33090f92020-12-17 20:12:46 +01002212 LY_CHECK_RET(parse_type_pattern(ctx, &type->patterns));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002213 type->flags |= LYS_SET_PATTERN;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002214 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002215 case LY_STMT_RANGE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002216 if (type->range) {
David Sedlákb3077192019-06-19 10:55:37 +02002217 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002218 return LY_EVALID;
2219 }
2220 type->range = calloc(1, sizeof *type->range);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002221 LY_CHECK_ERR_RET(!type->range, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002222
Radek Krejci33090f92020-12-17 20:12:46 +01002223 LY_CHECK_RET(parse_restr(ctx, kw, type->range));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002224 type->flags |= LYS_SET_RANGE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002225 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002226 case LY_STMT_REQUIRE_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002227 LY_CHECK_RET(parse_type_reqinstance(ctx, &type->require_instance, &type->flags, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002228 /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002229 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002230 case LY_STMT_TYPE:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002231 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), type->types, nest_type, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01002232 LY_CHECK_RET(parse_type(ctx, nest_type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002233 type->flags |= LYS_SET_TYPE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002234 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002235 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002236 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_TYPE, 0, &type->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002237 break;
2238 default:
David Sedlákb3077192019-06-19 10:55:37 +02002239 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002240 return LY_EVALID;
2241 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002242 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002243
2244cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002245 return ret;
2246}
2247
Michal Vaskoea5abea2018-09-18 13:10:54 +02002248/**
2249 * @brief Parse the leaf statement.
2250 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002251 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002252 * @param[in,out] siblings Siblings to add to.
2253 *
2254 * @return LY_ERR values.
2255 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002256LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002257parse_leaf(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002258{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002259 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002260 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002261 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002262 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002263 struct lysp_node_leaf *leaf;
2264
David Sedlák60adc092019-08-06 15:57:02 +02002265 /* create new leaf structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002266 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, leaf, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002267 leaf->nodetype = LYS_LEAF;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002268 leaf->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002269
Michal Vasko7fbc8162018-09-17 10:35:16 +02002270 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002271 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002272 INSERT_WORD_GOTO(ctx, buf, leaf->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002273
2274 /* parse substatements */
aPieceka24a2252021-05-07 10:52:31 +02002275 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002276 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002277 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002278 LY_CHECK_RET(parse_config(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002279 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002280 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01002281 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DEFAULT, 0, &leaf->dflt.str, Y_STR_ARG, &leaf->exts));
Michal Vasko8a67eff2021-12-07 14:04:47 +01002282 leaf->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002283 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002284 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002285 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 +02002286 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002287 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002288 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &leaf->iffeatures, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002289 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002290 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002291 LY_CHECK_RET(parse_mandatory(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002292 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002293 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002294 LY_CHECK_RET(parse_restrs(ctx, kw, &leaf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002295 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002296 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002297 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 +02002298 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002299 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002300 LY_CHECK_RET(parse_status(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002301 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002302 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002303 LY_CHECK_RET(parse_type(ctx, &leaf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002304 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002305 case LY_STMT_UNITS:
Radek Krejcifc596f92021-02-26 22:40:26 +01002306 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 +02002307 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002308 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002309 LY_CHECK_RET(parse_when(ctx, &leaf->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002310 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002311 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002312 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_LEAF, 0, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002313 break;
2314 default:
David Sedlákb3077192019-06-19 10:55:37 +02002315 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002316 return LY_EVALID;
2317 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002318 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002319 LY_CHECK_RET(ret);
Michal Vasko12ef5362022-09-16 15:13:58 +02002320
Radek Krejci6d6556c2018-11-08 09:37:45 +01002321checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002322 /* mandatory substatements */
2323 if (!leaf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002324 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002325 return LY_EVALID;
2326 }
2327
Michal Vasko12ef5362022-09-16 15:13:58 +02002328cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002329 return ret;
2330}
2331
Michal Vaskoea5abea2018-09-18 13:10:54 +02002332/**
2333 * @brief Parse the max-elements statement.
2334 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002335 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002336 * @param[in,out] max Value to write to.
2337 * @param[in,out] flags Flags to write to.
2338 * @param[in,out] exts Extension instances to add to.
2339 *
2340 * @return LY_ERR values.
2341 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002342LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002343parse_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 +02002344{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002345 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002346 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002347 size_t word_len;
Michal Vasko73d77ab2021-07-23 12:45:55 +02002348 unsigned long long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002349 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002350
2351 if (*flags & LYS_SET_MAX) {
David Sedlákb3077192019-06-19 10:55:37 +02002352 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002353 return LY_EVALID;
2354 }
2355 *flags |= LYS_SET_MAX;
2356
2357 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002358 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002359
2360 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
David Sedlákb3077192019-06-19 10:55:37 +02002361 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002362 free(buf);
2363 return LY_EVALID;
2364 }
2365
Radek Krejci7f9b6512019-09-18 13:11:09 +02002366 if (ly_strncmp("unbounded", word, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002367 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02002368 num = strtoull(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002369 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002370 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002371 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002372 free(buf);
2373 return LY_EVALID;
2374 }
2375 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002376 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002377 free(buf);
2378 return LY_EVALID;
2379 }
2380
2381 *max = num;
Radek Krejcid6315102021-02-02 15:26:34 +01002382 } else {
2383 /* unbounded */
2384 *max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002385 }
2386 free(buf);
2387
aPieceka24a2252021-05-07 10:52:31 +02002388 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002389 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002390 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002391 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MAX_ELEMENTS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002392 break;
2393 default:
David Sedlákb3077192019-06-19 10:55:37 +02002394 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002395 return LY_EVALID;
2396 }
2397 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002398 return ret;
2399}
2400
Michal Vaskoea5abea2018-09-18 13:10:54 +02002401/**
2402 * @brief Parse the min-elements statement.
2403 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002404 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002405 * @param[in,out] min Value to write to.
2406 * @param[in,out] flags Flags to write to.
2407 * @param[in,out] exts Extension instances to add to.
2408 *
2409 * @return LY_ERR values.
2410 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002411LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002412parse_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 +02002413{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002414 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002415 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002416 size_t word_len;
Michal Vasko73d77ab2021-07-23 12:45:55 +02002417 unsigned long long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002418 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002419
2420 if (*flags & LYS_SET_MIN) {
David Sedlákb3077192019-06-19 10:55:37 +02002421 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002422 return LY_EVALID;
2423 }
2424 *flags |= LYS_SET_MIN;
2425
2426 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002427 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002428
2429 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
David Sedlákb3077192019-06-19 10:55:37 +02002430 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002431 free(buf);
2432 return LY_EVALID;
2433 }
2434
2435 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02002436 num = strtoull(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002437 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002438 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002439 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002440 free(buf);
2441 return LY_EVALID;
2442 }
2443 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002444 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002445 free(buf);
2446 return LY_EVALID;
2447 }
2448 *min = num;
2449 free(buf);
2450
aPieceka24a2252021-05-07 10:52:31 +02002451 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002452 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002453 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002454 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MIN_ELEMENTS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002455 break;
2456 default:
David Sedlákb3077192019-06-19 10:55:37 +02002457 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002458 return LY_EVALID;
2459 }
2460 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002461 return ret;
2462}
2463
Michal Vaskoea5abea2018-09-18 13:10:54 +02002464/**
2465 * @brief Parse the ordered-by statement.
2466 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002467 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002468 * @param[in,out] flags Flags to write to.
2469 * @param[in,out] exts Extension instances to add to.
2470 *
2471 * @return LY_ERR values.
2472 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002473static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002474parse_orderedby(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002475{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002476 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002477 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002478 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002479 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002480
2481 if (*flags & LYS_ORDBY_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02002482 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002483 return LY_EVALID;
2484 }
2485
2486 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002487 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002488
Radek Krejcif13b87b2020-12-01 22:02:17 +01002489 if ((word_len == ly_strlen_const("system")) && !strncmp(word, "system", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002490 *flags |= LYS_ORDBY_SYSTEM;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002491 } else if ((word_len == ly_strlen_const("user")) && !strncmp(word, "user", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002492 *flags |= LYS_ORDBY_USER;
2493 } else {
David Sedlákb3077192019-06-19 10:55:37 +02002494 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002495 free(buf);
2496 return LY_EVALID;
2497 }
2498 free(buf);
2499
aPieceka24a2252021-05-07 10:52:31 +02002500 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002501 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002502 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002503 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_ORDERED_BY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002504 break;
2505 default:
David Sedlákb3077192019-06-19 10:55:37 +02002506 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002507 return LY_EVALID;
2508 }
2509 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002510 return ret;
2511}
2512
Michal Vaskoea5abea2018-09-18 13:10:54 +02002513/**
2514 * @brief Parse the leaf-list statement.
2515 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002516 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002517 * @param[in,out] siblings Siblings to add to.
2518 *
2519 * @return LY_ERR values.
2520 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002521LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002522parse_leaflist(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002523{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002524 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002525 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002526 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002527 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002528 struct lysp_node_leaflist *llist;
2529
David Sedlák60adc092019-08-06 15:57:02 +02002530 /* create new leaf-list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002531 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, llist, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002532 llist->nodetype = LYS_LEAFLIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002533 llist->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002534
Michal Vasko7fbc8162018-09-17 10:35:16 +02002535 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002536 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002537 INSERT_WORD_GOTO(ctx, buf, llist->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002538
2539 /* parse substatements */
aPieceka24a2252021-05-07 10:52:31 +02002540 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002541 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002542 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002543 LY_CHECK_RET(parse_config(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002544 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002545 case LY_STMT_DEFAULT:
Radek Krejci335332a2019-09-05 13:03:35 +02002546 PARSER_CHECK_STMTVER2_RET(ctx, "default", "leaf-list");
Radek Krejcifc596f92021-02-26 22:40:26 +01002547 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, &llist->dflts, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002548 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002549 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002550 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 +02002551 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002552 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002553 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &llist->iffeatures, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002554 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002555 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002556 LY_CHECK_RET(parse_maxelements(ctx, &llist->max, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002557 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002558 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002559 LY_CHECK_RET(parse_minelements(ctx, &llist->min, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002560 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002561 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002562 LY_CHECK_RET(parse_restrs(ctx, kw, &llist->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002563 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002564 case LY_STMT_ORDERED_BY:
Radek Krejci33090f92020-12-17 20:12:46 +01002565 LY_CHECK_RET(parse_orderedby(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002566 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002567 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002568 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 +02002569 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002570 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002571 LY_CHECK_RET(parse_status(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002572 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002573 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002574 LY_CHECK_RET(parse_type(ctx, &llist->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002575 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002576 case LY_STMT_UNITS:
Radek Krejcifc596f92021-02-26 22:40:26 +01002577 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 +02002578 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002579 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002580 LY_CHECK_RET(parse_when(ctx, &llist->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002581 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002582 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002583 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_LEAF_LIST, 0, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002584 break;
2585 default:
David Sedlákb3077192019-06-19 10:55:37 +02002586 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002587 return LY_EVALID;
2588 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002589 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002590 LY_CHECK_RET(ret);
Michal Vasko12ef5362022-09-16 15:13:58 +02002591
Radek Krejci6d6556c2018-11-08 09:37:45 +01002592checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002593 /* mandatory substatements */
2594 if (!llist->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002595 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002596 return LY_EVALID;
2597 }
2598
Michal Vasko12ef5362022-09-16 15:13:58 +02002599cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002600 return ret;
2601}
2602
Michal Vaskoea5abea2018-09-18 13:10:54 +02002603/**
2604 * @brief Parse the refine statement.
2605 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002606 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002607 * @param[in,out] refines Refines to add to.
2608 *
2609 * @return LY_ERR values.
2610 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002611static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002612parse_refine(struct lys_yang_parser_ctx *ctx, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002613{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002614 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002615 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002616 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002617 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002618 struct lysp_refine *rf;
2619
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002620 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002621
2622 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002623 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01002624 CHECK_NONEMPTY(ctx, word_len, "refine");
Michal Vasko12ef5362022-09-16 15:13:58 +02002625 INSERT_WORD_GOTO(ctx, buf, rf->nodeid, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002626
aPieceka24a2252021-05-07 10:52:31 +02002627 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002628 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002629 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002630 LY_CHECK_RET(parse_config(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002631 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002632 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01002633 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, &rf->dflts, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002634 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002635 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002636 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 +02002637 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002638 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02002639 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "refine");
Radek Krejcifc596f92021-02-26 22:40:26 +01002640 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &rf->iffeatures, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002641 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002642 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002643 LY_CHECK_RET(parse_maxelements(ctx, &rf->max, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002644 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002645 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002646 LY_CHECK_RET(parse_minelements(ctx, &rf->min, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002647 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002648 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002649 LY_CHECK_RET(parse_restrs(ctx, kw, &rf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002650 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002651 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002652 LY_CHECK_RET(parse_mandatory(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002653 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002654 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002655 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 +02002656 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002657 case LY_STMT_PRESENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002658 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 +02002659 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002660 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002661 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_REFINE, 0, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002662 break;
2663 default:
David Sedlákb3077192019-06-19 10:55:37 +02002664 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002665 return LY_EVALID;
2666 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002667 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002668
2669cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002670 return ret;
2671}
2672
Michal Vaskoea5abea2018-09-18 13:10:54 +02002673/**
2674 * @brief Parse the typedef statement.
2675 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002676 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002677 * @param[in,out] typedefs Typedefs to add to.
2678 *
2679 * @return LY_ERR values.
2680 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002681static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002682parse_typedef(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002683{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002684 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002685 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002686 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002687 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002688 struct lysp_tpdf *tpdf;
2689
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002690 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002691
2692 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002693 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002694 INSERT_WORD_GOTO(ctx, buf, tpdf->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002695
2696 /* parse substatements */
aPieceka24a2252021-05-07 10:52:31 +02002697 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002698 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002699 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01002700 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DEFAULT, 0, &tpdf->dflt.str, Y_STR_ARG, &tpdf->exts));
Michal Vasko8a67eff2021-12-07 14:04:47 +01002701 tpdf->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002702 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002703 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002704 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 +02002705 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002706 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002707 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 +02002708 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002709 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002710 LY_CHECK_RET(parse_status(ctx, &tpdf->flags, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002711 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002712 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002713 LY_CHECK_RET(parse_type(ctx, &tpdf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002714 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002715 case LY_STMT_UNITS:
Radek Krejcifc596f92021-02-26 22:40:26 +01002716 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 +02002717 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002718 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002719 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_TYPEDEF, 0, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002720 break;
2721 default:
David Sedlákb3077192019-06-19 10:55:37 +02002722 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002723 return LY_EVALID;
2724 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002725 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002726 LY_CHECK_RET(ret);
Michal Vasko12ef5362022-09-16 15:13:58 +02002727
Radek Krejcibbe09a92018-11-08 09:36:54 +01002728checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002729 /* mandatory substatements */
2730 if (!tpdf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002731 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002732 return LY_EVALID;
2733 }
2734
Radek Krejcibbe09a92018-11-08 09:36:54 +01002735 /* store data for collision check */
aPiecek75b83a02021-06-29 10:34:38 +02002736 if (parent) {
aPiecek8d4e75d2021-06-24 14:47:06 +02002737 assert(ctx->main_ctx);
2738 LY_CHECK_RET(ly_set_add(&ctx->main_ctx->tpdfs_nodes, parent, 0, NULL));
Radek Krejcibbe09a92018-11-08 09:36:54 +01002739 }
2740
Michal Vasko12ef5362022-09-16 15:13:58 +02002741cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002742 return ret;
2743}
2744
Michal Vaskoea5abea2018-09-18 13:10:54 +02002745/**
2746 * @brief Parse the input or output statement.
2747 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002748 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002749 * @param[in] kw Type of this particular keyword
2750 * @param[in,out] inout_p Input/output pointer to write to.
2751 *
2752 * @return LY_ERR values.
2753 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002754static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002755parse_inout(struct lys_yang_parser_ctx *ctx, enum ly_stmt inout_kw, struct lysp_node *parent,
Radek Krejci2a9fc652021-01-22 17:44:34 +01002756 struct lysp_node_action_inout *inout_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002757{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002758 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002759 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002760 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002761 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002762 ly_bool input = &((struct lysp_node_action *)parent)->input == inout_p ? 1 : 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002763
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002764 if (inout_p->nodetype) {
David Sedlákb3077192019-06-19 10:55:37 +02002765 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002766 return LY_EVALID;
2767 }
2768
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002769 /* initiate structure */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002770 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), input ? "input" : "output", 0, &inout_p->name));
2771 inout_p->nodetype = input ? LYS_INPUT : LYS_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002772 inout_p->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002773
2774 /* parse substatements */
aPieceka24a2252021-05-07 10:52:31 +02002775 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002776 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002777 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002778 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", ly_stmt2str(inout_kw));
Radek Krejci0f969882020-08-21 16:56:47 +02002779 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002780 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002781 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002782 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002783 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01002784 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002785 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002786 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002787 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002788 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002789 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01002790 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002791 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002792 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002793 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002794 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002795 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002796 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002797 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002798 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01002799 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002800 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002801 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002802 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)inout_p, &inout_p->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002803 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002804 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002805 PARSER_CHECK_STMTVER2_RET(ctx, "must", ly_stmt2str(inout_kw));
Radek Krejci33090f92020-12-17 20:12:46 +01002806 LY_CHECK_RET(parse_restrs(ctx, kw, &inout_p->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002807 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002808 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002809 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)inout_p, &inout_p->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002810 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002811 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002812 LY_CHECK_RET(parse_ext(ctx, word, word_len, inout_kw, 0, &inout_p->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002813 break;
2814 default:
David Sedlákb3077192019-06-19 10:55:37 +02002815 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002816 return LY_EVALID;
2817 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002818 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002819 LY_CHECK_RET(ret);
Michal Vaskob83af8a2020-01-06 09:49:22 +01002820
Radek Krejci7fc68292019-06-12 13:51:09 +02002821checks:
Radek Krejci01180ac2021-01-27 08:48:22 +01002822 if (!inout_p->child) {
Michal Vaskob83af8a2020-01-06 09:49:22 +01002823 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "data-def-stmt", ly_stmt2str(inout_kw));
2824 return LY_EVALID;
2825 }
2826
Michal Vasko7fbc8162018-09-17 10:35:16 +02002827 return ret;
2828}
2829
Michal Vaskoea5abea2018-09-18 13:10:54 +02002830/**
2831 * @brief Parse the action statement.
2832 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002833 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002834 * @param[in,out] actions Actions to add to.
2835 *
2836 * @return LY_ERR values.
2837 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002838LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002839parse_action(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002840{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002841 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002842 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002843 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002844 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002845 struct lysp_node_action *act;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002846
Radek Krejci2a9fc652021-01-22 17:44:34 +01002847 LY_LIST_NEW_RET(PARSER_CTX(ctx), actions, act, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002848
2849 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002850 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002851 INSERT_WORD_GOTO(ctx, buf, act->name, word, word_len, ret, cleanup);
Michal Vasko1bf09392020-03-27 12:38:10 +01002852 act->nodetype = parent ? LYS_ACTION : LYS_RPC;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002853 act->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002854
aPieceka24a2252021-05-07 10:52:31 +02002855 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002856 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002857 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002858 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 +02002859 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002860 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002861 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &act->iffeatures, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002862 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002863 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002864 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 +02002865 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002866 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002867 LY_CHECK_RET(parse_status(ctx, &act->flags, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002868 break;
2869
Radek Krejcid6b76452019-09-03 17:03:03 +02002870 case LY_STMT_INPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002871 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->input));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002872 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002873 case LY_STMT_OUTPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002874 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->output));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002875 break;
2876
Radek Krejcid6b76452019-09-03 17:03:03 +02002877 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002878 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)act, &act->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002879 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002880 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002881 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)act, &act->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002882 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002883 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002884 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 +02002885 break;
2886 default:
David Sedlákb3077192019-06-19 10:55:37 +02002887 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), parent ? "action" : "rpc");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002888 return LY_EVALID;
2889 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002890 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002891 LY_CHECK_RET(ret);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002892
Radek Krejci2a9fc652021-01-22 17:44:34 +01002893checks:
Michal Vasko7f45cf22020-10-01 12:49:44 +02002894 /* always initialize inout, they are technically present (needed for later deviations/refines) */
2895 if (!act->input.nodetype) {
2896 act->input.nodetype = LYS_INPUT;
2897 act->input.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002898 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "input", 0, &act->input.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002899 }
2900 if (!act->output.nodetype) {
2901 act->output.nodetype = LYS_OUTPUT;
2902 act->output.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002903 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "output", 0, &act->output.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002904 }
2905
Michal Vasko12ef5362022-09-16 15:13:58 +02002906cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002907 return ret;
2908}
2909
Michal Vaskoea5abea2018-09-18 13:10:54 +02002910/**
2911 * @brief Parse the notification statement.
2912 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002913 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002914 * @param[in,out] notifs Notifications to add to.
2915 *
2916 * @return LY_ERR values.
2917 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002918LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002919parse_notif(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002920{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002921 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002922 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002923 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002924 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002925 struct lysp_node_notif *notif;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002926
Radek Krejci2a9fc652021-01-22 17:44:34 +01002927 LY_LIST_NEW_RET(PARSER_CTX(ctx), notifs, notif, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002928
2929 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002930 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002931 INSERT_WORD_GOTO(ctx, buf, notif->name, word, word_len, ret, cleanup);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002932 notif->nodetype = LYS_NOTIF;
2933 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002934
aPieceka24a2252021-05-07 10:52:31 +02002935 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002936 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002937 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002938 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 +02002939 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002940 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002941 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &notif->iffeatures, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002942 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002943 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002944 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 +02002945 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002946 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002947 LY_CHECK_RET(parse_status(ctx, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002948 break;
2949
Radek Krejcid6b76452019-09-03 17:03:03 +02002950 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002951 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "notification");
Radek Krejci0f969882020-08-21 16:56:47 +02002952 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002953 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002954 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002955 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002956 case LY_STMT_CHOICE:
Michal Vasko5ae1e1a2021-02-08 09:53:26 +01002957 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002958 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002959 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002960 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002961 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002962 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01002963 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002964 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002965 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002966 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002967 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002968 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002969 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002970 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002971 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01002972 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002973 break;
2974
Radek Krejcid6b76452019-09-03 17:03:03 +02002975 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002976 PARSER_CHECK_STMTVER2_RET(ctx, "must", "notification");
Radek Krejci33090f92020-12-17 20:12:46 +01002977 LY_CHECK_RET(parse_restrs(ctx, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002978 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002979 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002980 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)notif, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002981 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002982 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002983 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002984 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002985 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002986 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_NOTIFICATION, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002987 break;
2988 default:
David Sedlákb3077192019-06-19 10:55:37 +02002989 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002990 return LY_EVALID;
2991 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002992 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002993
Michal Vasko12ef5362022-09-16 15:13:58 +02002994cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002995 return ret;
2996}
2997
Michal Vaskoea5abea2018-09-18 13:10:54 +02002998/**
2999 * @brief Parse the grouping statement.
3000 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003001 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003002 * @param[in,out] groupings Groupings to add to.
3003 *
3004 * @return LY_ERR values.
3005 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003006LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01003007parse_grouping(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003008{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003009 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003010 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003011 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003012 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003013 struct lysp_node_grp *grp;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003014
Radek Krejci2a9fc652021-01-22 17:44:34 +01003015 LY_LIST_NEW_RET(PARSER_CTX(ctx), groupings, grp, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003016
3017 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003018 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003019 INSERT_WORD_GOTO(ctx, buf, grp->name, word, word_len, ret, cleanup);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003020 grp->nodetype = LYS_GROUPING;
3021 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003022
aPiecek63e080d2021-06-29 13:53:28 +02003023 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003024 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003025 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003026 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 +02003027 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003028 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003029 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 +02003030 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003031 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003032 LY_CHECK_RET(parse_status(ctx, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003033 break;
3034
Radek Krejcid6b76452019-09-03 17:03:03 +02003035 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003036 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "grouping");
Radek Krejci0f969882020-08-21 16:56:47 +02003037 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003038 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01003039 LY_CHECK_RET(parse_any(ctx, kw, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003040 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003041 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01003042 LY_CHECK_RET(parse_choice(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003043 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003044 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01003045 LY_CHECK_RET(parse_container(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003046 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003047 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01003048 LY_CHECK_RET(parse_leaf(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003049 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003050 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003051 LY_CHECK_RET(parse_leaflist(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003052 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003053 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003054 LY_CHECK_RET(parse_list(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003055 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003056 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01003057 LY_CHECK_RET(parse_uses(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003058 break;
3059
Radek Krejcid6b76452019-09-03 17:03:03 +02003060 case LY_STMT_TYPEDEF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01003061 LY_CHECK_RET(parse_typedef(ctx, &grp->node, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003062 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003063 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003064 PARSER_CHECK_STMTVER2_RET(ctx, "action", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003065 LY_CHECK_RET(parse_action(ctx, &grp->node, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003066 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003067 case LY_STMT_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01003068 LY_CHECK_RET(parse_grouping(ctx, &grp->node, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003069 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003070 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003071 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003072 LY_CHECK_RET(parse_notif(ctx, &grp->node, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003073 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003074 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003075 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_GROUPING, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003076 break;
3077 default:
David Sedlákb3077192019-06-19 10:55:37 +02003078 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003079 return LY_EVALID;
3080 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003081 }
aPiecek63e080d2021-06-29 13:53:28 +02003082 LY_CHECK_RET(ret);
Michal Vasko12ef5362022-09-16 15:13:58 +02003083
aPiecek63e080d2021-06-29 13:53:28 +02003084checks:
3085 /* store data for collision check */
3086 if (parent) {
3087 assert(ctx->main_ctx);
3088 LY_CHECK_RET(ly_set_add(&ctx->main_ctx->grps_nodes, parent, 0, NULL));
3089 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003090
Michal Vasko12ef5362022-09-16 15:13:58 +02003091cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003092 return ret;
3093}
3094
Michal Vaskoea5abea2018-09-18 13:10:54 +02003095/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02003096 * @brief Parse the augment statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003097 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003098 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003099 * @param[in,out] augments Augments to add to.
3100 *
3101 * @return LY_ERR values.
3102 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003103LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01003104parse_augment(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003105{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003106 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003107 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003108 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003109 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003110 struct lysp_node_augment *aug;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003111
Radek Krejci2a9fc652021-01-22 17:44:34 +01003112 LY_LIST_NEW_RET(PARSER_CTX(ctx), augments, aug, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003113
3114 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003115 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01003116 CHECK_NONEMPTY(ctx, word_len, "augment");
Michal Vasko12ef5362022-09-16 15:13:58 +02003117 INSERT_WORD_GOTO(ctx, buf, aug->nodeid, word, word_len, ret, cleanup);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003118 aug->nodetype = LYS_AUGMENT;
3119 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003120
aPieceka24a2252021-05-07 10:52:31 +02003121 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003122 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003123 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003124 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 +02003125 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003126 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003127 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &aug->iffeatures, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003128 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003129 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003130 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 +02003131 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003132 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003133 LY_CHECK_RET(parse_status(ctx, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003134 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003135 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003136 LY_CHECK_RET(parse_when(ctx, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003137 break;
3138
Radek Krejcid6b76452019-09-03 17:03:03 +02003139 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003140 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "augment");
Radek Krejci0f969882020-08-21 16:56:47 +02003141 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003142 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003143 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003144 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003145 case LY_STMT_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003146 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003147 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003148 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003149 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003150 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003151 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003152 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003153 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003154 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003155 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003156 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003157 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003158 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003159 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003160 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003161 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003162 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003163 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003164 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003165 break;
3166
Radek Krejcid6b76452019-09-03 17:03:03 +02003167 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003168 PARSER_CHECK_STMTVER2_RET(ctx, "action", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003169 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003170 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003171 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003172 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003173 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003174 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003175 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003176 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_AUGMENT, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003177 break;
3178 default:
David Sedlákb3077192019-06-19 10:55:37 +02003179 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003180 return LY_EVALID;
3181 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003182 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003183
Michal Vasko12ef5362022-09-16 15:13:58 +02003184cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003185 return ret;
3186}
3187
Michal Vaskoea5abea2018-09-18 13:10:54 +02003188/**
3189 * @brief Parse the uses statement.
3190 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003191 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003192 * @param[in,out] siblings Siblings to add to.
3193 *
3194 * @return LY_ERR values.
3195 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003196LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003197parse_uses(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003198{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003199 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003200 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003201 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003202 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003203 struct lysp_node_uses *uses;
3204
David Sedlák60adc092019-08-06 15:57:02 +02003205 /* create uses structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003206 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, uses, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003207 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003208 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003209
Michal Vasko7fbc8162018-09-17 10:35:16 +02003210 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003211 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003212 INSERT_WORD_GOTO(ctx, buf, uses->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003213
3214 /* parse substatements */
aPieceka24a2252021-05-07 10:52:31 +02003215 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003216 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003217 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003218 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 +02003219 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003220 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003221 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &uses->iffeatures, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003222 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003223 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003224 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 +02003225 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003226 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003227 LY_CHECK_RET(parse_status(ctx, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003228 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003229 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003230 LY_CHECK_RET(parse_when(ctx, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003231 break;
3232
Radek Krejcid6b76452019-09-03 17:03:03 +02003233 case LY_STMT_REFINE:
Radek Krejci33090f92020-12-17 20:12:46 +01003234 LY_CHECK_RET(parse_refine(ctx, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003235 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003236 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003237 LY_CHECK_RET(parse_augment(ctx, (struct lysp_node *)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003238 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003239 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003240 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_USES, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003241 break;
3242 default:
David Sedlákb3077192019-06-19 10:55:37 +02003243 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003244 return LY_EVALID;
3245 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003246 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003247
Michal Vasko12ef5362022-09-16 15:13:58 +02003248cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003249 return ret;
3250}
3251
Michal Vaskoea5abea2018-09-18 13:10:54 +02003252/**
3253 * @brief Parse the case statement.
3254 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003255 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003256 * @param[in,out] siblings Siblings to add to.
3257 *
3258 * @return LY_ERR values.
3259 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003260LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003261parse_case(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003262{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003263 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003264 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003265 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003266 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003267 struct lysp_node_case *cas;
3268
David Sedlák60adc092019-08-06 15:57:02 +02003269 /* create new case structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003270 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cas, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003271 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003272 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003273
Michal Vasko7fbc8162018-09-17 10:35:16 +02003274 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003275 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003276 INSERT_WORD_GOTO(ctx, buf, cas->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003277
3278 /* parse substatements */
aPieceka24a2252021-05-07 10:52:31 +02003279 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003280 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003281 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003282 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 +02003283 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003284 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003285 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &cas->iffeatures, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003286 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003287 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003288 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 +02003289 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003290 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003291 LY_CHECK_RET(parse_status(ctx, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003292 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003293 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003294 LY_CHECK_RET(parse_when(ctx, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003295 break;
3296
Radek Krejcid6b76452019-09-03 17:03:03 +02003297 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003298 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "case");
Radek Krejci0f969882020-08-21 16:56:47 +02003299 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003300 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003301 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003302 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003303 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003304 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003305 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003306 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003307 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003308 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003309 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003310 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003311 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003312 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003313 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003314 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003315 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003316 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003317 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003318 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003319 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003320 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003321 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003322 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_CASE, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003323 break;
3324 default:
David Sedlákb3077192019-06-19 10:55:37 +02003325 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003326 return LY_EVALID;
3327 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003328 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003329
3330cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003331 return ret;
3332}
3333
Michal Vaskoea5abea2018-09-18 13:10:54 +02003334/**
3335 * @brief Parse the choice statement.
3336 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003337 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003338 * @param[in,out] siblings Siblings to add to.
3339 *
3340 * @return LY_ERR values.
3341 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003342LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003343parse_choice(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003344{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003345 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003346 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003347 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003348 enum ly_stmt kw;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003349 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003350
David Sedlák60adc092019-08-06 15:57:02 +02003351 /* create new choice structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003352 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, choice, next, LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003353 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003354 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003355
Michal Vasko7fbc8162018-09-17 10:35:16 +02003356 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003357 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003358 INSERT_WORD_GOTO(ctx, buf, choice->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003359
3360 /* parse substatements */
aPieceka24a2252021-05-07 10:52:31 +02003361 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003362 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003363 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003364 LY_CHECK_RET(parse_config(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003365 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003366 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003367 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 +02003368 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003369 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003370 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &choice->iffeatures, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003371 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003372 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01003373 LY_CHECK_RET(parse_mandatory(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003374 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003375 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003376 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 +02003377 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003378 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003379 LY_CHECK_RET(parse_status(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003380 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003381 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003382 LY_CHECK_RET(parse_when(ctx, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003383 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003384 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01003385 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 +02003386 &choice->exts));
Michal Vasko8a67eff2021-12-07 14:04:47 +01003387 choice->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003388 break;
3389
Radek Krejcid6b76452019-09-03 17:03:03 +02003390 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003391 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "choice");
Radek Krejci0f969882020-08-21 16:56:47 +02003392 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003393 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003394 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003395 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003396 case LY_STMT_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003397 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003398 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003399 case LY_STMT_CHOICE:
Radek Krejci335332a2019-09-05 13:03:35 +02003400 PARSER_CHECK_STMTVER2_RET(ctx, "choice", "choice");
Radek Krejci33090f92020-12-17 20:12:46 +01003401 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003402 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003403 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003404 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003405 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003406 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003407 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003408 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003409 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003410 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003411 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003412 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003413 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003414 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003415 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003416 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_CHOICE, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003417 break;
3418 default:
David Sedlákb3077192019-06-19 10:55:37 +02003419 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003420 return LY_EVALID;
3421 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003422 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003423
3424cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003425 return ret;
3426}
3427
Michal Vaskoea5abea2018-09-18 13:10:54 +02003428/**
3429 * @brief Parse the container statement.
3430 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003431 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003432 * @param[in,out] siblings Siblings to add to.
3433 *
3434 * @return LY_ERR values.
3435 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003436LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003437parse_container(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003438{
3439 LY_ERR ret = 0;
3440 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003441 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003442 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003443 struct lysp_node_container *cont;
3444
David Sedlák60adc092019-08-06 15:57:02 +02003445 /* create new container structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003446 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cont, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003447 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003448 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003449
Michal Vasko7fbc8162018-09-17 10:35:16 +02003450 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003451 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003452 INSERT_WORD_GOTO(ctx, buf, cont->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003453
3454 /* parse substatements */
aPieceka24a2252021-05-07 10:52:31 +02003455 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003456 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003457 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003458 LY_CHECK_RET(parse_config(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003459 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003460 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003461 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 +02003462 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003463 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003464 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &cont->iffeatures, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003465 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003466 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003467 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 +02003468 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003469 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003470 LY_CHECK_RET(parse_status(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003471 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003472 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003473 LY_CHECK_RET(parse_when(ctx, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003474 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003475 case LY_STMT_PRESENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003476 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 +02003477 break;
3478
Radek Krejcid6b76452019-09-03 17:03:03 +02003479 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003480 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "container");
Radek Krejci0f969882020-08-21 16:56:47 +02003481 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003482 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003483 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003484 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003485 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003486 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003487 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003488 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003489 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003490 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003491 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003492 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003493 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003494 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003495 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003496 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003497 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003498 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003499 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003500 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003501 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003502 break;
3503
Radek Krejcid6b76452019-09-03 17:03:03 +02003504 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003505 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)cont, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003506 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003507 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003508 LY_CHECK_RET(parse_restrs(ctx, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003509 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003510 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003511 PARSER_CHECK_STMTVER2_RET(ctx, "action", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003512 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003513 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003514 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003515 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003516 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003517 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003518 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003519 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003520 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003521 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003522 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_CONTAINER, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003523 break;
3524 default:
David Sedlákb3077192019-06-19 10:55:37 +02003525 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003526 return LY_EVALID;
3527 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003528 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003529
Michal Vasko12ef5362022-09-16 15:13:58 +02003530cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003531 return ret;
3532}
3533
Michal Vaskoea5abea2018-09-18 13:10:54 +02003534/**
3535 * @brief Parse the list statement.
3536 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003537 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003538 * @param[in,out] siblings Siblings to add to.
3539 *
3540 * @return LY_ERR values.
3541 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003542LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003543parse_list(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003544{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003545 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003546 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003547 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003548 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003549 struct lysp_node_list *list;
3550
David Sedlák60adc092019-08-06 15:57:02 +02003551 /* create new list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003552 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, list, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003553 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003554 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003555
Michal Vasko7fbc8162018-09-17 10:35:16 +02003556 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003557 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003558 INSERT_WORD_GOTO(ctx, buf, list->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003559
3560 /* parse substatements */
aPieceka24a2252021-05-07 10:52:31 +02003561 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003562 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003563 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003564 LY_CHECK_RET(parse_config(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003565 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003566 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003567 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 +02003568 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003569 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003570 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &list->iffeatures, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003571 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003572 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003573 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 +02003574 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003575 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003576 LY_CHECK_RET(parse_status(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003577 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003578 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003579 LY_CHECK_RET(parse_when(ctx, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003580 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003581 case LY_STMT_KEY:
Radek Krejcifc596f92021-02-26 22:40:26 +01003582 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 +02003583 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003584 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003585 LY_CHECK_RET(parse_maxelements(ctx, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003586 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003587 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003588 LY_CHECK_RET(parse_minelements(ctx, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003589 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003590 case LY_STMT_ORDERED_BY:
Radek Krejci33090f92020-12-17 20:12:46 +01003591 LY_CHECK_RET(parse_orderedby(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003592 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003593 case LY_STMT_UNIQUE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003594 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_UNIQUE, &list->uniques, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003595 break;
3596
Radek Krejcid6b76452019-09-03 17:03:03 +02003597 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003598 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "list");
Radek Krejci0f969882020-08-21 16:56:47 +02003599 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003600 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003601 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003602 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003603 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003604 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003605 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003606 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003607 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003608 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003609 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003610 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003611 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003612 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003613 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003614 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003615 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003616 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003617 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003618 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003619 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003620 break;
3621
Radek Krejcid6b76452019-09-03 17:03:03 +02003622 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003623 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)list, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003624 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003625 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003626 LY_CHECK_RET(parse_restrs(ctx, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003627 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003628 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003629 PARSER_CHECK_STMTVER2_RET(ctx, "action", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003630 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003631 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003632 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003633 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003634 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003635 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003636 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003637 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003638 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003639 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003640 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_LIST, 0, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003641 break;
3642 default:
David Sedlákb3077192019-06-19 10:55:37 +02003643 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003644 return LY_EVALID;
3645 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003646 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003647
Michal Vasko12ef5362022-09-16 15:13:58 +02003648cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003649 return ret;
3650}
3651
Michal Vaskoea5abea2018-09-18 13:10:54 +02003652/**
3653 * @brief Parse the yin-element statement.
3654 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003655 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003656 * @param[in,out] flags Flags to write to.
3657 * @param[in,out] exts Extension instances to add to.
3658 *
3659 * @return LY_ERR values.
3660 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003661static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003662parse_yinelement(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003663{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003664 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003665 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003666 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003667 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003668
3669 if (*flags & LYS_YINELEM_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02003670 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003671 return LY_EVALID;
3672 }
3673
3674 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003675 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003676
Radek Krejcif13b87b2020-12-01 22:02:17 +01003677 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003678 *flags |= LYS_YINELEM_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003679 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003680 *flags |= LYS_YINELEM_FALSE;
3681 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003682 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003683 free(buf);
3684 return LY_EVALID;
3685 }
3686 free(buf);
3687
aPieceka24a2252021-05-07 10:52:31 +02003688 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003689 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003690 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003691 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_YIN_ELEMENT, 0, exts));
Michal Vaskod989ba02020-08-24 10:59:24 +02003692 LY_CHECK_RET(ret);
3693 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003694 default:
David Sedlákb3077192019-06-19 10:55:37 +02003695 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003696 return LY_EVALID;
3697 }
3698 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003699 return ret;
3700}
3701
Michal Vaskoea5abea2018-09-18 13:10:54 +02003702/**
David Sedlák2444f8f2019-07-09 11:02:47 +02003703 * @brief Parse the argument statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003704 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003705 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003706 * @param[in,out] argument Value to write to.
3707 * @param[in,out] flags Flags to write to.
3708 * @param[in,out] exts Extension instances to add to.
3709 *
3710 * @return LY_ERR values.
3711 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003712static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003713parse_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 +02003714{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003715 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003716 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003717 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003718 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003719
3720 if (*argument) {
David Sedlákb3077192019-06-19 10:55:37 +02003721 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003722 return LY_EVALID;
3723 }
3724
3725 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003726 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003727 INSERT_WORD_GOTO(ctx, buf, *argument, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003728
aPieceka24a2252021-05-07 10:52:31 +02003729 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003730 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003731 case LY_STMT_YIN_ELEMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003732 LY_CHECK_RET(parse_yinelement(ctx, flags, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003733 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003734 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003735 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_ARGUMENT, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003736 break;
3737 default:
David Sedlákb3077192019-06-19 10:55:37 +02003738 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003739 return LY_EVALID;
3740 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003741 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003742
3743cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003744 return ret;
3745}
3746
Michal Vaskoea5abea2018-09-18 13:10:54 +02003747/**
3748 * @brief Parse the extension statement.
3749 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003750 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003751 * @param[in,out] extensions Extensions to add to.
3752 *
3753 * @return LY_ERR values.
3754 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003755static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003756parse_extension(struct lys_yang_parser_ctx *ctx, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003757{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003758 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003759 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003760 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003761 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003762 struct lysp_ext *ex;
3763
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003764 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003765
3766 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003767 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003768 INSERT_WORD_GOTO(ctx, buf, ex->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003769
aPieceka24a2252021-05-07 10:52:31 +02003770 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003771 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003772 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003773 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 +02003774 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003775 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003776 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 +02003777 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003778 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003779 LY_CHECK_RET(parse_status(ctx, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003780 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003781 case LY_STMT_ARGUMENT:
Radek Krejci9f87b0c2021-03-05 14:45:26 +01003782 LY_CHECK_RET(parse_argument(ctx, &ex->argname, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003783 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003784 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003785 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_EXTENSION, 0, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003786 break;
3787 default:
David Sedlákb3077192019-06-19 10:55:37 +02003788 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003789 return LY_EVALID;
3790 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003791 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003792
3793cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003794 return ret;
3795}
3796
Michal Vaskoea5abea2018-09-18 13:10:54 +02003797/**
3798 * @brief Parse the deviate statement.
3799 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003800 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003801 * @param[in,out] deviates Deviates to add to.
3802 *
3803 * @return LY_ERR values.
3804 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003805LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003806parse_deviate(struct lys_yang_parser_ctx *ctx, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003807{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003808 LY_ERR ret = LY_SUCCESS;
Michal Vasko12ef5362022-09-16 15:13:58 +02003809 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003810 size_t word_len, dev_mod;
Radek Krejcid6b76452019-09-03 17:03:03 +02003811 enum ly_stmt kw;
Michal Vasko12ef5362022-09-16 15:13:58 +02003812 struct lysp_deviate *d = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003813 struct lysp_deviate_add *d_add = NULL;
3814 struct lysp_deviate_rpl *d_rpl = NULL;
3815 struct lysp_deviate_del *d_del = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003816 const char **d_units = NULL;
3817 struct lysp_qname **d_uniques = NULL, **d_dflts = NULL;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02003818 struct lysp_restr **d_musts = NULL;
3819 uint16_t *d_flags = 0;
3820 uint32_t *d_min = 0, *d_max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003821
3822 /* get value */
Michal Vasko12ef5362022-09-16 15:13:58 +02003823 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003824
Radek Krejcif13b87b2020-12-01 22:02:17 +01003825 if ((word_len == ly_strlen_const("not-supported")) && !strncmp(word, "not-supported", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003826 dev_mod = LYS_DEV_NOT_SUPPORTED;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003827 } else if ((word_len == ly_strlen_const("add")) && !strncmp(word, "add", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003828 dev_mod = LYS_DEV_ADD;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003829 } else if ((word_len == ly_strlen_const("replace")) && !strncmp(word, "replace", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003830 dev_mod = LYS_DEV_REPLACE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003831 } else if ((word_len == ly_strlen_const("delete")) && !strncmp(word, "delete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003832 dev_mod = LYS_DEV_DELETE;
3833 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003834 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "deviate");
Michal Vasko12ef5362022-09-16 15:13:58 +02003835 ret = LY_EVALID;
3836 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003837 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003838
3839 /* create structure */
3840 switch (dev_mod) {
3841 case LYS_DEV_NOT_SUPPORTED:
3842 d = calloc(1, sizeof *d);
Michal Vasko12ef5362022-09-16 15:13:58 +02003843 LY_CHECK_ERR_GOTO(!d, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003844 break;
3845 case LYS_DEV_ADD:
3846 d_add = calloc(1, sizeof *d_add);
Michal Vasko12ef5362022-09-16 15:13:58 +02003847 LY_CHECK_ERR_GOTO(!d_add, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003848 d = (struct lysp_deviate *)d_add;
3849 d_units = &d_add->units;
3850 d_uniques = &d_add->uniques;
3851 d_dflts = &d_add->dflts;
3852 d_musts = &d_add->musts;
3853 d_flags = &d_add->flags;
3854 d_min = &d_add->min;
3855 d_max = &d_add->max;
3856 break;
3857 case LYS_DEV_REPLACE:
3858 d_rpl = calloc(1, sizeof *d_rpl);
Michal Vasko12ef5362022-09-16 15:13:58 +02003859 LY_CHECK_ERR_GOTO(!d_rpl, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003860 d = (struct lysp_deviate *)d_rpl;
3861 d_units = &d_rpl->units;
3862 d_flags = &d_rpl->flags;
3863 d_min = &d_rpl->min;
3864 d_max = &d_rpl->max;
3865 break;
3866 case LYS_DEV_DELETE:
3867 d_del = calloc(1, sizeof *d_del);
Michal Vasko12ef5362022-09-16 15:13:58 +02003868 LY_CHECK_ERR_GOTO(!d_del, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003869 d = (struct lysp_deviate *)d_del;
3870 d_units = &d_del->units;
3871 d_uniques = &d_del->uniques;
3872 d_dflts = &d_del->dflts;
3873 d_musts = &d_del->musts;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003874 break;
3875 default:
3876 assert(0);
Michal Vasko12ef5362022-09-16 15:13:58 +02003877 LOGINT(PARSER_CTX(ctx));
3878 ret = LY_EINT;
3879 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003880 }
3881 d->mod = dev_mod;
3882
Michal Vasko12ef5362022-09-16 15:13:58 +02003883 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto cleanup, goto cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003884 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003885 case LY_STMT_CONFIG:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003886 switch (dev_mod) {
3887 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003888 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003889 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003890 ret = LY_EVALID;
3891 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003892 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003893 LY_CHECK_GOTO(ret = parse_config(ctx, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003894 break;
3895 }
3896 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003897 case LY_STMT_DEFAULT:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003898 switch (dev_mod) {
3899 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003900 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003901 ret = LY_EVALID;
3902 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003903 case LYS_DEV_REPLACE:
Michal Vasko12ef5362022-09-16 15:13:58 +02003904 LY_CHECK_GOTO(ret = parse_text_field(ctx, LY_STMT_DEFAULT, 0, &d_rpl->dflt.str, Y_STR_ARG, &d->exts), cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01003905 d_rpl->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003906 break;
3907 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003908 LY_CHECK_GOTO(ret = parse_qnames(ctx, LY_STMT_DEFAULT, d_dflts, Y_STR_ARG, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003909 break;
3910 }
3911 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003912 case LY_STMT_MANDATORY:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003913 switch (dev_mod) {
3914 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003915 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003916 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003917 ret = LY_EVALID;
3918 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003919 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003920 LY_CHECK_GOTO(ret = parse_mandatory(ctx, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003921 break;
3922 }
3923 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003924 case LY_STMT_MAX_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003925 switch (dev_mod) {
3926 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003927 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003928 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003929 ret = LY_EVALID;
3930 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003931 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003932 LY_CHECK_GOTO(ret = parse_maxelements(ctx, d_max, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003933 break;
3934 }
3935 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003936 case LY_STMT_MIN_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003937 switch (dev_mod) {
3938 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003939 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003940 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003941 ret = LY_EVALID;
3942 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003943 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003944 LY_CHECK_GOTO(ret = parse_minelements(ctx, d_min, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003945 break;
3946 }
3947 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003948 case LY_STMT_MUST:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003949 switch (dev_mod) {
3950 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003951 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003952 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003953 ret = LY_EVALID;
3954 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003955 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003956 LY_CHECK_GOTO(ret = parse_restrs(ctx, kw, d_musts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003957 break;
3958 }
3959 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003960 case LY_STMT_TYPE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003961 switch (dev_mod) {
3962 case LYS_DEV_NOT_SUPPORTED:
3963 case LYS_DEV_ADD:
3964 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003965 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003966 ret = LY_EVALID;
3967 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003968 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003969 if (d_rpl->type) {
David Sedlákb3077192019-06-19 10:55:37 +02003970 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003971 ret = LY_EVALID;
3972 goto cleanup;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003973 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003974 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Michal Vasko12ef5362022-09-16 15:13:58 +02003975 LY_CHECK_ERR_GOTO(!d_rpl->type, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
3976 LY_CHECK_GOTO(ret = parse_type(ctx, d_rpl->type), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003977 break;
3978 }
3979 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003980 case LY_STMT_UNIQUE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003981 switch (dev_mod) {
3982 case LYS_DEV_NOT_SUPPORTED:
3983 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003984 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003985 ret = LY_EVALID;
3986 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003987 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003988 LY_CHECK_GOTO(ret = parse_qnames(ctx, LY_STMT_UNIQUE, d_uniques, Y_STR_ARG, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003989 break;
3990 }
3991 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003992 case LY_STMT_UNITS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003993 switch (dev_mod) {
3994 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003995 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003996 ret = LY_EVALID;
3997 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003998 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003999 LY_CHECK_GOTO(ret = parse_text_field(ctx, LY_STMT_UNITS, 0, d_units, Y_STR_ARG, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004000 break;
4001 }
4002 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004003 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko12ef5362022-09-16 15:13:58 +02004004 LY_CHECK_GOTO(ret = parse_ext(ctx, word, word_len, LY_STMT_DEVIATE, 0, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004005 break;
4006 default:
David Sedlákb3077192019-06-19 10:55:37 +02004007 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviate");
Michal Vasko12ef5362022-09-16 15:13:58 +02004008 ret = LY_EVALID;
4009 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004010 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004011 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004012
4013cleanup:
4014 free(buf);
4015 if (ret) {
4016 lysp_deviate_free(PARSER_CTX(ctx), d);
4017 free(d);
4018 } else {
4019 /* insert into siblings */
4020 LY_LIST_INSERT(deviates, d, next);
4021 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004022 return ret;
4023}
4024
Michal Vaskoea5abea2018-09-18 13:10:54 +02004025/**
4026 * @brief Parse the deviation statement.
4027 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004028 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004029 * @param[in,out] deviations Deviations to add to.
4030 *
4031 * @return LY_ERR values.
4032 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004033LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004034parse_deviation(struct lys_yang_parser_ctx *ctx, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004035{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004036 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004037 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004038 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004039 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004040 struct lysp_deviation *dev;
4041
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004042 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004043
4044 /* get value */
Michal Vasko12ef5362022-09-16 15:13:58 +02004045 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +01004046 CHECK_NONEMPTY(ctx, word_len, "deviation");
Michal Vasko12ef5362022-09-16 15:13:58 +02004047 INSERT_WORD_GOTO(ctx, buf, dev->nodeid, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004048
Michal Vasko12ef5362022-09-16 15:13:58 +02004049 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, goto cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004050 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004051 case LY_STMT_DESCRIPTION:
Michal Vasko12ef5362022-09-16 15:13:58 +02004052 LY_CHECK_GOTO(ret = parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &dev->dsc, Y_STR_ARG, &dev->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004053 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004054 case LY_STMT_DEVIATE:
Michal Vasko12ef5362022-09-16 15:13:58 +02004055 LY_CHECK_GOTO(ret = parse_deviate(ctx, &dev->deviates), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004056 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004057 case LY_STMT_REFERENCE:
Michal Vasko12ef5362022-09-16 15:13:58 +02004058 LY_CHECK_GOTO(ret = parse_text_field(ctx, LY_STMT_REFERENCE, 0, &dev->ref, Y_STR_ARG, &dev->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004059 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004060 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko12ef5362022-09-16 15:13:58 +02004061 LY_CHECK_GOTO(ret = parse_ext(ctx, word, word_len, LY_STMT_DEVIATION, 0, &dev->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004062 break;
4063 default:
David Sedlákb3077192019-06-19 10:55:37 +02004064 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviation");
Michal Vasko12ef5362022-09-16 15:13:58 +02004065 ret = LY_EVALID;
4066 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004067 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004068 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004069 LY_CHECK_GOTO(ret, cleanup);
4070
Radek Krejci6d6556c2018-11-08 09:37:45 +01004071checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004072 /* mandatory substatements */
4073 if (!dev->deviates) {
David Sedlákb3077192019-06-19 10:55:37 +02004074 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko12ef5362022-09-16 15:13:58 +02004075 ret = LY_EVALID;
4076 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004077 }
4078
Michal Vasko12ef5362022-09-16 15:13:58 +02004079cleanup:
4080 if (ret) {
4081 lysp_deviation_free(PARSER_CTX(ctx), dev);
4082 LY_ARRAY_DECREMENT_FREE(*deviations);
4083 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004084 return ret;
4085}
4086
Michal Vaskoea5abea2018-09-18 13:10:54 +02004087/**
4088 * @brief Parse the feature statement.
4089 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004090 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004091 * @param[in,out] features Features to add to.
4092 *
4093 * @return LY_ERR values.
4094 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004095LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004096parse_feature(struct lys_yang_parser_ctx *ctx, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004097{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004098 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004099 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004100 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004101 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004102 struct lysp_feature *feat;
4103
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004104 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004105
4106 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01004107 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004108 INSERT_WORD_GOTO(ctx, buf, feat->name, word, word_len, ret, cleanup);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004109
aPieceka24a2252021-05-07 10:52:31 +02004110 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004111 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004112 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004113 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 +02004114 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004115 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004116 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &feat->iffeatures, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004117 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004118 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004119 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 +02004120 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004121 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01004122 LY_CHECK_RET(parse_status(ctx, &feat->flags, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004123 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004124 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01004125 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_FEATURE, 0, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004126 break;
4127 default:
David Sedlákb3077192019-06-19 10:55:37 +02004128 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004129 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004130 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004131 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004132
4133cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004134 return ret;
4135}
4136
Michal Vaskoea5abea2018-09-18 13:10:54 +02004137/**
4138 * @brief Parse the identity statement.
4139 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004140 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004141 * @param[in,out] identities Identities to add to.
4142 *
4143 * @return LY_ERR values.
4144 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004145LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004146parse_identity(struct lys_yang_parser_ctx *ctx, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004147{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004148 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004149 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004150 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004151 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004152 struct lysp_ident *ident;
4153
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004154 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004155
4156 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01004157 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004158 INSERT_WORD_GOTO(ctx, buf, ident->name, word, word_len, ret, cleanup);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004159
aPieceka24a2252021-05-07 10:52:31 +02004160 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004161 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004162 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004163 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 +02004164 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004165 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02004166 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "identity");
Radek Krejcifc596f92021-02-26 22:40:26 +01004167 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &ident->iffeatures, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004168 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004169 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004170 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 +02004171 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004172 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01004173 LY_CHECK_RET(parse_status(ctx, &ident->flags, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004174 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004175 case LY_STMT_BASE:
Michal Vasko8a67eff2021-12-07 14:04:47 +01004176 if (ident->bases && (PARSER_CUR_PMOD(ctx)->version < LYS_VERSION_1_1)) {
David Sedlákb3077192019-06-19 10:55:37 +02004177 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 +01004178 return LY_EVALID;
4179 }
Radek Krejcifc596f92021-02-26 22:40:26 +01004180 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 +02004181 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004182 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01004183 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_IDENTITY, 0, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004184 break;
4185 default:
David Sedlákb3077192019-06-19 10:55:37 +02004186 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004187 return LY_EVALID;
4188 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004189 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004190
4191cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004192 return ret;
4193}
4194
Michal Vaskoea5abea2018-09-18 13:10:54 +02004195/**
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004196 * @brief Parse module substatements.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004197 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004198 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004199 * @param[in,out] mod Module to write to.
4200 *
4201 * @return LY_ERR values.
4202 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004203LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004204parse_module(struct lys_yang_parser_ctx *ctx, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004205{
4206 LY_ERR ret = 0;
4207 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004208 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004209 enum ly_stmt kw, prev_kw = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004210 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Michal Vasko8dc31992021-02-22 10:30:47 +01004211 const struct lysp_submodule *dup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004212
Michal Vaskoc3781c32020-10-06 14:04:08 +02004213 mod->is_submod = 0;
4214
4215 /* module name */
Radek Krejci33090f92020-12-17 20:12:46 +01004216 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004217 INSERT_WORD_GOTO(ctx, buf, mod->mod->name, word, word_len, ret, cleanup);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004218
aPieceka24a2252021-05-07 10:52:31 +02004219 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004220
Radek Krejcie3846472018-10-15 15:24:51 +02004221#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004222 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 +02004223
Michal Vasko7fbc8162018-09-17 10:35:16 +02004224 switch (kw) {
4225 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004226 case LY_STMT_NAMESPACE:
4227 case LY_STMT_PREFIX:
Radek Krejcie3846472018-10-15 15:24:51 +02004228 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4229 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004230 case LY_STMT_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004231 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004232 break;
4233 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004234 case LY_STMT_INCLUDE:
4235 case LY_STMT_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004236 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004237 break;
4238 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004239 case LY_STMT_ORGANIZATION:
4240 case LY_STMT_CONTACT:
4241 case LY_STMT_DESCRIPTION:
4242 case LY_STMT_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004243 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004244 break;
4245
4246 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004247 case LY_STMT_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004248 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004249 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004250 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004251 case LY_STMT_ANYDATA:
4252 case LY_STMT_ANYXML:
4253 case LY_STMT_AUGMENT:
4254 case LY_STMT_CHOICE:
4255 case LY_STMT_CONTAINER:
4256 case LY_STMT_DEVIATION:
4257 case LY_STMT_EXTENSION:
4258 case LY_STMT_FEATURE:
4259 case LY_STMT_GROUPING:
4260 case LY_STMT_IDENTITY:
4261 case LY_STMT_LEAF:
4262 case LY_STMT_LEAF_LIST:
4263 case LY_STMT_LIST:
4264 case LY_STMT_NOTIFICATION:
4265 case LY_STMT_RPC:
4266 case LY_STMT_TYPEDEF:
4267 case LY_STMT_USES:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004268 mod_stmt = Y_MOD_BODY;
4269 break;
Michal Vasko59a24f62021-12-14 11:18:32 +01004270 case LY_STMT_EXTENSION_INSTANCE:
4271 /* no place in the statement order defined */
4272 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004273 default:
4274 /* error handled in the next switch */
4275 break;
4276 }
Radek Krejcie3846472018-10-15 15:24:51 +02004277#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004278
Radek Krejcie3846472018-10-15 15:24:51 +02004279 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004280 switch (kw) {
4281 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004282 case LY_STMT_YANG_VERSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004283 LY_CHECK_RET(parse_yangversion(ctx, &mod->version, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004284 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004285 case LY_STMT_NAMESPACE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004286 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 +02004287 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004288 case LY_STMT_PREFIX:
Radek Krejcifc596f92021-02-26 22:40:26 +01004289 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 +02004290 break;
4291
4292 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004293 case LY_STMT_INCLUDE:
Radek Krejci33090f92020-12-17 20:12:46 +01004294 LY_CHECK_RET(parse_include(ctx, mod->mod->name, &mod->includes));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004295 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004296 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004297 LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, &mod->imports));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004298 break;
4299
4300 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004301 case LY_STMT_ORGANIZATION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004302 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 +02004303 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004304 case LY_STMT_CONTACT:
Radek Krejcifc596f92021-02-26 22:40:26 +01004305 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 +02004306 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004307 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004308 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 +02004309 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004310 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004311 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 +02004312 break;
4313
4314 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004315 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004316 LY_CHECK_RET(parse_revision(ctx, &mod->revs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004317 break;
4318
4319 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004320 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004321 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "module");
Radek Krejci0f969882020-08-21 16:56:47 +02004322 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004323 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004324 LY_CHECK_RET(parse_any(ctx, kw, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004325 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004326 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004327 LY_CHECK_RET(parse_choice(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004328 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004329 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004330 LY_CHECK_RET(parse_container(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004331 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004332 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004333 LY_CHECK_RET(parse_leaf(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004334 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004335 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004336 LY_CHECK_RET(parse_leaflist(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004337 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004338 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004339 LY_CHECK_RET(parse_list(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004340 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004341 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004342 LY_CHECK_RET(parse_uses(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004343 break;
4344
Radek Krejcid6b76452019-09-03 17:03:03 +02004345 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004346 LY_CHECK_RET(parse_augment(ctx, NULL, &mod->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004347 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004348 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004349 LY_CHECK_RET(parse_deviation(ctx, &mod->deviations));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004350 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004351 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004352 LY_CHECK_RET(parse_extension(ctx, &mod->extensions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004353 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004354 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004355 LY_CHECK_RET(parse_feature(ctx, &mod->features));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004356 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004357 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004358 LY_CHECK_RET(parse_grouping(ctx, NULL, &mod->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004359 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004360 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004361 LY_CHECK_RET(parse_identity(ctx, &mod->identities));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004362 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004363 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004364 LY_CHECK_RET(parse_notif(ctx, NULL, &mod->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004365 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004366 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004367 LY_CHECK_RET(parse_action(ctx, NULL, &mod->rpcs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004368 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004369 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004370 LY_CHECK_RET(parse_typedef(ctx, NULL, &mod->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004371 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004372 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01004373 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MODULE, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004374 break;
4375
4376 default:
David Sedlákb3077192019-06-19 10:55:37 +02004377 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004378 return LY_EVALID;
4379 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004380 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004381 LY_CHECK_RET(ret);
Radek Krejcie86bf772018-12-14 11:39:53 +01004382
Radek Krejci6d6556c2018-11-08 09:37:45 +01004383checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004384 /* mandatory substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004385 if (!mod->mod->ns) {
David Sedlákb3077192019-06-19 10:55:37 +02004386 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004387 return LY_EVALID;
4388 } else if (!mod->mod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004389 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004390 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004391 }
4392
Radek Krejcie9e987e2018-10-31 12:50:27 +01004393 /* submodules share the namespace with the module names, so there must not be
4394 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01004395 dup = ly_ctx_get_submodule_latest(PARSER_CTX(ctx), mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004396 if (dup) {
Radek Krejci854e1552020-12-21 15:05:23 +01004397 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004398 return LY_EVALID;
4399 }
4400
Michal Vasko12ef5362022-09-16 15:13:58 +02004401cleanup:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004402 return ret;
4403}
4404
4405/**
4406 * @brief Parse submodule substatements.
4407 *
4408 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004409 * @param[out] submod Parsed submodule structure.
4410 *
4411 * @return LY_ERR values.
4412 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004413LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004414parse_submodule(struct lys_yang_parser_ctx *ctx, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004415{
4416 LY_ERR ret = 0;
4417 char *buf, *word;
4418 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004419 enum ly_stmt kw, prev_kw = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004420 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Michal Vasko8dc31992021-02-22 10:30:47 +01004421 const struct lysp_submodule *dup;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004422
Michal Vaskoc3781c32020-10-06 14:04:08 +02004423 submod->is_submod = 1;
4424
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004425 /* submodule name */
Radek Krejci33090f92020-12-17 20:12:46 +01004426 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004427 INSERT_WORD_GOTO(ctx, buf, submod->name, word, word_len, ret, cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004428
aPieceka24a2252021-05-07 10:52:31 +02004429 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004430
4431#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004432 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 +01004433
4434 switch (kw) {
4435 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004436 case LY_STMT_BELONGS_TO:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004437 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4438 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004439 case LY_STMT_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004440 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4441 break;
4442 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004443 case LY_STMT_INCLUDE:
4444 case LY_STMT_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004445 CHECK_ORDER(Y_MOD_LINKAGE);
4446 break;
4447 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004448 case LY_STMT_ORGANIZATION:
4449 case LY_STMT_CONTACT:
4450 case LY_STMT_DESCRIPTION:
4451 case LY_STMT_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004452 CHECK_ORDER(Y_MOD_META);
4453 break;
4454
4455 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004456 case LY_STMT_REVISION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004457 CHECK_ORDER(Y_MOD_REVISION);
4458 break;
4459 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004460 case LY_STMT_ANYDATA:
4461 case LY_STMT_ANYXML:
4462 case LY_STMT_AUGMENT:
4463 case LY_STMT_CHOICE:
4464 case LY_STMT_CONTAINER:
4465 case LY_STMT_DEVIATION:
4466 case LY_STMT_EXTENSION:
4467 case LY_STMT_FEATURE:
4468 case LY_STMT_GROUPING:
4469 case LY_STMT_IDENTITY:
4470 case LY_STMT_LEAF:
4471 case LY_STMT_LEAF_LIST:
4472 case LY_STMT_LIST:
4473 case LY_STMT_NOTIFICATION:
4474 case LY_STMT_RPC:
4475 case LY_STMT_TYPEDEF:
4476 case LY_STMT_USES:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004477 mod_stmt = Y_MOD_BODY;
4478 break;
Michal Vasko59a24f62021-12-14 11:18:32 +01004479 case LY_STMT_EXTENSION_INSTANCE:
4480 /* no place in the statement order defined */
4481 break;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004482 default:
4483 /* error handled in the next switch */
4484 break;
4485 }
4486#undef CHECK_ORDER
4487
4488 prev_kw = kw;
4489 switch (kw) {
4490 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004491 case LY_STMT_YANG_VERSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004492 LY_CHECK_RET(parse_yangversion(ctx, &submod->version, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004493 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004494 case LY_STMT_BELONGS_TO:
Radek Krejci33090f92020-12-17 20:12:46 +01004495 LY_CHECK_RET(parse_belongsto(ctx, &submod->prefix, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004496 break;
4497
4498 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004499 case LY_STMT_INCLUDE:
Radek Krejcieeee95c2021-01-19 10:57:22 +01004500 if (submod->version == LYS_VERSION_1_1) {
4501 LOGWRN(PARSER_CTX(ctx), "YANG version 1.1 expects all includes in main module, includes in submodules (%s) are not necessary.",
4502 submod->name);
4503 }
Radek Krejci33090f92020-12-17 20:12:46 +01004504 LY_CHECK_RET(parse_include(ctx, submod->name, &submod->includes));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004505 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004506 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004507 LY_CHECK_RET(parse_import(ctx, submod->prefix, &submod->imports));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004508 break;
4509
4510 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004511 case LY_STMT_ORGANIZATION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004512 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 +01004513 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004514 case LY_STMT_CONTACT:
Radek Krejcifc596f92021-02-26 22:40:26 +01004515 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 +01004516 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004517 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004518 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 +01004519 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004520 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004521 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 +01004522 break;
4523
4524 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004525 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004526 LY_CHECK_RET(parse_revision(ctx, &submod->revs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004527 break;
4528
4529 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004530 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004531 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "submodule");
Radek Krejci0f969882020-08-21 16:56:47 +02004532 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004533 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004534 LY_CHECK_RET(parse_any(ctx, kw, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004535 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004536 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004537 LY_CHECK_RET(parse_choice(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004538 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004539 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004540 LY_CHECK_RET(parse_container(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004541 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004542 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004543 LY_CHECK_RET(parse_leaf(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004544 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004545 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004546 LY_CHECK_RET(parse_leaflist(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004547 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004548 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004549 LY_CHECK_RET(parse_list(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004550 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004551 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004552 LY_CHECK_RET(parse_uses(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004553 break;
4554
Radek Krejcid6b76452019-09-03 17:03:03 +02004555 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004556 LY_CHECK_RET(parse_augment(ctx, NULL, &submod->augments));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004557 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004558 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004559 LY_CHECK_RET(parse_deviation(ctx, &submod->deviations));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004560 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004561 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004562 LY_CHECK_RET(parse_extension(ctx, &submod->extensions));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004563 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004564 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004565 LY_CHECK_RET(parse_feature(ctx, &submod->features));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004566 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004567 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004568 LY_CHECK_RET(parse_grouping(ctx, NULL, &submod->groupings));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004569 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004570 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004571 LY_CHECK_RET(parse_identity(ctx, &submod->identities));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004572 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004573 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004574 LY_CHECK_RET(parse_notif(ctx, NULL, &submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004575 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004576 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004577 LY_CHECK_RET(parse_action(ctx, NULL, &submod->rpcs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004578 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004579 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004580 LY_CHECK_RET(parse_typedef(ctx, NULL, &submod->typedefs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004581 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004582 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01004583 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_SUBMODULE, 0, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004584 break;
4585
4586 default:
David Sedlákb3077192019-06-19 10:55:37 +02004587 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004588 return LY_EVALID;
4589 }
4590 }
4591 LY_CHECK_RET(ret);
4592
4593checks:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004594 /* mandatory substatements */
Michal Vaskoc3781c32020-10-06 14:04:08 +02004595 if (!submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004596 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004597 return LY_EVALID;
4598 }
4599
4600 /* submodules share the namespace with the module names, so there must not be
4601 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01004602 dup = ly_ctx_get_submodule_latest(PARSER_CTX(ctx), submod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02004603 /* main modules may have different revisions */
4604 if (dup && strcmp(dup->mod->name, submod->mod->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01004605 LOGVAL_PARSER(ctx, LY_VCODE_NAME_COL, "submodules", dup->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +01004606 return LY_EVALID;
4607 }
4608
Michal Vasko12ef5362022-09-16 15:13:58 +02004609cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004610 return ret;
4611}
4612
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004613/**
4614 * @brief Skip any redundant characters, namely whitespaces and comments.
4615 *
4616 * @param[in] ctx Yang parser context.
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004617 * @return LY_SUCCESS on success.
4618 * @return LY_EVALID on invalid comment.
4619 */
4620static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004621skip_redundant_chars(struct lys_yang_parser_ctx *ctx)
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004622{
4623 /* read some trailing spaces, new lines, or comments */
Radek Krejci33090f92020-12-17 20:12:46 +01004624 while (ctx->in->current[0]) {
4625 if (!strncmp(ctx->in->current, "//", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004626 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004627 ly_in_skip(ctx->in, 2);
4628 LY_CHECK_RET(skip_comment(ctx, 1));
4629 } else if (!strncmp(ctx->in->current, "/*", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004630 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004631 ly_in_skip(ctx->in, 2);
4632 LY_CHECK_RET(skip_comment(ctx, 2));
4633 } else if (isspace(ctx->in->current[0])) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004634 /* whitespace */
Radek Krejcidd713ce2021-01-04 23:12:12 +01004635 if (ctx->in->current[0] == '\n') {
4636 LY_IN_NEW_LINE(ctx->in);
4637 }
Radek Krejci33090f92020-12-17 20:12:46 +01004638 ly_in_skip(ctx->in, 1);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004639 } else {
4640 break;
4641 }
4642 }
4643
4644 return LY_SUCCESS;
4645}
4646
Radek Krejcid4557c62018-09-17 11:42:09 +02004647LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01004648yang_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 +02004649 struct ly_in *in, struct lysp_submodule **submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004650{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004651 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004652 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004653 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004654 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004655 struct lysp_submodule *mod_p = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004656
aPiecek56be92a2021-07-01 07:18:10 +02004657 assert(context && ly_ctx && main_ctx && in && submod);
4658
David Sedlák1b623122019-08-05 15:27:49 +02004659 /* create context */
4660 *context = calloc(1, sizeof **context);
4661 LY_CHECK_ERR_RET(!(*context), LOGMEM(ly_ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004662 (*context)->format = LYS_IN_YANG;
Radek Krejci33090f92020-12-17 20:12:46 +01004663 (*context)->in = in;
aPiecek56be92a2021-07-01 07:18:10 +02004664 (*context)->main_ctx = main_ctx;
David Sedlák1b623122019-08-05 15:27:49 +02004665
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004666 mod_p = calloc(1, sizeof *mod_p);
4667 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ly_ctx); ret = LY_EMEM, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01004668 mod_p->mod = PARSER_CUR_PMOD(main_ctx)->mod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004669 mod_p->parsing = 1;
Michal Vasko8a67eff2021-12-07 14:04:47 +01004670
4671 /* use main context parsed mods adding the current one */
4672 (*context)->parsed_mods = main_ctx->parsed_mods;
4673 ly_set_add((*context)->parsed_mods, mod_p, 1, NULL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004674
Radek Krejciddace2c2021-01-08 11:30:56 +01004675 LOG_LOCINIT(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004676
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004677 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004678 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004679 LY_CHECK_GOTO(ret, cleanup);
4680
Michal Vasko7fbc8162018-09-17 10:35:16 +02004681 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004682 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004683 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004684
Radek Krejcid6b76452019-09-03 17:03:03 +02004685 if (kw == LY_STMT_MODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004686 LOGERR(ly_ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004687 ret = LY_EINVAL;
4688 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004689 } else if (kw != LY_STMT_SUBMODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004690 LOGVAL_PARSER(*context, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004691 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004692 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004693 }
4694
Michal Vasko7fbc8162018-09-17 10:35:16 +02004695 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004696 ret = parse_submodule(*context, mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004697 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004698
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004699 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004700 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004701 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004702 if (in->current[0]) {
4703 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_SUBMOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004704 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004705 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004706 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004707
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004708 mod_p->parsing = 0;
4709 *submod = mod_p;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004710
Radek Krejcibbe09a92018-11-08 09:36:54 +01004711cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01004712 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004713 if (ret) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004714 lysp_module_free((struct lysp_module *)mod_p);
Michal Vaskob36053d2020-03-26 15:49:30 +01004715 yang_parser_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004716 *context = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004717 }
4718
4719 return ret;
4720}
4721
4722LY_ERR
aPiecekc3e26142021-06-22 14:25:49 +02004723yang_parse_module(struct lys_yang_parser_ctx **context, struct ly_in *in, struct lys_module *mod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004724{
4725 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004726 char *word;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004727 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004728 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004729 struct lysp_module *mod_p = NULL;
4730
David Sedlák1b623122019-08-05 15:27:49 +02004731 /* create context */
4732 *context = calloc(1, sizeof **context);
4733 LY_CHECK_ERR_RET(!(*context), LOGMEM(mod->ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004734 (*context)->format = LYS_IN_YANG;
Michal Vasko8a67eff2021-12-07 14:04:47 +01004735 LY_CHECK_ERR_RET(ly_set_new(&(*context)->parsed_mods), free(*context); LOGMEM(mod->ctx), LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01004736 (*context)->in = in;
aPiecek8d4e75d2021-06-24 14:47:06 +02004737 (*context)->main_ctx = (struct lys_parser_ctx *)(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004738
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004739 mod_p = calloc(1, sizeof *mod_p);
4740 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(mod->ctx), cleanup);
4741 mod_p->mod = mod;
Michal Vasko8a67eff2021-12-07 14:04:47 +01004742 ly_set_add((*context)->parsed_mods, mod_p, 1, NULL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004743
Radek Krejciddace2c2021-01-08 11:30:56 +01004744 LOG_LOCINIT(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004745
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004746 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004747 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004748 LY_CHECK_GOTO(ret, cleanup);
4749
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004750 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004751 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004752 LY_CHECK_GOTO(ret, cleanup);
4753
Radek Krejcid6b76452019-09-03 17:03:03 +02004754 if (kw == LY_STMT_SUBMODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004755 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 +01004756 ret = LY_EINVAL;
4757 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004758 } else if (kw != LY_STMT_MODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004759 LOGVAL_PARSER((*context), LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004760 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004761 goto cleanup;
4762 }
4763
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004764 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004765 ret = parse_module(*context, mod_p);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004766 LY_CHECK_GOTO(ret, cleanup);
4767
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004768 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004769 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004770 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004771 if (in->current[0]) {
4772 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_MOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004773 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004774 goto cleanup;
4775 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004776
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004777 mod->parsed = mod_p;
4778
4779cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01004780 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004781 if (ret) {
4782 lysp_module_free(mod_p);
Michal Vaskob36053d2020-03-26 15:49:30 +01004783 yang_parser_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004784 *context = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004785 }
4786
Michal Vasko7fbc8162018-09-17 10:35:16 +02004787 return ret;
4788}