blob: 6c3f985437c6a1f28e7b60254c6f3cfe0734fdad [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"
33#include "tree_schema.h"
Radek Krejci70853c52018-10-15 14:46:16 +020034#include "tree_schema_internal.h"
Radek Krejci44ceedc2018-10-02 15:54:31 +020035
Radek Krejciceaf2122019-01-02 15:03:26 +010036/**
37 * @brief Insert WORD into the libyang context's dictionary and store as TARGET.
38 * @param[in] CTX yang parser context to access libyang context.
39 * @param[in] BUF buffer in case the word is not a constant and can be inserted directly (zero-copy)
40 * @param[out] TARGET variable where to store the pointer to the inserted value.
41 * @param[in] WORD string to store.
42 * @param[in] LEN length of the string in WORD to store.
43 */
Radek Krejci011e4aa2020-09-04 15:22:31 +020044#define INSERT_WORD_RET(CTX, BUF, TARGET, WORD, LEN) \
Michal Vasko5d24f6c2020-10-13 13:49:06 +020045 if (BUF) {LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(CTX), WORD, &(TARGET)));}\
46 else {LY_CHECK_RET(lydict_insert(PARSER_CTX(CTX), LEN ? WORD : "", LEN, &(TARGET)));}
Radek Krejci44ceedc2018-10-02 15:54:31 +020047
Radek Krejciceaf2122019-01-02 15:03:26 +010048/**
Michal Vasko63f3d842020-07-08 10:10:14 +020049 * @brief Read from the IN structure COUNT items. Also updates the indent value in yang parser context
Radek Krejciceaf2122019-01-02 15:03:26 +010050 * @param[in] CTX yang parser context to update its indent value.
Radek Krejciceaf2122019-01-02 15:03:26 +010051 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
52 */
Radek Krejcid54412f2020-12-17 20:25:35 +010053#define MOVE_INPUT(CTX, COUNT) ly_in_skip((CTX)->in, COUNT);(CTX)->indent+=COUNT
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020054
Michal Vaskoea5abea2018-09-18 13:10:54 +020055/**
56 * @brief Loop through all substatements providing, return if there are none.
57 *
Radek Krejci44ceedc2018-10-02 15:54:31 +020058 * @param[in] CTX yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +020059 * @param[out] KW YANG keyword read.
60 * @param[out] WORD Pointer to the keyword itself.
61 * @param[out] WORD_LEN Length of the keyword.
62 * @param[out] ERR Variable for error storing.
63 *
64 * @return In case there are no substatements or a fatal error encountered.
65 */
Radek Krejci33090f92020-12-17 20:12:46 +010066#define YANG_READ_SUBSTMT_FOR(CTX, KW, WORD, WORD_LEN, ERR, CHECKGOTO) \
67 LY_CHECK_RET(get_keyword(CTX, &KW, &WORD, &WORD_LEN)); \
Radek Krejcid6b76452019-09-03 17:03:03 +020068 if (KW == LY_STMT_SYNTAX_SEMICOLON) { \
Radek Krejci6d6556c2018-11-08 09:37:45 +010069 CHECKGOTO; \
Radek Krejci6d9b9b52018-11-02 12:43:39 +010070 return LY_SUCCESS; \
Michal Vasko7fbc8162018-09-17 10:35:16 +020071 } \
Radek Krejcid6b76452019-09-03 17:03:03 +020072 if (KW != LY_STMT_SYNTAX_LEFT_BRACE) { \
David Sedlákb3077192019-06-19 10:55:37 +020073 LOGVAL_PARSER(CTX, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\", expected \";\" or \"{\".", ly_stmt2str(KW)); \
Michal Vasko7fbc8162018-09-17 10:35:16 +020074 return LY_EVALID; \
75 } \
Radek Krejci33090f92020-12-17 20:12:46 +010076 for (ERR = get_keyword(CTX, &KW, &WORD, &WORD_LEN); \
Radek Krejcid6b76452019-09-03 17:03:03 +020077 !ERR && (KW != LY_STMT_SYNTAX_RIGHT_BRACE); \
Radek Krejci33090f92020-12-17 20:12:46 +010078 ERR = get_keyword(CTX, &KW, &WORD, &WORD_LEN))
Michal Vasko7fbc8162018-09-17 10:35:16 +020079
Radek Krejci33090f92020-12-17 20:12:46 +010080LY_ERR parse_container(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent,
Radek Krejci0f969882020-08-21 16:56:47 +020081 struct lysp_node **siblings);
Radek Krejci33090f92020-12-17 20:12:46 +010082LY_ERR parse_uses(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
83LY_ERR parse_choice(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
84LY_ERR parse_case(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
85LY_ERR parse_list(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
Radek Krejci2a9fc652021-01-22 17:44:34 +010086LY_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 +020087
Michal Vaskoea5abea2018-09-18 13:10:54 +020088/**
89 * @brief Add another character to dynamic buffer, a low-level function.
90 *
Radek Krejci44ceedc2018-10-02 15:54:31 +020091 * Enlarge if needed. Updates \p input as well as \p buf_used.
Michal Vaskoea5abea2018-09-18 13:10:54 +020092 *
Radek Krejci404251e2018-10-09 12:06:44 +020093 * @param[in] ctx libyang context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +020094 * @param[in,out] in Input structure.
Radek Krejci44ceedc2018-10-02 15:54:31 +020095 * @param[in] len Number of bytes to get from the input string and copy into the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +020096 * @param[in,out] buf Buffer to use, can be moved by realloc().
97 * @param[in,out] buf_len Current size of the buffer.
Radek Krejci44ceedc2018-10-02 15:54:31 +020098 * @param[in,out] buf_used Currently used characters of the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +020099 *
100 * @return LY_ERR values.
101 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200102LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200103buf_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 +0200104{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100105#define BUF_STEP 16;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200106 if (*buf_len <= (*buf_used) + len) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100107 *buf_len += BUF_STEP;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200108 *buf = ly_realloc(*buf, *buf_len);
109 LY_CHECK_ERR_RET(!*buf, LOGMEM(ctx), LY_EMEM);
110 }
Radek Krejcic0917392019-04-10 13:04:04 +0200111 if (*buf_used) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200112 ly_in_read(in, &(*buf)[*buf_used], len);
Radek Krejcic0917392019-04-10 13:04:04 +0200113 } else {
Michal Vasko63f3d842020-07-08 10:10:14 +0200114 ly_in_read(in, *buf, len);
Radek Krejcic0917392019-04-10 13:04:04 +0200115 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200116
Radek Krejci44ceedc2018-10-02 15:54:31 +0200117 (*buf_used) += len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200118 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100119#undef BUF_STEP
Michal Vasko7fbc8162018-09-17 10:35:16 +0200120}
121
Michal Vaskoea5abea2018-09-18 13:10:54 +0200122/**
Radek Krejci44ceedc2018-10-02 15:54:31 +0200123 * @brief Store a single UTF8 character. It depends whether in a dynamically-allocated buffer or just as a pointer to the data.
124 *
125 * @param[in] ctx yang parser context for logging.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200126 * @param[in] arg Type of the input string to select method of checking character validity.
127 * @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 +0200128 * 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 +0200129 * @param[in,out] word_len Current length of the word pointed to by \p word_p.
130 * @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 +0200131 * @param[in,out] buf_len Current length of \p word_b.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200132 * @param[in] need_buf Flag if the dynamically allocated buffer is required.
David Sedlák40bb13b2019-07-10 14:34:18 +0200133 * @param[in,out] prefix Storage for internally used flag in case of possible prefixed identifiers:
134 * 0 - colon not yet found (no prefix)
135 * 1 - \p c is the colon character
136 * 2 - prefix already processed, now processing the identifier
Michal Vaskoea5abea2018-09-18 13:10:54 +0200137 *
138 * @return LY_ERR values.
139 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200140LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100141buf_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 +0200142 char **word_b, size_t *buf_len, ly_bool need_buf, uint8_t *prefix)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200143{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200144 uint32_t c;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200145 size_t len;
146
Radek Krejcif29b7c32019-04-09 16:17:49 +0200147 /* check valid combination of input paremeters - if need_buf specified, word_b must be provided */
148 assert(!need_buf || (need_buf && word_b));
149
Radek Krejci44ceedc2018-10-02 15:54:31 +0200150 /* get UTF8 code point (and number of bytes coding the character) */
Radek Krejci33090f92020-12-17 20:12:46 +0100151 LY_CHECK_ERR_RET(ly_getutf8(&ctx->in->current, &c, &len),
152 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[-len]), LY_EVALID);
153 ctx->in->current -= len;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200154 if (c == '\n') {
155 ctx->indent = 0;
Radek Krejcidd713ce2021-01-04 23:12:12 +0100156 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200157 } else {
158 /* note - even the multibyte character is count as 1 */
159 ++ctx->indent;
160 }
161
Radek Krejci44ceedc2018-10-02 15:54:31 +0200162 /* check character validity */
163 switch (arg) {
164 case Y_IDENTIF_ARG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100165 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !(*word_len), NULL));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200166 break;
167 case Y_PREF_IDENTIF_ARG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100168 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !(*word_len), prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200169 break;
170 case Y_STR_ARG:
171 case Y_MAYBE_STR_ARG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100172 LY_CHECK_RET(lysp_check_stringchar((struct lys_parser_ctx *)ctx, c));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200173 break;
174 }
175
Michal Vasko7fbc8162018-09-17 10:35:16 +0200176 if (word_b && *word_b) {
177 /* add another character into buffer */
Radek Krejci33090f92020-12-17 20:12:46 +0100178 if (buf_add_char(PARSER_CTX(ctx), ctx->in, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200179 return LY_EMEM;
180 }
181
182 /* in case of realloc */
183 *word_p = *word_b;
Radek Krejcif29b7c32019-04-09 16:17:49 +0200184 } else if (word_b && need_buf) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200185 /* first time we need a buffer, copy everything read up to now */
186 if (*word_len) {
187 *word_b = malloc(*word_len);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200188 LY_CHECK_ERR_RET(!*word_b, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200189 *buf_len = *word_len;
190 memcpy(*word_b, *word_p, *word_len);
191 }
192
193 /* add this new character into buffer */
Radek Krejci33090f92020-12-17 20:12:46 +0100194 if (buf_add_char(PARSER_CTX(ctx), ctx->in, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200195 return LY_EMEM;
196 }
197
198 /* in case of realloc */
199 *word_p = *word_b;
200 } else {
201 /* just remember the first character pointer */
202 if (!*word_p) {
Radek Krejci33090f92020-12-17 20:12:46 +0100203 *word_p = (char *)ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200204 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200205 /* ... and update the word's length */
206 (*word_len) += len;
Radek Krejci33090f92020-12-17 20:12:46 +0100207 ly_in_skip(ctx->in, len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200208 }
209
210 return LY_SUCCESS;
211}
212
Michal Vaskoea5abea2018-09-18 13:10:54 +0200213/**
214 * @brief Skip YANG comment in data.
215 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200216 * @param[in] ctx yang parser context for logging.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200217 * @param[in] comment Type of the comment to process:
218 * 1 for a one-line comment,
219 * 2 for a block comment.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200220 * @return LY_ERR values.
221 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200222LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100223skip_comment(struct lys_yang_parser_ctx *ctx, uint8_t comment)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200224{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100225 /* internal statuses: */
226#define COMMENT_NO 0 /* comment ended */
227#define COMMENT_LINE 1 /* in line comment */
228#define COMMENT_BLOCK 2 /* in block comment */
229#define COMMENT_BLOCK_END 3 /* in block comment with last read character '*' */
230
Radek Krejci33090f92020-12-17 20:12:46 +0100231 while (ctx->in->current[0] && comment) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200232 switch (comment) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100233 case COMMENT_LINE:
Radek Krejci33090f92020-12-17 20:12:46 +0100234 if (ctx->in->current[0] == '\n') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100235 comment = COMMENT_NO;
Radek Krejcid54412f2020-12-17 20:25:35 +0100236 LY_IN_NEW_LINE(ctx->in);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200237 }
238 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100239 case COMMENT_BLOCK:
Radek Krejci33090f92020-12-17 20:12:46 +0100240 if (ctx->in->current[0] == '*') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100241 comment = COMMENT_BLOCK_END;
Radek Krejci33090f92020-12-17 20:12:46 +0100242 } else if (ctx->in->current[0] == '\n') {
Radek Krejcid54412f2020-12-17 20:25:35 +0100243 LY_IN_NEW_LINE(ctx->in);
Radek Krejci15c80ca2018-10-09 11:01:31 +0200244 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200245 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100246 case COMMENT_BLOCK_END:
Radek Krejci33090f92020-12-17 20:12:46 +0100247 if (ctx->in->current[0] == '/') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100248 comment = COMMENT_NO;
Radek Krejci33090f92020-12-17 20:12:46 +0100249 } else if (ctx->in->current[0] != '*') {
250 if (ctx->in->current[0] == '\n') {
Radek Krejcid54412f2020-12-17 20:25:35 +0100251 LY_IN_NEW_LINE(ctx->in);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200252 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100253 comment = COMMENT_BLOCK;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200254 }
255 break;
256 default:
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200257 LOGINT_RET(PARSER_CTX(ctx));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200258 }
259
Radek Krejci33090f92020-12-17 20:12:46 +0100260 if (ctx->in->current[0] == '\n') {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200261 ctx->indent = 0;
262 } else {
263 ++ctx->indent;
264 }
Radek Krejci33090f92020-12-17 20:12:46 +0100265 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200266 }
267
Radek Krejci33090f92020-12-17 20:12:46 +0100268 if (!ctx->in->current[0] && (comment >= COMMENT_BLOCK)) {
David Sedlákb3077192019-06-19 10:55:37 +0200269 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Unexpected end-of-input, non-terminated comment.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200270 return LY_EVALID;
271 }
272
273 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100274
275#undef COMMENT_NO
276#undef COMMENT_LINE
277#undef COMMENT_BLOCK
278#undef COMMENT_BLOCK_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200279}
280
Michal Vaskoea5abea2018-09-18 13:10:54 +0200281/**
282 * @brief Read a quoted string from data.
283 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200284 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200285 * @param[in] arg Type of YANG keyword argument expected.
286 * @param[out] word_p Pointer to the read quoted string.
287 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read quoted string. If not needed,
288 * set to NULL. Otherwise equal to \p word_p.
289 * @param[out] word_len Length of the read quoted string.
290 * @param[out] buf_len Length of the dynamically-allocated buffer \p word_b.
291 * @param[in] indent Current indent (number of YANG spaces). Needed for correct multi-line string
292 * indenation in the final quoted string.
293 *
294 * @return LY_ERR values.
295 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200296static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100297read_qstring(struct lys_yang_parser_ctx *ctx, enum yang_arg arg, char **word_p, char **word_b,
Radek Krejci0f969882020-08-21 16:56:47 +0200298 size_t *word_len, size_t *buf_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200299{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100300 /* string parsing status: */
301#define STRING_ENDED 0 /* string ended */
302#define STRING_SINGLE_QUOTED 1 /* string with ' */
303#define STRING_DOUBLE_QUOTED 2 /* string with " */
304#define STRING_DOUBLE_QUOTED_ESCAPED 3 /* string with " with last character \ */
305#define STRING_PAUSED_NEXTSTRING 4 /* string finished, now skipping whitespaces looking for + */
306#define STRING_PAUSED_CONTINUE 5 /* string continues after +, skipping whitespaces */
307
Radek Krejci1deb5be2020-08-26 16:43:36 +0200308 uint8_t string;
309 uint64_t block_indent = 0, current_indent = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200310 ly_bool need_buf = 0;
311 uint8_t prefix = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200312 const char *c;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200313 uint64_t trailing_ws = 0; /* current number of stored trailing whitespace characters */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200314
Radek Krejci33090f92020-12-17 20:12:46 +0100315 if (ctx->in->current[0] == '\"') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100316 string = STRING_DOUBLE_QUOTED;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200317 current_indent = block_indent = ctx->indent + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200318 } else {
Radek Krejci33090f92020-12-17 20:12:46 +0100319 assert(ctx->in->current[0] == '\'');
Radek Krejcif13b87b2020-12-01 22:02:17 +0100320 string = STRING_SINGLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200321 }
Radek Krejci33090f92020-12-17 20:12:46 +0100322 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200323
Radek Krejci33090f92020-12-17 20:12:46 +0100324 while (ctx->in->current[0] && string) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200325 switch (string) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100326 case STRING_SINGLE_QUOTED:
Radek Krejci33090f92020-12-17 20:12:46 +0100327 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200328 case '\'':
329 /* string may be finished, but check for + */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100330 string = STRING_PAUSED_NEXTSTRING;
Radek Krejci33090f92020-12-17 20:12:46 +0100331 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200332 break;
333 default:
334 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100335 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 +0200336 break;
337 }
338 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100339 case STRING_DOUBLE_QUOTED:
Radek Krejci33090f92020-12-17 20:12:46 +0100340 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200341 case '\"':
342 /* string may be finished, but check for + */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100343 string = STRING_PAUSED_NEXTSTRING;
Radek Krejci33090f92020-12-17 20:12:46 +0100344 MOVE_INPUT(ctx, 1);
Radek Krejciff13cd12019-10-25 15:34:24 +0200345 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200346 break;
347 case '\\':
348 /* special character following */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100349 string = STRING_DOUBLE_QUOTED_ESCAPED;
Radek Krejciff13cd12019-10-25 15:34:24 +0200350
351 /* the backslash sequence is substituted, so we will need a buffer to store the result */
352 need_buf = 1;
353
354 /* move forward to the escaped character */
Radek Krejci33090f92020-12-17 20:12:46 +0100355 ++ctx->in->current;
Radek Krejciff13cd12019-10-25 15:34:24 +0200356
357 /* note that the trailing whitespaces are supposed to be trimmed before substitution of
358 * backslash-escaped characters (RFC 7950, 6.1.3), so we have to zero the trailing whitespaces counter */
359 trailing_ws = 0;
360
361 /* since the backslash-escaped character is handled as first non-whitespace character, stop eating indentation */
362 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200363 break;
364 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200365 if (current_indent < block_indent) {
366 ++current_indent;
Radek Krejci33090f92020-12-17 20:12:46 +0100367 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200368 } else {
Michal Vasko90edde42019-11-25 15:25:07 +0100369 /* check and store whitespace character */
Radek Krejci33090f92020-12-17 20:12:46 +0100370 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 +0100371 trailing_ws++;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200372 }
373 break;
374 case '\t':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200375 if (current_indent < block_indent) {
376 assert(need_buf);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100377 current_indent += Y_TAB_SPACES;
378 ctx->indent += Y_TAB_SPACES;
Michal Vaskod989ba02020-08-24 10:59:24 +0200379 for ( ; current_indent > block_indent; --current_indent, --ctx->indent) {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200380 /* store leftover spaces from the tab */
Radek Krejci33090f92020-12-17 20:12:46 +0100381 c = ctx->in->current;
382 ctx->in->current = " ";
383 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
384 ctx->in->current = c;
Michal Vasko90edde42019-11-25 15:25:07 +0100385 trailing_ws++;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200386 }
Radek Krejci33090f92020-12-17 20:12:46 +0100387 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200388 } else {
Michal Vasko90edde42019-11-25 15:25:07 +0100389 /* check and store whitespace character */
Radek Krejci33090f92020-12-17 20:12:46 +0100390 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 +0100391 trailing_ws++;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200392 /* additional characters for indentation - only 1 was count in buf_store_char */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100393 ctx->indent += Y_TAB_SPACES - 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200394 }
395 break;
396 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200397 if (block_indent) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200398 /* we will be removing the indents so we need our own buffer */
399 need_buf = 1;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200400
401 /* remove trailing tabs and spaces */
Radek Krejciff13cd12019-10-25 15:34:24 +0200402 (*word_len) = *word_len - trailing_ws;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200403
Radek Krejciff13cd12019-10-25 15:34:24 +0200404 /* restart indentation */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200405 current_indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200406 }
407
408 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100409 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 +0200410
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200411 /* reset context indentation counter for possible string after this one */
412 ctx->indent = 0;
Radek Krejciff13cd12019-10-25 15:34:24 +0200413 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200414 break;
415 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200416 /* first non-whitespace character, stop eating indentation */
417 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200418
419 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100420 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 +0200421 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200422 break;
423 }
424 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100425 case STRING_DOUBLE_QUOTED_ESCAPED:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200426 /* string encoded characters */
Radek Krejci33090f92020-12-17 20:12:46 +0100427 c = ctx->in->current;
428 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200429 case 'n':
Radek Krejci33090f92020-12-17 20:12:46 +0100430 ctx->in->current = "\n";
Radek Krejcidd713ce2021-01-04 23:12:12 +0100431 /* fix false newline count in buf_store_char() */
432 ctx->in->line--;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200433 break;
434 case 't':
Radek Krejci33090f92020-12-17 20:12:46 +0100435 ctx->in->current = "\t";
Michal Vasko7fbc8162018-09-17 10:35:16 +0200436 break;
437 case '\"':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200438 case '\\':
Michal Vasko63f3d842020-07-08 10:10:14 +0200439 /* ok as is */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200440 break;
441 default:
Michal Vasko63f3d842020-07-08 10:10:14 +0200442 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Double-quoted string unknown special character '\\%c'.",
Radek Krejci33090f92020-12-17 20:12:46 +0100443 ctx->in->current[0]);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200444 return LY_EVALID;
445 }
446
447 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100448 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 +0200449
Radek Krejcif13b87b2020-12-01 22:02:17 +0100450 string = STRING_DOUBLE_QUOTED;
Radek Krejci33090f92020-12-17 20:12:46 +0100451 ctx->in->current = c + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200452 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100453 case STRING_PAUSED_NEXTSTRING:
Radek Krejci33090f92020-12-17 20:12:46 +0100454 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200455 case '+':
456 /* string continues */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100457 string = STRING_PAUSED_CONTINUE;
Radek Krejciefd22f62018-09-27 11:47:58 +0200458 need_buf = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200459 break;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200460 case '\n':
Radek Krejcidd713ce2021-01-04 23:12:12 +0100461 LY_IN_NEW_LINE(ctx->in);
Radek Krejcicb3e6472021-01-06 08:19:01 +0100462 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200463 case ' ':
464 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200465 /* just skip */
466 break;
467 default:
468 /* string is finished */
469 goto string_end;
470 }
Radek Krejci33090f92020-12-17 20:12:46 +0100471 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200472 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100473 case STRING_PAUSED_CONTINUE:
Radek Krejci33090f92020-12-17 20:12:46 +0100474 switch (ctx->in->current[0]) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200475 case '\n':
Radek Krejcidd713ce2021-01-04 23:12:12 +0100476 LY_IN_NEW_LINE(ctx->in);
Radek Krejcicb3e6472021-01-06 08:19:01 +0100477 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200478 case ' ':
479 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200480 /* skip */
481 break;
482 case '\'':
Radek Krejcif13b87b2020-12-01 22:02:17 +0100483 string = STRING_SINGLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200484 break;
485 case '\"':
Radek Krejcif13b87b2020-12-01 22:02:17 +0100486 string = STRING_DOUBLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200487 break;
488 default:
489 /* it must be quoted again */
David Sedlákb3077192019-06-19 10:55:37 +0200490 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Both string parts divided by '+' must be quoted.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200491 return LY_EVALID;
492 }
Radek Krejci33090f92020-12-17 20:12:46 +0100493 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200494 break;
495 default:
496 return LY_EINT;
497 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200498 }
499
500string_end:
Michal Vasko69730152020-10-09 16:30:07 +0200501 if ((arg <= Y_PREF_IDENTIF_ARG) && !(*word_len)) {
Radek Krejci4e199f52019-05-28 09:09:28 +0200502 /* empty identifier */
David Sedlákb3077192019-06-19 10:55:37 +0200503 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Statement argument is required.");
Radek Krejci4e199f52019-05-28 09:09:28 +0200504 return LY_EVALID;
505 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200506 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100507
508#undef STRING_ENDED
509#undef STRING_SINGLE_QUOTED
510#undef STRING_DOUBLE_QUOTED
511#undef STRING_DOUBLE_QUOTED_ESCAPED
512#undef STRING_PAUSED_NEXTSTRING
513#undef STRING_PAUSED_CONTINUE
Michal Vasko7fbc8162018-09-17 10:35:16 +0200514}
515
Michal Vaskoea5abea2018-09-18 13:10:54 +0200516/**
517 * @brief Get another YANG string from the raw data.
518 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200519 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200520 * @param[in] arg Type of YANG keyword argument expected.
Radek Krejcid3ca0632019-04-16 16:54:54 +0200521 * @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 +0200522 * @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 +0200523 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read string. If not needed,
524 * set to NULL. Otherwise equal to \p word_p.
525 * @param[out] word_len Length of the read string.
526 *
527 * @return LY_ERR values.
Michal Vasko7fbc8162018-09-17 10:35:16 +0200528 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200529LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100530get_argument(struct lys_yang_parser_ctx *ctx, enum yang_arg arg, uint16_t *flags, char **word_p,
Radek Krejci0f969882020-08-21 16:56:47 +0200531 char **word_b, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200532{
Radek Krejci44ceedc2018-10-02 15:54:31 +0200533 size_t buf_len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200534 uint8_t prefix = 0;
Michal Vasko69730152020-10-09 16:30:07 +0200535
Michal Vasko7fbc8162018-09-17 10:35:16 +0200536 /* word buffer - dynamically allocated */
537 *word_b = NULL;
538
539 /* word pointer - just a pointer to data */
540 *word_p = NULL;
541
542 *word_len = 0;
Radek Krejci33090f92020-12-17 20:12:46 +0100543 while (ctx->in->current[0]) {
544 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200545 case '\'':
546 case '\"':
547 if (*word_len) {
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200548 /* invalid - quotes cannot be in unquoted string and only optsep, ; or { can follow it */
Radek Krejci33090f92020-12-17 20:12:46 +0100549 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200550 "unquoted string character, optsep, semicolon or opening brace");
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200551 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200552 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200553 if (flags) {
Radek Krejci33090f92020-12-17 20:12:46 +0100554 (*flags) |= ctx->in->current[0] == '\'' ? LYS_SINGLEQUOTED : LYS_DOUBLEQUOTED;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200555 }
Radek Krejci33090f92020-12-17 20:12:46 +0100556 LY_CHECK_RET(read_qstring(ctx, arg, word_p, word_b, word_len, &buf_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200557 goto str_end;
558 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100559 if (ctx->in->current[1] == '/') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200560 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100561 MOVE_INPUT(ctx, 2);
562 LY_CHECK_RET(skip_comment(ctx, 1));
563 } else if (ctx->in->current[1] == '*') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200564 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100565 MOVE_INPUT(ctx, 2);
566 LY_CHECK_RET(skip_comment(ctx, 2));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200567 } else {
568 /* not a comment after all */
Radek Krejci33090f92020-12-17 20:12:46 +0100569 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 +0200570 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200571 break;
572 case ' ':
573 if (*word_len) {
574 /* word is finished */
575 goto str_end;
576 }
Radek Krejci33090f92020-12-17 20:12:46 +0100577 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200578 break;
579 case '\t':
580 if (*word_len) {
581 /* word is finished */
582 goto str_end;
583 }
584 /* tabs count for 8 spaces */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100585 ctx->indent += Y_TAB_SPACES;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200586
Radek Krejci33090f92020-12-17 20:12:46 +0100587 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200588 break;
589 case '\n':
590 if (*word_len) {
591 /* word is finished */
592 goto str_end;
593 }
Radek Krejcidd713ce2021-01-04 23:12:12 +0100594 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid54412f2020-12-17 20:25:35 +0100595 MOVE_INPUT(ctx, 1);
596
Michal Vasko7fbc8162018-09-17 10:35:16 +0200597 /* reset indent */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200598 ctx->indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200599 break;
600 case ';':
601 case '{':
602 if (*word_len || (arg == Y_MAYBE_STR_ARG)) {
603 /* word is finished */
604 goto str_end;
605 }
606
Radek Krejci33090f92020-12-17 20:12:46 +0100607 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current, "an argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200608 return LY_EVALID;
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200609 case '}':
610 /* invalid - braces cannot be in unquoted string (opening braces terminates the string and can follow it) */
Radek Krejci33090f92020-12-17 20:12:46 +0100611 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200612 "unquoted string character, optsep, semicolon or opening brace");
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200613 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200614 default:
Radek Krejci33090f92020-12-17 20:12:46 +0100615 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 +0200616 break;
617 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200618 }
619
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200620 /* unexpected end of loop */
David Sedlákb3077192019-06-19 10:55:37 +0200621 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200622 return LY_EVALID;
623
Michal Vasko7fbc8162018-09-17 10:35:16 +0200624str_end:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200625 /* terminating NULL byte for buf */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200626 if (*word_b) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200627 (*word_b) = ly_realloc(*word_b, (*word_len) + 1);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200628 LY_CHECK_ERR_RET(!(*word_b), LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200629 (*word_b)[*word_len] = '\0';
Michal Vasko7fbc8162018-09-17 10:35:16 +0200630 *word_p = *word_b;
631 }
632
633 return LY_SUCCESS;
634}
635
Michal Vaskoea5abea2018-09-18 13:10:54 +0200636/**
637 * @brief Get another YANG keyword from the raw data.
638 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200639 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200640 * @param[out] kw YANG keyword read.
641 * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances.
642 * @param[out] word_len Length of the keyword in the data. Useful for extension instances.
643 *
644 * @return LY_ERR values.
645 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200646LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100647get_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 +0200648{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200649 uint8_t prefix;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200650 const char *word_start;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200651 size_t len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200652
653 if (word_p) {
654 *word_p = NULL;
655 *word_len = 0;
656 }
657
658 /* first skip "optsep", comments */
Radek Krejci33090f92020-12-17 20:12:46 +0100659 while (ctx->in->current[0]) {
660 switch (ctx->in->current[0]) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200661 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100662 if (ctx->in->current[1] == '/') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200663 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100664 MOVE_INPUT(ctx, 2);
665 LY_CHECK_RET(skip_comment(ctx, 1));
666 } else if (ctx->in->current[1] == '*') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200667 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100668 MOVE_INPUT(ctx, 2);
669 LY_CHECK_RET(skip_comment(ctx, 2));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200670 } else {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200671 /* error - not a comment after all, keyword cannot start with slash */
David Sedlákb3077192019-06-19 10:55:37 +0200672 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '/'.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200673 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200674 }
Radek Krejci13028282019-06-11 14:56:48 +0200675 continue;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200676 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200677 /* skip whitespaces (optsep) */
Radek Krejcidd713ce2021-01-04 23:12:12 +0100678 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200679 ctx->indent = 0;
680 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200681 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200682 /* skip whitespaces (optsep) */
683 ++ctx->indent;
684 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200685 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200686 /* skip whitespaces (optsep) */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100687 ctx->indent += Y_TAB_SPACES;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200688 break;
689 default:
690 /* either a keyword start or an invalid character */
691 goto keyword_start;
692 }
693
Radek Krejci33090f92020-12-17 20:12:46 +0100694 ly_in_skip(ctx->in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200695 }
696
697keyword_start:
Radek Krejci33090f92020-12-17 20:12:46 +0100698 word_start = ctx->in->current;
Radek Krejcid54412f2020-12-17 20:25:35 +0100699 *kw = lysp_match_kw(ctx->in, &ctx->indent);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200700
Michal Vasko69730152020-10-09 16:30:07 +0200701 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 +0200702 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200703 }
704
Radek Krejcid6b76452019-09-03 17:03:03 +0200705 if (*kw != LY_STMT_NONE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200706 /* make sure we have the whole keyword */
Radek Krejci33090f92020-12-17 20:12:46 +0100707 switch (ctx->in->current[0]) {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200708 case '\n':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200709 case '\t':
Radek Krejciabdd8062019-06-11 16:44:19 +0200710 case ' ':
711 /* mandatory "sep" is just checked, not eaten so nothing in the context is updated */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200712 break;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200713 case ':':
714 /* keyword is not actually a keyword, but prefix of an extension.
715 * To avoid repeated check of the prefix syntax, move to the point where the colon was read
716 * and we will be checking the keyword (extension instance) itself */
717 prefix = 1;
Radek Krejci33090f92020-12-17 20:12:46 +0100718 MOVE_INPUT(ctx, 1);
Radek Krejci156ccaf2018-10-15 15:49:17 +0200719 goto extension;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200720 case '{':
721 /* allowed only for input and output statements which can be without arguments */
Michal Vasko69730152020-10-09 16:30:07 +0200722 if ((*kw == LY_STMT_INPUT) || (*kw == LY_STMT_OUTPUT)) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200723 break;
724 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100725 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200726 default:
Radek Krejci33090f92020-12-17 20:12:46 +0100727 MOVE_INPUT(ctx, 1);
728 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start,
Michal Vasko69730152020-10-09 16:30:07 +0200729 "a keyword followed by a separator");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200730 return LY_EVALID;
731 }
732 } else {
733 /* still can be an extension */
734 prefix = 0;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200735extension:
Radek Krejcid54412f2020-12-17 20:25:35 +0100736 while (ctx->in->current[0] && (ctx->in->current[0] != ' ') && (ctx->in->current[0] != '\t') &&
737 (ctx->in->current[0] != '\n') && (ctx->in->current[0] != '{') && (ctx->in->current[0] != ';')) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200738 uint32_t c = 0;
739
Radek Krejci33090f92020-12-17 20:12:46 +0100740 LY_CHECK_ERR_RET(ly_getutf8(&ctx->in->current, &c, &len),
741 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[-len]), LY_EVALID);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200742 ++ctx->indent;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200743 /* check character validity */
Michal Vasko63f3d842020-07-08 10:10:14 +0200744 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c,
Radek Krejci33090f92020-12-17 20:12:46 +0100745 ctx->in->current - len == word_start ? 1 : 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200746 }
Radek Krejci33090f92020-12-17 20:12:46 +0100747 if (!ctx->in->current[0]) {
David Sedlákb3077192019-06-19 10:55:37 +0200748 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200749 return LY_EVALID;
750 }
751
752 /* prefix is mandatory for extension instances */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200753 if (prefix != 2) {
Radek Krejci33090f92020-12-17 20:12:46 +0100754 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start, "a keyword");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200755 return LY_EVALID;
756 }
757
Radek Krejcid6b76452019-09-03 17:03:03 +0200758 *kw = LY_STMT_EXTENSION_INSTANCE;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200759 }
Radek Krejci626df482018-10-11 15:06:31 +0200760success:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200761 if (word_p) {
762 *word_p = (char *)word_start;
Radek Krejci33090f92020-12-17 20:12:46 +0100763 *word_len = ctx->in->current - word_start;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200764 }
765
766 return LY_SUCCESS;
767}
768
Michal Vaskoea5abea2018-09-18 13:10:54 +0200769/**
770 * @brief Parse extension instance substatements.
771 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200772 * @param[in] ctx yang parser context for logging.
Radek Krejci335332a2019-09-05 13:03:35 +0200773 * @param[in] kw Statement keyword value matching @p word value.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200774 * @param[in] word Extension instance substatement name (keyword).
775 * @param[in] word_len Extension instance substatement name length.
776 * @param[in,out] child Children of this extension instance to add to.
777 *
778 * @return LY_ERR values.
779 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200780static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100781parse_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 +0200782 struct lysp_stmt **child)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200783{
784 char *buf;
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100785 LY_ERR ret = LY_SUCCESS;
Radek Krejci335332a2019-09-05 13:03:35 +0200786 enum ly_stmt child_kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200787 struct lysp_stmt *stmt, *par_child;
788
789 stmt = calloc(1, sizeof *stmt);
790 LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM);
791
Radek Krejcibb9b1982019-04-08 14:24:59 +0200792 /* insert into parent statements */
793 if (!*child) {
794 *child = stmt;
795 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +0200796 for (par_child = *child; par_child->next; par_child = par_child->next) {}
Radek Krejcibb9b1982019-04-08 14:24:59 +0200797 par_child->next = stmt;
798 }
799
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200800 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), word, word_len, &stmt->stmt));
Radek Krejci335332a2019-09-05 13:03:35 +0200801 stmt->kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200802
803 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100804 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, &stmt->flags, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200805
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
Radek Krejci33090f92020-12-17 20:12:46 +0100814 YANG_READ_SUBSTMT_FOR(ctx, child_kw, word, word_len, ret, ) {
815 LY_CHECK_RET(parse_ext_substmt(ctx, child_kw, word, word_len, &stmt->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200816 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200817 return ret;
818}
819
Michal Vaskoea5abea2018-09-18 13:10:54 +0200820/**
821 * @brief Parse extension instance.
822 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200823 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200824 * @param[in] ext_name Extension instance substatement name (keyword).
825 * @param[in] ext_name_len Extension instance substatement name length.
826 * @param[in] insubstmt Type of the keyword this extension instance is a substatement of.
827 * @param[in] insubstmt_index Index of the keyword instance this extension instance is a substatement of.
828 * @param[in,out] exts Extension instances to add to.
829 *
830 * @return LY_ERR values.
831 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200832static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100833parse_ext(struct lys_yang_parser_ctx *ctx, const char *ext_name, size_t ext_name_len, LYEXT_SUBSTMT insubstmt,
Radek Krejci0f969882020-08-21 16:56:47 +0200834 LY_ARRAY_COUNT_TYPE insubstmt_index, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200835{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100836 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200837 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200838 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200839 struct lysp_ext_instance *e;
Radek Krejcid6b76452019-09-03 17:03:03 +0200840 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200841
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200842 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *exts, e, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200843
844 /* store name and insubstmt info */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200845 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), ext_name, ext_name_len, &e->name));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200846 e->insubstmt = insubstmt;
847 e->insubstmt_index = insubstmt_index;
848
849 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100850 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200851
Radek Krejci0ae092d2018-09-20 16:43:19 +0200852 if (word) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200853 INSERT_WORD_RET(ctx, buf, e->argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200854 }
855
Radek Krejci33090f92020-12-17 20:12:46 +0100856 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
857 LY_CHECK_RET(parse_ext_substmt(ctx, kw, word, word_len, &e->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200858 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200859 return ret;
860}
861
Michal Vaskoea5abea2018-09-18 13:10:54 +0200862/**
863 * @brief Parse a generic text field without specific constraints. Those are contact, organization,
864 * description, etc...
865 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200866 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200867 * @param[in] substmt Type of this substatement.
868 * @param[in] substmt_index Index of this substatement.
869 * @param[in,out] value Place to store the parsed value.
870 * @param[in] arg Type of the YANG keyword argument (of the value).
871 * @param[in,out] exts Extension instances to add to.
872 *
873 * @return LY_ERR values.
874 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200875static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100876parse_text_field(struct lys_yang_parser_ctx *ctx, LYEXT_SUBSTMT substmt, uint32_t substmt_index,
Radek Krejci0f969882020-08-21 16:56:47 +0200877 const char **value, enum yang_arg arg, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200878{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100879 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200880 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200881 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200882 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200883
884 if (*value) {
David Sedlákb3077192019-06-19 10:55:37 +0200885 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200886 return LY_EVALID;
887 }
888
889 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +0100890 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200891
892 /* store value and spend buf if allocated */
Radek Krejci011e4aa2020-09-04 15:22:31 +0200893 INSERT_WORD_RET(ctx, buf, *value, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200894
Radek Krejci33090f92020-12-17 20:12:46 +0100895 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200896 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200897 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +0100898 LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, substmt_index, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200899 break;
900 default:
David Sedlákb3077192019-06-19 10:55:37 +0200901 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200902 return LY_EVALID;
903 }
904 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200905 return ret;
906}
907
Michal Vaskoea5abea2018-09-18 13:10:54 +0200908/**
909 * @brief Parse the yang-version statement.
910 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200911 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100912 * @param[out] version Storage for the parsed information.
Michal Vasko63f3d842020-07-08 10:10:14 +0200913 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200914 *
915 * @return LY_ERR values.
916 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200917static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100918parse_yangversion(struct lys_yang_parser_ctx *ctx, uint8_t *version, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200919{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100920 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200921 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200922 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200923 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200924
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100925 if (*version) {
David Sedlákb3077192019-06-19 10:55:37 +0200926 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200927 return LY_EVALID;
928 }
929
930 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +0100931 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200932
Radek Krejci96e48da2020-09-04 13:18:06 +0200933 if ((word_len == 1) && !strncmp(word, "1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100934 *version = LYS_VERSION_1_0;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100935 } else if ((word_len == ly_strlen_const("1.1")) && !strncmp(word, "1.1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100936 *version = LYS_VERSION_1_1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200937 } else {
David Sedlákb3077192019-06-19 10:55:37 +0200938 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200939 free(buf);
940 return LY_EVALID;
941 }
942 free(buf);
943
Radek Krejci33090f92020-12-17 20:12:46 +0100944 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200945 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200946 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +0100947 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_VERSION, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200948 break;
949 default:
David Sedlákb3077192019-06-19 10:55:37 +0200950 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200951 return LY_EVALID;
952 }
953 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200954 return ret;
955}
956
Michal Vaskoea5abea2018-09-18 13:10:54 +0200957/**
958 * @brief Parse the belongs-to statement.
959 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200960 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200961 * @param[in,out] prefix Place to store the parsed belongs-to prefix value.
962 * @param[in,out] exts Extension instances to add to.
963 *
964 * @return LY_ERR values.
965 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200966static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +0100967parse_belongsto(struct lys_yang_parser_ctx *ctx, const char **prefix, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200968{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100969 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200970 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200971 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200972 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200973
Michal Vaskoc3781c32020-10-06 14:04:08 +0200974 if (*prefix) {
David Sedlákb3077192019-06-19 10:55:37 +0200975 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200976 return LY_EVALID;
977 }
978
Michal Vaskoc3781c32020-10-06 14:04:08 +0200979 /* get value, it must match the main module */
Radek Krejci33090f92020-12-17 20:12:46 +0100980 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200981 if (ly_strncmp(ctx->parsed_mod->mod->name, word, word_len)) {
Michal Vaskoc3781c32020-10-06 14:04:08 +0200982 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 +0200983 (int)word_len, word, ctx->parsed_mod->mod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +0200984 free(buf);
985 return LY_EVALID;
986 }
987 free(buf);
Radek Krejcif09e4e82019-06-14 15:08:11 +0200988
Radek Krejci33090f92020-12-17 20:12:46 +0100989 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200990 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200991 case LY_STMT_PREFIX:
Radek Krejci33090f92020-12-17 20:12:46 +0100992 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_PREFIX, 0, prefix, Y_IDENTIF_ARG, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200993 break;
Radek Krejcid6b76452019-09-03 17:03:03 +0200994 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +0100995 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_BELONGSTO, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200996 break;
997 default:
David Sedlákb3077192019-06-19 10:55:37 +0200998 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200999 return LY_EVALID;
1000 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001001 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001002 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001003checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001004 /* mandatory substatements */
1005 if (!*prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001006 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001007 return LY_EVALID;
1008 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001009 return ret;
1010}
1011
Michal Vaskoea5abea2018-09-18 13:10:54 +02001012/**
1013 * @brief Parse the revision-date statement.
1014 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001015 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001016 * @param[in,out] rev Array to store the parsed value in.
1017 * @param[in,out] exts Extension instances to add to.
1018 *
1019 * @return LY_ERR values.
1020 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001021static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001022parse_revisiondate(struct lys_yang_parser_ctx *ctx, char *rev, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001023{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001024 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001025 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001026 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001027 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001028
1029 if (rev[0]) {
David Sedlákb3077192019-06-19 10:55:37 +02001030 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001031 return LY_EVALID;
1032 }
1033
1034 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001035 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001036
1037 /* check value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001038 if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision-date")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001039 free(buf);
1040 return LY_EVALID;
1041 }
1042
1043 /* store value and spend buf if allocated */
1044 strncpy(rev, word, word_len);
1045 free(buf);
1046
Radek Krejci33090f92020-12-17 20:12:46 +01001047 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001048 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001049 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001050 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_REVISIONDATE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001051 break;
1052 default:
David Sedlákb3077192019-06-19 10:55:37 +02001053 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001054 return LY_EVALID;
1055 }
1056 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001057 return ret;
1058}
1059
Michal Vaskoea5abea2018-09-18 13:10:54 +02001060/**
1061 * @brief Parse the include statement.
1062 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001063 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001064 * @param[in] module_name Name of the module to check name collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001065 * @param[in,out] includes Parsed includes to add to.
1066 *
1067 * @return LY_ERR values.
1068 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001069static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001070parse_include(struct lys_yang_parser_ctx *ctx, const char *module_name, struct lysp_include **includes)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001071{
Radek Krejcid33273d2018-10-25 14:55:52 +02001072 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001073 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001074 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001075 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001076 struct lysp_include *inc;
1077
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001078 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *includes, inc, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001079
1080 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001081 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001082
Radek Krejci011e4aa2020-09-04 15:22:31 +02001083 INSERT_WORD_RET(ctx, buf, inc->name, word, word_len);
Radek Krejci086c7132018-10-26 15:29:04 +02001084
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001085 /* submodules share the namespace with the module names, so there must not be
1086 * a module of the same name in the context, no need for revision matching */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001087 if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(PARSER_CTX(ctx), inc->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01001088 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", inc->name);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001089 return LY_EVALID;
1090 }
1091
Radek Krejci33090f92020-12-17 20:12:46 +01001092 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001093 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001094 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001095 PARSER_CHECK_STMTVER2_RET(ctx, "description", "include");
Radek Krejci33090f92020-12-17 20:12:46 +01001096 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &inc->dsc, Y_STR_ARG, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001097 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001098 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001099 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "include");
Radek Krejci33090f92020-12-17 20:12:46 +01001100 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &inc->ref, Y_STR_ARG, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001101 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001102 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001103 LY_CHECK_RET(parse_revisiondate(ctx, inc->rev, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001104 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001105 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001106 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001107 break;
1108 default:
David Sedlákb3077192019-06-19 10:55:37 +02001109 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001110 return LY_EVALID;
1111 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001112 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001113 return ret;
1114}
1115
Michal Vaskoea5abea2018-09-18 13:10:54 +02001116/**
1117 * @brief Parse the import statement.
1118 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001119 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001120 * @param[in] module_prefix Prefix of the module to check prefix collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001121 * @param[in,out] imports Parsed imports to add to.
1122 *
1123 * @return LY_ERR values.
1124 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001125static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001126parse_import(struct lys_yang_parser_ctx *ctx, const char *module_prefix, struct lysp_import **imports)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001127{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001128 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001129 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001130 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001131 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001132 struct lysp_import *imp;
1133
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001134 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *imports, imp, LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001135
1136 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001137 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02001138 INSERT_WORD_RET(ctx, buf, imp->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001139
Radek Krejci33090f92020-12-17 20:12:46 +01001140 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001141 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001142 case LY_STMT_PREFIX:
Radek Krejci33090f92020-12-17 20:12:46 +01001143 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_PREFIX, 0, &imp->prefix, Y_IDENTIF_ARG, &imp->exts));
Michal Vaskob36053d2020-03-26 15:49:30 +01001144 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 +02001145 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001146 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001147 PARSER_CHECK_STMTVER2_RET(ctx, "description", "import");
Radek Krejci33090f92020-12-17 20:12:46 +01001148 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &imp->dsc, Y_STR_ARG, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001149 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001150 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001151 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "import");
Radek Krejci33090f92020-12-17 20:12:46 +01001152 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &imp->ref, Y_STR_ARG, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001153 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001154 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001155 LY_CHECK_RET(parse_revisiondate(ctx, imp->rev, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001156 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001157 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001158 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001159 break;
1160 default:
David Sedlákb3077192019-06-19 10:55:37 +02001161 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001162 return LY_EVALID;
1163 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001164 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001165 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001166checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001167 /* mandatory substatements */
David Sedlákb3077192019-06-19 10:55:37 +02001168 LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001169
1170 return ret;
1171}
1172
Michal Vaskoea5abea2018-09-18 13:10:54 +02001173/**
1174 * @brief Parse the revision statement.
1175 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001176 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001177 * @param[in,out] revs Parsed revisions to add to.
1178 *
1179 * @return LY_ERR values.
1180 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001181static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001182parse_revision(struct lys_yang_parser_ctx *ctx, struct lysp_revision **revs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001183{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001184 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001185 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001186 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001187 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001188 struct lysp_revision *rev;
1189
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001190 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *revs, rev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001191
1192 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001193 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001194
1195 /* check value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001196 if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision")) {
David Sedlák68ef3dc2019-07-22 13:40:19 +02001197 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001198 return LY_EVALID;
1199 }
1200
Radek Krejcib7db73a2018-10-24 14:18:40 +02001201 strncpy(rev->date, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001202 free(buf);
1203
Radek Krejci33090f92020-12-17 20:12:46 +01001204 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001205 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001206 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01001207 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &rev->dsc, Y_STR_ARG, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001208 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001209 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001210 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &rev->ref, Y_STR_ARG, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001211 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001212 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001213 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001214 break;
1215 default:
David Sedlákb3077192019-06-19 10:55:37 +02001216 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001217 return LY_EVALID;
1218 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001219 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001220 return ret;
1221}
1222
Michal Vaskoea5abea2018-09-18 13:10:54 +02001223/**
1224 * @brief Parse a generic text field that can have more instances such as base.
1225 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001226 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001227 * @param[in] substmt Type of this substatement.
1228 * @param[in,out] texts Parsed values to add to.
1229 * @param[in] arg Type of the expected argument.
1230 * @param[in,out] exts Extension instances to add to.
1231 *
1232 * @return LY_ERR values.
1233 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001234static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001235parse_text_fields(struct lys_yang_parser_ctx *ctx, LYEXT_SUBSTMT substmt, const char ***texts, enum yang_arg arg,
Radek Krejci0f969882020-08-21 16:56:47 +02001236 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001237{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001238 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001239 char *buf, *word;
Radek Krejci151a5b72018-10-19 14:21:44 +02001240 const char **item;
1241 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001242 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001243
1244 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001245 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *texts, item, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001246
1247 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001248 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001249
Radek Krejci011e4aa2020-09-04 15:22:31 +02001250 INSERT_WORD_RET(ctx, buf, *item, word, word_len);
Radek Krejci33090f92020-12-17 20:12:46 +01001251 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001252 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001253 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001254 LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, LY_ARRAY_COUNT(*texts) - 1, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001255 break;
1256 default:
David Sedlákb3077192019-06-19 10:55:37 +02001257 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001258 return LY_EVALID;
1259 }
1260 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001261 return ret;
1262}
1263
Michal Vaskoea5abea2018-09-18 13:10:54 +02001264/**
Michal Vasko7f45cf22020-10-01 12:49:44 +02001265 * @brief Parse a generic text field that can have more instances such as base.
1266 *
1267 * @param[in] ctx yang parser context for logging.
Michal Vasko7f45cf22020-10-01 12:49:44 +02001268 * @param[in] substmt Type of this substatement.
1269 * @param[in,out] qnames Parsed qnames to add to.
1270 * @param[in] arg Type of the expected argument.
1271 * @param[in,out] exts Extension instances to add to.
1272 *
1273 * @return LY_ERR values.
1274 */
1275static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001276parse_qnames(struct lys_yang_parser_ctx *ctx, LYEXT_SUBSTMT substmt, struct lysp_qname **qnames,
Michal Vasko7f45cf22020-10-01 12:49:44 +02001277 enum yang_arg arg, struct lysp_ext_instance **exts)
1278{
1279 LY_ERR ret = LY_SUCCESS;
1280 char *buf, *word;
1281 struct lysp_qname *item;
1282 size_t word_len;
1283 enum ly_stmt kw;
1284
1285 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001286 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *qnames, item, LY_EMEM);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001287
1288 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001289 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001290
1291 INSERT_WORD_RET(ctx, buf, item->str, word, word_len);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001292 item->mod = ctx->parsed_mod;
Radek Krejci33090f92020-12-17 20:12:46 +01001293 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02001294 switch (kw) {
1295 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001296 LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, LY_ARRAY_COUNT(*qnames) - 1, exts));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001297 break;
1298 default:
1299 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
1300 return LY_EVALID;
1301 }
1302 }
1303 return ret;
1304}
1305
1306/**
Michal Vaskoea5abea2018-09-18 13:10:54 +02001307 * @brief Parse the config statement.
1308 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001309 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001310 * @param[in,out] flags Flags to add to.
1311 * @param[in,out] exts Extension instances to add to.
1312 *
1313 * @return LY_ERR values.
1314 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001315static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001316parse_config(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001317{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001318 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001319 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001320 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001321 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001322
1323 if (*flags & LYS_CONFIG_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001324 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001325 return LY_EVALID;
1326 }
1327
1328 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001329 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001330
Radek Krejcif13b87b2020-12-01 22:02:17 +01001331 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001332 *flags |= LYS_CONFIG_W;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001333 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001334 *flags |= LYS_CONFIG_R;
1335 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001336 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001337 free(buf);
1338 return LY_EVALID;
1339 }
1340 free(buf);
1341
Radek Krejci33090f92020-12-17 20:12:46 +01001342 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001343 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001344 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001345 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_CONFIG, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001346 break;
1347 default:
David Sedlákb3077192019-06-19 10:55:37 +02001348 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001349 return LY_EVALID;
1350 }
1351 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001352 return ret;
1353}
1354
Michal Vaskoea5abea2018-09-18 13:10:54 +02001355/**
1356 * @brief Parse the mandatory statement.
1357 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001358 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001359 * @param[in,out] flags Flags to add to.
1360 * @param[in,out] exts Extension instances to add to.
1361 *
1362 * @return LY_ERR values.
1363 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001364static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001365parse_mandatory(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001366{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001367 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001368 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001369 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001370 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001371
1372 if (*flags & LYS_MAND_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001373 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001374 return LY_EVALID;
1375 }
1376
1377 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001378 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001379
Radek Krejcif13b87b2020-12-01 22:02:17 +01001380 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001381 *flags |= LYS_MAND_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001382 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001383 *flags |= LYS_MAND_FALSE;
1384 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001385 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001386 free(buf);
1387 return LY_EVALID;
1388 }
1389 free(buf);
1390
Radek Krejci33090f92020-12-17 20:12:46 +01001391 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001392 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001393 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001394 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_MANDATORY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001395 break;
1396 default:
David Sedlákb3077192019-06-19 10:55:37 +02001397 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001398 return LY_EVALID;
1399 }
1400 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001401 return ret;
1402}
1403
Michal Vaskoea5abea2018-09-18 13:10:54 +02001404/**
1405 * @brief Parse a restriction such as range or length.
1406 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001407 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001408 * @param[in] restr_kw Type of this particular restriction.
1409 * @param[in,out] exts Extension instances to add to.
1410 *
1411 * @return LY_ERR values.
1412 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001413static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001414parse_restr(struct lys_yang_parser_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr *restr)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001415{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001416 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001417 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001418 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001419 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001420
1421 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001422 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001423
Michal Vaskob36053d2020-03-26 15:49:30 +01001424 CHECK_NONEMPTY(ctx, word_len, ly_stmt2str(restr_kw));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001425 INSERT_WORD_RET(ctx, buf, restr->arg.str, word, word_len);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001426 restr->arg.mod = ctx->parsed_mod;
Radek Krejci33090f92020-12-17 20:12:46 +01001427 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001428 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001429 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01001430 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001431 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001432 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001433 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001434 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001435 case LY_STMT_ERROR_APP_TAG:
Radek Krejci33090f92020-12-17 20:12:46 +01001436 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_ERRTAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001437 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001438 case LY_STMT_ERROR_MESSAGE:
Radek Krejci33090f92020-12-17 20:12:46 +01001439 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_ERRMSG, 0, &restr->emsg, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001440 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001441 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001442 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001443 break;
1444 default:
David Sedlákb3077192019-06-19 10:55:37 +02001445 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001446 return LY_EVALID;
1447 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001448 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001449 return ret;
1450}
1451
Michal Vaskoea5abea2018-09-18 13:10:54 +02001452/**
1453 * @brief Parse a restriction that can have more instances such as must.
1454 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001455 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001456 * @param[in] restr_kw Type of this particular restriction.
1457 * @param[in,out] restrs Restrictions to add to.
1458 *
1459 * @return LY_ERR values.
1460 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001461static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001462parse_restrs(struct lys_yang_parser_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr **restrs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001463{
1464 struct lysp_restr *restr;
1465
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001466 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *restrs, restr, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01001467 return parse_restr(ctx, restr_kw, restr);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001468}
1469
Michal Vaskoea5abea2018-09-18 13:10:54 +02001470/**
1471 * @brief Parse the status statement.
1472 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001473 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001474 * @param[in,out] flags Flags to add to.
1475 * @param[in,out] exts Extension instances to add to.
1476 *
1477 * @return LY_ERR values.
1478 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001479static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001480parse_status(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001481{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001482 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001483 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001484 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001485 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001486
1487 if (*flags & LYS_STATUS_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001488 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001489 return LY_EVALID;
1490 }
1491
1492 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001493 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001494
Radek Krejcif13b87b2020-12-01 22:02:17 +01001495 if ((word_len == ly_strlen_const("current")) && !strncmp(word, "current", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001496 *flags |= LYS_STATUS_CURR;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001497 } else if ((word_len == ly_strlen_const("deprecated")) && !strncmp(word, "deprecated", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001498 *flags |= LYS_STATUS_DEPRC;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001499 } else if ((word_len == ly_strlen_const("obsolete")) && !strncmp(word, "obsolete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001500 *flags |= LYS_STATUS_OBSLT;
1501 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001502 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001503 free(buf);
1504 return LY_EVALID;
1505 }
1506 free(buf);
1507
Radek Krejci33090f92020-12-17 20:12:46 +01001508 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001509 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001510 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001511 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_STATUS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001512 break;
1513 default:
David Sedlákb3077192019-06-19 10:55:37 +02001514 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001515 return LY_EVALID;
1516 }
1517 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001518 return ret;
1519}
1520
Michal Vaskoea5abea2018-09-18 13:10:54 +02001521/**
1522 * @brief Parse the when statement.
1523 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001524 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001525 * @param[in,out] when_p When pointer to parse to.
1526 *
1527 * @return LY_ERR values.
1528 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001529LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001530parse_when(struct lys_yang_parser_ctx *ctx, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001531{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001532 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001533 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001534 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001535 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001536 struct lysp_when *when;
1537
1538 if (*when_p) {
David Sedlákb3077192019-06-19 10:55:37 +02001539 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001540 return LY_EVALID;
1541 }
1542
1543 when = calloc(1, sizeof *when);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001544 LY_CHECK_ERR_RET(!when, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001545 *when_p = when;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001546
1547 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001548 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01001549 CHECK_NONEMPTY(ctx, word_len, "when");
Radek Krejci011e4aa2020-09-04 15:22:31 +02001550 INSERT_WORD_RET(ctx, buf, when->cond, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001551
Radek Krejci33090f92020-12-17 20:12:46 +01001552 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001553 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001554 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01001555 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &when->dsc, Y_STR_ARG, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001556 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001557 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001558 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &when->ref, Y_STR_ARG, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001559 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001560 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001561 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001562 break;
1563 default:
David Sedlákb3077192019-06-19 10:55:37 +02001564 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001565 return LY_EVALID;
1566 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001567 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001568 return ret;
1569}
1570
Michal Vaskoea5abea2018-09-18 13:10:54 +02001571/**
1572 * @brief Parse the anydata or anyxml statement.
1573 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001574 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001575 * @param[in] kw Type of this particular keyword.
1576 * @param[in,out] siblings Siblings to add to.
1577 *
1578 * @return LY_ERR values.
1579 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02001580LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001581parse_any(struct lys_yang_parser_ctx *ctx, enum ly_stmt kw, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001582{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001583 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001584 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001585 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001586 struct lysp_node_anydata *any;
1587
David Sedlák60adc092019-08-06 15:57:02 +02001588 /* create new structure and insert into siblings */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001589 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, any, next, LY_EMEM);
David Sedlák60adc092019-08-06 15:57:02 +02001590
Radek Krejcid6b76452019-09-03 17:03:03 +02001591 any->nodetype = kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001592 any->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001593
Michal Vasko7fbc8162018-09-17 10:35:16 +02001594 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01001595 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02001596 INSERT_WORD_RET(ctx, buf, any->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001597
1598 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01001599 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001600 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001601 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01001602 LY_CHECK_RET(parse_config(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001603 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001604 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01001605 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &any->dsc, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001606 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001607 case LY_STMT_IF_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01001608 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &any->iffeatures, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001609 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001610 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01001611 LY_CHECK_RET(parse_mandatory(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001612 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001613 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01001614 LY_CHECK_RET(parse_restrs(ctx, kw, &any->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001615 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001616 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001617 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &any->ref, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001618 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001619 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001620 LY_CHECK_RET(parse_status(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001621 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001622 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01001623 LY_CHECK_RET(parse_when(ctx, &any->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001624 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001625 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001626 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001627 break;
1628 default:
David Sedlákb3077192019-06-19 10:55:37 +02001629 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
Radek Krejci0f969882020-08-21 16:56:47 +02001630 (any->nodetype & LYS_ANYDATA) == LYS_ANYDATA ? ly_stmt2str(LY_STMT_ANYDATA) : ly_stmt2str(LY_STMT_ANYXML));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001631 return LY_EVALID;
1632 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001633 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001634 return ret;
1635}
1636
Michal Vaskoea5abea2018-09-18 13:10:54 +02001637/**
1638 * @brief Parse the value or position statement. Substatement of type enum statement.
1639 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001640 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001641 * @param[in] val_kw Type of this particular keyword.
1642 * @param[in,out] value Value to write to.
1643 * @param[in,out] flags Flags to write to.
1644 * @param[in,out] exts Extension instances to add to.
1645 *
1646 * @return LY_ERR values.
1647 */
David Sedlákd6ce6d72019-07-16 17:30:18 +02001648LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001649parse_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 +02001650 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001651{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001652 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001653 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001654 size_t word_len;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02001655 long int num = 0;
1656 unsigned long int unum = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001657 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001658
1659 if (*flags & LYS_SET_VALUE) {
David Sedlákb3077192019-06-19 10:55:37 +02001660 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001661 return LY_EVALID;
1662 }
1663 *flags |= LYS_SET_VALUE;
1664
1665 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001666 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001667
Radek Krejcid6b76452019-09-03 17:03:03 +02001668 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 +02001669 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001670 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001671 }
1672
1673 errno = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001674 if (val_kw == LY_STMT_VALUE) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001675 num = strtol(word, &ptr, LY_BASE_DEC);
Michal Vasko69730152020-10-09 16:30:07 +02001676 if ((num < INT64_C(-2147483648)) || (num > INT64_C(2147483647))) {
David Sedlákb3077192019-06-19 10:55:37 +02001677 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001678 goto error;
1679 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001680 } else {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001681 unum = strtoul(word, &ptr, LY_BASE_DEC);
Radek Krejci8b764662018-11-14 14:15:13 +01001682 if (unum > UINT64_C(4294967295)) {
David Sedlákb3077192019-06-19 10:55:37 +02001683 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001684 goto error;
1685 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001686 }
1687 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001688 if ((size_t)(ptr - word) != word_len) {
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;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001691 }
1692 if (errno == ERANGE) {
David Sedlákb3077192019-06-19 10:55:37 +02001693 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001694 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001695 }
Radek Krejcid6b76452019-09-03 17:03:03 +02001696 if (val_kw == LY_STMT_VALUE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001697 *value = num;
1698 } else {
1699 *value = unum;
1700 }
1701 free(buf);
1702
Radek Krejci33090f92020-12-17 20:12:46 +01001703 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001704 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001705 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001706 LY_CHECK_RET(parse_ext(ctx, word, word_len, val_kw == LY_STMT_VALUE ? LYEXT_SUBSTMT_VALUE : LYEXT_SUBSTMT_POSITION, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001707 break;
1708 default:
David Sedlákb3077192019-06-19 10:55:37 +02001709 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001710 return LY_EVALID;
1711 }
1712 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001713 return ret;
Radek Krejci8b764662018-11-14 14:15:13 +01001714
1715error:
1716 free(buf);
1717 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001718}
1719
Michal Vaskoea5abea2018-09-18 13:10:54 +02001720/**
1721 * @brief Parse the enum or bit statement. Substatement of type statement.
1722 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001723 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001724 * @param[in] enum_kw Type of this particular keyword.
1725 * @param[in,out] enums Enums or bits to add to.
1726 *
1727 * @return LY_ERR values.
1728 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001729static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001730parse_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 +02001731{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001732 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001733 char *buf, *word;
David Sedlák6544c182019-07-12 13:17:33 +02001734 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001735 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001736 struct lysp_type_enum *enm;
1737
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001738 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *enums, enm, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001739
1740 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001741 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 +02001742 if (enum_kw == LY_STMT_ENUM) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001743 ret = lysp_check_enum_name((struct lys_parser_ctx *)ctx, (const char *)word, word_len);
David Sedlák6544c182019-07-12 13:17:33 +02001744 LY_CHECK_ERR_RET(ret, free(buf), ret);
Radek Krejci335332a2019-09-05 13:03:35 +02001745 } /* else nothing specific for YANG_BIT */
Radek Krejci8b764662018-11-14 14:15:13 +01001746
Radek Krejci011e4aa2020-09-04 15:22:31 +02001747 INSERT_WORD_RET(ctx, buf, enm->name, word, word_len);
Radek Krejci8b764662018-11-14 14:15:13 +01001748 CHECK_UNIQUENESS(ctx, *enums, name, ly_stmt2str(enum_kw), enm->name);
1749
Radek Krejci33090f92020-12-17 20:12:46 +01001750 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001751 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001752 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01001753 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &enm->dsc, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001754 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001755 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02001756 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", ly_stmt2str(enum_kw));
Radek Krejci33090f92020-12-17 20:12:46 +01001757 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &enm->iffeatures, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001758 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001759 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001760 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &enm->ref, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001761 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001762 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001763 LY_CHECK_RET(parse_status(ctx, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001764 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001765 case LY_STMT_VALUE:
1766 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 +02001767 ly_stmt2str(enum_kw)), LY_EVALID);
Radek Krejci33090f92020-12-17 20:12:46 +01001768 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, &enm->value, &enm->flags, &enm->exts));
David Sedlák9fb515f2019-07-11 10:33:58 +02001769 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001770 case LY_STMT_POSITION:
1771 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 +02001772 ly_stmt2str(enum_kw)), LY_EVALID);
Radek Krejci33090f92020-12-17 20:12:46 +01001773 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, &enm->value, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001774 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001775 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001776 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001777 break;
1778 default:
David Sedlákb3077192019-06-19 10:55:37 +02001779 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001780 return LY_EVALID;
1781 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001782 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001783 return ret;
1784}
1785
Michal Vaskoea5abea2018-09-18 13:10:54 +02001786/**
1787 * @brief Parse the fraction-digits statement. Substatement of type statement.
1788 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001789 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001790 * @param[in,out] fracdig Value to write to.
1791 * @param[in,out] exts Extension instances to add to.
1792 *
1793 * @return LY_ERR values.
1794 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001795static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001796parse_type_fracdigits(struct lys_yang_parser_ctx *ctx, uint8_t *fracdig, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001797{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001798 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001799 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001800 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001801 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02001802 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001803
1804 if (*fracdig) {
David Sedlákb3077192019-06-19 10:55:37 +02001805 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001806 return LY_EVALID;
1807 }
1808
1809 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001810 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001811
1812 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
David Sedlákb3077192019-06-19 10:55:37 +02001813 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001814 free(buf);
1815 return LY_EVALID;
1816 }
1817
1818 errno = 0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001819 num = strtoul(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001820 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001821 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001822 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001823 free(buf);
1824 return LY_EVALID;
1825 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001826 if ((errno == ERANGE) || (num > LY_TYPE_DEC64_FD_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02001827 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001828 free(buf);
1829 return LY_EVALID;
1830 }
1831 *fracdig = num;
1832 free(buf);
1833
Radek Krejci33090f92020-12-17 20:12:46 +01001834 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001835 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001836 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001837 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_FRACDIGITS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001838 break;
1839 default:
David Sedlákb3077192019-06-19 10:55:37 +02001840 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001841 return LY_EVALID;
1842 }
1843 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001844 return ret;
1845}
1846
Michal Vaskoea5abea2018-09-18 13:10:54 +02001847/**
1848 * @brief Parse the require-instance statement. Substatement of type statement.
1849 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001850 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001851 * @param[in,out] reqinst Value to write to.
1852 * @param[in,out] flags Flags to write to.
1853 * @param[in,out] exts Extension instances to add to.
1854 *
1855 * @return LY_ERR values.
1856 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001857static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001858parse_type_reqinstance(struct lys_yang_parser_ctx *ctx, uint8_t *reqinst, uint16_t *flags,
Radek Krejci0f969882020-08-21 16:56:47 +02001859 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001860{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001861 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001862 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001863 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001864 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001865
1866 if (*flags & LYS_SET_REQINST) {
David Sedlákb3077192019-06-19 10:55:37 +02001867 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001868 return LY_EVALID;
1869 }
1870 *flags |= LYS_SET_REQINST;
1871
1872 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001873 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001874
Radek Krejcif13b87b2020-12-01 22:02:17 +01001875 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001876 *reqinst = 1;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001877 } else if ((word_len != ly_strlen_const("false")) || strncmp(word, "false", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001878 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001879 free(buf);
1880 return LY_EVALID;
1881 }
1882 free(buf);
1883
Radek Krejci33090f92020-12-17 20:12:46 +01001884 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001885 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001886 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001887 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_REQINSTANCE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001888 break;
1889 default:
David Sedlákb3077192019-06-19 10:55:37 +02001890 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001891 return LY_EVALID;
1892 }
1893 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001894 return ret;
1895}
1896
Michal Vaskoea5abea2018-09-18 13:10:54 +02001897/**
1898 * @brief Parse the modifier statement. Substatement of type pattern statement.
1899 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001900 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001901 * @param[in,out] pat Value to write to.
1902 * @param[in,out] exts Extension instances to add to.
1903 *
1904 * @return LY_ERR values.
1905 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001906static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001907parse_type_pattern_modifier(struct lys_yang_parser_ctx *ctx, const char **pat, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001908{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001909 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001910 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001911 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001912 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001913
Radek Krejcif13b87b2020-12-01 22:02:17 +01001914 if ((*pat)[0] == LYSP_RESTR_PATTERN_NACK) {
David Sedlákb3077192019-06-19 10:55:37 +02001915 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001916 return LY_EVALID;
1917 }
1918
1919 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001920 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001921
Radek Krejcif13b87b2020-12-01 22:02:17 +01001922 if ((word_len != ly_strlen_const("invert-match")) || strncmp(word, "invert-match", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001923 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001924 free(buf);
1925 return LY_EVALID;
1926 }
1927 free(buf);
1928
1929 /* replace the value in the dictionary */
1930 buf = malloc(strlen(*pat) + 1);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001931 LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001932 strcpy(buf, *pat);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001933 lydict_remove(PARSER_CTX(ctx), *pat);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001934
Radek Krejcif13b87b2020-12-01 22:02:17 +01001935 assert(buf[0] == LYSP_RESTR_PATTERN_ACK);
1936 buf[0] = LYSP_RESTR_PATTERN_NACK;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001937 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, pat));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001938
Radek Krejci33090f92020-12-17 20:12:46 +01001939 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001940 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001941 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001942 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_MODIFIER, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001943 break;
1944 default:
David Sedlákb3077192019-06-19 10:55:37 +02001945 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001946 return LY_EVALID;
1947 }
1948 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001949 return ret;
1950}
1951
Michal Vaskoea5abea2018-09-18 13:10:54 +02001952/**
1953 * @brief Parse the pattern statement. Substatement of type statement.
1954 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001955 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001956 * @param[in,out] patterns Restrictions to add to.
1957 *
1958 * @return LY_ERR values.
1959 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001960static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01001961parse_type_pattern(struct lys_yang_parser_ctx *ctx, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001962{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001963 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001964 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001965 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001966 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001967 struct lysp_restr *restr;
1968
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001969 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *patterns, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001970
1971 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001972 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001973
1974 /* add special meaning first byte */
1975 if (buf) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001976 buf = ly_realloc(buf, word_len + 2);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001977 word = buf;
1978 } else {
1979 buf = malloc(word_len + 2);
1980 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001981 LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci86d106e2018-10-18 09:53:19 +02001982 memmove(buf + 1, word, word_len);
Radek Krejcif13b87b2020-12-01 22:02:17 +01001983 buf[0] = LYSP_RESTR_PATTERN_ACK; /* pattern's default regular-match flag */
Radek Krejci86d106e2018-10-18 09:53:19 +02001984 buf[word_len + 1] = '\0'; /* terminating NULL byte */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001985 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, &restr->arg.str));
1986 restr->arg.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001987
Radek Krejci33090f92020-12-17 20:12:46 +01001988 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001989 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001990 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01001991 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001992 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001993 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01001994 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001995 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001996 case LY_STMT_ERROR_APP_TAG:
Radek Krejci33090f92020-12-17 20:12:46 +01001997 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_ERRTAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001998 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001999 case LY_STMT_ERROR_MESSAGE:
Radek Krejci33090f92020-12-17 20:12:46 +01002000 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_ERRMSG, 0, &restr->emsg, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002001 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002002 case LY_STMT_MODIFIER:
Radek Krejci335332a2019-09-05 13:03:35 +02002003 PARSER_CHECK_STMTVER2_RET(ctx, "modifier", "pattern");
Radek Krejci33090f92020-12-17 20:12:46 +01002004 LY_CHECK_RET(parse_type_pattern_modifier(ctx, &restr->arg.str, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002005 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002006 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002007 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002008 break;
2009 default:
David Sedlákb3077192019-06-19 10:55:37 +02002010 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002011 return LY_EVALID;
2012 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002013 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002014 return ret;
2015}
2016
Michal Vaskoea5abea2018-09-18 13:10:54 +02002017/**
2018 * @brief Parse the type statement.
2019 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002020 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002021 * @param[in,out] type Type to wrote to.
2022 *
2023 * @return LY_ERR values.
2024 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002025static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002026parse_type(struct lys_yang_parser_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002027{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002028 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002029 char *buf, *word;
Michal Vasko004d3152020-06-11 19:59:22 +02002030 const char *str_path = NULL;
Radek Krejciefd22f62018-09-27 11:47:58 +02002031 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002032 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002033 struct lysp_type *nest_type;
2034
2035 if (type->name) {
David Sedlákb3077192019-06-19 10:55:37 +02002036 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002037 return LY_EVALID;
2038 }
2039
2040 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002041 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002042 INSERT_WORD_RET(ctx, buf, type->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002043
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002044 /* set module */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002045 type->pmod = ctx->parsed_mod;
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002046
Radek Krejci33090f92020-12-17 20:12:46 +01002047 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002048 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002049 case LY_STMT_BASE:
Radek Krejci33090f92020-12-17 20:12:46 +01002050 LY_CHECK_RET(parse_text_fields(ctx, LYEXT_SUBSTMT_BASE, &type->bases, Y_PREF_IDENTIF_ARG, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002051 type->flags |= LYS_SET_BASE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002052 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002053 case LY_STMT_BIT:
Radek Krejci33090f92020-12-17 20:12:46 +01002054 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->bits));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002055 type->flags |= LYS_SET_BIT;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002056 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002057 case LY_STMT_ENUM:
Radek Krejci33090f92020-12-17 20:12:46 +01002058 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->enums));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002059 type->flags |= LYS_SET_ENUM;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002060 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002061 case LY_STMT_FRACTION_DIGITS:
Radek Krejci33090f92020-12-17 20:12:46 +01002062 LY_CHECK_RET(parse_type_fracdigits(ctx, &type->fraction_digits, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002063 type->flags |= LYS_SET_FRDIGITS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002064 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002065 case LY_STMT_LENGTH:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002066 if (type->length) {
David Sedlákb3077192019-06-19 10:55:37 +02002067 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002068 return LY_EVALID;
2069 }
2070 type->length = calloc(1, sizeof *type->length);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002071 LY_CHECK_ERR_RET(!type->length, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002072
Radek Krejci33090f92020-12-17 20:12:46 +01002073 LY_CHECK_RET(parse_restr(ctx, kw, type->length));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002074 type->flags |= LYS_SET_LENGTH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002075 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002076 case LY_STMT_PATH:
Michal Vasko004d3152020-06-11 19:59:22 +02002077 if (type->path) {
2078 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyext_substmt2str(LYEXT_SUBSTMT_PATH));
2079 return LY_EVALID;
2080 }
2081
Radek Krejci33090f92020-12-17 20:12:46 +01002082 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_PATH, 0, &str_path, Y_STR_ARG, &type->exts));
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002083 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 +02002084 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002085 lydict_remove(PARSER_CTX(ctx), str_path);
Michal Vasko004d3152020-06-11 19:59:22 +02002086 LY_CHECK_RET(ret);
Radek Krejcid505e3d2018-11-13 09:04:17 +01002087 type->flags |= LYS_SET_PATH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002088 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002089 case LY_STMT_PATTERN:
Radek Krejci33090f92020-12-17 20:12:46 +01002090 LY_CHECK_RET(parse_type_pattern(ctx, &type->patterns));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002091 type->flags |= LYS_SET_PATTERN;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002092 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002093 case LY_STMT_RANGE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002094 if (type->range) {
David Sedlákb3077192019-06-19 10:55:37 +02002095 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002096 return LY_EVALID;
2097 }
2098 type->range = calloc(1, sizeof *type->range);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002099 LY_CHECK_ERR_RET(!type->range, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002100
Radek Krejci33090f92020-12-17 20:12:46 +01002101 LY_CHECK_RET(parse_restr(ctx, kw, type->range));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002102 type->flags |= LYS_SET_RANGE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002103 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002104 case LY_STMT_REQUIRE_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002105 LY_CHECK_RET(parse_type_reqinstance(ctx, &type->require_instance, &type->flags, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002106 /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002107 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002108 case LY_STMT_TYPE:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002109 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), type->types, nest_type, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01002110 LY_CHECK_RET(parse_type(ctx, nest_type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002111 type->flags |= LYS_SET_TYPE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002112 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002113 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002114 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &type->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002115 break;
2116 default:
David Sedlákb3077192019-06-19 10:55:37 +02002117 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002118 return LY_EVALID;
2119 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002120 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002121 return ret;
2122}
2123
Michal Vaskoea5abea2018-09-18 13:10:54 +02002124/**
2125 * @brief Parse the leaf statement.
2126 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002127 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002128 * @param[in,out] siblings Siblings to add to.
2129 *
2130 * @return LY_ERR values.
2131 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002132LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002133parse_leaf(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002134{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002135 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002136 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002137 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002138 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002139 struct lysp_node_leaf *leaf;
2140
David Sedlák60adc092019-08-06 15:57:02 +02002141 /* create new leaf structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002142 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, leaf, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002143 leaf->nodetype = LYS_LEAF;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002144 leaf->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002145
Michal Vasko7fbc8162018-09-17 10:35:16 +02002146 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002147 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002148 INSERT_WORD_RET(ctx, buf, leaf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002149
2150 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01002151 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002152 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002153 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002154 LY_CHECK_RET(parse_config(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002155 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002156 case LY_STMT_DEFAULT:
Radek Krejci33090f92020-12-17 20:12:46 +01002157 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DEFAULT, 0, &leaf->dflt.str, Y_STR_ARG, &leaf->exts));
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002158 leaf->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002159 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002160 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01002161 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &leaf->dsc, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002162 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002163 case LY_STMT_IF_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01002164 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &leaf->iffeatures, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002165 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002166 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002167 LY_CHECK_RET(parse_mandatory(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002168 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002169 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002170 LY_CHECK_RET(parse_restrs(ctx, kw, &leaf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002171 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002172 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002173 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &leaf->ref, 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_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002176 LY_CHECK_RET(parse_status(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002177 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002178 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002179 LY_CHECK_RET(parse_type(ctx, &leaf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002180 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002181 case LY_STMT_UNITS:
Radek Krejci33090f92020-12-17 20:12:46 +01002182 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_UNITS, 0, &leaf->units, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002183 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002184 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002185 LY_CHECK_RET(parse_when(ctx, &leaf->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002186 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002187 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002188 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002189 break;
2190 default:
David Sedlákb3077192019-06-19 10:55:37 +02002191 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002192 return LY_EVALID;
2193 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002194 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002195 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002196checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002197 /* mandatory substatements */
2198 if (!leaf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002199 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002200 return LY_EVALID;
2201 }
2202
2203 return ret;
2204}
2205
Michal Vaskoea5abea2018-09-18 13:10:54 +02002206/**
2207 * @brief Parse the max-elements statement.
2208 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002209 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002210 * @param[in,out] max Value to write to.
2211 * @param[in,out] flags Flags to write to.
2212 * @param[in,out] exts Extension instances to add to.
2213 *
2214 * @return LY_ERR values.
2215 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002216LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002217parse_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 +02002218{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002219 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002220 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002221 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002222 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002223 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002224
2225 if (*flags & LYS_SET_MAX) {
David Sedlákb3077192019-06-19 10:55:37 +02002226 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002227 return LY_EVALID;
2228 }
2229 *flags |= LYS_SET_MAX;
2230
2231 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002232 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002233
2234 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
David Sedlákb3077192019-06-19 10:55:37 +02002235 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002236 free(buf);
2237 return LY_EVALID;
2238 }
2239
Radek Krejci7f9b6512019-09-18 13:11:09 +02002240 if (ly_strncmp("unbounded", word, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002241 errno = 0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002242 num = strtoul(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002243 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002244 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002245 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002246 free(buf);
2247 return LY_EVALID;
2248 }
2249 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002250 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002251 free(buf);
2252 return LY_EVALID;
2253 }
2254
2255 *max = num;
2256 }
2257 free(buf);
2258
Radek Krejci33090f92020-12-17 20:12:46 +01002259 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002260 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002261 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002262 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_MAX, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002263 break;
2264 default:
David Sedlákb3077192019-06-19 10:55:37 +02002265 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002266 return LY_EVALID;
2267 }
2268 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002269 return ret;
2270}
2271
Michal Vaskoea5abea2018-09-18 13:10:54 +02002272/**
2273 * @brief Parse the min-elements statement.
2274 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002275 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002276 * @param[in,out] min Value to write to.
2277 * @param[in,out] flags Flags to write to.
2278 * @param[in,out] exts Extension instances to add to.
2279 *
2280 * @return LY_ERR values.
2281 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002282LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002283parse_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 +02002284{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002285 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002286 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002287 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002288 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002289 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002290
2291 if (*flags & LYS_SET_MIN) {
David Sedlákb3077192019-06-19 10:55:37 +02002292 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002293 return LY_EVALID;
2294 }
2295 *flags |= LYS_SET_MIN;
2296
2297 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002298 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002299
2300 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
David Sedlákb3077192019-06-19 10:55:37 +02002301 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002302 free(buf);
2303 return LY_EVALID;
2304 }
2305
2306 errno = 0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002307 num = strtoul(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002308 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002309 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002310 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002311 free(buf);
2312 return LY_EVALID;
2313 }
2314 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002315 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002316 free(buf);
2317 return LY_EVALID;
2318 }
2319 *min = num;
2320 free(buf);
2321
Radek Krejci33090f92020-12-17 20:12:46 +01002322 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002323 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002324 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002325 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_MIN, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002326 break;
2327 default:
David Sedlákb3077192019-06-19 10:55:37 +02002328 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002329 return LY_EVALID;
2330 }
2331 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002332 return ret;
2333}
2334
Michal Vaskoea5abea2018-09-18 13:10:54 +02002335/**
2336 * @brief Parse the ordered-by statement.
2337 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002338 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002339 * @param[in,out] flags Flags to write to.
2340 * @param[in,out] exts Extension instances to add to.
2341 *
2342 * @return LY_ERR values.
2343 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002344static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002345parse_orderedby(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002346{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002347 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002348 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002349 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002350 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002351
2352 if (*flags & LYS_ORDBY_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02002353 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002354 return LY_EVALID;
2355 }
2356
2357 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002358 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002359
Radek Krejcif13b87b2020-12-01 22:02:17 +01002360 if ((word_len == ly_strlen_const("system")) && !strncmp(word, "system", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002361 *flags |= LYS_ORDBY_SYSTEM;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002362 } else if ((word_len == ly_strlen_const("user")) && !strncmp(word, "user", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002363 *flags |= LYS_ORDBY_USER;
2364 } else {
David Sedlákb3077192019-06-19 10:55:37 +02002365 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002366 free(buf);
2367 return LY_EVALID;
2368 }
2369 free(buf);
2370
Radek Krejci33090f92020-12-17 20:12:46 +01002371 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002372 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002373 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002374 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_ORDEREDBY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002375 break;
2376 default:
David Sedlákb3077192019-06-19 10:55:37 +02002377 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002378 return LY_EVALID;
2379 }
2380 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002381 return ret;
2382}
2383
Michal Vaskoea5abea2018-09-18 13:10:54 +02002384/**
2385 * @brief Parse the leaf-list statement.
2386 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002387 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002388 * @param[in,out] siblings Siblings to add to.
2389 *
2390 * @return LY_ERR values.
2391 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002392LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002393parse_leaflist(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002394{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002395 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002396 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002397 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002398 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002399 struct lysp_node_leaflist *llist;
2400
David Sedlák60adc092019-08-06 15:57:02 +02002401 /* create new leaf-list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002402 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, llist, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002403 llist->nodetype = LYS_LEAFLIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002404 llist->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002405
Michal Vasko7fbc8162018-09-17 10:35:16 +02002406 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002407 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002408 INSERT_WORD_RET(ctx, buf, llist->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002409
2410 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01002411 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002412 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002413 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002414 LY_CHECK_RET(parse_config(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002415 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002416 case LY_STMT_DEFAULT:
Radek Krejci335332a2019-09-05 13:03:35 +02002417 PARSER_CHECK_STMTVER2_RET(ctx, "default", "leaf-list");
Radek Krejci33090f92020-12-17 20:12:46 +01002418 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_DEFAULT, &llist->dflts, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002419 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002420 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01002421 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &llist->dsc, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002422 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002423 case LY_STMT_IF_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01002424 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &llist->iffeatures, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002425 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002426 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002427 LY_CHECK_RET(parse_maxelements(ctx, &llist->max, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002428 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002429 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002430 LY_CHECK_RET(parse_minelements(ctx, &llist->min, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002431 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002432 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002433 LY_CHECK_RET(parse_restrs(ctx, kw, &llist->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002434 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002435 case LY_STMT_ORDERED_BY:
Radek Krejci33090f92020-12-17 20:12:46 +01002436 LY_CHECK_RET(parse_orderedby(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002437 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002438 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002439 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &llist->ref, 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_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002442 LY_CHECK_RET(parse_status(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002443 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002444 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002445 LY_CHECK_RET(parse_type(ctx, &llist->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002446 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002447 case LY_STMT_UNITS:
Radek Krejci33090f92020-12-17 20:12:46 +01002448 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_UNITS, 0, &llist->units, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002449 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002450 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002451 LY_CHECK_RET(parse_when(ctx, &llist->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002452 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002453 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002454 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002455 break;
2456 default:
David Sedlákb3077192019-06-19 10:55:37 +02002457 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002458 return LY_EVALID;
2459 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002460 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002461 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002462checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002463 /* mandatory substatements */
2464 if (!llist->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002465 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002466 return LY_EVALID;
2467 }
2468
2469 return ret;
2470}
2471
Michal Vaskoea5abea2018-09-18 13:10:54 +02002472/**
2473 * @brief Parse the refine statement.
2474 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002475 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002476 * @param[in,out] refines Refines to add to.
2477 *
2478 * @return LY_ERR values.
2479 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002480static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002481parse_refine(struct lys_yang_parser_ctx *ctx, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002482{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002483 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002484 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002485 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002486 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002487 struct lysp_refine *rf;
2488
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002489 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002490
2491 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002492 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01002493 CHECK_NONEMPTY(ctx, word_len, "refine");
Radek Krejci011e4aa2020-09-04 15:22:31 +02002494 INSERT_WORD_RET(ctx, buf, rf->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002495
Radek Krejci33090f92020-12-17 20:12:46 +01002496 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002497 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002498 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002499 LY_CHECK_RET(parse_config(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002500 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002501 case LY_STMT_DEFAULT:
Radek Krejci33090f92020-12-17 20:12:46 +01002502 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_DEFAULT, &rf->dflts, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002503 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002504 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01002505 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &rf->dsc, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002506 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002507 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02002508 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "refine");
Radek Krejci33090f92020-12-17 20:12:46 +01002509 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &rf->iffeatures, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002510 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002511 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002512 LY_CHECK_RET(parse_maxelements(ctx, &rf->max, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002513 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002514 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002515 LY_CHECK_RET(parse_minelements(ctx, &rf->min, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002516 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002517 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002518 LY_CHECK_RET(parse_restrs(ctx, kw, &rf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002519 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002520 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002521 LY_CHECK_RET(parse_mandatory(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002522 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002523 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002524 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &rf->ref, 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_PRESENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002527 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_PRESENCE, 0, &rf->presence, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002528 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002529 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002530 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002531 break;
2532 default:
David Sedlákb3077192019-06-19 10:55:37 +02002533 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002534 return LY_EVALID;
2535 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002536 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002537 return ret;
2538}
2539
Michal Vaskoea5abea2018-09-18 13:10:54 +02002540/**
2541 * @brief Parse the typedef statement.
2542 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002543 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002544 * @param[in,out] typedefs Typedefs to add to.
2545 *
2546 * @return LY_ERR values.
2547 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002548static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002549parse_typedef(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002550{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002551 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002552 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002553 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002554 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002555 struct lysp_tpdf *tpdf;
2556
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002557 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002558
2559 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002560 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002561 INSERT_WORD_RET(ctx, buf, tpdf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002562
2563 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01002564 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002565 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002566 case LY_STMT_DEFAULT:
Radek Krejci33090f92020-12-17 20:12:46 +01002567 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DEFAULT, 0, &tpdf->dflt.str, Y_STR_ARG, &tpdf->exts));
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002568 tpdf->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002569 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002570 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01002571 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &tpdf->dsc, Y_STR_ARG, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002572 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002573 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002574 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &tpdf->ref, Y_STR_ARG, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002575 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002576 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002577 LY_CHECK_RET(parse_status(ctx, &tpdf->flags, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002578 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002579 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002580 LY_CHECK_RET(parse_type(ctx, &tpdf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002581 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002582 case LY_STMT_UNITS:
Radek Krejci33090f92020-12-17 20:12:46 +01002583 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_UNITS, 0, &tpdf->units, Y_STR_ARG, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002584 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002585 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002586 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002587 break;
2588 default:
David Sedlákb3077192019-06-19 10:55:37 +02002589 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002590 return LY_EVALID;
2591 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002592 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002593 LY_CHECK_RET(ret);
Radek Krejcibbe09a92018-11-08 09:36:54 +01002594checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002595 /* mandatory substatements */
2596 if (!tpdf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002597 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002598 return LY_EVALID;
2599 }
2600
Radek Krejcibbe09a92018-11-08 09:36:54 +01002601 /* store data for collision check */
Michal Vasko7f45cf22020-10-01 12:49:44 +02002602 if (parent && !(parent->nodetype & (LYS_GROUPING | LYS_RPC | LYS_ACTION | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF))) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02002603 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, parent, 0, NULL));
Radek Krejcibbe09a92018-11-08 09:36:54 +01002604 }
2605
Michal Vasko7fbc8162018-09-17 10:35:16 +02002606 return ret;
2607}
2608
Michal Vaskoea5abea2018-09-18 13:10:54 +02002609/**
2610 * @brief Parse the input or output statement.
2611 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002612 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002613 * @param[in] kw Type of this particular keyword
2614 * @param[in,out] inout_p Input/output pointer to write to.
2615 *
2616 * @return LY_ERR values.
2617 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002618static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01002619parse_inout(struct lys_yang_parser_ctx *ctx, enum ly_stmt inout_kw, struct lysp_node *parent,
Radek Krejci2a9fc652021-01-22 17:44:34 +01002620 struct lysp_node_action_inout *inout_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002621{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002622 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002623 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002624 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002625 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002626 ly_bool input = &((struct lysp_node_action *)parent)->input == inout_p ? 1 : 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002627
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002628 if (inout_p->nodetype) {
David Sedlákb3077192019-06-19 10:55:37 +02002629 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002630 return LY_EVALID;
2631 }
2632
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002633 /* initiate structure */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002634 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), input ? "input" : "output", 0, &inout_p->name));
2635 inout_p->nodetype = input ? LYS_INPUT : LYS_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002636 inout_p->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002637
2638 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01002639 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002640 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002641 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002642 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", ly_stmt2str(inout_kw));
Radek Krejci0f969882020-08-21 16:56:47 +02002643 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002644 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002645 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002646 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002647 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01002648 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002649 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002650 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002651 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002652 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002653 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01002654 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002655 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002656 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002657 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002658 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002659 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002660 LY_CHECK_RET(parse_list(ctx, (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_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01002663 LY_CHECK_RET(parse_uses(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_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002666 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)inout_p, &inout_p->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002667 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002668 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002669 PARSER_CHECK_STMTVER2_RET(ctx, "must", ly_stmt2str(inout_kw));
Radek Krejci33090f92020-12-17 20:12:46 +01002670 LY_CHECK_RET(parse_restrs(ctx, kw, &inout_p->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002671 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002672 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002673 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)inout_p, &inout_p->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002674 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002675 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002676 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inout_p->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002677 break;
2678 default:
David Sedlákb3077192019-06-19 10:55:37 +02002679 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002680 return LY_EVALID;
2681 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002682 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002683 LY_CHECK_RET(ret);
Michal Vaskob83af8a2020-01-06 09:49:22 +01002684
Radek Krejci7fc68292019-06-12 13:51:09 +02002685checks:
Radek Krejci01180ac2021-01-27 08:48:22 +01002686 if (!inout_p->child) {
Michal Vaskob83af8a2020-01-06 09:49:22 +01002687 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "data-def-stmt", ly_stmt2str(inout_kw));
2688 return LY_EVALID;
2689 }
2690
Michal Vasko7fbc8162018-09-17 10:35:16 +02002691 return ret;
2692}
2693
Michal Vaskoea5abea2018-09-18 13:10:54 +02002694/**
2695 * @brief Parse the action statement.
2696 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002697 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002698 * @param[in,out] actions Actions to add to.
2699 *
2700 * @return LY_ERR values.
2701 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002702LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002703parse_action(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002704{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002705 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002706 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002707 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002708 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002709 struct lysp_node_action *act;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002710
Radek Krejci2a9fc652021-01-22 17:44:34 +01002711 LY_LIST_NEW_RET(PARSER_CTX(ctx), actions, act, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002712
2713 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002714 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002715 INSERT_WORD_RET(ctx, buf, act->name, word, word_len);
Michal Vasko1bf09392020-03-27 12:38:10 +01002716 act->nodetype = parent ? LYS_ACTION : LYS_RPC;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002717 act->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002718
Radek Krejci33090f92020-12-17 20:12:46 +01002719 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002720 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002721 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01002722 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &act->dsc, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002723 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002724 case LY_STMT_IF_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01002725 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &act->iffeatures, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002726 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002727 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002728 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &act->ref, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002729 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002730 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002731 LY_CHECK_RET(parse_status(ctx, &act->flags, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002732 break;
2733
Radek Krejcid6b76452019-09-03 17:03:03 +02002734 case LY_STMT_INPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002735 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->input));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002736 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002737 case LY_STMT_OUTPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002738 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->output));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002739 break;
2740
Radek Krejcid6b76452019-09-03 17:03:03 +02002741 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002742 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)act, &act->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002743 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002744 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002745 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)act, &act->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002746 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002747 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002748 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002749 break;
2750 default:
David Sedlákb3077192019-06-19 10:55:37 +02002751 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), parent ? "action" : "rpc");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002752 return LY_EVALID;
2753 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002754 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002755 LY_CHECK_RET(ret);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002756
Radek Krejci2a9fc652021-01-22 17:44:34 +01002757checks:
Michal Vasko7f45cf22020-10-01 12:49:44 +02002758 /* always initialize inout, they are technically present (needed for later deviations/refines) */
2759 if (!act->input.nodetype) {
2760 act->input.nodetype = LYS_INPUT;
2761 act->input.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002762 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "input", 0, &act->input.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002763 }
2764 if (!act->output.nodetype) {
2765 act->output.nodetype = LYS_OUTPUT;
2766 act->output.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002767 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "output", 0, &act->output.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002768 }
2769
Michal Vasko7fbc8162018-09-17 10:35:16 +02002770 return ret;
2771}
2772
Michal Vaskoea5abea2018-09-18 13:10:54 +02002773/**
2774 * @brief Parse the notification statement.
2775 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002776 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002777 * @param[in,out] notifs Notifications to add to.
2778 *
2779 * @return LY_ERR values.
2780 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002781LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002782parse_notif(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002783{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002784 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002785 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002786 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002787 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002788 struct lysp_node_notif *notif;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002789
Radek Krejci2a9fc652021-01-22 17:44:34 +01002790 LY_LIST_NEW_RET(PARSER_CTX(ctx), notifs, notif, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002791
2792 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002793 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002794 INSERT_WORD_RET(ctx, buf, notif->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002795 notif->nodetype = LYS_NOTIF;
2796 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002797
Radek Krejci2a9fc652021-01-22 17:44:34 +01002798 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002799 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002800 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01002801 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &notif->dsc, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002802 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002803 case LY_STMT_IF_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01002804 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &notif->iffeatures, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002805 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002806 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002807 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &notif->ref, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002808 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002809 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002810 LY_CHECK_RET(parse_status(ctx, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002811 break;
2812
Radek Krejcid6b76452019-09-03 17:03:03 +02002813 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002814 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "notification");
Radek Krejci0f969882020-08-21 16:56:47 +02002815 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002816 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002817 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002818 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002819 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01002820 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002821 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002822 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002823 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002824 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002825 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01002826 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002827 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002828 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002829 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002830 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002831 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002832 LY_CHECK_RET(parse_list(ctx, (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_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01002835 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002836 break;
2837
Radek Krejcid6b76452019-09-03 17:03:03 +02002838 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002839 PARSER_CHECK_STMTVER2_RET(ctx, "must", "notification");
Radek Krejci33090f92020-12-17 20:12:46 +01002840 LY_CHECK_RET(parse_restrs(ctx, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002841 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002842 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002843 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)notif, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002844 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002845 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002846 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002847 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002848 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002849 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002850 break;
2851 default:
David Sedlákb3077192019-06-19 10:55:37 +02002852 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002853 return LY_EVALID;
2854 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002855 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002856
Michal Vasko7fbc8162018-09-17 10:35:16 +02002857 return ret;
2858}
2859
Michal Vaskoea5abea2018-09-18 13:10:54 +02002860/**
2861 * @brief Parse the grouping statement.
2862 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002863 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002864 * @param[in,out] groupings Groupings to add to.
2865 *
2866 * @return LY_ERR values.
2867 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002868LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002869parse_grouping(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002870{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002871 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002872 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002873 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002874 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002875 struct lysp_node_grp *grp;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002876
Radek Krejci2a9fc652021-01-22 17:44:34 +01002877 LY_LIST_NEW_RET(PARSER_CTX(ctx), groupings, grp, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002878
2879 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002880 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02002881 INSERT_WORD_RET(ctx, buf, grp->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002882 grp->nodetype = LYS_GROUPING;
2883 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002884
Radek Krejci2a9fc652021-01-22 17:44:34 +01002885 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002886 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002887 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01002888 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &grp->dsc, Y_STR_ARG, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002889 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002890 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002891 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &grp->ref, Y_STR_ARG, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002892 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002893 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002894 LY_CHECK_RET(parse_status(ctx, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002895 break;
2896
Radek Krejcid6b76452019-09-03 17:03:03 +02002897 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002898 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "grouping");
Radek Krejci0f969882020-08-21 16:56:47 +02002899 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002900 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002901 LY_CHECK_RET(parse_any(ctx, kw, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002902 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002903 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01002904 LY_CHECK_RET(parse_choice(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002905 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002906 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002907 LY_CHECK_RET(parse_container(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002908 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002909 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01002910 LY_CHECK_RET(parse_leaf(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002911 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002912 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002913 LY_CHECK_RET(parse_leaflist(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002914 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002915 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002916 LY_CHECK_RET(parse_list(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002917 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002918 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01002919 LY_CHECK_RET(parse_uses(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002920 break;
2921
Radek Krejcid6b76452019-09-03 17:03:03 +02002922 case LY_STMT_TYPEDEF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01002923 LY_CHECK_RET(parse_typedef(ctx, &grp->node, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002924 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002925 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02002926 PARSER_CHECK_STMTVER2_RET(ctx, "action", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002927 LY_CHECK_RET(parse_action(ctx, &grp->node, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002928 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002929 case LY_STMT_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01002930 LY_CHECK_RET(parse_grouping(ctx, &grp->node, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002931 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002932 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02002933 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002934 LY_CHECK_RET(parse_notif(ctx, &grp->node, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002935 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002936 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002937 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002938 break;
2939 default:
David Sedlákb3077192019-06-19 10:55:37 +02002940 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002941 return LY_EVALID;
2942 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002943 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002944
Michal Vasko7fbc8162018-09-17 10:35:16 +02002945 return ret;
2946}
2947
Michal Vaskoea5abea2018-09-18 13:10:54 +02002948/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02002949 * @brief Parse the augment statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002950 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002951 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002952 * @param[in,out] augments Augments to add to.
2953 *
2954 * @return LY_ERR values.
2955 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002956LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002957parse_augment(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002958{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002959 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002960 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002961 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002962 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002963 struct lysp_node_augment *aug;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002964
Radek Krejci2a9fc652021-01-22 17:44:34 +01002965 LY_LIST_NEW_RET(PARSER_CTX(ctx), augments, aug, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002966
2967 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002968 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01002969 CHECK_NONEMPTY(ctx, word_len, "augment");
Radek Krejci011e4aa2020-09-04 15:22:31 +02002970 INSERT_WORD_RET(ctx, buf, aug->nodeid, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002971 aug->nodetype = LYS_AUGMENT;
2972 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002973
Radek Krejci2a9fc652021-01-22 17:44:34 +01002974 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002975 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002976 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01002977 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &aug->dsc, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002978 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002979 case LY_STMT_IF_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01002980 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &aug->iffeatures, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002981 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002982 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01002983 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &aug->ref, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002984 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002985 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002986 LY_CHECK_RET(parse_status(ctx, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002987 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002988 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002989 LY_CHECK_RET(parse_when(ctx, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002990 break;
2991
Radek Krejcid6b76452019-09-03 17:03:03 +02002992 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002993 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "augment");
Radek Krejci0f969882020-08-21 16:56:47 +02002994 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002995 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01002996 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002997 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002998 case LY_STMT_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01002999 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003000 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003001 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003002 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003003 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003004 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003005 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003006 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003007 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003008 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003009 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003010 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003011 LY_CHECK_RET(parse_leaflist(ctx, (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_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003014 LY_CHECK_RET(parse_list(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_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003017 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003018 break;
3019
Radek Krejcid6b76452019-09-03 17:03:03 +02003020 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003021 PARSER_CHECK_STMTVER2_RET(ctx, "action", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003022 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003023 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003024 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003025 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003026 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003027 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003028 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003029 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003030 break;
3031 default:
David Sedlákb3077192019-06-19 10:55:37 +02003032 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003033 return LY_EVALID;
3034 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003035 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003036
Michal Vasko7fbc8162018-09-17 10:35:16 +02003037 return ret;
3038}
3039
Michal Vaskoea5abea2018-09-18 13:10:54 +02003040/**
3041 * @brief Parse the uses statement.
3042 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003043 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003044 * @param[in,out] siblings Siblings to add to.
3045 *
3046 * @return LY_ERR values.
3047 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003048LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003049parse_uses(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003050{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003051 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003052 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003053 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003054 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003055 struct lysp_node_uses *uses;
3056
David Sedlák60adc092019-08-06 15:57:02 +02003057 /* create uses structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003058 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, uses, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003059 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003060 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003061
Michal Vasko7fbc8162018-09-17 10:35:16 +02003062 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003063 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003064 INSERT_WORD_RET(ctx, buf, uses->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003065
3066 /* parse substatements */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003067 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003068 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003069 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01003070 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &uses->dsc, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003071 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003072 case LY_STMT_IF_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01003073 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &uses->iffeatures, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003074 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003075 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003076 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &uses->ref, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003077 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003078 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003079 LY_CHECK_RET(parse_status(ctx, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003080 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003081 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003082 LY_CHECK_RET(parse_when(ctx, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003083 break;
3084
Radek Krejcid6b76452019-09-03 17:03:03 +02003085 case LY_STMT_REFINE:
Radek Krejci33090f92020-12-17 20:12:46 +01003086 LY_CHECK_RET(parse_refine(ctx, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003087 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003088 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003089 LY_CHECK_RET(parse_augment(ctx, (struct lysp_node *)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003090 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003091 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003092 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003093 break;
3094 default:
David Sedlákb3077192019-06-19 10:55:37 +02003095 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003096 return LY_EVALID;
3097 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003098 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003099
Michal Vasko7fbc8162018-09-17 10:35:16 +02003100 return ret;
3101}
3102
Michal Vaskoea5abea2018-09-18 13:10:54 +02003103/**
3104 * @brief Parse the case statement.
3105 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003106 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003107 * @param[in,out] siblings Siblings to add to.
3108 *
3109 * @return LY_ERR values.
3110 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003111LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003112parse_case(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003113{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003114 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003115 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003116 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003117 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003118 struct lysp_node_case *cas;
3119
David Sedlák60adc092019-08-06 15:57:02 +02003120 /* create new case structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003121 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cas, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003122 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003123 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003124
Michal Vasko7fbc8162018-09-17 10:35:16 +02003125 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003126 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003127 INSERT_WORD_RET(ctx, buf, cas->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003128
3129 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01003130 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003131 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003132 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01003133 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &cas->dsc, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003134 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003135 case LY_STMT_IF_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01003136 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &cas->iffeatures, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003137 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003138 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003139 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &cas->ref, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003140 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003141 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003142 LY_CHECK_RET(parse_status(ctx, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003143 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003144 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003145 LY_CHECK_RET(parse_when(ctx, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003146 break;
3147
Radek Krejcid6b76452019-09-03 17:03:03 +02003148 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003149 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "case");
Radek Krejci0f969882020-08-21 16:56:47 +02003150 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003151 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003152 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003153 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003154 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003155 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003156 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003157 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003158 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003159 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003160 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003161 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003162 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003163 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003164 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003165 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003166 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003167 LY_CHECK_RET(parse_list(ctx, (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_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003170 LY_CHECK_RET(parse_uses(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_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003173 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003174 break;
3175 default:
David Sedlákb3077192019-06-19 10:55:37 +02003176 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003177 return LY_EVALID;
3178 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003179 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003180 return ret;
3181}
3182
Michal Vaskoea5abea2018-09-18 13:10:54 +02003183/**
3184 * @brief Parse the choice statement.
3185 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003186 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003187 * @param[in,out] siblings Siblings to add to.
3188 *
3189 * @return LY_ERR values.
3190 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003191LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003192parse_choice(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003193{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003194 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003195 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003196 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003197 enum ly_stmt kw;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003198 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003199
David Sedlák60adc092019-08-06 15:57:02 +02003200 /* create new choice structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003201 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, choice, next, LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003202 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003203 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003204
Michal Vasko7fbc8162018-09-17 10:35:16 +02003205 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003206 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003207 INSERT_WORD_RET(ctx, buf, choice->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003208
3209 /* parse substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01003210 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003211 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003212 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003213 LY_CHECK_RET(parse_config(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003214 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003215 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01003216 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &choice->dsc, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003217 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003218 case LY_STMT_IF_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01003219 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &choice->iffeatures, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003220 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003221 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01003222 LY_CHECK_RET(parse_mandatory(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003223 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003224 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003225 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &choice->ref, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003226 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003227 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003228 LY_CHECK_RET(parse_status(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_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003231 LY_CHECK_RET(parse_when(ctx, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003232 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003233 case LY_STMT_DEFAULT:
Radek Krejci33090f92020-12-17 20:12:46 +01003234 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DEFAULT, 0, &choice->dflt.str, Y_PREF_IDENTIF_ARG,
Michal Vasko7f45cf22020-10-01 12:49:44 +02003235 &choice->exts));
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003236 choice->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003237 break;
3238
Radek Krejcid6b76452019-09-03 17:03:03 +02003239 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003240 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "choice");
Radek Krejci0f969882020-08-21 16:56:47 +02003241 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003242 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003243 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003244 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003245 case LY_STMT_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003246 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003247 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003248 case LY_STMT_CHOICE:
Radek Krejci335332a2019-09-05 13:03:35 +02003249 PARSER_CHECK_STMTVER2_RET(ctx, "choice", "choice");
Radek Krejci33090f92020-12-17 20:12:46 +01003250 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003251 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003252 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003253 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003254 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003255 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003256 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003257 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003258 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003259 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003260 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003261 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003262 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003263 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003264 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003265 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003266 break;
3267 default:
David Sedlákb3077192019-06-19 10:55:37 +02003268 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003269 return LY_EVALID;
3270 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003271 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003272 return ret;
3273}
3274
Michal Vaskoea5abea2018-09-18 13:10:54 +02003275/**
3276 * @brief Parse the container statement.
3277 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003278 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003279 * @param[in,out] siblings Siblings to add to.
3280 *
3281 * @return LY_ERR values.
3282 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003283LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003284parse_container(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003285{
3286 LY_ERR ret = 0;
3287 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003288 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003289 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003290 struct lysp_node_container *cont;
3291
David Sedlák60adc092019-08-06 15:57:02 +02003292 /* create new container structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003293 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cont, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003294 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003295 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003296
Michal Vasko7fbc8162018-09-17 10:35:16 +02003297 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003298 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003299 INSERT_WORD_RET(ctx, buf, cont->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003300
3301 /* parse substatements */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003302 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003303 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003304 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003305 LY_CHECK_RET(parse_config(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003306 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003307 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01003308 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &cont->dsc, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003309 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003310 case LY_STMT_IF_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01003311 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &cont->iffeatures, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003312 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003313 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003314 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &cont->ref, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003315 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003316 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003317 LY_CHECK_RET(parse_status(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003318 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003319 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003320 LY_CHECK_RET(parse_when(ctx, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003321 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003322 case LY_STMT_PRESENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003323 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_PRESENCE, 0, &cont->presence, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003324 break;
3325
Radek Krejcid6b76452019-09-03 17:03:03 +02003326 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003327 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "container");
Radek Krejci0f969882020-08-21 16:56:47 +02003328 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003329 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003330 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003331 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003332 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003333 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003334 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003335 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003336 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003337 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003338 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003339 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003340 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003341 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003342 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003343 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003344 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003345 LY_CHECK_RET(parse_list(ctx, (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_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003348 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003349 break;
3350
Radek Krejcid6b76452019-09-03 17:03:03 +02003351 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003352 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)cont, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003353 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003354 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003355 LY_CHECK_RET(parse_restrs(ctx, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003356 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003357 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003358 PARSER_CHECK_STMTVER2_RET(ctx, "action", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003359 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003360 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003361 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003362 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003363 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003364 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003365 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003366 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003367 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003368 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003369 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003370 break;
3371 default:
David Sedlákb3077192019-06-19 10:55:37 +02003372 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003373 return LY_EVALID;
3374 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003375 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003376
Michal Vasko7fbc8162018-09-17 10:35:16 +02003377 return ret;
3378}
3379
Michal Vaskoea5abea2018-09-18 13:10:54 +02003380/**
3381 * @brief Parse the list statement.
3382 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003383 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003384 * @param[in,out] siblings Siblings to add to.
3385 *
3386 * @return LY_ERR values.
3387 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003388LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003389parse_list(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003390{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003391 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003392 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003393 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003394 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003395 struct lysp_node_list *list;
3396
David Sedlák60adc092019-08-06 15:57:02 +02003397 /* create new list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003398 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, list, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003399 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003400 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003401
Michal Vasko7fbc8162018-09-17 10:35:16 +02003402 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003403 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003404 INSERT_WORD_RET(ctx, buf, list->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003405
3406 /* parse substatements */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003407 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003408 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003409 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003410 LY_CHECK_RET(parse_config(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003411 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003412 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01003413 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &list->dsc, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003414 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003415 case LY_STMT_IF_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01003416 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &list->iffeatures, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003417 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003418 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003419 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &list->ref, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003420 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003421 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003422 LY_CHECK_RET(parse_status(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003423 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003424 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003425 LY_CHECK_RET(parse_when(ctx, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003426 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003427 case LY_STMT_KEY:
Radek Krejci33090f92020-12-17 20:12:46 +01003428 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_KEY, 0, &list->key, 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_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003431 LY_CHECK_RET(parse_maxelements(ctx, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003432 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003433 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003434 LY_CHECK_RET(parse_minelements(ctx, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003435 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003436 case LY_STMT_ORDERED_BY:
Radek Krejci33090f92020-12-17 20:12:46 +01003437 LY_CHECK_RET(parse_orderedby(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_UNIQUE:
Radek Krejci33090f92020-12-17 20:12:46 +01003440 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_UNIQUE, &list->uniques, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003441 break;
3442
Radek Krejcid6b76452019-09-03 17:03:03 +02003443 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003444 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "list");
Radek Krejci0f969882020-08-21 16:56:47 +02003445 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003446 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003447 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003448 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003449 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003450 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003451 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003452 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003453 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003454 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003455 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003456 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003457 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003458 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003459 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003460 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003461 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003462 LY_CHECK_RET(parse_list(ctx, (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_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003465 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003466 break;
3467
Radek Krejcid6b76452019-09-03 17:03:03 +02003468 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003469 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)list, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003470 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003471 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003472 LY_CHECK_RET(parse_restrs(ctx, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003473 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003474 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003475 PARSER_CHECK_STMTVER2_RET(ctx, "action", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003476 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003477 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003478 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003479 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003480 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003481 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003482 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003483 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003484 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003485 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003486 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003487 break;
3488 default:
David Sedlákb3077192019-06-19 10:55:37 +02003489 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003490 return LY_EVALID;
3491 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003492 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003493
Michal Vasko7fbc8162018-09-17 10:35:16 +02003494 return ret;
3495}
3496
Michal Vaskoea5abea2018-09-18 13:10:54 +02003497/**
3498 * @brief Parse the yin-element statement.
3499 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003500 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003501 * @param[in,out] flags Flags to write to.
3502 * @param[in,out] exts Extension instances to add to.
3503 *
3504 * @return LY_ERR values.
3505 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003506static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003507parse_yinelement(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003508{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003509 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003510 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003511 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003512 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003513
3514 if (*flags & LYS_YINELEM_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02003515 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003516 return LY_EVALID;
3517 }
3518
3519 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003520 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003521
Radek Krejcif13b87b2020-12-01 22:02:17 +01003522 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003523 *flags |= LYS_YINELEM_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003524 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003525 *flags |= LYS_YINELEM_FALSE;
3526 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003527 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003528 free(buf);
3529 return LY_EVALID;
3530 }
3531 free(buf);
3532
Radek Krejci33090f92020-12-17 20:12:46 +01003533 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003534 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003535 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003536 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_YINELEM, 0, exts));
Michal Vaskod989ba02020-08-24 10:59:24 +02003537 LY_CHECK_RET(ret);
3538 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003539 default:
David Sedlákb3077192019-06-19 10:55:37 +02003540 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003541 return LY_EVALID;
3542 }
3543 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003544 return ret;
3545}
3546
Michal Vaskoea5abea2018-09-18 13:10:54 +02003547/**
David Sedlák2444f8f2019-07-09 11:02:47 +02003548 * @brief Parse the argument statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003549 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003550 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003551 * @param[in,out] argument Value to write to.
3552 * @param[in,out] flags Flags to write to.
3553 * @param[in,out] exts Extension instances to add to.
3554 *
3555 * @return LY_ERR values.
3556 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003557static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003558parse_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 +02003559{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003560 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003561 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003562 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003563 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003564
3565 if (*argument) {
David Sedlákb3077192019-06-19 10:55:37 +02003566 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003567 return LY_EVALID;
3568 }
3569
3570 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003571 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003572 INSERT_WORD_RET(ctx, buf, *argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003573
Radek Krejci33090f92020-12-17 20:12:46 +01003574 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003575 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003576 case LY_STMT_YIN_ELEMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003577 LY_CHECK_RET(parse_yinelement(ctx, flags, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003578 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003579 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003580 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_ARGUMENT, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003581 break;
3582 default:
David Sedlákb3077192019-06-19 10:55:37 +02003583 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003584 return LY_EVALID;
3585 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003586 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003587 return ret;
3588}
3589
Michal Vaskoea5abea2018-09-18 13:10:54 +02003590/**
3591 * @brief Parse the extension statement.
3592 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003593 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003594 * @param[in,out] extensions Extensions to add to.
3595 *
3596 * @return LY_ERR values.
3597 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003598static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003599parse_extension(struct lys_yang_parser_ctx *ctx, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003600{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003601 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003602 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003603 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003604 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003605 struct lysp_ext *ex;
3606
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003607 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003608
3609 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003610 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003611 INSERT_WORD_RET(ctx, buf, ex->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003612
Radek Krejci33090f92020-12-17 20:12:46 +01003613 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003614 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003615 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01003616 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &ex->dsc, Y_STR_ARG, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003617 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003618 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003619 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &ex->ref, Y_STR_ARG, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003620 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003621 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003622 LY_CHECK_RET(parse_status(ctx, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003623 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003624 case LY_STMT_ARGUMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003625 LY_CHECK_RET(parse_argument(ctx, &ex->argument, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003626 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003627 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003628 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003629 break;
3630 default:
David Sedlákb3077192019-06-19 10:55:37 +02003631 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003632 return LY_EVALID;
3633 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003634 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003635 return ret;
3636}
3637
Michal Vaskoea5abea2018-09-18 13:10:54 +02003638/**
3639 * @brief Parse the deviate statement.
3640 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003641 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003642 * @param[in,out] deviates Deviates to add to.
3643 *
3644 * @return LY_ERR values.
3645 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003646LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003647parse_deviate(struct lys_yang_parser_ctx *ctx, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003648{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003649 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003650 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003651 size_t word_len, dev_mod;
Radek Krejcid6b76452019-09-03 17:03:03 +02003652 enum ly_stmt kw;
David Sedlák60adc092019-08-06 15:57:02 +02003653 struct lysp_deviate *d;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003654 struct lysp_deviate_add *d_add = NULL;
3655 struct lysp_deviate_rpl *d_rpl = NULL;
3656 struct lysp_deviate_del *d_del = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003657 const char **d_units = NULL;
3658 struct lysp_qname **d_uniques = NULL, **d_dflts = NULL;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02003659 struct lysp_restr **d_musts = NULL;
3660 uint16_t *d_flags = 0;
3661 uint32_t *d_min = 0, *d_max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003662
3663 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003664 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003665
Radek Krejcif13b87b2020-12-01 22:02:17 +01003666 if ((word_len == ly_strlen_const("not-supported")) && !strncmp(word, "not-supported", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003667 dev_mod = LYS_DEV_NOT_SUPPORTED;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003668 } else if ((word_len == ly_strlen_const("add")) && !strncmp(word, "add", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003669 dev_mod = LYS_DEV_ADD;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003670 } else if ((word_len == ly_strlen_const("replace")) && !strncmp(word, "replace", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003671 dev_mod = LYS_DEV_REPLACE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003672 } else if ((word_len == ly_strlen_const("delete")) && !strncmp(word, "delete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003673 dev_mod = LYS_DEV_DELETE;
3674 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003675 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003676 free(buf);
3677 return LY_EVALID;
3678 }
3679 free(buf);
3680
3681 /* create structure */
3682 switch (dev_mod) {
3683 case LYS_DEV_NOT_SUPPORTED:
3684 d = calloc(1, sizeof *d);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003685 LY_CHECK_ERR_RET(!d, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003686 break;
3687 case LYS_DEV_ADD:
3688 d_add = calloc(1, sizeof *d_add);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003689 LY_CHECK_ERR_RET(!d_add, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003690 d = (struct lysp_deviate *)d_add;
3691 d_units = &d_add->units;
3692 d_uniques = &d_add->uniques;
3693 d_dflts = &d_add->dflts;
3694 d_musts = &d_add->musts;
3695 d_flags = &d_add->flags;
3696 d_min = &d_add->min;
3697 d_max = &d_add->max;
3698 break;
3699 case LYS_DEV_REPLACE:
3700 d_rpl = calloc(1, sizeof *d_rpl);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003701 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003702 d = (struct lysp_deviate *)d_rpl;
3703 d_units = &d_rpl->units;
3704 d_flags = &d_rpl->flags;
3705 d_min = &d_rpl->min;
3706 d_max = &d_rpl->max;
3707 break;
3708 case LYS_DEV_DELETE:
3709 d_del = calloc(1, sizeof *d_del);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003710 LY_CHECK_ERR_RET(!d_del, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003711 d = (struct lysp_deviate *)d_del;
3712 d_units = &d_del->units;
3713 d_uniques = &d_del->uniques;
3714 d_dflts = &d_del->dflts;
3715 d_musts = &d_del->musts;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003716 break;
3717 default:
3718 assert(0);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003719 LOGINT_RET(PARSER_CTX(ctx));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003720 }
3721 d->mod = dev_mod;
3722
3723 /* insert into siblings */
David Sedlák60adc092019-08-06 15:57:02 +02003724 LY_LIST_INSERT(deviates, d, next);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003725
Radek Krejci33090f92020-12-17 20:12:46 +01003726 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003727 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003728 case LY_STMT_CONFIG:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003729 switch (dev_mod) {
3730 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003731 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003732 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003733 return LY_EVALID;
3734 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003735 LY_CHECK_RET(parse_config(ctx, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003736 break;
3737 }
3738 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003739 case LY_STMT_DEFAULT:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003740 switch (dev_mod) {
3741 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003742 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003743 return LY_EVALID;
3744 case LYS_DEV_REPLACE:
Radek Krejci33090f92020-12-17 20:12:46 +01003745 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DEFAULT, 0, &d_rpl->dflt.str, Y_STR_ARG, &d->exts));
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003746 d_rpl->dflt.mod = ctx->parsed_mod;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003747 break;
3748 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003749 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_DEFAULT, d_dflts, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003750 break;
3751 }
3752 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003753 case LY_STMT_MANDATORY:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003754 switch (dev_mod) {
3755 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003756 case LYS_DEV_DELETE:
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 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003760 LY_CHECK_RET(parse_mandatory(ctx, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003761 break;
3762 }
3763 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003764 case LY_STMT_MAX_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003765 switch (dev_mod) {
3766 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003767 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003768 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003769 return LY_EVALID;
3770 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003771 LY_CHECK_RET(parse_maxelements(ctx, d_max, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003772 break;
3773 }
3774 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003775 case LY_STMT_MIN_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003776 switch (dev_mod) {
3777 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003778 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003779 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003780 return LY_EVALID;
3781 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003782 LY_CHECK_RET(parse_minelements(ctx, d_min, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003783 break;
3784 }
3785 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003786 case LY_STMT_MUST:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003787 switch (dev_mod) {
3788 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003789 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003790 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003791 return LY_EVALID;
3792 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003793 LY_CHECK_RET(parse_restrs(ctx, kw, d_musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003794 break;
3795 }
3796 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003797 case LY_STMT_TYPE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003798 switch (dev_mod) {
3799 case LYS_DEV_NOT_SUPPORTED:
3800 case LYS_DEV_ADD:
3801 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003802 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003803 return LY_EVALID;
3804 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003805 if (d_rpl->type) {
David Sedlákb3077192019-06-19 10:55:37 +02003806 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003807 return LY_EVALID;
3808 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003809 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003810 LY_CHECK_ERR_RET(!d_rpl->type, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01003811 LY_CHECK_RET(parse_type(ctx, d_rpl->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003812 break;
3813 }
3814 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003815 case LY_STMT_UNIQUE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003816 switch (dev_mod) {
3817 case LYS_DEV_NOT_SUPPORTED:
3818 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003819 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003820 return LY_EVALID;
3821 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003822 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_UNIQUE, d_uniques, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003823 break;
3824 }
3825 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003826 case LY_STMT_UNITS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003827 switch (dev_mod) {
3828 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003829 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003830 return LY_EVALID;
3831 default:
Radek Krejci33090f92020-12-17 20:12:46 +01003832 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_UNITS, 0, d_units, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003833 break;
3834 }
3835 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003836 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003837 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003838 break;
3839 default:
David Sedlákb3077192019-06-19 10:55:37 +02003840 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003841 return LY_EVALID;
3842 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003843 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003844 return ret;
3845}
3846
Michal Vaskoea5abea2018-09-18 13:10:54 +02003847/**
3848 * @brief Parse the deviation statement.
3849 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003850 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003851 * @param[in,out] deviations Deviations to add to.
3852 *
3853 * @return LY_ERR values.
3854 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003855LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003856parse_deviation(struct lys_yang_parser_ctx *ctx, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003857{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003858 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003859 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003860 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003861 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003862 struct lysp_deviation *dev;
3863
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003864 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003865
3866 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003867 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01003868 CHECK_NONEMPTY(ctx, word_len, "deviation");
Radek Krejci011e4aa2020-09-04 15:22:31 +02003869 INSERT_WORD_RET(ctx, buf, dev->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003870
Radek Krejci33090f92020-12-17 20:12:46 +01003871 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003872 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003873 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01003874 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &dev->dsc, Y_STR_ARG, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003875 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003876 case LY_STMT_DEVIATE:
Radek Krejci33090f92020-12-17 20:12:46 +01003877 LY_CHECK_RET(parse_deviate(ctx, &dev->deviates));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003878 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003879 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003880 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &dev->ref, Y_STR_ARG, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003881 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003882 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003883 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003884 break;
3885 default:
David Sedlákb3077192019-06-19 10:55:37 +02003886 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003887 return LY_EVALID;
3888 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003889 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003890 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01003891checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003892 /* mandatory substatements */
3893 if (!dev->deviates) {
David Sedlákb3077192019-06-19 10:55:37 +02003894 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003895 return LY_EVALID;
3896 }
3897
3898 return ret;
3899}
3900
Michal Vaskoea5abea2018-09-18 13:10:54 +02003901/**
3902 * @brief Parse the feature statement.
3903 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003904 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003905 * @param[in,out] features Features to add to.
3906 *
3907 * @return LY_ERR values.
3908 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003909LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003910parse_feature(struct lys_yang_parser_ctx *ctx, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003911{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003912 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003913 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003914 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003915 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003916 struct lysp_feature *feat;
3917
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003918 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003919
3920 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003921 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003922 INSERT_WORD_RET(ctx, buf, feat->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01003923
Radek Krejci33090f92020-12-17 20:12:46 +01003924 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003925 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003926 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01003927 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &feat->dsc, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003928 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003929 case LY_STMT_IF_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01003930 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &feat->iffeatures, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003931 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003932 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003933 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &feat->ref, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003934 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003935 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003936 LY_CHECK_RET(parse_status(ctx, &feat->flags, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003937 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003938 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003939 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003940 break;
3941 default:
David Sedlákb3077192019-06-19 10:55:37 +02003942 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003943 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003944 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003945 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003946 return ret;
3947}
3948
Michal Vaskoea5abea2018-09-18 13:10:54 +02003949/**
3950 * @brief Parse the identity statement.
3951 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003952 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003953 * @param[in,out] identities Identities to add to.
3954 *
3955 * @return LY_ERR values.
3956 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003957LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01003958parse_identity(struct lys_yang_parser_ctx *ctx, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003959{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003960 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003961 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003962 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003963 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003964 struct lysp_ident *ident;
3965
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003966 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003967
3968 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003969 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02003970 INSERT_WORD_RET(ctx, buf, ident->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01003971
Radek Krejci33090f92020-12-17 20:12:46 +01003972 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003973 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003974 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01003975 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &ident->dsc, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003976 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003977 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02003978 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "identity");
Radek Krejci33090f92020-12-17 20:12:46 +01003979 LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &ident->iffeatures, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003980 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003981 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003982 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &ident->ref, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003983 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003984 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003985 LY_CHECK_RET(parse_status(ctx, &ident->flags, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003986 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003987 case LY_STMT_BASE:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003988 if (ident->bases && (ctx->parsed_mod->version < LYS_VERSION_1_1)) {
David Sedlákb3077192019-06-19 10:55:37 +02003989 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 +01003990 return LY_EVALID;
3991 }
Radek Krejci33090f92020-12-17 20:12:46 +01003992 LY_CHECK_RET(parse_text_fields(ctx, LYEXT_SUBSTMT_BASE, &ident->bases, Y_PREF_IDENTIF_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003993 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003994 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01003995 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003996 break;
3997 default:
David Sedlákb3077192019-06-19 10:55:37 +02003998 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003999 return LY_EVALID;
4000 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004001 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004002 return ret;
4003}
4004
Michal Vaskoea5abea2018-09-18 13:10:54 +02004005/**
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004006 * @brief Parse module substatements.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004007 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004008 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004009 * @param[in,out] mod Module to write to.
4010 *
4011 * @return LY_ERR values.
4012 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004013LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004014parse_module(struct lys_yang_parser_ctx *ctx, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004015{
4016 LY_ERR ret = 0;
4017 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004018 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004019 enum ly_stmt kw, prev_kw = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004020 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004021 struct lysp_submodule *dup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004022
Michal Vaskoc3781c32020-10-06 14:04:08 +02004023 mod->is_submod = 0;
4024
4025 /* module name */
Radek Krejci33090f92020-12-17 20:12:46 +01004026 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004027 INSERT_WORD_RET(ctx, buf, mod->mod->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004028
Radek Krejci33090f92020-12-17 20:12:46 +01004029 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004030
Radek Krejcie3846472018-10-15 15:24:51 +02004031#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004032 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 +02004033
Michal Vasko7fbc8162018-09-17 10:35:16 +02004034 switch (kw) {
4035 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004036 case LY_STMT_NAMESPACE:
4037 case LY_STMT_PREFIX:
Radek Krejcie3846472018-10-15 15:24:51 +02004038 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4039 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004040 case LY_STMT_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004041 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004042 break;
4043 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004044 case LY_STMT_INCLUDE:
4045 case LY_STMT_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004046 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004047 break;
4048 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004049 case LY_STMT_ORGANIZATION:
4050 case LY_STMT_CONTACT:
4051 case LY_STMT_DESCRIPTION:
4052 case LY_STMT_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004053 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004054 break;
4055
4056 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004057 case LY_STMT_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004058 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004059 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004060 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004061 case LY_STMT_ANYDATA:
4062 case LY_STMT_ANYXML:
4063 case LY_STMT_AUGMENT:
4064 case LY_STMT_CHOICE:
4065 case LY_STMT_CONTAINER:
4066 case LY_STMT_DEVIATION:
4067 case LY_STMT_EXTENSION:
4068 case LY_STMT_FEATURE:
4069 case LY_STMT_GROUPING:
4070 case LY_STMT_IDENTITY:
4071 case LY_STMT_LEAF:
4072 case LY_STMT_LEAF_LIST:
4073 case LY_STMT_LIST:
4074 case LY_STMT_NOTIFICATION:
4075 case LY_STMT_RPC:
4076 case LY_STMT_TYPEDEF:
4077 case LY_STMT_USES:
4078 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004079 mod_stmt = Y_MOD_BODY;
4080 break;
4081 default:
4082 /* error handled in the next switch */
4083 break;
4084 }
Radek Krejcie3846472018-10-15 15:24:51 +02004085#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004086
Radek Krejcie3846472018-10-15 15:24:51 +02004087 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004088 switch (kw) {
4089 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004090 case LY_STMT_YANG_VERSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004091 LY_CHECK_RET(parse_yangversion(ctx, &mod->version, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004092 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004093 case LY_STMT_NAMESPACE:
Radek Krejci33090f92020-12-17 20:12:46 +01004094 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_NAMESPACE, 0, &mod->mod->ns, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004095 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004096 case LY_STMT_PREFIX:
Radek Krejci33090f92020-12-17 20:12:46 +01004097 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_PREFIX, 0, &mod->mod->prefix, Y_IDENTIF_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004098 break;
4099
4100 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004101 case LY_STMT_INCLUDE:
Radek Krejci33090f92020-12-17 20:12:46 +01004102 LY_CHECK_RET(parse_include(ctx, mod->mod->name, &mod->includes));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004103 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004104 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004105 LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, &mod->imports));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004106 break;
4107
4108 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004109 case LY_STMT_ORGANIZATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004110 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_ORGANIZATION, 0, &mod->mod->org, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004111 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004112 case LY_STMT_CONTACT:
Radek Krejci33090f92020-12-17 20:12:46 +01004113 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_CONTACT, 0, &mod->mod->contact, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004114 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004115 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01004116 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &mod->mod->dsc, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004117 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004118 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01004119 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &mod->mod->ref, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004120 break;
4121
4122 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004123 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004124 LY_CHECK_RET(parse_revision(ctx, &mod->revs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004125 break;
4126
4127 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004128 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004129 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "module");
Radek Krejci0f969882020-08-21 16:56:47 +02004130 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004131 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004132 LY_CHECK_RET(parse_any(ctx, kw, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004133 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004134 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004135 LY_CHECK_RET(parse_choice(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004136 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004137 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004138 LY_CHECK_RET(parse_container(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004139 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004140 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004141 LY_CHECK_RET(parse_leaf(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004142 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004143 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004144 LY_CHECK_RET(parse_leaflist(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004145 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004146 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004147 LY_CHECK_RET(parse_list(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004148 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004149 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004150 LY_CHECK_RET(parse_uses(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004151 break;
4152
Radek Krejcid6b76452019-09-03 17:03:03 +02004153 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004154 LY_CHECK_RET(parse_augment(ctx, NULL, &mod->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004155 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004156 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004157 LY_CHECK_RET(parse_deviation(ctx, &mod->deviations));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004158 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004159 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004160 LY_CHECK_RET(parse_extension(ctx, &mod->extensions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004161 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004162 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004163 LY_CHECK_RET(parse_feature(ctx, &mod->features));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004164 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004165 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004166 LY_CHECK_RET(parse_grouping(ctx, NULL, &mod->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004167 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004168 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004169 LY_CHECK_RET(parse_identity(ctx, &mod->identities));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004170 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004171 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004172 LY_CHECK_RET(parse_notif(ctx, NULL, &mod->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004173 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004174 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004175 LY_CHECK_RET(parse_action(ctx, NULL, &mod->rpcs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004176 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004177 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004178 LY_CHECK_RET(parse_typedef(ctx, NULL, &mod->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004179 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004180 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01004181 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004182 break;
4183
4184 default:
David Sedlákb3077192019-06-19 10:55:37 +02004185 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004186 return LY_EVALID;
4187 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004188 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004189 LY_CHECK_RET(ret);
Radek Krejcie86bf772018-12-14 11:39:53 +01004190
Radek Krejci6d6556c2018-11-08 09:37:45 +01004191checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004192 /* mandatory substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004193 if (!mod->mod->ns) {
David Sedlákb3077192019-06-19 10:55:37 +02004194 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004195 return LY_EVALID;
4196 } else if (!mod->mod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004197 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004198 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004199 }
4200
Radek Krejcie9e987e2018-10-31 12:50:27 +01004201 /* submodules share the namespace with the module names, so there must not be
4202 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004203 dup = ly_ctx_get_submodule(PARSER_CTX(ctx), NULL, mod->mod->name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004204 if (dup) {
Radek Krejci854e1552020-12-21 15:05:23 +01004205 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004206 return LY_EVALID;
4207 }
4208
4209 return ret;
4210}
4211
4212/**
4213 * @brief Parse submodule substatements.
4214 *
4215 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004216 * @param[out] submod Parsed submodule structure.
4217 *
4218 * @return LY_ERR values.
4219 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004220LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004221parse_submodule(struct lys_yang_parser_ctx *ctx, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004222{
4223 LY_ERR ret = 0;
4224 char *buf, *word;
4225 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004226 enum ly_stmt kw, prev_kw = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004227 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
4228 struct lysp_submodule *dup;
4229
Michal Vaskoc3781c32020-10-06 14:04:08 +02004230 submod->is_submod = 1;
4231
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004232 /* submodule name */
Radek Krejci33090f92020-12-17 20:12:46 +01004233 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci011e4aa2020-09-04 15:22:31 +02004234 INSERT_WORD_RET(ctx, buf, submod->name, word, word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004235
Radek Krejci33090f92020-12-17 20:12:46 +01004236 YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004237
4238#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004239 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 +01004240
4241 switch (kw) {
4242 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004243 case LY_STMT_BELONGS_TO:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004244 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4245 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004246 case LY_STMT_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004247 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4248 break;
4249 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004250 case LY_STMT_INCLUDE:
4251 case LY_STMT_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004252 CHECK_ORDER(Y_MOD_LINKAGE);
4253 break;
4254 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004255 case LY_STMT_ORGANIZATION:
4256 case LY_STMT_CONTACT:
4257 case LY_STMT_DESCRIPTION:
4258 case LY_STMT_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004259 CHECK_ORDER(Y_MOD_META);
4260 break;
4261
4262 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004263 case LY_STMT_REVISION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004264 CHECK_ORDER(Y_MOD_REVISION);
4265 break;
4266 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004267 case LY_STMT_ANYDATA:
4268 case LY_STMT_ANYXML:
4269 case LY_STMT_AUGMENT:
4270 case LY_STMT_CHOICE:
4271 case LY_STMT_CONTAINER:
4272 case LY_STMT_DEVIATION:
4273 case LY_STMT_EXTENSION:
4274 case LY_STMT_FEATURE:
4275 case LY_STMT_GROUPING:
4276 case LY_STMT_IDENTITY:
4277 case LY_STMT_LEAF:
4278 case LY_STMT_LEAF_LIST:
4279 case LY_STMT_LIST:
4280 case LY_STMT_NOTIFICATION:
4281 case LY_STMT_RPC:
4282 case LY_STMT_TYPEDEF:
4283 case LY_STMT_USES:
4284 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004285 mod_stmt = Y_MOD_BODY;
4286 break;
4287 default:
4288 /* error handled in the next switch */
4289 break;
4290 }
4291#undef CHECK_ORDER
4292
4293 prev_kw = kw;
4294 switch (kw) {
4295 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004296 case LY_STMT_YANG_VERSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004297 LY_CHECK_RET(parse_yangversion(ctx, &submod->version, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004298 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004299 case LY_STMT_BELONGS_TO:
Radek Krejci33090f92020-12-17 20:12:46 +01004300 LY_CHECK_RET(parse_belongsto(ctx, &submod->prefix, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004301 break;
4302
4303 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004304 case LY_STMT_INCLUDE:
Radek Krejcieeee95c2021-01-19 10:57:22 +01004305 if (submod->version == LYS_VERSION_1_1) {
4306 LOGWRN(PARSER_CTX(ctx), "YANG version 1.1 expects all includes in main module, includes in submodules (%s) are not necessary.",
4307 submod->name);
4308 }
Radek Krejci33090f92020-12-17 20:12:46 +01004309 LY_CHECK_RET(parse_include(ctx, submod->name, &submod->includes));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004310 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004311 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004312 LY_CHECK_RET(parse_import(ctx, submod->prefix, &submod->imports));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004313 break;
4314
4315 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004316 case LY_STMT_ORGANIZATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004317 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_ORGANIZATION, 0, &submod->org, Y_STR_ARG, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004318 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004319 case LY_STMT_CONTACT:
Radek Krejci33090f92020-12-17 20:12:46 +01004320 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_CONTACT, 0, &submod->contact, Y_STR_ARG, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004321 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004322 case LY_STMT_DESCRIPTION:
Radek Krejci33090f92020-12-17 20:12:46 +01004323 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &submod->dsc, Y_STR_ARG, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004324 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004325 case LY_STMT_REFERENCE:
Radek Krejci33090f92020-12-17 20:12:46 +01004326 LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &submod->ref, Y_STR_ARG, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004327 break;
4328
4329 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004330 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004331 LY_CHECK_RET(parse_revision(ctx, &submod->revs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004332 break;
4333
4334 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004335 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004336 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "submodule");
Radek Krejci0f969882020-08-21 16:56:47 +02004337 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004338 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004339 LY_CHECK_RET(parse_any(ctx, kw, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004340 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004341 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004342 LY_CHECK_RET(parse_choice(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004343 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004344 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004345 LY_CHECK_RET(parse_container(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004346 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004347 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004348 LY_CHECK_RET(parse_leaf(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004349 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004350 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004351 LY_CHECK_RET(parse_leaflist(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004352 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004353 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004354 LY_CHECK_RET(parse_list(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004355 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004356 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004357 LY_CHECK_RET(parse_uses(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004358 break;
4359
Radek Krejcid6b76452019-09-03 17:03:03 +02004360 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004361 LY_CHECK_RET(parse_augment(ctx, NULL, &submod->augments));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004362 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004363 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004364 LY_CHECK_RET(parse_deviation(ctx, &submod->deviations));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004365 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004366 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004367 LY_CHECK_RET(parse_extension(ctx, &submod->extensions));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004368 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004369 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004370 LY_CHECK_RET(parse_feature(ctx, &submod->features));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004371 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004372 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004373 LY_CHECK_RET(parse_grouping(ctx, NULL, &submod->groupings));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004374 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004375 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004376 LY_CHECK_RET(parse_identity(ctx, &submod->identities));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004377 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004378 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004379 LY_CHECK_RET(parse_notif(ctx, NULL, &submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004380 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004381 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004382 LY_CHECK_RET(parse_action(ctx, NULL, &submod->rpcs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004383 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004384 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004385 LY_CHECK_RET(parse_typedef(ctx, NULL, &submod->typedefs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004386 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004387 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci33090f92020-12-17 20:12:46 +01004388 LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004389 break;
4390
4391 default:
David Sedlákb3077192019-06-19 10:55:37 +02004392 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004393 return LY_EVALID;
4394 }
4395 }
4396 LY_CHECK_RET(ret);
4397
4398checks:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004399 /* mandatory substatements */
Michal Vaskoc3781c32020-10-06 14:04:08 +02004400 if (!submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004401 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004402 return LY_EVALID;
4403 }
4404
4405 /* submodules share the namespace with the module names, so there must not be
4406 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004407 dup = ly_ctx_get_submodule(PARSER_CTX(ctx), NULL, submod->name, NULL);
Michal Vaskoc3781c32020-10-06 14:04:08 +02004408 /* main modules may have different revisions */
4409 if (dup && strcmp(dup->mod->name, submod->mod->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01004410 LOGVAL_PARSER(ctx, LY_VCODE_NAME_COL, "submodules", dup->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +01004411 return LY_EVALID;
4412 }
4413
Michal Vasko7fbc8162018-09-17 10:35:16 +02004414 return ret;
4415}
4416
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004417/**
4418 * @brief Skip any redundant characters, namely whitespaces and comments.
4419 *
4420 * @param[in] ctx Yang parser context.
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004421 * @return LY_SUCCESS on success.
4422 * @return LY_EVALID on invalid comment.
4423 */
4424static LY_ERR
Radek Krejci33090f92020-12-17 20:12:46 +01004425skip_redundant_chars(struct lys_yang_parser_ctx *ctx)
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004426{
4427 /* read some trailing spaces, new lines, or comments */
Radek Krejci33090f92020-12-17 20:12:46 +01004428 while (ctx->in->current[0]) {
4429 if (!strncmp(ctx->in->current, "//", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004430 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004431 ly_in_skip(ctx->in, 2);
4432 LY_CHECK_RET(skip_comment(ctx, 1));
4433 } else if (!strncmp(ctx->in->current, "/*", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004434 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004435 ly_in_skip(ctx->in, 2);
4436 LY_CHECK_RET(skip_comment(ctx, 2));
4437 } else if (isspace(ctx->in->current[0])) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004438 /* whitespace */
Radek Krejcidd713ce2021-01-04 23:12:12 +01004439 if (ctx->in->current[0] == '\n') {
4440 LY_IN_NEW_LINE(ctx->in);
4441 }
Radek Krejci33090f92020-12-17 20:12:46 +01004442 ly_in_skip(ctx->in, 1);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004443 } else {
4444 break;
4445 }
4446 }
4447
4448 return LY_SUCCESS;
4449}
4450
Radek Krejcid4557c62018-09-17 11:42:09 +02004451LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01004452yang_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 +02004453 struct ly_in *in, struct lysp_submodule **submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004454{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004455 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004456 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004457 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004458 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004459 struct lysp_submodule *mod_p = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004460
David Sedlák1b623122019-08-05 15:27:49 +02004461 /* create context */
4462 *context = calloc(1, sizeof **context);
4463 LY_CHECK_ERR_RET(!(*context), LOGMEM(ly_ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004464 (*context)->format = LYS_IN_YANG;
Michal Vasko405cc9e2020-12-01 12:01:27 +01004465 (*context)->unres = main_ctx->unres;
Radek Krejci33090f92020-12-17 20:12:46 +01004466 (*context)->in = in;
David Sedlák1b623122019-08-05 15:27:49 +02004467
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004468 mod_p = calloc(1, sizeof *mod_p);
4469 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ly_ctx); ret = LY_EMEM, cleanup);
4470 mod_p->mod = main_ctx->parsed_mod->mod;
4471 mod_p->parsing = 1;
4472 (*context)->parsed_mod = (struct lysp_module *)mod_p;
4473
Radek Krejciddace2c2021-01-08 11:30:56 +01004474 LOG_LOCINIT(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004475
David Sedlák1b623122019-08-05 15:27:49 +02004476 /* map the typedefs and groupings list from main context to the submodule's context */
4477 memcpy(&(*context)->tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
4478 memcpy(&(*context)->grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes);
4479
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004480 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004481 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004482 LY_CHECK_GOTO(ret, cleanup);
4483
Michal Vasko7fbc8162018-09-17 10:35:16 +02004484 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004485 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004486 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004487
Radek Krejcid6b76452019-09-03 17:03:03 +02004488 if (kw == LY_STMT_MODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004489 LOGERR(ly_ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004490 ret = LY_EINVAL;
4491 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004492 } else if (kw != LY_STMT_SUBMODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004493 LOGVAL_PARSER(*context, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004494 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004495 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004496 }
4497
Michal Vasko7fbc8162018-09-17 10:35:16 +02004498 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004499 ret = parse_submodule(*context, mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004500 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004501
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004502 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004503 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004504 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004505 if (in->current[0]) {
4506 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_SUBMOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004507 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004508 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004509 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004510
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004511 mod_p->parsing = 0;
4512 *submod = mod_p;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004513
Radek Krejcibbe09a92018-11-08 09:36:54 +01004514cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01004515 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004516 if (ret) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004517 lysp_module_free((struct lysp_module *)mod_p);
Michal Vaskob36053d2020-03-26 15:49:30 +01004518 yang_parser_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004519 *context = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004520 }
4521
4522 return ret;
4523}
4524
4525LY_ERR
Michal Vasko405cc9e2020-12-01 12:01:27 +01004526yang_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 +01004527{
4528 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004529 char *word;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004530 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004531 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004532 struct lysp_module *mod_p = NULL;
4533
David Sedlák1b623122019-08-05 15:27:49 +02004534 /* create context */
4535 *context = calloc(1, sizeof **context);
4536 LY_CHECK_ERR_RET(!(*context), LOGMEM(mod->ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004537 (*context)->format = LYS_IN_YANG;
Michal Vasko405cc9e2020-12-01 12:01:27 +01004538 (*context)->unres = unres;
Radek Krejci33090f92020-12-17 20:12:46 +01004539 (*context)->in = in;
David Sedlák1b623122019-08-05 15:27:49 +02004540
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004541 mod_p = calloc(1, sizeof *mod_p);
4542 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(mod->ctx), cleanup);
4543 mod_p->mod = mod;
4544 mod_p->parsing = 1;
4545 (*context)->parsed_mod = mod_p;
4546
Radek Krejciddace2c2021-01-08 11:30:56 +01004547 LOG_LOCINIT(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004548
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004549 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004550 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004551 LY_CHECK_GOTO(ret, cleanup);
4552
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004553 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004554 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004555 LY_CHECK_GOTO(ret, cleanup);
4556
Radek Krejcid6b76452019-09-03 17:03:03 +02004557 if (kw == LY_STMT_SUBMODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004558 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 +01004559 ret = LY_EINVAL;
4560 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004561 } else if (kw != LY_STMT_MODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004562 LOGVAL_PARSER((*context), LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004563 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004564 goto cleanup;
4565 }
4566
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004567 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004568 ret = parse_module(*context, mod_p);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004569 LY_CHECK_GOTO(ret, cleanup);
4570
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004571 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004572 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004573 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004574 if (in->current[0]) {
4575 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_MOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004576 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004577 goto cleanup;
4578 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004579
4580 mod_p->parsing = 0;
4581 mod->parsed = mod_p;
4582
4583cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01004584 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004585 if (ret) {
4586 lysp_module_free(mod_p);
Michal Vaskob36053d2020-03-26 15:49:30 +01004587 yang_parser_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004588 *context = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004589 }
4590
Michal Vasko7fbc8162018-09-17 10:35:16 +02004591 return ret;
4592}