blob: cae539814873522f81255112940ff44b548a4539 [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"
Michal Vaskofc2cd072021-02-24 13:17:17 +010033#include "tree_data_internal.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 Krejciceaf2122019-01-02 15:03:26 +010037/**
38 * @brief Insert WORD into the libyang context's dictionary and store as TARGET.
39 * @param[in] CTX yang parser context to access libyang context.
40 * @param[in] BUF buffer in case the word is not a constant and can be inserted directly (zero-copy)
41 * @param[out] TARGET variable where to store the pointer to the inserted value.
42 * @param[in] WORD string to store.
43 * @param[in] LEN length of the string in WORD to store.
44 */
Radek Krejci011e4aa2020-09-04 15:22:31 +020045#define INSERT_WORD_RET(CTX, BUF, TARGET, WORD, LEN) \
Michal Vasko5d24f6c2020-10-13 13:49:06 +020046 if (BUF) {LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(CTX), WORD, &(TARGET)));}\
47 else {LY_CHECK_RET(lydict_insert(PARSER_CTX(CTX), LEN ? WORD : "", LEN, &(TARGET)));}
Radek Krejci44ceedc2018-10-02 15:54:31 +020048
Radek Krejciceaf2122019-01-02 15:03:26 +010049/**
Michal Vasko63f3d842020-07-08 10:10:14 +020050 * @brief Read from the IN structure COUNT items. Also updates the indent value in yang parser context
Radek Krejciceaf2122019-01-02 15:03:26 +010051 * @param[in] CTX yang parser context to update its indent value.
Radek Krejciceaf2122019-01-02 15:03:26 +010052 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
53 */
Radek Krejcid54412f2020-12-17 20:25:35 +010054#define MOVE_INPUT(CTX, COUNT) ly_in_skip((CTX)->in, COUNT);(CTX)->indent+=COUNT
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020055
Michal Vaskoea5abea2018-09-18 13:10:54 +020056/**
57 * @brief Loop through all substatements providing, return if there are none.
58 *
Radek Krejci44ceedc2018-10-02 15:54:31 +020059 * @param[in] CTX yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +020060 * @param[out] KW YANG keyword read.
61 * @param[out] WORD Pointer to the keyword itself.
62 * @param[out] WORD_LEN Length of the keyword.
63 * @param[out] ERR Variable for error storing.
64 *
65 * @return In case there are no substatements or a fatal error encountered.
66 */
Radek Krejci33090f92020-12-17 20:12:46 +010067#define YANG_READ_SUBSTMT_FOR(CTX, KW, WORD, WORD_LEN, ERR, CHECKGOTO) \
68 LY_CHECK_RET(get_keyword(CTX, &KW, &WORD, &WORD_LEN)); \
Radek Krejcid6b76452019-09-03 17:03:03 +020069 if (KW == LY_STMT_SYNTAX_SEMICOLON) { \
Radek Krejci6d6556c2018-11-08 09:37:45 +010070 CHECKGOTO; \
Radek Krejci6d9b9b52018-11-02 12:43:39 +010071 return LY_SUCCESS; \
Michal Vasko7fbc8162018-09-17 10:35:16 +020072 } \
Radek Krejcid6b76452019-09-03 17:03:03 +020073 if (KW != LY_STMT_SYNTAX_LEFT_BRACE) { \
David Sedlákb3077192019-06-19 10:55:37 +020074 LOGVAL_PARSER(CTX, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\", expected \";\" or \"{\".", ly_stmt2str(KW)); \
Michal Vasko7fbc8162018-09-17 10:35:16 +020075 return LY_EVALID; \
76 } \
Radek Krejci33090f92020-12-17 20:12:46 +010077 for (ERR = get_keyword(CTX, &KW, &WORD, &WORD_LEN); \
Radek Krejcid6b76452019-09-03 17:03:03 +020078 !ERR && (KW != LY_STMT_SYNTAX_RIGHT_BRACE); \
Radek Krejci33090f92020-12-17 20:12:46 +010079 ERR = get_keyword(CTX, &KW, &WORD, &WORD_LEN))
Michal Vasko7fbc8162018-09-17 10:35:16 +020080
Radek Krejci33090f92020-12-17 20:12:46 +010081LY_ERR parse_container(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent,
Radek Krejci0f969882020-08-21 16:56:47 +020082 struct lysp_node **siblings);
Radek Krejci33090f92020-12-17 20:12:46 +010083LY_ERR parse_uses(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
84LY_ERR parse_choice(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
85LY_ERR parse_case(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
86LY_ERR parse_list(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
Radek Krejci2a9fc652021-01-22 17:44:34 +010087LY_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 +020088
Michal Vaskoea5abea2018-09-18 13:10:54 +020089/**
90 * @brief Add another character to dynamic buffer, a low-level function.
91 *
Radek Krejci44ceedc2018-10-02 15:54:31 +020092 * Enlarge if needed. Updates \p input as well as \p buf_used.
Michal Vaskoea5abea2018-09-18 13:10:54 +020093 *
Radek Krejci404251e2018-10-09 12:06:44 +020094 * @param[in] ctx libyang context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +020095 * @param[in,out] in Input structure.
Radek Krejci44ceedc2018-10-02 15:54:31 +020096 * @param[in] len Number of bytes to get from the input string and copy into the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +020097 * @param[in,out] buf Buffer to use, can be moved by realloc().
98 * @param[in,out] buf_len Current size of the buffer.
Radek Krejci44ceedc2018-10-02 15:54:31 +020099 * @param[in,out] buf_used Currently used characters of the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200100 *
101 * @return LY_ERR values.
102 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200103LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200104buf_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 +0200105{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100106#define BUF_STEP 16;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200107 if (*buf_len <= (*buf_used) + len) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100108 *buf_len += BUF_STEP;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200109 *buf = ly_realloc(*buf, *buf_len);
110 LY_CHECK_ERR_RET(!*buf, LOGMEM(ctx), LY_EMEM);
111 }
Radek Krejcic0917392019-04-10 13:04:04 +0200112 if (*buf_used) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200113 ly_in_read(in, &(*buf)[*buf_used], len);
Radek Krejcic0917392019-04-10 13:04:04 +0200114 } else {
Michal Vasko63f3d842020-07-08 10:10:14 +0200115 ly_in_read(in, *buf, len);
Radek Krejcic0917392019-04-10 13:04:04 +0200116 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200117
Radek Krejci44ceedc2018-10-02 15:54:31 +0200118 (*buf_used) += len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200119 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100120#undef BUF_STEP
Michal Vasko7fbc8162018-09-17 10:35:16 +0200121}
122
Michal Vaskoea5abea2018-09-18 13:10:54 +0200123/**
Radek Krejci44ceedc2018-10-02 15:54:31 +0200124 * @brief Store a single UTF8 character. It depends whether in a dynamically-allocated buffer or just as a pointer to the data.
125 *
126 * @param[in] ctx yang parser context for logging.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200127 * @param[in] arg Type of the input string to select method of checking character validity.
128 * @param[in,out] word_p Word pointer. If buffer (\p word_b) was not yet needed, it is just a pointer to the first
Michal Vaskoea5abea2018-09-18 13:10:54 +0200129 * stored character. If buffer was needed (\p word_b is non-NULL or \p need_buf is set), it is pointing to the buffer.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200130 * @param[in,out] word_len Current length of the word pointed to by \p word_p.
131 * @param[in,out] word_b Word buffer. Is kept NULL as long as it is not requested (word is a substring of the data).
Michal Vaskoea5abea2018-09-18 13:10:54 +0200132 * @param[in,out] buf_len Current length of \p word_b.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200133 * @param[in] need_buf Flag if the dynamically allocated buffer is required.
David Sedlák40bb13b2019-07-10 14:34:18 +0200134 * @param[in,out] prefix Storage for internally used flag in case of possible prefixed identifiers:
135 * 0 - colon not yet found (no prefix)
136 * 1 - \p c is the colon character
137 * 2 - prefix already processed, now processing the identifier
Michal Vaskoea5abea2018-09-18 13:10:54 +0200138 *
139 * @return LY_ERR values.
140 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200141LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100142buf_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 +0200143 char **word_b, size_t *buf_len, ly_bool need_buf, uint8_t *prefix)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200144{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200145 uint32_t c;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200146 size_t len;
147
Radek Krejcif29b7c32019-04-09 16:17:49 +0200148 /* check valid combination of input paremeters - if need_buf specified, word_b must be provided */
149 assert(!need_buf || (need_buf && word_b));
150
Radek Krejci44ceedc2018-10-02 15:54:31 +0200151 /* get UTF8 code point (and number of bytes coding the character) */
Radek Krejci33090f92020-12-17 20:12:46 +0100152 LY_CHECK_ERR_RET(ly_getutf8(&ctx->in->current, &c, &len),
153 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[-len]), LY_EVALID);
154 ctx->in->current -= len;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200155 if (c == '\n') {
156 ctx->indent = 0;
Radek Krejcidd713ce2021-01-04 23:12:12 +0100157 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200158 } else {
159 /* note - even the multibyte character is count as 1 */
160 ++ctx->indent;
161 }
162
Radek Krejci44ceedc2018-10-02 15:54:31 +0200163 /* check character validity */
164 switch (arg) {
165 case Y_IDENTIF_ARG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100166 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !(*word_len), NULL));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200167 break;
168 case Y_PREF_IDENTIF_ARG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100169 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !(*word_len), prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200170 break;
171 case Y_STR_ARG:
172 case Y_MAYBE_STR_ARG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100173 LY_CHECK_RET(lysp_check_stringchar((struct lys_parser_ctx *)ctx, c));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200174 break;
175 }
176
Michal Vasko7fbc8162018-09-17 10:35:16 +0200177 if (word_b && *word_b) {
178 /* add another character into buffer */
Radek Krejci33090f92020-12-17 20:12:46 +0100179 if (buf_add_char(PARSER_CTX(ctx), ctx->in, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200180 return LY_EMEM;
181 }
182
183 /* in case of realloc */
184 *word_p = *word_b;
Radek Krejcif29b7c32019-04-09 16:17:49 +0200185 } else if (word_b && need_buf) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200186 /* first time we need a buffer, copy everything read up to now */
187 if (*word_len) {
188 *word_b = malloc(*word_len);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200189 LY_CHECK_ERR_RET(!*word_b, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200190 *buf_len = *word_len;
191 memcpy(*word_b, *word_p, *word_len);
192 }
193
194 /* add this new character into buffer */
Radek Krejci33090f92020-12-17 20:12:46 +0100195 if (buf_add_char(PARSER_CTX(ctx), ctx->in, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200196 return LY_EMEM;
197 }
198
199 /* in case of realloc */
200 *word_p = *word_b;
201 } else {
202 /* just remember the first character pointer */
203 if (!*word_p) {
Radek Krejci33090f92020-12-17 20:12:46 +0100204 *word_p = (char *)ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200205 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200206 /* ... and update the word's length */
207 (*word_len) += len;
Radek Krejci33090f92020-12-17 20:12:46 +0100208 ly_in_skip(ctx->in, len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200209 }
210
211 return LY_SUCCESS;
212}
213
Michal Vaskoea5abea2018-09-18 13:10:54 +0200214/**
215 * @brief Skip YANG comment in data.
216 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200217 * @param[in] ctx yang parser context for logging.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200218 * @param[in] comment Type of the comment to process:
219 * 1 for a one-line comment,
220 * 2 for a block comment.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200221 * @return LY_ERR values.
222 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200223LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100224skip_comment(struct lys_yang_parser_ctx *ctx, uint8_t comment)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200225{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100226 /* internal statuses: */
227#define COMMENT_NO 0 /* comment ended */
228#define COMMENT_LINE 1 /* in line comment */
229#define COMMENT_BLOCK 2 /* in block comment */
230#define COMMENT_BLOCK_END 3 /* in block comment with last read character '*' */
231
Radek Krejci33090f92020-12-17 20:12:46 +0100232 while (ctx->in->current[0] && comment) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200233 switch (comment) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100234 case COMMENT_LINE:
Radek Krejci33090f92020-12-17 20:12:46 +0100235 if (ctx->in->current[0] == '\n') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100236 comment = COMMENT_NO;
Radek Krejcid54412f2020-12-17 20:25:35 +0100237 LY_IN_NEW_LINE(ctx->in);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200238 }
239 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100240 case COMMENT_BLOCK:
Radek Krejci33090f92020-12-17 20:12:46 +0100241 if (ctx->in->current[0] == '*') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100242 comment = COMMENT_BLOCK_END;
Radek Krejci33090f92020-12-17 20:12:46 +0100243 } else if (ctx->in->current[0] == '\n') {
Radek Krejcid54412f2020-12-17 20:25:35 +0100244 LY_IN_NEW_LINE(ctx->in);
Radek Krejci15c80ca2018-10-09 11:01:31 +0200245 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200246 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100247 case COMMENT_BLOCK_END:
Radek Krejci33090f92020-12-17 20:12:46 +0100248 if (ctx->in->current[0] == '/') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100249 comment = COMMENT_NO;
Radek Krejci33090f92020-12-17 20:12:46 +0100250 } else if (ctx->in->current[0] != '*') {
251 if (ctx->in->current[0] == '\n') {
Radek Krejcid54412f2020-12-17 20:25:35 +0100252 LY_IN_NEW_LINE(ctx->in);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200253 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100254 comment = COMMENT_BLOCK;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200255 }
256 break;
257 default:
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200258 LOGINT_RET(PARSER_CTX(ctx));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200259 }
260
Radek Krejci33090f92020-12-17 20:12:46 +0100261 if (ctx->in->current[0] == '\n') {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200262 ctx->indent = 0;
263 } else {
264 ++ctx->indent;
265 }
Radek Krejci33090f92020-12-17 20:12:46 +0100266 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200267 }
268
Radek Krejci33090f92020-12-17 20:12:46 +0100269 if (!ctx->in->current[0] && (comment >= COMMENT_BLOCK)) {
David Sedlákb3077192019-06-19 10:55:37 +0200270 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Unexpected end-of-input, non-terminated comment.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200271 return LY_EVALID;
272 }
273
274 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100275
276#undef COMMENT_NO
277#undef COMMENT_LINE
278#undef COMMENT_BLOCK
279#undef COMMENT_BLOCK_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200280}
281
Michal Vaskoea5abea2018-09-18 13:10:54 +0200282/**
283 * @brief Read a quoted string from data.
284 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200285 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200286 * @param[in] arg Type of YANG keyword argument expected.
287 * @param[out] word_p Pointer to the read quoted string.
288 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read quoted string. If not needed,
289 * set to NULL. Otherwise equal to \p word_p.
290 * @param[out] word_len Length of the read quoted string.
291 * @param[out] buf_len Length of the dynamically-allocated buffer \p word_b.
292 * @param[in] indent Current indent (number of YANG spaces). Needed for correct multi-line string
293 * indenation in the final quoted string.
294 *
295 * @return LY_ERR values.
296 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200297static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100298read_qstring(struct lys_yang_parser_ctx *ctx, enum yang_arg arg, char **word_p, char **word_b,
Radek Krejci0f969882020-08-21 16:56:47 +0200299 size_t *word_len, size_t *buf_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200300{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100301 /* string parsing status: */
302#define STRING_ENDED 0 /* string ended */
303#define STRING_SINGLE_QUOTED 1 /* string with ' */
304#define STRING_DOUBLE_QUOTED 2 /* string with " */
305#define STRING_DOUBLE_QUOTED_ESCAPED 3 /* string with " with last character \ */
306#define STRING_PAUSED_NEXTSTRING 4 /* string finished, now skipping whitespaces looking for + */
307#define STRING_PAUSED_CONTINUE 5 /* string continues after +, skipping whitespaces */
308
Radek Krejci1deb5be2020-08-26 16:43:36 +0200309 uint8_t string;
310 uint64_t block_indent = 0, current_indent = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200311 ly_bool need_buf = 0;
312 uint8_t prefix = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200313 const char *c;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200314 uint64_t trailing_ws = 0; /* current number of stored trailing whitespace characters */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200315
Radek Krejci33090f92020-12-17 20:12:46 +0100316 if (ctx->in->current[0] == '\"') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100317 string = STRING_DOUBLE_QUOTED;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200318 current_indent = block_indent = ctx->indent + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200319 } else {
Radek Krejci33090f92020-12-17 20:12:46 +0100320 assert(ctx->in->current[0] == '\'');
Radek Krejcif13b87b2020-12-01 22:02:17 +0100321 string = STRING_SINGLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200322 }
Radek Krejci33090f92020-12-17 20:12:46 +0100323 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200324
Radek Krejci33090f92020-12-17 20:12:46 +0100325 while (ctx->in->current[0] && string) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200326 switch (string) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100327 case STRING_SINGLE_QUOTED:
Radek Krejci33090f92020-12-17 20:12:46 +0100328 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200329 case '\'':
330 /* string may be finished, but check for + */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100331 string = STRING_PAUSED_NEXTSTRING;
Radek Krejci33090f92020-12-17 20:12:46 +0100332 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200333 break;
334 default:
335 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100336 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 +0200337 break;
338 }
339 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100340 case STRING_DOUBLE_QUOTED:
Radek Krejci33090f92020-12-17 20:12:46 +0100341 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200342 case '\"':
343 /* string may be finished, but check for + */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100344 string = STRING_PAUSED_NEXTSTRING;
Radek Krejci33090f92020-12-17 20:12:46 +0100345 MOVE_INPUT(ctx, 1);
Radek Krejciff13cd12019-10-25 15:34:24 +0200346 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200347 break;
348 case '\\':
349 /* special character following */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100350 string = STRING_DOUBLE_QUOTED_ESCAPED;
Radek Krejciff13cd12019-10-25 15:34:24 +0200351
352 /* the backslash sequence is substituted, so we will need a buffer to store the result */
353 need_buf = 1;
354
355 /* move forward to the escaped character */
Radek Krejci33090f92020-12-17 20:12:46 +0100356 ++ctx->in->current;
Radek Krejciff13cd12019-10-25 15:34:24 +0200357
358 /* note that the trailing whitespaces are supposed to be trimmed before substitution of
359 * backslash-escaped characters (RFC 7950, 6.1.3), so we have to zero the trailing whitespaces counter */
360 trailing_ws = 0;
361
362 /* since the backslash-escaped character is handled as first non-whitespace character, stop eating indentation */
363 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200364 break;
365 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200366 if (current_indent < block_indent) {
367 ++current_indent;
Radek Krejci33090f92020-12-17 20:12:46 +0100368 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200369 } else {
Michal Vasko90edde42019-11-25 15:25:07 +0100370 /* check and store whitespace character */
Radek Krejci33090f92020-12-17 20:12:46 +0100371 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 +0100372 trailing_ws++;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200373 }
374 break;
375 case '\t':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200376 if (current_indent < block_indent) {
377 assert(need_buf);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100378 current_indent += Y_TAB_SPACES;
379 ctx->indent += Y_TAB_SPACES;
Michal Vaskod989ba02020-08-24 10:59:24 +0200380 for ( ; current_indent > block_indent; --current_indent, --ctx->indent) {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200381 /* store leftover spaces from the tab */
Radek Krejci33090f92020-12-17 20:12:46 +0100382 c = ctx->in->current;
383 ctx->in->current = " ";
384 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
385 ctx->in->current = c;
Michal Vasko90edde42019-11-25 15:25:07 +0100386 trailing_ws++;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200387 }
Radek Krejci33090f92020-12-17 20:12:46 +0100388 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200389 } else {
Michal Vasko90edde42019-11-25 15:25:07 +0100390 /* check and store whitespace character */
Radek Krejci33090f92020-12-17 20:12:46 +0100391 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 +0100392 trailing_ws++;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200393 /* additional characters for indentation - only 1 was count in buf_store_char */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100394 ctx->indent += Y_TAB_SPACES - 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200395 }
396 break;
397 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200398 if (block_indent) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200399 /* we will be removing the indents so we need our own buffer */
400 need_buf = 1;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200401
402 /* remove trailing tabs and spaces */
Radek Krejciff13cd12019-10-25 15:34:24 +0200403 (*word_len) = *word_len - trailing_ws;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200404
Radek Krejciff13cd12019-10-25 15:34:24 +0200405 /* restart indentation */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200406 current_indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200407 }
408
409 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100410 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 +0200411
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200412 /* reset context indentation counter for possible string after this one */
413 ctx->indent = 0;
Radek Krejciff13cd12019-10-25 15:34:24 +0200414 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200415 break;
416 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200417 /* first non-whitespace character, stop eating indentation */
418 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200419
420 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100421 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 +0200422 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200423 break;
424 }
425 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100426 case STRING_DOUBLE_QUOTED_ESCAPED:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200427 /* string encoded characters */
Radek Krejci33090f92020-12-17 20:12:46 +0100428 c = ctx->in->current;
429 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200430 case 'n':
Radek Krejci33090f92020-12-17 20:12:46 +0100431 ctx->in->current = "\n";
Radek Krejcidd713ce2021-01-04 23:12:12 +0100432 /* fix false newline count in buf_store_char() */
433 ctx->in->line--;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200434 break;
435 case 't':
Radek Krejci33090f92020-12-17 20:12:46 +0100436 ctx->in->current = "\t";
Michal Vasko7fbc8162018-09-17 10:35:16 +0200437 break;
438 case '\"':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200439 case '\\':
Michal Vasko63f3d842020-07-08 10:10:14 +0200440 /* ok as is */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200441 break;
442 default:
Michal Vasko63f3d842020-07-08 10:10:14 +0200443 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Double-quoted string unknown special character '\\%c'.",
Radek Krejci33090f92020-12-17 20:12:46 +0100444 ctx->in->current[0]);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200445 return LY_EVALID;
446 }
447
448 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100449 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 +0200450
Radek Krejcif13b87b2020-12-01 22:02:17 +0100451 string = STRING_DOUBLE_QUOTED;
Radek Krejci33090f92020-12-17 20:12:46 +0100452 ctx->in->current = c + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200453 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100454 case STRING_PAUSED_NEXTSTRING:
Radek Krejci33090f92020-12-17 20:12:46 +0100455 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200456 case '+':
457 /* string continues */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100458 string = STRING_PAUSED_CONTINUE;
Radek Krejciefd22f62018-09-27 11:47:58 +0200459 need_buf = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200460 break;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200461 case '\n':
Radek Krejcidd713ce2021-01-04 23:12:12 +0100462 LY_IN_NEW_LINE(ctx->in);
Radek Krejcicb3e6472021-01-06 08:19:01 +0100463 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200464 case ' ':
465 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200466 /* just skip */
467 break;
468 default:
469 /* string is finished */
470 goto string_end;
471 }
Radek Krejci33090f92020-12-17 20:12:46 +0100472 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200473 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100474 case STRING_PAUSED_CONTINUE:
Radek Krejci33090f92020-12-17 20:12:46 +0100475 switch (ctx->in->current[0]) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200476 case '\n':
Radek Krejcidd713ce2021-01-04 23:12:12 +0100477 LY_IN_NEW_LINE(ctx->in);
Radek Krejcicb3e6472021-01-06 08:19:01 +0100478 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200479 case ' ':
480 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200481 /* skip */
482 break;
483 case '\'':
Radek Krejcif13b87b2020-12-01 22:02:17 +0100484 string = STRING_SINGLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200485 break;
486 case '\"':
Radek Krejcif13b87b2020-12-01 22:02:17 +0100487 string = STRING_DOUBLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200488 break;
489 default:
490 /* it must be quoted again */
David Sedlákb3077192019-06-19 10:55:37 +0200491 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Both string parts divided by '+' must be quoted.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200492 return LY_EVALID;
493 }
Radek Krejci33090f92020-12-17 20:12:46 +0100494 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200495 break;
496 default:
497 return LY_EINT;
498 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200499 }
500
501string_end:
Michal Vasko69730152020-10-09 16:30:07 +0200502 if ((arg <= Y_PREF_IDENTIF_ARG) && !(*word_len)) {
Radek Krejci4e199f52019-05-28 09:09:28 +0200503 /* empty identifier */
David Sedlákb3077192019-06-19 10:55:37 +0200504 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Statement argument is required.");
Radek Krejci4e199f52019-05-28 09:09:28 +0200505 return LY_EVALID;
506 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200507 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100508
509#undef STRING_ENDED
510#undef STRING_SINGLE_QUOTED
511#undef STRING_DOUBLE_QUOTED
512#undef STRING_DOUBLE_QUOTED_ESCAPED
513#undef STRING_PAUSED_NEXTSTRING
514#undef STRING_PAUSED_CONTINUE
Michal Vasko7fbc8162018-09-17 10:35:16 +0200515}
516
Michal Vaskoea5abea2018-09-18 13:10:54 +0200517/**
518 * @brief Get another YANG string from the raw data.
519 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200520 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200521 * @param[in] arg Type of YANG keyword argument expected.
Radek Krejcid3ca0632019-04-16 16:54:54 +0200522 * @param[out] flags optional output argument to get flag of the argument's quoting (LYS_*QOUTED - see [schema node flags](@ref snodeflags))
Michal Vasko2ca70f52018-09-27 11:04:51 +0200523 * @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 +0200524 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read string. If not needed,
525 * set to NULL. Otherwise equal to \p word_p.
526 * @param[out] word_len Length of the read string.
527 *
528 * @return LY_ERR values.
Michal Vasko7fbc8162018-09-17 10:35:16 +0200529 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200530LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100531get_argument(struct lys_yang_parser_ctx *ctx, enum yang_arg arg, uint16_t *flags, char **word_p,
Radek Krejci0f969882020-08-21 16:56:47 +0200532 char **word_b, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200533{
Radek Krejci44ceedc2018-10-02 15:54:31 +0200534 size_t buf_len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200535 uint8_t prefix = 0;
Michal Vasko69730152020-10-09 16:30:07 +0200536
Michal Vasko7fbc8162018-09-17 10:35:16 +0200537 /* word buffer - dynamically allocated */
538 *word_b = NULL;
539
540 /* word pointer - just a pointer to data */
541 *word_p = NULL;
542
543 *word_len = 0;
Radek Krejci33090f92020-12-17 20:12:46 +0100544 while (ctx->in->current[0]) {
545 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200546 case '\'':
547 case '\"':
548 if (*word_len) {
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200549 /* invalid - quotes cannot be in unquoted string and only optsep, ; or { can follow it */
Radek Krejci33090f92020-12-17 20:12:46 +0100550 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200551 "unquoted string character, optsep, semicolon or opening brace");
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200552 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200553 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200554 if (flags) {
Radek Krejci33090f92020-12-17 20:12:46 +0100555 (*flags) |= ctx->in->current[0] == '\'' ? LYS_SINGLEQUOTED : LYS_DOUBLEQUOTED;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200556 }
Radek Krejci33090f92020-12-17 20:12:46 +0100557 LY_CHECK_RET(read_qstring(ctx, arg, word_p, word_b, word_len, &buf_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200558 goto str_end;
559 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100560 if (ctx->in->current[1] == '/') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200561 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100562 MOVE_INPUT(ctx, 2);
563 LY_CHECK_RET(skip_comment(ctx, 1));
564 } else if (ctx->in->current[1] == '*') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200565 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100566 MOVE_INPUT(ctx, 2);
567 LY_CHECK_RET(skip_comment(ctx, 2));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200568 } else {
569 /* not a comment after all */
Radek Krejci33090f92020-12-17 20:12:46 +0100570 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, &buf_len, 0, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200571 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200572 break;
573 case ' ':
574 if (*word_len) {
575 /* word is finished */
576 goto str_end;
577 }
Radek Krejci33090f92020-12-17 20:12:46 +0100578 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200579 break;
580 case '\t':
581 if (*word_len) {
582 /* word is finished */
583 goto str_end;
584 }
585 /* tabs count for 8 spaces */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100586 ctx->indent += Y_TAB_SPACES;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200587
Radek Krejci33090f92020-12-17 20:12:46 +0100588 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200589 break;
590 case '\n':
591 if (*word_len) {
592 /* word is finished */
593 goto str_end;
594 }
Radek Krejcidd713ce2021-01-04 23:12:12 +0100595 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid54412f2020-12-17 20:25:35 +0100596 MOVE_INPUT(ctx, 1);
597
Michal Vasko7fbc8162018-09-17 10:35:16 +0200598 /* reset indent */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200599 ctx->indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200600 break;
601 case ';':
602 case '{':
603 if (*word_len || (arg == Y_MAYBE_STR_ARG)) {
604 /* word is finished */
605 goto str_end;
606 }
607
Radek Krejci33090f92020-12-17 20:12:46 +0100608 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current, "an argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200609 return LY_EVALID;
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200610 case '}':
611 /* invalid - braces cannot be in unquoted string (opening braces terminates the string and can follow it) */
Radek Krejci33090f92020-12-17 20:12:46 +0100612 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200613 "unquoted string character, optsep, semicolon or opening brace");
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200614 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200615 default:
Radek Krejci33090f92020-12-17 20:12:46 +0100616 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, &buf_len, 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200617 break;
618 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200619 }
620
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200621 /* unexpected end of loop */
David Sedlákb3077192019-06-19 10:55:37 +0200622 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200623 return LY_EVALID;
624
Michal Vasko7fbc8162018-09-17 10:35:16 +0200625str_end:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200626 /* terminating NULL byte for buf */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200627 if (*word_b) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200628 (*word_b) = ly_realloc(*word_b, (*word_len) + 1);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200629 LY_CHECK_ERR_RET(!(*word_b), LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200630 (*word_b)[*word_len] = '\0';
Michal Vasko7fbc8162018-09-17 10:35:16 +0200631 *word_p = *word_b;
632 }
633
634 return LY_SUCCESS;
635}
636
Michal Vaskoea5abea2018-09-18 13:10:54 +0200637/**
638 * @brief Get another YANG keyword from the raw data.
639 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200640 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200641 * @param[out] kw YANG keyword read.
642 * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances.
643 * @param[out] word_len Length of the keyword in the data. Useful for extension instances.
644 *
645 * @return LY_ERR values.
646 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200647LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100648get_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 +0200649{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200650 uint8_t prefix;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200651 const char *word_start;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200652 size_t len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200653
654 if (word_p) {
655 *word_p = NULL;
656 *word_len = 0;
657 }
658
659 /* first skip "optsep", comments */
Radek Krejci33090f92020-12-17 20:12:46 +0100660 while (ctx->in->current[0]) {
661 switch (ctx->in->current[0]) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200662 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100663 if (ctx->in->current[1] == '/') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200664 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100665 MOVE_INPUT(ctx, 2);
666 LY_CHECK_RET(skip_comment(ctx, 1));
667 } else if (ctx->in->current[1] == '*') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200668 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100669 MOVE_INPUT(ctx, 2);
670 LY_CHECK_RET(skip_comment(ctx, 2));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200671 } else {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200672 /* error - not a comment after all, keyword cannot start with slash */
David Sedlákb3077192019-06-19 10:55:37 +0200673 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '/'.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200674 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200675 }
Radek Krejci13028282019-06-11 14:56:48 +0200676 continue;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200677 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200678 /* skip whitespaces (optsep) */
Radek Krejcidd713ce2021-01-04 23:12:12 +0100679 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200680 ctx->indent = 0;
681 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200682 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200683 /* skip whitespaces (optsep) */
684 ++ctx->indent;
685 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200686 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200687 /* skip whitespaces (optsep) */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100688 ctx->indent += Y_TAB_SPACES;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200689 break;
690 default:
691 /* either a keyword start or an invalid character */
692 goto keyword_start;
693 }
694
Radek Krejci33090f92020-12-17 20:12:46 +0100695 ly_in_skip(ctx->in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200696 }
697
698keyword_start:
Radek Krejci33090f92020-12-17 20:12:46 +0100699 word_start = ctx->in->current;
Radek Krejcid54412f2020-12-17 20:25:35 +0100700 *kw = lysp_match_kw(ctx->in, &ctx->indent);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200701
Michal Vasko69730152020-10-09 16:30:07 +0200702 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 +0200703 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200704 }
705
Radek Krejcid6b76452019-09-03 17:03:03 +0200706 if (*kw != LY_STMT_NONE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200707 /* make sure we have the whole keyword */
Radek Krejci33090f92020-12-17 20:12:46 +0100708 switch (ctx->in->current[0]) {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200709 case '\n':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200710 case '\t':
Radek Krejciabdd8062019-06-11 16:44:19 +0200711 case ' ':
712 /* mandatory "sep" is just checked, not eaten so nothing in the context is updated */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200713 break;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200714 case ':':
715 /* keyword is not actually a keyword, but prefix of an extension.
716 * To avoid repeated check of the prefix syntax, move to the point where the colon was read
717 * and we will be checking the keyword (extension instance) itself */
718 prefix = 1;
Radek Krejci33090f92020-12-17 20:12:46 +0100719 MOVE_INPUT(ctx, 1);
Radek Krejci156ccaf2018-10-15 15:49:17 +0200720 goto extension;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200721 case '{':
722 /* allowed only for input and output statements which can be without arguments */
Michal Vasko69730152020-10-09 16:30:07 +0200723 if ((*kw == LY_STMT_INPUT) || (*kw == LY_STMT_OUTPUT)) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200724 break;
725 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100726 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200727 default:
Radek Krejci33090f92020-12-17 20:12:46 +0100728 MOVE_INPUT(ctx, 1);
729 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start,
Michal Vasko69730152020-10-09 16:30:07 +0200730 "a keyword followed by a separator");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200731 return LY_EVALID;
732 }
733 } else {
734 /* still can be an extension */
735 prefix = 0;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200736extension:
Radek Krejcid54412f2020-12-17 20:25:35 +0100737 while (ctx->in->current[0] && (ctx->in->current[0] != ' ') && (ctx->in->current[0] != '\t') &&
738 (ctx->in->current[0] != '\n') && (ctx->in->current[0] != '{') && (ctx->in->current[0] != ';')) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200739 uint32_t c = 0;
740
Radek Krejci33090f92020-12-17 20:12:46 +0100741 LY_CHECK_ERR_RET(ly_getutf8(&ctx->in->current, &c, &len),
742 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[-len]), LY_EVALID);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200743 ++ctx->indent;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200744 /* check character validity */
Michal Vasko63f3d842020-07-08 10:10:14 +0200745 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c,
Radek Krejci33090f92020-12-17 20:12:46 +0100746 ctx->in->current - len == word_start ? 1 : 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200747 }
Radek Krejci33090f92020-12-17 20:12:46 +0100748 if (!ctx->in->current[0]) {
David Sedlákb3077192019-06-19 10:55:37 +0200749 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200750 return LY_EVALID;
751 }
752
753 /* prefix is mandatory for extension instances */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200754 if (prefix != 2) {
Radek Krejci33090f92020-12-17 20:12:46 +0100755 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start, "a keyword");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200756 return LY_EVALID;
757 }
758
Radek Krejcid6b76452019-09-03 17:03:03 +0200759 *kw = LY_STMT_EXTENSION_INSTANCE;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200760 }
Radek Krejci626df482018-10-11 15:06:31 +0200761success:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200762 if (word_p) {
763 *word_p = (char *)word_start;
Radek Krejci33090f92020-12-17 20:12:46 +0100764 *word_len = ctx->in->current - word_start;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200765 }
766
767 return LY_SUCCESS;
768}
769
Michal Vaskoea5abea2018-09-18 13:10:54 +0200770/**
771 * @brief Parse extension instance substatements.
772 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200773 * @param[in] ctx yang parser context for logging.
Radek Krejci335332a2019-09-05 13:03:35 +0200774 * @param[in] kw Statement keyword value matching @p word value.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200775 * @param[in] word Extension instance substatement name (keyword).
776 * @param[in] word_len Extension instance substatement name length.
777 * @param[in,out] child Children of this extension instance to add to.
778 *
779 * @return LY_ERR values.
780 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200781static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100782parse_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 +0200783 struct lysp_stmt **child)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200784{
785 char *buf;
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100786 LY_ERR ret = LY_SUCCESS;
Radek Krejci335332a2019-09-05 13:03:35 +0200787 enum ly_stmt child_kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200788 struct lysp_stmt *stmt, *par_child;
789
790 stmt = calloc(1, sizeof *stmt);
791 LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM);
792
Radek Krejcibb9b1982019-04-08 14:24:59 +0200793 /* insert into parent statements */
794 if (!*child) {
795 *child = stmt;
796 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +0200797 for (par_child = *child; par_child->next; par_child = par_child->next) {}
Radek Krejcibb9b1982019-04-08 14:24:59 +0200798 par_child->next = stmt;
799 }
800
Michal Vaskofc2cd072021-02-24 13:17:17 +0100801 /* statement */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200802 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), word, word_len, &stmt->stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200803
804 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100805 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, &stmt->flags, &word, &buf, &word_len));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200806 if (word) {
807 if (buf) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200808 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), word, &stmt->arg));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200809 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200810 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), word, word_len, &stmt->arg));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200811 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200812 }
813
Michal Vaskofc2cd072021-02-24 13:17:17 +0100814 stmt->format = LY_PREF_SCHEMA;
815 stmt->prefix_data = ctx->parsed_mod;
816 stmt->kw = kw;
817
Radek Krejci33090f92020-12-17 20:12:46 +0100818 YANG_READ_SUBSTMT_FOR(ctx, child_kw, word, word_len, ret, ) {
819 LY_CHECK_RET(parse_ext_substmt(ctx, child_kw, word, word_len, &stmt->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200820 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200821 return ret;
822}
823
Michal Vaskoea5abea2018-09-18 13:10:54 +0200824/**
825 * @brief Parse extension instance.
826 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200827 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200828 * @param[in] ext_name Extension instance substatement name (keyword).
829 * @param[in] ext_name_len Extension instance substatement name length.
Radek Krejcifc596f92021-02-26 22:40:26 +0100830 * @param[in] insubstmt The statement this extension instance is a substatement of.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200831 * @param[in] insubstmt_index Index of the keyword instance this extension instance is a substatement of.
832 * @param[in,out] exts Extension instances to add to.
833 *
834 * @return LY_ERR values.
835 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200836static LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +0100837parse_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 +0200838 LY_ARRAY_COUNT_TYPE insubstmt_index, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200839{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100840 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200841 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200842 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200843 struct lysp_ext_instance *e;
Radek Krejcid6b76452019-09-03 17:03:03 +0200844 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200845
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200846 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *exts, e, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200847
Michal Vaskofc2cd072021-02-24 13:17:17 +0100848 if (!ly_strnchr(ext_name, ':', ext_name_len)) {
849 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Extension instance \"%*.s\" without the mandatory prefix.", ext_name_len, ext_name);
850 return LY_EVALID;
851 }
852
853 /* store name */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200854 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), ext_name, ext_name_len, &e->name));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200855
856 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100857 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, NULL, &word, &buf, &word_len));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200858 if (word) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200859 INSERT_WORD_RET(ctx, buf, e->argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200860 }
861
Michal Vaskofc2cd072021-02-24 13:17:17 +0100862 /* store the rest of information */
863 e->format = LY_PREF_SCHEMA;
864 e->prefix_data = ctx->parsed_mod;
865 e->insubstmt = insubstmt;
866 e->insubstmt_index = insubstmt_index;
867
Radek Krejci33090f92020-12-17 20:12:46 +0100868 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
869 LY_CHECK_RET(parse_ext_substmt(ctx, kw, word, word_len, &e->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200870 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200871 return ret;
872}
873
Michal Vaskoea5abea2018-09-18 13:10:54 +0200874/**
875 * @brief Parse a generic text field without specific constraints. Those are contact, organization,
876 * description, etc...
877 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200878 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200879 * @param[in] substmt Type of this substatement.
880 * @param[in] substmt_index Index of this substatement.
881 * @param[in,out] value Place to store the parsed value.
882 * @param[in] arg Type of the YANG keyword argument (of the value).
883 * @param[in,out] exts Extension instances to add to.
884 *
885 * @return LY_ERR values.
886 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200887static LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +0100888parse_text_field(struct lys_yang_parser_ctx *ctx, enum ly_stmt substmt, uint32_t substmt_index,
Radek Krejci0f969882020-08-21 16:56:47 +0200889 const char **value, enum yang_arg arg, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200890{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100891 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200892 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200893 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200894 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200895
896 if (*value) {
David Sedlákb3077192019-06-19 10:55:37 +0200897 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200898 return LY_EVALID;
899 }
900
901 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +0100902 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200903
904 /* store value and spend buf if allocated */
Radek Krejci011e4aa2020-09-04 15:22:31 +0200905 INSERT_WORD_RET(ctx, buf, *value, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200906
Radek Krejci33090f92020-12-17 20:12:46 +0100907 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200908 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200909 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +0100910 LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, substmt_index, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200911 break;
912 default:
David Sedlákb3077192019-06-19 10:55:37 +0200913 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200914 return LY_EVALID;
915 }
916 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200917 return ret;
918}
919
Michal Vaskoea5abea2018-09-18 13:10:54 +0200920/**
921 * @brief Parse the yang-version statement.
922 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200923 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100924 * @param[out] version Storage for the parsed information.
Michal Vasko63f3d842020-07-08 10:10:14 +0200925 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200926 *
927 * @return LY_ERR values.
928 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200929static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100930parse_yangversion(struct lys_yang_parser_ctx *ctx, uint8_t *version, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200931{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100932 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200933 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200934 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200935 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200936
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100937 if (*version) {
David Sedlákb3077192019-06-19 10:55:37 +0200938 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200939 return LY_EVALID;
940 }
941
942 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +0100943 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200944
Radek Krejci96e48da2020-09-04 13:18:06 +0200945 if ((word_len == 1) && !strncmp(word, "1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100946 *version = LYS_VERSION_1_0;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100947 } else if ((word_len == ly_strlen_const("1.1")) && !strncmp(word, "1.1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100948 *version = LYS_VERSION_1_1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200949 } else {
David Sedlákb3077192019-06-19 10:55:37 +0200950 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200951 free(buf);
952 return LY_EVALID;
953 }
954 free(buf);
955
Radek Krejci33090f92020-12-17 20:12:46 +0100956 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200957 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200958 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +0100959 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_YANG_VERSION, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200960 break;
961 default:
David Sedlákb3077192019-06-19 10:55:37 +0200962 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200963 return LY_EVALID;
964 }
965 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200966 return ret;
967}
968
Michal Vaskoea5abea2018-09-18 13:10:54 +0200969/**
970 * @brief Parse the belongs-to statement.
971 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200972 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200973 * @param[in,out] prefix Place to store the parsed belongs-to prefix value.
974 * @param[in,out] exts Extension instances to add to.
975 *
976 * @return LY_ERR values.
977 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200978static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100979parse_belongsto(struct lys_yang_parser_ctx *ctx, const char **prefix, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200980{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100981 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200982 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200983 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200984 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200985
Michal Vaskoc3781c32020-10-06 14:04:08 +0200986 if (*prefix) {
David Sedlákb3077192019-06-19 10:55:37 +0200987 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200988 return LY_EVALID;
989 }
990
Michal Vaskoc3781c32020-10-06 14:04:08 +0200991 /* get value, it must match the main module */
Radek Krejci33090f92020-12-17 20:12:46 +0100992 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200993 if (ly_strncmp(ctx->parsed_mod->mod->name, word, word_len)) {
Michal Vaskoc3781c32020-10-06 14:04:08 +0200994 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 +0200995 (int)word_len, word, ctx->parsed_mod->mod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +0200996 free(buf);
997 return LY_EVALID;
998 }
999 free(buf);
Radek Krejcif09e4e82019-06-14 15:08:11 +02001000
Radek Krejci33090f92020-12-17 20:12:46 +01001001 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001002 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001003 case LY_STMT_PREFIX:
Radek Krejcifc596f92021-02-26 22:40:26 +01001004 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_PREFIX, 0, prefix, Y_IDENTIF_ARG, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001005 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001006 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001007 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_BELONGS_TO, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001008 break;
1009 default:
David Sedlákb3077192019-06-19 10:55:37 +02001010 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001011 return LY_EVALID;
1012 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001013 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001014 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001015checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001016 /* mandatory substatements */
1017 if (!*prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001018 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001019 return LY_EVALID;
1020 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001021 return ret;
1022}
1023
Michal Vaskoea5abea2018-09-18 13:10:54 +02001024/**
1025 * @brief Parse the revision-date statement.
1026 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001027 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001028 * @param[in,out] rev Array to store the parsed value in.
1029 * @param[in,out] exts Extension instances to add to.
1030 *
1031 * @return LY_ERR values.
1032 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001033static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001034parse_revisiondate(struct lys_yang_parser_ctx *ctx, char *rev, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001035{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001036 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001037 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001038 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001039 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001040
1041 if (rev[0]) {
David Sedlákb3077192019-06-19 10:55:37 +02001042 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001043 return LY_EVALID;
1044 }
1045
1046 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001047 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001048
1049 /* check value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001050 if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision-date")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001051 free(buf);
1052 return LY_EVALID;
1053 }
1054
1055 /* store value and spend buf if allocated */
1056 strncpy(rev, word, word_len);
1057 free(buf);
1058
Radek Krejci33090f92020-12-17 20:12:46 +01001059 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001060 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001061 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001062 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_REVISION_DATE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001063 break;
1064 default:
David Sedlákb3077192019-06-19 10:55:37 +02001065 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001066 return LY_EVALID;
1067 }
1068 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001069 return ret;
1070}
1071
Michal Vaskoea5abea2018-09-18 13:10:54 +02001072/**
1073 * @brief Parse the include statement.
1074 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001075 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001076 * @param[in] module_name Name of the module to check name collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001077 * @param[in,out] includes Parsed includes to add to.
1078 *
1079 * @return LY_ERR values.
1080 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001081static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001082parse_include(struct lys_yang_parser_ctx *ctx, const char *module_name, struct lysp_include **includes)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001083{
Radek Krejcid33273d2018-10-25 14:55:52 +02001084 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001085 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001086 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001087 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001088 struct lysp_include *inc;
1089
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001090 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *includes, inc, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001091
1092 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001093 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001094
Radek Krejci011e4aa2020-09-04 15:22:31 +02001095 INSERT_WORD_RET(ctx, buf, inc->name, word, word_len);
Radek Krejci086c7132018-10-26 15:29:04 +02001096
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001097 /* submodules share the namespace with the module names, so there must not be
1098 * a module of the same name in the context, no need for revision matching */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001099 if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(PARSER_CTX(ctx), inc->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01001100 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", inc->name);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001101 return LY_EVALID;
1102 }
1103
Radek Krejci33090f92020-12-17 20:12:46 +01001104 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001105 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001106 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001107 PARSER_CHECK_STMTVER2_RET(ctx, "description", "include");
Radek Krejcifc596f92021-02-26 22:40:26 +01001108 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 +02001109 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001110 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001111 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "include");
Radek Krejcifc596f92021-02-26 22:40:26 +01001112 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 +02001113 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001114 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001115 LY_CHECK_RET(parse_revisiondate(ctx, inc->rev, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001116 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001117 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001118 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_INCLUDE, 0, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001119 break;
1120 default:
David Sedlákb3077192019-06-19 10:55:37 +02001121 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001122 return LY_EVALID;
1123 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001124 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001125 return ret;
1126}
1127
Michal Vaskoea5abea2018-09-18 13:10:54 +02001128/**
1129 * @brief Parse the import statement.
1130 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001131 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001132 * @param[in] module_prefix Prefix of the module to check prefix collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001133 * @param[in,out] imports Parsed imports to add to.
1134 *
1135 * @return LY_ERR values.
1136 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001137static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001138parse_import(struct lys_yang_parser_ctx *ctx, const char *module_prefix, struct lysp_import **imports)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001139{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001140 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001141 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001142 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001143 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001144 struct lysp_import *imp;
1145
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001146 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *imports, imp, LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001147
1148 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001149 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02001150 INSERT_WORD_RET(ctx, buf, imp->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001151
Radek Krejci33090f92020-12-17 20:12:46 +01001152 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001153 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001154 case LY_STMT_PREFIX:
Radek Krejcifc596f92021-02-26 22:40:26 +01001155 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 +01001156 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 +02001157 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001158 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001159 PARSER_CHECK_STMTVER2_RET(ctx, "description", "import");
Radek Krejcifc596f92021-02-26 22:40:26 +01001160 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 +02001161 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001162 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001163 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "import");
Radek Krejcifc596f92021-02-26 22:40:26 +01001164 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 +02001165 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001166 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001167 LY_CHECK_RET(parse_revisiondate(ctx, imp->rev, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001168 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001169 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001170 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_IMPORT, 0, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001171 break;
1172 default:
David Sedlákb3077192019-06-19 10:55:37 +02001173 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001174 return LY_EVALID;
1175 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001176 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001177 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001178checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001179 /* mandatory substatements */
David Sedlákb3077192019-06-19 10:55:37 +02001180 LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001181
1182 return ret;
1183}
1184
Michal Vaskoea5abea2018-09-18 13:10:54 +02001185/**
1186 * @brief Parse the revision statement.
1187 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001188 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001189 * @param[in,out] revs Parsed revisions to add to.
1190 *
1191 * @return LY_ERR values.
1192 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001193static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001194parse_revision(struct lys_yang_parser_ctx *ctx, struct lysp_revision **revs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001195{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001196 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001197 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001198 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001199 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001200 struct lysp_revision *rev;
1201
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001202 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *revs, rev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001203
1204 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001205 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001206
1207 /* check value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001208 if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision")) {
David Sedlák68ef3dc2019-07-22 13:40:19 +02001209 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001210 return LY_EVALID;
1211 }
1212
Radek Krejcib7db73a2018-10-24 14:18:40 +02001213 strncpy(rev->date, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001214 free(buf);
1215
Radek Krejci33090f92020-12-17 20:12:46 +01001216 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001217 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001218 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001219 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 +02001220 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001221 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001222 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 +02001223 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001224 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001225 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_REVISION, 0, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001226 break;
1227 default:
David Sedlákb3077192019-06-19 10:55:37 +02001228 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001229 return LY_EVALID;
1230 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001231 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001232 return ret;
1233}
1234
Michal Vaskoea5abea2018-09-18 13:10:54 +02001235/**
1236 * @brief Parse a generic text field that can have more instances such as base.
1237 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001238 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001239 * @param[in] substmt Type of this substatement.
1240 * @param[in,out] texts Parsed values to add to.
1241 * @param[in] arg Type of the expected argument.
1242 * @param[in,out] exts Extension instances to add to.
1243 *
1244 * @return LY_ERR values.
1245 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001246static LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +01001247parse_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 +02001248 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001249{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001250 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001251 char *buf, *word;
Radek Krejci151a5b72018-10-19 14:21:44 +02001252 const char **item;
1253 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001254 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001255
1256 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001257 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *texts, item, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001258
1259 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001260 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001261
Radek Krejci011e4aa2020-09-04 15:22:31 +02001262 INSERT_WORD_RET(ctx, buf, *item, word, word_len);
Radek Krejci33090f92020-12-17 20:12:46 +01001263 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001264 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001265 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001266 LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, LY_ARRAY_COUNT(*texts) - 1, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001267 break;
1268 default:
David Sedlákb3077192019-06-19 10:55:37 +02001269 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001270 return LY_EVALID;
1271 }
1272 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001273 return ret;
1274}
1275
Michal Vaskoea5abea2018-09-18 13:10:54 +02001276/**
Michal Vasko7f45cf22020-10-01 12:49:44 +02001277 * @brief Parse a generic text field that can have more instances such as base.
1278 *
1279 * @param[in] ctx yang parser context for logging.
Michal Vasko7f45cf22020-10-01 12:49:44 +02001280 * @param[in] substmt Type of this substatement.
1281 * @param[in,out] qnames Parsed qnames to add to.
1282 * @param[in] arg Type of the expected argument.
1283 * @param[in,out] exts Extension instances to add to.
1284 *
1285 * @return LY_ERR values.
1286 */
1287static LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +01001288parse_qnames(struct lys_yang_parser_ctx *ctx, enum ly_stmt substmt, struct lysp_qname **qnames,
Michal Vasko7f45cf22020-10-01 12:49:44 +02001289 enum yang_arg arg, struct lysp_ext_instance **exts)
1290{
1291 LY_ERR ret = LY_SUCCESS;
1292 char *buf, *word;
1293 struct lysp_qname *item;
1294 size_t word_len;
1295 enum ly_stmt kw;
1296
1297 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001298 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *qnames, item, LY_EMEM);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001299
1300 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001301 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001302
1303 INSERT_WORD_RET(ctx, buf, item->str, word, word_len);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001304 item->mod = ctx->parsed_mod;
Radek Krejci33090f92020-12-17 20:12:46 +01001305 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02001306 switch (kw) {
1307 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001308 LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, LY_ARRAY_COUNT(*qnames) - 1, exts));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001309 break;
1310 default:
1311 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
1312 return LY_EVALID;
1313 }
1314 }
1315 return ret;
1316}
1317
1318/**
Michal Vaskoea5abea2018-09-18 13:10:54 +02001319 * @brief Parse the config statement.
1320 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001321 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001322 * @param[in,out] flags Flags to add to.
1323 * @param[in,out] exts Extension instances to add to.
1324 *
1325 * @return LY_ERR values.
1326 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001327static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001328parse_config(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001329{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001330 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001331 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001332 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001333 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001334
1335 if (*flags & LYS_CONFIG_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001336 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001337 return LY_EVALID;
1338 }
1339
1340 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001341 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001342
Radek Krejcif13b87b2020-12-01 22:02:17 +01001343 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001344 *flags |= LYS_CONFIG_W;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001345 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001346 *flags |= LYS_CONFIG_R;
1347 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001348 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001349 free(buf);
1350 return LY_EVALID;
1351 }
1352 free(buf);
1353
Radek Krejci33090f92020-12-17 20:12:46 +01001354 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001355 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001356 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001357 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_CONFIG, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001358 break;
1359 default:
David Sedlákb3077192019-06-19 10:55:37 +02001360 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001361 return LY_EVALID;
1362 }
1363 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001364 return ret;
1365}
1366
Michal Vaskoea5abea2018-09-18 13:10:54 +02001367/**
1368 * @brief Parse the mandatory statement.
1369 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001370 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001371 * @param[in,out] flags Flags to add to.
1372 * @param[in,out] exts Extension instances to add to.
1373 *
1374 * @return LY_ERR values.
1375 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001376static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001377parse_mandatory(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001378{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001379 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001380 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001381 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001382 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001383
1384 if (*flags & LYS_MAND_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001385 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001386 return LY_EVALID;
1387 }
1388
1389 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001390 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001391
Radek Krejcif13b87b2020-12-01 22:02:17 +01001392 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001393 *flags |= LYS_MAND_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001394 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001395 *flags |= LYS_MAND_FALSE;
1396 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001397 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001398 free(buf);
1399 return LY_EVALID;
1400 }
1401 free(buf);
1402
Radek Krejci33090f92020-12-17 20:12:46 +01001403 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001404 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001405 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001406 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MANDATORY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001407 break;
1408 default:
David Sedlákb3077192019-06-19 10:55:37 +02001409 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001410 return LY_EVALID;
1411 }
1412 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001413 return ret;
1414}
1415
Michal Vaskoea5abea2018-09-18 13:10:54 +02001416/**
1417 * @brief Parse a restriction such as range or length.
1418 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001419 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001420 * @param[in] restr_kw Type of this particular restriction.
1421 * @param[in,out] exts Extension instances to add to.
1422 *
1423 * @return LY_ERR values.
1424 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001425static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001426parse_restr(struct lys_yang_parser_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr *restr)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001427{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001428 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001429 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001430 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001431 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001432
1433 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001434 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001435
Michal Vaskob36053d2020-03-26 15:49:30 +01001436 CHECK_NONEMPTY(ctx, word_len, ly_stmt2str(restr_kw));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001437 INSERT_WORD_RET(ctx, buf, restr->arg.str, word, word_len);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001438 restr->arg.mod = ctx->parsed_mod;
Radek Krejci33090f92020-12-17 20:12:46 +01001439 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001440 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001441 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001442 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 +02001443 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001444 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001445 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 +02001446 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001447 case LY_STMT_ERROR_APP_TAG:
Radek Krejcifc596f92021-02-26 22:40:26 +01001448 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 +02001449 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001450 case LY_STMT_ERROR_MESSAGE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001451 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 +02001452 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001453 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001454 LY_CHECK_RET(parse_ext(ctx, word, word_len, restr_kw, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001455 break;
1456 default:
David Sedlákb3077192019-06-19 10:55:37 +02001457 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001458 return LY_EVALID;
1459 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001460 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001461 return ret;
1462}
1463
Michal Vaskoea5abea2018-09-18 13:10:54 +02001464/**
1465 * @brief Parse a restriction that can have more instances such as must.
1466 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001467 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001468 * @param[in] restr_kw Type of this particular restriction.
1469 * @param[in,out] restrs Restrictions to add to.
1470 *
1471 * @return LY_ERR values.
1472 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001473static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001474parse_restrs(struct lys_yang_parser_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr **restrs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001475{
1476 struct lysp_restr *restr;
1477
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001478 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *restrs, restr, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01001479 return parse_restr(ctx, restr_kw, restr);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001480}
1481
Michal Vaskoea5abea2018-09-18 13:10:54 +02001482/**
1483 * @brief Parse the status statement.
1484 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001485 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001486 * @param[in,out] flags Flags to add to.
1487 * @param[in,out] exts Extension instances to add to.
1488 *
1489 * @return LY_ERR values.
1490 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001491static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001492parse_status(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001493{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001494 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001495 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001496 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001497 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001498
1499 if (*flags & LYS_STATUS_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001500 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001501 return LY_EVALID;
1502 }
1503
1504 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001505 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001506
Radek Krejcif13b87b2020-12-01 22:02:17 +01001507 if ((word_len == ly_strlen_const("current")) && !strncmp(word, "current", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001508 *flags |= LYS_STATUS_CURR;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001509 } else if ((word_len == ly_strlen_const("deprecated")) && !strncmp(word, "deprecated", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001510 *flags |= LYS_STATUS_DEPRC;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001511 } else if ((word_len == ly_strlen_const("obsolete")) && !strncmp(word, "obsolete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001512 *flags |= LYS_STATUS_OBSLT;
1513 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001514 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001515 free(buf);
1516 return LY_EVALID;
1517 }
1518 free(buf);
1519
Radek Krejci33090f92020-12-17 20:12:46 +01001520 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001521 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001522 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001523 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_STATUS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001524 break;
1525 default:
David Sedlákb3077192019-06-19 10:55:37 +02001526 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001527 return LY_EVALID;
1528 }
1529 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001530 return ret;
1531}
1532
Michal Vaskoea5abea2018-09-18 13:10:54 +02001533/**
1534 * @brief Parse the when statement.
1535 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001536 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001537 * @param[in,out] when_p When pointer to parse to.
1538 *
1539 * @return LY_ERR values.
1540 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001541LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001542parse_when(struct lys_yang_parser_ctx *ctx, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001543{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001544 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001545 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001546 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001547 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001548 struct lysp_when *when;
1549
1550 if (*when_p) {
David Sedlákb3077192019-06-19 10:55:37 +02001551 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001552 return LY_EVALID;
1553 }
1554
1555 when = calloc(1, sizeof *when);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001556 LY_CHECK_ERR_RET(!when, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001557 *when_p = when;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001558
1559 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001560 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01001561 CHECK_NONEMPTY(ctx, word_len, "when");
Radek Krejci011e4aa2020-09-04 15:22:31 +02001562 INSERT_WORD_RET(ctx, buf, when->cond, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001563
Radek Krejci33090f92020-12-17 20:12:46 +01001564 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001565 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001566 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001567 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 +02001568 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001569 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001570 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 +02001571 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001572 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001573 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_WHEN, 0, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001574 break;
1575 default:
David Sedlákb3077192019-06-19 10:55:37 +02001576 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001577 return LY_EVALID;
1578 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001579 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001580 return ret;
1581}
1582
Michal Vaskoea5abea2018-09-18 13:10:54 +02001583/**
1584 * @brief Parse the anydata or anyxml statement.
1585 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001586 * @param[in] ctx yang parser context for logging.
Radek Krejci39b7fc22021-02-26 23:29:18 +01001587 * @param[in] any_kw Type of this particular keyword.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001588 * @param[in,out] siblings Siblings to add to.
1589 *
1590 * @return LY_ERR values.
1591 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02001592LY_ERR
Radek Krejci39b7fc22021-02-26 23:29:18 +01001593parse_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 +02001594{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001595 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001596 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001597 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001598 struct lysp_node_anydata *any;
Radek Krejci39b7fc22021-02-26 23:29:18 +01001599 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001600
David Sedlák60adc092019-08-06 15:57:02 +02001601 /* create new structure and insert into siblings */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001602 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, any, next, LY_EMEM);
David Sedlák60adc092019-08-06 15:57:02 +02001603
Radek Krejci39b7fc22021-02-26 23:29:18 +01001604 any->nodetype = any_kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001605 any->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001606
Michal Vasko7fbc8162018-09-17 10:35:16 +02001607 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01001608 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02001609 INSERT_WORD_RET(ctx, buf, any->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001610
1611 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01001612 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001613 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001614 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01001615 LY_CHECK_RET(parse_config(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001616 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001617 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001618 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 +02001619 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001620 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001621 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &any->iffeatures, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001622 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001623 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01001624 LY_CHECK_RET(parse_mandatory(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001625 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001626 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01001627 LY_CHECK_RET(parse_restrs(ctx, kw, &any->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001628 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001629 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001630 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 +02001631 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001632 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001633 LY_CHECK_RET(parse_status(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001634 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001635 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01001636 LY_CHECK_RET(parse_when(ctx, &any->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001637 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001638 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001639 LY_CHECK_RET(parse_ext(ctx, word, word_len, any_kw, 0, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001640 break;
1641 default:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001642 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(any_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001643 return LY_EVALID;
1644 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001645 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001646 return ret;
1647}
1648
Michal Vaskoea5abea2018-09-18 13:10:54 +02001649/**
1650 * @brief Parse the value or position statement. Substatement of type enum statement.
1651 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001652 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001653 * @param[in] val_kw Type of this particular keyword.
1654 * @param[in,out] value Value to write to.
1655 * @param[in,out] flags Flags to write to.
1656 * @param[in,out] exts Extension instances to add to.
1657 *
1658 * @return LY_ERR values.
1659 */
David Sedlákd6ce6d72019-07-16 17:30:18 +02001660LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001661parse_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 +02001662 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001663{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001664 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001665 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001666 size_t word_len;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02001667 long int num = 0;
1668 unsigned long int unum = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001669 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001670
1671 if (*flags & LYS_SET_VALUE) {
David Sedlákb3077192019-06-19 10:55:37 +02001672 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001673 return LY_EVALID;
1674 }
1675 *flags |= LYS_SET_VALUE;
1676
1677 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001678 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001679
Radek Krejcid6b76452019-09-03 17:03:03 +02001680 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 +02001681 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001682 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001683 }
1684
1685 errno = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001686 if (val_kw == LY_STMT_VALUE) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001687 num = strtol(word, &ptr, LY_BASE_DEC);
Michal Vasko69730152020-10-09 16:30:07 +02001688 if ((num < INT64_C(-2147483648)) || (num > INT64_C(2147483647))) {
David Sedlákb3077192019-06-19 10:55:37 +02001689 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001690 goto error;
1691 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001692 } else {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001693 unum = strtoul(word, &ptr, LY_BASE_DEC);
Radek Krejci8b764662018-11-14 14:15:13 +01001694 if (unum > UINT64_C(4294967295)) {
David Sedlákb3077192019-06-19 10:55:37 +02001695 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001696 goto error;
1697 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001698 }
1699 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001700 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001701 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001702 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001703 }
1704 if (errno == ERANGE) {
David Sedlákb3077192019-06-19 10:55:37 +02001705 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001706 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001707 }
Radek Krejcid6b76452019-09-03 17:03:03 +02001708 if (val_kw == LY_STMT_VALUE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001709 *value = num;
1710 } else {
1711 *value = unum;
1712 }
1713 free(buf);
1714
Radek Krejci33090f92020-12-17 20:12:46 +01001715 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001716 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001717 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001718 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 +02001719 break;
1720 default:
David Sedlákb3077192019-06-19 10:55:37 +02001721 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001722 return LY_EVALID;
1723 }
1724 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001725 return ret;
Radek Krejci8b764662018-11-14 14:15:13 +01001726
1727error:
1728 free(buf);
1729 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001730}
1731
Michal Vaskoea5abea2018-09-18 13:10:54 +02001732/**
1733 * @brief Parse the enum or bit statement. Substatement of type statement.
1734 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001735 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001736 * @param[in] enum_kw Type of this particular keyword.
1737 * @param[in,out] enums Enums or bits to add to.
1738 *
1739 * @return LY_ERR values.
1740 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001741static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001742parse_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 +02001743{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001744 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001745 char *buf, *word;
David Sedlák6544c182019-07-12 13:17:33 +02001746 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001747 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001748 struct lysp_type_enum *enm;
1749
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001750 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *enums, enm, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001751
1752 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001753 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 +02001754 if (enum_kw == LY_STMT_ENUM) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001755 ret = lysp_check_enum_name((struct lys_parser_ctx *)ctx, (const char *)word, word_len);
David Sedlák6544c182019-07-12 13:17:33 +02001756 LY_CHECK_ERR_RET(ret, free(buf), ret);
Radek Krejci335332a2019-09-05 13:03:35 +02001757 } /* else nothing specific for YANG_BIT */
Radek Krejci8b764662018-11-14 14:15:13 +01001758
Radek Krejci011e4aa2020-09-04 15:22:31 +02001759 INSERT_WORD_RET(ctx, buf, enm->name, word, word_len);
Radek Krejci8b764662018-11-14 14:15:13 +01001760 CHECK_UNIQUENESS(ctx, *enums, name, ly_stmt2str(enum_kw), enm->name);
1761
Radek Krejci33090f92020-12-17 20:12:46 +01001762 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001763 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001764 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01001765 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 +02001766 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001767 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02001768 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", ly_stmt2str(enum_kw));
Radek Krejcifc596f92021-02-26 22:40:26 +01001769 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &enm->iffeatures, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001770 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001771 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001772 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 +02001773 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001774 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001775 LY_CHECK_RET(parse_status(ctx, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001776 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001777 case LY_STMT_VALUE:
1778 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 +02001779 ly_stmt2str(enum_kw)), LY_EVALID);
Radek Krejci33090f92020-12-17 20:12:46 +01001780 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, &enm->value, &enm->flags, &enm->exts));
David Sedlák9fb515f2019-07-11 10:33:58 +02001781 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001782 case LY_STMT_POSITION:
1783 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 +02001784 ly_stmt2str(enum_kw)), LY_EVALID);
Radek Krejci33090f92020-12-17 20:12:46 +01001785 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, &enm->value, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001786 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001787 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001788 LY_CHECK_RET(parse_ext(ctx, word, word_len, enum_kw, 0, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001789 break;
1790 default:
David Sedlákb3077192019-06-19 10:55:37 +02001791 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001792 return LY_EVALID;
1793 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001794 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001795 return ret;
1796}
1797
Michal Vaskoea5abea2018-09-18 13:10:54 +02001798/**
1799 * @brief Parse the fraction-digits statement. Substatement of type statement.
1800 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001801 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001802 * @param[in,out] fracdig Value to write to.
1803 * @param[in,out] exts Extension instances to add to.
1804 *
1805 * @return LY_ERR values.
1806 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001807static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001808parse_type_fracdigits(struct lys_yang_parser_ctx *ctx, uint8_t *fracdig, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001809{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001810 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001811 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001812 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001813 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02001814 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001815
1816 if (*fracdig) {
David Sedlákb3077192019-06-19 10:55:37 +02001817 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001818 return LY_EVALID;
1819 }
1820
1821 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001822 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001823
1824 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
David Sedlákb3077192019-06-19 10:55:37 +02001825 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001826 free(buf);
1827 return LY_EVALID;
1828 }
1829
1830 errno = 0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001831 num = strtoul(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001832 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001833 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001834 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001835 free(buf);
1836 return LY_EVALID;
1837 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001838 if ((errno == ERANGE) || (num > LY_TYPE_DEC64_FD_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02001839 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001840 free(buf);
1841 return LY_EVALID;
1842 }
1843 *fracdig = num;
1844 free(buf);
1845
Radek Krejci33090f92020-12-17 20:12:46 +01001846 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001847 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001848 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001849 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_FRACTION_DIGITS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001850 break;
1851 default:
David Sedlákb3077192019-06-19 10:55:37 +02001852 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001853 return LY_EVALID;
1854 }
1855 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001856 return ret;
1857}
1858
Michal Vaskoea5abea2018-09-18 13:10:54 +02001859/**
1860 * @brief Parse the require-instance statement. Substatement of type statement.
1861 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001862 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001863 * @param[in,out] reqinst Value to write to.
1864 * @param[in,out] flags Flags to write to.
1865 * @param[in,out] exts Extension instances to add to.
1866 *
1867 * @return LY_ERR values.
1868 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001869static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001870parse_type_reqinstance(struct lys_yang_parser_ctx *ctx, uint8_t *reqinst, uint16_t *flags,
Radek Krejci0f969882020-08-21 16:56:47 +02001871 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001872{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001873 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001874 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001875 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001876 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001877
1878 if (*flags & LYS_SET_REQINST) {
David Sedlákb3077192019-06-19 10:55:37 +02001879 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001880 return LY_EVALID;
1881 }
1882 *flags |= LYS_SET_REQINST;
1883
1884 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001885 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001886
Radek Krejcif13b87b2020-12-01 22:02:17 +01001887 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001888 *reqinst = 1;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001889 } else if ((word_len != ly_strlen_const("false")) || strncmp(word, "false", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001890 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001891 free(buf);
1892 return LY_EVALID;
1893 }
1894 free(buf);
1895
Radek Krejci33090f92020-12-17 20:12:46 +01001896 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001897 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001898 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001899 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_REQUIRE_INSTANCE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001900 break;
1901 default:
David Sedlákb3077192019-06-19 10:55:37 +02001902 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001903 return LY_EVALID;
1904 }
1905 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001906 return ret;
1907}
1908
Michal Vaskoea5abea2018-09-18 13:10:54 +02001909/**
1910 * @brief Parse the modifier statement. Substatement of type pattern statement.
1911 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001912 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001913 * @param[in,out] pat Value to write to.
1914 * @param[in,out] exts Extension instances to add to.
1915 *
1916 * @return LY_ERR values.
1917 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001918static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001919parse_type_pattern_modifier(struct lys_yang_parser_ctx *ctx, const char **pat, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001920{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001921 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001922 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001923 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001924 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001925
Radek Krejcif13b87b2020-12-01 22:02:17 +01001926 if ((*pat)[0] == LYSP_RESTR_PATTERN_NACK) {
David Sedlákb3077192019-06-19 10:55:37 +02001927 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001928 return LY_EVALID;
1929 }
1930
1931 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001932 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001933
Radek Krejcif13b87b2020-12-01 22:02:17 +01001934 if ((word_len != ly_strlen_const("invert-match")) || strncmp(word, "invert-match", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001935 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001936 free(buf);
1937 return LY_EVALID;
1938 }
1939 free(buf);
1940
1941 /* replace the value in the dictionary */
1942 buf = malloc(strlen(*pat) + 1);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001943 LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001944 strcpy(buf, *pat);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001945 lydict_remove(PARSER_CTX(ctx), *pat);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001946
Radek Krejcif13b87b2020-12-01 22:02:17 +01001947 assert(buf[0] == LYSP_RESTR_PATTERN_ACK);
1948 buf[0] = LYSP_RESTR_PATTERN_NACK;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001949 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, pat));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001950
Radek Krejci33090f92020-12-17 20:12:46 +01001951 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001952 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001953 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001954 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MODIFIER, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001955 break;
1956 default:
David Sedlákb3077192019-06-19 10:55:37 +02001957 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001958 return LY_EVALID;
1959 }
1960 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001961 return ret;
1962}
1963
Michal Vaskoea5abea2018-09-18 13:10:54 +02001964/**
1965 * @brief Parse the pattern statement. Substatement of type statement.
1966 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001967 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001968 * @param[in,out] patterns Restrictions to add to.
1969 *
1970 * @return LY_ERR values.
1971 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001972static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001973parse_type_pattern(struct lys_yang_parser_ctx *ctx, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001974{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001975 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001976 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001977 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001978 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001979 struct lysp_restr *restr;
1980
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001981 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *patterns, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001982
1983 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001984 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001985
1986 /* add special meaning first byte */
1987 if (buf) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001988 buf = ly_realloc(buf, word_len + 2);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001989 word = buf;
1990 } else {
1991 buf = malloc(word_len + 2);
1992 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001993 LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci86d106e2018-10-18 09:53:19 +02001994 memmove(buf + 1, word, word_len);
Radek Krejcif13b87b2020-12-01 22:02:17 +01001995 buf[0] = LYSP_RESTR_PATTERN_ACK; /* pattern's default regular-match flag */
Radek Krejci86d106e2018-10-18 09:53:19 +02001996 buf[word_len + 1] = '\0'; /* terminating NULL byte */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001997 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, &restr->arg.str));
1998 restr->arg.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001999
Radek Krejci33090f92020-12-17 20:12:46 +01002000 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002001 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002002 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002003 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 +02002004 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002005 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002006 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 +02002007 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002008 case LY_STMT_ERROR_APP_TAG:
Radek Krejcifc596f92021-02-26 22:40:26 +01002009 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 +02002010 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002011 case LY_STMT_ERROR_MESSAGE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002012 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 +02002013 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002014 case LY_STMT_MODIFIER:
Radek Krejci335332a2019-09-05 13:03:35 +02002015 PARSER_CHECK_STMTVER2_RET(ctx, "modifier", "pattern");
Radek Krejci33090f92020-12-17 20:12:46 +01002016 LY_CHECK_RET(parse_type_pattern_modifier(ctx, &restr->arg.str, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002017 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002018 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002019 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_PATTERN, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002020 break;
2021 default:
David Sedlákb3077192019-06-19 10:55:37 +02002022 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002023 return LY_EVALID;
2024 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002025 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002026 return ret;
2027}
2028
Michal Vaskoea5abea2018-09-18 13:10:54 +02002029/**
2030 * @brief Parse the type statement.
2031 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002032 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002033 * @param[in,out] type Type to wrote to.
2034 *
2035 * @return LY_ERR values.
2036 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002037static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002038parse_type(struct lys_yang_parser_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002039{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002040 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002041 char *buf, *word;
Michal Vasko004d3152020-06-11 19:59:22 +02002042 const char *str_path = NULL;
Radek Krejciefd22f62018-09-27 11:47:58 +02002043 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002044 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002045 struct lysp_type *nest_type;
2046
2047 if (type->name) {
David Sedlákb3077192019-06-19 10:55:37 +02002048 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002049 return LY_EVALID;
2050 }
2051
2052 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002053 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002054 INSERT_WORD_RET(ctx, buf, type->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002055
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002056 /* set module */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002057 type->pmod = ctx->parsed_mod;
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002058
Radek Krejci33090f92020-12-17 20:12:46 +01002059 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002060 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002061 case LY_STMT_BASE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002062 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 +01002063 type->flags |= LYS_SET_BASE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002064 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002065 case LY_STMT_BIT:
Radek Krejci33090f92020-12-17 20:12:46 +01002066 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->bits));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002067 type->flags |= LYS_SET_BIT;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002068 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002069 case LY_STMT_ENUM:
Radek Krejci33090f92020-12-17 20:12:46 +01002070 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->enums));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002071 type->flags |= LYS_SET_ENUM;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002072 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002073 case LY_STMT_FRACTION_DIGITS:
Radek Krejci33090f92020-12-17 20:12:46 +01002074 LY_CHECK_RET(parse_type_fracdigits(ctx, &type->fraction_digits, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002075 type->flags |= LYS_SET_FRDIGITS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002076 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002077 case LY_STMT_LENGTH:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002078 if (type->length) {
David Sedlákb3077192019-06-19 10:55:37 +02002079 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002080 return LY_EVALID;
2081 }
2082 type->length = calloc(1, sizeof *type->length);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002083 LY_CHECK_ERR_RET(!type->length, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002084
Radek Krejci33090f92020-12-17 20:12:46 +01002085 LY_CHECK_RET(parse_restr(ctx, kw, type->length));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002086 type->flags |= LYS_SET_LENGTH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002087 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002088 case LY_STMT_PATH:
Michal Vasko004d3152020-06-11 19:59:22 +02002089 if (type->path) {
Radek Krejcifc596f92021-02-26 22:40:26 +01002090 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyext_substmt2str(LY_STMT_PATH));
Michal Vasko004d3152020-06-11 19:59:22 +02002091 return LY_EVALID;
2092 }
2093
Radek Krejcifc596f92021-02-26 22:40:26 +01002094 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 +02002095 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 +02002096 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002097 lydict_remove(PARSER_CTX(ctx), str_path);
Michal Vasko004d3152020-06-11 19:59:22 +02002098 LY_CHECK_RET(ret);
Radek Krejcid505e3d2018-11-13 09:04:17 +01002099 type->flags |= LYS_SET_PATH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002100 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002101 case LY_STMT_PATTERN:
Radek Krejci33090f92020-12-17 20:12:46 +01002102 LY_CHECK_RET(parse_type_pattern(ctx, &type->patterns));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002103 type->flags |= LYS_SET_PATTERN;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002104 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002105 case LY_STMT_RANGE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002106 if (type->range) {
David Sedlákb3077192019-06-19 10:55:37 +02002107 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002108 return LY_EVALID;
2109 }
2110 type->range = calloc(1, sizeof *type->range);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002111 LY_CHECK_ERR_RET(!type->range, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002112
Radek Krejci33090f92020-12-17 20:12:46 +01002113 LY_CHECK_RET(parse_restr(ctx, kw, type->range));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002114 type->flags |= LYS_SET_RANGE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002115 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002116 case LY_STMT_REQUIRE_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002117 LY_CHECK_RET(parse_type_reqinstance(ctx, &type->require_instance, &type->flags, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002118 /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002119 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002120 case LY_STMT_TYPE:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002121 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), type->types, nest_type, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01002122 LY_CHECK_RET(parse_type(ctx, nest_type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002123 type->flags |= LYS_SET_TYPE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002124 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002125 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002126 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_TYPE, 0, &type->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002127 break;
2128 default:
David Sedlákb3077192019-06-19 10:55:37 +02002129 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002130 return LY_EVALID;
2131 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002132 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002133 return ret;
2134}
2135
Michal Vaskoea5abea2018-09-18 13:10:54 +02002136/**
2137 * @brief Parse the leaf statement.
2138 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002139 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002140 * @param[in,out] siblings Siblings to add to.
2141 *
2142 * @return LY_ERR values.
2143 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002144LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002145parse_leaf(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002146{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002147 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002148 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002149 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002150 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002151 struct lysp_node_leaf *leaf;
2152
David Sedlák60adc092019-08-06 15:57:02 +02002153 /* create new leaf structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002154 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, leaf, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002155 leaf->nodetype = LYS_LEAF;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002156 leaf->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002157
Michal Vasko7fbc8162018-09-17 10:35:16 +02002158 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002159 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002160 INSERT_WORD_RET(ctx, buf, leaf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002161
2162 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01002163 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002164 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002165 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002166 LY_CHECK_RET(parse_config(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002167 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002168 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01002169 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 +02002170 leaf->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002171 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002172 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002173 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 +02002174 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002175 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002176 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &leaf->iffeatures, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002177 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002178 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002179 LY_CHECK_RET(parse_mandatory(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002180 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002181 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002182 LY_CHECK_RET(parse_restrs(ctx, kw, &leaf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002183 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002184 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002185 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 +02002186 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002187 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002188 LY_CHECK_RET(parse_status(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002189 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002190 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002191 LY_CHECK_RET(parse_type(ctx, &leaf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002192 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002193 case LY_STMT_UNITS:
Radek Krejcifc596f92021-02-26 22:40:26 +01002194 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 +02002195 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002196 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002197 LY_CHECK_RET(parse_when(ctx, &leaf->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002198 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002199 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002200 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_LEAF, 0, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002201 break;
2202 default:
David Sedlákb3077192019-06-19 10:55:37 +02002203 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002204 return LY_EVALID;
2205 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002206 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002207 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002208checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002209 /* mandatory substatements */
2210 if (!leaf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002211 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002212 return LY_EVALID;
2213 }
2214
2215 return ret;
2216}
2217
Michal Vaskoea5abea2018-09-18 13:10:54 +02002218/**
2219 * @brief Parse the max-elements statement.
2220 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002221 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002222 * @param[in,out] max Value to write to.
2223 * @param[in,out] flags Flags to write to.
2224 * @param[in,out] exts Extension instances to add to.
2225 *
2226 * @return LY_ERR values.
2227 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002228LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002229parse_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 +02002230{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002231 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002232 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002233 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002234 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002235 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002236
2237 if (*flags & LYS_SET_MAX) {
David Sedlákb3077192019-06-19 10:55:37 +02002238 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002239 return LY_EVALID;
2240 }
2241 *flags |= LYS_SET_MAX;
2242
2243 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002244 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002245
2246 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
David Sedlákb3077192019-06-19 10:55:37 +02002247 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002248 free(buf);
2249 return LY_EVALID;
2250 }
2251
Radek Krejci7f9b6512019-09-18 13:11:09 +02002252 if (ly_strncmp("unbounded", word, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002253 errno = 0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002254 num = strtoul(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002255 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002256 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002257 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002258 free(buf);
2259 return LY_EVALID;
2260 }
2261 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002262 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002263 free(buf);
2264 return LY_EVALID;
2265 }
2266
2267 *max = num;
Radek Krejcid6315102021-02-02 15:26:34 +01002268 } else {
2269 /* unbounded */
2270 *max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002271 }
2272 free(buf);
2273
Radek Krejci33090f92020-12-17 20:12:46 +01002274 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002275 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002276 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002277 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MAX_ELEMENTS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002278 break;
2279 default:
David Sedlákb3077192019-06-19 10:55:37 +02002280 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002281 return LY_EVALID;
2282 }
2283 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002284 return ret;
2285}
2286
Michal Vaskoea5abea2018-09-18 13:10:54 +02002287/**
2288 * @brief Parse the min-elements statement.
2289 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002290 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002291 * @param[in,out] min Value to write to.
2292 * @param[in,out] flags Flags to write to.
2293 * @param[in,out] exts Extension instances to add to.
2294 *
2295 * @return LY_ERR values.
2296 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002297LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002298parse_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 +02002299{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002300 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002301 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002302 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002303 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002304 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002305
2306 if (*flags & LYS_SET_MIN) {
David Sedlákb3077192019-06-19 10:55:37 +02002307 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002308 return LY_EVALID;
2309 }
2310 *flags |= LYS_SET_MIN;
2311
2312 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002313 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002314
2315 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
David Sedlákb3077192019-06-19 10:55:37 +02002316 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002317 free(buf);
2318 return LY_EVALID;
2319 }
2320
2321 errno = 0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002322 num = strtoul(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002323 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002324 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002325 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002326 free(buf);
2327 return LY_EVALID;
2328 }
2329 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002330 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002331 free(buf);
2332 return LY_EVALID;
2333 }
2334 *min = num;
2335 free(buf);
2336
Radek Krejci33090f92020-12-17 20:12:46 +01002337 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002338 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002339 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002340 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MIN_ELEMENTS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002341 break;
2342 default:
David Sedlákb3077192019-06-19 10:55:37 +02002343 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002344 return LY_EVALID;
2345 }
2346 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002347 return ret;
2348}
2349
Michal Vaskoea5abea2018-09-18 13:10:54 +02002350/**
2351 * @brief Parse the ordered-by statement.
2352 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002353 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002354 * @param[in,out] flags Flags to write to.
2355 * @param[in,out] exts Extension instances to add to.
2356 *
2357 * @return LY_ERR values.
2358 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002359static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002360parse_orderedby(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002361{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002362 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002363 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002364 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002365 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002366
2367 if (*flags & LYS_ORDBY_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02002368 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002369 return LY_EVALID;
2370 }
2371
2372 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002373 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002374
Radek Krejcif13b87b2020-12-01 22:02:17 +01002375 if ((word_len == ly_strlen_const("system")) && !strncmp(word, "system", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002376 *flags |= LYS_ORDBY_SYSTEM;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002377 } else if ((word_len == ly_strlen_const("user")) && !strncmp(word, "user", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002378 *flags |= LYS_ORDBY_USER;
2379 } else {
David Sedlákb3077192019-06-19 10:55:37 +02002380 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002381 free(buf);
2382 return LY_EVALID;
2383 }
2384 free(buf);
2385
Radek Krejci33090f92020-12-17 20:12:46 +01002386 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002387 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002388 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002389 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_ORDERED_BY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002390 break;
2391 default:
David Sedlákb3077192019-06-19 10:55:37 +02002392 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002393 return LY_EVALID;
2394 }
2395 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002396 return ret;
2397}
2398
Michal Vaskoea5abea2018-09-18 13:10:54 +02002399/**
2400 * @brief Parse the leaf-list statement.
2401 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002402 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002403 * @param[in,out] siblings Siblings to add to.
2404 *
2405 * @return LY_ERR values.
2406 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002407LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002408parse_leaflist(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002409{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002410 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002411 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002412 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002413 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002414 struct lysp_node_leaflist *llist;
2415
David Sedlák60adc092019-08-06 15:57:02 +02002416 /* create new leaf-list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002417 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, llist, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002418 llist->nodetype = LYS_LEAFLIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002419 llist->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002420
Michal Vasko7fbc8162018-09-17 10:35:16 +02002421 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002422 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002423 INSERT_WORD_RET(ctx, buf, llist->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002424
2425 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01002426 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002427 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002428 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002429 LY_CHECK_RET(parse_config(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002430 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002431 case LY_STMT_DEFAULT:
Radek Krejci335332a2019-09-05 13:03:35 +02002432 PARSER_CHECK_STMTVER2_RET(ctx, "default", "leaf-list");
Radek Krejcifc596f92021-02-26 22:40:26 +01002433 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, &llist->dflts, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002434 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002435 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002436 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 +02002437 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002438 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002439 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &llist->iffeatures, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002440 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002441 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002442 LY_CHECK_RET(parse_maxelements(ctx, &llist->max, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002443 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002444 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002445 LY_CHECK_RET(parse_minelements(ctx, &llist->min, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002446 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002447 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002448 LY_CHECK_RET(parse_restrs(ctx, kw, &llist->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002449 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002450 case LY_STMT_ORDERED_BY:
Radek Krejci33090f92020-12-17 20:12:46 +01002451 LY_CHECK_RET(parse_orderedby(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002452 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002453 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002454 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 +02002455 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002456 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002457 LY_CHECK_RET(parse_status(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002458 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002459 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002460 LY_CHECK_RET(parse_type(ctx, &llist->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002461 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002462 case LY_STMT_UNITS:
Radek Krejcifc596f92021-02-26 22:40:26 +01002463 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 +02002464 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002465 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002466 LY_CHECK_RET(parse_when(ctx, &llist->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002467 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002468 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002469 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_LEAF_LIST, 0, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002470 break;
2471 default:
David Sedlákb3077192019-06-19 10:55:37 +02002472 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002473 return LY_EVALID;
2474 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002475 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002476 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002477checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002478 /* mandatory substatements */
2479 if (!llist->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002480 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002481 return LY_EVALID;
2482 }
2483
2484 return ret;
2485}
2486
Michal Vaskoea5abea2018-09-18 13:10:54 +02002487/**
2488 * @brief Parse the refine statement.
2489 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002490 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002491 * @param[in,out] refines Refines to add to.
2492 *
2493 * @return LY_ERR values.
2494 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002495static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002496parse_refine(struct lys_yang_parser_ctx *ctx, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002497{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002498 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002499 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002500 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002501 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002502 struct lysp_refine *rf;
2503
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002504 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002505
2506 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002507 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01002508 CHECK_NONEMPTY(ctx, word_len, "refine");
Radek Krejci011e4aa2020-09-04 15:22:31 +02002509 INSERT_WORD_RET(ctx, buf, rf->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002510
Radek Krejci33090f92020-12-17 20:12:46 +01002511 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002512 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002513 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002514 LY_CHECK_RET(parse_config(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002515 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002516 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01002517 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, &rf->dflts, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002518 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002519 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002520 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 +02002521 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002522 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02002523 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "refine");
Radek Krejcifc596f92021-02-26 22:40:26 +01002524 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &rf->iffeatures, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002525 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002526 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002527 LY_CHECK_RET(parse_maxelements(ctx, &rf->max, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002528 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002529 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002530 LY_CHECK_RET(parse_minelements(ctx, &rf->min, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002531 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002532 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002533 LY_CHECK_RET(parse_restrs(ctx, kw, &rf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002534 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002535 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002536 LY_CHECK_RET(parse_mandatory(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002537 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002538 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002539 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 +02002540 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002541 case LY_STMT_PRESENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002542 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 +02002543 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002544 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002545 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_REFINE, 0, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002546 break;
2547 default:
David Sedlákb3077192019-06-19 10:55:37 +02002548 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002549 return LY_EVALID;
2550 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002551 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002552 return ret;
2553}
2554
Michal Vaskoea5abea2018-09-18 13:10:54 +02002555/**
2556 * @brief Parse the typedef statement.
2557 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002558 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002559 * @param[in,out] typedefs Typedefs to add to.
2560 *
2561 * @return LY_ERR values.
2562 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002563static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002564parse_typedef(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002565{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002566 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002567 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002568 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002569 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002570 struct lysp_tpdf *tpdf;
2571
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002572 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002573
2574 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002575 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002576 INSERT_WORD_RET(ctx, buf, tpdf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002577
2578 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01002579 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002580 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002581 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01002582 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 +02002583 tpdf->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002584 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002585 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002586 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 +02002587 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002588 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002589 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 +02002590 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002591 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002592 LY_CHECK_RET(parse_status(ctx, &tpdf->flags, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002593 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002594 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002595 LY_CHECK_RET(parse_type(ctx, &tpdf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002596 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002597 case LY_STMT_UNITS:
Radek Krejcifc596f92021-02-26 22:40:26 +01002598 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 +02002599 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002600 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002601 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_TYPEDEF, 0, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002602 break;
2603 default:
David Sedlákb3077192019-06-19 10:55:37 +02002604 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002605 return LY_EVALID;
2606 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002607 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002608 LY_CHECK_RET(ret);
Radek Krejcibbe09a92018-11-08 09:36:54 +01002609checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002610 /* mandatory substatements */
2611 if (!tpdf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002612 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002613 return LY_EVALID;
2614 }
2615
Radek Krejcibbe09a92018-11-08 09:36:54 +01002616 /* store data for collision check */
Michal Vasko7f45cf22020-10-01 12:49:44 +02002617 if (parent && !(parent->nodetype & (LYS_GROUPING | LYS_RPC | LYS_ACTION | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF))) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02002618 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, parent, 0, NULL));
Radek Krejcibbe09a92018-11-08 09:36:54 +01002619 }
2620
Michal Vasko7fbc8162018-09-17 10:35:16 +02002621 return ret;
2622}
2623
Michal Vaskoea5abea2018-09-18 13:10:54 +02002624/**
2625 * @brief Parse the input or output statement.
2626 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002627 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002628 * @param[in] kw Type of this particular keyword
2629 * @param[in,out] inout_p Input/output pointer to write to.
2630 *
2631 * @return LY_ERR values.
2632 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002633static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002634parse_inout(struct lys_yang_parser_ctx *ctx, enum ly_stmt inout_kw, struct lysp_node *parent,
Radek Krejci2a9fc652021-01-22 17:44:34 +01002635 struct lysp_node_action_inout *inout_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002636{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002637 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002638 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002639 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002640 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002641 ly_bool input = &((struct lysp_node_action *)parent)->input == inout_p ? 1 : 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002642
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002643 if (inout_p->nodetype) {
David Sedlákb3077192019-06-19 10:55:37 +02002644 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002645 return LY_EVALID;
2646 }
2647
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002648 /* initiate structure */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002649 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), input ? "input" : "output", 0, &inout_p->name));
2650 inout_p->nodetype = input ? LYS_INPUT : LYS_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002651 inout_p->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002652
2653 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01002654 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002655 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002656 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002657 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", ly_stmt2str(inout_kw));
Radek Krejci0f969882020-08-21 16:56:47 +02002658 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002659 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002660 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002661 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002662 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01002663 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002664 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002665 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002666 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002667 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002668 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01002669 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002670 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002671 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002672 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002673 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002674 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002675 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002676 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002677 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01002678 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002679 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002680 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002681 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)inout_p, &inout_p->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002682 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002683 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002684 PARSER_CHECK_STMTVER2_RET(ctx, "must", ly_stmt2str(inout_kw));
Radek Krejci33090f92020-12-17 20:12:46 +01002685 LY_CHECK_RET(parse_restrs(ctx, kw, &inout_p->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002686 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002687 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002688 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)inout_p, &inout_p->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002689 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002690 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002691 LY_CHECK_RET(parse_ext(ctx, word, word_len, inout_kw, 0, &inout_p->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002692 break;
2693 default:
David Sedlákb3077192019-06-19 10:55:37 +02002694 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002695 return LY_EVALID;
2696 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002697 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002698 LY_CHECK_RET(ret);
Michal Vaskob83af8a2020-01-06 09:49:22 +01002699
Radek Krejci7fc68292019-06-12 13:51:09 +02002700checks:
Radek Krejci01180ac2021-01-27 08:48:22 +01002701 if (!inout_p->child) {
Michal Vaskob83af8a2020-01-06 09:49:22 +01002702 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "data-def-stmt", ly_stmt2str(inout_kw));
2703 return LY_EVALID;
2704 }
2705
Michal Vasko7fbc8162018-09-17 10:35:16 +02002706 return ret;
2707}
2708
Michal Vaskoea5abea2018-09-18 13:10:54 +02002709/**
2710 * @brief Parse the action statement.
2711 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002712 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002713 * @param[in,out] actions Actions to add to.
2714 *
2715 * @return LY_ERR values.
2716 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002717LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002718parse_action(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002719{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002720 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002721 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002722 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002723 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002724 struct lysp_node_action *act;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002725
Radek Krejci2a9fc652021-01-22 17:44:34 +01002726 LY_LIST_NEW_RET(PARSER_CTX(ctx), actions, act, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002727
2728 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002729 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002730 INSERT_WORD_RET(ctx, buf, act->name, word, word_len);
Michal Vasko1bf09392020-03-27 12:38:10 +01002731 act->nodetype = parent ? LYS_ACTION : LYS_RPC;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002732 act->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002733
Radek Krejci33090f92020-12-17 20:12:46 +01002734 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002735 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002736 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002737 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 +02002738 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002739 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002740 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &act->iffeatures, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002741 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002742 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002743 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 +02002744 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002745 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002746 LY_CHECK_RET(parse_status(ctx, &act->flags, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002747 break;
2748
Radek Krejcid6b76452019-09-03 17:03:03 +02002749 case LY_STMT_INPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002750 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->input));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002751 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002752 case LY_STMT_OUTPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002753 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->output));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002754 break;
2755
Radek Krejcid6b76452019-09-03 17:03:03 +02002756 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002757 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)act, &act->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002758 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002759 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002760 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)act, &act->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002761 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002762 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002763 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 +02002764 break;
2765 default:
David Sedlákb3077192019-06-19 10:55:37 +02002766 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), parent ? "action" : "rpc");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002767 return LY_EVALID;
2768 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002769 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002770 LY_CHECK_RET(ret);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002771
Radek Krejci2a9fc652021-01-22 17:44:34 +01002772checks:
Michal Vasko7f45cf22020-10-01 12:49:44 +02002773 /* always initialize inout, they are technically present (needed for later deviations/refines) */
2774 if (!act->input.nodetype) {
2775 act->input.nodetype = LYS_INPUT;
2776 act->input.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002777 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "input", 0, &act->input.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002778 }
2779 if (!act->output.nodetype) {
2780 act->output.nodetype = LYS_OUTPUT;
2781 act->output.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002782 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "output", 0, &act->output.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002783 }
2784
Michal Vasko7fbc8162018-09-17 10:35:16 +02002785 return ret;
2786}
2787
Michal Vaskoea5abea2018-09-18 13:10:54 +02002788/**
2789 * @brief Parse the notification statement.
2790 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002791 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002792 * @param[in,out] notifs Notifications to add to.
2793 *
2794 * @return LY_ERR values.
2795 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002796LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002797parse_notif(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002798{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002799 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002800 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002801 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002802 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002803 struct lysp_node_notif *notif;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002804
Radek Krejci2a9fc652021-01-22 17:44:34 +01002805 LY_LIST_NEW_RET(PARSER_CTX(ctx), notifs, notif, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002806
2807 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002808 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002809 INSERT_WORD_RET(ctx, buf, notif->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002810 notif->nodetype = LYS_NOTIF;
2811 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002812
Radek Krejci2a9fc652021-01-22 17:44:34 +01002813 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002814 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002815 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002816 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 +02002817 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002818 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002819 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &notif->iffeatures, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002820 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002821 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002822 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 +02002823 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002824 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002825 LY_CHECK_RET(parse_status(ctx, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002826 break;
2827
Radek Krejcid6b76452019-09-03 17:03:03 +02002828 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002829 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "notification");
Radek Krejci0f969882020-08-21 16:56:47 +02002830 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002831 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002832 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002833 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002834 case LY_STMT_CHOICE:
Michal Vasko5ae1e1a2021-02-08 09:53:26 +01002835 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002836 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002837 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002838 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002839 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002840 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01002841 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002842 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002843 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002844 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002845 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002846 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002847 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002848 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002849 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01002850 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002851 break;
2852
Radek Krejcid6b76452019-09-03 17:03:03 +02002853 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002854 PARSER_CHECK_STMTVER2_RET(ctx, "must", "notification");
Radek Krejci33090f92020-12-17 20:12:46 +01002855 LY_CHECK_RET(parse_restrs(ctx, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002856 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002857 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002858 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)notif, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002859 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002860 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002861 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002862 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002863 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002864 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_NOTIFICATION, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002865 break;
2866 default:
David Sedlákb3077192019-06-19 10:55:37 +02002867 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002868 return LY_EVALID;
2869 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002870 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002871
Michal Vasko7fbc8162018-09-17 10:35:16 +02002872 return ret;
2873}
2874
Michal Vaskoea5abea2018-09-18 13:10:54 +02002875/**
2876 * @brief Parse the grouping statement.
2877 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002878 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002879 * @param[in,out] groupings Groupings to add to.
2880 *
2881 * @return LY_ERR values.
2882 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002883LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002884parse_grouping(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002885{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002886 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002887 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002888 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002889 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002890 struct lysp_node_grp *grp;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002891
Radek Krejci2a9fc652021-01-22 17:44:34 +01002892 LY_LIST_NEW_RET(PARSER_CTX(ctx), groupings, grp, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002893
2894 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002895 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002896 INSERT_WORD_RET(ctx, buf, grp->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002897 grp->nodetype = LYS_GROUPING;
2898 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002899
Radek Krejci2a9fc652021-01-22 17:44:34 +01002900 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002901 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002902 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002903 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 +02002904 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002905 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002906 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 +02002907 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002908 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002909 LY_CHECK_RET(parse_status(ctx, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002910 break;
2911
Radek Krejcid6b76452019-09-03 17:03:03 +02002912 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002913 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "grouping");
Radek Krejci0f969882020-08-21 16:56:47 +02002914 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002915 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002916 LY_CHECK_RET(parse_any(ctx, kw, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002917 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002918 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01002919 LY_CHECK_RET(parse_choice(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002920 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002921 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002922 LY_CHECK_RET(parse_container(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002923 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002924 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01002925 LY_CHECK_RET(parse_leaf(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002926 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002927 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002928 LY_CHECK_RET(parse_leaflist(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002929 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002930 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002931 LY_CHECK_RET(parse_list(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002932 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002933 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01002934 LY_CHECK_RET(parse_uses(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002935 break;
2936
Radek Krejcid6b76452019-09-03 17:03:03 +02002937 case LY_STMT_TYPEDEF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01002938 LY_CHECK_RET(parse_typedef(ctx, &grp->node, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002939 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002940 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02002941 PARSER_CHECK_STMTVER2_RET(ctx, "action", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002942 LY_CHECK_RET(parse_action(ctx, &grp->node, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002943 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002944 case LY_STMT_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01002945 LY_CHECK_RET(parse_grouping(ctx, &grp->node, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002946 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002947 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02002948 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002949 LY_CHECK_RET(parse_notif(ctx, &grp->node, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002950 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002951 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002952 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_GROUPING, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002953 break;
2954 default:
David Sedlákb3077192019-06-19 10:55:37 +02002955 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002956 return LY_EVALID;
2957 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002958 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002959
Michal Vasko7fbc8162018-09-17 10:35:16 +02002960 return ret;
2961}
2962
Michal Vaskoea5abea2018-09-18 13:10:54 +02002963/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02002964 * @brief Parse the augment statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002965 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002966 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002967 * @param[in,out] augments Augments to add to.
2968 *
2969 * @return LY_ERR values.
2970 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002971LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002972parse_augment(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002973{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002974 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002975 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002976 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002977 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002978 struct lysp_node_augment *aug;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002979
Radek Krejci2a9fc652021-01-22 17:44:34 +01002980 LY_LIST_NEW_RET(PARSER_CTX(ctx), augments, aug, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002981
2982 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002983 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01002984 CHECK_NONEMPTY(ctx, word_len, "augment");
Radek Krejci011e4aa2020-09-04 15:22:31 +02002985 INSERT_WORD_RET(ctx, buf, aug->nodeid, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002986 aug->nodetype = LYS_AUGMENT;
2987 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002988
Radek Krejci2a9fc652021-01-22 17:44:34 +01002989 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002990 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002991 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01002992 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 +02002993 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002994 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002995 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &aug->iffeatures, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002996 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002997 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002998 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 +02002999 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003000 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003001 LY_CHECK_RET(parse_status(ctx, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003002 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003003 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003004 LY_CHECK_RET(parse_when(ctx, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003005 break;
3006
Radek Krejcid6b76452019-09-03 17:03:03 +02003007 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003008 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "augment");
Radek Krejci0f969882020-08-21 16:56:47 +02003009 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003010 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003011 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003012 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003013 case LY_STMT_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003014 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003015 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003016 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003017 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003018 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003019 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003020 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003021 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003022 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003023 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003024 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003025 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003026 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003027 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003028 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003029 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003030 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003031 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003032 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003033 break;
3034
Radek Krejcid6b76452019-09-03 17:03:03 +02003035 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003036 PARSER_CHECK_STMTVER2_RET(ctx, "action", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003037 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003038 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003039 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003040 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003041 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003042 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003043 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003044 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_AUGMENT, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003045 break;
3046 default:
David Sedlákb3077192019-06-19 10:55:37 +02003047 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003048 return LY_EVALID;
3049 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003050 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003051
Michal Vasko7fbc8162018-09-17 10:35:16 +02003052 return ret;
3053}
3054
Michal Vaskoea5abea2018-09-18 13:10:54 +02003055/**
3056 * @brief Parse the uses statement.
3057 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003058 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003059 * @param[in,out] siblings Siblings to add to.
3060 *
3061 * @return LY_ERR values.
3062 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003063LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003064parse_uses(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003065{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003066 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003067 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003068 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003069 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003070 struct lysp_node_uses *uses;
3071
David Sedlák60adc092019-08-06 15:57:02 +02003072 /* create uses structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003073 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, uses, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003074 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003075 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003076
Michal Vasko7fbc8162018-09-17 10:35:16 +02003077 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003078 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003079 INSERT_WORD_RET(ctx, buf, uses->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003080
3081 /* parse substatements */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003082 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003083 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003084 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003085 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 +02003086 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003087 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003088 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &uses->iffeatures, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003089 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003090 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003091 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 +02003092 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003093 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003094 LY_CHECK_RET(parse_status(ctx, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003095 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003096 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003097 LY_CHECK_RET(parse_when(ctx, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003098 break;
3099
Radek Krejcid6b76452019-09-03 17:03:03 +02003100 case LY_STMT_REFINE:
Radek Krejci33090f92020-12-17 20:12:46 +01003101 LY_CHECK_RET(parse_refine(ctx, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003102 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003103 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003104 LY_CHECK_RET(parse_augment(ctx, (struct lysp_node *)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003105 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003106 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003107 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_USES, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003108 break;
3109 default:
David Sedlákb3077192019-06-19 10:55:37 +02003110 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003111 return LY_EVALID;
3112 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003113 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003114
Michal Vasko7fbc8162018-09-17 10:35:16 +02003115 return ret;
3116}
3117
Michal Vaskoea5abea2018-09-18 13:10:54 +02003118/**
3119 * @brief Parse the case statement.
3120 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003121 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003122 * @param[in,out] siblings Siblings to add to.
3123 *
3124 * @return LY_ERR values.
3125 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003126LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003127parse_case(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003128{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003129 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003130 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003131 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003132 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003133 struct lysp_node_case *cas;
3134
David Sedlák60adc092019-08-06 15:57:02 +02003135 /* create new case structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003136 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cas, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003137 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003138 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003139
Michal Vasko7fbc8162018-09-17 10:35:16 +02003140 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003141 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003142 INSERT_WORD_RET(ctx, buf, cas->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003143
3144 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01003145 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003146 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003147 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003148 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 +02003149 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003150 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003151 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &cas->iffeatures, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003152 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003153 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003154 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 +02003155 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003156 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003157 LY_CHECK_RET(parse_status(ctx, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003158 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003159 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003160 LY_CHECK_RET(parse_when(ctx, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003161 break;
3162
Radek Krejcid6b76452019-09-03 17:03:03 +02003163 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003164 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "case");
Radek Krejci0f969882020-08-21 16:56:47 +02003165 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003166 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003167 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003168 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003169 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003170 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003171 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003172 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003173 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003174 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003175 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003176 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003177 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003178 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003179 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003180 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003181 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003182 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003183 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003184 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003185 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003186 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003187 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003188 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_CASE, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003189 break;
3190 default:
David Sedlákb3077192019-06-19 10:55:37 +02003191 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003192 return LY_EVALID;
3193 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003194 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003195 return ret;
3196}
3197
Michal Vaskoea5abea2018-09-18 13:10:54 +02003198/**
3199 * @brief Parse the choice statement.
3200 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003201 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003202 * @param[in,out] siblings Siblings to add to.
3203 *
3204 * @return LY_ERR values.
3205 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003206LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003207parse_choice(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003208{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003209 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003210 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003211 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003212 enum ly_stmt kw;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003213 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003214
David Sedlák60adc092019-08-06 15:57:02 +02003215 /* create new choice structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003216 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, choice, next, LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003217 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003218 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003219
Michal Vasko7fbc8162018-09-17 10:35:16 +02003220 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003221 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003222 INSERT_WORD_RET(ctx, buf, choice->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003223
3224 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01003225 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003226 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003227 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003228 LY_CHECK_RET(parse_config(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003229 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003230 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003231 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 +02003232 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003233 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003234 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &choice->iffeatures, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003235 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003236 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01003237 LY_CHECK_RET(parse_mandatory(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003238 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003239 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003240 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 +02003241 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003242 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003243 LY_CHECK_RET(parse_status(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003244 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003245 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003246 LY_CHECK_RET(parse_when(ctx, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003247 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003248 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01003249 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 +02003250 &choice->exts));
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003251 choice->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003252 break;
3253
Radek Krejcid6b76452019-09-03 17:03:03 +02003254 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003255 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "choice");
Radek Krejci0f969882020-08-21 16:56:47 +02003256 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003257 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003258 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003259 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003260 case LY_STMT_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003261 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003262 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003263 case LY_STMT_CHOICE:
Radek Krejci335332a2019-09-05 13:03:35 +02003264 PARSER_CHECK_STMTVER2_RET(ctx, "choice", "choice");
Radek Krejci33090f92020-12-17 20:12:46 +01003265 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003266 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003267 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003268 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003269 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003270 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003271 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003272 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003273 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003274 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003275 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003276 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003277 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003278 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003279 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003280 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_CHOICE, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003281 break;
3282 default:
David Sedlákb3077192019-06-19 10:55:37 +02003283 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003284 return LY_EVALID;
3285 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003286 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003287 return ret;
3288}
3289
Michal Vaskoea5abea2018-09-18 13:10:54 +02003290/**
3291 * @brief Parse the container statement.
3292 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003293 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003294 * @param[in,out] siblings Siblings to add to.
3295 *
3296 * @return LY_ERR values.
3297 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003298LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003299parse_container(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003300{
3301 LY_ERR ret = 0;
3302 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003303 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003304 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003305 struct lysp_node_container *cont;
3306
David Sedlák60adc092019-08-06 15:57:02 +02003307 /* create new container structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003308 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cont, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003309 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003310 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003311
Michal Vasko7fbc8162018-09-17 10:35:16 +02003312 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003313 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003314 INSERT_WORD_RET(ctx, buf, cont->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003315
3316 /* parse substatements */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003317 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003318 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003319 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003320 LY_CHECK_RET(parse_config(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003321 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003322 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003323 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 +02003324 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003325 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003326 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &cont->iffeatures, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003327 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003328 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003329 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 +02003330 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003331 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003332 LY_CHECK_RET(parse_status(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003333 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003334 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003335 LY_CHECK_RET(parse_when(ctx, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003336 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003337 case LY_STMT_PRESENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003338 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 +02003339 break;
3340
Radek Krejcid6b76452019-09-03 17:03:03 +02003341 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003342 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "container");
Radek Krejci0f969882020-08-21 16:56:47 +02003343 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003344 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003345 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003346 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003347 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003348 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003349 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003350 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003351 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003352 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003353 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003354 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003355 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003356 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003357 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003358 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003359 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003360 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003361 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003362 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003363 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003364 break;
3365
Radek Krejcid6b76452019-09-03 17:03:03 +02003366 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003367 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)cont, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003368 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003369 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003370 LY_CHECK_RET(parse_restrs(ctx, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003371 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003372 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003373 PARSER_CHECK_STMTVER2_RET(ctx, "action", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003374 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003375 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003376 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003377 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003378 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003379 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003380 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003381 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003382 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003383 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003384 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_CONTAINER, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003385 break;
3386 default:
David Sedlákb3077192019-06-19 10:55:37 +02003387 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003388 return LY_EVALID;
3389 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003390 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003391
Michal Vasko7fbc8162018-09-17 10:35:16 +02003392 return ret;
3393}
3394
Michal Vaskoea5abea2018-09-18 13:10:54 +02003395/**
3396 * @brief Parse the list statement.
3397 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003398 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003399 * @param[in,out] siblings Siblings to add to.
3400 *
3401 * @return LY_ERR values.
3402 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003403LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003404parse_list(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003405{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003406 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003407 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003408 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003409 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003410 struct lysp_node_list *list;
3411
David Sedlák60adc092019-08-06 15:57:02 +02003412 /* create new list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003413 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, list, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003414 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003415 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003416
Michal Vasko7fbc8162018-09-17 10:35:16 +02003417 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003418 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003419 INSERT_WORD_RET(ctx, buf, list->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003420
3421 /* parse substatements */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003422 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003423 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003424 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003425 LY_CHECK_RET(parse_config(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003426 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003427 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003428 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 +02003429 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003430 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003431 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &list->iffeatures, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003432 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003433 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003434 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 +02003435 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003436 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003437 LY_CHECK_RET(parse_status(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003438 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003439 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003440 LY_CHECK_RET(parse_when(ctx, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003441 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003442 case LY_STMT_KEY:
Radek Krejcifc596f92021-02-26 22:40:26 +01003443 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 +02003444 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003445 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003446 LY_CHECK_RET(parse_maxelements(ctx, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003447 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003448 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003449 LY_CHECK_RET(parse_minelements(ctx, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003450 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003451 case LY_STMT_ORDERED_BY:
Radek Krejci33090f92020-12-17 20:12:46 +01003452 LY_CHECK_RET(parse_orderedby(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003453 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003454 case LY_STMT_UNIQUE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003455 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_UNIQUE, &list->uniques, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003456 break;
3457
Radek Krejcid6b76452019-09-03 17:03:03 +02003458 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003459 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "list");
Radek Krejci0f969882020-08-21 16:56:47 +02003460 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003461 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003462 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003463 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003464 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003465 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003466 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003467 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003468 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003469 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003470 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003471 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003472 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003473 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003474 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003475 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003476 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003477 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003478 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003479 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003480 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003481 break;
3482
Radek Krejcid6b76452019-09-03 17:03:03 +02003483 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003484 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)list, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003485 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003486 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003487 LY_CHECK_RET(parse_restrs(ctx, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003488 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003489 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003490 PARSER_CHECK_STMTVER2_RET(ctx, "action", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003491 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003492 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003493 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003494 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003495 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003496 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003497 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003498 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003499 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003500 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003501 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_LIST, 0, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003502 break;
3503 default:
David Sedlákb3077192019-06-19 10:55:37 +02003504 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003505 return LY_EVALID;
3506 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003507 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003508
Michal Vasko7fbc8162018-09-17 10:35:16 +02003509 return ret;
3510}
3511
Michal Vaskoea5abea2018-09-18 13:10:54 +02003512/**
3513 * @brief Parse the yin-element statement.
3514 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003515 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003516 * @param[in,out] flags Flags to write to.
3517 * @param[in,out] exts Extension instances to add to.
3518 *
3519 * @return LY_ERR values.
3520 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003521static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003522parse_yinelement(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003523{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003524 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003525 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003526 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003527 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003528
3529 if (*flags & LYS_YINELEM_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02003530 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003531 return LY_EVALID;
3532 }
3533
3534 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003535 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003536
Radek Krejcif13b87b2020-12-01 22:02:17 +01003537 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003538 *flags |= LYS_YINELEM_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003539 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003540 *flags |= LYS_YINELEM_FALSE;
3541 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003542 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003543 free(buf);
3544 return LY_EVALID;
3545 }
3546 free(buf);
3547
Radek Krejci33090f92020-12-17 20:12:46 +01003548 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003549 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003550 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003551 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_YIN_ELEMENT, 0, exts));
Michal Vaskod989ba02020-08-24 10:59:24 +02003552 LY_CHECK_RET(ret);
3553 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003554 default:
David Sedlákb3077192019-06-19 10:55:37 +02003555 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003556 return LY_EVALID;
3557 }
3558 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003559 return ret;
3560}
3561
Michal Vaskoea5abea2018-09-18 13:10:54 +02003562/**
David Sedlák2444f8f2019-07-09 11:02:47 +02003563 * @brief Parse the argument statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003564 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003565 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003566 * @param[in,out] argument Value to write to.
3567 * @param[in,out] flags Flags to write to.
3568 * @param[in,out] exts Extension instances to add to.
3569 *
3570 * @return LY_ERR values.
3571 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003572static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003573parse_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 +02003574{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003575 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003576 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003577 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003578 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003579
3580 if (*argument) {
David Sedlákb3077192019-06-19 10:55:37 +02003581 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003582 return LY_EVALID;
3583 }
3584
3585 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003586 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003587 INSERT_WORD_RET(ctx, buf, *argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003588
Radek Krejci33090f92020-12-17 20:12:46 +01003589 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003590 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003591 case LY_STMT_YIN_ELEMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003592 LY_CHECK_RET(parse_yinelement(ctx, flags, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003593 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003594 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003595 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_ARGUMENT, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003596 break;
3597 default:
David Sedlákb3077192019-06-19 10:55:37 +02003598 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003599 return LY_EVALID;
3600 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003601 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003602 return ret;
3603}
3604
Michal Vaskoea5abea2018-09-18 13:10:54 +02003605/**
3606 * @brief Parse the extension statement.
3607 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003608 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003609 * @param[in,out] extensions Extensions to add to.
3610 *
3611 * @return LY_ERR values.
3612 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003613static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003614parse_extension(struct lys_yang_parser_ctx *ctx, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003615{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003616 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003617 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003618 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003619 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003620 struct lysp_ext *ex;
3621
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003622 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003623
3624 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003625 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003626 INSERT_WORD_RET(ctx, buf, ex->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003627
Radek Krejci33090f92020-12-17 20:12:46 +01003628 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003629 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003630 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003631 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 +02003632 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003633 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003634 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 +02003635 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003636 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003637 LY_CHECK_RET(parse_status(ctx, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003638 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003639 case LY_STMT_ARGUMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003640 LY_CHECK_RET(parse_argument(ctx, &ex->argument, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003641 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003642 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003643 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_EXTENSION, 0, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003644 break;
3645 default:
David Sedlákb3077192019-06-19 10:55:37 +02003646 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003647 return LY_EVALID;
3648 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003649 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003650 return ret;
3651}
3652
Michal Vaskoea5abea2018-09-18 13:10:54 +02003653/**
3654 * @brief Parse the deviate statement.
3655 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003656 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003657 * @param[in,out] deviates Deviates to add to.
3658 *
3659 * @return LY_ERR values.
3660 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003661LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003662parse_deviate(struct lys_yang_parser_ctx *ctx, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003663{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003664 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003665 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003666 size_t word_len, dev_mod;
Radek Krejcid6b76452019-09-03 17:03:03 +02003667 enum ly_stmt kw;
David Sedlák60adc092019-08-06 15:57:02 +02003668 struct lysp_deviate *d;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003669 struct lysp_deviate_add *d_add = NULL;
3670 struct lysp_deviate_rpl *d_rpl = NULL;
3671 struct lysp_deviate_del *d_del = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003672 const char **d_units = NULL;
3673 struct lysp_qname **d_uniques = NULL, **d_dflts = NULL;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02003674 struct lysp_restr **d_musts = NULL;
3675 uint16_t *d_flags = 0;
3676 uint32_t *d_min = 0, *d_max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003677
3678 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003679 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003680
Radek Krejcif13b87b2020-12-01 22:02:17 +01003681 if ((word_len == ly_strlen_const("not-supported")) && !strncmp(word, "not-supported", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003682 dev_mod = LYS_DEV_NOT_SUPPORTED;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003683 } else if ((word_len == ly_strlen_const("add")) && !strncmp(word, "add", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003684 dev_mod = LYS_DEV_ADD;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003685 } else if ((word_len == ly_strlen_const("replace")) && !strncmp(word, "replace", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003686 dev_mod = LYS_DEV_REPLACE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003687 } else if ((word_len == ly_strlen_const("delete")) && !strncmp(word, "delete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003688 dev_mod = LYS_DEV_DELETE;
3689 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003690 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003691 free(buf);
3692 return LY_EVALID;
3693 }
3694 free(buf);
3695
3696 /* create structure */
3697 switch (dev_mod) {
3698 case LYS_DEV_NOT_SUPPORTED:
3699 d = calloc(1, sizeof *d);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003700 LY_CHECK_ERR_RET(!d, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003701 break;
3702 case LYS_DEV_ADD:
3703 d_add = calloc(1, sizeof *d_add);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003704 LY_CHECK_ERR_RET(!d_add, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003705 d = (struct lysp_deviate *)d_add;
3706 d_units = &d_add->units;
3707 d_uniques = &d_add->uniques;
3708 d_dflts = &d_add->dflts;
3709 d_musts = &d_add->musts;
3710 d_flags = &d_add->flags;
3711 d_min = &d_add->min;
3712 d_max = &d_add->max;
3713 break;
3714 case LYS_DEV_REPLACE:
3715 d_rpl = calloc(1, sizeof *d_rpl);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003716 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003717 d = (struct lysp_deviate *)d_rpl;
3718 d_units = &d_rpl->units;
3719 d_flags = &d_rpl->flags;
3720 d_min = &d_rpl->min;
3721 d_max = &d_rpl->max;
3722 break;
3723 case LYS_DEV_DELETE:
3724 d_del = calloc(1, sizeof *d_del);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003725 LY_CHECK_ERR_RET(!d_del, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003726 d = (struct lysp_deviate *)d_del;
3727 d_units = &d_del->units;
3728 d_uniques = &d_del->uniques;
3729 d_dflts = &d_del->dflts;
3730 d_musts = &d_del->musts;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003731 break;
3732 default:
3733 assert(0);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003734 LOGINT_RET(PARSER_CTX(ctx));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003735 }
3736 d->mod = dev_mod;
3737
3738 /* insert into siblings */
David Sedlák60adc092019-08-06 15:57:02 +02003739 LY_LIST_INSERT(deviates, d, next);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003740
Radek Krejci33090f92020-12-17 20:12:46 +01003741 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003742 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003743 case LY_STMT_CONFIG:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003744 switch (dev_mod) {
3745 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003746 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003747 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003748 return LY_EVALID;
3749 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003750 LY_CHECK_RET(parse_config(ctx, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003751 break;
3752 }
3753 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003754 case LY_STMT_DEFAULT:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003755 switch (dev_mod) {
3756 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003757 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003758 return LY_EVALID;
3759 case LYS_DEV_REPLACE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003760 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 +02003761 d_rpl->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003762 break;
3763 default:
Radek Krejcifc596f92021-02-26 22:40:26 +01003764 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, d_dflts, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003765 break;
3766 }
3767 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003768 case LY_STMT_MANDATORY:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003769 switch (dev_mod) {
3770 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003771 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003772 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003773 return LY_EVALID;
3774 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003775 LY_CHECK_RET(parse_mandatory(ctx, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003776 break;
3777 }
3778 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003779 case LY_STMT_MAX_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003780 switch (dev_mod) {
3781 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003782 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003783 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003784 return LY_EVALID;
3785 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003786 LY_CHECK_RET(parse_maxelements(ctx, d_max, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003787 break;
3788 }
3789 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003790 case LY_STMT_MIN_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003791 switch (dev_mod) {
3792 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003793 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003794 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003795 return LY_EVALID;
3796 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003797 LY_CHECK_RET(parse_minelements(ctx, d_min, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003798 break;
3799 }
3800 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003801 case LY_STMT_MUST:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003802 switch (dev_mod) {
3803 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003804 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003805 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003806 return LY_EVALID;
3807 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003808 LY_CHECK_RET(parse_restrs(ctx, kw, d_musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003809 break;
3810 }
3811 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003812 case LY_STMT_TYPE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003813 switch (dev_mod) {
3814 case LYS_DEV_NOT_SUPPORTED:
3815 case LYS_DEV_ADD:
3816 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003817 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003818 return LY_EVALID;
3819 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003820 if (d_rpl->type) {
David Sedlákb3077192019-06-19 10:55:37 +02003821 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003822 return LY_EVALID;
3823 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003824 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003825 LY_CHECK_ERR_RET(!d_rpl->type, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01003826 LY_CHECK_RET(parse_type(ctx, d_rpl->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003827 break;
3828 }
3829 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003830 case LY_STMT_UNIQUE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003831 switch (dev_mod) {
3832 case LYS_DEV_NOT_SUPPORTED:
3833 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003834 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003835 return LY_EVALID;
3836 default:
Radek Krejcifc596f92021-02-26 22:40:26 +01003837 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_UNIQUE, d_uniques, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003838 break;
3839 }
3840 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003841 case LY_STMT_UNITS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003842 switch (dev_mod) {
3843 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003844 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003845 return LY_EVALID;
3846 default:
Radek Krejcifc596f92021-02-26 22:40:26 +01003847 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 +02003848 break;
3849 }
3850 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003851 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003852 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_DEVIATE, 0, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003853 break;
3854 default:
David Sedlákb3077192019-06-19 10:55:37 +02003855 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003856 return LY_EVALID;
3857 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003858 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003859 return ret;
3860}
3861
Michal Vaskoea5abea2018-09-18 13:10:54 +02003862/**
3863 * @brief Parse the deviation statement.
3864 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003865 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003866 * @param[in,out] deviations Deviations to add to.
3867 *
3868 * @return LY_ERR values.
3869 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003870LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003871parse_deviation(struct lys_yang_parser_ctx *ctx, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003872{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003873 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003874 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003875 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003876 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003877 struct lysp_deviation *dev;
3878
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003879 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003880
3881 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003882 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01003883 CHECK_NONEMPTY(ctx, word_len, "deviation");
Radek Krejci011e4aa2020-09-04 15:22:31 +02003884 INSERT_WORD_RET(ctx, buf, dev->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003885
Radek Krejci33090f92020-12-17 20:12:46 +01003886 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003887 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003888 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003889 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 +02003890 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003891 case LY_STMT_DEVIATE:
Radek Krejci33090f92020-12-17 20:12:46 +01003892 LY_CHECK_RET(parse_deviate(ctx, &dev->deviates));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003893 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003894 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003895 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 +02003896 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003897 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003898 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_DEVIATION, 0, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003899 break;
3900 default:
David Sedlákb3077192019-06-19 10:55:37 +02003901 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003902 return LY_EVALID;
3903 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003904 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003905 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01003906checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003907 /* mandatory substatements */
3908 if (!dev->deviates) {
David Sedlákb3077192019-06-19 10:55:37 +02003909 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003910 return LY_EVALID;
3911 }
3912
3913 return ret;
3914}
3915
Michal Vaskoea5abea2018-09-18 13:10:54 +02003916/**
3917 * @brief Parse the feature statement.
3918 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003919 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003920 * @param[in,out] features Features to add to.
3921 *
3922 * @return LY_ERR values.
3923 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003924LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003925parse_feature(struct lys_yang_parser_ctx *ctx, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003926{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003927 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003928 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003929 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003930 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003931 struct lysp_feature *feat;
3932
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003933 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003934
3935 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003936 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003937 INSERT_WORD_RET(ctx, buf, feat->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01003938
Radek Krejci33090f92020-12-17 20:12:46 +01003939 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003940 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003941 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003942 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 +02003943 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003944 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003945 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &feat->iffeatures, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003946 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003947 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003948 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 +02003949 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003950 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003951 LY_CHECK_RET(parse_status(ctx, &feat->flags, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003952 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003953 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003954 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_FEATURE, 0, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003955 break;
3956 default:
David Sedlákb3077192019-06-19 10:55:37 +02003957 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003958 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003959 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003960 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003961 return ret;
3962}
3963
Michal Vaskoea5abea2018-09-18 13:10:54 +02003964/**
3965 * @brief Parse the identity statement.
3966 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003967 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003968 * @param[in,out] identities Identities to add to.
3969 *
3970 * @return LY_ERR values.
3971 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003972LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003973parse_identity(struct lys_yang_parser_ctx *ctx, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003974{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003975 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003976 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003977 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003978 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003979 struct lysp_ident *ident;
3980
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003981 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003982
3983 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003984 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003985 INSERT_WORD_RET(ctx, buf, ident->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01003986
Radek Krejci33090f92020-12-17 20:12:46 +01003987 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003988 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003989 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01003990 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 +02003991 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003992 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02003993 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "identity");
Radek Krejcifc596f92021-02-26 22:40:26 +01003994 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &ident->iffeatures, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003995 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003996 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003997 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 +02003998 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003999 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01004000 LY_CHECK_RET(parse_status(ctx, &ident->flags, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004001 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004002 case LY_STMT_BASE:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004003 if (ident->bases && (ctx->parsed_mod->version < LYS_VERSION_1_1)) {
David Sedlákb3077192019-06-19 10:55:37 +02004004 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 +01004005 return LY_EVALID;
4006 }
Radek Krejcifc596f92021-02-26 22:40:26 +01004007 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 +02004008 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004009 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01004010 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_IDENTITY, 0, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004011 break;
4012 default:
David Sedlákb3077192019-06-19 10:55:37 +02004013 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004014 return LY_EVALID;
4015 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004016 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004017 return ret;
4018}
4019
Michal Vaskoea5abea2018-09-18 13:10:54 +02004020/**
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004021 * @brief Parse module substatements.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004022 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004023 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004024 * @param[in,out] mod Module to write to.
4025 *
4026 * @return LY_ERR values.
4027 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004028LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004029parse_module(struct lys_yang_parser_ctx *ctx, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004030{
4031 LY_ERR ret = 0;
4032 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004033 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004034 enum ly_stmt kw, prev_kw = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004035 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Michal Vasko8dc31992021-02-22 10:30:47 +01004036 const struct lysp_submodule *dup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004037
Michal Vaskoc3781c32020-10-06 14:04:08 +02004038 mod->is_submod = 0;
4039
4040 /* module name */
Radek Krejci33090f92020-12-17 20:12:46 +01004041 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004042 INSERT_WORD_RET(ctx, buf, mod->mod->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004043
Radek Krejci33090f92020-12-17 20:12:46 +01004044 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004045
Radek Krejcie3846472018-10-15 15:24:51 +02004046#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004047 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 +02004048
Michal Vasko7fbc8162018-09-17 10:35:16 +02004049 switch (kw) {
4050 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004051 case LY_STMT_NAMESPACE:
4052 case LY_STMT_PREFIX:
Radek Krejcie3846472018-10-15 15:24:51 +02004053 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4054 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004055 case LY_STMT_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004056 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004057 break;
4058 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004059 case LY_STMT_INCLUDE:
4060 case LY_STMT_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004061 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004062 break;
4063 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004064 case LY_STMT_ORGANIZATION:
4065 case LY_STMT_CONTACT:
4066 case LY_STMT_DESCRIPTION:
4067 case LY_STMT_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004068 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004069 break;
4070
4071 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004072 case LY_STMT_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004073 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004074 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004075 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004076 case LY_STMT_ANYDATA:
4077 case LY_STMT_ANYXML:
4078 case LY_STMT_AUGMENT:
4079 case LY_STMT_CHOICE:
4080 case LY_STMT_CONTAINER:
4081 case LY_STMT_DEVIATION:
4082 case LY_STMT_EXTENSION:
4083 case LY_STMT_FEATURE:
4084 case LY_STMT_GROUPING:
4085 case LY_STMT_IDENTITY:
4086 case LY_STMT_LEAF:
4087 case LY_STMT_LEAF_LIST:
4088 case LY_STMT_LIST:
4089 case LY_STMT_NOTIFICATION:
4090 case LY_STMT_RPC:
4091 case LY_STMT_TYPEDEF:
4092 case LY_STMT_USES:
4093 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004094 mod_stmt = Y_MOD_BODY;
4095 break;
4096 default:
4097 /* error handled in the next switch */
4098 break;
4099 }
Radek Krejcie3846472018-10-15 15:24:51 +02004100#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004101
Radek Krejcie3846472018-10-15 15:24:51 +02004102 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004103 switch (kw) {
4104 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004105 case LY_STMT_YANG_VERSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004106 LY_CHECK_RET(parse_yangversion(ctx, &mod->version, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004107 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004108 case LY_STMT_NAMESPACE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004109 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 +02004110 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004111 case LY_STMT_PREFIX:
Radek Krejcifc596f92021-02-26 22:40:26 +01004112 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 +02004113 break;
4114
4115 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004116 case LY_STMT_INCLUDE:
Radek Krejci33090f92020-12-17 20:12:46 +01004117 LY_CHECK_RET(parse_include(ctx, mod->mod->name, &mod->includes));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004118 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004119 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004120 LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, &mod->imports));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004121 break;
4122
4123 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004124 case LY_STMT_ORGANIZATION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004125 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 +02004126 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004127 case LY_STMT_CONTACT:
Radek Krejcifc596f92021-02-26 22:40:26 +01004128 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 +02004129 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004130 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004131 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 +02004132 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004133 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004134 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 +02004135 break;
4136
4137 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004138 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004139 LY_CHECK_RET(parse_revision(ctx, &mod->revs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004140 break;
4141
4142 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004143 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004144 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "module");
Radek Krejci0f969882020-08-21 16:56:47 +02004145 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004146 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004147 LY_CHECK_RET(parse_any(ctx, kw, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004148 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004149 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004150 LY_CHECK_RET(parse_choice(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004151 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004152 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004153 LY_CHECK_RET(parse_container(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004154 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004155 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004156 LY_CHECK_RET(parse_leaf(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004157 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004158 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004159 LY_CHECK_RET(parse_leaflist(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004160 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004161 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004162 LY_CHECK_RET(parse_list(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004163 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004164 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004165 LY_CHECK_RET(parse_uses(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004166 break;
4167
Radek Krejcid6b76452019-09-03 17:03:03 +02004168 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004169 LY_CHECK_RET(parse_augment(ctx, NULL, &mod->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004170 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004171 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004172 LY_CHECK_RET(parse_deviation(ctx, &mod->deviations));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004173 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004174 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004175 LY_CHECK_RET(parse_extension(ctx, &mod->extensions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004176 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004177 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004178 LY_CHECK_RET(parse_feature(ctx, &mod->features));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004179 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004180 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004181 LY_CHECK_RET(parse_grouping(ctx, NULL, &mod->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004182 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004183 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004184 LY_CHECK_RET(parse_identity(ctx, &mod->identities));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004185 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004186 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004187 LY_CHECK_RET(parse_notif(ctx, NULL, &mod->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004188 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004189 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004190 LY_CHECK_RET(parse_action(ctx, NULL, &mod->rpcs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004191 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004192 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004193 LY_CHECK_RET(parse_typedef(ctx, NULL, &mod->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004194 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004195 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01004196 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_MODULE, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004197 break;
4198
4199 default:
David Sedlákb3077192019-06-19 10:55:37 +02004200 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004201 return LY_EVALID;
4202 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004203 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004204 LY_CHECK_RET(ret);
Radek Krejcie86bf772018-12-14 11:39:53 +01004205
Radek Krejci6d6556c2018-11-08 09:37:45 +01004206checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004207 /* mandatory substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004208 if (!mod->mod->ns) {
David Sedlákb3077192019-06-19 10:55:37 +02004209 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004210 return LY_EVALID;
4211 } else if (!mod->mod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004212 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004213 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004214 }
4215
Radek Krejcie9e987e2018-10-31 12:50:27 +01004216 /* submodules share the namespace with the module names, so there must not be
4217 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01004218 dup = ly_ctx_get_submodule_latest(PARSER_CTX(ctx), mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004219 if (dup) {
Radek Krejci854e1552020-12-21 15:05:23 +01004220 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004221 return LY_EVALID;
4222 }
4223
4224 return ret;
4225}
4226
4227/**
4228 * @brief Parse submodule substatements.
4229 *
4230 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004231 * @param[out] submod Parsed submodule structure.
4232 *
4233 * @return LY_ERR values.
4234 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004235LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004236parse_submodule(struct lys_yang_parser_ctx *ctx, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004237{
4238 LY_ERR ret = 0;
4239 char *buf, *word;
4240 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004241 enum ly_stmt kw, prev_kw = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004242 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Michal Vasko8dc31992021-02-22 10:30:47 +01004243 const struct lysp_submodule *dup;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004244
Michal Vaskoc3781c32020-10-06 14:04:08 +02004245 submod->is_submod = 1;
4246
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004247 /* submodule name */
Radek Krejci33090f92020-12-17 20:12:46 +01004248 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004249 INSERT_WORD_RET(ctx, buf, submod->name, word, word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004250
Radek Krejci33090f92020-12-17 20:12:46 +01004251 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004252
4253#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004254 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 +01004255
4256 switch (kw) {
4257 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004258 case LY_STMT_BELONGS_TO:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004259 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4260 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004261 case LY_STMT_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004262 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4263 break;
4264 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004265 case LY_STMT_INCLUDE:
4266 case LY_STMT_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004267 CHECK_ORDER(Y_MOD_LINKAGE);
4268 break;
4269 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004270 case LY_STMT_ORGANIZATION:
4271 case LY_STMT_CONTACT:
4272 case LY_STMT_DESCRIPTION:
4273 case LY_STMT_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004274 CHECK_ORDER(Y_MOD_META);
4275 break;
4276
4277 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004278 case LY_STMT_REVISION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004279 CHECK_ORDER(Y_MOD_REVISION);
4280 break;
4281 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004282 case LY_STMT_ANYDATA:
4283 case LY_STMT_ANYXML:
4284 case LY_STMT_AUGMENT:
4285 case LY_STMT_CHOICE:
4286 case LY_STMT_CONTAINER:
4287 case LY_STMT_DEVIATION:
4288 case LY_STMT_EXTENSION:
4289 case LY_STMT_FEATURE:
4290 case LY_STMT_GROUPING:
4291 case LY_STMT_IDENTITY:
4292 case LY_STMT_LEAF:
4293 case LY_STMT_LEAF_LIST:
4294 case LY_STMT_LIST:
4295 case LY_STMT_NOTIFICATION:
4296 case LY_STMT_RPC:
4297 case LY_STMT_TYPEDEF:
4298 case LY_STMT_USES:
4299 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004300 mod_stmt = Y_MOD_BODY;
4301 break;
4302 default:
4303 /* error handled in the next switch */
4304 break;
4305 }
4306#undef CHECK_ORDER
4307
4308 prev_kw = kw;
4309 switch (kw) {
4310 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004311 case LY_STMT_YANG_VERSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004312 LY_CHECK_RET(parse_yangversion(ctx, &submod->version, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004313 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004314 case LY_STMT_BELONGS_TO:
Radek Krejci33090f92020-12-17 20:12:46 +01004315 LY_CHECK_RET(parse_belongsto(ctx, &submod->prefix, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004316 break;
4317
4318 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004319 case LY_STMT_INCLUDE:
Radek Krejcieeee95c2021-01-19 10:57:22 +01004320 if (submod->version == LYS_VERSION_1_1) {
4321 LOGWRN(PARSER_CTX(ctx), "YANG version 1.1 expects all includes in main module, includes in submodules (%s) are not necessary.",
4322 submod->name);
4323 }
Radek Krejci33090f92020-12-17 20:12:46 +01004324 LY_CHECK_RET(parse_include(ctx, submod->name, &submod->includes));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004325 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004326 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004327 LY_CHECK_RET(parse_import(ctx, submod->prefix, &submod->imports));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004328 break;
4329
4330 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004331 case LY_STMT_ORGANIZATION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004332 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 +01004333 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004334 case LY_STMT_CONTACT:
Radek Krejcifc596f92021-02-26 22:40:26 +01004335 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 +01004336 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004337 case LY_STMT_DESCRIPTION:
Radek Krejcifc596f92021-02-26 22:40:26 +01004338 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 +01004339 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004340 case LY_STMT_REFERENCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004341 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 +01004342 break;
4343
4344 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004345 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004346 LY_CHECK_RET(parse_revision(ctx, &submod->revs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004347 break;
4348
4349 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004350 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004351 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "submodule");
Radek Krejci0f969882020-08-21 16:56:47 +02004352 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004353 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004354 LY_CHECK_RET(parse_any(ctx, kw, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004355 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004356 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004357 LY_CHECK_RET(parse_choice(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004358 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004359 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004360 LY_CHECK_RET(parse_container(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004361 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004362 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004363 LY_CHECK_RET(parse_leaf(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004364 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004365 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004366 LY_CHECK_RET(parse_leaflist(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004367 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004368 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004369 LY_CHECK_RET(parse_list(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004370 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004371 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004372 LY_CHECK_RET(parse_uses(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004373 break;
4374
Radek Krejcid6b76452019-09-03 17:03:03 +02004375 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004376 LY_CHECK_RET(parse_augment(ctx, NULL, &submod->augments));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004377 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004378 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004379 LY_CHECK_RET(parse_deviation(ctx, &submod->deviations));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004380 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004381 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004382 LY_CHECK_RET(parse_extension(ctx, &submod->extensions));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004383 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004384 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004385 LY_CHECK_RET(parse_feature(ctx, &submod->features));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004386 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004387 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004388 LY_CHECK_RET(parse_grouping(ctx, NULL, &submod->groupings));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004389 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004390 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004391 LY_CHECK_RET(parse_identity(ctx, &submod->identities));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004392 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004393 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004394 LY_CHECK_RET(parse_notif(ctx, NULL, &submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004395 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004396 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004397 LY_CHECK_RET(parse_action(ctx, NULL, &submod->rpcs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004398 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004399 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004400 LY_CHECK_RET(parse_typedef(ctx, NULL, &submod->typedefs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004401 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004402 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01004403 LY_CHECK_RET(parse_ext(ctx, word, word_len, LY_STMT_SUBMODULE, 0, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004404 break;
4405
4406 default:
David Sedlákb3077192019-06-19 10:55:37 +02004407 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004408 return LY_EVALID;
4409 }
4410 }
4411 LY_CHECK_RET(ret);
4412
4413checks:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004414 /* mandatory substatements */
Michal Vaskoc3781c32020-10-06 14:04:08 +02004415 if (!submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004416 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004417 return LY_EVALID;
4418 }
4419
4420 /* submodules share the namespace with the module names, so there must not be
4421 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01004422 dup = ly_ctx_get_submodule_latest(PARSER_CTX(ctx), submod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02004423 /* main modules may have different revisions */
4424 if (dup && strcmp(dup->mod->name, submod->mod->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01004425 LOGVAL_PARSER(ctx, LY_VCODE_NAME_COL, "submodules", dup->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +01004426 return LY_EVALID;
4427 }
4428
Michal Vasko7fbc8162018-09-17 10:35:16 +02004429 return ret;
4430}
4431
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004432/**
4433 * @brief Skip any redundant characters, namely whitespaces and comments.
4434 *
4435 * @param[in] ctx Yang parser context.
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004436 * @return LY_SUCCESS on success.
4437 * @return LY_EVALID on invalid comment.
4438 */
4439static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004440skip_redundant_chars(struct lys_yang_parser_ctx *ctx)
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004441{
4442 /* read some trailing spaces, new lines, or comments */
Radek Krejci33090f92020-12-17 20:12:46 +01004443 while (ctx->in->current[0]) {
4444 if (!strncmp(ctx->in->current, "//", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004445 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004446 ly_in_skip(ctx->in, 2);
4447 LY_CHECK_RET(skip_comment(ctx, 1));
4448 } else if (!strncmp(ctx->in->current, "/*", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004449 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004450 ly_in_skip(ctx->in, 2);
4451 LY_CHECK_RET(skip_comment(ctx, 2));
4452 } else if (isspace(ctx->in->current[0])) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004453 /* whitespace */
Radek Krejcidd713ce2021-01-04 23:12:12 +01004454 if (ctx->in->current[0] == '\n') {
4455 LY_IN_NEW_LINE(ctx->in);
4456 }
Radek Krejci33090f92020-12-17 20:12:46 +01004457 ly_in_skip(ctx->in, 1);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004458 } else {
4459 break;
4460 }
4461 }
4462
4463 return LY_SUCCESS;
4464}
4465
Radek Krejcid4557c62018-09-17 11:42:09 +02004466LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01004467yang_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 +02004468 struct ly_in *in, struct lysp_submodule **submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004469{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004470 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004471 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004472 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004473 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004474 struct lysp_submodule *mod_p = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004475
David Sedlák1b623122019-08-05 15:27:49 +02004476 /* create context */
4477 *context = calloc(1, sizeof **context);
4478 LY_CHECK_ERR_RET(!(*context), LOGMEM(ly_ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004479 (*context)->format = LYS_IN_YANG;
Michal Vasko405cc9e2020-12-01 12:01:27 +01004480 (*context)->unres = main_ctx->unres;
Radek Krejci33090f92020-12-17 20:12:46 +01004481 (*context)->in = in;
David Sedlák1b623122019-08-05 15:27:49 +02004482
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004483 mod_p = calloc(1, sizeof *mod_p);
4484 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ly_ctx); ret = LY_EMEM, cleanup);
4485 mod_p->mod = main_ctx->parsed_mod->mod;
4486 mod_p->parsing = 1;
4487 (*context)->parsed_mod = (struct lysp_module *)mod_p;
4488
Radek Krejciddace2c2021-01-08 11:30:56 +01004489 LOG_LOCINIT(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004490
David Sedlák1b623122019-08-05 15:27:49 +02004491 /* map the typedefs and groupings list from main context to the submodule's context */
4492 memcpy(&(*context)->tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
4493 memcpy(&(*context)->grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes);
4494
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004495 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004496 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004497 LY_CHECK_GOTO(ret, cleanup);
4498
Michal Vasko7fbc8162018-09-17 10:35:16 +02004499 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004500 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004501 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004502
Radek Krejcid6b76452019-09-03 17:03:03 +02004503 if (kw == LY_STMT_MODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004504 LOGERR(ly_ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004505 ret = LY_EINVAL;
4506 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004507 } else if (kw != LY_STMT_SUBMODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004508 LOGVAL_PARSER(*context, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004509 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004510 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004511 }
4512
Michal Vasko7fbc8162018-09-17 10:35:16 +02004513 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004514 ret = parse_submodule(*context, mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004515 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004516
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004517 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004518 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004519 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004520 if (in->current[0]) {
4521 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_SUBMOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004522 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004523 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004524 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004525
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004526 mod_p->parsing = 0;
4527 *submod = mod_p;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004528
Radek Krejcibbe09a92018-11-08 09:36:54 +01004529cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01004530 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004531 if (ret) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004532 lysp_module_free((struct lysp_module *)mod_p);
Michal Vaskob36053d2020-03-26 15:49:30 +01004533 yang_parser_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004534 *context = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004535 }
4536
4537 return ret;
4538}
4539
4540LY_ERR
Michal Vasko405cc9e2020-12-01 12:01:27 +01004541yang_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 +01004542{
4543 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004544 char *word;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004545 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004546 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004547 struct lysp_module *mod_p = NULL;
4548
David Sedlák1b623122019-08-05 15:27:49 +02004549 /* create context */
4550 *context = calloc(1, sizeof **context);
4551 LY_CHECK_ERR_RET(!(*context), LOGMEM(mod->ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004552 (*context)->format = LYS_IN_YANG;
Michal Vasko405cc9e2020-12-01 12:01:27 +01004553 (*context)->unres = unres;
Radek Krejci33090f92020-12-17 20:12:46 +01004554 (*context)->in = in;
David Sedlák1b623122019-08-05 15:27:49 +02004555
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004556 mod_p = calloc(1, sizeof *mod_p);
4557 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(mod->ctx), cleanup);
4558 mod_p->mod = mod;
4559 mod_p->parsing = 1;
4560 (*context)->parsed_mod = mod_p;
4561
Radek Krejciddace2c2021-01-08 11:30:56 +01004562 LOG_LOCINIT(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004563
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004564 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004565 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004566 LY_CHECK_GOTO(ret, cleanup);
4567
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004568 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004569 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004570 LY_CHECK_GOTO(ret, cleanup);
4571
Radek Krejcid6b76452019-09-03 17:03:03 +02004572 if (kw == LY_STMT_SUBMODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004573 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 +01004574 ret = LY_EINVAL;
4575 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004576 } else if (kw != LY_STMT_MODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004577 LOGVAL_PARSER((*context), LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004578 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004579 goto cleanup;
4580 }
4581
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004582 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004583 ret = parse_module(*context, mod_p);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004584 LY_CHECK_GOTO(ret, cleanup);
4585
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004586 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004587 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004588 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004589 if (in->current[0]) {
4590 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_MOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004591 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004592 goto cleanup;
4593 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004594
4595 mod_p->parsing = 0;
4596 mod->parsed = mod_p;
4597
4598cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01004599 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004600 if (ret) {
4601 lysp_module_free(mod_p);
Michal Vaskob36053d2020-03-26 15:49:30 +01004602 yang_parser_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004603 *context = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004604 }
4605
Michal Vasko7fbc8162018-09-17 10:35:16 +02004606 return ret;
4607}