blob: b14a3a997d9ae61e7c83c72694c456917afbef5a [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>
David Sedlák3b4db242018-10-19 16:11:01 +02004 * @brief YIN parser.
5 *
David Sedlákb1ce3f82019-06-05 14:37:26 +02006 * Copyright (c) 2015 - 2019 CESNET, z.s.p.o.
David Sedlák3b4db242018-10-19 16:11:01 +02007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
David Sedlákf824ad52018-10-14 23:58:15 +020013 */
Michal Vaskofc2cd072021-02-24 13:17:17 +010014#define _GNU_SOURCE
15
David Sedlák3ffbc522019-07-02 17:49:28 +020016#include <assert.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020017#include <ctype.h>
18#include <errno.h>
19#include <stdarg.h>
20#include <stdbool.h>
Radek Krejci47fab892020-11-05 17:02:41 +010021#include <stdint.h>
David Sedlák3b4db242018-10-19 16:11:01 +020022#include <stdio.h>
23#include <stdlib.h>
David Sedlák872c7b42018-10-26 13:15:20 +020024#include <string.h>
David Sedlákf824ad52018-10-14 23:58:15 +020025
Radek Krejci535ea9f2020-05-29 16:01:05 +020026#include "common.h"
Michal Vaskofc2cd072021-02-24 13:17:17 +010027#include "compat.h"
David Sedlákf824ad52018-10-14 23:58:15 +020028#include "context.h"
David Sedlákecf5eb82019-06-03 14:12:44 +020029#include "dict.h"
Radek Krejci47fab892020-11-05 17:02:41 +010030#include "in.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020031#include "in_internal.h"
Radek Krejci47fab892020-11-05 17:02:41 +010032#include "log.h"
Michal Vasko63f3d842020-07-08 10:10:14 +020033#include "parser_internal.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020034#include "parser_schema.h"
Michal Vasko69730152020-10-09 16:30:07 +020035#include "path.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020036#include "set.h"
David Sedlákecf5eb82019-06-03 14:12:44 +020037#include "tree.h"
Michal Vaskofc2cd072021-02-24 13:17:17 +010038#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010039#include "tree_edit.h"
David Sedlákecf5eb82019-06-03 14:12:44 +020040#include "tree_schema.h"
David Sedlák3b4db242018-10-19 16:11:01 +020041#include "tree_schema_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020042#include "xml.h"
David Sedlák00250342019-06-21 14:19:39 +020043
Radek Krejci77114102021-03-10 15:21:57 +010044struct lys_glob_unres;
45
David Sedlák2b214ac2019-06-06 16:11:03 +020046/**
47 * @brief check if given string is URI of yin namespace.
David Sedlák8985a142019-07-31 16:43:06 +020048 *
David Sedlák2b214ac2019-06-06 16:11:03 +020049 * @param ns Namespace URI to check.
50 *
51 * @return true if ns equals YIN_NS_URI false otherwise.
52 */
53#define IS_YIN_NS(ns) (strcmp(ns, YIN_NS_URI) == 0)
54
Michal Vaskoafac7822020-10-20 14:22:26 +020055enum yin_argument {
56 YIN_ARG_UNKNOWN = 0, /**< parsed argument can not be matched with any supported yin argument keyword */
57 YIN_ARG_NAME, /**< argument name */
58 YIN_ARG_TARGET_NODE, /**< argument target-node */
59 YIN_ARG_MODULE, /**< argument module */
60 YIN_ARG_VALUE, /**< argument value */
61 YIN_ARG_TEXT, /**< argument text */
62 YIN_ARG_CONDITION, /**< argument condition */
63 YIN_ARG_URI, /**< argument uri */
64 YIN_ARG_DATE, /**< argument data */
65 YIN_ARG_TAG, /**< argument tag */
66 YIN_ARG_NONE /**< empty (special value) */
67};
68
Michal Vasko22df3f02020-08-24 13:29:22 +020069const char * const yin_attr_list[] = {
David Sedlákf6251182019-06-06 10:22:13 +020070 [YIN_ARG_NAME] = "name",
71 [YIN_ARG_TARGET_NODE] = "target-node",
72 [YIN_ARG_MODULE] = "module",
73 [YIN_ARG_VALUE] = "value",
74 [YIN_ARG_TEXT] = "text",
75 [YIN_ARG_CONDITION] = "condition",
76 [YIN_ARG_URI] = "uri",
77 [YIN_ARG_DATE] = "date",
78 [YIN_ARG_TAG] = "tag",
David Sedlák071f7662019-09-12 02:02:51 +020079 [YIN_ARG_NONE] = "none",
David Sedlákf6251182019-06-06 10:22:13 +020080};
81
Michal Vaskoafac7822020-10-20 14:22:26 +020082#define yin_attr2str(STMT) yin_attr_list[STMT]
83
84#define VALID_VALS1 " Only valid value is \"%s\"."
85#define VALID_VALS2 " Valid values are \"%s\" and \"%s\"."
86#define VALID_VALS3 " Valid values are \"%s\", \"%s\" and \"%s\"."
87#define VALID_VALS4 " Valid values are \"%s\", \"%s\", \"%s\" and \"%s\"."
88
89/* shortcut to determin if keyword can in general be subelement of deviation regardles of it's type */
90#define isdevsub(kw) (kw == LY_STMT_CONFIG || kw == LY_STMT_DEFAULT || kw == LY_STMT_MANDATORY || \
91 kw == LY_STMT_MAX_ELEMENTS || kw == LY_STMT_MIN_ELEMENTS || \
92 kw == LY_STMT_MUST || kw == LY_STMT_TYPE || kw == LY_STMT_UNIQUE || \
93 kw == LY_STMT_UNITS || kw == LY_STMT_EXTENSION_INSTANCE)
94
95/* flags to set constraints of subelements */
96#define YIN_SUBELEM_MANDATORY 0x01 /**< is set when subelement is mandatory */
97#define YIN_SUBELEM_UNIQUE 0x02 /**< is set when subelement is unique */
98#define YIN_SUBELEM_FIRST 0x04 /**< is set when subelement is actually yang argument mapped to yin element */
99#define YIN_SUBELEM_VER2 0x08 /**< subelemnt is allowed only in modules with version at least 2 (YANG 1.1) */
100
101#define YIN_SUBELEM_PARSED 0x80 /**< is set during parsing when given subelement is encountered for the first
102 time to simply check validity of given constraints */
103
104struct yin_subelement {
105 enum ly_stmt type; /**< type of keyword */
106 void *dest; /**< meta infromation passed to responsible function (mostly information about where parsed subelement should be stored) */
107 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 */
108};
109
110/* Meta information passed to yin_parse_argument function,
111 holds information about where content of argument element will be stored. */
112struct yin_argument_meta {
113 uint16_t *flags; /**< Argument flags */
114 const char **argument; /**< Argument value */
115};
116
117/**
118 * @brief Meta information passed to functions working with tree_schema,
119 * that require additional information about parent node.
120 */
121struct tree_node_meta {
122 struct lysp_node *parent; /**< parent node */
123 struct lysp_node **nodes; /**< linked list of siblings */
124};
125
126/**
127 * @brief Meta information passed to yin_parse_import function.
128 */
129struct import_meta {
130 const char *prefix; /**< module prefix. */
131 struct lysp_import **imports; /**< imports to add to. */
132};
133
134/**
135 * @brief Meta information passed to yin_parse_include function.
136 */
137struct include_meta {
138 const char *name; /**< Module/submodule name. */
139 struct lysp_include **includes; /**< [Sized array](@ref sizedarrays) of parsed includes to add to. */
140};
141
142/**
143 * @brief Meta information passed to yin_parse_inout function.
144 */
145struct inout_meta {
146 struct lysp_node *parent; /**< Parent node. */
Radek Krejci2a9fc652021-01-22 17:44:34 +0100147 struct lysp_node_action_inout *inout_p; /**< inout_p Input/output pointer to write to. */
Michal Vaskoafac7822020-10-20 14:22:26 +0200148};
149
150/**
151 * @brief Meta information passed to yin_parse_minmax function.
152 */
153struct minmax_dev_meta {
154 uint32_t *lim; /**< min/max value to write to. */
155 uint16_t *flags; /**< min/max flags to write to. */
156 struct lysp_ext_instance **exts; /**< extension instances to add to. */
157};
158
159LY_ERR yin_parse_content(struct lys_yin_parser_ctx *ctx, struct yin_subelement *subelem_info, size_t subelem_info_size,
160 enum ly_stmt current_element, const char **text_content, struct lysp_ext_instance **exts);
161
162/**
163 * @brief Match yang keyword from yin data.
164 *
165 * @param[in,out] ctx Yin parser context for logging and to store current state.
166 * @param[in] name Start of keyword name
167 * @param[in] name_len Lenght of keyword name.
168 * @param[in] prefix Start of keyword prefix.
169 * @param[in] prefix_len Lenght of prefix.
170 * @param[in] parrent Identification of parrent element, use LY_STMT_NONE for elements without parrent.
171 *
172 * @return yang_keyword values.
173 */
Radek Krejcid6b76452019-09-03 17:03:03 +0200174enum ly_stmt
Michal Vaskob36053d2020-03-26 15:49:30 +0100175yin_match_keyword(struct lys_yin_parser_ctx *ctx, const char *name, size_t name_len,
Radek Krejci0f969882020-08-21 16:56:47 +0200176 const char *prefix, size_t prefix_len, enum ly_stmt parent)
David Sedlák1bccdfa2019-06-17 15:55:27 +0200177{
David Sedlák8f7a1172019-06-20 14:42:18 +0200178 const char *start = NULL;
Radek Krejcid6b76452019-09-03 17:03:03 +0200179 enum ly_stmt kw = LY_STMT_NONE;
David Sedlák8f7a1172019-06-20 14:42:18 +0200180 const struct lyxml_ns *ns = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +0200181 struct ly_in *in;
David Sedlák8f7a1172019-06-20 14:42:18 +0200182
Michal Vasko69730152020-10-09 16:30:07 +0200183 if (!name || (name_len == 0)) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200184 return LY_STMT_NONE;
David Sedlák1bccdfa2019-06-17 15:55:27 +0200185 }
186
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200187 ns = lyxml_ns_get(&ctx->xmlctx->ns, prefix, prefix_len);
David Sedlák8f7a1172019-06-20 14:42:18 +0200188 if (ns) {
189 if (!IS_YIN_NS(ns->uri)) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200190 return LY_STMT_EXTENSION_INSTANCE;
David Sedlák8f7a1172019-06-20 14:42:18 +0200191 }
192 } else {
193 /* elements without namespace are automatically unknown */
Radek Krejcid6b76452019-09-03 17:03:03 +0200194 return LY_STMT_NONE;
David Sedlák8f7a1172019-06-20 14:42:18 +0200195 }
David Sedlák1bccdfa2019-06-17 15:55:27 +0200196
Michal Vasko63f3d842020-07-08 10:10:14 +0200197 LY_CHECK_RET(ly_in_new_memory(name, &in), LY_STMT_NONE);
198 start = in->current;
Radek Krejcid54412f2020-12-17 20:25:35 +0100199 kw = lysp_match_kw(in, NULL);
Michal Vasko63f3d842020-07-08 10:10:14 +0200200 name = in->current;
201 ly_in_free(in, 0);
David Sedlák8f7a1172019-06-20 14:42:18 +0200202
203 if (name - start == (long int)name_len) {
David Sedlákc1771b12019-07-10 15:55:46 +0200204 /* 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 +0200205 if ((kw == LY_STMT_VALUE) && (parent == LY_STMT_ERROR_MESSAGE)) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200206 return LY_STMT_ARG_VALUE;
David Sedlákc1771b12019-07-10 15:55:46 +0200207 }
David Sedlák1bccdfa2019-06-17 15:55:27 +0200208 return kw;
209 } else {
David Sedlák3ffbc522019-07-02 17:49:28 +0200210 if (strncmp(start, "text", name_len) == 0) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200211 return LY_STMT_ARG_TEXT;
David Sedlák3ffbc522019-07-02 17:49:28 +0200212 } else {
Radek Krejcid6b76452019-09-03 17:03:03 +0200213 return LY_STMT_NONE;
David Sedlák3ffbc522019-07-02 17:49:28 +0200214 }
David Sedlák1bccdfa2019-06-17 15:55:27 +0200215 }
216}
217
Michal Vaskoafac7822020-10-20 14:22:26 +0200218/**
219 * @brief Match argument name.
220 *
221 * @param[in] name String representing name.
222 * @param[in] len Lenght of the name.
223 *
224 * @return yin_argument values.
225 */
David Sedlákc5b20842019-08-13 10:18:31 +0200226enum yin_argument
David Sedlák060b00e2019-06-19 11:12:06 +0200227yin_match_argument_name(const char *name, size_t len)
David Sedlák3b4db242018-10-19 16:11:01 +0200228{
David Sedlákc5b20842019-08-13 10:18:31 +0200229 enum yin_argument arg = YIN_ARG_UNKNOWN;
David Sedlák872c7b42018-10-26 13:15:20 +0200230 size_t already_read = 0;
Michal Vasko69730152020-10-09 16:30:07 +0200231
David Sedlák7ff55a92019-06-17 11:11:41 +0200232 LY_CHECK_RET(len == 0, YIN_ARG_NONE);
David Sedlák3b4db242018-10-19 16:11:01 +0200233
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200234#define READ_INC(LEN) already_read += LEN
235#define ARG_SET(STMT) arg=STMT
236#define ARG_CHECK(STR, LEN) (!strncmp((name) + already_read, STR, LEN) && (READ_INC(LEN)))
David Sedlákc10e7902018-12-17 02:17:59 +0100237
David Sedlák1c8b2702019-02-22 11:03:02 +0100238 switch (*name) {
David Sedlák94de2aa2019-02-15 12:42:11 +0100239 case 'c':
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200240 READ_INC(1);
241 if (ARG_CHECK("ondition", 8)) {
242 ARG_SET(YIN_ARG_CONDITION);
243 }
David Sedlák3b4db242018-10-19 16:11:01 +0200244 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200245
David Sedlák94de2aa2019-02-15 12:42:11 +0100246 case 'd':
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200247 READ_INC(1);
248 if (ARG_CHECK("ate", 3)) {
249 ARG_SET(YIN_ARG_DATE);
250 }
David Sedlák3b4db242018-10-19 16:11:01 +0200251 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200252
David Sedlák94de2aa2019-02-15 12:42:11 +0100253 case 'm':
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200254 READ_INC(1);
255 if (ARG_CHECK("odule", 5)) {
256 ARG_SET(YIN_ARG_MODULE);
257 }
David Sedlák872c7b42018-10-26 13:15:20 +0200258 break;
259
David Sedlák94de2aa2019-02-15 12:42:11 +0100260 case 'n':
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200261 READ_INC(1);
262 if (ARG_CHECK("ame", 3)) {
263 ARG_SET(YIN_ARG_NAME);
264 }
David Sedlák872c7b42018-10-26 13:15:20 +0200265 break;
266
David Sedlák94de2aa2019-02-15 12:42:11 +0100267 case 't':
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200268 READ_INC(1);
269 if (ARG_CHECK("a", 1)) {
270 if (ARG_CHECK("g", 1)) {
271 ARG_SET(YIN_ARG_TAG);
272 } else if (ARG_CHECK("rget-node", 9)) {
273 ARG_SET(YIN_ARG_TARGET_NODE);
274 }
275 } else if (ARG_CHECK("ext", 3)) {
276 ARG_SET(YIN_ARG_TEXT);
277 }
David Sedlák3b4db242018-10-19 16:11:01 +0200278 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200279
David Sedlák94de2aa2019-02-15 12:42:11 +0100280 case 'u':
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200281 READ_INC(1);
282 if (ARG_CHECK("ri", 2)) {
283 ARG_SET(YIN_ARG_URI);
284 }
David Sedlák3b4db242018-10-19 16:11:01 +0200285 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200286
David Sedlák94de2aa2019-02-15 12:42:11 +0100287 case 'v':
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200288 READ_INC(1);
289 if (ARG_CHECK("alue", 4)) {
290 ARG_SET(YIN_ARG_VALUE);
291 }
David Sedlák3b4db242018-10-19 16:11:01 +0200292 break;
293 }
294
David Sedlákc10e7902018-12-17 02:17:59 +0100295 /* whole argument must be matched */
David Sedlák872c7b42018-10-26 13:15:20 +0200296 if (already_read != len) {
David Sedláka7406952019-04-05 10:33:07 +0200297 arg = YIN_ARG_UNKNOWN;
David Sedlák872c7b42018-10-26 13:15:20 +0200298 }
299
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200300#undef ARG_CHECK
301#undef ARG_SET
302#undef READ_INC
David Sedlák18730132019-03-15 15:51:34 +0100303
David Sedlák872c7b42018-10-26 13:15:20 +0200304 return arg;
David Sedlák3b4db242018-10-19 16:11:01 +0200305}
306
Radek Krejcid6b76452019-09-03 17:03:03 +0200307#define IS_NODE_ELEM(kw) (kw == LY_STMT_ANYXML || kw == LY_STMT_ANYDATA || kw == LY_STMT_LEAF || kw == LY_STMT_LEAF_LIST || \
308 kw == LY_STMT_TYPEDEF || kw == LY_STMT_USES || kw == LY_STMT_LIST || kw == LY_STMT_NOTIFICATION || \
309 kw == LY_STMT_GROUPING || kw == LY_STMT_CONTAINER || kw == LY_STMT_CASE || kw == LY_STMT_CHOICE || \
310 kw == LY_STMT_ACTION || kw == LY_STMT_RPC || kw == LY_STMT_AUGMENT)
David Sedlák81497a32019-08-13 16:56:26 +0200311
Radek Krejcid6b76452019-09-03 17:03:03 +0200312#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 +0200313
David Sedlák26ea1432019-08-14 13:42:23 +0200314/**
315 * @brief Free subelems information allocated on heap.
316 *
317 * @param[in] count Size of subelems array.
318 * @param[in] subelems Subelems array to free.
319 */
David Sedlák81497a32019-08-13 16:56:26 +0200320static void
321subelems_deallocator(size_t count, struct yin_subelement *subelems)
322{
Michal Vaskod989ba02020-08-24 10:59:24 +0200323 for (size_t i = 0; i < count; ++i) {
David Sedlák81497a32019-08-13 16:56:26 +0200324 if (HAS_META(subelems[i].type)) {
325 free(subelems[i].dest);
326 }
327 }
328
329 free(subelems);
330}
331
David Sedlák26ea1432019-08-14 13:42:23 +0200332/**
333 * @brief Allocate subelems information on heap.
334 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200335 * @param[in] ctx YIN parser context, used for logging.
David Sedlák26ea1432019-08-14 13:42:23 +0200336 * @param[in] count Number of subelements.
337 * @param[in] parent Parent node if any.
338 * @param[out] result Allocated subelems array.
339 *
340 * @return LY_SUCCESS on success LY_EMEM on memmory allocation failure.
341 */
David Sedlák81497a32019-08-13 16:56:26 +0200342static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100343subelems_allocator(struct lys_yin_parser_ctx *ctx, size_t count, struct lysp_node *parent,
Radek Krejci0f969882020-08-21 16:56:47 +0200344 struct yin_subelement **result, ...)
David Sedlák81497a32019-08-13 16:56:26 +0200345{
346 va_list ap;
347
348 *result = calloc(count, sizeof **result);
David Sedlákbf8a2b72019-08-14 16:48:10 +0200349 LY_CHECK_GOTO(!(*result), mem_err);
David Sedlák81497a32019-08-13 16:56:26 +0200350
351 va_start(ap, result);
352 for (size_t i = 0; i < count; ++i) {
353 /* TYPE */
Radek Krejcid6b76452019-09-03 17:03:03 +0200354 (*result)[i].type = va_arg(ap, enum ly_stmt);
David Sedlák81497a32019-08-13 16:56:26 +0200355 /* DEST */
356 if (IS_NODE_ELEM((*result)[i].type)) {
357 struct tree_node_meta *node_meta = NULL;
358 node_meta = calloc(1, sizeof *node_meta);
David Sedlákbf8a2b72019-08-14 16:48:10 +0200359 LY_CHECK_GOTO(!node_meta, mem_err);
David Sedlák81497a32019-08-13 16:56:26 +0200360 node_meta->parent = parent;
David Sedlákbf8a2b72019-08-14 16:48:10 +0200361 node_meta->nodes = va_arg(ap, void *);
David Sedlák81497a32019-08-13 16:56:26 +0200362 (*result)[i].dest = node_meta;
Radek Krejcid6b76452019-09-03 17:03:03 +0200363 } else if ((*result)[i].type == LY_STMT_IMPORT) {
David Sedlák81497a32019-08-13 16:56:26 +0200364 struct import_meta *imp_meta = NULL;
365 imp_meta = calloc(1, sizeof *imp_meta);
David Sedlákbf8a2b72019-08-14 16:48:10 +0200366 LY_CHECK_GOTO(!imp_meta, mem_err);
David Sedlák81497a32019-08-13 16:56:26 +0200367 imp_meta->prefix = va_arg(ap, const char *);
368 imp_meta->imports = va_arg(ap, struct lysp_import **);
369 (*result)[i].dest = imp_meta;
Radek Krejcid6b76452019-09-03 17:03:03 +0200370 } else if ((*result)[i].type == LY_STMT_INCLUDE) {
David Sedlák81497a32019-08-13 16:56:26 +0200371 struct include_meta *inc_meta = NULL;
372 inc_meta = calloc(1, sizeof *inc_meta);
David Sedlákbf8a2b72019-08-14 16:48:10 +0200373 LY_CHECK_GOTO(!inc_meta, mem_err);
David Sedlák81497a32019-08-13 16:56:26 +0200374 inc_meta->name = va_arg(ap, const char *);
375 inc_meta->includes = va_arg(ap, struct lysp_include **);
376 (*result)[i].dest = inc_meta;
Michal Vasko69730152020-10-09 16:30:07 +0200377 } else if (((*result)[i].type == LY_STMT_INPUT) || ((*result)[i].type == LY_STMT_OUTPUT)) {
David Sedlák81497a32019-08-13 16:56:26 +0200378 struct inout_meta *inout_meta = NULL;
379 inout_meta = calloc(1, sizeof *inout_meta);
David Sedlákbf8a2b72019-08-14 16:48:10 +0200380 LY_CHECK_GOTO(!inout_meta, mem_err);
David Sedlák81497a32019-08-13 16:56:26 +0200381 inout_meta->parent = parent;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100382 inout_meta->inout_p = va_arg(ap, struct lysp_node_action_inout *);
David Sedlák81497a32019-08-13 16:56:26 +0200383 (*result)[i].dest = inout_meta;
384 } else {
385 (*result)[i].dest = va_arg(ap, void *);
386 }
387 /* FLAGS */
388 (*result)[i].flags = va_arg(ap, int);
389 }
390 va_end(ap);
391
392 return LY_SUCCESS;
393
David Sedlákbf8a2b72019-08-14 16:48:10 +0200394mem_err:
David Sedlák81497a32019-08-13 16:56:26 +0200395 subelems_deallocator(count, *result);
Michal Vaskob36053d2020-03-26 15:49:30 +0100396 LOGMEM(ctx->xmlctx->ctx);
David Sedlák81497a32019-08-13 16:56:26 +0200397 return LY_EMEM;
398}
399
Michal Vaskoafac7822020-10-20 14:22:26 +0200400/**
401 * @brief Check that val is valid UTF8 character sequence of val_type.
402 * Doesn't check empty string, only character validity.
403 *
404 * @param[in] ctx Yin parser context for logging.
405 * @param[in] val_type Type of the input string to select method of checking character validity.
406 *
407 * @return LY_ERR values.
408 */
David Sedlák8f7a1172019-06-20 14:42:18 +0200409LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100410yin_validate_value(struct lys_yin_parser_ctx *ctx, enum yang_arg val_type)
David Sedlák4a650532019-07-10 11:55:18 +0200411{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200412 uint8_t prefix = 0;
413 uint32_t c;
Michal Vaskob36053d2020-03-26 15:49:30 +0100414 size_t utf8_char_len, already_read = 0;
415 const char *val;
416
417 assert((ctx->xmlctx->status == LYXML_ELEM_CONTENT) || (ctx->xmlctx->status == LYXML_ATTR_CONTENT));
418
419 val = ctx->xmlctx->value;
420 while (already_read < ctx->xmlctx->value_len) {
David Sedlák4a650532019-07-10 11:55:18 +0200421 LY_CHECK_ERR_RET(ly_getutf8((const char **)&val, &c, &utf8_char_len),
Michal Vasko69730152020-10-09 16:30:07 +0200422 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INCHAR, (val)[-utf8_char_len]), LY_EVALID);
David Sedlák4a650532019-07-10 11:55:18 +0200423
424 switch (val_type) {
425 case Y_IDENTIF_ARG:
Radek Krejci1deb5be2020-08-26 16:43:36 +0200426 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 +0200427 break;
428 case Y_PREF_IDENTIF_ARG:
Radek Krejci1deb5be2020-08-26 16:43:36 +0200429 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 +0200430 break;
431 case Y_STR_ARG:
432 case Y_MAYBE_STR_ARG:
433 LY_CHECK_RET(lysp_check_stringchar((struct lys_parser_ctx *)ctx, c));
434 break;
435 }
Radek Iša2eac74c2021-03-15 15:53:08 +0100436
437 already_read += utf8_char_len;
438 LY_CHECK_ERR_RET(already_read > ctx->xmlctx->value_len, LOGINT(ctx->xmlctx->ctx), LY_EINT);
David Sedlák4a650532019-07-10 11:55:18 +0200439 }
440
Radek Iša42cc0b72021-03-15 16:51:31 +0100441 if ((already_read == 0) &&
442 ((val_type == Y_PREF_IDENTIF_ARG) || (val_type == Y_IDENTIF_ARG))) {
443 /* Empty identifier or prefix*/
444 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YIN, "Empty identifier is not allowed.");
445 return LY_EVALID;
446 }
447
David Sedlák4a650532019-07-10 11:55:18 +0200448 return LY_SUCCESS;
449}
450
David Sedlákb4e44562019-07-04 15:42:12 +0200451/**
Michal Vaskob36053d2020-03-26 15:49:30 +0100452 * @brief Parse yin attributes.
David Sedlákb4e44562019-07-04 15:42:12 +0200453 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200454 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák4a650532019-07-10 11:55:18 +0200455 * @param[in] arg_type Type of argument that is expected in parsed element (use YIN_ARG_NONE for elements without
456 * special argument).
David Sedlákbf8a2b72019-08-14 16:48:10 +0200457 * @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 +0200458 * @param[in] val_type Type of expected value of attribute.
David Sedlákb4e44562019-07-04 15:42:12 +0200459 * @param[in] current_element Identification of current element, used for logging.
460 *
461 * @return LY_ERR values.
462 */
463static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100464yin_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 +0200465 enum ly_stmt current_element)
David Sedlák8f7a1172019-06-20 14:42:18 +0200466{
David Sedlákc5b20842019-08-13 10:18:31 +0200467 enum yin_argument arg = YIN_ARG_UNKNOWN;
David Sedlák619db942019-07-03 14:47:30 +0200468 bool found = false;
David Sedlák8f7a1172019-06-20 14:42:18 +0200469
David Sedlák1bccdfa2019-06-17 15:55:27 +0200470 /* validation of attributes */
Michal Vaskob36053d2020-03-26 15:49:30 +0100471 while (ctx->xmlctx->status == LYXML_ATTRIBUTE) {
David Sedlák00250342019-06-21 14:19:39 +0200472 /* yin arguments represented as attributes have no namespace, which in this case means no prefix */
Michal Vaskob36053d2020-03-26 15:49:30 +0100473 if (!ctx->xmlctx->prefix) {
474 arg = yin_match_argument_name(ctx->xmlctx->name, ctx->xmlctx->name_len);
David Sedlák7ff55a92019-06-17 11:11:41 +0200475 if (arg == YIN_ARG_NONE) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100476 /* skip it */
477 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
David Sedlák7ff55a92019-06-17 11:11:41 +0200478 } else if (arg == arg_type) {
David Sedlák1538a842019-08-08 15:38:51 +0200479 LY_CHECK_ERR_RET(found, LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_DUP_ATTR,
Michal Vasko69730152020-10-09 16:30:07 +0200480 yin_attr2str(arg), ly_stmt2str(current_element)), LY_EVALID);
David Sedlák619db942019-07-03 14:47:30 +0200481 found = true;
Michal Vaskob36053d2020-03-26 15:49:30 +0100482
483 /* go to value */
484 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
485 LY_CHECK_RET(yin_validate_value(ctx, val_type));
Radek Krejci011e4aa2020-09-04 15:22:31 +0200486 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 +0200487 LY_CHECK_RET(!(*arg_val), LY_EMEM);
David Sedlák2b214ac2019-06-06 16:11:03 +0200488 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100489 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_UNEXP_ATTR, ctx->xmlctx->name_len,
Michal Vasko69730152020-10-09 16:30:07 +0200490 ctx->xmlctx->name, ly_stmt2str(current_element));
David Sedlák619db942019-07-03 14:47:30 +0200491 return LY_EVALID;
David Sedláka7406952019-04-05 10:33:07 +0200492 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100493 } else {
494 /* skip it */
495 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
David Sedláka7406952019-04-05 10:33:07 +0200496 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100497
498 /* next attribute */
499 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
David Sedláka7406952019-04-05 10:33:07 +0200500 }
501
David Sedlák292763b2019-07-09 11:10:53 +0200502 /* anything else than Y_MAYBE_STR_ARG is mandatory */
Michal Vasko69730152020-10-09 16:30:07 +0200503 if ((val_type != Y_MAYBE_STR_ARG) && !found) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100504 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LYVE_SYNTAX_YIN, "Missing mandatory attribute %s of %s element.",
Michal Vasko69730152020-10-09 16:30:07 +0200505 yin_attr2str(arg_type), ly_stmt2str(current_element));
David Sedlák619db942019-07-03 14:47:30 +0200506 return LY_EVALID;
507 }
508
509 return LY_SUCCESS;
David Sedláka7406952019-04-05 10:33:07 +0200510}
511
David Sedlákd6e56892019-07-01 15:40:24 +0200512/**
Radek Krejci12b1c152019-09-05 16:20:48 +0200513 * @brief Get record with given type.
David Sedlákd6e56892019-07-01 15:40:24 +0200514 *
515 * @param[in] type Type of wanted record.
516 * @param[in] array_size Size of array.
517 * @param[in] array Searched array.
518 *
519 * @return Pointer to desired record on success, NULL if element is not in the array.
520 */
David Sedlákb4e44562019-07-04 15:42:12 +0200521static struct yin_subelement *
Radek Krejci1deb5be2020-08-26 16:43:36 +0200522get_record(enum ly_stmt type, LY_ARRAY_COUNT_TYPE array_size, struct yin_subelement *array)
David Sedlákd6e56892019-07-01 15:40:24 +0200523{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200524 for (LY_ARRAY_COUNT_TYPE u = 0; u < array_size; ++u) {
Radek Krejci12b1c152019-09-05 16:20:48 +0200525 if (array[u].type == type) {
526 return &array[u];
David Sedlákd6e56892019-07-01 15:40:24 +0200527 }
528 }
David Sedlákd6e56892019-07-01 15:40:24 +0200529 return NULL;
530}
531
David Sedlákbba38e52019-07-09 15:20:01 +0200532/**
533 * @brief Helper function to check mandatory constraint of subelement.
534 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200535 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200536 * @param[in] subelem_info Array of information about subelements.
537 * @param[in] subelem_info_size Size of subelem_info array.
538 * @param[in] current_element Identification of element that is currently being parsed, used for logging.
539 *
540 * @return LY_ERR values.
541 */
542static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100543yin_check_subelem_mandatory_constraint(struct lys_yin_parser_ctx *ctx, struct yin_subelement *subelem_info,
Radek Krejci0f969882020-08-21 16:56:47 +0200544 signed char subelem_info_size, enum ly_stmt current_element)
David Sedlák21f87cd2019-07-03 16:53:23 +0200545{
David Sedlákb0faad82019-07-04 14:28:59 +0200546 for (signed char i = 0; i < subelem_info_size; ++i) {
David Sedlák5545f5d2019-07-11 11:55:16 +0200547 /* if there is element that is mandatory and isn't parsed log error and return LY_EVALID */
Radek Krejci12b1c152019-09-05 16:20:48 +0200548 if ((subelem_info[i].flags & YIN_SUBELEM_MANDATORY) && !(subelem_info[i].flags & YIN_SUBELEM_PARSED)) {
David Sedlák1538a842019-08-08 15:38:51 +0200549 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_MAND_SUBELEM,
Michal Vasko69730152020-10-09 16:30:07 +0200550 ly_stmt2str(subelem_info[i].type), ly_stmt2str(current_element));
David Sedlák21f87cd2019-07-03 16:53:23 +0200551 return LY_EVALID;
552 }
553 }
554
555 return LY_SUCCESS;
556}
557
David Sedlákbba38e52019-07-09 15:20:01 +0200558/**
559 * @brief Helper function to check "first" constraint of subelement.
560 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200561 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200562 * @param[in] subelem_info Array of information about subelements.
563 * @param[in] subelem_info_size Size of subelem_info array.
564 * @param[in] current_element Identification of element that is currently being parsed, used for logging.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200565 * @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 +0200566 *
567 * @return LY_ERR values.
568 */
569static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100570yin_check_subelem_first_constraint(struct lys_yin_parser_ctx *ctx, struct yin_subelement *subelem_info,
Radek Krejci0f969882020-08-21 16:56:47 +0200571 signed char subelem_info_size, enum ly_stmt current_element,
572 struct yin_subelement *exp_first)
David Sedlák21f87cd2019-07-03 16:53:23 +0200573{
David Sedlákb0faad82019-07-04 14:28:59 +0200574 for (signed char i = 0; i < subelem_info_size; ++i) {
David Sedlák21f87cd2019-07-03 16:53:23 +0200575 if (subelem_info[i].flags & YIN_SUBELEM_PARSED) {
David Sedlák1538a842019-08-08 15:38:51 +0200576 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_FIRT_SUBELEM,
Michal Vasko69730152020-10-09 16:30:07 +0200577 ly_stmt2str(exp_first->type), ly_stmt2str(current_element));
David Sedlák21f87cd2019-07-03 16:53:23 +0200578 return LY_EVALID;
579 }
580 }
581
582 return LY_SUCCESS;
583}
584
David Sedlákbba38e52019-07-09 15:20:01 +0200585/**
David Sedlákb4e44562019-07-04 15:42:12 +0200586 * @brief Parse simple element without any special constraints and argument mapped to yin attribute,
587 * for example prefix or namespace element.
David Sedlákd6e56892019-07-01 15:40:24 +0200588 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200589 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákb4e44562019-07-04 15:42:12 +0200590 * @param[in] kw Type of current element.
591 * @param[out] value Where value of attribute should be stored.
592 * @param[in] arg_type Expected type of attribute.
David Sedlák292763b2019-07-09 11:10:53 +0200593 * @param[in] arg_val_type Type of expected value of attribute.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200594 * @param[in,out] exts Extension instances to add to.
David Sedlákd6e56892019-07-01 15:40:24 +0200595 * @return LY_ERR values.
596 */
David Sedlákb4e44562019-07-04 15:42:12 +0200597static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100598yin_parse_simple_element(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, const char **value,
Radek Krejci0f969882020-08-21 16:56:47 +0200599 enum yin_argument arg_type, enum yang_arg arg_val_type, struct lysp_ext_instance **exts)
David Sedlákb4e44562019-07-04 15:42:12 +0200600{
Michal Vaskob36053d2020-03-26 15:49:30 +0100601 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
602 LY_CHECK_RET(yin_parse_attribute(ctx, arg_type, value, arg_val_type, kw));
Radek Krejcif13b87b2020-12-01 22:02:17 +0100603 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200604 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
605 };
David Sedlákb4e44562019-07-04 15:42:12 +0200606
Radek Krejcif13b87b2020-12-01 22:02:17 +0100607 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), kw, NULL, exts);
David Sedlákb4e44562019-07-04 15:42:12 +0200608}
609
610/**
David Sedlák6542aed2019-08-14 10:47:43 +0200611 * @brief Parse path element.
612 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200613 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák6542aed2019-08-14 10:47:43 +0200614 * @param[in] kw Type of current element.
615 * @param[out] type Type structure to store parsed value, flags and extension instances.
David Sedlák6542aed2019-08-14 10:47:43 +0200616 * @return LY_ERR values.
617 */
618static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100619yin_parse_path(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, struct lysp_type *type)
David Sedlák6542aed2019-08-14 10:47:43 +0200620{
Michal Vasko004d3152020-06-11 19:59:22 +0200621 LY_ERR ret;
622 const char *str_path;
623
624 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 +0200625 ret = ly_path_parse(ctx->xmlctx->ctx, NULL, str_path, 0, 1, LY_PATH_BEGIN_EITHER,
Michal Vasko69730152020-10-09 16:30:07 +0200626 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path);
Michal Vasko004d3152020-06-11 19:59:22 +0200627 lydict_remove(ctx->xmlctx->ctx, str_path);
628 LY_CHECK_RET(ret);
David Sedlák6542aed2019-08-14 10:47:43 +0200629 type->flags |= LYS_SET_PATH;
630
631 return LY_SUCCESS;
632}
633
634/**
David Sedlákd3983112019-07-12 11:20:56 +0200635 * @brief Parse pattern element.
636 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200637 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200638 * @param[in,out] type Type structure to store parsed value, flags and extension instances.
David Sedlákd3983112019-07-12 11:20:56 +0200639 * @return LY_ERR values.
640 */
641static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100642yin_parse_pattern(struct lys_yin_parser_ctx *ctx, struct lysp_type *type)
David Sedlákd3983112019-07-12 11:20:56 +0200643{
644 const char *real_value = NULL;
645 char *saved_value = NULL;
646 struct lysp_restr *restr;
647
Michal Vaskob36053d2020-03-26 15:49:30 +0100648 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, type->patterns, restr, LY_EMEM);
649 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
650 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 +0200651 size_t len = strlen(real_value);
652
Michal Vasko55a16b92021-09-15 08:51:35 +0200653 /* empty pattern is not valid */
654 if (!len) {
655 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, real_value, "value", ly_stmt2str(LY_STMT_PATTERN));
656 lydict_remove(ctx->xmlctx->ctx, real_value);
657 return LY_EVALID;
658 }
659
David Sedlákd3983112019-07-12 11:20:56 +0200660 saved_value = malloc(len + 2);
Michal Vaskob36053d2020-03-26 15:49:30 +0100661 LY_CHECK_ERR_RET(!saved_value, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
David Sedlákd3983112019-07-12 11:20:56 +0200662 memmove(saved_value + 1, real_value, len);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100663 lydict_remove(ctx->xmlctx->ctx, real_value);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100664 saved_value[0] = LYSP_RESTR_PATTERN_ACK;
David Sedlákd3983112019-07-12 11:20:56 +0200665 saved_value[len + 1] = '\0';
Michal Vasko7f45cf22020-10-01 12:49:44 +0200666 LY_CHECK_RET(lydict_insert_zc(ctx->xmlctx->ctx, saved_value, &restr->arg.str));
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200667 restr->arg.mod = ctx->parsed_mod;
David Sedlákd3983112019-07-12 11:20:56 +0200668 type->flags |= LYS_SET_PATTERN;
669
Radek Krejcif13b87b2020-12-01 22:02:17 +0100670 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200671 {LY_STMT_DESCRIPTION, &restr->dsc, YIN_SUBELEM_UNIQUE},
672 {LY_STMT_ERROR_APP_TAG, &restr->eapptag, YIN_SUBELEM_UNIQUE},
673 {LY_STMT_ERROR_MESSAGE, &restr->emsg, YIN_SUBELEM_UNIQUE},
674 {LY_STMT_MODIFIER, &restr->arg, YIN_SUBELEM_UNIQUE},
675 {LY_STMT_REFERENCE, &restr->ref, YIN_SUBELEM_UNIQUE},
676 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
677 };
Michal Vasko69730152020-10-09 16:30:07 +0200678
Radek Krejcif13b87b2020-12-01 22:02:17 +0100679 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_PATTERN, NULL, &restr->exts);
David Sedlákd3983112019-07-12 11:20:56 +0200680}
681
David Sedlákc5b20842019-08-13 10:18:31 +0200682/**
683 * @brief Parse fraction-digits element.
684 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200685 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákc5b20842019-08-13 10:18:31 +0200686 * @param[in,out] type Type structure to store value, flags and extension instances.
David Sedlákc5b20842019-08-13 10:18:31 +0200687 * @return LY_ERR values.
688 */
David Sedlákf75d55e2019-07-12 16:52:50 +0200689static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100690yin_parse_fracdigits(struct lys_yin_parser_ctx *ctx, struct lysp_type *type)
David Sedlákf75d55e2019-07-12 16:52:50 +0200691{
692 const char *temp_val = NULL;
693 char *ptr;
Michal Vasko73d77ab2021-07-23 12:45:55 +0200694 unsigned long long int num;
David Sedlákf75d55e2019-07-12 16:52:50 +0200695
Michal Vaskob36053d2020-03-26 15:49:30 +0100696 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
697 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 +0200698
Michal Vasko69730152020-10-09 16:30:07 +0200699 if ((temp_val[0] == '\0') || (temp_val[0] == '0') || !isdigit(temp_val[0])) {
David Sedlák1538a842019-08-08 15:38:51 +0200700 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "fraction-digits");
Michal Vaskoe180ed02021-02-05 16:31:20 +0100701 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákf75d55e2019-07-12 16:52:50 +0200702 return LY_EVALID;
703 }
704
705 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +0200706 num = strtoull(temp_val, &ptr, LY_BASE_DEC);
David Sedlákf75d55e2019-07-12 16:52:50 +0200707 if (*ptr != '\0') {
David Sedlák1538a842019-08-08 15:38:51 +0200708 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "fraction-digits");
Michal Vaskoe180ed02021-02-05 16:31:20 +0100709 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákf75d55e2019-07-12 16:52:50 +0200710 return LY_EVALID;
711 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100712 if ((errno == ERANGE) || (num > LY_TYPE_DEC64_FD_MAX)) {
David Sedlák1538a842019-08-08 15:38:51 +0200713 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "fraction-digits");
Michal Vaskoe180ed02021-02-05 16:31:20 +0100714 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákf75d55e2019-07-12 16:52:50 +0200715 return LY_EVALID;
716 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100717 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákf75d55e2019-07-12 16:52:50 +0200718 type->fraction_digits = num;
719 type->flags |= LYS_SET_FRDIGITS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100720 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200721 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
722 };
Michal Vasko69730152020-10-09 16:30:07 +0200723
Radek Krejcif13b87b2020-12-01 22:02:17 +0100724 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_FRACTION_DIGITS, NULL, &type->exts);
David Sedlákf75d55e2019-07-12 16:52:50 +0200725}
726
David Sedlák07869a52019-07-12 14:28:19 +0200727/**
David Sedlák43801c92019-08-05 15:58:54 +0200728 * @brief Parse enum element.
David Sedlák07869a52019-07-12 14:28:19 +0200729 *
730 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200731 * @param[in,out] type Type structure to store parsed value, flags and extension instances.
David Sedlák07869a52019-07-12 14:28:19 +0200732 * @return LY_ERR values.
733 */
David Sedlákca36c422019-07-12 12:47:55 +0200734static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100735yin_parse_enum(struct lys_yin_parser_ctx *ctx, struct lysp_type *type)
David Sedlákca36c422019-07-12 12:47:55 +0200736{
David Sedlák07869a52019-07-12 14:28:19 +0200737 struct lysp_type_enum *en;
David Sedlák1e696782019-07-17 15:06:07 +0200738
Michal Vaskob36053d2020-03-26 15:49:30 +0100739 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, type->enums, en, LY_EMEM);
David Sedlák43801c92019-08-05 15:58:54 +0200740 type->flags |= LYS_SET_ENUM;
Michal Vaskob36053d2020-03-26 15:49:30 +0100741 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
742 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 +0200743 LY_CHECK_RET(lysp_check_enum_name((struct lys_parser_ctx *)ctx, en->name, strlen(en->name)));
Michal Vaskob36053d2020-03-26 15:49:30 +0100744 CHECK_NONEMPTY((struct lys_parser_ctx *)ctx, strlen(en->name), "enum");
David Sedlák43801c92019-08-05 15:58:54 +0200745 CHECK_UNIQUENESS((struct lys_parser_ctx *)ctx, type->enums, name, "enum", en->name);
David Sedlákca36c422019-07-12 12:47:55 +0200746
Radek Krejcif13b87b2020-12-01 22:02:17 +0100747 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200748 {LY_STMT_DESCRIPTION, &en->dsc, YIN_SUBELEM_UNIQUE},
749 {LY_STMT_IF_FEATURE, &en->iffeatures, 0},
750 {LY_STMT_REFERENCE, &en->ref, YIN_SUBELEM_UNIQUE},
751 {LY_STMT_STATUS, &en->flags, YIN_SUBELEM_UNIQUE},
752 {LY_STMT_VALUE, en, YIN_SUBELEM_UNIQUE},
753 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
754 };
Michal Vasko69730152020-10-09 16:30:07 +0200755
Radek Krejcif13b87b2020-12-01 22:02:17 +0100756 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_ENUM, NULL, &en->exts);
David Sedlák43801c92019-08-05 15:58:54 +0200757}
758
759/**
760 * @brief Parse bit element.
761 *
762 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200763 * @param[in,out] type Type structure to store parsed value, flags and extension instances.
David Sedlák43801c92019-08-05 15:58:54 +0200764 * @return LY_ERR values.
765 */
766static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100767yin_parse_bit(struct lys_yin_parser_ctx *ctx, struct lysp_type *type)
David Sedlák43801c92019-08-05 15:58:54 +0200768{
769 struct lysp_type_enum *en;
770
Michal Vaskob36053d2020-03-26 15:49:30 +0100771 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, type->bits, en, LY_EMEM);
David Sedlák43801c92019-08-05 15:58:54 +0200772 type->flags |= LYS_SET_BIT;
Michal Vaskob36053d2020-03-26 15:49:30 +0100773 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
774 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 +0100775 CHECK_UNIQUENESS((struct lys_parser_ctx *)ctx, type->bits, name, "bit", en->name);
David Sedlák43801c92019-08-05 15:58:54 +0200776
Radek Krejcif13b87b2020-12-01 22:02:17 +0100777 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200778 {LY_STMT_DESCRIPTION, &en->dsc, YIN_SUBELEM_UNIQUE},
779 {LY_STMT_IF_FEATURE, &en->iffeatures, 0},
780 {LY_STMT_POSITION, en, YIN_SUBELEM_UNIQUE},
781 {LY_STMT_REFERENCE, &en->ref, YIN_SUBELEM_UNIQUE},
782 {LY_STMT_STATUS, &en->flags, YIN_SUBELEM_UNIQUE},
783 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
784 };
Michal Vasko69730152020-10-09 16:30:07 +0200785
Radek Krejcif13b87b2020-12-01 22:02:17 +0100786 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_BIT, NULL, &en->exts);
David Sedlákca36c422019-07-12 12:47:55 +0200787}
788
David Sedlákd3983112019-07-12 11:20:56 +0200789/**
David Sedlák5f8191e2019-07-08 16:35:52 +0200790 * @brief Parse simple element without any special constraints and argument mapped to yin attribute, that can have
791 * more instances, such as base or if-feature.
792 *
793 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák5f8191e2019-07-08 16:35:52 +0200794 * @param[in] kw Type of current element.
795 * @param[out] values Parsed values to add to.
796 * @param[in] arg_type Expected type of attribute.
David Sedlák292763b2019-07-09 11:10:53 +0200797 * @param[in] arg_val_type Type of expected value of attribute.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200798 * @param[in,out] exts Extension instances to add to.
David Sedlák5f8191e2019-07-08 16:35:52 +0200799 * @return LY_ERR values.
800 */
801static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100802yin_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 +0200803 enum yang_arg arg_val_type, struct lysp_ext_instance **exts)
David Sedlák5f8191e2019-07-08 16:35:52 +0200804{
805 const char **value;
Michal Vasko69730152020-10-09 16:30:07 +0200806
Michal Vaskob36053d2020-03-26 15:49:30 +0100807 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *values, value, LY_EMEM);
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200808 LY_ARRAY_COUNT_TYPE index = LY_ARRAY_COUNT(*values) - 1;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100809 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200810 {LY_STMT_EXTENSION_INSTANCE, &index, 0}
811 };
David Sedlák968ac342019-07-11 15:17:59 +0200812
Michal Vaskob36053d2020-03-26 15:49:30 +0100813 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
814 LY_CHECK_RET(yin_parse_attribute(ctx, arg_type, value, arg_val_type, kw));
David Sedlák5f8191e2019-07-08 16:35:52 +0200815
Radek Krejcif13b87b2020-12-01 22:02:17 +0100816 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), kw, NULL, exts);
David Sedlák5f8191e2019-07-08 16:35:52 +0200817}
818
819/**
David Sedlák6542aed2019-08-14 10:47:43 +0200820 * @brief Parse simple element without any special constraints and argument mapped to yin attribute.
821 *
822 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák6542aed2019-08-14 10:47:43 +0200823 * @param[in] kw Type of current element.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200824 * @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 +0200825 * @param[in] arg_type Expected type of attribute.
826 * @param[in] arg_val_type Type of expected value of attribute.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200827 * @param[in,out] exts Extension instances to add to.
David Sedlák6542aed2019-08-14 10:47:43 +0200828 * @return LY_ERR values.
829 */
830static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100831yin_parse_simple_elem(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, struct yin_subelement *subinfo,
Radek Krejci0f969882020-08-21 16:56:47 +0200832 enum yin_argument arg_type, enum yang_arg arg_val_type, struct lysp_ext_instance **exts)
David Sedlák6542aed2019-08-14 10:47:43 +0200833{
834 if (subinfo->flags & YIN_SUBELEM_UNIQUE) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100835 LY_CHECK_RET(yin_parse_simple_element(ctx, kw, (const char **)subinfo->dest,
Michal Vasko69730152020-10-09 16:30:07 +0200836 arg_type, arg_val_type, exts));
David Sedlák6542aed2019-08-14 10:47:43 +0200837 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100838 LY_CHECK_RET(yin_parse_simple_elements(ctx, kw, (const char ***)subinfo->dest,
Michal Vasko69730152020-10-09 16:30:07 +0200839 arg_type, arg_val_type, exts));
David Sedlák6542aed2019-08-14 10:47:43 +0200840 }
841
842 return LY_SUCCESS;
843}
844
845/**
846 * @brief Parse base element.
847 *
848 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák6542aed2019-08-14 10:47:43 +0200849 * @param[in] parent Identification of parent element.
850 * @param[out] dest Where parsed values should be stored.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200851 * @param[in,out] exts Extension instances to add to.
David Sedlák6542aed2019-08-14 10:47:43 +0200852 * @return LY_ERR values.
853 */
854static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100855yin_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 +0200856{
857 struct lysp_type *type = NULL;
858
Radek Krejcid6b76452019-09-03 17:03:03 +0200859 if (parent == LY_STMT_TYPE) {
David Sedlák6542aed2019-08-14 10:47:43 +0200860 type = (struct lysp_type *)dest;
Michal Vaskob36053d2020-03-26 15:49:30 +0100861 LY_CHECK_RET(yin_parse_simple_elements(ctx, LY_STMT_BASE, &type->bases, YIN_ARG_NAME,
Michal Vasko69730152020-10-09 16:30:07 +0200862 Y_PREF_IDENTIF_ARG, exts));
David Sedlák6542aed2019-08-14 10:47:43 +0200863 type->flags |= LYS_SET_BASE;
Radek Krejcid6b76452019-09-03 17:03:03 +0200864 } else if (parent == LY_STMT_IDENTITY) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100865 LY_CHECK_RET(yin_parse_simple_elements(ctx, LY_STMT_BASE, (const char ***)dest,
Michal Vasko69730152020-10-09 16:30:07 +0200866 YIN_ARG_NAME, Y_PREF_IDENTIF_ARG, exts));
David Sedlák6542aed2019-08-14 10:47:43 +0200867 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100868 LOGINT(ctx->xmlctx->ctx);
David Sedlák6542aed2019-08-14 10:47:43 +0200869 return LY_EINT;
870 }
871
872 return LY_SUCCESS;
873}
874
875/**
David Sedlákbf8a2b72019-08-14 16:48:10 +0200876 * @brief Parse require-instance element.
David Sedlákcf5569a2019-07-11 13:31:34 +0200877 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200878 * @param[in,out] ctx YIN parser context for logging and to store current state.
Michal Vaskob36053d2020-03-26 15:49:30 +0100879 * @param[out] type Type structure to store value, flag and extensions.
David Sedlákcf5569a2019-07-11 13:31:34 +0200880 * @return LY_ERR values.
881 */
882static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100883yin_pasrse_reqinstance(struct lys_yin_parser_ctx *ctx, struct lysp_type *type)
David Sedlákcf5569a2019-07-11 13:31:34 +0200884{
885 const char *temp_val = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100886 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200887 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
888 };
David Sedlákcf5569a2019-07-11 13:31:34 +0200889
890 type->flags |= LYS_SET_REQINST;
Michal Vaskob36053d2020-03-26 15:49:30 +0100891 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
892 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 +0200893 if (strcmp(temp_val, "true") == 0) {
894 type->require_instance = 1;
895 } else if (strcmp(temp_val, "false") != 0) {
David Sedlák26ea1432019-08-14 13:42:23 +0200896 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value",
Michal Vasko69730152020-10-09 16:30:07 +0200897 "require-instance", "true", "false");
Michal Vaskoe180ed02021-02-05 16:31:20 +0100898 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákcf5569a2019-07-11 13:31:34 +0200899 return LY_EVALID;
900 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100901 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákcf5569a2019-07-11 13:31:34 +0200902
Radek Krejcif13b87b2020-12-01 22:02:17 +0100903 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_REQUIRE_INSTANCE, NULL, &type->exts);
David Sedlákcf5569a2019-07-11 13:31:34 +0200904}
905
906/**
David Sedlákce77bf52019-07-11 16:59:31 +0200907 * @brief Parse modifier element.
908 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200909 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákce77bf52019-07-11 16:59:31 +0200910 * @param[in,out] pat Value to write to.
911 * @param[in,out] exts Extension instances to add to.
912 *
913 * @return LY_ERR values.
914 */
915static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100916yin_parse_modifier(struct lys_yin_parser_ctx *ctx, const char **pat, struct lysp_ext_instance **exts)
David Sedlákce77bf52019-07-11 16:59:31 +0200917{
David Sedlákd3983112019-07-12 11:20:56 +0200918 assert(**pat == 0x06);
David Sedlákce77bf52019-07-11 16:59:31 +0200919 const char *temp_val;
920 char *modified_val;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100921 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200922 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
923 };
David Sedlákce77bf52019-07-11 16:59:31 +0200924
Michal Vaskob36053d2020-03-26 15:49:30 +0100925 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
926 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 +0200927 if (strcmp(temp_val, "invert-match") != 0) {
David Sedlák26ea1432019-08-14 13:42:23 +0200928 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS1, temp_val, "value",
Michal Vasko69730152020-10-09 16:30:07 +0200929 "modifier", "invert-match");
Michal Vaskoe180ed02021-02-05 16:31:20 +0100930 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákce77bf52019-07-11 16:59:31 +0200931 return LY_EVALID;
932 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100933 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákce77bf52019-07-11 16:59:31 +0200934
935 /* allocate new value */
David Sedlákd3983112019-07-12 11:20:56 +0200936 modified_val = malloc(strlen(*pat) + 1);
Michal Vaskob36053d2020-03-26 15:49:30 +0100937 LY_CHECK_ERR_RET(!modified_val, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
David Sedlákd3983112019-07-12 11:20:56 +0200938 strcpy(modified_val, *pat);
Michal Vaskob36053d2020-03-26 15:49:30 +0100939 lydict_remove(ctx->xmlctx->ctx, *pat);
David Sedlákce77bf52019-07-11 16:59:31 +0200940
941 /* modify the new value */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100942 modified_val[0] = LYSP_RESTR_PATTERN_NACK;
Radek Krejci011e4aa2020-09-04 15:22:31 +0200943 LY_CHECK_RET(lydict_insert_zc(ctx->xmlctx->ctx, modified_val, pat));
David Sedlákce77bf52019-07-11 16:59:31 +0200944
Radek Krejcif13b87b2020-12-01 22:02:17 +0100945 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_MODIFIER, NULL, exts);
David Sedlákce77bf52019-07-11 16:59:31 +0200946}
947
948/**
David Sedlákb7296dd2019-07-11 14:58:38 +0200949 * @brief Parse a restriction element (length, range or one instance of must).
950 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200951 * @param[in,out] ctx YIN parser context for logging and to store current state.
Radek Krejcid6b76452019-09-03 17:03:03 +0200952 * @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 +0200953 * @param[in] restr Value to write to.
David Sedlákb7296dd2019-07-11 14:58:38 +0200954 */
955static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100956yin_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 +0200957{
Radek Krejcid6b76452019-09-03 17:03:03 +0200958 assert(restr_kw == LY_STMT_MUST || restr_kw == LY_STMT_LENGTH || restr_kw == LY_STMT_RANGE);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100959 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200960 {LY_STMT_DESCRIPTION, &restr->dsc, YIN_SUBELEM_UNIQUE},
961 {LY_STMT_ERROR_APP_TAG, &restr->eapptag, YIN_SUBELEM_UNIQUE},
962 {LY_STMT_ERROR_MESSAGE, &restr->emsg, YIN_SUBELEM_UNIQUE},
963 {LY_STMT_REFERENCE, &restr->ref, YIN_SUBELEM_UNIQUE},
964 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
965 };
David Sedlákb7296dd2019-07-11 14:58:38 +0200966 /* argument of must is called condition, but argument of length and range is called value */
Radek Krejcid6b76452019-09-03 17:03:03 +0200967 enum yin_argument arg_type = (restr_kw == LY_STMT_MUST) ? YIN_ARG_CONDITION : YIN_ARG_VALUE;
David Sedlákb7296dd2019-07-11 14:58:38 +0200968
Michal Vaskob36053d2020-03-26 15:49:30 +0100969 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
Michal Vasko7f45cf22020-10-01 12:49:44 +0200970 LY_CHECK_RET(yin_parse_attribute(ctx, arg_type, &restr->arg.str, Y_STR_ARG, restr_kw));
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200971 restr->arg.mod = ctx->parsed_mod;
Michal Vaskob36053d2020-03-26 15:49:30 +0100972
Radek Krejcif13b87b2020-12-01 22:02:17 +0100973 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), restr_kw, NULL, &restr->exts);
David Sedlákb7296dd2019-07-11 14:58:38 +0200974}
975
976/**
David Sedlák6542aed2019-08-14 10:47:43 +0200977 * @brief Parse range element.
978 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200979 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák6542aed2019-08-14 10:47:43 +0200980 * @param[out] type Type structure to store parsed value and flags.
981 *
982 * @return LY_ERR values.
983 */
984static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100985yin_parse_range(struct lys_yin_parser_ctx *ctx, struct lysp_type *type)
David Sedlák6542aed2019-08-14 10:47:43 +0200986{
987 type->range = calloc(1, sizeof *type->range);
Michal Vaskob36053d2020-03-26 15:49:30 +0100988 LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
989 LY_CHECK_RET(yin_parse_restriction(ctx, LY_STMT_RANGE, type->range));
Michal Vasko44f3d2c2020-08-24 09:49:38 +0200990 type->flags |= LYS_SET_RANGE;
David Sedlák6542aed2019-08-14 10:47:43 +0200991
992 return LY_SUCCESS;
993}
994
995/**
996 * @brief Parse length element.
997 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200998 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák6542aed2019-08-14 10:47:43 +0200999 * @param[out] type Type structure to store parsed value and flags.
1000 *
1001 * @return LY_ERR values.
1002 */
1003static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001004yin_parse_length(struct lys_yin_parser_ctx *ctx, struct lysp_type *type)
David Sedlák6542aed2019-08-14 10:47:43 +02001005{
1006 type->length = calloc(1, sizeof *type->length);
Michal Vaskob36053d2020-03-26 15:49:30 +01001007 LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
1008 LY_CHECK_RET(yin_parse_restriction(ctx, LY_STMT_LENGTH, type->length));
David Sedlák6542aed2019-08-14 10:47:43 +02001009 type->flags |= LYS_SET_LENGTH;
1010
1011 return LY_SUCCESS;
1012}
1013
1014/**
David Sedlákbc9ec9c2019-07-11 15:53:55 +02001015 * @brief Parse must element.
1016 *
1017 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbc9ec9c2019-07-11 15:53:55 +02001018 * @param[in,out] restrs Restrictions to add to.
1019 *
1020 * @return LY_ERR values.
1021 */
1022static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001023yin_parse_must(struct lys_yin_parser_ctx *ctx, struct lysp_restr **restrs)
David Sedlákbc9ec9c2019-07-11 15:53:55 +02001024{
1025 struct lysp_restr *restr;
1026
Michal Vaskob36053d2020-03-26 15:49:30 +01001027 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *restrs, restr, LY_EMEM);
1028 return yin_parse_restriction(ctx, LY_STMT_MUST, restr);
David Sedlákbc9ec9c2019-07-11 15:53:55 +02001029}
1030
1031/**
Michal Vasko7f45cf22020-10-01 12:49:44 +02001032 * @brief Parse a node id into an array.
1033 *
1034 * @param[in,out] ctx YIN parser context for logging and to store current state.
1035 * @param[in] kw Type of current element.
1036 * @param[in] subinfo Information about subelement, is used to determin which function should be called and where to store parsed value.
1037 * @param[in,out] exts Extension instances to add to.
1038 *
1039 * @return LY_ERR values.
1040 */
1041static LY_ERR
1042yin_parse_qname(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, struct yin_subelement *subinfo,
1043 struct lysp_ext_instance **exts)
1044{
1045 struct lysp_qname *qname, **qnames;
1046
1047 switch (kw) {
1048 case LY_STMT_DEFAULT:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001049 if (subinfo->flags & YIN_SUBELEM_UNIQUE) {
1050 qname = (struct lysp_qname *)subinfo->dest;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001051 } else {
1052 qnames = (struct lysp_qname **)subinfo->dest;
1053 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *qnames, qname, LY_EMEM);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001054 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001055 qname->mod = ctx->parsed_mod;
1056 return yin_parse_simple_element(ctx, kw, &qname->str, YIN_ARG_VALUE, Y_STR_ARG, exts);
Radek Išabe8a6532021-03-29 08:54:22 +02001057 case LY_STMT_UNIQUE:
1058 assert(!(subinfo->flags & YIN_SUBELEM_UNIQUE));
1059 qnames = (struct lysp_qname **)subinfo->dest;
1060 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *qnames, qname, LY_EMEM);
1061 qname->mod = ctx->parsed_mod;
1062 return yin_parse_simple_element(ctx, kw, &qname->str, YIN_ARG_TAG, Y_STR_ARG, exts);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001063 case LY_STMT_IF_FEATURE:
1064 assert(!(subinfo->flags & YIN_SUBELEM_UNIQUE));
1065 qnames = (struct lysp_qname **)subinfo->dest;
1066 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *qnames, qname, LY_EMEM);
1067 qname->mod = ctx->parsed_mod;
1068 return yin_parse_simple_element(ctx, kw, &qname->str, YIN_ARG_NAME, Y_STR_ARG, exts);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001069 default:
1070 break;
1071 }
1072
1073 LOGINT(ctx->xmlctx->ctx);
1074 return LY_EINT;
1075}
1076
1077/**
David Sedlák5545f5d2019-07-11 11:55:16 +02001078 * @brief Parse position or value element.
1079 *
1080 * @param[in,out] ctx YIN parser context for logging and to store current state.
Radek Krejcid6b76452019-09-03 17:03:03 +02001081 * @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 +02001082 * @param[out] enm Enum structure to save value, flags and extension instances.
David Sedlák5545f5d2019-07-11 11:55:16 +02001083 *
1084 * @return LY_ERR values.
1085 */
1086static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001087yin_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 +02001088{
Radek Krejcid6b76452019-09-03 17:03:03 +02001089 assert(kw == LY_STMT_POSITION || kw == LY_STMT_VALUE);
David Sedlák5545f5d2019-07-11 11:55:16 +02001090 const char *temp_val = NULL;
1091 char *ptr;
Michal Vasko73d77ab2021-07-23 12:45:55 +02001092 long long int num = 0;
1093 unsigned long long int unum = 0;
David Sedlák5545f5d2019-07-11 11:55:16 +02001094
1095 /* set value flag */
1096 enm->flags |= LYS_SET_VALUE;
1097
1098 /* get attribute value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001099 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1100 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, kw));
Michal Vasko69730152020-10-09 16:30:07 +02001101 if (!temp_val || (temp_val[0] == '\0') || (temp_val[0] == '+') ||
Radek Krejci0f969882020-08-21 16:56:47 +02001102 ((temp_val[0] == '0') && (temp_val[1] != '\0')) || ((kw == LY_STMT_POSITION) && !strcmp(temp_val, "-0"))) {
David Sedlák1538a842019-08-08 15:38:51 +02001103 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 +02001104 goto error;
1105 }
1106
1107 /* convert value */
1108 errno = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001109 if (kw == LY_STMT_VALUE) {
Michal Vasko73d77ab2021-07-23 12:45:55 +02001110 num = strtoll(temp_val, &ptr, LY_BASE_DEC);
Michal Vasko69730152020-10-09 16:30:07 +02001111 if ((num < INT64_C(-2147483648)) || (num > INT64_C(2147483647))) {
David Sedlák1538a842019-08-08 15:38:51 +02001112 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 +02001113 goto error;
1114 }
1115 } else {
Michal Vasko73d77ab2021-07-23 12:45:55 +02001116 unum = strtoull(temp_val, &ptr, LY_BASE_DEC);
David Sedlák5545f5d2019-07-11 11:55:16 +02001117 if (unum > UINT64_C(4294967295)) {
David Sedlák1538a842019-08-08 15:38:51 +02001118 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 +02001119 goto error;
1120 }
1121 }
1122 /* check if whole argument value was converted */
1123 if (*ptr != '\0') {
David Sedlák1538a842019-08-08 15:38:51 +02001124 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 +02001125 goto error;
David Sedlák5545f5d2019-07-11 11:55:16 +02001126 }
1127 if (errno == ERANGE) {
David Sedlák1538a842019-08-08 15:38:51 +02001128 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 +02001129 goto error;
1130 }
1131 /* save correctly ternary operator can't be used because num and unum have different signes */
Radek Krejcid6b76452019-09-03 17:03:03 +02001132 if (kw == LY_STMT_VALUE) {
David Sedlák5545f5d2019-07-11 11:55:16 +02001133 enm->value = num;
1134 } else {
1135 enm->value = unum;
1136 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01001137 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlák5545f5d2019-07-11 11:55:16 +02001138
1139 /* parse subelements */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001140 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001141 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1142 };
Michal Vasko69730152020-10-09 16:30:07 +02001143
Radek Krejcif13b87b2020-12-01 22:02:17 +01001144 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), kw, NULL, &enm->exts);
David Sedlák5545f5d2019-07-11 11:55:16 +02001145
David Sedlákbf8a2b72019-08-14 16:48:10 +02001146error:
Michal Vaskoe180ed02021-02-05 16:31:20 +01001147 lydict_remove(ctx->xmlctx->ctx, temp_val);
Michal Vaskob36053d2020-03-26 15:49:30 +01001148 return LY_EVALID;
David Sedlák5545f5d2019-07-11 11:55:16 +02001149}
1150
David Sedlák05404f62019-07-24 14:11:53 +02001151/**
1152 * @brief Parse belongs-to element.
1153 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001154 * @param[in] ctx YIN parser context for logging and to store current state.
David Sedlák05404f62019-07-24 14:11:53 +02001155 * @param[out] submod Structure of submodule that is being parsed.
1156 * @param[in,out] exts Extension instances to add to.
1157 *
1158 * @return LY_ERR values
1159 */
1160static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001161yin_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 +02001162{
Michal Vaskoc3781c32020-10-06 14:04:08 +02001163 const char *belongsto;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001164 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001165 {LY_STMT_PREFIX, &submod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
1166 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1167 };
David Sedlák05404f62019-07-24 14:11:53 +02001168
Michal Vaskob36053d2020-03-26 15:49:30 +01001169 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
Michal Vaskoc3781c32020-10-06 14:04:08 +02001170 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_MODULE, &belongsto, Y_IDENTIF_ARG, LY_STMT_BELONGS_TO));
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001171 if (ctx->parsed_mod->mod->name != belongsto) {
Michal Vaskoc3781c32020-10-06 14:04:08 +02001172 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YIN, "Submodule \"belongs-to\" value \"%s\" does not match its module name \"%s\".",
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001173 belongsto, ctx->parsed_mod->mod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02001174 lydict_remove(ctx->xmlctx->ctx, belongsto);
1175 return LY_EVALID;
1176 }
1177 lydict_remove(ctx->xmlctx->ctx, belongsto);
Michal Vaskob36053d2020-03-26 15:49:30 +01001178
Radek Krejcif13b87b2020-12-01 22:02:17 +01001179 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_BELONGS_TO, NULL, exts);
David Sedlák05404f62019-07-24 14:11:53 +02001180}
1181
David Sedlák5545f5d2019-07-11 11:55:16 +02001182/**
David Sedlákc1771b12019-07-10 15:55:46 +02001183 * @brief Function to parse meta tags (description, contact, ...) eg. elements with
David Sedlákbf8a2b72019-08-14 16:48:10 +02001184 * text element as child.
David Sedlákb4e44562019-07-04 15:42:12 +02001185 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001186 * @param[in,out] ctx YIN parser context for logging and to store current state.
Radek Krejcid6b76452019-09-03 17:03:03 +02001187 * @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 +02001188 * @param[out] value Where the content of meta element should be stored.
David Sedlákbf8a2b72019-08-14 16:48:10 +02001189 * @param[in,out] exts Extension instances to add to.
David Sedlákb4e44562019-07-04 15:42:12 +02001190 *
1191 * @return LY_ERR values.
1192 */
1193static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001194yin_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 +02001195{
Radek Krejcid6b76452019-09-03 17:03:03 +02001196 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 +02001197
Radek Krejcif13b87b2020-12-01 22:02:17 +01001198 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001199 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1200 {LY_STMT_ARG_TEXT, value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE | YIN_SUBELEM_FIRST}
1201 };
Michal Vasko69730152020-10-09 16:30:07 +02001202
David Sedlákdf2a9732019-08-07 13:23:16 +02001203 /* check attributes */
Michal Vaskob36053d2020-03-26 15:49:30 +01001204 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1205 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 +02001206
David Sedlákdf2a9732019-08-07 13:23:16 +02001207 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001208 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), elem_type, NULL, exts);
David Sedlákb4e44562019-07-04 15:42:12 +02001209}
1210
1211/**
David Sedlákc1771b12019-07-10 15:55:46 +02001212 * @brief Parse error-message element.
1213 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001214 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákc1771b12019-07-10 15:55:46 +02001215 * @param[out] value Where the content of error-message element should be stored.
David Sedlákbf8a2b72019-08-14 16:48:10 +02001216 * @param[in,out] exts Extension instances to add to.
David Sedlákc1771b12019-07-10 15:55:46 +02001217 *
1218 * @return LY_ERR values.
1219 */
1220static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001221yin_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 +02001222{
Radek Krejcif13b87b2020-12-01 22:02:17 +01001223 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001224 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1225 {LY_STMT_ARG_VALUE, value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE | YIN_SUBELEM_FIRST}
1226 };
David Sedlákc1771b12019-07-10 15:55:46 +02001227
David Sedlákdf2a9732019-08-07 13:23:16 +02001228 /* check attributes */
Michal Vaskob36053d2020-03-26 15:49:30 +01001229 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1230 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 +02001231
Radek Krejcif13b87b2020-12-01 22:02:17 +01001232 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_ERROR_MESSAGE, NULL, exts);
David Sedlákc1771b12019-07-10 15:55:46 +02001233}
1234
1235/**
David Sedlák6542aed2019-08-14 10:47:43 +02001236 * @brief Parse type element.
David Sedlák374d2b32019-07-17 15:06:55 +02001237 *
1238 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák6542aed2019-08-14 10:47:43 +02001239 * @param[in] parent Identification of parent element.
David Sedlákbf8a2b72019-08-14 16:48:10 +02001240 * @param[in,out] type Type to write to.
David Sedlák374d2b32019-07-17 15:06:55 +02001241 *
1242 * @return LY_ERR values.
1243 */
1244static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001245yin_parse_type(struct lys_yin_parser_ctx *ctx, enum ly_stmt parent, struct yin_subelement *subinfo)
David Sedlák374d2b32019-07-17 15:06:55 +02001246{
David Sedlák6542aed2019-08-14 10:47:43 +02001247 struct lysp_type *type = NULL;
Michal Vasko69730152020-10-09 16:30:07 +02001248
Radek Krejcid6b76452019-09-03 17:03:03 +02001249 if (parent == LY_STMT_DEVIATE) {
David Sedlák6542aed2019-08-14 10:47:43 +02001250 *(struct lysp_type **)subinfo->dest = calloc(1, sizeof **(struct lysp_type **)subinfo->dest);
Michal Vaskob36053d2020-03-26 15:49:30 +01001251 LY_CHECK_ERR_RET(!(*(struct lysp_type **)subinfo->dest), LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
David Sedlák6542aed2019-08-14 10:47:43 +02001252 type = *((struct lysp_type **)subinfo->dest);
Michal Vaskod989ba02020-08-24 10:59:24 +02001253 } else {
David Sedlák6542aed2019-08-14 10:47:43 +02001254 type = (struct lysp_type *)subinfo->dest;
1255 }
Michal Vaskoe9c050f2020-10-06 14:01:23 +02001256
David Sedlák6542aed2019-08-14 10:47:43 +02001257 /* type as child of another type */
Radek Krejcid6b76452019-09-03 17:03:03 +02001258 if (parent == LY_STMT_TYPE) {
David Sedlák6542aed2019-08-14 10:47:43 +02001259 struct lysp_type *nested_type = NULL;
Michal Vaskob36053d2020-03-26 15:49:30 +01001260 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, type->types, nested_type, LY_EMEM);
David Sedlák6542aed2019-08-14 10:47:43 +02001261 type->flags |= LYS_SET_TYPE;
1262 type = nested_type;
1263 }
Michal Vaskoe9c050f2020-10-06 14:01:23 +02001264
Michal Vasko5bbcc242021-02-23 09:29:13 +01001265 type->pmod = ctx->parsed_mod;
1266
Radek Krejcif13b87b2020-12-01 22:02:17 +01001267 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001268 {LY_STMT_BASE, type, 0},
1269 {LY_STMT_BIT, type, 0},
1270 {LY_STMT_ENUM, type, 0},
1271 {LY_STMT_FRACTION_DIGITS, type, YIN_SUBELEM_UNIQUE},
1272 {LY_STMT_LENGTH, type, YIN_SUBELEM_UNIQUE},
1273 {LY_STMT_PATH, type, YIN_SUBELEM_UNIQUE},
1274 {LY_STMT_PATTERN, type, 0},
1275 {LY_STMT_RANGE, type, YIN_SUBELEM_UNIQUE},
1276 {LY_STMT_REQUIRE_INSTANCE, type, YIN_SUBELEM_UNIQUE},
Michal Vasko608b0c02021-09-23 08:26:53 +02001277 {LY_STMT_TYPE, type, 0},
Radek Krejci0f969882020-08-21 16:56:47 +02001278 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1279 };
Michal Vaskob36053d2020-03-26 15:49:30 +01001280
1281 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1282 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 +01001283 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_TYPE, NULL, &type->exts);
David Sedlák374d2b32019-07-17 15:06:55 +02001284}
1285
David Sedlák1af868e2019-07-17 17:03:14 +02001286/**
1287 * @brief Parse max-elements element.
1288 *
1289 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák1af868e2019-07-17 17:03:14 +02001290 * @param[in,out] max Value to write to.
David Sedláka2dad212019-07-18 12:45:19 +02001291 * @param[in] flags Flags to write to.
David Sedlák1af868e2019-07-17 17:03:14 +02001292 * @param[in,out] exts Extension instances to add to.
1293 *
1294 * @return LY_ERR values.
1295 */
1296static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001297yin_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 +02001298{
1299 const char *temp_val = NULL;
1300 char *ptr;
Michal Vasko73d77ab2021-07-23 12:45:55 +02001301 unsigned long long int num;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001302 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001303 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1304 };
David Sedlák374d2b32019-07-17 15:06:55 +02001305
David Sedlák1af868e2019-07-17 17:03:14 +02001306 *flags |= LYS_SET_MAX;
Michal Vaskob36053d2020-03-26 15:49:30 +01001307 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1308 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 +02001309 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 +02001310 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "max-elements");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001311 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlák1af868e2019-07-17 17:03:14 +02001312 return LY_EVALID;
1313 }
1314
1315 if (strcmp(temp_val, "unbounded")) {
1316 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02001317 num = strtoull(temp_val, &ptr, LY_BASE_DEC);
David Sedlák1af868e2019-07-17 17:03:14 +02001318 if (*ptr != '\0') {
David Sedlák1538a842019-08-08 15:38:51 +02001319 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "max-elements");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001320 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlák1af868e2019-07-17 17:03:14 +02001321 return LY_EVALID;
1322 }
1323 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlák1538a842019-08-08 15:38:51 +02001324 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_OOB_YIN, temp_val, "value", "max-elements");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001325 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlák1af868e2019-07-17 17:03:14 +02001326 return LY_EVALID;
1327 }
1328 *max = num;
1329 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01001330 lydict_remove(ctx->xmlctx->ctx, temp_val);
Radek Krejcif13b87b2020-12-01 22:02:17 +01001331 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_MAX_ELEMENTS, NULL, exts);
David Sedlák1af868e2019-07-17 17:03:14 +02001332}
David Sedlák374d2b32019-07-17 15:06:55 +02001333
1334/**
David Sedlákbf8a2b72019-08-14 16:48:10 +02001335 * @brief Parse min-elements element.
David Sedlák09e18c92019-07-18 11:17:11 +02001336 *
1337 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák09e18c92019-07-18 11:17:11 +02001338 * @param[in,out] min Value to write to.
David Sedláka2dad212019-07-18 12:45:19 +02001339 * @param[in] flags Flags to write to.
David Sedlák09e18c92019-07-18 11:17:11 +02001340 * @param[in,out] exts Extension instances to add to.
1341 *
1342 * @return LY_ERR values.
1343 */
1344static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001345yin_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 +02001346{
1347 const char *temp_val = NULL;
1348 char *ptr;
Michal Vasko73d77ab2021-07-23 12:45:55 +02001349 unsigned long long int num;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001350 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001351 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1352 };
David Sedlák09e18c92019-07-18 11:17:11 +02001353
1354 *flags |= LYS_SET_MIN;
Michal Vaskob36053d2020-03-26 15:49:30 +01001355 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1356 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 +02001357
Michal Vasko69730152020-10-09 16:30:07 +02001358 if (!temp_val || (temp_val[0] == '\0') || ((temp_val[0] == '0') && (temp_val[1] != '\0'))) {
David Sedlák1538a842019-08-08 15:38:51 +02001359 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "min-elements");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001360 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlák09e18c92019-07-18 11:17:11 +02001361 return LY_EVALID;
1362 }
1363
1364 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02001365 num = strtoull(temp_val, &ptr, LY_BASE_DEC);
David Sedlák09e18c92019-07-18 11:17:11 +02001366 if (ptr[0] != 0) {
David Sedlák1538a842019-08-08 15:38:51 +02001367 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "min-elements");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001368 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlák09e18c92019-07-18 11:17:11 +02001369 return LY_EVALID;
1370 }
Michal Vasko69730152020-10-09 16:30:07 +02001371 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlák1538a842019-08-08 15:38:51 +02001372 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_OOB_YIN, temp_val, "value", "min-elements");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001373 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlák09e18c92019-07-18 11:17:11 +02001374 return LY_EVALID;
1375 }
1376 *min = num;
Michal Vaskoe180ed02021-02-05 16:31:20 +01001377 lydict_remove(ctx->xmlctx->ctx, temp_val);
Radek Krejcif13b87b2020-12-01 22:02:17 +01001378 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_MIN_ELEMENTS, NULL, exts);
David Sedlák09e18c92019-07-18 11:17:11 +02001379}
1380
David Sedláka2dad212019-07-18 12:45:19 +02001381/**
1382 * @brief Parse min-elements or max-elements element.
1383 *
1384 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedláka2dad212019-07-18 12:45:19 +02001385 * @param[in] parent Identification of parent element.
1386 * @param[in] current Identification of current element.
1387 * @param[in] dest Where the parsed value and flags should be stored.
1388 *
1389 * @return LY_ERR values.
1390 */
David Sedlák09e18c92019-07-18 11:17:11 +02001391static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001392yin_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 +02001393{
Radek Krejcid6b76452019-09-03 17:03:03 +02001394 assert(current == LY_STMT_MAX_ELEMENTS || current == LY_STMT_MIN_ELEMENTS);
1395 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 +02001396 uint32_t *lim;
1397 uint16_t *flags;
1398 struct lysp_ext_instance **exts;
1399
Radek Krejcid6b76452019-09-03 17:03:03 +02001400 if (parent == LY_STMT_LEAF_LIST) {
1401 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 +02001402 flags = &((struct lysp_node_leaflist *)dest)->flags;
1403 exts = &((struct lysp_node_leaflist *)dest)->exts;
Radek Krejcid6b76452019-09-03 17:03:03 +02001404 } else if (parent == LY_STMT_REFINE) {
1405 lim = (current == LY_STMT_MAX_ELEMENTS) ? &((struct lysp_refine *)dest)->max : &((struct lysp_refine *)dest)->min;
David Sedlák09e18c92019-07-18 11:17:11 +02001406 flags = &((struct lysp_refine *)dest)->flags;
1407 exts = &((struct lysp_refine *)dest)->exts;
Radek Krejcid6b76452019-09-03 17:03:03 +02001408 } else if (parent == LY_STMT_LIST) {
1409 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 +02001410 flags = &((struct lysp_node_list *)dest)->flags;
1411 exts = &((struct lysp_node_list *)dest)->exts;
David Sedlák4ffcec82019-07-25 15:10:21 +02001412 } else {
1413 lim = ((struct minmax_dev_meta *)dest)->lim;
1414 flags = ((struct minmax_dev_meta *)dest)->flags;
1415 exts = ((struct minmax_dev_meta *)dest)->exts;
David Sedlák09e18c92019-07-18 11:17:11 +02001416 }
1417
Radek Krejcid6b76452019-09-03 17:03:03 +02001418 if (current == LY_STMT_MAX_ELEMENTS) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001419 LY_CHECK_RET(yin_parse_maxelements(ctx, lim, flags, exts));
David Sedlák09e18c92019-07-18 11:17:11 +02001420 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +01001421 LY_CHECK_RET(yin_parse_minelements(ctx, lim, flags, exts));
David Sedlák09e18c92019-07-18 11:17:11 +02001422 }
1423
1424 return LY_SUCCESS;
1425}
1426
1427/**
David Sedlákbf8a2b72019-08-14 16:48:10 +02001428 * @brief Parse ordered-by element.
David Sedláka2dad212019-07-18 12:45:19 +02001429 *
1430 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedláka2dad212019-07-18 12:45:19 +02001431 * @param[out] flags Flags to write to.
David Sedlákbf8a2b72019-08-14 16:48:10 +02001432 * @param[in,out] exts Extension instances to add to.
David Sedláka2dad212019-07-18 12:45:19 +02001433 *
1434 * @return LY_ERR values.
1435 */
1436static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001437yin_parse_orderedby(struct lys_yin_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
David Sedláka2dad212019-07-18 12:45:19 +02001438{
1439 const char *temp_val;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001440 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001441 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1442 };
David Sedláka2dad212019-07-18 12:45:19 +02001443
Michal Vaskob36053d2020-03-26 15:49:30 +01001444 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1445 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 +02001446 if (strcmp(temp_val, "system") == 0) {
1447 *flags |= LYS_ORDBY_SYSTEM;
1448 } else if (strcmp(temp_val, "user") == 0) {
1449 *flags |= LYS_ORDBY_USER;
1450 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02001451 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value",
Michal Vasko69730152020-10-09 16:30:07 +02001452 "ordered-by", "system", "user");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001453 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedláka2dad212019-07-18 12:45:19 +02001454 return LY_EVALID;
1455 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01001456 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedláka2dad212019-07-18 12:45:19 +02001457
Radek Krejcif13b87b2020-12-01 22:02:17 +01001458 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_ORDERED_BY, NULL, exts);
David Sedláka2dad212019-07-18 12:45:19 +02001459}
1460
1461/**
David Sedlákbf8a2b72019-08-14 16:48:10 +02001462 * @brief Parse any-data or any-xml element.
David Sedlák8a83bbb2019-07-18 14:46:00 +02001463 *
1464 * @param[in,out] ctx YIN parser context for logging and to store current state.
Radek Krejcid6b76452019-09-03 17:03:03 +02001465 * @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 +02001466 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlák8a83bbb2019-07-18 14:46:00 +02001467 *
1468 * @return LY_ERR values.
1469 */
1470static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001471yin_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 +02001472{
David Sedlák8a83bbb2019-07-18 14:46:00 +02001473 struct lysp_node_anydata *any;
1474
David Sedlák8d552d62019-08-06 15:29:05 +02001475 /* create new sibling */
Michal Vaskob36053d2020-03-26 15:49:30 +01001476 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, any, next, LY_EMEM);
Radek Krejcid6b76452019-09-03 17:03:03 +02001477 any->nodetype = (any_kw == LY_STMT_ANYDATA) ? LYS_ANYDATA : LYS_ANYXML;
David Sedlák8a83bbb2019-07-18 14:46:00 +02001478 any->parent = node_meta->parent;
1479
David Sedlákbf8a2b72019-08-14 16:48:10 +02001480 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001481 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1482 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 +02001483
Radek Krejcif13b87b2020-12-01 22:02:17 +01001484 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001485 {LY_STMT_CONFIG, &any->flags, YIN_SUBELEM_UNIQUE},
1486 {LY_STMT_DESCRIPTION, &any->dsc, YIN_SUBELEM_UNIQUE},
1487 {LY_STMT_IF_FEATURE, &any->iffeatures, 0},
1488 {LY_STMT_MANDATORY, &any->flags, YIN_SUBELEM_UNIQUE},
1489 {LY_STMT_MUST, &any->musts, 0},
1490 {LY_STMT_REFERENCE, &any->ref, YIN_SUBELEM_UNIQUE},
1491 {LY_STMT_STATUS, &any->flags, YIN_SUBELEM_UNIQUE},
1492 {LY_STMT_WHEN, &any->when, YIN_SUBELEM_UNIQUE},
1493 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1494 };
Michal Vasko69730152020-10-09 16:30:07 +02001495
Radek Krejcif13b87b2020-12-01 22:02:17 +01001496 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), any_kw, NULL, &any->exts);
David Sedlák8a83bbb2019-07-18 14:46:00 +02001497}
1498
1499/**
David Sedlák203ca3a2019-07-18 15:26:25 +02001500 * @brief parse leaf element.
1501 *
1502 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákad83cf92019-08-13 12:53:53 +02001503 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlák203ca3a2019-07-18 15:26:25 +02001504 *
1505 * @return LY_ERR values.
1506 */
1507static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001508yin_parse_leaf(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta)
David Sedlák203ca3a2019-07-18 15:26:25 +02001509{
David Sedlák203ca3a2019-07-18 15:26:25 +02001510 struct lysp_node_leaf *leaf;
1511
David Sedlák8d552d62019-08-06 15:29:05 +02001512 /* create structure new leaf */
Michal Vaskob36053d2020-03-26 15:49:30 +01001513 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, leaf, next, LY_EMEM);
David Sedlák203ca3a2019-07-18 15:26:25 +02001514 leaf->nodetype = LYS_LEAF;
1515 leaf->parent = node_meta->parent;
1516
David Sedlák203ca3a2019-07-18 15:26:25 +02001517 /* parser argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001518 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1519 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 +02001520
1521 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001522 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001523 {LY_STMT_CONFIG, &leaf->flags, YIN_SUBELEM_UNIQUE},
1524 {LY_STMT_DEFAULT, &leaf->dflt, YIN_SUBELEM_UNIQUE},
1525 {LY_STMT_DESCRIPTION, &leaf->dsc, YIN_SUBELEM_UNIQUE},
1526 {LY_STMT_IF_FEATURE, &leaf->iffeatures, 0},
1527 {LY_STMT_MANDATORY, &leaf->flags, YIN_SUBELEM_UNIQUE},
1528 {LY_STMT_MUST, &leaf->musts, 0},
1529 {LY_STMT_REFERENCE, &leaf->ref, YIN_SUBELEM_UNIQUE},
1530 {LY_STMT_STATUS, &leaf->flags, YIN_SUBELEM_UNIQUE},
1531 {LY_STMT_TYPE, &leaf->type, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_MANDATORY},
1532 {LY_STMT_UNITS, &leaf->units, YIN_SUBELEM_UNIQUE},
1533 {LY_STMT_WHEN, &leaf->when, YIN_SUBELEM_UNIQUE},
1534 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1535 };
Michal Vasko69730152020-10-09 16:30:07 +02001536
Radek Krejcif13b87b2020-12-01 22:02:17 +01001537 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_LEAF, NULL, &leaf->exts);
David Sedlák203ca3a2019-07-18 15:26:25 +02001538}
1539
1540/**
David Sedlákc3da3ef2019-07-19 12:56:08 +02001541 * @brief Parse leaf-list element.
1542 *
1543 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákad83cf92019-08-13 12:53:53 +02001544 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlákc3da3ef2019-07-19 12:56:08 +02001545 *
1546 * @return LY_ERR values.
1547 */
1548static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001549yin_parse_leaflist(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta)
David Sedlákc3da3ef2019-07-19 12:56:08 +02001550{
David Sedlákc3da3ef2019-07-19 12:56:08 +02001551 struct lysp_node_leaflist *llist;
David Sedlákc3da3ef2019-07-19 12:56:08 +02001552
Michal Vaskob36053d2020-03-26 15:49:30 +01001553 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, llist, next, LY_EMEM);
David Sedlák8d552d62019-08-06 15:29:05 +02001554
David Sedlákc3da3ef2019-07-19 12:56:08 +02001555 llist->nodetype = LYS_LEAFLIST;
1556 llist->parent = node_meta->parent;
1557
David Sedlákc3da3ef2019-07-19 12:56:08 +02001558 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001559 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1560 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 +02001561
1562 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001563 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001564 {LY_STMT_CONFIG, &llist->flags, YIN_SUBELEM_UNIQUE},
1565 {LY_STMT_DEFAULT, &llist->dflts, 0},
1566 {LY_STMT_DESCRIPTION, &llist->dsc, YIN_SUBELEM_UNIQUE},
1567 {LY_STMT_IF_FEATURE, &llist->iffeatures, 0},
1568 {LY_STMT_MAX_ELEMENTS, llist, YIN_SUBELEM_UNIQUE},
1569 {LY_STMT_MIN_ELEMENTS, llist, YIN_SUBELEM_UNIQUE},
1570 {LY_STMT_MUST, &llist->musts, 0},
1571 {LY_STMT_ORDERED_BY, &llist->flags, YIN_SUBELEM_UNIQUE},
1572 {LY_STMT_REFERENCE, &llist->ref, YIN_SUBELEM_UNIQUE},
1573 {LY_STMT_STATUS, &llist->flags, YIN_SUBELEM_UNIQUE},
1574 {LY_STMT_TYPE, &llist->type, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_MANDATORY},
1575 {LY_STMT_UNITS, &llist->units, YIN_SUBELEM_UNIQUE},
1576 {LY_STMT_WHEN, &llist->when, YIN_SUBELEM_UNIQUE},
1577 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1578 };
Michal Vasko69730152020-10-09 16:30:07 +02001579
Radek Krejcif13b87b2020-12-01 22:02:17 +01001580 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 +02001581
David Sedlákbf8a2b72019-08-14 16:48:10 +02001582 /* check invalid combination of subelements */
David Sedlákc3da3ef2019-07-19 12:56:08 +02001583 if ((llist->min) && (llist->dflts)) {
David Sedlák1538a842019-08-08 15:38:51 +02001584 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INCHILDSTMSCOMB_YIN, "min-elements", "default", "leaf-list");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001585 return LY_EVALID;
1586 }
Michal Vasko69730152020-10-09 16:30:07 +02001587 if (llist->max && (llist->min > llist->max)) {
David Sedlák1538a842019-08-08 15:38:51 +02001588 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_MINMAX, llist->min, llist->max);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001589 return LY_EVALID;
1590 }
1591
1592 return LY_SUCCESS;
1593}
1594
1595/**
David Sedlák04e17b22019-07-19 15:29:48 +02001596 * @brief Parse typedef element.
1597 *
1598 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák05404f62019-07-24 14:11:53 +02001599 * @param[in] typedef_meta Meta information about parent node and typedefs to add to.
David Sedlák04e17b22019-07-19 15:29:48 +02001600 *
1601 * @return LY_ERR values.
1602 */
1603static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001604yin_parse_typedef(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *typedef_meta)
David Sedlák04e17b22019-07-19 15:29:48 +02001605{
1606 struct lysp_tpdf *tpdf;
David Sedlákbf8a2b72019-08-14 16:48:10 +02001607 struct lysp_tpdf **tpdfs = (struct lysp_tpdf **)typedef_meta->nodes;
Michal Vasko69730152020-10-09 16:30:07 +02001608
Michal Vaskob36053d2020-03-26 15:49:30 +01001609 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *tpdfs, tpdf, LY_EMEM);
David Sedlák04e17b22019-07-19 15:29:48 +02001610
1611 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001612 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1613 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 +02001614
1615 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001616 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001617 {LY_STMT_DEFAULT, &tpdf->dflt, YIN_SUBELEM_UNIQUE},
1618 {LY_STMT_DESCRIPTION, &tpdf->dsc, YIN_SUBELEM_UNIQUE},
1619 {LY_STMT_REFERENCE, &tpdf->ref, YIN_SUBELEM_UNIQUE},
1620 {LY_STMT_STATUS, &tpdf->flags, YIN_SUBELEM_UNIQUE},
1621 {LY_STMT_TYPE, &tpdf->type, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_MANDATORY},
1622 {LY_STMT_UNITS, &tpdf->units, YIN_SUBELEM_UNIQUE},
1623 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1624 };
Michal Vasko69730152020-10-09 16:30:07 +02001625
Radek Krejcif13b87b2020-12-01 22:02:17 +01001626 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 +02001627
1628 /* store data for collision check */
aPiecek75b83a02021-06-29 10:34:38 +02001629 if (typedef_meta->parent) {
aPiecek8d4e75d2021-06-24 14:47:06 +02001630 assert(ctx->main_ctx);
1631 LY_CHECK_RET(ly_set_add(&ctx->main_ctx->tpdfs_nodes, typedef_meta->parent, 0, NULL));
David Sedlák04e17b22019-07-19 15:29:48 +02001632 }
1633
1634 return LY_SUCCESS;
1635}
1636
1637/**
David Sedlákd2d676a2019-07-22 11:28:19 +02001638 * @brief Parse refine element.
1639 *
1640 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákd2d676a2019-07-22 11:28:19 +02001641 * @param[in,out] refines Refines to add to.
1642 *
1643 * @return LY_ERR values.
1644 */
1645static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001646yin_parse_refine(struct lys_yin_parser_ctx *ctx, struct lysp_refine **refines)
David Sedlákd2d676a2019-07-22 11:28:19 +02001647{
1648 struct lysp_refine *rf;
1649
1650 /* allocate new refine */
Michal Vaskob36053d2020-03-26 15:49:30 +01001651 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *refines, rf, LY_EMEM);
David Sedlákd2d676a2019-07-22 11:28:19 +02001652
1653 /* parse attribute */
Michal Vaskob36053d2020-03-26 15:49:30 +01001654 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1655 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_TARGET_NODE, &rf->nodeid, Y_STR_ARG, LY_STMT_REFINE));
1656 CHECK_NONEMPTY(ctx, strlen(rf->nodeid), "refine");
David Sedlákd2d676a2019-07-22 11:28:19 +02001657
1658 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001659 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001660 {LY_STMT_CONFIG, &rf->flags, YIN_SUBELEM_UNIQUE},
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001661 {LY_STMT_DEFAULT, &rf->dflts, 0},
Radek Krejci0f969882020-08-21 16:56:47 +02001662 {LY_STMT_DESCRIPTION, &rf->dsc, YIN_SUBELEM_UNIQUE},
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001663 {LY_STMT_IF_FEATURE, &rf->iffeatures, 0},
Radek Krejci0f969882020-08-21 16:56:47 +02001664 {LY_STMT_MANDATORY, &rf->flags, YIN_SUBELEM_UNIQUE},
1665 {LY_STMT_MAX_ELEMENTS, rf, YIN_SUBELEM_UNIQUE},
1666 {LY_STMT_MIN_ELEMENTS, rf, YIN_SUBELEM_UNIQUE},
1667 {LY_STMT_MUST, &rf->musts, 0},
1668 {LY_STMT_PRESENCE, &rf->presence, YIN_SUBELEM_UNIQUE},
1669 {LY_STMT_REFERENCE, &rf->ref, YIN_SUBELEM_UNIQUE},
1670 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1671 };
Michal Vasko69730152020-10-09 16:30:07 +02001672
Radek Krejcif13b87b2020-12-01 22:02:17 +01001673 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_REFINE, NULL, &rf->exts);
David Sedlákd2d676a2019-07-22 11:28:19 +02001674}
1675
1676/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02001677 * @brief Parse uses element.
1678 *
1679 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákad83cf92019-08-13 12:53:53 +02001680 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlák0d6de5a2019-07-22 13:25:44 +02001681 *
1682 * @return LY_ERR values.
1683 */
1684static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001685yin_parse_uses(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta)
David Sedlák0d6de5a2019-07-22 13:25:44 +02001686{
David Sedlák0d6de5a2019-07-22 13:25:44 +02001687 struct lysp_node_uses *uses;
1688
David Sedlák8d552d62019-08-06 15:29:05 +02001689 /* create new uses */
Michal Vaskob36053d2020-03-26 15:49:30 +01001690 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, uses, next, LY_EMEM);
David Sedlák0d6de5a2019-07-22 13:25:44 +02001691 uses->nodetype = LYS_USES;
1692 uses->parent = node_meta->parent;
1693
David Sedlák0d6de5a2019-07-22 13:25:44 +02001694 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001695 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1696 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 +02001697
1698 /* parse content */
David Sedlák6881b512019-08-13 12:52:00 +02001699 struct tree_node_meta augments = {(struct lysp_node *)uses, (struct lysp_node **)&uses->augments};
Radek Krejcif13b87b2020-12-01 22:02:17 +01001700 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001701 {LY_STMT_AUGMENT, &augments, 0},
1702 {LY_STMT_DESCRIPTION, &uses->dsc, YIN_SUBELEM_UNIQUE},
1703 {LY_STMT_IF_FEATURE, &uses->iffeatures, 0},
1704 {LY_STMT_REFERENCE, &uses->ref, YIN_SUBELEM_UNIQUE},
1705 {LY_STMT_REFINE, &uses->refines, 0},
1706 {LY_STMT_STATUS, &uses->flags, YIN_SUBELEM_UNIQUE},
1707 {LY_STMT_WHEN, &uses->when, YIN_SUBELEM_UNIQUE},
1708 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1709 };
Michal Vasko69730152020-10-09 16:30:07 +02001710
Radek Krejcif13b87b2020-12-01 22:02:17 +01001711 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 +02001712
1713 return LY_SUCCESS;
1714}
1715
1716/**
David Sedlákaa854b02019-07-22 14:17:10 +02001717 * @brief Parse revision element.
1718 *
1719 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákaa854b02019-07-22 14:17:10 +02001720 * @param[in,out] revs Parsed revisions to add to.
1721 *
1722 * @return LY_ERR values.
1723 */
1724static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001725yin_parse_revision(struct lys_yin_parser_ctx *ctx, struct lysp_revision **revs)
David Sedlákaa854b02019-07-22 14:17:10 +02001726{
1727 struct lysp_revision *rev;
1728 const char *temp_date = NULL;
1729
1730 /* allocate new reivison */
Michal Vaskob36053d2020-03-26 15:49:30 +01001731 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *revs, rev, LY_EMEM);
David Sedlákaa854b02019-07-22 14:17:10 +02001732
1733 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001734 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1735 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 +02001736 /* check value */
1737 if (lysp_check_date((struct lys_parser_ctx *)ctx, temp_date, strlen(temp_date), "revision")) {
Michal Vaskoe180ed02021-02-05 16:31:20 +01001738 lydict_remove(ctx->xmlctx->ctx, temp_date);
David Sedlákaa854b02019-07-22 14:17:10 +02001739 return LY_EVALID;
1740 }
Radek Krejci36642162021-01-12 16:49:22 +01001741 memcpy(rev->date, temp_date, LY_REV_SIZE);
Michal Vaskoe180ed02021-02-05 16:31:20 +01001742 lydict_remove(ctx->xmlctx->ctx, temp_date);
David Sedlákaa854b02019-07-22 14:17:10 +02001743
1744 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001745 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001746 {LY_STMT_DESCRIPTION, &rev->dsc, YIN_SUBELEM_UNIQUE},
1747 {LY_STMT_REFERENCE, &rev->ref, YIN_SUBELEM_UNIQUE},
1748 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1749 };
Michal Vasko69730152020-10-09 16:30:07 +02001750
Radek Krejcif13b87b2020-12-01 22:02:17 +01001751 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_REVISION, NULL, &rev->exts);
David Sedlákaa854b02019-07-22 14:17:10 +02001752}
1753
David Sedlák5e13dea2019-07-22 16:06:45 +02001754/**
1755 * @brief Parse include element.
1756 *
1757 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák5e13dea2019-07-22 16:06:45 +02001758 * @param[in,out] inc_meta Meta informatinou about module/submodule name and includes to add to.
1759 *
1760 * @return LY_ERR values.
1761 */
David Sedlák0c2bab92019-07-22 15:33:19 +02001762static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001763yin_parse_include(struct lys_yin_parser_ctx *ctx, struct include_meta *inc_meta)
David Sedlák0c2bab92019-07-22 15:33:19 +02001764{
1765 struct lysp_include *inc;
1766
1767 /* allocate new include */
Michal Vaskob36053d2020-03-26 15:49:30 +01001768 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *inc_meta->includes, inc, LY_EMEM);
David Sedlák0c2bab92019-07-22 15:33:19 +02001769
1770 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001771 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1772 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 +02001773
1774 /* submodules share the namespace with the module names, so there must not be
1775 * a module of the same name in the context, no need for revision matching */
Michal Vaskob36053d2020-03-26 15:49:30 +01001776 if (!strcmp(inc_meta->name, inc->name) || ly_ctx_get_module_latest(ctx->xmlctx->ctx, inc->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01001777 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_NAME2_COL, "module", "submodule", inc->name);
David Sedlák0c2bab92019-07-22 15:33:19 +02001778 return LY_EVALID;
1779 }
1780
1781 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001782 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001783 {LY_STMT_DESCRIPTION, &inc->dsc, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_VER2},
1784 {LY_STMT_REFERENCE, &inc->ref, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_VER2},
1785 {LY_STMT_REVISION_DATE, &inc->rev, YIN_SUBELEM_UNIQUE},
1786 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1787 };
Michal Vasko69730152020-10-09 16:30:07 +02001788
Radek Krejcif13b87b2020-12-01 22:02:17 +01001789 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_INCLUDE, NULL, &inc->exts);
David Sedlák0c2bab92019-07-22 15:33:19 +02001790}
1791
David Sedlákaa854b02019-07-22 14:17:10 +02001792/**
David Sedlákbf8a2b72019-08-14 16:48:10 +02001793 * @brief Parse revision-date element.
David Sedlákdfbbb442019-08-06 16:33:21 +02001794 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001795 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001796 * @param[in,out] rev Array to store the parsed value in.
1797 * @param[in,out] exts Extension instances to add to.
1798 *
1799 * @return LY_ERR values.
1800 */
1801static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001802yin_parse_revision_date(struct lys_yin_parser_ctx *ctx, char *rev, struct lysp_ext_instance **exts)
David Sedlákdfbbb442019-08-06 16:33:21 +02001803{
1804 const char *temp_rev;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001805 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001806 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1807 };
David Sedlákdfbbb442019-08-06 16:33:21 +02001808
Michal Vaskob36053d2020-03-26 15:49:30 +01001809 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1810 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 +02001811 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 +01001812 lydict_remove(ctx->xmlctx->ctx, temp_rev), LY_EVALID);
David Sedlákdfbbb442019-08-06 16:33:21 +02001813
1814 strcpy(rev, temp_rev);
Michal Vaskoe180ed02021-02-05 16:31:20 +01001815 lydict_remove(ctx->xmlctx->ctx, temp_rev);
David Sedlákdfbbb442019-08-06 16:33:21 +02001816
Radek Krejcif13b87b2020-12-01 22:02:17 +01001817 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_REVISION_DATE, NULL, exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02001818}
1819
1820/**
1821 * @brief Parse config element.
1822 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001823 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001824 * @param[in,out] flags Flags to add to.
1825 * @param[in,out] exts Extension instances to add to.
1826 *
1827 * @return LY_ERR values.
1828 */
1829static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001830yin_parse_config(struct lys_yin_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
David Sedlákdfbbb442019-08-06 16:33:21 +02001831{
1832 const char *temp_val = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001833 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001834 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1835 };
David Sedlákdfbbb442019-08-06 16:33:21 +02001836
Michal Vaskob36053d2020-03-26 15:49:30 +01001837 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1838 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 +02001839 if (strcmp(temp_val, "true") == 0) {
1840 *flags |= LYS_CONFIG_W;
1841 } else if (strcmp(temp_val, "false") == 0) {
1842 *flags |= LYS_CONFIG_R;
1843 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02001844 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value", "config",
Michal Vasko69730152020-10-09 16:30:07 +02001845 "true", "false");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001846 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákdfbbb442019-08-06 16:33:21 +02001847 return LY_EVALID;
1848 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01001849 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákdfbbb442019-08-06 16:33:21 +02001850
Radek Krejcif13b87b2020-12-01 22:02:17 +01001851 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_CONFIG, NULL, exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02001852}
1853
1854/**
1855 * @brief Parse yang-version element.
1856 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001857 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001858 * @param[out] version Storage for the parsed information.
David Sedlákbf8a2b72019-08-14 16:48:10 +02001859 * @param[in,out] exts Extension instances to add to.
David Sedlákdfbbb442019-08-06 16:33:21 +02001860 *
1861 * @return LY_ERR values.
1862 */
1863static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001864yin_parse_yangversion(struct lys_yin_parser_ctx *ctx, uint8_t *version, struct lysp_ext_instance **exts)
David Sedlákdfbbb442019-08-06 16:33:21 +02001865{
1866 const char *temp_version = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001867 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001868 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1869 };
David Sedlákdfbbb442019-08-06 16:33:21 +02001870
Michal Vaskob36053d2020-03-26 15:49:30 +01001871 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1872 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 +02001873 if (strcmp(temp_version, "1") == 0) {
David Sedlákdfbbb442019-08-06 16:33:21 +02001874 *version = LYS_VERSION_1_0;
1875 } else if (strcmp(temp_version, "1.1") == 0) {
1876 *version = LYS_VERSION_1_1;
1877 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02001878 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_version, "value",
Michal Vasko69730152020-10-09 16:30:07 +02001879 "yang-version", "1", "1.1");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001880 lydict_remove(ctx->xmlctx->ctx, temp_version);
David Sedlákdfbbb442019-08-06 16:33:21 +02001881 return LY_EVALID;
1882 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01001883 lydict_remove(ctx->xmlctx->ctx, temp_version);
David Sedlákdfbbb442019-08-06 16:33:21 +02001884
Radek Krejcif13b87b2020-12-01 22:02:17 +01001885 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_YANG_VERSION, NULL, exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02001886}
1887
1888/**
1889 * @brief Parse import element.
1890 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001891 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001892 * @param[in,out] imp_meta Meta information about prefix and imports to add to.
1893 *
1894 * @return LY_ERR values.
1895 */
1896static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001897yin_parse_import(struct lys_yin_parser_ctx *ctx, struct import_meta *imp_meta)
David Sedlákdfbbb442019-08-06 16:33:21 +02001898{
1899 struct lysp_import *imp;
Michal Vasko69730152020-10-09 16:30:07 +02001900
David Sedlákdfbbb442019-08-06 16:33:21 +02001901 /* allocate new element in sized array for import */
Michal Vaskob36053d2020-03-26 15:49:30 +01001902 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *imp_meta->imports, imp, LY_EMEM);
David Sedlákdfbbb442019-08-06 16:33:21 +02001903
Radek Krejcif13b87b2020-12-01 22:02:17 +01001904 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001905 {LY_STMT_DESCRIPTION, &imp->dsc, YIN_SUBELEM_UNIQUE},
1906 {LY_STMT_PREFIX, &imp->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
1907 {LY_STMT_REFERENCE, &imp->ref, YIN_SUBELEM_UNIQUE},
1908 {LY_STMT_REVISION_DATE, imp->rev, YIN_SUBELEM_UNIQUE},
1909 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1910 };
David Sedlákdfbbb442019-08-06 16:33:21 +02001911
1912 /* parse import attributes */
Michal Vaskob36053d2020-03-26 15:49:30 +01001913 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1914 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 +01001915 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 +02001916 /* check prefix validity */
1917 LY_CHECK_RET(lysp_check_prefix((struct lys_parser_ctx *)ctx, *imp_meta->imports, imp_meta->prefix, &imp->prefix), LY_EVALID);
1918
1919 return LY_SUCCESS;
1920}
1921
1922/**
1923 * @brief Parse mandatory element.
1924 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001925 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001926 * @param[in,out] flags Flags to add to.
1927 * @param[in,out] exts Extension instances to add to.
1928 *
1929 * @return LY_ERR values.
1930 */
1931static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001932yin_parse_mandatory(struct lys_yin_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
David Sedlákdfbbb442019-08-06 16:33:21 +02001933{
1934 const char *temp_val = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001935 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001936 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1937 };
David Sedlákdfbbb442019-08-06 16:33:21 +02001938
Michal Vaskob36053d2020-03-26 15:49:30 +01001939 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1940 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 +02001941 if (strcmp(temp_val, "true") == 0) {
1942 *flags |= LYS_MAND_TRUE;
1943 } else if (strcmp(temp_val, "false") == 0) {
1944 *flags |= LYS_MAND_FALSE;
1945 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02001946 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value",
Michal Vasko69730152020-10-09 16:30:07 +02001947 "mandatory", "true", "false");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001948 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákdfbbb442019-08-06 16:33:21 +02001949 return LY_EVALID;
1950 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01001951 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákdfbbb442019-08-06 16:33:21 +02001952
Radek Krejcif13b87b2020-12-01 22:02:17 +01001953 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_MANDATORY, NULL, exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02001954}
1955
1956/**
1957 * @brief Parse status element.
1958 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001959 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001960 * @param[in,out] flags Flags to add to.
1961 * @param[in,out] exts Extension instances to add to.
1962 *
1963 * @return LY_ERR values.
1964 */
1965static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001966yin_parse_status(struct lys_yin_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
David Sedlákdfbbb442019-08-06 16:33:21 +02001967{
1968 const char *value = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001969 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001970 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1971 };
David Sedlákdfbbb442019-08-06 16:33:21 +02001972
Michal Vaskob36053d2020-03-26 15:49:30 +01001973 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1974 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 +02001975 if (strcmp(value, "current") == 0) {
1976 *flags |= LYS_STATUS_CURR;
1977 } else if (strcmp(value, "deprecated") == 0) {
1978 *flags |= LYS_STATUS_DEPRC;
1979 } else if (strcmp(value, "obsolete") == 0) {
1980 *flags |= LYS_STATUS_OBSLT;
1981 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02001982 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS3, value, "value",
Michal Vasko69730152020-10-09 16:30:07 +02001983 "status", "current", "deprecated", "obsolete");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001984 lydict_remove(ctx->xmlctx->ctx, value);
David Sedlákdfbbb442019-08-06 16:33:21 +02001985 return LY_EVALID;
1986 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01001987 lydict_remove(ctx->xmlctx->ctx, value);
David Sedlákdfbbb442019-08-06 16:33:21 +02001988
Radek Krejcif13b87b2020-12-01 22:02:17 +01001989 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_STATUS, NULL, exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02001990}
1991
1992/**
1993 * @brief Parse when element.
1994 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001995 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001996 * @param[out] when_p When pointer to parse to.
1997 */
1998static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001999yin_parse_when(struct lys_yin_parser_ctx *ctx, struct lysp_when **when_p)
David Sedlákdfbbb442019-08-06 16:33:21 +02002000{
2001 struct lysp_when *when;
David Sedláka56e0012019-08-15 13:21:25 +02002002 LY_ERR ret = LY_SUCCESS;
2003
David Sedlákdfbbb442019-08-06 16:33:21 +02002004 when = calloc(1, sizeof *when);
Michal Vaskob36053d2020-03-26 15:49:30 +01002005 LY_CHECK_ERR_RET(!when, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
Radek Krejcif6923e82020-07-02 16:36:53 +02002006
2007 ret = lyxml_ctx_next(ctx->xmlctx);
2008 LY_CHECK_ERR_RET(ret, free(when), ret);
2009
Michal Vaskob36053d2020-03-26 15:49:30 +01002010 ret = yin_parse_attribute(ctx, YIN_ARG_CONDITION, &when->cond, Y_STR_ARG, LY_STMT_WHEN);
David Sedláka56e0012019-08-15 13:21:25 +02002011 LY_CHECK_ERR_RET(ret, free(when), ret);
2012
David Sedlákdfbbb442019-08-06 16:33:21 +02002013 *when_p = when;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002014 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002015 {LY_STMT_DESCRIPTION, &when->dsc, YIN_SUBELEM_UNIQUE},
2016 {LY_STMT_REFERENCE, &when->ref, YIN_SUBELEM_UNIQUE},
2017 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
2018 };
David Sedlákdfbbb442019-08-06 16:33:21 +02002019
Radek Krejcif13b87b2020-12-01 22:02:17 +01002020 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_WHEN, NULL, &when->exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02002021}
2022
2023/**
2024 * @brief Parse yin-elemenet element.
2025 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02002026 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02002027 * @param[in,out] data Data to read from, always moved to currently handled position.
2028 * @param[in,out] flags Flags to add to.
David Sedlákbf8a2b72019-08-14 16:48:10 +02002029 * @prama[in,out] exts Extension instances to add to.
David Sedlákdfbbb442019-08-06 16:33:21 +02002030 *
2031 * @return LY_ERR values.
2032 */
2033static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002034yin_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 +02002035{
2036 const char *temp_val = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002037 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002038 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
2039 };
David Sedlákdfbbb442019-08-06 16:33:21 +02002040
Michal Vaskob36053d2020-03-26 15:49:30 +01002041 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2042 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 +02002043 if (strcmp(temp_val, "true") == 0) {
2044 *flags |= LYS_YINELEM_TRUE;
2045 } else if (strcmp(temp_val, "false") == 0) {
2046 *flags |= LYS_YINELEM_FALSE;
2047 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02002048 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value",
Michal Vasko69730152020-10-09 16:30:07 +02002049 "yin-element", "true", "false");
Michal Vaskoe180ed02021-02-05 16:31:20 +01002050 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákdfbbb442019-08-06 16:33:21 +02002051 return LY_EVALID;
2052 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01002053 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákdfbbb442019-08-06 16:33:21 +02002054
Radek Krejcif13b87b2020-12-01 22:02:17 +01002055 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_YIN_ELEMENT, NULL, exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02002056}
2057
2058/**
2059 * @brief Parse argument element.
2060 *
Michal Vaskob36053d2020-03-26 15:49:30 +01002061 * @param[in,out] xmlctx Xml context.
David Sedlákbf8a2b72019-08-14 16:48:10 +02002062 * @param[in,out] arg_meta Meta information about destionation of parsed data.
2063 * @param[in,out] exts Extension instances to add to.
David Sedlákdfbbb442019-08-06 16:33:21 +02002064 *
2065 * @return LY_ERR values.
2066 */
2067static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002068yin_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 +02002069{
Radek Krejcif13b87b2020-12-01 22:02:17 +01002070 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002071 {LY_STMT_YIN_ELEMENT, arg_meta->flags, YIN_SUBELEM_UNIQUE},
2072 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
2073 };
David Sedlákdfbbb442019-08-06 16:33:21 +02002074
Michal Vaskob36053d2020-03-26 15:49:30 +01002075 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2076 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 +02002077
Radek Krejcif13b87b2020-12-01 22:02:17 +01002078 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_ARGUMENT, NULL, exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02002079}
2080
2081/**
2082 * @brief Parse the extension statement.
2083 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02002084 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02002085 * @param[in,out] extensions Extensions to add to.
2086 *
2087 * @return LY_ERR values.
2088 */
2089static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002090yin_parse_extension(struct lys_yin_parser_ctx *ctx, struct lysp_ext **extensions)
David Sedlákdfbbb442019-08-06 16:33:21 +02002091{
2092 struct lysp_ext *ex;
Michal Vasko69730152020-10-09 16:30:07 +02002093
Michal Vaskob36053d2020-03-26 15:49:30 +01002094 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *extensions, ex, LY_EMEM);
2095 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2096 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 +02002097
Radek Krejci9f87b0c2021-03-05 14:45:26 +01002098 struct yin_argument_meta arg_info = {&ex->flags, &ex->argname};
Radek Krejcif13b87b2020-12-01 22:02:17 +01002099 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002100 {LY_STMT_ARGUMENT, &arg_info, YIN_SUBELEM_UNIQUE},
2101 {LY_STMT_DESCRIPTION, &ex->dsc, YIN_SUBELEM_UNIQUE},
2102 {LY_STMT_REFERENCE, &ex->ref, YIN_SUBELEM_UNIQUE},
2103 {LY_STMT_STATUS, &ex->flags, YIN_SUBELEM_UNIQUE},
2104 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
2105 };
David Sedlákdfbbb442019-08-06 16:33:21 +02002106
Radek Krejcif13b87b2020-12-01 22:02:17 +01002107 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_EXTENSION, NULL, &ex->exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02002108}
2109
2110/**
David Sedlák5e13dea2019-07-22 16:06:45 +02002111 * @brief Parse feature element.
2112 *
2113 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák5e13dea2019-07-22 16:06:45 +02002114 * @param[in,out] features Features to add to.
2115 *
2116 * @return LY_ERR values.
2117 */
2118static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002119yin_parse_feature(struct lys_yin_parser_ctx *ctx, struct lysp_feature **features)
David Sedlák5e13dea2019-07-22 16:06:45 +02002120{
2121 struct lysp_feature *feat;
2122
2123 /* allocate new feature */
Michal Vaskob36053d2020-03-26 15:49:30 +01002124 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *features, feat, LY_EMEM);
David Sedlák5e13dea2019-07-22 16:06:45 +02002125
2126 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002127 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2128 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 +02002129
2130 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002131 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002132 {LY_STMT_DESCRIPTION, &feat->dsc, YIN_SUBELEM_UNIQUE},
2133 {LY_STMT_IF_FEATURE, &feat->iffeatures, 0},
2134 {LY_STMT_REFERENCE, &feat->ref, YIN_SUBELEM_UNIQUE},
2135 {LY_STMT_STATUS, &feat->flags, YIN_SUBELEM_UNIQUE},
2136 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
2137 };
Michal Vasko69730152020-10-09 16:30:07 +02002138
Radek Krejcif13b87b2020-12-01 22:02:17 +01002139 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_FEATURE, NULL, &feat->exts);
David Sedlák5e13dea2019-07-22 16:06:45 +02002140}
2141
2142/**
David Sedlák28794f22019-07-22 16:45:00 +02002143 * @brief Parse identity element.
2144 *
2145 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák28794f22019-07-22 16:45:00 +02002146 * @param[in,out] identities Identities to add to.
2147 *
2148 * @return LY_ERR values.
2149 */
2150static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002151yin_parse_identity(struct lys_yin_parser_ctx *ctx, struct lysp_ident **identities)
David Sedlák28794f22019-07-22 16:45:00 +02002152{
2153 struct lysp_ident *ident;
2154
2155 /* allocate new identity */
Michal Vaskob36053d2020-03-26 15:49:30 +01002156 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *identities, ident, LY_EMEM);
David Sedlák28794f22019-07-22 16:45:00 +02002157
2158 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002159 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2160 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 +02002161
2162 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002163 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002164 {LY_STMT_BASE, &ident->bases, 0},
2165 {LY_STMT_DESCRIPTION, &ident->dsc, YIN_SUBELEM_UNIQUE},
2166 {LY_STMT_IF_FEATURE, &ident->iffeatures, YIN_SUBELEM_VER2},
2167 {LY_STMT_REFERENCE, &ident->ref, YIN_SUBELEM_UNIQUE},
2168 {LY_STMT_STATUS, &ident->flags, YIN_SUBELEM_UNIQUE},
2169 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
2170 };
Michal Vasko69730152020-10-09 16:30:07 +02002171
Radek Krejcif13b87b2020-12-01 22:02:17 +01002172 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_IDENTITY, NULL, &ident->exts);
David Sedlák28794f22019-07-22 16:45:00 +02002173}
2174
2175/**
David Sedlákaf536aa2019-07-23 13:42:23 +02002176 * @brief Parse list element.
2177 *
2178 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákad83cf92019-08-13 12:53:53 +02002179 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlákaf536aa2019-07-23 13:42:23 +02002180 *
2181 * @return LY_ERR values.
2182 */
2183static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002184yin_parse_list(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta)
David Sedlákaf536aa2019-07-23 13:42:23 +02002185{
David Sedlákaf536aa2019-07-23 13:42:23 +02002186 struct lysp_node_list *list;
David Sedlák81497a32019-08-13 16:56:26 +02002187 LY_ERR ret = LY_SUCCESS;
2188 struct yin_subelement *subelems = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002189 size_t subelems_size;
David Sedlákaf536aa2019-07-23 13:42:23 +02002190
Michal Vaskob36053d2020-03-26 15:49:30 +01002191 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, list, next, LY_EMEM);
David Sedlákaf536aa2019-07-23 13:42:23 +02002192 list->nodetype = LYS_LIST;
2193 list->parent = node_meta->parent;
2194
David Sedlákaf536aa2019-07-23 13:42:23 +02002195 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002196 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2197 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 +02002198
2199 /* parse list content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002200 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 25, (struct lysp_node *)list, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02002201 LY_STMT_ACTION, &list->actions, 0,
2202 LY_STMT_ANYDATA, &list->child, 0,
2203 LY_STMT_ANYXML, &list->child, 0,
2204 LY_STMT_CHOICE, &list->child, 0,
2205 LY_STMT_CONFIG, &list->flags, YIN_SUBELEM_UNIQUE,
2206 LY_STMT_CONTAINER, &list->child, 0,
2207 LY_STMT_DESCRIPTION, &list->dsc, YIN_SUBELEM_UNIQUE,
2208 LY_STMT_GROUPING, &list->groupings, 0,
2209 LY_STMT_IF_FEATURE, &list->iffeatures, 0,
2210 LY_STMT_KEY, &list->key, YIN_SUBELEM_UNIQUE,
2211 LY_STMT_LEAF, &list->child, 0,
2212 LY_STMT_LEAF_LIST, &list->child, 0,
2213 LY_STMT_LIST, &list->child, 0,
2214 LY_STMT_MAX_ELEMENTS, list, YIN_SUBELEM_UNIQUE,
2215 LY_STMT_MIN_ELEMENTS, list, YIN_SUBELEM_UNIQUE,
2216 LY_STMT_MUST, &list->musts, 0,
2217 LY_STMT_NOTIFICATION, &list->notifs, 0,
2218 LY_STMT_ORDERED_BY, &list->flags, YIN_SUBELEM_UNIQUE,
2219 LY_STMT_REFERENCE, &list->ref, YIN_SUBELEM_UNIQUE,
2220 LY_STMT_STATUS, &list->flags, YIN_SUBELEM_UNIQUE,
2221 LY_STMT_TYPEDEF, &list->typedefs, 0,
2222 LY_STMT_UNIQUE, &list->uniques, 0,
2223 LY_STMT_USES, &list->child, 0,
2224 LY_STMT_WHEN, &list->when, YIN_SUBELEM_UNIQUE,
2225 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
Radek Krejcif13b87b2020-12-01 22:02:17 +01002226 ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_LIST, NULL, &list->exts);
2227 subelems_deallocator(subelems_size, subelems);
David Sedlák81497a32019-08-13 16:56:26 +02002228 LY_CHECK_RET(ret);
David Sedlákaf536aa2019-07-23 13:42:23 +02002229
Michal Vasko69730152020-10-09 16:30:07 +02002230 if (list->max && (list->min > list->max)) {
David Sedlák1538a842019-08-08 15:38:51 +02002231 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_MINMAX, list->min, list->max);
David Sedlákaf536aa2019-07-23 13:42:23 +02002232 return LY_EVALID;
2233 }
2234
2235 return LY_SUCCESS;
2236}
2237
2238/**
David Sedlák031b9e72019-07-23 15:19:37 +02002239 * @brief Parse notification element.
2240 *
2241 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák05404f62019-07-24 14:11:53 +02002242 * @param[in,out] notif_meta Meta information about parent node and notifications to add to.
David Sedlák031b9e72019-07-23 15:19:37 +02002243 *
2244 * @return LY_ERR values.
2245 */
2246static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002247yin_parse_notification(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *notif_meta)
David Sedlák031b9e72019-07-23 15:19:37 +02002248{
Radek Krejci2a9fc652021-01-22 17:44:34 +01002249 struct lysp_node_notif *notif;
2250 struct lysp_node_notif **notifs = (struct lysp_node_notif **)notif_meta->nodes;
David Sedlák81497a32019-08-13 16:56:26 +02002251 LY_ERR ret = LY_SUCCESS;
2252 struct yin_subelement *subelems = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002253 size_t subelems_size;
David Sedlák031b9e72019-07-23 15:19:37 +02002254
2255 /* allocate new notification */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002256 LY_LIST_NEW_RET(ctx->xmlctx->ctx, notifs, notif, next, LY_EMEM);
David Sedlák031b9e72019-07-23 15:19:37 +02002257 notif->nodetype = LYS_NOTIF;
2258 notif->parent = notif_meta->parent;
2259
2260 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002261 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2262 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 +02002263
2264 /* parse notification content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002265 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 16, (struct lysp_node *)notif, &subelems,
Radek Krejci01180ac2021-01-27 08:48:22 +01002266 LY_STMT_ANYDATA, &notif->child, 0,
2267 LY_STMT_ANYXML, &notif->child, 0,
2268 LY_STMT_CHOICE, &notif->child, 0,
2269 LY_STMT_CONTAINER, &notif->child, 0,
Michal Vasko69730152020-10-09 16:30:07 +02002270 LY_STMT_DESCRIPTION, &notif->dsc, YIN_SUBELEM_UNIQUE,
2271 LY_STMT_GROUPING, &notif->groupings, 0,
2272 LY_STMT_IF_FEATURE, &notif->iffeatures, 0,
Radek Krejci01180ac2021-01-27 08:48:22 +01002273 LY_STMT_LEAF, &notif->child, 0,
2274 LY_STMT_LEAF_LIST, &notif->child, 0,
2275 LY_STMT_LIST, &notif->child, 0,
Michal Vasko69730152020-10-09 16:30:07 +02002276 LY_STMT_MUST, &notif->musts, YIN_SUBELEM_VER2,
2277 LY_STMT_REFERENCE, &notif->ref, YIN_SUBELEM_UNIQUE,
2278 LY_STMT_STATUS, &notif->flags, YIN_SUBELEM_UNIQUE,
2279 LY_STMT_TYPEDEF, &notif->typedefs, 0,
Radek Krejci01180ac2021-01-27 08:48:22 +01002280 LY_STMT_USES, &notif->child, 0,
Michal Vasko69730152020-10-09 16:30:07 +02002281 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
David Sedlák81497a32019-08-13 16:56:26 +02002282
Radek Krejcif13b87b2020-12-01 22:02:17 +01002283 ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_NOTIFICATION, NULL, &notif->exts);
2284 subelems_deallocator(subelems_size, subelems);
David Sedlák81497a32019-08-13 16:56:26 +02002285 LY_CHECK_RET(ret);
David Sedlák031b9e72019-07-23 15:19:37 +02002286
David Sedlák031b9e72019-07-23 15:19:37 +02002287 return LY_SUCCESS;
2288}
2289
2290/**
David Sedlákbf8a2b72019-08-14 16:48:10 +02002291 * @brief Parse grouping element.
David Sedláke3ce9ef2019-07-23 16:34:30 +02002292 *
2293 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbf8a2b72019-08-14 16:48:10 +02002294 * @param[in,out] gr_meta Meta information about parent node and groupings to add to.
David Sedláke3ce9ef2019-07-23 16:34:30 +02002295 *
2296 * @return LY_ERR values.
2297 */
2298static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002299yin_parse_grouping(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *gr_meta)
David Sedláke3ce9ef2019-07-23 16:34:30 +02002300{
Radek Krejci2a9fc652021-01-22 17:44:34 +01002301 struct lysp_node_grp *grp;
2302 struct lysp_node_grp **grps = (struct lysp_node_grp **)gr_meta->nodes;
David Sedlák81497a32019-08-13 16:56:26 +02002303 LY_ERR ret = LY_SUCCESS;
2304 struct yin_subelement *subelems = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002305 size_t subelems_size;
David Sedláke3ce9ef2019-07-23 16:34:30 +02002306
2307 /* create new grouping */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002308 LY_LIST_NEW_RET(ctx->xmlctx->ctx, grps, grp, next, LY_EMEM);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002309 grp->nodetype = LYS_GROUPING;
2310 grp->parent = gr_meta->parent;
2311
2312 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002313 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2314 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 +02002315
2316 /* parse grouping content */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002317 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 16, &grp->node, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02002318 LY_STMT_ACTION, &grp->actions, 0,
Radek Krejci01180ac2021-01-27 08:48:22 +01002319 LY_STMT_ANYDATA, &grp->child, 0,
2320 LY_STMT_ANYXML, &grp->child, 0,
2321 LY_STMT_CHOICE, &grp->child, 0,
2322 LY_STMT_CONTAINER, &grp->child, 0,
Michal Vasko69730152020-10-09 16:30:07 +02002323 LY_STMT_DESCRIPTION, &grp->dsc, YIN_SUBELEM_UNIQUE,
2324 LY_STMT_GROUPING, &grp->groupings, 0,
Radek Krejci01180ac2021-01-27 08:48:22 +01002325 LY_STMT_LEAF, &grp->child, 0,
2326 LY_STMT_LEAF_LIST, &grp->child, 0,
2327 LY_STMT_LIST, &grp->child, 0,
Michal Vasko69730152020-10-09 16:30:07 +02002328 LY_STMT_NOTIFICATION, &grp->notifs, 0,
2329 LY_STMT_REFERENCE, &grp->ref, YIN_SUBELEM_UNIQUE,
2330 LY_STMT_STATUS, &grp->flags, YIN_SUBELEM_UNIQUE,
2331 LY_STMT_TYPEDEF, &grp->typedefs, 0,
Radek Krejci01180ac2021-01-27 08:48:22 +01002332 LY_STMT_USES, &grp->child, 0,
Michal Vasko69730152020-10-09 16:30:07 +02002333 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
Radek Krejcif13b87b2020-12-01 22:02:17 +01002334 ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_GROUPING, NULL, &grp->exts);
2335 subelems_deallocator(subelems_size, subelems);
David Sedlák81497a32019-08-13 16:56:26 +02002336
aPiecek63e080d2021-06-29 13:53:28 +02002337 /* store data for collision check */
2338 if (!ret && grp->parent) {
2339 assert(ctx->main_ctx);
2340 LY_CHECK_RET(ly_set_add(&ctx->main_ctx->grps_nodes, grp->parent, 0, NULL));
2341 }
2342
Radek Krejci2a9fc652021-01-22 17:44:34 +01002343 return ret;
David Sedláke3ce9ef2019-07-23 16:34:30 +02002344}
2345
2346/**
David Sedlákbf8a2b72019-08-14 16:48:10 +02002347 * @brief Parse container element.
David Sedlákf111bcb2019-07-23 17:15:51 +02002348 *
2349 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákad83cf92019-08-13 12:53:53 +02002350 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlákf111bcb2019-07-23 17:15:51 +02002351 *
2352 * @return LY_ERR values.
2353 */
2354static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002355yin_parse_container(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta)
David Sedlákf111bcb2019-07-23 17:15:51 +02002356{
David Sedlákf111bcb2019-07-23 17:15:51 +02002357 struct lysp_node_container *cont;
David Sedlák81497a32019-08-13 16:56:26 +02002358 LY_ERR ret = LY_SUCCESS;
2359 struct yin_subelement *subelems = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002360 size_t subelems_size;
David Sedlákf111bcb2019-07-23 17:15:51 +02002361
2362 /* create new container */
Michal Vaskob36053d2020-03-26 15:49:30 +01002363 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, cont, next, LY_EMEM);
David Sedlákf111bcb2019-07-23 17:15:51 +02002364 cont->nodetype = LYS_CONTAINER;
2365 cont->parent = node_meta->parent;
2366
David Sedlákf111bcb2019-07-23 17:15:51 +02002367 /* parse aegument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002368 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2369 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 +02002370
2371 /* parse container content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002372 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 21, (struct lysp_node *)cont, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02002373 LY_STMT_ACTION, &cont->actions, YIN_SUBELEM_VER2,
2374 LY_STMT_ANYDATA, &cont->child, YIN_SUBELEM_VER2,
2375 LY_STMT_ANYXML, &cont->child, 0,
2376 LY_STMT_CHOICE, &cont->child, 0,
2377 LY_STMT_CONFIG, &cont->flags, YIN_SUBELEM_UNIQUE,
2378 LY_STMT_CONTAINER, &cont->child, 0,
2379 LY_STMT_DESCRIPTION, &cont->dsc, YIN_SUBELEM_UNIQUE,
2380 LY_STMT_GROUPING, &cont->groupings, 0,
2381 LY_STMT_IF_FEATURE, &cont->iffeatures, 0,
2382 LY_STMT_LEAF, &cont->child, 0,
2383 LY_STMT_LEAF_LIST, &cont->child, 0,
2384 LY_STMT_LIST, &cont->child, 0,
2385 LY_STMT_MUST, &cont->musts, 0,
2386 LY_STMT_NOTIFICATION, &cont->notifs, YIN_SUBELEM_VER2,
2387 LY_STMT_PRESENCE, &cont->presence, YIN_SUBELEM_UNIQUE,
2388 LY_STMT_REFERENCE, &cont->ref, YIN_SUBELEM_UNIQUE,
2389 LY_STMT_STATUS, &cont->flags, YIN_SUBELEM_UNIQUE,
2390 LY_STMT_TYPEDEF, &cont->typedefs, 0,
2391 LY_STMT_USES, &cont->child, 0,
2392 LY_STMT_WHEN, &cont->when, YIN_SUBELEM_UNIQUE,
2393 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
Radek Krejcif13b87b2020-12-01 22:02:17 +01002394 ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_CONTAINER, NULL, &cont->exts);
2395 subelems_deallocator(subelems_size, subelems);
David Sedlák81497a32019-08-13 16:56:26 +02002396
Radek Krejci2a9fc652021-01-22 17:44:34 +01002397 return ret;
David Sedlákf111bcb2019-07-23 17:15:51 +02002398}
2399
2400/**
David Sedlák5379d392019-07-24 10:42:03 +02002401 * @brief Parse case element.
2402 *
2403 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákad83cf92019-08-13 12:53:53 +02002404 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlák5379d392019-07-24 10:42:03 +02002405 *
2406 * @return LY_ERR values.
2407 */
2408static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002409yin_parse_case(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta)
David Sedlák5379d392019-07-24 10:42:03 +02002410{
David Sedlák5379d392019-07-24 10:42:03 +02002411 struct lysp_node_case *cas;
David Sedlák81497a32019-08-13 16:56:26 +02002412 LY_ERR ret = LY_SUCCESS;
Michal Vasko69730152020-10-09 16:30:07 +02002413 struct yin_subelement *subelems = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002414 size_t subelems_size;
David Sedlák5379d392019-07-24 10:42:03 +02002415
2416 /* create new case */
Michal Vaskob36053d2020-03-26 15:49:30 +01002417 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, cas, next, LY_EMEM);
David Sedlák5379d392019-07-24 10:42:03 +02002418 cas->nodetype = LYS_CASE;
2419 cas->parent = node_meta->parent;
2420
David Sedlák5379d392019-07-24 10:42:03 +02002421 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002422 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2423 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 +02002424
2425 /* parse case content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002426 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 14, (struct lysp_node *)cas, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02002427 LY_STMT_ANYDATA, &cas->child, YIN_SUBELEM_VER2,
2428 LY_STMT_ANYXML, &cas->child, 0,
2429 LY_STMT_CHOICE, &cas->child, 0,
2430 LY_STMT_CONTAINER, &cas->child, 0,
2431 LY_STMT_DESCRIPTION, &cas->dsc, YIN_SUBELEM_UNIQUE,
2432 LY_STMT_IF_FEATURE, &cas->iffeatures, 0,
2433 LY_STMT_LEAF, &cas->child, 0,
2434 LY_STMT_LEAF_LIST, &cas->child, 0,
2435 LY_STMT_LIST, &cas->child, 0,
2436 LY_STMT_REFERENCE, &cas->ref, YIN_SUBELEM_UNIQUE,
2437 LY_STMT_STATUS, &cas->flags, YIN_SUBELEM_UNIQUE,
2438 LY_STMT_USES, &cas->child, 0,
2439 LY_STMT_WHEN, &cas->when, YIN_SUBELEM_UNIQUE,
2440 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
Radek Krejcif13b87b2020-12-01 22:02:17 +01002441 ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_CASE, NULL, &cas->exts);
2442 subelems_deallocator(subelems_size, subelems);
David Sedlák81497a32019-08-13 16:56:26 +02002443
2444 return ret;
David Sedlák5379d392019-07-24 10:42:03 +02002445}
2446
2447/**
David Sedlák05404f62019-07-24 14:11:53 +02002448 * @brief Parse choice element.
David Sedlákb7abcfa2019-07-24 12:33:35 +02002449 *
2450 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákad83cf92019-08-13 12:53:53 +02002451 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlákb7abcfa2019-07-24 12:33:35 +02002452 *
2453 * @return LY_ERR values.
2454 */
2455LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002456yin_parse_choice(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta)
David Sedlákb7abcfa2019-07-24 12:33:35 +02002457{
David Sedlák81497a32019-08-13 16:56:26 +02002458 LY_ERR ret = LY_SUCCESS;
2459 struct yin_subelement *subelems = NULL;
David Sedlákb7abcfa2019-07-24 12:33:35 +02002460 struct lysp_node_choice *choice;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002461 size_t subelems_size;
David Sedlákb7abcfa2019-07-24 12:33:35 +02002462
2463 /* create new choice */
Michal Vaskob36053d2020-03-26 15:49:30 +01002464 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, choice, next, LY_EMEM);
David Sedlák8d552d62019-08-06 15:29:05 +02002465
David Sedlákb7abcfa2019-07-24 12:33:35 +02002466 choice->nodetype = LYS_CHOICE;
2467 choice->parent = node_meta->parent;
2468
David Sedlákb7abcfa2019-07-24 12:33:35 +02002469 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002470 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2471 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 +02002472
2473 /* parse choice content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002474 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 17, (struct lysp_node *)choice, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02002475 LY_STMT_ANYDATA, &choice->child, YIN_SUBELEM_VER2,
2476 LY_STMT_ANYXML, &choice->child, 0,
2477 LY_STMT_CASE, &choice->child, 0,
2478 LY_STMT_CHOICE, &choice->child, YIN_SUBELEM_VER2,
2479 LY_STMT_CONFIG, &choice->flags, YIN_SUBELEM_UNIQUE,
2480 LY_STMT_CONTAINER, &choice->child, 0,
2481 LY_STMT_DEFAULT, &choice->dflt, YIN_SUBELEM_UNIQUE,
2482 LY_STMT_DESCRIPTION, &choice->dsc, YIN_SUBELEM_UNIQUE,
2483 LY_STMT_IF_FEATURE, &choice->iffeatures, 0,
2484 LY_STMT_LEAF, &choice->child, 0,
2485 LY_STMT_LEAF_LIST, &choice->child, 0,
2486 LY_STMT_LIST, &choice->child, 0,
2487 LY_STMT_MANDATORY, &choice->flags, YIN_SUBELEM_UNIQUE,
2488 LY_STMT_REFERENCE, &choice->ref, YIN_SUBELEM_UNIQUE,
2489 LY_STMT_STATUS, &choice->flags, YIN_SUBELEM_UNIQUE,
2490 LY_STMT_WHEN, &choice->when, YIN_SUBELEM_UNIQUE,
2491 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
Radek Krejcif13b87b2020-12-01 22:02:17 +01002492 ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_CHOICE, NULL, &choice->exts);
2493 subelems_deallocator(subelems_size, subelems);
David Sedlák81497a32019-08-13 16:56:26 +02002494 return ret;
David Sedlákb7abcfa2019-07-24 12:33:35 +02002495}
2496
2497/**
David Sedlák05404f62019-07-24 14:11:53 +02002498 * @brief Parse input or output element.
2499 *
2500 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbf8a2b72019-08-14 16:48:10 +02002501 * @param[in] inout_kw Identification of input/output element.
David Sedlák05404f62019-07-24 14:11:53 +02002502 * @param[in] inout_meta Meta information about parent node and siblings and input/output pointer to write to.
2503 *
2504 * @return LY_ERR values.
2505 */
2506static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002507yin_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 +02002508{
David Sedlák81497a32019-08-13 16:56:26 +02002509 LY_ERR ret = LY_SUCCESS;
2510 struct yin_subelement *subelems = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002511 size_t subelems_size;
David Sedlák81497a32019-08-13 16:56:26 +02002512
David Sedlák05404f62019-07-24 14:11:53 +02002513 /* initiate structure */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002514 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 +02002515 inout_meta->inout_p->nodetype = (inout_kw == LY_STMT_INPUT) ? LYS_INPUT : LYS_OUTPUT;
David Sedlák05404f62019-07-24 14:11:53 +02002516 inout_meta->inout_p->parent = inout_meta->parent;
2517
2518 /* check attributes */
Michal Vaskob36053d2020-03-26 15:49:30 +01002519 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2520 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 +02002521
2522 /* parser input/output content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002523 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 12, (struct lysp_node *)inout_meta->inout_p, &subelems,
Radek Krejci01180ac2021-01-27 08:48:22 +01002524 LY_STMT_ANYDATA, &inout_meta->inout_p->child, YIN_SUBELEM_VER2,
2525 LY_STMT_ANYXML, &inout_meta->inout_p->child, 0,
2526 LY_STMT_CHOICE, &inout_meta->inout_p->child, 0,
2527 LY_STMT_CONTAINER, &inout_meta->inout_p->child, 0,
Michal Vasko69730152020-10-09 16:30:07 +02002528 LY_STMT_GROUPING, &inout_meta->inout_p->groupings, 0,
Radek Krejci01180ac2021-01-27 08:48:22 +01002529 LY_STMT_LEAF, &inout_meta->inout_p->child, 0,
2530 LY_STMT_LEAF_LIST, &inout_meta->inout_p->child, 0,
2531 LY_STMT_LIST, &inout_meta->inout_p->child, 0,
Michal Vasko69730152020-10-09 16:30:07 +02002532 LY_STMT_MUST, &inout_meta->inout_p->musts, YIN_SUBELEM_VER2,
2533 LY_STMT_TYPEDEF, &inout_meta->inout_p->typedefs, 0,
Radek Krejci01180ac2021-01-27 08:48:22 +01002534 LY_STMT_USES, &inout_meta->inout_p->child, 0,
Michal Vasko69730152020-10-09 16:30:07 +02002535 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
Radek Krejcif13b87b2020-12-01 22:02:17 +01002536 ret = yin_parse_content(ctx, subelems, subelems_size, inout_kw, NULL, &inout_meta->inout_p->exts);
2537 subelems_deallocator(subelems_size, subelems);
David Sedlák81497a32019-08-13 16:56:26 +02002538 LY_CHECK_RET(ret);
David Sedlák05404f62019-07-24 14:11:53 +02002539
Radek Krejci01180ac2021-01-27 08:48:22 +01002540 if (!inout_meta->inout_p->child) {
Michal Vaskob83af8a2020-01-06 09:49:22 +01002541 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_MISSTMT, "data-def-stmt", ly_stmt2str(inout_kw));
2542 return LY_EVALID;
2543 }
2544
David Sedlák05404f62019-07-24 14:11:53 +02002545 return LY_SUCCESS;
2546}
2547
David Sedlák992fb7c2019-07-24 16:51:01 +02002548/**
2549 * @brief Parse action element.
2550 *
2551 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák992fb7c2019-07-24 16:51:01 +02002552 * @param[in] act_meta Meta information about parent node and actions to add to.
2553 *
2554 * @return LY_ERR values.
2555 */
David Sedlák85d0eca2019-07-24 15:15:21 +02002556static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002557yin_parse_action(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *act_meta)
David Sedlák85d0eca2019-07-24 15:15:21 +02002558{
Radek Krejci2a9fc652021-01-22 17:44:34 +01002559 struct lysp_node_action *act, **acts = (struct lysp_node_action **)act_meta->nodes;
David Sedlák81497a32019-08-13 16:56:26 +02002560 LY_ERR ret = LY_SUCCESS;
2561 struct yin_subelement *subelems = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002562 size_t subelems_size;
Radek Krejci39b7fc22021-02-26 23:29:18 +01002563 enum ly_stmt kw = act_meta->parent ? LY_STMT_ACTION : LY_STMT_RPC;
David Sedlák85d0eca2019-07-24 15:15:21 +02002564
2565 /* create new action */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002566 LY_LIST_NEW_RET(ctx->xmlctx->ctx, acts, act, next, LY_EMEM);
Michal Vasko1bf09392020-03-27 12:38:10 +01002567 act->nodetype = act_meta->parent ? LYS_ACTION : LYS_RPC;
David Sedlák85d0eca2019-07-24 15:15:21 +02002568 act->parent = act_meta->parent;
2569
2570 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002571 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
Radek Krejci39b7fc22021-02-26 23:29:18 +01002572 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &act->name, Y_IDENTIF_ARG, kw));
David Sedlák85d0eca2019-07-24 15:15:21 +02002573
2574 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002575 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 9, (struct lysp_node *)act, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02002576 LY_STMT_DESCRIPTION, &act->dsc, YIN_SUBELEM_UNIQUE,
2577 LY_STMT_GROUPING, &act->groupings, 0,
2578 LY_STMT_IF_FEATURE, &act->iffeatures, 0,
2579 LY_STMT_INPUT, &act->input, YIN_SUBELEM_UNIQUE,
2580 LY_STMT_OUTPUT, &act->output, YIN_SUBELEM_UNIQUE,
2581 LY_STMT_REFERENCE, &act->ref, YIN_SUBELEM_UNIQUE,
2582 LY_STMT_STATUS, &act->flags, YIN_SUBELEM_UNIQUE,
2583 LY_STMT_TYPEDEF, &act->typedefs, 0,
2584 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
Radek Krejci39b7fc22021-02-26 23:29:18 +01002585 ret = (yin_parse_content(ctx, subelems, subelems_size, kw, NULL, &act->exts));
Radek Krejcif13b87b2020-12-01 22:02:17 +01002586 subelems_deallocator(subelems_size, subelems);
David Sedlák81497a32019-08-13 16:56:26 +02002587 LY_CHECK_RET(ret);
2588
Michal Vasko7f45cf22020-10-01 12:49:44 +02002589 /* always initialize inout, they are technically present (needed for later deviations/refines) */
2590 if (!act->input.nodetype) {
2591 act->input.nodetype = LYS_INPUT;
2592 act->input.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002593 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "input", 0, &act->input.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002594 }
2595 if (!act->output.nodetype) {
2596 act->output.nodetype = LYS_OUTPUT;
2597 act->output.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002598 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "output", 0, &act->output.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002599 }
2600
David Sedlák85d0eca2019-07-24 15:15:21 +02002601 return LY_SUCCESS;
2602}
2603
David Sedlák05404f62019-07-24 14:11:53 +02002604/**
David Sedlák992fb7c2019-07-24 16:51:01 +02002605 * @brief Parse augment element.
2606 *
2607 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák992fb7c2019-07-24 16:51:01 +02002608 * @param[in] aug_meta Meta information about parent node and augments to add to.
2609 *
2610 * @return LY_ERR values.
2611 */
2612static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002613yin_parse_augment(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *aug_meta)
David Sedlák992fb7c2019-07-24 16:51:01 +02002614{
Radek Krejci2a9fc652021-01-22 17:44:34 +01002615 struct lysp_node_augment *aug;
2616 struct lysp_node_augment **augs = (struct lysp_node_augment **)aug_meta->nodes;
David Sedlák81497a32019-08-13 16:56:26 +02002617 LY_ERR ret = LY_SUCCESS;
2618 struct yin_subelement *subelems = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002619 size_t subelems_size;
David Sedlák992fb7c2019-07-24 16:51:01 +02002620
2621 /* create new augment */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002622 LY_LIST_NEW_RET(ctx->xmlctx->ctx, augs, aug, next, LY_EMEM);
David Sedlák992fb7c2019-07-24 16:51:01 +02002623 aug->nodetype = LYS_AUGMENT;
2624 aug->parent = aug_meta->parent;
2625
2626 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002627 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2628 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_TARGET_NODE, &aug->nodeid, Y_STR_ARG, LY_STMT_AUGMENT));
2629 CHECK_NONEMPTY((struct lys_parser_ctx *)ctx, strlen(aug->nodeid), "augment");
David Sedlák992fb7c2019-07-24 16:51:01 +02002630
2631 /* parser augment content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002632 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 17, (struct lysp_node *)aug, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02002633 LY_STMT_ACTION, &aug->actions, YIN_SUBELEM_VER2,
2634 LY_STMT_ANYDATA, &aug->child, YIN_SUBELEM_VER2,
2635 LY_STMT_ANYXML, &aug->child, 0,
2636 LY_STMT_CASE, &aug->child, 0,
2637 LY_STMT_CHOICE, &aug->child, 0,
2638 LY_STMT_CONTAINER, &aug->child, 0,
2639 LY_STMT_DESCRIPTION, &aug->dsc, YIN_SUBELEM_UNIQUE,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002640 LY_STMT_IF_FEATURE, &aug->iffeatures, 0,
Michal Vasko69730152020-10-09 16:30:07 +02002641 LY_STMT_LEAF, &aug->child, 0,
2642 LY_STMT_LEAF_LIST, &aug->child, 0,
2643 LY_STMT_LIST, &aug->child, 0,
2644 LY_STMT_NOTIFICATION, &aug->notifs, YIN_SUBELEM_VER2,
2645 LY_STMT_REFERENCE, &aug->ref, YIN_SUBELEM_UNIQUE,
2646 LY_STMT_STATUS, &aug->flags, YIN_SUBELEM_UNIQUE,
2647 LY_STMT_USES, &aug->child, 0,
2648 LY_STMT_WHEN, &aug->when, YIN_SUBELEM_UNIQUE,
2649 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
Radek Krejcif13b87b2020-12-01 22:02:17 +01002650 ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_AUGMENT, NULL, &aug->exts);
2651 subelems_deallocator(subelems_size, subelems);
David Sedlák992fb7c2019-07-24 16:51:01 +02002652
Radek Krejci2a9fc652021-01-22 17:44:34 +01002653 return ret;
David Sedlák992fb7c2019-07-24 16:51:01 +02002654}
2655
David Sedlák8b754462019-07-25 16:22:13 +02002656/**
2657 * @brief Parse deviate element.
2658 *
2659 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák8b754462019-07-25 16:22:13 +02002660 * @param[in] deviates Deviates to add to.
2661 *
2662 * @return LY_ERR values.
2663 */
David Sedlák4ffcec82019-07-25 15:10:21 +02002664static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002665yin_parse_deviate(struct lys_yin_parser_ctx *ctx, struct lysp_deviate **deviates)
David Sedlák4ffcec82019-07-25 15:10:21 +02002666{
2667 LY_ERR ret = LY_SUCCESS;
2668 uint8_t dev_mod;
2669 const char *temp_val;
David Sedlák8d552d62019-08-06 15:29:05 +02002670 struct lysp_deviate *d;
David Sedlák4ffcec82019-07-25 15:10:21 +02002671 struct lysp_deviate_add *d_add = NULL;
2672 struct lysp_deviate_rpl *d_rpl = NULL;
2673 struct lysp_deviate_del *d_del = NULL;
2674
2675 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002676 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2677 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 +02002678
2679 if (strcmp(temp_val, "not-supported") == 0) {
2680 dev_mod = LYS_DEV_NOT_SUPPORTED;
2681 } else if (strcmp(temp_val, "add") == 0) {
2682 dev_mod = LYS_DEV_ADD;
2683 } else if (strcmp(temp_val, "replace") == 0) {
2684 dev_mod = LYS_DEV_REPLACE;
2685 } else if (strcmp(temp_val, "delete") == 0) {
2686 dev_mod = LYS_DEV_DELETE;
2687 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02002688 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS4, temp_val, "value", "deviate",
Michal Vasko69730152020-10-09 16:30:07 +02002689 "not-supported", "add", "replace", "delete");
Michal Vaskoe180ed02021-02-05 16:31:20 +01002690 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlák4ffcec82019-07-25 15:10:21 +02002691 return LY_EVALID;
2692 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01002693 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlák4ffcec82019-07-25 15:10:21 +02002694
2695 if (dev_mod == LYS_DEV_NOT_SUPPORTED) {
2696 d = calloc(1, sizeof *d);
Michal Vaskob36053d2020-03-26 15:49:30 +01002697 LY_CHECK_ERR_RET(!d, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
Radek Krejcif13b87b2020-12-01 22:02:17 +01002698 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002699 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
2700 };
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 };
Radek Krejcif13b87b2020-12-01 22:02:17 +01002720 ret = yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_DEVIATE, NULL, &d_add->exts);
David Sedlák4ffcec82019-07-25 15:10:21 +02002721
2722 } else if (dev_mod == LYS_DEV_REPLACE) {
2723 d_rpl = calloc(1, sizeof *d_rpl);
Michal Vaskob36053d2020-03-26 15:49:30 +01002724 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
David Sedlák4ffcec82019-07-25 15:10:21 +02002725 d = (struct lysp_deviate *)d_rpl;
2726 struct minmax_dev_meta min = {&d_rpl->min, &d_rpl->flags, &d_rpl->exts};
2727 struct minmax_dev_meta max = {&d_rpl->max, &d_rpl->flags, &d_rpl->exts};
Radek Krejcif13b87b2020-12-01 22:02:17 +01002728 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002729 {LY_STMT_CONFIG, &d_rpl->flags, YIN_SUBELEM_UNIQUE},
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002730 {LY_STMT_DEFAULT, &d_rpl->dflt, YIN_SUBELEM_UNIQUE},
Radek Krejci0f969882020-08-21 16:56:47 +02002731 {LY_STMT_MANDATORY, &d_rpl->flags, YIN_SUBELEM_UNIQUE},
2732 {LY_STMT_MAX_ELEMENTS, &max, YIN_SUBELEM_UNIQUE},
2733 {LY_STMT_MIN_ELEMENTS, &min, YIN_SUBELEM_UNIQUE},
2734 {LY_STMT_TYPE, &d_rpl->type, YIN_SUBELEM_UNIQUE},
2735 {LY_STMT_UNITS, &d_rpl->units, YIN_SUBELEM_UNIQUE},
2736 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
2737 };
Radek Krejcif13b87b2020-12-01 22:02:17 +01002738 ret = yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_DEVIATE, NULL, &d_rpl->exts);
David Sedlák4ffcec82019-07-25 15:10:21 +02002739
2740 } else {
2741 d_del = calloc(1, sizeof *d_del);
Michal Vaskob36053d2020-03-26 15:49:30 +01002742 LY_CHECK_ERR_RET(!d_del, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
David Sedlák4ffcec82019-07-25 15:10:21 +02002743 d = (struct lysp_deviate *)d_del;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002744 struct yin_subelement subelems[] = {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002745 {LY_STMT_DEFAULT, &d_del->dflts, 0},
Radek Krejci0f969882020-08-21 16:56:47 +02002746 {LY_STMT_MUST, &d_del->musts, 0},
2747 {LY_STMT_UNIQUE, &d_del->uniques, 0},
2748 {LY_STMT_UNITS, &d_del->units, YIN_SUBELEM_UNIQUE},
2749 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
2750 };
Radek Krejcif13b87b2020-12-01 22:02:17 +01002751 ret = yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_DEVIATE, NULL, &d_del->exts);
David Sedlák4ffcec82019-07-25 15:10:21 +02002752 }
2753 LY_CHECK_GOTO(ret, cleanup);
2754
2755 d->mod = dev_mod;
2756 /* insert into siblings */
David Sedlák8d552d62019-08-06 15:29:05 +02002757 LY_LIST_INSERT(deviates, d, next);
David Sedlák4ffcec82019-07-25 15:10:21 +02002758
2759 return ret;
2760
2761cleanup:
2762 free(d);
David Sedlák4ffcec82019-07-25 15:10:21 +02002763 return ret;
2764}
2765
David Sedlák992fb7c2019-07-24 16:51:01 +02002766/**
David Sedlák8b754462019-07-25 16:22:13 +02002767 * @brief Parse deviation element.
2768 *
2769 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák8b754462019-07-25 16:22:13 +02002770 * @param[in] deviations Deviations to add to.
2771 *
2772 * @return LY_ERR values.
2773 */
2774static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002775yin_parse_deviation(struct lys_yin_parser_ctx *ctx, struct lysp_deviation **deviations)
David Sedlák8b754462019-07-25 16:22:13 +02002776{
2777 struct lysp_deviation *dev;
2778
2779 /* create new deviation */
Michal Vaskob36053d2020-03-26 15:49:30 +01002780 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *deviations, dev, LY_EMEM);
David Sedlák8b754462019-07-25 16:22:13 +02002781
2782 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002783 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2784 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_TARGET_NODE, &dev->nodeid, Y_STR_ARG, LY_STMT_DEVIATION));
2785 CHECK_NONEMPTY((struct lys_parser_ctx *)ctx, strlen(dev->nodeid), "deviation");
Radek Krejcif13b87b2020-12-01 22:02:17 +01002786 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002787 {LY_STMT_DESCRIPTION, &dev->dsc, YIN_SUBELEM_UNIQUE},
2788 {LY_STMT_DEVIATE, &dev->deviates, YIN_SUBELEM_MANDATORY},
2789 {LY_STMT_REFERENCE, &dev->ref, YIN_SUBELEM_UNIQUE},
2790 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
2791 };
Michal Vasko69730152020-10-09 16:30:07 +02002792
Radek Krejcif13b87b2020-12-01 22:02:17 +01002793 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_DEVIATION, NULL, &dev->exts);
David Sedlák8b754462019-07-25 16:22:13 +02002794}
2795
2796/**
David Sedlákc5b20842019-08-13 10:18:31 +02002797 * @brief map keyword to keyword-group.
2798 *
2799 * @param[in] ctx YIN parser context used for logging.
2800 * @param[in] kw Keyword that is child of module or submodule.
2801 * @param[out] group Group of keyword.
2802 *
2803 * @return LY_SUCCESS on success LY_EINT if kw can't be mapped to kw_group, should not happen if called correctly.
2804 */
David Sedláke6cd89e2019-08-07 12:46:02 +02002805static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002806kw2kw_group(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, enum yang_module_stmt *group)
David Sedláke6cd89e2019-08-07 12:46:02 +02002807{
2808 switch (kw) {
Radek Krejci0f969882020-08-21 16:56:47 +02002809 /* module header */
2810 case LY_STMT_NONE:
2811 case LY_STMT_NAMESPACE:
2812 case LY_STMT_PREFIX:
2813 case LY_STMT_BELONGS_TO:
2814 case LY_STMT_YANG_VERSION:
2815 *group = Y_MOD_MODULE_HEADER;
2816 break;
2817 /* linkage */
2818 case LY_STMT_INCLUDE:
2819 case LY_STMT_IMPORT:
2820 *group = Y_MOD_LINKAGE;
2821 break;
2822 /* meta */
2823 case LY_STMT_ORGANIZATION:
2824 case LY_STMT_CONTACT:
2825 case LY_STMT_DESCRIPTION:
2826 case LY_STMT_REFERENCE:
2827 *group = Y_MOD_META;
2828 break;
2829 /* revision */
2830 case LY_STMT_REVISION:
2831 *group = Y_MOD_REVISION;
2832 break;
2833 /* body */
2834 case LY_STMT_ANYDATA:
2835 case LY_STMT_ANYXML:
2836 case LY_STMT_AUGMENT:
2837 case LY_STMT_CHOICE:
2838 case LY_STMT_CONTAINER:
2839 case LY_STMT_DEVIATION:
2840 case LY_STMT_EXTENSION:
2841 case LY_STMT_FEATURE:
2842 case LY_STMT_GROUPING:
2843 case LY_STMT_IDENTITY:
2844 case LY_STMT_LEAF:
2845 case LY_STMT_LEAF_LIST:
2846 case LY_STMT_LIST:
2847 case LY_STMT_NOTIFICATION:
2848 case LY_STMT_RPC:
2849 case LY_STMT_TYPEDEF:
2850 case LY_STMT_USES:
2851 case LY_STMT_EXTENSION_INSTANCE:
2852 *group = Y_MOD_BODY;
2853 break;
2854 default:
2855 LOGINT(ctx->xmlctx->ctx);
2856 return LY_EINT;
David Sedláke6cd89e2019-08-07 12:46:02 +02002857 }
2858
2859 return LY_SUCCESS;
2860}
2861
David Sedlákc5b20842019-08-13 10:18:31 +02002862/**
2863 * @brief Check if relative order of two keywords is valid.
2864 *
2865 * @param[in] ctx YIN parser context used for logging.
2866 * @param[in] kw Current keyword.
2867 * @param[in] next_kw Next keyword.
Radek Krejcid6b76452019-09-03 17:03:03 +02002868 * @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 +02002869 * because relative order is required only in module and submodule sub-elements, used for logging.
2870 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02002871 * @return LY_SUCCESS on success and LY_EVALID if relative order is invalid.
David Sedlákc5b20842019-08-13 10:18:31 +02002872 */
David Sedláke6cd89e2019-08-07 12:46:02 +02002873static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002874yin_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 +02002875{
Radek Krejcid6b76452019-09-03 17:03:03 +02002876 assert(parrent == LY_STMT_MODULE || parrent == LY_STMT_SUBMODULE);
David Sedláke6cd89e2019-08-07 12:46:02 +02002877 enum yang_module_stmt gr, next_gr;
2878
2879 LY_CHECK_RET(kw2kw_group(ctx, kw, &gr));
2880 LY_CHECK_RET(kw2kw_group(ctx, next_kw, &next_gr));
2881
2882 if (gr > next_gr) {
2883 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INORDER_YIN, ly_stmt2str(parrent), ly_stmt2str(next_kw), ly_stmt2str(kw));
2884 return LY_EVALID;
2885 }
2886
2887 return LY_SUCCESS;
2888}
2889
David Sedlákc5dd0942019-09-13 10:23:59 +02002890/**
Michal Vaskoafac7822020-10-20 14:22:26 +02002891 * @brief Parse argument of extension subelement that is classic yang keyword and not another instance of extension.
2892 *
2893 * @param[in,out] ctx Yin parser context for logging and to store current state.
2894 * @param[in] elem_type Type of element that is currently being parsed.
2895 * @param[out] arg Value to write to.
2896 *
2897 * @return LY_ERR values.
2898 */
2899static LY_ERR
2900yin_parse_extension_instance_arg(struct lys_yin_parser_ctx *ctx, enum ly_stmt elem_type, const char **arg)
2901{
2902 enum ly_stmt child;
2903
2904 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2905
2906 switch (elem_type) {
2907 case LY_STMT_ACTION:
2908 case LY_STMT_ANYDATA:
2909 case LY_STMT_ANYXML:
2910 case LY_STMT_ARGUMENT:
2911 case LY_STMT_BASE:
2912 case LY_STMT_BIT:
2913 case LY_STMT_CASE:
2914 case LY_STMT_CHOICE:
2915 case LY_STMT_CONTAINER:
2916 case LY_STMT_ENUM:
2917 case LY_STMT_EXTENSION:
2918 case LY_STMT_FEATURE:
2919 case LY_STMT_GROUPING:
2920 case LY_STMT_IDENTITY:
2921 case LY_STMT_IF_FEATURE:
2922 case LY_STMT_LEAF:
2923 case LY_STMT_LEAF_LIST:
2924 case LY_STMT_LIST:
2925 case LY_STMT_MODULE:
2926 case LY_STMT_NOTIFICATION:
2927 case LY_STMT_RPC:
2928 case LY_STMT_SUBMODULE:
2929 case LY_STMT_TYPE:
2930 case LY_STMT_TYPEDEF:
2931 case LY_STMT_UNITS:
2932 case LY_STMT_USES:
2933 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, arg, Y_MAYBE_STR_ARG, elem_type));
2934 break;
2935 case LY_STMT_AUGMENT:
2936 case LY_STMT_DEVIATION:
2937 case LY_STMT_REFINE:
2938 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_TARGET_NODE, arg, Y_MAYBE_STR_ARG, elem_type));
2939 break;
2940 case LY_STMT_CONFIG:
2941 case LY_STMT_DEFAULT:
2942 case LY_STMT_DEVIATE:
2943 case LY_STMT_ERROR_APP_TAG:
2944 case LY_STMT_FRACTION_DIGITS:
2945 case LY_STMT_KEY:
2946 case LY_STMT_LENGTH:
2947 case LY_STMT_MANDATORY:
2948 case LY_STMT_MAX_ELEMENTS:
2949 case LY_STMT_MIN_ELEMENTS:
2950 case LY_STMT_MODIFIER:
2951 case LY_STMT_ORDERED_BY:
2952 case LY_STMT_PATH:
2953 case LY_STMT_PATTERN:
2954 case LY_STMT_POSITION:
2955 case LY_STMT_PREFIX:
2956 case LY_STMT_PRESENCE:
2957 case LY_STMT_RANGE:
2958 case LY_STMT_REQUIRE_INSTANCE:
2959 case LY_STMT_STATUS:
2960 case LY_STMT_VALUE:
2961 case LY_STMT_YANG_VERSION:
2962 case LY_STMT_YIN_ELEMENT:
2963 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, arg, Y_MAYBE_STR_ARG, elem_type));
2964 break;
2965 case LY_STMT_IMPORT:
2966 case LY_STMT_INCLUDE:
2967 case LY_STMT_BELONGS_TO:
2968 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_MODULE, arg, Y_MAYBE_STR_ARG, elem_type));
2969 break;
2970 case LY_STMT_INPUT:
2971 case LY_STMT_OUTPUT:
2972 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NONE, arg, Y_MAYBE_STR_ARG, elem_type));
2973 break;
2974 case LY_STMT_MUST:
2975 case LY_STMT_WHEN:
2976 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_CONDITION, arg, Y_MAYBE_STR_ARG, elem_type));
2977 break;
2978 case LY_STMT_NAMESPACE:
2979 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_URI, arg, Y_MAYBE_STR_ARG, elem_type));
2980 break;
2981 case LY_STMT_REVISION:
2982 case LY_STMT_REVISION_DATE:
2983 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_DATE, arg, Y_MAYBE_STR_ARG, elem_type));
2984 break;
2985 case LY_STMT_UNIQUE:
2986 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_TAG, arg, Y_MAYBE_STR_ARG, elem_type));
2987 break;
2988 /* argument is mapped to yin element */
2989 case LY_STMT_CONTACT:
2990 case LY_STMT_DESCRIPTION:
2991 case LY_STMT_ORGANIZATION:
2992 case LY_STMT_REFERENCE:
2993 case LY_STMT_ERROR_MESSAGE:
2994 /* there shouldn't be any attribute, argument is supposed to be first subelement */
2995 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NONE, arg, Y_MAYBE_STR_ARG, elem_type));
2996
2997 /* no content */
2998 assert(ctx->xmlctx->status == LYXML_ELEM_CONTENT);
2999 if (ctx->xmlctx->ws_only) {
3000 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3001 }
3002 if (((ctx->xmlctx->status == LYXML_ELEM_CONTENT) && !ctx->xmlctx->ws_only) || (ctx->xmlctx->status != LYXML_ELEMENT)) {
3003 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_FIRT_SUBELEM,
3004 elem_type == LY_STMT_ERROR_MESSAGE ? "value" : "text", ly_stmt2str(elem_type));
3005 return LY_EVALID;
3006 }
3007
3008 /* parse child element */
3009 child = yin_match_keyword(ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, ctx->xmlctx->prefix, ctx->xmlctx->prefix_len, elem_type);
3010 if (((elem_type == LY_STMT_ERROR_MESSAGE) && (child != LY_STMT_ARG_VALUE)) ||
3011 ((elem_type != LY_STMT_ERROR_MESSAGE) && (child != LY_STMT_ARG_TEXT))) {
3012 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_UNEXP_SUBELEM, ctx->xmlctx->name_len, ctx->xmlctx->name,
3013 ly_stmt2str(elem_type));
3014 return LY_EVALID;
3015 }
3016 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3017
3018 /* no attributes expected? TODO */
3019 while (ctx->xmlctx->status == LYXML_ATTRIBUTE) {
3020 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3021 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3022 }
3023
3024 /* load and save content */
3025 INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic, *arg);
3026 LY_CHECK_RET(!*arg, LY_EMEM);
3027
3028 /* load closing tag of subelement */
3029 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3030
3031 /* if only subelement was parsed as argument, load also closing tag TODO what? */
3032 /*if (ctx->xmlctx->status == LYXML_ELEMENT) {
3033 LY_CHECK_RET(lyxml_get_element(&ctx->xmlctx, data, &prefix, &prefix_len, &name, &name_len));
3034 }*/
3035 break;
3036 default:
3037 LOGINT(ctx->xmlctx->ctx);
3038 return LY_EINT;
3039 }
3040
3041 return LY_SUCCESS;
3042}
3043
3044/**
3045 * @brief Parse yin element into generic structure.
3046 *
3047 * @param[in,out] ctx Yin parser context for XML context, logging, and to store current state.
3048 * @param[in] parent Identification of parent element.
3049 * @param[out] element Where the element structure should be stored.
3050 *
3051 * @return LY_ERR values.
3052 */
3053LY_ERR
3054yin_parse_element_generic(struct lys_yin_parser_ctx *ctx, enum ly_stmt parent, struct lysp_stmt **element)
3055{
3056 LY_ERR ret = LY_SUCCESS;
3057 struct lysp_stmt *last = NULL, *new = NULL;
Michal Vaskofc2cd072021-02-24 13:17:17 +01003058 char *id;
Michal Vaskoafac7822020-10-20 14:22:26 +02003059
3060 assert(ctx->xmlctx->status == LYXML_ELEMENT);
3061
3062 /* allocate new structure for element */
3063 *element = calloc(1, sizeof(**element));
3064 LY_CHECK_ERR_GOTO(!(*element), LOGMEM(ctx->xmlctx->ctx); ret = LY_EMEM, cleanup);
Michal Vaskofc2cd072021-02-24 13:17:17 +01003065
3066 /* store identifier */
3067 if (ctx->xmlctx->prefix_len) {
3068 if (asprintf(&id, "%.*s:%.*s", (int)ctx->xmlctx->prefix_len, ctx->xmlctx->prefix, (int)ctx->xmlctx->name_len,
3069 ctx->xmlctx->name) == -1) {
3070 LOGMEM(ctx->xmlctx->ctx);
3071 ret = LY_EMEM;
3072 goto cleanup;
3073 }
3074 LY_CHECK_GOTO(ret = lydict_insert_zc(ctx->xmlctx->ctx, id, &(*element)->stmt), cleanup);
3075
3076 /* store prefix data for the statement */
Radek Krejci8df109d2021-04-23 12:19:08 +02003077 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 +01003078 &ctx->xmlctx->ns, &(*element)->format, &(*element)->prefix_data), cleanup);
3079 } else {
3080 LY_CHECK_GOTO(ret = lydict_insert(ctx->xmlctx->ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, &(*element)->stmt), cleanup);
3081 }
Michal Vaskoafac7822020-10-20 14:22:26 +02003082
3083 (*element)->kw = yin_match_keyword(ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, ctx->xmlctx->prefix,
3084 ctx->xmlctx->prefix_len, parent);
3085
3086 last = (*element)->child;
3087 if ((*element)->kw == LY_STMT_NONE) {
3088 /* unrecognized element */
3089 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_UNEXP_SUBELEM, ctx->xmlctx->name_len, ctx->xmlctx->name,
3090 ly_stmt2str(parent));
3091 ret = LY_EVALID;
3092 goto cleanup;
3093 } else if ((*element)->kw != LY_STMT_EXTENSION_INSTANCE) {
3094 /* element is known yang keyword, which means argument can be parsed correctly. */
3095 ret = yin_parse_extension_instance_arg(ctx, (*element)->kw, &(*element)->arg);
3096 LY_CHECK_GOTO(ret, cleanup);
3097 } else {
3098 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3099
3100 /* load attributes in generic way, save all attributes in linked list */
3101 while (ctx->xmlctx->status == LYXML_ATTRIBUTE) {
3102 new = calloc(1, sizeof(*last));
3103 LY_CHECK_ERR_GOTO(!new, LOGMEM(ctx->xmlctx->ctx); ret = LY_EMEM, cleanup);
3104 if (!(*element)->child) {
3105 /* save first */
3106 (*element)->child = new;
3107 } else {
3108 last->next = new;
3109 }
3110 last = new;
3111
3112 last->flags |= LYS_YIN_ATTR;
3113 LY_CHECK_GOTO(ret = lydict_insert(ctx->xmlctx->ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, &last->stmt), cleanup);
3114 last->kw = LY_STMT_NONE;
3115 /* attributes with prefix are ignored */
3116 if (!ctx->xmlctx->prefix) {
3117 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3118
3119 INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic, last->arg);
3120 LY_CHECK_ERR_GOTO(!last->arg, ret = LY_EMEM, cleanup);
3121 } else {
3122 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3123 }
3124 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3125 }
3126 }
3127
3128 if ((ctx->xmlctx->status != LYXML_ELEM_CONTENT) || ctx->xmlctx->ws_only) {
3129 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3130 while (ctx->xmlctx->status == LYXML_ELEMENT) {
3131 /* parse subelements */
3132 ret = yin_parse_element_generic(ctx, (*element)->kw, &new);
3133 LY_CHECK_GOTO(ret, cleanup);
3134 if (!(*element)->child) {
3135 /* save first */
3136 (*element)->child = new;
3137 } else {
3138 last->next = new;
3139 }
3140 last = new;
3141
3142 assert(ctx->xmlctx->status == LYXML_ELEM_CLOSE);
3143 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3144 }
3145 } else {
3146 /* save element content */
3147 if (ctx->xmlctx->value_len) {
3148 INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic, (*element)->arg);
3149 LY_CHECK_ERR_GOTO(!(*element)->arg, ret = LY_EMEM, cleanup);
Michal Vaskofc2cd072021-02-24 13:17:17 +01003150
3151 /* store prefix data for the argument as well */
Radek Krejci8df109d2021-04-23 12:19:08 +02003152 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 +01003153 &ctx->xmlctx->ns, &(*element)->format, &(*element)->prefix_data), cleanup);
Michal Vaskoafac7822020-10-20 14:22:26 +02003154 }
3155
3156 /* read closing tag */
3157 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3158 }
3159
3160cleanup:
3161 return ret;
3162}
3163
3164/**
3165 * @brief Parse instance of extension.
3166 *
3167 * @param[in,out] ctx Yin parser context for logging and to store current state.
Radek Krejcifc596f92021-02-26 22:40:26 +01003168 * @param[in] subelem The statement this extension instance is a subelement of.
Michal Vaskoafac7822020-10-20 14:22:26 +02003169 * @param[in] subelem_index Index of the keyword instance this extension instance is a subelement of
3170 * @param[in,out] exts Extension instance to add to.
3171 *
3172 * @return LY_ERR values.
3173 */
3174LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +01003175yin_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 +02003176 struct lysp_ext_instance **exts)
3177{
3178 struct lysp_ext_instance *e;
3179 struct lysp_stmt *last_subelem = NULL, *new_subelem = NULL;
Michal Vaskofc2cd072021-02-24 13:17:17 +01003180 char *ext_name;
Michal Vaskoafac7822020-10-20 14:22:26 +02003181
3182 assert(ctx->xmlctx->status == LYXML_ELEMENT);
Radek Krejci3aac9a72020-12-01 12:24:26 +01003183 assert(exts);
Michal Vaskoafac7822020-10-20 14:22:26 +02003184
3185 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *exts, e, LY_EMEM);
3186
Michal Vaskofc2cd072021-02-24 13:17:17 +01003187 if (!ctx->xmlctx->prefix_len) {
3188 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Extension instance \"%*.s\" without the mandatory prefix.",
3189 (int)ctx->xmlctx->name_len, ctx->xmlctx->name);
3190 return LY_EVALID;
3191 }
3192
3193 /* store prefixed name */
3194 if (asprintf(&ext_name, "%.*s:%.*s", (int)ctx->xmlctx->prefix_len, ctx->xmlctx->prefix, (int)ctx->xmlctx->name_len,
3195 ctx->xmlctx->name) == -1) {
3196 LOGMEM(ctx->xmlctx->ctx);
3197 return LY_EMEM;
3198 }
3199 LY_CHECK_RET(lydict_insert_zc(ctx->xmlctx->ctx, ext_name, &e->name));
3200
3201 /* store prefix data for the name */
Radek Krejci8df109d2021-04-23 12:19:08 +02003202 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 +01003203 &e->format, &e->prefix_data));
3204
Radek Krejciab430862021-03-02 20:13:40 +01003205 e->parent_stmt = subelem;
3206 e->parent_stmt_index = subelem_index;
Michal Vaskofc2cd072021-02-24 13:17:17 +01003207
Michal Vaskoafac7822020-10-20 14:22:26 +02003208 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3209
3210 /* store attributes as subelements */
3211 while (ctx->xmlctx->status == LYXML_ATTRIBUTE) {
3212 if (!ctx->xmlctx->prefix) {
3213 new_subelem = calloc(1, sizeof(*new_subelem));
3214 if (!e->child) {
3215 e->child = new_subelem;
3216 } else {
3217 last_subelem->next = new_subelem;
3218 }
3219 last_subelem = new_subelem;
3220
3221 last_subelem->flags |= LYS_YIN_ATTR;
3222 LY_CHECK_RET(lydict_insert(ctx->xmlctx->ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, &last_subelem->stmt));
3223 LY_CHECK_RET(!last_subelem->stmt, LY_EMEM);
3224 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3225
Michal Vaskofc2cd072021-02-24 13:17:17 +01003226 INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic,
3227 last_subelem->arg);
Michal Vaskoafac7822020-10-20 14:22:26 +02003228 LY_CHECK_RET(!last_subelem->arg, LY_EMEM);
3229 } else {
3230 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3231 }
3232
3233 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3234 }
3235
3236 /* parse subelements */
3237 assert(ctx->xmlctx->status == LYXML_ELEM_CONTENT);
3238 if (ctx->xmlctx->ws_only) {
3239 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3240 while (ctx->xmlctx->status == LYXML_ELEMENT) {
3241 LY_CHECK_RET(yin_parse_element_generic(ctx, LY_STMT_EXTENSION_INSTANCE, &new_subelem));
3242 if (!e->child) {
3243 e->child = new_subelem;
3244 } else {
3245 last_subelem->next = new_subelem;
3246 }
3247 last_subelem = new_subelem;
3248
3249 assert(ctx->xmlctx->status == LYXML_ELEM_CLOSE);
3250 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3251 }
3252 } else if (ctx->xmlctx->value_len) {
3253 /* save text content */
3254 INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic, e->argument);
3255 LY_CHECK_RET(!e->argument, LY_EMEM);
3256
Michal Vaskofc2cd072021-02-24 13:17:17 +01003257 /* store prefix data for the argument as well */
Radek Krejci8df109d2021-04-23 12:19:08 +02003258 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 +01003259 &ctx->xmlctx->ns, &e->format, &e->prefix_data));
Michal Vaskofc2cd072021-02-24 13:17:17 +01003260
Michal Vaskoafac7822020-10-20 14:22:26 +02003261 /* parser next */
3262 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3263 }
3264
aPiecek60d9d672021-04-27 15:49:57 +02003265 e->parsed = NULL;
3266
Michal Vaskoafac7822020-10-20 14:22:26 +02003267 return LY_SUCCESS;
3268}
3269
3270/**
3271 * @brief Generic function for content parsing
3272 *
3273 * @param[in,out] ctx Yin parser context for logging and to store current state.
3274 * @param[in] subelem_info array of valid subelement types and meta information
3275 * @param[in] subelem_info_size Size of subelem_info array.
3276 * @param[in] current_element Type of current element.
3277 * @param[out] text_content Where the text content of element should be stored if any. Text content is ignored if set to NULL.
3278 * @param[in,out] exts Extension instance to add to. Can be set to null if element cannot have extension as subelements.
3279 *
3280 * @return LY_ERR values.
3281 */
David Sedlákd6e56892019-07-01 15:40:24 +02003282LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01003283yin_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 +02003284 enum ly_stmt current_element, const char **text_content, struct lysp_ext_instance **exts)
David Sedlákd6e56892019-07-01 15:40:24 +02003285{
3286 LY_ERR ret = LY_SUCCESS;
Michal Vaskob36053d2020-03-26 15:49:30 +01003287 enum LYXML_PARSER_STATUS next_status;
Radek Krejcid6b76452019-09-03 17:03:03 +02003288 enum ly_stmt kw = LY_STMT_NONE, last_kw = LY_STMT_NONE;
David Sedlák1af868e2019-07-17 17:03:14 +02003289 struct yin_subelement *subelem = NULL;
David Sedlák09e18c92019-07-18 11:17:11 +02003290
Michal Vaskob36053d2020-03-26 15:49:30 +01003291 assert(ctx->xmlctx->status == LYXML_ELEM_CONTENT);
David Sedlákd6e56892019-07-01 15:40:24 +02003292
Michal Vaskob36053d2020-03-26 15:49:30 +01003293 if (ctx->xmlctx->ws_only) {
3294 /* check whether there are any children */
3295 LY_CHECK_GOTO(ret = lyxml_ctx_peek(ctx->xmlctx, &next_status), cleanup);
3296 } else {
3297 /* we want to parse the value */
3298 next_status = LYXML_ELEM_CLOSE;
3299 }
3300
3301 if (next_status == LYXML_ELEMENT) {
3302 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3303
3304 /* current element has subelements as content */
3305 while (ctx->xmlctx->status == LYXML_ELEMENT) {
3306 /* match keyword */
3307 last_kw = kw;
3308 kw = yin_match_keyword(ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, ctx->xmlctx->prefix,
Michal Vasko69730152020-10-09 16:30:07 +02003309 ctx->xmlctx->prefix_len, current_element);
Michal Vaskob36053d2020-03-26 15:49:30 +01003310
3311 /* check if this element can be child of current element */
3312 subelem = get_record(kw, subelem_info_size, subelem_info);
3313 if (!subelem) {
Michal Vasko69730152020-10-09 16:30:07 +02003314 if ((current_element == LY_STMT_DEVIATE) && isdevsub(kw)) {
Michal Vaskob36053d2020-03-26 15:49:30 +01003315 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INDEV_YIN, ly_stmt2str(kw));
3316 } else {
3317 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_UNEXP_SUBELEM, ctx->xmlctx->name_len,
Michal Vasko69730152020-10-09 16:30:07 +02003318 ctx->xmlctx->name, ly_stmt2str(current_element));
Michal Vaskob36053d2020-03-26 15:49:30 +01003319 }
3320 ret = LY_EVALID;
3321 goto cleanup;
3322 }
3323
3324 /* relative order is required only in module and submodule sub-elements */
Michal Vasko69730152020-10-09 16:30:07 +02003325 if ((current_element == LY_STMT_MODULE) || (current_element == LY_STMT_SUBMODULE)) {
Michal Vaskob36053d2020-03-26 15:49:30 +01003326 ret = yin_check_relative_order(ctx, last_kw, kw, current_element);
3327 LY_CHECK_GOTO(ret, cleanup);
3328 }
3329
3330 /* flag check */
3331 if ((subelem->flags & YIN_SUBELEM_UNIQUE) && (subelem->flags & YIN_SUBELEM_PARSED)) {
3332 /* subelement uniquenes */
3333 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_SUBELEM_REDEF, ly_stmt2str(kw), ly_stmt2str(current_element));
3334 return LY_EVALID;
3335 }
3336 if (subelem->flags & YIN_SUBELEM_FIRST) {
3337 /* subelement is supposed to be defined as first subelement */
3338 ret = yin_check_subelem_first_constraint(ctx, subelem_info, subelem_info_size, current_element, subelem);
3339 LY_CHECK_GOTO(ret, cleanup);
3340 }
3341 if (subelem->flags & YIN_SUBELEM_VER2) {
3342 /* subelement is supported only in version 1.1 or higher */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003343 if (ctx->parsed_mod->version < LYS_VERSION_1_1) {
Michal Vaskob36053d2020-03-26 15:49:30 +01003344 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 +02003345 ret = LY_EVALID;
3346 goto cleanup;
3347 }
David Sedlákd6e56892019-07-01 15:40:24 +02003348 }
Michal Vaskob36053d2020-03-26 15:49:30 +01003349 /* note that element was parsed for easy uniqueness check in next iterations */
3350 subelem->flags |= YIN_SUBELEM_PARSED;
3351
3352 switch (kw) {
3353 /* call responsible function */
3354 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003355 ret = yin_parse_extension_instance(ctx, current_element,
Michal Vasko69730152020-10-09 16:30:07 +02003356 (subelem->dest) ? *((LY_ARRAY_COUNT_TYPE *)subelem->dest) : 0, exts);
Michal Vaskob36053d2020-03-26 15:49:30 +01003357 break;
3358 case LY_STMT_ACTION:
3359 case LY_STMT_RPC:
3360 ret = yin_parse_action(ctx, (struct tree_node_meta *)subelem->dest);
3361 break;
3362 case LY_STMT_ANYDATA:
3363 case LY_STMT_ANYXML:
3364 ret = yin_parse_any(ctx, kw, (struct tree_node_meta *)subelem->dest);
3365 break;
3366 case LY_STMT_ARGUMENT:
3367 ret = yin_parse_argument(ctx, (struct yin_argument_meta *)subelem->dest, exts);
3368 break;
3369 case LY_STMT_AUGMENT:
3370 ret = yin_parse_augment(ctx, (struct tree_node_meta *)subelem->dest);
3371 break;
3372 case LY_STMT_BASE:
3373 ret = yin_parse_base(ctx, current_element, subelem->dest, exts);
3374 break;
3375 case LY_STMT_BELONGS_TO:
3376 ret = yin_parse_belongs_to(ctx, (struct lysp_submodule *)subelem->dest, exts);
3377 break;
3378 case LY_STMT_BIT:
3379 ret = yin_parse_bit(ctx, (struct lysp_type *)subelem->dest);
3380 break;
3381 case LY_STMT_CASE:
3382 ret = yin_parse_case(ctx, (struct tree_node_meta *)subelem->dest);
3383 break;
3384 case LY_STMT_CHOICE:
3385 ret = yin_parse_choice(ctx, (struct tree_node_meta *)subelem->dest);
3386 break;
3387 case LY_STMT_CONFIG:
3388 ret = yin_parse_config(ctx, (uint16_t *)subelem->dest, exts);
3389 break;
3390 case LY_STMT_CONTACT:
3391 case LY_STMT_DESCRIPTION:
3392 case LY_STMT_ORGANIZATION:
3393 case LY_STMT_REFERENCE:
3394 ret = yin_parse_meta(ctx, kw, (const char **)subelem->dest, exts);
3395 break;
3396 case LY_STMT_CONTAINER:
3397 ret = yin_parse_container(ctx, (struct tree_node_meta *)subelem->dest);
3398 break;
3399 case LY_STMT_DEFAULT:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003400 ret = yin_parse_qname(ctx, kw, subelem, exts);
3401 break;
Michal Vaskob36053d2020-03-26 15:49:30 +01003402 case LY_STMT_ERROR_APP_TAG:
3403 case LY_STMT_KEY:
3404 case LY_STMT_PRESENCE:
3405 ret = yin_parse_simple_elem(ctx, kw, subelem, YIN_ARG_VALUE, Y_STR_ARG, exts);
3406 break;
3407 case LY_STMT_DEVIATE:
3408 ret = yin_parse_deviate(ctx, (struct lysp_deviate **)subelem->dest);
3409 break;
3410 case LY_STMT_DEVIATION:
3411 ret = yin_parse_deviation(ctx, (struct lysp_deviation **)subelem->dest);
3412 break;
3413 case LY_STMT_ENUM:
3414 ret = yin_parse_enum(ctx, (struct lysp_type *)subelem->dest);
3415 break;
3416 case LY_STMT_ERROR_MESSAGE:
3417 ret = yin_parse_err_msg(ctx, (const char **)subelem->dest, exts);
3418 break;
3419 case LY_STMT_EXTENSION:
3420 ret = yin_parse_extension(ctx, (struct lysp_ext **)subelem->dest);
3421 break;
3422 case LY_STMT_FEATURE:
3423 ret = yin_parse_feature(ctx, (struct lysp_feature **)subelem->dest);
3424 break;
3425 case LY_STMT_FRACTION_DIGITS:
3426 ret = yin_parse_fracdigits(ctx, (struct lysp_type *)subelem->dest);
3427 break;
3428 case LY_STMT_GROUPING:
3429 ret = yin_parse_grouping(ctx, (struct tree_node_meta *)subelem->dest);
3430 break;
3431 case LY_STMT_IDENTITY:
3432 ret = yin_parse_identity(ctx, (struct lysp_ident **)subelem->dest);
3433 break;
Radek Išabe8a6532021-03-29 08:54:22 +02003434 case LY_STMT_UNIQUE:
Michal Vaskob36053d2020-03-26 15:49:30 +01003435 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003436 ret = yin_parse_qname(ctx, kw, subelem, exts);
3437 break;
Michal Vaskob36053d2020-03-26 15:49:30 +01003438 case LY_STMT_UNITS:
3439 ret = yin_parse_simple_elem(ctx, kw, subelem, YIN_ARG_NAME, Y_STR_ARG, exts);
3440 break;
3441 case LY_STMT_IMPORT:
3442 ret = yin_parse_import(ctx, (struct import_meta *)subelem->dest);
3443 break;
3444 case LY_STMT_INCLUDE:
Radek Krejcieeee95c2021-01-19 10:57:22 +01003445 if ((current_element == LY_STMT_SUBMODULE) && (ctx->parsed_mod->version == LYS_VERSION_1_1)) {
3446 LOGWRN(PARSER_CTX(ctx), "YANG version 1.1 expects all includes in main module, includes in submodules (%s) are not necessary.",
3447 ((struct lysp_submodule *)(ctx->parsed_mod))->name);
3448 }
Michal Vaskob36053d2020-03-26 15:49:30 +01003449 ret = yin_parse_include(ctx, (struct include_meta *)subelem->dest);
3450 break;
3451 case LY_STMT_INPUT:
3452 case LY_STMT_OUTPUT:
3453 ret = yin_parse_inout(ctx, kw, (struct inout_meta *)subelem->dest);
3454 break;
3455 case LY_STMT_LEAF:
3456 ret = yin_parse_leaf(ctx, (struct tree_node_meta *)subelem->dest);
3457 break;
3458 case LY_STMT_LEAF_LIST:
3459 ret = yin_parse_leaflist(ctx, (struct tree_node_meta *)subelem->dest);
3460 break;
3461 case LY_STMT_LENGTH:
3462 ret = yin_parse_length(ctx, (struct lysp_type *)subelem->dest);
3463 break;
3464 case LY_STMT_LIST:
3465 ret = yin_parse_list(ctx, (struct tree_node_meta *)subelem->dest);
3466 break;
3467 case LY_STMT_MANDATORY:
3468 ret = yin_parse_mandatory(ctx, (uint16_t *)subelem->dest, exts);
3469 break;
3470 case LY_STMT_MAX_ELEMENTS:
3471 case LY_STMT_MIN_ELEMENTS:
3472 ret = yin_parse_minmax(ctx, current_element, kw, subelem->dest);
3473 break;
3474 case LY_STMT_MODIFIER:
3475 ret = yin_parse_modifier(ctx, (const char **)subelem->dest, exts);
3476 break;
3477 case LY_STMT_MUST:
3478 ret = yin_parse_must(ctx, (struct lysp_restr **)subelem->dest);
3479 break;
3480 case LY_STMT_NAMESPACE:
3481 ret = yin_parse_simple_elem(ctx, kw, subelem, YIN_ARG_URI, Y_STR_ARG, exts);
3482 break;
3483 case LY_STMT_NOTIFICATION:
3484 ret = yin_parse_notification(ctx, (struct tree_node_meta *)subelem->dest);
3485 break;
3486 case LY_STMT_ORDERED_BY:
3487 ret = yin_parse_orderedby(ctx, (uint16_t *)subelem->dest, exts);
3488 break;
3489 case LY_STMT_PATH:
3490 ret = yin_parse_path(ctx, kw, (struct lysp_type *)subelem->dest);
3491 break;
3492 case LY_STMT_PATTERN:
3493 ret = yin_parse_pattern(ctx, (struct lysp_type *)subelem->dest);
3494 break;
3495 case LY_STMT_VALUE:
3496 case LY_STMT_POSITION:
3497 ret = yin_parse_value_pos(ctx, kw, (struct lysp_type_enum *)subelem->dest);
3498 break;
3499 case LY_STMT_PREFIX:
3500 ret = yin_parse_simple_elem(ctx, kw, subelem, YIN_ARG_VALUE, Y_IDENTIF_ARG, exts);
3501 break;
3502 case LY_STMT_RANGE:
3503 ret = yin_parse_range(ctx, (struct lysp_type *)subelem->dest);
3504 break;
3505 case LY_STMT_REFINE:
3506 ret = yin_parse_refine(ctx, (struct lysp_refine **)subelem->dest);
3507 break;
3508 case LY_STMT_REQUIRE_INSTANCE:
3509 ret = yin_pasrse_reqinstance(ctx, (struct lysp_type *)subelem->dest);
3510 break;
3511 case LY_STMT_REVISION:
3512 ret = yin_parse_revision(ctx, (struct lysp_revision **)subelem->dest);
3513 break;
3514 case LY_STMT_REVISION_DATE:
3515 ret = yin_parse_revision_date(ctx, (char *)subelem->dest, exts);
3516 break;
3517 case LY_STMT_STATUS:
3518 ret = yin_parse_status(ctx, (uint16_t *)subelem->dest, exts);
3519 break;
3520 case LY_STMT_TYPE:
3521 ret = yin_parse_type(ctx, current_element, subelem);
3522 break;
3523 case LY_STMT_TYPEDEF:
3524 ret = yin_parse_typedef(ctx, (struct tree_node_meta *)subelem->dest);
3525 break;
Michal Vaskob36053d2020-03-26 15:49:30 +01003526 case LY_STMT_USES:
3527 ret = yin_parse_uses(ctx, (struct tree_node_meta *)subelem->dest);
3528 break;
3529 case LY_STMT_WHEN:
3530 ret = yin_parse_when(ctx, (struct lysp_when **)subelem->dest);
3531 break;
3532 case LY_STMT_YANG_VERSION:
3533 ret = yin_parse_yangversion(ctx, (uint8_t *)subelem->dest, exts);
3534 break;
3535 case LY_STMT_YIN_ELEMENT:
3536 ret = yin_parse_yin_element(ctx, (uint16_t *)subelem->dest, exts);
3537 break;
3538 case LY_STMT_ARG_TEXT:
3539 case LY_STMT_ARG_VALUE:
3540 /* TODO what to do with content/attributes? */
3541 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3542 while (ctx->xmlctx->status == LYXML_ATTRIBUTE) {
3543 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3544 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3545 }
3546 ret = yin_parse_content(ctx, NULL, 0, kw, (const char **)subelem->dest, NULL);
3547 break;
3548 default:
3549 LOGINT(ctx->xmlctx->ctx);
3550 ret = LY_EINT;
David Sedlák3ffbc522019-07-02 17:49:28 +02003551 }
Michal Vaskob36053d2020-03-26 15:49:30 +01003552 LY_CHECK_GOTO(ret, cleanup);
3553 subelem = NULL;
3554
3555 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
David Sedlákd6e56892019-07-01 15:40:24 +02003556 }
Michal Vaskob36053d2020-03-26 15:49:30 +01003557 } else {
3558 LY_CHECK_RET(ret);
3559 /* elements with text or none content */
3560 /* save text content, if text_content isn't set, it's just ignored */
3561 /* no resources are allocated in this branch, no need to use cleanup label */
3562 LY_CHECK_RET(yin_validate_value(ctx, Y_STR_ARG));
3563 if (text_content) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02003564 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 +01003565 LY_CHECK_RET(!*text_content, LY_EMEM);
3566 }
3567
3568 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
David Sedlákd6e56892019-07-01 15:40:24 +02003569 }
Michal Vaskob36053d2020-03-26 15:49:30 +01003570
David Sedlák8b754462019-07-25 16:22:13 +02003571 /* mandatory subelemnts are checked only after whole element was succesfully parsed */
3572 LY_CHECK_RET(yin_check_subelem_mandatory_constraint(ctx, subelem_info, subelem_info_size, current_element));
David Sedlákd6e56892019-07-01 15:40:24 +02003573
3574cleanup:
David Sedlákd6e56892019-07-01 15:40:24 +02003575 return ret;
3576}
3577
David Sedlákaa98bba2019-09-12 11:52:14 +02003578/**
Michal Vaskoafac7822020-10-20 14:22:26 +02003579 * @brief Parse module element.
David Sedlákaa98bba2019-09-12 11:52:14 +02003580 *
3581 * @param[in,out] ctx Yin parser context for logging and to store current state.
Michal Vaskoafac7822020-10-20 14:22:26 +02003582 * @param[out] mod Parsed module structure.
David Sedlákaa98bba2019-09-12 11:52:14 +02003583 *
3584 * @return LY_ERR values.
3585 */
David Sedlák1e2cdd02019-06-27 14:17:43 +02003586LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01003587yin_parse_mod(struct lys_yin_parser_ctx *ctx, struct lysp_module *mod)
David Sedlák3b4db242018-10-19 16:11:01 +02003588{
David Sedlák81497a32019-08-13 16:56:26 +02003589 LY_ERR ret = LY_SUCCESS;
3590 struct yin_subelement *subelems = NULL;
Michal Vasko8dc31992021-02-22 10:30:47 +01003591 const struct lysp_submodule *dup;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003592 size_t subelems_size;
David Sedlák3b4db242018-10-19 16:11:01 +02003593
Michal Vaskoc3781c32020-10-06 14:04:08 +02003594 mod->is_submod = 0;
3595
Michal Vaskob36053d2020-03-26 15:49:30 +01003596 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3597 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 +01003598 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 28, NULL, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02003599 LY_STMT_ANYDATA, &mod->data, YIN_SUBELEM_VER2,
3600 LY_STMT_ANYXML, &mod->data, 0,
3601 LY_STMT_AUGMENT, &mod->augments, 0,
3602 LY_STMT_CHOICE, &mod->data, 0,
3603 LY_STMT_CONTACT, &mod->mod->contact, YIN_SUBELEM_UNIQUE,
3604 LY_STMT_CONTAINER, &mod->data, 0,
3605 LY_STMT_DESCRIPTION, &mod->mod->dsc, YIN_SUBELEM_UNIQUE,
3606 LY_STMT_DEVIATION, &mod->deviations, 0,
3607 LY_STMT_EXTENSION, &mod->extensions, 0,
3608 LY_STMT_FEATURE, &mod->features, 0,
3609 LY_STMT_GROUPING, &mod->groupings, 0,
3610 LY_STMT_IDENTITY, &mod->identities, 0,
3611 LY_STMT_IMPORT, mod->mod->prefix, &mod->imports, 0,
3612 LY_STMT_INCLUDE, mod->mod->name, &mod->includes, 0,
3613 LY_STMT_LEAF, &mod->data, 0,
3614 LY_STMT_LEAF_LIST, &mod->data, 0,
3615 LY_STMT_LIST, &mod->data, 0,
3616 LY_STMT_NAMESPACE, &mod->mod->ns, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE,
3617 LY_STMT_NOTIFICATION, &mod->notifs, 0,
3618 LY_STMT_ORGANIZATION, &mod->mod->org, YIN_SUBELEM_UNIQUE,
3619 LY_STMT_PREFIX, &mod->mod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE,
3620 LY_STMT_REFERENCE, &mod->mod->ref, YIN_SUBELEM_UNIQUE,
3621 LY_STMT_REVISION, &mod->revs, 0,
3622 LY_STMT_RPC, &mod->rpcs, 0,
3623 LY_STMT_TYPEDEF, &mod->typedefs, 0,
3624 LY_STMT_USES, &mod->data, 0,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003625 LY_STMT_YANG_VERSION, &mod->version, YIN_SUBELEM_UNIQUE,
Michal Vasko69730152020-10-09 16:30:07 +02003626 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
David Sedlák81497a32019-08-13 16:56:26 +02003627
Radek Krejcif13b87b2020-12-01 22:02:17 +01003628 ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_MODULE, NULL, &mod->exts);
3629 subelems_deallocator(subelems_size, subelems);
David Sedláked809f12019-09-13 10:15:11 +02003630 LY_CHECK_RET(ret);
David Sedlák81497a32019-08-13 16:56:26 +02003631
David Sedláked809f12019-09-13 10:15:11 +02003632 /* submodules share the namespace with the module names, so there must not be
3633 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01003634 dup = ly_ctx_get_submodule_latest(ctx->xmlctx->ctx, mod->mod->name);
David Sedláked809f12019-09-13 10:15:11 +02003635 if (dup) {
Radek Krejci854e1552020-12-21 15:05:23 +01003636 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_NAME2_COL, "module", "submodule", mod->mod->name);
David Sedláked809f12019-09-13 10:15:11 +02003637 return LY_EVALID;
3638 }
3639
3640 return LY_SUCCESS;
David Sedlák3b4db242018-10-19 16:11:01 +02003641}
3642
Michal Vaskoafac7822020-10-20 14:22:26 +02003643/**
3644 * @brief Parse submodule element.
3645 *
3646 * @param[in,out] ctx Yin parser context for logging and to store current state.
3647 * @param[in] mod_attrs Attributes of submodule element.
3648 * @param[out] submod Parsed submodule structure.
3649 *
3650 * @return LY_ERR values.
3651 */
David Sedlák3b4db242018-10-19 16:11:01 +02003652LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01003653yin_parse_submod(struct lys_yin_parser_ctx *ctx, struct lysp_submodule *submod)
David Sedlák298ff6d2019-07-26 14:29:03 +02003654{
David Sedlák81497a32019-08-13 16:56:26 +02003655 LY_ERR ret = LY_SUCCESS;
3656 struct yin_subelement *subelems = NULL;
Michal Vasko8dc31992021-02-22 10:30:47 +01003657 const struct lysp_submodule *dup;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003658 size_t subelems_size;
David Sedlák298ff6d2019-07-26 14:29:03 +02003659
Michal Vaskoc3781c32020-10-06 14:04:08 +02003660 submod->is_submod = 1;
3661
Michal Vaskob36053d2020-03-26 15:49:30 +01003662 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3663 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 +01003664 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 27, NULL, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02003665 LY_STMT_ANYDATA, &submod->data, YIN_SUBELEM_VER2,
3666 LY_STMT_ANYXML, &submod->data, 0,
3667 LY_STMT_AUGMENT, &submod->augments, 0,
3668 LY_STMT_BELONGS_TO, submod, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE,
3669 LY_STMT_CHOICE, &submod->data, 0,
3670 LY_STMT_CONTACT, &submod->contact, YIN_SUBELEM_UNIQUE,
3671 LY_STMT_CONTAINER, &submod->data, 0,
3672 LY_STMT_DESCRIPTION, &submod->dsc, YIN_SUBELEM_UNIQUE,
3673 LY_STMT_DEVIATION, &submod->deviations, 0,
3674 LY_STMT_EXTENSION, &submod->extensions, 0,
3675 LY_STMT_FEATURE, &submod->features, 0,
3676 LY_STMT_GROUPING, &submod->groupings, 0,
3677 LY_STMT_IDENTITY, &submod->identities, 0,
3678 LY_STMT_IMPORT, submod->prefix, &submod->imports, 0,
3679 LY_STMT_INCLUDE, submod->name, &submod->includes, 0,
3680 LY_STMT_LEAF, &submod->data, 0,
3681 LY_STMT_LEAF_LIST, &submod->data, 0,
3682 LY_STMT_LIST, &submod->data, 0,
3683 LY_STMT_NOTIFICATION, &submod->notifs, 0,
3684 LY_STMT_ORGANIZATION, &submod->org, YIN_SUBELEM_UNIQUE,
3685 LY_STMT_REFERENCE, &submod->ref, YIN_SUBELEM_UNIQUE,
3686 LY_STMT_REVISION, &submod->revs, 0,
3687 LY_STMT_RPC, &submod->rpcs, 0,
3688 LY_STMT_TYPEDEF, &submod->typedefs, 0,
3689 LY_STMT_USES, &submod->data, 0,
3690 LY_STMT_YANG_VERSION, &submod->version, YIN_SUBELEM_UNIQUE,
3691 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
David Sedlák81497a32019-08-13 16:56:26 +02003692
Radek Krejcif13b87b2020-12-01 22:02:17 +01003693 ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_SUBMODULE, NULL, &submod->exts);
3694 subelems_deallocator(subelems_size, subelems);
David Sedláked809f12019-09-13 10:15:11 +02003695 LY_CHECK_RET(ret);
David Sedlák81497a32019-08-13 16:56:26 +02003696
David Sedláked809f12019-09-13 10:15:11 +02003697 /* submodules share the namespace with the module names, so there must not be
3698 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01003699 dup = ly_ctx_get_submodule_latest(ctx->xmlctx->ctx, submod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02003700 if (dup && strcmp(dup->mod->name, submod->mod->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01003701 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_NAME_COL, "submodules", dup->name);
David Sedláked809f12019-09-13 10:15:11 +02003702 return LY_EVALID;
3703 }
3704
3705 return LY_SUCCESS;
David Sedlák298ff6d2019-07-26 14:29:03 +02003706}
3707
3708LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003709yin_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 +02003710 struct ly_in *in, struct lysp_submodule **submod)
David Sedlák8985a142019-07-31 16:43:06 +02003711{
Radek Krejcid6b76452019-09-03 17:03:03 +02003712 enum ly_stmt kw = LY_STMT_NONE;
David Sedlák8985a142019-07-31 16:43:06 +02003713 LY_ERR ret = LY_SUCCESS;
David Sedlák8985a142019-07-31 16:43:06 +02003714 struct lysp_submodule *mod_p = NULL;
3715
aPiecek56be92a2021-07-01 07:18:10 +02003716 assert(yin_ctx && ctx && main_ctx && in && submod);
3717
David Sedlák8985a142019-07-31 16:43:06 +02003718 /* create context */
3719 *yin_ctx = calloc(1, sizeof **yin_ctx);
David Sedlák1b623122019-08-05 15:27:49 +02003720 LY_CHECK_ERR_RET(!(*yin_ctx), LOGMEM(ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01003721 (*yin_ctx)->format = LYS_IN_YIN;
aPiecek56be92a2021-07-01 07:18:10 +02003722 (*yin_ctx)->main_ctx = main_ctx;
Michal Vasko63f3d842020-07-08 10:10:14 +02003723 LY_CHECK_RET(lyxml_ctx_new(ctx, in, &(*yin_ctx)->xmlctx));
David Sedlák8985a142019-07-31 16:43:06 +02003724
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003725 mod_p = calloc(1, sizeof *mod_p);
3726 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3727 mod_p->mod = main_ctx->parsed_mod->mod;
3728 mod_p->parsing = 1;
3729 (*yin_ctx)->parsed_mod = (struct lysp_module *)mod_p;
3730
David Sedlák8985a142019-07-31 16:43:06 +02003731 /* check submodule */
Michal Vaskob36053d2020-03-26 15:49:30 +01003732 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 +02003733 (*yin_ctx)->xmlctx->prefix_len, LY_STMT_NONE);
Radek Krejcid6b76452019-09-03 17:03:03 +02003734 if (kw == LY_STMT_MODULE) {
David Sedlák8985a142019-07-31 16:43:06 +02003735 LOGERR(ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
3736 ret = LY_EINVAL;
3737 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02003738 } else if (kw != LY_STMT_SUBMODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02003739 LOGVAL_PARSER((struct lys_parser_ctx *)*yin_ctx, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
David Sedlák8985a142019-07-31 16:43:06 +02003740 ret = LY_EVALID;
3741 goto cleanup;
3742 }
3743
Michal Vaskob36053d2020-03-26 15:49:30 +01003744 ret = yin_parse_submod(*yin_ctx, mod_p);
David Sedlák8985a142019-07-31 16:43:06 +02003745 LY_CHECK_GOTO(ret, cleanup);
3746
David Sedlákbf8a2b72019-08-14 16:48:10 +02003747 /* skip possible trailing whitespaces at end of the input */
Michal Vasko63f3d842020-07-08 10:10:14 +02003748 while (isspace(in->current[0])) {
Radek Krejcidd713ce2021-01-04 23:12:12 +01003749 if (in->current[0] == '\n') {
3750 LY_IN_NEW_LINE(in);
3751 }
Michal Vasko63f3d842020-07-08 10:10:14 +02003752 ly_in_skip(in, 1);
David Sedlák6d781b62019-08-02 15:22:52 +02003753 }
Michal Vasko63f3d842020-07-08 10:10:14 +02003754 if (in->current[0]) {
3755 LOGVAL_PARSER((struct lys_parser_ctx *)*yin_ctx, LY_VCODE_TRAILING_SUBMOD, 15, in->current,
Michal Vasko69730152020-10-09 16:30:07 +02003756 strlen(in->current) > 15 ? "..." : "");
David Sedlák6d781b62019-08-02 15:22:52 +02003757 ret = LY_EVALID;
3758 goto cleanup;
3759 }
3760
David Sedlák8985a142019-07-31 16:43:06 +02003761 mod_p->parsing = 0;
3762 *submod = mod_p;
3763
3764cleanup:
3765 if (ret) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003766 lysp_module_free((struct lysp_module *)mod_p);
David Sedlák1b623122019-08-05 15:27:49 +02003767 yin_parser_ctx_free(*yin_ctx);
3768 *yin_ctx = NULL;
David Sedlák8985a142019-07-31 16:43:06 +02003769 }
David Sedlák8985a142019-07-31 16:43:06 +02003770 return ret;
3771}
3772
3773LY_ERR
aPiecekc3e26142021-06-22 14:25:49 +02003774yin_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 +02003775{
David Sedláke4889912018-11-02 09:52:40 +01003776 LY_ERR ret = LY_SUCCESS;
Radek Krejcid6b76452019-09-03 17:03:03 +02003777 enum ly_stmt kw = LY_STMT_NONE;
David Sedlák3017da42019-02-15 09:48:04 +01003778 struct lysp_module *mod_p = NULL;
David Sedlák3b4db242018-10-19 16:11:01 +02003779
David Sedlák8985a142019-07-31 16:43:06 +02003780 /* create context */
3781 *yin_ctx = calloc(1, sizeof **yin_ctx);
David Sedlák1b623122019-08-05 15:27:49 +02003782 LY_CHECK_ERR_RET(!(*yin_ctx), LOGMEM(mod->ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01003783 (*yin_ctx)->format = LYS_IN_YIN;
Michal Vasko63f3d842020-07-08 10:10:14 +02003784 LY_CHECK_RET(lyxml_ctx_new(mod->ctx, in, &(*yin_ctx)->xmlctx));
aPiecek8d4e75d2021-06-24 14:47:06 +02003785 (*yin_ctx)->main_ctx = (struct lys_parser_ctx *)(*yin_ctx);
David Sedlákda8ffa32019-07-08 14:17:10 +02003786
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003787 mod_p = calloc(1, sizeof *mod_p);
3788 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(mod->ctx), cleanup);
3789 mod_p->mod = mod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003790 (*yin_ctx)->parsed_mod = mod_p;
3791
David Sedlák8985a142019-07-31 16:43:06 +02003792 /* check module */
Michal Vaskob36053d2020-03-26 15:49:30 +01003793 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 +02003794 (*yin_ctx)->xmlctx->prefix_len, LY_STMT_NONE);
Radek Krejcid6b76452019-09-03 17:03:03 +02003795 if (kw == LY_STMT_SUBMODULE) {
David Sedlák8985a142019-07-31 16:43:06 +02003796 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 +01003797 ret = LY_EINVAL;
3798 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02003799 } else if (kw != LY_STMT_MODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02003800 LOGVAL_PARSER((struct lys_parser_ctx *)*yin_ctx, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
David Sedlák3017da42019-02-15 09:48:04 +01003801 ret = LY_EVALID;
3802 goto cleanup;
David Sedláke4889912018-11-02 09:52:40 +01003803 }
3804
David Sedlák00250342019-06-21 14:19:39 +02003805 /* parse module substatements */
Michal Vaskob36053d2020-03-26 15:49:30 +01003806 ret = yin_parse_mod(*yin_ctx, mod_p);
David Sedlák3017da42019-02-15 09:48:04 +01003807 LY_CHECK_GOTO(ret, cleanup);
David Sedlák2e411422018-12-17 02:35:39 +01003808
David Sedlákbf8a2b72019-08-14 16:48:10 +02003809 /* skip possible trailing whitespaces at end of the input */
Michal Vasko63f3d842020-07-08 10:10:14 +02003810 while (isspace(in->current[0])) {
Radek Krejcidd713ce2021-01-04 23:12:12 +01003811 if (in->current[0] == '\n') {
3812 LY_IN_NEW_LINE(in);
3813 }
Michal Vasko63f3d842020-07-08 10:10:14 +02003814 ly_in_skip(in, 1);
David Sedlák6d781b62019-08-02 15:22:52 +02003815 }
Michal Vasko63f3d842020-07-08 10:10:14 +02003816 if (in->current[0]) {
3817 LOGVAL_PARSER((struct lys_parser_ctx *)*yin_ctx, LY_VCODE_TRAILING_MOD, 15, in->current,
Michal Vasko69730152020-10-09 16:30:07 +02003818 strlen(in->current) > 15 ? "..." : "");
David Sedlák6d781b62019-08-02 15:22:52 +02003819 ret = LY_EVALID;
3820 goto cleanup;
3821 }
3822
David Sedlák3017da42019-02-15 09:48:04 +01003823 mod->parsed = mod_p;
3824
3825cleanup:
David Sedlák8f7a1172019-06-20 14:42:18 +02003826 if (ret != LY_SUCCESS) {
David Sedlák3017da42019-02-15 09:48:04 +01003827 lysp_module_free(mod_p);
David Sedlák1b623122019-08-05 15:27:49 +02003828 yin_parser_ctx_free(*yin_ctx);
3829 *yin_ctx = NULL;
David Sedlák3017da42019-02-15 09:48:04 +01003830 }
David Sedlák2e411422018-12-17 02:35:39 +01003831 return ret;
David Sedlák3b4db242018-10-19 16:11:01 +02003832}