blob: 0880269e03381966add094449d77a1f5fc734236 [file] [log] [blame]
Michal Vasko7fbc8162018-09-17 10:35:16 +02001/**
2 * @file parser_yang.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief YANG parser
5 *
6 * Copyright (c) 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Michal Vasko63f3d842020-07-08 10:10:14 +020014#include "parser_internal.h"
Michal Vasko7fbc8162018-09-17 10:35:16 +020015
Radek Krejcie7b95092019-05-15 11:03:07 +020016#include <assert.h>
17#include <ctype.h>
18#include <errno.h>
19#include <stdint.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23
Radek Krejci535ea9f2020-05-29 16:01:05 +020024#include "common.h"
Michal Vasko7fbc8162018-09-17 10:35:16 +020025#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020026#include "dict.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020027#include "in_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020028#include "log.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020029#include "parser_schema.h"
Michal Vasko69730152020-10-09 16:30:07 +020030#include "path.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020031#include "set.h"
32#include "tree.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010033#include "tree_edit.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020034#include "tree_schema.h"
Radek Krejci70853c52018-10-15 14:46:16 +020035#include "tree_schema_internal.h"
Radek Krejci44ceedc2018-10-02 15:54:31 +020036
Radek Krejci77114102021-03-10 15:21:57 +010037struct lys_glob_unres;
38
Radek Krejciceaf2122019-01-02 15:03:26 +010039/**
40 * @brief Insert WORD into the libyang context's dictionary and store as TARGET.
41 * @param[in] CTX yang parser context to access libyang context.
42 * @param[in] BUF buffer in case the word is not a constant and can be inserted directly (zero-copy)
43 * @param[out] TARGET variable where to store the pointer to the inserted value.
44 * @param[in] WORD string to store.
45 * @param[in] LEN length of the string in WORD to store.
46 */
Radek Krejci011e4aa2020-09-04 15:22:31 +020047#define INSERT_WORD_RET(CTX, BUF, TARGET, WORD, LEN) \
Michal Vasko5d24f6c2020-10-13 13:49:06 +020048 if (BUF) {LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(CTX), WORD, &(TARGET)));}\
49 else {LY_CHECK_RET(lydict_insert(PARSER_CTX(CTX), LEN ? WORD : "", LEN, &(TARGET)));}
Radek Krejci44ceedc2018-10-02 15:54:31 +020050
Radek Krejciceaf2122019-01-02 15:03:26 +010051/**
Michal Vasko63f3d842020-07-08 10:10:14 +020052 * @brief Read from the IN structure COUNT items. Also updates the indent value in yang parser context
Radek Krejciceaf2122019-01-02 15:03:26 +010053 * @param[in] CTX yang parser context to update its indent value.
Radek Krejciceaf2122019-01-02 15:03:26 +010054 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
55 */
Radek Krejcid54412f2020-12-17 20:25:35 +010056#define MOVE_INPUT(CTX, COUNT) ly_in_skip((CTX)->in, COUNT);(CTX)->indent+=COUNT
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020057
Michal Vaskoea5abea2018-09-18 13:10:54 +020058/**
aPieceka24a2252021-05-07 10:52:31 +020059 * @brief Loop through all substatements.
Michal Vaskoea5abea2018-09-18 13:10:54 +020060 *
Radek Krejci44ceedc2018-10-02 15:54:31 +020061 * @param[in] CTX yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +020062 * @param[out] KW YANG keyword read.
63 * @param[out] WORD Pointer to the keyword itself.
64 * @param[out] WORD_LEN Length of the keyword.
65 * @param[out] ERR Variable for error storing.
aPieceka24a2252021-05-07 10:52:31 +020066 * @param[in] SUC_CMD Command is applied if a semicolon is found, so no
67 * substatements are available. It is expected to contain a return or goto command.
68 * @param[in] ERR_CMD Command is applied if an error occurs before loop through
69 * substatements. It is expected to contain return or goto command.
Michal Vaskoea5abea2018-09-18 13:10:54 +020070 *
71 * @return In case there are no substatements or a fatal error encountered.
72 */
aPieceka24a2252021-05-07 10:52:31 +020073#define YANG_READ_SUBSTMT_FOR(CTX, KW, WORD, WORD_LEN, ERR, SUC_CMD, ERR_CMD) \
74 if ((ERR = get_keyword(CTX, &KW, &WORD, &WORD_LEN))) { \
75 ERR_CMD; \
76 } \
Radek Krejcid6b76452019-09-03 17:03:03 +020077 if (KW == LY_STMT_SYNTAX_SEMICOLON) { \
aPieceka24a2252021-05-07 10:52:31 +020078 SUC_CMD; \
Michal Vasko7fbc8162018-09-17 10:35:16 +020079 } \
Radek Krejcid6b76452019-09-03 17:03:03 +020080 if (KW != LY_STMT_SYNTAX_LEFT_BRACE) { \
David Sedlákb3077192019-06-19 10:55:37 +020081 LOGVAL_PARSER(CTX, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\", expected \";\" or \"{\".", ly_stmt2str(KW)); \
aPieceka24a2252021-05-07 10:52:31 +020082 ERR = LY_EVALID; \
83 ERR_CMD; \
Michal Vasko7fbc8162018-09-17 10:35:16 +020084 } \
Radek Krejci33090f92020-12-17 20:12:46 +010085 for (ERR = get_keyword(CTX, &KW, &WORD, &WORD_LEN); \
Radek Krejcid6b76452019-09-03 17:03:03 +020086 !ERR && (KW != LY_STMT_SYNTAX_RIGHT_BRACE); \
Radek Krejci33090f92020-12-17 20:12:46 +010087 ERR = get_keyword(CTX, &KW, &WORD, &WORD_LEN))
Michal Vasko7fbc8162018-09-17 10:35:16 +020088
Radek Krejci33090f92020-12-17 20:12:46 +010089LY_ERR parse_container(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent,
Radek Krejci0f969882020-08-21 16:56:47 +020090 struct lysp_node **siblings);
Radek Krejci33090f92020-12-17 20:12:46 +010091LY_ERR parse_uses(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
92LY_ERR parse_choice(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
93LY_ERR parse_case(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
94LY_ERR parse_list(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
Radek Krejci2a9fc652021-01-22 17:44:34 +010095LY_ERR parse_grouping(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_grp **groupings);
Michal Vasko7fbc8162018-09-17 10:35:16 +020096
Michal Vaskoea5abea2018-09-18 13:10:54 +020097/**
98 * @brief Add another character to dynamic buffer, a low-level function.
99 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200100 * Enlarge if needed. Updates \p input as well as \p buf_used.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200101 *
Radek Krejci404251e2018-10-09 12:06:44 +0200102 * @param[in] ctx libyang context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200103 * @param[in,out] in Input structure.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200104 * @param[in] len Number of bytes to get from the input string and copy into the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200105 * @param[in,out] buf Buffer to use, can be moved by realloc().
106 * @param[in,out] buf_len Current size of the buffer.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200107 * @param[in,out] buf_used Currently used characters of the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200108 *
109 * @return LY_ERR values.
110 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200111LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200112buf_add_char(struct ly_ctx *ctx, struct ly_in *in, size_t len, char **buf, size_t *buf_len, size_t *buf_used)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200113{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100114#define BUF_STEP 16;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200115 if (*buf_len <= (*buf_used) + len) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100116 *buf_len += BUF_STEP;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200117 *buf = ly_realloc(*buf, *buf_len);
118 LY_CHECK_ERR_RET(!*buf, LOGMEM(ctx), LY_EMEM);
119 }
Radek Krejcic0917392019-04-10 13:04:04 +0200120 if (*buf_used) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200121 ly_in_read(in, &(*buf)[*buf_used], len);
Radek Krejcic0917392019-04-10 13:04:04 +0200122 } else {
Michal Vasko63f3d842020-07-08 10:10:14 +0200123 ly_in_read(in, *buf, len);
Radek Krejcic0917392019-04-10 13:04:04 +0200124 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200125
Radek Krejci44ceedc2018-10-02 15:54:31 +0200126 (*buf_used) += len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200127 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100128#undef BUF_STEP
Michal Vasko7fbc8162018-09-17 10:35:16 +0200129}
130
Michal Vaskoea5abea2018-09-18 13:10:54 +0200131/**
Radek Krejci44ceedc2018-10-02 15:54:31 +0200132 * @brief Store a single UTF8 character. It depends whether in a dynamically-allocated buffer or just as a pointer to the data.
133 *
134 * @param[in] ctx yang parser context for logging.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200135 * @param[in] arg Type of the input string to select method of checking character validity.
136 * @param[in,out] word_p Word pointer. If buffer (\p word_b) was not yet needed, it is just a pointer to the first
Michal Vaskoea5abea2018-09-18 13:10:54 +0200137 * stored character. If buffer was needed (\p word_b is non-NULL or \p need_buf is set), it is pointing to the buffer.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200138 * @param[in,out] word_len Current length of the word pointed to by \p word_p.
139 * @param[in,out] word_b Word buffer. Is kept NULL as long as it is not requested (word is a substring of the data).
Michal Vaskoea5abea2018-09-18 13:10:54 +0200140 * @param[in,out] buf_len Current length of \p word_b.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200141 * @param[in] need_buf Flag if the dynamically allocated buffer is required.
David Sedlák40bb13b2019-07-10 14:34:18 +0200142 * @param[in,out] prefix Storage for internally used flag in case of possible prefixed identifiers:
143 * 0 - colon not yet found (no prefix)
144 * 1 - \p c is the colon character
145 * 2 - prefix already processed, now processing the identifier
Michal Vaskoea5abea2018-09-18 13:10:54 +0200146 *
147 * @return LY_ERR values.
148 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200149LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100150buf_store_char(struct lys_yang_parser_ctx *ctx, enum yang_arg arg, char **word_p, size_t *word_len,
Radek Krejci857189e2020-09-01 13:26:36 +0200151 char **word_b, size_t *buf_len, ly_bool need_buf, uint8_t *prefix)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200152{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200153 uint32_t c;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200154 size_t len;
155
Radek Krejcif29b7c32019-04-09 16:17:49 +0200156 /* check valid combination of input paremeters - if need_buf specified, word_b must be provided */
157 assert(!need_buf || (need_buf && word_b));
158
Radek Krejci44ceedc2018-10-02 15:54:31 +0200159 /* get UTF8 code point (and number of bytes coding the character) */
Radek Krejci33090f92020-12-17 20:12:46 +0100160 LY_CHECK_ERR_RET(ly_getutf8(&ctx->in->current, &c, &len),
161 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[-len]), LY_EVALID);
162 ctx->in->current -= len;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200163 if (c == '\n') {
164 ctx->indent = 0;
Radek Krejcidd713ce2021-01-04 23:12:12 +0100165 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200166 } else {
167 /* note - even the multibyte character is count as 1 */
168 ++ctx->indent;
169 }
170
Radek Krejci44ceedc2018-10-02 15:54:31 +0200171 /* check character validity */
172 switch (arg) {
173 case Y_IDENTIF_ARG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100174 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !(*word_len), NULL));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200175 break;
176 case Y_PREF_IDENTIF_ARG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100177 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !(*word_len), prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200178 break;
179 case Y_STR_ARG:
180 case Y_MAYBE_STR_ARG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100181 LY_CHECK_RET(lysp_check_stringchar((struct lys_parser_ctx *)ctx, c));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200182 break;
183 }
184
Michal Vasko7fbc8162018-09-17 10:35:16 +0200185 if (word_b && *word_b) {
186 /* add another character into buffer */
Radek Krejci33090f92020-12-17 20:12:46 +0100187 if (buf_add_char(PARSER_CTX(ctx), ctx->in, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200188 return LY_EMEM;
189 }
190
191 /* in case of realloc */
192 *word_p = *word_b;
Radek Krejcif29b7c32019-04-09 16:17:49 +0200193 } else if (word_b && need_buf) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200194 /* first time we need a buffer, copy everything read up to now */
195 if (*word_len) {
196 *word_b = malloc(*word_len);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200197 LY_CHECK_ERR_RET(!*word_b, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200198 *buf_len = *word_len;
199 memcpy(*word_b, *word_p, *word_len);
200 }
201
202 /* add this new character into buffer */
Radek Krejci33090f92020-12-17 20:12:46 +0100203 if (buf_add_char(PARSER_CTX(ctx), ctx->in, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200204 return LY_EMEM;
205 }
206
207 /* in case of realloc */
208 *word_p = *word_b;
209 } else {
210 /* just remember the first character pointer */
211 if (!*word_p) {
Radek Krejci33090f92020-12-17 20:12:46 +0100212 *word_p = (char *)ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200213 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200214 /* ... and update the word's length */
215 (*word_len) += len;
Radek Krejci33090f92020-12-17 20:12:46 +0100216 ly_in_skip(ctx->in, len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200217 }
218
219 return LY_SUCCESS;
220}
221
Michal Vaskoea5abea2018-09-18 13:10:54 +0200222/**
223 * @brief Skip YANG comment in data.
224 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200225 * @param[in] ctx yang parser context for logging.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200226 * @param[in] comment Type of the comment to process:
227 * 1 for a one-line comment,
228 * 2 for a block comment.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200229 * @return LY_ERR values.
230 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200231LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100232skip_comment(struct lys_yang_parser_ctx *ctx, uint8_t comment)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200233{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100234 /* internal statuses: */
235#define COMMENT_NO 0 /* comment ended */
236#define COMMENT_LINE 1 /* in line comment */
237#define COMMENT_BLOCK 2 /* in block comment */
238#define COMMENT_BLOCK_END 3 /* in block comment with last read character '*' */
239
Radek Krejci33090f92020-12-17 20:12:46 +0100240 while (ctx->in->current[0] && comment) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200241 switch (comment) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100242 case COMMENT_LINE:
Radek Krejci33090f92020-12-17 20:12:46 +0100243 if (ctx->in->current[0] == '\n') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100244 comment = COMMENT_NO;
Radek Krejcid54412f2020-12-17 20:25:35 +0100245 LY_IN_NEW_LINE(ctx->in);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200246 }
247 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100248 case COMMENT_BLOCK:
Radek Krejci33090f92020-12-17 20:12:46 +0100249 if (ctx->in->current[0] == '*') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100250 comment = COMMENT_BLOCK_END;
Radek Krejci33090f92020-12-17 20:12:46 +0100251 } else if (ctx->in->current[0] == '\n') {
Radek Krejcid54412f2020-12-17 20:25:35 +0100252 LY_IN_NEW_LINE(ctx->in);
Radek Krejci15c80ca2018-10-09 11:01:31 +0200253 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200254 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100255 case COMMENT_BLOCK_END:
Radek Krejci33090f92020-12-17 20:12:46 +0100256 if (ctx->in->current[0] == '/') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100257 comment = COMMENT_NO;
Radek Krejci33090f92020-12-17 20:12:46 +0100258 } else if (ctx->in->current[0] != '*') {
259 if (ctx->in->current[0] == '\n') {
Radek Krejcid54412f2020-12-17 20:25:35 +0100260 LY_IN_NEW_LINE(ctx->in);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200261 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100262 comment = COMMENT_BLOCK;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200263 }
264 break;
265 default:
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200266 LOGINT_RET(PARSER_CTX(ctx));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200267 }
268
Radek Krejci33090f92020-12-17 20:12:46 +0100269 if (ctx->in->current[0] == '\n') {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200270 ctx->indent = 0;
271 } else {
272 ++ctx->indent;
273 }
Radek Krejci33090f92020-12-17 20:12:46 +0100274 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200275 }
276
Radek Krejci33090f92020-12-17 20:12:46 +0100277 if (!ctx->in->current[0] && (comment >= COMMENT_BLOCK)) {
David Sedlákb3077192019-06-19 10:55:37 +0200278 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Unexpected end-of-input, non-terminated comment.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200279 return LY_EVALID;
280 }
281
282 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100283
284#undef COMMENT_NO
285#undef COMMENT_LINE
286#undef COMMENT_BLOCK
287#undef COMMENT_BLOCK_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200288}
289
Michal Vaskoea5abea2018-09-18 13:10:54 +0200290/**
291 * @brief Read a quoted string from data.
292 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200293 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200294 * @param[in] arg Type of YANG keyword argument expected.
295 * @param[out] word_p Pointer to the read quoted string.
296 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read quoted string. If not needed,
297 * set to NULL. Otherwise equal to \p word_p.
298 * @param[out] word_len Length of the read quoted string.
299 * @param[out] buf_len Length of the dynamically-allocated buffer \p word_b.
300 * @param[in] indent Current indent (number of YANG spaces). Needed for correct multi-line string
301 * indenation in the final quoted string.
302 *
303 * @return LY_ERR values.
304 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200305static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100306read_qstring(struct lys_yang_parser_ctx *ctx, enum yang_arg arg, char **word_p, char **word_b,
Radek Krejci0f969882020-08-21 16:56:47 +0200307 size_t *word_len, size_t *buf_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200308{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100309 /* string parsing status: */
310#define STRING_ENDED 0 /* string ended */
311#define STRING_SINGLE_QUOTED 1 /* string with ' */
312#define STRING_DOUBLE_QUOTED 2 /* string with " */
313#define STRING_DOUBLE_QUOTED_ESCAPED 3 /* string with " with last character \ */
314#define STRING_PAUSED_NEXTSTRING 4 /* string finished, now skipping whitespaces looking for + */
315#define STRING_PAUSED_CONTINUE 5 /* string continues after +, skipping whitespaces */
316
Radek Krejci1deb5be2020-08-26 16:43:36 +0200317 uint8_t string;
318 uint64_t block_indent = 0, current_indent = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200319 ly_bool need_buf = 0;
320 uint8_t prefix = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200321 const char *c;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200322 uint64_t trailing_ws = 0; /* current number of stored trailing whitespace characters */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200323
Radek Krejci33090f92020-12-17 20:12:46 +0100324 if (ctx->in->current[0] == '\"') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100325 string = STRING_DOUBLE_QUOTED;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200326 current_indent = block_indent = ctx->indent + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200327 } else {
Radek Krejci33090f92020-12-17 20:12:46 +0100328 assert(ctx->in->current[0] == '\'');
Radek Krejcif13b87b2020-12-01 22:02:17 +0100329 string = STRING_SINGLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200330 }
Radek Krejci33090f92020-12-17 20:12:46 +0100331 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200332
Radek Krejci33090f92020-12-17 20:12:46 +0100333 while (ctx->in->current[0] && string) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200334 switch (string) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100335 case STRING_SINGLE_QUOTED:
Radek Krejci33090f92020-12-17 20:12:46 +0100336 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200337 case '\'':
338 /* string may be finished, but check for + */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100339 string = STRING_PAUSED_NEXTSTRING;
Radek Krejci33090f92020-12-17 20:12:46 +0100340 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200341 break;
342 default:
343 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100344 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200345 break;
346 }
347 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100348 case STRING_DOUBLE_QUOTED:
Radek Krejci33090f92020-12-17 20:12:46 +0100349 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200350 case '\"':
351 /* string may be finished, but check for + */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100352 string = STRING_PAUSED_NEXTSTRING;
Radek Krejci33090f92020-12-17 20:12:46 +0100353 MOVE_INPUT(ctx, 1);
Radek Krejciff13cd12019-10-25 15:34:24 +0200354 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200355 break;
356 case '\\':
357 /* special character following */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100358 string = STRING_DOUBLE_QUOTED_ESCAPED;
Radek Krejciff13cd12019-10-25 15:34:24 +0200359
360 /* the backslash sequence is substituted, so we will need a buffer to store the result */
361 need_buf = 1;
362
363 /* move forward to the escaped character */
Radek Krejci33090f92020-12-17 20:12:46 +0100364 ++ctx->in->current;
Radek Krejciff13cd12019-10-25 15:34:24 +0200365
366 /* note that the trailing whitespaces are supposed to be trimmed before substitution of
367 * backslash-escaped characters (RFC 7950, 6.1.3), so we have to zero the trailing whitespaces counter */
368 trailing_ws = 0;
369
370 /* since the backslash-escaped character is handled as first non-whitespace character, stop eating indentation */
371 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200372 break;
373 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200374 if (current_indent < block_indent) {
375 ++current_indent;
Radek Krejci33090f92020-12-17 20:12:46 +0100376 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200377 } else {
Michal Vasko90edde42019-11-25 15:25:07 +0100378 /* check and store whitespace character */
Radek Krejci33090f92020-12-17 20:12:46 +0100379 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko90edde42019-11-25 15:25:07 +0100380 trailing_ws++;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200381 }
382 break;
383 case '\t':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200384 if (current_indent < block_indent) {
385 assert(need_buf);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100386 current_indent += Y_TAB_SPACES;
387 ctx->indent += Y_TAB_SPACES;
Michal Vaskod989ba02020-08-24 10:59:24 +0200388 for ( ; current_indent > block_indent; --current_indent, --ctx->indent) {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200389 /* store leftover spaces from the tab */
Radek Krejci33090f92020-12-17 20:12:46 +0100390 c = ctx->in->current;
391 ctx->in->current = " ";
392 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
393 ctx->in->current = c;
Michal Vasko90edde42019-11-25 15:25:07 +0100394 trailing_ws++;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200395 }
Radek Krejci33090f92020-12-17 20:12:46 +0100396 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200397 } else {
Michal Vasko90edde42019-11-25 15:25:07 +0100398 /* check and store whitespace character */
Radek Krejci33090f92020-12-17 20:12:46 +0100399 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko90edde42019-11-25 15:25:07 +0100400 trailing_ws++;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200401 /* additional characters for indentation - only 1 was count in buf_store_char */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100402 ctx->indent += Y_TAB_SPACES - 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200403 }
404 break;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200405 case '\r':
406 if (ctx->in->current[1] != '\n') {
407 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
408 return LY_EVALID;
409 }
410 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200411 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200412 if (block_indent) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200413 /* we will be removing the indents so we need our own buffer */
414 need_buf = 1;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200415
416 /* remove trailing tabs and spaces */
Radek Krejciff13cd12019-10-25 15:34:24 +0200417 (*word_len) = *word_len - trailing_ws;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200418
Radek Krejciff13cd12019-10-25 15:34:24 +0200419 /* restart indentation */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200420 current_indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200421 }
422
423 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100424 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200425
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200426 /* reset context indentation counter for possible string after this one */
427 ctx->indent = 0;
Radek Krejciff13cd12019-10-25 15:34:24 +0200428 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200429 break;
430 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200431 /* first non-whitespace character, stop eating indentation */
432 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200433
434 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100435 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Radek Krejciff13cd12019-10-25 15:34:24 +0200436 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200437 break;
438 }
439 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100440 case STRING_DOUBLE_QUOTED_ESCAPED:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200441 /* string encoded characters */
Radek Krejci33090f92020-12-17 20:12:46 +0100442 c = ctx->in->current;
443 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200444 case 'n':
Radek Krejci33090f92020-12-17 20:12:46 +0100445 ctx->in->current = "\n";
Radek Krejcidd713ce2021-01-04 23:12:12 +0100446 /* fix false newline count in buf_store_char() */
447 ctx->in->line--;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200448 break;
449 case 't':
Radek Krejci33090f92020-12-17 20:12:46 +0100450 ctx->in->current = "\t";
Michal Vasko7fbc8162018-09-17 10:35:16 +0200451 break;
452 case '\"':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200453 case '\\':
Michal Vasko63f3d842020-07-08 10:10:14 +0200454 /* ok as is */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200455 break;
456 default:
Michal Vasko63f3d842020-07-08 10:10:14 +0200457 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Double-quoted string unknown special character '\\%c'.",
Radek Krejci33090f92020-12-17 20:12:46 +0100458 ctx->in->current[0]);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200459 return LY_EVALID;
460 }
461
462 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100463 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200464
Radek Krejcif13b87b2020-12-01 22:02:17 +0100465 string = STRING_DOUBLE_QUOTED;
Radek Krejci33090f92020-12-17 20:12:46 +0100466 ctx->in->current = c + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200467 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100468 case STRING_PAUSED_NEXTSTRING:
Radek Krejci33090f92020-12-17 20:12:46 +0100469 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200470 case '+':
471 /* string continues */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100472 string = STRING_PAUSED_CONTINUE;
Radek Krejciefd22f62018-09-27 11:47:58 +0200473 need_buf = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200474 break;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200475 case '\r':
476 if (ctx->in->current[1] != '\n') {
477 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
478 return LY_EVALID;
479 }
480 MOVE_INPUT(ctx, 1);
481 /* fallthrough */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200482 case '\n':
Radek Krejcidd713ce2021-01-04 23:12:12 +0100483 LY_IN_NEW_LINE(ctx->in);
Radek Krejcicb3e6472021-01-06 08:19:01 +0100484 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200485 case ' ':
486 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200487 /* just skip */
488 break;
489 default:
490 /* string is finished */
491 goto string_end;
492 }
Radek Krejci33090f92020-12-17 20:12:46 +0100493 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200494 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100495 case STRING_PAUSED_CONTINUE:
Radek Krejci33090f92020-12-17 20:12:46 +0100496 switch (ctx->in->current[0]) {
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200497 case '\r':
498 if (ctx->in->current[1] != '\n') {
499 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
500 return LY_EVALID;
501 }
502 MOVE_INPUT(ctx, 1);
503 /* fallthrough */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200504 case '\n':
Radek Krejcidd713ce2021-01-04 23:12:12 +0100505 LY_IN_NEW_LINE(ctx->in);
Radek Krejcicb3e6472021-01-06 08:19:01 +0100506 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200507 case ' ':
508 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200509 /* skip */
510 break;
511 case '\'':
Radek Krejcif13b87b2020-12-01 22:02:17 +0100512 string = STRING_SINGLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200513 break;
514 case '\"':
Radek Krejcif13b87b2020-12-01 22:02:17 +0100515 string = STRING_DOUBLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200516 break;
517 default:
518 /* it must be quoted again */
David Sedlákb3077192019-06-19 10:55:37 +0200519 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Both string parts divided by '+' must be quoted.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200520 return LY_EVALID;
521 }
Radek Krejci33090f92020-12-17 20:12:46 +0100522 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200523 break;
524 default:
525 return LY_EINT;
526 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200527 }
528
529string_end:
Michal Vasko69730152020-10-09 16:30:07 +0200530 if ((arg <= Y_PREF_IDENTIF_ARG) && !(*word_len)) {
Radek Krejci4e199f52019-05-28 09:09:28 +0200531 /* empty identifier */
David Sedlákb3077192019-06-19 10:55:37 +0200532 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Statement argument is required.");
Radek Krejci4e199f52019-05-28 09:09:28 +0200533 return LY_EVALID;
534 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200535 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100536
537#undef STRING_ENDED
538#undef STRING_SINGLE_QUOTED
539#undef STRING_DOUBLE_QUOTED
540#undef STRING_DOUBLE_QUOTED_ESCAPED
541#undef STRING_PAUSED_NEXTSTRING
542#undef STRING_PAUSED_CONTINUE
Michal Vasko7fbc8162018-09-17 10:35:16 +0200543}
544
Michal Vaskoea5abea2018-09-18 13:10:54 +0200545/**
546 * @brief Get another YANG string from the raw data.
547 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200548 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200549 * @param[in] arg Type of YANG keyword argument expected.
Michal Vaskob68ea142021-04-26 13:46:00 +0200550 * @param[out] flags optional output argument to get flag of the argument's quoting (LYS_*QUOTED - see
551 * [schema node flags](@ref snodeflags))
Michal Vasko2ca70f52018-09-27 11:04:51 +0200552 * @param[out] word_p Pointer to the read string. Can return NULL if \p arg is #Y_MAYBE_STR_ARG.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200553 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read string. If not needed,
554 * set to NULL. Otherwise equal to \p word_p.
555 * @param[out] word_len Length of the read string.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200556 * @return LY_ERR values.
Michal Vasko7fbc8162018-09-17 10:35:16 +0200557 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200558LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100559get_argument(struct lys_yang_parser_ctx *ctx, enum yang_arg arg, uint16_t *flags, char **word_p,
Radek Krejci0f969882020-08-21 16:56:47 +0200560 char **word_b, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200561{
Michal Vaskob68ea142021-04-26 13:46:00 +0200562 LY_ERR ret;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200563 size_t buf_len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200564 uint8_t prefix = 0;
Michal Vasko69730152020-10-09 16:30:07 +0200565
Michal Vasko7fbc8162018-09-17 10:35:16 +0200566 /* word buffer - dynamically allocated */
567 *word_b = NULL;
568
569 /* word pointer - just a pointer to data */
570 *word_p = NULL;
571
572 *word_len = 0;
Radek Krejci33090f92020-12-17 20:12:46 +0100573 while (ctx->in->current[0]) {
574 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200575 case '\'':
576 case '\"':
577 if (*word_len) {
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200578 /* invalid - quotes cannot be in unquoted string and only optsep, ; or { can follow it */
Radek Krejci33090f92020-12-17 20:12:46 +0100579 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200580 "unquoted string character, optsep, semicolon or opening brace");
Michal Vaskob68ea142021-04-26 13:46:00 +0200581 ret = LY_EVALID;
582 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200583 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200584 if (flags) {
Radek Krejci33090f92020-12-17 20:12:46 +0100585 (*flags) |= ctx->in->current[0] == '\'' ? LYS_SINGLEQUOTED : LYS_DOUBLEQUOTED;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200586 }
Michal Vaskob68ea142021-04-26 13:46:00 +0200587 LY_CHECK_GOTO(ret = read_qstring(ctx, arg, word_p, word_b, word_len, &buf_len), error);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200588 goto str_end;
589 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100590 if (ctx->in->current[1] == '/') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200591 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100592 MOVE_INPUT(ctx, 2);
Michal Vaskob68ea142021-04-26 13:46:00 +0200593 LY_CHECK_GOTO(ret = skip_comment(ctx, 1), error);
Radek Krejci33090f92020-12-17 20:12:46 +0100594 } else if (ctx->in->current[1] == '*') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200595 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100596 MOVE_INPUT(ctx, 2);
Michal Vaskob68ea142021-04-26 13:46:00 +0200597 LY_CHECK_GOTO(ret = skip_comment(ctx, 2), error);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200598 } else {
599 /* not a comment after all */
Michal Vaskob68ea142021-04-26 13:46:00 +0200600 LY_CHECK_GOTO(ret = buf_store_char(ctx, arg, word_p, word_len, word_b, &buf_len, 0, &prefix), error);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200601 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200602 break;
603 case ' ':
604 if (*word_len) {
605 /* word is finished */
606 goto str_end;
607 }
Radek Krejci33090f92020-12-17 20:12:46 +0100608 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200609 break;
610 case '\t':
611 if (*word_len) {
612 /* word is finished */
613 goto str_end;
614 }
615 /* tabs count for 8 spaces */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100616 ctx->indent += Y_TAB_SPACES;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200617
Radek Krejci33090f92020-12-17 20:12:46 +0100618 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200619 break;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200620 case '\r':
621 if (ctx->in->current[1] != '\n') {
622 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
Michal Vaskob68ea142021-04-26 13:46:00 +0200623 ret = LY_EVALID;
624 goto error;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200625 }
626 MOVE_INPUT(ctx, 1);
627 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200628 case '\n':
629 if (*word_len) {
630 /* word is finished */
631 goto str_end;
632 }
Radek Krejcidd713ce2021-01-04 23:12:12 +0100633 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid54412f2020-12-17 20:25:35 +0100634 MOVE_INPUT(ctx, 1);
635
Michal Vasko7fbc8162018-09-17 10:35:16 +0200636 /* reset indent */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200637 ctx->indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200638 break;
639 case ';':
640 case '{':
641 if (*word_len || (arg == Y_MAYBE_STR_ARG)) {
642 /* word is finished */
643 goto str_end;
644 }
645
Radek Krejci33090f92020-12-17 20:12:46 +0100646 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current, "an argument");
Michal Vaskob68ea142021-04-26 13:46:00 +0200647 ret = LY_EVALID;
648 goto error;
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200649 case '}':
650 /* invalid - braces cannot be in unquoted string (opening braces terminates the string and can follow it) */
Radek Krejci33090f92020-12-17 20:12:46 +0100651 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200652 "unquoted string character, optsep, semicolon or opening brace");
Michal Vaskob68ea142021-04-26 13:46:00 +0200653 ret = LY_EVALID;
654 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200655 default:
Michal Vaskob68ea142021-04-26 13:46:00 +0200656 LY_CHECK_GOTO(ret = buf_store_char(ctx, arg, word_p, word_len, word_b, &buf_len, 0, &prefix), error);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200657 break;
658 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200659 }
660
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200661 /* unexpected end of loop */
David Sedlákb3077192019-06-19 10:55:37 +0200662 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vaskob68ea142021-04-26 13:46:00 +0200663 ret = LY_EVALID;
664 goto error;
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200665
Michal Vasko7fbc8162018-09-17 10:35:16 +0200666str_end:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200667 /* terminating NULL byte for buf */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200668 if (*word_b) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200669 (*word_b) = ly_realloc(*word_b, (*word_len) + 1);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200670 LY_CHECK_ERR_RET(!(*word_b), LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200671 (*word_b)[*word_len] = '\0';
Michal Vasko7fbc8162018-09-17 10:35:16 +0200672 *word_p = *word_b;
673 }
674
675 return LY_SUCCESS;
Michal Vaskob68ea142021-04-26 13:46:00 +0200676
677error:
678 free(*word_b);
679 *word_b = NULL;
680 return ret;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200681}
682
Michal Vaskoea5abea2018-09-18 13:10:54 +0200683/**
684 * @brief Get another YANG keyword from the raw data.
685 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200686 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200687 * @param[out] kw YANG keyword read.
688 * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances.
689 * @param[out] word_len Length of the keyword in the data. Useful for extension instances.
690 *
691 * @return LY_ERR values.
692 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200693LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100694get_keyword(struct lys_yang_parser_ctx *ctx, enum ly_stmt *kw, char **word_p, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200695{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200696 uint8_t prefix;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200697 const char *word_start;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200698 size_t len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200699
700 if (word_p) {
701 *word_p = NULL;
702 *word_len = 0;
703 }
704
705 /* first skip "optsep", comments */
Radek Krejci33090f92020-12-17 20:12:46 +0100706 while (ctx->in->current[0]) {
707 switch (ctx->in->current[0]) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200708 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100709 if (ctx->in->current[1] == '/') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200710 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100711 MOVE_INPUT(ctx, 2);
712 LY_CHECK_RET(skip_comment(ctx, 1));
713 } else if (ctx->in->current[1] == '*') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200714 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100715 MOVE_INPUT(ctx, 2);
716 LY_CHECK_RET(skip_comment(ctx, 2));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200717 } else {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200718 /* error - not a comment after all, keyword cannot start with slash */
David Sedlákb3077192019-06-19 10:55:37 +0200719 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '/'.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200720 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200721 }
Radek Krejci13028282019-06-11 14:56:48 +0200722 continue;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200723 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200724 /* skip whitespaces (optsep) */
Radek Krejcidd713ce2021-01-04 23:12:12 +0100725 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200726 ctx->indent = 0;
727 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200728 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200729 /* skip whitespaces (optsep) */
730 ++ctx->indent;
731 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200732 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200733 /* skip whitespaces (optsep) */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100734 ctx->indent += Y_TAB_SPACES;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200735 break;
Michal Vasko50dcbc22021-03-25 12:21:20 +0100736 case '\r':
737 /* possible CRLF endline */
738 if (ctx->in->current[1] == '\n') {
739 break;
740 }
Radek Krejcid43298b2021-03-25 16:17:15 +0100741 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200742 default:
743 /* either a keyword start or an invalid character */
744 goto keyword_start;
745 }
746
Radek Krejci33090f92020-12-17 20:12:46 +0100747 ly_in_skip(ctx->in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200748 }
749
750keyword_start:
Radek Krejci33090f92020-12-17 20:12:46 +0100751 word_start = ctx->in->current;
Radek Krejcid54412f2020-12-17 20:25:35 +0100752 *kw = lysp_match_kw(ctx->in, &ctx->indent);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200753
Michal Vasko69730152020-10-09 16:30:07 +0200754 if ((*kw == LY_STMT_SYNTAX_SEMICOLON) || (*kw == LY_STMT_SYNTAX_LEFT_BRACE) || (*kw == LY_STMT_SYNTAX_RIGHT_BRACE)) {
Radek Krejci626df482018-10-11 15:06:31 +0200755 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200756 }
757
Radek Krejcid6b76452019-09-03 17:03:03 +0200758 if (*kw != LY_STMT_NONE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200759 /* make sure we have the whole keyword */
Radek Krejci33090f92020-12-17 20:12:46 +0100760 switch (ctx->in->current[0]) {
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200761 case '\r':
762 if (ctx->in->current[1] != '\n') {
763 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
764 return LY_EVALID;
765 }
766 MOVE_INPUT(ctx, 1);
767 /* fallthrough */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200768 case '\n':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200769 case '\t':
Radek Krejciabdd8062019-06-11 16:44:19 +0200770 case ' ':
771 /* mandatory "sep" is just checked, not eaten so nothing in the context is updated */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200772 break;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200773 case ':':
774 /* keyword is not actually a keyword, but prefix of an extension.
775 * To avoid repeated check of the prefix syntax, move to the point where the colon was read
776 * and we will be checking the keyword (extension instance) itself */
777 prefix = 1;
Radek Krejci33090f92020-12-17 20:12:46 +0100778 MOVE_INPUT(ctx, 1);
Radek Krejci156ccaf2018-10-15 15:49:17 +0200779 goto extension;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200780 case '{':
781 /* allowed only for input and output statements which can be without arguments */
Michal Vasko69730152020-10-09 16:30:07 +0200782 if ((*kw == LY_STMT_INPUT) || (*kw == LY_STMT_OUTPUT)) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200783 break;
784 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100785 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200786 default:
Radek Krejci33090f92020-12-17 20:12:46 +0100787 MOVE_INPUT(ctx, 1);
788 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start,
Michal Vasko69730152020-10-09 16:30:07 +0200789 "a keyword followed by a separator");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200790 return LY_EVALID;
791 }
792 } else {
793 /* still can be an extension */
794 prefix = 0;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200795
Radek Krejci156ccaf2018-10-15 15:49:17 +0200796extension:
Radek Krejcid54412f2020-12-17 20:25:35 +0100797 while (ctx->in->current[0] && (ctx->in->current[0] != ' ') && (ctx->in->current[0] != '\t') &&
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200798 (ctx->in->current[0] != '\n') && (ctx->in->current[0] != '\r') && (ctx->in->current[0] != '{') &&
799 (ctx->in->current[0] != ';')) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200800 uint32_t c = 0;
801
Radek Krejci33090f92020-12-17 20:12:46 +0100802 LY_CHECK_ERR_RET(ly_getutf8(&ctx->in->current, &c, &len),
803 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[-len]), LY_EVALID);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200804 ++ctx->indent;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200805 /* check character validity */
Michal Vasko63f3d842020-07-08 10:10:14 +0200806 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c,
Radek Krejci33090f92020-12-17 20:12:46 +0100807 ctx->in->current - len == word_start ? 1 : 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200808 }
Radek Krejci33090f92020-12-17 20:12:46 +0100809 if (!ctx->in->current[0]) {
David Sedlákb3077192019-06-19 10:55:37 +0200810 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200811 return LY_EVALID;
812 }
813
814 /* prefix is mandatory for extension instances */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200815 if (prefix != 2) {
Radek Krejci33090f92020-12-17 20:12:46 +0100816 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start, "a keyword");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200817 return LY_EVALID;
818 }
819
Radek Krejcid6b76452019-09-03 17:03:03 +0200820 *kw = LY_STMT_EXTENSION_INSTANCE;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200821 }
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200822
Radek Krejci626df482018-10-11 15:06:31 +0200823success:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200824 if (word_p) {
825 *word_p = (char *)word_start;
Radek Krejci33090f92020-12-17 20:12:46 +0100826 *word_len = ctx->in->current - word_start;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200827 }
828
829 return LY_SUCCESS;
830}
831
Michal Vaskoea5abea2018-09-18 13:10:54 +0200832/**
833 * @brief Parse extension instance substatements.
834 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200835 * @param[in] ctx yang parser context for logging.
Radek Krejci335332a2019-09-05 13:03:35 +0200836 * @param[in] kw Statement keyword value matching @p word value.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200837 * @param[in] word Extension instance substatement name (keyword).
838 * @param[in] word_len Extension instance substatement name length.
839 * @param[in,out] child Children of this extension instance to add to.
840 *
841 * @return LY_ERR values.
842 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200843static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100844parse_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 +0200845 struct lysp_stmt **child)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200846{
847 char *buf;
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100848 LY_ERR ret = LY_SUCCESS;
Radek Krejci335332a2019-09-05 13:03:35 +0200849 enum ly_stmt child_kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200850 struct lysp_stmt *stmt, *par_child;
851
852 stmt = calloc(1, sizeof *stmt);
853 LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM);
854
Radek Krejcibb9b1982019-04-08 14:24:59 +0200855 /* insert into parent statements */
856 if (!*child) {
857 *child = stmt;
858 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +0200859 for (par_child = *child; par_child->next; par_child = par_child->next) {}
Radek Krejcibb9b1982019-04-08 14:24:59 +0200860 par_child->next = stmt;
861 }
862
Michal Vaskofc2cd072021-02-24 13:17:17 +0100863 /* statement */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200864 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), word, word_len, &stmt->stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200865
866 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100867 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, &stmt->flags, &word, &buf, &word_len));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200868 if (word) {
aPieceka24a2252021-05-07 10:52:31 +0200869 INSERT_WORD_RET(ctx, buf, stmt->arg, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200870 }
871
Radek Krejci8df109d2021-04-23 12:19:08 +0200872 stmt->format = LY_VALUE_SCHEMA;
Michal Vaskofc2cd072021-02-24 13:17:17 +0100873 stmt->prefix_data = ctx->parsed_mod;
874 stmt->kw = kw;
875
aPieceka24a2252021-05-07 10:52:31 +0200876 YANG_READ_SUBSTMT_FOR(ctx, child_kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Radek Krejci33090f92020-12-17 20:12:46 +0100877 LY_CHECK_RET(parse_ext_substmt(ctx, child_kw, word, word_len, &stmt->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200878 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200879 return ret;
880}
881
Michal Vaskoea5abea2018-09-18 13:10:54 +0200882/**
883 * @brief Parse extension instance.
884 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200885 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200886 * @param[in] ext_name Extension instance substatement name (keyword).
887 * @param[in] ext_name_len Extension instance substatement name length.
Radek Krejcifc596f92021-02-26 22:40:26 +0100888 * @param[in] insubstmt The statement this extension instance is a substatement of.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200889 * @param[in] insubstmt_index Index of the keyword instance this extension instance is a substatement of.
890 * @param[in,out] exts Extension instances to add to.
891 *
892 * @return LY_ERR values.
893 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200894static LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +0100895parse_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 +0200896 LY_ARRAY_COUNT_TYPE insubstmt_index, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200897{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100898 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200899 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200900 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200901 struct lysp_ext_instance *e;
Radek Krejcid6b76452019-09-03 17:03:03 +0200902 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200903
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200904 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *exts, e, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200905
Michal Vaskofc2cd072021-02-24 13:17:17 +0100906 if (!ly_strnchr(ext_name, ':', ext_name_len)) {
907 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Extension instance \"%*.s\" without the mandatory prefix.", ext_name_len, ext_name);
908 return LY_EVALID;
909 }
910
911 /* store name */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200912 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), ext_name, ext_name_len, &e->name));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200913
914 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100915 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, NULL, &word, &buf, &word_len));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200916 if (word) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200917 INSERT_WORD_RET(ctx, buf, e->argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200918 }
919
Michal Vaskofc2cd072021-02-24 13:17:17 +0100920 /* store the rest of information */
Radek Krejci8df109d2021-04-23 12:19:08 +0200921 e->format = LY_VALUE_SCHEMA;
aPiecek60d9d672021-04-27 15:49:57 +0200922 e->parsed = NULL;
Michal Vaskofc2cd072021-02-24 13:17:17 +0100923 e->prefix_data = ctx->parsed_mod;
Radek Krejciab430862021-03-02 20:13:40 +0100924 e->parent_stmt = insubstmt;
925 e->parent_stmt_index = insubstmt_index;
Michal Vaskofc2cd072021-02-24 13:17:17 +0100926
aPieceka24a2252021-05-07 10:52:31 +0200927 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Radek Krejci33090f92020-12-17 20:12:46 +0100928 LY_CHECK_RET(parse_ext_substmt(ctx, kw, word, word_len, &e->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200929 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200930 return ret;
931}
932
Michal Vaskoea5abea2018-09-18 13:10:54 +0200933/**
934 * @brief Parse a generic text field without specific constraints. Those are contact, organization,
935 * description, etc...
936 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200937 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200938 * @param[in] substmt Type of this substatement.
939 * @param[in] substmt_index Index of this substatement.
940 * @param[in,out] value Place to store the parsed value.
941 * @param[in] arg Type of the YANG keyword argument (of the value).
942 * @param[in,out] exts Extension instances to add to.
943 *
944 * @return LY_ERR values.
945 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200946static LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +0100947parse_text_field(struct lys_yang_parser_ctx *ctx, enum ly_stmt substmt, uint32_t substmt_index,
Radek Krejci0f969882020-08-21 16:56:47 +0200948 const char **value, enum yang_arg arg, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200949{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100950 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200951 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200952 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200953 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200954
955 if (*value) {
Radek Krejci3972b332021-03-02 16:34:31 +0100956 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200957 return LY_EVALID;
958 }
959
960 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +0100961 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200962
963 /* store value and spend buf if allocated */
Radek Krejci011e4aa2020-09-04 15:22:31 +0200964 INSERT_WORD_RET(ctx, buf, *value, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200965
aPieceka24a2252021-05-07 10:52:31 +0200966 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200967 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200968 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +0100969 LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, substmt_index, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200970 break;
971 default:
Radek Krejci3972b332021-03-02 16:34:31 +0100972 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200973 return LY_EVALID;
974 }
975 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200976 return ret;
977}
978
Michal Vaskoea5abea2018-09-18 13:10:54 +0200979/**
980 * @brief Parse the yang-version statement.
981 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200982 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100983 * @param[out] version Storage for the parsed information.
Michal Vasko63f3d842020-07-08 10:10:14 +0200984 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200985 *
986 * @return LY_ERR values.
987 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200988static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100989parse_yangversion(struct lys_yang_parser_ctx *ctx, uint8_t *version, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200990{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100991 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200992 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200993 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200994 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200995
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100996 if (*version) {
David Sedlákb3077192019-06-19 10:55:37 +0200997 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200998 return LY_EVALID;
999 }
1000
1001 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001002 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001003
Radek Krejci96e48da2020-09-04 13:18:06 +02001004 if ((word_len == 1) && !strncmp(word, "1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001005 *version = LYS_VERSION_1_0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001006 } else if ((word_len == ly_strlen_const("1.1")) && !strncmp(word, "1.1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001007 *version = LYS_VERSION_1_1;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001008 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001009 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001010 free(buf);
1011 return LY_EVALID;
1012 }
1013 free(buf);
1014
aPieceka24a2252021-05-07 10:52:31 +02001015 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001016 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001017 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001018 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_YANG_VERSION, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001019 break;
1020 default:
David Sedlákb3077192019-06-19 10:55:37 +02001021 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001022 return LY_EVALID;
1023 }
1024 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001025 return ret;
1026}
1027
Michal Vaskoea5abea2018-09-18 13:10:54 +02001028/**
1029 * @brief Parse the belongs-to statement.
1030 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001031 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001032 * @param[in,out] prefix Place to store the parsed belongs-to prefix value.
1033 * @param[in,out] exts Extension instances to add to.
1034 *
1035 * @return LY_ERR values.
1036 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001037static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001038parse_belongsto(struct lys_yang_parser_ctx *ctx, const char **prefix, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001039{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001040 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001041 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001042 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001043 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001044
Michal Vaskoc3781c32020-10-06 14:04:08 +02001045 if (*prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001046 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001047 return LY_EVALID;
1048 }
1049
Michal Vaskoc3781c32020-10-06 14:04:08 +02001050 /* get value, it must match the main module */
Radek Krejci33090f92020-12-17 20:12:46 +01001051 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001052 if (ly_strncmp(ctx->parsed_mod->mod->name, word, word_len)) {
Michal Vaskoc3781c32020-10-06 14:04:08 +02001053 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Submodule \"belongs-to\" value \"%.*s\" does not match its module name \"%s\".",
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001054 (int)word_len, word, ctx->parsed_mod->mod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02001055 free(buf);
1056 return LY_EVALID;
1057 }
1058 free(buf);
Radek Krejcif09e4e82019-06-14 15:08:11 +02001059
aPieceka24a2252021-05-07 10:52:31 +02001060 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001061 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001062 case LY_STMT_PREFIX:
Radek Krejcifc596f92021-02-26 22:40:26 +01001063 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_PREFIX, 0, prefix, Y_IDENTIF_ARG, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001064 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001065 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001066 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_BELONGS_TO, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001067 break;
1068 default:
David Sedlákb3077192019-06-19 10:55:37 +02001069 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001070 return LY_EVALID;
1071 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001072 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001073 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001074checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001075 /* mandatory substatements */
1076 if (!*prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001077 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001078 return LY_EVALID;
1079 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001080 return ret;
1081}
1082
Michal Vaskoea5abea2018-09-18 13:10:54 +02001083/**
1084 * @brief Parse the revision-date statement.
1085 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001086 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001087 * @param[in,out] rev Array to store the parsed value in.
1088 * @param[in,out] exts Extension instances to add to.
1089 *
1090 * @return LY_ERR values.
1091 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001092static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001093parse_revisiondate(struct lys_yang_parser_ctx *ctx, char *rev, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001094{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001095 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001096 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001097 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001098 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001099
1100 if (rev[0]) {
David Sedlákb3077192019-06-19 10:55:37 +02001101 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001102 return LY_EVALID;
1103 }
1104
1105 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001106 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001107
1108 /* check value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001109 if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision-date")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001110 free(buf);
1111 return LY_EVALID;
1112 }
1113
1114 /* store value and spend buf if allocated */
1115 strncpy(rev, word, word_len);
1116 free(buf);
1117
aPieceka24a2252021-05-07 10:52:31 +02001118 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001119 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001120 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001121 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_REVISION_DATE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001122 break;
1123 default:
David Sedlákb3077192019-06-19 10:55:37 +02001124 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001125 return LY_EVALID;
1126 }
1127 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001128 return ret;
1129}
1130
Michal Vaskoea5abea2018-09-18 13:10:54 +02001131/**
1132 * @brief Parse the include statement.
1133 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001134 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001135 * @param[in] module_name Name of the module to check name collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001136 * @param[in,out] includes Parsed includes to add to.
1137 *
1138 * @return LY_ERR values.
1139 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001140static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001141parse_include(struct lys_yang_parser_ctx *ctx, const char *module_name, struct lysp_include **includes)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001142{
Radek Krejcid33273d2018-10-25 14:55:52 +02001143 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001144 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001145 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001146 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001147 struct lysp_include *inc;
1148
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001149 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *includes, inc, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001150
1151 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001152 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001153
Radek Krejci011e4aa2020-09-04 15:22:31 +02001154 INSERT_WORD_RET(ctx, buf, inc->name, word, word_len);
Radek Krejci086c7132018-10-26 15:29:04 +02001155
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001156 /* submodules share the namespace with the module names, so there must not be
1157 * a module of the same name in the context, no need for revision matching */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001158 if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(PARSER_CTX(ctx), inc->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01001159 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", inc->name);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001160 return LY_EVALID;
1161 }
1162
aPieceka24a2252021-05-07 10:52:31 +02001163 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001164 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001165 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001166 PARSER_CHECK_STMTVER2_RET(ctx, "description", "include");
Radek Krejcifc596f92021-02-26 22:40:26 +01001167 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 +02001168 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001169 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001170 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "include");
Radek Krejcifc596f92021-02-26 22:40:26 +01001171 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 +02001172 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001173 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001174 LY_CHECK_RET(parse_revisiondate(ctx, inc->rev, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001175 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001176 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001177 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_INCLUDE, 0, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001178 break;
1179 default:
David Sedlákb3077192019-06-19 10:55:37 +02001180 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001181 return LY_EVALID;
1182 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001183 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001184 return ret;
1185}
1186
Michal Vaskoea5abea2018-09-18 13:10:54 +02001187/**
1188 * @brief Parse the import statement.
1189 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001190 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001191 * @param[in] module_prefix Prefix of the module to check prefix collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001192 * @param[in,out] imports Parsed imports to add to.
1193 *
1194 * @return LY_ERR values.
1195 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001196static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001197parse_import(struct lys_yang_parser_ctx *ctx, const char *module_prefix, struct lysp_import **imports)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001198{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001199 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001200 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001201 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001202 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001203 struct lysp_import *imp;
1204
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001205 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *imports, imp, LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001206
1207 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001208 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02001209 INSERT_WORD_RET(ctx, buf, imp->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001210
aPieceka24a2252021-05-07 10:52:31 +02001211 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001212 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001213 case LY_STMT_PREFIX:
Radek Krejcifc596f92021-02-26 22:40:26 +01001214 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 +01001215 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 +02001216 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001217 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001218 PARSER_CHECK_STMTVER2_RET(ctx, "description", "import");
Radek Krejcifc596f92021-02-26 22:40:26 +01001219 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 +02001220 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001221 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001222 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "import");
Radek Krejcifc596f92021-02-26 22:40:26 +01001223 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 +02001224 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001225 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001226 LY_CHECK_RET(parse_revisiondate(ctx, imp->rev, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001227 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001228 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001229 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_IMPORT, 0, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001230 break;
1231 default:
David Sedlákb3077192019-06-19 10:55:37 +02001232 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001233 return LY_EVALID;
1234 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001235 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001236 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001237checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001238 /* mandatory substatements */
David Sedlákb3077192019-06-19 10:55:37 +02001239 LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001240
1241 return ret;
1242}
1243
Michal Vaskoea5abea2018-09-18 13:10:54 +02001244/**
1245 * @brief Parse the revision statement.
1246 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001247 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001248 * @param[in,out] revs Parsed revisions to add to.
1249 *
1250 * @return LY_ERR values.
1251 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001252static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001253parse_revision(struct lys_yang_parser_ctx *ctx, struct lysp_revision **revs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001254{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001255 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001256 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001257 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001258 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001259 struct lysp_revision *rev;
1260
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001261 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *revs, rev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001262
1263 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001264 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001265
1266 /* check value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001267 if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision")) {
David Sedlák68ef3dc2019-07-22 13:40:19 +02001268 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001269 return LY_EVALID;
1270 }
1271
Radek Krejcib7db73a2018-10-24 14:18:40 +02001272 strncpy(rev->date, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001273 free(buf);
1274
aPieceka24a2252021-05-07 10:52:31 +02001275 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001276 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001277 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001278 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 +02001279 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001280 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001281 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 +02001282 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001283 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001284 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_REVISION, 0, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001285 break;
1286 default:
David Sedlákb3077192019-06-19 10:55:37 +02001287 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001288 return LY_EVALID;
1289 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001290 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001291 return ret;
1292}
1293
Michal Vaskoea5abea2018-09-18 13:10:54 +02001294/**
1295 * @brief Parse a generic text field that can have more instances such as base.
1296 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001297 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001298 * @param[in] substmt Type of this substatement.
1299 * @param[in,out] texts Parsed values to add to.
1300 * @param[in] arg Type of the expected argument.
1301 * @param[in,out] exts Extension instances to add to.
1302 *
1303 * @return LY_ERR values.
1304 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001305static LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +01001306parse_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 +02001307 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001308{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001309 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001310 char *buf, *word;
Radek Krejci151a5b72018-10-19 14:21:44 +02001311 const char **item;
1312 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001313 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001314
1315 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001316 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *texts, item, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001317
1318 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001319 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001320
Radek Krejci011e4aa2020-09-04 15:22:31 +02001321 INSERT_WORD_RET(ctx, buf, *item, word, word_len);
aPieceka24a2252021-05-07 10:52:31 +02001322 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001323 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001324 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001325 LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, LY_ARRAY_COUNT(*texts) - 1, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001326 break;
1327 default:
Radek Krejci3972b332021-03-02 16:34:31 +01001328 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001329 return LY_EVALID;
1330 }
1331 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001332 return ret;
1333}
1334
Michal Vaskoea5abea2018-09-18 13:10:54 +02001335/**
Michal Vasko7f45cf22020-10-01 12:49:44 +02001336 * @brief Parse a generic text field that can have more instances such as base.
1337 *
1338 * @param[in] ctx yang parser context for logging.
Michal Vasko7f45cf22020-10-01 12:49:44 +02001339 * @param[in] substmt Type of this substatement.
1340 * @param[in,out] qnames Parsed qnames to add to.
1341 * @param[in] arg Type of the expected argument.
1342 * @param[in,out] exts Extension instances to add to.
1343 *
1344 * @return LY_ERR values.
1345 */
1346static LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +01001347parse_qnames(struct lys_yang_parser_ctx *ctx, enum ly_stmt substmt, struct lysp_qname **qnames,
Michal Vasko7f45cf22020-10-01 12:49:44 +02001348 enum yang_arg arg, struct lysp_ext_instance **exts)
1349{
1350 LY_ERR ret = LY_SUCCESS;
1351 char *buf, *word;
1352 struct lysp_qname *item;
1353 size_t word_len;
1354 enum ly_stmt kw;
1355
1356 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001357 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *qnames, item, LY_EMEM);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001358
1359 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001360 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001361
1362 INSERT_WORD_RET(ctx, buf, item->str, word, word_len);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001363 item->mod = ctx->parsed_mod;
aPieceka24a2252021-05-07 10:52:31 +02001364 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02001365 switch (kw) {
1366 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001367 LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, LY_ARRAY_COUNT(*qnames) - 1, exts));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001368 break;
1369 default:
Radek Krejci3972b332021-03-02 16:34:31 +01001370 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(substmt));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001371 return LY_EVALID;
1372 }
1373 }
1374 return ret;
1375}
1376
1377/**
Michal Vaskoea5abea2018-09-18 13:10:54 +02001378 * @brief Parse the config statement.
1379 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001380 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001381 * @param[in,out] flags Flags to add to.
1382 * @param[in,out] exts Extension instances to add to.
1383 *
1384 * @return LY_ERR values.
1385 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001386static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001387parse_config(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001388{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001389 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001390 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001391 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001392 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001393
1394 if (*flags & LYS_CONFIG_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001395 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001396 return LY_EVALID;
1397 }
1398
1399 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001400 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001401
Radek Krejcif13b87b2020-12-01 22:02:17 +01001402 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001403 *flags |= LYS_CONFIG_W;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001404 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001405 *flags |= LYS_CONFIG_R;
1406 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001407 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001408 free(buf);
1409 return LY_EVALID;
1410 }
1411 free(buf);
1412
aPieceka24a2252021-05-07 10:52:31 +02001413 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001414 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001415 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001416 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_CONFIG, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001417 break;
1418 default:
David Sedlákb3077192019-06-19 10:55:37 +02001419 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001420 return LY_EVALID;
1421 }
1422 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001423 return ret;
1424}
1425
Michal Vaskoea5abea2018-09-18 13:10:54 +02001426/**
1427 * @brief Parse the mandatory statement.
1428 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001429 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001430 * @param[in,out] flags Flags to add to.
1431 * @param[in,out] exts Extension instances to add to.
1432 *
1433 * @return LY_ERR values.
1434 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001435static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001436parse_mandatory(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001437{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001438 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001439 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001440 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001441 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001442
1443 if (*flags & LYS_MAND_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001444 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001445 return LY_EVALID;
1446 }
1447
1448 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001449 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001450
Radek Krejcif13b87b2020-12-01 22:02:17 +01001451 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001452 *flags |= LYS_MAND_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001453 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001454 *flags |= LYS_MAND_FALSE;
1455 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001456 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001457 free(buf);
1458 return LY_EVALID;
1459 }
1460 free(buf);
1461
aPieceka24a2252021-05-07 10:52:31 +02001462 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001463 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001464 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001465 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MANDATORY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001466 break;
1467 default:
David Sedlákb3077192019-06-19 10:55:37 +02001468 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001469 return LY_EVALID;
1470 }
1471 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001472 return ret;
1473}
1474
Michal Vaskoea5abea2018-09-18 13:10:54 +02001475/**
1476 * @brief Parse a restriction such as range or length.
1477 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001478 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001479 * @param[in] restr_kw Type of this particular restriction.
1480 * @param[in,out] exts Extension instances to add to.
1481 *
1482 * @return LY_ERR values.
1483 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001484static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001485parse_restr(struct lys_yang_parser_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr *restr)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001486{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001487 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001488 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001489 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001490 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001491
1492 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001493 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001494
Michal Vaskob36053d2020-03-26 15:49:30 +01001495 CHECK_NONEMPTY(ctx, word_len, ly_stmt2str(restr_kw));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001496 INSERT_WORD_RET(ctx, buf, restr->arg.str, word, word_len);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001497 restr->arg.mod = ctx->parsed_mod;
aPieceka24a2252021-05-07 10:52:31 +02001498 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001499 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001500 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001501 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 +02001502 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001503 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001504 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 +02001505 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001506 case LY_STMT_ERROR_APP_TAG:
Radek Krejcifc596f92021-02-26 22:40:26 +01001507 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 +02001508 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001509 case LY_STMT_ERROR_MESSAGE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001510 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 +02001511 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001512 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001513 LY_CHECK_RET(parse_ext(ctx, word, word_len, restr_kw, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001514 break;
1515 default:
David Sedlákb3077192019-06-19 10:55:37 +02001516 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001517 return LY_EVALID;
1518 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001519 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001520 return ret;
1521}
1522
Michal Vaskoea5abea2018-09-18 13:10:54 +02001523/**
1524 * @brief Parse a restriction that can have more instances such as must.
1525 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001526 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001527 * @param[in] restr_kw Type of this particular restriction.
1528 * @param[in,out] restrs Restrictions to add to.
1529 *
1530 * @return LY_ERR values.
1531 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001532static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001533parse_restrs(struct lys_yang_parser_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr **restrs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001534{
1535 struct lysp_restr *restr;
1536
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001537 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *restrs, restr, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01001538 return parse_restr(ctx, restr_kw, restr);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001539}
1540
Michal Vaskoea5abea2018-09-18 13:10:54 +02001541/**
1542 * @brief Parse the status statement.
1543 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001544 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001545 * @param[in,out] flags Flags to add to.
1546 * @param[in,out] exts Extension instances to add to.
1547 *
1548 * @return LY_ERR values.
1549 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001550static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001551parse_status(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001552{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001553 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001554 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001555 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001556 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001557
1558 if (*flags & LYS_STATUS_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001559 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001560 return LY_EVALID;
1561 }
1562
1563 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001564 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001565
Radek Krejcif13b87b2020-12-01 22:02:17 +01001566 if ((word_len == ly_strlen_const("current")) && !strncmp(word, "current", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001567 *flags |= LYS_STATUS_CURR;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001568 } else if ((word_len == ly_strlen_const("deprecated")) && !strncmp(word, "deprecated", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001569 *flags |= LYS_STATUS_DEPRC;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001570 } else if ((word_len == ly_strlen_const("obsolete")) && !strncmp(word, "obsolete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001571 *flags |= LYS_STATUS_OBSLT;
1572 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001573 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001574 free(buf);
1575 return LY_EVALID;
1576 }
1577 free(buf);
1578
aPieceka24a2252021-05-07 10:52:31 +02001579 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001580 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001581 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001582 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_STATUS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001583 break;
1584 default:
David Sedlákb3077192019-06-19 10:55:37 +02001585 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001586 return LY_EVALID;
1587 }
1588 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001589 return ret;
1590}
1591
Michal Vaskoea5abea2018-09-18 13:10:54 +02001592/**
1593 * @brief Parse the when statement.
1594 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001595 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001596 * @param[in,out] when_p When pointer to parse to.
1597 *
1598 * @return LY_ERR values.
1599 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001600LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001601parse_when(struct lys_yang_parser_ctx *ctx, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001602{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001603 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001604 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001605 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001606 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001607 struct lysp_when *when;
1608
1609 if (*when_p) {
David Sedlákb3077192019-06-19 10:55:37 +02001610 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001611 return LY_EVALID;
1612 }
1613
1614 when = calloc(1, sizeof *when);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001615 LY_CHECK_ERR_RET(!when, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001616 *when_p = when;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001617
1618 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001619 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01001620 CHECK_NONEMPTY(ctx, word_len, "when");
Radek Krejci011e4aa2020-09-04 15:22:31 +02001621 INSERT_WORD_RET(ctx, buf, when->cond, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001622
aPieceka24a2252021-05-07 10:52:31 +02001623 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001624 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001625 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001626 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &when->dsc, Y_STR_ARG, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001627 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001628 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001629 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &when->ref, Y_STR_ARG, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001630 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001631 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001632 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_WHEN, 0, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001633 break;
1634 default:
David Sedlákb3077192019-06-19 10:55:37 +02001635 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001636 return LY_EVALID;
1637 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001638 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001639 return ret;
1640}
1641
Michal Vaskoea5abea2018-09-18 13:10:54 +02001642/**
1643 * @brief Parse the anydata or anyxml statement.
1644 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001645 * @param[in] ctx yang parser context for logging.
Radek Krejci39b7fc22021-02-26 23:29:18 +01001646 * @param[in] any_kw Type of this particular keyword.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001647 * @param[in,out] siblings Siblings to add to.
1648 *
1649 * @return LY_ERR values.
1650 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02001651LY_ERR
Radek Krejci39b7fc22021-02-26 23:29:18 +01001652parse_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 +02001653{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001654 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001655 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001656 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001657 struct lysp_node_anydata *any;
Radek Krejci39b7fc22021-02-26 23:29:18 +01001658 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001659
David Sedlák60adc092019-08-06 15:57:02 +02001660 /* create new structure and insert into siblings */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001661 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, any, next, LY_EMEM);
David Sedlák60adc092019-08-06 15:57:02 +02001662
Radek Krejci39b7fc22021-02-26 23:29:18 +01001663 any->nodetype = any_kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001664 any->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001665
Michal Vasko7fbc8162018-09-17 10:35:16 +02001666 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01001667 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02001668 INSERT_WORD_RET(ctx, buf, any->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001669
1670 /* parse substatements */
aPieceka24a2252021-05-07 10:52:31 +02001671 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001672 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001673 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01001674 LY_CHECK_RET(parse_config(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001675 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001676 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001677 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 +02001678 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001679 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001680 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &any->iffeatures, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001681 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001682 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01001683 LY_CHECK_RET(parse_mandatory(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001684 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001685 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01001686 LY_CHECK_RET(parse_restrs(ctx, kw, &any->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001687 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001688 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001689 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 +02001690 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001691 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001692 LY_CHECK_RET(parse_status(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001693 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001694 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01001695 LY_CHECK_RET(parse_when(ctx, &any->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001696 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001697 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001698 LY_CHECK_RET(parse_ext(ctx, word, word_len, any_kw, 0, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001699 break;
1700 default:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001701 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(any_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001702 return LY_EVALID;
1703 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001704 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001705 return ret;
1706}
1707
Michal Vaskoea5abea2018-09-18 13:10:54 +02001708/**
1709 * @brief Parse the value or position statement. Substatement of type enum statement.
1710 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001711 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001712 * @param[in] val_kw Type of this particular keyword.
1713 * @param[in,out] value Value to write to.
1714 * @param[in,out] flags Flags to write to.
1715 * @param[in,out] exts Extension instances to add to.
1716 *
1717 * @return LY_ERR values.
1718 */
David Sedlákd6ce6d72019-07-16 17:30:18 +02001719LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001720parse_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 +02001721 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001722{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001723 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001724 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001725 size_t word_len;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02001726 long int num = 0;
1727 unsigned long int unum = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001728 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001729
1730 if (*flags & LYS_SET_VALUE) {
David Sedlákb3077192019-06-19 10:55:37 +02001731 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001732 return LY_EVALID;
1733 }
1734 *flags |= LYS_SET_VALUE;
1735
1736 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001737 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001738
Radek Krejcid6b76452019-09-03 17:03:03 +02001739 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 +02001740 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001741 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001742 }
1743
1744 errno = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001745 if (val_kw == LY_STMT_VALUE) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001746 num = strtol(word, &ptr, LY_BASE_DEC);
Michal Vasko69730152020-10-09 16:30:07 +02001747 if ((num < INT64_C(-2147483648)) || (num > INT64_C(2147483647))) {
David Sedlákb3077192019-06-19 10:55:37 +02001748 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001749 goto error;
1750 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001751 } else {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001752 unum = strtoul(word, &ptr, LY_BASE_DEC);
Radek Krejci8b764662018-11-14 14:15:13 +01001753 if (unum > UINT64_C(4294967295)) {
David Sedlákb3077192019-06-19 10:55:37 +02001754 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001755 goto error;
1756 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001757 }
1758 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001759 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001760 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001761 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001762 }
1763 if (errno == ERANGE) {
David Sedlákb3077192019-06-19 10:55:37 +02001764 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001765 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001766 }
Radek Krejcid6b76452019-09-03 17:03:03 +02001767 if (val_kw == LY_STMT_VALUE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001768 *value = num;
1769 } else {
1770 *value = unum;
1771 }
1772 free(buf);
1773
aPieceka24a2252021-05-07 10:52:31 +02001774 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001775 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001776 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001777 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 +02001778 break;
1779 default:
David Sedlákb3077192019-06-19 10:55:37 +02001780 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001781 return LY_EVALID;
1782 }
1783 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001784 return ret;
Radek Krejci8b764662018-11-14 14:15:13 +01001785
1786error:
1787 free(buf);
1788 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001789}
1790
Michal Vaskoea5abea2018-09-18 13:10:54 +02001791/**
1792 * @brief Parse the enum or bit statement. Substatement of type statement.
1793 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001794 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001795 * @param[in] enum_kw Type of this particular keyword.
1796 * @param[in,out] enums Enums or bits to add to.
1797 *
1798 * @return LY_ERR values.
1799 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001800static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001801parse_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 +02001802{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001803 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001804 char *buf, *word;
David Sedlák6544c182019-07-12 13:17:33 +02001805 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001806 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001807 struct lysp_type_enum *enm;
1808
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001809 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *enums, enm, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001810
1811 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001812 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 +02001813 if (enum_kw == LY_STMT_ENUM) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001814 ret = lysp_check_enum_name((struct lys_parser_ctx *)ctx, (const char *)word, word_len);
David Sedlák6544c182019-07-12 13:17:33 +02001815 LY_CHECK_ERR_RET(ret, free(buf), ret);
Radek Krejci335332a2019-09-05 13:03:35 +02001816 } /* else nothing specific for YANG_BIT */
Radek Krejci8b764662018-11-14 14:15:13 +01001817
Radek Krejci011e4aa2020-09-04 15:22:31 +02001818 INSERT_WORD_RET(ctx, buf, enm->name, word, word_len);
Radek Krejci8b764662018-11-14 14:15:13 +01001819 CHECK_UNIQUENESS(ctx, *enums, name, ly_stmt2str(enum_kw), enm->name);
1820
aPieceka24a2252021-05-07 10:52:31 +02001821 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001822 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001823 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001824 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 +02001825 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001826 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02001827 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", ly_stmt2str(enum_kw));
Radek Krejcifc596f92021-02-26 22:40:26 +01001828 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &enm->iffeatures, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001829 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001830 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001831 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 +02001832 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001833 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001834 LY_CHECK_RET(parse_status(ctx, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001835 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001836 case LY_STMT_VALUE:
1837 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 +02001838 ly_stmt2str(enum_kw)), LY_EVALID);
Radek Krejci33090f92020-12-17 20:12:46 +01001839 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, &enm->value, &enm->flags, &enm->exts));
David Sedlák9fb515f2019-07-11 10:33:58 +02001840 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001841 case LY_STMT_POSITION:
1842 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 +02001843 ly_stmt2str(enum_kw)), LY_EVALID);
Radek Krejci33090f92020-12-17 20:12:46 +01001844 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, &enm->value, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001845 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001846 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001847 LY_CHECK_RET(parse_ext(ctx, word, word_len, enum_kw, 0, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001848 break;
1849 default:
David Sedlákb3077192019-06-19 10:55:37 +02001850 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001851 return LY_EVALID;
1852 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001853 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001854 return ret;
1855}
1856
Michal Vaskoea5abea2018-09-18 13:10:54 +02001857/**
1858 * @brief Parse the fraction-digits statement. Substatement of type statement.
1859 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001860 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001861 * @param[in,out] fracdig Value to write to.
1862 * @param[in,out] exts Extension instances to add to.
1863 *
1864 * @return LY_ERR values.
1865 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001866static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001867parse_type_fracdigits(struct lys_yang_parser_ctx *ctx, uint8_t *fracdig, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001868{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001869 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001870 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001871 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001872 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02001873 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001874
1875 if (*fracdig) {
David Sedlákb3077192019-06-19 10:55:37 +02001876 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001877 return LY_EVALID;
1878 }
1879
1880 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001881 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001882
1883 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
David Sedlákb3077192019-06-19 10:55:37 +02001884 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001885 free(buf);
1886 return LY_EVALID;
1887 }
1888
1889 errno = 0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001890 num = strtoul(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001891 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001892 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001893 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001894 free(buf);
1895 return LY_EVALID;
1896 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001897 if ((errno == ERANGE) || (num > LY_TYPE_DEC64_FD_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02001898 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001899 free(buf);
1900 return LY_EVALID;
1901 }
1902 *fracdig = num;
1903 free(buf);
1904
aPieceka24a2252021-05-07 10:52:31 +02001905 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001906 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001907 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001908 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_FRACTION_DIGITS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001909 break;
1910 default:
David Sedlákb3077192019-06-19 10:55:37 +02001911 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001912 return LY_EVALID;
1913 }
1914 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001915 return ret;
1916}
1917
Michal Vaskoea5abea2018-09-18 13:10:54 +02001918/**
1919 * @brief Parse the require-instance statement. Substatement of type statement.
1920 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001921 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001922 * @param[in,out] reqinst Value to write to.
1923 * @param[in,out] flags Flags to write to.
1924 * @param[in,out] exts Extension instances to add to.
1925 *
1926 * @return LY_ERR values.
1927 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001928static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001929parse_type_reqinstance(struct lys_yang_parser_ctx *ctx, uint8_t *reqinst, uint16_t *flags,
Radek Krejci0f969882020-08-21 16:56:47 +02001930 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001931{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001932 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001933 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001934 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001935 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001936
1937 if (*flags & LYS_SET_REQINST) {
David Sedlákb3077192019-06-19 10:55:37 +02001938 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001939 return LY_EVALID;
1940 }
1941 *flags |= LYS_SET_REQINST;
1942
1943 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001944 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001945
Radek Krejcif13b87b2020-12-01 22:02:17 +01001946 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001947 *reqinst = 1;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001948 } else if ((word_len != ly_strlen_const("false")) || strncmp(word, "false", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001949 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001950 free(buf);
1951 return LY_EVALID;
1952 }
1953 free(buf);
1954
aPieceka24a2252021-05-07 10:52:31 +02001955 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001956 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001957 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001958 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_REQUIRE_INSTANCE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001959 break;
1960 default:
David Sedlákb3077192019-06-19 10:55:37 +02001961 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001962 return LY_EVALID;
1963 }
1964 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001965 return ret;
1966}
1967
Michal Vaskoea5abea2018-09-18 13:10:54 +02001968/**
1969 * @brief Parse the modifier statement. Substatement of type pattern statement.
1970 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001971 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001972 * @param[in,out] pat Value to write to.
1973 * @param[in,out] exts Extension instances to add to.
1974 *
1975 * @return LY_ERR values.
1976 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001977static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001978parse_type_pattern_modifier(struct lys_yang_parser_ctx *ctx, const char **pat, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001979{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001980 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001981 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001982 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001983 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001984
Radek Krejcif13b87b2020-12-01 22:02:17 +01001985 if ((*pat)[0] == LYSP_RESTR_PATTERN_NACK) {
David Sedlákb3077192019-06-19 10:55:37 +02001986 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001987 return LY_EVALID;
1988 }
1989
1990 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001991 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001992
Radek Krejcif13b87b2020-12-01 22:02:17 +01001993 if ((word_len != ly_strlen_const("invert-match")) || strncmp(word, "invert-match", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001994 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001995 free(buf);
1996 return LY_EVALID;
1997 }
1998 free(buf);
1999
2000 /* replace the value in the dictionary */
2001 buf = malloc(strlen(*pat) + 1);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002002 LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002003 strcpy(buf, *pat);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002004 lydict_remove(PARSER_CTX(ctx), *pat);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002005
Radek Krejcif13b87b2020-12-01 22:02:17 +01002006 assert(buf[0] == LYSP_RESTR_PATTERN_ACK);
2007 buf[0] = LYSP_RESTR_PATTERN_NACK;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002008 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, pat));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002009
aPieceka24a2252021-05-07 10:52:31 +02002010 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002011 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002012 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002013 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MODIFIER, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002014 break;
2015 default:
David Sedlákb3077192019-06-19 10:55:37 +02002016 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002017 return LY_EVALID;
2018 }
2019 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002020 return ret;
2021}
2022
Michal Vaskoea5abea2018-09-18 13:10:54 +02002023/**
2024 * @brief Parse the pattern statement. Substatement of type statement.
2025 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002026 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002027 * @param[in,out] patterns Restrictions to add to.
2028 *
2029 * @return LY_ERR values.
2030 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002031static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002032parse_type_pattern(struct lys_yang_parser_ctx *ctx, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002033{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002034 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002035 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002036 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002037 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002038 struct lysp_restr *restr;
2039
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002040 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *patterns, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002041
2042 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002043 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002044
2045 /* add special meaning first byte */
2046 if (buf) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01002047 buf = ly_realloc(buf, word_len + 2);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002048 word = buf;
2049 } else {
2050 buf = malloc(word_len + 2);
2051 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002052 LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci86d106e2018-10-18 09:53:19 +02002053 memmove(buf + 1, word, word_len);
Radek Krejcif13b87b2020-12-01 22:02:17 +01002054 buf[0] = LYSP_RESTR_PATTERN_ACK; /* pattern's default regular-match flag */
Radek Krejci86d106e2018-10-18 09:53:19 +02002055 buf[word_len + 1] = '\0'; /* terminating NULL byte */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002056 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, &restr->arg.str));
2057 restr->arg.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002058
aPieceka24a2252021-05-07 10:52:31 +02002059 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002060 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002061 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002062 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 +02002063 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002064 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002065 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 +02002066 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002067 case LY_STMT_ERROR_APP_TAG:
Radek Krejcifc596f92021-02-26 22:40:26 +01002068 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 +02002069 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002070 case LY_STMT_ERROR_MESSAGE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002071 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 +02002072 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002073 case LY_STMT_MODIFIER:
Radek Krejci335332a2019-09-05 13:03:35 +02002074 PARSER_CHECK_STMTVER2_RET(ctx, "modifier", "pattern");
Radek Krejci33090f92020-12-17 20:12:46 +01002075 LY_CHECK_RET(parse_type_pattern_modifier(ctx, &restr->arg.str, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002076 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002077 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002078 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_PATTERN, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002079 break;
2080 default:
David Sedlákb3077192019-06-19 10:55:37 +02002081 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002082 return LY_EVALID;
2083 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002084 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002085 return ret;
2086}
2087
Michal Vaskoea5abea2018-09-18 13:10:54 +02002088/**
2089 * @brief Parse the type statement.
2090 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002091 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002092 * @param[in,out] type Type to wrote to.
2093 *
2094 * @return LY_ERR values.
2095 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002096static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002097parse_type(struct lys_yang_parser_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002098{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002099 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002100 char *buf, *word;
Michal Vasko004d3152020-06-11 19:59:22 +02002101 const char *str_path = NULL;
Radek Krejciefd22f62018-09-27 11:47:58 +02002102 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002103 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002104 struct lysp_type *nest_type;
2105
2106 if (type->name) {
David Sedlákb3077192019-06-19 10:55:37 +02002107 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002108 return LY_EVALID;
2109 }
2110
2111 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002112 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002113 INSERT_WORD_RET(ctx, buf, type->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002114
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002115 /* set module */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002116 type->pmod = ctx->parsed_mod;
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002117
aPieceka24a2252021-05-07 10:52:31 +02002118 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002119 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002120 case LY_STMT_BASE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002121 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 +01002122 type->flags |= LYS_SET_BASE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002123 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002124 case LY_STMT_BIT:
Radek Krejci33090f92020-12-17 20:12:46 +01002125 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->bits));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002126 type->flags |= LYS_SET_BIT;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002127 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002128 case LY_STMT_ENUM:
Radek Krejci33090f92020-12-17 20:12:46 +01002129 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->enums));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002130 type->flags |= LYS_SET_ENUM;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002131 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002132 case LY_STMT_FRACTION_DIGITS:
Radek Krejci33090f92020-12-17 20:12:46 +01002133 LY_CHECK_RET(parse_type_fracdigits(ctx, &type->fraction_digits, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002134 type->flags |= LYS_SET_FRDIGITS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002135 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002136 case LY_STMT_LENGTH:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002137 if (type->length) {
David Sedlákb3077192019-06-19 10:55:37 +02002138 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002139 return LY_EVALID;
2140 }
2141 type->length = calloc(1, sizeof *type->length);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002142 LY_CHECK_ERR_RET(!type->length, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002143
Radek Krejci33090f92020-12-17 20:12:46 +01002144 LY_CHECK_RET(parse_restr(ctx, kw, type->length));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002145 type->flags |= LYS_SET_LENGTH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002146 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002147 case LY_STMT_PATH:
Michal Vasko004d3152020-06-11 19:59:22 +02002148 if (type->path) {
Radek Krejci3972b332021-03-02 16:34:31 +01002149 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(LY_STMT_PATH));
Michal Vasko004d3152020-06-11 19:59:22 +02002150 return LY_EVALID;
2151 }
2152
Radek Krejcifc596f92021-02-26 22:40:26 +01002153 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_PATH, 0, &str_path, Y_STR_ARG, &type->exts));
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002154 ret = ly_path_parse(PARSER_CTX(ctx), NULL, str_path, 0, LY_PATH_BEGIN_EITHER, LY_PATH_LREF_TRUE,
Michal Vasko69730152020-10-09 16:30:07 +02002155 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002156 lydict_remove(PARSER_CTX(ctx), str_path);
Michal Vasko004d3152020-06-11 19:59:22 +02002157 LY_CHECK_RET(ret);
Radek Krejcid505e3d2018-11-13 09:04:17 +01002158 type->flags |= LYS_SET_PATH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002159 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002160 case LY_STMT_PATTERN:
Radek Krejci33090f92020-12-17 20:12:46 +01002161 LY_CHECK_RET(parse_type_pattern(ctx, &type->patterns));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002162 type->flags |= LYS_SET_PATTERN;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002163 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002164 case LY_STMT_RANGE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002165 if (type->range) {
David Sedlákb3077192019-06-19 10:55:37 +02002166 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002167 return LY_EVALID;
2168 }
2169 type->range = calloc(1, sizeof *type->range);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002170 LY_CHECK_ERR_RET(!type->range, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002171
Radek Krejci33090f92020-12-17 20:12:46 +01002172 LY_CHECK_RET(parse_restr(ctx, kw, type->range));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002173 type->flags |= LYS_SET_RANGE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002174 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002175 case LY_STMT_REQUIRE_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002176 LY_CHECK_RET(parse_type_reqinstance(ctx, &type->require_instance, &type->flags, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002177 /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002178 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002179 case LY_STMT_TYPE:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002180 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), type->types, nest_type, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01002181 LY_CHECK_RET(parse_type(ctx, nest_type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002182 type->flags |= LYS_SET_TYPE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002183 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002184 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002185 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_TYPE, 0, &type->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002186 break;
2187 default:
David Sedlákb3077192019-06-19 10:55:37 +02002188 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002189 return LY_EVALID;
2190 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002191 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002192 return ret;
2193}
2194
Michal Vaskoea5abea2018-09-18 13:10:54 +02002195/**
2196 * @brief Parse the leaf statement.
2197 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002198 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002199 * @param[in,out] siblings Siblings to add to.
2200 *
2201 * @return LY_ERR values.
2202 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002203LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002204parse_leaf(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002205{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002206 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002207 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002208 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002209 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002210 struct lysp_node_leaf *leaf;
2211
David Sedlák60adc092019-08-06 15:57:02 +02002212 /* create new leaf structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002213 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, leaf, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002214 leaf->nodetype = LYS_LEAF;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002215 leaf->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002216
Michal Vasko7fbc8162018-09-17 10:35:16 +02002217 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002218 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002219 INSERT_WORD_RET(ctx, buf, leaf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002220
2221 /* parse substatements */
aPieceka24a2252021-05-07 10:52:31 +02002222 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002223 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002224 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002225 LY_CHECK_RET(parse_config(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002226 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002227 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01002228 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DEFAULT, 0, &leaf->dflt.str, Y_STR_ARG, &leaf->exts));
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002229 leaf->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002230 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002231 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002232 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 +02002233 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002234 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002235 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &leaf->iffeatures, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002236 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002237 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002238 LY_CHECK_RET(parse_mandatory(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002239 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002240 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002241 LY_CHECK_RET(parse_restrs(ctx, kw, &leaf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002242 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002243 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002244 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 +02002245 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002246 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002247 LY_CHECK_RET(parse_status(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002248 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002249 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002250 LY_CHECK_RET(parse_type(ctx, &leaf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002251 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002252 case LY_STMT_UNITS:
Radek Krejcifc596f92021-02-26 22:40:26 +01002253 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 +02002254 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002255 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002256 LY_CHECK_RET(parse_when(ctx, &leaf->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002257 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002258 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002259 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_LEAF, 0, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002260 break;
2261 default:
David Sedlákb3077192019-06-19 10:55:37 +02002262 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002263 return LY_EVALID;
2264 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002265 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002266 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002267checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002268 /* mandatory substatements */
2269 if (!leaf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002270 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002271 return LY_EVALID;
2272 }
2273
2274 return ret;
2275}
2276
Michal Vaskoea5abea2018-09-18 13:10:54 +02002277/**
2278 * @brief Parse the max-elements statement.
2279 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002280 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002281 * @param[in,out] max Value to write to.
2282 * @param[in,out] flags Flags to write to.
2283 * @param[in,out] exts Extension instances to add to.
2284 *
2285 * @return LY_ERR values.
2286 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002287LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002288parse_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 +02002289{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002290 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002291 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002292 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002293 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002294 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002295
2296 if (*flags & LYS_SET_MAX) {
David Sedlákb3077192019-06-19 10:55:37 +02002297 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002298 return LY_EVALID;
2299 }
2300 *flags |= LYS_SET_MAX;
2301
2302 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002303 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002304
2305 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
David Sedlákb3077192019-06-19 10:55:37 +02002306 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002307 free(buf);
2308 return LY_EVALID;
2309 }
2310
Radek Krejci7f9b6512019-09-18 13:11:09 +02002311 if (ly_strncmp("unbounded", word, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002312 errno = 0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002313 num = strtoul(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002314 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002315 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002316 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002317 free(buf);
2318 return LY_EVALID;
2319 }
2320 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002321 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002322 free(buf);
2323 return LY_EVALID;
2324 }
2325
2326 *max = num;
Radek Krejcid6315102021-02-02 15:26:34 +01002327 } else {
2328 /* unbounded */
2329 *max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002330 }
2331 free(buf);
2332
aPieceka24a2252021-05-07 10:52:31 +02002333 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002334 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002335 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002336 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MAX_ELEMENTS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002337 break;
2338 default:
David Sedlákb3077192019-06-19 10:55:37 +02002339 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002340 return LY_EVALID;
2341 }
2342 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002343 return ret;
2344}
2345
Michal Vaskoea5abea2018-09-18 13:10:54 +02002346/**
2347 * @brief Parse the min-elements statement.
2348 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002349 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002350 * @param[in,out] min Value to write to.
2351 * @param[in,out] flags Flags to write to.
2352 * @param[in,out] exts Extension instances to add to.
2353 *
2354 * @return LY_ERR values.
2355 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002356LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002357parse_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 +02002358{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002359 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002360 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002361 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002362 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002363 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002364
2365 if (*flags & LYS_SET_MIN) {
David Sedlákb3077192019-06-19 10:55:37 +02002366 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002367 return LY_EVALID;
2368 }
2369 *flags |= LYS_SET_MIN;
2370
2371 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002372 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002373
2374 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
David Sedlákb3077192019-06-19 10:55:37 +02002375 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002376 free(buf);
2377 return LY_EVALID;
2378 }
2379
2380 errno = 0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002381 num = strtoul(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002382 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002383 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002384 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002385 free(buf);
2386 return LY_EVALID;
2387 }
2388 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002389 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002390 free(buf);
2391 return LY_EVALID;
2392 }
2393 *min = num;
2394 free(buf);
2395
aPieceka24a2252021-05-07 10:52:31 +02002396 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002397 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002398 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002399 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MIN_ELEMENTS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002400 break;
2401 default:
David Sedlákb3077192019-06-19 10:55:37 +02002402 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002403 return LY_EVALID;
2404 }
2405 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002406 return ret;
2407}
2408
Michal Vaskoea5abea2018-09-18 13:10:54 +02002409/**
2410 * @brief Parse the ordered-by statement.
2411 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002412 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002413 * @param[in,out] flags Flags to write to.
2414 * @param[in,out] exts Extension instances to add to.
2415 *
2416 * @return LY_ERR values.
2417 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002418static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002419parse_orderedby(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002420{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002421 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002422 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002423 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002424 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002425
2426 if (*flags & LYS_ORDBY_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02002427 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002428 return LY_EVALID;
2429 }
2430
2431 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002432 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002433
Radek Krejcif13b87b2020-12-01 22:02:17 +01002434 if ((word_len == ly_strlen_const("system")) && !strncmp(word, "system", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002435 *flags |= LYS_ORDBY_SYSTEM;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002436 } else if ((word_len == ly_strlen_const("user")) && !strncmp(word, "user", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002437 *flags |= LYS_ORDBY_USER;
2438 } else {
David Sedlákb3077192019-06-19 10:55:37 +02002439 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002440 free(buf);
2441 return LY_EVALID;
2442 }
2443 free(buf);
2444
aPieceka24a2252021-05-07 10:52:31 +02002445 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002446 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002447 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002448 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_ORDERED_BY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002449 break;
2450 default:
David Sedlákb3077192019-06-19 10:55:37 +02002451 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002452 return LY_EVALID;
2453 }
2454 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002455 return ret;
2456}
2457
Michal Vaskoea5abea2018-09-18 13:10:54 +02002458/**
2459 * @brief Parse the leaf-list statement.
2460 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002461 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002462 * @param[in,out] siblings Siblings to add to.
2463 *
2464 * @return LY_ERR values.
2465 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002466LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002467parse_leaflist(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002468{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002469 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002470 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002471 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002472 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002473 struct lysp_node_leaflist *llist;
2474
David Sedlák60adc092019-08-06 15:57:02 +02002475 /* create new leaf-list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002476 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, llist, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002477 llist->nodetype = LYS_LEAFLIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002478 llist->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002479
Michal Vasko7fbc8162018-09-17 10:35:16 +02002480 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002481 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002482 INSERT_WORD_RET(ctx, buf, llist->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002483
2484 /* parse substatements */
aPieceka24a2252021-05-07 10:52:31 +02002485 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002486 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002487 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002488 LY_CHECK_RET(parse_config(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002489 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002490 case LY_STMT_DEFAULT:
Radek Krejci335332a2019-09-05 13:03:35 +02002491 PARSER_CHECK_STMTVER2_RET(ctx, "default", "leaf-list");
Radek Krejcifc596f92021-02-26 22:40:26 +01002492 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, &llist->dflts, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002493 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002494 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002495 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 +02002496 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002497 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002498 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &llist->iffeatures, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002499 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002500 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002501 LY_CHECK_RET(parse_maxelements(ctx, &llist->max, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002502 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002503 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002504 LY_CHECK_RET(parse_minelements(ctx, &llist->min, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002505 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002506 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002507 LY_CHECK_RET(parse_restrs(ctx, kw, &llist->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002508 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002509 case LY_STMT_ORDERED_BY:
Radek Krejci33090f92020-12-17 20:12:46 +01002510 LY_CHECK_RET(parse_orderedby(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002511 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002512 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002513 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 +02002514 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002515 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002516 LY_CHECK_RET(parse_status(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002517 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002518 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002519 LY_CHECK_RET(parse_type(ctx, &llist->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002520 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002521 case LY_STMT_UNITS:
Radek Krejcifc596f92021-02-26 22:40:26 +01002522 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 +02002523 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002524 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002525 LY_CHECK_RET(parse_when(ctx, &llist->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002526 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002527 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002528 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_LEAF_LIST, 0, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002529 break;
2530 default:
David Sedlákb3077192019-06-19 10:55:37 +02002531 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002532 return LY_EVALID;
2533 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002534 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002535 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002536checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002537 /* mandatory substatements */
2538 if (!llist->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002539 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002540 return LY_EVALID;
2541 }
2542
2543 return ret;
2544}
2545
Michal Vaskoea5abea2018-09-18 13:10:54 +02002546/**
2547 * @brief Parse the refine statement.
2548 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002549 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002550 * @param[in,out] refines Refines to add to.
2551 *
2552 * @return LY_ERR values.
2553 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002554static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002555parse_refine(struct lys_yang_parser_ctx *ctx, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002556{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002557 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002558 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002559 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002560 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002561 struct lysp_refine *rf;
2562
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002563 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002564
2565 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002566 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01002567 CHECK_NONEMPTY(ctx, word_len, "refine");
Radek Krejci011e4aa2020-09-04 15:22:31 +02002568 INSERT_WORD_RET(ctx, buf, rf->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002569
aPieceka24a2252021-05-07 10:52:31 +02002570 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002571 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002572 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002573 LY_CHECK_RET(parse_config(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002574 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002575 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01002576 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, &rf->dflts, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002577 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002578 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002579 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 +02002580 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002581 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02002582 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "refine");
Radek Krejcifc596f92021-02-26 22:40:26 +01002583 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &rf->iffeatures, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002584 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002585 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002586 LY_CHECK_RET(parse_maxelements(ctx, &rf->max, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002587 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002588 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002589 LY_CHECK_RET(parse_minelements(ctx, &rf->min, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002590 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002591 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002592 LY_CHECK_RET(parse_restrs(ctx, kw, &rf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002593 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002594 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002595 LY_CHECK_RET(parse_mandatory(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002596 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002597 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002598 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 +02002599 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002600 case LY_STMT_PRESENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002601 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 +02002602 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002603 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002604 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_REFINE, 0, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002605 break;
2606 default:
David Sedlákb3077192019-06-19 10:55:37 +02002607 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002608 return LY_EVALID;
2609 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002610 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002611 return ret;
2612}
2613
Michal Vaskoea5abea2018-09-18 13:10:54 +02002614/**
2615 * @brief Parse the typedef statement.
2616 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002617 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002618 * @param[in,out] typedefs Typedefs to add to.
2619 *
2620 * @return LY_ERR values.
2621 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002622static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002623parse_typedef(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002624{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002625 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002626 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002627 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002628 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002629 struct lysp_tpdf *tpdf;
2630
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002631 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002632
2633 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002634 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002635 INSERT_WORD_RET(ctx, buf, tpdf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002636
2637 /* parse substatements */
aPieceka24a2252021-05-07 10:52:31 +02002638 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002639 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002640 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01002641 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DEFAULT, 0, &tpdf->dflt.str, Y_STR_ARG, &tpdf->exts));
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002642 tpdf->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002643 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002644 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002645 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 +02002646 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002647 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002648 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 +02002649 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002650 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002651 LY_CHECK_RET(parse_status(ctx, &tpdf->flags, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002652 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002653 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002654 LY_CHECK_RET(parse_type(ctx, &tpdf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002655 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002656 case LY_STMT_UNITS:
Radek Krejcifc596f92021-02-26 22:40:26 +01002657 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 +02002658 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002659 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002660 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_TYPEDEF, 0, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002661 break;
2662 default:
David Sedlákb3077192019-06-19 10:55:37 +02002663 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002664 return LY_EVALID;
2665 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002666 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002667 LY_CHECK_RET(ret);
Radek Krejcibbe09a92018-11-08 09:36:54 +01002668checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002669 /* mandatory substatements */
2670 if (!tpdf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002671 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002672 return LY_EVALID;
2673 }
2674
Radek Krejcibbe09a92018-11-08 09:36:54 +01002675 /* store data for collision check */
Michal Vasko7f45cf22020-10-01 12:49:44 +02002676 if (parent && !(parent->nodetype & (LYS_GROUPING | LYS_RPC | LYS_ACTION | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF))) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02002677 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, parent, 0, NULL));
Radek Krejcibbe09a92018-11-08 09:36:54 +01002678 }
2679
Michal Vasko7fbc8162018-09-17 10:35:16 +02002680 return ret;
2681}
2682
Michal Vaskoea5abea2018-09-18 13:10:54 +02002683/**
2684 * @brief Parse the input or output statement.
2685 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002686 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002687 * @param[in] kw Type of this particular keyword
2688 * @param[in,out] inout_p Input/output pointer to write to.
2689 *
2690 * @return LY_ERR values.
2691 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002692static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002693parse_inout(struct lys_yang_parser_ctx *ctx, enum ly_stmt inout_kw, struct lysp_node *parent,
Radek Krejci2a9fc652021-01-22 17:44:34 +01002694 struct lysp_node_action_inout *inout_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002695{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002696 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002697 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002698 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002699 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002700 ly_bool input = &((struct lysp_node_action *)parent)->input == inout_p ? 1 : 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002701
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002702 if (inout_p->nodetype) {
David Sedlákb3077192019-06-19 10:55:37 +02002703 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002704 return LY_EVALID;
2705 }
2706
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002707 /* initiate structure */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002708 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), input ? "input" : "output", 0, &inout_p->name));
2709 inout_p->nodetype = input ? LYS_INPUT : LYS_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002710 inout_p->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002711
2712 /* parse substatements */
aPieceka24a2252021-05-07 10:52:31 +02002713 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002714 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002715 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002716 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", ly_stmt2str(inout_kw));
Radek Krejci0f969882020-08-21 16:56:47 +02002717 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002718 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002719 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002720 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002721 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01002722 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002723 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002724 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002725 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002726 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002727 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01002728 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002729 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002730 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002731 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002732 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002733 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002734 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002735 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002736 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01002737 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002738 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002739 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002740 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)inout_p, &inout_p->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002741 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002742 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002743 PARSER_CHECK_STMTVER2_RET(ctx, "must", ly_stmt2str(inout_kw));
Radek Krejci33090f92020-12-17 20:12:46 +01002744 LY_CHECK_RET(parse_restrs(ctx, kw, &inout_p->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002745 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002746 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002747 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)inout_p, &inout_p->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002748 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002749 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002750 LY_CHECK_RET(parse_ext(ctx, word, word_len, inout_kw, 0, &inout_p->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002751 break;
2752 default:
David Sedlákb3077192019-06-19 10:55:37 +02002753 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002754 return LY_EVALID;
2755 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002756 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002757 LY_CHECK_RET(ret);
Michal Vaskob83af8a2020-01-06 09:49:22 +01002758
Radek Krejci7fc68292019-06-12 13:51:09 +02002759checks:
Radek Krejci01180ac2021-01-27 08:48:22 +01002760 if (!inout_p->child) {
Michal Vaskob83af8a2020-01-06 09:49:22 +01002761 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "data-def-stmt", ly_stmt2str(inout_kw));
2762 return LY_EVALID;
2763 }
2764
Michal Vasko7fbc8162018-09-17 10:35:16 +02002765 return ret;
2766}
2767
Michal Vaskoea5abea2018-09-18 13:10:54 +02002768/**
2769 * @brief Parse the action statement.
2770 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002771 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002772 * @param[in,out] actions Actions to add to.
2773 *
2774 * @return LY_ERR values.
2775 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002776LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002777parse_action(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002778{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002779 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002780 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002781 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002782 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002783 struct lysp_node_action *act;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002784
Radek Krejci2a9fc652021-01-22 17:44:34 +01002785 LY_LIST_NEW_RET(PARSER_CTX(ctx), actions, act, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002786
2787 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002788 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002789 INSERT_WORD_RET(ctx, buf, act->name, word, word_len);
Michal Vasko1bf09392020-03-27 12:38:10 +01002790 act->nodetype = parent ? LYS_ACTION : LYS_RPC;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002791 act->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002792
aPieceka24a2252021-05-07 10:52:31 +02002793 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002794 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002795 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002796 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 +02002797 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002798 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002799 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &act->iffeatures, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002800 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002801 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002802 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 +02002803 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002804 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002805 LY_CHECK_RET(parse_status(ctx, &act->flags, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002806 break;
2807
Radek Krejcid6b76452019-09-03 17:03:03 +02002808 case LY_STMT_INPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002809 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->input));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002810 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002811 case LY_STMT_OUTPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002812 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->output));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002813 break;
2814
Radek Krejcid6b76452019-09-03 17:03:03 +02002815 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002816 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)act, &act->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002817 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002818 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002819 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)act, &act->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002820 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002821 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002822 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 +02002823 break;
2824 default:
David Sedlákb3077192019-06-19 10:55:37 +02002825 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), parent ? "action" : "rpc");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002826 return LY_EVALID;
2827 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002828 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002829 LY_CHECK_RET(ret);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002830
Radek Krejci2a9fc652021-01-22 17:44:34 +01002831checks:
Michal Vasko7f45cf22020-10-01 12:49:44 +02002832 /* always initialize inout, they are technically present (needed for later deviations/refines) */
2833 if (!act->input.nodetype) {
2834 act->input.nodetype = LYS_INPUT;
2835 act->input.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002836 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "input", 0, &act->input.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002837 }
2838 if (!act->output.nodetype) {
2839 act->output.nodetype = LYS_OUTPUT;
2840 act->output.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002841 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "output", 0, &act->output.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002842 }
2843
Michal Vasko7fbc8162018-09-17 10:35:16 +02002844 return ret;
2845}
2846
Michal Vaskoea5abea2018-09-18 13:10:54 +02002847/**
2848 * @brief Parse the notification statement.
2849 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002850 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002851 * @param[in,out] notifs Notifications to add to.
2852 *
2853 * @return LY_ERR values.
2854 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002855LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002856parse_notif(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002857{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002858 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002859 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002860 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002861 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002862 struct lysp_node_notif *notif;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002863
Radek Krejci2a9fc652021-01-22 17:44:34 +01002864 LY_LIST_NEW_RET(PARSER_CTX(ctx), notifs, notif, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002865
2866 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002867 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002868 INSERT_WORD_RET(ctx, buf, notif->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002869 notif->nodetype = LYS_NOTIF;
2870 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002871
aPieceka24a2252021-05-07 10:52:31 +02002872 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002873 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002874 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002875 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 +02002876 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002877 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002878 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &notif->iffeatures, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002879 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002880 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002881 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 +02002882 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002883 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002884 LY_CHECK_RET(parse_status(ctx, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002885 break;
2886
Radek Krejcid6b76452019-09-03 17:03:03 +02002887 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002888 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "notification");
Radek Krejci0f969882020-08-21 16:56:47 +02002889 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002890 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002891 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002892 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002893 case LY_STMT_CHOICE:
Michal Vasko5ae1e1a2021-02-08 09:53:26 +01002894 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002895 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002896 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002897 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002898 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002899 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01002900 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002901 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002902 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002903 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002904 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002905 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002906 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002907 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002908 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01002909 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002910 break;
2911
Radek Krejcid6b76452019-09-03 17:03:03 +02002912 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002913 PARSER_CHECK_STMTVER2_RET(ctx, "must", "notification");
Radek Krejci33090f92020-12-17 20:12:46 +01002914 LY_CHECK_RET(parse_restrs(ctx, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002915 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002916 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002917 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)notif, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002918 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002919 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002920 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002921 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002922 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002923 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_NOTIFICATION, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002924 break;
2925 default:
David Sedlákb3077192019-06-19 10:55:37 +02002926 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002927 return LY_EVALID;
2928 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002929 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002930
Michal Vasko7fbc8162018-09-17 10:35:16 +02002931 return ret;
2932}
2933
Michal Vaskoea5abea2018-09-18 13:10:54 +02002934/**
2935 * @brief Parse the grouping statement.
2936 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002937 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002938 * @param[in,out] groupings Groupings to add to.
2939 *
2940 * @return LY_ERR values.
2941 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002942LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002943parse_grouping(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002944{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002945 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002946 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002947 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002948 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002949 struct lysp_node_grp *grp;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002950
Radek Krejci2a9fc652021-01-22 17:44:34 +01002951 LY_LIST_NEW_RET(PARSER_CTX(ctx), groupings, grp, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002952
2953 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002954 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002955 INSERT_WORD_RET(ctx, buf, grp->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002956 grp->nodetype = LYS_GROUPING;
2957 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002958
aPieceka24a2252021-05-07 10:52:31 +02002959 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002960 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002961 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002962 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 +02002963 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002964 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002965 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 +02002966 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002967 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002968 LY_CHECK_RET(parse_status(ctx, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002969 break;
2970
Radek Krejcid6b76452019-09-03 17:03:03 +02002971 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002972 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "grouping");
Radek Krejci0f969882020-08-21 16:56:47 +02002973 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002974 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002975 LY_CHECK_RET(parse_any(ctx, kw, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002976 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002977 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01002978 LY_CHECK_RET(parse_choice(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002979 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002980 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002981 LY_CHECK_RET(parse_container(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002982 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002983 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01002984 LY_CHECK_RET(parse_leaf(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002985 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002986 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002987 LY_CHECK_RET(parse_leaflist(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002988 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002989 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002990 LY_CHECK_RET(parse_list(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002991 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002992 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01002993 LY_CHECK_RET(parse_uses(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002994 break;
2995
Radek Krejcid6b76452019-09-03 17:03:03 +02002996 case LY_STMT_TYPEDEF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01002997 LY_CHECK_RET(parse_typedef(ctx, &grp->node, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002998 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002999 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003000 PARSER_CHECK_STMTVER2_RET(ctx, "action", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003001 LY_CHECK_RET(parse_action(ctx, &grp->node, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003002 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003003 case LY_STMT_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01003004 LY_CHECK_RET(parse_grouping(ctx, &grp->node, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003005 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003006 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003007 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003008 LY_CHECK_RET(parse_notif(ctx, &grp->node, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003009 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003010 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003011 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_GROUPING, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003012 break;
3013 default:
David Sedlákb3077192019-06-19 10:55:37 +02003014 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003015 return LY_EVALID;
3016 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003017 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003018
Michal Vasko7fbc8162018-09-17 10:35:16 +02003019 return ret;
3020}
3021
Michal Vaskoea5abea2018-09-18 13:10:54 +02003022/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02003023 * @brief Parse the augment statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003024 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003025 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003026 * @param[in,out] augments Augments to add to.
3027 *
3028 * @return LY_ERR values.
3029 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003030LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01003031parse_augment(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003032{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003033 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003034 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003035 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003036 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003037 struct lysp_node_augment *aug;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003038
Radek Krejci2a9fc652021-01-22 17:44:34 +01003039 LY_LIST_NEW_RET(PARSER_CTX(ctx), augments, aug, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003040
3041 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003042 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01003043 CHECK_NONEMPTY(ctx, word_len, "augment");
Radek Krejci011e4aa2020-09-04 15:22:31 +02003044 INSERT_WORD_RET(ctx, buf, aug->nodeid, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003045 aug->nodetype = LYS_AUGMENT;
3046 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003047
aPieceka24a2252021-05-07 10:52:31 +02003048 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003049 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003050 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003051 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 +02003052 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003053 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003054 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &aug->iffeatures, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003055 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003056 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003057 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 +02003058 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003059 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003060 LY_CHECK_RET(parse_status(ctx, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003061 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003062 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003063 LY_CHECK_RET(parse_when(ctx, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003064 break;
3065
Radek Krejcid6b76452019-09-03 17:03:03 +02003066 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003067 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "augment");
Radek Krejci0f969882020-08-21 16:56:47 +02003068 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003069 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003070 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003071 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003072 case LY_STMT_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003073 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003074 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003075 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003076 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003077 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003078 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003079 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003080 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003081 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003082 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003083 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003084 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003085 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003086 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003087 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003088 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003089 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003090 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003091 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003092 break;
3093
Radek Krejcid6b76452019-09-03 17:03:03 +02003094 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003095 PARSER_CHECK_STMTVER2_RET(ctx, "action", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003096 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003097 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003098 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003099 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003100 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003101 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003102 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003103 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_AUGMENT, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003104 break;
3105 default:
David Sedlákb3077192019-06-19 10:55:37 +02003106 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003107 return LY_EVALID;
3108 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003109 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003110
Michal Vasko7fbc8162018-09-17 10:35:16 +02003111 return ret;
3112}
3113
Michal Vaskoea5abea2018-09-18 13:10:54 +02003114/**
3115 * @brief Parse the uses statement.
3116 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003117 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003118 * @param[in,out] siblings Siblings to add to.
3119 *
3120 * @return LY_ERR values.
3121 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003122LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003123parse_uses(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003124{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003125 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003126 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003127 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003128 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003129 struct lysp_node_uses *uses;
3130
David Sedlák60adc092019-08-06 15:57:02 +02003131 /* create uses structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003132 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, uses, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003133 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003134 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003135
Michal Vasko7fbc8162018-09-17 10:35:16 +02003136 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003137 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003138 INSERT_WORD_RET(ctx, buf, uses->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003139
3140 /* parse substatements */
aPieceka24a2252021-05-07 10:52:31 +02003141 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003142 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003143 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003144 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 +02003145 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003146 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003147 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &uses->iffeatures, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003148 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003149 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003150 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 +02003151 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003152 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003153 LY_CHECK_RET(parse_status(ctx, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003154 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003155 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003156 LY_CHECK_RET(parse_when(ctx, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003157 break;
3158
Radek Krejcid6b76452019-09-03 17:03:03 +02003159 case LY_STMT_REFINE:
Radek Krejci33090f92020-12-17 20:12:46 +01003160 LY_CHECK_RET(parse_refine(ctx, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003161 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003162 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003163 LY_CHECK_RET(parse_augment(ctx, (struct lysp_node *)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003164 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003165 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003166 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_USES, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003167 break;
3168 default:
David Sedlákb3077192019-06-19 10:55:37 +02003169 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003170 return LY_EVALID;
3171 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003172 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003173
Michal Vasko7fbc8162018-09-17 10:35:16 +02003174 return ret;
3175}
3176
Michal Vaskoea5abea2018-09-18 13:10:54 +02003177/**
3178 * @brief Parse the case statement.
3179 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003180 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003181 * @param[in,out] siblings Siblings to add to.
3182 *
3183 * @return LY_ERR values.
3184 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003185LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003186parse_case(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003187{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003188 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003189 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003190 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003191 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003192 struct lysp_node_case *cas;
3193
David Sedlák60adc092019-08-06 15:57:02 +02003194 /* create new case structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003195 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cas, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003196 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003197 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003198
Michal Vasko7fbc8162018-09-17 10:35:16 +02003199 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003200 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003201 INSERT_WORD_RET(ctx, buf, cas->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003202
3203 /* parse substatements */
aPieceka24a2252021-05-07 10:52:31 +02003204 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003205 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003206 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003207 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 +02003208 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003209 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003210 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &cas->iffeatures, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003211 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003212 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003213 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 +02003214 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003215 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003216 LY_CHECK_RET(parse_status(ctx, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003217 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003218 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003219 LY_CHECK_RET(parse_when(ctx, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003220 break;
3221
Radek Krejcid6b76452019-09-03 17:03:03 +02003222 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003223 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "case");
Radek Krejci0f969882020-08-21 16:56:47 +02003224 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003225 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003226 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003227 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003228 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003229 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003230 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003231 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003232 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003233 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003234 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003235 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003236 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003237 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003238 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003239 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003240 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003241 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003242 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003243 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003244 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003245 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003246 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003247 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_CASE, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003248 break;
3249 default:
David Sedlákb3077192019-06-19 10:55:37 +02003250 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003251 return LY_EVALID;
3252 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003253 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003254 return ret;
3255}
3256
Michal Vaskoea5abea2018-09-18 13:10:54 +02003257/**
3258 * @brief Parse the choice statement.
3259 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003260 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003261 * @param[in,out] siblings Siblings to add to.
3262 *
3263 * @return LY_ERR values.
3264 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003265LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003266parse_choice(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003267{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003268 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003269 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003270 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003271 enum ly_stmt kw;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003272 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003273
David Sedlák60adc092019-08-06 15:57:02 +02003274 /* create new choice structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003275 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, choice, next, LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003276 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003277 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003278
Michal Vasko7fbc8162018-09-17 10:35:16 +02003279 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003280 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003281 INSERT_WORD_RET(ctx, buf, choice->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003282
3283 /* parse substatements */
aPieceka24a2252021-05-07 10:52:31 +02003284 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003285 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003286 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003287 LY_CHECK_RET(parse_config(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003288 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003289 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003290 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 +02003291 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003292 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003293 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &choice->iffeatures, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003294 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003295 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01003296 LY_CHECK_RET(parse_mandatory(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003297 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003298 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003299 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 +02003300 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003301 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003302 LY_CHECK_RET(parse_status(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003303 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003304 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003305 LY_CHECK_RET(parse_when(ctx, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003306 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003307 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01003308 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 +02003309 &choice->exts));
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003310 choice->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003311 break;
3312
Radek Krejcid6b76452019-09-03 17:03:03 +02003313 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003314 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "choice");
Radek Krejci0f969882020-08-21 16:56:47 +02003315 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003316 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003317 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003318 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003319 case LY_STMT_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003320 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003321 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003322 case LY_STMT_CHOICE:
Radek Krejci335332a2019-09-05 13:03:35 +02003323 PARSER_CHECK_STMTVER2_RET(ctx, "choice", "choice");
Radek Krejci33090f92020-12-17 20:12:46 +01003324 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003325 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003326 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003327 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003328 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003329 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003330 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003331 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003332 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003333 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003334 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003335 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003336 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003337 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003338 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003339 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_CHOICE, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003340 break;
3341 default:
David Sedlákb3077192019-06-19 10:55:37 +02003342 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003343 return LY_EVALID;
3344 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003345 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003346 return ret;
3347}
3348
Michal Vaskoea5abea2018-09-18 13:10:54 +02003349/**
3350 * @brief Parse the container statement.
3351 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003352 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003353 * @param[in,out] siblings Siblings to add to.
3354 *
3355 * @return LY_ERR values.
3356 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003357LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003358parse_container(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003359{
3360 LY_ERR ret = 0;
3361 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003362 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003363 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003364 struct lysp_node_container *cont;
3365
David Sedlák60adc092019-08-06 15:57:02 +02003366 /* create new container structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003367 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cont, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003368 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003369 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003370
Michal Vasko7fbc8162018-09-17 10:35:16 +02003371 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003372 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003373 INSERT_WORD_RET(ctx, buf, cont->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003374
3375 /* parse substatements */
aPieceka24a2252021-05-07 10:52:31 +02003376 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003377 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003378 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003379 LY_CHECK_RET(parse_config(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003380 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003381 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003382 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 +02003383 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003384 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003385 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &cont->iffeatures, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003386 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003387 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003388 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 +02003389 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003390 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003391 LY_CHECK_RET(parse_status(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003392 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003393 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003394 LY_CHECK_RET(parse_when(ctx, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003395 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003396 case LY_STMT_PRESENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003397 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 +02003398 break;
3399
Radek Krejcid6b76452019-09-03 17:03:03 +02003400 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003401 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "container");
Radek Krejci0f969882020-08-21 16:56:47 +02003402 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003403 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003404 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003405 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003406 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003407 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003408 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003409 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003410 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003411 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003412 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003413 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003414 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003415 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003416 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003417 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003418 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003419 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003420 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003421 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003422 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003423 break;
3424
Radek Krejcid6b76452019-09-03 17:03:03 +02003425 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003426 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)cont, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003427 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003428 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003429 LY_CHECK_RET(parse_restrs(ctx, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003430 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003431 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003432 PARSER_CHECK_STMTVER2_RET(ctx, "action", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003433 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003434 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003435 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003436 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003437 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003438 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003439 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003440 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003441 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003442 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003443 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_CONTAINER, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003444 break;
3445 default:
David Sedlákb3077192019-06-19 10:55:37 +02003446 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003447 return LY_EVALID;
3448 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003449 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003450
Michal Vasko7fbc8162018-09-17 10:35:16 +02003451 return ret;
3452}
3453
Michal Vaskoea5abea2018-09-18 13:10:54 +02003454/**
3455 * @brief Parse the list statement.
3456 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003457 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003458 * @param[in,out] siblings Siblings to add to.
3459 *
3460 * @return LY_ERR values.
3461 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003462LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003463parse_list(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003464{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003465 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003466 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003467 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003468 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003469 struct lysp_node_list *list;
3470
David Sedlák60adc092019-08-06 15:57:02 +02003471 /* create new list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003472 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, list, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003473 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003474 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003475
Michal Vasko7fbc8162018-09-17 10:35:16 +02003476 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003477 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003478 INSERT_WORD_RET(ctx, buf, list->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003479
3480 /* parse substatements */
aPieceka24a2252021-05-07 10:52:31 +02003481 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003482 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003483 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003484 LY_CHECK_RET(parse_config(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003485 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003486 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003487 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 +02003488 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003489 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003490 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &list->iffeatures, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003491 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003492 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003493 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 +02003494 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003495 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003496 LY_CHECK_RET(parse_status(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003497 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003498 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003499 LY_CHECK_RET(parse_when(ctx, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003500 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003501 case LY_STMT_KEY:
Radek Krejcifc596f92021-02-26 22:40:26 +01003502 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 +02003503 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003504 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003505 LY_CHECK_RET(parse_maxelements(ctx, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003506 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003507 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003508 LY_CHECK_RET(parse_minelements(ctx, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003509 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003510 case LY_STMT_ORDERED_BY:
Radek Krejci33090f92020-12-17 20:12:46 +01003511 LY_CHECK_RET(parse_orderedby(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003512 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003513 case LY_STMT_UNIQUE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003514 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_UNIQUE, &list->uniques, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003515 break;
3516
Radek Krejcid6b76452019-09-03 17:03:03 +02003517 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003518 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "list");
Radek Krejci0f969882020-08-21 16:56:47 +02003519 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003520 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003521 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003522 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003523 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003524 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003525 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003526 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003527 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003528 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003529 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003530 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003531 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003532 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003533 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003534 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003535 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003536 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003537 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003538 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003539 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003540 break;
3541
Radek Krejcid6b76452019-09-03 17:03:03 +02003542 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003543 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)list, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003544 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003545 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003546 LY_CHECK_RET(parse_restrs(ctx, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003547 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003548 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003549 PARSER_CHECK_STMTVER2_RET(ctx, "action", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003550 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003551 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003552 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003553 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003554 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003555 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003556 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003557 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003558 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003559 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003560 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_LIST, 0, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003561 break;
3562 default:
David Sedlákb3077192019-06-19 10:55:37 +02003563 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003564 return LY_EVALID;
3565 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003566 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003567
Michal Vasko7fbc8162018-09-17 10:35:16 +02003568 return ret;
3569}
3570
Michal Vaskoea5abea2018-09-18 13:10:54 +02003571/**
3572 * @brief Parse the yin-element statement.
3573 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003574 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003575 * @param[in,out] flags Flags to write to.
3576 * @param[in,out] exts Extension instances to add to.
3577 *
3578 * @return LY_ERR values.
3579 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003580static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003581parse_yinelement(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003582{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003583 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003584 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003585 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003586 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003587
3588 if (*flags & LYS_YINELEM_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02003589 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003590 return LY_EVALID;
3591 }
3592
3593 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003594 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003595
Radek Krejcif13b87b2020-12-01 22:02:17 +01003596 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003597 *flags |= LYS_YINELEM_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003598 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003599 *flags |= LYS_YINELEM_FALSE;
3600 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003601 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003602 free(buf);
3603 return LY_EVALID;
3604 }
3605 free(buf);
3606
aPieceka24a2252021-05-07 10:52:31 +02003607 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003608 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003609 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003610 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_YIN_ELEMENT, 0, exts));
Michal Vaskod989ba02020-08-24 10:59:24 +02003611 LY_CHECK_RET(ret);
3612 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003613 default:
David Sedlákb3077192019-06-19 10:55:37 +02003614 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003615 return LY_EVALID;
3616 }
3617 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003618 return ret;
3619}
3620
Michal Vaskoea5abea2018-09-18 13:10:54 +02003621/**
David Sedlák2444f8f2019-07-09 11:02:47 +02003622 * @brief Parse the argument statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003623 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003624 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003625 * @param[in,out] argument Value to write to.
3626 * @param[in,out] flags Flags to write to.
3627 * @param[in,out] exts Extension instances to add to.
3628 *
3629 * @return LY_ERR values.
3630 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003631static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003632parse_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 +02003633{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003634 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003635 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003636 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003637 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003638
3639 if (*argument) {
David Sedlákb3077192019-06-19 10:55:37 +02003640 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003641 return LY_EVALID;
3642 }
3643
3644 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003645 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003646 INSERT_WORD_RET(ctx, buf, *argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003647
aPieceka24a2252021-05-07 10:52:31 +02003648 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003649 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003650 case LY_STMT_YIN_ELEMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003651 LY_CHECK_RET(parse_yinelement(ctx, flags, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003652 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003653 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003654 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_ARGUMENT, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003655 break;
3656 default:
David Sedlákb3077192019-06-19 10:55:37 +02003657 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003658 return LY_EVALID;
3659 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003660 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003661 return ret;
3662}
3663
Michal Vaskoea5abea2018-09-18 13:10:54 +02003664/**
3665 * @brief Parse the extension statement.
3666 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003667 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003668 * @param[in,out] extensions Extensions to add to.
3669 *
3670 * @return LY_ERR values.
3671 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003672static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003673parse_extension(struct lys_yang_parser_ctx *ctx, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003674{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003675 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003676 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003677 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003678 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003679 struct lysp_ext *ex;
3680
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003681 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003682
3683 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003684 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003685 INSERT_WORD_RET(ctx, buf, ex->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003686
aPieceka24a2252021-05-07 10:52:31 +02003687 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003688 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003689 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003690 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 +02003691 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003692 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003693 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 +02003694 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003695 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003696 LY_CHECK_RET(parse_status(ctx, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003697 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003698 case LY_STMT_ARGUMENT:
Radek Krejci9f87b0c2021-03-05 14:45:26 +01003699 LY_CHECK_RET(parse_argument(ctx, &ex->argname, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003700 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003701 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003702 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_EXTENSION, 0, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003703 break;
3704 default:
David Sedlákb3077192019-06-19 10:55:37 +02003705 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003706 return LY_EVALID;
3707 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003708 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003709 return ret;
3710}
3711
Michal Vaskoea5abea2018-09-18 13:10:54 +02003712/**
3713 * @brief Parse the deviate statement.
3714 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003715 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003716 * @param[in,out] deviates Deviates to add to.
3717 *
3718 * @return LY_ERR values.
3719 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003720LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003721parse_deviate(struct lys_yang_parser_ctx *ctx, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003722{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003723 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003724 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003725 size_t word_len, dev_mod;
Radek Krejcid6b76452019-09-03 17:03:03 +02003726 enum ly_stmt kw;
David Sedlák60adc092019-08-06 15:57:02 +02003727 struct lysp_deviate *d;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003728 struct lysp_deviate_add *d_add = NULL;
3729 struct lysp_deviate_rpl *d_rpl = NULL;
3730 struct lysp_deviate_del *d_del = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003731 const char **d_units = NULL;
3732 struct lysp_qname **d_uniques = NULL, **d_dflts = NULL;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02003733 struct lysp_restr **d_musts = NULL;
3734 uint16_t *d_flags = 0;
3735 uint32_t *d_min = 0, *d_max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003736
3737 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003738 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003739
Radek Krejcif13b87b2020-12-01 22:02:17 +01003740 if ((word_len == ly_strlen_const("not-supported")) && !strncmp(word, "not-supported", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003741 dev_mod = LYS_DEV_NOT_SUPPORTED;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003742 } else if ((word_len == ly_strlen_const("add")) && !strncmp(word, "add", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003743 dev_mod = LYS_DEV_ADD;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003744 } else if ((word_len == ly_strlen_const("replace")) && !strncmp(word, "replace", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003745 dev_mod = LYS_DEV_REPLACE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003746 } else if ((word_len == ly_strlen_const("delete")) && !strncmp(word, "delete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003747 dev_mod = LYS_DEV_DELETE;
3748 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003749 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003750 free(buf);
3751 return LY_EVALID;
3752 }
3753 free(buf);
3754
3755 /* create structure */
3756 switch (dev_mod) {
3757 case LYS_DEV_NOT_SUPPORTED:
3758 d = calloc(1, sizeof *d);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003759 LY_CHECK_ERR_RET(!d, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003760 break;
3761 case LYS_DEV_ADD:
3762 d_add = calloc(1, sizeof *d_add);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003763 LY_CHECK_ERR_RET(!d_add, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003764 d = (struct lysp_deviate *)d_add;
3765 d_units = &d_add->units;
3766 d_uniques = &d_add->uniques;
3767 d_dflts = &d_add->dflts;
3768 d_musts = &d_add->musts;
3769 d_flags = &d_add->flags;
3770 d_min = &d_add->min;
3771 d_max = &d_add->max;
3772 break;
3773 case LYS_DEV_REPLACE:
3774 d_rpl = calloc(1, sizeof *d_rpl);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003775 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003776 d = (struct lysp_deviate *)d_rpl;
3777 d_units = &d_rpl->units;
3778 d_flags = &d_rpl->flags;
3779 d_min = &d_rpl->min;
3780 d_max = &d_rpl->max;
3781 break;
3782 case LYS_DEV_DELETE:
3783 d_del = calloc(1, sizeof *d_del);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003784 LY_CHECK_ERR_RET(!d_del, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003785 d = (struct lysp_deviate *)d_del;
3786 d_units = &d_del->units;
3787 d_uniques = &d_del->uniques;
3788 d_dflts = &d_del->dflts;
3789 d_musts = &d_del->musts;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003790 break;
3791 default:
3792 assert(0);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003793 LOGINT_RET(PARSER_CTX(ctx));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003794 }
3795 d->mod = dev_mod;
3796
3797 /* insert into siblings */
David Sedlák60adc092019-08-06 15:57:02 +02003798 LY_LIST_INSERT(deviates, d, next);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003799
aPieceka24a2252021-05-07 10:52:31 +02003800 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003801 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003802 case LY_STMT_CONFIG:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003803 switch (dev_mod) {
3804 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003805 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003806 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003807 return LY_EVALID;
3808 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003809 LY_CHECK_RET(parse_config(ctx, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003810 break;
3811 }
3812 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003813 case LY_STMT_DEFAULT:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003814 switch (dev_mod) {
3815 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003816 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003817 return LY_EVALID;
3818 case LYS_DEV_REPLACE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003819 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DEFAULT, 0, &d_rpl->dflt.str, Y_STR_ARG, &d->exts));
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003820 d_rpl->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003821 break;
3822 default:
Radek Krejcifc596f92021-02-26 22:40:26 +01003823 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, d_dflts, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003824 break;
3825 }
3826 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003827 case LY_STMT_MANDATORY:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003828 switch (dev_mod) {
3829 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003830 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003831 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003832 return LY_EVALID;
3833 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003834 LY_CHECK_RET(parse_mandatory(ctx, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003835 break;
3836 }
3837 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003838 case LY_STMT_MAX_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003839 switch (dev_mod) {
3840 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003841 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003842 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003843 return LY_EVALID;
3844 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003845 LY_CHECK_RET(parse_maxelements(ctx, d_max, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003846 break;
3847 }
3848 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003849 case LY_STMT_MIN_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003850 switch (dev_mod) {
3851 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003852 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003853 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003854 return LY_EVALID;
3855 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003856 LY_CHECK_RET(parse_minelements(ctx, d_min, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003857 break;
3858 }
3859 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003860 case LY_STMT_MUST:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003861 switch (dev_mod) {
3862 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003863 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003864 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003865 return LY_EVALID;
3866 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003867 LY_CHECK_RET(parse_restrs(ctx, kw, d_musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003868 break;
3869 }
3870 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003871 case LY_STMT_TYPE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003872 switch (dev_mod) {
3873 case LYS_DEV_NOT_SUPPORTED:
3874 case LYS_DEV_ADD:
3875 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003876 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003877 return LY_EVALID;
3878 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003879 if (d_rpl->type) {
David Sedlákb3077192019-06-19 10:55:37 +02003880 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003881 return LY_EVALID;
3882 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003883 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003884 LY_CHECK_ERR_RET(!d_rpl->type, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01003885 LY_CHECK_RET(parse_type(ctx, d_rpl->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003886 break;
3887 }
3888 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003889 case LY_STMT_UNIQUE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003890 switch (dev_mod) {
3891 case LYS_DEV_NOT_SUPPORTED:
3892 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003893 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003894 return LY_EVALID;
3895 default:
Radek Krejcifc596f92021-02-26 22:40:26 +01003896 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_UNIQUE, d_uniques, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003897 break;
3898 }
3899 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003900 case LY_STMT_UNITS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003901 switch (dev_mod) {
3902 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003903 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003904 return LY_EVALID;
3905 default:
Radek Krejcifc596f92021-02-26 22:40:26 +01003906 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_UNITS, 0, d_units, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003907 break;
3908 }
3909 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003910 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003911 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_DEVIATE, 0, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003912 break;
3913 default:
David Sedlákb3077192019-06-19 10:55:37 +02003914 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003915 return LY_EVALID;
3916 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003917 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003918 return ret;
3919}
3920
Michal Vaskoea5abea2018-09-18 13:10:54 +02003921/**
3922 * @brief Parse the deviation statement.
3923 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003924 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003925 * @param[in,out] deviations Deviations to add to.
3926 *
3927 * @return LY_ERR values.
3928 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003929LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003930parse_deviation(struct lys_yang_parser_ctx *ctx, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003931{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003932 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003933 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003934 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003935 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003936 struct lysp_deviation *dev;
3937
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003938 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003939
3940 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003941 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01003942 CHECK_NONEMPTY(ctx, word_len, "deviation");
Radek Krejci011e4aa2020-09-04 15:22:31 +02003943 INSERT_WORD_RET(ctx, buf, dev->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003944
aPieceka24a2252021-05-07 10:52:31 +02003945 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003946 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003947 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003948 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &dev->dsc, Y_STR_ARG, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003949 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003950 case LY_STMT_DEVIATE:
Radek Krejci33090f92020-12-17 20:12:46 +01003951 LY_CHECK_RET(parse_deviate(ctx, &dev->deviates));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003952 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003953 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003954 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_REFERENCE, 0, &dev->ref, Y_STR_ARG, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003955 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003956 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003957 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_DEVIATION, 0, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003958 break;
3959 default:
David Sedlákb3077192019-06-19 10:55:37 +02003960 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003961 return LY_EVALID;
3962 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003963 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003964 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01003965checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003966 /* mandatory substatements */
3967 if (!dev->deviates) {
David Sedlákb3077192019-06-19 10:55:37 +02003968 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003969 return LY_EVALID;
3970 }
3971
3972 return ret;
3973}
3974
Michal Vaskoea5abea2018-09-18 13:10:54 +02003975/**
3976 * @brief Parse the feature statement.
3977 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003978 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003979 * @param[in,out] features Features to add to.
3980 *
3981 * @return LY_ERR values.
3982 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003983LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003984parse_feature(struct lys_yang_parser_ctx *ctx, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003985{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003986 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003987 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003988 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003989 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003990 struct lysp_feature *feat;
3991
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003992 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003993
3994 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003995 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003996 INSERT_WORD_RET(ctx, buf, feat->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01003997
aPieceka24a2252021-05-07 10:52:31 +02003998 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003999 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004000 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004001 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 +02004002 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004003 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004004 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &feat->iffeatures, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004005 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004006 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004007 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 +02004008 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004009 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01004010 LY_CHECK_RET(parse_status(ctx, &feat->flags, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004011 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004012 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01004013 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_FEATURE, 0, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004014 break;
4015 default:
David Sedlákb3077192019-06-19 10:55:37 +02004016 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004017 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004018 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004019 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004020 return ret;
4021}
4022
Michal Vaskoea5abea2018-09-18 13:10:54 +02004023/**
4024 * @brief Parse the identity statement.
4025 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004026 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004027 * @param[in,out] identities Identities to add to.
4028 *
4029 * @return LY_ERR values.
4030 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004031LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004032parse_identity(struct lys_yang_parser_ctx *ctx, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004033{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004034 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004035 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004036 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004037 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004038 struct lysp_ident *ident;
4039
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004040 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004041
4042 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01004043 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004044 INSERT_WORD_RET(ctx, buf, ident->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004045
aPieceka24a2252021-05-07 10:52:31 +02004046 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004047 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004048 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004049 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 +02004050 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004051 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02004052 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "identity");
Radek Krejcifc596f92021-02-26 22:40:26 +01004053 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &ident->iffeatures, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004054 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004055 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004056 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 +02004057 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004058 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01004059 LY_CHECK_RET(parse_status(ctx, &ident->flags, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004060 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004061 case LY_STMT_BASE:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004062 if (ident->bases && (ctx->parsed_mod->version < LYS_VERSION_1_1)) {
David Sedlákb3077192019-06-19 10:55:37 +02004063 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Identity can be derived from multiple base identities only in YANG 1.1 modules");
Radek Krejci10113652018-11-14 16:56:50 +01004064 return LY_EVALID;
4065 }
Radek Krejcifc596f92021-02-26 22:40:26 +01004066 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 +02004067 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004068 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01004069 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_IDENTITY, 0, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004070 break;
4071 default:
David Sedlákb3077192019-06-19 10:55:37 +02004072 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004073 return LY_EVALID;
4074 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004075 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004076 return ret;
4077}
4078
Michal Vaskoea5abea2018-09-18 13:10:54 +02004079/**
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004080 * @brief Parse module substatements.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004081 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004082 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004083 * @param[in,out] mod Module to write to.
4084 *
4085 * @return LY_ERR values.
4086 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004087LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004088parse_module(struct lys_yang_parser_ctx *ctx, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004089{
4090 LY_ERR ret = 0;
4091 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004092 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004093 enum ly_stmt kw, prev_kw = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004094 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Michal Vasko8dc31992021-02-22 10:30:47 +01004095 const struct lysp_submodule *dup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004096
Michal Vaskoc3781c32020-10-06 14:04:08 +02004097 mod->is_submod = 0;
4098
4099 /* module name */
Radek Krejci33090f92020-12-17 20:12:46 +01004100 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004101 INSERT_WORD_RET(ctx, buf, mod->mod->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004102
aPieceka24a2252021-05-07 10:52:31 +02004103 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004104
Radek Krejcie3846472018-10-15 15:24:51 +02004105#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004106 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 +02004107
Michal Vasko7fbc8162018-09-17 10:35:16 +02004108 switch (kw) {
4109 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004110 case LY_STMT_NAMESPACE:
4111 case LY_STMT_PREFIX:
Radek Krejcie3846472018-10-15 15:24:51 +02004112 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4113 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004114 case LY_STMT_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004115 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004116 break;
4117 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004118 case LY_STMT_INCLUDE:
4119 case LY_STMT_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004120 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004121 break;
4122 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004123 case LY_STMT_ORGANIZATION:
4124 case LY_STMT_CONTACT:
4125 case LY_STMT_DESCRIPTION:
4126 case LY_STMT_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004127 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004128 break;
4129
4130 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004131 case LY_STMT_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004132 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004133 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004134 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004135 case LY_STMT_ANYDATA:
4136 case LY_STMT_ANYXML:
4137 case LY_STMT_AUGMENT:
4138 case LY_STMT_CHOICE:
4139 case LY_STMT_CONTAINER:
4140 case LY_STMT_DEVIATION:
4141 case LY_STMT_EXTENSION:
4142 case LY_STMT_FEATURE:
4143 case LY_STMT_GROUPING:
4144 case LY_STMT_IDENTITY:
4145 case LY_STMT_LEAF:
4146 case LY_STMT_LEAF_LIST:
4147 case LY_STMT_LIST:
4148 case LY_STMT_NOTIFICATION:
4149 case LY_STMT_RPC:
4150 case LY_STMT_TYPEDEF:
4151 case LY_STMT_USES:
4152 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004153 mod_stmt = Y_MOD_BODY;
4154 break;
4155 default:
4156 /* error handled in the next switch */
4157 break;
4158 }
Radek Krejcie3846472018-10-15 15:24:51 +02004159#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004160
Radek Krejcie3846472018-10-15 15:24:51 +02004161 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004162 switch (kw) {
4163 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004164 case LY_STMT_YANG_VERSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004165 LY_CHECK_RET(parse_yangversion(ctx, &mod->version, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004166 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004167 case LY_STMT_NAMESPACE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004168 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 +02004169 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004170 case LY_STMT_PREFIX:
Radek Krejcifc596f92021-02-26 22:40:26 +01004171 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 +02004172 break;
4173
4174 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004175 case LY_STMT_INCLUDE:
Radek Krejci33090f92020-12-17 20:12:46 +01004176 LY_CHECK_RET(parse_include(ctx, mod->mod->name, &mod->includes));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004177 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004178 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004179 LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, &mod->imports));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004180 break;
4181
4182 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004183 case LY_STMT_ORGANIZATION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004184 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 +02004185 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004186 case LY_STMT_CONTACT:
Radek Krejcifc596f92021-02-26 22:40:26 +01004187 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 +02004188 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004189 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004190 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 +02004191 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004192 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004193 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 +02004194 break;
4195
4196 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004197 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004198 LY_CHECK_RET(parse_revision(ctx, &mod->revs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004199 break;
4200
4201 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004202 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004203 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "module");
Radek Krejci0f969882020-08-21 16:56:47 +02004204 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004205 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004206 LY_CHECK_RET(parse_any(ctx, kw, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004207 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004208 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004209 LY_CHECK_RET(parse_choice(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004210 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004211 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004212 LY_CHECK_RET(parse_container(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004213 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004214 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004215 LY_CHECK_RET(parse_leaf(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004216 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004217 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004218 LY_CHECK_RET(parse_leaflist(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004219 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004220 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004221 LY_CHECK_RET(parse_list(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004222 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004223 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004224 LY_CHECK_RET(parse_uses(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004225 break;
4226
Radek Krejcid6b76452019-09-03 17:03:03 +02004227 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004228 LY_CHECK_RET(parse_augment(ctx, NULL, &mod->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004229 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004230 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004231 LY_CHECK_RET(parse_deviation(ctx, &mod->deviations));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004232 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004233 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004234 LY_CHECK_RET(parse_extension(ctx, &mod->extensions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004235 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004236 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004237 LY_CHECK_RET(parse_feature(ctx, &mod->features));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004238 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004239 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004240 LY_CHECK_RET(parse_grouping(ctx, NULL, &mod->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004241 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004242 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004243 LY_CHECK_RET(parse_identity(ctx, &mod->identities));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004244 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004245 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004246 LY_CHECK_RET(parse_notif(ctx, NULL, &mod->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004247 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004248 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004249 LY_CHECK_RET(parse_action(ctx, NULL, &mod->rpcs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004250 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004251 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004252 LY_CHECK_RET(parse_typedef(ctx, NULL, &mod->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004253 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004254 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01004255 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MODULE, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004256 break;
4257
4258 default:
David Sedlákb3077192019-06-19 10:55:37 +02004259 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004260 return LY_EVALID;
4261 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004262 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004263 LY_CHECK_RET(ret);
Radek Krejcie86bf772018-12-14 11:39:53 +01004264
Radek Krejci6d6556c2018-11-08 09:37:45 +01004265checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004266 /* mandatory substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004267 if (!mod->mod->ns) {
David Sedlákb3077192019-06-19 10:55:37 +02004268 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004269 return LY_EVALID;
4270 } else if (!mod->mod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004271 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004272 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004273 }
4274
Radek Krejcie9e987e2018-10-31 12:50:27 +01004275 /* submodules share the namespace with the module names, so there must not be
4276 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01004277 dup = ly_ctx_get_submodule_latest(PARSER_CTX(ctx), mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004278 if (dup) {
Radek Krejci854e1552020-12-21 15:05:23 +01004279 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004280 return LY_EVALID;
4281 }
4282
4283 return ret;
4284}
4285
4286/**
4287 * @brief Parse submodule substatements.
4288 *
4289 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004290 * @param[out] submod Parsed submodule structure.
4291 *
4292 * @return LY_ERR values.
4293 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004294LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004295parse_submodule(struct lys_yang_parser_ctx *ctx, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004296{
4297 LY_ERR ret = 0;
4298 char *buf, *word;
4299 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004300 enum ly_stmt kw, prev_kw = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004301 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Michal Vasko8dc31992021-02-22 10:30:47 +01004302 const struct lysp_submodule *dup;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004303
Michal Vaskoc3781c32020-10-06 14:04:08 +02004304 submod->is_submod = 1;
4305
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004306 /* submodule name */
Radek Krejci33090f92020-12-17 20:12:46 +01004307 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004308 INSERT_WORD_RET(ctx, buf, submod->name, word, word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004309
aPieceka24a2252021-05-07 10:52:31 +02004310 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks, return ret) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004311
4312#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004313 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 +01004314
4315 switch (kw) {
4316 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004317 case LY_STMT_BELONGS_TO:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004318 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4319 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004320 case LY_STMT_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004321 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4322 break;
4323 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004324 case LY_STMT_INCLUDE:
4325 case LY_STMT_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004326 CHECK_ORDER(Y_MOD_LINKAGE);
4327 break;
4328 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004329 case LY_STMT_ORGANIZATION:
4330 case LY_STMT_CONTACT:
4331 case LY_STMT_DESCRIPTION:
4332 case LY_STMT_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004333 CHECK_ORDER(Y_MOD_META);
4334 break;
4335
4336 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004337 case LY_STMT_REVISION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004338 CHECK_ORDER(Y_MOD_REVISION);
4339 break;
4340 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004341 case LY_STMT_ANYDATA:
4342 case LY_STMT_ANYXML:
4343 case LY_STMT_AUGMENT:
4344 case LY_STMT_CHOICE:
4345 case LY_STMT_CONTAINER:
4346 case LY_STMT_DEVIATION:
4347 case LY_STMT_EXTENSION:
4348 case LY_STMT_FEATURE:
4349 case LY_STMT_GROUPING:
4350 case LY_STMT_IDENTITY:
4351 case LY_STMT_LEAF:
4352 case LY_STMT_LEAF_LIST:
4353 case LY_STMT_LIST:
4354 case LY_STMT_NOTIFICATION:
4355 case LY_STMT_RPC:
4356 case LY_STMT_TYPEDEF:
4357 case LY_STMT_USES:
4358 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004359 mod_stmt = Y_MOD_BODY;
4360 break;
4361 default:
4362 /* error handled in the next switch */
4363 break;
4364 }
4365#undef CHECK_ORDER
4366
4367 prev_kw = kw;
4368 switch (kw) {
4369 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004370 case LY_STMT_YANG_VERSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004371 LY_CHECK_RET(parse_yangversion(ctx, &submod->version, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004372 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004373 case LY_STMT_BELONGS_TO:
Radek Krejci33090f92020-12-17 20:12:46 +01004374 LY_CHECK_RET(parse_belongsto(ctx, &submod->prefix, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004375 break;
4376
4377 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004378 case LY_STMT_INCLUDE:
Radek Krejcieeee95c2021-01-19 10:57:22 +01004379 if (submod->version == LYS_VERSION_1_1) {
4380 LOGWRN(PARSER_CTX(ctx), "YANG version 1.1 expects all includes in main module, includes in submodules (%s) are not necessary.",
4381 submod->name);
4382 }
Radek Krejci33090f92020-12-17 20:12:46 +01004383 LY_CHECK_RET(parse_include(ctx, submod->name, &submod->includes));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004384 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004385 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004386 LY_CHECK_RET(parse_import(ctx, submod->prefix, &submod->imports));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004387 break;
4388
4389 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004390 case LY_STMT_ORGANIZATION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004391 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 +01004392 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004393 case LY_STMT_CONTACT:
Radek Krejcifc596f92021-02-26 22:40:26 +01004394 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 +01004395 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004396 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004397 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 +01004398 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004399 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004400 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 +01004401 break;
4402
4403 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004404 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004405 LY_CHECK_RET(parse_revision(ctx, &submod->revs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004406 break;
4407
4408 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004409 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004410 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "submodule");
Radek Krejci0f969882020-08-21 16:56:47 +02004411 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004412 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004413 LY_CHECK_RET(parse_any(ctx, kw, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004414 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004415 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004416 LY_CHECK_RET(parse_choice(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004417 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004418 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004419 LY_CHECK_RET(parse_container(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004420 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004421 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004422 LY_CHECK_RET(parse_leaf(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004423 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004424 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004425 LY_CHECK_RET(parse_leaflist(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004426 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004427 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004428 LY_CHECK_RET(parse_list(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004429 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004430 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004431 LY_CHECK_RET(parse_uses(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004432 break;
4433
Radek Krejcid6b76452019-09-03 17:03:03 +02004434 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004435 LY_CHECK_RET(parse_augment(ctx, NULL, &submod->augments));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004436 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004437 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004438 LY_CHECK_RET(parse_deviation(ctx, &submod->deviations));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004439 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004440 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004441 LY_CHECK_RET(parse_extension(ctx, &submod->extensions));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004442 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004443 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004444 LY_CHECK_RET(parse_feature(ctx, &submod->features));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004445 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004446 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004447 LY_CHECK_RET(parse_grouping(ctx, NULL, &submod->groupings));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004448 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004449 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004450 LY_CHECK_RET(parse_identity(ctx, &submod->identities));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004451 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004452 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004453 LY_CHECK_RET(parse_notif(ctx, NULL, &submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004454 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004455 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004456 LY_CHECK_RET(parse_action(ctx, NULL, &submod->rpcs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004457 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004458 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004459 LY_CHECK_RET(parse_typedef(ctx, NULL, &submod->typedefs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004460 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004461 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01004462 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_SUBMODULE, 0, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004463 break;
4464
4465 default:
David Sedlákb3077192019-06-19 10:55:37 +02004466 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004467 return LY_EVALID;
4468 }
4469 }
4470 LY_CHECK_RET(ret);
4471
4472checks:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004473 /* mandatory substatements */
Michal Vaskoc3781c32020-10-06 14:04:08 +02004474 if (!submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004475 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004476 return LY_EVALID;
4477 }
4478
4479 /* submodules share the namespace with the module names, so there must not be
4480 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01004481 dup = ly_ctx_get_submodule_latest(PARSER_CTX(ctx), submod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02004482 /* main modules may have different revisions */
4483 if (dup && strcmp(dup->mod->name, submod->mod->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01004484 LOGVAL_PARSER(ctx, LY_VCODE_NAME_COL, "submodules", dup->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +01004485 return LY_EVALID;
4486 }
4487
Michal Vasko7fbc8162018-09-17 10:35:16 +02004488 return ret;
4489}
4490
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004491/**
4492 * @brief Skip any redundant characters, namely whitespaces and comments.
4493 *
4494 * @param[in] ctx Yang parser context.
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004495 * @return LY_SUCCESS on success.
4496 * @return LY_EVALID on invalid comment.
4497 */
4498static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004499skip_redundant_chars(struct lys_yang_parser_ctx *ctx)
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004500{
4501 /* read some trailing spaces, new lines, or comments */
Radek Krejci33090f92020-12-17 20:12:46 +01004502 while (ctx->in->current[0]) {
4503 if (!strncmp(ctx->in->current, "//", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004504 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004505 ly_in_skip(ctx->in, 2);
4506 LY_CHECK_RET(skip_comment(ctx, 1));
4507 } else if (!strncmp(ctx->in->current, "/*", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004508 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004509 ly_in_skip(ctx->in, 2);
4510 LY_CHECK_RET(skip_comment(ctx, 2));
4511 } else if (isspace(ctx->in->current[0])) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004512 /* whitespace */
Radek Krejcidd713ce2021-01-04 23:12:12 +01004513 if (ctx->in->current[0] == '\n') {
4514 LY_IN_NEW_LINE(ctx->in);
4515 }
Radek Krejci33090f92020-12-17 20:12:46 +01004516 ly_in_skip(ctx->in, 1);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004517 } else {
4518 break;
4519 }
4520 }
4521
4522 return LY_SUCCESS;
4523}
4524
Radek Krejcid4557c62018-09-17 11:42:09 +02004525LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01004526yang_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 +02004527 struct ly_in *in, struct lysp_submodule **submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004528{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004529 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004530 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004531 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004532 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004533 struct lysp_submodule *mod_p = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004534
David Sedlák1b623122019-08-05 15:27:49 +02004535 /* create context */
4536 *context = calloc(1, sizeof **context);
4537 LY_CHECK_ERR_RET(!(*context), LOGMEM(ly_ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004538 (*context)->format = LYS_IN_YANG;
Michal Vasko405cc9e2020-12-01 12:01:27 +01004539 (*context)->unres = main_ctx->unres;
Radek Krejci33090f92020-12-17 20:12:46 +01004540 (*context)->in = in;
David Sedlák1b623122019-08-05 15:27:49 +02004541
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004542 mod_p = calloc(1, sizeof *mod_p);
4543 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ly_ctx); ret = LY_EMEM, cleanup);
4544 mod_p->mod = main_ctx->parsed_mod->mod;
4545 mod_p->parsing = 1;
4546 (*context)->parsed_mod = (struct lysp_module *)mod_p;
4547
Radek Krejciddace2c2021-01-08 11:30:56 +01004548 LOG_LOCINIT(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004549
David Sedlák1b623122019-08-05 15:27:49 +02004550 /* map the typedefs and groupings list from main context to the submodule's context */
4551 memcpy(&(*context)->tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
4552 memcpy(&(*context)->grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes);
4553
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004554 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004555 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004556 LY_CHECK_GOTO(ret, cleanup);
4557
Michal Vasko7fbc8162018-09-17 10:35:16 +02004558 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004559 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004560 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004561
Radek Krejcid6b76452019-09-03 17:03:03 +02004562 if (kw == LY_STMT_MODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004563 LOGERR(ly_ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004564 ret = LY_EINVAL;
4565 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004566 } else if (kw != LY_STMT_SUBMODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004567 LOGVAL_PARSER(*context, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004568 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004569 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004570 }
4571
Michal Vasko7fbc8162018-09-17 10:35:16 +02004572 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004573 ret = parse_submodule(*context, mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004574 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004575
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004576 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004577 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004578 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004579 if (in->current[0]) {
4580 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_SUBMOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004581 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004582 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004583 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004584
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004585 mod_p->parsing = 0;
4586 *submod = mod_p;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004587
Radek Krejcibbe09a92018-11-08 09:36:54 +01004588cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01004589 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004590 if (ret) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004591 lysp_module_free((struct lysp_module *)mod_p);
Michal Vaskob36053d2020-03-26 15:49:30 +01004592 yang_parser_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004593 *context = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004594 }
4595
4596 return ret;
4597}
4598
4599LY_ERR
Michal Vasko405cc9e2020-12-01 12:01:27 +01004600yang_parse_module(struct lys_yang_parser_ctx **context, struct ly_in *in, struct lys_module *mod, struct lys_glob_unres *unres)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004601{
4602 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004603 char *word;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004604 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004605 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004606 struct lysp_module *mod_p = NULL;
4607
David Sedlák1b623122019-08-05 15:27:49 +02004608 /* create context */
4609 *context = calloc(1, sizeof **context);
4610 LY_CHECK_ERR_RET(!(*context), LOGMEM(mod->ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004611 (*context)->format = LYS_IN_YANG;
Michal Vasko405cc9e2020-12-01 12:01:27 +01004612 (*context)->unres = unres;
Radek Krejci33090f92020-12-17 20:12:46 +01004613 (*context)->in = in;
David Sedlák1b623122019-08-05 15:27:49 +02004614
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004615 mod_p = calloc(1, sizeof *mod_p);
4616 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(mod->ctx), cleanup);
4617 mod_p->mod = mod;
4618 mod_p->parsing = 1;
4619 (*context)->parsed_mod = mod_p;
4620
Radek Krejciddace2c2021-01-08 11:30:56 +01004621 LOG_LOCINIT(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004622
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004623 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004624 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004625 LY_CHECK_GOTO(ret, cleanup);
4626
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004627 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004628 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004629 LY_CHECK_GOTO(ret, cleanup);
4630
Radek Krejcid6b76452019-09-03 17:03:03 +02004631 if (kw == LY_STMT_SUBMODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004632 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 +01004633 ret = LY_EINVAL;
4634 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004635 } else if (kw != LY_STMT_MODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004636 LOGVAL_PARSER((*context), LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004637 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004638 goto cleanup;
4639 }
4640
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004641 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004642 ret = parse_module(*context, mod_p);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004643 LY_CHECK_GOTO(ret, cleanup);
4644
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004645 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004646 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004647 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004648 if (in->current[0]) {
4649 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_MOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004650 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004651 goto cleanup;
4652 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004653
4654 mod_p->parsing = 0;
4655 mod->parsed = mod_p;
4656
4657cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01004658 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004659 if (ret) {
4660 lysp_module_free(mod_p);
Michal Vaskob36053d2020-03-26 15:49:30 +01004661 yang_parser_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004662 *context = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004663 }
4664
Michal Vasko7fbc8162018-09-17 10:35:16 +02004665 return ret;
4666}