blob: 1e201b00d081c4eae552975b2c012f1d00e5d9e0 [file] [log] [blame]
David Sedlákf824ad52018-10-14 23:58:15 +02001/**
2 * @file parser_yin.c
3 * @author David Sedlák <xsedla1d@stud.fit.vutbr.cz>
Michal Vaskoc636ea42022-09-16 10:20:31 +02004 * @author Michal Vasko <mvasko@cesnet.cz>
David Sedlák3b4db242018-10-19 16:11:01 +02005 * @brief YIN parser.
6 *
Michal Vaskoc636ea42022-09-16 10:20:31 +02007 * Copyright (c) 2015 - 2022 CESNET, z.s.p.o.
David Sedlák3b4db242018-10-19 16:11:01 +02008 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
David Sedlákf824ad52018-10-14 23:58:15 +020014 */
Michal Vaskofc2cd072021-02-24 13:17:17 +010015#define _GNU_SOURCE
16
David Sedlák3ffbc522019-07-02 17:49:28 +020017#include <assert.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020018#include <ctype.h>
19#include <errno.h>
20#include <stdarg.h>
21#include <stdbool.h>
Radek Krejci47fab892020-11-05 17:02:41 +010022#include <stdint.h>
David Sedlák3b4db242018-10-19 16:11:01 +020023#include <stdio.h>
24#include <stdlib.h>
David Sedlák872c7b42018-10-26 13:15:20 +020025#include <string.h>
David Sedlákf824ad52018-10-14 23:58:15 +020026
Radek Krejci535ea9f2020-05-29 16:01:05 +020027#include "common.h"
Michal Vaskofc2cd072021-02-24 13:17:17 +010028#include "compat.h"
David Sedlákf824ad52018-10-14 23:58:15 +020029#include "context.h"
David Sedlákecf5eb82019-06-03 14:12:44 +020030#include "dict.h"
Radek Krejci47fab892020-11-05 17:02:41 +010031#include "in.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020032#include "in_internal.h"
Radek Krejci47fab892020-11-05 17:02:41 +010033#include "log.h"
Michal Vasko63f3d842020-07-08 10:10:14 +020034#include "parser_internal.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020035#include "parser_schema.h"
Michal Vasko69730152020-10-09 16:30:07 +020036#include "path.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020037#include "set.h"
David Sedlákecf5eb82019-06-03 14:12:44 +020038#include "tree.h"
Michal Vaskofc2cd072021-02-24 13:17:17 +010039#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010040#include "tree_edit.h"
David Sedlákecf5eb82019-06-03 14:12:44 +020041#include "tree_schema.h"
David Sedlák3b4db242018-10-19 16:11:01 +020042#include "tree_schema_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020043#include "xml.h"
David Sedlák00250342019-06-21 14:19:39 +020044
Radek Krejci77114102021-03-10 15:21:57 +010045struct lys_glob_unres;
46
David Sedlák2b214ac2019-06-06 16:11:03 +020047/**
48 * @brief check if given string is URI of yin namespace.
David Sedlák8985a142019-07-31 16:43:06 +020049 *
David Sedlák2b214ac2019-06-06 16:11:03 +020050 * @param ns Namespace URI to check.
51 *
52 * @return true if ns equals YIN_NS_URI false otherwise.
53 */
54#define IS_YIN_NS(ns) (strcmp(ns, YIN_NS_URI) == 0)
55
Michal Vaskoafac7822020-10-20 14:22:26 +020056enum yin_argument {
57 YIN_ARG_UNKNOWN = 0, /**< parsed argument can not be matched with any supported yin argument keyword */
58 YIN_ARG_NAME, /**< argument name */
59 YIN_ARG_TARGET_NODE, /**< argument target-node */
60 YIN_ARG_MODULE, /**< argument module */
61 YIN_ARG_VALUE, /**< argument value */
62 YIN_ARG_TEXT, /**< argument text */
63 YIN_ARG_CONDITION, /**< argument condition */
64 YIN_ARG_URI, /**< argument uri */
65 YIN_ARG_DATE, /**< argument data */
66 YIN_ARG_TAG, /**< argument tag */
67 YIN_ARG_NONE /**< empty (special value) */
68};
69
Michal Vasko22df3f02020-08-24 13:29:22 +020070const char * const yin_attr_list[] = {
David Sedlákf6251182019-06-06 10:22:13 +020071 [YIN_ARG_NAME] = "name",
72 [YIN_ARG_TARGET_NODE] = "target-node",
73 [YIN_ARG_MODULE] = "module",
74 [YIN_ARG_VALUE] = "value",
75 [YIN_ARG_TEXT] = "text",
76 [YIN_ARG_CONDITION] = "condition",
77 [YIN_ARG_URI] = "uri",
78 [YIN_ARG_DATE] = "date",
79 [YIN_ARG_TAG] = "tag",
David Sedlák071f7662019-09-12 02:02:51 +020080 [YIN_ARG_NONE] = "none",
David Sedlákf6251182019-06-06 10:22:13 +020081};
82
Michal Vaskoafac7822020-10-20 14:22:26 +020083#define yin_attr2str(STMT) yin_attr_list[STMT]
84
85#define VALID_VALS1 " Only valid value is \"%s\"."
86#define VALID_VALS2 " Valid values are \"%s\" and \"%s\"."
87#define VALID_VALS3 " Valid values are \"%s\", \"%s\" and \"%s\"."
88#define VALID_VALS4 " Valid values are \"%s\", \"%s\", \"%s\" and \"%s\"."
89
90/* shortcut to determin if keyword can in general be subelement of deviation regardles of it's type */
91#define isdevsub(kw) (kw == LY_STMT_CONFIG || kw == LY_STMT_DEFAULT || kw == LY_STMT_MANDATORY || \
92 kw == LY_STMT_MAX_ELEMENTS || kw == LY_STMT_MIN_ELEMENTS || \
93 kw == LY_STMT_MUST || kw == LY_STMT_TYPE || kw == LY_STMT_UNIQUE || \
94 kw == LY_STMT_UNITS || kw == LY_STMT_EXTENSION_INSTANCE)
95
96/* flags to set constraints of subelements */
97#define YIN_SUBELEM_MANDATORY 0x01 /**< is set when subelement is mandatory */
98#define YIN_SUBELEM_UNIQUE 0x02 /**< is set when subelement is unique */
99#define YIN_SUBELEM_FIRST 0x04 /**< is set when subelement is actually yang argument mapped to yin element */
100#define YIN_SUBELEM_VER2 0x08 /**< subelemnt is allowed only in modules with version at least 2 (YANG 1.1) */
101
102#define YIN_SUBELEM_PARSED 0x80 /**< is set during parsing when given subelement is encountered for the first
103 time to simply check validity of given constraints */
104
105struct yin_subelement {
106 enum ly_stmt type; /**< type of keyword */
107 void *dest; /**< meta infromation passed to responsible function (mostly information about where parsed subelement should be stored) */
108 uint16_t flags; /**< describes constraints of subelement can be set to YIN_SUBELEM_MANDATORY, YIN_SUBELEM_UNIQUE, YIN_SUBELEM_FIRST, YIN_SUBELEM_VER2, and YIN_SUBELEM_DEFAULT_TEXT */
109};
110
111/* Meta information passed to yin_parse_argument function,
112 holds information about where content of argument element will be stored. */
113struct yin_argument_meta {
114 uint16_t *flags; /**< Argument flags */
115 const char **argument; /**< Argument value */
116};
117
118/**
119 * @brief Meta information passed to functions working with tree_schema,
120 * that require additional information about parent node.
121 */
122struct tree_node_meta {
123 struct lysp_node *parent; /**< parent node */
124 struct lysp_node **nodes; /**< linked list of siblings */
125};
126
127/**
128 * @brief Meta information passed to yin_parse_import function.
129 */
130struct import_meta {
131 const char *prefix; /**< module prefix. */
132 struct lysp_import **imports; /**< imports to add to. */
133};
134
135/**
136 * @brief Meta information passed to yin_parse_include function.
137 */
138struct include_meta {
139 const char *name; /**< Module/submodule name. */
140 struct lysp_include **includes; /**< [Sized array](@ref sizedarrays) of parsed includes to add to. */
141};
142
143/**
144 * @brief Meta information passed to yin_parse_inout function.
145 */
146struct inout_meta {
147 struct lysp_node *parent; /**< Parent node. */
Radek Krejci2a9fc652021-01-22 17:44:34 +0100148 struct lysp_node_action_inout *inout_p; /**< inout_p Input/output pointer to write to. */
Michal Vaskoafac7822020-10-20 14:22:26 +0200149};
150
151/**
152 * @brief Meta information passed to yin_parse_minmax function.
153 */
154struct minmax_dev_meta {
155 uint32_t *lim; /**< min/max value to write to. */
156 uint16_t *flags; /**< min/max flags to write to. */
157 struct lysp_ext_instance **exts; /**< extension instances to add to. */
158};
159
160LY_ERR yin_parse_content(struct lys_yin_parser_ctx *ctx, struct yin_subelement *subelem_info, size_t subelem_info_size,
161 enum ly_stmt current_element, const char **text_content, struct lysp_ext_instance **exts);
162
163/**
164 * @brief Match yang keyword from yin data.
165 *
166 * @param[in,out] ctx Yin parser context for logging and to store current state.
167 * @param[in] name Start of keyword name
168 * @param[in] name_len Lenght of keyword name.
169 * @param[in] prefix Start of keyword prefix.
170 * @param[in] prefix_len Lenght of prefix.
171 * @param[in] parrent Identification of parrent element, use LY_STMT_NONE for elements without parrent.
172 *
173 * @return yang_keyword values.
174 */
Radek Krejcid6b76452019-09-03 17:03:03 +0200175enum ly_stmt
Michal Vaskob36053d2020-03-26 15:49:30 +0100176yin_match_keyword(struct lys_yin_parser_ctx *ctx, const char *name, size_t name_len,
Radek Krejci0f969882020-08-21 16:56:47 +0200177 const char *prefix, size_t prefix_len, enum ly_stmt parent)
David Sedlák1bccdfa2019-06-17 15:55:27 +0200178{
David Sedlák8f7a1172019-06-20 14:42:18 +0200179 const char *start = NULL;
Radek Krejcid6b76452019-09-03 17:03:03 +0200180 enum ly_stmt kw = LY_STMT_NONE;
David Sedlák8f7a1172019-06-20 14:42:18 +0200181 const struct lyxml_ns *ns = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +0200182 struct ly_in *in;
David Sedlák8f7a1172019-06-20 14:42:18 +0200183
Michal Vasko69730152020-10-09 16:30:07 +0200184 if (!name || (name_len == 0)) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200185 return LY_STMT_NONE;
David Sedlák1bccdfa2019-06-17 15:55:27 +0200186 }
187
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200188 ns = lyxml_ns_get(&ctx->xmlctx->ns, prefix, prefix_len);
David Sedlák8f7a1172019-06-20 14:42:18 +0200189 if (ns) {
190 if (!IS_YIN_NS(ns->uri)) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200191 return LY_STMT_EXTENSION_INSTANCE;
David Sedlák8f7a1172019-06-20 14:42:18 +0200192 }
193 } else {
194 /* elements without namespace are automatically unknown */
Radek Krejcid6b76452019-09-03 17:03:03 +0200195 return LY_STMT_NONE;
David Sedlák8f7a1172019-06-20 14:42:18 +0200196 }
David Sedlák1bccdfa2019-06-17 15:55:27 +0200197
Michal Vasko63f3d842020-07-08 10:10:14 +0200198 LY_CHECK_RET(ly_in_new_memory(name, &in), LY_STMT_NONE);
199 start = in->current;
Radek Krejcid54412f2020-12-17 20:25:35 +0100200 kw = lysp_match_kw(in, NULL);
Michal Vasko63f3d842020-07-08 10:10:14 +0200201 name = in->current;
202 ly_in_free(in, 0);
David Sedlák8f7a1172019-06-20 14:42:18 +0200203
204 if (name - start == (long int)name_len) {
David Sedlákc1771b12019-07-10 15:55:46 +0200205 /* this is done because of collision in yang statement value and yang argument mapped to yin element value */
Michal Vasko69730152020-10-09 16:30:07 +0200206 if ((kw == LY_STMT_VALUE) && (parent == LY_STMT_ERROR_MESSAGE)) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200207 return LY_STMT_ARG_VALUE;
David Sedlákc1771b12019-07-10 15:55:46 +0200208 }
David Sedlák1bccdfa2019-06-17 15:55:27 +0200209 return kw;
210 } else {
David Sedlák3ffbc522019-07-02 17:49:28 +0200211 if (strncmp(start, "text", name_len) == 0) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200212 return LY_STMT_ARG_TEXT;
David Sedlák3ffbc522019-07-02 17:49:28 +0200213 } else {
Radek Krejcid6b76452019-09-03 17:03:03 +0200214 return LY_STMT_NONE;
David Sedlák3ffbc522019-07-02 17:49:28 +0200215 }
David Sedlák1bccdfa2019-06-17 15:55:27 +0200216 }
217}
218
Michal Vaskoafac7822020-10-20 14:22:26 +0200219/**
220 * @brief Match argument name.
221 *
222 * @param[in] name String representing name.
223 * @param[in] len Lenght of the name.
224 *
225 * @return yin_argument values.
226 */
David Sedlákc5b20842019-08-13 10:18:31 +0200227enum yin_argument
David Sedlák060b00e2019-06-19 11:12:06 +0200228yin_match_argument_name(const char *name, size_t len)
David Sedlák3b4db242018-10-19 16:11:01 +0200229{
David Sedlákc5b20842019-08-13 10:18:31 +0200230 enum yin_argument arg = YIN_ARG_UNKNOWN;
David Sedlák872c7b42018-10-26 13:15:20 +0200231 size_t already_read = 0;
Michal Vasko69730152020-10-09 16:30:07 +0200232
David Sedlák7ff55a92019-06-17 11:11:41 +0200233 LY_CHECK_RET(len == 0, YIN_ARG_NONE);
David Sedlák3b4db242018-10-19 16:11:01 +0200234
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200235#define READ_INC(LEN) already_read += LEN
236#define ARG_SET(STMT) arg=STMT
237#define ARG_CHECK(STR, LEN) (!strncmp((name) + already_read, STR, LEN) && (READ_INC(LEN)))
David Sedlákc10e7902018-12-17 02:17:59 +0100238
David Sedlák1c8b2702019-02-22 11:03:02 +0100239 switch (*name) {
David Sedlák94de2aa2019-02-15 12:42:11 +0100240 case 'c':
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200241 READ_INC(1);
242 if (ARG_CHECK("ondition", 8)) {
243 ARG_SET(YIN_ARG_CONDITION);
244 }
David Sedlák3b4db242018-10-19 16:11:01 +0200245 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200246
David Sedlák94de2aa2019-02-15 12:42:11 +0100247 case 'd':
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200248 READ_INC(1);
249 if (ARG_CHECK("ate", 3)) {
250 ARG_SET(YIN_ARG_DATE);
251 }
David Sedlák3b4db242018-10-19 16:11:01 +0200252 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200253
David Sedlák94de2aa2019-02-15 12:42:11 +0100254 case 'm':
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200255 READ_INC(1);
256 if (ARG_CHECK("odule", 5)) {
257 ARG_SET(YIN_ARG_MODULE);
258 }
David Sedlák872c7b42018-10-26 13:15:20 +0200259 break;
260
David Sedlák94de2aa2019-02-15 12:42:11 +0100261 case 'n':
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200262 READ_INC(1);
263 if (ARG_CHECK("ame", 3)) {
264 ARG_SET(YIN_ARG_NAME);
265 }
David Sedlák872c7b42018-10-26 13:15:20 +0200266 break;
267
David Sedlák94de2aa2019-02-15 12:42:11 +0100268 case 't':
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200269 READ_INC(1);
270 if (ARG_CHECK("a", 1)) {
271 if (ARG_CHECK("g", 1)) {
272 ARG_SET(YIN_ARG_TAG);
273 } else if (ARG_CHECK("rget-node", 9)) {
274 ARG_SET(YIN_ARG_TARGET_NODE);
275 }
276 } else if (ARG_CHECK("ext", 3)) {
277 ARG_SET(YIN_ARG_TEXT);
278 }
David Sedlák3b4db242018-10-19 16:11:01 +0200279 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200280
David Sedlák94de2aa2019-02-15 12:42:11 +0100281 case 'u':
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200282 READ_INC(1);
283 if (ARG_CHECK("ri", 2)) {
284 ARG_SET(YIN_ARG_URI);
285 }
David Sedlák3b4db242018-10-19 16:11:01 +0200286 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200287
David Sedlák94de2aa2019-02-15 12:42:11 +0100288 case 'v':
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200289 READ_INC(1);
290 if (ARG_CHECK("alue", 4)) {
291 ARG_SET(YIN_ARG_VALUE);
292 }
David Sedlák3b4db242018-10-19 16:11:01 +0200293 break;
294 }
295
David Sedlákc10e7902018-12-17 02:17:59 +0100296 /* whole argument must be matched */
David Sedlák872c7b42018-10-26 13:15:20 +0200297 if (already_read != len) {
David Sedláka7406952019-04-05 10:33:07 +0200298 arg = YIN_ARG_UNKNOWN;
David Sedlák872c7b42018-10-26 13:15:20 +0200299 }
300
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200301#undef ARG_CHECK
302#undef ARG_SET
303#undef READ_INC
David Sedlák18730132019-03-15 15:51:34 +0100304
David Sedlák872c7b42018-10-26 13:15:20 +0200305 return arg;
David Sedlák3b4db242018-10-19 16:11:01 +0200306}
307
Radek Krejcid6b76452019-09-03 17:03:03 +0200308#define IS_NODE_ELEM(kw) (kw == LY_STMT_ANYXML || kw == LY_STMT_ANYDATA || kw == LY_STMT_LEAF || kw == LY_STMT_LEAF_LIST || \
309 kw == LY_STMT_TYPEDEF || kw == LY_STMT_USES || kw == LY_STMT_LIST || kw == LY_STMT_NOTIFICATION || \
310 kw == LY_STMT_GROUPING || kw == LY_STMT_CONTAINER || kw == LY_STMT_CASE || kw == LY_STMT_CHOICE || \
311 kw == LY_STMT_ACTION || kw == LY_STMT_RPC || kw == LY_STMT_AUGMENT)
David Sedlák81497a32019-08-13 16:56:26 +0200312
Radek Krejcid6b76452019-09-03 17:03:03 +0200313#define HAS_META(kw) (IS_NODE_ELEM(kw) || kw == LY_STMT_IMPORT || kw == LY_STMT_INCLUDE || kw == LY_STMT_INPUT || kw == LY_STMT_OUTPUT)
David Sedlák81497a32019-08-13 16:56:26 +0200314
David Sedlák26ea1432019-08-14 13:42:23 +0200315/**
316 * @brief Free subelems information allocated on heap.
317 *
318 * @param[in] count Size of subelems array.
319 * @param[in] subelems Subelems array to free.
320 */
David Sedlák81497a32019-08-13 16:56:26 +0200321static void
322subelems_deallocator(size_t count, struct yin_subelement *subelems)
323{
Michal Vaskod989ba02020-08-24 10:59:24 +0200324 for (size_t i = 0; i < count; ++i) {
David Sedlák81497a32019-08-13 16:56:26 +0200325 if (HAS_META(subelems[i].type)) {
326 free(subelems[i].dest);
327 }
328 }
329
330 free(subelems);
331}
332
David Sedlák26ea1432019-08-14 13:42:23 +0200333/**
334 * @brief Allocate subelems information on heap.
335 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200336 * @param[in] ctx YIN parser context, used for logging.
David Sedlák26ea1432019-08-14 13:42:23 +0200337 * @param[in] count Number of subelements.
338 * @param[in] parent Parent node if any.
339 * @param[out] result Allocated subelems array.
340 *
341 * @return LY_SUCCESS on success LY_EMEM on memmory allocation failure.
342 */
David Sedlák81497a32019-08-13 16:56:26 +0200343static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100344subelems_allocator(struct lys_yin_parser_ctx *ctx, size_t count, struct lysp_node *parent,
Radek Krejci0f969882020-08-21 16:56:47 +0200345 struct yin_subelement **result, ...)
David Sedlák81497a32019-08-13 16:56:26 +0200346{
347 va_list ap;
348
349 *result = calloc(count, sizeof **result);
David Sedlákbf8a2b72019-08-14 16:48:10 +0200350 LY_CHECK_GOTO(!(*result), mem_err);
David Sedlák81497a32019-08-13 16:56:26 +0200351
352 va_start(ap, result);
353 for (size_t i = 0; i < count; ++i) {
354 /* TYPE */
Radek Krejcid6b76452019-09-03 17:03:03 +0200355 (*result)[i].type = va_arg(ap, enum ly_stmt);
David Sedlák81497a32019-08-13 16:56:26 +0200356 /* DEST */
357 if (IS_NODE_ELEM((*result)[i].type)) {
358 struct tree_node_meta *node_meta = NULL;
Michal Vasko26bbb272022-08-02 14:54:33 +0200359
David Sedlák81497a32019-08-13 16:56:26 +0200360 node_meta = calloc(1, sizeof *node_meta);
David Sedlákbf8a2b72019-08-14 16:48:10 +0200361 LY_CHECK_GOTO(!node_meta, mem_err);
David Sedlák81497a32019-08-13 16:56:26 +0200362 node_meta->parent = parent;
David Sedlákbf8a2b72019-08-14 16:48:10 +0200363 node_meta->nodes = va_arg(ap, void *);
David Sedlák81497a32019-08-13 16:56:26 +0200364 (*result)[i].dest = node_meta;
Radek Krejcid6b76452019-09-03 17:03:03 +0200365 } else if ((*result)[i].type == LY_STMT_IMPORT) {
David Sedlák81497a32019-08-13 16:56:26 +0200366 struct import_meta *imp_meta = NULL;
Michal Vasko26bbb272022-08-02 14:54:33 +0200367
David Sedlák81497a32019-08-13 16:56:26 +0200368 imp_meta = calloc(1, sizeof *imp_meta);
David Sedlákbf8a2b72019-08-14 16:48:10 +0200369 LY_CHECK_GOTO(!imp_meta, mem_err);
David Sedlák81497a32019-08-13 16:56:26 +0200370 imp_meta->prefix = va_arg(ap, const char *);
371 imp_meta->imports = va_arg(ap, struct lysp_import **);
372 (*result)[i].dest = imp_meta;
Radek Krejcid6b76452019-09-03 17:03:03 +0200373 } else if ((*result)[i].type == LY_STMT_INCLUDE) {
David Sedlák81497a32019-08-13 16:56:26 +0200374 struct include_meta *inc_meta = NULL;
Michal Vasko26bbb272022-08-02 14:54:33 +0200375
David Sedlák81497a32019-08-13 16:56:26 +0200376 inc_meta = calloc(1, sizeof *inc_meta);
David Sedlákbf8a2b72019-08-14 16:48:10 +0200377 LY_CHECK_GOTO(!inc_meta, mem_err);
David Sedlák81497a32019-08-13 16:56:26 +0200378 inc_meta->name = va_arg(ap, const char *);
379 inc_meta->includes = va_arg(ap, struct lysp_include **);
380 (*result)[i].dest = inc_meta;
Michal Vasko69730152020-10-09 16:30:07 +0200381 } else if (((*result)[i].type == LY_STMT_INPUT) || ((*result)[i].type == LY_STMT_OUTPUT)) {
David Sedlák81497a32019-08-13 16:56:26 +0200382 struct inout_meta *inout_meta = NULL;
Michal Vasko26bbb272022-08-02 14:54:33 +0200383
David Sedlák81497a32019-08-13 16:56:26 +0200384 inout_meta = calloc(1, sizeof *inout_meta);
David Sedlákbf8a2b72019-08-14 16:48:10 +0200385 LY_CHECK_GOTO(!inout_meta, mem_err);
David Sedlák81497a32019-08-13 16:56:26 +0200386 inout_meta->parent = parent;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100387 inout_meta->inout_p = va_arg(ap, struct lysp_node_action_inout *);
David Sedlák81497a32019-08-13 16:56:26 +0200388 (*result)[i].dest = inout_meta;
389 } else {
390 (*result)[i].dest = va_arg(ap, void *);
391 }
392 /* FLAGS */
393 (*result)[i].flags = va_arg(ap, int);
394 }
395 va_end(ap);
396
397 return LY_SUCCESS;
398
David Sedlákbf8a2b72019-08-14 16:48:10 +0200399mem_err:
David Sedlák81497a32019-08-13 16:56:26 +0200400 subelems_deallocator(count, *result);
Michal Vaskob36053d2020-03-26 15:49:30 +0100401 LOGMEM(ctx->xmlctx->ctx);
David Sedlák81497a32019-08-13 16:56:26 +0200402 return LY_EMEM;
403}
404
Michal Vaskoafac7822020-10-20 14:22:26 +0200405/**
406 * @brief Check that val is valid UTF8 character sequence of val_type.
407 * Doesn't check empty string, only character validity.
408 *
409 * @param[in] ctx Yin parser context for logging.
410 * @param[in] val_type Type of the input string to select method of checking character validity.
411 *
412 * @return LY_ERR values.
413 */
David Sedlák8f7a1172019-06-20 14:42:18 +0200414LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100415yin_validate_value(struct lys_yin_parser_ctx *ctx, enum yang_arg val_type)
David Sedlák4a650532019-07-10 11:55:18 +0200416{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200417 uint8_t prefix = 0;
418 uint32_t c;
Michal Vaskob36053d2020-03-26 15:49:30 +0100419 size_t utf8_char_len, already_read = 0;
420 const char *val;
421
422 assert((ctx->xmlctx->status == LYXML_ELEM_CONTENT) || (ctx->xmlctx->status == LYXML_ATTR_CONTENT));
423
424 val = ctx->xmlctx->value;
425 while (already_read < ctx->xmlctx->value_len) {
David Sedlák4a650532019-07-10 11:55:18 +0200426 LY_CHECK_ERR_RET(ly_getutf8((const char **)&val, &c, &utf8_char_len),
Michal Vasko69730152020-10-09 16:30:07 +0200427 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INCHAR, (val)[-utf8_char_len]), LY_EVALID);
David Sedlák4a650532019-07-10 11:55:18 +0200428
429 switch (val_type) {
430 case Y_IDENTIF_ARG:
Radek Krejci1deb5be2020-08-26 16:43:36 +0200431 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, already_read ? 0 : 1, NULL));
David Sedlák4a650532019-07-10 11:55:18 +0200432 break;
433 case Y_PREF_IDENTIF_ARG:
Radek Krejci1deb5be2020-08-26 16:43:36 +0200434 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, already_read ? 0 : 1, &prefix));
David Sedlák4a650532019-07-10 11:55:18 +0200435 break;
436 case Y_STR_ARG:
437 case Y_MAYBE_STR_ARG:
438 LY_CHECK_RET(lysp_check_stringchar((struct lys_parser_ctx *)ctx, c));
439 break;
440 }
Radek Iša2eac74c2021-03-15 15:53:08 +0100441
442 already_read += utf8_char_len;
443 LY_CHECK_ERR_RET(already_read > ctx->xmlctx->value_len, LOGINT(ctx->xmlctx->ctx), LY_EINT);
David Sedlák4a650532019-07-10 11:55:18 +0200444 }
445
Radek Iša42cc0b72021-03-15 16:51:31 +0100446 if ((already_read == 0) &&
447 ((val_type == Y_PREF_IDENTIF_ARG) || (val_type == Y_IDENTIF_ARG))) {
448 /* Empty identifier or prefix*/
449 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YIN, "Empty identifier is not allowed.");
450 return LY_EVALID;
451 }
452
David Sedlák4a650532019-07-10 11:55:18 +0200453 return LY_SUCCESS;
454}
455
David Sedlákb4e44562019-07-04 15:42:12 +0200456/**
Michal Vaskob36053d2020-03-26 15:49:30 +0100457 * @brief Parse yin attributes.
David Sedlákb4e44562019-07-04 15:42:12 +0200458 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200459 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák4a650532019-07-10 11:55:18 +0200460 * @param[in] arg_type Type of argument that is expected in parsed element (use YIN_ARG_NONE for elements without
461 * special argument).
David Sedlákbf8a2b72019-08-14 16:48:10 +0200462 * @param[out] arg_val Where value of argument should be stored. Can be NULL iff arg_type is specified as YIN_ARG_NONE.
David Sedlák292763b2019-07-09 11:10:53 +0200463 * @param[in] val_type Type of expected value of attribute.
David Sedlákb4e44562019-07-04 15:42:12 +0200464 * @param[in] current_element Identification of current element, used for logging.
465 *
466 * @return LY_ERR values.
467 */
468static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100469yin_parse_attribute(struct lys_yin_parser_ctx *ctx, enum yin_argument arg_type, const char **arg_val, enum yang_arg val_type,
Radek Krejci0f969882020-08-21 16:56:47 +0200470 enum ly_stmt current_element)
David Sedlák8f7a1172019-06-20 14:42:18 +0200471{
David Sedlákc5b20842019-08-13 10:18:31 +0200472 enum yin_argument arg = YIN_ARG_UNKNOWN;
David Sedlák619db942019-07-03 14:47:30 +0200473 bool found = false;
David Sedlák8f7a1172019-06-20 14:42:18 +0200474
David Sedlák1bccdfa2019-06-17 15:55:27 +0200475 /* validation of attributes */
Michal Vaskob36053d2020-03-26 15:49:30 +0100476 while (ctx->xmlctx->status == LYXML_ATTRIBUTE) {
David Sedlák00250342019-06-21 14:19:39 +0200477 /* yin arguments represented as attributes have no namespace, which in this case means no prefix */
Michal Vaskob36053d2020-03-26 15:49:30 +0100478 if (!ctx->xmlctx->prefix) {
479 arg = yin_match_argument_name(ctx->xmlctx->name, ctx->xmlctx->name_len);
David Sedlák7ff55a92019-06-17 11:11:41 +0200480 if (arg == YIN_ARG_NONE) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100481 /* skip it */
482 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
David Sedlák7ff55a92019-06-17 11:11:41 +0200483 } else if (arg == arg_type) {
David Sedlák1538a842019-08-08 15:38:51 +0200484 LY_CHECK_ERR_RET(found, LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_DUP_ATTR,
Michal Vasko69730152020-10-09 16:30:07 +0200485 yin_attr2str(arg), ly_stmt2str(current_element)), LY_EVALID);
David Sedlák619db942019-07-03 14:47:30 +0200486 found = true;
Michal Vaskob36053d2020-03-26 15:49:30 +0100487
488 /* go to value */
489 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
490 LY_CHECK_RET(yin_validate_value(ctx, val_type));
Radek Krejci011e4aa2020-09-04 15:22:31 +0200491 INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic, *arg_val);
David Sedlák169cc522019-08-15 13:23:45 +0200492 LY_CHECK_RET(!(*arg_val), LY_EMEM);
David Sedlák2b214ac2019-06-06 16:11:03 +0200493 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100494 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_UNEXP_ATTR, ctx->xmlctx->name_len,
Michal Vasko69730152020-10-09 16:30:07 +0200495 ctx->xmlctx->name, ly_stmt2str(current_element));
David Sedlák619db942019-07-03 14:47:30 +0200496 return LY_EVALID;
David Sedláka7406952019-04-05 10:33:07 +0200497 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100498 } else {
499 /* skip it */
500 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
David Sedláka7406952019-04-05 10:33:07 +0200501 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100502
503 /* next attribute */
504 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
David Sedláka7406952019-04-05 10:33:07 +0200505 }
506
David Sedlák292763b2019-07-09 11:10:53 +0200507 /* anything else than Y_MAYBE_STR_ARG is mandatory */
Michal Vasko69730152020-10-09 16:30:07 +0200508 if ((val_type != Y_MAYBE_STR_ARG) && !found) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100509 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LYVE_SYNTAX_YIN, "Missing mandatory attribute %s of %s element.",
Michal Vasko69730152020-10-09 16:30:07 +0200510 yin_attr2str(arg_type), ly_stmt2str(current_element));
David Sedlák619db942019-07-03 14:47:30 +0200511 return LY_EVALID;
512 }
513
514 return LY_SUCCESS;
David Sedláka7406952019-04-05 10:33:07 +0200515}
516
David Sedlákd6e56892019-07-01 15:40:24 +0200517/**
Radek Krejci12b1c152019-09-05 16:20:48 +0200518 * @brief Get record with given type.
David Sedlákd6e56892019-07-01 15:40:24 +0200519 *
520 * @param[in] type Type of wanted record.
521 * @param[in] array_size Size of array.
522 * @param[in] array Searched array.
523 *
524 * @return Pointer to desired record on success, NULL if element is not in the array.
525 */
David Sedlákb4e44562019-07-04 15:42:12 +0200526static struct yin_subelement *
Radek Krejci1deb5be2020-08-26 16:43:36 +0200527get_record(enum ly_stmt type, LY_ARRAY_COUNT_TYPE array_size, struct yin_subelement *array)
David Sedlákd6e56892019-07-01 15:40:24 +0200528{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200529 for (LY_ARRAY_COUNT_TYPE u = 0; u < array_size; ++u) {
Radek Krejci12b1c152019-09-05 16:20:48 +0200530 if (array[u].type == type) {
531 return &array[u];
David Sedlákd6e56892019-07-01 15:40:24 +0200532 }
533 }
David Sedlákd6e56892019-07-01 15:40:24 +0200534 return NULL;
535}
536
David Sedlákbba38e52019-07-09 15:20:01 +0200537/**
538 * @brief Helper function to check mandatory constraint of subelement.
539 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200540 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200541 * @param[in] subelem_info Array of information about subelements.
542 * @param[in] subelem_info_size Size of subelem_info array.
543 * @param[in] current_element Identification of element that is currently being parsed, used for logging.
544 *
545 * @return LY_ERR values.
546 */
547static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100548yin_check_subelem_mandatory_constraint(struct lys_yin_parser_ctx *ctx, struct yin_subelement *subelem_info,
Radek Krejci0f969882020-08-21 16:56:47 +0200549 signed char subelem_info_size, enum ly_stmt current_element)
David Sedlák21f87cd2019-07-03 16:53:23 +0200550{
David Sedlákb0faad82019-07-04 14:28:59 +0200551 for (signed char i = 0; i < subelem_info_size; ++i) {
David Sedlák5545f5d2019-07-11 11:55:16 +0200552 /* if there is element that is mandatory and isn't parsed log error and return LY_EVALID */
Radek Krejci12b1c152019-09-05 16:20:48 +0200553 if ((subelem_info[i].flags & YIN_SUBELEM_MANDATORY) && !(subelem_info[i].flags & YIN_SUBELEM_PARSED)) {
David Sedlák1538a842019-08-08 15:38:51 +0200554 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_MAND_SUBELEM,
Michal Vasko69730152020-10-09 16:30:07 +0200555 ly_stmt2str(subelem_info[i].type), ly_stmt2str(current_element));
David Sedlák21f87cd2019-07-03 16:53:23 +0200556 return LY_EVALID;
557 }
558 }
559
560 return LY_SUCCESS;
561}
562
David Sedlákbba38e52019-07-09 15:20:01 +0200563/**
564 * @brief Helper function to check "first" constraint of subelement.
565 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200566 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200567 * @param[in] subelem_info Array of information about subelements.
568 * @param[in] subelem_info_size Size of subelem_info array.
569 * @param[in] current_element Identification of element that is currently being parsed, used for logging.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200570 * @param[in] exp_first Record in subelem_info array that is expected to be defined as first subelement, used for logging.
David Sedlákbba38e52019-07-09 15:20:01 +0200571 *
572 * @return LY_ERR values.
573 */
574static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100575yin_check_subelem_first_constraint(struct lys_yin_parser_ctx *ctx, struct yin_subelement *subelem_info,
Radek Krejci0f969882020-08-21 16:56:47 +0200576 signed char subelem_info_size, enum ly_stmt current_element,
577 struct yin_subelement *exp_first)
David Sedlák21f87cd2019-07-03 16:53:23 +0200578{
David Sedlákb0faad82019-07-04 14:28:59 +0200579 for (signed char i = 0; i < subelem_info_size; ++i) {
David Sedlák21f87cd2019-07-03 16:53:23 +0200580 if (subelem_info[i].flags & YIN_SUBELEM_PARSED) {
David Sedlák1538a842019-08-08 15:38:51 +0200581 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_FIRT_SUBELEM,
Michal Vasko69730152020-10-09 16:30:07 +0200582 ly_stmt2str(exp_first->type), ly_stmt2str(current_element));
David Sedlák21f87cd2019-07-03 16:53:23 +0200583 return LY_EVALID;
584 }
585 }
586
587 return LY_SUCCESS;
588}
589
David Sedlákbba38e52019-07-09 15:20:01 +0200590/**
David Sedlákb4e44562019-07-04 15:42:12 +0200591 * @brief Parse simple element without any special constraints and argument mapped to yin attribute,
592 * for example prefix or namespace element.
David Sedlákd6e56892019-07-01 15:40:24 +0200593 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200594 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákb4e44562019-07-04 15:42:12 +0200595 * @param[in] kw Type of current element.
596 * @param[out] value Where value of attribute should be stored.
597 * @param[in] arg_type Expected type of attribute.
David Sedlák292763b2019-07-09 11:10:53 +0200598 * @param[in] arg_val_type Type of expected value of attribute.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200599 * @param[in,out] exts Extension instances to add to.
David Sedlákd6e56892019-07-01 15:40:24 +0200600 * @return LY_ERR values.
601 */
David Sedlákb4e44562019-07-04 15:42:12 +0200602static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100603yin_parse_simple_element(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, const char **value,
Radek Krejci0f969882020-08-21 16:56:47 +0200604 enum yin_argument arg_type, enum yang_arg arg_val_type, struct lysp_ext_instance **exts)
David Sedlákb4e44562019-07-04 15:42:12 +0200605{
Michal Vaskob36053d2020-03-26 15:49:30 +0100606 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
607 LY_CHECK_RET(yin_parse_attribute(ctx, arg_type, value, arg_val_type, kw));
Radek Krejcif13b87b2020-12-01 22:02:17 +0100608 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200609 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
610 };
David Sedlákb4e44562019-07-04 15:42:12 +0200611
Radek Krejcif13b87b2020-12-01 22:02:17 +0100612 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), kw, NULL, exts);
David Sedlákb4e44562019-07-04 15:42:12 +0200613}
614
615/**
David Sedlák6542aed2019-08-14 10:47:43 +0200616 * @brief Parse path element.
617 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200618 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák6542aed2019-08-14 10:47:43 +0200619 * @param[in] kw Type of current element.
620 * @param[out] type Type structure to store parsed value, flags and extension instances.
David Sedlák6542aed2019-08-14 10:47:43 +0200621 * @return LY_ERR values.
622 */
623static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100624yin_parse_path(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, struct lysp_type *type)
David Sedlák6542aed2019-08-14 10:47:43 +0200625{
Michal Vasko004d3152020-06-11 19:59:22 +0200626 LY_ERR ret;
627 const char *str_path;
628
629 LY_CHECK_RET(yin_parse_simple_element(ctx, kw, &str_path, YIN_ARG_VALUE, Y_STR_ARG, &type->exts));
Michal Vaskoed725d72021-06-23 12:03:45 +0200630 ret = ly_path_parse(ctx->xmlctx->ctx, NULL, str_path, 0, 1, LY_PATH_BEGIN_EITHER,
Michal Vasko69730152020-10-09 16:30:07 +0200631 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path);
Michal Vasko004d3152020-06-11 19:59:22 +0200632 lydict_remove(ctx->xmlctx->ctx, str_path);
633 LY_CHECK_RET(ret);
David Sedlák6542aed2019-08-14 10:47:43 +0200634 type->flags |= LYS_SET_PATH;
635
636 return LY_SUCCESS;
637}
638
639/**
David Sedlákd3983112019-07-12 11:20:56 +0200640 * @brief Parse pattern element.
641 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200642 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200643 * @param[in,out] type Type structure to store parsed value, flags and extension instances.
David Sedlákd3983112019-07-12 11:20:56 +0200644 * @return LY_ERR values.
645 */
646static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100647yin_parse_pattern(struct lys_yin_parser_ctx *ctx, struct lysp_type *type)
David Sedlákd3983112019-07-12 11:20:56 +0200648{
649 const char *real_value = NULL;
650 char *saved_value = NULL;
651 struct lysp_restr *restr;
652
Michal Vaskob36053d2020-03-26 15:49:30 +0100653 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, type->patterns, restr, LY_EMEM);
654 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
655 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &real_value, Y_STR_ARG, LY_STMT_PATTERN));
David Sedlákd3983112019-07-12 11:20:56 +0200656 size_t len = strlen(real_value);
657
658 saved_value = malloc(len + 2);
Michal Vaskob36053d2020-03-26 15:49:30 +0100659 LY_CHECK_ERR_RET(!saved_value, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
David Sedlákd3983112019-07-12 11:20:56 +0200660 memmove(saved_value + 1, real_value, len);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100661 lydict_remove(ctx->xmlctx->ctx, real_value);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100662 saved_value[0] = LYSP_RESTR_PATTERN_ACK;
David Sedlákd3983112019-07-12 11:20:56 +0200663 saved_value[len + 1] = '\0';
Michal Vasko7f45cf22020-10-01 12:49:44 +0200664 LY_CHECK_RET(lydict_insert_zc(ctx->xmlctx->ctx, saved_value, &restr->arg.str));
Michal Vasko8a67eff2021-12-07 14:04:47 +0100665 restr->arg.mod = PARSER_CUR_PMOD(ctx);
David Sedlákd3983112019-07-12 11:20:56 +0200666 type->flags |= LYS_SET_PATTERN;
667
Radek Krejcif13b87b2020-12-01 22:02:17 +0100668 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200669 {LY_STMT_DESCRIPTION, &restr->dsc, YIN_SUBELEM_UNIQUE},
670 {LY_STMT_ERROR_APP_TAG, &restr->eapptag, YIN_SUBELEM_UNIQUE},
671 {LY_STMT_ERROR_MESSAGE, &restr->emsg, YIN_SUBELEM_UNIQUE},
672 {LY_STMT_MODIFIER, &restr->arg, YIN_SUBELEM_UNIQUE},
673 {LY_STMT_REFERENCE, &restr->ref, YIN_SUBELEM_UNIQUE},
674 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
675 };
Michal Vasko69730152020-10-09 16:30:07 +0200676
Radek Krejcif13b87b2020-12-01 22:02:17 +0100677 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_PATTERN, NULL, &restr->exts);
David Sedlákd3983112019-07-12 11:20:56 +0200678}
679
David Sedlákc5b20842019-08-13 10:18:31 +0200680/**
681 * @brief Parse fraction-digits element.
682 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200683 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákc5b20842019-08-13 10:18:31 +0200684 * @param[in,out] type Type structure to store value, flags and extension instances.
David Sedlákc5b20842019-08-13 10:18:31 +0200685 * @return LY_ERR values.
686 */
David Sedlákf75d55e2019-07-12 16:52:50 +0200687static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100688yin_parse_fracdigits(struct lys_yin_parser_ctx *ctx, struct lysp_type *type)
David Sedlákf75d55e2019-07-12 16:52:50 +0200689{
690 const char *temp_val = NULL;
691 char *ptr;
Michal Vasko73d77ab2021-07-23 12:45:55 +0200692 unsigned long long int num;
David Sedlákf75d55e2019-07-12 16:52:50 +0200693
Michal Vaskob36053d2020-03-26 15:49:30 +0100694 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
695 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_FRACTION_DIGITS));
David Sedlákf75d55e2019-07-12 16:52:50 +0200696
Michal Vasko69730152020-10-09 16:30:07 +0200697 if ((temp_val[0] == '\0') || (temp_val[0] == '0') || !isdigit(temp_val[0])) {
David Sedlák1538a842019-08-08 15:38:51 +0200698 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "fraction-digits");
Michal Vaskoe180ed02021-02-05 16:31:20 +0100699 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákf75d55e2019-07-12 16:52:50 +0200700 return LY_EVALID;
701 }
702
703 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +0200704 num = strtoull(temp_val, &ptr, LY_BASE_DEC);
David Sedlákf75d55e2019-07-12 16:52:50 +0200705 if (*ptr != '\0') {
David Sedlák1538a842019-08-08 15:38:51 +0200706 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "fraction-digits");
Michal Vaskoe180ed02021-02-05 16:31:20 +0100707 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákf75d55e2019-07-12 16:52:50 +0200708 return LY_EVALID;
709 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100710 if ((errno == ERANGE) || (num > LY_TYPE_DEC64_FD_MAX)) {
David Sedlák1538a842019-08-08 15:38:51 +0200711 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "fraction-digits");
Michal Vaskoe180ed02021-02-05 16:31:20 +0100712 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákf75d55e2019-07-12 16:52:50 +0200713 return LY_EVALID;
714 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100715 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákf75d55e2019-07-12 16:52:50 +0200716 type->fraction_digits = num;
717 type->flags |= LYS_SET_FRDIGITS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100718 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200719 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
720 };
Michal Vasko69730152020-10-09 16:30:07 +0200721
Radek Krejcif13b87b2020-12-01 22:02:17 +0100722 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_FRACTION_DIGITS, NULL, &type->exts);
David Sedlákf75d55e2019-07-12 16:52:50 +0200723}
724
David Sedlák07869a52019-07-12 14:28:19 +0200725/**
David Sedlák43801c92019-08-05 15:58:54 +0200726 * @brief Parse enum element.
David Sedlák07869a52019-07-12 14:28:19 +0200727 *
728 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200729 * @param[in,out] type Type structure to store parsed value, flags and extension instances.
David Sedlák07869a52019-07-12 14:28:19 +0200730 * @return LY_ERR values.
731 */
David Sedlákca36c422019-07-12 12:47:55 +0200732static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100733yin_parse_enum(struct lys_yin_parser_ctx *ctx, struct lysp_type *type)
David Sedlákca36c422019-07-12 12:47:55 +0200734{
David Sedlák07869a52019-07-12 14:28:19 +0200735 struct lysp_type_enum *en;
David Sedlák1e696782019-07-17 15:06:07 +0200736
Michal Vaskob36053d2020-03-26 15:49:30 +0100737 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, type->enums, en, LY_EMEM);
David Sedlák43801c92019-08-05 15:58:54 +0200738 type->flags |= LYS_SET_ENUM;
Michal Vaskob36053d2020-03-26 15:49:30 +0100739 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
740 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &en->name, Y_STR_ARG, LY_STMT_ENUM));
David Sedlák43801c92019-08-05 15:58:54 +0200741 LY_CHECK_RET(lysp_check_enum_name((struct lys_parser_ctx *)ctx, en->name, strlen(en->name)));
Michal Vaskob36053d2020-03-26 15:49:30 +0100742 CHECK_NONEMPTY((struct lys_parser_ctx *)ctx, strlen(en->name), "enum");
David Sedlák43801c92019-08-05 15:58:54 +0200743 CHECK_UNIQUENESS((struct lys_parser_ctx *)ctx, type->enums, name, "enum", en->name);
David Sedlákca36c422019-07-12 12:47:55 +0200744
Radek Krejcif13b87b2020-12-01 22:02:17 +0100745 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200746 {LY_STMT_DESCRIPTION, &en->dsc, YIN_SUBELEM_UNIQUE},
747 {LY_STMT_IF_FEATURE, &en->iffeatures, 0},
748 {LY_STMT_REFERENCE, &en->ref, YIN_SUBELEM_UNIQUE},
749 {LY_STMT_STATUS, &en->flags, YIN_SUBELEM_UNIQUE},
750 {LY_STMT_VALUE, en, YIN_SUBELEM_UNIQUE},
751 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
752 };
Michal Vasko69730152020-10-09 16:30:07 +0200753
Radek Krejcif13b87b2020-12-01 22:02:17 +0100754 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_ENUM, NULL, &en->exts);
David Sedlák43801c92019-08-05 15:58:54 +0200755}
756
757/**
758 * @brief Parse bit element.
759 *
760 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200761 * @param[in,out] type Type structure to store parsed value, flags and extension instances.
David Sedlák43801c92019-08-05 15:58:54 +0200762 * @return LY_ERR values.
763 */
764static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100765yin_parse_bit(struct lys_yin_parser_ctx *ctx, struct lysp_type *type)
David Sedlák43801c92019-08-05 15:58:54 +0200766{
767 struct lysp_type_enum *en;
768
Michal Vaskob36053d2020-03-26 15:49:30 +0100769 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, type->bits, en, LY_EMEM);
David Sedlák43801c92019-08-05 15:58:54 +0200770 type->flags |= LYS_SET_BIT;
Michal Vaskob36053d2020-03-26 15:49:30 +0100771 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
772 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &en->name, Y_IDENTIF_ARG, LY_STMT_BIT));
Radek Iša8b640882021-03-12 17:59:34 +0100773 CHECK_UNIQUENESS((struct lys_parser_ctx *)ctx, type->bits, name, "bit", en->name);
David Sedlák43801c92019-08-05 15:58:54 +0200774
Radek Krejcif13b87b2020-12-01 22:02:17 +0100775 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200776 {LY_STMT_DESCRIPTION, &en->dsc, YIN_SUBELEM_UNIQUE},
777 {LY_STMT_IF_FEATURE, &en->iffeatures, 0},
778 {LY_STMT_POSITION, en, YIN_SUBELEM_UNIQUE},
779 {LY_STMT_REFERENCE, &en->ref, YIN_SUBELEM_UNIQUE},
780 {LY_STMT_STATUS, &en->flags, YIN_SUBELEM_UNIQUE},
781 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
782 };
Michal Vasko69730152020-10-09 16:30:07 +0200783
Radek Krejcif13b87b2020-12-01 22:02:17 +0100784 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_BIT, NULL, &en->exts);
David Sedlákca36c422019-07-12 12:47:55 +0200785}
786
David Sedlákd3983112019-07-12 11:20:56 +0200787/**
David Sedlák5f8191e2019-07-08 16:35:52 +0200788 * @brief Parse simple element without any special constraints and argument mapped to yin attribute, that can have
789 * more instances, such as base or if-feature.
790 *
791 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák5f8191e2019-07-08 16:35:52 +0200792 * @param[in] kw Type of current element.
793 * @param[out] values Parsed values to add to.
794 * @param[in] arg_type Expected type of attribute.
David Sedlák292763b2019-07-09 11:10:53 +0200795 * @param[in] arg_val_type Type of expected value of attribute.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200796 * @param[in,out] exts Extension instances to add to.
David Sedlák5f8191e2019-07-08 16:35:52 +0200797 * @return LY_ERR values.
798 */
799static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100800yin_parse_simple_elements(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, const char ***values, enum yin_argument arg_type,
Radek Krejci0f969882020-08-21 16:56:47 +0200801 enum yang_arg arg_val_type, struct lysp_ext_instance **exts)
David Sedlák5f8191e2019-07-08 16:35:52 +0200802{
803 const char **value;
Michal Vasko69730152020-10-09 16:30:07 +0200804
Michal Vaskob36053d2020-03-26 15:49:30 +0100805 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *values, value, LY_EMEM);
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200806 LY_ARRAY_COUNT_TYPE index = LY_ARRAY_COUNT(*values) - 1;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100807 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200808 {LY_STMT_EXTENSION_INSTANCE, &index, 0}
809 };
David Sedlák968ac342019-07-11 15:17:59 +0200810
Michal Vaskob36053d2020-03-26 15:49:30 +0100811 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
812 LY_CHECK_RET(yin_parse_attribute(ctx, arg_type, value, arg_val_type, kw));
David Sedlák5f8191e2019-07-08 16:35:52 +0200813
Radek Krejcif13b87b2020-12-01 22:02:17 +0100814 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), kw, NULL, exts);
David Sedlák5f8191e2019-07-08 16:35:52 +0200815}
816
817/**
David Sedlák6542aed2019-08-14 10:47:43 +0200818 * @brief Parse simple element without any special constraints and argument mapped to yin attribute.
819 *
820 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák6542aed2019-08-14 10:47:43 +0200821 * @param[in] kw Type of current element.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200822 * @param[in] subinfo Information about subelement, is used to determin which function should be called and where to store parsed value.
David Sedlák6542aed2019-08-14 10:47:43 +0200823 * @param[in] arg_type Expected type of attribute.
824 * @param[in] arg_val_type Type of expected value of attribute.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200825 * @param[in,out] exts Extension instances to add to.
David Sedlák6542aed2019-08-14 10:47:43 +0200826 * @return LY_ERR values.
827 */
828static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100829yin_parse_simple_elem(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, struct yin_subelement *subinfo,
Radek Krejci0f969882020-08-21 16:56:47 +0200830 enum yin_argument arg_type, enum yang_arg arg_val_type, struct lysp_ext_instance **exts)
David Sedlák6542aed2019-08-14 10:47:43 +0200831{
832 if (subinfo->flags & YIN_SUBELEM_UNIQUE) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100833 LY_CHECK_RET(yin_parse_simple_element(ctx, kw, (const char **)subinfo->dest,
Michal Vasko69730152020-10-09 16:30:07 +0200834 arg_type, arg_val_type, exts));
David Sedlák6542aed2019-08-14 10:47:43 +0200835 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100836 LY_CHECK_RET(yin_parse_simple_elements(ctx, kw, (const char ***)subinfo->dest,
Michal Vasko69730152020-10-09 16:30:07 +0200837 arg_type, arg_val_type, exts));
David Sedlák6542aed2019-08-14 10:47:43 +0200838 }
839
840 return LY_SUCCESS;
841}
842
843/**
844 * @brief Parse base element.
845 *
846 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák6542aed2019-08-14 10:47:43 +0200847 * @param[in] parent Identification of parent element.
848 * @param[out] dest Where parsed values should be stored.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200849 * @param[in,out] exts Extension instances to add to.
David Sedlák6542aed2019-08-14 10:47:43 +0200850 * @return LY_ERR values.
851 */
852static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100853yin_parse_base(struct lys_yin_parser_ctx *ctx, enum ly_stmt parent, void *dest, struct lysp_ext_instance **exts)
David Sedlák6542aed2019-08-14 10:47:43 +0200854{
855 struct lysp_type *type = NULL;
856
Radek Krejcid6b76452019-09-03 17:03:03 +0200857 if (parent == LY_STMT_TYPE) {
David Sedlák6542aed2019-08-14 10:47:43 +0200858 type = (struct lysp_type *)dest;
Michal Vaskob36053d2020-03-26 15:49:30 +0100859 LY_CHECK_RET(yin_parse_simple_elements(ctx, LY_STMT_BASE, &type->bases, YIN_ARG_NAME,
Michal Vasko69730152020-10-09 16:30:07 +0200860 Y_PREF_IDENTIF_ARG, exts));
David Sedlák6542aed2019-08-14 10:47:43 +0200861 type->flags |= LYS_SET_BASE;
Radek Krejcid6b76452019-09-03 17:03:03 +0200862 } else if (parent == LY_STMT_IDENTITY) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100863 LY_CHECK_RET(yin_parse_simple_elements(ctx, LY_STMT_BASE, (const char ***)dest,
Michal Vasko69730152020-10-09 16:30:07 +0200864 YIN_ARG_NAME, Y_PREF_IDENTIF_ARG, exts));
David Sedlák6542aed2019-08-14 10:47:43 +0200865 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100866 LOGINT(ctx->xmlctx->ctx);
David Sedlák6542aed2019-08-14 10:47:43 +0200867 return LY_EINT;
868 }
869
870 return LY_SUCCESS;
871}
872
873/**
David Sedlákbf8a2b72019-08-14 16:48:10 +0200874 * @brief Parse require-instance element.
David Sedlákcf5569a2019-07-11 13:31:34 +0200875 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200876 * @param[in,out] ctx YIN parser context for logging and to store current state.
Michal Vaskob36053d2020-03-26 15:49:30 +0100877 * @param[out] type Type structure to store value, flag and extensions.
David Sedlákcf5569a2019-07-11 13:31:34 +0200878 * @return LY_ERR values.
879 */
880static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100881yin_pasrse_reqinstance(struct lys_yin_parser_ctx *ctx, struct lysp_type *type)
David Sedlákcf5569a2019-07-11 13:31:34 +0200882{
883 const char *temp_val = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100884 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200885 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
886 };
David Sedlákcf5569a2019-07-11 13:31:34 +0200887
888 type->flags |= LYS_SET_REQINST;
Michal Vaskob36053d2020-03-26 15:49:30 +0100889 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
890 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_REQUIRE_INSTANCE));
David Sedlákcf5569a2019-07-11 13:31:34 +0200891 if (strcmp(temp_val, "true") == 0) {
892 type->require_instance = 1;
893 } else if (strcmp(temp_val, "false") != 0) {
David Sedlák26ea1432019-08-14 13:42:23 +0200894 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value",
Michal Vasko69730152020-10-09 16:30:07 +0200895 "require-instance", "true", "false");
Michal Vaskoe180ed02021-02-05 16:31:20 +0100896 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákcf5569a2019-07-11 13:31:34 +0200897 return LY_EVALID;
898 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100899 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákcf5569a2019-07-11 13:31:34 +0200900
Radek Krejcif13b87b2020-12-01 22:02:17 +0100901 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_REQUIRE_INSTANCE, NULL, &type->exts);
David Sedlákcf5569a2019-07-11 13:31:34 +0200902}
903
904/**
David Sedlákce77bf52019-07-11 16:59:31 +0200905 * @brief Parse modifier element.
906 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200907 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákce77bf52019-07-11 16:59:31 +0200908 * @param[in,out] pat Value to write to.
909 * @param[in,out] exts Extension instances to add to.
910 *
911 * @return LY_ERR values.
912 */
913static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100914yin_parse_modifier(struct lys_yin_parser_ctx *ctx, const char **pat, struct lysp_ext_instance **exts)
David Sedlákce77bf52019-07-11 16:59:31 +0200915{
David Sedlákd3983112019-07-12 11:20:56 +0200916 assert(**pat == 0x06);
David Sedlákce77bf52019-07-11 16:59:31 +0200917 const char *temp_val;
918 char *modified_val;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100919 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200920 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
921 };
David Sedlákce77bf52019-07-11 16:59:31 +0200922
Michal Vaskob36053d2020-03-26 15:49:30 +0100923 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
924 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_MODIFIER));
David Sedlákce77bf52019-07-11 16:59:31 +0200925 if (strcmp(temp_val, "invert-match") != 0) {
David Sedlák26ea1432019-08-14 13:42:23 +0200926 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS1, temp_val, "value",
Michal Vasko69730152020-10-09 16:30:07 +0200927 "modifier", "invert-match");
Michal Vaskoe180ed02021-02-05 16:31:20 +0100928 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákce77bf52019-07-11 16:59:31 +0200929 return LY_EVALID;
930 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100931 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákce77bf52019-07-11 16:59:31 +0200932
933 /* allocate new value */
David Sedlákd3983112019-07-12 11:20:56 +0200934 modified_val = malloc(strlen(*pat) + 1);
Michal Vaskob36053d2020-03-26 15:49:30 +0100935 LY_CHECK_ERR_RET(!modified_val, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
David Sedlákd3983112019-07-12 11:20:56 +0200936 strcpy(modified_val, *pat);
Michal Vaskob36053d2020-03-26 15:49:30 +0100937 lydict_remove(ctx->xmlctx->ctx, *pat);
David Sedlákce77bf52019-07-11 16:59:31 +0200938
939 /* modify the new value */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100940 modified_val[0] = LYSP_RESTR_PATTERN_NACK;
Radek Krejci011e4aa2020-09-04 15:22:31 +0200941 LY_CHECK_RET(lydict_insert_zc(ctx->xmlctx->ctx, modified_val, pat));
David Sedlákce77bf52019-07-11 16:59:31 +0200942
Radek Krejcif13b87b2020-12-01 22:02:17 +0100943 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_MODIFIER, NULL, exts);
David Sedlákce77bf52019-07-11 16:59:31 +0200944}
945
946/**
David Sedlákb7296dd2019-07-11 14:58:38 +0200947 * @brief Parse a restriction element (length, range or one instance of must).
948 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200949 * @param[in,out] ctx YIN parser context for logging and to store current state.
Radek Krejcid6b76452019-09-03 17:03:03 +0200950 * @param[in] restr_kw Identificaton of element that is being parsed, can be set to LY_STMT_MUST, LY_STMT_LENGTH or LY_STMT_RANGE.
David Sedlák6542aed2019-08-14 10:47:43 +0200951 * @param[in] restr Value to write to.
David Sedlákb7296dd2019-07-11 14:58:38 +0200952 */
953static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100954yin_parse_restriction(struct lys_yin_parser_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr *restr)
David Sedlákb7296dd2019-07-11 14:58:38 +0200955{
Radek Krejcid6b76452019-09-03 17:03:03 +0200956 assert(restr_kw == LY_STMT_MUST || restr_kw == LY_STMT_LENGTH || restr_kw == LY_STMT_RANGE);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100957 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200958 {LY_STMT_DESCRIPTION, &restr->dsc, YIN_SUBELEM_UNIQUE},
959 {LY_STMT_ERROR_APP_TAG, &restr->eapptag, YIN_SUBELEM_UNIQUE},
960 {LY_STMT_ERROR_MESSAGE, &restr->emsg, YIN_SUBELEM_UNIQUE},
961 {LY_STMT_REFERENCE, &restr->ref, YIN_SUBELEM_UNIQUE},
962 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
963 };
David Sedlákb7296dd2019-07-11 14:58:38 +0200964 /* argument of must is called condition, but argument of length and range is called value */
Radek Krejcid6b76452019-09-03 17:03:03 +0200965 enum yin_argument arg_type = (restr_kw == LY_STMT_MUST) ? YIN_ARG_CONDITION : YIN_ARG_VALUE;
David Sedlákb7296dd2019-07-11 14:58:38 +0200966
Michal Vaskob36053d2020-03-26 15:49:30 +0100967 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
Michal Vasko7f45cf22020-10-01 12:49:44 +0200968 LY_CHECK_RET(yin_parse_attribute(ctx, arg_type, &restr->arg.str, Y_STR_ARG, restr_kw));
Michal Vasko8a67eff2021-12-07 14:04:47 +0100969 restr->arg.mod = PARSER_CUR_PMOD(ctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100970
Radek Krejcif13b87b2020-12-01 22:02:17 +0100971 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), restr_kw, NULL, &restr->exts);
David Sedlákb7296dd2019-07-11 14:58:38 +0200972}
973
974/**
David Sedlák6542aed2019-08-14 10:47:43 +0200975 * @brief Parse range element.
976 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200977 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák6542aed2019-08-14 10:47:43 +0200978 * @param[out] type Type structure to store parsed value and flags.
979 *
980 * @return LY_ERR values.
981 */
982static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100983yin_parse_range(struct lys_yin_parser_ctx *ctx, struct lysp_type *type)
David Sedlák6542aed2019-08-14 10:47:43 +0200984{
985 type->range = calloc(1, sizeof *type->range);
Michal Vaskob36053d2020-03-26 15:49:30 +0100986 LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
987 LY_CHECK_RET(yin_parse_restriction(ctx, LY_STMT_RANGE, type->range));
Michal Vasko44f3d2c2020-08-24 09:49:38 +0200988 type->flags |= LYS_SET_RANGE;
David Sedlák6542aed2019-08-14 10:47:43 +0200989
990 return LY_SUCCESS;
991}
992
993/**
994 * @brief Parse length element.
995 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200996 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák6542aed2019-08-14 10:47:43 +0200997 * @param[out] type Type structure to store parsed value and flags.
998 *
999 * @return LY_ERR values.
1000 */
1001static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001002yin_parse_length(struct lys_yin_parser_ctx *ctx, struct lysp_type *type)
David Sedlák6542aed2019-08-14 10:47:43 +02001003{
1004 type->length = calloc(1, sizeof *type->length);
Michal Vaskob36053d2020-03-26 15:49:30 +01001005 LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
1006 LY_CHECK_RET(yin_parse_restriction(ctx, LY_STMT_LENGTH, type->length));
David Sedlák6542aed2019-08-14 10:47:43 +02001007 type->flags |= LYS_SET_LENGTH;
1008
1009 return LY_SUCCESS;
1010}
1011
1012/**
David Sedlákbc9ec9c2019-07-11 15:53:55 +02001013 * @brief Parse must element.
1014 *
1015 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbc9ec9c2019-07-11 15:53:55 +02001016 * @param[in,out] restrs Restrictions to add to.
1017 *
1018 * @return LY_ERR values.
1019 */
1020static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001021yin_parse_must(struct lys_yin_parser_ctx *ctx, struct lysp_restr **restrs)
David Sedlákbc9ec9c2019-07-11 15:53:55 +02001022{
1023 struct lysp_restr *restr;
1024
Michal Vaskob36053d2020-03-26 15:49:30 +01001025 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *restrs, restr, LY_EMEM);
1026 return yin_parse_restriction(ctx, LY_STMT_MUST, restr);
David Sedlákbc9ec9c2019-07-11 15:53:55 +02001027}
1028
1029/**
Michal Vasko7f45cf22020-10-01 12:49:44 +02001030 * @brief Parse a node id into an array.
1031 *
1032 * @param[in,out] ctx YIN parser context for logging and to store current state.
1033 * @param[in] kw Type of current element.
1034 * @param[in] subinfo Information about subelement, is used to determin which function should be called and where to store parsed value.
1035 * @param[in,out] exts Extension instances to add to.
1036 *
1037 * @return LY_ERR values.
1038 */
1039static LY_ERR
1040yin_parse_qname(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, struct yin_subelement *subinfo,
1041 struct lysp_ext_instance **exts)
1042{
1043 struct lysp_qname *qname, **qnames;
1044
1045 switch (kw) {
1046 case LY_STMT_DEFAULT:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001047 if (subinfo->flags & YIN_SUBELEM_UNIQUE) {
1048 qname = (struct lysp_qname *)subinfo->dest;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001049 } else {
1050 qnames = (struct lysp_qname **)subinfo->dest;
1051 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *qnames, qname, LY_EMEM);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001052 }
Michal Vasko8a67eff2021-12-07 14:04:47 +01001053 qname->mod = PARSER_CUR_PMOD(ctx);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001054 return yin_parse_simple_element(ctx, kw, &qname->str, YIN_ARG_VALUE, Y_STR_ARG, exts);
Radek Išabe8a6532021-03-29 08:54:22 +02001055 case LY_STMT_UNIQUE:
1056 assert(!(subinfo->flags & YIN_SUBELEM_UNIQUE));
1057 qnames = (struct lysp_qname **)subinfo->dest;
1058 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *qnames, qname, LY_EMEM);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001059 qname->mod = PARSER_CUR_PMOD(ctx);
Radek Išabe8a6532021-03-29 08:54:22 +02001060 return yin_parse_simple_element(ctx, kw, &qname->str, YIN_ARG_TAG, Y_STR_ARG, exts);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001061 case LY_STMT_IF_FEATURE:
1062 assert(!(subinfo->flags & YIN_SUBELEM_UNIQUE));
1063 qnames = (struct lysp_qname **)subinfo->dest;
1064 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *qnames, qname, LY_EMEM);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001065 qname->mod = PARSER_CUR_PMOD(ctx);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001066 return yin_parse_simple_element(ctx, kw, &qname->str, YIN_ARG_NAME, Y_STR_ARG, exts);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001067 default:
1068 break;
1069 }
1070
1071 LOGINT(ctx->xmlctx->ctx);
1072 return LY_EINT;
1073}
1074
1075/**
David Sedlák5545f5d2019-07-11 11:55:16 +02001076 * @brief Parse position or value element.
1077 *
1078 * @param[in,out] ctx YIN parser context for logging and to store current state.
Radek Krejcid6b76452019-09-03 17:03:03 +02001079 * @param[in] kw Type of current element, can be set to LY_STMT_POSITION or LY_STMT_VALUE.
David Sedlákbf8a2b72019-08-14 16:48:10 +02001080 * @param[out] enm Enum structure to save value, flags and extension instances.
David Sedlák5545f5d2019-07-11 11:55:16 +02001081 *
1082 * @return LY_ERR values.
1083 */
1084static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001085yin_parse_value_pos(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, struct lysp_type_enum *enm)
David Sedlák5545f5d2019-07-11 11:55:16 +02001086{
Radek Krejcid6b76452019-09-03 17:03:03 +02001087 assert(kw == LY_STMT_POSITION || kw == LY_STMT_VALUE);
David Sedlák5545f5d2019-07-11 11:55:16 +02001088 const char *temp_val = NULL;
1089 char *ptr;
Michal Vasko73d77ab2021-07-23 12:45:55 +02001090 long long int num = 0;
1091 unsigned long long int unum = 0;
David Sedlák5545f5d2019-07-11 11:55:16 +02001092
1093 /* set value flag */
1094 enm->flags |= LYS_SET_VALUE;
1095
1096 /* get attribute value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001097 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1098 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, kw));
Michal Vasko69730152020-10-09 16:30:07 +02001099 if (!temp_val || (temp_val[0] == '\0') || (temp_val[0] == '+') ||
Radek Krejci0f969882020-08-21 16:56:47 +02001100 ((temp_val[0] == '0') && (temp_val[1] != '\0')) || ((kw == LY_STMT_POSITION) && !strcmp(temp_val, "-0"))) {
David Sedlák1538a842019-08-08 15:38:51 +02001101 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", ly_stmt2str(kw));
David Sedlák5545f5d2019-07-11 11:55:16 +02001102 goto error;
1103 }
1104
1105 /* convert value */
1106 errno = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001107 if (kw == LY_STMT_VALUE) {
Michal Vasko73d77ab2021-07-23 12:45:55 +02001108 num = strtoll(temp_val, &ptr, LY_BASE_DEC);
Michal Vasko69730152020-10-09 16:30:07 +02001109 if ((num < INT64_C(-2147483648)) || (num > INT64_C(2147483647))) {
David Sedlák1538a842019-08-08 15:38:51 +02001110 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", ly_stmt2str(kw));
David Sedlák5545f5d2019-07-11 11:55:16 +02001111 goto error;
1112 }
1113 } else {
Michal Vasko73d77ab2021-07-23 12:45:55 +02001114 unum = strtoull(temp_val, &ptr, LY_BASE_DEC);
David Sedlák5545f5d2019-07-11 11:55:16 +02001115 if (unum > UINT64_C(4294967295)) {
David Sedlák1538a842019-08-08 15:38:51 +02001116 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", ly_stmt2str(kw));
David Sedlák5545f5d2019-07-11 11:55:16 +02001117 goto error;
1118 }
1119 }
1120 /* check if whole argument value was converted */
1121 if (*ptr != '\0') {
David Sedlák1538a842019-08-08 15:38:51 +02001122 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", ly_stmt2str(kw));
David Sedlákebcd0eb2019-07-16 17:55:12 +02001123 goto error;
David Sedlák5545f5d2019-07-11 11:55:16 +02001124 }
1125 if (errno == ERANGE) {
David Sedlák1538a842019-08-08 15:38:51 +02001126 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_OOB_YIN, temp_val, "value", ly_stmt2str(kw));
David Sedlák5545f5d2019-07-11 11:55:16 +02001127 goto error;
1128 }
1129 /* save correctly ternary operator can't be used because num and unum have different signes */
Radek Krejcid6b76452019-09-03 17:03:03 +02001130 if (kw == LY_STMT_VALUE) {
David Sedlák5545f5d2019-07-11 11:55:16 +02001131 enm->value = num;
1132 } else {
1133 enm->value = unum;
1134 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01001135 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlák5545f5d2019-07-11 11:55:16 +02001136
1137 /* parse subelements */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001138 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001139 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1140 };
Michal Vasko69730152020-10-09 16:30:07 +02001141
Radek Krejcif13b87b2020-12-01 22:02:17 +01001142 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), kw, NULL, &enm->exts);
David Sedlák5545f5d2019-07-11 11:55:16 +02001143
David Sedlákbf8a2b72019-08-14 16:48:10 +02001144error:
Michal Vaskoe180ed02021-02-05 16:31:20 +01001145 lydict_remove(ctx->xmlctx->ctx, temp_val);
Michal Vaskob36053d2020-03-26 15:49:30 +01001146 return LY_EVALID;
David Sedlák5545f5d2019-07-11 11:55:16 +02001147}
1148
David Sedlák05404f62019-07-24 14:11:53 +02001149/**
1150 * @brief Parse belongs-to element.
1151 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001152 * @param[in] ctx YIN parser context for logging and to store current state.
David Sedlák05404f62019-07-24 14:11:53 +02001153 * @param[out] submod Structure of submodule that is being parsed.
1154 * @param[in,out] exts Extension instances to add to.
1155 *
1156 * @return LY_ERR values
1157 */
1158static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001159yin_parse_belongs_to(struct lys_yin_parser_ctx *ctx, struct lysp_submodule *submod, struct lysp_ext_instance **exts)
David Sedlák05404f62019-07-24 14:11:53 +02001160{
Michal Vaskoc3781c32020-10-06 14:04:08 +02001161 const char *belongsto;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001162 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001163 {LY_STMT_PREFIX, &submod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
1164 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1165 };
David Sedlák05404f62019-07-24 14:11:53 +02001166
Michal Vaskob36053d2020-03-26 15:49:30 +01001167 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
Michal Vaskoc3781c32020-10-06 14:04:08 +02001168 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_MODULE, &belongsto, Y_IDENTIF_ARG, LY_STMT_BELONGS_TO));
Michal Vasko8a67eff2021-12-07 14:04:47 +01001169 if (PARSER_CUR_PMOD(ctx)->mod->name != belongsto) {
Michal Vaskoc3781c32020-10-06 14:04:08 +02001170 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YIN, "Submodule \"belongs-to\" value \"%s\" does not match its module name \"%s\".",
Michal Vasko8a67eff2021-12-07 14:04:47 +01001171 belongsto, PARSER_CUR_PMOD(ctx)->mod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02001172 lydict_remove(ctx->xmlctx->ctx, belongsto);
1173 return LY_EVALID;
1174 }
1175 lydict_remove(ctx->xmlctx->ctx, belongsto);
Michal Vaskob36053d2020-03-26 15:49:30 +01001176
Radek Krejcif13b87b2020-12-01 22:02:17 +01001177 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_BELONGS_TO, NULL, exts);
David Sedlák05404f62019-07-24 14:11:53 +02001178}
1179
David Sedlák5545f5d2019-07-11 11:55:16 +02001180/**
David Sedlákc1771b12019-07-10 15:55:46 +02001181 * @brief Function to parse meta tags (description, contact, ...) eg. elements with
David Sedlákbf8a2b72019-08-14 16:48:10 +02001182 * text element as child.
David Sedlákb4e44562019-07-04 15:42:12 +02001183 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001184 * @param[in,out] ctx YIN parser context for logging and to store current state.
Radek Krejcid6b76452019-09-03 17:03:03 +02001185 * @param[in] elem_type Type of element can be set to LY_STMT_ORGANIZATION or LY_STMT_CONTACT or LY_STMT_DESCRIPTION or LY_STMT_REFERENCE.
David Sedlákb4e44562019-07-04 15:42:12 +02001186 * @param[out] value Where the content of meta element should be stored.
David Sedlákbf8a2b72019-08-14 16:48:10 +02001187 * @param[in,out] exts Extension instances to add to.
David Sedlákb4e44562019-07-04 15:42:12 +02001188 *
1189 * @return LY_ERR values.
1190 */
1191static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001192yin_parse_meta(struct lys_yin_parser_ctx *ctx, enum ly_stmt elem_type, const char **value, struct lysp_ext_instance **exts)
David Sedlákb4e44562019-07-04 15:42:12 +02001193{
Radek Krejcid6b76452019-09-03 17:03:03 +02001194 assert(elem_type == LY_STMT_ORGANIZATION || elem_type == LY_STMT_CONTACT || elem_type == LY_STMT_DESCRIPTION || elem_type == LY_STMT_REFERENCE);
David Sedlákb4e44562019-07-04 15:42:12 +02001195
Radek Krejcif13b87b2020-12-01 22:02:17 +01001196 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001197 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1198 {LY_STMT_ARG_TEXT, value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE | YIN_SUBELEM_FIRST}
1199 };
Michal Vasko69730152020-10-09 16:30:07 +02001200
David Sedlákdf2a9732019-08-07 13:23:16 +02001201 /* check attributes */
Michal Vaskob36053d2020-03-26 15:49:30 +01001202 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1203 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NONE, NULL, Y_MAYBE_STR_ARG, elem_type));
David Sedlákb4e44562019-07-04 15:42:12 +02001204
David Sedlákdf2a9732019-08-07 13:23:16 +02001205 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001206 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), elem_type, NULL, exts);
David Sedlákb4e44562019-07-04 15:42:12 +02001207}
1208
1209/**
David Sedlákc1771b12019-07-10 15:55:46 +02001210 * @brief Parse error-message element.
1211 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001212 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákc1771b12019-07-10 15:55:46 +02001213 * @param[out] value Where the content of error-message element should be stored.
David Sedlákbf8a2b72019-08-14 16:48:10 +02001214 * @param[in,out] exts Extension instances to add to.
David Sedlákc1771b12019-07-10 15:55:46 +02001215 *
1216 * @return LY_ERR values.
1217 */
1218static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001219yin_parse_err_msg(struct lys_yin_parser_ctx *ctx, const char **value, struct lysp_ext_instance **exts)
David Sedlákc1771b12019-07-10 15:55:46 +02001220{
Radek Krejcif13b87b2020-12-01 22:02:17 +01001221 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001222 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1223 {LY_STMT_ARG_VALUE, value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE | YIN_SUBELEM_FIRST}
1224 };
David Sedlákc1771b12019-07-10 15:55:46 +02001225
David Sedlákdf2a9732019-08-07 13:23:16 +02001226 /* check attributes */
Michal Vaskob36053d2020-03-26 15:49:30 +01001227 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1228 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NONE, NULL, Y_MAYBE_STR_ARG, LY_STMT_ERROR_MESSAGE));
David Sedlákdf2a9732019-08-07 13:23:16 +02001229
Radek Krejcif13b87b2020-12-01 22:02:17 +01001230 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_ERROR_MESSAGE, NULL, exts);
David Sedlákc1771b12019-07-10 15:55:46 +02001231}
1232
1233/**
David Sedlák6542aed2019-08-14 10:47:43 +02001234 * @brief Parse type element.
David Sedlák374d2b32019-07-17 15:06:55 +02001235 *
1236 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák6542aed2019-08-14 10:47:43 +02001237 * @param[in] parent Identification of parent element.
David Sedlákbf8a2b72019-08-14 16:48:10 +02001238 * @param[in,out] type Type to write to.
David Sedlák374d2b32019-07-17 15:06:55 +02001239 *
1240 * @return LY_ERR values.
1241 */
1242static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001243yin_parse_type(struct lys_yin_parser_ctx *ctx, enum ly_stmt parent, struct yin_subelement *subinfo)
David Sedlák374d2b32019-07-17 15:06:55 +02001244{
David Sedlák6542aed2019-08-14 10:47:43 +02001245 struct lysp_type *type = NULL;
Michal Vasko69730152020-10-09 16:30:07 +02001246
Radek Krejcid6b76452019-09-03 17:03:03 +02001247 if (parent == LY_STMT_DEVIATE) {
David Sedlák6542aed2019-08-14 10:47:43 +02001248 *(struct lysp_type **)subinfo->dest = calloc(1, sizeof **(struct lysp_type **)subinfo->dest);
Michal Vaskob36053d2020-03-26 15:49:30 +01001249 LY_CHECK_ERR_RET(!(*(struct lysp_type **)subinfo->dest), LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
David Sedlák6542aed2019-08-14 10:47:43 +02001250 type = *((struct lysp_type **)subinfo->dest);
Michal Vaskod989ba02020-08-24 10:59:24 +02001251 } else {
David Sedlák6542aed2019-08-14 10:47:43 +02001252 type = (struct lysp_type *)subinfo->dest;
1253 }
Michal Vaskoe9c050f2020-10-06 14:01:23 +02001254
David Sedlák6542aed2019-08-14 10:47:43 +02001255 /* type as child of another type */
Radek Krejcid6b76452019-09-03 17:03:03 +02001256 if (parent == LY_STMT_TYPE) {
David Sedlák6542aed2019-08-14 10:47:43 +02001257 struct lysp_type *nested_type = NULL;
Michal Vasko26bbb272022-08-02 14:54:33 +02001258
Michal Vaskob36053d2020-03-26 15:49:30 +01001259 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, type->types, nested_type, LY_EMEM);
David Sedlák6542aed2019-08-14 10:47:43 +02001260 type->flags |= LYS_SET_TYPE;
1261 type = nested_type;
1262 }
Michal Vaskoe9c050f2020-10-06 14:01:23 +02001263
Michal Vasko8a67eff2021-12-07 14:04:47 +01001264 type->pmod = PARSER_CUR_PMOD(ctx);
Michal Vasko5bbcc242021-02-23 09:29:13 +01001265
Radek Krejcif13b87b2020-12-01 22:02:17 +01001266 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001267 {LY_STMT_BASE, type, 0},
1268 {LY_STMT_BIT, type, 0},
1269 {LY_STMT_ENUM, type, 0},
1270 {LY_STMT_FRACTION_DIGITS, type, YIN_SUBELEM_UNIQUE},
1271 {LY_STMT_LENGTH, type, YIN_SUBELEM_UNIQUE},
1272 {LY_STMT_PATH, type, YIN_SUBELEM_UNIQUE},
1273 {LY_STMT_PATTERN, type, 0},
1274 {LY_STMT_RANGE, type, YIN_SUBELEM_UNIQUE},
1275 {LY_STMT_REQUIRE_INSTANCE, type, YIN_SUBELEM_UNIQUE},
Michal Vasko608b0c02021-09-23 08:26:53 +02001276 {LY_STMT_TYPE, type, 0},
Radek Krejci0f969882020-08-21 16:56:47 +02001277 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1278 };
Michal Vaskob36053d2020-03-26 15:49:30 +01001279
1280 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1281 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &type->name, Y_PREF_IDENTIF_ARG, LY_STMT_TYPE));
Radek Krejcif13b87b2020-12-01 22:02:17 +01001282 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_TYPE, NULL, &type->exts);
David Sedlák374d2b32019-07-17 15:06:55 +02001283}
1284
David Sedlák1af868e2019-07-17 17:03:14 +02001285/**
1286 * @brief Parse max-elements element.
1287 *
1288 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák1af868e2019-07-17 17:03:14 +02001289 * @param[in,out] max Value to write to.
David Sedláka2dad212019-07-18 12:45:19 +02001290 * @param[in] flags Flags to write to.
David Sedlák1af868e2019-07-17 17:03:14 +02001291 * @param[in,out] exts Extension instances to add to.
1292 *
1293 * @return LY_ERR values.
1294 */
1295static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001296yin_parse_maxelements(struct lys_yin_parser_ctx *ctx, uint32_t *max, uint16_t *flags, struct lysp_ext_instance **exts)
David Sedlák1af868e2019-07-17 17:03:14 +02001297{
1298 const char *temp_val = NULL;
1299 char *ptr;
Michal Vasko73d77ab2021-07-23 12:45:55 +02001300 unsigned long long int num;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001301 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001302 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1303 };
David Sedlák374d2b32019-07-17 15:06:55 +02001304
David Sedlák1af868e2019-07-17 17:03:14 +02001305 *flags |= LYS_SET_MAX;
Michal Vaskob36053d2020-03-26 15:49:30 +01001306 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1307 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_MAX_ELEMENTS));
Michal Vasko69730152020-10-09 16:30:07 +02001308 if (!temp_val || (temp_val[0] == '\0') || (temp_val[0] == '0') || ((temp_val[0] != 'u') && !isdigit(temp_val[0]))) {
David Sedlák1538a842019-08-08 15:38:51 +02001309 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "max-elements");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001310 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlák1af868e2019-07-17 17:03:14 +02001311 return LY_EVALID;
1312 }
1313
1314 if (strcmp(temp_val, "unbounded")) {
1315 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02001316 num = strtoull(temp_val, &ptr, LY_BASE_DEC);
David Sedlák1af868e2019-07-17 17:03:14 +02001317 if (*ptr != '\0') {
David Sedlák1538a842019-08-08 15:38:51 +02001318 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "max-elements");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001319 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlák1af868e2019-07-17 17:03:14 +02001320 return LY_EVALID;
1321 }
1322 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlák1538a842019-08-08 15:38:51 +02001323 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_OOB_YIN, temp_val, "value", "max-elements");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001324 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlák1af868e2019-07-17 17:03:14 +02001325 return LY_EVALID;
1326 }
1327 *max = num;
1328 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01001329 lydict_remove(ctx->xmlctx->ctx, temp_val);
Radek Krejcif13b87b2020-12-01 22:02:17 +01001330 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_MAX_ELEMENTS, NULL, exts);
David Sedlák1af868e2019-07-17 17:03:14 +02001331}
David Sedlák374d2b32019-07-17 15:06:55 +02001332
1333/**
David Sedlákbf8a2b72019-08-14 16:48:10 +02001334 * @brief Parse min-elements element.
David Sedlák09e18c92019-07-18 11:17:11 +02001335 *
1336 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák09e18c92019-07-18 11:17:11 +02001337 * @param[in,out] min Value to write to.
David Sedláka2dad212019-07-18 12:45:19 +02001338 * @param[in] flags Flags to write to.
David Sedlák09e18c92019-07-18 11:17:11 +02001339 * @param[in,out] exts Extension instances to add to.
1340 *
1341 * @return LY_ERR values.
1342 */
1343static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001344yin_parse_minelements(struct lys_yin_parser_ctx *ctx, uint32_t *min, uint16_t *flags, struct lysp_ext_instance **exts)
David Sedlák09e18c92019-07-18 11:17:11 +02001345{
1346 const char *temp_val = NULL;
1347 char *ptr;
Michal Vasko73d77ab2021-07-23 12:45:55 +02001348 unsigned long long int num;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001349 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001350 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1351 };
David Sedlák09e18c92019-07-18 11:17:11 +02001352
1353 *flags |= LYS_SET_MIN;
Michal Vaskob36053d2020-03-26 15:49:30 +01001354 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1355 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_MIN_ELEMENTS));
David Sedlák09e18c92019-07-18 11:17:11 +02001356
Michal Vasko69730152020-10-09 16:30:07 +02001357 if (!temp_val || (temp_val[0] == '\0') || ((temp_val[0] == '0') && (temp_val[1] != '\0'))) {
David Sedlák1538a842019-08-08 15:38:51 +02001358 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "min-elements");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001359 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlák09e18c92019-07-18 11:17:11 +02001360 return LY_EVALID;
1361 }
1362
1363 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02001364 num = strtoull(temp_val, &ptr, LY_BASE_DEC);
David Sedlák09e18c92019-07-18 11:17:11 +02001365 if (ptr[0] != 0) {
David Sedlák1538a842019-08-08 15:38:51 +02001366 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "min-elements");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001367 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlák09e18c92019-07-18 11:17:11 +02001368 return LY_EVALID;
1369 }
Michal Vasko69730152020-10-09 16:30:07 +02001370 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlák1538a842019-08-08 15:38:51 +02001371 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_OOB_YIN, temp_val, "value", "min-elements");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001372 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlák09e18c92019-07-18 11:17:11 +02001373 return LY_EVALID;
1374 }
1375 *min = num;
Michal Vaskoe180ed02021-02-05 16:31:20 +01001376 lydict_remove(ctx->xmlctx->ctx, temp_val);
Radek Krejcif13b87b2020-12-01 22:02:17 +01001377 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_MIN_ELEMENTS, NULL, exts);
David Sedlák09e18c92019-07-18 11:17:11 +02001378}
1379
David Sedláka2dad212019-07-18 12:45:19 +02001380/**
1381 * @brief Parse min-elements or max-elements element.
1382 *
1383 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedláka2dad212019-07-18 12:45:19 +02001384 * @param[in] parent Identification of parent element.
1385 * @param[in] current Identification of current element.
1386 * @param[in] dest Where the parsed value and flags should be stored.
1387 *
1388 * @return LY_ERR values.
1389 */
David Sedlák09e18c92019-07-18 11:17:11 +02001390static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001391yin_parse_minmax(struct lys_yin_parser_ctx *ctx, enum ly_stmt parent, enum ly_stmt current, void *dest)
David Sedlák09e18c92019-07-18 11:17:11 +02001392{
Radek Krejcid6b76452019-09-03 17:03:03 +02001393 assert(current == LY_STMT_MAX_ELEMENTS || current == LY_STMT_MIN_ELEMENTS);
1394 assert(parent == LY_STMT_LEAF_LIST || parent == LY_STMT_REFINE || parent == LY_STMT_LIST || parent == LY_STMT_DEVIATE);
David Sedlák09e18c92019-07-18 11:17:11 +02001395 uint32_t *lim;
1396 uint16_t *flags;
1397 struct lysp_ext_instance **exts;
1398
Radek Krejcid6b76452019-09-03 17:03:03 +02001399 if (parent == LY_STMT_LEAF_LIST) {
1400 lim = (current == LY_STMT_MAX_ELEMENTS) ? &((struct lysp_node_leaflist *)dest)->max : &((struct lysp_node_leaflist *)dest)->min;
David Sedlák09e18c92019-07-18 11:17:11 +02001401 flags = &((struct lysp_node_leaflist *)dest)->flags;
1402 exts = &((struct lysp_node_leaflist *)dest)->exts;
Radek Krejcid6b76452019-09-03 17:03:03 +02001403 } else if (parent == LY_STMT_REFINE) {
1404 lim = (current == LY_STMT_MAX_ELEMENTS) ? &((struct lysp_refine *)dest)->max : &((struct lysp_refine *)dest)->min;
David Sedlák09e18c92019-07-18 11:17:11 +02001405 flags = &((struct lysp_refine *)dest)->flags;
1406 exts = &((struct lysp_refine *)dest)->exts;
Radek Krejcid6b76452019-09-03 17:03:03 +02001407 } else if (parent == LY_STMT_LIST) {
1408 lim = (current == LY_STMT_MAX_ELEMENTS) ? &((struct lysp_node_list *)dest)->max : &((struct lysp_node_list *)dest)->min;
David Sedlák09e18c92019-07-18 11:17:11 +02001409 flags = &((struct lysp_node_list *)dest)->flags;
1410 exts = &((struct lysp_node_list *)dest)->exts;
David Sedlák4ffcec82019-07-25 15:10:21 +02001411 } else {
1412 lim = ((struct minmax_dev_meta *)dest)->lim;
1413 flags = ((struct minmax_dev_meta *)dest)->flags;
1414 exts = ((struct minmax_dev_meta *)dest)->exts;
David Sedlák09e18c92019-07-18 11:17:11 +02001415 }
1416
Radek Krejcid6b76452019-09-03 17:03:03 +02001417 if (current == LY_STMT_MAX_ELEMENTS) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001418 LY_CHECK_RET(yin_parse_maxelements(ctx, lim, flags, exts));
David Sedlák09e18c92019-07-18 11:17:11 +02001419 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +01001420 LY_CHECK_RET(yin_parse_minelements(ctx, lim, flags, exts));
David Sedlák09e18c92019-07-18 11:17:11 +02001421 }
1422
1423 return LY_SUCCESS;
1424}
1425
1426/**
David Sedlákbf8a2b72019-08-14 16:48:10 +02001427 * @brief Parse ordered-by element.
David Sedláka2dad212019-07-18 12:45:19 +02001428 *
1429 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedláka2dad212019-07-18 12:45:19 +02001430 * @param[out] flags Flags to write to.
David Sedlákbf8a2b72019-08-14 16:48:10 +02001431 * @param[in,out] exts Extension instances to add to.
David Sedláka2dad212019-07-18 12:45:19 +02001432 *
1433 * @return LY_ERR values.
1434 */
1435static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001436yin_parse_orderedby(struct lys_yin_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
David Sedláka2dad212019-07-18 12:45:19 +02001437{
1438 const char *temp_val;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001439 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001440 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1441 };
David Sedláka2dad212019-07-18 12:45:19 +02001442
Michal Vaskob36053d2020-03-26 15:49:30 +01001443 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1444 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_ORDERED_BY));
David Sedláka2dad212019-07-18 12:45:19 +02001445 if (strcmp(temp_val, "system") == 0) {
1446 *flags |= LYS_ORDBY_SYSTEM;
1447 } else if (strcmp(temp_val, "user") == 0) {
1448 *flags |= LYS_ORDBY_USER;
1449 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02001450 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value",
Michal Vasko69730152020-10-09 16:30:07 +02001451 "ordered-by", "system", "user");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001452 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedláka2dad212019-07-18 12:45:19 +02001453 return LY_EVALID;
1454 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01001455 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedláka2dad212019-07-18 12:45:19 +02001456
Radek Krejcif13b87b2020-12-01 22:02:17 +01001457 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_ORDERED_BY, NULL, exts);
David Sedláka2dad212019-07-18 12:45:19 +02001458}
1459
1460/**
David Sedlákbf8a2b72019-08-14 16:48:10 +02001461 * @brief Parse any-data or any-xml element.
David Sedlák8a83bbb2019-07-18 14:46:00 +02001462 *
1463 * @param[in,out] ctx YIN parser context for logging and to store current state.
Radek Krejcid6b76452019-09-03 17:03:03 +02001464 * @param[in] any_kw Identification of current element, can be set to LY_STMT_ANYDATA or LY_STMT_ANYXML
David Sedlákad83cf92019-08-13 12:53:53 +02001465 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlák8a83bbb2019-07-18 14:46:00 +02001466 *
1467 * @return LY_ERR values.
1468 */
1469static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001470yin_parse_any(struct lys_yin_parser_ctx *ctx, enum ly_stmt any_kw, struct tree_node_meta *node_meta)
David Sedlák8a83bbb2019-07-18 14:46:00 +02001471{
David Sedlák8a83bbb2019-07-18 14:46:00 +02001472 struct lysp_node_anydata *any;
1473
David Sedlák8d552d62019-08-06 15:29:05 +02001474 /* create new sibling */
Michal Vaskob36053d2020-03-26 15:49:30 +01001475 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, any, next, LY_EMEM);
Radek Krejcid6b76452019-09-03 17:03:03 +02001476 any->nodetype = (any_kw == LY_STMT_ANYDATA) ? LYS_ANYDATA : LYS_ANYXML;
David Sedlák8a83bbb2019-07-18 14:46:00 +02001477 any->parent = node_meta->parent;
1478
David Sedlákbf8a2b72019-08-14 16:48:10 +02001479 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001480 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1481 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &any->name, Y_IDENTIF_ARG, any_kw));
David Sedlák8a83bbb2019-07-18 14:46:00 +02001482
Radek Krejcif13b87b2020-12-01 22:02:17 +01001483 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001484 {LY_STMT_CONFIG, &any->flags, YIN_SUBELEM_UNIQUE},
1485 {LY_STMT_DESCRIPTION, &any->dsc, YIN_SUBELEM_UNIQUE},
1486 {LY_STMT_IF_FEATURE, &any->iffeatures, 0},
1487 {LY_STMT_MANDATORY, &any->flags, YIN_SUBELEM_UNIQUE},
1488 {LY_STMT_MUST, &any->musts, 0},
1489 {LY_STMT_REFERENCE, &any->ref, YIN_SUBELEM_UNIQUE},
1490 {LY_STMT_STATUS, &any->flags, YIN_SUBELEM_UNIQUE},
1491 {LY_STMT_WHEN, &any->when, YIN_SUBELEM_UNIQUE},
1492 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1493 };
Michal Vasko69730152020-10-09 16:30:07 +02001494
Radek Krejcif13b87b2020-12-01 22:02:17 +01001495 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), any_kw, NULL, &any->exts);
David Sedlák8a83bbb2019-07-18 14:46:00 +02001496}
1497
1498/**
David Sedlák203ca3a2019-07-18 15:26:25 +02001499 * @brief parse leaf element.
1500 *
1501 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákad83cf92019-08-13 12:53:53 +02001502 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlák203ca3a2019-07-18 15:26:25 +02001503 *
1504 * @return LY_ERR values.
1505 */
1506static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001507yin_parse_leaf(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta)
David Sedlák203ca3a2019-07-18 15:26:25 +02001508{
David Sedlák203ca3a2019-07-18 15:26:25 +02001509 struct lysp_node_leaf *leaf;
1510
David Sedlák8d552d62019-08-06 15:29:05 +02001511 /* create structure new leaf */
Michal Vaskob36053d2020-03-26 15:49:30 +01001512 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, leaf, next, LY_EMEM);
David Sedlák203ca3a2019-07-18 15:26:25 +02001513 leaf->nodetype = LYS_LEAF;
1514 leaf->parent = node_meta->parent;
1515
David Sedlák203ca3a2019-07-18 15:26:25 +02001516 /* parser argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001517 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1518 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &leaf->name, Y_IDENTIF_ARG, LY_STMT_LEAF));
David Sedlák203ca3a2019-07-18 15:26:25 +02001519
1520 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001521 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001522 {LY_STMT_CONFIG, &leaf->flags, YIN_SUBELEM_UNIQUE},
1523 {LY_STMT_DEFAULT, &leaf->dflt, YIN_SUBELEM_UNIQUE},
1524 {LY_STMT_DESCRIPTION, &leaf->dsc, YIN_SUBELEM_UNIQUE},
1525 {LY_STMT_IF_FEATURE, &leaf->iffeatures, 0},
1526 {LY_STMT_MANDATORY, &leaf->flags, YIN_SUBELEM_UNIQUE},
1527 {LY_STMT_MUST, &leaf->musts, 0},
1528 {LY_STMT_REFERENCE, &leaf->ref, YIN_SUBELEM_UNIQUE},
1529 {LY_STMT_STATUS, &leaf->flags, YIN_SUBELEM_UNIQUE},
1530 {LY_STMT_TYPE, &leaf->type, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_MANDATORY},
1531 {LY_STMT_UNITS, &leaf->units, YIN_SUBELEM_UNIQUE},
1532 {LY_STMT_WHEN, &leaf->when, YIN_SUBELEM_UNIQUE},
1533 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1534 };
Michal Vasko69730152020-10-09 16:30:07 +02001535
Radek Krejcif13b87b2020-12-01 22:02:17 +01001536 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_LEAF, NULL, &leaf->exts);
David Sedlák203ca3a2019-07-18 15:26:25 +02001537}
1538
1539/**
David Sedlákc3da3ef2019-07-19 12:56:08 +02001540 * @brief Parse leaf-list element.
1541 *
1542 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákad83cf92019-08-13 12:53:53 +02001543 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlákc3da3ef2019-07-19 12:56:08 +02001544 *
1545 * @return LY_ERR values.
1546 */
1547static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001548yin_parse_leaflist(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta)
David Sedlákc3da3ef2019-07-19 12:56:08 +02001549{
David Sedlákc3da3ef2019-07-19 12:56:08 +02001550 struct lysp_node_leaflist *llist;
David Sedlákc3da3ef2019-07-19 12:56:08 +02001551
Michal Vaskob36053d2020-03-26 15:49:30 +01001552 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, llist, next, LY_EMEM);
David Sedlák8d552d62019-08-06 15:29:05 +02001553
David Sedlákc3da3ef2019-07-19 12:56:08 +02001554 llist->nodetype = LYS_LEAFLIST;
1555 llist->parent = node_meta->parent;
1556
David Sedlákc3da3ef2019-07-19 12:56:08 +02001557 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001558 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1559 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &llist->name, Y_IDENTIF_ARG, LY_STMT_LEAF_LIST));
David Sedlákc3da3ef2019-07-19 12:56:08 +02001560
1561 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001562 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001563 {LY_STMT_CONFIG, &llist->flags, YIN_SUBELEM_UNIQUE},
1564 {LY_STMT_DEFAULT, &llist->dflts, 0},
1565 {LY_STMT_DESCRIPTION, &llist->dsc, YIN_SUBELEM_UNIQUE},
1566 {LY_STMT_IF_FEATURE, &llist->iffeatures, 0},
1567 {LY_STMT_MAX_ELEMENTS, llist, YIN_SUBELEM_UNIQUE},
1568 {LY_STMT_MIN_ELEMENTS, llist, YIN_SUBELEM_UNIQUE},
1569 {LY_STMT_MUST, &llist->musts, 0},
1570 {LY_STMT_ORDERED_BY, &llist->flags, YIN_SUBELEM_UNIQUE},
1571 {LY_STMT_REFERENCE, &llist->ref, YIN_SUBELEM_UNIQUE},
1572 {LY_STMT_STATUS, &llist->flags, YIN_SUBELEM_UNIQUE},
1573 {LY_STMT_TYPE, &llist->type, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_MANDATORY},
1574 {LY_STMT_UNITS, &llist->units, YIN_SUBELEM_UNIQUE},
1575 {LY_STMT_WHEN, &llist->when, YIN_SUBELEM_UNIQUE},
1576 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1577 };
Michal Vasko69730152020-10-09 16:30:07 +02001578
Radek Krejcif13b87b2020-12-01 22:02:17 +01001579 LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_LEAF_LIST, NULL, &llist->exts));
David Sedlákc3da3ef2019-07-19 12:56:08 +02001580
David Sedlákbf8a2b72019-08-14 16:48:10 +02001581 /* check invalid combination of subelements */
David Sedlákc3da3ef2019-07-19 12:56:08 +02001582 if ((llist->min) && (llist->dflts)) {
David Sedlák1538a842019-08-08 15:38:51 +02001583 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INCHILDSTMSCOMB_YIN, "min-elements", "default", "leaf-list");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001584 return LY_EVALID;
1585 }
Michal Vasko69730152020-10-09 16:30:07 +02001586 if (llist->max && (llist->min > llist->max)) {
David Sedlák1538a842019-08-08 15:38:51 +02001587 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_MINMAX, llist->min, llist->max);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001588 return LY_EVALID;
1589 }
1590
1591 return LY_SUCCESS;
1592}
1593
1594/**
David Sedlák04e17b22019-07-19 15:29:48 +02001595 * @brief Parse typedef element.
1596 *
1597 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák05404f62019-07-24 14:11:53 +02001598 * @param[in] typedef_meta Meta information about parent node and typedefs to add to.
David Sedlák04e17b22019-07-19 15:29:48 +02001599 *
1600 * @return LY_ERR values.
1601 */
1602static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001603yin_parse_typedef(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *typedef_meta)
David Sedlák04e17b22019-07-19 15:29:48 +02001604{
1605 struct lysp_tpdf *tpdf;
David Sedlákbf8a2b72019-08-14 16:48:10 +02001606 struct lysp_tpdf **tpdfs = (struct lysp_tpdf **)typedef_meta->nodes;
Michal Vasko69730152020-10-09 16:30:07 +02001607
Michal Vaskob36053d2020-03-26 15:49:30 +01001608 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *tpdfs, tpdf, LY_EMEM);
David Sedlák04e17b22019-07-19 15:29:48 +02001609
1610 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001611 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1612 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &tpdf->name, Y_IDENTIF_ARG, LY_STMT_TYPEDEF));
David Sedlák04e17b22019-07-19 15:29:48 +02001613
1614 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001615 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001616 {LY_STMT_DEFAULT, &tpdf->dflt, YIN_SUBELEM_UNIQUE},
1617 {LY_STMT_DESCRIPTION, &tpdf->dsc, YIN_SUBELEM_UNIQUE},
1618 {LY_STMT_REFERENCE, &tpdf->ref, YIN_SUBELEM_UNIQUE},
1619 {LY_STMT_STATUS, &tpdf->flags, YIN_SUBELEM_UNIQUE},
1620 {LY_STMT_TYPE, &tpdf->type, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_MANDATORY},
1621 {LY_STMT_UNITS, &tpdf->units, YIN_SUBELEM_UNIQUE},
1622 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1623 };
Michal Vasko69730152020-10-09 16:30:07 +02001624
Radek Krejcif13b87b2020-12-01 22:02:17 +01001625 LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_TYPEDEF, NULL, &tpdf->exts));
David Sedlák04e17b22019-07-19 15:29:48 +02001626
1627 /* store data for collision check */
aPiecek75b83a02021-06-29 10:34:38 +02001628 if (typedef_meta->parent) {
aPiecek8d4e75d2021-06-24 14:47:06 +02001629 assert(ctx->main_ctx);
1630 LY_CHECK_RET(ly_set_add(&ctx->main_ctx->tpdfs_nodes, typedef_meta->parent, 0, NULL));
David Sedlák04e17b22019-07-19 15:29:48 +02001631 }
1632
1633 return LY_SUCCESS;
1634}
1635
1636/**
David Sedlákd2d676a2019-07-22 11:28:19 +02001637 * @brief Parse refine element.
1638 *
1639 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákd2d676a2019-07-22 11:28:19 +02001640 * @param[in,out] refines Refines to add to.
1641 *
1642 * @return LY_ERR values.
1643 */
1644static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001645yin_parse_refine(struct lys_yin_parser_ctx *ctx, struct lysp_refine **refines)
David Sedlákd2d676a2019-07-22 11:28:19 +02001646{
1647 struct lysp_refine *rf;
1648
1649 /* allocate new refine */
Michal Vaskob36053d2020-03-26 15:49:30 +01001650 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *refines, rf, LY_EMEM);
David Sedlákd2d676a2019-07-22 11:28:19 +02001651
1652 /* parse attribute */
Michal Vaskob36053d2020-03-26 15:49:30 +01001653 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1654 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_TARGET_NODE, &rf->nodeid, Y_STR_ARG, LY_STMT_REFINE));
1655 CHECK_NONEMPTY(ctx, strlen(rf->nodeid), "refine");
David Sedlákd2d676a2019-07-22 11:28:19 +02001656
1657 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001658 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001659 {LY_STMT_CONFIG, &rf->flags, YIN_SUBELEM_UNIQUE},
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001660 {LY_STMT_DEFAULT, &rf->dflts, 0},
Radek Krejci0f969882020-08-21 16:56:47 +02001661 {LY_STMT_DESCRIPTION, &rf->dsc, YIN_SUBELEM_UNIQUE},
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001662 {LY_STMT_IF_FEATURE, &rf->iffeatures, 0},
Radek Krejci0f969882020-08-21 16:56:47 +02001663 {LY_STMT_MANDATORY, &rf->flags, YIN_SUBELEM_UNIQUE},
1664 {LY_STMT_MAX_ELEMENTS, rf, YIN_SUBELEM_UNIQUE},
1665 {LY_STMT_MIN_ELEMENTS, rf, YIN_SUBELEM_UNIQUE},
1666 {LY_STMT_MUST, &rf->musts, 0},
1667 {LY_STMT_PRESENCE, &rf->presence, YIN_SUBELEM_UNIQUE},
1668 {LY_STMT_REFERENCE, &rf->ref, YIN_SUBELEM_UNIQUE},
1669 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1670 };
Michal Vasko69730152020-10-09 16:30:07 +02001671
Radek Krejcif13b87b2020-12-01 22:02:17 +01001672 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_REFINE, NULL, &rf->exts);
David Sedlákd2d676a2019-07-22 11:28:19 +02001673}
1674
1675/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02001676 * @brief Parse uses element.
1677 *
1678 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákad83cf92019-08-13 12:53:53 +02001679 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlák0d6de5a2019-07-22 13:25:44 +02001680 *
1681 * @return LY_ERR values.
1682 */
1683static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001684yin_parse_uses(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta)
David Sedlák0d6de5a2019-07-22 13:25:44 +02001685{
David Sedlák0d6de5a2019-07-22 13:25:44 +02001686 struct lysp_node_uses *uses;
1687
David Sedlák8d552d62019-08-06 15:29:05 +02001688 /* create new uses */
Michal Vaskob36053d2020-03-26 15:49:30 +01001689 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, uses, next, LY_EMEM);
David Sedlák0d6de5a2019-07-22 13:25:44 +02001690 uses->nodetype = LYS_USES;
1691 uses->parent = node_meta->parent;
1692
David Sedlák0d6de5a2019-07-22 13:25:44 +02001693 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001694 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1695 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &uses->name, Y_PREF_IDENTIF_ARG, LY_STMT_USES));
David Sedlák0d6de5a2019-07-22 13:25:44 +02001696
1697 /* parse content */
David Sedlák6881b512019-08-13 12:52:00 +02001698 struct tree_node_meta augments = {(struct lysp_node *)uses, (struct lysp_node **)&uses->augments};
Radek Krejcif13b87b2020-12-01 22:02:17 +01001699 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001700 {LY_STMT_AUGMENT, &augments, 0},
1701 {LY_STMT_DESCRIPTION, &uses->dsc, YIN_SUBELEM_UNIQUE},
1702 {LY_STMT_IF_FEATURE, &uses->iffeatures, 0},
1703 {LY_STMT_REFERENCE, &uses->ref, YIN_SUBELEM_UNIQUE},
1704 {LY_STMT_REFINE, &uses->refines, 0},
1705 {LY_STMT_STATUS, &uses->flags, YIN_SUBELEM_UNIQUE},
1706 {LY_STMT_WHEN, &uses->when, YIN_SUBELEM_UNIQUE},
1707 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1708 };
Michal Vasko69730152020-10-09 16:30:07 +02001709
Radek Krejcif13b87b2020-12-01 22:02:17 +01001710 LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_USES, NULL, &uses->exts));
David Sedlák0d6de5a2019-07-22 13:25:44 +02001711
1712 return LY_SUCCESS;
1713}
1714
1715/**
David Sedlákaa854b02019-07-22 14:17:10 +02001716 * @brief Parse revision element.
1717 *
1718 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákaa854b02019-07-22 14:17:10 +02001719 * @param[in,out] revs Parsed revisions to add to.
1720 *
1721 * @return LY_ERR values.
1722 */
1723static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001724yin_parse_revision(struct lys_yin_parser_ctx *ctx, struct lysp_revision **revs)
David Sedlákaa854b02019-07-22 14:17:10 +02001725{
1726 struct lysp_revision *rev;
1727 const char *temp_date = NULL;
1728
1729 /* allocate new reivison */
Michal Vaskob36053d2020-03-26 15:49:30 +01001730 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *revs, rev, LY_EMEM);
David Sedlákaa854b02019-07-22 14:17:10 +02001731
1732 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001733 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1734 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_DATE, &temp_date, Y_STR_ARG, LY_STMT_REVISION));
David Sedlákaa854b02019-07-22 14:17:10 +02001735 /* check value */
1736 if (lysp_check_date((struct lys_parser_ctx *)ctx, temp_date, strlen(temp_date), "revision")) {
Michal Vaskoe180ed02021-02-05 16:31:20 +01001737 lydict_remove(ctx->xmlctx->ctx, temp_date);
David Sedlákaa854b02019-07-22 14:17:10 +02001738 return LY_EVALID;
1739 }
Radek Krejci36642162021-01-12 16:49:22 +01001740 memcpy(rev->date, temp_date, LY_REV_SIZE);
Michal Vaskoe180ed02021-02-05 16:31:20 +01001741 lydict_remove(ctx->xmlctx->ctx, temp_date);
David Sedlákaa854b02019-07-22 14:17:10 +02001742
1743 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001744 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001745 {LY_STMT_DESCRIPTION, &rev->dsc, YIN_SUBELEM_UNIQUE},
1746 {LY_STMT_REFERENCE, &rev->ref, YIN_SUBELEM_UNIQUE},
1747 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1748 };
Michal Vasko69730152020-10-09 16:30:07 +02001749
Radek Krejcif13b87b2020-12-01 22:02:17 +01001750 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_REVISION, NULL, &rev->exts);
David Sedlákaa854b02019-07-22 14:17:10 +02001751}
1752
David Sedlák5e13dea2019-07-22 16:06:45 +02001753/**
1754 * @brief Parse include element.
1755 *
1756 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák5e13dea2019-07-22 16:06:45 +02001757 * @param[in,out] inc_meta Meta informatinou about module/submodule name and includes to add to.
1758 *
1759 * @return LY_ERR values.
1760 */
David Sedlák0c2bab92019-07-22 15:33:19 +02001761static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001762yin_parse_include(struct lys_yin_parser_ctx *ctx, struct include_meta *inc_meta)
David Sedlák0c2bab92019-07-22 15:33:19 +02001763{
1764 struct lysp_include *inc;
1765
1766 /* allocate new include */
Michal Vaskob36053d2020-03-26 15:49:30 +01001767 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *inc_meta->includes, inc, LY_EMEM);
David Sedlák0c2bab92019-07-22 15:33:19 +02001768
1769 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001770 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1771 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_MODULE, &inc->name, Y_IDENTIF_ARG, LY_STMT_INCLUDE));
David Sedlák0c2bab92019-07-22 15:33:19 +02001772
1773 /* submodules share the namespace with the module names, so there must not be
1774 * a module of the same name in the context, no need for revision matching */
Michal Vaskob36053d2020-03-26 15:49:30 +01001775 if (!strcmp(inc_meta->name, inc->name) || ly_ctx_get_module_latest(ctx->xmlctx->ctx, inc->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01001776 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_NAME2_COL, "module", "submodule", inc->name);
David Sedlák0c2bab92019-07-22 15:33:19 +02001777 return LY_EVALID;
1778 }
1779
1780 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001781 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001782 {LY_STMT_DESCRIPTION, &inc->dsc, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_VER2},
1783 {LY_STMT_REFERENCE, &inc->ref, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_VER2},
1784 {LY_STMT_REVISION_DATE, &inc->rev, YIN_SUBELEM_UNIQUE},
1785 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1786 };
Michal Vasko69730152020-10-09 16:30:07 +02001787
Radek Krejcif13b87b2020-12-01 22:02:17 +01001788 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_INCLUDE, NULL, &inc->exts);
David Sedlák0c2bab92019-07-22 15:33:19 +02001789}
1790
David Sedlákaa854b02019-07-22 14:17:10 +02001791/**
David Sedlákbf8a2b72019-08-14 16:48:10 +02001792 * @brief Parse revision-date element.
David Sedlákdfbbb442019-08-06 16:33:21 +02001793 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001794 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001795 * @param[in,out] rev Array to store the parsed value in.
1796 * @param[in,out] exts Extension instances to add to.
1797 *
1798 * @return LY_ERR values.
1799 */
1800static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001801yin_parse_revision_date(struct lys_yin_parser_ctx *ctx, char *rev, struct lysp_ext_instance **exts)
David Sedlákdfbbb442019-08-06 16:33:21 +02001802{
1803 const char *temp_rev;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001804 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001805 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1806 };
David Sedlákdfbbb442019-08-06 16:33:21 +02001807
Michal Vaskob36053d2020-03-26 15:49:30 +01001808 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1809 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_DATE, &temp_rev, Y_STR_ARG, LY_STMT_REVISION_DATE));
David Sedlákdfbbb442019-08-06 16:33:21 +02001810 LY_CHECK_ERR_RET(lysp_check_date((struct lys_parser_ctx *)ctx, temp_rev, strlen(temp_rev), "revision-date") != LY_SUCCESS,
Michal Vaskoe180ed02021-02-05 16:31:20 +01001811 lydict_remove(ctx->xmlctx->ctx, temp_rev), LY_EVALID);
David Sedlákdfbbb442019-08-06 16:33:21 +02001812
1813 strcpy(rev, temp_rev);
Michal Vaskoe180ed02021-02-05 16:31:20 +01001814 lydict_remove(ctx->xmlctx->ctx, temp_rev);
David Sedlákdfbbb442019-08-06 16:33:21 +02001815
Radek Krejcif13b87b2020-12-01 22:02:17 +01001816 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_REVISION_DATE, NULL, exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02001817}
1818
1819/**
1820 * @brief Parse config element.
1821 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001822 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001823 * @param[in,out] flags Flags to add to.
1824 * @param[in,out] exts Extension instances to add to.
1825 *
1826 * @return LY_ERR values.
1827 */
1828static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001829yin_parse_config(struct lys_yin_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
David Sedlákdfbbb442019-08-06 16:33:21 +02001830{
1831 const char *temp_val = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001832 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001833 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1834 };
David Sedlákdfbbb442019-08-06 16:33:21 +02001835
Michal Vaskob36053d2020-03-26 15:49:30 +01001836 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1837 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_CONFIG));
David Sedlákdfbbb442019-08-06 16:33:21 +02001838 if (strcmp(temp_val, "true") == 0) {
1839 *flags |= LYS_CONFIG_W;
1840 } else if (strcmp(temp_val, "false") == 0) {
1841 *flags |= LYS_CONFIG_R;
1842 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02001843 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value", "config",
Michal Vasko69730152020-10-09 16:30:07 +02001844 "true", "false");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001845 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákdfbbb442019-08-06 16:33:21 +02001846 return LY_EVALID;
1847 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01001848 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákdfbbb442019-08-06 16:33:21 +02001849
Radek Krejcif13b87b2020-12-01 22:02:17 +01001850 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_CONFIG, NULL, exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02001851}
1852
1853/**
1854 * @brief Parse yang-version element.
1855 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001856 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001857 * @param[out] version Storage for the parsed information.
David Sedlákbf8a2b72019-08-14 16:48:10 +02001858 * @param[in,out] exts Extension instances to add to.
David Sedlákdfbbb442019-08-06 16:33:21 +02001859 *
1860 * @return LY_ERR values.
1861 */
1862static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001863yin_parse_yangversion(struct lys_yin_parser_ctx *ctx, uint8_t *version, struct lysp_ext_instance **exts)
David Sedlákdfbbb442019-08-06 16:33:21 +02001864{
1865 const char *temp_version = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001866 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001867 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1868 };
David Sedlákdfbbb442019-08-06 16:33:21 +02001869
Michal Vaskob36053d2020-03-26 15:49:30 +01001870 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1871 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_version, Y_STR_ARG, LY_STMT_YANG_VERSION));
Radek Krejci96e48da2020-09-04 13:18:06 +02001872 if (strcmp(temp_version, "1") == 0) {
David Sedlákdfbbb442019-08-06 16:33:21 +02001873 *version = LYS_VERSION_1_0;
1874 } else if (strcmp(temp_version, "1.1") == 0) {
1875 *version = LYS_VERSION_1_1;
1876 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02001877 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_version, "value",
Michal Vasko69730152020-10-09 16:30:07 +02001878 "yang-version", "1", "1.1");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001879 lydict_remove(ctx->xmlctx->ctx, temp_version);
David Sedlákdfbbb442019-08-06 16:33:21 +02001880 return LY_EVALID;
1881 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01001882 lydict_remove(ctx->xmlctx->ctx, temp_version);
David Sedlákdfbbb442019-08-06 16:33:21 +02001883
Radek Krejcif13b87b2020-12-01 22:02:17 +01001884 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_YANG_VERSION, NULL, exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02001885}
1886
1887/**
1888 * @brief Parse import element.
1889 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001890 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001891 * @param[in,out] imp_meta Meta information about prefix and imports to add to.
1892 *
1893 * @return LY_ERR values.
1894 */
1895static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001896yin_parse_import(struct lys_yin_parser_ctx *ctx, struct import_meta *imp_meta)
David Sedlákdfbbb442019-08-06 16:33:21 +02001897{
1898 struct lysp_import *imp;
Michal Vasko69730152020-10-09 16:30:07 +02001899
David Sedlákdfbbb442019-08-06 16:33:21 +02001900 /* allocate new element in sized array for import */
Michal Vaskob36053d2020-03-26 15:49:30 +01001901 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *imp_meta->imports, imp, LY_EMEM);
David Sedlákdfbbb442019-08-06 16:33:21 +02001902
Radek Krejcif13b87b2020-12-01 22:02:17 +01001903 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001904 {LY_STMT_DESCRIPTION, &imp->dsc, YIN_SUBELEM_UNIQUE},
1905 {LY_STMT_PREFIX, &imp->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
1906 {LY_STMT_REFERENCE, &imp->ref, YIN_SUBELEM_UNIQUE},
1907 {LY_STMT_REVISION_DATE, imp->rev, YIN_SUBELEM_UNIQUE},
1908 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1909 };
David Sedlákdfbbb442019-08-06 16:33:21 +02001910
1911 /* parse import attributes */
Michal Vaskob36053d2020-03-26 15:49:30 +01001912 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1913 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_MODULE, &imp->name, Y_IDENTIF_ARG, LY_STMT_IMPORT));
Radek Krejcif13b87b2020-12-01 22:02:17 +01001914 LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_IMPORT, NULL, &imp->exts));
David Sedlákdfbbb442019-08-06 16:33:21 +02001915 /* check prefix validity */
1916 LY_CHECK_RET(lysp_check_prefix((struct lys_parser_ctx *)ctx, *imp_meta->imports, imp_meta->prefix, &imp->prefix), LY_EVALID);
1917
1918 return LY_SUCCESS;
1919}
1920
1921/**
1922 * @brief Parse mandatory element.
1923 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001924 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001925 * @param[in,out] flags Flags to add to.
1926 * @param[in,out] exts Extension instances to add to.
1927 *
1928 * @return LY_ERR values.
1929 */
1930static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001931yin_parse_mandatory(struct lys_yin_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
David Sedlákdfbbb442019-08-06 16:33:21 +02001932{
1933 const char *temp_val = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001934 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001935 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1936 };
David Sedlákdfbbb442019-08-06 16:33:21 +02001937
Michal Vaskob36053d2020-03-26 15:49:30 +01001938 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1939 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_MANDATORY));
David Sedlákdfbbb442019-08-06 16:33:21 +02001940 if (strcmp(temp_val, "true") == 0) {
1941 *flags |= LYS_MAND_TRUE;
1942 } else if (strcmp(temp_val, "false") == 0) {
1943 *flags |= LYS_MAND_FALSE;
1944 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02001945 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value",
Michal Vasko69730152020-10-09 16:30:07 +02001946 "mandatory", "true", "false");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001947 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákdfbbb442019-08-06 16:33:21 +02001948 return LY_EVALID;
1949 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01001950 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákdfbbb442019-08-06 16:33:21 +02001951
Radek Krejcif13b87b2020-12-01 22:02:17 +01001952 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_MANDATORY, NULL, exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02001953}
1954
1955/**
1956 * @brief Parse status element.
1957 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001958 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001959 * @param[in,out] flags Flags to add to.
1960 * @param[in,out] exts Extension instances to add to.
1961 *
1962 * @return LY_ERR values.
1963 */
1964static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001965yin_parse_status(struct lys_yin_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
David Sedlákdfbbb442019-08-06 16:33:21 +02001966{
1967 const char *value = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001968 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001969 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1970 };
David Sedlákdfbbb442019-08-06 16:33:21 +02001971
Michal Vaskob36053d2020-03-26 15:49:30 +01001972 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1973 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &value, Y_STR_ARG, LY_STMT_STATUS));
David Sedlákdfbbb442019-08-06 16:33:21 +02001974 if (strcmp(value, "current") == 0) {
1975 *flags |= LYS_STATUS_CURR;
1976 } else if (strcmp(value, "deprecated") == 0) {
1977 *flags |= LYS_STATUS_DEPRC;
1978 } else if (strcmp(value, "obsolete") == 0) {
1979 *flags |= LYS_STATUS_OBSLT;
1980 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02001981 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS3, value, "value",
Michal Vasko69730152020-10-09 16:30:07 +02001982 "status", "current", "deprecated", "obsolete");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001983 lydict_remove(ctx->xmlctx->ctx, value);
David Sedlákdfbbb442019-08-06 16:33:21 +02001984 return LY_EVALID;
1985 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01001986 lydict_remove(ctx->xmlctx->ctx, value);
David Sedlákdfbbb442019-08-06 16:33:21 +02001987
Radek Krejcif13b87b2020-12-01 22:02:17 +01001988 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_STATUS, NULL, exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02001989}
1990
1991/**
1992 * @brief Parse when element.
1993 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001994 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001995 * @param[out] when_p When pointer to parse to.
1996 */
1997static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001998yin_parse_when(struct lys_yin_parser_ctx *ctx, struct lysp_when **when_p)
David Sedlákdfbbb442019-08-06 16:33:21 +02001999{
2000 struct lysp_when *when;
David Sedláka56e0012019-08-15 13:21:25 +02002001 LY_ERR ret = LY_SUCCESS;
2002
David Sedlákdfbbb442019-08-06 16:33:21 +02002003 when = calloc(1, sizeof *when);
Michal Vaskob36053d2020-03-26 15:49:30 +01002004 LY_CHECK_ERR_RET(!when, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
Radek Krejcif6923e82020-07-02 16:36:53 +02002005
2006 ret = lyxml_ctx_next(ctx->xmlctx);
2007 LY_CHECK_ERR_RET(ret, free(when), ret);
2008
Michal Vaskob36053d2020-03-26 15:49:30 +01002009 ret = yin_parse_attribute(ctx, YIN_ARG_CONDITION, &when->cond, Y_STR_ARG, LY_STMT_WHEN);
David Sedláka56e0012019-08-15 13:21:25 +02002010 LY_CHECK_ERR_RET(ret, free(when), ret);
2011
David Sedlákdfbbb442019-08-06 16:33:21 +02002012 *when_p = when;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002013 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002014 {LY_STMT_DESCRIPTION, &when->dsc, YIN_SUBELEM_UNIQUE},
2015 {LY_STMT_REFERENCE, &when->ref, YIN_SUBELEM_UNIQUE},
2016 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
2017 };
David Sedlákdfbbb442019-08-06 16:33:21 +02002018
Radek Krejcif13b87b2020-12-01 22:02:17 +01002019 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_WHEN, NULL, &when->exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02002020}
2021
2022/**
2023 * @brief Parse yin-elemenet element.
2024 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02002025 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02002026 * @param[in,out] data Data to read from, always moved to currently handled position.
2027 * @param[in,out] flags Flags to add to.
David Sedlákbf8a2b72019-08-14 16:48:10 +02002028 * @prama[in,out] exts Extension instances to add to.
David Sedlákdfbbb442019-08-06 16:33:21 +02002029 *
2030 * @return LY_ERR values.
2031 */
2032static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002033yin_parse_yin_element(struct lys_yin_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
David Sedlákdfbbb442019-08-06 16:33:21 +02002034{
2035 const char *temp_val = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002036 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002037 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
2038 };
David Sedlákdfbbb442019-08-06 16:33:21 +02002039
Michal Vaskob36053d2020-03-26 15:49:30 +01002040 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2041 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_YIN_ELEMENT));
David Sedlákdfbbb442019-08-06 16:33:21 +02002042 if (strcmp(temp_val, "true") == 0) {
2043 *flags |= LYS_YINELEM_TRUE;
2044 } else if (strcmp(temp_val, "false") == 0) {
2045 *flags |= LYS_YINELEM_FALSE;
2046 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02002047 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value",
Michal Vasko69730152020-10-09 16:30:07 +02002048 "yin-element", "true", "false");
Michal Vaskoe180ed02021-02-05 16:31:20 +01002049 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákdfbbb442019-08-06 16:33:21 +02002050 return LY_EVALID;
2051 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01002052 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákdfbbb442019-08-06 16:33:21 +02002053
Radek Krejcif13b87b2020-12-01 22:02:17 +01002054 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_YIN_ELEMENT, NULL, exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02002055}
2056
2057/**
2058 * @brief Parse argument element.
2059 *
Michal Vaskob36053d2020-03-26 15:49:30 +01002060 * @param[in,out] xmlctx Xml context.
David Sedlákbf8a2b72019-08-14 16:48:10 +02002061 * @param[in,out] arg_meta Meta information about destionation of parsed data.
2062 * @param[in,out] exts Extension instances to add to.
David Sedlákdfbbb442019-08-06 16:33:21 +02002063 *
2064 * @return LY_ERR values.
2065 */
2066static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002067yin_parse_argument(struct lys_yin_parser_ctx *ctx, struct yin_argument_meta *arg_meta, struct lysp_ext_instance **exts)
David Sedlákdfbbb442019-08-06 16:33:21 +02002068{
Radek Krejcif13b87b2020-12-01 22:02:17 +01002069 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002070 {LY_STMT_YIN_ELEMENT, arg_meta->flags, YIN_SUBELEM_UNIQUE},
2071 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
2072 };
David Sedlákdfbbb442019-08-06 16:33:21 +02002073
Michal Vaskob36053d2020-03-26 15:49:30 +01002074 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2075 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, arg_meta->argument, Y_IDENTIF_ARG, LY_STMT_ARGUMENT));
David Sedlákdfbbb442019-08-06 16:33:21 +02002076
Radek Krejcif13b87b2020-12-01 22:02:17 +01002077 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_ARGUMENT, NULL, exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02002078}
2079
2080/**
2081 * @brief Parse the extension statement.
2082 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02002083 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02002084 * @param[in,out] extensions Extensions to add to.
2085 *
2086 * @return LY_ERR values.
2087 */
2088static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002089yin_parse_extension(struct lys_yin_parser_ctx *ctx, struct lysp_ext **extensions)
David Sedlákdfbbb442019-08-06 16:33:21 +02002090{
2091 struct lysp_ext *ex;
Michal Vasko69730152020-10-09 16:30:07 +02002092
Michal Vaskob36053d2020-03-26 15:49:30 +01002093 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *extensions, ex, LY_EMEM);
2094 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2095 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &ex->name, Y_IDENTIF_ARG, LY_STMT_EXTENSION));
David Sedlákdfbbb442019-08-06 16:33:21 +02002096
Radek Krejci9f87b0c2021-03-05 14:45:26 +01002097 struct yin_argument_meta arg_info = {&ex->flags, &ex->argname};
Radek Krejcif13b87b2020-12-01 22:02:17 +01002098 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002099 {LY_STMT_ARGUMENT, &arg_info, YIN_SUBELEM_UNIQUE},
2100 {LY_STMT_DESCRIPTION, &ex->dsc, YIN_SUBELEM_UNIQUE},
2101 {LY_STMT_REFERENCE, &ex->ref, YIN_SUBELEM_UNIQUE},
2102 {LY_STMT_STATUS, &ex->flags, YIN_SUBELEM_UNIQUE},
2103 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
2104 };
David Sedlákdfbbb442019-08-06 16:33:21 +02002105
Radek Krejcif13b87b2020-12-01 22:02:17 +01002106 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_EXTENSION, NULL, &ex->exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02002107}
2108
2109/**
David Sedlák5e13dea2019-07-22 16:06:45 +02002110 * @brief Parse feature element.
2111 *
2112 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák5e13dea2019-07-22 16:06:45 +02002113 * @param[in,out] features Features to add to.
2114 *
2115 * @return LY_ERR values.
2116 */
2117static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002118yin_parse_feature(struct lys_yin_parser_ctx *ctx, struct lysp_feature **features)
David Sedlák5e13dea2019-07-22 16:06:45 +02002119{
2120 struct lysp_feature *feat;
2121
2122 /* allocate new feature */
Michal Vaskob36053d2020-03-26 15:49:30 +01002123 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *features, feat, LY_EMEM);
David Sedlák5e13dea2019-07-22 16:06:45 +02002124
2125 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002126 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2127 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &feat->name, Y_IDENTIF_ARG, LY_STMT_FEATURE));
David Sedlák5e13dea2019-07-22 16:06:45 +02002128
2129 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002130 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002131 {LY_STMT_DESCRIPTION, &feat->dsc, YIN_SUBELEM_UNIQUE},
2132 {LY_STMT_IF_FEATURE, &feat->iffeatures, 0},
2133 {LY_STMT_REFERENCE, &feat->ref, YIN_SUBELEM_UNIQUE},
2134 {LY_STMT_STATUS, &feat->flags, YIN_SUBELEM_UNIQUE},
2135 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
2136 };
Michal Vasko69730152020-10-09 16:30:07 +02002137
Radek Krejcif13b87b2020-12-01 22:02:17 +01002138 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_FEATURE, NULL, &feat->exts);
David Sedlák5e13dea2019-07-22 16:06:45 +02002139}
2140
2141/**
David Sedlák28794f22019-07-22 16:45:00 +02002142 * @brief Parse identity element.
2143 *
2144 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák28794f22019-07-22 16:45:00 +02002145 * @param[in,out] identities Identities to add to.
2146 *
2147 * @return LY_ERR values.
2148 */
2149static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002150yin_parse_identity(struct lys_yin_parser_ctx *ctx, struct lysp_ident **identities)
David Sedlák28794f22019-07-22 16:45:00 +02002151{
2152 struct lysp_ident *ident;
2153
2154 /* allocate new identity */
Michal Vaskob36053d2020-03-26 15:49:30 +01002155 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *identities, ident, LY_EMEM);
David Sedlák28794f22019-07-22 16:45:00 +02002156
2157 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002158 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2159 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &ident->name, Y_IDENTIF_ARG, LY_STMT_IDENTITY));
David Sedlák28794f22019-07-22 16:45:00 +02002160
2161 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002162 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002163 {LY_STMT_BASE, &ident->bases, 0},
2164 {LY_STMT_DESCRIPTION, &ident->dsc, YIN_SUBELEM_UNIQUE},
2165 {LY_STMT_IF_FEATURE, &ident->iffeatures, YIN_SUBELEM_VER2},
2166 {LY_STMT_REFERENCE, &ident->ref, YIN_SUBELEM_UNIQUE},
2167 {LY_STMT_STATUS, &ident->flags, YIN_SUBELEM_UNIQUE},
2168 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
2169 };
Michal Vasko69730152020-10-09 16:30:07 +02002170
Radek Krejcif13b87b2020-12-01 22:02:17 +01002171 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_IDENTITY, NULL, &ident->exts);
David Sedlák28794f22019-07-22 16:45:00 +02002172}
2173
2174/**
David Sedlákaf536aa2019-07-23 13:42:23 +02002175 * @brief Parse list element.
2176 *
2177 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákad83cf92019-08-13 12:53:53 +02002178 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlákaf536aa2019-07-23 13:42:23 +02002179 *
2180 * @return LY_ERR values.
2181 */
2182static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002183yin_parse_list(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta)
David Sedlákaf536aa2019-07-23 13:42:23 +02002184{
David Sedlákaf536aa2019-07-23 13:42:23 +02002185 struct lysp_node_list *list;
David Sedlák81497a32019-08-13 16:56:26 +02002186 LY_ERR ret = LY_SUCCESS;
2187 struct yin_subelement *subelems = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002188 size_t subelems_size;
David Sedlákaf536aa2019-07-23 13:42:23 +02002189
Michal Vaskob36053d2020-03-26 15:49:30 +01002190 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, list, next, LY_EMEM);
David Sedlákaf536aa2019-07-23 13:42:23 +02002191 list->nodetype = LYS_LIST;
2192 list->parent = node_meta->parent;
2193
David Sedlákaf536aa2019-07-23 13:42:23 +02002194 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002195 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2196 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &list->name, Y_IDENTIF_ARG, LY_STMT_LIST));
David Sedlákaf536aa2019-07-23 13:42:23 +02002197
2198 /* parse list content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002199 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 25, (struct lysp_node *)list, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02002200 LY_STMT_ACTION, &list->actions, 0,
2201 LY_STMT_ANYDATA, &list->child, 0,
2202 LY_STMT_ANYXML, &list->child, 0,
2203 LY_STMT_CHOICE, &list->child, 0,
2204 LY_STMT_CONFIG, &list->flags, YIN_SUBELEM_UNIQUE,
2205 LY_STMT_CONTAINER, &list->child, 0,
2206 LY_STMT_DESCRIPTION, &list->dsc, YIN_SUBELEM_UNIQUE,
2207 LY_STMT_GROUPING, &list->groupings, 0,
2208 LY_STMT_IF_FEATURE, &list->iffeatures, 0,
2209 LY_STMT_KEY, &list->key, YIN_SUBELEM_UNIQUE,
2210 LY_STMT_LEAF, &list->child, 0,
2211 LY_STMT_LEAF_LIST, &list->child, 0,
2212 LY_STMT_LIST, &list->child, 0,
2213 LY_STMT_MAX_ELEMENTS, list, YIN_SUBELEM_UNIQUE,
2214 LY_STMT_MIN_ELEMENTS, list, YIN_SUBELEM_UNIQUE,
2215 LY_STMT_MUST, &list->musts, 0,
2216 LY_STMT_NOTIFICATION, &list->notifs, 0,
2217 LY_STMT_ORDERED_BY, &list->flags, YIN_SUBELEM_UNIQUE,
2218 LY_STMT_REFERENCE, &list->ref, YIN_SUBELEM_UNIQUE,
2219 LY_STMT_STATUS, &list->flags, YIN_SUBELEM_UNIQUE,
2220 LY_STMT_TYPEDEF, &list->typedefs, 0,
2221 LY_STMT_UNIQUE, &list->uniques, 0,
2222 LY_STMT_USES, &list->child, 0,
2223 LY_STMT_WHEN, &list->when, YIN_SUBELEM_UNIQUE,
2224 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
Radek Krejcif13b87b2020-12-01 22:02:17 +01002225 ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_LIST, NULL, &list->exts);
2226 subelems_deallocator(subelems_size, subelems);
David Sedlák81497a32019-08-13 16:56:26 +02002227 LY_CHECK_RET(ret);
David Sedlákaf536aa2019-07-23 13:42:23 +02002228
Michal Vasko69730152020-10-09 16:30:07 +02002229 if (list->max && (list->min > list->max)) {
David Sedlák1538a842019-08-08 15:38:51 +02002230 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_MINMAX, list->min, list->max);
David Sedlákaf536aa2019-07-23 13:42:23 +02002231 return LY_EVALID;
2232 }
2233
2234 return LY_SUCCESS;
2235}
2236
2237/**
David Sedlák031b9e72019-07-23 15:19:37 +02002238 * @brief Parse notification element.
2239 *
2240 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák05404f62019-07-24 14:11:53 +02002241 * @param[in,out] notif_meta Meta information about parent node and notifications to add to.
David Sedlák031b9e72019-07-23 15:19:37 +02002242 *
2243 * @return LY_ERR values.
2244 */
2245static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002246yin_parse_notification(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *notif_meta)
David Sedlák031b9e72019-07-23 15:19:37 +02002247{
Radek Krejci2a9fc652021-01-22 17:44:34 +01002248 struct lysp_node_notif *notif;
2249 struct lysp_node_notif **notifs = (struct lysp_node_notif **)notif_meta->nodes;
David Sedlák81497a32019-08-13 16:56:26 +02002250 LY_ERR ret = LY_SUCCESS;
2251 struct yin_subelement *subelems = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002252 size_t subelems_size;
David Sedlák031b9e72019-07-23 15:19:37 +02002253
2254 /* allocate new notification */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002255 LY_LIST_NEW_RET(ctx->xmlctx->ctx, notifs, notif, next, LY_EMEM);
David Sedlák031b9e72019-07-23 15:19:37 +02002256 notif->nodetype = LYS_NOTIF;
2257 notif->parent = notif_meta->parent;
2258
2259 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002260 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2261 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &notif->name, Y_IDENTIF_ARG, LY_STMT_NOTIFICATION));
David Sedlák031b9e72019-07-23 15:19:37 +02002262
2263 /* parse notification content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002264 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 16, (struct lysp_node *)notif, &subelems,
Radek Krejci01180ac2021-01-27 08:48:22 +01002265 LY_STMT_ANYDATA, &notif->child, 0,
2266 LY_STMT_ANYXML, &notif->child, 0,
2267 LY_STMT_CHOICE, &notif->child, 0,
2268 LY_STMT_CONTAINER, &notif->child, 0,
Michal Vasko69730152020-10-09 16:30:07 +02002269 LY_STMT_DESCRIPTION, &notif->dsc, YIN_SUBELEM_UNIQUE,
2270 LY_STMT_GROUPING, &notif->groupings, 0,
2271 LY_STMT_IF_FEATURE, &notif->iffeatures, 0,
Radek Krejci01180ac2021-01-27 08:48:22 +01002272 LY_STMT_LEAF, &notif->child, 0,
2273 LY_STMT_LEAF_LIST, &notif->child, 0,
2274 LY_STMT_LIST, &notif->child, 0,
Michal Vasko69730152020-10-09 16:30:07 +02002275 LY_STMT_MUST, &notif->musts, YIN_SUBELEM_VER2,
2276 LY_STMT_REFERENCE, &notif->ref, YIN_SUBELEM_UNIQUE,
2277 LY_STMT_STATUS, &notif->flags, YIN_SUBELEM_UNIQUE,
2278 LY_STMT_TYPEDEF, &notif->typedefs, 0,
Radek Krejci01180ac2021-01-27 08:48:22 +01002279 LY_STMT_USES, &notif->child, 0,
Michal Vasko69730152020-10-09 16:30:07 +02002280 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
David Sedlák81497a32019-08-13 16:56:26 +02002281
Radek Krejcif13b87b2020-12-01 22:02:17 +01002282 ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_NOTIFICATION, NULL, &notif->exts);
2283 subelems_deallocator(subelems_size, subelems);
David Sedlák81497a32019-08-13 16:56:26 +02002284 LY_CHECK_RET(ret);
David Sedlák031b9e72019-07-23 15:19:37 +02002285
David Sedlák031b9e72019-07-23 15:19:37 +02002286 return LY_SUCCESS;
2287}
2288
2289/**
David Sedlákbf8a2b72019-08-14 16:48:10 +02002290 * @brief Parse grouping element.
David Sedláke3ce9ef2019-07-23 16:34:30 +02002291 *
2292 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbf8a2b72019-08-14 16:48:10 +02002293 * @param[in,out] gr_meta Meta information about parent node and groupings to add to.
David Sedláke3ce9ef2019-07-23 16:34:30 +02002294 *
2295 * @return LY_ERR values.
2296 */
2297static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002298yin_parse_grouping(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *gr_meta)
David Sedláke3ce9ef2019-07-23 16:34:30 +02002299{
Radek Krejci2a9fc652021-01-22 17:44:34 +01002300 struct lysp_node_grp *grp;
2301 struct lysp_node_grp **grps = (struct lysp_node_grp **)gr_meta->nodes;
David Sedlák81497a32019-08-13 16:56:26 +02002302 LY_ERR ret = LY_SUCCESS;
2303 struct yin_subelement *subelems = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002304 size_t subelems_size;
David Sedláke3ce9ef2019-07-23 16:34:30 +02002305
2306 /* create new grouping */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002307 LY_LIST_NEW_RET(ctx->xmlctx->ctx, grps, grp, next, LY_EMEM);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002308 grp->nodetype = LYS_GROUPING;
2309 grp->parent = gr_meta->parent;
2310
2311 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002312 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2313 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &grp->name, Y_IDENTIF_ARG, LY_STMT_GROUPING));
David Sedláke3ce9ef2019-07-23 16:34:30 +02002314
2315 /* parse grouping content */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002316 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 16, &grp->node, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02002317 LY_STMT_ACTION, &grp->actions, 0,
Radek Krejci01180ac2021-01-27 08:48:22 +01002318 LY_STMT_ANYDATA, &grp->child, 0,
2319 LY_STMT_ANYXML, &grp->child, 0,
2320 LY_STMT_CHOICE, &grp->child, 0,
2321 LY_STMT_CONTAINER, &grp->child, 0,
Michal Vasko69730152020-10-09 16:30:07 +02002322 LY_STMT_DESCRIPTION, &grp->dsc, YIN_SUBELEM_UNIQUE,
2323 LY_STMT_GROUPING, &grp->groupings, 0,
Radek Krejci01180ac2021-01-27 08:48:22 +01002324 LY_STMT_LEAF, &grp->child, 0,
2325 LY_STMT_LEAF_LIST, &grp->child, 0,
2326 LY_STMT_LIST, &grp->child, 0,
Michal Vasko69730152020-10-09 16:30:07 +02002327 LY_STMT_NOTIFICATION, &grp->notifs, 0,
2328 LY_STMT_REFERENCE, &grp->ref, YIN_SUBELEM_UNIQUE,
2329 LY_STMT_STATUS, &grp->flags, YIN_SUBELEM_UNIQUE,
2330 LY_STMT_TYPEDEF, &grp->typedefs, 0,
Radek Krejci01180ac2021-01-27 08:48:22 +01002331 LY_STMT_USES, &grp->child, 0,
Michal Vasko69730152020-10-09 16:30:07 +02002332 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
Radek Krejcif13b87b2020-12-01 22:02:17 +01002333 ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_GROUPING, NULL, &grp->exts);
2334 subelems_deallocator(subelems_size, subelems);
David Sedlák81497a32019-08-13 16:56:26 +02002335
aPiecek63e080d2021-06-29 13:53:28 +02002336 /* store data for collision check */
2337 if (!ret && grp->parent) {
2338 assert(ctx->main_ctx);
2339 LY_CHECK_RET(ly_set_add(&ctx->main_ctx->grps_nodes, grp->parent, 0, NULL));
2340 }
2341
Radek Krejci2a9fc652021-01-22 17:44:34 +01002342 return ret;
David Sedláke3ce9ef2019-07-23 16:34:30 +02002343}
2344
2345/**
David Sedlákbf8a2b72019-08-14 16:48:10 +02002346 * @brief Parse container element.
David Sedlákf111bcb2019-07-23 17:15:51 +02002347 *
2348 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákad83cf92019-08-13 12:53:53 +02002349 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlákf111bcb2019-07-23 17:15:51 +02002350 *
2351 * @return LY_ERR values.
2352 */
2353static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002354yin_parse_container(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta)
David Sedlákf111bcb2019-07-23 17:15:51 +02002355{
David Sedlákf111bcb2019-07-23 17:15:51 +02002356 struct lysp_node_container *cont;
David Sedlák81497a32019-08-13 16:56:26 +02002357 LY_ERR ret = LY_SUCCESS;
2358 struct yin_subelement *subelems = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002359 size_t subelems_size;
David Sedlákf111bcb2019-07-23 17:15:51 +02002360
2361 /* create new container */
Michal Vaskob36053d2020-03-26 15:49:30 +01002362 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, cont, next, LY_EMEM);
David Sedlákf111bcb2019-07-23 17:15:51 +02002363 cont->nodetype = LYS_CONTAINER;
2364 cont->parent = node_meta->parent;
2365
David Sedlákf111bcb2019-07-23 17:15:51 +02002366 /* parse aegument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002367 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2368 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &cont->name, Y_IDENTIF_ARG, LY_STMT_CONTAINER));
David Sedlákf111bcb2019-07-23 17:15:51 +02002369
2370 /* parse container content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002371 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 21, (struct lysp_node *)cont, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02002372 LY_STMT_ACTION, &cont->actions, YIN_SUBELEM_VER2,
2373 LY_STMT_ANYDATA, &cont->child, YIN_SUBELEM_VER2,
2374 LY_STMT_ANYXML, &cont->child, 0,
2375 LY_STMT_CHOICE, &cont->child, 0,
2376 LY_STMT_CONFIG, &cont->flags, YIN_SUBELEM_UNIQUE,
2377 LY_STMT_CONTAINER, &cont->child, 0,
2378 LY_STMT_DESCRIPTION, &cont->dsc, YIN_SUBELEM_UNIQUE,
2379 LY_STMT_GROUPING, &cont->groupings, 0,
2380 LY_STMT_IF_FEATURE, &cont->iffeatures, 0,
2381 LY_STMT_LEAF, &cont->child, 0,
2382 LY_STMT_LEAF_LIST, &cont->child, 0,
2383 LY_STMT_LIST, &cont->child, 0,
2384 LY_STMT_MUST, &cont->musts, 0,
2385 LY_STMT_NOTIFICATION, &cont->notifs, YIN_SUBELEM_VER2,
2386 LY_STMT_PRESENCE, &cont->presence, YIN_SUBELEM_UNIQUE,
2387 LY_STMT_REFERENCE, &cont->ref, YIN_SUBELEM_UNIQUE,
2388 LY_STMT_STATUS, &cont->flags, YIN_SUBELEM_UNIQUE,
2389 LY_STMT_TYPEDEF, &cont->typedefs, 0,
2390 LY_STMT_USES, &cont->child, 0,
2391 LY_STMT_WHEN, &cont->when, YIN_SUBELEM_UNIQUE,
2392 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
Radek Krejcif13b87b2020-12-01 22:02:17 +01002393 ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_CONTAINER, NULL, &cont->exts);
2394 subelems_deallocator(subelems_size, subelems);
David Sedlák81497a32019-08-13 16:56:26 +02002395
Radek Krejci2a9fc652021-01-22 17:44:34 +01002396 return ret;
David Sedlákf111bcb2019-07-23 17:15:51 +02002397}
2398
2399/**
David Sedlák5379d392019-07-24 10:42:03 +02002400 * @brief Parse case element.
2401 *
2402 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákad83cf92019-08-13 12:53:53 +02002403 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlák5379d392019-07-24 10:42:03 +02002404 *
2405 * @return LY_ERR values.
2406 */
2407static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002408yin_parse_case(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta)
David Sedlák5379d392019-07-24 10:42:03 +02002409{
David Sedlák5379d392019-07-24 10:42:03 +02002410 struct lysp_node_case *cas;
David Sedlák81497a32019-08-13 16:56:26 +02002411 LY_ERR ret = LY_SUCCESS;
Michal Vasko69730152020-10-09 16:30:07 +02002412 struct yin_subelement *subelems = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002413 size_t subelems_size;
David Sedlák5379d392019-07-24 10:42:03 +02002414
2415 /* create new case */
Michal Vaskob36053d2020-03-26 15:49:30 +01002416 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, cas, next, LY_EMEM);
David Sedlák5379d392019-07-24 10:42:03 +02002417 cas->nodetype = LYS_CASE;
2418 cas->parent = node_meta->parent;
2419
David Sedlák5379d392019-07-24 10:42:03 +02002420 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002421 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2422 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &cas->name, Y_IDENTIF_ARG, LY_STMT_CASE));
David Sedlák5379d392019-07-24 10:42:03 +02002423
2424 /* parse case content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002425 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 14, (struct lysp_node *)cas, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02002426 LY_STMT_ANYDATA, &cas->child, YIN_SUBELEM_VER2,
2427 LY_STMT_ANYXML, &cas->child, 0,
2428 LY_STMT_CHOICE, &cas->child, 0,
2429 LY_STMT_CONTAINER, &cas->child, 0,
2430 LY_STMT_DESCRIPTION, &cas->dsc, YIN_SUBELEM_UNIQUE,
2431 LY_STMT_IF_FEATURE, &cas->iffeatures, 0,
2432 LY_STMT_LEAF, &cas->child, 0,
2433 LY_STMT_LEAF_LIST, &cas->child, 0,
2434 LY_STMT_LIST, &cas->child, 0,
2435 LY_STMT_REFERENCE, &cas->ref, YIN_SUBELEM_UNIQUE,
2436 LY_STMT_STATUS, &cas->flags, YIN_SUBELEM_UNIQUE,
2437 LY_STMT_USES, &cas->child, 0,
2438 LY_STMT_WHEN, &cas->when, YIN_SUBELEM_UNIQUE,
2439 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
Radek Krejcif13b87b2020-12-01 22:02:17 +01002440 ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_CASE, NULL, &cas->exts);
2441 subelems_deallocator(subelems_size, subelems);
David Sedlák81497a32019-08-13 16:56:26 +02002442
2443 return ret;
David Sedlák5379d392019-07-24 10:42:03 +02002444}
2445
2446/**
David Sedlák05404f62019-07-24 14:11:53 +02002447 * @brief Parse choice element.
David Sedlákb7abcfa2019-07-24 12:33:35 +02002448 *
2449 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákad83cf92019-08-13 12:53:53 +02002450 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlákb7abcfa2019-07-24 12:33:35 +02002451 *
2452 * @return LY_ERR values.
2453 */
2454LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002455yin_parse_choice(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta)
David Sedlákb7abcfa2019-07-24 12:33:35 +02002456{
David Sedlák81497a32019-08-13 16:56:26 +02002457 LY_ERR ret = LY_SUCCESS;
2458 struct yin_subelement *subelems = NULL;
David Sedlákb7abcfa2019-07-24 12:33:35 +02002459 struct lysp_node_choice *choice;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002460 size_t subelems_size;
David Sedlákb7abcfa2019-07-24 12:33:35 +02002461
2462 /* create new choice */
Michal Vaskob36053d2020-03-26 15:49:30 +01002463 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, choice, next, LY_EMEM);
David Sedlák8d552d62019-08-06 15:29:05 +02002464
David Sedlákb7abcfa2019-07-24 12:33:35 +02002465 choice->nodetype = LYS_CHOICE;
2466 choice->parent = node_meta->parent;
2467
David Sedlákb7abcfa2019-07-24 12:33:35 +02002468 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002469 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2470 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &choice->name, Y_IDENTIF_ARG, LY_STMT_CHOICE));
David Sedlákb7abcfa2019-07-24 12:33:35 +02002471
2472 /* parse choice content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002473 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 17, (struct lysp_node *)choice, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02002474 LY_STMT_ANYDATA, &choice->child, YIN_SUBELEM_VER2,
2475 LY_STMT_ANYXML, &choice->child, 0,
2476 LY_STMT_CASE, &choice->child, 0,
2477 LY_STMT_CHOICE, &choice->child, YIN_SUBELEM_VER2,
2478 LY_STMT_CONFIG, &choice->flags, YIN_SUBELEM_UNIQUE,
2479 LY_STMT_CONTAINER, &choice->child, 0,
2480 LY_STMT_DEFAULT, &choice->dflt, YIN_SUBELEM_UNIQUE,
2481 LY_STMT_DESCRIPTION, &choice->dsc, YIN_SUBELEM_UNIQUE,
2482 LY_STMT_IF_FEATURE, &choice->iffeatures, 0,
2483 LY_STMT_LEAF, &choice->child, 0,
2484 LY_STMT_LEAF_LIST, &choice->child, 0,
2485 LY_STMT_LIST, &choice->child, 0,
2486 LY_STMT_MANDATORY, &choice->flags, YIN_SUBELEM_UNIQUE,
2487 LY_STMT_REFERENCE, &choice->ref, YIN_SUBELEM_UNIQUE,
2488 LY_STMT_STATUS, &choice->flags, YIN_SUBELEM_UNIQUE,
2489 LY_STMT_WHEN, &choice->when, YIN_SUBELEM_UNIQUE,
2490 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
Radek Krejcif13b87b2020-12-01 22:02:17 +01002491 ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_CHOICE, NULL, &choice->exts);
2492 subelems_deallocator(subelems_size, subelems);
David Sedlák81497a32019-08-13 16:56:26 +02002493 return ret;
David Sedlákb7abcfa2019-07-24 12:33:35 +02002494}
2495
2496/**
David Sedlák05404f62019-07-24 14:11:53 +02002497 * @brief Parse input or output element.
2498 *
2499 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbf8a2b72019-08-14 16:48:10 +02002500 * @param[in] inout_kw Identification of input/output element.
David Sedlák05404f62019-07-24 14:11:53 +02002501 * @param[in] inout_meta Meta information about parent node and siblings and input/output pointer to write to.
2502 *
2503 * @return LY_ERR values.
2504 */
2505static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002506yin_parse_inout(struct lys_yin_parser_ctx *ctx, enum ly_stmt inout_kw, struct inout_meta *inout_meta)
David Sedlák05404f62019-07-24 14:11:53 +02002507{
David Sedlák81497a32019-08-13 16:56:26 +02002508 LY_ERR ret = LY_SUCCESS;
2509 struct yin_subelement *subelems = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002510 size_t subelems_size;
David Sedlák81497a32019-08-13 16:56:26 +02002511
David Sedlák05404f62019-07-24 14:11:53 +02002512 /* initiate structure */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002513 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), (inout_kw == LY_STMT_INPUT) ? "input" : "output", 0, &inout_meta->inout_p->name));
Radek Krejcid6b76452019-09-03 17:03:03 +02002514 inout_meta->inout_p->nodetype = (inout_kw == LY_STMT_INPUT) ? LYS_INPUT : LYS_OUTPUT;
David Sedlák05404f62019-07-24 14:11:53 +02002515 inout_meta->inout_p->parent = inout_meta->parent;
2516
2517 /* check attributes */
Michal Vaskob36053d2020-03-26 15:49:30 +01002518 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2519 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NONE, NULL, Y_MAYBE_STR_ARG, inout_kw));
David Sedlák05404f62019-07-24 14:11:53 +02002520
2521 /* parser input/output content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002522 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 12, (struct lysp_node *)inout_meta->inout_p, &subelems,
Radek Krejci01180ac2021-01-27 08:48:22 +01002523 LY_STMT_ANYDATA, &inout_meta->inout_p->child, YIN_SUBELEM_VER2,
2524 LY_STMT_ANYXML, &inout_meta->inout_p->child, 0,
2525 LY_STMT_CHOICE, &inout_meta->inout_p->child, 0,
2526 LY_STMT_CONTAINER, &inout_meta->inout_p->child, 0,
Michal Vasko69730152020-10-09 16:30:07 +02002527 LY_STMT_GROUPING, &inout_meta->inout_p->groupings, 0,
Radek Krejci01180ac2021-01-27 08:48:22 +01002528 LY_STMT_LEAF, &inout_meta->inout_p->child, 0,
2529 LY_STMT_LEAF_LIST, &inout_meta->inout_p->child, 0,
2530 LY_STMT_LIST, &inout_meta->inout_p->child, 0,
Michal Vasko69730152020-10-09 16:30:07 +02002531 LY_STMT_MUST, &inout_meta->inout_p->musts, YIN_SUBELEM_VER2,
2532 LY_STMT_TYPEDEF, &inout_meta->inout_p->typedefs, 0,
Radek Krejci01180ac2021-01-27 08:48:22 +01002533 LY_STMT_USES, &inout_meta->inout_p->child, 0,
Michal Vasko69730152020-10-09 16:30:07 +02002534 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
Radek Krejcif13b87b2020-12-01 22:02:17 +01002535 ret = yin_parse_content(ctx, subelems, subelems_size, inout_kw, NULL, &inout_meta->inout_p->exts);
2536 subelems_deallocator(subelems_size, subelems);
David Sedlák81497a32019-08-13 16:56:26 +02002537 LY_CHECK_RET(ret);
David Sedlák05404f62019-07-24 14:11:53 +02002538
Radek Krejci01180ac2021-01-27 08:48:22 +01002539 if (!inout_meta->inout_p->child) {
Michal Vaskob83af8a2020-01-06 09:49:22 +01002540 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_MISSTMT, "data-def-stmt", ly_stmt2str(inout_kw));
2541 return LY_EVALID;
2542 }
2543
David Sedlák05404f62019-07-24 14:11:53 +02002544 return LY_SUCCESS;
2545}
2546
David Sedlák992fb7c2019-07-24 16:51:01 +02002547/**
2548 * @brief Parse action element.
2549 *
2550 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák992fb7c2019-07-24 16:51:01 +02002551 * @param[in] act_meta Meta information about parent node and actions to add to.
2552 *
2553 * @return LY_ERR values.
2554 */
David Sedlák85d0eca2019-07-24 15:15:21 +02002555static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002556yin_parse_action(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *act_meta)
David Sedlák85d0eca2019-07-24 15:15:21 +02002557{
Radek Krejci2a9fc652021-01-22 17:44:34 +01002558 struct lysp_node_action *act, **acts = (struct lysp_node_action **)act_meta->nodes;
David Sedlák81497a32019-08-13 16:56:26 +02002559 LY_ERR ret = LY_SUCCESS;
2560 struct yin_subelement *subelems = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002561 size_t subelems_size;
Radek Krejci39b7fc22021-02-26 23:29:18 +01002562 enum ly_stmt kw = act_meta->parent ? LY_STMT_ACTION : LY_STMT_RPC;
David Sedlák85d0eca2019-07-24 15:15:21 +02002563
2564 /* create new action */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002565 LY_LIST_NEW_RET(ctx->xmlctx->ctx, acts, act, next, LY_EMEM);
Michal Vasko1bf09392020-03-27 12:38:10 +01002566 act->nodetype = act_meta->parent ? LYS_ACTION : LYS_RPC;
David Sedlák85d0eca2019-07-24 15:15:21 +02002567 act->parent = act_meta->parent;
2568
2569 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002570 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
Radek Krejci39b7fc22021-02-26 23:29:18 +01002571 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &act->name, Y_IDENTIF_ARG, kw));
David Sedlák85d0eca2019-07-24 15:15:21 +02002572
2573 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002574 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 9, (struct lysp_node *)act, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02002575 LY_STMT_DESCRIPTION, &act->dsc, YIN_SUBELEM_UNIQUE,
2576 LY_STMT_GROUPING, &act->groupings, 0,
2577 LY_STMT_IF_FEATURE, &act->iffeatures, 0,
2578 LY_STMT_INPUT, &act->input, YIN_SUBELEM_UNIQUE,
2579 LY_STMT_OUTPUT, &act->output, YIN_SUBELEM_UNIQUE,
2580 LY_STMT_REFERENCE, &act->ref, YIN_SUBELEM_UNIQUE,
2581 LY_STMT_STATUS, &act->flags, YIN_SUBELEM_UNIQUE,
2582 LY_STMT_TYPEDEF, &act->typedefs, 0,
2583 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
Radek Krejci39b7fc22021-02-26 23:29:18 +01002584 ret = (yin_parse_content(ctx, subelems, subelems_size, kw, NULL, &act->exts));
Radek Krejcif13b87b2020-12-01 22:02:17 +01002585 subelems_deallocator(subelems_size, subelems);
David Sedlák81497a32019-08-13 16:56:26 +02002586 LY_CHECK_RET(ret);
2587
Michal Vasko7f45cf22020-10-01 12:49:44 +02002588 /* always initialize inout, they are technically present (needed for later deviations/refines) */
2589 if (!act->input.nodetype) {
2590 act->input.nodetype = LYS_INPUT;
2591 act->input.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002592 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "input", 0, &act->input.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002593 }
2594 if (!act->output.nodetype) {
2595 act->output.nodetype = LYS_OUTPUT;
2596 act->output.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002597 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "output", 0, &act->output.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002598 }
2599
David Sedlák85d0eca2019-07-24 15:15:21 +02002600 return LY_SUCCESS;
2601}
2602
David Sedlák05404f62019-07-24 14:11:53 +02002603/**
David Sedlák992fb7c2019-07-24 16:51:01 +02002604 * @brief Parse augment element.
2605 *
2606 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák992fb7c2019-07-24 16:51:01 +02002607 * @param[in] aug_meta Meta information about parent node and augments to add to.
2608 *
2609 * @return LY_ERR values.
2610 */
2611static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002612yin_parse_augment(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *aug_meta)
David Sedlák992fb7c2019-07-24 16:51:01 +02002613{
Radek Krejci2a9fc652021-01-22 17:44:34 +01002614 struct lysp_node_augment *aug;
2615 struct lysp_node_augment **augs = (struct lysp_node_augment **)aug_meta->nodes;
David Sedlák81497a32019-08-13 16:56:26 +02002616 LY_ERR ret = LY_SUCCESS;
2617 struct yin_subelement *subelems = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002618 size_t subelems_size;
David Sedlák992fb7c2019-07-24 16:51:01 +02002619
2620 /* create new augment */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002621 LY_LIST_NEW_RET(ctx->xmlctx->ctx, augs, aug, next, LY_EMEM);
David Sedlák992fb7c2019-07-24 16:51:01 +02002622 aug->nodetype = LYS_AUGMENT;
2623 aug->parent = aug_meta->parent;
2624
2625 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002626 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2627 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_TARGET_NODE, &aug->nodeid, Y_STR_ARG, LY_STMT_AUGMENT));
2628 CHECK_NONEMPTY((struct lys_parser_ctx *)ctx, strlen(aug->nodeid), "augment");
David Sedlák992fb7c2019-07-24 16:51:01 +02002629
2630 /* parser augment content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002631 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 17, (struct lysp_node *)aug, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02002632 LY_STMT_ACTION, &aug->actions, YIN_SUBELEM_VER2,
2633 LY_STMT_ANYDATA, &aug->child, YIN_SUBELEM_VER2,
2634 LY_STMT_ANYXML, &aug->child, 0,
2635 LY_STMT_CASE, &aug->child, 0,
2636 LY_STMT_CHOICE, &aug->child, 0,
2637 LY_STMT_CONTAINER, &aug->child, 0,
2638 LY_STMT_DESCRIPTION, &aug->dsc, YIN_SUBELEM_UNIQUE,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002639 LY_STMT_IF_FEATURE, &aug->iffeatures, 0,
Michal Vasko69730152020-10-09 16:30:07 +02002640 LY_STMT_LEAF, &aug->child, 0,
2641 LY_STMT_LEAF_LIST, &aug->child, 0,
2642 LY_STMT_LIST, &aug->child, 0,
2643 LY_STMT_NOTIFICATION, &aug->notifs, YIN_SUBELEM_VER2,
2644 LY_STMT_REFERENCE, &aug->ref, YIN_SUBELEM_UNIQUE,
2645 LY_STMT_STATUS, &aug->flags, YIN_SUBELEM_UNIQUE,
2646 LY_STMT_USES, &aug->child, 0,
2647 LY_STMT_WHEN, &aug->when, YIN_SUBELEM_UNIQUE,
2648 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
Radek Krejcif13b87b2020-12-01 22:02:17 +01002649 ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_AUGMENT, NULL, &aug->exts);
2650 subelems_deallocator(subelems_size, subelems);
David Sedlák992fb7c2019-07-24 16:51:01 +02002651
Radek Krejci2a9fc652021-01-22 17:44:34 +01002652 return ret;
David Sedlák992fb7c2019-07-24 16:51:01 +02002653}
2654
David Sedlák8b754462019-07-25 16:22:13 +02002655/**
2656 * @brief Parse deviate element.
2657 *
2658 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák8b754462019-07-25 16:22:13 +02002659 * @param[in] deviates Deviates to add to.
2660 *
2661 * @return LY_ERR values.
2662 */
David Sedlák4ffcec82019-07-25 15:10:21 +02002663static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002664yin_parse_deviate(struct lys_yin_parser_ctx *ctx, struct lysp_deviate **deviates)
David Sedlák4ffcec82019-07-25 15:10:21 +02002665{
2666 LY_ERR ret = LY_SUCCESS;
2667 uint8_t dev_mod;
2668 const char *temp_val;
David Sedlák8d552d62019-08-06 15:29:05 +02002669 struct lysp_deviate *d;
David Sedlák4ffcec82019-07-25 15:10:21 +02002670 struct lysp_deviate_add *d_add = NULL;
2671 struct lysp_deviate_rpl *d_rpl = NULL;
2672 struct lysp_deviate_del *d_del = NULL;
2673
2674 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002675 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2676 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_DEVIATE));
David Sedlák4ffcec82019-07-25 15:10:21 +02002677
2678 if (strcmp(temp_val, "not-supported") == 0) {
2679 dev_mod = LYS_DEV_NOT_SUPPORTED;
2680 } else if (strcmp(temp_val, "add") == 0) {
2681 dev_mod = LYS_DEV_ADD;
2682 } else if (strcmp(temp_val, "replace") == 0) {
2683 dev_mod = LYS_DEV_REPLACE;
2684 } else if (strcmp(temp_val, "delete") == 0) {
2685 dev_mod = LYS_DEV_DELETE;
2686 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02002687 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS4, temp_val, "value", "deviate",
Michal Vasko69730152020-10-09 16:30:07 +02002688 "not-supported", "add", "replace", "delete");
Michal Vaskoe180ed02021-02-05 16:31:20 +01002689 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlák4ffcec82019-07-25 15:10:21 +02002690 return LY_EVALID;
2691 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01002692 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlák4ffcec82019-07-25 15:10:21 +02002693
2694 if (dev_mod == LYS_DEV_NOT_SUPPORTED) {
2695 d = calloc(1, sizeof *d);
Michal Vaskob36053d2020-03-26 15:49:30 +01002696 LY_CHECK_ERR_RET(!d, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
Radek Krejcif13b87b2020-12-01 22:02:17 +01002697 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002698 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
2699 };
Michal Vasko26bbb272022-08-02 14:54:33 +02002700
Radek Krejcif13b87b2020-12-01 22:02:17 +01002701 ret = yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_DEVIATE, NULL, &d->exts);
David Sedlák4ffcec82019-07-25 15:10:21 +02002702
2703 } else if (dev_mod == LYS_DEV_ADD) {
2704 d_add = calloc(1, sizeof *d_add);
Michal Vaskob36053d2020-03-26 15:49:30 +01002705 LY_CHECK_ERR_RET(!d_add, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
David Sedlák4ffcec82019-07-25 15:10:21 +02002706 d = (struct lysp_deviate *)d_add;
2707 struct minmax_dev_meta min = {&d_add->min, &d_add->flags, &d_add->exts};
2708 struct minmax_dev_meta max = {&d_add->max, &d_add->flags, &d_add->exts};
Radek Krejcif13b87b2020-12-01 22:02:17 +01002709 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002710 {LY_STMT_CONFIG, &d_add->flags, YIN_SUBELEM_UNIQUE},
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002711 {LY_STMT_DEFAULT, &d_add->dflts, 0},
Radek Krejci0f969882020-08-21 16:56:47 +02002712 {LY_STMT_MANDATORY, &d_add->flags, YIN_SUBELEM_UNIQUE},
2713 {LY_STMT_MAX_ELEMENTS, &max, YIN_SUBELEM_UNIQUE},
2714 {LY_STMT_MIN_ELEMENTS, &min, YIN_SUBELEM_UNIQUE},
2715 {LY_STMT_MUST, &d_add->musts, 0},
2716 {LY_STMT_UNIQUE, &d_add->uniques, 0},
2717 {LY_STMT_UNITS, &d_add->units, YIN_SUBELEM_UNIQUE},
2718 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
2719 };
Michal Vasko26bbb272022-08-02 14:54:33 +02002720
Radek Krejcif13b87b2020-12-01 22:02:17 +01002721 ret = yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_DEVIATE, NULL, &d_add->exts);
David Sedlák4ffcec82019-07-25 15:10:21 +02002722
2723 } else if (dev_mod == LYS_DEV_REPLACE) {
2724 d_rpl = calloc(1, sizeof *d_rpl);
Michal Vaskob36053d2020-03-26 15:49:30 +01002725 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
David Sedlák4ffcec82019-07-25 15:10:21 +02002726 d = (struct lysp_deviate *)d_rpl;
2727 struct minmax_dev_meta min = {&d_rpl->min, &d_rpl->flags, &d_rpl->exts};
2728 struct minmax_dev_meta max = {&d_rpl->max, &d_rpl->flags, &d_rpl->exts};
Radek Krejcif13b87b2020-12-01 22:02:17 +01002729 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002730 {LY_STMT_CONFIG, &d_rpl->flags, YIN_SUBELEM_UNIQUE},
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002731 {LY_STMT_DEFAULT, &d_rpl->dflt, YIN_SUBELEM_UNIQUE},
Radek Krejci0f969882020-08-21 16:56:47 +02002732 {LY_STMT_MANDATORY, &d_rpl->flags, YIN_SUBELEM_UNIQUE},
2733 {LY_STMT_MAX_ELEMENTS, &max, YIN_SUBELEM_UNIQUE},
2734 {LY_STMT_MIN_ELEMENTS, &min, YIN_SUBELEM_UNIQUE},
2735 {LY_STMT_TYPE, &d_rpl->type, YIN_SUBELEM_UNIQUE},
2736 {LY_STMT_UNITS, &d_rpl->units, YIN_SUBELEM_UNIQUE},
2737 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
2738 };
Michal Vasko26bbb272022-08-02 14:54:33 +02002739
Radek Krejcif13b87b2020-12-01 22:02:17 +01002740 ret = yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_DEVIATE, NULL, &d_rpl->exts);
David Sedlák4ffcec82019-07-25 15:10:21 +02002741
2742 } else {
2743 d_del = calloc(1, sizeof *d_del);
Michal Vaskob36053d2020-03-26 15:49:30 +01002744 LY_CHECK_ERR_RET(!d_del, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
David Sedlák4ffcec82019-07-25 15:10:21 +02002745 d = (struct lysp_deviate *)d_del;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002746 struct yin_subelement subelems[] = {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002747 {LY_STMT_DEFAULT, &d_del->dflts, 0},
Radek Krejci0f969882020-08-21 16:56:47 +02002748 {LY_STMT_MUST, &d_del->musts, 0},
2749 {LY_STMT_UNIQUE, &d_del->uniques, 0},
2750 {LY_STMT_UNITS, &d_del->units, YIN_SUBELEM_UNIQUE},
2751 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
2752 };
Michal Vasko26bbb272022-08-02 14:54:33 +02002753
Radek Krejcif13b87b2020-12-01 22:02:17 +01002754 ret = yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_DEVIATE, NULL, &d_del->exts);
David Sedlák4ffcec82019-07-25 15:10:21 +02002755 }
2756 LY_CHECK_GOTO(ret, cleanup);
2757
2758 d->mod = dev_mod;
2759 /* insert into siblings */
David Sedlák8d552d62019-08-06 15:29:05 +02002760 LY_LIST_INSERT(deviates, d, next);
David Sedlák4ffcec82019-07-25 15:10:21 +02002761
2762 return ret;
2763
2764cleanup:
2765 free(d);
David Sedlák4ffcec82019-07-25 15:10:21 +02002766 return ret;
2767}
2768
David Sedlák992fb7c2019-07-24 16:51:01 +02002769/**
David Sedlák8b754462019-07-25 16:22:13 +02002770 * @brief Parse deviation element.
2771 *
2772 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák8b754462019-07-25 16:22:13 +02002773 * @param[in] deviations Deviations to add to.
David Sedlák8b754462019-07-25 16:22:13 +02002774 * @return LY_ERR values.
2775 */
2776static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002777yin_parse_deviation(struct lys_yin_parser_ctx *ctx, struct lysp_deviation **deviations)
David Sedlák8b754462019-07-25 16:22:13 +02002778{
Michal Vasko12ef5362022-09-16 15:13:58 +02002779 LY_ERR ret = LY_SUCCESS;
2780 struct lysp_deviation *dev = NULL;
Michal Vaskoc636ea42022-09-16 10:20:31 +02002781 struct lysf_ctx fctx = {.ctx = PARSER_CTX(ctx)};
David Sedlák8b754462019-07-25 16:22:13 +02002782
2783 /* create new deviation */
Michal Vaskob36053d2020-03-26 15:49:30 +01002784 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *deviations, dev, LY_EMEM);
David Sedlák8b754462019-07-25 16:22:13 +02002785
2786 /* parse argument */
Michal Vasko12ef5362022-09-16 15:13:58 +02002787 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
2788 LY_CHECK_GOTO(ret = yin_parse_attribute(ctx, YIN_ARG_TARGET_NODE, &dev->nodeid, Y_STR_ARG, LY_STMT_DEVIATION), cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +01002789 CHECK_NONEMPTY((struct lys_parser_ctx *)ctx, strlen(dev->nodeid), "deviation");
Radek Krejcif13b87b2020-12-01 22:02:17 +01002790 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002791 {LY_STMT_DESCRIPTION, &dev->dsc, YIN_SUBELEM_UNIQUE},
2792 {LY_STMT_DEVIATE, &dev->deviates, YIN_SUBELEM_MANDATORY},
2793 {LY_STMT_REFERENCE, &dev->ref, YIN_SUBELEM_UNIQUE},
2794 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
2795 };
Michal Vasko69730152020-10-09 16:30:07 +02002796
Michal Vasko12ef5362022-09-16 15:13:58 +02002797 LY_CHECK_GOTO(ret = yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_DEVIATION, NULL, &dev->exts), cleanup);
2798
2799cleanup:
2800 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02002801 lysp_deviation_free(&fctx, dev);
Michal Vasko12ef5362022-09-16 15:13:58 +02002802 LY_ARRAY_DECREMENT_FREE(*deviations);
2803 }
2804 return ret;
David Sedlák8b754462019-07-25 16:22:13 +02002805}
2806
2807/**
David Sedlákc5b20842019-08-13 10:18:31 +02002808 * @brief map keyword to keyword-group.
2809 *
2810 * @param[in] ctx YIN parser context used for logging.
2811 * @param[in] kw Keyword that is child of module or submodule.
2812 * @param[out] group Group of keyword.
David Sedlákc5b20842019-08-13 10:18:31 +02002813 * @return LY_SUCCESS on success LY_EINT if kw can't be mapped to kw_group, should not happen if called correctly.
2814 */
David Sedláke6cd89e2019-08-07 12:46:02 +02002815static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002816kw2kw_group(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, enum yang_module_stmt *group)
David Sedláke6cd89e2019-08-07 12:46:02 +02002817{
2818 switch (kw) {
Radek Krejci0f969882020-08-21 16:56:47 +02002819 /* module header */
2820 case LY_STMT_NONE:
2821 case LY_STMT_NAMESPACE:
2822 case LY_STMT_PREFIX:
2823 case LY_STMT_BELONGS_TO:
2824 case LY_STMT_YANG_VERSION:
2825 *group = Y_MOD_MODULE_HEADER;
2826 break;
2827 /* linkage */
2828 case LY_STMT_INCLUDE:
2829 case LY_STMT_IMPORT:
2830 *group = Y_MOD_LINKAGE;
2831 break;
2832 /* meta */
2833 case LY_STMT_ORGANIZATION:
2834 case LY_STMT_CONTACT:
2835 case LY_STMT_DESCRIPTION:
2836 case LY_STMT_REFERENCE:
2837 *group = Y_MOD_META;
2838 break;
2839 /* revision */
2840 case LY_STMT_REVISION:
2841 *group = Y_MOD_REVISION;
2842 break;
2843 /* body */
2844 case LY_STMT_ANYDATA:
2845 case LY_STMT_ANYXML:
2846 case LY_STMT_AUGMENT:
2847 case LY_STMT_CHOICE:
2848 case LY_STMT_CONTAINER:
2849 case LY_STMT_DEVIATION:
2850 case LY_STMT_EXTENSION:
2851 case LY_STMT_FEATURE:
2852 case LY_STMT_GROUPING:
2853 case LY_STMT_IDENTITY:
2854 case LY_STMT_LEAF:
2855 case LY_STMT_LEAF_LIST:
2856 case LY_STMT_LIST:
2857 case LY_STMT_NOTIFICATION:
2858 case LY_STMT_RPC:
2859 case LY_STMT_TYPEDEF:
2860 case LY_STMT_USES:
2861 case LY_STMT_EXTENSION_INSTANCE:
2862 *group = Y_MOD_BODY;
2863 break;
2864 default:
2865 LOGINT(ctx->xmlctx->ctx);
2866 return LY_EINT;
David Sedláke6cd89e2019-08-07 12:46:02 +02002867 }
2868
2869 return LY_SUCCESS;
2870}
2871
David Sedlákc5b20842019-08-13 10:18:31 +02002872/**
2873 * @brief Check if relative order of two keywords is valid.
2874 *
2875 * @param[in] ctx YIN parser context used for logging.
2876 * @param[in] kw Current keyword.
2877 * @param[in] next_kw Next keyword.
Radek Krejcid6b76452019-09-03 17:03:03 +02002878 * @param[in] parrent Identification of parrent element, can be se to to LY_STMT_MODULE of LY_STMT_SUBMODULE,
David Sedlákc5b20842019-08-13 10:18:31 +02002879 * because relative order is required only in module and submodule sub-elements, used for logging.
David Sedlákbf8a2b72019-08-14 16:48:10 +02002880 * @return LY_SUCCESS on success and LY_EVALID if relative order is invalid.
David Sedlákc5b20842019-08-13 10:18:31 +02002881 */
David Sedláke6cd89e2019-08-07 12:46:02 +02002882static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002883yin_check_relative_order(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, enum ly_stmt next_kw, enum ly_stmt parrent)
David Sedláke6cd89e2019-08-07 12:46:02 +02002884{
Radek Krejcid6b76452019-09-03 17:03:03 +02002885 assert(parrent == LY_STMT_MODULE || parrent == LY_STMT_SUBMODULE);
David Sedláke6cd89e2019-08-07 12:46:02 +02002886 enum yang_module_stmt gr, next_gr;
2887
Michal Vaskoc78293f2021-12-16 15:40:33 +01002888 if (kw == LY_STMT_EXTENSION_INSTANCE) {
2889 /* no order defined */
2890 return LY_SUCCESS;
2891 }
2892
David Sedláke6cd89e2019-08-07 12:46:02 +02002893 LY_CHECK_RET(kw2kw_group(ctx, kw, &gr));
2894 LY_CHECK_RET(kw2kw_group(ctx, next_kw, &next_gr));
2895
2896 if (gr > next_gr) {
2897 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INORDER_YIN, ly_stmt2str(parrent), ly_stmt2str(next_kw), ly_stmt2str(kw));
2898 return LY_EVALID;
2899 }
2900
2901 return LY_SUCCESS;
2902}
2903
David Sedlákc5dd0942019-09-13 10:23:59 +02002904/**
Michal Vaskoafac7822020-10-20 14:22:26 +02002905 * @brief Parse argument of extension subelement that is classic yang keyword and not another instance of extension.
2906 *
2907 * @param[in,out] ctx Yin parser context for logging and to store current state.
2908 * @param[in] elem_type Type of element that is currently being parsed.
2909 * @param[out] arg Value to write to.
Michal Vaskoafac7822020-10-20 14:22:26 +02002910 * @return LY_ERR values.
2911 */
2912static LY_ERR
2913yin_parse_extension_instance_arg(struct lys_yin_parser_ctx *ctx, enum ly_stmt elem_type, const char **arg)
2914{
2915 enum ly_stmt child;
2916
2917 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2918
2919 switch (elem_type) {
2920 case LY_STMT_ACTION:
2921 case LY_STMT_ANYDATA:
2922 case LY_STMT_ANYXML:
2923 case LY_STMT_ARGUMENT:
2924 case LY_STMT_BASE:
2925 case LY_STMT_BIT:
2926 case LY_STMT_CASE:
2927 case LY_STMT_CHOICE:
2928 case LY_STMT_CONTAINER:
2929 case LY_STMT_ENUM:
2930 case LY_STMT_EXTENSION:
2931 case LY_STMT_FEATURE:
2932 case LY_STMT_GROUPING:
2933 case LY_STMT_IDENTITY:
2934 case LY_STMT_IF_FEATURE:
2935 case LY_STMT_LEAF:
2936 case LY_STMT_LEAF_LIST:
2937 case LY_STMT_LIST:
2938 case LY_STMT_MODULE:
2939 case LY_STMT_NOTIFICATION:
2940 case LY_STMT_RPC:
2941 case LY_STMT_SUBMODULE:
2942 case LY_STMT_TYPE:
2943 case LY_STMT_TYPEDEF:
2944 case LY_STMT_UNITS:
2945 case LY_STMT_USES:
2946 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, arg, Y_MAYBE_STR_ARG, elem_type));
2947 break;
2948 case LY_STMT_AUGMENT:
2949 case LY_STMT_DEVIATION:
2950 case LY_STMT_REFINE:
2951 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_TARGET_NODE, arg, Y_MAYBE_STR_ARG, elem_type));
2952 break;
2953 case LY_STMT_CONFIG:
2954 case LY_STMT_DEFAULT:
2955 case LY_STMT_DEVIATE:
2956 case LY_STMT_ERROR_APP_TAG:
2957 case LY_STMT_FRACTION_DIGITS:
2958 case LY_STMT_KEY:
2959 case LY_STMT_LENGTH:
2960 case LY_STMT_MANDATORY:
2961 case LY_STMT_MAX_ELEMENTS:
2962 case LY_STMT_MIN_ELEMENTS:
2963 case LY_STMT_MODIFIER:
2964 case LY_STMT_ORDERED_BY:
2965 case LY_STMT_PATH:
2966 case LY_STMT_PATTERN:
2967 case LY_STMT_POSITION:
2968 case LY_STMT_PREFIX:
2969 case LY_STMT_PRESENCE:
2970 case LY_STMT_RANGE:
2971 case LY_STMT_REQUIRE_INSTANCE:
2972 case LY_STMT_STATUS:
2973 case LY_STMT_VALUE:
2974 case LY_STMT_YANG_VERSION:
2975 case LY_STMT_YIN_ELEMENT:
2976 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, arg, Y_MAYBE_STR_ARG, elem_type));
2977 break;
2978 case LY_STMT_IMPORT:
2979 case LY_STMT_INCLUDE:
2980 case LY_STMT_BELONGS_TO:
2981 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_MODULE, arg, Y_MAYBE_STR_ARG, elem_type));
2982 break;
2983 case LY_STMT_INPUT:
2984 case LY_STMT_OUTPUT:
2985 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NONE, arg, Y_MAYBE_STR_ARG, elem_type));
2986 break;
2987 case LY_STMT_MUST:
2988 case LY_STMT_WHEN:
2989 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_CONDITION, arg, Y_MAYBE_STR_ARG, elem_type));
2990 break;
2991 case LY_STMT_NAMESPACE:
2992 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_URI, arg, Y_MAYBE_STR_ARG, elem_type));
2993 break;
2994 case LY_STMT_REVISION:
2995 case LY_STMT_REVISION_DATE:
2996 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_DATE, arg, Y_MAYBE_STR_ARG, elem_type));
2997 break;
2998 case LY_STMT_UNIQUE:
2999 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_TAG, arg, Y_MAYBE_STR_ARG, elem_type));
3000 break;
3001 /* argument is mapped to yin element */
3002 case LY_STMT_CONTACT:
3003 case LY_STMT_DESCRIPTION:
3004 case LY_STMT_ORGANIZATION:
3005 case LY_STMT_REFERENCE:
3006 case LY_STMT_ERROR_MESSAGE:
3007 /* there shouldn't be any attribute, argument is supposed to be first subelement */
3008 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NONE, arg, Y_MAYBE_STR_ARG, elem_type));
3009
3010 /* no content */
3011 assert(ctx->xmlctx->status == LYXML_ELEM_CONTENT);
3012 if (ctx->xmlctx->ws_only) {
3013 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3014 }
3015 if (((ctx->xmlctx->status == LYXML_ELEM_CONTENT) && !ctx->xmlctx->ws_only) || (ctx->xmlctx->status != LYXML_ELEMENT)) {
3016 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_FIRT_SUBELEM,
3017 elem_type == LY_STMT_ERROR_MESSAGE ? "value" : "text", ly_stmt2str(elem_type));
3018 return LY_EVALID;
3019 }
3020
3021 /* parse child element */
3022 child = yin_match_keyword(ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, ctx->xmlctx->prefix, ctx->xmlctx->prefix_len, elem_type);
3023 if (((elem_type == LY_STMT_ERROR_MESSAGE) && (child != LY_STMT_ARG_VALUE)) ||
3024 ((elem_type != LY_STMT_ERROR_MESSAGE) && (child != LY_STMT_ARG_TEXT))) {
3025 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_UNEXP_SUBELEM, ctx->xmlctx->name_len, ctx->xmlctx->name,
3026 ly_stmt2str(elem_type));
3027 return LY_EVALID;
3028 }
3029 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3030
3031 /* no attributes expected? TODO */
3032 while (ctx->xmlctx->status == LYXML_ATTRIBUTE) {
3033 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3034 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3035 }
3036
3037 /* load and save content */
3038 INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic, *arg);
3039 LY_CHECK_RET(!*arg, LY_EMEM);
3040
3041 /* load closing tag of subelement */
3042 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3043
3044 /* if only subelement was parsed as argument, load also closing tag TODO what? */
3045 /*if (ctx->xmlctx->status == LYXML_ELEMENT) {
3046 LY_CHECK_RET(lyxml_get_element(&ctx->xmlctx, data, &prefix, &prefix_len, &name, &name_len));
3047 }*/
3048 break;
3049 default:
3050 LOGINT(ctx->xmlctx->ctx);
3051 return LY_EINT;
3052 }
3053
3054 return LY_SUCCESS;
3055}
3056
3057/**
3058 * @brief Parse yin element into generic structure.
3059 *
3060 * @param[in,out] ctx Yin parser context for XML context, logging, and to store current state.
3061 * @param[in] parent Identification of parent element.
3062 * @param[out] element Where the element structure should be stored.
Michal Vaskoafac7822020-10-20 14:22:26 +02003063 * @return LY_ERR values.
3064 */
3065LY_ERR
3066yin_parse_element_generic(struct lys_yin_parser_ctx *ctx, enum ly_stmt parent, struct lysp_stmt **element)
3067{
3068 LY_ERR ret = LY_SUCCESS;
3069 struct lysp_stmt *last = NULL, *new = NULL;
Michal Vaskofc2cd072021-02-24 13:17:17 +01003070 char *id;
Michal Vaskoafac7822020-10-20 14:22:26 +02003071
3072 assert(ctx->xmlctx->status == LYXML_ELEMENT);
3073
3074 /* allocate new structure for element */
3075 *element = calloc(1, sizeof(**element));
3076 LY_CHECK_ERR_GOTO(!(*element), LOGMEM(ctx->xmlctx->ctx); ret = LY_EMEM, cleanup);
Michal Vaskofc2cd072021-02-24 13:17:17 +01003077
3078 /* store identifier */
3079 if (ctx->xmlctx->prefix_len) {
3080 if (asprintf(&id, "%.*s:%.*s", (int)ctx->xmlctx->prefix_len, ctx->xmlctx->prefix, (int)ctx->xmlctx->name_len,
3081 ctx->xmlctx->name) == -1) {
3082 LOGMEM(ctx->xmlctx->ctx);
3083 ret = LY_EMEM;
3084 goto cleanup;
3085 }
3086 LY_CHECK_GOTO(ret = lydict_insert_zc(ctx->xmlctx->ctx, id, &(*element)->stmt), cleanup);
3087
3088 /* store prefix data for the statement */
Radek Krejci8df109d2021-04-23 12:19:08 +02003089 LY_CHECK_GOTO(ret = ly_store_prefix_data(ctx->xmlctx->ctx, (*element)->stmt, strlen((*element)->stmt), LY_VALUE_XML,
Michal Vaskofc2cd072021-02-24 13:17:17 +01003090 &ctx->xmlctx->ns, &(*element)->format, &(*element)->prefix_data), cleanup);
3091 } else {
3092 LY_CHECK_GOTO(ret = lydict_insert(ctx->xmlctx->ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, &(*element)->stmt), cleanup);
3093 }
Michal Vaskoafac7822020-10-20 14:22:26 +02003094
3095 (*element)->kw = yin_match_keyword(ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, ctx->xmlctx->prefix,
3096 ctx->xmlctx->prefix_len, parent);
3097
3098 last = (*element)->child;
3099 if ((*element)->kw == LY_STMT_NONE) {
3100 /* unrecognized element */
3101 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_UNEXP_SUBELEM, ctx->xmlctx->name_len, ctx->xmlctx->name,
3102 ly_stmt2str(parent));
3103 ret = LY_EVALID;
3104 goto cleanup;
3105 } else if ((*element)->kw != LY_STMT_EXTENSION_INSTANCE) {
3106 /* element is known yang keyword, which means argument can be parsed correctly. */
3107 ret = yin_parse_extension_instance_arg(ctx, (*element)->kw, &(*element)->arg);
3108 LY_CHECK_GOTO(ret, cleanup);
3109 } else {
3110 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3111
3112 /* load attributes in generic way, save all attributes in linked list */
3113 while (ctx->xmlctx->status == LYXML_ATTRIBUTE) {
3114 new = calloc(1, sizeof(*last));
3115 LY_CHECK_ERR_GOTO(!new, LOGMEM(ctx->xmlctx->ctx); ret = LY_EMEM, cleanup);
3116 if (!(*element)->child) {
3117 /* save first */
3118 (*element)->child = new;
3119 } else {
3120 last->next = new;
3121 }
3122 last = new;
3123
3124 last->flags |= LYS_YIN_ATTR;
3125 LY_CHECK_GOTO(ret = lydict_insert(ctx->xmlctx->ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, &last->stmt), cleanup);
3126 last->kw = LY_STMT_NONE;
3127 /* attributes with prefix are ignored */
3128 if (!ctx->xmlctx->prefix) {
3129 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3130
3131 INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic, last->arg);
3132 LY_CHECK_ERR_GOTO(!last->arg, ret = LY_EMEM, cleanup);
3133 } else {
3134 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3135 }
3136 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3137 }
3138 }
3139
3140 if ((ctx->xmlctx->status != LYXML_ELEM_CONTENT) || ctx->xmlctx->ws_only) {
3141 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3142 while (ctx->xmlctx->status == LYXML_ELEMENT) {
3143 /* parse subelements */
3144 ret = yin_parse_element_generic(ctx, (*element)->kw, &new);
3145 LY_CHECK_GOTO(ret, cleanup);
3146 if (!(*element)->child) {
3147 /* save first */
3148 (*element)->child = new;
3149 } else {
3150 last->next = new;
3151 }
3152 last = new;
3153
3154 assert(ctx->xmlctx->status == LYXML_ELEM_CLOSE);
3155 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3156 }
3157 } else {
3158 /* save element content */
3159 if (ctx->xmlctx->value_len) {
3160 INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic, (*element)->arg);
3161 LY_CHECK_ERR_GOTO(!(*element)->arg, ret = LY_EMEM, cleanup);
Michal Vaskofc2cd072021-02-24 13:17:17 +01003162
3163 /* store prefix data for the argument as well */
Radek Krejci8df109d2021-04-23 12:19:08 +02003164 LY_CHECK_GOTO(ret = ly_store_prefix_data(ctx->xmlctx->ctx, (*element)->arg, strlen((*element)->arg), LY_VALUE_XML,
Michal Vaskofc2cd072021-02-24 13:17:17 +01003165 &ctx->xmlctx->ns, &(*element)->format, &(*element)->prefix_data), cleanup);
Michal Vaskoafac7822020-10-20 14:22:26 +02003166 }
3167
3168 /* read closing tag */
3169 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3170 }
3171
3172cleanup:
3173 return ret;
3174}
3175
3176/**
3177 * @brief Parse instance of extension.
3178 *
3179 * @param[in,out] ctx Yin parser context for logging and to store current state.
Radek Krejcifc596f92021-02-26 22:40:26 +01003180 * @param[in] subelem The statement this extension instance is a subelement of.
Michal Vaskoafac7822020-10-20 14:22:26 +02003181 * @param[in] subelem_index Index of the keyword instance this extension instance is a subelement of
3182 * @param[in,out] exts Extension instance to add to.
Michal Vaskoafac7822020-10-20 14:22:26 +02003183 * @return LY_ERR values.
3184 */
3185LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +01003186yin_parse_extension_instance(struct lys_yin_parser_ctx *ctx, enum ly_stmt subelem, LY_ARRAY_COUNT_TYPE subelem_index,
Michal Vaskoafac7822020-10-20 14:22:26 +02003187 struct lysp_ext_instance **exts)
3188{
3189 struct lysp_ext_instance *e;
3190 struct lysp_stmt *last_subelem = NULL, *new_subelem = NULL;
Michal Vaskofc2cd072021-02-24 13:17:17 +01003191 char *ext_name;
Michal Vaskoafac7822020-10-20 14:22:26 +02003192
3193 assert(ctx->xmlctx->status == LYXML_ELEMENT);
Radek Krejci3aac9a72020-12-01 12:24:26 +01003194 assert(exts);
Michal Vaskoafac7822020-10-20 14:22:26 +02003195
3196 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *exts, e, LY_EMEM);
3197
Michal Vaskofc2cd072021-02-24 13:17:17 +01003198 if (!ctx->xmlctx->prefix_len) {
3199 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Extension instance \"%*.s\" without the mandatory prefix.",
3200 (int)ctx->xmlctx->name_len, ctx->xmlctx->name);
3201 return LY_EVALID;
3202 }
3203
3204 /* store prefixed name */
3205 if (asprintf(&ext_name, "%.*s:%.*s", (int)ctx->xmlctx->prefix_len, ctx->xmlctx->prefix, (int)ctx->xmlctx->name_len,
3206 ctx->xmlctx->name) == -1) {
3207 LOGMEM(ctx->xmlctx->ctx);
3208 return LY_EMEM;
3209 }
3210 LY_CHECK_RET(lydict_insert_zc(ctx->xmlctx->ctx, ext_name, &e->name));
3211
3212 /* store prefix data for the name */
Radek Krejci8df109d2021-04-23 12:19:08 +02003213 LY_CHECK_RET(ly_store_prefix_data(ctx->xmlctx->ctx, e->name, strlen(e->name), LY_VALUE_XML, &ctx->xmlctx->ns,
Michal Vaskofc2cd072021-02-24 13:17:17 +01003214 &e->format, &e->prefix_data));
3215
Radek Krejciab430862021-03-02 20:13:40 +01003216 e->parent_stmt = subelem;
3217 e->parent_stmt_index = subelem_index;
Michal Vaskofc2cd072021-02-24 13:17:17 +01003218
Michal Vaskoafac7822020-10-20 14:22:26 +02003219 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3220
3221 /* store attributes as subelements */
3222 while (ctx->xmlctx->status == LYXML_ATTRIBUTE) {
3223 if (!ctx->xmlctx->prefix) {
3224 new_subelem = calloc(1, sizeof(*new_subelem));
3225 if (!e->child) {
3226 e->child = new_subelem;
3227 } else {
3228 last_subelem->next = new_subelem;
3229 }
3230 last_subelem = new_subelem;
3231
3232 last_subelem->flags |= LYS_YIN_ATTR;
3233 LY_CHECK_RET(lydict_insert(ctx->xmlctx->ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, &last_subelem->stmt));
3234 LY_CHECK_RET(!last_subelem->stmt, LY_EMEM);
3235 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3236
Michal Vaskofc2cd072021-02-24 13:17:17 +01003237 INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic,
3238 last_subelem->arg);
Michal Vaskoafac7822020-10-20 14:22:26 +02003239 LY_CHECK_RET(!last_subelem->arg, LY_EMEM);
3240 } else {
3241 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3242 }
3243
3244 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3245 }
3246
3247 /* parse subelements */
3248 assert(ctx->xmlctx->status == LYXML_ELEM_CONTENT);
3249 if (ctx->xmlctx->ws_only) {
3250 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3251 while (ctx->xmlctx->status == LYXML_ELEMENT) {
3252 LY_CHECK_RET(yin_parse_element_generic(ctx, LY_STMT_EXTENSION_INSTANCE, &new_subelem));
3253 if (!e->child) {
3254 e->child = new_subelem;
3255 } else {
3256 last_subelem->next = new_subelem;
3257 }
3258 last_subelem = new_subelem;
3259
3260 assert(ctx->xmlctx->status == LYXML_ELEM_CLOSE);
3261 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3262 }
3263 } else if (ctx->xmlctx->value_len) {
3264 /* save text content */
3265 INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic, e->argument);
3266 LY_CHECK_RET(!e->argument, LY_EMEM);
3267
Michal Vaskofc2cd072021-02-24 13:17:17 +01003268 /* store prefix data for the argument as well */
Radek Krejci8df109d2021-04-23 12:19:08 +02003269 LY_CHECK_RET(ly_store_prefix_data(ctx->xmlctx->ctx, e->argument, strlen(e->argument), LY_VALUE_XML,
Radek Krejci07a55962021-03-02 20:16:43 +01003270 &ctx->xmlctx->ns, &e->format, &e->prefix_data));
Michal Vaskofc2cd072021-02-24 13:17:17 +01003271
Michal Vaskoafac7822020-10-20 14:22:26 +02003272 /* parser next */
3273 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3274 }
3275
aPiecek60d9d672021-04-27 15:49:57 +02003276 e->parsed = NULL;
3277
Michal Vaskoafac7822020-10-20 14:22:26 +02003278 return LY_SUCCESS;
3279}
3280
3281/**
3282 * @brief Generic function for content parsing
3283 *
3284 * @param[in,out] ctx Yin parser context for logging and to store current state.
3285 * @param[in] subelem_info array of valid subelement types and meta information
3286 * @param[in] subelem_info_size Size of subelem_info array.
3287 * @param[in] current_element Type of current element.
3288 * @param[out] text_content Where the text content of element should be stored if any. Text content is ignored if set to NULL.
3289 * @param[in,out] exts Extension instance to add to. Can be set to null if element cannot have extension as subelements.
Michal Vaskoafac7822020-10-20 14:22:26 +02003290 * @return LY_ERR values.
3291 */
David Sedlákd6e56892019-07-01 15:40:24 +02003292LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01003293yin_parse_content(struct lys_yin_parser_ctx *ctx, struct yin_subelement *subelem_info, size_t subelem_info_size,
Radek Krejci0f969882020-08-21 16:56:47 +02003294 enum ly_stmt current_element, const char **text_content, struct lysp_ext_instance **exts)
David Sedlákd6e56892019-07-01 15:40:24 +02003295{
3296 LY_ERR ret = LY_SUCCESS;
Michal Vaskob36053d2020-03-26 15:49:30 +01003297 enum LYXML_PARSER_STATUS next_status;
Radek Krejcid6b76452019-09-03 17:03:03 +02003298 enum ly_stmt kw = LY_STMT_NONE, last_kw = LY_STMT_NONE;
David Sedlák1af868e2019-07-17 17:03:14 +02003299 struct yin_subelement *subelem = NULL;
David Sedlák09e18c92019-07-18 11:17:11 +02003300
Michal Vaskob36053d2020-03-26 15:49:30 +01003301 assert(ctx->xmlctx->status == LYXML_ELEM_CONTENT);
David Sedlákd6e56892019-07-01 15:40:24 +02003302
Michal Vaskob36053d2020-03-26 15:49:30 +01003303 if (ctx->xmlctx->ws_only) {
3304 /* check whether there are any children */
3305 LY_CHECK_GOTO(ret = lyxml_ctx_peek(ctx->xmlctx, &next_status), cleanup);
3306 } else {
3307 /* we want to parse the value */
3308 next_status = LYXML_ELEM_CLOSE;
3309 }
3310
3311 if (next_status == LYXML_ELEMENT) {
3312 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3313
3314 /* current element has subelements as content */
3315 while (ctx->xmlctx->status == LYXML_ELEMENT) {
3316 /* match keyword */
3317 last_kw = kw;
3318 kw = yin_match_keyword(ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, ctx->xmlctx->prefix,
Michal Vasko69730152020-10-09 16:30:07 +02003319 ctx->xmlctx->prefix_len, current_element);
Michal Vaskob36053d2020-03-26 15:49:30 +01003320
3321 /* check if this element can be child of current element */
3322 subelem = get_record(kw, subelem_info_size, subelem_info);
3323 if (!subelem) {
Michal Vasko69730152020-10-09 16:30:07 +02003324 if ((current_element == LY_STMT_DEVIATE) && isdevsub(kw)) {
Michal Vaskob36053d2020-03-26 15:49:30 +01003325 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INDEV_YIN, ly_stmt2str(kw));
3326 } else {
3327 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_UNEXP_SUBELEM, ctx->xmlctx->name_len,
Michal Vasko69730152020-10-09 16:30:07 +02003328 ctx->xmlctx->name, ly_stmt2str(current_element));
Michal Vaskob36053d2020-03-26 15:49:30 +01003329 }
3330 ret = LY_EVALID;
3331 goto cleanup;
3332 }
3333
3334 /* relative order is required only in module and submodule sub-elements */
Michal Vasko69730152020-10-09 16:30:07 +02003335 if ((current_element == LY_STMT_MODULE) || (current_element == LY_STMT_SUBMODULE)) {
Michal Vaskob36053d2020-03-26 15:49:30 +01003336 ret = yin_check_relative_order(ctx, last_kw, kw, current_element);
3337 LY_CHECK_GOTO(ret, cleanup);
3338 }
3339
3340 /* flag check */
3341 if ((subelem->flags & YIN_SUBELEM_UNIQUE) && (subelem->flags & YIN_SUBELEM_PARSED)) {
3342 /* subelement uniquenes */
3343 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_SUBELEM_REDEF, ly_stmt2str(kw), ly_stmt2str(current_element));
3344 return LY_EVALID;
3345 }
3346 if (subelem->flags & YIN_SUBELEM_FIRST) {
3347 /* subelement is supposed to be defined as first subelement */
3348 ret = yin_check_subelem_first_constraint(ctx, subelem_info, subelem_info_size, current_element, subelem);
3349 LY_CHECK_GOTO(ret, cleanup);
3350 }
3351 if (subelem->flags & YIN_SUBELEM_VER2) {
3352 /* subelement is supported only in version 1.1 or higher */
Michal Vasko8a67eff2021-12-07 14:04:47 +01003353 if (PARSER_CUR_PMOD(ctx)->version < LYS_VERSION_1_1) {
Michal Vaskob36053d2020-03-26 15:49:30 +01003354 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INSUBELEM2, ly_stmt2str(kw), ly_stmt2str(current_element));
David Sedlákd6e56892019-07-01 15:40:24 +02003355 ret = LY_EVALID;
3356 goto cleanup;
3357 }
David Sedlákd6e56892019-07-01 15:40:24 +02003358 }
Michal Vaskob36053d2020-03-26 15:49:30 +01003359 /* note that element was parsed for easy uniqueness check in next iterations */
3360 subelem->flags |= YIN_SUBELEM_PARSED;
3361
3362 switch (kw) {
3363 /* call responsible function */
3364 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003365 ret = yin_parse_extension_instance(ctx, current_element,
Michal Vasko69730152020-10-09 16:30:07 +02003366 (subelem->dest) ? *((LY_ARRAY_COUNT_TYPE *)subelem->dest) : 0, exts);
Michal Vaskob36053d2020-03-26 15:49:30 +01003367 break;
3368 case LY_STMT_ACTION:
3369 case LY_STMT_RPC:
3370 ret = yin_parse_action(ctx, (struct tree_node_meta *)subelem->dest);
3371 break;
3372 case LY_STMT_ANYDATA:
3373 case LY_STMT_ANYXML:
3374 ret = yin_parse_any(ctx, kw, (struct tree_node_meta *)subelem->dest);
3375 break;
3376 case LY_STMT_ARGUMENT:
3377 ret = yin_parse_argument(ctx, (struct yin_argument_meta *)subelem->dest, exts);
3378 break;
3379 case LY_STMT_AUGMENT:
3380 ret = yin_parse_augment(ctx, (struct tree_node_meta *)subelem->dest);
3381 break;
3382 case LY_STMT_BASE:
3383 ret = yin_parse_base(ctx, current_element, subelem->dest, exts);
3384 break;
3385 case LY_STMT_BELONGS_TO:
3386 ret = yin_parse_belongs_to(ctx, (struct lysp_submodule *)subelem->dest, exts);
3387 break;
3388 case LY_STMT_BIT:
3389 ret = yin_parse_bit(ctx, (struct lysp_type *)subelem->dest);
3390 break;
3391 case LY_STMT_CASE:
3392 ret = yin_parse_case(ctx, (struct tree_node_meta *)subelem->dest);
3393 break;
3394 case LY_STMT_CHOICE:
3395 ret = yin_parse_choice(ctx, (struct tree_node_meta *)subelem->dest);
3396 break;
3397 case LY_STMT_CONFIG:
3398 ret = yin_parse_config(ctx, (uint16_t *)subelem->dest, exts);
3399 break;
3400 case LY_STMT_CONTACT:
3401 case LY_STMT_DESCRIPTION:
3402 case LY_STMT_ORGANIZATION:
3403 case LY_STMT_REFERENCE:
3404 ret = yin_parse_meta(ctx, kw, (const char **)subelem->dest, exts);
3405 break;
3406 case LY_STMT_CONTAINER:
3407 ret = yin_parse_container(ctx, (struct tree_node_meta *)subelem->dest);
3408 break;
3409 case LY_STMT_DEFAULT:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003410 ret = yin_parse_qname(ctx, kw, subelem, exts);
3411 break;
Michal Vaskob36053d2020-03-26 15:49:30 +01003412 case LY_STMT_ERROR_APP_TAG:
3413 case LY_STMT_KEY:
3414 case LY_STMT_PRESENCE:
3415 ret = yin_parse_simple_elem(ctx, kw, subelem, YIN_ARG_VALUE, Y_STR_ARG, exts);
3416 break;
3417 case LY_STMT_DEVIATE:
3418 ret = yin_parse_deviate(ctx, (struct lysp_deviate **)subelem->dest);
3419 break;
3420 case LY_STMT_DEVIATION:
3421 ret = yin_parse_deviation(ctx, (struct lysp_deviation **)subelem->dest);
3422 break;
3423 case LY_STMT_ENUM:
3424 ret = yin_parse_enum(ctx, (struct lysp_type *)subelem->dest);
3425 break;
3426 case LY_STMT_ERROR_MESSAGE:
3427 ret = yin_parse_err_msg(ctx, (const char **)subelem->dest, exts);
3428 break;
3429 case LY_STMT_EXTENSION:
3430 ret = yin_parse_extension(ctx, (struct lysp_ext **)subelem->dest);
3431 break;
3432 case LY_STMT_FEATURE:
3433 ret = yin_parse_feature(ctx, (struct lysp_feature **)subelem->dest);
3434 break;
3435 case LY_STMT_FRACTION_DIGITS:
3436 ret = yin_parse_fracdigits(ctx, (struct lysp_type *)subelem->dest);
3437 break;
3438 case LY_STMT_GROUPING:
3439 ret = yin_parse_grouping(ctx, (struct tree_node_meta *)subelem->dest);
3440 break;
3441 case LY_STMT_IDENTITY:
3442 ret = yin_parse_identity(ctx, (struct lysp_ident **)subelem->dest);
3443 break;
Radek Išabe8a6532021-03-29 08:54:22 +02003444 case LY_STMT_UNIQUE:
Michal Vaskob36053d2020-03-26 15:49:30 +01003445 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003446 ret = yin_parse_qname(ctx, kw, subelem, exts);
3447 break;
Michal Vaskob36053d2020-03-26 15:49:30 +01003448 case LY_STMT_UNITS:
3449 ret = yin_parse_simple_elem(ctx, kw, subelem, YIN_ARG_NAME, Y_STR_ARG, exts);
3450 break;
3451 case LY_STMT_IMPORT:
3452 ret = yin_parse_import(ctx, (struct import_meta *)subelem->dest);
3453 break;
3454 case LY_STMT_INCLUDE:
Michal Vasko8a67eff2021-12-07 14:04:47 +01003455 if ((current_element == LY_STMT_SUBMODULE) && (PARSER_CUR_PMOD(ctx)->version == LYS_VERSION_1_1)) {
Radek Krejcieeee95c2021-01-19 10:57:22 +01003456 LOGWRN(PARSER_CTX(ctx), "YANG version 1.1 expects all includes in main module, includes in submodules (%s) are not necessary.",
Michal Vasko8a67eff2021-12-07 14:04:47 +01003457 ((struct lysp_submodule *)PARSER_CUR_PMOD(ctx))->name);
Radek Krejcieeee95c2021-01-19 10:57:22 +01003458 }
Michal Vaskob36053d2020-03-26 15:49:30 +01003459 ret = yin_parse_include(ctx, (struct include_meta *)subelem->dest);
3460 break;
3461 case LY_STMT_INPUT:
3462 case LY_STMT_OUTPUT:
3463 ret = yin_parse_inout(ctx, kw, (struct inout_meta *)subelem->dest);
3464 break;
3465 case LY_STMT_LEAF:
3466 ret = yin_parse_leaf(ctx, (struct tree_node_meta *)subelem->dest);
3467 break;
3468 case LY_STMT_LEAF_LIST:
3469 ret = yin_parse_leaflist(ctx, (struct tree_node_meta *)subelem->dest);
3470 break;
3471 case LY_STMT_LENGTH:
3472 ret = yin_parse_length(ctx, (struct lysp_type *)subelem->dest);
3473 break;
3474 case LY_STMT_LIST:
3475 ret = yin_parse_list(ctx, (struct tree_node_meta *)subelem->dest);
3476 break;
3477 case LY_STMT_MANDATORY:
3478 ret = yin_parse_mandatory(ctx, (uint16_t *)subelem->dest, exts);
3479 break;
3480 case LY_STMT_MAX_ELEMENTS:
3481 case LY_STMT_MIN_ELEMENTS:
3482 ret = yin_parse_minmax(ctx, current_element, kw, subelem->dest);
3483 break;
3484 case LY_STMT_MODIFIER:
3485 ret = yin_parse_modifier(ctx, (const char **)subelem->dest, exts);
3486 break;
3487 case LY_STMT_MUST:
3488 ret = yin_parse_must(ctx, (struct lysp_restr **)subelem->dest);
3489 break;
3490 case LY_STMT_NAMESPACE:
3491 ret = yin_parse_simple_elem(ctx, kw, subelem, YIN_ARG_URI, Y_STR_ARG, exts);
3492 break;
3493 case LY_STMT_NOTIFICATION:
3494 ret = yin_parse_notification(ctx, (struct tree_node_meta *)subelem->dest);
3495 break;
3496 case LY_STMT_ORDERED_BY:
3497 ret = yin_parse_orderedby(ctx, (uint16_t *)subelem->dest, exts);
3498 break;
3499 case LY_STMT_PATH:
3500 ret = yin_parse_path(ctx, kw, (struct lysp_type *)subelem->dest);
3501 break;
3502 case LY_STMT_PATTERN:
3503 ret = yin_parse_pattern(ctx, (struct lysp_type *)subelem->dest);
3504 break;
3505 case LY_STMT_VALUE:
3506 case LY_STMT_POSITION:
3507 ret = yin_parse_value_pos(ctx, kw, (struct lysp_type_enum *)subelem->dest);
3508 break;
3509 case LY_STMT_PREFIX:
3510 ret = yin_parse_simple_elem(ctx, kw, subelem, YIN_ARG_VALUE, Y_IDENTIF_ARG, exts);
3511 break;
3512 case LY_STMT_RANGE:
3513 ret = yin_parse_range(ctx, (struct lysp_type *)subelem->dest);
3514 break;
3515 case LY_STMT_REFINE:
3516 ret = yin_parse_refine(ctx, (struct lysp_refine **)subelem->dest);
3517 break;
3518 case LY_STMT_REQUIRE_INSTANCE:
3519 ret = yin_pasrse_reqinstance(ctx, (struct lysp_type *)subelem->dest);
3520 break;
3521 case LY_STMT_REVISION:
3522 ret = yin_parse_revision(ctx, (struct lysp_revision **)subelem->dest);
3523 break;
3524 case LY_STMT_REVISION_DATE:
3525 ret = yin_parse_revision_date(ctx, (char *)subelem->dest, exts);
3526 break;
3527 case LY_STMT_STATUS:
3528 ret = yin_parse_status(ctx, (uint16_t *)subelem->dest, exts);
3529 break;
3530 case LY_STMT_TYPE:
3531 ret = yin_parse_type(ctx, current_element, subelem);
3532 break;
3533 case LY_STMT_TYPEDEF:
3534 ret = yin_parse_typedef(ctx, (struct tree_node_meta *)subelem->dest);
3535 break;
Michal Vaskob36053d2020-03-26 15:49:30 +01003536 case LY_STMT_USES:
3537 ret = yin_parse_uses(ctx, (struct tree_node_meta *)subelem->dest);
3538 break;
3539 case LY_STMT_WHEN:
3540 ret = yin_parse_when(ctx, (struct lysp_when **)subelem->dest);
3541 break;
3542 case LY_STMT_YANG_VERSION:
3543 ret = yin_parse_yangversion(ctx, (uint8_t *)subelem->dest, exts);
3544 break;
3545 case LY_STMT_YIN_ELEMENT:
3546 ret = yin_parse_yin_element(ctx, (uint16_t *)subelem->dest, exts);
3547 break;
3548 case LY_STMT_ARG_TEXT:
3549 case LY_STMT_ARG_VALUE:
3550 /* TODO what to do with content/attributes? */
3551 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3552 while (ctx->xmlctx->status == LYXML_ATTRIBUTE) {
3553 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3554 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3555 }
3556 ret = yin_parse_content(ctx, NULL, 0, kw, (const char **)subelem->dest, NULL);
3557 break;
3558 default:
3559 LOGINT(ctx->xmlctx->ctx);
3560 ret = LY_EINT;
David Sedlák3ffbc522019-07-02 17:49:28 +02003561 }
Michal Vaskob36053d2020-03-26 15:49:30 +01003562 LY_CHECK_GOTO(ret, cleanup);
3563 subelem = NULL;
3564
3565 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
David Sedlákd6e56892019-07-01 15:40:24 +02003566 }
Michal Vaskob36053d2020-03-26 15:49:30 +01003567 } else {
3568 LY_CHECK_RET(ret);
3569 /* elements with text or none content */
3570 /* save text content, if text_content isn't set, it's just ignored */
3571 /* no resources are allocated in this branch, no need to use cleanup label */
3572 LY_CHECK_RET(yin_validate_value(ctx, Y_STR_ARG));
3573 if (text_content) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02003574 INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic, *text_content);
Michal Vaskob36053d2020-03-26 15:49:30 +01003575 LY_CHECK_RET(!*text_content, LY_EMEM);
3576 }
3577
3578 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
David Sedlákd6e56892019-07-01 15:40:24 +02003579 }
Michal Vaskob36053d2020-03-26 15:49:30 +01003580
David Sedlák8b754462019-07-25 16:22:13 +02003581 /* mandatory subelemnts are checked only after whole element was succesfully parsed */
3582 LY_CHECK_RET(yin_check_subelem_mandatory_constraint(ctx, subelem_info, subelem_info_size, current_element));
David Sedlákd6e56892019-07-01 15:40:24 +02003583
3584cleanup:
David Sedlákd6e56892019-07-01 15:40:24 +02003585 return ret;
3586}
3587
David Sedlákaa98bba2019-09-12 11:52:14 +02003588/**
Michal Vaskoafac7822020-10-20 14:22:26 +02003589 * @brief Parse module element.
David Sedlákaa98bba2019-09-12 11:52:14 +02003590 *
3591 * @param[in,out] ctx Yin parser context for logging and to store current state.
Michal Vaskoafac7822020-10-20 14:22:26 +02003592 * @param[out] mod Parsed module structure.
David Sedlákaa98bba2019-09-12 11:52:14 +02003593 * @return LY_ERR values.
3594 */
David Sedlák1e2cdd02019-06-27 14:17:43 +02003595LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01003596yin_parse_mod(struct lys_yin_parser_ctx *ctx, struct lysp_module *mod)
David Sedlák3b4db242018-10-19 16:11:01 +02003597{
David Sedlák81497a32019-08-13 16:56:26 +02003598 LY_ERR ret = LY_SUCCESS;
3599 struct yin_subelement *subelems = NULL;
Michal Vasko8dc31992021-02-22 10:30:47 +01003600 const struct lysp_submodule *dup;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003601 size_t subelems_size;
David Sedlák3b4db242018-10-19 16:11:01 +02003602
Michal Vaskoc3781c32020-10-06 14:04:08 +02003603 mod->is_submod = 0;
3604
Michal Vaskob36053d2020-03-26 15:49:30 +01003605 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3606 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &mod->mod->name, Y_IDENTIF_ARG, LY_STMT_MODULE));
Radek Krejcif13b87b2020-12-01 22:02:17 +01003607 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 28, NULL, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02003608 LY_STMT_ANYDATA, &mod->data, YIN_SUBELEM_VER2,
3609 LY_STMT_ANYXML, &mod->data, 0,
3610 LY_STMT_AUGMENT, &mod->augments, 0,
3611 LY_STMT_CHOICE, &mod->data, 0,
3612 LY_STMT_CONTACT, &mod->mod->contact, YIN_SUBELEM_UNIQUE,
3613 LY_STMT_CONTAINER, &mod->data, 0,
3614 LY_STMT_DESCRIPTION, &mod->mod->dsc, YIN_SUBELEM_UNIQUE,
3615 LY_STMT_DEVIATION, &mod->deviations, 0,
3616 LY_STMT_EXTENSION, &mod->extensions, 0,
3617 LY_STMT_FEATURE, &mod->features, 0,
3618 LY_STMT_GROUPING, &mod->groupings, 0,
3619 LY_STMT_IDENTITY, &mod->identities, 0,
3620 LY_STMT_IMPORT, mod->mod->prefix, &mod->imports, 0,
3621 LY_STMT_INCLUDE, mod->mod->name, &mod->includes, 0,
3622 LY_STMT_LEAF, &mod->data, 0,
3623 LY_STMT_LEAF_LIST, &mod->data, 0,
3624 LY_STMT_LIST, &mod->data, 0,
3625 LY_STMT_NAMESPACE, &mod->mod->ns, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE,
3626 LY_STMT_NOTIFICATION, &mod->notifs, 0,
3627 LY_STMT_ORGANIZATION, &mod->mod->org, YIN_SUBELEM_UNIQUE,
3628 LY_STMT_PREFIX, &mod->mod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE,
3629 LY_STMT_REFERENCE, &mod->mod->ref, YIN_SUBELEM_UNIQUE,
3630 LY_STMT_REVISION, &mod->revs, 0,
3631 LY_STMT_RPC, &mod->rpcs, 0,
3632 LY_STMT_TYPEDEF, &mod->typedefs, 0,
3633 LY_STMT_USES, &mod->data, 0,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003634 LY_STMT_YANG_VERSION, &mod->version, YIN_SUBELEM_UNIQUE,
Michal Vasko69730152020-10-09 16:30:07 +02003635 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
David Sedlák81497a32019-08-13 16:56:26 +02003636
Radek Krejcif13b87b2020-12-01 22:02:17 +01003637 ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_MODULE, NULL, &mod->exts);
3638 subelems_deallocator(subelems_size, subelems);
David Sedláked809f12019-09-13 10:15:11 +02003639 LY_CHECK_RET(ret);
David Sedlák81497a32019-08-13 16:56:26 +02003640
David Sedláked809f12019-09-13 10:15:11 +02003641 /* submodules share the namespace with the module names, so there must not be
3642 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01003643 dup = ly_ctx_get_submodule_latest(ctx->xmlctx->ctx, mod->mod->name);
David Sedláked809f12019-09-13 10:15:11 +02003644 if (dup) {
Radek Krejci854e1552020-12-21 15:05:23 +01003645 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_NAME2_COL, "module", "submodule", mod->mod->name);
David Sedláked809f12019-09-13 10:15:11 +02003646 return LY_EVALID;
3647 }
3648
3649 return LY_SUCCESS;
David Sedlák3b4db242018-10-19 16:11:01 +02003650}
3651
Michal Vaskoafac7822020-10-20 14:22:26 +02003652/**
3653 * @brief Parse submodule element.
3654 *
3655 * @param[in,out] ctx Yin parser context for logging and to store current state.
3656 * @param[in] mod_attrs Attributes of submodule element.
3657 * @param[out] submod Parsed submodule structure.
Michal Vaskoafac7822020-10-20 14:22:26 +02003658 * @return LY_ERR values.
3659 */
David Sedlák3b4db242018-10-19 16:11:01 +02003660LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01003661yin_parse_submod(struct lys_yin_parser_ctx *ctx, struct lysp_submodule *submod)
David Sedlák298ff6d2019-07-26 14:29:03 +02003662{
David Sedlák81497a32019-08-13 16:56:26 +02003663 LY_ERR ret = LY_SUCCESS;
3664 struct yin_subelement *subelems = NULL;
Michal Vasko8dc31992021-02-22 10:30:47 +01003665 const struct lysp_submodule *dup;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003666 size_t subelems_size;
David Sedlák298ff6d2019-07-26 14:29:03 +02003667
Michal Vaskoc3781c32020-10-06 14:04:08 +02003668 submod->is_submod = 1;
3669
Michal Vaskob36053d2020-03-26 15:49:30 +01003670 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3671 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &submod->name, Y_IDENTIF_ARG, LY_STMT_SUBMODULE));
Radek Krejcif13b87b2020-12-01 22:02:17 +01003672 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 27, NULL, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02003673 LY_STMT_ANYDATA, &submod->data, YIN_SUBELEM_VER2,
3674 LY_STMT_ANYXML, &submod->data, 0,
3675 LY_STMT_AUGMENT, &submod->augments, 0,
3676 LY_STMT_BELONGS_TO, submod, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE,
3677 LY_STMT_CHOICE, &submod->data, 0,
3678 LY_STMT_CONTACT, &submod->contact, YIN_SUBELEM_UNIQUE,
3679 LY_STMT_CONTAINER, &submod->data, 0,
3680 LY_STMT_DESCRIPTION, &submod->dsc, YIN_SUBELEM_UNIQUE,
3681 LY_STMT_DEVIATION, &submod->deviations, 0,
3682 LY_STMT_EXTENSION, &submod->extensions, 0,
3683 LY_STMT_FEATURE, &submod->features, 0,
3684 LY_STMT_GROUPING, &submod->groupings, 0,
3685 LY_STMT_IDENTITY, &submod->identities, 0,
3686 LY_STMT_IMPORT, submod->prefix, &submod->imports, 0,
3687 LY_STMT_INCLUDE, submod->name, &submod->includes, 0,
3688 LY_STMT_LEAF, &submod->data, 0,
3689 LY_STMT_LEAF_LIST, &submod->data, 0,
3690 LY_STMT_LIST, &submod->data, 0,
3691 LY_STMT_NOTIFICATION, &submod->notifs, 0,
3692 LY_STMT_ORGANIZATION, &submod->org, YIN_SUBELEM_UNIQUE,
3693 LY_STMT_REFERENCE, &submod->ref, YIN_SUBELEM_UNIQUE,
3694 LY_STMT_REVISION, &submod->revs, 0,
3695 LY_STMT_RPC, &submod->rpcs, 0,
3696 LY_STMT_TYPEDEF, &submod->typedefs, 0,
3697 LY_STMT_USES, &submod->data, 0,
3698 LY_STMT_YANG_VERSION, &submod->version, YIN_SUBELEM_UNIQUE,
3699 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
David Sedlák81497a32019-08-13 16:56:26 +02003700
Radek Krejcif13b87b2020-12-01 22:02:17 +01003701 ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_SUBMODULE, NULL, &submod->exts);
3702 subelems_deallocator(subelems_size, subelems);
David Sedláked809f12019-09-13 10:15:11 +02003703 LY_CHECK_RET(ret);
David Sedlák81497a32019-08-13 16:56:26 +02003704
David Sedláked809f12019-09-13 10:15:11 +02003705 /* submodules share the namespace with the module names, so there must not be
3706 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01003707 dup = ly_ctx_get_submodule_latest(ctx->xmlctx->ctx, submod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02003708 if (dup && strcmp(dup->mod->name, submod->mod->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01003709 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_NAME_COL, "submodules", dup->name);
David Sedláked809f12019-09-13 10:15:11 +02003710 return LY_EVALID;
3711 }
3712
3713 return LY_SUCCESS;
David Sedlák298ff6d2019-07-26 14:29:03 +02003714}
3715
3716LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003717yin_parse_submodule(struct lys_yin_parser_ctx **yin_ctx, struct ly_ctx *ctx, struct lys_parser_ctx *main_ctx,
Radek Krejci0f969882020-08-21 16:56:47 +02003718 struct ly_in *in, struct lysp_submodule **submod)
David Sedlák8985a142019-07-31 16:43:06 +02003719{
Radek Krejcid6b76452019-09-03 17:03:03 +02003720 enum ly_stmt kw = LY_STMT_NONE;
David Sedlák8985a142019-07-31 16:43:06 +02003721 LY_ERR ret = LY_SUCCESS;
David Sedlák8985a142019-07-31 16:43:06 +02003722 struct lysp_submodule *mod_p = NULL;
Michal Vaskoc636ea42022-09-16 10:20:31 +02003723 struct lysf_ctx fctx = {.ctx = ctx};
David Sedlák8985a142019-07-31 16:43:06 +02003724
aPiecek56be92a2021-07-01 07:18:10 +02003725 assert(yin_ctx && ctx && main_ctx && in && submod);
3726
David Sedlák8985a142019-07-31 16:43:06 +02003727 /* create context */
3728 *yin_ctx = calloc(1, sizeof **yin_ctx);
David Sedlák1b623122019-08-05 15:27:49 +02003729 LY_CHECK_ERR_RET(!(*yin_ctx), LOGMEM(ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01003730 (*yin_ctx)->format = LYS_IN_YIN;
aPiecek56be92a2021-07-01 07:18:10 +02003731 (*yin_ctx)->main_ctx = main_ctx;
Michal Vasko63f3d842020-07-08 10:10:14 +02003732 LY_CHECK_RET(lyxml_ctx_new(ctx, in, &(*yin_ctx)->xmlctx));
David Sedlák8985a142019-07-31 16:43:06 +02003733
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003734 mod_p = calloc(1, sizeof *mod_p);
3735 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01003736 mod_p->mod = PARSER_CUR_PMOD(main_ctx)->mod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003737 mod_p->parsing = 1;
Michal Vasko8a67eff2021-12-07 14:04:47 +01003738
3739 /* use main context parsed mods adding the current one */
3740 (*yin_ctx)->parsed_mods = main_ctx->parsed_mods;
3741 ly_set_add((*yin_ctx)->parsed_mods, mod_p, 1, NULL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003742
David Sedlák8985a142019-07-31 16:43:06 +02003743 /* check submodule */
Michal Vaskob36053d2020-03-26 15:49:30 +01003744 kw = yin_match_keyword(*yin_ctx, (*yin_ctx)->xmlctx->name, (*yin_ctx)->xmlctx->name_len, (*yin_ctx)->xmlctx->prefix,
Michal Vasko69730152020-10-09 16:30:07 +02003745 (*yin_ctx)->xmlctx->prefix_len, LY_STMT_NONE);
Radek Krejcid6b76452019-09-03 17:03:03 +02003746 if (kw == LY_STMT_MODULE) {
David Sedlák8985a142019-07-31 16:43:06 +02003747 LOGERR(ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
3748 ret = LY_EINVAL;
3749 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02003750 } else if (kw != LY_STMT_SUBMODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02003751 LOGVAL_PARSER((struct lys_parser_ctx *)*yin_ctx, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
David Sedlák8985a142019-07-31 16:43:06 +02003752 ret = LY_EVALID;
3753 goto cleanup;
3754 }
3755
Michal Vaskob36053d2020-03-26 15:49:30 +01003756 ret = yin_parse_submod(*yin_ctx, mod_p);
David Sedlák8985a142019-07-31 16:43:06 +02003757 LY_CHECK_GOTO(ret, cleanup);
3758
David Sedlákbf8a2b72019-08-14 16:48:10 +02003759 /* skip possible trailing whitespaces at end of the input */
Michal Vasko63f3d842020-07-08 10:10:14 +02003760 while (isspace(in->current[0])) {
Radek Krejcidd713ce2021-01-04 23:12:12 +01003761 if (in->current[0] == '\n') {
3762 LY_IN_NEW_LINE(in);
3763 }
Michal Vasko63f3d842020-07-08 10:10:14 +02003764 ly_in_skip(in, 1);
David Sedlák6d781b62019-08-02 15:22:52 +02003765 }
Michal Vasko63f3d842020-07-08 10:10:14 +02003766 if (in->current[0]) {
3767 LOGVAL_PARSER((struct lys_parser_ctx *)*yin_ctx, LY_VCODE_TRAILING_SUBMOD, 15, in->current,
Michal Vasko69730152020-10-09 16:30:07 +02003768 strlen(in->current) > 15 ? "..." : "");
David Sedlák6d781b62019-08-02 15:22:52 +02003769 ret = LY_EVALID;
3770 goto cleanup;
3771 }
3772
David Sedlák8985a142019-07-31 16:43:06 +02003773 mod_p->parsing = 0;
3774 *submod = mod_p;
3775
3776cleanup:
3777 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02003778 lysp_module_free(&fctx, (struct lysp_module *)mod_p);
David Sedlák1b623122019-08-05 15:27:49 +02003779 yin_parser_ctx_free(*yin_ctx);
3780 *yin_ctx = NULL;
David Sedlák8985a142019-07-31 16:43:06 +02003781 }
David Sedlák8985a142019-07-31 16:43:06 +02003782 return ret;
3783}
3784
3785LY_ERR
aPiecekc3e26142021-06-22 14:25:49 +02003786yin_parse_module(struct lys_yin_parser_ctx **yin_ctx, struct ly_in *in, struct lys_module *mod)
David Sedlák3b4db242018-10-19 16:11:01 +02003787{
David Sedláke4889912018-11-02 09:52:40 +01003788 LY_ERR ret = LY_SUCCESS;
Radek Krejcid6b76452019-09-03 17:03:03 +02003789 enum ly_stmt kw = LY_STMT_NONE;
David Sedlák3017da42019-02-15 09:48:04 +01003790 struct lysp_module *mod_p = NULL;
Michal Vaskoc636ea42022-09-16 10:20:31 +02003791 struct lysf_ctx fctx = {.ctx = mod->ctx};
David Sedlák3b4db242018-10-19 16:11:01 +02003792
David Sedlák8985a142019-07-31 16:43:06 +02003793 /* create context */
3794 *yin_ctx = calloc(1, sizeof **yin_ctx);
David Sedlák1b623122019-08-05 15:27:49 +02003795 LY_CHECK_ERR_RET(!(*yin_ctx), LOGMEM(mod->ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01003796 (*yin_ctx)->format = LYS_IN_YIN;
Michal Vasko8a67eff2021-12-07 14:04:47 +01003797 LY_CHECK_ERR_RET(ly_set_new(&(*yin_ctx)->parsed_mods), free(*yin_ctx); LOGMEM(mod->ctx), LY_EMEM);
Michal Vasko63f3d842020-07-08 10:10:14 +02003798 LY_CHECK_RET(lyxml_ctx_new(mod->ctx, in, &(*yin_ctx)->xmlctx));
aPiecek8d4e75d2021-06-24 14:47:06 +02003799 (*yin_ctx)->main_ctx = (struct lys_parser_ctx *)(*yin_ctx);
David Sedlákda8ffa32019-07-08 14:17:10 +02003800
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003801 mod_p = calloc(1, sizeof *mod_p);
3802 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(mod->ctx), cleanup);
3803 mod_p->mod = mod;
Michal Vasko8a67eff2021-12-07 14:04:47 +01003804 ly_set_add((*yin_ctx)->parsed_mods, mod_p, 1, NULL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003805
David Sedlák8985a142019-07-31 16:43:06 +02003806 /* check module */
Michal Vaskob36053d2020-03-26 15:49:30 +01003807 kw = yin_match_keyword(*yin_ctx, (*yin_ctx)->xmlctx->name, (*yin_ctx)->xmlctx->name_len, (*yin_ctx)->xmlctx->prefix,
Michal Vasko69730152020-10-09 16:30:07 +02003808 (*yin_ctx)->xmlctx->prefix_len, LY_STMT_NONE);
Radek Krejcid6b76452019-09-03 17:03:03 +02003809 if (kw == LY_STMT_SUBMODULE) {
David Sedlák8985a142019-07-31 16:43:06 +02003810 LOGERR(mod->ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module.");
David Sedlák3017da42019-02-15 09:48:04 +01003811 ret = LY_EINVAL;
3812 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02003813 } else if (kw != LY_STMT_MODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02003814 LOGVAL_PARSER((struct lys_parser_ctx *)*yin_ctx, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
David Sedlák3017da42019-02-15 09:48:04 +01003815 ret = LY_EVALID;
3816 goto cleanup;
David Sedláke4889912018-11-02 09:52:40 +01003817 }
3818
David Sedlák00250342019-06-21 14:19:39 +02003819 /* parse module substatements */
Michal Vaskob36053d2020-03-26 15:49:30 +01003820 ret = yin_parse_mod(*yin_ctx, mod_p);
David Sedlák3017da42019-02-15 09:48:04 +01003821 LY_CHECK_GOTO(ret, cleanup);
David Sedlák2e411422018-12-17 02:35:39 +01003822
David Sedlákbf8a2b72019-08-14 16:48:10 +02003823 /* skip possible trailing whitespaces at end of the input */
Michal Vasko63f3d842020-07-08 10:10:14 +02003824 while (isspace(in->current[0])) {
Radek Krejcidd713ce2021-01-04 23:12:12 +01003825 if (in->current[0] == '\n') {
3826 LY_IN_NEW_LINE(in);
3827 }
Michal Vasko63f3d842020-07-08 10:10:14 +02003828 ly_in_skip(in, 1);
David Sedlák6d781b62019-08-02 15:22:52 +02003829 }
Michal Vasko63f3d842020-07-08 10:10:14 +02003830 if (in->current[0]) {
3831 LOGVAL_PARSER((struct lys_parser_ctx *)*yin_ctx, LY_VCODE_TRAILING_MOD, 15, in->current,
Michal Vasko69730152020-10-09 16:30:07 +02003832 strlen(in->current) > 15 ? "..." : "");
David Sedlák6d781b62019-08-02 15:22:52 +02003833 ret = LY_EVALID;
3834 goto cleanup;
3835 }
3836
David Sedlák3017da42019-02-15 09:48:04 +01003837 mod->parsed = mod_p;
3838
3839cleanup:
David Sedlák8f7a1172019-06-20 14:42:18 +02003840 if (ret != LY_SUCCESS) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02003841 lysp_module_free(&fctx, mod_p);
David Sedlák1b623122019-08-05 15:27:49 +02003842 yin_parser_ctx_free(*yin_ctx);
3843 *yin_ctx = NULL;
David Sedlák3017da42019-02-15 09:48:04 +01003844 }
David Sedlák2e411422018-12-17 02:35:39 +01003845 return ret;
David Sedlák3b4db242018-10-19 16:11:01 +02003846}