blob: 73827965ca95832ef0ace20dd7be54efbe6c37b3 [file] [log] [blame]
David Sedlákf824ad52018-10-14 23:58:15 +02001/**
2 * @file parser_yin.c
3 * @author David Sedlák <xsedla1d@stud.fit.vutbr.cz>
Michal Vaskoc636ea42022-09-16 10:20:31 +02004 * @author Michal Vasko <mvasko@cesnet.cz>
David Sedlák3b4db242018-10-19 16:11:01 +02005 * @brief YIN parser.
6 *
Michal Vaskoc636ea42022-09-16 10:20:31 +02007 * Copyright (c) 2015 - 2022 CESNET, z.s.p.o.
David Sedlák3b4db242018-10-19 16:11:01 +02008 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
David Sedlákf824ad52018-10-14 23:58:15 +020014 */
Michal Vaskofc2cd072021-02-24 13:17:17 +010015#define _GNU_SOURCE
16
David Sedlák3ffbc522019-07-02 17:49:28 +020017#include <assert.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020018#include <ctype.h>
19#include <errno.h>
20#include <stdarg.h>
21#include <stdbool.h>
Radek Krejci47fab892020-11-05 17:02:41 +010022#include <stdint.h>
David Sedlák3b4db242018-10-19 16:11:01 +020023#include <stdio.h>
24#include <stdlib.h>
David Sedlák872c7b42018-10-26 13:15:20 +020025#include <string.h>
David Sedlákf824ad52018-10-14 23:58:15 +020026
Radek Krejci535ea9f2020-05-29 16:01:05 +020027#include "common.h"
Michal Vaskofc2cd072021-02-24 13:17:17 +010028#include "compat.h"
David Sedlákf824ad52018-10-14 23:58:15 +020029#include "context.h"
David Sedlákecf5eb82019-06-03 14:12:44 +020030#include "dict.h"
Radek Krejci47fab892020-11-05 17:02:41 +010031#include "in.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020032#include "in_internal.h"
Radek Krejci47fab892020-11-05 17:02:41 +010033#include "log.h"
Michal Vasko63f3d842020-07-08 10:10:14 +020034#include "parser_internal.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020035#include "parser_schema.h"
Michal Vasko69730152020-10-09 16:30:07 +020036#include "path.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020037#include "set.h"
David Sedlákecf5eb82019-06-03 14:12:44 +020038#include "tree.h"
Michal Vaskofc2cd072021-02-24 13:17:17 +010039#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010040#include "tree_edit.h"
David Sedlákecf5eb82019-06-03 14:12:44 +020041#include "tree_schema.h"
David Sedlák3b4db242018-10-19 16:11:01 +020042#include "tree_schema_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020043#include "xml.h"
David Sedlák00250342019-06-21 14:19:39 +020044
Radek Krejci77114102021-03-10 15:21:57 +010045struct lys_glob_unres;
46
David Sedlák2b214ac2019-06-06 16:11:03 +020047/**
48 * @brief check if given string is URI of yin namespace.
David Sedlák8985a142019-07-31 16:43:06 +020049 *
David Sedlák2b214ac2019-06-06 16:11:03 +020050 * @param ns Namespace URI to check.
51 *
52 * @return true if ns equals YIN_NS_URI false otherwise.
53 */
54#define IS_YIN_NS(ns) (strcmp(ns, YIN_NS_URI) == 0)
55
Michal Vaskoafac7822020-10-20 14:22:26 +020056enum yin_argument {
57 YIN_ARG_UNKNOWN = 0, /**< parsed argument can not be matched with any supported yin argument keyword */
58 YIN_ARG_NAME, /**< argument name */
59 YIN_ARG_TARGET_NODE, /**< argument target-node */
60 YIN_ARG_MODULE, /**< argument module */
61 YIN_ARG_VALUE, /**< argument value */
62 YIN_ARG_TEXT, /**< argument text */
63 YIN_ARG_CONDITION, /**< argument condition */
64 YIN_ARG_URI, /**< argument uri */
65 YIN_ARG_DATE, /**< argument data */
66 YIN_ARG_TAG, /**< argument tag */
67 YIN_ARG_NONE /**< empty (special value) */
68};
69
Michal Vasko22df3f02020-08-24 13:29:22 +020070const char * const yin_attr_list[] = {
David Sedlákf6251182019-06-06 10:22:13 +020071 [YIN_ARG_NAME] = "name",
72 [YIN_ARG_TARGET_NODE] = "target-node",
73 [YIN_ARG_MODULE] = "module",
74 [YIN_ARG_VALUE] = "value",
75 [YIN_ARG_TEXT] = "text",
76 [YIN_ARG_CONDITION] = "condition",
77 [YIN_ARG_URI] = "uri",
78 [YIN_ARG_DATE] = "date",
79 [YIN_ARG_TAG] = "tag",
David Sedlák071f7662019-09-12 02:02:51 +020080 [YIN_ARG_NONE] = "none",
David Sedlákf6251182019-06-06 10:22:13 +020081};
82
Michal Vaskoafac7822020-10-20 14:22:26 +020083#define yin_attr2str(STMT) yin_attr_list[STMT]
84
85#define VALID_VALS1 " Only valid value is \"%s\"."
86#define VALID_VALS2 " Valid values are \"%s\" and \"%s\"."
87#define VALID_VALS3 " Valid values are \"%s\", \"%s\" and \"%s\"."
88#define VALID_VALS4 " Valid values are \"%s\", \"%s\", \"%s\" and \"%s\"."
89
90/* shortcut to determin if keyword can in general be subelement of deviation regardles of it's type */
91#define isdevsub(kw) (kw == LY_STMT_CONFIG || kw == LY_STMT_DEFAULT || kw == LY_STMT_MANDATORY || \
92 kw == LY_STMT_MAX_ELEMENTS || kw == LY_STMT_MIN_ELEMENTS || \
93 kw == LY_STMT_MUST || kw == LY_STMT_TYPE || kw == LY_STMT_UNIQUE || \
94 kw == LY_STMT_UNITS || kw == LY_STMT_EXTENSION_INSTANCE)
95
96/* flags to set constraints of subelements */
97#define YIN_SUBELEM_MANDATORY 0x01 /**< is set when subelement is mandatory */
98#define YIN_SUBELEM_UNIQUE 0x02 /**< is set when subelement is unique */
99#define YIN_SUBELEM_FIRST 0x04 /**< is set when subelement is actually yang argument mapped to yin element */
100#define YIN_SUBELEM_VER2 0x08 /**< subelemnt is allowed only in modules with version at least 2 (YANG 1.1) */
101
102#define YIN_SUBELEM_PARSED 0x80 /**< is set during parsing when given subelement is encountered for the first
103 time to simply check validity of given constraints */
104
105struct yin_subelement {
106 enum ly_stmt type; /**< type of keyword */
Michal Vasko193dacd2022-10-13 08:43:05 +0200107 void *dest; /**< meta infromation passed to responsible function (mostly information about where parsed
108 subelement should be stored) */
109 uint16_t flags; /**< describes constraints of subelement can be set to YIN_SUBELEM_MANDATORY,
110 YIN_SUBELEM_UNIQUE, YIN_SUBELEM_FIRST, YIN_SUBELEM_VER2, and YIN_SUBELEM_DEFAULT_TEXT */
Michal Vaskoafac7822020-10-20 14:22:26 +0200111};
112
113/* Meta information passed to yin_parse_argument function,
114 holds information about where content of argument element will be stored. */
115struct yin_argument_meta {
116 uint16_t *flags; /**< Argument flags */
117 const char **argument; /**< Argument value */
118};
119
120/**
121 * @brief Meta information passed to functions working with tree_schema,
122 * that require additional information about parent node.
123 */
124struct tree_node_meta {
125 struct lysp_node *parent; /**< parent node */
126 struct lysp_node **nodes; /**< linked list of siblings */
127};
128
129/**
130 * @brief Meta information passed to yin_parse_import function.
131 */
132struct import_meta {
133 const char *prefix; /**< module prefix. */
134 struct lysp_import **imports; /**< imports to add to. */
135};
136
137/**
138 * @brief Meta information passed to yin_parse_include function.
139 */
140struct include_meta {
141 const char *name; /**< Module/submodule name. */
142 struct lysp_include **includes; /**< [Sized array](@ref sizedarrays) of parsed includes to add to. */
143};
144
145/**
146 * @brief Meta information passed to yin_parse_inout function.
147 */
148struct inout_meta {
149 struct lysp_node *parent; /**< Parent node. */
Radek Krejci2a9fc652021-01-22 17:44:34 +0100150 struct lysp_node_action_inout *inout_p; /**< inout_p Input/output pointer to write to. */
Michal Vaskoafac7822020-10-20 14:22:26 +0200151};
152
153/**
154 * @brief Meta information passed to yin_parse_minmax function.
155 */
156struct minmax_dev_meta {
157 uint32_t *lim; /**< min/max value to write to. */
158 uint16_t *flags; /**< min/max flags to write to. */
159 struct lysp_ext_instance **exts; /**< extension instances to add to. */
160};
161
Michal Vaskod0625d72022-10-06 15:02:50 +0200162LY_ERR yin_parse_content(struct lysp_yin_ctx *ctx, struct yin_subelement *subelem_info, size_t subelem_info_size,
Michal Vasko193dacd2022-10-13 08:43:05 +0200163 const void *parent, enum ly_stmt parent_stmt, const char **text_content, struct lysp_ext_instance **exts);
Michal Vaskoafac7822020-10-20 14:22:26 +0200164
165/**
166 * @brief Match yang keyword from yin data.
167 *
168 * @param[in,out] ctx Yin parser context for logging and to store current state.
169 * @param[in] name Start of keyword name
170 * @param[in] name_len Lenght of keyword name.
171 * @param[in] prefix Start of keyword prefix.
172 * @param[in] prefix_len Lenght of prefix.
Michal Vasko193dacd2022-10-13 08:43:05 +0200173 * @param[in] parent Identification of parent element, use LY_STMT_NONE for elements without parent.
Michal Vaskoafac7822020-10-20 14:22:26 +0200174 * @return yang_keyword values.
175 */
Radek Krejcid6b76452019-09-03 17:03:03 +0200176enum ly_stmt
Michal Vasko193dacd2022-10-13 08:43:05 +0200177yin_match_keyword(struct lysp_yin_ctx *ctx, const char *name, size_t name_len, const char *prefix, size_t prefix_len,
178 enum ly_stmt parent)
David Sedlák1bccdfa2019-06-17 15:55:27 +0200179{
David Sedlák8f7a1172019-06-20 14:42:18 +0200180 const char *start = NULL;
Radek Krejcid6b76452019-09-03 17:03:03 +0200181 enum ly_stmt kw = LY_STMT_NONE;
David Sedlák8f7a1172019-06-20 14:42:18 +0200182 const struct lyxml_ns *ns = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +0200183 struct ly_in *in;
David Sedlák8f7a1172019-06-20 14:42:18 +0200184
Michal Vasko69730152020-10-09 16:30:07 +0200185 if (!name || (name_len == 0)) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200186 return LY_STMT_NONE;
David Sedlák1bccdfa2019-06-17 15:55:27 +0200187 }
188
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200189 ns = lyxml_ns_get(&ctx->xmlctx->ns, prefix, prefix_len);
David Sedlák8f7a1172019-06-20 14:42:18 +0200190 if (ns) {
191 if (!IS_YIN_NS(ns->uri)) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200192 return LY_STMT_EXTENSION_INSTANCE;
David Sedlák8f7a1172019-06-20 14:42:18 +0200193 }
194 } else {
195 /* elements without namespace are automatically unknown */
Radek Krejcid6b76452019-09-03 17:03:03 +0200196 return LY_STMT_NONE;
David Sedlák8f7a1172019-06-20 14:42:18 +0200197 }
David Sedlák1bccdfa2019-06-17 15:55:27 +0200198
Michal Vasko63f3d842020-07-08 10:10:14 +0200199 LY_CHECK_RET(ly_in_new_memory(name, &in), LY_STMT_NONE);
200 start = in->current;
Radek Krejcid54412f2020-12-17 20:25:35 +0100201 kw = lysp_match_kw(in, NULL);
Michal Vasko63f3d842020-07-08 10:10:14 +0200202 name = in->current;
203 ly_in_free(in, 0);
David Sedlák8f7a1172019-06-20 14:42:18 +0200204
205 if (name - start == (long int)name_len) {
David Sedlákc1771b12019-07-10 15:55:46 +0200206 /* 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 +0200207 if ((kw == LY_STMT_VALUE) && (parent == LY_STMT_ERROR_MESSAGE)) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200208 return LY_STMT_ARG_VALUE;
David Sedlákc1771b12019-07-10 15:55:46 +0200209 }
David Sedlák1bccdfa2019-06-17 15:55:27 +0200210 return kw;
211 } else {
David Sedlák3ffbc522019-07-02 17:49:28 +0200212 if (strncmp(start, "text", name_len) == 0) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200213 return LY_STMT_ARG_TEXT;
David Sedlák3ffbc522019-07-02 17:49:28 +0200214 } else {
Radek Krejcid6b76452019-09-03 17:03:03 +0200215 return LY_STMT_NONE;
David Sedlák3ffbc522019-07-02 17:49:28 +0200216 }
David Sedlák1bccdfa2019-06-17 15:55:27 +0200217 }
218}
219
Michal Vaskoafac7822020-10-20 14:22:26 +0200220/**
221 * @brief Match argument name.
222 *
223 * @param[in] name String representing name.
224 * @param[in] len Lenght of the name.
Michal Vaskoafac7822020-10-20 14:22:26 +0200225 * @return yin_argument values.
226 */
David Sedlákc5b20842019-08-13 10:18:31 +0200227enum yin_argument
David Sedlák060b00e2019-06-19 11:12:06 +0200228yin_match_argument_name(const char *name, size_t len)
David Sedlák3b4db242018-10-19 16:11:01 +0200229{
David Sedlákc5b20842019-08-13 10:18:31 +0200230 enum yin_argument arg = YIN_ARG_UNKNOWN;
David Sedlák872c7b42018-10-26 13:15:20 +0200231 size_t already_read = 0;
Michal Vasko69730152020-10-09 16:30:07 +0200232
David Sedlák7ff55a92019-06-17 11:11:41 +0200233 LY_CHECK_RET(len == 0, YIN_ARG_NONE);
David Sedlák3b4db242018-10-19 16:11:01 +0200234
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200235#define READ_INC(LEN) already_read += LEN
236#define ARG_SET(STMT) arg=STMT
237#define ARG_CHECK(STR, LEN) (!strncmp((name) + already_read, STR, LEN) && (READ_INC(LEN)))
David Sedlákc10e7902018-12-17 02:17:59 +0100238
David Sedlák1c8b2702019-02-22 11:03:02 +0100239 switch (*name) {
David Sedlák94de2aa2019-02-15 12:42:11 +0100240 case 'c':
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200241 READ_INC(1);
242 if (ARG_CHECK("ondition", 8)) {
243 ARG_SET(YIN_ARG_CONDITION);
244 }
David Sedlák3b4db242018-10-19 16:11:01 +0200245 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200246
David Sedlák94de2aa2019-02-15 12:42:11 +0100247 case 'd':
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200248 READ_INC(1);
249 if (ARG_CHECK("ate", 3)) {
250 ARG_SET(YIN_ARG_DATE);
251 }
David Sedlák3b4db242018-10-19 16:11:01 +0200252 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200253
David Sedlák94de2aa2019-02-15 12:42:11 +0100254 case 'm':
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200255 READ_INC(1);
256 if (ARG_CHECK("odule", 5)) {
257 ARG_SET(YIN_ARG_MODULE);
258 }
David Sedlák872c7b42018-10-26 13:15:20 +0200259 break;
260
David Sedlák94de2aa2019-02-15 12:42:11 +0100261 case 'n':
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200262 READ_INC(1);
263 if (ARG_CHECK("ame", 3)) {
264 ARG_SET(YIN_ARG_NAME);
265 }
David Sedlák872c7b42018-10-26 13:15:20 +0200266 break;
267
David Sedlák94de2aa2019-02-15 12:42:11 +0100268 case 't':
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200269 READ_INC(1);
270 if (ARG_CHECK("a", 1)) {
271 if (ARG_CHECK("g", 1)) {
272 ARG_SET(YIN_ARG_TAG);
273 } else if (ARG_CHECK("rget-node", 9)) {
274 ARG_SET(YIN_ARG_TARGET_NODE);
275 }
276 } else if (ARG_CHECK("ext", 3)) {
277 ARG_SET(YIN_ARG_TEXT);
278 }
David Sedlák3b4db242018-10-19 16:11:01 +0200279 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200280
David Sedlák94de2aa2019-02-15 12:42:11 +0100281 case 'u':
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200282 READ_INC(1);
283 if (ARG_CHECK("ri", 2)) {
284 ARG_SET(YIN_ARG_URI);
285 }
David Sedlák3b4db242018-10-19 16:11:01 +0200286 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200287
David Sedlák94de2aa2019-02-15 12:42:11 +0100288 case 'v':
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200289 READ_INC(1);
290 if (ARG_CHECK("alue", 4)) {
291 ARG_SET(YIN_ARG_VALUE);
292 }
David Sedlák3b4db242018-10-19 16:11:01 +0200293 break;
294 }
295
David Sedlákc10e7902018-12-17 02:17:59 +0100296 /* whole argument must be matched */
David Sedlák872c7b42018-10-26 13:15:20 +0200297 if (already_read != len) {
David Sedláka7406952019-04-05 10:33:07 +0200298 arg = YIN_ARG_UNKNOWN;
David Sedlák872c7b42018-10-26 13:15:20 +0200299 }
300
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200301#undef ARG_CHECK
302#undef ARG_SET
303#undef READ_INC
David Sedlák18730132019-03-15 15:51:34 +0100304
David Sedlák872c7b42018-10-26 13:15:20 +0200305 return arg;
David Sedlák3b4db242018-10-19 16:11:01 +0200306}
307
Radek Krejcid6b76452019-09-03 17:03:03 +0200308#define IS_NODE_ELEM(kw) (kw == LY_STMT_ANYXML || kw == LY_STMT_ANYDATA || kw == LY_STMT_LEAF || kw == LY_STMT_LEAF_LIST || \
309 kw == LY_STMT_TYPEDEF || kw == LY_STMT_USES || kw == LY_STMT_LIST || kw == LY_STMT_NOTIFICATION || \
310 kw == LY_STMT_GROUPING || kw == LY_STMT_CONTAINER || kw == LY_STMT_CASE || kw == LY_STMT_CHOICE || \
311 kw == LY_STMT_ACTION || kw == LY_STMT_RPC || kw == LY_STMT_AUGMENT)
David Sedlák81497a32019-08-13 16:56:26 +0200312
Radek Krejcid6b76452019-09-03 17:03:03 +0200313#define HAS_META(kw) (IS_NODE_ELEM(kw) || kw == LY_STMT_IMPORT || kw == LY_STMT_INCLUDE || kw == LY_STMT_INPUT || kw == LY_STMT_OUTPUT)
David Sedlák81497a32019-08-13 16:56:26 +0200314
David Sedlák26ea1432019-08-14 13:42:23 +0200315/**
316 * @brief Free subelems information allocated on heap.
317 *
318 * @param[in] count Size of subelems array.
319 * @param[in] subelems Subelems array to free.
320 */
David Sedlák81497a32019-08-13 16:56:26 +0200321static void
322subelems_deallocator(size_t count, struct yin_subelement *subelems)
323{
Michal Vaskod989ba02020-08-24 10:59:24 +0200324 for (size_t i = 0; i < count; ++i) {
David Sedlák81497a32019-08-13 16:56:26 +0200325 if (HAS_META(subelems[i].type)) {
326 free(subelems[i].dest);
327 }
328 }
329
330 free(subelems);
331}
332
David Sedlák26ea1432019-08-14 13:42:23 +0200333/**
334 * @brief Allocate subelems information on heap.
335 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200336 * @param[in] ctx YIN parser context, used for logging.
David Sedlák26ea1432019-08-14 13:42:23 +0200337 * @param[in] count Number of subelements.
338 * @param[in] parent Parent node if any.
339 * @param[out] result Allocated subelems array.
Michal Vasko193dacd2022-10-13 08:43:05 +0200340 * @return LY_ERR values.
David Sedlák26ea1432019-08-14 13:42:23 +0200341 */
David Sedlák81497a32019-08-13 16:56:26 +0200342static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200343subelems_allocator(struct lysp_yin_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;
Michal Vasko26bbb272022-08-02 14:54:33 +0200358
David Sedlák81497a32019-08-13 16:56:26 +0200359 node_meta = calloc(1, sizeof *node_meta);
David Sedlákbf8a2b72019-08-14 16:48:10 +0200360 LY_CHECK_GOTO(!node_meta, mem_err);
David Sedlák81497a32019-08-13 16:56:26 +0200361 node_meta->parent = parent;
David Sedlákbf8a2b72019-08-14 16:48:10 +0200362 node_meta->nodes = va_arg(ap, void *);
David Sedlák81497a32019-08-13 16:56:26 +0200363 (*result)[i].dest = node_meta;
Radek Krejcid6b76452019-09-03 17:03:03 +0200364 } else if ((*result)[i].type == LY_STMT_IMPORT) {
David Sedlák81497a32019-08-13 16:56:26 +0200365 struct import_meta *imp_meta = NULL;
Michal Vasko26bbb272022-08-02 14:54:33 +0200366
David Sedlák81497a32019-08-13 16:56:26 +0200367 imp_meta = calloc(1, sizeof *imp_meta);
David Sedlákbf8a2b72019-08-14 16:48:10 +0200368 LY_CHECK_GOTO(!imp_meta, mem_err);
David Sedlák81497a32019-08-13 16:56:26 +0200369 imp_meta->prefix = va_arg(ap, const char *);
370 imp_meta->imports = va_arg(ap, struct lysp_import **);
371 (*result)[i].dest = imp_meta;
Radek Krejcid6b76452019-09-03 17:03:03 +0200372 } else if ((*result)[i].type == LY_STMT_INCLUDE) {
David Sedlák81497a32019-08-13 16:56:26 +0200373 struct include_meta *inc_meta = NULL;
Michal Vasko26bbb272022-08-02 14:54:33 +0200374
David Sedlák81497a32019-08-13 16:56:26 +0200375 inc_meta = calloc(1, sizeof *inc_meta);
David Sedlákbf8a2b72019-08-14 16:48:10 +0200376 LY_CHECK_GOTO(!inc_meta, mem_err);
David Sedlák81497a32019-08-13 16:56:26 +0200377 inc_meta->name = va_arg(ap, const char *);
378 inc_meta->includes = va_arg(ap, struct lysp_include **);
379 (*result)[i].dest = inc_meta;
Michal Vasko69730152020-10-09 16:30:07 +0200380 } else if (((*result)[i].type == LY_STMT_INPUT) || ((*result)[i].type == LY_STMT_OUTPUT)) {
David Sedlák81497a32019-08-13 16:56:26 +0200381 struct inout_meta *inout_meta = NULL;
Michal Vasko26bbb272022-08-02 14:54:33 +0200382
David Sedlák81497a32019-08-13 16:56:26 +0200383 inout_meta = calloc(1, sizeof *inout_meta);
David Sedlákbf8a2b72019-08-14 16:48:10 +0200384 LY_CHECK_GOTO(!inout_meta, mem_err);
David Sedlák81497a32019-08-13 16:56:26 +0200385 inout_meta->parent = parent;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100386 inout_meta->inout_p = va_arg(ap, struct lysp_node_action_inout *);
David Sedlák81497a32019-08-13 16:56:26 +0200387 (*result)[i].dest = inout_meta;
388 } else {
389 (*result)[i].dest = va_arg(ap, void *);
390 }
391 /* FLAGS */
392 (*result)[i].flags = va_arg(ap, int);
393 }
394 va_end(ap);
395
396 return LY_SUCCESS;
397
David Sedlákbf8a2b72019-08-14 16:48:10 +0200398mem_err:
David Sedlák81497a32019-08-13 16:56:26 +0200399 subelems_deallocator(count, *result);
Michal Vaskob36053d2020-03-26 15:49:30 +0100400 LOGMEM(ctx->xmlctx->ctx);
David Sedlák81497a32019-08-13 16:56:26 +0200401 return LY_EMEM;
402}
403
Michal Vaskoafac7822020-10-20 14:22:26 +0200404/**
405 * @brief Check that val is valid UTF8 character sequence of val_type.
406 * Doesn't check empty string, only character validity.
407 *
408 * @param[in] ctx Yin parser context for logging.
409 * @param[in] val_type Type of the input string to select method of checking character validity.
Michal Vaskoafac7822020-10-20 14:22:26 +0200410 * @return LY_ERR values.
411 */
David Sedlák8f7a1172019-06-20 14:42:18 +0200412LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200413yin_validate_value(struct lysp_yin_ctx *ctx, enum yang_arg val_type)
David Sedlák4a650532019-07-10 11:55:18 +0200414{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200415 uint8_t prefix = 0;
416 uint32_t c;
Michal Vaskob36053d2020-03-26 15:49:30 +0100417 size_t utf8_char_len, already_read = 0;
418 const char *val;
419
420 assert((ctx->xmlctx->status == LYXML_ELEM_CONTENT) || (ctx->xmlctx->status == LYXML_ATTR_CONTENT));
421
422 val = ctx->xmlctx->value;
423 while (already_read < ctx->xmlctx->value_len) {
David Sedlák4a650532019-07-10 11:55:18 +0200424 LY_CHECK_ERR_RET(ly_getutf8((const char **)&val, &c, &utf8_char_len),
Michal Vaskod0625d72022-10-06 15:02:50 +0200425 LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INCHAR, (val)[-utf8_char_len]), LY_EVALID);
David Sedlák4a650532019-07-10 11:55:18 +0200426
427 switch (val_type) {
428 case Y_IDENTIF_ARG:
Michal Vaskod0625d72022-10-06 15:02:50 +0200429 LY_CHECK_RET(lysp_check_identifierchar((struct lysp_ctx *)ctx, c, already_read ? 0 : 1, NULL));
David Sedlák4a650532019-07-10 11:55:18 +0200430 break;
431 case Y_PREF_IDENTIF_ARG:
Michal Vaskod0625d72022-10-06 15:02:50 +0200432 LY_CHECK_RET(lysp_check_identifierchar((struct lysp_ctx *)ctx, c, already_read ? 0 : 1, &prefix));
David Sedlák4a650532019-07-10 11:55:18 +0200433 break;
434 case Y_STR_ARG:
435 case Y_MAYBE_STR_ARG:
Michal Vaskod0625d72022-10-06 15:02:50 +0200436 LY_CHECK_RET(lysp_check_stringchar((struct lysp_ctx *)ctx, c));
David Sedlák4a650532019-07-10 11:55:18 +0200437 break;
438 }
Radek Iša2eac74c2021-03-15 15:53:08 +0100439
440 already_read += utf8_char_len;
441 LY_CHECK_ERR_RET(already_read > ctx->xmlctx->value_len, LOGINT(ctx->xmlctx->ctx), LY_EINT);
David Sedlák4a650532019-07-10 11:55:18 +0200442 }
443
Radek Iša42cc0b72021-03-15 16:51:31 +0100444 if ((already_read == 0) &&
445 ((val_type == Y_PREF_IDENTIF_ARG) || (val_type == Y_IDENTIF_ARG))) {
446 /* Empty identifier or prefix*/
447 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YIN, "Empty identifier is not allowed.");
448 return LY_EVALID;
449 }
450
David Sedlák4a650532019-07-10 11:55:18 +0200451 return LY_SUCCESS;
452}
453
David Sedlákb4e44562019-07-04 15:42:12 +0200454/**
Michal Vaskob36053d2020-03-26 15:49:30 +0100455 * @brief Parse yin attributes.
David Sedlákb4e44562019-07-04 15:42:12 +0200456 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200457 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák4a650532019-07-10 11:55:18 +0200458 * @param[in] arg_type Type of argument that is expected in parsed element (use YIN_ARG_NONE for elements without
459 * special argument).
David Sedlákbf8a2b72019-08-14 16:48:10 +0200460 * @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 +0200461 * @param[in] val_type Type of expected value of attribute.
David Sedlákb4e44562019-07-04 15:42:12 +0200462 * @param[in] current_element Identification of current element, used for logging.
David Sedlákb4e44562019-07-04 15:42:12 +0200463 * @return LY_ERR values.
464 */
465static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200466yin_parse_attribute(struct lysp_yin_ctx *ctx, enum yin_argument arg_type, const char **arg_val, enum yang_arg val_type,
Radek Krejci0f969882020-08-21 16:56:47 +0200467 enum ly_stmt current_element)
David Sedlák8f7a1172019-06-20 14:42:18 +0200468{
David Sedlákc5b20842019-08-13 10:18:31 +0200469 enum yin_argument arg = YIN_ARG_UNKNOWN;
David Sedlák619db942019-07-03 14:47:30 +0200470 bool found = false;
David Sedlák8f7a1172019-06-20 14:42:18 +0200471
David Sedlák1bccdfa2019-06-17 15:55:27 +0200472 /* validation of attributes */
Michal Vaskob36053d2020-03-26 15:49:30 +0100473 while (ctx->xmlctx->status == LYXML_ATTRIBUTE) {
David Sedlák00250342019-06-21 14:19:39 +0200474 /* yin arguments represented as attributes have no namespace, which in this case means no prefix */
Michal Vaskob36053d2020-03-26 15:49:30 +0100475 if (!ctx->xmlctx->prefix) {
476 arg = yin_match_argument_name(ctx->xmlctx->name, ctx->xmlctx->name_len);
David Sedlák7ff55a92019-06-17 11:11:41 +0200477 if (arg == YIN_ARG_NONE) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100478 /* skip it */
479 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
David Sedlák7ff55a92019-06-17 11:11:41 +0200480 } else if (arg == arg_type) {
Michal Vaskod0625d72022-10-06 15:02:50 +0200481 LY_CHECK_ERR_RET(found, LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_DUP_ATTR,
Michal Vasko193dacd2022-10-13 08:43:05 +0200482 yin_attr2str(arg), lyplg_ext_stmt2str(current_element)), LY_EVALID);
David Sedlák619db942019-07-03 14:47:30 +0200483 found = true;
Michal Vaskob36053d2020-03-26 15:49:30 +0100484
485 /* go to value */
486 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
487 LY_CHECK_RET(yin_validate_value(ctx, val_type));
Radek Krejci011e4aa2020-09-04 15:22:31 +0200488 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 +0200489 LY_CHECK_RET(!(*arg_val), LY_EMEM);
David Sedlák2b214ac2019-06-06 16:11:03 +0200490 } else {
Michal Vaskod0625d72022-10-06 15:02:50 +0200491 LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_UNEXP_ATTR, ctx->xmlctx->name_len,
Michal Vasko193dacd2022-10-13 08:43:05 +0200492 ctx->xmlctx->name, lyplg_ext_stmt2str(current_element));
David Sedlák619db942019-07-03 14:47:30 +0200493 return LY_EVALID;
David Sedláka7406952019-04-05 10:33:07 +0200494 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100495 } else {
496 /* skip it */
497 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
David Sedláka7406952019-04-05 10:33:07 +0200498 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100499
500 /* next attribute */
501 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
David Sedláka7406952019-04-05 10:33:07 +0200502 }
503
David Sedlák292763b2019-07-09 11:10:53 +0200504 /* anything else than Y_MAYBE_STR_ARG is mandatory */
Michal Vasko69730152020-10-09 16:30:07 +0200505 if ((val_type != Y_MAYBE_STR_ARG) && !found) {
Michal Vaskod0625d72022-10-06 15:02:50 +0200506 LOGVAL_PARSER((struct lysp_ctx *)ctx, LYVE_SYNTAX_YIN, "Missing mandatory attribute %s of %s element.",
Michal Vasko193dacd2022-10-13 08:43:05 +0200507 yin_attr2str(arg_type), lyplg_ext_stmt2str(current_element));
David Sedlák619db942019-07-03 14:47:30 +0200508 return LY_EVALID;
509 }
510
511 return LY_SUCCESS;
David Sedláka7406952019-04-05 10:33:07 +0200512}
513
David Sedlákd6e56892019-07-01 15:40:24 +0200514/**
Radek Krejci12b1c152019-09-05 16:20:48 +0200515 * @brief Get record with given type.
David Sedlákd6e56892019-07-01 15:40:24 +0200516 *
517 * @param[in] type Type of wanted record.
518 * @param[in] array_size Size of array.
519 * @param[in] array Searched array.
David Sedlákd6e56892019-07-01 15:40:24 +0200520 * @return Pointer to desired record on success, NULL if element is not in the array.
521 */
David Sedlákb4e44562019-07-04 15:42:12 +0200522static struct yin_subelement *
Radek Krejci1deb5be2020-08-26 16:43:36 +0200523get_record(enum ly_stmt type, LY_ARRAY_COUNT_TYPE array_size, struct yin_subelement *array)
David Sedlákd6e56892019-07-01 15:40:24 +0200524{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200525 for (LY_ARRAY_COUNT_TYPE u = 0; u < array_size; ++u) {
Radek Krejci12b1c152019-09-05 16:20:48 +0200526 if (array[u].type == type) {
527 return &array[u];
David Sedlákd6e56892019-07-01 15:40:24 +0200528 }
529 }
David Sedlákd6e56892019-07-01 15:40:24 +0200530 return NULL;
531}
532
David Sedlákbba38e52019-07-09 15:20:01 +0200533/**
534 * @brief Helper function to check mandatory constraint of subelement.
535 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200536 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200537 * @param[in] subelem_info Array of information about subelements.
538 * @param[in] subelem_info_size Size of subelem_info array.
539 * @param[in] current_element Identification of element that is currently being parsed, used for logging.
David Sedlákbba38e52019-07-09 15:20:01 +0200540 * @return LY_ERR values.
541 */
542static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200543yin_check_subelem_mandatory_constraint(struct lysp_yin_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)) {
Michal Vaskod0625d72022-10-06 15:02:50 +0200549 LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_MAND_SUBELEM,
Michal Vasko193dacd2022-10-13 08:43:05 +0200550 lyplg_ext_stmt2str(subelem_info[i].type), lyplg_ext_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 * @return LY_ERR values.
567 */
568static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200569yin_check_subelem_first_constraint(struct lysp_yin_ctx *ctx, struct yin_subelement *subelem_info,
Radek Krejci0f969882020-08-21 16:56:47 +0200570 signed char subelem_info_size, enum ly_stmt current_element,
571 struct yin_subelement *exp_first)
David Sedlák21f87cd2019-07-03 16:53:23 +0200572{
David Sedlákb0faad82019-07-04 14:28:59 +0200573 for (signed char i = 0; i < subelem_info_size; ++i) {
David Sedlák21f87cd2019-07-03 16:53:23 +0200574 if (subelem_info[i].flags & YIN_SUBELEM_PARSED) {
Michal Vaskod0625d72022-10-06 15:02:50 +0200575 LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_FIRT_SUBELEM,
Michal Vasko193dacd2022-10-13 08:43:05 +0200576 lyplg_ext_stmt2str(exp_first->type), lyplg_ext_stmt2str(current_element));
David Sedlák21f87cd2019-07-03 16:53:23 +0200577 return LY_EVALID;
578 }
579 }
580
581 return LY_SUCCESS;
582}
583
David Sedlákbba38e52019-07-09 15:20:01 +0200584/**
David Sedlákb4e44562019-07-04 15:42:12 +0200585 * @brief Parse simple element without any special constraints and argument mapped to yin attribute,
586 * for example prefix or namespace element.
David Sedlákd6e56892019-07-01 15:40:24 +0200587 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200588 * @param[in,out] ctx YIN parser context for logging and to store current state.
Michal Vasko193dacd2022-10-13 08:43:05 +0200589 * @param[in] parent Current statement parent.
590 * @param[in] parent_stmt Type of @p parent statement.
David Sedlákb4e44562019-07-04 15:42:12 +0200591 * @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 Vasko193dacd2022-10-13 08:43:05 +0200598yin_parse_simple_element(struct lysp_yin_ctx *ctx, const void *parent, enum ly_stmt parent_stmt, 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{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100601 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200602 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
603 };
David Sedlákb4e44562019-07-04 15:42:12 +0200604
Michal Vasko193dacd2022-10-13 08:43:05 +0200605 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
606 LY_CHECK_RET(yin_parse_attribute(ctx, arg_type, value, arg_val_type, parent_stmt));
607
608 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), parent, parent_stmt, NULL, exts);
David Sedlákb4e44562019-07-04 15:42:12 +0200609}
610
611/**
David Sedlák6542aed2019-08-14 10:47:43 +0200612 * @brief Parse path element.
613 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200614 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák6542aed2019-08-14 10:47:43 +0200615 * @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 Vasko193dacd2022-10-13 08:43:05 +0200619yin_parse_path(struct lysp_yin_ctx *ctx, 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
Michal Vasko193dacd2022-10-13 08:43:05 +0200624 LY_CHECK_RET(yin_parse_simple_element(ctx, type, LY_STMT_PATH, &str_path, YIN_ARG_VALUE, Y_STR_ARG, &type->exts));
625
Michal Vaskoed725d72021-06-23 12:03:45 +0200626 ret = ly_path_parse(ctx->xmlctx->ctx, NULL, str_path, 0, 1, LY_PATH_BEGIN_EITHER,
Michal Vasko69730152020-10-09 16:30:07 +0200627 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path);
Michal Vasko004d3152020-06-11 19:59:22 +0200628 lydict_remove(ctx->xmlctx->ctx, str_path);
629 LY_CHECK_RET(ret);
David Sedlák6542aed2019-08-14 10:47:43 +0200630 type->flags |= LYS_SET_PATH;
631
632 return LY_SUCCESS;
633}
634
635/**
David Sedlákd3983112019-07-12 11:20:56 +0200636 * @brief Parse pattern element.
637 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200638 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200639 * @param[in,out] type Type structure to store parsed value, flags and extension instances.
David Sedlákd3983112019-07-12 11:20:56 +0200640 * @return LY_ERR values.
641 */
642static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200643yin_parse_pattern(struct lysp_yin_ctx *ctx, struct lysp_type *type)
David Sedlákd3983112019-07-12 11:20:56 +0200644{
645 const char *real_value = NULL;
646 char *saved_value = NULL;
647 struct lysp_restr *restr;
648
Michal Vaskob36053d2020-03-26 15:49:30 +0100649 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, type->patterns, restr, LY_EMEM);
650 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
651 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 +0200652 size_t len = strlen(real_value);
653
654 saved_value = malloc(len + 2);
Michal Vaskob36053d2020-03-26 15:49:30 +0100655 LY_CHECK_ERR_RET(!saved_value, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
David Sedlákd3983112019-07-12 11:20:56 +0200656 memmove(saved_value + 1, real_value, len);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100657 lydict_remove(ctx->xmlctx->ctx, real_value);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100658 saved_value[0] = LYSP_RESTR_PATTERN_ACK;
David Sedlákd3983112019-07-12 11:20:56 +0200659 saved_value[len + 1] = '\0';
Michal Vasko7f45cf22020-10-01 12:49:44 +0200660 LY_CHECK_RET(lydict_insert_zc(ctx->xmlctx->ctx, saved_value, &restr->arg.str));
Michal Vasko8a67eff2021-12-07 14:04:47 +0100661 restr->arg.mod = PARSER_CUR_PMOD(ctx);
David Sedlákd3983112019-07-12 11:20:56 +0200662 type->flags |= LYS_SET_PATTERN;
663
Radek Krejcif13b87b2020-12-01 22:02:17 +0100664 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200665 {LY_STMT_DESCRIPTION, &restr->dsc, YIN_SUBELEM_UNIQUE},
666 {LY_STMT_ERROR_APP_TAG, &restr->eapptag, YIN_SUBELEM_UNIQUE},
667 {LY_STMT_ERROR_MESSAGE, &restr->emsg, YIN_SUBELEM_UNIQUE},
668 {LY_STMT_MODIFIER, &restr->arg, YIN_SUBELEM_UNIQUE},
669 {LY_STMT_REFERENCE, &restr->ref, YIN_SUBELEM_UNIQUE},
670 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
671 };
Michal Vasko69730152020-10-09 16:30:07 +0200672
Michal Vasko193dacd2022-10-13 08:43:05 +0200673 LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), restr, LY_STMT_PATTERN, NULL, &restr->exts));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +0200674
675 /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
676 LY_CHECK_RET(ly_set_add(&ctx->ext_inst, restr->exts, 1, NULL));
677
678 return LY_SUCCESS;
David Sedlákd3983112019-07-12 11:20:56 +0200679}
680
David Sedlákc5b20842019-08-13 10:18:31 +0200681/**
682 * @brief Parse fraction-digits element.
683 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200684 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákc5b20842019-08-13 10:18:31 +0200685 * @param[in,out] type Type structure to store value, flags and extension instances.
David Sedlákc5b20842019-08-13 10:18:31 +0200686 * @return LY_ERR values.
687 */
David Sedlákf75d55e2019-07-12 16:52:50 +0200688static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200689yin_parse_fracdigits(struct lysp_yin_ctx *ctx, struct lysp_type *type)
David Sedlákf75d55e2019-07-12 16:52:50 +0200690{
691 const char *temp_val = NULL;
692 char *ptr;
Michal Vasko73d77ab2021-07-23 12:45:55 +0200693 unsigned long long int num;
David Sedlákf75d55e2019-07-12 16:52:50 +0200694
Michal Vaskob36053d2020-03-26 15:49:30 +0100695 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
696 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 +0200697
Michal Vasko69730152020-10-09 16:30:07 +0200698 if ((temp_val[0] == '\0') || (temp_val[0] == '0') || !isdigit(temp_val[0])) {
Michal Vaskod0625d72022-10-06 15:02:50 +0200699 LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "fraction-digits");
Michal Vaskoe180ed02021-02-05 16:31:20 +0100700 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákf75d55e2019-07-12 16:52:50 +0200701 return LY_EVALID;
702 }
703
704 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +0200705 num = strtoull(temp_val, &ptr, LY_BASE_DEC);
David Sedlákf75d55e2019-07-12 16:52:50 +0200706 if (*ptr != '\0') {
Michal Vaskod0625d72022-10-06 15:02:50 +0200707 LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "fraction-digits");
Michal Vaskoe180ed02021-02-05 16:31:20 +0100708 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákf75d55e2019-07-12 16:52:50 +0200709 return LY_EVALID;
710 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100711 if ((errno == ERANGE) || (num > LY_TYPE_DEC64_FD_MAX)) {
Michal Vaskod0625d72022-10-06 15:02:50 +0200712 LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "fraction-digits");
Michal Vaskoe180ed02021-02-05 16:31:20 +0100713 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákf75d55e2019-07-12 16:52:50 +0200714 return LY_EVALID;
715 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100716 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákf75d55e2019-07-12 16:52:50 +0200717 type->fraction_digits = num;
718 type->flags |= LYS_SET_FRDIGITS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100719 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200720 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
721 };
Michal Vasko69730152020-10-09 16:30:07 +0200722
Michal Vasko193dacd2022-10-13 08:43:05 +0200723 LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), type, LY_STMT_FRACTION_DIGITS, NULL, &type->exts));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +0200724
725 /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
726 LY_CHECK_RET(ly_set_add(&ctx->ext_inst, type->exts, 1, NULL));
727
728 return LY_SUCCESS;
David Sedlákf75d55e2019-07-12 16:52:50 +0200729}
730
David Sedlák07869a52019-07-12 14:28:19 +0200731/**
David Sedlák43801c92019-08-05 15:58:54 +0200732 * @brief Parse enum element.
David Sedlák07869a52019-07-12 14:28:19 +0200733 *
734 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200735 * @param[in,out] type Type structure to store parsed value, flags and extension instances.
David Sedlák07869a52019-07-12 14:28:19 +0200736 * @return LY_ERR values.
737 */
David Sedlákca36c422019-07-12 12:47:55 +0200738static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200739yin_parse_enum(struct lysp_yin_ctx *ctx, struct lysp_type *type)
David Sedlákca36c422019-07-12 12:47:55 +0200740{
David Sedlák07869a52019-07-12 14:28:19 +0200741 struct lysp_type_enum *en;
David Sedlák1e696782019-07-17 15:06:07 +0200742
Michal Vaskob36053d2020-03-26 15:49:30 +0100743 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, type->enums, en, LY_EMEM);
David Sedlák43801c92019-08-05 15:58:54 +0200744 type->flags |= LYS_SET_ENUM;
Michal Vaskob36053d2020-03-26 15:49:30 +0100745 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
746 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &en->name, Y_STR_ARG, LY_STMT_ENUM));
Michal Vaskod0625d72022-10-06 15:02:50 +0200747 LY_CHECK_RET(lysp_check_enum_name((struct lysp_ctx *)ctx, en->name, strlen(en->name)));
748 CHECK_NONEMPTY((struct lysp_ctx *)ctx, strlen(en->name), "enum");
749 CHECK_UNIQUENESS((struct lysp_ctx *)ctx, type->enums, name, "enum", en->name);
David Sedlákca36c422019-07-12 12:47:55 +0200750
Radek Krejcif13b87b2020-12-01 22:02:17 +0100751 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200752 {LY_STMT_DESCRIPTION, &en->dsc, YIN_SUBELEM_UNIQUE},
753 {LY_STMT_IF_FEATURE, &en->iffeatures, 0},
754 {LY_STMT_REFERENCE, &en->ref, YIN_SUBELEM_UNIQUE},
755 {LY_STMT_STATUS, &en->flags, YIN_SUBELEM_UNIQUE},
756 {LY_STMT_VALUE, en, YIN_SUBELEM_UNIQUE},
757 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
758 };
Michal Vasko69730152020-10-09 16:30:07 +0200759
Michal Vasko193dacd2022-10-13 08:43:05 +0200760 LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), en, LY_STMT_ENUM, NULL, &en->exts));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +0200761
762 /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
763 LY_CHECK_RET(ly_set_add(&ctx->ext_inst, en->exts, 1, NULL));
764
765 return LY_SUCCESS;
David Sedlák43801c92019-08-05 15:58:54 +0200766}
767
768/**
769 * @brief Parse bit element.
770 *
771 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200772 * @param[in,out] type Type structure to store parsed value, flags and extension instances.
David Sedlák43801c92019-08-05 15:58:54 +0200773 * @return LY_ERR values.
774 */
775static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200776yin_parse_bit(struct lysp_yin_ctx *ctx, struct lysp_type *type)
David Sedlák43801c92019-08-05 15:58:54 +0200777{
778 struct lysp_type_enum *en;
779
Michal Vaskob36053d2020-03-26 15:49:30 +0100780 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, type->bits, en, LY_EMEM);
David Sedlák43801c92019-08-05 15:58:54 +0200781 type->flags |= LYS_SET_BIT;
Michal Vaskob36053d2020-03-26 15:49:30 +0100782 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
783 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &en->name, Y_IDENTIF_ARG, LY_STMT_BIT));
Michal Vaskod0625d72022-10-06 15:02:50 +0200784 CHECK_UNIQUENESS((struct lysp_ctx *)ctx, type->bits, name, "bit", en->name);
David Sedlák43801c92019-08-05 15:58:54 +0200785
Radek Krejcif13b87b2020-12-01 22:02:17 +0100786 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200787 {LY_STMT_DESCRIPTION, &en->dsc, YIN_SUBELEM_UNIQUE},
788 {LY_STMT_IF_FEATURE, &en->iffeatures, 0},
789 {LY_STMT_POSITION, en, YIN_SUBELEM_UNIQUE},
790 {LY_STMT_REFERENCE, &en->ref, YIN_SUBELEM_UNIQUE},
791 {LY_STMT_STATUS, &en->flags, YIN_SUBELEM_UNIQUE},
792 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
793 };
Michal Vasko69730152020-10-09 16:30:07 +0200794
Michal Vasko193dacd2022-10-13 08:43:05 +0200795 LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), en, LY_STMT_BIT, NULL, &en->exts));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +0200796
797 /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
798 LY_CHECK_RET(ly_set_add(&ctx->ext_inst, en->exts, 1, NULL));
799
800 return LY_SUCCESS;
David Sedlákca36c422019-07-12 12:47:55 +0200801}
802
David Sedlákd3983112019-07-12 11:20:56 +0200803/**
David Sedlák5f8191e2019-07-08 16:35:52 +0200804 * @brief Parse simple element without any special constraints and argument mapped to yin attribute, that can have
805 * more instances, such as base or if-feature.
806 *
807 * @param[in,out] ctx YIN parser context for logging and to store current state.
Michal Vasko193dacd2022-10-13 08:43:05 +0200808 * @param[in] parent_stmt Type of parent statement.
David Sedlák5f8191e2019-07-08 16:35:52 +0200809 * @param[out] values Parsed values to add to.
810 * @param[in] arg_type Expected type of attribute.
David Sedlák292763b2019-07-09 11:10:53 +0200811 * @param[in] arg_val_type Type of expected value of attribute.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200812 * @param[in,out] exts Extension instances to add to.
David Sedlák5f8191e2019-07-08 16:35:52 +0200813 * @return LY_ERR values.
814 */
815static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200816yin_parse_simple_elements(struct lysp_yin_ctx *ctx, enum ly_stmt parent_stmt, const char ***values,
817 enum yin_argument arg_type, enum yang_arg arg_val_type, struct lysp_ext_instance **exts)
David Sedlák5f8191e2019-07-08 16:35:52 +0200818{
819 const char **value;
Michal Vasko193dacd2022-10-13 08:43:05 +0200820 LY_ARRAY_COUNT_TYPE index = LY_ARRAY_COUNT(*values);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100821 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200822 {LY_STMT_EXTENSION_INSTANCE, &index, 0}
823 };
David Sedlák968ac342019-07-11 15:17:59 +0200824
Michal Vasko193dacd2022-10-13 08:43:05 +0200825 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *values, value, LY_EMEM);
David Sedlák5f8191e2019-07-08 16:35:52 +0200826
Michal Vasko193dacd2022-10-13 08:43:05 +0200827 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
828 LY_CHECK_RET(yin_parse_attribute(ctx, arg_type, value, arg_val_type, parent_stmt));
829
830 LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), *values, parent_stmt, NULL, exts));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +0200831
832 return LY_SUCCESS;
David Sedlák5f8191e2019-07-08 16:35:52 +0200833}
834
835/**
David Sedlák6542aed2019-08-14 10:47:43 +0200836 * @brief Parse simple element without any special constraints and argument mapped to yin attribute.
837 *
838 * @param[in,out] ctx YIN parser context for logging and to store current state.
Michal Vasko193dacd2022-10-13 08:43:05 +0200839 * @param[in] parent Current statement parent.
840 * @param[in] parent_stmt Type of @p parent statement.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200841 * @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 +0200842 * @param[in] arg_type Expected type of attribute.
843 * @param[in] arg_val_type Type of expected value of attribute.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200844 * @param[in,out] exts Extension instances to add to.
David Sedlák6542aed2019-08-14 10:47:43 +0200845 * @return LY_ERR values.
846 */
847static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200848yin_parse_simple_elem(struct lysp_yin_ctx *ctx, const void *parent, enum ly_stmt parent_stmt, struct yin_subelement *subinfo,
Radek Krejci0f969882020-08-21 16:56:47 +0200849 enum yin_argument arg_type, enum yang_arg arg_val_type, struct lysp_ext_instance **exts)
David Sedlák6542aed2019-08-14 10:47:43 +0200850{
851 if (subinfo->flags & YIN_SUBELEM_UNIQUE) {
Michal Vasko193dacd2022-10-13 08:43:05 +0200852 LY_CHECK_RET(yin_parse_simple_element(ctx, parent, parent_stmt, (const char **)subinfo->dest, arg_type,
853 arg_val_type, exts));
David Sedlák6542aed2019-08-14 10:47:43 +0200854 } else {
Michal Vasko193dacd2022-10-13 08:43:05 +0200855 LY_CHECK_RET(yin_parse_simple_elements(ctx, parent_stmt, (const char ***)subinfo->dest, arg_type,
856 arg_val_type, exts));
David Sedlák6542aed2019-08-14 10:47:43 +0200857 }
858
859 return LY_SUCCESS;
860}
861
862/**
863 * @brief Parse base element.
864 *
865 * @param[in,out] ctx YIN parser context for logging and to store current state.
Michal Vasko193dacd2022-10-13 08:43:05 +0200866 * @param[in] parent_stmt Type of parent statement.
David Sedlák6542aed2019-08-14 10:47:43 +0200867 * @param[out] dest Where parsed values should be stored.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200868 * @param[in,out] exts Extension instances to add to.
David Sedlák6542aed2019-08-14 10:47:43 +0200869 * @return LY_ERR values.
870 */
871static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200872yin_parse_base(struct lysp_yin_ctx *ctx, enum ly_stmt parent_stmt, void *dest, struct lysp_ext_instance **exts)
David Sedlák6542aed2019-08-14 10:47:43 +0200873{
Michal Vasko193dacd2022-10-13 08:43:05 +0200874 struct lysp_type *type;
David Sedlák6542aed2019-08-14 10:47:43 +0200875
Michal Vasko193dacd2022-10-13 08:43:05 +0200876 if (parent_stmt == LY_STMT_TYPE) {
David Sedlák6542aed2019-08-14 10:47:43 +0200877 type = (struct lysp_type *)dest;
Michal Vasko193dacd2022-10-13 08:43:05 +0200878 LY_CHECK_RET(yin_parse_simple_elements(ctx, LY_STMT_BASE, &type->bases, YIN_ARG_NAME, Y_PREF_IDENTIF_ARG, exts));
David Sedlák6542aed2019-08-14 10:47:43 +0200879 type->flags |= LYS_SET_BASE;
Michal Vasko193dacd2022-10-13 08:43:05 +0200880 } else if (parent_stmt == LY_STMT_IDENTITY) {
881 LY_CHECK_RET(yin_parse_simple_elements(ctx, LY_STMT_BASE, (const char ***)dest, YIN_ARG_NAME, Y_PREF_IDENTIF_ARG, exts));
David Sedlák6542aed2019-08-14 10:47:43 +0200882 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100883 LOGINT(ctx->xmlctx->ctx);
David Sedlák6542aed2019-08-14 10:47:43 +0200884 return LY_EINT;
885 }
886
887 return LY_SUCCESS;
888}
889
890/**
David Sedlákbf8a2b72019-08-14 16:48:10 +0200891 * @brief Parse require-instance element.
David Sedlákcf5569a2019-07-11 13:31:34 +0200892 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200893 * @param[in,out] ctx YIN parser context for logging and to store current state.
Michal Vaskob36053d2020-03-26 15:49:30 +0100894 * @param[out] type Type structure to store value, flag and extensions.
David Sedlákcf5569a2019-07-11 13:31:34 +0200895 * @return LY_ERR values.
896 */
897static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200898yin_pasrse_reqinstance(struct lysp_yin_ctx *ctx, struct lysp_type *type)
David Sedlákcf5569a2019-07-11 13:31:34 +0200899{
900 const char *temp_val = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100901 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200902 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
903 };
David Sedlákcf5569a2019-07-11 13:31:34 +0200904
905 type->flags |= LYS_SET_REQINST;
Michal Vaskob36053d2020-03-26 15:49:30 +0100906 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
907 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 +0200908 if (strcmp(temp_val, "true") == 0) {
909 type->require_instance = 1;
910 } else if (strcmp(temp_val, "false") != 0) {
Michal Vaskod0625d72022-10-06 15:02:50 +0200911 LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value",
Michal Vasko69730152020-10-09 16:30:07 +0200912 "require-instance", "true", "false");
Michal Vaskoe180ed02021-02-05 16:31:20 +0100913 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákcf5569a2019-07-11 13:31:34 +0200914 return LY_EVALID;
915 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100916 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákcf5569a2019-07-11 13:31:34 +0200917
Michal Vasko193dacd2022-10-13 08:43:05 +0200918 LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), type, LY_STMT_REQUIRE_INSTANCE, NULL, &type->exts));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +0200919
920 /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
921 LY_CHECK_RET(ly_set_add(&ctx->ext_inst, type->exts, 1, NULL));
922
923 return LY_SUCCESS;
David Sedlákcf5569a2019-07-11 13:31:34 +0200924}
925
926/**
David Sedlákce77bf52019-07-11 16:59:31 +0200927 * @brief Parse modifier element.
928 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200929 * @param[in,out] ctx YIN parser context for logging and to store current state.
Michal Vasko193dacd2022-10-13 08:43:05 +0200930 * @param[in] parent Current statement parent.
David Sedlákce77bf52019-07-11 16:59:31 +0200931 * @param[in,out] pat Value to write to.
932 * @param[in,out] exts Extension instances to add to.
David Sedlákce77bf52019-07-11 16:59:31 +0200933 * @return LY_ERR values.
934 */
935static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200936yin_parse_modifier(struct lysp_yin_ctx *ctx, const void *parent, const char **pat, struct lysp_ext_instance **exts)
David Sedlákce77bf52019-07-11 16:59:31 +0200937{
938 const char *temp_val;
939 char *modified_val;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100940 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200941 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
942 };
David Sedlákce77bf52019-07-11 16:59:31 +0200943
Michal Vasko193dacd2022-10-13 08:43:05 +0200944 assert(**pat == 0x06);
945
Michal Vaskob36053d2020-03-26 15:49:30 +0100946 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
947 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 +0200948 if (strcmp(temp_val, "invert-match") != 0) {
Michal Vaskod0625d72022-10-06 15:02:50 +0200949 LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS1, temp_val, "value",
Michal Vasko69730152020-10-09 16:30:07 +0200950 "modifier", "invert-match");
Michal Vaskoe180ed02021-02-05 16:31:20 +0100951 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákce77bf52019-07-11 16:59:31 +0200952 return LY_EVALID;
953 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100954 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákce77bf52019-07-11 16:59:31 +0200955
956 /* allocate new value */
David Sedlákd3983112019-07-12 11:20:56 +0200957 modified_val = malloc(strlen(*pat) + 1);
Michal Vaskob36053d2020-03-26 15:49:30 +0100958 LY_CHECK_ERR_RET(!modified_val, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
David Sedlákd3983112019-07-12 11:20:56 +0200959 strcpy(modified_val, *pat);
Michal Vaskob36053d2020-03-26 15:49:30 +0100960 lydict_remove(ctx->xmlctx->ctx, *pat);
David Sedlákce77bf52019-07-11 16:59:31 +0200961
962 /* modify the new value */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100963 modified_val[0] = LYSP_RESTR_PATTERN_NACK;
Radek Krejci011e4aa2020-09-04 15:22:31 +0200964 LY_CHECK_RET(lydict_insert_zc(ctx->xmlctx->ctx, modified_val, pat));
David Sedlákce77bf52019-07-11 16:59:31 +0200965
Michal Vasko193dacd2022-10-13 08:43:05 +0200966 LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), parent, LY_STMT_MODIFIER, NULL, exts));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +0200967
968 return LY_SUCCESS;
David Sedlákce77bf52019-07-11 16:59:31 +0200969}
970
971/**
David Sedlákb7296dd2019-07-11 14:58:38 +0200972 * @brief Parse a restriction element (length, range or one instance of must).
973 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200974 * @param[in,out] ctx YIN parser context for logging and to store current state.
Radek Krejcid6b76452019-09-03 17:03:03 +0200975 * @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 +0200976 * @param[in] restr Value to write to.
Michal Vasko193dacd2022-10-13 08:43:05 +0200977 * @return LY_ERR values.
David Sedlákb7296dd2019-07-11 14:58:38 +0200978 */
979static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200980yin_parse_restriction(struct lysp_yin_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr *restr)
David Sedlákb7296dd2019-07-11 14:58:38 +0200981{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100982 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200983 {LY_STMT_DESCRIPTION, &restr->dsc, YIN_SUBELEM_UNIQUE},
984 {LY_STMT_ERROR_APP_TAG, &restr->eapptag, YIN_SUBELEM_UNIQUE},
985 {LY_STMT_ERROR_MESSAGE, &restr->emsg, YIN_SUBELEM_UNIQUE},
986 {LY_STMT_REFERENCE, &restr->ref, YIN_SUBELEM_UNIQUE},
987 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
988 };
David Sedlákb7296dd2019-07-11 14:58:38 +0200989 /* argument of must is called condition, but argument of length and range is called value */
Radek Krejcid6b76452019-09-03 17:03:03 +0200990 enum yin_argument arg_type = (restr_kw == LY_STMT_MUST) ? YIN_ARG_CONDITION : YIN_ARG_VALUE;
David Sedlákb7296dd2019-07-11 14:58:38 +0200991
Michal Vasko193dacd2022-10-13 08:43:05 +0200992 assert(restr_kw == LY_STMT_MUST || restr_kw == LY_STMT_LENGTH || restr_kw == LY_STMT_RANGE);
993
Michal Vaskob36053d2020-03-26 15:49:30 +0100994 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
Michal Vasko7f45cf22020-10-01 12:49:44 +0200995 LY_CHECK_RET(yin_parse_attribute(ctx, arg_type, &restr->arg.str, Y_STR_ARG, restr_kw));
Michal Vasko8a67eff2021-12-07 14:04:47 +0100996 restr->arg.mod = PARSER_CUR_PMOD(ctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100997
Michal Vasko193dacd2022-10-13 08:43:05 +0200998 LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), restr, restr_kw, NULL, &restr->exts));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +0200999
1000 /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
1001 LY_CHECK_RET(ly_set_add(&ctx->ext_inst, restr->exts, 1, NULL));
1002
1003 return LY_SUCCESS;
David Sedlákb7296dd2019-07-11 14:58:38 +02001004}
1005
1006/**
David Sedlák6542aed2019-08-14 10:47:43 +02001007 * @brief Parse range element.
David Sedlákbf8a2b72019-08-14 16:48:10 +02001008 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák6542aed2019-08-14 10:47:43 +02001009 * @param[out] type Type structure to store parsed value and flags.
David Sedlák6542aed2019-08-14 10:47:43 +02001010 * @return LY_ERR values.
1011 */
1012static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001013yin_parse_range(struct lysp_yin_ctx *ctx, struct lysp_type *type)
David Sedlák6542aed2019-08-14 10:47:43 +02001014{
1015 type->range = calloc(1, sizeof *type->range);
Michal Vaskob36053d2020-03-26 15:49:30 +01001016 LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
1017 LY_CHECK_RET(yin_parse_restriction(ctx, LY_STMT_RANGE, type->range));
Michal Vasko44f3d2c2020-08-24 09:49:38 +02001018 type->flags |= LYS_SET_RANGE;
David Sedlák6542aed2019-08-14 10:47:43 +02001019
1020 return LY_SUCCESS;
1021}
1022
1023/**
1024 * @brief Parse length element.
1025 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001026 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák6542aed2019-08-14 10:47:43 +02001027 * @param[out] type Type structure to store parsed value and flags.
David Sedlák6542aed2019-08-14 10:47:43 +02001028 * @return LY_ERR values.
1029 */
1030static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001031yin_parse_length(struct lysp_yin_ctx *ctx, struct lysp_type *type)
David Sedlák6542aed2019-08-14 10:47:43 +02001032{
1033 type->length = calloc(1, sizeof *type->length);
Michal Vaskob36053d2020-03-26 15:49:30 +01001034 LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
1035 LY_CHECK_RET(yin_parse_restriction(ctx, LY_STMT_LENGTH, type->length));
David Sedlák6542aed2019-08-14 10:47:43 +02001036 type->flags |= LYS_SET_LENGTH;
1037
1038 return LY_SUCCESS;
1039}
1040
1041/**
David Sedlákbc9ec9c2019-07-11 15:53:55 +02001042 * @brief Parse must element.
1043 *
1044 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbc9ec9c2019-07-11 15:53:55 +02001045 * @param[in,out] restrs Restrictions to add to.
David Sedlákbc9ec9c2019-07-11 15:53:55 +02001046 * @return LY_ERR values.
1047 */
1048static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001049yin_parse_must(struct lysp_yin_ctx *ctx, struct lysp_restr **restrs)
David Sedlákbc9ec9c2019-07-11 15:53:55 +02001050{
1051 struct lysp_restr *restr;
1052
Michal Vaskob36053d2020-03-26 15:49:30 +01001053 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *restrs, restr, LY_EMEM);
1054 return yin_parse_restriction(ctx, LY_STMT_MUST, restr);
David Sedlákbc9ec9c2019-07-11 15:53:55 +02001055}
1056
1057/**
Michal Vasko7f45cf22020-10-01 12:49:44 +02001058 * @brief Parse a node id into an array.
1059 *
1060 * @param[in,out] ctx YIN parser context for logging and to store current state.
Michal Vasko193dacd2022-10-13 08:43:05 +02001061 * @param[in] parent_stmt Type of parent statement.
Michal Vasko7f45cf22020-10-01 12:49:44 +02001062 * @param[in] subinfo Information about subelement, is used to determin which function should be called and where to store parsed value.
1063 * @param[in,out] exts Extension instances to add to.
Michal Vasko7f45cf22020-10-01 12:49:44 +02001064 * @return LY_ERR values.
1065 */
1066static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001067yin_parse_qname(struct lysp_yin_ctx *ctx, enum ly_stmt parent_stmt, struct yin_subelement *subinfo,
Michal Vasko7f45cf22020-10-01 12:49:44 +02001068 struct lysp_ext_instance **exts)
1069{
1070 struct lysp_qname *qname, **qnames;
1071
Michal Vasko193dacd2022-10-13 08:43:05 +02001072 switch (parent_stmt) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02001073 case LY_STMT_DEFAULT:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001074 if (subinfo->flags & YIN_SUBELEM_UNIQUE) {
1075 qname = (struct lysp_qname *)subinfo->dest;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001076 } else {
1077 qnames = (struct lysp_qname **)subinfo->dest;
1078 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *qnames, qname, LY_EMEM);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001079 }
Michal Vasko8a67eff2021-12-07 14:04:47 +01001080 qname->mod = PARSER_CUR_PMOD(ctx);
Michal Vasko193dacd2022-10-13 08:43:05 +02001081 return yin_parse_simple_element(ctx, qname, parent_stmt, &qname->str, YIN_ARG_VALUE, Y_STR_ARG, exts);
Radek Išabe8a6532021-03-29 08:54:22 +02001082 case LY_STMT_UNIQUE:
1083 assert(!(subinfo->flags & YIN_SUBELEM_UNIQUE));
Michal Vasko193dacd2022-10-13 08:43:05 +02001084
Radek Išabe8a6532021-03-29 08:54:22 +02001085 qnames = (struct lysp_qname **)subinfo->dest;
1086 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *qnames, qname, LY_EMEM);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001087 qname->mod = PARSER_CUR_PMOD(ctx);
Michal Vasko193dacd2022-10-13 08:43:05 +02001088 return yin_parse_simple_element(ctx, *qnames, parent_stmt, &qname->str, YIN_ARG_TAG, Y_STR_ARG, exts);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001089 case LY_STMT_IF_FEATURE:
1090 assert(!(subinfo->flags & YIN_SUBELEM_UNIQUE));
Michal Vasko193dacd2022-10-13 08:43:05 +02001091
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001092 qnames = (struct lysp_qname **)subinfo->dest;
1093 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *qnames, qname, LY_EMEM);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001094 qname->mod = PARSER_CUR_PMOD(ctx);
Michal Vasko193dacd2022-10-13 08:43:05 +02001095 return yin_parse_simple_element(ctx, *qnames, parent_stmt, &qname->str, YIN_ARG_NAME, Y_STR_ARG, exts);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001096 default:
1097 break;
1098 }
1099
1100 LOGINT(ctx->xmlctx->ctx);
1101 return LY_EINT;
1102}
1103
1104/**
David Sedlák5545f5d2019-07-11 11:55:16 +02001105 * @brief Parse position or value element.
1106 *
1107 * @param[in,out] ctx YIN parser context for logging and to store current state.
Michal Vasko193dacd2022-10-13 08:43:05 +02001108 * @param[in] parent_stmt Type of parent statement.
David Sedlákbf8a2b72019-08-14 16:48:10 +02001109 * @param[out] enm Enum structure to save value, flags and extension instances.
David Sedlák5545f5d2019-07-11 11:55:16 +02001110 * @return LY_ERR values.
1111 */
1112static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001113yin_parse_value_pos(struct lysp_yin_ctx *ctx, enum ly_stmt parent_stmt, struct lysp_type_enum *enm)
David Sedlák5545f5d2019-07-11 11:55:16 +02001114{
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001115 LY_ERR ret = LY_SUCCESS;
David Sedlák5545f5d2019-07-11 11:55:16 +02001116 const char *temp_val = NULL;
1117 char *ptr;
Michal Vasko73d77ab2021-07-23 12:45:55 +02001118 long long int num = 0;
1119 unsigned long long int unum = 0;
David Sedlák5545f5d2019-07-11 11:55:16 +02001120
Michal Vasko193dacd2022-10-13 08:43:05 +02001121 assert(parent_stmt == LY_STMT_POSITION || parent_stmt == LY_STMT_VALUE);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001122
David Sedlák5545f5d2019-07-11 11:55:16 +02001123 /* set value flag */
1124 enm->flags |= LYS_SET_VALUE;
1125
1126 /* get attribute value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001127 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
Michal Vasko193dacd2022-10-13 08:43:05 +02001128 LY_CHECK_GOTO(ret = yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, parent_stmt), cleanup);
Michal Vasko69730152020-10-09 16:30:07 +02001129 if (!temp_val || (temp_val[0] == '\0') || (temp_val[0] == '+') ||
Michal Vasko193dacd2022-10-13 08:43:05 +02001130 ((temp_val[0] == '0') && (temp_val[1] != '\0')) || ((parent_stmt == LY_STMT_POSITION) && !strcmp(temp_val, "-0"))) {
1131 LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", lyplg_ext_stmt2str(parent_stmt));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001132 ret = LY_EVALID;
1133 goto cleanup;
David Sedlák5545f5d2019-07-11 11:55:16 +02001134 }
1135
1136 /* convert value */
1137 errno = 0;
Michal Vasko193dacd2022-10-13 08:43:05 +02001138 if (parent_stmt == LY_STMT_VALUE) {
Michal Vasko73d77ab2021-07-23 12:45:55 +02001139 num = strtoll(temp_val, &ptr, LY_BASE_DEC);
Michal Vasko69730152020-10-09 16:30:07 +02001140 if ((num < INT64_C(-2147483648)) || (num > INT64_C(2147483647))) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001141 LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", lyplg_ext_stmt2str(parent_stmt));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001142 ret = LY_EVALID;
1143 goto cleanup;
David Sedlák5545f5d2019-07-11 11:55:16 +02001144 }
1145 } else {
Michal Vasko73d77ab2021-07-23 12:45:55 +02001146 unum = strtoull(temp_val, &ptr, LY_BASE_DEC);
David Sedlák5545f5d2019-07-11 11:55:16 +02001147 if (unum > UINT64_C(4294967295)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001148 LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", lyplg_ext_stmt2str(parent_stmt));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001149 ret = LY_EVALID;
1150 goto cleanup;
David Sedlák5545f5d2019-07-11 11:55:16 +02001151 }
1152 }
1153 /* check if whole argument value was converted */
1154 if (*ptr != '\0') {
Michal Vasko193dacd2022-10-13 08:43:05 +02001155 LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", lyplg_ext_stmt2str(parent_stmt));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001156 ret = LY_EVALID;
1157 goto cleanup;
David Sedlák5545f5d2019-07-11 11:55:16 +02001158 }
1159 if (errno == ERANGE) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001160 LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_OOB_YIN, temp_val, "value", lyplg_ext_stmt2str(parent_stmt));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001161 ret = LY_EVALID;
1162 goto cleanup;
David Sedlák5545f5d2019-07-11 11:55:16 +02001163 }
1164 /* save correctly ternary operator can't be used because num and unum have different signes */
Michal Vasko193dacd2022-10-13 08:43:05 +02001165 if (parent_stmt == LY_STMT_VALUE) {
David Sedlák5545f5d2019-07-11 11:55:16 +02001166 enm->value = num;
1167 } else {
1168 enm->value = unum;
1169 }
David Sedlák5545f5d2019-07-11 11:55:16 +02001170
1171 /* parse subelements */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001172 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001173 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1174 };
Michal Vasko69730152020-10-09 16:30:07 +02001175
Michal Vasko193dacd2022-10-13 08:43:05 +02001176 LY_CHECK_GOTO(ret = yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), enm, parent_stmt, NULL, &enm->exts), cleanup);
David Sedlák5545f5d2019-07-11 11:55:16 +02001177
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001178 /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
1179 LY_CHECK_GOTO(ret = ly_set_add(&ctx->ext_inst, enm->exts, 1, NULL), cleanup);
1180
1181cleanup:
Michal Vaskoe180ed02021-02-05 16:31:20 +01001182 lydict_remove(ctx->xmlctx->ctx, temp_val);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001183 return ret;
David Sedlák5545f5d2019-07-11 11:55:16 +02001184}
1185
David Sedlák05404f62019-07-24 14:11:53 +02001186/**
1187 * @brief Parse belongs-to element.
1188 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001189 * @param[in] ctx YIN parser context for logging and to store current state.
David Sedlák05404f62019-07-24 14:11:53 +02001190 * @param[out] submod Structure of submodule that is being parsed.
1191 * @param[in,out] exts Extension instances to add to.
David Sedlák05404f62019-07-24 14:11:53 +02001192 * @return LY_ERR values
1193 */
1194static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001195yin_parse_belongs_to(struct lysp_yin_ctx *ctx, struct lysp_submodule *submod, struct lysp_ext_instance **exts)
David Sedlák05404f62019-07-24 14:11:53 +02001196{
Michal Vaskoc3781c32020-10-06 14:04:08 +02001197 const char *belongsto;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001198 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001199 {LY_STMT_PREFIX, &submod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
1200 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1201 };
David Sedlák05404f62019-07-24 14:11:53 +02001202
Michal Vaskob36053d2020-03-26 15:49:30 +01001203 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
Michal Vaskoc3781c32020-10-06 14:04:08 +02001204 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_MODULE, &belongsto, Y_IDENTIF_ARG, LY_STMT_BELONGS_TO));
Michal Vasko8a67eff2021-12-07 14:04:47 +01001205 if (PARSER_CUR_PMOD(ctx)->mod->name != belongsto) {
Michal Vaskoc3781c32020-10-06 14:04:08 +02001206 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YIN, "Submodule \"belongs-to\" value \"%s\" does not match its module name \"%s\".",
Michal Vasko8a67eff2021-12-07 14:04:47 +01001207 belongsto, PARSER_CUR_PMOD(ctx)->mod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02001208 lydict_remove(ctx->xmlctx->ctx, belongsto);
1209 return LY_EVALID;
1210 }
1211 lydict_remove(ctx->xmlctx->ctx, belongsto);
Michal Vaskob36053d2020-03-26 15:49:30 +01001212
Michal Vasko193dacd2022-10-13 08:43:05 +02001213 LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), submod, LY_STMT_BELONGS_TO, NULL, exts));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001214
1215 return LY_SUCCESS;
David Sedlák05404f62019-07-24 14:11:53 +02001216}
1217
David Sedlák5545f5d2019-07-11 11:55:16 +02001218/**
David Sedlákc1771b12019-07-10 15:55:46 +02001219 * @brief Function to parse meta tags (description, contact, ...) eg. elements with
David Sedlákbf8a2b72019-08-14 16:48:10 +02001220 * text element as child.
David Sedlákb4e44562019-07-04 15:42:12 +02001221 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001222 * @param[in,out] ctx YIN parser context for logging and to store current state.
Michal Vasko193dacd2022-10-13 08:43:05 +02001223 * @param[in] parent Current statement parent.
1224 * @param[in] parent_stmt Type of @p parent statement.
David Sedlákb4e44562019-07-04 15:42:12 +02001225 * @param[out] value Where the content of meta element should be stored.
David Sedlákbf8a2b72019-08-14 16:48:10 +02001226 * @param[in,out] exts Extension instances to add to.
David Sedlákb4e44562019-07-04 15:42:12 +02001227 * @return LY_ERR values.
1228 */
1229static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001230yin_parse_meta(struct lysp_yin_ctx *ctx, const void *parent, enum ly_stmt parent_stmt, const char **value,
1231 struct lysp_ext_instance **exts)
David Sedlákb4e44562019-07-04 15:42:12 +02001232{
Radek Krejcif13b87b2020-12-01 22:02:17 +01001233 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001234 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1235 {LY_STMT_ARG_TEXT, value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE | YIN_SUBELEM_FIRST}
1236 };
Michal Vasko69730152020-10-09 16:30:07 +02001237
Michal Vasko193dacd2022-10-13 08:43:05 +02001238 assert(parent_stmt == LY_STMT_ORGANIZATION || parent_stmt == LY_STMT_CONTACT || parent_stmt == LY_STMT_DESCRIPTION ||
1239 parent_stmt == LY_STMT_REFERENCE);
1240
David Sedlákdf2a9732019-08-07 13:23:16 +02001241 /* check attributes */
Michal Vaskob36053d2020-03-26 15:49:30 +01001242 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
Michal Vasko193dacd2022-10-13 08:43:05 +02001243 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NONE, NULL, Y_MAYBE_STR_ARG, parent_stmt));
David Sedlákb4e44562019-07-04 15:42:12 +02001244
David Sedlákdf2a9732019-08-07 13:23:16 +02001245 /* parse content */
Michal Vasko193dacd2022-10-13 08:43:05 +02001246 LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), parent, parent_stmt, NULL, exts));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001247
1248 return LY_SUCCESS;
David Sedlákb4e44562019-07-04 15:42:12 +02001249}
1250
1251/**
David Sedlákc1771b12019-07-10 15:55:46 +02001252 * @brief Parse error-message element.
1253 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001254 * @param[in,out] ctx YIN parser context for logging and to store current state.
Michal Vasko193dacd2022-10-13 08:43:05 +02001255 * @param[in] parent Current statement parent.
David Sedlákc1771b12019-07-10 15:55:46 +02001256 * @param[out] value Where the content of error-message element should be stored.
David Sedlákbf8a2b72019-08-14 16:48:10 +02001257 * @param[in,out] exts Extension instances to add to.
David Sedlákc1771b12019-07-10 15:55:46 +02001258 * @return LY_ERR values.
1259 */
1260static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001261yin_parse_err_msg(struct lysp_yin_ctx *ctx, const void *parent, const char **value, struct lysp_ext_instance **exts)
David Sedlákc1771b12019-07-10 15:55:46 +02001262{
Radek Krejcif13b87b2020-12-01 22:02:17 +01001263 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001264 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1265 {LY_STMT_ARG_VALUE, value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE | YIN_SUBELEM_FIRST}
1266 };
David Sedlákc1771b12019-07-10 15:55:46 +02001267
David Sedlákdf2a9732019-08-07 13:23:16 +02001268 /* check attributes */
Michal Vaskob36053d2020-03-26 15:49:30 +01001269 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1270 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 +02001271
Michal Vasko193dacd2022-10-13 08:43:05 +02001272 LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), parent, LY_STMT_ERROR_MESSAGE, NULL, exts));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001273
1274 return LY_SUCCESS;
David Sedlákc1771b12019-07-10 15:55:46 +02001275}
1276
1277/**
David Sedlák6542aed2019-08-14 10:47:43 +02001278 * @brief Parse type element.
David Sedlák374d2b32019-07-17 15:06:55 +02001279 *
1280 * @param[in,out] ctx YIN parser context for logging and to store current state.
Michal Vasko193dacd2022-10-13 08:43:05 +02001281 * @param[in] parent_stmt Type of parent statement.
1282 * @param[in,out] subinfo Information about subelement, is used to determin which function should be called and where
1283 * to store parsed value.
David Sedlák374d2b32019-07-17 15:06:55 +02001284 * @return LY_ERR values.
1285 */
1286static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001287yin_parse_type(struct lysp_yin_ctx *ctx, enum ly_stmt parent_stmt, struct yin_subelement *subinfo)
David Sedlák374d2b32019-07-17 15:06:55 +02001288{
David Sedlák6542aed2019-08-14 10:47:43 +02001289 struct lysp_type *type = NULL;
Michal Vasko69730152020-10-09 16:30:07 +02001290
Michal Vasko193dacd2022-10-13 08:43:05 +02001291 if (parent_stmt == LY_STMT_DEVIATE) {
David Sedlák6542aed2019-08-14 10:47:43 +02001292 *(struct lysp_type **)subinfo->dest = calloc(1, sizeof **(struct lysp_type **)subinfo->dest);
Michal Vaskob36053d2020-03-26 15:49:30 +01001293 LY_CHECK_ERR_RET(!(*(struct lysp_type **)subinfo->dest), LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
David Sedlák6542aed2019-08-14 10:47:43 +02001294 type = *((struct lysp_type **)subinfo->dest);
Michal Vaskod989ba02020-08-24 10:59:24 +02001295 } else {
David Sedlák6542aed2019-08-14 10:47:43 +02001296 type = (struct lysp_type *)subinfo->dest;
1297 }
Michal Vaskoe9c050f2020-10-06 14:01:23 +02001298
David Sedlák6542aed2019-08-14 10:47:43 +02001299 /* type as child of another type */
Michal Vasko193dacd2022-10-13 08:43:05 +02001300 if (parent_stmt == LY_STMT_TYPE) {
David Sedlák6542aed2019-08-14 10:47:43 +02001301 struct lysp_type *nested_type = NULL;
Michal Vasko26bbb272022-08-02 14:54:33 +02001302
Michal Vaskob36053d2020-03-26 15:49:30 +01001303 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, type->types, nested_type, LY_EMEM);
David Sedlák6542aed2019-08-14 10:47:43 +02001304 type->flags |= LYS_SET_TYPE;
1305 type = nested_type;
1306 }
Michal Vaskoe9c050f2020-10-06 14:01:23 +02001307
Michal Vasko8a67eff2021-12-07 14:04:47 +01001308 type->pmod = PARSER_CUR_PMOD(ctx);
Michal Vasko5bbcc242021-02-23 09:29:13 +01001309
Radek Krejcif13b87b2020-12-01 22:02:17 +01001310 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001311 {LY_STMT_BASE, type, 0},
1312 {LY_STMT_BIT, type, 0},
1313 {LY_STMT_ENUM, type, 0},
1314 {LY_STMT_FRACTION_DIGITS, type, YIN_SUBELEM_UNIQUE},
1315 {LY_STMT_LENGTH, type, YIN_SUBELEM_UNIQUE},
1316 {LY_STMT_PATH, type, YIN_SUBELEM_UNIQUE},
1317 {LY_STMT_PATTERN, type, 0},
1318 {LY_STMT_RANGE, type, YIN_SUBELEM_UNIQUE},
1319 {LY_STMT_REQUIRE_INSTANCE, type, YIN_SUBELEM_UNIQUE},
Michal Vasko608b0c02021-09-23 08:26:53 +02001320 {LY_STMT_TYPE, type, 0},
Radek Krejci0f969882020-08-21 16:56:47 +02001321 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1322 };
Michal Vaskob36053d2020-03-26 15:49:30 +01001323
1324 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1325 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &type->name, Y_PREF_IDENTIF_ARG, LY_STMT_TYPE));
Michal Vasko193dacd2022-10-13 08:43:05 +02001326 LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), type, LY_STMT_TYPE, NULL, &type->exts));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001327
1328 /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
1329 LY_CHECK_RET(ly_set_add(&ctx->ext_inst, type->exts, 1, NULL));
1330
1331 return LY_SUCCESS;
David Sedlák374d2b32019-07-17 15:06:55 +02001332}
1333
David Sedlák1af868e2019-07-17 17:03:14 +02001334/**
1335 * @brief Parse max-elements element.
1336 *
1337 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák1af868e2019-07-17 17:03:14 +02001338 * @param[in,out] max Value to write to.
David Sedláka2dad212019-07-18 12:45:19 +02001339 * @param[in] flags Flags to write to.
David Sedlák1af868e2019-07-17 17:03:14 +02001340 * @param[in,out] exts Extension instances to add to.
David Sedlák1af868e2019-07-17 17:03:14 +02001341 * @return LY_ERR values.
1342 */
1343static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001344yin_parse_maxelements(struct lysp_yin_ctx *ctx, uint32_t *max, uint16_t *flags, struct lysp_ext_instance **exts)
David Sedlák1af868e2019-07-17 17:03:14 +02001345{
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001346 LY_ERR ret = LY_SUCCESS;
David Sedlák1af868e2019-07-17 17:03:14 +02001347 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ák374d2b32019-07-17 15:06:55 +02001353
David Sedlák1af868e2019-07-17 17:03:14 +02001354 *flags |= LYS_SET_MAX;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001355 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
1356 LY_CHECK_GOTO(ret = yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_MAX_ELEMENTS), cleanup);
Michal Vasko69730152020-10-09 16:30:07 +02001357 if (!temp_val || (temp_val[0] == '\0') || (temp_val[0] == '0') || ((temp_val[0] != 'u') && !isdigit(temp_val[0]))) {
Michal Vaskod0625d72022-10-06 15:02:50 +02001358 LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001359 ret = LY_EVALID;
1360 goto cleanup;
David Sedlák1af868e2019-07-17 17:03:14 +02001361 }
1362
1363 if (strcmp(temp_val, "unbounded")) {
1364 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02001365 num = strtoull(temp_val, &ptr, LY_BASE_DEC);
David Sedlák1af868e2019-07-17 17:03:14 +02001366 if (*ptr != '\0') {
Michal Vaskod0625d72022-10-06 15:02:50 +02001367 LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001368 ret = LY_EVALID;
1369 goto cleanup;
David Sedlák1af868e2019-07-17 17:03:14 +02001370 }
1371 if ((errno == ERANGE) || (num > UINT32_MAX)) {
Michal Vaskod0625d72022-10-06 15:02:50 +02001372 LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_OOB_YIN, temp_val, "value", "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001373 ret = LY_EVALID;
1374 goto cleanup;
David Sedlák1af868e2019-07-17 17:03:14 +02001375 }
1376 *max = num;
1377 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001378
Michal Vasko193dacd2022-10-13 08:43:05 +02001379 ret = yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), max, LY_STMT_MAX_ELEMENTS, NULL, exts);
1380 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001381
1382cleanup:
Michal Vaskoe180ed02021-02-05 16:31:20 +01001383 lydict_remove(ctx->xmlctx->ctx, temp_val);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001384 return ret;
David Sedlák1af868e2019-07-17 17:03:14 +02001385}
David Sedlák374d2b32019-07-17 15:06:55 +02001386
1387/**
David Sedlákbf8a2b72019-08-14 16:48:10 +02001388 * @brief Parse min-elements element.
David Sedlák09e18c92019-07-18 11:17:11 +02001389 *
1390 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák09e18c92019-07-18 11:17:11 +02001391 * @param[in,out] min Value to write to.
David Sedláka2dad212019-07-18 12:45:19 +02001392 * @param[in] flags Flags to write to.
David Sedlák09e18c92019-07-18 11:17:11 +02001393 * @param[in,out] exts Extension instances to add to.
David Sedlák09e18c92019-07-18 11:17:11 +02001394 * @return LY_ERR values.
1395 */
1396static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001397yin_parse_minelements(struct lysp_yin_ctx *ctx, uint32_t *min, uint16_t *flags, struct lysp_ext_instance **exts)
David Sedlák09e18c92019-07-18 11:17:11 +02001398{
1399 const char *temp_val = NULL;
1400 char *ptr;
Michal Vasko73d77ab2021-07-23 12:45:55 +02001401 unsigned long long int num;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001402 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001403 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1404 };
David Sedlák09e18c92019-07-18 11:17:11 +02001405
1406 *flags |= LYS_SET_MIN;
Michal Vaskob36053d2020-03-26 15:49:30 +01001407 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1408 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 +02001409
Michal Vasko69730152020-10-09 16:30:07 +02001410 if (!temp_val || (temp_val[0] == '\0') || ((temp_val[0] == '0') && (temp_val[1] != '\0'))) {
Michal Vaskod0625d72022-10-06 15:02:50 +02001411 LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "min-elements");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001412 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlák09e18c92019-07-18 11:17:11 +02001413 return LY_EVALID;
1414 }
1415
1416 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02001417 num = strtoull(temp_val, &ptr, LY_BASE_DEC);
David Sedlák09e18c92019-07-18 11:17:11 +02001418 if (ptr[0] != 0) {
Michal Vaskod0625d72022-10-06 15:02:50 +02001419 LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "min-elements");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001420 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlák09e18c92019-07-18 11:17:11 +02001421 return LY_EVALID;
1422 }
Michal Vasko69730152020-10-09 16:30:07 +02001423 if ((errno == ERANGE) || (num > UINT32_MAX)) {
Michal Vaskod0625d72022-10-06 15:02:50 +02001424 LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_OOB_YIN, temp_val, "value", "min-elements");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001425 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlák09e18c92019-07-18 11:17:11 +02001426 return LY_EVALID;
1427 }
1428 *min = num;
Michal Vaskoe180ed02021-02-05 16:31:20 +01001429 lydict_remove(ctx->xmlctx->ctx, temp_val);
Michal Vasko193dacd2022-10-13 08:43:05 +02001430 LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), min, LY_STMT_MIN_ELEMENTS, NULL, exts));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001431
1432 return LY_SUCCESS;
David Sedlák09e18c92019-07-18 11:17:11 +02001433}
1434
David Sedláka2dad212019-07-18 12:45:19 +02001435/**
1436 * @brief Parse min-elements or max-elements element.
1437 *
1438 * @param[in,out] ctx YIN parser context for logging and to store current state.
Michal Vasko193dacd2022-10-13 08:43:05 +02001439 * @param[in] parent_stmt Type of parent statement.
1440 * @param[in] cur_stmt Type of current element.
David Sedláka2dad212019-07-18 12:45:19 +02001441 * @param[in] dest Where the parsed value and flags should be stored.
David Sedláka2dad212019-07-18 12:45:19 +02001442 * @return LY_ERR values.
1443 */
David Sedlák09e18c92019-07-18 11:17:11 +02001444static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001445yin_parse_minmax(struct lysp_yin_ctx *ctx, enum ly_stmt parent_stmt, enum ly_stmt cur_stmt, void *dest)
David Sedlák09e18c92019-07-18 11:17:11 +02001446{
David Sedlák09e18c92019-07-18 11:17:11 +02001447 uint32_t *lim;
1448 uint16_t *flags;
1449 struct lysp_ext_instance **exts;
1450
Michal Vasko193dacd2022-10-13 08:43:05 +02001451 assert(cur_stmt == LY_STMT_MAX_ELEMENTS || cur_stmt == LY_STMT_MIN_ELEMENTS);
1452 assert(parent_stmt == LY_STMT_LEAF_LIST || parent_stmt == LY_STMT_REFINE || parent_stmt == LY_STMT_LIST ||
1453 parent_stmt == LY_STMT_DEVIATE);
1454
1455 if (parent_stmt == LY_STMT_LEAF_LIST) {
1456 lim = (cur_stmt == LY_STMT_MAX_ELEMENTS) ? &((struct lysp_node_leaflist *)dest)->max : &((struct lysp_node_leaflist *)dest)->min;
David Sedlák09e18c92019-07-18 11:17:11 +02001457 flags = &((struct lysp_node_leaflist *)dest)->flags;
1458 exts = &((struct lysp_node_leaflist *)dest)->exts;
Michal Vasko193dacd2022-10-13 08:43:05 +02001459 } else if (parent_stmt == LY_STMT_REFINE) {
1460 lim = (cur_stmt == LY_STMT_MAX_ELEMENTS) ? &((struct lysp_refine *)dest)->max : &((struct lysp_refine *)dest)->min;
David Sedlák09e18c92019-07-18 11:17:11 +02001461 flags = &((struct lysp_refine *)dest)->flags;
1462 exts = &((struct lysp_refine *)dest)->exts;
Michal Vasko193dacd2022-10-13 08:43:05 +02001463 } else if (parent_stmt == LY_STMT_LIST) {
1464 lim = (cur_stmt == LY_STMT_MAX_ELEMENTS) ? &((struct lysp_node_list *)dest)->max : &((struct lysp_node_list *)dest)->min;
David Sedlák09e18c92019-07-18 11:17:11 +02001465 flags = &((struct lysp_node_list *)dest)->flags;
1466 exts = &((struct lysp_node_list *)dest)->exts;
David Sedlák4ffcec82019-07-25 15:10:21 +02001467 } else {
1468 lim = ((struct minmax_dev_meta *)dest)->lim;
1469 flags = ((struct minmax_dev_meta *)dest)->flags;
1470 exts = ((struct minmax_dev_meta *)dest)->exts;
David Sedlák09e18c92019-07-18 11:17:11 +02001471 }
1472
Michal Vasko193dacd2022-10-13 08:43:05 +02001473 if (cur_stmt == LY_STMT_MAX_ELEMENTS) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001474 LY_CHECK_RET(yin_parse_maxelements(ctx, lim, flags, exts));
David Sedlák09e18c92019-07-18 11:17:11 +02001475 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +01001476 LY_CHECK_RET(yin_parse_minelements(ctx, lim, flags, exts));
David Sedlák09e18c92019-07-18 11:17:11 +02001477 }
1478
1479 return LY_SUCCESS;
1480}
1481
1482/**
David Sedlákbf8a2b72019-08-14 16:48:10 +02001483 * @brief Parse ordered-by element.
David Sedláka2dad212019-07-18 12:45:19 +02001484 *
1485 * @param[in,out] ctx YIN parser context for logging and to store current state.
Michal Vasko193dacd2022-10-13 08:43:05 +02001486 * @param[in] parent Current statement parent.
David Sedláka2dad212019-07-18 12:45:19 +02001487 * @param[out] flags Flags to write to.
David Sedlákbf8a2b72019-08-14 16:48:10 +02001488 * @param[in,out] exts Extension instances to add to.
David Sedláka2dad212019-07-18 12:45:19 +02001489 * @return LY_ERR values.
1490 */
1491static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001492yin_parse_orderedby(struct lysp_yin_ctx *ctx, const void *parent, uint16_t *flags, struct lysp_ext_instance **exts)
David Sedláka2dad212019-07-18 12:45:19 +02001493{
1494 const char *temp_val;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001495 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001496 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1497 };
David Sedláka2dad212019-07-18 12:45:19 +02001498
Michal Vaskob36053d2020-03-26 15:49:30 +01001499 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1500 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 +02001501 if (strcmp(temp_val, "system") == 0) {
1502 *flags |= LYS_ORDBY_SYSTEM;
1503 } else if (strcmp(temp_val, "user") == 0) {
1504 *flags |= LYS_ORDBY_USER;
1505 } else {
Michal Vaskod0625d72022-10-06 15:02:50 +02001506 LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value",
Michal Vasko69730152020-10-09 16:30:07 +02001507 "ordered-by", "system", "user");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001508 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedláka2dad212019-07-18 12:45:19 +02001509 return LY_EVALID;
1510 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01001511 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedláka2dad212019-07-18 12:45:19 +02001512
Michal Vasko193dacd2022-10-13 08:43:05 +02001513 LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), parent, LY_STMT_ORDERED_BY, NULL, exts));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001514
1515 return LY_SUCCESS;
David Sedláka2dad212019-07-18 12:45:19 +02001516}
1517
1518/**
David Sedlákbf8a2b72019-08-14 16:48:10 +02001519 * @brief Parse any-data or any-xml element.
David Sedlák8a83bbb2019-07-18 14:46:00 +02001520 *
1521 * @param[in,out] ctx YIN parser context for logging and to store current state.
Michal Vasko193dacd2022-10-13 08:43:05 +02001522 * @param[in] any_stmt Type of current statement, can be set to LY_STMT_ANYDATA or LY_STMT_ANYXML
David Sedlákad83cf92019-08-13 12:53:53 +02001523 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlák8a83bbb2019-07-18 14:46:00 +02001524 * @return LY_ERR values.
1525 */
1526static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001527yin_parse_any(struct lysp_yin_ctx *ctx, enum ly_stmt any_kw, struct tree_node_meta *node_meta)
David Sedlák8a83bbb2019-07-18 14:46:00 +02001528{
David Sedlák8a83bbb2019-07-18 14:46:00 +02001529 struct lysp_node_anydata *any;
1530
David Sedlák8d552d62019-08-06 15:29:05 +02001531 /* create new sibling */
Michal Vaskob36053d2020-03-26 15:49:30 +01001532 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, any, next, LY_EMEM);
Radek Krejcid6b76452019-09-03 17:03:03 +02001533 any->nodetype = (any_kw == LY_STMT_ANYDATA) ? LYS_ANYDATA : LYS_ANYXML;
David Sedlák8a83bbb2019-07-18 14:46:00 +02001534 any->parent = node_meta->parent;
1535
David Sedlákbf8a2b72019-08-14 16:48:10 +02001536 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001537 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1538 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 +02001539
Radek Krejcif13b87b2020-12-01 22:02:17 +01001540 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001541 {LY_STMT_CONFIG, &any->flags, YIN_SUBELEM_UNIQUE},
1542 {LY_STMT_DESCRIPTION, &any->dsc, YIN_SUBELEM_UNIQUE},
1543 {LY_STMT_IF_FEATURE, &any->iffeatures, 0},
1544 {LY_STMT_MANDATORY, &any->flags, YIN_SUBELEM_UNIQUE},
1545 {LY_STMT_MUST, &any->musts, 0},
1546 {LY_STMT_REFERENCE, &any->ref, YIN_SUBELEM_UNIQUE},
1547 {LY_STMT_STATUS, &any->flags, YIN_SUBELEM_UNIQUE},
1548 {LY_STMT_WHEN, &any->when, YIN_SUBELEM_UNIQUE},
1549 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1550 };
Michal Vasko69730152020-10-09 16:30:07 +02001551
Michal Vasko193dacd2022-10-13 08:43:05 +02001552 LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), any, any_kw, NULL, &any->exts));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001553
1554 /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
1555 LY_CHECK_RET(ly_set_add(&ctx->ext_inst, any->exts, 1, NULL));
1556
1557 return LY_SUCCESS;
David Sedlák8a83bbb2019-07-18 14:46:00 +02001558}
1559
1560/**
David Sedlák203ca3a2019-07-18 15:26:25 +02001561 * @brief parse leaf element.
1562 *
1563 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákad83cf92019-08-13 12:53:53 +02001564 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlák203ca3a2019-07-18 15:26:25 +02001565 * @return LY_ERR values.
1566 */
1567static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001568yin_parse_leaf(struct lysp_yin_ctx *ctx, struct tree_node_meta *node_meta)
David Sedlák203ca3a2019-07-18 15:26:25 +02001569{
David Sedlák203ca3a2019-07-18 15:26:25 +02001570 struct lysp_node_leaf *leaf;
1571
David Sedlák8d552d62019-08-06 15:29:05 +02001572 /* create structure new leaf */
Michal Vaskob36053d2020-03-26 15:49:30 +01001573 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, leaf, next, LY_EMEM);
David Sedlák203ca3a2019-07-18 15:26:25 +02001574 leaf->nodetype = LYS_LEAF;
1575 leaf->parent = node_meta->parent;
1576
David Sedlák203ca3a2019-07-18 15:26:25 +02001577 /* parser argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001578 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1579 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 +02001580
1581 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001582 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001583 {LY_STMT_CONFIG, &leaf->flags, YIN_SUBELEM_UNIQUE},
1584 {LY_STMT_DEFAULT, &leaf->dflt, YIN_SUBELEM_UNIQUE},
1585 {LY_STMT_DESCRIPTION, &leaf->dsc, YIN_SUBELEM_UNIQUE},
1586 {LY_STMT_IF_FEATURE, &leaf->iffeatures, 0},
1587 {LY_STMT_MANDATORY, &leaf->flags, YIN_SUBELEM_UNIQUE},
1588 {LY_STMT_MUST, &leaf->musts, 0},
1589 {LY_STMT_REFERENCE, &leaf->ref, YIN_SUBELEM_UNIQUE},
1590 {LY_STMT_STATUS, &leaf->flags, YIN_SUBELEM_UNIQUE},
1591 {LY_STMT_TYPE, &leaf->type, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_MANDATORY},
1592 {LY_STMT_UNITS, &leaf->units, YIN_SUBELEM_UNIQUE},
1593 {LY_STMT_WHEN, &leaf->when, YIN_SUBELEM_UNIQUE},
1594 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1595 };
Michal Vasko69730152020-10-09 16:30:07 +02001596
Michal Vasko193dacd2022-10-13 08:43:05 +02001597 LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), leaf, LY_STMT_LEAF, NULL, &leaf->exts));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001598
1599 /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
1600 LY_CHECK_RET(ly_set_add(&ctx->ext_inst, leaf->exts, 1, NULL));
1601
1602 return LY_SUCCESS;
David Sedlák203ca3a2019-07-18 15:26:25 +02001603}
1604
1605/**
David Sedlákc3da3ef2019-07-19 12:56:08 +02001606 * @brief Parse leaf-list element.
1607 *
1608 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákad83cf92019-08-13 12:53:53 +02001609 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlákc3da3ef2019-07-19 12:56:08 +02001610 * @return LY_ERR values.
1611 */
1612static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001613yin_parse_leaflist(struct lysp_yin_ctx *ctx, struct tree_node_meta *node_meta)
David Sedlákc3da3ef2019-07-19 12:56:08 +02001614{
David Sedlákc3da3ef2019-07-19 12:56:08 +02001615 struct lysp_node_leaflist *llist;
David Sedlákc3da3ef2019-07-19 12:56:08 +02001616
Michal Vaskob36053d2020-03-26 15:49:30 +01001617 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, llist, next, LY_EMEM);
David Sedlák8d552d62019-08-06 15:29:05 +02001618
David Sedlákc3da3ef2019-07-19 12:56:08 +02001619 llist->nodetype = LYS_LEAFLIST;
1620 llist->parent = node_meta->parent;
1621
David Sedlákc3da3ef2019-07-19 12:56:08 +02001622 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001623 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1624 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 +02001625
1626 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001627 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001628 {LY_STMT_CONFIG, &llist->flags, YIN_SUBELEM_UNIQUE},
1629 {LY_STMT_DEFAULT, &llist->dflts, 0},
1630 {LY_STMT_DESCRIPTION, &llist->dsc, YIN_SUBELEM_UNIQUE},
1631 {LY_STMT_IF_FEATURE, &llist->iffeatures, 0},
1632 {LY_STMT_MAX_ELEMENTS, llist, YIN_SUBELEM_UNIQUE},
1633 {LY_STMT_MIN_ELEMENTS, llist, YIN_SUBELEM_UNIQUE},
1634 {LY_STMT_MUST, &llist->musts, 0},
1635 {LY_STMT_ORDERED_BY, &llist->flags, YIN_SUBELEM_UNIQUE},
1636 {LY_STMT_REFERENCE, &llist->ref, YIN_SUBELEM_UNIQUE},
1637 {LY_STMT_STATUS, &llist->flags, YIN_SUBELEM_UNIQUE},
1638 {LY_STMT_TYPE, &llist->type, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_MANDATORY},
1639 {LY_STMT_UNITS, &llist->units, YIN_SUBELEM_UNIQUE},
1640 {LY_STMT_WHEN, &llist->when, YIN_SUBELEM_UNIQUE},
1641 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1642 };
Michal Vasko69730152020-10-09 16:30:07 +02001643
Michal Vasko193dacd2022-10-13 08:43:05 +02001644 LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), llist, LY_STMT_LEAF_LIST, NULL, &llist->exts));
David Sedlákc3da3ef2019-07-19 12:56:08 +02001645
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001646 /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
1647 LY_CHECK_RET(ly_set_add(&ctx->ext_inst, llist->exts, 1, NULL));
1648
David Sedlákbf8a2b72019-08-14 16:48:10 +02001649 /* check invalid combination of subelements */
David Sedlákc3da3ef2019-07-19 12:56:08 +02001650 if ((llist->min) && (llist->dflts)) {
Michal Vaskod0625d72022-10-06 15:02:50 +02001651 LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INCHILDSTMSCOMB_YIN, "min-elements", "default", "leaf-list");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001652 return LY_EVALID;
1653 }
Michal Vasko69730152020-10-09 16:30:07 +02001654 if (llist->max && (llist->min > llist->max)) {
Michal Vaskod0625d72022-10-06 15:02:50 +02001655 LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INVAL_MINMAX, llist->min, llist->max);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001656 return LY_EVALID;
1657 }
1658
1659 return LY_SUCCESS;
1660}
1661
1662/**
David Sedlák04e17b22019-07-19 15:29:48 +02001663 * @brief Parse typedef element.
1664 *
1665 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák05404f62019-07-24 14:11:53 +02001666 * @param[in] typedef_meta Meta information about parent node and typedefs to add to.
David Sedlák04e17b22019-07-19 15:29:48 +02001667 * @return LY_ERR values.
1668 */
1669static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001670yin_parse_typedef(struct lysp_yin_ctx *ctx, struct tree_node_meta *typedef_meta)
David Sedlák04e17b22019-07-19 15:29:48 +02001671{
1672 struct lysp_tpdf *tpdf;
David Sedlákbf8a2b72019-08-14 16:48:10 +02001673 struct lysp_tpdf **tpdfs = (struct lysp_tpdf **)typedef_meta->nodes;
Michal Vasko69730152020-10-09 16:30:07 +02001674
Michal Vaskob36053d2020-03-26 15:49:30 +01001675 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *tpdfs, tpdf, LY_EMEM);
David Sedlák04e17b22019-07-19 15:29:48 +02001676
1677 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001678 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1679 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 +02001680
1681 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001682 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001683 {LY_STMT_DEFAULT, &tpdf->dflt, YIN_SUBELEM_UNIQUE},
1684 {LY_STMT_DESCRIPTION, &tpdf->dsc, YIN_SUBELEM_UNIQUE},
1685 {LY_STMT_REFERENCE, &tpdf->ref, YIN_SUBELEM_UNIQUE},
1686 {LY_STMT_STATUS, &tpdf->flags, YIN_SUBELEM_UNIQUE},
1687 {LY_STMT_TYPE, &tpdf->type, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_MANDATORY},
1688 {LY_STMT_UNITS, &tpdf->units, YIN_SUBELEM_UNIQUE},
1689 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1690 };
Michal Vasko69730152020-10-09 16:30:07 +02001691
Michal Vasko193dacd2022-10-13 08:43:05 +02001692 LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), tpdf, LY_STMT_TYPEDEF, NULL, &tpdf->exts));
David Sedlák04e17b22019-07-19 15:29:48 +02001693
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001694 /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
1695 LY_CHECK_RET(ly_set_add(&ctx->ext_inst, tpdf->exts, 1, NULL));
1696
David Sedlák04e17b22019-07-19 15:29:48 +02001697 /* store data for collision check */
aPiecek75b83a02021-06-29 10:34:38 +02001698 if (typedef_meta->parent) {
aPiecek8d4e75d2021-06-24 14:47:06 +02001699 assert(ctx->main_ctx);
1700 LY_CHECK_RET(ly_set_add(&ctx->main_ctx->tpdfs_nodes, typedef_meta->parent, 0, NULL));
David Sedlák04e17b22019-07-19 15:29:48 +02001701 }
1702
1703 return LY_SUCCESS;
1704}
1705
1706/**
David Sedlákd2d676a2019-07-22 11:28:19 +02001707 * @brief Parse refine element.
1708 *
1709 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákd2d676a2019-07-22 11:28:19 +02001710 * @param[in,out] refines Refines to add to.
David Sedlákd2d676a2019-07-22 11:28:19 +02001711 * @return LY_ERR values.
1712 */
1713static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001714yin_parse_refine(struct lysp_yin_ctx *ctx, struct lysp_refine **refines)
David Sedlákd2d676a2019-07-22 11:28:19 +02001715{
1716 struct lysp_refine *rf;
1717
1718 /* allocate new refine */
Michal Vaskob36053d2020-03-26 15:49:30 +01001719 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *refines, rf, LY_EMEM);
David Sedlákd2d676a2019-07-22 11:28:19 +02001720
1721 /* parse attribute */
Michal Vaskob36053d2020-03-26 15:49:30 +01001722 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1723 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_TARGET_NODE, &rf->nodeid, Y_STR_ARG, LY_STMT_REFINE));
1724 CHECK_NONEMPTY(ctx, strlen(rf->nodeid), "refine");
David Sedlákd2d676a2019-07-22 11:28:19 +02001725
1726 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001727 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001728 {LY_STMT_CONFIG, &rf->flags, YIN_SUBELEM_UNIQUE},
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001729 {LY_STMT_DEFAULT, &rf->dflts, 0},
Radek Krejci0f969882020-08-21 16:56:47 +02001730 {LY_STMT_DESCRIPTION, &rf->dsc, YIN_SUBELEM_UNIQUE},
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001731 {LY_STMT_IF_FEATURE, &rf->iffeatures, 0},
Radek Krejci0f969882020-08-21 16:56:47 +02001732 {LY_STMT_MANDATORY, &rf->flags, YIN_SUBELEM_UNIQUE},
1733 {LY_STMT_MAX_ELEMENTS, rf, YIN_SUBELEM_UNIQUE},
1734 {LY_STMT_MIN_ELEMENTS, rf, YIN_SUBELEM_UNIQUE},
1735 {LY_STMT_MUST, &rf->musts, 0},
1736 {LY_STMT_PRESENCE, &rf->presence, YIN_SUBELEM_UNIQUE},
1737 {LY_STMT_REFERENCE, &rf->ref, YIN_SUBELEM_UNIQUE},
1738 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1739 };
Michal Vasko69730152020-10-09 16:30:07 +02001740
Michal Vasko193dacd2022-10-13 08:43:05 +02001741 LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), rf, LY_STMT_REFINE, NULL, &rf->exts));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001742
1743 /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
1744 LY_CHECK_RET(ly_set_add(&ctx->ext_inst, rf->exts, 1, NULL));
1745
1746 return LY_SUCCESS;
David Sedlákd2d676a2019-07-22 11:28:19 +02001747}
1748
1749/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02001750 * @brief Parse uses element.
1751 *
1752 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákad83cf92019-08-13 12:53:53 +02001753 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlák0d6de5a2019-07-22 13:25:44 +02001754 * @return LY_ERR values.
1755 */
1756static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001757yin_parse_uses(struct lysp_yin_ctx *ctx, struct tree_node_meta *node_meta)
David Sedlák0d6de5a2019-07-22 13:25:44 +02001758{
David Sedlák0d6de5a2019-07-22 13:25:44 +02001759 struct lysp_node_uses *uses;
1760
David Sedlák8d552d62019-08-06 15:29:05 +02001761 /* create new uses */
Michal Vaskob36053d2020-03-26 15:49:30 +01001762 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, uses, next, LY_EMEM);
David Sedlák0d6de5a2019-07-22 13:25:44 +02001763 uses->nodetype = LYS_USES;
1764 uses->parent = node_meta->parent;
1765
David Sedlák0d6de5a2019-07-22 13:25:44 +02001766 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001767 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1768 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 +02001769
1770 /* parse content */
David Sedlák6881b512019-08-13 12:52:00 +02001771 struct tree_node_meta augments = {(struct lysp_node *)uses, (struct lysp_node **)&uses->augments};
Radek Krejcif13b87b2020-12-01 22:02:17 +01001772 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001773 {LY_STMT_AUGMENT, &augments, 0},
1774 {LY_STMT_DESCRIPTION, &uses->dsc, YIN_SUBELEM_UNIQUE},
1775 {LY_STMT_IF_FEATURE, &uses->iffeatures, 0},
1776 {LY_STMT_REFERENCE, &uses->ref, YIN_SUBELEM_UNIQUE},
1777 {LY_STMT_REFINE, &uses->refines, 0},
1778 {LY_STMT_STATUS, &uses->flags, YIN_SUBELEM_UNIQUE},
1779 {LY_STMT_WHEN, &uses->when, YIN_SUBELEM_UNIQUE},
1780 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1781 };
Michal Vasko69730152020-10-09 16:30:07 +02001782
Michal Vasko193dacd2022-10-13 08:43:05 +02001783 LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), uses, LY_STMT_USES, NULL, &uses->exts));
David Sedlák0d6de5a2019-07-22 13:25:44 +02001784
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001785 /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
1786 LY_CHECK_RET(ly_set_add(&ctx->ext_inst, uses->exts, 1, NULL));
1787
David Sedlák0d6de5a2019-07-22 13:25:44 +02001788 return LY_SUCCESS;
1789}
1790
1791/**
David Sedlákaa854b02019-07-22 14:17:10 +02001792 * @brief Parse revision element.
1793 *
1794 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákaa854b02019-07-22 14:17:10 +02001795 * @param[in,out] revs Parsed revisions to add to.
David Sedlákaa854b02019-07-22 14:17:10 +02001796 * @return LY_ERR values.
1797 */
1798static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001799yin_parse_revision(struct lysp_yin_ctx *ctx, struct lysp_revision **revs)
David Sedlákaa854b02019-07-22 14:17:10 +02001800{
1801 struct lysp_revision *rev;
1802 const char *temp_date = NULL;
1803
1804 /* allocate new reivison */
Michal Vaskob36053d2020-03-26 15:49:30 +01001805 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *revs, rev, LY_EMEM);
David Sedlákaa854b02019-07-22 14:17:10 +02001806
1807 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001808 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1809 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_DATE, &temp_date, Y_STR_ARG, LY_STMT_REVISION));
David Sedlákaa854b02019-07-22 14:17:10 +02001810 /* check value */
Michal Vaskod0625d72022-10-06 15:02:50 +02001811 if (lysp_check_date((struct lysp_ctx *)ctx, temp_date, strlen(temp_date), "revision")) {
Michal Vaskoe180ed02021-02-05 16:31:20 +01001812 lydict_remove(ctx->xmlctx->ctx, temp_date);
David Sedlákaa854b02019-07-22 14:17:10 +02001813 return LY_EVALID;
1814 }
Radek Krejci36642162021-01-12 16:49:22 +01001815 memcpy(rev->date, temp_date, LY_REV_SIZE);
Michal Vaskoe180ed02021-02-05 16:31:20 +01001816 lydict_remove(ctx->xmlctx->ctx, temp_date);
David Sedlákaa854b02019-07-22 14:17:10 +02001817
1818 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001819 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001820 {LY_STMT_DESCRIPTION, &rev->dsc, YIN_SUBELEM_UNIQUE},
1821 {LY_STMT_REFERENCE, &rev->ref, YIN_SUBELEM_UNIQUE},
1822 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1823 };
Michal Vasko69730152020-10-09 16:30:07 +02001824
Michal Vasko193dacd2022-10-13 08:43:05 +02001825 LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), rev, LY_STMT_REVISION, NULL, &rev->exts));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001826
1827 /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
1828 LY_CHECK_RET(ly_set_add(&ctx->ext_inst, rev->exts, 1, NULL));
1829
1830 return LY_SUCCESS;
David Sedlákaa854b02019-07-22 14:17:10 +02001831}
1832
David Sedlák5e13dea2019-07-22 16:06:45 +02001833/**
1834 * @brief Parse include element.
1835 *
1836 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák5e13dea2019-07-22 16:06:45 +02001837 * @param[in,out] inc_meta Meta informatinou about module/submodule name and includes to add to.
David Sedlák5e13dea2019-07-22 16:06:45 +02001838 * @return LY_ERR values.
1839 */
David Sedlák0c2bab92019-07-22 15:33:19 +02001840static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001841yin_parse_include(struct lysp_yin_ctx *ctx, struct include_meta *inc_meta)
David Sedlák0c2bab92019-07-22 15:33:19 +02001842{
1843 struct lysp_include *inc;
1844
1845 /* allocate new include */
Michal Vaskob36053d2020-03-26 15:49:30 +01001846 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *inc_meta->includes, inc, LY_EMEM);
David Sedlák0c2bab92019-07-22 15:33:19 +02001847
1848 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001849 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1850 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 +02001851
1852 /* submodules share the namespace with the module names, so there must not be
1853 * a module of the same name in the context, no need for revision matching */
Michal Vaskob36053d2020-03-26 15:49:30 +01001854 if (!strcmp(inc_meta->name, inc->name) || ly_ctx_get_module_latest(ctx->xmlctx->ctx, inc->name)) {
Michal Vaskod0625d72022-10-06 15:02:50 +02001855 LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_NAME2_COL, "module", "submodule", inc->name);
David Sedlák0c2bab92019-07-22 15:33:19 +02001856 return LY_EVALID;
1857 }
1858
1859 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001860 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001861 {LY_STMT_DESCRIPTION, &inc->dsc, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_VER2},
1862 {LY_STMT_REFERENCE, &inc->ref, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_VER2},
1863 {LY_STMT_REVISION_DATE, &inc->rev, YIN_SUBELEM_UNIQUE},
1864 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1865 };
Michal Vasko69730152020-10-09 16:30:07 +02001866
Michal Vasko193dacd2022-10-13 08:43:05 +02001867 LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), inc, LY_STMT_INCLUDE, NULL, &inc->exts));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001868
1869 /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
1870 LY_CHECK_RET(ly_set_add(&ctx->ext_inst, inc->exts, 1, NULL));
1871
1872 return LY_SUCCESS;
David Sedlák0c2bab92019-07-22 15:33:19 +02001873}
1874
David Sedlákaa854b02019-07-22 14:17:10 +02001875/**
David Sedlákbf8a2b72019-08-14 16:48:10 +02001876 * @brief Parse revision-date element.
David Sedlákdfbbb442019-08-06 16:33:21 +02001877 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001878 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001879 * @param[in,out] rev Array to store the parsed value in.
1880 * @param[in,out] exts Extension instances to add to.
David Sedlákdfbbb442019-08-06 16:33:21 +02001881 * @return LY_ERR values.
1882 */
1883static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001884yin_parse_revision_date(struct lysp_yin_ctx *ctx, char *rev, struct lysp_ext_instance **exts)
David Sedlákdfbbb442019-08-06 16:33:21 +02001885{
1886 const char *temp_rev;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001887 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001888 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1889 };
David Sedlákdfbbb442019-08-06 16:33:21 +02001890
Michal Vaskob36053d2020-03-26 15:49:30 +01001891 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1892 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_DATE, &temp_rev, Y_STR_ARG, LY_STMT_REVISION_DATE));
Michal Vaskod0625d72022-10-06 15:02:50 +02001893 LY_CHECK_ERR_RET(lysp_check_date((struct lysp_ctx *)ctx, temp_rev, strlen(temp_rev), "revision-date") != LY_SUCCESS,
Michal Vaskoe180ed02021-02-05 16:31:20 +01001894 lydict_remove(ctx->xmlctx->ctx, temp_rev), LY_EVALID);
David Sedlákdfbbb442019-08-06 16:33:21 +02001895
1896 strcpy(rev, temp_rev);
Michal Vaskoe180ed02021-02-05 16:31:20 +01001897 lydict_remove(ctx->xmlctx->ctx, temp_rev);
David Sedlákdfbbb442019-08-06 16:33:21 +02001898
Michal Vasko193dacd2022-10-13 08:43:05 +02001899 LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), rev, LY_STMT_REVISION_DATE, NULL, exts));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001900
1901 return LY_SUCCESS;
David Sedlákdfbbb442019-08-06 16:33:21 +02001902}
1903
1904/**
1905 * @brief Parse config element.
1906 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001907 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001908 * @param[in,out] flags Flags to add to.
1909 * @param[in,out] exts Extension instances to add to.
David Sedlákdfbbb442019-08-06 16:33:21 +02001910 * @return LY_ERR values.
1911 */
1912static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001913yin_parse_config(struct lysp_yin_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
David Sedlákdfbbb442019-08-06 16:33:21 +02001914{
1915 const char *temp_val = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001916 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001917 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1918 };
David Sedlákdfbbb442019-08-06 16:33:21 +02001919
Michal Vaskob36053d2020-03-26 15:49:30 +01001920 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1921 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 +02001922 if (strcmp(temp_val, "true") == 0) {
1923 *flags |= LYS_CONFIG_W;
1924 } else if (strcmp(temp_val, "false") == 0) {
1925 *flags |= LYS_CONFIG_R;
1926 } else {
Michal Vaskod0625d72022-10-06 15:02:50 +02001927 LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value", "config",
Michal Vasko69730152020-10-09 16:30:07 +02001928 "true", "false");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001929 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákdfbbb442019-08-06 16:33:21 +02001930 return LY_EVALID;
1931 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01001932 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákdfbbb442019-08-06 16:33:21 +02001933
Michal Vasko193dacd2022-10-13 08:43:05 +02001934 LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), flags, LY_STMT_CONFIG, NULL, exts));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001935
1936 return LY_SUCCESS;
David Sedlákdfbbb442019-08-06 16:33:21 +02001937}
1938
1939/**
1940 * @brief Parse yang-version element.
1941 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001942 * @param[in,out] ctx YIN parser context for logging and to store current state.
Michal Vasko193dacd2022-10-13 08:43:05 +02001943 * @param[in] parent Current statement parent.
David Sedlákdfbbb442019-08-06 16:33:21 +02001944 * @param[out] version Storage for the parsed information.
David Sedlákbf8a2b72019-08-14 16:48:10 +02001945 * @param[in,out] exts Extension instances to add to.
David Sedlákdfbbb442019-08-06 16:33:21 +02001946 * @return LY_ERR values.
1947 */
1948static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001949yin_parse_yangversion(struct lysp_yin_ctx *ctx, const void *parent, uint8_t *version, struct lysp_ext_instance **exts)
David Sedlákdfbbb442019-08-06 16:33:21 +02001950{
1951 const char *temp_version = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001952 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001953 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1954 };
David Sedlákdfbbb442019-08-06 16:33:21 +02001955
Michal Vaskob36053d2020-03-26 15:49:30 +01001956 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1957 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 +02001958 if (strcmp(temp_version, "1") == 0) {
David Sedlákdfbbb442019-08-06 16:33:21 +02001959 *version = LYS_VERSION_1_0;
1960 } else if (strcmp(temp_version, "1.1") == 0) {
1961 *version = LYS_VERSION_1_1;
1962 } else {
Michal Vaskod0625d72022-10-06 15:02:50 +02001963 LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_version, "value",
Michal Vasko69730152020-10-09 16:30:07 +02001964 "yang-version", "1", "1.1");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001965 lydict_remove(ctx->xmlctx->ctx, temp_version);
David Sedlákdfbbb442019-08-06 16:33:21 +02001966 return LY_EVALID;
1967 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01001968 lydict_remove(ctx->xmlctx->ctx, temp_version);
David Sedlákdfbbb442019-08-06 16:33:21 +02001969
Michal Vasko193dacd2022-10-13 08:43:05 +02001970 LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), parent, LY_STMT_YANG_VERSION, NULL, exts));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001971
1972 return LY_SUCCESS;
David Sedlákdfbbb442019-08-06 16:33:21 +02001973}
1974
1975/**
1976 * @brief Parse import element.
1977 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001978 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001979 * @param[in,out] imp_meta Meta information about prefix and imports to add to.
David Sedlákdfbbb442019-08-06 16:33:21 +02001980 * @return LY_ERR values.
1981 */
1982static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001983yin_parse_import(struct lysp_yin_ctx *ctx, struct import_meta *imp_meta)
David Sedlákdfbbb442019-08-06 16:33:21 +02001984{
1985 struct lysp_import *imp;
Michal Vasko69730152020-10-09 16:30:07 +02001986
David Sedlákdfbbb442019-08-06 16:33:21 +02001987 /* allocate new element in sized array for import */
Michal Vaskob36053d2020-03-26 15:49:30 +01001988 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *imp_meta->imports, imp, LY_EMEM);
David Sedlákdfbbb442019-08-06 16:33:21 +02001989
Radek Krejcif13b87b2020-12-01 22:02:17 +01001990 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001991 {LY_STMT_DESCRIPTION, &imp->dsc, YIN_SUBELEM_UNIQUE},
1992 {LY_STMT_PREFIX, &imp->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
1993 {LY_STMT_REFERENCE, &imp->ref, YIN_SUBELEM_UNIQUE},
1994 {LY_STMT_REVISION_DATE, imp->rev, YIN_SUBELEM_UNIQUE},
1995 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1996 };
David Sedlákdfbbb442019-08-06 16:33:21 +02001997
1998 /* parse import attributes */
Michal Vaskob36053d2020-03-26 15:49:30 +01001999 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2000 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_MODULE, &imp->name, Y_IDENTIF_ARG, LY_STMT_IMPORT));
Michal Vasko193dacd2022-10-13 08:43:05 +02002001 LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), imp, LY_STMT_IMPORT, NULL, &imp->exts));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002002
2003 /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
2004 LY_CHECK_RET(ly_set_add(&ctx->ext_inst, imp->exts, 1, NULL));
2005
David Sedlákdfbbb442019-08-06 16:33:21 +02002006 /* check prefix validity */
Michal Vaskod0625d72022-10-06 15:02:50 +02002007 LY_CHECK_RET(lysp_check_prefix((struct lysp_ctx *)ctx, *imp_meta->imports, imp_meta->prefix, &imp->prefix), LY_EVALID);
David Sedlákdfbbb442019-08-06 16:33:21 +02002008
2009 return LY_SUCCESS;
2010}
2011
2012/**
2013 * @brief Parse mandatory element.
2014 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02002015 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02002016 * @param[in,out] flags Flags to add to.
2017 * @param[in,out] exts Extension instances to add to.
David Sedlákdfbbb442019-08-06 16:33:21 +02002018 * @return LY_ERR values.
2019 */
2020static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002021yin_parse_mandatory(struct lysp_yin_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
David Sedlákdfbbb442019-08-06 16:33:21 +02002022{
2023 const char *temp_val = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002024 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002025 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
2026 };
David Sedlákdfbbb442019-08-06 16:33:21 +02002027
Michal Vaskob36053d2020-03-26 15:49:30 +01002028 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2029 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 +02002030 if (strcmp(temp_val, "true") == 0) {
2031 *flags |= LYS_MAND_TRUE;
2032 } else if (strcmp(temp_val, "false") == 0) {
2033 *flags |= LYS_MAND_FALSE;
2034 } else {
Michal Vaskod0625d72022-10-06 15:02:50 +02002035 LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value",
Michal Vasko69730152020-10-09 16:30:07 +02002036 "mandatory", "true", "false");
Michal Vaskoe180ed02021-02-05 16:31:20 +01002037 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákdfbbb442019-08-06 16:33:21 +02002038 return LY_EVALID;
2039 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01002040 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákdfbbb442019-08-06 16:33:21 +02002041
Michal Vasko193dacd2022-10-13 08:43:05 +02002042 LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), flags, LY_STMT_MANDATORY, NULL, exts));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002043
2044 return LY_SUCCESS;
David Sedlákdfbbb442019-08-06 16:33:21 +02002045}
2046
2047/**
2048 * @brief Parse status element.
2049 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02002050 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02002051 * @param[in,out] flags Flags to add to.
2052 * @param[in,out] exts Extension instances to add to.
David Sedlákdfbbb442019-08-06 16:33:21 +02002053 * @return LY_ERR values.
2054 */
2055static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002056yin_parse_status(struct lysp_yin_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
David Sedlákdfbbb442019-08-06 16:33:21 +02002057{
2058 const char *value = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002059 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002060 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
2061 };
David Sedlákdfbbb442019-08-06 16:33:21 +02002062
Michal Vaskob36053d2020-03-26 15:49:30 +01002063 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2064 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 +02002065 if (strcmp(value, "current") == 0) {
2066 *flags |= LYS_STATUS_CURR;
2067 } else if (strcmp(value, "deprecated") == 0) {
2068 *flags |= LYS_STATUS_DEPRC;
2069 } else if (strcmp(value, "obsolete") == 0) {
2070 *flags |= LYS_STATUS_OBSLT;
2071 } else {
Michal Vaskod0625d72022-10-06 15:02:50 +02002072 LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS3, value, "value",
Michal Vasko69730152020-10-09 16:30:07 +02002073 "status", "current", "deprecated", "obsolete");
Michal Vaskoe180ed02021-02-05 16:31:20 +01002074 lydict_remove(ctx->xmlctx->ctx, value);
David Sedlákdfbbb442019-08-06 16:33:21 +02002075 return LY_EVALID;
2076 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01002077 lydict_remove(ctx->xmlctx->ctx, value);
David Sedlákdfbbb442019-08-06 16:33:21 +02002078
Michal Vasko193dacd2022-10-13 08:43:05 +02002079 LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), flags, LY_STMT_STATUS, NULL, exts));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002080
2081 return LY_SUCCESS;
David Sedlákdfbbb442019-08-06 16:33:21 +02002082}
2083
2084/**
2085 * @brief Parse when element.
2086 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02002087 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02002088 * @param[out] when_p When pointer to parse to.
Michal Vasko193dacd2022-10-13 08:43:05 +02002089 * @return LY_ERR values.
David Sedlákdfbbb442019-08-06 16:33:21 +02002090 */
2091static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002092yin_parse_when(struct lysp_yin_ctx *ctx, struct lysp_when **when_p)
David Sedlákdfbbb442019-08-06 16:33:21 +02002093{
2094 struct lysp_when *when;
David Sedláka56e0012019-08-15 13:21:25 +02002095 LY_ERR ret = LY_SUCCESS;
2096
David Sedlákdfbbb442019-08-06 16:33:21 +02002097 when = calloc(1, sizeof *when);
Michal Vaskob36053d2020-03-26 15:49:30 +01002098 LY_CHECK_ERR_RET(!when, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
Radek Krejcif6923e82020-07-02 16:36:53 +02002099
2100 ret = lyxml_ctx_next(ctx->xmlctx);
2101 LY_CHECK_ERR_RET(ret, free(when), ret);
2102
Michal Vaskob36053d2020-03-26 15:49:30 +01002103 ret = yin_parse_attribute(ctx, YIN_ARG_CONDITION, &when->cond, Y_STR_ARG, LY_STMT_WHEN);
David Sedláka56e0012019-08-15 13:21:25 +02002104 LY_CHECK_ERR_RET(ret, free(when), ret);
2105
David Sedlákdfbbb442019-08-06 16:33:21 +02002106 *when_p = when;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002107 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002108 {LY_STMT_DESCRIPTION, &when->dsc, YIN_SUBELEM_UNIQUE},
2109 {LY_STMT_REFERENCE, &when->ref, YIN_SUBELEM_UNIQUE},
2110 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
2111 };
David Sedlákdfbbb442019-08-06 16:33:21 +02002112
Michal Vasko193dacd2022-10-13 08:43:05 +02002113 LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), when, LY_STMT_WHEN, NULL, &when->exts));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002114
2115 /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
2116 LY_CHECK_RET(ly_set_add(&ctx->ext_inst, when->exts, 1, NULL));
2117
2118 return LY_SUCCESS;
David Sedlákdfbbb442019-08-06 16:33:21 +02002119}
2120
2121/**
2122 * @brief Parse yin-elemenet element.
2123 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02002124 * @param[in,out] ctx YIN parser context for logging and to store current state.
Michal Vasko193dacd2022-10-13 08:43:05 +02002125 * @param[in] parent Current statement parent.
David Sedlákdfbbb442019-08-06 16:33:21 +02002126 * @param[in,out] flags Flags to add to.
Michal Vasko193dacd2022-10-13 08:43:05 +02002127 * @param[in,out] exts Extension instances to add to.
David Sedlákdfbbb442019-08-06 16:33:21 +02002128 * @return LY_ERR values.
2129 */
2130static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02002131yin_parse_yin_element(struct lysp_yin_ctx *ctx, const void *parent, uint16_t *flags, struct lysp_ext_instance **exts)
David Sedlákdfbbb442019-08-06 16:33:21 +02002132{
2133 const char *temp_val = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002134 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002135 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
2136 };
David Sedlákdfbbb442019-08-06 16:33:21 +02002137
Michal Vaskob36053d2020-03-26 15:49:30 +01002138 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2139 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 +02002140 if (strcmp(temp_val, "true") == 0) {
2141 *flags |= LYS_YINELEM_TRUE;
2142 } else if (strcmp(temp_val, "false") == 0) {
2143 *flags |= LYS_YINELEM_FALSE;
2144 } else {
Michal Vaskod0625d72022-10-06 15:02:50 +02002145 LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value",
Michal Vasko69730152020-10-09 16:30:07 +02002146 "yin-element", "true", "false");
Michal Vaskoe180ed02021-02-05 16:31:20 +01002147 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákdfbbb442019-08-06 16:33:21 +02002148 return LY_EVALID;
2149 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01002150 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákdfbbb442019-08-06 16:33:21 +02002151
Michal Vasko193dacd2022-10-13 08:43:05 +02002152 LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), parent, LY_STMT_YIN_ELEMENT, NULL, exts));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002153
2154 return LY_SUCCESS;
David Sedlákdfbbb442019-08-06 16:33:21 +02002155}
2156
2157/**
2158 * @brief Parse argument element.
2159 *
Michal Vasko193dacd2022-10-13 08:43:05 +02002160 * @param[in,out] ctx YIN parser context for logging and to store current state.
2161 * @param[in] parent Current statement parent.
2162 * @param[in,out] arg_meta Meta information about destination of parsed data.
David Sedlákbf8a2b72019-08-14 16:48:10 +02002163 * @param[in,out] exts Extension instances to add to.
David Sedlákdfbbb442019-08-06 16:33:21 +02002164 * @return LY_ERR values.
2165 */
2166static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02002167yin_parse_argument(struct lysp_yin_ctx *ctx, const void *parent, struct yin_argument_meta *arg_meta, struct lysp_ext_instance **exts)
David Sedlákdfbbb442019-08-06 16:33:21 +02002168{
Radek Krejcif13b87b2020-12-01 22:02:17 +01002169 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002170 {LY_STMT_YIN_ELEMENT, arg_meta->flags, YIN_SUBELEM_UNIQUE},
2171 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
2172 };
David Sedlákdfbbb442019-08-06 16:33:21 +02002173
Michal Vaskob36053d2020-03-26 15:49:30 +01002174 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2175 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 +02002176
Michal Vasko193dacd2022-10-13 08:43:05 +02002177 LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), parent, LY_STMT_ARGUMENT, NULL, exts));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002178
2179 return LY_SUCCESS;
David Sedlákdfbbb442019-08-06 16:33:21 +02002180}
2181
2182/**
2183 * @brief Parse the extension statement.
2184 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02002185 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02002186 * @param[in,out] extensions Extensions to add to.
David Sedlákdfbbb442019-08-06 16:33:21 +02002187 * @return LY_ERR values.
2188 */
2189static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002190yin_parse_extension(struct lysp_yin_ctx *ctx, struct lysp_ext **extensions)
David Sedlákdfbbb442019-08-06 16:33:21 +02002191{
2192 struct lysp_ext *ex;
Michal Vasko69730152020-10-09 16:30:07 +02002193
Michal Vaskob36053d2020-03-26 15:49:30 +01002194 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *extensions, ex, LY_EMEM);
2195 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2196 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 +02002197
Radek Krejci9f87b0c2021-03-05 14:45:26 +01002198 struct yin_argument_meta arg_info = {&ex->flags, &ex->argname};
Radek Krejcif13b87b2020-12-01 22:02:17 +01002199 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002200 {LY_STMT_ARGUMENT, &arg_info, YIN_SUBELEM_UNIQUE},
2201 {LY_STMT_DESCRIPTION, &ex->dsc, YIN_SUBELEM_UNIQUE},
2202 {LY_STMT_REFERENCE, &ex->ref, YIN_SUBELEM_UNIQUE},
2203 {LY_STMT_STATUS, &ex->flags, YIN_SUBELEM_UNIQUE},
2204 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
2205 };
David Sedlákdfbbb442019-08-06 16:33:21 +02002206
Michal Vasko193dacd2022-10-13 08:43:05 +02002207 LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), ex, LY_STMT_EXTENSION, NULL, &ex->exts));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002208
2209 /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
2210 LY_CHECK_RET(ly_set_add(&ctx->ext_inst, ex->exts, 1, NULL));
2211
2212 return LY_SUCCESS;
David Sedlákdfbbb442019-08-06 16:33:21 +02002213}
2214
2215/**
David Sedlák5e13dea2019-07-22 16:06:45 +02002216 * @brief Parse feature element.
2217 *
2218 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák5e13dea2019-07-22 16:06:45 +02002219 * @param[in,out] features Features to add to.
David Sedlák5e13dea2019-07-22 16:06:45 +02002220 * @return LY_ERR values.
2221 */
2222static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002223yin_parse_feature(struct lysp_yin_ctx *ctx, struct lysp_feature **features)
David Sedlák5e13dea2019-07-22 16:06:45 +02002224{
2225 struct lysp_feature *feat;
2226
2227 /* allocate new feature */
Michal Vaskob36053d2020-03-26 15:49:30 +01002228 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *features, feat, LY_EMEM);
David Sedlák5e13dea2019-07-22 16:06:45 +02002229
2230 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002231 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2232 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 +02002233
2234 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002235 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002236 {LY_STMT_DESCRIPTION, &feat->dsc, YIN_SUBELEM_UNIQUE},
2237 {LY_STMT_IF_FEATURE, &feat->iffeatures, 0},
2238 {LY_STMT_REFERENCE, &feat->ref, YIN_SUBELEM_UNIQUE},
2239 {LY_STMT_STATUS, &feat->flags, YIN_SUBELEM_UNIQUE},
2240 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
2241 };
Michal Vasko69730152020-10-09 16:30:07 +02002242
Michal Vasko193dacd2022-10-13 08:43:05 +02002243 LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), feat, LY_STMT_FEATURE, NULL, &feat->exts));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002244
2245 /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
2246 LY_CHECK_RET(ly_set_add(&ctx->ext_inst, feat->exts, 1, NULL));
2247
2248 return LY_SUCCESS;
David Sedlák5e13dea2019-07-22 16:06:45 +02002249}
2250
2251/**
David Sedlák28794f22019-07-22 16:45:00 +02002252 * @brief Parse identity element.
2253 *
2254 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák28794f22019-07-22 16:45:00 +02002255 * @param[in,out] identities Identities to add to.
David Sedlák28794f22019-07-22 16:45:00 +02002256 * @return LY_ERR values.
2257 */
2258static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002259yin_parse_identity(struct lysp_yin_ctx *ctx, struct lysp_ident **identities)
David Sedlák28794f22019-07-22 16:45:00 +02002260{
2261 struct lysp_ident *ident;
2262
2263 /* allocate new identity */
Michal Vaskob36053d2020-03-26 15:49:30 +01002264 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *identities, ident, LY_EMEM);
David Sedlák28794f22019-07-22 16:45:00 +02002265
2266 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002267 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2268 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 +02002269
2270 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002271 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002272 {LY_STMT_BASE, &ident->bases, 0},
2273 {LY_STMT_DESCRIPTION, &ident->dsc, YIN_SUBELEM_UNIQUE},
2274 {LY_STMT_IF_FEATURE, &ident->iffeatures, YIN_SUBELEM_VER2},
2275 {LY_STMT_REFERENCE, &ident->ref, YIN_SUBELEM_UNIQUE},
2276 {LY_STMT_STATUS, &ident->flags, YIN_SUBELEM_UNIQUE},
2277 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
2278 };
Michal Vasko69730152020-10-09 16:30:07 +02002279
Michal Vasko193dacd2022-10-13 08:43:05 +02002280 LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), ident, LY_STMT_IDENTITY, NULL, &ident->exts));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002281
2282 /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
2283 LY_CHECK_RET(ly_set_add(&ctx->ext_inst, ident->exts, 1, NULL));
2284
2285 return LY_SUCCESS;
David Sedlák28794f22019-07-22 16:45:00 +02002286}
2287
2288/**
David Sedlákaf536aa2019-07-23 13:42:23 +02002289 * @brief Parse list element.
2290 *
2291 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákad83cf92019-08-13 12:53:53 +02002292 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlákaf536aa2019-07-23 13:42:23 +02002293 * @return LY_ERR values.
2294 */
2295static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002296yin_parse_list(struct lysp_yin_ctx *ctx, struct tree_node_meta *node_meta)
David Sedlákaf536aa2019-07-23 13:42:23 +02002297{
David Sedlákaf536aa2019-07-23 13:42:23 +02002298 struct lysp_node_list *list;
David Sedlák81497a32019-08-13 16:56:26 +02002299 LY_ERR ret = LY_SUCCESS;
2300 struct yin_subelement *subelems = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002301 size_t subelems_size;
David Sedlákaf536aa2019-07-23 13:42:23 +02002302
Michal Vaskob36053d2020-03-26 15:49:30 +01002303 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, list, next, LY_EMEM);
David Sedlákaf536aa2019-07-23 13:42:23 +02002304 list->nodetype = LYS_LIST;
2305 list->parent = node_meta->parent;
2306
David Sedlákaf536aa2019-07-23 13:42:23 +02002307 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002308 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2309 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 +02002310
2311 /* parse list content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002312 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 25, (struct lysp_node *)list, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02002313 LY_STMT_ACTION, &list->actions, 0,
2314 LY_STMT_ANYDATA, &list->child, 0,
2315 LY_STMT_ANYXML, &list->child, 0,
2316 LY_STMT_CHOICE, &list->child, 0,
2317 LY_STMT_CONFIG, &list->flags, YIN_SUBELEM_UNIQUE,
2318 LY_STMT_CONTAINER, &list->child, 0,
2319 LY_STMT_DESCRIPTION, &list->dsc, YIN_SUBELEM_UNIQUE,
2320 LY_STMT_GROUPING, &list->groupings, 0,
2321 LY_STMT_IF_FEATURE, &list->iffeatures, 0,
2322 LY_STMT_KEY, &list->key, YIN_SUBELEM_UNIQUE,
2323 LY_STMT_LEAF, &list->child, 0,
2324 LY_STMT_LEAF_LIST, &list->child, 0,
2325 LY_STMT_LIST, &list->child, 0,
2326 LY_STMT_MAX_ELEMENTS, list, YIN_SUBELEM_UNIQUE,
2327 LY_STMT_MIN_ELEMENTS, list, YIN_SUBELEM_UNIQUE,
2328 LY_STMT_MUST, &list->musts, 0,
2329 LY_STMT_NOTIFICATION, &list->notifs, 0,
2330 LY_STMT_ORDERED_BY, &list->flags, YIN_SUBELEM_UNIQUE,
2331 LY_STMT_REFERENCE, &list->ref, YIN_SUBELEM_UNIQUE,
2332 LY_STMT_STATUS, &list->flags, YIN_SUBELEM_UNIQUE,
2333 LY_STMT_TYPEDEF, &list->typedefs, 0,
2334 LY_STMT_UNIQUE, &list->uniques, 0,
2335 LY_STMT_USES, &list->child, 0,
2336 LY_STMT_WHEN, &list->when, YIN_SUBELEM_UNIQUE,
2337 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
Michal Vasko193dacd2022-10-13 08:43:05 +02002338 ret = yin_parse_content(ctx, subelems, subelems_size, list, LY_STMT_LIST, NULL, &list->exts);
Radek Krejcif13b87b2020-12-01 22:02:17 +01002339 subelems_deallocator(subelems_size, subelems);
David Sedlák81497a32019-08-13 16:56:26 +02002340 LY_CHECK_RET(ret);
David Sedlákaf536aa2019-07-23 13:42:23 +02002341
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002342 /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
2343 LY_CHECK_RET(ly_set_add(&ctx->ext_inst, list->exts, 1, NULL));
2344
Michal Vasko69730152020-10-09 16:30:07 +02002345 if (list->max && (list->min > list->max)) {
Michal Vaskod0625d72022-10-06 15:02:50 +02002346 LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INVAL_MINMAX, list->min, list->max);
David Sedlákaf536aa2019-07-23 13:42:23 +02002347 return LY_EVALID;
2348 }
2349
2350 return LY_SUCCESS;
2351}
2352
2353/**
David Sedlák031b9e72019-07-23 15:19:37 +02002354 * @brief Parse notification element.
2355 *
2356 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák05404f62019-07-24 14:11:53 +02002357 * @param[in,out] notif_meta Meta information about parent node and notifications to add to.
David Sedlák031b9e72019-07-23 15:19:37 +02002358 * @return LY_ERR values.
2359 */
2360static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002361yin_parse_notification(struct lysp_yin_ctx *ctx, struct tree_node_meta *notif_meta)
David Sedlák031b9e72019-07-23 15:19:37 +02002362{
Radek Krejci2a9fc652021-01-22 17:44:34 +01002363 struct lysp_node_notif *notif;
2364 struct lysp_node_notif **notifs = (struct lysp_node_notif **)notif_meta->nodes;
David Sedlák81497a32019-08-13 16:56:26 +02002365 LY_ERR ret = LY_SUCCESS;
2366 struct yin_subelement *subelems = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002367 size_t subelems_size;
David Sedlák031b9e72019-07-23 15:19:37 +02002368
2369 /* allocate new notification */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002370 LY_LIST_NEW_RET(ctx->xmlctx->ctx, notifs, notif, next, LY_EMEM);
David Sedlák031b9e72019-07-23 15:19:37 +02002371 notif->nodetype = LYS_NOTIF;
2372 notif->parent = notif_meta->parent;
2373
2374 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002375 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2376 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 +02002377
2378 /* parse notification content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002379 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 16, (struct lysp_node *)notif, &subelems,
Radek Krejci01180ac2021-01-27 08:48:22 +01002380 LY_STMT_ANYDATA, &notif->child, 0,
2381 LY_STMT_ANYXML, &notif->child, 0,
2382 LY_STMT_CHOICE, &notif->child, 0,
2383 LY_STMT_CONTAINER, &notif->child, 0,
Michal Vasko69730152020-10-09 16:30:07 +02002384 LY_STMT_DESCRIPTION, &notif->dsc, YIN_SUBELEM_UNIQUE,
2385 LY_STMT_GROUPING, &notif->groupings, 0,
2386 LY_STMT_IF_FEATURE, &notif->iffeatures, 0,
Radek Krejci01180ac2021-01-27 08:48:22 +01002387 LY_STMT_LEAF, &notif->child, 0,
2388 LY_STMT_LEAF_LIST, &notif->child, 0,
2389 LY_STMT_LIST, &notif->child, 0,
Michal Vasko69730152020-10-09 16:30:07 +02002390 LY_STMT_MUST, &notif->musts, YIN_SUBELEM_VER2,
2391 LY_STMT_REFERENCE, &notif->ref, YIN_SUBELEM_UNIQUE,
2392 LY_STMT_STATUS, &notif->flags, YIN_SUBELEM_UNIQUE,
2393 LY_STMT_TYPEDEF, &notif->typedefs, 0,
Radek Krejci01180ac2021-01-27 08:48:22 +01002394 LY_STMT_USES, &notif->child, 0,
Michal Vasko69730152020-10-09 16:30:07 +02002395 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
David Sedlák81497a32019-08-13 16:56:26 +02002396
Michal Vasko193dacd2022-10-13 08:43:05 +02002397 ret = yin_parse_content(ctx, subelems, subelems_size, notif, LY_STMT_NOTIFICATION, NULL, &notif->exts);
Radek Krejcif13b87b2020-12-01 22:02:17 +01002398 subelems_deallocator(subelems_size, subelems);
David Sedlák81497a32019-08-13 16:56:26 +02002399 LY_CHECK_RET(ret);
David Sedlák031b9e72019-07-23 15:19:37 +02002400
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002401 /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
2402 LY_CHECK_RET(ly_set_add(&ctx->ext_inst, notif->exts, 1, NULL));
2403
David Sedlák031b9e72019-07-23 15:19:37 +02002404 return LY_SUCCESS;
2405}
2406
2407/**
David Sedlákbf8a2b72019-08-14 16:48:10 +02002408 * @brief Parse grouping element.
David Sedláke3ce9ef2019-07-23 16:34:30 +02002409 *
2410 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbf8a2b72019-08-14 16:48:10 +02002411 * @param[in,out] gr_meta Meta information about parent node and groupings to add to.
David Sedláke3ce9ef2019-07-23 16:34:30 +02002412 * @return LY_ERR values.
2413 */
2414static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002415yin_parse_grouping(struct lysp_yin_ctx *ctx, struct tree_node_meta *gr_meta)
David Sedláke3ce9ef2019-07-23 16:34:30 +02002416{
Radek Krejci2a9fc652021-01-22 17:44:34 +01002417 struct lysp_node_grp *grp;
2418 struct lysp_node_grp **grps = (struct lysp_node_grp **)gr_meta->nodes;
David Sedlák81497a32019-08-13 16:56:26 +02002419 LY_ERR ret = LY_SUCCESS;
2420 struct yin_subelement *subelems = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002421 size_t subelems_size;
David Sedláke3ce9ef2019-07-23 16:34:30 +02002422
2423 /* create new grouping */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002424 LY_LIST_NEW_RET(ctx->xmlctx->ctx, grps, grp, next, LY_EMEM);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002425 grp->nodetype = LYS_GROUPING;
2426 grp->parent = gr_meta->parent;
2427
2428 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002429 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2430 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 +02002431
2432 /* parse grouping content */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002433 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 16, &grp->node, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02002434 LY_STMT_ACTION, &grp->actions, 0,
Radek Krejci01180ac2021-01-27 08:48:22 +01002435 LY_STMT_ANYDATA, &grp->child, 0,
2436 LY_STMT_ANYXML, &grp->child, 0,
2437 LY_STMT_CHOICE, &grp->child, 0,
2438 LY_STMT_CONTAINER, &grp->child, 0,
Michal Vasko69730152020-10-09 16:30:07 +02002439 LY_STMT_DESCRIPTION, &grp->dsc, YIN_SUBELEM_UNIQUE,
2440 LY_STMT_GROUPING, &grp->groupings, 0,
Radek Krejci01180ac2021-01-27 08:48:22 +01002441 LY_STMT_LEAF, &grp->child, 0,
2442 LY_STMT_LEAF_LIST, &grp->child, 0,
2443 LY_STMT_LIST, &grp->child, 0,
Michal Vasko69730152020-10-09 16:30:07 +02002444 LY_STMT_NOTIFICATION, &grp->notifs, 0,
2445 LY_STMT_REFERENCE, &grp->ref, YIN_SUBELEM_UNIQUE,
2446 LY_STMT_STATUS, &grp->flags, YIN_SUBELEM_UNIQUE,
2447 LY_STMT_TYPEDEF, &grp->typedefs, 0,
Radek Krejci01180ac2021-01-27 08:48:22 +01002448 LY_STMT_USES, &grp->child, 0,
Michal Vasko69730152020-10-09 16:30:07 +02002449 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
Michal Vasko193dacd2022-10-13 08:43:05 +02002450 ret = yin_parse_content(ctx, subelems, subelems_size, grp, LY_STMT_GROUPING, NULL, &grp->exts);
Radek Krejcif13b87b2020-12-01 22:02:17 +01002451 subelems_deallocator(subelems_size, subelems);
David Sedlák81497a32019-08-13 16:56:26 +02002452
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002453 /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
2454 LY_CHECK_RET(ly_set_add(&ctx->ext_inst, grp->exts, 1, NULL));
2455
aPiecek63e080d2021-06-29 13:53:28 +02002456 /* store data for collision check */
2457 if (!ret && grp->parent) {
2458 assert(ctx->main_ctx);
2459 LY_CHECK_RET(ly_set_add(&ctx->main_ctx->grps_nodes, grp->parent, 0, NULL));
2460 }
2461
Radek Krejci2a9fc652021-01-22 17:44:34 +01002462 return ret;
David Sedláke3ce9ef2019-07-23 16:34:30 +02002463}
2464
2465/**
David Sedlákbf8a2b72019-08-14 16:48:10 +02002466 * @brief Parse container element.
David Sedlákf111bcb2019-07-23 17:15:51 +02002467 *
2468 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákad83cf92019-08-13 12:53:53 +02002469 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlákf111bcb2019-07-23 17:15:51 +02002470 * @return LY_ERR values.
2471 */
2472static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002473yin_parse_container(struct lysp_yin_ctx *ctx, struct tree_node_meta *node_meta)
David Sedlákf111bcb2019-07-23 17:15:51 +02002474{
David Sedlákf111bcb2019-07-23 17:15:51 +02002475 struct lysp_node_container *cont;
David Sedlák81497a32019-08-13 16:56:26 +02002476 LY_ERR ret = LY_SUCCESS;
2477 struct yin_subelement *subelems = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002478 size_t subelems_size;
David Sedlákf111bcb2019-07-23 17:15:51 +02002479
2480 /* create new container */
Michal Vaskob36053d2020-03-26 15:49:30 +01002481 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, cont, next, LY_EMEM);
David Sedlákf111bcb2019-07-23 17:15:51 +02002482 cont->nodetype = LYS_CONTAINER;
2483 cont->parent = node_meta->parent;
2484
David Sedlákf111bcb2019-07-23 17:15:51 +02002485 /* parse aegument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002486 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2487 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 +02002488
2489 /* parse container content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002490 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 21, (struct lysp_node *)cont, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02002491 LY_STMT_ACTION, &cont->actions, YIN_SUBELEM_VER2,
2492 LY_STMT_ANYDATA, &cont->child, YIN_SUBELEM_VER2,
2493 LY_STMT_ANYXML, &cont->child, 0,
2494 LY_STMT_CHOICE, &cont->child, 0,
2495 LY_STMT_CONFIG, &cont->flags, YIN_SUBELEM_UNIQUE,
2496 LY_STMT_CONTAINER, &cont->child, 0,
2497 LY_STMT_DESCRIPTION, &cont->dsc, YIN_SUBELEM_UNIQUE,
2498 LY_STMT_GROUPING, &cont->groupings, 0,
2499 LY_STMT_IF_FEATURE, &cont->iffeatures, 0,
2500 LY_STMT_LEAF, &cont->child, 0,
2501 LY_STMT_LEAF_LIST, &cont->child, 0,
2502 LY_STMT_LIST, &cont->child, 0,
2503 LY_STMT_MUST, &cont->musts, 0,
2504 LY_STMT_NOTIFICATION, &cont->notifs, YIN_SUBELEM_VER2,
2505 LY_STMT_PRESENCE, &cont->presence, YIN_SUBELEM_UNIQUE,
2506 LY_STMT_REFERENCE, &cont->ref, YIN_SUBELEM_UNIQUE,
2507 LY_STMT_STATUS, &cont->flags, YIN_SUBELEM_UNIQUE,
2508 LY_STMT_TYPEDEF, &cont->typedefs, 0,
2509 LY_STMT_USES, &cont->child, 0,
2510 LY_STMT_WHEN, &cont->when, YIN_SUBELEM_UNIQUE,
2511 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
Michal Vasko193dacd2022-10-13 08:43:05 +02002512 ret = yin_parse_content(ctx, subelems, subelems_size, cont, LY_STMT_CONTAINER, NULL, &cont->exts);
Radek Krejcif13b87b2020-12-01 22:02:17 +01002513 subelems_deallocator(subelems_size, subelems);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002514 LY_CHECK_RET(ret);
David Sedlák81497a32019-08-13 16:56:26 +02002515
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002516 /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
2517 LY_CHECK_RET(ly_set_add(&ctx->ext_inst, cont->exts, 1, NULL));
2518
2519 return LY_SUCCESS;
David Sedlákf111bcb2019-07-23 17:15:51 +02002520}
2521
2522/**
David Sedlák5379d392019-07-24 10:42:03 +02002523 * @brief Parse case element.
2524 *
2525 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákad83cf92019-08-13 12:53:53 +02002526 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlák5379d392019-07-24 10:42:03 +02002527 * @return LY_ERR values.
2528 */
2529static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002530yin_parse_case(struct lysp_yin_ctx *ctx, struct tree_node_meta *node_meta)
David Sedlák5379d392019-07-24 10:42:03 +02002531{
David Sedlák5379d392019-07-24 10:42:03 +02002532 struct lysp_node_case *cas;
David Sedlák81497a32019-08-13 16:56:26 +02002533 LY_ERR ret = LY_SUCCESS;
Michal Vasko69730152020-10-09 16:30:07 +02002534 struct yin_subelement *subelems = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002535 size_t subelems_size;
David Sedlák5379d392019-07-24 10:42:03 +02002536
2537 /* create new case */
Michal Vaskob36053d2020-03-26 15:49:30 +01002538 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, cas, next, LY_EMEM);
David Sedlák5379d392019-07-24 10:42:03 +02002539 cas->nodetype = LYS_CASE;
2540 cas->parent = node_meta->parent;
2541
David Sedlák5379d392019-07-24 10:42:03 +02002542 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002543 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2544 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 +02002545
2546 /* parse case content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002547 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 14, (struct lysp_node *)cas, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02002548 LY_STMT_ANYDATA, &cas->child, YIN_SUBELEM_VER2,
2549 LY_STMT_ANYXML, &cas->child, 0,
2550 LY_STMT_CHOICE, &cas->child, 0,
2551 LY_STMT_CONTAINER, &cas->child, 0,
2552 LY_STMT_DESCRIPTION, &cas->dsc, YIN_SUBELEM_UNIQUE,
2553 LY_STMT_IF_FEATURE, &cas->iffeatures, 0,
2554 LY_STMT_LEAF, &cas->child, 0,
2555 LY_STMT_LEAF_LIST, &cas->child, 0,
2556 LY_STMT_LIST, &cas->child, 0,
2557 LY_STMT_REFERENCE, &cas->ref, YIN_SUBELEM_UNIQUE,
2558 LY_STMT_STATUS, &cas->flags, YIN_SUBELEM_UNIQUE,
2559 LY_STMT_USES, &cas->child, 0,
2560 LY_STMT_WHEN, &cas->when, YIN_SUBELEM_UNIQUE,
2561 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
Michal Vasko193dacd2022-10-13 08:43:05 +02002562 ret = yin_parse_content(ctx, subelems, subelems_size, cas, LY_STMT_CASE, NULL, &cas->exts);
Radek Krejcif13b87b2020-12-01 22:02:17 +01002563 subelems_deallocator(subelems_size, subelems);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002564 LY_CHECK_RET(ret);
David Sedlák81497a32019-08-13 16:56:26 +02002565
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002566 /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
2567 LY_CHECK_RET(ly_set_add(&ctx->ext_inst, cas->exts, 1, NULL));
2568
2569 return LY_SUCCESS;
David Sedlák5379d392019-07-24 10:42:03 +02002570}
2571
2572/**
David Sedlák05404f62019-07-24 14:11:53 +02002573 * @brief Parse choice element.
David Sedlákb7abcfa2019-07-24 12:33:35 +02002574 *
2575 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákad83cf92019-08-13 12:53:53 +02002576 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlákb7abcfa2019-07-24 12:33:35 +02002577 * @return LY_ERR values.
2578 */
2579LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002580yin_parse_choice(struct lysp_yin_ctx *ctx, struct tree_node_meta *node_meta)
David Sedlákb7abcfa2019-07-24 12:33:35 +02002581{
David Sedlák81497a32019-08-13 16:56:26 +02002582 LY_ERR ret = LY_SUCCESS;
2583 struct yin_subelement *subelems = NULL;
David Sedlákb7abcfa2019-07-24 12:33:35 +02002584 struct lysp_node_choice *choice;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002585 size_t subelems_size;
David Sedlákb7abcfa2019-07-24 12:33:35 +02002586
2587 /* create new choice */
Michal Vaskob36053d2020-03-26 15:49:30 +01002588 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, choice, next, LY_EMEM);
David Sedlák8d552d62019-08-06 15:29:05 +02002589
David Sedlákb7abcfa2019-07-24 12:33:35 +02002590 choice->nodetype = LYS_CHOICE;
2591 choice->parent = node_meta->parent;
2592
David Sedlákb7abcfa2019-07-24 12:33:35 +02002593 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002594 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2595 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 +02002596
2597 /* parse choice content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002598 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 17, (struct lysp_node *)choice, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02002599 LY_STMT_ANYDATA, &choice->child, YIN_SUBELEM_VER2,
2600 LY_STMT_ANYXML, &choice->child, 0,
2601 LY_STMT_CASE, &choice->child, 0,
2602 LY_STMT_CHOICE, &choice->child, YIN_SUBELEM_VER2,
2603 LY_STMT_CONFIG, &choice->flags, YIN_SUBELEM_UNIQUE,
2604 LY_STMT_CONTAINER, &choice->child, 0,
2605 LY_STMT_DEFAULT, &choice->dflt, YIN_SUBELEM_UNIQUE,
2606 LY_STMT_DESCRIPTION, &choice->dsc, YIN_SUBELEM_UNIQUE,
2607 LY_STMT_IF_FEATURE, &choice->iffeatures, 0,
2608 LY_STMT_LEAF, &choice->child, 0,
2609 LY_STMT_LEAF_LIST, &choice->child, 0,
2610 LY_STMT_LIST, &choice->child, 0,
2611 LY_STMT_MANDATORY, &choice->flags, YIN_SUBELEM_UNIQUE,
2612 LY_STMT_REFERENCE, &choice->ref, YIN_SUBELEM_UNIQUE,
2613 LY_STMT_STATUS, &choice->flags, YIN_SUBELEM_UNIQUE,
2614 LY_STMT_WHEN, &choice->when, YIN_SUBELEM_UNIQUE,
2615 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
Michal Vasko193dacd2022-10-13 08:43:05 +02002616 ret = yin_parse_content(ctx, subelems, subelems_size, choice, LY_STMT_CHOICE, NULL, &choice->exts);
Radek Krejcif13b87b2020-12-01 22:02:17 +01002617 subelems_deallocator(subelems_size, subelems);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002618 LY_CHECK_RET(ret);
2619
2620 /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
2621 LY_CHECK_RET(ly_set_add(&ctx->ext_inst, choice->exts, 1, NULL));
2622
David Sedlák81497a32019-08-13 16:56:26 +02002623 return ret;
David Sedlákb7abcfa2019-07-24 12:33:35 +02002624}
2625
2626/**
David Sedlák05404f62019-07-24 14:11:53 +02002627 * @brief Parse input or output element.
2628 *
2629 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbf8a2b72019-08-14 16:48:10 +02002630 * @param[in] inout_kw Identification of input/output element.
David Sedlák05404f62019-07-24 14:11:53 +02002631 * @param[in] inout_meta Meta information about parent node and siblings and input/output pointer to write to.
David Sedlák05404f62019-07-24 14:11:53 +02002632 * @return LY_ERR values.
2633 */
2634static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002635yin_parse_inout(struct lysp_yin_ctx *ctx, enum ly_stmt inout_kw, struct inout_meta *inout_meta)
David Sedlák05404f62019-07-24 14:11:53 +02002636{
David Sedlák81497a32019-08-13 16:56:26 +02002637 LY_ERR ret = LY_SUCCESS;
2638 struct yin_subelement *subelems = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002639 size_t subelems_size;
David Sedlák81497a32019-08-13 16:56:26 +02002640
David Sedlák05404f62019-07-24 14:11:53 +02002641 /* initiate structure */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002642 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 +02002643 inout_meta->inout_p->nodetype = (inout_kw == LY_STMT_INPUT) ? LYS_INPUT : LYS_OUTPUT;
David Sedlák05404f62019-07-24 14:11:53 +02002644 inout_meta->inout_p->parent = inout_meta->parent;
2645
2646 /* check attributes */
Michal Vaskob36053d2020-03-26 15:49:30 +01002647 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2648 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 +02002649
2650 /* parser input/output content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002651 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 12, (struct lysp_node *)inout_meta->inout_p, &subelems,
Radek Krejci01180ac2021-01-27 08:48:22 +01002652 LY_STMT_ANYDATA, &inout_meta->inout_p->child, YIN_SUBELEM_VER2,
2653 LY_STMT_ANYXML, &inout_meta->inout_p->child, 0,
2654 LY_STMT_CHOICE, &inout_meta->inout_p->child, 0,
2655 LY_STMT_CONTAINER, &inout_meta->inout_p->child, 0,
Michal Vasko69730152020-10-09 16:30:07 +02002656 LY_STMT_GROUPING, &inout_meta->inout_p->groupings, 0,
Radek Krejci01180ac2021-01-27 08:48:22 +01002657 LY_STMT_LEAF, &inout_meta->inout_p->child, 0,
2658 LY_STMT_LEAF_LIST, &inout_meta->inout_p->child, 0,
2659 LY_STMT_LIST, &inout_meta->inout_p->child, 0,
Michal Vasko69730152020-10-09 16:30:07 +02002660 LY_STMT_MUST, &inout_meta->inout_p->musts, YIN_SUBELEM_VER2,
2661 LY_STMT_TYPEDEF, &inout_meta->inout_p->typedefs, 0,
Radek Krejci01180ac2021-01-27 08:48:22 +01002662 LY_STMT_USES, &inout_meta->inout_p->child, 0,
Michal Vasko69730152020-10-09 16:30:07 +02002663 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
Michal Vasko193dacd2022-10-13 08:43:05 +02002664 ret = yin_parse_content(ctx, subelems, subelems_size, inout_meta->inout_p, inout_kw, NULL, &inout_meta->inout_p->exts);
Radek Krejcif13b87b2020-12-01 22:02:17 +01002665 subelems_deallocator(subelems_size, subelems);
David Sedlák81497a32019-08-13 16:56:26 +02002666 LY_CHECK_RET(ret);
David Sedlák05404f62019-07-24 14:11:53 +02002667
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002668 /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
2669 LY_CHECK_RET(ly_set_add(&ctx->ext_inst, inout_meta->inout_p->exts, 1, NULL));
2670
Radek Krejci01180ac2021-01-27 08:48:22 +01002671 if (!inout_meta->inout_p->child) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002672 LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_MISSTMT, "data-def-stmt", lyplg_ext_stmt2str(inout_kw));
Michal Vaskob83af8a2020-01-06 09:49:22 +01002673 return LY_EVALID;
2674 }
2675
David Sedlák05404f62019-07-24 14:11:53 +02002676 return LY_SUCCESS;
2677}
2678
David Sedlák992fb7c2019-07-24 16:51:01 +02002679/**
2680 * @brief Parse action element.
2681 *
2682 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák992fb7c2019-07-24 16:51:01 +02002683 * @param[in] act_meta Meta information about parent node and actions to add to.
David Sedlák992fb7c2019-07-24 16:51:01 +02002684 * @return LY_ERR values.
2685 */
David Sedlák85d0eca2019-07-24 15:15:21 +02002686static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002687yin_parse_action(struct lysp_yin_ctx *ctx, struct tree_node_meta *act_meta)
David Sedlák85d0eca2019-07-24 15:15:21 +02002688{
Radek Krejci2a9fc652021-01-22 17:44:34 +01002689 struct lysp_node_action *act, **acts = (struct lysp_node_action **)act_meta->nodes;
David Sedlák81497a32019-08-13 16:56:26 +02002690 LY_ERR ret = LY_SUCCESS;
2691 struct yin_subelement *subelems = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002692 size_t subelems_size;
Radek Krejci39b7fc22021-02-26 23:29:18 +01002693 enum ly_stmt kw = act_meta->parent ? LY_STMT_ACTION : LY_STMT_RPC;
David Sedlák85d0eca2019-07-24 15:15:21 +02002694
2695 /* create new action */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002696 LY_LIST_NEW_RET(ctx->xmlctx->ctx, acts, act, next, LY_EMEM);
Michal Vasko1bf09392020-03-27 12:38:10 +01002697 act->nodetype = act_meta->parent ? LYS_ACTION : LYS_RPC;
David Sedlák85d0eca2019-07-24 15:15:21 +02002698 act->parent = act_meta->parent;
2699
2700 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002701 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
Radek Krejci39b7fc22021-02-26 23:29:18 +01002702 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &act->name, Y_IDENTIF_ARG, kw));
David Sedlák85d0eca2019-07-24 15:15:21 +02002703
2704 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002705 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 9, (struct lysp_node *)act, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02002706 LY_STMT_DESCRIPTION, &act->dsc, YIN_SUBELEM_UNIQUE,
2707 LY_STMT_GROUPING, &act->groupings, 0,
2708 LY_STMT_IF_FEATURE, &act->iffeatures, 0,
2709 LY_STMT_INPUT, &act->input, YIN_SUBELEM_UNIQUE,
2710 LY_STMT_OUTPUT, &act->output, YIN_SUBELEM_UNIQUE,
2711 LY_STMT_REFERENCE, &act->ref, YIN_SUBELEM_UNIQUE,
2712 LY_STMT_STATUS, &act->flags, YIN_SUBELEM_UNIQUE,
2713 LY_STMT_TYPEDEF, &act->typedefs, 0,
2714 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
Michal Vasko193dacd2022-10-13 08:43:05 +02002715 ret = (yin_parse_content(ctx, subelems, subelems_size, act, kw, NULL, &act->exts));
Radek Krejcif13b87b2020-12-01 22:02:17 +01002716 subelems_deallocator(subelems_size, subelems);
David Sedlák81497a32019-08-13 16:56:26 +02002717 LY_CHECK_RET(ret);
2718
Michal Vasko7f45cf22020-10-01 12:49:44 +02002719 /* always initialize inout, they are technically present (needed for later deviations/refines) */
2720 if (!act->input.nodetype) {
2721 act->input.nodetype = LYS_INPUT;
2722 act->input.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002723 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "input", 0, &act->input.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002724 }
2725 if (!act->output.nodetype) {
2726 act->output.nodetype = LYS_OUTPUT;
2727 act->output.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002728 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "output", 0, &act->output.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002729 }
2730
David Sedlák85d0eca2019-07-24 15:15:21 +02002731 return LY_SUCCESS;
2732}
2733
David Sedlák05404f62019-07-24 14:11:53 +02002734/**
David Sedlák992fb7c2019-07-24 16:51:01 +02002735 * @brief Parse augment element.
2736 *
2737 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák992fb7c2019-07-24 16:51:01 +02002738 * @param[in] aug_meta Meta information about parent node and augments to add to.
David Sedlák992fb7c2019-07-24 16:51:01 +02002739 * @return LY_ERR values.
2740 */
2741static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002742yin_parse_augment(struct lysp_yin_ctx *ctx, struct tree_node_meta *aug_meta)
David Sedlák992fb7c2019-07-24 16:51:01 +02002743{
Radek Krejci2a9fc652021-01-22 17:44:34 +01002744 struct lysp_node_augment *aug;
2745 struct lysp_node_augment **augs = (struct lysp_node_augment **)aug_meta->nodes;
David Sedlák81497a32019-08-13 16:56:26 +02002746 LY_ERR ret = LY_SUCCESS;
2747 struct yin_subelement *subelems = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002748 size_t subelems_size;
David Sedlák992fb7c2019-07-24 16:51:01 +02002749
2750 /* create new augment */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002751 LY_LIST_NEW_RET(ctx->xmlctx->ctx, augs, aug, next, LY_EMEM);
David Sedlák992fb7c2019-07-24 16:51:01 +02002752 aug->nodetype = LYS_AUGMENT;
2753 aug->parent = aug_meta->parent;
2754
2755 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002756 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2757 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_TARGET_NODE, &aug->nodeid, Y_STR_ARG, LY_STMT_AUGMENT));
Michal Vaskod0625d72022-10-06 15:02:50 +02002758 CHECK_NONEMPTY((struct lysp_ctx *)ctx, strlen(aug->nodeid), "augment");
David Sedlák992fb7c2019-07-24 16:51:01 +02002759
2760 /* parser augment content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002761 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 17, (struct lysp_node *)aug, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02002762 LY_STMT_ACTION, &aug->actions, YIN_SUBELEM_VER2,
2763 LY_STMT_ANYDATA, &aug->child, YIN_SUBELEM_VER2,
2764 LY_STMT_ANYXML, &aug->child, 0,
2765 LY_STMT_CASE, &aug->child, 0,
2766 LY_STMT_CHOICE, &aug->child, 0,
2767 LY_STMT_CONTAINER, &aug->child, 0,
2768 LY_STMT_DESCRIPTION, &aug->dsc, YIN_SUBELEM_UNIQUE,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002769 LY_STMT_IF_FEATURE, &aug->iffeatures, 0,
Michal Vasko69730152020-10-09 16:30:07 +02002770 LY_STMT_LEAF, &aug->child, 0,
2771 LY_STMT_LEAF_LIST, &aug->child, 0,
2772 LY_STMT_LIST, &aug->child, 0,
2773 LY_STMT_NOTIFICATION, &aug->notifs, YIN_SUBELEM_VER2,
2774 LY_STMT_REFERENCE, &aug->ref, YIN_SUBELEM_UNIQUE,
2775 LY_STMT_STATUS, &aug->flags, YIN_SUBELEM_UNIQUE,
2776 LY_STMT_USES, &aug->child, 0,
2777 LY_STMT_WHEN, &aug->when, YIN_SUBELEM_UNIQUE,
2778 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
Michal Vasko193dacd2022-10-13 08:43:05 +02002779 ret = yin_parse_content(ctx, subelems, subelems_size, aug, LY_STMT_AUGMENT, NULL, &aug->exts);
Radek Krejcif13b87b2020-12-01 22:02:17 +01002780 subelems_deallocator(subelems_size, subelems);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002781 LY_CHECK_RET(ret);
David Sedlák992fb7c2019-07-24 16:51:01 +02002782
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002783 /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
2784 LY_CHECK_RET(ly_set_add(&ctx->ext_inst, aug->exts, 1, NULL));
2785
2786 return LY_SUCCESS;
David Sedlák992fb7c2019-07-24 16:51:01 +02002787}
2788
David Sedlák8b754462019-07-25 16:22:13 +02002789/**
2790 * @brief Parse deviate element.
2791 *
2792 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák8b754462019-07-25 16:22:13 +02002793 * @param[in] deviates Deviates to add to.
David Sedlák8b754462019-07-25 16:22:13 +02002794 * @return LY_ERR values.
2795 */
David Sedlák4ffcec82019-07-25 15:10:21 +02002796static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002797yin_parse_deviate(struct lysp_yin_ctx *ctx, struct lysp_deviate **deviates)
David Sedlák4ffcec82019-07-25 15:10:21 +02002798{
2799 LY_ERR ret = LY_SUCCESS;
2800 uint8_t dev_mod;
2801 const char *temp_val;
David Sedlák8d552d62019-08-06 15:29:05 +02002802 struct lysp_deviate *d;
David Sedlák4ffcec82019-07-25 15:10:21 +02002803 struct lysp_deviate_add *d_add = NULL;
2804 struct lysp_deviate_rpl *d_rpl = NULL;
2805 struct lysp_deviate_del *d_del = NULL;
2806
2807 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002808 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2809 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 +02002810
2811 if (strcmp(temp_val, "not-supported") == 0) {
2812 dev_mod = LYS_DEV_NOT_SUPPORTED;
2813 } else if (strcmp(temp_val, "add") == 0) {
2814 dev_mod = LYS_DEV_ADD;
2815 } else if (strcmp(temp_val, "replace") == 0) {
2816 dev_mod = LYS_DEV_REPLACE;
2817 } else if (strcmp(temp_val, "delete") == 0) {
2818 dev_mod = LYS_DEV_DELETE;
2819 } else {
Michal Vaskod0625d72022-10-06 15:02:50 +02002820 LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS4, temp_val, "value", "deviate",
Michal Vasko69730152020-10-09 16:30:07 +02002821 "not-supported", "add", "replace", "delete");
Michal Vaskoe180ed02021-02-05 16:31:20 +01002822 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlák4ffcec82019-07-25 15:10:21 +02002823 return LY_EVALID;
2824 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01002825 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlák4ffcec82019-07-25 15:10:21 +02002826
2827 if (dev_mod == LYS_DEV_NOT_SUPPORTED) {
2828 d = calloc(1, sizeof *d);
Michal Vaskob36053d2020-03-26 15:49:30 +01002829 LY_CHECK_ERR_RET(!d, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
Radek Krejcif13b87b2020-12-01 22:02:17 +01002830 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002831 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
2832 };
Michal Vasko26bbb272022-08-02 14:54:33 +02002833
Michal Vasko193dacd2022-10-13 08:43:05 +02002834 ret = yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), d, LY_STMT_DEVIATE, NULL, &d->exts);
David Sedlák4ffcec82019-07-25 15:10:21 +02002835
2836 } else if (dev_mod == LYS_DEV_ADD) {
2837 d_add = calloc(1, sizeof *d_add);
Michal Vaskob36053d2020-03-26 15:49:30 +01002838 LY_CHECK_ERR_RET(!d_add, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
David Sedlák4ffcec82019-07-25 15:10:21 +02002839 d = (struct lysp_deviate *)d_add;
2840 struct minmax_dev_meta min = {&d_add->min, &d_add->flags, &d_add->exts};
2841 struct minmax_dev_meta max = {&d_add->max, &d_add->flags, &d_add->exts};
Radek Krejcif13b87b2020-12-01 22:02:17 +01002842 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002843 {LY_STMT_CONFIG, &d_add->flags, YIN_SUBELEM_UNIQUE},
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002844 {LY_STMT_DEFAULT, &d_add->dflts, 0},
Radek Krejci0f969882020-08-21 16:56:47 +02002845 {LY_STMT_MANDATORY, &d_add->flags, YIN_SUBELEM_UNIQUE},
2846 {LY_STMT_MAX_ELEMENTS, &max, YIN_SUBELEM_UNIQUE},
2847 {LY_STMT_MIN_ELEMENTS, &min, YIN_SUBELEM_UNIQUE},
2848 {LY_STMT_MUST, &d_add->musts, 0},
2849 {LY_STMT_UNIQUE, &d_add->uniques, 0},
2850 {LY_STMT_UNITS, &d_add->units, YIN_SUBELEM_UNIQUE},
2851 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
2852 };
Michal Vasko26bbb272022-08-02 14:54:33 +02002853
Michal Vasko193dacd2022-10-13 08:43:05 +02002854 ret = yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), d, LY_STMT_DEVIATE, NULL, &d->exts);
David Sedlák4ffcec82019-07-25 15:10:21 +02002855
2856 } else if (dev_mod == LYS_DEV_REPLACE) {
2857 d_rpl = calloc(1, sizeof *d_rpl);
Michal Vaskob36053d2020-03-26 15:49:30 +01002858 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
David Sedlák4ffcec82019-07-25 15:10:21 +02002859 d = (struct lysp_deviate *)d_rpl;
2860 struct minmax_dev_meta min = {&d_rpl->min, &d_rpl->flags, &d_rpl->exts};
2861 struct minmax_dev_meta max = {&d_rpl->max, &d_rpl->flags, &d_rpl->exts};
Radek Krejcif13b87b2020-12-01 22:02:17 +01002862 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002863 {LY_STMT_CONFIG, &d_rpl->flags, YIN_SUBELEM_UNIQUE},
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002864 {LY_STMT_DEFAULT, &d_rpl->dflt, YIN_SUBELEM_UNIQUE},
Radek Krejci0f969882020-08-21 16:56:47 +02002865 {LY_STMT_MANDATORY, &d_rpl->flags, YIN_SUBELEM_UNIQUE},
2866 {LY_STMT_MAX_ELEMENTS, &max, YIN_SUBELEM_UNIQUE},
2867 {LY_STMT_MIN_ELEMENTS, &min, YIN_SUBELEM_UNIQUE},
2868 {LY_STMT_TYPE, &d_rpl->type, YIN_SUBELEM_UNIQUE},
2869 {LY_STMT_UNITS, &d_rpl->units, YIN_SUBELEM_UNIQUE},
2870 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
2871 };
Michal Vasko26bbb272022-08-02 14:54:33 +02002872
Michal Vasko193dacd2022-10-13 08:43:05 +02002873 ret = yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), d, LY_STMT_DEVIATE, NULL, &d->exts);
David Sedlák4ffcec82019-07-25 15:10:21 +02002874
2875 } else {
2876 d_del = calloc(1, sizeof *d_del);
Michal Vaskob36053d2020-03-26 15:49:30 +01002877 LY_CHECK_ERR_RET(!d_del, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
David Sedlák4ffcec82019-07-25 15:10:21 +02002878 d = (struct lysp_deviate *)d_del;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002879 struct yin_subelement subelems[] = {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002880 {LY_STMT_DEFAULT, &d_del->dflts, 0},
Radek Krejci0f969882020-08-21 16:56:47 +02002881 {LY_STMT_MUST, &d_del->musts, 0},
2882 {LY_STMT_UNIQUE, &d_del->uniques, 0},
2883 {LY_STMT_UNITS, &d_del->units, YIN_SUBELEM_UNIQUE},
2884 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
2885 };
Michal Vasko26bbb272022-08-02 14:54:33 +02002886
Michal Vasko193dacd2022-10-13 08:43:05 +02002887 ret = yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), d, LY_STMT_DEVIATE, NULL, &d->exts);
David Sedlák4ffcec82019-07-25 15:10:21 +02002888 }
2889 LY_CHECK_GOTO(ret, cleanup);
2890
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002891 /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
2892 LY_CHECK_GOTO(ret = ly_set_add(&ctx->ext_inst, d->exts, 1, NULL), cleanup);
2893
David Sedlák4ffcec82019-07-25 15:10:21 +02002894 d->mod = dev_mod;
2895 /* insert into siblings */
David Sedlák8d552d62019-08-06 15:29:05 +02002896 LY_LIST_INSERT(deviates, d, next);
David Sedlák4ffcec82019-07-25 15:10:21 +02002897
2898 return ret;
2899
2900cleanup:
2901 free(d);
David Sedlák4ffcec82019-07-25 15:10:21 +02002902 return ret;
2903}
2904
David Sedlák992fb7c2019-07-24 16:51:01 +02002905/**
David Sedlák8b754462019-07-25 16:22:13 +02002906 * @brief Parse deviation element.
2907 *
2908 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák8b754462019-07-25 16:22:13 +02002909 * @param[in] deviations Deviations to add to.
David Sedlák8b754462019-07-25 16:22:13 +02002910 * @return LY_ERR values.
2911 */
2912static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002913yin_parse_deviation(struct lysp_yin_ctx *ctx, struct lysp_deviation **deviations)
David Sedlák8b754462019-07-25 16:22:13 +02002914{
Michal Vasko12ef5362022-09-16 15:13:58 +02002915 LY_ERR ret = LY_SUCCESS;
2916 struct lysp_deviation *dev = NULL;
Michal Vaskoc636ea42022-09-16 10:20:31 +02002917 struct lysf_ctx fctx = {.ctx = PARSER_CTX(ctx)};
David Sedlák8b754462019-07-25 16:22:13 +02002918
2919 /* create new deviation */
Michal Vaskob36053d2020-03-26 15:49:30 +01002920 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *deviations, dev, LY_EMEM);
David Sedlák8b754462019-07-25 16:22:13 +02002921
2922 /* parse argument */
Michal Vasko12ef5362022-09-16 15:13:58 +02002923 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
2924 LY_CHECK_GOTO(ret = yin_parse_attribute(ctx, YIN_ARG_TARGET_NODE, &dev->nodeid, Y_STR_ARG, LY_STMT_DEVIATION), cleanup);
Michal Vaskod0625d72022-10-06 15:02:50 +02002925 CHECK_NONEMPTY((struct lysp_ctx *)ctx, strlen(dev->nodeid), "deviation");
Radek Krejcif13b87b2020-12-01 22:02:17 +01002926 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002927 {LY_STMT_DESCRIPTION, &dev->dsc, YIN_SUBELEM_UNIQUE},
2928 {LY_STMT_DEVIATE, &dev->deviates, YIN_SUBELEM_MANDATORY},
2929 {LY_STMT_REFERENCE, &dev->ref, YIN_SUBELEM_UNIQUE},
2930 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
2931 };
Michal Vasko69730152020-10-09 16:30:07 +02002932
Michal Vasko193dacd2022-10-13 08:43:05 +02002933 ret = yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), dev, LY_STMT_DEVIATION, NULL, &dev->exts);
2934 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko12ef5362022-09-16 15:13:58 +02002935
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002936 /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
2937 LY_CHECK_GOTO(ret = ly_set_add(&ctx->ext_inst, dev->exts, 1, NULL), cleanup);
2938
Michal Vasko12ef5362022-09-16 15:13:58 +02002939cleanup:
2940 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02002941 lysp_deviation_free(&fctx, dev);
Michal Vasko12ef5362022-09-16 15:13:58 +02002942 LY_ARRAY_DECREMENT_FREE(*deviations);
2943 }
2944 return ret;
David Sedlák8b754462019-07-25 16:22:13 +02002945}
2946
2947/**
David Sedlákc5b20842019-08-13 10:18:31 +02002948 * @brief map keyword to keyword-group.
2949 *
2950 * @param[in] ctx YIN parser context used for logging.
2951 * @param[in] kw Keyword that is child of module or submodule.
2952 * @param[out] group Group of keyword.
David Sedlákc5b20842019-08-13 10:18:31 +02002953 * @return LY_SUCCESS on success LY_EINT if kw can't be mapped to kw_group, should not happen if called correctly.
2954 */
David Sedláke6cd89e2019-08-07 12:46:02 +02002955static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002956kw2kw_group(struct lysp_yin_ctx *ctx, enum ly_stmt kw, enum yang_module_stmt *group)
David Sedláke6cd89e2019-08-07 12:46:02 +02002957{
2958 switch (kw) {
Radek Krejci0f969882020-08-21 16:56:47 +02002959 /* module header */
2960 case LY_STMT_NONE:
2961 case LY_STMT_NAMESPACE:
2962 case LY_STMT_PREFIX:
2963 case LY_STMT_BELONGS_TO:
2964 case LY_STMT_YANG_VERSION:
2965 *group = Y_MOD_MODULE_HEADER;
2966 break;
2967 /* linkage */
2968 case LY_STMT_INCLUDE:
2969 case LY_STMT_IMPORT:
2970 *group = Y_MOD_LINKAGE;
2971 break;
2972 /* meta */
2973 case LY_STMT_ORGANIZATION:
2974 case LY_STMT_CONTACT:
2975 case LY_STMT_DESCRIPTION:
2976 case LY_STMT_REFERENCE:
2977 *group = Y_MOD_META;
2978 break;
2979 /* revision */
2980 case LY_STMT_REVISION:
2981 *group = Y_MOD_REVISION;
2982 break;
2983 /* body */
2984 case LY_STMT_ANYDATA:
2985 case LY_STMT_ANYXML:
2986 case LY_STMT_AUGMENT:
2987 case LY_STMT_CHOICE:
2988 case LY_STMT_CONTAINER:
2989 case LY_STMT_DEVIATION:
2990 case LY_STMT_EXTENSION:
2991 case LY_STMT_FEATURE:
2992 case LY_STMT_GROUPING:
2993 case LY_STMT_IDENTITY:
2994 case LY_STMT_LEAF:
2995 case LY_STMT_LEAF_LIST:
2996 case LY_STMT_LIST:
2997 case LY_STMT_NOTIFICATION:
2998 case LY_STMT_RPC:
2999 case LY_STMT_TYPEDEF:
3000 case LY_STMT_USES:
3001 case LY_STMT_EXTENSION_INSTANCE:
3002 *group = Y_MOD_BODY;
3003 break;
3004 default:
3005 LOGINT(ctx->xmlctx->ctx);
3006 return LY_EINT;
David Sedláke6cd89e2019-08-07 12:46:02 +02003007 }
3008
3009 return LY_SUCCESS;
3010}
3011
David Sedlákc5b20842019-08-13 10:18:31 +02003012/**
3013 * @brief Check if relative order of two keywords is valid.
3014 *
3015 * @param[in] ctx YIN parser context used for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02003016 * @param[in] cur_stmt Type of current statement.
3017 * @param[in] next_stmt Type of next statement.
3018 * @param[in] parent_stmt Type of parent statement, can be se to to LY_STMT_MODULE of LY_STMT_SUBMODULE,
David Sedlákc5b20842019-08-13 10:18:31 +02003019 * because relative order is required only in module and submodule sub-elements, used for logging.
David Sedlákbf8a2b72019-08-14 16:48:10 +02003020 * @return LY_SUCCESS on success and LY_EVALID if relative order is invalid.
David Sedlákc5b20842019-08-13 10:18:31 +02003021 */
David Sedláke6cd89e2019-08-07 12:46:02 +02003022static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02003023yin_check_relative_order(struct lysp_yin_ctx *ctx, enum ly_stmt cur_stmt, enum ly_stmt next_stmt, enum ly_stmt parent_stmt)
David Sedláke6cd89e2019-08-07 12:46:02 +02003024{
David Sedláke6cd89e2019-08-07 12:46:02 +02003025 enum yang_module_stmt gr, next_gr;
3026
Michal Vasko193dacd2022-10-13 08:43:05 +02003027 assert(parent_stmt == LY_STMT_MODULE || parent_stmt == LY_STMT_SUBMODULE);
3028
3029 if (cur_stmt == LY_STMT_EXTENSION_INSTANCE) {
Michal Vaskoc78293f2021-12-16 15:40:33 +01003030 /* no order defined */
3031 return LY_SUCCESS;
3032 }
3033
Michal Vasko193dacd2022-10-13 08:43:05 +02003034 LY_CHECK_RET(kw2kw_group(ctx, cur_stmt, &gr));
3035 LY_CHECK_RET(kw2kw_group(ctx, next_stmt, &next_gr));
David Sedláke6cd89e2019-08-07 12:46:02 +02003036
3037 if (gr > next_gr) {
Michal Vasko193dacd2022-10-13 08:43:05 +02003038 LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INORDER_YIN, lyplg_ext_stmt2str(parent_stmt), lyplg_ext_stmt2str(next_stmt),
3039 lyplg_ext_stmt2str(cur_stmt));
David Sedláke6cd89e2019-08-07 12:46:02 +02003040 return LY_EVALID;
3041 }
3042
3043 return LY_SUCCESS;
3044}
3045
David Sedlákc5dd0942019-09-13 10:23:59 +02003046/**
Michal Vaskoafac7822020-10-20 14:22:26 +02003047 * @brief Parse argument of extension subelement that is classic yang keyword and not another instance of extension.
3048 *
3049 * @param[in,out] ctx Yin parser context for logging and to store current state.
Michal Vasko193dacd2022-10-13 08:43:05 +02003050 * @param[in] parent_stmt Type of parent statement.
Michal Vaskoafac7822020-10-20 14:22:26 +02003051 * @param[out] arg Value to write to.
Michal Vaskoafac7822020-10-20 14:22:26 +02003052 * @return LY_ERR values.
3053 */
3054static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02003055yin_parse_extension_instance_arg(struct lysp_yin_ctx *ctx, enum ly_stmt parent_stmt, const char **arg)
Michal Vaskoafac7822020-10-20 14:22:26 +02003056{
3057 enum ly_stmt child;
3058
3059 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3060
Michal Vasko193dacd2022-10-13 08:43:05 +02003061 switch (parent_stmt) {
Michal Vaskoafac7822020-10-20 14:22:26 +02003062 case LY_STMT_ACTION:
3063 case LY_STMT_ANYDATA:
3064 case LY_STMT_ANYXML:
3065 case LY_STMT_ARGUMENT:
3066 case LY_STMT_BASE:
3067 case LY_STMT_BIT:
3068 case LY_STMT_CASE:
3069 case LY_STMT_CHOICE:
3070 case LY_STMT_CONTAINER:
3071 case LY_STMT_ENUM:
3072 case LY_STMT_EXTENSION:
3073 case LY_STMT_FEATURE:
3074 case LY_STMT_GROUPING:
3075 case LY_STMT_IDENTITY:
3076 case LY_STMT_IF_FEATURE:
3077 case LY_STMT_LEAF:
3078 case LY_STMT_LEAF_LIST:
3079 case LY_STMT_LIST:
3080 case LY_STMT_MODULE:
3081 case LY_STMT_NOTIFICATION:
3082 case LY_STMT_RPC:
3083 case LY_STMT_SUBMODULE:
3084 case LY_STMT_TYPE:
3085 case LY_STMT_TYPEDEF:
3086 case LY_STMT_UNITS:
3087 case LY_STMT_USES:
Michal Vasko193dacd2022-10-13 08:43:05 +02003088 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, arg, Y_MAYBE_STR_ARG, parent_stmt));
Michal Vaskoafac7822020-10-20 14:22:26 +02003089 break;
3090 case LY_STMT_AUGMENT:
3091 case LY_STMT_DEVIATION:
3092 case LY_STMT_REFINE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003093 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_TARGET_NODE, arg, Y_MAYBE_STR_ARG, parent_stmt));
Michal Vaskoafac7822020-10-20 14:22:26 +02003094 break;
3095 case LY_STMT_CONFIG:
3096 case LY_STMT_DEFAULT:
3097 case LY_STMT_DEVIATE:
3098 case LY_STMT_ERROR_APP_TAG:
3099 case LY_STMT_FRACTION_DIGITS:
3100 case LY_STMT_KEY:
3101 case LY_STMT_LENGTH:
3102 case LY_STMT_MANDATORY:
3103 case LY_STMT_MAX_ELEMENTS:
3104 case LY_STMT_MIN_ELEMENTS:
3105 case LY_STMT_MODIFIER:
3106 case LY_STMT_ORDERED_BY:
3107 case LY_STMT_PATH:
3108 case LY_STMT_PATTERN:
3109 case LY_STMT_POSITION:
3110 case LY_STMT_PREFIX:
3111 case LY_STMT_PRESENCE:
3112 case LY_STMT_RANGE:
3113 case LY_STMT_REQUIRE_INSTANCE:
3114 case LY_STMT_STATUS:
3115 case LY_STMT_VALUE:
3116 case LY_STMT_YANG_VERSION:
3117 case LY_STMT_YIN_ELEMENT:
Michal Vasko193dacd2022-10-13 08:43:05 +02003118 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, arg, Y_MAYBE_STR_ARG, parent_stmt));
Michal Vaskoafac7822020-10-20 14:22:26 +02003119 break;
3120 case LY_STMT_IMPORT:
3121 case LY_STMT_INCLUDE:
3122 case LY_STMT_BELONGS_TO:
Michal Vasko193dacd2022-10-13 08:43:05 +02003123 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_MODULE, arg, Y_MAYBE_STR_ARG, parent_stmt));
Michal Vaskoafac7822020-10-20 14:22:26 +02003124 break;
3125 case LY_STMT_INPUT:
3126 case LY_STMT_OUTPUT:
Michal Vasko193dacd2022-10-13 08:43:05 +02003127 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NONE, arg, Y_MAYBE_STR_ARG, parent_stmt));
Michal Vaskoafac7822020-10-20 14:22:26 +02003128 break;
3129 case LY_STMT_MUST:
3130 case LY_STMT_WHEN:
Michal Vasko193dacd2022-10-13 08:43:05 +02003131 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_CONDITION, arg, Y_MAYBE_STR_ARG, parent_stmt));
Michal Vaskoafac7822020-10-20 14:22:26 +02003132 break;
3133 case LY_STMT_NAMESPACE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003134 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_URI, arg, Y_MAYBE_STR_ARG, parent_stmt));
Michal Vaskoafac7822020-10-20 14:22:26 +02003135 break;
3136 case LY_STMT_REVISION:
3137 case LY_STMT_REVISION_DATE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003138 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_DATE, arg, Y_MAYBE_STR_ARG, parent_stmt));
Michal Vaskoafac7822020-10-20 14:22:26 +02003139 break;
3140 case LY_STMT_UNIQUE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003141 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_TAG, arg, Y_MAYBE_STR_ARG, parent_stmt));
Michal Vaskoafac7822020-10-20 14:22:26 +02003142 break;
3143 /* argument is mapped to yin element */
3144 case LY_STMT_CONTACT:
3145 case LY_STMT_DESCRIPTION:
3146 case LY_STMT_ORGANIZATION:
3147 case LY_STMT_REFERENCE:
3148 case LY_STMT_ERROR_MESSAGE:
3149 /* there shouldn't be any attribute, argument is supposed to be first subelement */
Michal Vasko193dacd2022-10-13 08:43:05 +02003150 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NONE, arg, Y_MAYBE_STR_ARG, parent_stmt));
Michal Vaskoafac7822020-10-20 14:22:26 +02003151
3152 /* no content */
3153 assert(ctx->xmlctx->status == LYXML_ELEM_CONTENT);
3154 if (ctx->xmlctx->ws_only) {
3155 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3156 }
3157 if (((ctx->xmlctx->status == LYXML_ELEM_CONTENT) && !ctx->xmlctx->ws_only) || (ctx->xmlctx->status != LYXML_ELEMENT)) {
Michal Vaskod0625d72022-10-06 15:02:50 +02003158 LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_FIRT_SUBELEM,
Michal Vasko193dacd2022-10-13 08:43:05 +02003159 parent_stmt == LY_STMT_ERROR_MESSAGE ? "value" : "text", lyplg_ext_stmt2str(parent_stmt));
Michal Vaskoafac7822020-10-20 14:22:26 +02003160 return LY_EVALID;
3161 }
3162
3163 /* parse child element */
Michal Vasko193dacd2022-10-13 08:43:05 +02003164 child = yin_match_keyword(ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, ctx->xmlctx->prefix,
3165 ctx->xmlctx->prefix_len, parent_stmt);
3166 if (((parent_stmt == LY_STMT_ERROR_MESSAGE) && (child != LY_STMT_ARG_VALUE)) ||
3167 ((parent_stmt != LY_STMT_ERROR_MESSAGE) && (child != LY_STMT_ARG_TEXT))) {
Michal Vaskod0625d72022-10-06 15:02:50 +02003168 LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_UNEXP_SUBELEM, ctx->xmlctx->name_len, ctx->xmlctx->name,
Michal Vasko193dacd2022-10-13 08:43:05 +02003169 lyplg_ext_stmt2str(parent_stmt));
Michal Vaskoafac7822020-10-20 14:22:26 +02003170 return LY_EVALID;
3171 }
3172 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3173
Michal Vasko193dacd2022-10-13 08:43:05 +02003174 /* no attributes expected */
Michal Vaskoafac7822020-10-20 14:22:26 +02003175 while (ctx->xmlctx->status == LYXML_ATTRIBUTE) {
3176 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3177 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3178 }
3179
3180 /* load and save content */
3181 INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic, *arg);
3182 LY_CHECK_RET(!*arg, LY_EMEM);
3183
3184 /* load closing tag of subelement */
3185 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
Michal Vaskoafac7822020-10-20 14:22:26 +02003186 break;
3187 default:
3188 LOGINT(ctx->xmlctx->ctx);
3189 return LY_EINT;
3190 }
3191
3192 return LY_SUCCESS;
3193}
3194
3195/**
3196 * @brief Parse yin element into generic structure.
3197 *
3198 * @param[in,out] ctx Yin parser context for XML context, logging, and to store current state.
Michal Vasko193dacd2022-10-13 08:43:05 +02003199 * @param[in] parent_stmt Type of parent statement.
Michal Vaskoafac7822020-10-20 14:22:26 +02003200 * @param[out] element Where the element structure should be stored.
Michal Vaskoafac7822020-10-20 14:22:26 +02003201 * @return LY_ERR values.
3202 */
3203LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02003204yin_parse_element_generic(struct lysp_yin_ctx *ctx, enum ly_stmt parent_stmt, struct lysp_stmt **element)
Michal Vaskoafac7822020-10-20 14:22:26 +02003205{
3206 LY_ERR ret = LY_SUCCESS;
3207 struct lysp_stmt *last = NULL, *new = NULL;
Michal Vaskofc2cd072021-02-24 13:17:17 +01003208 char *id;
Michal Vaskoafac7822020-10-20 14:22:26 +02003209
3210 assert(ctx->xmlctx->status == LYXML_ELEMENT);
3211
3212 /* allocate new structure for element */
3213 *element = calloc(1, sizeof(**element));
3214 LY_CHECK_ERR_GOTO(!(*element), LOGMEM(ctx->xmlctx->ctx); ret = LY_EMEM, cleanup);
Michal Vaskofc2cd072021-02-24 13:17:17 +01003215
3216 /* store identifier */
3217 if (ctx->xmlctx->prefix_len) {
3218 if (asprintf(&id, "%.*s:%.*s", (int)ctx->xmlctx->prefix_len, ctx->xmlctx->prefix, (int)ctx->xmlctx->name_len,
3219 ctx->xmlctx->name) == -1) {
3220 LOGMEM(ctx->xmlctx->ctx);
3221 ret = LY_EMEM;
3222 goto cleanup;
3223 }
3224 LY_CHECK_GOTO(ret = lydict_insert_zc(ctx->xmlctx->ctx, id, &(*element)->stmt), cleanup);
3225
3226 /* store prefix data for the statement */
Radek Krejci8df109d2021-04-23 12:19:08 +02003227 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 +01003228 &ctx->xmlctx->ns, &(*element)->format, &(*element)->prefix_data), cleanup);
3229 } else {
3230 LY_CHECK_GOTO(ret = lydict_insert(ctx->xmlctx->ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, &(*element)->stmt), cleanup);
3231 }
Michal Vaskoafac7822020-10-20 14:22:26 +02003232
3233 (*element)->kw = yin_match_keyword(ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, ctx->xmlctx->prefix,
Michal Vasko193dacd2022-10-13 08:43:05 +02003234 ctx->xmlctx->prefix_len, parent_stmt);
Michal Vaskoafac7822020-10-20 14:22:26 +02003235
3236 last = (*element)->child;
3237 if ((*element)->kw == LY_STMT_NONE) {
3238 /* unrecognized element */
Michal Vaskod0625d72022-10-06 15:02:50 +02003239 LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_UNEXP_SUBELEM, ctx->xmlctx->name_len, ctx->xmlctx->name,
Michal Vasko193dacd2022-10-13 08:43:05 +02003240 lyplg_ext_stmt2str(parent_stmt));
Michal Vaskoafac7822020-10-20 14:22:26 +02003241 ret = LY_EVALID;
3242 goto cleanup;
3243 } else if ((*element)->kw != LY_STMT_EXTENSION_INSTANCE) {
3244 /* element is known yang keyword, which means argument can be parsed correctly. */
3245 ret = yin_parse_extension_instance_arg(ctx, (*element)->kw, &(*element)->arg);
3246 LY_CHECK_GOTO(ret, cleanup);
3247 } else {
3248 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3249
3250 /* load attributes in generic way, save all attributes in linked list */
3251 while (ctx->xmlctx->status == LYXML_ATTRIBUTE) {
3252 new = calloc(1, sizeof(*last));
3253 LY_CHECK_ERR_GOTO(!new, LOGMEM(ctx->xmlctx->ctx); ret = LY_EMEM, cleanup);
3254 if (!(*element)->child) {
3255 /* save first */
3256 (*element)->child = new;
3257 } else {
3258 last->next = new;
3259 }
3260 last = new;
3261
3262 last->flags |= LYS_YIN_ATTR;
3263 LY_CHECK_GOTO(ret = lydict_insert(ctx->xmlctx->ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, &last->stmt), cleanup);
3264 last->kw = LY_STMT_NONE;
3265 /* attributes with prefix are ignored */
3266 if (!ctx->xmlctx->prefix) {
3267 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3268
3269 INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic, last->arg);
3270 LY_CHECK_ERR_GOTO(!last->arg, ret = LY_EMEM, cleanup);
3271 } else {
3272 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3273 }
3274 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3275 }
3276 }
3277
3278 if ((ctx->xmlctx->status != LYXML_ELEM_CONTENT) || ctx->xmlctx->ws_only) {
3279 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3280 while (ctx->xmlctx->status == LYXML_ELEMENT) {
3281 /* parse subelements */
3282 ret = yin_parse_element_generic(ctx, (*element)->kw, &new);
3283 LY_CHECK_GOTO(ret, cleanup);
3284 if (!(*element)->child) {
3285 /* save first */
3286 (*element)->child = new;
3287 } else {
3288 last->next = new;
3289 }
3290 last = new;
3291
3292 assert(ctx->xmlctx->status == LYXML_ELEM_CLOSE);
3293 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3294 }
3295 } else {
3296 /* save element content */
3297 if (ctx->xmlctx->value_len) {
3298 INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic, (*element)->arg);
3299 LY_CHECK_ERR_GOTO(!(*element)->arg, ret = LY_EMEM, cleanup);
Michal Vaskofc2cd072021-02-24 13:17:17 +01003300
3301 /* store prefix data for the argument as well */
Radek Krejci8df109d2021-04-23 12:19:08 +02003302 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 +01003303 &ctx->xmlctx->ns, &(*element)->format, &(*element)->prefix_data), cleanup);
Michal Vaskoafac7822020-10-20 14:22:26 +02003304 }
3305
3306 /* read closing tag */
3307 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3308 }
3309
3310cleanup:
3311 return ret;
3312}
3313
3314/**
3315 * @brief Parse instance of extension.
3316 *
3317 * @param[in,out] ctx Yin parser context for logging and to store current state.
Michal Vasko193dacd2022-10-13 08:43:05 +02003318 * @param[in] parent Current statement parent.
3319 * @param[in] parent_stmt Type of @p parent statement.
3320 * @param[in] parent_stmt_index In case of several @p parent_stmt, index of the relevant @p parent statement.
Michal Vaskoafac7822020-10-20 14:22:26 +02003321 * @param[in,out] exts Extension instance to add to.
Michal Vaskoafac7822020-10-20 14:22:26 +02003322 * @return LY_ERR values.
3323 */
3324LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02003325yin_parse_extension_instance(struct lysp_yin_ctx *ctx, const void *parent, enum ly_stmt parent_stmt,
3326 LY_ARRAY_COUNT_TYPE parent_stmt_index, struct lysp_ext_instance **exts)
Michal Vaskoafac7822020-10-20 14:22:26 +02003327{
3328 struct lysp_ext_instance *e;
3329 struct lysp_stmt *last_subelem = NULL, *new_subelem = NULL;
Michal Vaskofc2cd072021-02-24 13:17:17 +01003330 char *ext_name;
Michal Vaskoafac7822020-10-20 14:22:26 +02003331
3332 assert(ctx->xmlctx->status == LYXML_ELEMENT);
Radek Krejci3aac9a72020-12-01 12:24:26 +01003333 assert(exts);
Michal Vaskoafac7822020-10-20 14:22:26 +02003334
3335 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *exts, e, LY_EMEM);
3336
Michal Vaskofc2cd072021-02-24 13:17:17 +01003337 if (!ctx->xmlctx->prefix_len) {
3338 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Extension instance \"%*.s\" without the mandatory prefix.",
3339 (int)ctx->xmlctx->name_len, ctx->xmlctx->name);
3340 return LY_EVALID;
3341 }
3342
3343 /* store prefixed name */
3344 if (asprintf(&ext_name, "%.*s:%.*s", (int)ctx->xmlctx->prefix_len, ctx->xmlctx->prefix, (int)ctx->xmlctx->name_len,
3345 ctx->xmlctx->name) == -1) {
3346 LOGMEM(ctx->xmlctx->ctx);
3347 return LY_EMEM;
3348 }
3349 LY_CHECK_RET(lydict_insert_zc(ctx->xmlctx->ctx, ext_name, &e->name));
3350
3351 /* store prefix data for the name */
Radek Krejci8df109d2021-04-23 12:19:08 +02003352 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 +01003353 &e->format, &e->prefix_data));
3354
Michal Vasko193dacd2022-10-13 08:43:05 +02003355 e->parent = (void *)parent;
3356 e->parent_stmt = parent_stmt;
3357 e->parent_stmt_index = parent_stmt_index;
Michal Vaskofc2cd072021-02-24 13:17:17 +01003358
Michal Vaskoafac7822020-10-20 14:22:26 +02003359 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3360
3361 /* store attributes as subelements */
3362 while (ctx->xmlctx->status == LYXML_ATTRIBUTE) {
3363 if (!ctx->xmlctx->prefix) {
3364 new_subelem = calloc(1, sizeof(*new_subelem));
3365 if (!e->child) {
3366 e->child = new_subelem;
3367 } else {
3368 last_subelem->next = new_subelem;
3369 }
3370 last_subelem = new_subelem;
3371
3372 last_subelem->flags |= LYS_YIN_ATTR;
3373 LY_CHECK_RET(lydict_insert(ctx->xmlctx->ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, &last_subelem->stmt));
3374 LY_CHECK_RET(!last_subelem->stmt, LY_EMEM);
3375 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3376
Michal Vaskofc2cd072021-02-24 13:17:17 +01003377 INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic,
3378 last_subelem->arg);
Michal Vaskoafac7822020-10-20 14:22:26 +02003379 LY_CHECK_RET(!last_subelem->arg, LY_EMEM);
3380 } else {
3381 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3382 }
3383
3384 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3385 }
3386
3387 /* parse subelements */
3388 assert(ctx->xmlctx->status == LYXML_ELEM_CONTENT);
3389 if (ctx->xmlctx->ws_only) {
3390 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3391 while (ctx->xmlctx->status == LYXML_ELEMENT) {
3392 LY_CHECK_RET(yin_parse_element_generic(ctx, LY_STMT_EXTENSION_INSTANCE, &new_subelem));
3393 if (!e->child) {
3394 e->child = new_subelem;
3395 } else {
3396 last_subelem->next = new_subelem;
3397 }
3398 last_subelem = new_subelem;
3399
3400 assert(ctx->xmlctx->status == LYXML_ELEM_CLOSE);
3401 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3402 }
3403 } else if (ctx->xmlctx->value_len) {
Michal Vasko193dacd2022-10-13 08:43:05 +02003404 /* invalid text content */
3405 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Extension instance \"%s\" with unexpected text content \".*s\".", ext_name,
3406 (int)ctx->xmlctx->value_len, ctx->xmlctx->value);
3407 return LY_EVALID;
Michal Vaskoafac7822020-10-20 14:22:26 +02003408 }
3409
aPiecek60d9d672021-04-27 15:49:57 +02003410 e->parsed = NULL;
3411
Michal Vaskoafac7822020-10-20 14:22:26 +02003412 return LY_SUCCESS;
3413}
3414
3415/**
3416 * @brief Generic function for content parsing
3417 *
3418 * @param[in,out] ctx Yin parser context for logging and to store current state.
3419 * @param[in] subelem_info array of valid subelement types and meta information
3420 * @param[in] subelem_info_size Size of subelem_info array.
Michal Vasko193dacd2022-10-13 08:43:05 +02003421 * @param[in] parent Current statement parent.
3422 * @param[in] parent_stmt Type of @p parent statement.
Michal Vaskoafac7822020-10-20 14:22:26 +02003423 * @param[out] text_content Where the text content of element should be stored if any. Text content is ignored if set to NULL.
3424 * @param[in,out] exts Extension instance to add to. Can be set to null if element cannot have extension as subelements.
Michal Vaskoafac7822020-10-20 14:22:26 +02003425 * @return LY_ERR values.
3426 */
David Sedlákd6e56892019-07-01 15:40:24 +02003427LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003428yin_parse_content(struct lysp_yin_ctx *ctx, struct yin_subelement *subelem_info, size_t subelem_info_size,
Michal Vasko193dacd2022-10-13 08:43:05 +02003429 const void *parent, enum ly_stmt parent_stmt, const char **text_content, struct lysp_ext_instance **exts)
David Sedlákd6e56892019-07-01 15:40:24 +02003430{
3431 LY_ERR ret = LY_SUCCESS;
Michal Vaskob36053d2020-03-26 15:49:30 +01003432 enum LYXML_PARSER_STATUS next_status;
Michal Vasko193dacd2022-10-13 08:43:05 +02003433 enum ly_stmt cur_stmt = LY_STMT_NONE, last_stmt = LY_STMT_NONE;
David Sedlák1af868e2019-07-17 17:03:14 +02003434 struct yin_subelement *subelem = NULL;
David Sedlák09e18c92019-07-18 11:17:11 +02003435
Michal Vaskob36053d2020-03-26 15:49:30 +01003436 assert(ctx->xmlctx->status == LYXML_ELEM_CONTENT);
David Sedlákd6e56892019-07-01 15:40:24 +02003437
Michal Vaskob36053d2020-03-26 15:49:30 +01003438 if (ctx->xmlctx->ws_only) {
3439 /* check whether there are any children */
3440 LY_CHECK_GOTO(ret = lyxml_ctx_peek(ctx->xmlctx, &next_status), cleanup);
3441 } else {
3442 /* we want to parse the value */
3443 next_status = LYXML_ELEM_CLOSE;
3444 }
3445
3446 if (next_status == LYXML_ELEMENT) {
3447 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3448
3449 /* current element has subelements as content */
3450 while (ctx->xmlctx->status == LYXML_ELEMENT) {
3451 /* match keyword */
Michal Vasko193dacd2022-10-13 08:43:05 +02003452 last_stmt = cur_stmt;
3453 cur_stmt = yin_match_keyword(ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, ctx->xmlctx->prefix,
3454 ctx->xmlctx->prefix_len, parent_stmt);
Michal Vaskob36053d2020-03-26 15:49:30 +01003455
3456 /* check if this element can be child of current element */
Michal Vasko193dacd2022-10-13 08:43:05 +02003457 subelem = get_record(cur_stmt, subelem_info_size, subelem_info);
Michal Vaskob36053d2020-03-26 15:49:30 +01003458 if (!subelem) {
Michal Vasko193dacd2022-10-13 08:43:05 +02003459 if ((parent_stmt == LY_STMT_DEVIATE) && isdevsub(cur_stmt)) {
3460 LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INDEV_YIN, lyplg_ext_stmt2str(cur_stmt));
Michal Vaskob36053d2020-03-26 15:49:30 +01003461 } else {
Michal Vaskod0625d72022-10-06 15:02:50 +02003462 LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_UNEXP_SUBELEM, ctx->xmlctx->name_len,
Michal Vasko193dacd2022-10-13 08:43:05 +02003463 ctx->xmlctx->name, lyplg_ext_stmt2str(parent_stmt));
Michal Vaskob36053d2020-03-26 15:49:30 +01003464 }
3465 ret = LY_EVALID;
3466 goto cleanup;
3467 }
3468
3469 /* relative order is required only in module and submodule sub-elements */
Michal Vasko193dacd2022-10-13 08:43:05 +02003470 if ((parent_stmt == LY_STMT_MODULE) || (parent_stmt == LY_STMT_SUBMODULE)) {
3471 ret = yin_check_relative_order(ctx, last_stmt, cur_stmt, parent_stmt);
Michal Vaskob36053d2020-03-26 15:49:30 +01003472 LY_CHECK_GOTO(ret, cleanup);
3473 }
3474
3475 /* flag check */
3476 if ((subelem->flags & YIN_SUBELEM_UNIQUE) && (subelem->flags & YIN_SUBELEM_PARSED)) {
3477 /* subelement uniquenes */
Michal Vasko193dacd2022-10-13 08:43:05 +02003478 LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_SUBELEM_REDEF, lyplg_ext_stmt2str(cur_stmt), lyplg_ext_stmt2str(parent_stmt));
Michal Vaskob36053d2020-03-26 15:49:30 +01003479 return LY_EVALID;
3480 }
3481 if (subelem->flags & YIN_SUBELEM_FIRST) {
3482 /* subelement is supposed to be defined as first subelement */
Michal Vasko193dacd2022-10-13 08:43:05 +02003483 ret = yin_check_subelem_first_constraint(ctx, subelem_info, subelem_info_size, parent_stmt, subelem);
Michal Vaskob36053d2020-03-26 15:49:30 +01003484 LY_CHECK_GOTO(ret, cleanup);
3485 }
3486 if (subelem->flags & YIN_SUBELEM_VER2) {
3487 /* subelement is supported only in version 1.1 or higher */
Michal Vasko8a67eff2021-12-07 14:04:47 +01003488 if (PARSER_CUR_PMOD(ctx)->version < LYS_VERSION_1_1) {
Michal Vasko193dacd2022-10-13 08:43:05 +02003489 LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INSUBELEM2, lyplg_ext_stmt2str(cur_stmt), lyplg_ext_stmt2str(parent_stmt));
David Sedlákd6e56892019-07-01 15:40:24 +02003490 ret = LY_EVALID;
3491 goto cleanup;
3492 }
David Sedlákd6e56892019-07-01 15:40:24 +02003493 }
Michal Vaskob36053d2020-03-26 15:49:30 +01003494 /* note that element was parsed for easy uniqueness check in next iterations */
3495 subelem->flags |= YIN_SUBELEM_PARSED;
3496
Michal Vasko193dacd2022-10-13 08:43:05 +02003497 switch (cur_stmt) {
Michal Vaskob36053d2020-03-26 15:49:30 +01003498 /* call responsible function */
3499 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003500 ret = yin_parse_extension_instance(ctx, parent, parent_stmt,
Michal Vasko69730152020-10-09 16:30:07 +02003501 (subelem->dest) ? *((LY_ARRAY_COUNT_TYPE *)subelem->dest) : 0, exts);
Michal Vaskob36053d2020-03-26 15:49:30 +01003502 break;
3503 case LY_STMT_ACTION:
3504 case LY_STMT_RPC:
3505 ret = yin_parse_action(ctx, (struct tree_node_meta *)subelem->dest);
3506 break;
3507 case LY_STMT_ANYDATA:
3508 case LY_STMT_ANYXML:
Michal Vasko193dacd2022-10-13 08:43:05 +02003509 ret = yin_parse_any(ctx, cur_stmt, (struct tree_node_meta *)subelem->dest);
Michal Vaskob36053d2020-03-26 15:49:30 +01003510 break;
3511 case LY_STMT_ARGUMENT:
Michal Vasko193dacd2022-10-13 08:43:05 +02003512 ret = yin_parse_argument(ctx, parent, (struct yin_argument_meta *)subelem->dest, exts);
Michal Vaskob36053d2020-03-26 15:49:30 +01003513 break;
3514 case LY_STMT_AUGMENT:
3515 ret = yin_parse_augment(ctx, (struct tree_node_meta *)subelem->dest);
3516 break;
3517 case LY_STMT_BASE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003518 ret = yin_parse_base(ctx, parent_stmt, subelem->dest, exts);
Michal Vaskob36053d2020-03-26 15:49:30 +01003519 break;
3520 case LY_STMT_BELONGS_TO:
3521 ret = yin_parse_belongs_to(ctx, (struct lysp_submodule *)subelem->dest, exts);
3522 break;
3523 case LY_STMT_BIT:
3524 ret = yin_parse_bit(ctx, (struct lysp_type *)subelem->dest);
3525 break;
3526 case LY_STMT_CASE:
3527 ret = yin_parse_case(ctx, (struct tree_node_meta *)subelem->dest);
3528 break;
3529 case LY_STMT_CHOICE:
3530 ret = yin_parse_choice(ctx, (struct tree_node_meta *)subelem->dest);
3531 break;
3532 case LY_STMT_CONFIG:
3533 ret = yin_parse_config(ctx, (uint16_t *)subelem->dest, exts);
3534 break;
3535 case LY_STMT_CONTACT:
3536 case LY_STMT_DESCRIPTION:
3537 case LY_STMT_ORGANIZATION:
3538 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003539 ret = yin_parse_meta(ctx, parent, cur_stmt, (const char **)subelem->dest, exts);
Michal Vaskob36053d2020-03-26 15:49:30 +01003540 break;
3541 case LY_STMT_CONTAINER:
3542 ret = yin_parse_container(ctx, (struct tree_node_meta *)subelem->dest);
3543 break;
3544 case LY_STMT_DEFAULT:
Michal Vasko193dacd2022-10-13 08:43:05 +02003545 ret = yin_parse_qname(ctx, cur_stmt, subelem, exts);
Michal Vasko7f45cf22020-10-01 12:49:44 +02003546 break;
Michal Vaskob36053d2020-03-26 15:49:30 +01003547 case LY_STMT_ERROR_APP_TAG:
3548 case LY_STMT_KEY:
Michal Vasko193dacd2022-10-13 08:43:05 +02003549 ret = yin_parse_simple_elem(ctx, parent, cur_stmt, subelem, YIN_ARG_VALUE, Y_STR_ARG, exts);
3550 break;
Michal Vaskob36053d2020-03-26 15:49:30 +01003551 case LY_STMT_PRESENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003552 ret = yin_parse_simple_elem(ctx, *(const char **)subelem->dest, cur_stmt, subelem, YIN_ARG_VALUE, Y_STR_ARG, exts);
Michal Vaskob36053d2020-03-26 15:49:30 +01003553 break;
3554 case LY_STMT_DEVIATE:
3555 ret = yin_parse_deviate(ctx, (struct lysp_deviate **)subelem->dest);
3556 break;
3557 case LY_STMT_DEVIATION:
3558 ret = yin_parse_deviation(ctx, (struct lysp_deviation **)subelem->dest);
3559 break;
3560 case LY_STMT_ENUM:
3561 ret = yin_parse_enum(ctx, (struct lysp_type *)subelem->dest);
3562 break;
3563 case LY_STMT_ERROR_MESSAGE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003564 ret = yin_parse_err_msg(ctx, parent, (const char **)subelem->dest, exts);
Michal Vaskob36053d2020-03-26 15:49:30 +01003565 break;
3566 case LY_STMT_EXTENSION:
3567 ret = yin_parse_extension(ctx, (struct lysp_ext **)subelem->dest);
3568 break;
3569 case LY_STMT_FEATURE:
3570 ret = yin_parse_feature(ctx, (struct lysp_feature **)subelem->dest);
3571 break;
3572 case LY_STMT_FRACTION_DIGITS:
3573 ret = yin_parse_fracdigits(ctx, (struct lysp_type *)subelem->dest);
3574 break;
3575 case LY_STMT_GROUPING:
3576 ret = yin_parse_grouping(ctx, (struct tree_node_meta *)subelem->dest);
3577 break;
3578 case LY_STMT_IDENTITY:
3579 ret = yin_parse_identity(ctx, (struct lysp_ident **)subelem->dest);
3580 break;
Radek Išabe8a6532021-03-29 08:54:22 +02003581 case LY_STMT_UNIQUE:
Michal Vaskob36053d2020-03-26 15:49:30 +01003582 case LY_STMT_IF_FEATURE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003583 ret = yin_parse_qname(ctx, cur_stmt, subelem, exts);
Michal Vasko7f45cf22020-10-01 12:49:44 +02003584 break;
Michal Vaskob36053d2020-03-26 15:49:30 +01003585 case LY_STMT_UNITS:
Michal Vasko193dacd2022-10-13 08:43:05 +02003586 ret = yin_parse_simple_elem(ctx, *(const char **)subelem->dest, cur_stmt, subelem, YIN_ARG_NAME, Y_STR_ARG, exts);
Michal Vaskob36053d2020-03-26 15:49:30 +01003587 break;
3588 case LY_STMT_IMPORT:
3589 ret = yin_parse_import(ctx, (struct import_meta *)subelem->dest);
3590 break;
3591 case LY_STMT_INCLUDE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003592 if ((parent_stmt == LY_STMT_SUBMODULE) && (PARSER_CUR_PMOD(ctx)->version == LYS_VERSION_1_1)) {
3593 LOGWRN(PARSER_CTX(ctx), "YANG version 1.1 expects all includes in main module, includes in "
3594 "submodules (%s) are not necessary.", ((struct lysp_submodule *)PARSER_CUR_PMOD(ctx))->name);
Radek Krejcieeee95c2021-01-19 10:57:22 +01003595 }
Michal Vaskob36053d2020-03-26 15:49:30 +01003596 ret = yin_parse_include(ctx, (struct include_meta *)subelem->dest);
3597 break;
3598 case LY_STMT_INPUT:
3599 case LY_STMT_OUTPUT:
Michal Vasko193dacd2022-10-13 08:43:05 +02003600 ret = yin_parse_inout(ctx, cur_stmt, (struct inout_meta *)subelem->dest);
Michal Vaskob36053d2020-03-26 15:49:30 +01003601 break;
3602 case LY_STMT_LEAF:
3603 ret = yin_parse_leaf(ctx, (struct tree_node_meta *)subelem->dest);
3604 break;
3605 case LY_STMT_LEAF_LIST:
3606 ret = yin_parse_leaflist(ctx, (struct tree_node_meta *)subelem->dest);
3607 break;
3608 case LY_STMT_LENGTH:
3609 ret = yin_parse_length(ctx, (struct lysp_type *)subelem->dest);
3610 break;
3611 case LY_STMT_LIST:
3612 ret = yin_parse_list(ctx, (struct tree_node_meta *)subelem->dest);
3613 break;
3614 case LY_STMT_MANDATORY:
3615 ret = yin_parse_mandatory(ctx, (uint16_t *)subelem->dest, exts);
3616 break;
3617 case LY_STMT_MAX_ELEMENTS:
3618 case LY_STMT_MIN_ELEMENTS:
Michal Vasko193dacd2022-10-13 08:43:05 +02003619 ret = yin_parse_minmax(ctx, parent_stmt, cur_stmt, subelem->dest);
Michal Vaskob36053d2020-03-26 15:49:30 +01003620 break;
3621 case LY_STMT_MODIFIER:
Michal Vasko193dacd2022-10-13 08:43:05 +02003622 ret = yin_parse_modifier(ctx, parent, (const char **)subelem->dest, exts);
Michal Vaskob36053d2020-03-26 15:49:30 +01003623 break;
3624 case LY_STMT_MUST:
3625 ret = yin_parse_must(ctx, (struct lysp_restr **)subelem->dest);
3626 break;
3627 case LY_STMT_NAMESPACE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003628 ret = yin_parse_simple_elem(ctx, parent, cur_stmt, subelem, YIN_ARG_URI, Y_STR_ARG, exts);
Michal Vaskob36053d2020-03-26 15:49:30 +01003629 break;
3630 case LY_STMT_NOTIFICATION:
3631 ret = yin_parse_notification(ctx, (struct tree_node_meta *)subelem->dest);
3632 break;
3633 case LY_STMT_ORDERED_BY:
Michal Vasko193dacd2022-10-13 08:43:05 +02003634 ret = yin_parse_orderedby(ctx, parent, (uint16_t *)subelem->dest, exts);
Michal Vaskob36053d2020-03-26 15:49:30 +01003635 break;
3636 case LY_STMT_PATH:
Michal Vasko193dacd2022-10-13 08:43:05 +02003637 ret = yin_parse_path(ctx, (struct lysp_type *)subelem->dest);
Michal Vaskob36053d2020-03-26 15:49:30 +01003638 break;
3639 case LY_STMT_PATTERN:
3640 ret = yin_parse_pattern(ctx, (struct lysp_type *)subelem->dest);
3641 break;
3642 case LY_STMT_VALUE:
3643 case LY_STMT_POSITION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003644 ret = yin_parse_value_pos(ctx, cur_stmt, (struct lysp_type_enum *)subelem->dest);
Michal Vaskob36053d2020-03-26 15:49:30 +01003645 break;
3646 case LY_STMT_PREFIX:
Michal Vasko193dacd2022-10-13 08:43:05 +02003647 ret = yin_parse_simple_elem(ctx, *(const char **)subelem->dest, cur_stmt, subelem, YIN_ARG_VALUE,
3648 Y_IDENTIF_ARG, exts);
Michal Vaskob36053d2020-03-26 15:49:30 +01003649 break;
3650 case LY_STMT_RANGE:
3651 ret = yin_parse_range(ctx, (struct lysp_type *)subelem->dest);
3652 break;
3653 case LY_STMT_REFINE:
3654 ret = yin_parse_refine(ctx, (struct lysp_refine **)subelem->dest);
3655 break;
3656 case LY_STMT_REQUIRE_INSTANCE:
3657 ret = yin_pasrse_reqinstance(ctx, (struct lysp_type *)subelem->dest);
3658 break;
3659 case LY_STMT_REVISION:
3660 ret = yin_parse_revision(ctx, (struct lysp_revision **)subelem->dest);
3661 break;
3662 case LY_STMT_REVISION_DATE:
3663 ret = yin_parse_revision_date(ctx, (char *)subelem->dest, exts);
3664 break;
3665 case LY_STMT_STATUS:
3666 ret = yin_parse_status(ctx, (uint16_t *)subelem->dest, exts);
3667 break;
3668 case LY_STMT_TYPE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003669 ret = yin_parse_type(ctx, parent_stmt, subelem);
Michal Vaskob36053d2020-03-26 15:49:30 +01003670 break;
3671 case LY_STMT_TYPEDEF:
3672 ret = yin_parse_typedef(ctx, (struct tree_node_meta *)subelem->dest);
3673 break;
Michal Vaskob36053d2020-03-26 15:49:30 +01003674 case LY_STMT_USES:
3675 ret = yin_parse_uses(ctx, (struct tree_node_meta *)subelem->dest);
3676 break;
3677 case LY_STMT_WHEN:
3678 ret = yin_parse_when(ctx, (struct lysp_when **)subelem->dest);
3679 break;
3680 case LY_STMT_YANG_VERSION:
Michal Vasko193dacd2022-10-13 08:43:05 +02003681 ret = yin_parse_yangversion(ctx, parent, (uint8_t *)subelem->dest, exts);
Michal Vaskob36053d2020-03-26 15:49:30 +01003682 break;
3683 case LY_STMT_YIN_ELEMENT:
Michal Vasko193dacd2022-10-13 08:43:05 +02003684 ret = yin_parse_yin_element(ctx, parent, (uint16_t *)subelem->dest, exts);
Michal Vaskob36053d2020-03-26 15:49:30 +01003685 break;
3686 case LY_STMT_ARG_TEXT:
3687 case LY_STMT_ARG_VALUE:
3688 /* TODO what to do with content/attributes? */
3689 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3690 while (ctx->xmlctx->status == LYXML_ATTRIBUTE) {
3691 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3692 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3693 }
Michal Vasko193dacd2022-10-13 08:43:05 +02003694 ret = yin_parse_content(ctx, NULL, 0, parent, cur_stmt, (const char **)subelem->dest, NULL);
Michal Vaskob36053d2020-03-26 15:49:30 +01003695 break;
3696 default:
3697 LOGINT(ctx->xmlctx->ctx);
3698 ret = LY_EINT;
David Sedlák3ffbc522019-07-02 17:49:28 +02003699 }
Michal Vaskob36053d2020-03-26 15:49:30 +01003700 LY_CHECK_GOTO(ret, cleanup);
3701 subelem = NULL;
3702
3703 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
David Sedlákd6e56892019-07-01 15:40:24 +02003704 }
Michal Vaskob36053d2020-03-26 15:49:30 +01003705 } else {
3706 LY_CHECK_RET(ret);
3707 /* elements with text or none content */
3708 /* save text content, if text_content isn't set, it's just ignored */
3709 /* no resources are allocated in this branch, no need to use cleanup label */
3710 LY_CHECK_RET(yin_validate_value(ctx, Y_STR_ARG));
3711 if (text_content) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02003712 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 +01003713 LY_CHECK_RET(!*text_content, LY_EMEM);
3714 }
3715
3716 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
David Sedlákd6e56892019-07-01 15:40:24 +02003717 }
Michal Vaskob36053d2020-03-26 15:49:30 +01003718
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003719 /* mandatory subelements are checked only after whole element was succesfully parsed */
Michal Vasko193dacd2022-10-13 08:43:05 +02003720 LY_CHECK_RET(yin_check_subelem_mandatory_constraint(ctx, subelem_info, subelem_info_size, parent_stmt));
David Sedlákd6e56892019-07-01 15:40:24 +02003721
3722cleanup:
David Sedlákd6e56892019-07-01 15:40:24 +02003723 return ret;
3724}
3725
David Sedlákaa98bba2019-09-12 11:52:14 +02003726/**
Michal Vaskoafac7822020-10-20 14:22:26 +02003727 * @brief Parse module element.
David Sedlákaa98bba2019-09-12 11:52:14 +02003728 *
3729 * @param[in,out] ctx Yin parser context for logging and to store current state.
Michal Vaskoafac7822020-10-20 14:22:26 +02003730 * @param[out] mod Parsed module structure.
David Sedlákaa98bba2019-09-12 11:52:14 +02003731 * @return LY_ERR values.
3732 */
David Sedlák1e2cdd02019-06-27 14:17:43 +02003733LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003734yin_parse_mod(struct lysp_yin_ctx *ctx, struct lysp_module *mod)
David Sedlák3b4db242018-10-19 16:11:01 +02003735{
David Sedlák81497a32019-08-13 16:56:26 +02003736 LY_ERR ret = LY_SUCCESS;
3737 struct yin_subelement *subelems = NULL;
Michal Vasko8dc31992021-02-22 10:30:47 +01003738 const struct lysp_submodule *dup;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003739 size_t subelems_size;
David Sedlák3b4db242018-10-19 16:11:01 +02003740
Michal Vaskoc3781c32020-10-06 14:04:08 +02003741 mod->is_submod = 0;
3742
Michal Vaskob36053d2020-03-26 15:49:30 +01003743 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3744 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 +01003745 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 28, NULL, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02003746 LY_STMT_ANYDATA, &mod->data, YIN_SUBELEM_VER2,
3747 LY_STMT_ANYXML, &mod->data, 0,
3748 LY_STMT_AUGMENT, &mod->augments, 0,
3749 LY_STMT_CHOICE, &mod->data, 0,
3750 LY_STMT_CONTACT, &mod->mod->contact, YIN_SUBELEM_UNIQUE,
3751 LY_STMT_CONTAINER, &mod->data, 0,
3752 LY_STMT_DESCRIPTION, &mod->mod->dsc, YIN_SUBELEM_UNIQUE,
3753 LY_STMT_DEVIATION, &mod->deviations, 0,
3754 LY_STMT_EXTENSION, &mod->extensions, 0,
3755 LY_STMT_FEATURE, &mod->features, 0,
3756 LY_STMT_GROUPING, &mod->groupings, 0,
3757 LY_STMT_IDENTITY, &mod->identities, 0,
3758 LY_STMT_IMPORT, mod->mod->prefix, &mod->imports, 0,
3759 LY_STMT_INCLUDE, mod->mod->name, &mod->includes, 0,
3760 LY_STMT_LEAF, &mod->data, 0,
3761 LY_STMT_LEAF_LIST, &mod->data, 0,
3762 LY_STMT_LIST, &mod->data, 0,
3763 LY_STMT_NAMESPACE, &mod->mod->ns, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE,
3764 LY_STMT_NOTIFICATION, &mod->notifs, 0,
3765 LY_STMT_ORGANIZATION, &mod->mod->org, YIN_SUBELEM_UNIQUE,
3766 LY_STMT_PREFIX, &mod->mod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE,
3767 LY_STMT_REFERENCE, &mod->mod->ref, YIN_SUBELEM_UNIQUE,
3768 LY_STMT_REVISION, &mod->revs, 0,
3769 LY_STMT_RPC, &mod->rpcs, 0,
3770 LY_STMT_TYPEDEF, &mod->typedefs, 0,
3771 LY_STMT_USES, &mod->data, 0,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003772 LY_STMT_YANG_VERSION, &mod->version, YIN_SUBELEM_UNIQUE,
Michal Vasko69730152020-10-09 16:30:07 +02003773 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
David Sedlák81497a32019-08-13 16:56:26 +02003774
Michal Vasko193dacd2022-10-13 08:43:05 +02003775 ret = yin_parse_content(ctx, subelems, subelems_size, mod, LY_STMT_MODULE, NULL, &mod->exts);
Radek Krejcif13b87b2020-12-01 22:02:17 +01003776 subelems_deallocator(subelems_size, subelems);
David Sedláked809f12019-09-13 10:15:11 +02003777 LY_CHECK_RET(ret);
David Sedlák81497a32019-08-13 16:56:26 +02003778
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003779 /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
3780 LY_CHECK_RET(ly_set_add(&ctx->ext_inst, mod->exts, 1, NULL));
3781
David Sedláked809f12019-09-13 10:15:11 +02003782 /* submodules share the namespace with the module names, so there must not be
3783 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01003784 dup = ly_ctx_get_submodule_latest(ctx->xmlctx->ctx, mod->mod->name);
David Sedláked809f12019-09-13 10:15:11 +02003785 if (dup) {
Michal Vaskod0625d72022-10-06 15:02:50 +02003786 LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_NAME2_COL, "module", "submodule", mod->mod->name);
David Sedláked809f12019-09-13 10:15:11 +02003787 return LY_EVALID;
3788 }
3789
3790 return LY_SUCCESS;
David Sedlák3b4db242018-10-19 16:11:01 +02003791}
3792
Michal Vaskoafac7822020-10-20 14:22:26 +02003793/**
3794 * @brief Parse submodule element.
3795 *
3796 * @param[in,out] ctx Yin parser context for logging and to store current state.
3797 * @param[in] mod_attrs Attributes of submodule element.
3798 * @param[out] submod Parsed submodule structure.
Michal Vaskoafac7822020-10-20 14:22:26 +02003799 * @return LY_ERR values.
3800 */
David Sedlák3b4db242018-10-19 16:11:01 +02003801LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003802yin_parse_submod(struct lysp_yin_ctx *ctx, struct lysp_submodule *submod)
David Sedlák298ff6d2019-07-26 14:29:03 +02003803{
David Sedlák81497a32019-08-13 16:56:26 +02003804 LY_ERR ret = LY_SUCCESS;
3805 struct yin_subelement *subelems = NULL;
Michal Vasko8dc31992021-02-22 10:30:47 +01003806 const struct lysp_submodule *dup;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003807 size_t subelems_size;
David Sedlák298ff6d2019-07-26 14:29:03 +02003808
Michal Vaskoc3781c32020-10-06 14:04:08 +02003809 submod->is_submod = 1;
3810
Michal Vaskob36053d2020-03-26 15:49:30 +01003811 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3812 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 +01003813 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 27, NULL, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02003814 LY_STMT_ANYDATA, &submod->data, YIN_SUBELEM_VER2,
3815 LY_STMT_ANYXML, &submod->data, 0,
3816 LY_STMT_AUGMENT, &submod->augments, 0,
3817 LY_STMT_BELONGS_TO, submod, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE,
3818 LY_STMT_CHOICE, &submod->data, 0,
3819 LY_STMT_CONTACT, &submod->contact, YIN_SUBELEM_UNIQUE,
3820 LY_STMT_CONTAINER, &submod->data, 0,
3821 LY_STMT_DESCRIPTION, &submod->dsc, YIN_SUBELEM_UNIQUE,
3822 LY_STMT_DEVIATION, &submod->deviations, 0,
3823 LY_STMT_EXTENSION, &submod->extensions, 0,
3824 LY_STMT_FEATURE, &submod->features, 0,
3825 LY_STMT_GROUPING, &submod->groupings, 0,
3826 LY_STMT_IDENTITY, &submod->identities, 0,
3827 LY_STMT_IMPORT, submod->prefix, &submod->imports, 0,
3828 LY_STMT_INCLUDE, submod->name, &submod->includes, 0,
3829 LY_STMT_LEAF, &submod->data, 0,
3830 LY_STMT_LEAF_LIST, &submod->data, 0,
3831 LY_STMT_LIST, &submod->data, 0,
3832 LY_STMT_NOTIFICATION, &submod->notifs, 0,
3833 LY_STMT_ORGANIZATION, &submod->org, YIN_SUBELEM_UNIQUE,
3834 LY_STMT_REFERENCE, &submod->ref, YIN_SUBELEM_UNIQUE,
3835 LY_STMT_REVISION, &submod->revs, 0,
3836 LY_STMT_RPC, &submod->rpcs, 0,
3837 LY_STMT_TYPEDEF, &submod->typedefs, 0,
3838 LY_STMT_USES, &submod->data, 0,
3839 LY_STMT_YANG_VERSION, &submod->version, YIN_SUBELEM_UNIQUE,
3840 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
David Sedlák81497a32019-08-13 16:56:26 +02003841
Michal Vasko193dacd2022-10-13 08:43:05 +02003842 ret = yin_parse_content(ctx, subelems, subelems_size, submod, LY_STMT_SUBMODULE, NULL, &submod->exts);
Radek Krejcif13b87b2020-12-01 22:02:17 +01003843 subelems_deallocator(subelems_size, subelems);
David Sedláked809f12019-09-13 10:15:11 +02003844 LY_CHECK_RET(ret);
David Sedlák81497a32019-08-13 16:56:26 +02003845
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003846 /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
3847 LY_CHECK_RET(ly_set_add(&ctx->ext_inst, submod->exts, 1, NULL));
3848
David Sedláked809f12019-09-13 10:15:11 +02003849 /* submodules share the namespace with the module names, so there must not be
3850 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01003851 dup = ly_ctx_get_submodule_latest(ctx->xmlctx->ctx, submod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02003852 if (dup && strcmp(dup->mod->name, submod->mod->name)) {
Michal Vaskod0625d72022-10-06 15:02:50 +02003853 LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_NAME_COL, "submodules", dup->name);
David Sedláked809f12019-09-13 10:15:11 +02003854 return LY_EVALID;
3855 }
3856
3857 return LY_SUCCESS;
David Sedlák298ff6d2019-07-26 14:29:03 +02003858}
3859
3860LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003861yin_parse_submodule(struct lysp_yin_ctx **yin_ctx, struct ly_ctx *ctx, struct lysp_ctx *main_ctx,
Radek Krejci0f969882020-08-21 16:56:47 +02003862 struct ly_in *in, struct lysp_submodule **submod)
David Sedlák8985a142019-07-31 16:43:06 +02003863{
Radek Krejcid6b76452019-09-03 17:03:03 +02003864 enum ly_stmt kw = LY_STMT_NONE;
David Sedlák8985a142019-07-31 16:43:06 +02003865 LY_ERR ret = LY_SUCCESS;
David Sedlák8985a142019-07-31 16:43:06 +02003866 struct lysp_submodule *mod_p = NULL;
Michal Vaskoc636ea42022-09-16 10:20:31 +02003867 struct lysf_ctx fctx = {.ctx = ctx};
David Sedlák8985a142019-07-31 16:43:06 +02003868
aPiecek56be92a2021-07-01 07:18:10 +02003869 assert(yin_ctx && ctx && main_ctx && in && submod);
3870
David Sedlák8985a142019-07-31 16:43:06 +02003871 /* create context */
3872 *yin_ctx = calloc(1, sizeof **yin_ctx);
David Sedlák1b623122019-08-05 15:27:49 +02003873 LY_CHECK_ERR_RET(!(*yin_ctx), LOGMEM(ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01003874 (*yin_ctx)->format = LYS_IN_YIN;
aPiecek56be92a2021-07-01 07:18:10 +02003875 (*yin_ctx)->main_ctx = main_ctx;
Michal Vasko63f3d842020-07-08 10:10:14 +02003876 LY_CHECK_RET(lyxml_ctx_new(ctx, in, &(*yin_ctx)->xmlctx));
David Sedlák8985a142019-07-31 16:43:06 +02003877
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003878 mod_p = calloc(1, sizeof *mod_p);
3879 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01003880 mod_p->mod = PARSER_CUR_PMOD(main_ctx)->mod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003881 mod_p->parsing = 1;
Michal Vasko8a67eff2021-12-07 14:04:47 +01003882
3883 /* use main context parsed mods adding the current one */
3884 (*yin_ctx)->parsed_mods = main_ctx->parsed_mods;
3885 ly_set_add((*yin_ctx)->parsed_mods, mod_p, 1, NULL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003886
David Sedlák8985a142019-07-31 16:43:06 +02003887 /* check submodule */
Michal Vaskob36053d2020-03-26 15:49:30 +01003888 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 +02003889 (*yin_ctx)->xmlctx->prefix_len, LY_STMT_NONE);
Radek Krejcid6b76452019-09-03 17:03:03 +02003890 if (kw == LY_STMT_MODULE) {
Michal Vasko193dacd2022-10-13 08:43:05 +02003891 LOGERR(ctx, LY_EDENIED, "Input data contains module when a submodule is expected.");
David Sedlák8985a142019-07-31 16:43:06 +02003892 ret = LY_EINVAL;
3893 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02003894 } else if (kw != LY_STMT_SUBMODULE) {
Michal Vasko193dacd2022-10-13 08:43:05 +02003895 LOGVAL_PARSER((struct lysp_ctx *)*yin_ctx, LY_VCODE_MOD_SUBOMD, lyplg_ext_stmt2str(kw));
David Sedlák8985a142019-07-31 16:43:06 +02003896 ret = LY_EVALID;
3897 goto cleanup;
3898 }
3899
Michal Vaskob36053d2020-03-26 15:49:30 +01003900 ret = yin_parse_submod(*yin_ctx, mod_p);
David Sedlák8985a142019-07-31 16:43:06 +02003901 LY_CHECK_GOTO(ret, cleanup);
3902
David Sedlákbf8a2b72019-08-14 16:48:10 +02003903 /* skip possible trailing whitespaces at end of the input */
Michal Vasko63f3d842020-07-08 10:10:14 +02003904 while (isspace(in->current[0])) {
Radek Krejcidd713ce2021-01-04 23:12:12 +01003905 if (in->current[0] == '\n') {
3906 LY_IN_NEW_LINE(in);
3907 }
Michal Vasko63f3d842020-07-08 10:10:14 +02003908 ly_in_skip(in, 1);
David Sedlák6d781b62019-08-02 15:22:52 +02003909 }
Michal Vasko63f3d842020-07-08 10:10:14 +02003910 if (in->current[0]) {
Michal Vaskod0625d72022-10-06 15:02:50 +02003911 LOGVAL_PARSER((struct lysp_ctx *)*yin_ctx, LY_VCODE_TRAILING_SUBMOD, 15, in->current,
Michal Vasko69730152020-10-09 16:30:07 +02003912 strlen(in->current) > 15 ? "..." : "");
David Sedlák6d781b62019-08-02 15:22:52 +02003913 ret = LY_EVALID;
3914 goto cleanup;
3915 }
3916
David Sedlák8985a142019-07-31 16:43:06 +02003917 mod_p->parsing = 0;
3918 *submod = mod_p;
3919
3920cleanup:
Michal Vasko193dacd2022-10-13 08:43:05 +02003921 LOG_LOCBACK(0, 0, 0, 1);
David Sedlák8985a142019-07-31 16:43:06 +02003922 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02003923 lysp_module_free(&fctx, (struct lysp_module *)mod_p);
Michal Vaskod0625d72022-10-06 15:02:50 +02003924 lysp_yin_ctx_free(*yin_ctx);
David Sedlák1b623122019-08-05 15:27:49 +02003925 *yin_ctx = NULL;
David Sedlák8985a142019-07-31 16:43:06 +02003926 }
David Sedlák8985a142019-07-31 16:43:06 +02003927 return ret;
3928}
3929
3930LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003931yin_parse_module(struct lysp_yin_ctx **yin_ctx, struct ly_in *in, struct lys_module *mod)
David Sedlák3b4db242018-10-19 16:11:01 +02003932{
David Sedláke4889912018-11-02 09:52:40 +01003933 LY_ERR ret = LY_SUCCESS;
Radek Krejcid6b76452019-09-03 17:03:03 +02003934 enum ly_stmt kw = LY_STMT_NONE;
David Sedlák3017da42019-02-15 09:48:04 +01003935 struct lysp_module *mod_p = NULL;
Michal Vaskoc636ea42022-09-16 10:20:31 +02003936 struct lysf_ctx fctx = {.ctx = mod->ctx};
David Sedlák3b4db242018-10-19 16:11:01 +02003937
David Sedlák8985a142019-07-31 16:43:06 +02003938 /* create context */
3939 *yin_ctx = calloc(1, sizeof **yin_ctx);
David Sedlák1b623122019-08-05 15:27:49 +02003940 LY_CHECK_ERR_RET(!(*yin_ctx), LOGMEM(mod->ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01003941 (*yin_ctx)->format = LYS_IN_YIN;
Michal Vasko8a67eff2021-12-07 14:04:47 +01003942 LY_CHECK_ERR_RET(ly_set_new(&(*yin_ctx)->parsed_mods), free(*yin_ctx); LOGMEM(mod->ctx), LY_EMEM);
Michal Vasko63f3d842020-07-08 10:10:14 +02003943 LY_CHECK_RET(lyxml_ctx_new(mod->ctx, in, &(*yin_ctx)->xmlctx));
Michal Vaskod0625d72022-10-06 15:02:50 +02003944 (*yin_ctx)->main_ctx = (struct lysp_ctx *)(*yin_ctx);
David Sedlákda8ffa32019-07-08 14:17:10 +02003945
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003946 mod_p = calloc(1, sizeof *mod_p);
3947 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(mod->ctx), cleanup);
3948 mod_p->mod = mod;
Michal Vasko8a67eff2021-12-07 14:04:47 +01003949 ly_set_add((*yin_ctx)->parsed_mods, mod_p, 1, NULL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003950
David Sedlák8985a142019-07-31 16:43:06 +02003951 /* check module */
Michal Vaskob36053d2020-03-26 15:49:30 +01003952 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 +02003953 (*yin_ctx)->xmlctx->prefix_len, LY_STMT_NONE);
Radek Krejcid6b76452019-09-03 17:03:03 +02003954 if (kw == LY_STMT_SUBMODULE) {
David Sedlák8985a142019-07-31 16:43:06 +02003955 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 +01003956 ret = LY_EINVAL;
3957 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02003958 } else if (kw != LY_STMT_MODULE) {
Michal Vasko193dacd2022-10-13 08:43:05 +02003959 LOGVAL_PARSER((struct lysp_ctx *)*yin_ctx, LY_VCODE_MOD_SUBOMD, lyplg_ext_stmt2str(kw));
David Sedlák3017da42019-02-15 09:48:04 +01003960 ret = LY_EVALID;
3961 goto cleanup;
David Sedláke4889912018-11-02 09:52:40 +01003962 }
3963
David Sedlák00250342019-06-21 14:19:39 +02003964 /* parse module substatements */
Michal Vaskob36053d2020-03-26 15:49:30 +01003965 ret = yin_parse_mod(*yin_ctx, mod_p);
David Sedlák3017da42019-02-15 09:48:04 +01003966 LY_CHECK_GOTO(ret, cleanup);
David Sedlák2e411422018-12-17 02:35:39 +01003967
David Sedlákbf8a2b72019-08-14 16:48:10 +02003968 /* skip possible trailing whitespaces at end of the input */
Michal Vasko63f3d842020-07-08 10:10:14 +02003969 while (isspace(in->current[0])) {
Radek Krejcidd713ce2021-01-04 23:12:12 +01003970 if (in->current[0] == '\n') {
3971 LY_IN_NEW_LINE(in);
3972 }
Michal Vasko63f3d842020-07-08 10:10:14 +02003973 ly_in_skip(in, 1);
David Sedlák6d781b62019-08-02 15:22:52 +02003974 }
Michal Vasko63f3d842020-07-08 10:10:14 +02003975 if (in->current[0]) {
Michal Vaskod0625d72022-10-06 15:02:50 +02003976 LOGVAL_PARSER((struct lysp_ctx *)*yin_ctx, LY_VCODE_TRAILING_MOD, 15, in->current,
Michal Vasko69730152020-10-09 16:30:07 +02003977 strlen(in->current) > 15 ? "..." : "");
David Sedlák6d781b62019-08-02 15:22:52 +02003978 ret = LY_EVALID;
3979 goto cleanup;
3980 }
3981
David Sedlák3017da42019-02-15 09:48:04 +01003982 mod->parsed = mod_p;
3983
3984cleanup:
Michal Vasko193dacd2022-10-13 08:43:05 +02003985 LOG_LOCBACK(0, 0, 0, 1);
3986 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02003987 lysp_module_free(&fctx, mod_p);
Michal Vaskod0625d72022-10-06 15:02:50 +02003988 lysp_yin_ctx_free(*yin_ctx);
David Sedlák1b623122019-08-05 15:27:49 +02003989 *yin_ctx = NULL;
David Sedlák3017da42019-02-15 09:48:04 +01003990 }
David Sedlák2e411422018-12-17 02:35:39 +01003991 return ret;
David Sedlák3b4db242018-10-19 16:11:01 +02003992}