blob: 171fb36ddeb073883567217e4bacb6b0943047ef [file] [log] [blame]
David Sedlákf824ad52018-10-14 23:58:15 +02001/**
2 * @file parser_yin.c
3 * @author David Sedlák <xsedla1d@stud.fit.vutbr.cz>
David Sedlák3b4db242018-10-19 16:11:01 +02004 * @brief YIN parser.
5 *
David Sedlákb1ce3f82019-06-05 14:37:26 +02006 * Copyright (c) 2015 - 2019 CESNET, z.s.p.o.
David Sedlák3b4db242018-10-19 16:11:01 +02007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
David Sedlákf824ad52018-10-14 23:58:15 +020013 */
David Sedlák3ffbc522019-07-02 17:49:28 +020014#include <assert.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020015#include <ctype.h>
16#include <errno.h>
17#include <stdarg.h>
18#include <stdbool.h>
Radek Krejci47fab892020-11-05 17:02:41 +010019#include <stdint.h>
David Sedlák3b4db242018-10-19 16:11:01 +020020#include <stdio.h>
21#include <stdlib.h>
David Sedlák872c7b42018-10-26 13:15:20 +020022#include <string.h>
David Sedlákf824ad52018-10-14 23:58:15 +020023
Radek Krejci535ea9f2020-05-29 16:01:05 +020024#include "common.h"
David Sedlákf824ad52018-10-14 23:58:15 +020025#include "context.h"
David Sedlákecf5eb82019-06-03 14:12:44 +020026#include "dict.h"
Radek Krejci47fab892020-11-05 17:02:41 +010027#include "in.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020028#include "in_internal.h"
Radek Krejci47fab892020-11-05 17:02:41 +010029#include "log.h"
Michal Vasko63f3d842020-07-08 10:10:14 +020030#include "parser_internal.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020031#include "parser_schema.h"
Michal Vasko69730152020-10-09 16:30:07 +020032#include "path.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020033#include "set.h"
David Sedlákecf5eb82019-06-03 14:12:44 +020034#include "tree.h"
35#include "tree_schema.h"
David Sedlák3b4db242018-10-19 16:11:01 +020036#include "tree_schema_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020037#include "xml.h"
David Sedlák00250342019-06-21 14:19:39 +020038
David Sedlák2b214ac2019-06-06 16:11:03 +020039/**
40 * @brief check if given string is URI of yin namespace.
David Sedlák8985a142019-07-31 16:43:06 +020041 *
David Sedlák2b214ac2019-06-06 16:11:03 +020042 * @param ns Namespace URI to check.
43 *
44 * @return true if ns equals YIN_NS_URI false otherwise.
45 */
46#define IS_YIN_NS(ns) (strcmp(ns, YIN_NS_URI) == 0)
47
Michal Vaskoafac7822020-10-20 14:22:26 +020048enum yin_argument {
49 YIN_ARG_UNKNOWN = 0, /**< parsed argument can not be matched with any supported yin argument keyword */
50 YIN_ARG_NAME, /**< argument name */
51 YIN_ARG_TARGET_NODE, /**< argument target-node */
52 YIN_ARG_MODULE, /**< argument module */
53 YIN_ARG_VALUE, /**< argument value */
54 YIN_ARG_TEXT, /**< argument text */
55 YIN_ARG_CONDITION, /**< argument condition */
56 YIN_ARG_URI, /**< argument uri */
57 YIN_ARG_DATE, /**< argument data */
58 YIN_ARG_TAG, /**< argument tag */
59 YIN_ARG_NONE /**< empty (special value) */
60};
61
Michal Vasko22df3f02020-08-24 13:29:22 +020062const char * const yin_attr_list[] = {
David Sedlákf6251182019-06-06 10:22:13 +020063 [YIN_ARG_NAME] = "name",
64 [YIN_ARG_TARGET_NODE] = "target-node",
65 [YIN_ARG_MODULE] = "module",
66 [YIN_ARG_VALUE] = "value",
67 [YIN_ARG_TEXT] = "text",
68 [YIN_ARG_CONDITION] = "condition",
69 [YIN_ARG_URI] = "uri",
70 [YIN_ARG_DATE] = "date",
71 [YIN_ARG_TAG] = "tag",
David Sedlák071f7662019-09-12 02:02:51 +020072 [YIN_ARG_NONE] = "none",
David Sedlákf6251182019-06-06 10:22:13 +020073};
74
Michal Vaskoafac7822020-10-20 14:22:26 +020075#define yin_attr2str(STMT) yin_attr_list[STMT]
76
77#define VALID_VALS1 " Only valid value is \"%s\"."
78#define VALID_VALS2 " Valid values are \"%s\" and \"%s\"."
79#define VALID_VALS3 " Valid values are \"%s\", \"%s\" and \"%s\"."
80#define VALID_VALS4 " Valid values are \"%s\", \"%s\", \"%s\" and \"%s\"."
81
82/* shortcut to determin if keyword can in general be subelement of deviation regardles of it's type */
83#define isdevsub(kw) (kw == LY_STMT_CONFIG || kw == LY_STMT_DEFAULT || kw == LY_STMT_MANDATORY || \
84 kw == LY_STMT_MAX_ELEMENTS || kw == LY_STMT_MIN_ELEMENTS || \
85 kw == LY_STMT_MUST || kw == LY_STMT_TYPE || kw == LY_STMT_UNIQUE || \
86 kw == LY_STMT_UNITS || kw == LY_STMT_EXTENSION_INSTANCE)
87
88/* flags to set constraints of subelements */
89#define YIN_SUBELEM_MANDATORY 0x01 /**< is set when subelement is mandatory */
90#define YIN_SUBELEM_UNIQUE 0x02 /**< is set when subelement is unique */
91#define YIN_SUBELEM_FIRST 0x04 /**< is set when subelement is actually yang argument mapped to yin element */
92#define YIN_SUBELEM_VER2 0x08 /**< subelemnt is allowed only in modules with version at least 2 (YANG 1.1) */
93
94#define YIN_SUBELEM_PARSED 0x80 /**< is set during parsing when given subelement is encountered for the first
95 time to simply check validity of given constraints */
96
97struct yin_subelement {
98 enum ly_stmt type; /**< type of keyword */
99 void *dest; /**< meta infromation passed to responsible function (mostly information about where parsed subelement should be stored) */
100 uint16_t flags; /**< describes constraints of subelement can be set to YIN_SUBELEM_MANDATORY, YIN_SUBELEM_UNIQUE, YIN_SUBELEM_FIRST, YIN_SUBELEM_VER2, and YIN_SUBELEM_DEFAULT_TEXT */
101};
102
103/* Meta information passed to yin_parse_argument function,
104 holds information about where content of argument element will be stored. */
105struct yin_argument_meta {
106 uint16_t *flags; /**< Argument flags */
107 const char **argument; /**< Argument value */
108};
109
110/**
111 * @brief Meta information passed to functions working with tree_schema,
112 * that require additional information about parent node.
113 */
114struct tree_node_meta {
115 struct lysp_node *parent; /**< parent node */
116 struct lysp_node **nodes; /**< linked list of siblings */
117};
118
119/**
120 * @brief Meta information passed to yin_parse_import function.
121 */
122struct import_meta {
123 const char *prefix; /**< module prefix. */
124 struct lysp_import **imports; /**< imports to add to. */
125};
126
127/**
128 * @brief Meta information passed to yin_parse_include function.
129 */
130struct include_meta {
131 const char *name; /**< Module/submodule name. */
132 struct lysp_include **includes; /**< [Sized array](@ref sizedarrays) of parsed includes to add to. */
133};
134
135/**
136 * @brief Meta information passed to yin_parse_inout function.
137 */
138struct inout_meta {
139 struct lysp_node *parent; /**< Parent node. */
140 struct lysp_action_inout *inout_p; /**< inout_p Input/output pointer to write to. */
141};
142
143/**
144 * @brief Meta information passed to yin_parse_minmax function.
145 */
146struct minmax_dev_meta {
147 uint32_t *lim; /**< min/max value to write to. */
148 uint16_t *flags; /**< min/max flags to write to. */
149 struct lysp_ext_instance **exts; /**< extension instances to add to. */
150};
151
152LY_ERR yin_parse_content(struct lys_yin_parser_ctx *ctx, struct yin_subelement *subelem_info, size_t subelem_info_size,
153 enum ly_stmt current_element, const char **text_content, struct lysp_ext_instance **exts);
154
155/**
156 * @brief Match yang keyword from yin data.
157 *
158 * @param[in,out] ctx Yin parser context for logging and to store current state.
159 * @param[in] name Start of keyword name
160 * @param[in] name_len Lenght of keyword name.
161 * @param[in] prefix Start of keyword prefix.
162 * @param[in] prefix_len Lenght of prefix.
163 * @param[in] parrent Identification of parrent element, use LY_STMT_NONE for elements without parrent.
164 *
165 * @return yang_keyword values.
166 */
Radek Krejcid6b76452019-09-03 17:03:03 +0200167enum ly_stmt
Michal Vaskob36053d2020-03-26 15:49:30 +0100168yin_match_keyword(struct lys_yin_parser_ctx *ctx, const char *name, size_t name_len,
Radek Krejci0f969882020-08-21 16:56:47 +0200169 const char *prefix, size_t prefix_len, enum ly_stmt parent)
David Sedlák1bccdfa2019-06-17 15:55:27 +0200170{
David Sedlák8f7a1172019-06-20 14:42:18 +0200171 const char *start = NULL;
Radek Krejcid6b76452019-09-03 17:03:03 +0200172 enum ly_stmt kw = LY_STMT_NONE;
David Sedlák8f7a1172019-06-20 14:42:18 +0200173 const struct lyxml_ns *ns = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +0200174 struct ly_in *in;
David Sedlák8f7a1172019-06-20 14:42:18 +0200175
Michal Vasko69730152020-10-09 16:30:07 +0200176 if (!name || (name_len == 0)) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200177 return LY_STMT_NONE;
David Sedlák1bccdfa2019-06-17 15:55:27 +0200178 }
179
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200180 ns = lyxml_ns_get(&ctx->xmlctx->ns, prefix, prefix_len);
David Sedlák8f7a1172019-06-20 14:42:18 +0200181 if (ns) {
182 if (!IS_YIN_NS(ns->uri)) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200183 return LY_STMT_EXTENSION_INSTANCE;
David Sedlák8f7a1172019-06-20 14:42:18 +0200184 }
185 } else {
186 /* elements without namespace are automatically unknown */
Radek Krejcid6b76452019-09-03 17:03:03 +0200187 return LY_STMT_NONE;
David Sedlák8f7a1172019-06-20 14:42:18 +0200188 }
David Sedlák1bccdfa2019-06-17 15:55:27 +0200189
Michal Vasko63f3d842020-07-08 10:10:14 +0200190 LY_CHECK_RET(ly_in_new_memory(name, &in), LY_STMT_NONE);
191 start = in->current;
Radek Krejcid54412f2020-12-17 20:25:35 +0100192 kw = lysp_match_kw(in, NULL);
Michal Vasko63f3d842020-07-08 10:10:14 +0200193 name = in->current;
194 ly_in_free(in, 0);
David Sedlák8f7a1172019-06-20 14:42:18 +0200195
196 if (name - start == (long int)name_len) {
David Sedlákc1771b12019-07-10 15:55:46 +0200197 /* 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 +0200198 if ((kw == LY_STMT_VALUE) && (parent == LY_STMT_ERROR_MESSAGE)) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200199 return LY_STMT_ARG_VALUE;
David Sedlákc1771b12019-07-10 15:55:46 +0200200 }
David Sedlák1bccdfa2019-06-17 15:55:27 +0200201 return kw;
202 } else {
David Sedlák3ffbc522019-07-02 17:49:28 +0200203 if (strncmp(start, "text", name_len) == 0) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200204 return LY_STMT_ARG_TEXT;
David Sedlák3ffbc522019-07-02 17:49:28 +0200205 } else {
Radek Krejcid6b76452019-09-03 17:03:03 +0200206 return LY_STMT_NONE;
David Sedlák3ffbc522019-07-02 17:49:28 +0200207 }
David Sedlák1bccdfa2019-06-17 15:55:27 +0200208 }
209}
210
Michal Vaskoafac7822020-10-20 14:22:26 +0200211/**
212 * @brief Match argument name.
213 *
214 * @param[in] name String representing name.
215 * @param[in] len Lenght of the name.
216 *
217 * @return yin_argument values.
218 */
David Sedlákc5b20842019-08-13 10:18:31 +0200219enum yin_argument
David Sedlák060b00e2019-06-19 11:12:06 +0200220yin_match_argument_name(const char *name, size_t len)
David Sedlák3b4db242018-10-19 16:11:01 +0200221{
David Sedlákc5b20842019-08-13 10:18:31 +0200222 enum yin_argument arg = YIN_ARG_UNKNOWN;
David Sedlák872c7b42018-10-26 13:15:20 +0200223 size_t already_read = 0;
Michal Vasko69730152020-10-09 16:30:07 +0200224
David Sedlák7ff55a92019-06-17 11:11:41 +0200225 LY_CHECK_RET(len == 0, YIN_ARG_NONE);
David Sedlák3b4db242018-10-19 16:11:01 +0200226
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200227#define READ_INC(LEN) already_read += LEN
228#define ARG_SET(STMT) arg=STMT
229#define ARG_CHECK(STR, LEN) (!strncmp((name) + already_read, STR, LEN) && (READ_INC(LEN)))
David Sedlákc10e7902018-12-17 02:17:59 +0100230
David Sedlák1c8b2702019-02-22 11:03:02 +0100231 switch (*name) {
David Sedlák94de2aa2019-02-15 12:42:11 +0100232 case 'c':
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200233 READ_INC(1);
234 if (ARG_CHECK("ondition", 8)) {
235 ARG_SET(YIN_ARG_CONDITION);
236 }
David Sedlák3b4db242018-10-19 16:11:01 +0200237 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200238
David Sedlák94de2aa2019-02-15 12:42:11 +0100239 case 'd':
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200240 READ_INC(1);
241 if (ARG_CHECK("ate", 3)) {
242 ARG_SET(YIN_ARG_DATE);
243 }
David Sedlák3b4db242018-10-19 16:11:01 +0200244 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200245
David Sedlák94de2aa2019-02-15 12:42:11 +0100246 case 'm':
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200247 READ_INC(1);
248 if (ARG_CHECK("odule", 5)) {
249 ARG_SET(YIN_ARG_MODULE);
250 }
David Sedlák872c7b42018-10-26 13:15:20 +0200251 break;
252
David Sedlák94de2aa2019-02-15 12:42:11 +0100253 case 'n':
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200254 READ_INC(1);
255 if (ARG_CHECK("ame", 3)) {
256 ARG_SET(YIN_ARG_NAME);
257 }
David Sedlák872c7b42018-10-26 13:15:20 +0200258 break;
259
David Sedlák94de2aa2019-02-15 12:42:11 +0100260 case 't':
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200261 READ_INC(1);
262 if (ARG_CHECK("a", 1)) {
263 if (ARG_CHECK("g", 1)) {
264 ARG_SET(YIN_ARG_TAG);
265 } else if (ARG_CHECK("rget-node", 9)) {
266 ARG_SET(YIN_ARG_TARGET_NODE);
267 }
268 } else if (ARG_CHECK("ext", 3)) {
269 ARG_SET(YIN_ARG_TEXT);
270 }
David Sedlák3b4db242018-10-19 16:11:01 +0200271 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200272
David Sedlák94de2aa2019-02-15 12:42:11 +0100273 case 'u':
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200274 READ_INC(1);
275 if (ARG_CHECK("ri", 2)) {
276 ARG_SET(YIN_ARG_URI);
277 }
David Sedlák3b4db242018-10-19 16:11:01 +0200278 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200279
David Sedlák94de2aa2019-02-15 12:42:11 +0100280 case 'v':
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200281 READ_INC(1);
282 if (ARG_CHECK("alue", 4)) {
283 ARG_SET(YIN_ARG_VALUE);
284 }
David Sedlák3b4db242018-10-19 16:11:01 +0200285 break;
286 }
287
David Sedlákc10e7902018-12-17 02:17:59 +0100288 /* whole argument must be matched */
David Sedlák872c7b42018-10-26 13:15:20 +0200289 if (already_read != len) {
David Sedláka7406952019-04-05 10:33:07 +0200290 arg = YIN_ARG_UNKNOWN;
David Sedlák872c7b42018-10-26 13:15:20 +0200291 }
292
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200293#undef ARG_CHECK
294#undef ARG_SET
295#undef READ_INC
David Sedlák18730132019-03-15 15:51:34 +0100296
David Sedlák872c7b42018-10-26 13:15:20 +0200297 return arg;
David Sedlák3b4db242018-10-19 16:11:01 +0200298}
299
Radek Krejcid6b76452019-09-03 17:03:03 +0200300#define IS_NODE_ELEM(kw) (kw == LY_STMT_ANYXML || kw == LY_STMT_ANYDATA || kw == LY_STMT_LEAF || kw == LY_STMT_LEAF_LIST || \
301 kw == LY_STMT_TYPEDEF || kw == LY_STMT_USES || kw == LY_STMT_LIST || kw == LY_STMT_NOTIFICATION || \
302 kw == LY_STMT_GROUPING || kw == LY_STMT_CONTAINER || kw == LY_STMT_CASE || kw == LY_STMT_CHOICE || \
303 kw == LY_STMT_ACTION || kw == LY_STMT_RPC || kw == LY_STMT_AUGMENT)
David Sedlák81497a32019-08-13 16:56:26 +0200304
Radek Krejcid6b76452019-09-03 17:03:03 +0200305#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 +0200306
David Sedlák26ea1432019-08-14 13:42:23 +0200307/**
308 * @brief Free subelems information allocated on heap.
309 *
310 * @param[in] count Size of subelems array.
311 * @param[in] subelems Subelems array to free.
312 */
David Sedlák81497a32019-08-13 16:56:26 +0200313static void
314subelems_deallocator(size_t count, struct yin_subelement *subelems)
315{
Michal Vaskod989ba02020-08-24 10:59:24 +0200316 for (size_t i = 0; i < count; ++i) {
David Sedlák81497a32019-08-13 16:56:26 +0200317 if (HAS_META(subelems[i].type)) {
318 free(subelems[i].dest);
319 }
320 }
321
322 free(subelems);
323}
324
David Sedlák26ea1432019-08-14 13:42:23 +0200325/**
326 * @brief Allocate subelems information on heap.
327 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200328 * @param[in] ctx YIN parser context, used for logging.
David Sedlák26ea1432019-08-14 13:42:23 +0200329 * @param[in] count Number of subelements.
330 * @param[in] parent Parent node if any.
331 * @param[out] result Allocated subelems array.
332 *
333 * @return LY_SUCCESS on success LY_EMEM on memmory allocation failure.
334 */
David Sedlák81497a32019-08-13 16:56:26 +0200335static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100336subelems_allocator(struct lys_yin_parser_ctx *ctx, size_t count, struct lysp_node *parent,
Radek Krejci0f969882020-08-21 16:56:47 +0200337 struct yin_subelement **result, ...)
David Sedlák81497a32019-08-13 16:56:26 +0200338{
339 va_list ap;
340
341 *result = calloc(count, sizeof **result);
David Sedlákbf8a2b72019-08-14 16:48:10 +0200342 LY_CHECK_GOTO(!(*result), mem_err);
David Sedlák81497a32019-08-13 16:56:26 +0200343
344 va_start(ap, result);
345 for (size_t i = 0; i < count; ++i) {
346 /* TYPE */
Radek Krejcid6b76452019-09-03 17:03:03 +0200347 (*result)[i].type = va_arg(ap, enum ly_stmt);
David Sedlák81497a32019-08-13 16:56:26 +0200348 /* DEST */
349 if (IS_NODE_ELEM((*result)[i].type)) {
350 struct tree_node_meta *node_meta = NULL;
351 node_meta = calloc(1, sizeof *node_meta);
David Sedlákbf8a2b72019-08-14 16:48:10 +0200352 LY_CHECK_GOTO(!node_meta, mem_err);
David Sedlák81497a32019-08-13 16:56:26 +0200353 node_meta->parent = parent;
David Sedlákbf8a2b72019-08-14 16:48:10 +0200354 node_meta->nodes = va_arg(ap, void *);
David Sedlák81497a32019-08-13 16:56:26 +0200355 (*result)[i].dest = node_meta;
Radek Krejcid6b76452019-09-03 17:03:03 +0200356 } else if ((*result)[i].type == LY_STMT_IMPORT) {
David Sedlák81497a32019-08-13 16:56:26 +0200357 struct import_meta *imp_meta = NULL;
358 imp_meta = calloc(1, sizeof *imp_meta);
David Sedlákbf8a2b72019-08-14 16:48:10 +0200359 LY_CHECK_GOTO(!imp_meta, mem_err);
David Sedlák81497a32019-08-13 16:56:26 +0200360 imp_meta->prefix = va_arg(ap, const char *);
361 imp_meta->imports = va_arg(ap, struct lysp_import **);
362 (*result)[i].dest = imp_meta;
Radek Krejcid6b76452019-09-03 17:03:03 +0200363 } else if ((*result)[i].type == LY_STMT_INCLUDE) {
David Sedlák81497a32019-08-13 16:56:26 +0200364 struct include_meta *inc_meta = NULL;
365 inc_meta = calloc(1, sizeof *inc_meta);
David Sedlákbf8a2b72019-08-14 16:48:10 +0200366 LY_CHECK_GOTO(!inc_meta, mem_err);
David Sedlák81497a32019-08-13 16:56:26 +0200367 inc_meta->name = va_arg(ap, const char *);
368 inc_meta->includes = va_arg(ap, struct lysp_include **);
369 (*result)[i].dest = inc_meta;
Michal Vasko69730152020-10-09 16:30:07 +0200370 } else if (((*result)[i].type == LY_STMT_INPUT) || ((*result)[i].type == LY_STMT_OUTPUT)) {
David Sedlák81497a32019-08-13 16:56:26 +0200371 struct inout_meta *inout_meta = NULL;
372 inout_meta = calloc(1, sizeof *inout_meta);
David Sedlákbf8a2b72019-08-14 16:48:10 +0200373 LY_CHECK_GOTO(!inout_meta, mem_err);
David Sedlák81497a32019-08-13 16:56:26 +0200374 inout_meta->parent = parent;
375 inout_meta->inout_p = va_arg(ap, struct lysp_action_inout *);
376 (*result)[i].dest = inout_meta;
377 } else {
378 (*result)[i].dest = va_arg(ap, void *);
379 }
380 /* FLAGS */
381 (*result)[i].flags = va_arg(ap, int);
382 }
383 va_end(ap);
384
385 return LY_SUCCESS;
386
David Sedlákbf8a2b72019-08-14 16:48:10 +0200387mem_err:
David Sedlák81497a32019-08-13 16:56:26 +0200388 subelems_deallocator(count, *result);
Michal Vaskob36053d2020-03-26 15:49:30 +0100389 LOGMEM(ctx->xmlctx->ctx);
David Sedlák81497a32019-08-13 16:56:26 +0200390 return LY_EMEM;
391}
392
Michal Vaskoafac7822020-10-20 14:22:26 +0200393/**
394 * @brief Check that val is valid UTF8 character sequence of val_type.
395 * Doesn't check empty string, only character validity.
396 *
397 * @param[in] ctx Yin parser context for logging.
398 * @param[in] val_type Type of the input string to select method of checking character validity.
399 *
400 * @return LY_ERR values.
401 */
David Sedlák8f7a1172019-06-20 14:42:18 +0200402LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100403yin_validate_value(struct lys_yin_parser_ctx *ctx, enum yang_arg val_type)
David Sedlák4a650532019-07-10 11:55:18 +0200404{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200405 uint8_t prefix = 0;
406 uint32_t c;
Michal Vaskob36053d2020-03-26 15:49:30 +0100407 size_t utf8_char_len, already_read = 0;
408 const char *val;
409
410 assert((ctx->xmlctx->status == LYXML_ELEM_CONTENT) || (ctx->xmlctx->status == LYXML_ATTR_CONTENT));
411
412 val = ctx->xmlctx->value;
413 while (already_read < ctx->xmlctx->value_len) {
David Sedlák4a650532019-07-10 11:55:18 +0200414 LY_CHECK_ERR_RET(ly_getutf8((const char **)&val, &c, &utf8_char_len),
Michal Vasko69730152020-10-09 16:30:07 +0200415 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INCHAR, (val)[-utf8_char_len]), LY_EVALID);
David Sedlák4a650532019-07-10 11:55:18 +0200416 already_read += utf8_char_len;
Michal Vaskob36053d2020-03-26 15:49:30 +0100417 LY_CHECK_ERR_RET(already_read > ctx->xmlctx->value_len, LOGINT(ctx->xmlctx->ctx), LY_EINT);
David Sedlák4a650532019-07-10 11:55:18 +0200418
419 switch (val_type) {
420 case Y_IDENTIF_ARG:
Radek Krejci1deb5be2020-08-26 16:43:36 +0200421 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, already_read ? 0 : 1, NULL));
David Sedlák4a650532019-07-10 11:55:18 +0200422 break;
423 case Y_PREF_IDENTIF_ARG:
Radek Krejci1deb5be2020-08-26 16:43:36 +0200424 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, already_read ? 0 : 1, &prefix));
David Sedlák4a650532019-07-10 11:55:18 +0200425 break;
426 case Y_STR_ARG:
427 case Y_MAYBE_STR_ARG:
428 LY_CHECK_RET(lysp_check_stringchar((struct lys_parser_ctx *)ctx, c));
429 break;
430 }
431 }
432
433 return LY_SUCCESS;
434}
435
David Sedlákb4e44562019-07-04 15:42:12 +0200436/**
Michal Vaskob36053d2020-03-26 15:49:30 +0100437 * @brief Parse yin attributes.
David Sedlákb4e44562019-07-04 15:42:12 +0200438 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200439 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák4a650532019-07-10 11:55:18 +0200440 * @param[in] arg_type Type of argument that is expected in parsed element (use YIN_ARG_NONE for elements without
441 * special argument).
David Sedlákbf8a2b72019-08-14 16:48:10 +0200442 * @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 +0200443 * @param[in] val_type Type of expected value of attribute.
David Sedlákb4e44562019-07-04 15:42:12 +0200444 * @param[in] current_element Identification of current element, used for logging.
445 *
446 * @return LY_ERR values.
447 */
448static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100449yin_parse_attribute(struct lys_yin_parser_ctx *ctx, enum yin_argument arg_type, const char **arg_val, enum yang_arg val_type,
Radek Krejci0f969882020-08-21 16:56:47 +0200450 enum ly_stmt current_element)
David Sedlák8f7a1172019-06-20 14:42:18 +0200451{
David Sedlákc5b20842019-08-13 10:18:31 +0200452 enum yin_argument arg = YIN_ARG_UNKNOWN;
David Sedlák619db942019-07-03 14:47:30 +0200453 bool found = false;
David Sedlák8f7a1172019-06-20 14:42:18 +0200454
David Sedlák1bccdfa2019-06-17 15:55:27 +0200455 /* validation of attributes */
Michal Vaskob36053d2020-03-26 15:49:30 +0100456 while (ctx->xmlctx->status == LYXML_ATTRIBUTE) {
David Sedlák00250342019-06-21 14:19:39 +0200457 /* yin arguments represented as attributes have no namespace, which in this case means no prefix */
Michal Vaskob36053d2020-03-26 15:49:30 +0100458 if (!ctx->xmlctx->prefix) {
459 arg = yin_match_argument_name(ctx->xmlctx->name, ctx->xmlctx->name_len);
David Sedlák7ff55a92019-06-17 11:11:41 +0200460 if (arg == YIN_ARG_NONE) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100461 /* skip it */
462 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
David Sedlák7ff55a92019-06-17 11:11:41 +0200463 } else if (arg == arg_type) {
David Sedlák1538a842019-08-08 15:38:51 +0200464 LY_CHECK_ERR_RET(found, LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_DUP_ATTR,
Michal Vasko69730152020-10-09 16:30:07 +0200465 yin_attr2str(arg), ly_stmt2str(current_element)), LY_EVALID);
David Sedlák619db942019-07-03 14:47:30 +0200466 found = true;
Michal Vaskob36053d2020-03-26 15:49:30 +0100467
468 /* go to value */
469 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
470 LY_CHECK_RET(yin_validate_value(ctx, val_type));
Radek Krejci011e4aa2020-09-04 15:22:31 +0200471 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 +0200472 LY_CHECK_RET(!(*arg_val), LY_EMEM);
David Sedlák2b214ac2019-06-06 16:11:03 +0200473 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100474 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_UNEXP_ATTR, ctx->xmlctx->name_len,
Michal Vasko69730152020-10-09 16:30:07 +0200475 ctx->xmlctx->name, ly_stmt2str(current_element));
David Sedlák619db942019-07-03 14:47:30 +0200476 return LY_EVALID;
David Sedláka7406952019-04-05 10:33:07 +0200477 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100478 } else {
479 /* skip it */
480 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
David Sedláka7406952019-04-05 10:33:07 +0200481 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100482
483 /* next attribute */
484 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
David Sedláka7406952019-04-05 10:33:07 +0200485 }
486
David Sedlák292763b2019-07-09 11:10:53 +0200487 /* anything else than Y_MAYBE_STR_ARG is mandatory */
Michal Vasko69730152020-10-09 16:30:07 +0200488 if ((val_type != Y_MAYBE_STR_ARG) && !found) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100489 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LYVE_SYNTAX_YIN, "Missing mandatory attribute %s of %s element.",
Michal Vasko69730152020-10-09 16:30:07 +0200490 yin_attr2str(arg_type), ly_stmt2str(current_element));
David Sedlák619db942019-07-03 14:47:30 +0200491 return LY_EVALID;
492 }
493
494 return LY_SUCCESS;
David Sedláka7406952019-04-05 10:33:07 +0200495}
496
David Sedlákd6e56892019-07-01 15:40:24 +0200497/**
Radek Krejci12b1c152019-09-05 16:20:48 +0200498 * @brief Get record with given type.
David Sedlákd6e56892019-07-01 15:40:24 +0200499 *
500 * @param[in] type Type of wanted record.
501 * @param[in] array_size Size of array.
502 * @param[in] array Searched array.
503 *
504 * @return Pointer to desired record on success, NULL if element is not in the array.
505 */
David Sedlákb4e44562019-07-04 15:42:12 +0200506static struct yin_subelement *
Radek Krejci1deb5be2020-08-26 16:43:36 +0200507get_record(enum ly_stmt type, LY_ARRAY_COUNT_TYPE array_size, struct yin_subelement *array)
David Sedlákd6e56892019-07-01 15:40:24 +0200508{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200509 for (LY_ARRAY_COUNT_TYPE u = 0; u < array_size; ++u) {
Radek Krejci12b1c152019-09-05 16:20:48 +0200510 if (array[u].type == type) {
511 return &array[u];
David Sedlákd6e56892019-07-01 15:40:24 +0200512 }
513 }
David Sedlákd6e56892019-07-01 15:40:24 +0200514 return NULL;
515}
516
David Sedlákbba38e52019-07-09 15:20:01 +0200517/**
518 * @brief Helper function to check mandatory constraint of subelement.
519 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200520 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200521 * @param[in] subelem_info Array of information about subelements.
522 * @param[in] subelem_info_size Size of subelem_info array.
523 * @param[in] current_element Identification of element that is currently being parsed, used for logging.
524 *
525 * @return LY_ERR values.
526 */
527static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100528yin_check_subelem_mandatory_constraint(struct lys_yin_parser_ctx *ctx, struct yin_subelement *subelem_info,
Radek Krejci0f969882020-08-21 16:56:47 +0200529 signed char subelem_info_size, enum ly_stmt current_element)
David Sedlák21f87cd2019-07-03 16:53:23 +0200530{
David Sedlákb0faad82019-07-04 14:28:59 +0200531 for (signed char i = 0; i < subelem_info_size; ++i) {
David Sedlák5545f5d2019-07-11 11:55:16 +0200532 /* if there is element that is mandatory and isn't parsed log error and return LY_EVALID */
Radek Krejci12b1c152019-09-05 16:20:48 +0200533 if ((subelem_info[i].flags & YIN_SUBELEM_MANDATORY) && !(subelem_info[i].flags & YIN_SUBELEM_PARSED)) {
David Sedlák1538a842019-08-08 15:38:51 +0200534 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_MAND_SUBELEM,
Michal Vasko69730152020-10-09 16:30:07 +0200535 ly_stmt2str(subelem_info[i].type), ly_stmt2str(current_element));
David Sedlák21f87cd2019-07-03 16:53:23 +0200536 return LY_EVALID;
537 }
538 }
539
540 return LY_SUCCESS;
541}
542
David Sedlákbba38e52019-07-09 15:20:01 +0200543/**
544 * @brief Helper function to check "first" constraint of subelement.
545 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200546 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200547 * @param[in] subelem_info Array of information about subelements.
548 * @param[in] subelem_info_size Size of subelem_info array.
549 * @param[in] current_element Identification of element that is currently being parsed, used for logging.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200550 * @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 +0200551 *
552 * @return LY_ERR values.
553 */
554static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100555yin_check_subelem_first_constraint(struct lys_yin_parser_ctx *ctx, struct yin_subelement *subelem_info,
Radek Krejci0f969882020-08-21 16:56:47 +0200556 signed char subelem_info_size, enum ly_stmt current_element,
557 struct yin_subelement *exp_first)
David Sedlák21f87cd2019-07-03 16:53:23 +0200558{
David Sedlákb0faad82019-07-04 14:28:59 +0200559 for (signed char i = 0; i < subelem_info_size; ++i) {
David Sedlák21f87cd2019-07-03 16:53:23 +0200560 if (subelem_info[i].flags & YIN_SUBELEM_PARSED) {
David Sedlák1538a842019-08-08 15:38:51 +0200561 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_FIRT_SUBELEM,
Michal Vasko69730152020-10-09 16:30:07 +0200562 ly_stmt2str(exp_first->type), ly_stmt2str(current_element));
David Sedlák21f87cd2019-07-03 16:53:23 +0200563 return LY_EVALID;
564 }
565 }
566
567 return LY_SUCCESS;
568}
569
David Sedlákbba38e52019-07-09 15:20:01 +0200570/**
David Sedlákb4e44562019-07-04 15:42:12 +0200571 * @brief Parse simple element without any special constraints and argument mapped to yin attribute,
572 * for example prefix or namespace element.
David Sedlákd6e56892019-07-01 15:40:24 +0200573 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200574 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákb4e44562019-07-04 15:42:12 +0200575 * @param[in] kw Type of current element.
576 * @param[out] value Where value of attribute should be stored.
577 * @param[in] arg_type Expected type of attribute.
David Sedlák292763b2019-07-09 11:10:53 +0200578 * @param[in] arg_val_type Type of expected value of attribute.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200579 * @param[in,out] exts Extension instances to add to.
David Sedlákd6e56892019-07-01 15:40:24 +0200580 * @return LY_ERR values.
581 */
David Sedlákb4e44562019-07-04 15:42:12 +0200582static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100583yin_parse_simple_element(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, const char **value,
Radek Krejci0f969882020-08-21 16:56:47 +0200584 enum yin_argument arg_type, enum yang_arg arg_val_type, struct lysp_ext_instance **exts)
David Sedlákb4e44562019-07-04 15:42:12 +0200585{
Michal Vaskob36053d2020-03-26 15:49:30 +0100586 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
587 LY_CHECK_RET(yin_parse_attribute(ctx, arg_type, value, arg_val_type, kw));
Radek Krejcif13b87b2020-12-01 22:02:17 +0100588 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200589 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
590 };
David Sedlákb4e44562019-07-04 15:42:12 +0200591
Radek Krejcif13b87b2020-12-01 22:02:17 +0100592 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), kw, NULL, exts);
David Sedlákb4e44562019-07-04 15:42:12 +0200593}
594
595/**
David Sedlák6542aed2019-08-14 10:47:43 +0200596 * @brief Parse path element.
597 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200598 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák6542aed2019-08-14 10:47:43 +0200599 * @param[in] kw Type of current element.
600 * @param[out] type Type structure to store parsed value, flags and extension instances.
David Sedlák6542aed2019-08-14 10:47:43 +0200601 * @return LY_ERR values.
602 */
603static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100604yin_parse_path(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, struct lysp_type *type)
David Sedlák6542aed2019-08-14 10:47:43 +0200605{
Michal Vasko004d3152020-06-11 19:59:22 +0200606 LY_ERR ret;
607 const char *str_path;
608
609 LY_CHECK_RET(yin_parse_simple_element(ctx, kw, &str_path, YIN_ARG_VALUE, Y_STR_ARG, &type->exts));
Michal Vasko6b26e742020-07-17 15:02:10 +0200610 ret = ly_path_parse(ctx->xmlctx->ctx, NULL, str_path, 0, LY_PATH_BEGIN_EITHER, LY_PATH_LREF_TRUE,
Michal Vasko69730152020-10-09 16:30:07 +0200611 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path);
Michal Vasko004d3152020-06-11 19:59:22 +0200612 lydict_remove(ctx->xmlctx->ctx, str_path);
613 LY_CHECK_RET(ret);
David Sedlák6542aed2019-08-14 10:47:43 +0200614 type->flags |= LYS_SET_PATH;
615
616 return LY_SUCCESS;
617}
618
619/**
David Sedlákd3983112019-07-12 11:20:56 +0200620 * @brief Parse pattern element.
621 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200622 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200623 * @param[in,out] type Type structure to store parsed value, flags and extension instances.
David Sedlákd3983112019-07-12 11:20:56 +0200624 * @return LY_ERR values.
625 */
626static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100627yin_parse_pattern(struct lys_yin_parser_ctx *ctx, struct lysp_type *type)
David Sedlákd3983112019-07-12 11:20:56 +0200628{
629 const char *real_value = NULL;
630 char *saved_value = NULL;
631 struct lysp_restr *restr;
632
Michal Vaskob36053d2020-03-26 15:49:30 +0100633 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, type->patterns, restr, LY_EMEM);
634 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
635 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 +0200636 size_t len = strlen(real_value);
637
638 saved_value = malloc(len + 2);
Michal Vaskob36053d2020-03-26 15:49:30 +0100639 LY_CHECK_ERR_RET(!saved_value, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
David Sedlákd3983112019-07-12 11:20:56 +0200640 memmove(saved_value + 1, real_value, len);
Michal Vaskob36053d2020-03-26 15:49:30 +0100641 FREE_STRING(ctx->xmlctx->ctx, real_value);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100642 saved_value[0] = LYSP_RESTR_PATTERN_ACK;
David Sedlákd3983112019-07-12 11:20:56 +0200643 saved_value[len + 1] = '\0';
Michal Vasko7f45cf22020-10-01 12:49:44 +0200644 LY_CHECK_RET(lydict_insert_zc(ctx->xmlctx->ctx, saved_value, &restr->arg.str));
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200645 restr->arg.mod = ctx->parsed_mod;
David Sedlákd3983112019-07-12 11:20:56 +0200646 type->flags |= LYS_SET_PATTERN;
647
Radek Krejcif13b87b2020-12-01 22:02:17 +0100648 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200649 {LY_STMT_DESCRIPTION, &restr->dsc, YIN_SUBELEM_UNIQUE},
650 {LY_STMT_ERROR_APP_TAG, &restr->eapptag, YIN_SUBELEM_UNIQUE},
651 {LY_STMT_ERROR_MESSAGE, &restr->emsg, YIN_SUBELEM_UNIQUE},
652 {LY_STMT_MODIFIER, &restr->arg, YIN_SUBELEM_UNIQUE},
653 {LY_STMT_REFERENCE, &restr->ref, YIN_SUBELEM_UNIQUE},
654 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
655 };
Michal Vasko69730152020-10-09 16:30:07 +0200656
Radek Krejcif13b87b2020-12-01 22:02:17 +0100657 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_PATTERN, NULL, &restr->exts);
David Sedlákd3983112019-07-12 11:20:56 +0200658}
659
David Sedlákc5b20842019-08-13 10:18:31 +0200660/**
661 * @brief Parse fraction-digits element.
662 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200663 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákc5b20842019-08-13 10:18:31 +0200664 * @param[in,out] type Type structure to store value, flags and extension instances.
David Sedlákc5b20842019-08-13 10:18:31 +0200665 * @return LY_ERR values.
666 */
David Sedlákf75d55e2019-07-12 16:52:50 +0200667static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100668yin_parse_fracdigits(struct lys_yin_parser_ctx *ctx, struct lysp_type *type)
David Sedlákf75d55e2019-07-12 16:52:50 +0200669{
670 const char *temp_val = NULL;
671 char *ptr;
672 unsigned long int num;
673
Michal Vaskob36053d2020-03-26 15:49:30 +0100674 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
675 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 +0200676
Michal Vasko69730152020-10-09 16:30:07 +0200677 if ((temp_val[0] == '\0') || (temp_val[0] == '0') || !isdigit(temp_val[0])) {
David Sedlák1538a842019-08-08 15:38:51 +0200678 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "fraction-digits");
Michal Vaskob36053d2020-03-26 15:49:30 +0100679 FREE_STRING(ctx->xmlctx->ctx, temp_val);
David Sedlákf75d55e2019-07-12 16:52:50 +0200680 return LY_EVALID;
681 }
682
683 errno = 0;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100684 num = strtoul(temp_val, &ptr, LY_BASE_DEC);
David Sedlákf75d55e2019-07-12 16:52:50 +0200685 if (*ptr != '\0') {
David Sedlák1538a842019-08-08 15:38:51 +0200686 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "fraction-digits");
Michal Vaskob36053d2020-03-26 15:49:30 +0100687 FREE_STRING(ctx->xmlctx->ctx, temp_val);
David Sedlákf75d55e2019-07-12 16:52:50 +0200688 return LY_EVALID;
689 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100690 if ((errno == ERANGE) || (num > LY_TYPE_DEC64_FD_MAX)) {
David Sedlák1538a842019-08-08 15:38:51 +0200691 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "fraction-digits");
Michal Vaskob36053d2020-03-26 15:49:30 +0100692 FREE_STRING(ctx->xmlctx->ctx, temp_val);
David Sedlákf75d55e2019-07-12 16:52:50 +0200693 return LY_EVALID;
694 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100695 FREE_STRING(ctx->xmlctx->ctx, temp_val);
David Sedlákf75d55e2019-07-12 16:52:50 +0200696 type->fraction_digits = num;
697 type->flags |= LYS_SET_FRDIGITS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100698 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200699 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
700 };
Michal Vasko69730152020-10-09 16:30:07 +0200701
Radek Krejcif13b87b2020-12-01 22:02:17 +0100702 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_FRACTION_DIGITS, NULL, &type->exts);
David Sedlákf75d55e2019-07-12 16:52:50 +0200703}
704
David Sedlák07869a52019-07-12 14:28:19 +0200705/**
David Sedlák43801c92019-08-05 15:58:54 +0200706 * @brief Parse enum element.
David Sedlák07869a52019-07-12 14:28:19 +0200707 *
708 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200709 * @param[in,out] type Type structure to store parsed value, flags and extension instances.
David Sedlák07869a52019-07-12 14:28:19 +0200710 * @return LY_ERR values.
711 */
David Sedlákca36c422019-07-12 12:47:55 +0200712static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100713yin_parse_enum(struct lys_yin_parser_ctx *ctx, struct lysp_type *type)
David Sedlákca36c422019-07-12 12:47:55 +0200714{
David Sedlák07869a52019-07-12 14:28:19 +0200715 struct lysp_type_enum *en;
David Sedlák1e696782019-07-17 15:06:07 +0200716
Michal Vaskob36053d2020-03-26 15:49:30 +0100717 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, type->enums, en, LY_EMEM);
David Sedlák43801c92019-08-05 15:58:54 +0200718 type->flags |= LYS_SET_ENUM;
Michal Vaskob36053d2020-03-26 15:49:30 +0100719 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
720 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &en->name, Y_STR_ARG, LY_STMT_ENUM));
David Sedlák43801c92019-08-05 15:58:54 +0200721 LY_CHECK_RET(lysp_check_enum_name((struct lys_parser_ctx *)ctx, en->name, strlen(en->name)));
Michal Vaskob36053d2020-03-26 15:49:30 +0100722 CHECK_NONEMPTY((struct lys_parser_ctx *)ctx, strlen(en->name), "enum");
David Sedlák43801c92019-08-05 15:58:54 +0200723 CHECK_UNIQUENESS((struct lys_parser_ctx *)ctx, type->enums, name, "enum", en->name);
David Sedlákca36c422019-07-12 12:47:55 +0200724
Radek Krejcif13b87b2020-12-01 22:02:17 +0100725 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200726 {LY_STMT_DESCRIPTION, &en->dsc, YIN_SUBELEM_UNIQUE},
727 {LY_STMT_IF_FEATURE, &en->iffeatures, 0},
728 {LY_STMT_REFERENCE, &en->ref, YIN_SUBELEM_UNIQUE},
729 {LY_STMT_STATUS, &en->flags, YIN_SUBELEM_UNIQUE},
730 {LY_STMT_VALUE, en, YIN_SUBELEM_UNIQUE},
731 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
732 };
Michal Vasko69730152020-10-09 16:30:07 +0200733
Radek Krejcif13b87b2020-12-01 22:02:17 +0100734 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_ENUM, NULL, &en->exts);
David Sedlák43801c92019-08-05 15:58:54 +0200735}
736
737/**
738 * @brief Parse bit element.
739 *
740 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200741 * @param[in,out] type Type structure to store parsed value, flags and extension instances.
David Sedlák43801c92019-08-05 15:58:54 +0200742 * @return LY_ERR values.
743 */
744static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100745yin_parse_bit(struct lys_yin_parser_ctx *ctx, struct lysp_type *type)
David Sedlák43801c92019-08-05 15:58:54 +0200746{
747 struct lysp_type_enum *en;
748
Michal Vaskob36053d2020-03-26 15:49:30 +0100749 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, type->bits, en, LY_EMEM);
David Sedlák43801c92019-08-05 15:58:54 +0200750 type->flags |= LYS_SET_BIT;
Michal Vaskob36053d2020-03-26 15:49:30 +0100751 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
752 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &en->name, Y_IDENTIF_ARG, LY_STMT_BIT));
David Sedlák43801c92019-08-05 15:58:54 +0200753 CHECK_UNIQUENESS((struct lys_parser_ctx *)ctx, type->enums, name, "bit", en->name);
754
Radek Krejcif13b87b2020-12-01 22:02:17 +0100755 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200756 {LY_STMT_DESCRIPTION, &en->dsc, YIN_SUBELEM_UNIQUE},
757 {LY_STMT_IF_FEATURE, &en->iffeatures, 0},
758 {LY_STMT_POSITION, en, YIN_SUBELEM_UNIQUE},
759 {LY_STMT_REFERENCE, &en->ref, YIN_SUBELEM_UNIQUE},
760 {LY_STMT_STATUS, &en->flags, YIN_SUBELEM_UNIQUE},
761 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
762 };
Michal Vasko69730152020-10-09 16:30:07 +0200763
Radek Krejcif13b87b2020-12-01 22:02:17 +0100764 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_BIT, NULL, &en->exts);
David Sedlákca36c422019-07-12 12:47:55 +0200765}
766
David Sedlákd3983112019-07-12 11:20:56 +0200767/**
David Sedlák5f8191e2019-07-08 16:35:52 +0200768 * @brief Parse simple element without any special constraints and argument mapped to yin attribute, that can have
769 * more instances, such as base or if-feature.
770 *
771 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák5f8191e2019-07-08 16:35:52 +0200772 * @param[in] kw Type of current element.
773 * @param[out] values Parsed values to add to.
774 * @param[in] arg_type Expected type of attribute.
David Sedlák292763b2019-07-09 11:10:53 +0200775 * @param[in] arg_val_type Type of expected value of attribute.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200776 * @param[in,out] exts Extension instances to add to.
David Sedlák5f8191e2019-07-08 16:35:52 +0200777 * @return LY_ERR values.
778 */
779static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100780yin_parse_simple_elements(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, const char ***values, enum yin_argument arg_type,
Radek Krejci0f969882020-08-21 16:56:47 +0200781 enum yang_arg arg_val_type, struct lysp_ext_instance **exts)
David Sedlák5f8191e2019-07-08 16:35:52 +0200782{
783 const char **value;
Michal Vasko69730152020-10-09 16:30:07 +0200784
Michal Vaskob36053d2020-03-26 15:49:30 +0100785 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *values, value, LY_EMEM);
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200786 LY_ARRAY_COUNT_TYPE index = LY_ARRAY_COUNT(*values) - 1;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100787 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200788 {LY_STMT_EXTENSION_INSTANCE, &index, 0}
789 };
David Sedlák968ac342019-07-11 15:17:59 +0200790
Michal Vaskob36053d2020-03-26 15:49:30 +0100791 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
792 LY_CHECK_RET(yin_parse_attribute(ctx, arg_type, value, arg_val_type, kw));
David Sedlák5f8191e2019-07-08 16:35:52 +0200793
Radek Krejcif13b87b2020-12-01 22:02:17 +0100794 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), kw, NULL, exts);
David Sedlák5f8191e2019-07-08 16:35:52 +0200795}
796
797/**
David Sedlák6542aed2019-08-14 10:47:43 +0200798 * @brief Parse simple element without any special constraints and argument mapped to yin attribute.
799 *
800 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák6542aed2019-08-14 10:47:43 +0200801 * @param[in] kw Type of current element.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200802 * @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 +0200803 * @param[in] arg_type Expected type of attribute.
804 * @param[in] arg_val_type Type of expected value of attribute.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200805 * @param[in,out] exts Extension instances to add to.
David Sedlák6542aed2019-08-14 10:47:43 +0200806 * @return LY_ERR values.
807 */
808static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100809yin_parse_simple_elem(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, struct yin_subelement *subinfo,
Radek Krejci0f969882020-08-21 16:56:47 +0200810 enum yin_argument arg_type, enum yang_arg arg_val_type, struct lysp_ext_instance **exts)
David Sedlák6542aed2019-08-14 10:47:43 +0200811{
812 if (subinfo->flags & YIN_SUBELEM_UNIQUE) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100813 LY_CHECK_RET(yin_parse_simple_element(ctx, kw, (const char **)subinfo->dest,
Michal Vasko69730152020-10-09 16:30:07 +0200814 arg_type, arg_val_type, exts));
David Sedlák6542aed2019-08-14 10:47:43 +0200815 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100816 LY_CHECK_RET(yin_parse_simple_elements(ctx, kw, (const char ***)subinfo->dest,
Michal Vasko69730152020-10-09 16:30:07 +0200817 arg_type, arg_val_type, exts));
David Sedlák6542aed2019-08-14 10:47:43 +0200818 }
819
820 return LY_SUCCESS;
821}
822
823/**
824 * @brief Parse base element.
825 *
826 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák6542aed2019-08-14 10:47:43 +0200827 * @param[in] parent Identification of parent element.
828 * @param[out] dest Where parsed values should be stored.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200829 * @param[in,out] exts Extension instances to add to.
David Sedlák6542aed2019-08-14 10:47:43 +0200830 * @return LY_ERR values.
831 */
832static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100833yin_parse_base(struct lys_yin_parser_ctx *ctx, enum ly_stmt parent, void *dest, struct lysp_ext_instance **exts)
David Sedlák6542aed2019-08-14 10:47:43 +0200834{
835 struct lysp_type *type = NULL;
836
Radek Krejcid6b76452019-09-03 17:03:03 +0200837 if (parent == LY_STMT_TYPE) {
David Sedlák6542aed2019-08-14 10:47:43 +0200838 type = (struct lysp_type *)dest;
Michal Vaskob36053d2020-03-26 15:49:30 +0100839 LY_CHECK_RET(yin_parse_simple_elements(ctx, LY_STMT_BASE, &type->bases, YIN_ARG_NAME,
Michal Vasko69730152020-10-09 16:30:07 +0200840 Y_PREF_IDENTIF_ARG, exts));
David Sedlák6542aed2019-08-14 10:47:43 +0200841 type->flags |= LYS_SET_BASE;
Radek Krejcid6b76452019-09-03 17:03:03 +0200842 } else if (parent == LY_STMT_IDENTITY) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100843 LY_CHECK_RET(yin_parse_simple_elements(ctx, LY_STMT_BASE, (const char ***)dest,
Michal Vasko69730152020-10-09 16:30:07 +0200844 YIN_ARG_NAME, Y_PREF_IDENTIF_ARG, exts));
David Sedlák6542aed2019-08-14 10:47:43 +0200845 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100846 LOGINT(ctx->xmlctx->ctx);
David Sedlák6542aed2019-08-14 10:47:43 +0200847 return LY_EINT;
848 }
849
850 return LY_SUCCESS;
851}
852
853/**
David Sedlákbf8a2b72019-08-14 16:48:10 +0200854 * @brief Parse require-instance element.
David Sedlákcf5569a2019-07-11 13:31:34 +0200855 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200856 * @param[in,out] ctx YIN parser context for logging and to store current state.
Michal Vaskob36053d2020-03-26 15:49:30 +0100857 * @param[out] type Type structure to store value, flag and extensions.
David Sedlákcf5569a2019-07-11 13:31:34 +0200858 * @return LY_ERR values.
859 */
860static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100861yin_pasrse_reqinstance(struct lys_yin_parser_ctx *ctx, struct lysp_type *type)
David Sedlákcf5569a2019-07-11 13:31:34 +0200862{
863 const char *temp_val = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100864 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200865 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
866 };
David Sedlákcf5569a2019-07-11 13:31:34 +0200867
868 type->flags |= LYS_SET_REQINST;
Michal Vaskob36053d2020-03-26 15:49:30 +0100869 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
870 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 +0200871 if (strcmp(temp_val, "true") == 0) {
872 type->require_instance = 1;
873 } else if (strcmp(temp_val, "false") != 0) {
David Sedlák26ea1432019-08-14 13:42:23 +0200874 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value",
Michal Vasko69730152020-10-09 16:30:07 +0200875 "require-instance", "true", "false");
Michal Vaskob36053d2020-03-26 15:49:30 +0100876 FREE_STRING(ctx->xmlctx->ctx, temp_val);
David Sedlákcf5569a2019-07-11 13:31:34 +0200877 return LY_EVALID;
878 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100879 FREE_STRING(ctx->xmlctx->ctx, temp_val);
David Sedlákcf5569a2019-07-11 13:31:34 +0200880
Radek Krejcif13b87b2020-12-01 22:02:17 +0100881 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_REQUIRE_INSTANCE, NULL, &type->exts);
David Sedlákcf5569a2019-07-11 13:31:34 +0200882}
883
884/**
David Sedlákce77bf52019-07-11 16:59:31 +0200885 * @brief Parse modifier element.
886 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200887 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákce77bf52019-07-11 16:59:31 +0200888 * @param[in,out] pat Value to write to.
889 * @param[in,out] exts Extension instances to add to.
890 *
891 * @return LY_ERR values.
892 */
893static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100894yin_parse_modifier(struct lys_yin_parser_ctx *ctx, const char **pat, struct lysp_ext_instance **exts)
David Sedlákce77bf52019-07-11 16:59:31 +0200895{
David Sedlákd3983112019-07-12 11:20:56 +0200896 assert(**pat == 0x06);
David Sedlákce77bf52019-07-11 16:59:31 +0200897 const char *temp_val;
898 char *modified_val;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100899 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200900 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
901 };
David Sedlákce77bf52019-07-11 16:59:31 +0200902
Michal Vaskob36053d2020-03-26 15:49:30 +0100903 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
904 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 +0200905 if (strcmp(temp_val, "invert-match") != 0) {
David Sedlák26ea1432019-08-14 13:42:23 +0200906 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS1, temp_val, "value",
Michal Vasko69730152020-10-09 16:30:07 +0200907 "modifier", "invert-match");
Michal Vaskob36053d2020-03-26 15:49:30 +0100908 FREE_STRING(ctx->xmlctx->ctx, temp_val);
David Sedlákce77bf52019-07-11 16:59:31 +0200909 return LY_EVALID;
910 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100911 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákce77bf52019-07-11 16:59:31 +0200912
913 /* allocate new value */
David Sedlákd3983112019-07-12 11:20:56 +0200914 modified_val = malloc(strlen(*pat) + 1);
Michal Vaskob36053d2020-03-26 15:49:30 +0100915 LY_CHECK_ERR_RET(!modified_val, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
David Sedlákd3983112019-07-12 11:20:56 +0200916 strcpy(modified_val, *pat);
Michal Vaskob36053d2020-03-26 15:49:30 +0100917 lydict_remove(ctx->xmlctx->ctx, *pat);
David Sedlákce77bf52019-07-11 16:59:31 +0200918
919 /* modify the new value */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100920 modified_val[0] = LYSP_RESTR_PATTERN_NACK;
Radek Krejci011e4aa2020-09-04 15:22:31 +0200921 LY_CHECK_RET(lydict_insert_zc(ctx->xmlctx->ctx, modified_val, pat));
David Sedlákce77bf52019-07-11 16:59:31 +0200922
Radek Krejcif13b87b2020-12-01 22:02:17 +0100923 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_MODIFIER, NULL, exts);
David Sedlákce77bf52019-07-11 16:59:31 +0200924}
925
926/**
David Sedlákb7296dd2019-07-11 14:58:38 +0200927 * @brief Parse a restriction element (length, range or one instance of must).
928 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200929 * @param[in,out] ctx YIN parser context for logging and to store current state.
Radek Krejcid6b76452019-09-03 17:03:03 +0200930 * @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 +0200931 * @param[in] restr Value to write to.
David Sedlákb7296dd2019-07-11 14:58:38 +0200932 */
933static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100934yin_parse_restriction(struct lys_yin_parser_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr *restr)
David Sedlákb7296dd2019-07-11 14:58:38 +0200935{
Radek Krejcid6b76452019-09-03 17:03:03 +0200936 assert(restr_kw == LY_STMT_MUST || restr_kw == LY_STMT_LENGTH || restr_kw == LY_STMT_RANGE);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100937 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200938 {LY_STMT_DESCRIPTION, &restr->dsc, YIN_SUBELEM_UNIQUE},
939 {LY_STMT_ERROR_APP_TAG, &restr->eapptag, YIN_SUBELEM_UNIQUE},
940 {LY_STMT_ERROR_MESSAGE, &restr->emsg, YIN_SUBELEM_UNIQUE},
941 {LY_STMT_REFERENCE, &restr->ref, YIN_SUBELEM_UNIQUE},
942 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
943 };
David Sedlákb7296dd2019-07-11 14:58:38 +0200944 /* argument of must is called condition, but argument of length and range is called value */
Radek Krejcid6b76452019-09-03 17:03:03 +0200945 enum yin_argument arg_type = (restr_kw == LY_STMT_MUST) ? YIN_ARG_CONDITION : YIN_ARG_VALUE;
David Sedlákb7296dd2019-07-11 14:58:38 +0200946
Michal Vaskob36053d2020-03-26 15:49:30 +0100947 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
Michal Vasko7f45cf22020-10-01 12:49:44 +0200948 LY_CHECK_RET(yin_parse_attribute(ctx, arg_type, &restr->arg.str, Y_STR_ARG, restr_kw));
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200949 restr->arg.mod = ctx->parsed_mod;
Michal Vaskob36053d2020-03-26 15:49:30 +0100950
Radek Krejcif13b87b2020-12-01 22:02:17 +0100951 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), restr_kw, NULL, &restr->exts);
David Sedlákb7296dd2019-07-11 14:58:38 +0200952}
953
954/**
David Sedlák6542aed2019-08-14 10:47:43 +0200955 * @brief Parse range element.
956 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200957 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák6542aed2019-08-14 10:47:43 +0200958 * @param[out] type Type structure to store parsed value and flags.
959 *
960 * @return LY_ERR values.
961 */
962static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100963yin_parse_range(struct lys_yin_parser_ctx *ctx, struct lysp_type *type)
David Sedlák6542aed2019-08-14 10:47:43 +0200964{
965 type->range = calloc(1, sizeof *type->range);
Michal Vaskob36053d2020-03-26 15:49:30 +0100966 LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
967 LY_CHECK_RET(yin_parse_restriction(ctx, LY_STMT_RANGE, type->range));
Michal Vasko44f3d2c2020-08-24 09:49:38 +0200968 type->flags |= LYS_SET_RANGE;
David Sedlák6542aed2019-08-14 10:47:43 +0200969
970 return LY_SUCCESS;
971}
972
973/**
974 * @brief Parse length element.
975 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200976 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák6542aed2019-08-14 10:47:43 +0200977 * @param[out] type Type structure to store parsed value and flags.
978 *
979 * @return LY_ERR values.
980 */
981static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100982yin_parse_length(struct lys_yin_parser_ctx *ctx, struct lysp_type *type)
David Sedlák6542aed2019-08-14 10:47:43 +0200983{
984 type->length = calloc(1, sizeof *type->length);
Michal Vaskob36053d2020-03-26 15:49:30 +0100985 LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
986 LY_CHECK_RET(yin_parse_restriction(ctx, LY_STMT_LENGTH, type->length));
David Sedlák6542aed2019-08-14 10:47:43 +0200987 type->flags |= LYS_SET_LENGTH;
988
989 return LY_SUCCESS;
990}
991
992/**
David Sedlákbc9ec9c2019-07-11 15:53:55 +0200993 * @brief Parse must element.
994 *
995 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbc9ec9c2019-07-11 15:53:55 +0200996 * @param[in,out] restrs Restrictions to add to.
997 *
998 * @return LY_ERR values.
999 */
1000static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001001yin_parse_must(struct lys_yin_parser_ctx *ctx, struct lysp_restr **restrs)
David Sedlákbc9ec9c2019-07-11 15:53:55 +02001002{
1003 struct lysp_restr *restr;
1004
Michal Vaskob36053d2020-03-26 15:49:30 +01001005 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *restrs, restr, LY_EMEM);
1006 return yin_parse_restriction(ctx, LY_STMT_MUST, restr);
David Sedlákbc9ec9c2019-07-11 15:53:55 +02001007}
1008
1009/**
Michal Vasko7f45cf22020-10-01 12:49:44 +02001010 * @brief Parse a node id into an array.
1011 *
1012 * @param[in,out] ctx YIN parser context for logging and to store current state.
1013 * @param[in] kw Type of current element.
1014 * @param[in] subinfo Information about subelement, is used to determin which function should be called and where to store parsed value.
1015 * @param[in,out] exts Extension instances to add to.
1016 *
1017 * @return LY_ERR values.
1018 */
1019static LY_ERR
1020yin_parse_qname(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, struct yin_subelement *subinfo,
1021 struct lysp_ext_instance **exts)
1022{
1023 struct lysp_qname *qname, **qnames;
1024
1025 switch (kw) {
1026 case LY_STMT_DEFAULT:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001027 if (subinfo->flags & YIN_SUBELEM_UNIQUE) {
1028 qname = (struct lysp_qname *)subinfo->dest;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001029 } else {
1030 qnames = (struct lysp_qname **)subinfo->dest;
1031 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *qnames, qname, LY_EMEM);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001032 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001033 qname->mod = ctx->parsed_mod;
1034 return yin_parse_simple_element(ctx, kw, &qname->str, YIN_ARG_VALUE, Y_STR_ARG, exts);
1035 case LY_STMT_IF_FEATURE:
1036 assert(!(subinfo->flags & YIN_SUBELEM_UNIQUE));
1037 qnames = (struct lysp_qname **)subinfo->dest;
1038 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *qnames, qname, LY_EMEM);
1039 qname->mod = ctx->parsed_mod;
1040 return yin_parse_simple_element(ctx, kw, &qname->str, YIN_ARG_NAME, Y_STR_ARG, exts);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001041 default:
1042 break;
1043 }
1044
1045 LOGINT(ctx->xmlctx->ctx);
1046 return LY_EINT;
1047}
1048
1049/**
David Sedlák5545f5d2019-07-11 11:55:16 +02001050 * @brief Parse position or value element.
1051 *
1052 * @param[in,out] ctx YIN parser context for logging and to store current state.
Radek Krejcid6b76452019-09-03 17:03:03 +02001053 * @param[in] kw Type of current element, can be set to LY_STMT_POSITION or LY_STMT_VALUE.
David Sedlákbf8a2b72019-08-14 16:48:10 +02001054 * @param[out] enm Enum structure to save value, flags and extension instances.
David Sedlák5545f5d2019-07-11 11:55:16 +02001055 *
1056 * @return LY_ERR values.
1057 */
1058static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001059yin_parse_value_pos(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, struct lysp_type_enum *enm)
David Sedlák5545f5d2019-07-11 11:55:16 +02001060{
Radek Krejcid6b76452019-09-03 17:03:03 +02001061 assert(kw == LY_STMT_POSITION || kw == LY_STMT_VALUE);
David Sedlák5545f5d2019-07-11 11:55:16 +02001062 const char *temp_val = NULL;
1063 char *ptr;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02001064 long int num = 0;
1065 unsigned long int unum = 0;
David Sedlák5545f5d2019-07-11 11:55:16 +02001066
1067 /* set value flag */
1068 enm->flags |= LYS_SET_VALUE;
1069
1070 /* get attribute value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001071 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1072 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, kw));
Michal Vasko69730152020-10-09 16:30:07 +02001073 if (!temp_val || (temp_val[0] == '\0') || (temp_val[0] == '+') ||
Radek Krejci0f969882020-08-21 16:56:47 +02001074 ((temp_val[0] == '0') && (temp_val[1] != '\0')) || ((kw == LY_STMT_POSITION) && !strcmp(temp_val, "-0"))) {
David Sedlák1538a842019-08-08 15:38:51 +02001075 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", ly_stmt2str(kw));
David Sedlák5545f5d2019-07-11 11:55:16 +02001076 goto error;
1077 }
1078
1079 /* convert value */
1080 errno = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001081 if (kw == LY_STMT_VALUE) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001082 num = strtol(temp_val, &ptr, LY_BASE_DEC);
Michal Vasko69730152020-10-09 16:30:07 +02001083 if ((num < INT64_C(-2147483648)) || (num > INT64_C(2147483647))) {
David Sedlák1538a842019-08-08 15:38:51 +02001084 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", ly_stmt2str(kw));
David Sedlák5545f5d2019-07-11 11:55:16 +02001085 goto error;
1086 }
1087 } else {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001088 unum = strtoul(temp_val, &ptr, LY_BASE_DEC);
David Sedlák5545f5d2019-07-11 11:55:16 +02001089 if (unum > UINT64_C(4294967295)) {
David Sedlák1538a842019-08-08 15:38:51 +02001090 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", ly_stmt2str(kw));
David Sedlák5545f5d2019-07-11 11:55:16 +02001091 goto error;
1092 }
1093 }
1094 /* check if whole argument value was converted */
1095 if (*ptr != '\0') {
David Sedlák1538a842019-08-08 15:38:51 +02001096 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", ly_stmt2str(kw));
David Sedlákebcd0eb2019-07-16 17:55:12 +02001097 goto error;
David Sedlák5545f5d2019-07-11 11:55:16 +02001098 }
1099 if (errno == ERANGE) {
David Sedlák1538a842019-08-08 15:38:51 +02001100 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_OOB_YIN, temp_val, "value", ly_stmt2str(kw));
David Sedlák5545f5d2019-07-11 11:55:16 +02001101 goto error;
1102 }
1103 /* save correctly ternary operator can't be used because num and unum have different signes */
Radek Krejcid6b76452019-09-03 17:03:03 +02001104 if (kw == LY_STMT_VALUE) {
David Sedlák5545f5d2019-07-11 11:55:16 +02001105 enm->value = num;
1106 } else {
1107 enm->value = unum;
1108 }
Michal Vaskob36053d2020-03-26 15:49:30 +01001109 FREE_STRING(ctx->xmlctx->ctx, temp_val);
David Sedlák5545f5d2019-07-11 11:55:16 +02001110
1111 /* parse subelements */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001112 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001113 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1114 };
Michal Vasko69730152020-10-09 16:30:07 +02001115
Radek Krejcif13b87b2020-12-01 22:02:17 +01001116 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), kw, NULL, &enm->exts);
David Sedlák5545f5d2019-07-11 11:55:16 +02001117
David Sedlákbf8a2b72019-08-14 16:48:10 +02001118error:
Michal Vaskob36053d2020-03-26 15:49:30 +01001119 FREE_STRING(ctx->xmlctx->ctx, temp_val);
1120 return LY_EVALID;
David Sedlák5545f5d2019-07-11 11:55:16 +02001121}
1122
David Sedlák05404f62019-07-24 14:11:53 +02001123/**
1124 * @brief Parse belongs-to element.
1125 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001126 * @param[in] ctx YIN parser context for logging and to store current state.
David Sedlák05404f62019-07-24 14:11:53 +02001127 * @param[out] submod Structure of submodule that is being parsed.
1128 * @param[in,out] exts Extension instances to add to.
1129 *
1130 * @return LY_ERR values
1131 */
1132static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001133yin_parse_belongs_to(struct lys_yin_parser_ctx *ctx, struct lysp_submodule *submod, struct lysp_ext_instance **exts)
David Sedlák05404f62019-07-24 14:11:53 +02001134{
Michal Vaskoc3781c32020-10-06 14:04:08 +02001135 const char *belongsto;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001136 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001137 {LY_STMT_PREFIX, &submod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
1138 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1139 };
David Sedlák05404f62019-07-24 14:11:53 +02001140
Michal Vaskob36053d2020-03-26 15:49:30 +01001141 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
Michal Vaskoc3781c32020-10-06 14:04:08 +02001142 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_MODULE, &belongsto, Y_IDENTIF_ARG, LY_STMT_BELONGS_TO));
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001143 if (ctx->parsed_mod->mod->name != belongsto) {
Michal Vaskoc3781c32020-10-06 14:04:08 +02001144 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YIN, "Submodule \"belongs-to\" value \"%s\" does not match its module name \"%s\".",
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001145 belongsto, ctx->parsed_mod->mod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02001146 lydict_remove(ctx->xmlctx->ctx, belongsto);
1147 return LY_EVALID;
1148 }
1149 lydict_remove(ctx->xmlctx->ctx, belongsto);
Michal Vaskob36053d2020-03-26 15:49:30 +01001150
Radek Krejcif13b87b2020-12-01 22:02:17 +01001151 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_BELONGS_TO, NULL, exts);
David Sedlák05404f62019-07-24 14:11:53 +02001152}
1153
David Sedlák5545f5d2019-07-11 11:55:16 +02001154/**
David Sedlákc1771b12019-07-10 15:55:46 +02001155 * @brief Function to parse meta tags (description, contact, ...) eg. elements with
David Sedlákbf8a2b72019-08-14 16:48:10 +02001156 * text element as child.
David Sedlákb4e44562019-07-04 15:42:12 +02001157 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001158 * @param[in,out] ctx YIN parser context for logging and to store current state.
Radek Krejcid6b76452019-09-03 17:03:03 +02001159 * @param[in] elem_type Type of element can be set to LY_STMT_ORGANIZATION or LY_STMT_CONTACT or LY_STMT_DESCRIPTION or LY_STMT_REFERENCE.
David Sedlákb4e44562019-07-04 15:42:12 +02001160 * @param[out] value Where the content of meta element should be stored.
David Sedlákbf8a2b72019-08-14 16:48:10 +02001161 * @param[in,out] exts Extension instances to add to.
David Sedlákb4e44562019-07-04 15:42:12 +02001162 *
1163 * @return LY_ERR values.
1164 */
1165static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001166yin_parse_meta(struct lys_yin_parser_ctx *ctx, enum ly_stmt elem_type, const char **value, struct lysp_ext_instance **exts)
David Sedlákb4e44562019-07-04 15:42:12 +02001167{
Radek Krejcid6b76452019-09-03 17:03:03 +02001168 assert(elem_type == LY_STMT_ORGANIZATION || elem_type == LY_STMT_CONTACT || elem_type == LY_STMT_DESCRIPTION || elem_type == LY_STMT_REFERENCE);
David Sedlákb4e44562019-07-04 15:42:12 +02001169
Radek Krejcif13b87b2020-12-01 22:02:17 +01001170 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001171 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1172 {LY_STMT_ARG_TEXT, value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE | YIN_SUBELEM_FIRST}
1173 };
Michal Vasko69730152020-10-09 16:30:07 +02001174
David Sedlákdf2a9732019-08-07 13:23:16 +02001175 /* check attributes */
Michal Vaskob36053d2020-03-26 15:49:30 +01001176 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1177 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NONE, NULL, Y_MAYBE_STR_ARG, elem_type));
David Sedlákb4e44562019-07-04 15:42:12 +02001178
David Sedlákdf2a9732019-08-07 13:23:16 +02001179 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001180 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), elem_type, NULL, exts);
David Sedlákb4e44562019-07-04 15:42:12 +02001181}
1182
1183/**
David Sedlákc1771b12019-07-10 15:55:46 +02001184 * @brief Parse error-message element.
1185 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001186 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákc1771b12019-07-10 15:55:46 +02001187 * @param[out] value Where the content of error-message element should be stored.
David Sedlákbf8a2b72019-08-14 16:48:10 +02001188 * @param[in,out] exts Extension instances to add to.
David Sedlákc1771b12019-07-10 15:55:46 +02001189 *
1190 * @return LY_ERR values.
1191 */
1192static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001193yin_parse_err_msg(struct lys_yin_parser_ctx *ctx, const char **value, struct lysp_ext_instance **exts)
David Sedlákc1771b12019-07-10 15:55:46 +02001194{
Radek Krejcif13b87b2020-12-01 22:02:17 +01001195 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001196 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1197 {LY_STMT_ARG_VALUE, value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE | YIN_SUBELEM_FIRST}
1198 };
David Sedlákc1771b12019-07-10 15:55:46 +02001199
David Sedlákdf2a9732019-08-07 13:23:16 +02001200 /* check attributes */
Michal Vaskob36053d2020-03-26 15:49:30 +01001201 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1202 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 +02001203
Radek Krejcif13b87b2020-12-01 22:02:17 +01001204 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_ERROR_MESSAGE, NULL, exts);
David Sedlákc1771b12019-07-10 15:55:46 +02001205}
1206
1207/**
David Sedlák6542aed2019-08-14 10:47:43 +02001208 * @brief Parse type element.
David Sedlák374d2b32019-07-17 15:06:55 +02001209 *
1210 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák6542aed2019-08-14 10:47:43 +02001211 * @param[in] parent Identification of parent element.
David Sedlákbf8a2b72019-08-14 16:48:10 +02001212 * @param[in,out] type Type to write to.
David Sedlák374d2b32019-07-17 15:06:55 +02001213 *
1214 * @return LY_ERR values.
1215 */
1216static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001217yin_parse_type(struct lys_yin_parser_ctx *ctx, enum ly_stmt parent, struct yin_subelement *subinfo)
David Sedlák374d2b32019-07-17 15:06:55 +02001218{
David Sedlák6542aed2019-08-14 10:47:43 +02001219 struct lysp_type *type = NULL;
Michal Vasko69730152020-10-09 16:30:07 +02001220
Radek Krejcid6b76452019-09-03 17:03:03 +02001221 if (parent == LY_STMT_DEVIATE) {
David Sedlák6542aed2019-08-14 10:47:43 +02001222 *(struct lysp_type **)subinfo->dest = calloc(1, sizeof **(struct lysp_type **)subinfo->dest);
Michal Vaskob36053d2020-03-26 15:49:30 +01001223 LY_CHECK_ERR_RET(!(*(struct lysp_type **)subinfo->dest), LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
David Sedlák6542aed2019-08-14 10:47:43 +02001224 type = *((struct lysp_type **)subinfo->dest);
Michal Vaskod989ba02020-08-24 10:59:24 +02001225 } else {
David Sedlák6542aed2019-08-14 10:47:43 +02001226 type = (struct lysp_type *)subinfo->dest;
1227 }
Michal Vaskoe9c050f2020-10-06 14:01:23 +02001228
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001229 type->pmod = ctx->parsed_mod;
Michal Vaskoe9c050f2020-10-06 14:01:23 +02001230
David Sedlák6542aed2019-08-14 10:47:43 +02001231 /* type as child of another type */
Radek Krejcid6b76452019-09-03 17:03:03 +02001232 if (parent == LY_STMT_TYPE) {
David Sedlák6542aed2019-08-14 10:47:43 +02001233 struct lysp_type *nested_type = NULL;
Michal Vaskob36053d2020-03-26 15:49:30 +01001234 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, type->types, nested_type, LY_EMEM);
David Sedlák6542aed2019-08-14 10:47:43 +02001235 type->flags |= LYS_SET_TYPE;
1236 type = nested_type;
1237 }
Michal Vaskoe9c050f2020-10-06 14:01:23 +02001238
Radek Krejcif13b87b2020-12-01 22:02:17 +01001239 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001240 {LY_STMT_BASE, type, 0},
1241 {LY_STMT_BIT, type, 0},
1242 {LY_STMT_ENUM, type, 0},
1243 {LY_STMT_FRACTION_DIGITS, type, YIN_SUBELEM_UNIQUE},
1244 {LY_STMT_LENGTH, type, YIN_SUBELEM_UNIQUE},
1245 {LY_STMT_PATH, type, YIN_SUBELEM_UNIQUE},
1246 {LY_STMT_PATTERN, type, 0},
1247 {LY_STMT_RANGE, type, YIN_SUBELEM_UNIQUE},
1248 {LY_STMT_REQUIRE_INSTANCE, type, YIN_SUBELEM_UNIQUE},
1249 {LY_STMT_TYPE, type},
1250 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1251 };
Michal Vaskob36053d2020-03-26 15:49:30 +01001252
1253 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1254 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &type->name, Y_PREF_IDENTIF_ARG, LY_STMT_TYPE));
Radek Krejcif13b87b2020-12-01 22:02:17 +01001255 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_TYPE, NULL, &type->exts);
David Sedlák374d2b32019-07-17 15:06:55 +02001256}
1257
David Sedlák1af868e2019-07-17 17:03:14 +02001258/**
1259 * @brief Parse max-elements element.
1260 *
1261 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák1af868e2019-07-17 17:03:14 +02001262 * @param[in,out] max Value to write to.
David Sedláka2dad212019-07-18 12:45:19 +02001263 * @param[in] flags Flags to write to.
David Sedlák1af868e2019-07-17 17:03:14 +02001264 * @param[in,out] exts Extension instances to add to.
1265 *
1266 * @return LY_ERR values.
1267 */
1268static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001269yin_parse_maxelements(struct lys_yin_parser_ctx *ctx, uint32_t *max, uint16_t *flags, struct lysp_ext_instance **exts)
David Sedlák1af868e2019-07-17 17:03:14 +02001270{
1271 const char *temp_val = NULL;
1272 char *ptr;
1273 unsigned long int num;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001274 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001275 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1276 };
David Sedlák374d2b32019-07-17 15:06:55 +02001277
David Sedlák1af868e2019-07-17 17:03:14 +02001278 *flags |= LYS_SET_MAX;
Michal Vaskob36053d2020-03-26 15:49:30 +01001279 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1280 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_MAX_ELEMENTS));
Michal Vasko69730152020-10-09 16:30:07 +02001281 if (!temp_val || (temp_val[0] == '\0') || (temp_val[0] == '0') || ((temp_val[0] != 'u') && !isdigit(temp_val[0]))) {
David Sedlák1538a842019-08-08 15:38:51 +02001282 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "max-elements");
Michal Vaskob36053d2020-03-26 15:49:30 +01001283 FREE_STRING(ctx->xmlctx->ctx, temp_val);
David Sedlák1af868e2019-07-17 17:03:14 +02001284 return LY_EVALID;
1285 }
1286
1287 if (strcmp(temp_val, "unbounded")) {
1288 errno = 0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001289 num = strtoul(temp_val, &ptr, LY_BASE_DEC);
David Sedlák1af868e2019-07-17 17:03:14 +02001290 if (*ptr != '\0') {
David Sedlák1538a842019-08-08 15:38:51 +02001291 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "max-elements");
Michal Vaskob36053d2020-03-26 15:49:30 +01001292 FREE_STRING(ctx->xmlctx->ctx, temp_val);
David Sedlák1af868e2019-07-17 17:03:14 +02001293 return LY_EVALID;
1294 }
1295 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlák1538a842019-08-08 15:38:51 +02001296 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_OOB_YIN, temp_val, "value", "max-elements");
Michal Vaskob36053d2020-03-26 15:49:30 +01001297 FREE_STRING(ctx->xmlctx->ctx, temp_val);
David Sedlák1af868e2019-07-17 17:03:14 +02001298 return LY_EVALID;
1299 }
1300 *max = num;
1301 }
Michal Vaskob36053d2020-03-26 15:49:30 +01001302 FREE_STRING(ctx->xmlctx->ctx, temp_val);
Radek Krejcif13b87b2020-12-01 22:02:17 +01001303 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_MAX_ELEMENTS, NULL, exts);
David Sedlák1af868e2019-07-17 17:03:14 +02001304}
David Sedlák374d2b32019-07-17 15:06:55 +02001305
1306/**
David Sedlákbf8a2b72019-08-14 16:48:10 +02001307 * @brief Parse min-elements element.
David Sedlák09e18c92019-07-18 11:17:11 +02001308 *
1309 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák09e18c92019-07-18 11:17:11 +02001310 * @param[in,out] min Value to write to.
David Sedláka2dad212019-07-18 12:45:19 +02001311 * @param[in] flags Flags to write to.
David Sedlák09e18c92019-07-18 11:17:11 +02001312 * @param[in,out] exts Extension instances to add to.
1313 *
1314 * @return LY_ERR values.
1315 */
1316static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001317yin_parse_minelements(struct lys_yin_parser_ctx *ctx, uint32_t *min, uint16_t *flags, struct lysp_ext_instance **exts)
David Sedlák09e18c92019-07-18 11:17:11 +02001318{
1319 const char *temp_val = NULL;
1320 char *ptr;
1321 unsigned long int num;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001322 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001323 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1324 };
David Sedlák09e18c92019-07-18 11:17:11 +02001325
1326 *flags |= LYS_SET_MIN;
Michal Vaskob36053d2020-03-26 15:49:30 +01001327 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1328 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 +02001329
Michal Vasko69730152020-10-09 16:30:07 +02001330 if (!temp_val || (temp_val[0] == '\0') || ((temp_val[0] == '0') && (temp_val[1] != '\0'))) {
David Sedlák1538a842019-08-08 15:38:51 +02001331 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "min-elements");
Michal Vaskob36053d2020-03-26 15:49:30 +01001332 FREE_STRING(ctx->xmlctx->ctx, temp_val);
David Sedlák09e18c92019-07-18 11:17:11 +02001333 return LY_EVALID;
1334 }
1335
1336 errno = 0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001337 num = strtoul(temp_val, &ptr, LY_BASE_DEC);
David Sedlák09e18c92019-07-18 11:17:11 +02001338 if (ptr[0] != 0) {
David Sedlák1538a842019-08-08 15:38:51 +02001339 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "min-elements");
Michal Vaskob36053d2020-03-26 15:49:30 +01001340 FREE_STRING(ctx->xmlctx->ctx, temp_val);
David Sedlák09e18c92019-07-18 11:17:11 +02001341 return LY_EVALID;
1342 }
Michal Vasko69730152020-10-09 16:30:07 +02001343 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlák1538a842019-08-08 15:38:51 +02001344 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_OOB_YIN, temp_val, "value", "min-elements");
Michal Vaskob36053d2020-03-26 15:49:30 +01001345 FREE_STRING(ctx->xmlctx->ctx, temp_val);
David Sedlák09e18c92019-07-18 11:17:11 +02001346 return LY_EVALID;
1347 }
1348 *min = num;
Michal Vaskob36053d2020-03-26 15:49:30 +01001349 FREE_STRING(ctx->xmlctx->ctx, temp_val);
Radek Krejcif13b87b2020-12-01 22:02:17 +01001350 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_MIN_ELEMENTS, NULL, exts);
David Sedlák09e18c92019-07-18 11:17:11 +02001351}
1352
David Sedláka2dad212019-07-18 12:45:19 +02001353/**
1354 * @brief Parse min-elements or max-elements element.
1355 *
1356 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedláka2dad212019-07-18 12:45:19 +02001357 * @param[in] parent Identification of parent element.
1358 * @param[in] current Identification of current element.
1359 * @param[in] dest Where the parsed value and flags should be stored.
1360 *
1361 * @return LY_ERR values.
1362 */
David Sedlák09e18c92019-07-18 11:17:11 +02001363static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001364yin_parse_minmax(struct lys_yin_parser_ctx *ctx, enum ly_stmt parent, enum ly_stmt current, void *dest)
David Sedlák09e18c92019-07-18 11:17:11 +02001365{
Radek Krejcid6b76452019-09-03 17:03:03 +02001366 assert(current == LY_STMT_MAX_ELEMENTS || current == LY_STMT_MIN_ELEMENTS);
1367 assert(parent == LY_STMT_LEAF_LIST || parent == LY_STMT_REFINE || parent == LY_STMT_LIST || parent == LY_STMT_DEVIATE);
David Sedlák09e18c92019-07-18 11:17:11 +02001368 uint32_t *lim;
1369 uint16_t *flags;
1370 struct lysp_ext_instance **exts;
1371
Radek Krejcid6b76452019-09-03 17:03:03 +02001372 if (parent == LY_STMT_LEAF_LIST) {
1373 lim = (current == LY_STMT_MAX_ELEMENTS) ? &((struct lysp_node_leaflist *)dest)->max : &((struct lysp_node_leaflist *)dest)->min;
David Sedlák09e18c92019-07-18 11:17:11 +02001374 flags = &((struct lysp_node_leaflist *)dest)->flags;
1375 exts = &((struct lysp_node_leaflist *)dest)->exts;
Radek Krejcid6b76452019-09-03 17:03:03 +02001376 } else if (parent == LY_STMT_REFINE) {
1377 lim = (current == LY_STMT_MAX_ELEMENTS) ? &((struct lysp_refine *)dest)->max : &((struct lysp_refine *)dest)->min;
David Sedlák09e18c92019-07-18 11:17:11 +02001378 flags = &((struct lysp_refine *)dest)->flags;
1379 exts = &((struct lysp_refine *)dest)->exts;
Radek Krejcid6b76452019-09-03 17:03:03 +02001380 } else if (parent == LY_STMT_LIST) {
1381 lim = (current == LY_STMT_MAX_ELEMENTS) ? &((struct lysp_node_list *)dest)->max : &((struct lysp_node_list *)dest)->min;
David Sedlák09e18c92019-07-18 11:17:11 +02001382 flags = &((struct lysp_node_list *)dest)->flags;
1383 exts = &((struct lysp_node_list *)dest)->exts;
David Sedlák4ffcec82019-07-25 15:10:21 +02001384 } else {
1385 lim = ((struct minmax_dev_meta *)dest)->lim;
1386 flags = ((struct minmax_dev_meta *)dest)->flags;
1387 exts = ((struct minmax_dev_meta *)dest)->exts;
David Sedlák09e18c92019-07-18 11:17:11 +02001388 }
1389
Radek Krejcid6b76452019-09-03 17:03:03 +02001390 if (current == LY_STMT_MAX_ELEMENTS) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001391 LY_CHECK_RET(yin_parse_maxelements(ctx, lim, flags, exts));
David Sedlák09e18c92019-07-18 11:17:11 +02001392 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +01001393 LY_CHECK_RET(yin_parse_minelements(ctx, lim, flags, exts));
David Sedlák09e18c92019-07-18 11:17:11 +02001394 }
1395
1396 return LY_SUCCESS;
1397}
1398
1399/**
David Sedlákbf8a2b72019-08-14 16:48:10 +02001400 * @brief Parse ordered-by element.
David Sedláka2dad212019-07-18 12:45:19 +02001401 *
1402 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedláka2dad212019-07-18 12:45:19 +02001403 * @param[out] flags Flags to write to.
David Sedlákbf8a2b72019-08-14 16:48:10 +02001404 * @param[in,out] exts Extension instances to add to.
David Sedláka2dad212019-07-18 12:45:19 +02001405 *
1406 * @return LY_ERR values.
1407 */
1408static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001409yin_parse_orderedby(struct lys_yin_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
David Sedláka2dad212019-07-18 12:45:19 +02001410{
1411 const char *temp_val;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001412 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001413 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1414 };
David Sedláka2dad212019-07-18 12:45:19 +02001415
Michal Vaskob36053d2020-03-26 15:49:30 +01001416 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1417 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 +02001418 if (strcmp(temp_val, "system") == 0) {
1419 *flags |= LYS_ORDBY_SYSTEM;
1420 } else if (strcmp(temp_val, "user") == 0) {
1421 *flags |= LYS_ORDBY_USER;
1422 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02001423 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value",
Michal Vasko69730152020-10-09 16:30:07 +02001424 "ordered-by", "system", "user");
Michal Vaskob36053d2020-03-26 15:49:30 +01001425 FREE_STRING(ctx->xmlctx->ctx, temp_val);
David Sedláka2dad212019-07-18 12:45:19 +02001426 return LY_EVALID;
1427 }
Michal Vaskob36053d2020-03-26 15:49:30 +01001428 FREE_STRING(ctx->xmlctx->ctx, temp_val);
David Sedláka2dad212019-07-18 12:45:19 +02001429
Radek Krejcif13b87b2020-12-01 22:02:17 +01001430 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_ORDERED_BY, NULL, exts);
David Sedláka2dad212019-07-18 12:45:19 +02001431}
1432
1433/**
David Sedlákbf8a2b72019-08-14 16:48:10 +02001434 * @brief Parse any-data or any-xml element.
David Sedlák8a83bbb2019-07-18 14:46:00 +02001435 *
1436 * @param[in,out] ctx YIN parser context for logging and to store current state.
Radek Krejcid6b76452019-09-03 17:03:03 +02001437 * @param[in] any_kw Identification of current element, can be set to LY_STMT_ANYDATA or LY_STMT_ANYXML
David Sedlákad83cf92019-08-13 12:53:53 +02001438 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlák8a83bbb2019-07-18 14:46:00 +02001439 *
1440 * @return LY_ERR values.
1441 */
1442static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001443yin_parse_any(struct lys_yin_parser_ctx *ctx, enum ly_stmt any_kw, struct tree_node_meta *node_meta)
David Sedlák8a83bbb2019-07-18 14:46:00 +02001444{
David Sedlák8a83bbb2019-07-18 14:46:00 +02001445 struct lysp_node_anydata *any;
1446
David Sedlák8d552d62019-08-06 15:29:05 +02001447 /* create new sibling */
Michal Vaskob36053d2020-03-26 15:49:30 +01001448 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, any, next, LY_EMEM);
Radek Krejcid6b76452019-09-03 17:03:03 +02001449 any->nodetype = (any_kw == LY_STMT_ANYDATA) ? LYS_ANYDATA : LYS_ANYXML;
David Sedlák8a83bbb2019-07-18 14:46:00 +02001450 any->parent = node_meta->parent;
1451
David Sedlákbf8a2b72019-08-14 16:48:10 +02001452 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001453 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1454 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 +02001455
Radek Krejcif13b87b2020-12-01 22:02:17 +01001456 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001457 {LY_STMT_CONFIG, &any->flags, YIN_SUBELEM_UNIQUE},
1458 {LY_STMT_DESCRIPTION, &any->dsc, YIN_SUBELEM_UNIQUE},
1459 {LY_STMT_IF_FEATURE, &any->iffeatures, 0},
1460 {LY_STMT_MANDATORY, &any->flags, YIN_SUBELEM_UNIQUE},
1461 {LY_STMT_MUST, &any->musts, 0},
1462 {LY_STMT_REFERENCE, &any->ref, YIN_SUBELEM_UNIQUE},
1463 {LY_STMT_STATUS, &any->flags, YIN_SUBELEM_UNIQUE},
1464 {LY_STMT_WHEN, &any->when, YIN_SUBELEM_UNIQUE},
1465 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1466 };
Michal Vasko69730152020-10-09 16:30:07 +02001467
Radek Krejcif13b87b2020-12-01 22:02:17 +01001468 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), any_kw, NULL, &any->exts);
David Sedlák8a83bbb2019-07-18 14:46:00 +02001469}
1470
1471/**
David Sedlák203ca3a2019-07-18 15:26:25 +02001472 * @brief parse leaf element.
1473 *
1474 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákad83cf92019-08-13 12:53:53 +02001475 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlák203ca3a2019-07-18 15:26:25 +02001476 *
1477 * @return LY_ERR values.
1478 */
1479static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001480yin_parse_leaf(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta)
David Sedlák203ca3a2019-07-18 15:26:25 +02001481{
David Sedlák203ca3a2019-07-18 15:26:25 +02001482 struct lysp_node_leaf *leaf;
1483
David Sedlák8d552d62019-08-06 15:29:05 +02001484 /* create structure new leaf */
Michal Vaskob36053d2020-03-26 15:49:30 +01001485 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, leaf, next, LY_EMEM);
David Sedlák203ca3a2019-07-18 15:26:25 +02001486 leaf->nodetype = LYS_LEAF;
1487 leaf->parent = node_meta->parent;
1488
David Sedlák203ca3a2019-07-18 15:26:25 +02001489 /* parser argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001490 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1491 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 +02001492
1493 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001494 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001495 {LY_STMT_CONFIG, &leaf->flags, YIN_SUBELEM_UNIQUE},
1496 {LY_STMT_DEFAULT, &leaf->dflt, YIN_SUBELEM_UNIQUE},
1497 {LY_STMT_DESCRIPTION, &leaf->dsc, YIN_SUBELEM_UNIQUE},
1498 {LY_STMT_IF_FEATURE, &leaf->iffeatures, 0},
1499 {LY_STMT_MANDATORY, &leaf->flags, YIN_SUBELEM_UNIQUE},
1500 {LY_STMT_MUST, &leaf->musts, 0},
1501 {LY_STMT_REFERENCE, &leaf->ref, YIN_SUBELEM_UNIQUE},
1502 {LY_STMT_STATUS, &leaf->flags, YIN_SUBELEM_UNIQUE},
1503 {LY_STMT_TYPE, &leaf->type, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_MANDATORY},
1504 {LY_STMT_UNITS, &leaf->units, YIN_SUBELEM_UNIQUE},
1505 {LY_STMT_WHEN, &leaf->when, YIN_SUBELEM_UNIQUE},
1506 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1507 };
Michal Vasko69730152020-10-09 16:30:07 +02001508
Radek Krejcif13b87b2020-12-01 22:02:17 +01001509 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_LEAF, NULL, &leaf->exts);
David Sedlák203ca3a2019-07-18 15:26:25 +02001510}
1511
1512/**
David Sedlákc3da3ef2019-07-19 12:56:08 +02001513 * @brief Parse leaf-list element.
1514 *
1515 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákad83cf92019-08-13 12:53:53 +02001516 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlákc3da3ef2019-07-19 12:56:08 +02001517 *
1518 * @return LY_ERR values.
1519 */
1520static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001521yin_parse_leaflist(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta)
David Sedlákc3da3ef2019-07-19 12:56:08 +02001522{
David Sedlákc3da3ef2019-07-19 12:56:08 +02001523 struct lysp_node_leaflist *llist;
David Sedlákc3da3ef2019-07-19 12:56:08 +02001524
Michal Vaskob36053d2020-03-26 15:49:30 +01001525 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, llist, next, LY_EMEM);
David Sedlák8d552d62019-08-06 15:29:05 +02001526
David Sedlákc3da3ef2019-07-19 12:56:08 +02001527 llist->nodetype = LYS_LEAFLIST;
1528 llist->parent = node_meta->parent;
1529
David Sedlákc3da3ef2019-07-19 12:56:08 +02001530 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001531 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1532 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 +02001533
1534 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001535 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001536 {LY_STMT_CONFIG, &llist->flags, YIN_SUBELEM_UNIQUE},
1537 {LY_STMT_DEFAULT, &llist->dflts, 0},
1538 {LY_STMT_DESCRIPTION, &llist->dsc, YIN_SUBELEM_UNIQUE},
1539 {LY_STMT_IF_FEATURE, &llist->iffeatures, 0},
1540 {LY_STMT_MAX_ELEMENTS, llist, YIN_SUBELEM_UNIQUE},
1541 {LY_STMT_MIN_ELEMENTS, llist, YIN_SUBELEM_UNIQUE},
1542 {LY_STMT_MUST, &llist->musts, 0},
1543 {LY_STMT_ORDERED_BY, &llist->flags, YIN_SUBELEM_UNIQUE},
1544 {LY_STMT_REFERENCE, &llist->ref, YIN_SUBELEM_UNIQUE},
1545 {LY_STMT_STATUS, &llist->flags, YIN_SUBELEM_UNIQUE},
1546 {LY_STMT_TYPE, &llist->type, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_MANDATORY},
1547 {LY_STMT_UNITS, &llist->units, YIN_SUBELEM_UNIQUE},
1548 {LY_STMT_WHEN, &llist->when, YIN_SUBELEM_UNIQUE},
1549 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1550 };
Michal Vasko69730152020-10-09 16:30:07 +02001551
Radek Krejcif13b87b2020-12-01 22:02:17 +01001552 LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_LEAF_LIST, NULL, &llist->exts));
David Sedlákc3da3ef2019-07-19 12:56:08 +02001553
David Sedlákbf8a2b72019-08-14 16:48:10 +02001554 /* check invalid combination of subelements */
David Sedlákc3da3ef2019-07-19 12:56:08 +02001555 if ((llist->min) && (llist->dflts)) {
David Sedlák1538a842019-08-08 15:38:51 +02001556 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INCHILDSTMSCOMB_YIN, "min-elements", "default", "leaf-list");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001557 return LY_EVALID;
1558 }
Michal Vasko69730152020-10-09 16:30:07 +02001559 if (llist->max && (llist->min > llist->max)) {
David Sedlák1538a842019-08-08 15:38:51 +02001560 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_MINMAX, llist->min, llist->max);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001561 return LY_EVALID;
1562 }
1563
1564 return LY_SUCCESS;
1565}
1566
1567/**
David Sedlák04e17b22019-07-19 15:29:48 +02001568 * @brief Parse typedef element.
1569 *
1570 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák05404f62019-07-24 14:11:53 +02001571 * @param[in] typedef_meta Meta information about parent node and typedefs to add to.
David Sedlák04e17b22019-07-19 15:29:48 +02001572 *
1573 * @return LY_ERR values.
1574 */
1575static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001576yin_parse_typedef(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *typedef_meta)
David Sedlák04e17b22019-07-19 15:29:48 +02001577{
1578 struct lysp_tpdf *tpdf;
David Sedlákbf8a2b72019-08-14 16:48:10 +02001579 struct lysp_tpdf **tpdfs = (struct lysp_tpdf **)typedef_meta->nodes;
Michal Vasko69730152020-10-09 16:30:07 +02001580
Michal Vaskob36053d2020-03-26 15:49:30 +01001581 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *tpdfs, tpdf, LY_EMEM);
David Sedlák04e17b22019-07-19 15:29:48 +02001582
1583 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001584 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1585 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 +02001586
1587 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001588 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001589 {LY_STMT_DEFAULT, &tpdf->dflt, YIN_SUBELEM_UNIQUE},
1590 {LY_STMT_DESCRIPTION, &tpdf->dsc, YIN_SUBELEM_UNIQUE},
1591 {LY_STMT_REFERENCE, &tpdf->ref, YIN_SUBELEM_UNIQUE},
1592 {LY_STMT_STATUS, &tpdf->flags, YIN_SUBELEM_UNIQUE},
1593 {LY_STMT_TYPE, &tpdf->type, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_MANDATORY},
1594 {LY_STMT_UNITS, &tpdf->units, YIN_SUBELEM_UNIQUE},
1595 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1596 };
Michal Vasko69730152020-10-09 16:30:07 +02001597
Radek Krejcif13b87b2020-12-01 22:02:17 +01001598 LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_TYPEDEF, NULL, &tpdf->exts));
David Sedlák04e17b22019-07-19 15:29:48 +02001599
1600 /* store data for collision check */
Michal Vasko69730152020-10-09 16:30:07 +02001601 if (typedef_meta->parent && !(typedef_meta->parent->nodetype & (LYS_GROUPING | LYS_RPC | LYS_ACTION | LYS_INPUT |
1602 LYS_OUTPUT | LYS_NOTIF))) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02001603 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, typedef_meta->parent, 0, NULL));
David Sedlák04e17b22019-07-19 15:29:48 +02001604 }
1605
1606 return LY_SUCCESS;
1607}
1608
1609/**
David Sedlákd2d676a2019-07-22 11:28:19 +02001610 * @brief Parse refine element.
1611 *
1612 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákd2d676a2019-07-22 11:28:19 +02001613 * @param[in,out] refines Refines to add to.
1614 *
1615 * @return LY_ERR values.
1616 */
1617static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001618yin_parse_refine(struct lys_yin_parser_ctx *ctx, struct lysp_refine **refines)
David Sedlákd2d676a2019-07-22 11:28:19 +02001619{
1620 struct lysp_refine *rf;
1621
1622 /* allocate new refine */
Michal Vaskob36053d2020-03-26 15:49:30 +01001623 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *refines, rf, LY_EMEM);
David Sedlákd2d676a2019-07-22 11:28:19 +02001624
1625 /* parse attribute */
Michal Vaskob36053d2020-03-26 15:49:30 +01001626 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1627 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_TARGET_NODE, &rf->nodeid, Y_STR_ARG, LY_STMT_REFINE));
1628 CHECK_NONEMPTY(ctx, strlen(rf->nodeid), "refine");
David Sedlákd2d676a2019-07-22 11:28:19 +02001629
1630 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001631 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001632 {LY_STMT_CONFIG, &rf->flags, YIN_SUBELEM_UNIQUE},
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001633 {LY_STMT_DEFAULT, &rf->dflts, 0},
Radek Krejci0f969882020-08-21 16:56:47 +02001634 {LY_STMT_DESCRIPTION, &rf->dsc, YIN_SUBELEM_UNIQUE},
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001635 {LY_STMT_IF_FEATURE, &rf->iffeatures, 0},
Radek Krejci0f969882020-08-21 16:56:47 +02001636 {LY_STMT_MANDATORY, &rf->flags, YIN_SUBELEM_UNIQUE},
1637 {LY_STMT_MAX_ELEMENTS, rf, YIN_SUBELEM_UNIQUE},
1638 {LY_STMT_MIN_ELEMENTS, rf, YIN_SUBELEM_UNIQUE},
1639 {LY_STMT_MUST, &rf->musts, 0},
1640 {LY_STMT_PRESENCE, &rf->presence, YIN_SUBELEM_UNIQUE},
1641 {LY_STMT_REFERENCE, &rf->ref, YIN_SUBELEM_UNIQUE},
1642 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1643 };
Michal Vasko69730152020-10-09 16:30:07 +02001644
Radek Krejcif13b87b2020-12-01 22:02:17 +01001645 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_REFINE, NULL, &rf->exts);
David Sedlákd2d676a2019-07-22 11:28:19 +02001646}
1647
1648/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02001649 * @brief Parse uses element.
1650 *
1651 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákad83cf92019-08-13 12:53:53 +02001652 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlák0d6de5a2019-07-22 13:25:44 +02001653 *
1654 * @return LY_ERR values.
1655 */
1656static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001657yin_parse_uses(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta)
David Sedlák0d6de5a2019-07-22 13:25:44 +02001658{
David Sedlák0d6de5a2019-07-22 13:25:44 +02001659 struct lysp_node_uses *uses;
1660
David Sedlák8d552d62019-08-06 15:29:05 +02001661 /* create new uses */
Michal Vaskob36053d2020-03-26 15:49:30 +01001662 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, uses, next, LY_EMEM);
David Sedlák0d6de5a2019-07-22 13:25:44 +02001663 uses->nodetype = LYS_USES;
1664 uses->parent = node_meta->parent;
1665
David Sedlák0d6de5a2019-07-22 13:25:44 +02001666 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001667 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1668 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 +02001669
1670 /* parse content */
David Sedlák6881b512019-08-13 12:52:00 +02001671 struct tree_node_meta augments = {(struct lysp_node *)uses, (struct lysp_node **)&uses->augments};
Radek Krejcif13b87b2020-12-01 22:02:17 +01001672 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001673 {LY_STMT_AUGMENT, &augments, 0},
1674 {LY_STMT_DESCRIPTION, &uses->dsc, YIN_SUBELEM_UNIQUE},
1675 {LY_STMT_IF_FEATURE, &uses->iffeatures, 0},
1676 {LY_STMT_REFERENCE, &uses->ref, YIN_SUBELEM_UNIQUE},
1677 {LY_STMT_REFINE, &uses->refines, 0},
1678 {LY_STMT_STATUS, &uses->flags, YIN_SUBELEM_UNIQUE},
1679 {LY_STMT_WHEN, &uses->when, YIN_SUBELEM_UNIQUE},
1680 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1681 };
Michal Vasko69730152020-10-09 16:30:07 +02001682
Radek Krejcif13b87b2020-12-01 22:02:17 +01001683 LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_USES, NULL, &uses->exts));
David Sedlák0d6de5a2019-07-22 13:25:44 +02001684 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, NULL, uses->augments, NULL, NULL));
1685
1686 return LY_SUCCESS;
1687}
1688
1689/**
David Sedlákaa854b02019-07-22 14:17:10 +02001690 * @brief Parse revision element.
1691 *
1692 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákaa854b02019-07-22 14:17:10 +02001693 * @param[in,out] revs Parsed revisions to add to.
1694 *
1695 * @return LY_ERR values.
1696 */
1697static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001698yin_parse_revision(struct lys_yin_parser_ctx *ctx, struct lysp_revision **revs)
David Sedlákaa854b02019-07-22 14:17:10 +02001699{
1700 struct lysp_revision *rev;
1701 const char *temp_date = NULL;
1702
1703 /* allocate new reivison */
Michal Vaskob36053d2020-03-26 15:49:30 +01001704 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *revs, rev, LY_EMEM);
David Sedlákaa854b02019-07-22 14:17:10 +02001705
1706 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001707 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1708 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 +02001709 /* check value */
1710 if (lysp_check_date((struct lys_parser_ctx *)ctx, temp_date, strlen(temp_date), "revision")) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001711 FREE_STRING(ctx->xmlctx->ctx, temp_date);
David Sedlákaa854b02019-07-22 14:17:10 +02001712 return LY_EVALID;
1713 }
1714 strcpy(rev->date, temp_date);
Michal Vaskob36053d2020-03-26 15:49:30 +01001715 FREE_STRING(ctx->xmlctx->ctx, temp_date);
David Sedlákaa854b02019-07-22 14:17:10 +02001716
1717 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001718 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001719 {LY_STMT_DESCRIPTION, &rev->dsc, YIN_SUBELEM_UNIQUE},
1720 {LY_STMT_REFERENCE, &rev->ref, YIN_SUBELEM_UNIQUE},
1721 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1722 };
Michal Vasko69730152020-10-09 16:30:07 +02001723
Radek Krejcif13b87b2020-12-01 22:02:17 +01001724 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_REVISION, NULL, &rev->exts);
David Sedlákaa854b02019-07-22 14:17:10 +02001725}
1726
David Sedlák5e13dea2019-07-22 16:06:45 +02001727/**
1728 * @brief Parse include element.
1729 *
1730 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák5e13dea2019-07-22 16:06:45 +02001731 * @param[in,out] inc_meta Meta informatinou about module/submodule name and includes to add to.
1732 *
1733 * @return LY_ERR values.
1734 */
David Sedlák0c2bab92019-07-22 15:33:19 +02001735static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001736yin_parse_include(struct lys_yin_parser_ctx *ctx, struct include_meta *inc_meta)
David Sedlák0c2bab92019-07-22 15:33:19 +02001737{
1738 struct lysp_include *inc;
1739
1740 /* allocate new include */
Michal Vaskob36053d2020-03-26 15:49:30 +01001741 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *inc_meta->includes, inc, LY_EMEM);
David Sedlák0c2bab92019-07-22 15:33:19 +02001742
1743 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001744 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1745 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 +02001746
1747 /* submodules share the namespace with the module names, so there must not be
1748 * a module of the same name in the context, no need for revision matching */
Michal Vaskob36053d2020-03-26 15:49:30 +01001749 if (!strcmp(inc_meta->name, inc->name) || ly_ctx_get_module_latest(ctx->xmlctx->ctx, inc->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01001750 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_NAME2_COL, "module", "submodule", inc->name);
David Sedlák0c2bab92019-07-22 15:33:19 +02001751 return LY_EVALID;
1752 }
1753
1754 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001755 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001756 {LY_STMT_DESCRIPTION, &inc->dsc, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_VER2},
1757 {LY_STMT_REFERENCE, &inc->ref, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_VER2},
1758 {LY_STMT_REVISION_DATE, &inc->rev, YIN_SUBELEM_UNIQUE},
1759 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1760 };
Michal Vasko69730152020-10-09 16:30:07 +02001761
Radek Krejcif13b87b2020-12-01 22:02:17 +01001762 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_INCLUDE, NULL, &inc->exts);
David Sedlák0c2bab92019-07-22 15:33:19 +02001763}
1764
David Sedlákaa854b02019-07-22 14:17:10 +02001765/**
David Sedlákbf8a2b72019-08-14 16:48:10 +02001766 * @brief Parse revision-date element.
David Sedlákdfbbb442019-08-06 16:33:21 +02001767 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001768 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001769 * @param[in,out] rev Array to store the parsed value in.
1770 * @param[in,out] exts Extension instances to add to.
1771 *
1772 * @return LY_ERR values.
1773 */
1774static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001775yin_parse_revision_date(struct lys_yin_parser_ctx *ctx, char *rev, struct lysp_ext_instance **exts)
David Sedlákdfbbb442019-08-06 16:33:21 +02001776{
1777 const char *temp_rev;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001778 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001779 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1780 };
David Sedlákdfbbb442019-08-06 16:33:21 +02001781
Michal Vaskob36053d2020-03-26 15:49:30 +01001782 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1783 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_DATE, &temp_rev, Y_STR_ARG, LY_STMT_REVISION_DATE));
David Sedlákdfbbb442019-08-06 16:33:21 +02001784 LY_CHECK_ERR_RET(lysp_check_date((struct lys_parser_ctx *)ctx, temp_rev, strlen(temp_rev), "revision-date") != LY_SUCCESS,
Michal Vasko69730152020-10-09 16:30:07 +02001785 FREE_STRING(ctx->xmlctx->ctx, temp_rev), LY_EVALID);
David Sedlákdfbbb442019-08-06 16:33:21 +02001786
1787 strcpy(rev, temp_rev);
Michal Vaskob36053d2020-03-26 15:49:30 +01001788 FREE_STRING(ctx->xmlctx->ctx, temp_rev);
David Sedlákdfbbb442019-08-06 16:33:21 +02001789
Radek Krejcif13b87b2020-12-01 22:02:17 +01001790 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_REVISION_DATE, NULL, exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02001791}
1792
1793/**
1794 * @brief Parse config element.
1795 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001796 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001797 * @param[in,out] flags Flags to add to.
1798 * @param[in,out] exts Extension instances to add to.
1799 *
1800 * @return LY_ERR values.
1801 */
1802static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001803yin_parse_config(struct lys_yin_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
David Sedlákdfbbb442019-08-06 16:33:21 +02001804{
1805 const char *temp_val = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001806 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001807 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1808 };
David Sedlákdfbbb442019-08-06 16:33:21 +02001809
Michal Vaskob36053d2020-03-26 15:49:30 +01001810 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1811 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 +02001812 if (strcmp(temp_val, "true") == 0) {
1813 *flags |= LYS_CONFIG_W;
1814 } else if (strcmp(temp_val, "false") == 0) {
1815 *flags |= LYS_CONFIG_R;
1816 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02001817 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value", "config",
Michal Vasko69730152020-10-09 16:30:07 +02001818 "true", "false");
Michal Vaskob36053d2020-03-26 15:49:30 +01001819 FREE_STRING(ctx->xmlctx->ctx, temp_val);
David Sedlákdfbbb442019-08-06 16:33:21 +02001820 return LY_EVALID;
1821 }
Michal Vaskob36053d2020-03-26 15:49:30 +01001822 FREE_STRING(ctx->xmlctx->ctx, temp_val);
David Sedlákdfbbb442019-08-06 16:33:21 +02001823
Radek Krejcif13b87b2020-12-01 22:02:17 +01001824 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_CONFIG, NULL, exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02001825}
1826
1827/**
1828 * @brief Parse yang-version element.
1829 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001830 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001831 * @param[out] version Storage for the parsed information.
David Sedlákbf8a2b72019-08-14 16:48:10 +02001832 * @param[in,out] exts Extension instances to add to.
David Sedlákdfbbb442019-08-06 16:33:21 +02001833 *
1834 * @return LY_ERR values.
1835 */
1836static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001837yin_parse_yangversion(struct lys_yin_parser_ctx *ctx, uint8_t *version, struct lysp_ext_instance **exts)
David Sedlákdfbbb442019-08-06 16:33:21 +02001838{
1839 const char *temp_version = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001840 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001841 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1842 };
David Sedlákdfbbb442019-08-06 16:33:21 +02001843
Michal Vaskob36053d2020-03-26 15:49:30 +01001844 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1845 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 +02001846 if (strcmp(temp_version, "1") == 0) {
David Sedlákdfbbb442019-08-06 16:33:21 +02001847 *version = LYS_VERSION_1_0;
1848 } else if (strcmp(temp_version, "1.1") == 0) {
1849 *version = LYS_VERSION_1_1;
1850 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02001851 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_version, "value",
Michal Vasko69730152020-10-09 16:30:07 +02001852 "yang-version", "1", "1.1");
Michal Vaskob36053d2020-03-26 15:49:30 +01001853 FREE_STRING(ctx->xmlctx->ctx, temp_version);
David Sedlákdfbbb442019-08-06 16:33:21 +02001854 return LY_EVALID;
1855 }
Michal Vaskob36053d2020-03-26 15:49:30 +01001856 FREE_STRING(ctx->xmlctx->ctx, temp_version);
David Sedlákdfbbb442019-08-06 16:33:21 +02001857
Radek Krejcif13b87b2020-12-01 22:02:17 +01001858 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_YANG_VERSION, NULL, exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02001859}
1860
1861/**
1862 * @brief Parse import element.
1863 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001864 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001865 * @param[in,out] imp_meta Meta information about prefix and imports to add to.
1866 *
1867 * @return LY_ERR values.
1868 */
1869static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001870yin_parse_import(struct lys_yin_parser_ctx *ctx, struct import_meta *imp_meta)
David Sedlákdfbbb442019-08-06 16:33:21 +02001871{
1872 struct lysp_import *imp;
Michal Vasko69730152020-10-09 16:30:07 +02001873
David Sedlákdfbbb442019-08-06 16:33:21 +02001874 /* allocate new element in sized array for import */
Michal Vaskob36053d2020-03-26 15:49:30 +01001875 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *imp_meta->imports, imp, LY_EMEM);
David Sedlákdfbbb442019-08-06 16:33:21 +02001876
Radek Krejcif13b87b2020-12-01 22:02:17 +01001877 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001878 {LY_STMT_DESCRIPTION, &imp->dsc, YIN_SUBELEM_UNIQUE},
1879 {LY_STMT_PREFIX, &imp->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
1880 {LY_STMT_REFERENCE, &imp->ref, YIN_SUBELEM_UNIQUE},
1881 {LY_STMT_REVISION_DATE, imp->rev, YIN_SUBELEM_UNIQUE},
1882 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1883 };
David Sedlákdfbbb442019-08-06 16:33:21 +02001884
1885 /* parse import attributes */
Michal Vaskob36053d2020-03-26 15:49:30 +01001886 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1887 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_MODULE, &imp->name, Y_IDENTIF_ARG, LY_STMT_IMPORT));
Radek Krejcif13b87b2020-12-01 22:02:17 +01001888 LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_IMPORT, NULL, &imp->exts));
David Sedlákdfbbb442019-08-06 16:33:21 +02001889 /* check prefix validity */
1890 LY_CHECK_RET(lysp_check_prefix((struct lys_parser_ctx *)ctx, *imp_meta->imports, imp_meta->prefix, &imp->prefix), LY_EVALID);
1891
1892 return LY_SUCCESS;
1893}
1894
1895/**
1896 * @brief Parse mandatory element.
1897 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001898 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001899 * @param[in,out] flags Flags to add to.
1900 * @param[in,out] exts Extension instances to add to.
1901 *
1902 * @return LY_ERR values.
1903 */
1904static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001905yin_parse_mandatory(struct lys_yin_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
David Sedlákdfbbb442019-08-06 16:33:21 +02001906{
1907 const char *temp_val = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001908 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001909 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1910 };
David Sedlákdfbbb442019-08-06 16:33:21 +02001911
Michal Vaskob36053d2020-03-26 15:49:30 +01001912 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1913 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_MANDATORY));
David Sedlákdfbbb442019-08-06 16:33:21 +02001914 if (strcmp(temp_val, "true") == 0) {
1915 *flags |= LYS_MAND_TRUE;
1916 } else if (strcmp(temp_val, "false") == 0) {
1917 *flags |= LYS_MAND_FALSE;
1918 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02001919 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value",
Michal Vasko69730152020-10-09 16:30:07 +02001920 "mandatory", "true", "false");
Michal Vaskob36053d2020-03-26 15:49:30 +01001921 FREE_STRING(ctx->xmlctx->ctx, temp_val);
David Sedlákdfbbb442019-08-06 16:33:21 +02001922 return LY_EVALID;
1923 }
Michal Vaskob36053d2020-03-26 15:49:30 +01001924 FREE_STRING(ctx->xmlctx->ctx, temp_val);
David Sedlákdfbbb442019-08-06 16:33:21 +02001925
Radek Krejcif13b87b2020-12-01 22:02:17 +01001926 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_MANDATORY, NULL, exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02001927}
1928
1929/**
1930 * @brief Parse status element.
1931 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001932 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001933 * @param[in,out] flags Flags to add to.
1934 * @param[in,out] exts Extension instances to add to.
1935 *
1936 * @return LY_ERR values.
1937 */
1938static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001939yin_parse_status(struct lys_yin_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
David Sedlákdfbbb442019-08-06 16:33:21 +02001940{
1941 const char *value = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001942 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001943 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1944 };
David Sedlákdfbbb442019-08-06 16:33:21 +02001945
Michal Vaskob36053d2020-03-26 15:49:30 +01001946 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1947 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 +02001948 if (strcmp(value, "current") == 0) {
1949 *flags |= LYS_STATUS_CURR;
1950 } else if (strcmp(value, "deprecated") == 0) {
1951 *flags |= LYS_STATUS_DEPRC;
1952 } else if (strcmp(value, "obsolete") == 0) {
1953 *flags |= LYS_STATUS_OBSLT;
1954 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02001955 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS3, value, "value",
Michal Vasko69730152020-10-09 16:30:07 +02001956 "status", "current", "deprecated", "obsolete");
Michal Vaskob36053d2020-03-26 15:49:30 +01001957 FREE_STRING(ctx->xmlctx->ctx, value);
David Sedlákdfbbb442019-08-06 16:33:21 +02001958 return LY_EVALID;
1959 }
Michal Vaskob36053d2020-03-26 15:49:30 +01001960 FREE_STRING(ctx->xmlctx->ctx, value);
David Sedlákdfbbb442019-08-06 16:33:21 +02001961
Radek Krejcif13b87b2020-12-01 22:02:17 +01001962 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_STATUS, NULL, exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02001963}
1964
1965/**
1966 * @brief Parse when element.
1967 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001968 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001969 * @param[out] when_p When pointer to parse to.
1970 */
1971static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001972yin_parse_when(struct lys_yin_parser_ctx *ctx, struct lysp_when **when_p)
David Sedlákdfbbb442019-08-06 16:33:21 +02001973{
1974 struct lysp_when *when;
David Sedláka56e0012019-08-15 13:21:25 +02001975 LY_ERR ret = LY_SUCCESS;
1976
David Sedlákdfbbb442019-08-06 16:33:21 +02001977 when = calloc(1, sizeof *when);
Michal Vaskob36053d2020-03-26 15:49:30 +01001978 LY_CHECK_ERR_RET(!when, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
Radek Krejcif6923e82020-07-02 16:36:53 +02001979
1980 ret = lyxml_ctx_next(ctx->xmlctx);
1981 LY_CHECK_ERR_RET(ret, free(when), ret);
1982
Michal Vaskob36053d2020-03-26 15:49:30 +01001983 ret = yin_parse_attribute(ctx, YIN_ARG_CONDITION, &when->cond, Y_STR_ARG, LY_STMT_WHEN);
David Sedláka56e0012019-08-15 13:21:25 +02001984 LY_CHECK_ERR_RET(ret, free(when), ret);
1985
David Sedlákdfbbb442019-08-06 16:33:21 +02001986 *when_p = when;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001987 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001988 {LY_STMT_DESCRIPTION, &when->dsc, YIN_SUBELEM_UNIQUE},
1989 {LY_STMT_REFERENCE, &when->ref, YIN_SUBELEM_UNIQUE},
1990 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1991 };
David Sedlákdfbbb442019-08-06 16:33:21 +02001992
Radek Krejcif13b87b2020-12-01 22:02:17 +01001993 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_WHEN, NULL, &when->exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02001994}
1995
1996/**
1997 * @brief Parse yin-elemenet element.
1998 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001999 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02002000 * @param[in,out] data Data to read from, always moved to currently handled position.
2001 * @param[in,out] flags Flags to add to.
David Sedlákbf8a2b72019-08-14 16:48:10 +02002002 * @prama[in,out] exts Extension instances to add to.
David Sedlákdfbbb442019-08-06 16:33:21 +02002003 *
2004 * @return LY_ERR values.
2005 */
2006static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002007yin_parse_yin_element(struct lys_yin_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
David Sedlákdfbbb442019-08-06 16:33:21 +02002008{
2009 const char *temp_val = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002010 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002011 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
2012 };
David Sedlákdfbbb442019-08-06 16:33:21 +02002013
Michal Vaskob36053d2020-03-26 15:49:30 +01002014 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2015 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 +02002016 if (strcmp(temp_val, "true") == 0) {
2017 *flags |= LYS_YINELEM_TRUE;
2018 } else if (strcmp(temp_val, "false") == 0) {
2019 *flags |= LYS_YINELEM_FALSE;
2020 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02002021 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value",
Michal Vasko69730152020-10-09 16:30:07 +02002022 "yin-element", "true", "false");
Michal Vaskob36053d2020-03-26 15:49:30 +01002023 FREE_STRING(ctx->xmlctx->ctx, temp_val);
David Sedlákdfbbb442019-08-06 16:33:21 +02002024 return LY_EVALID;
2025 }
Michal Vaskob36053d2020-03-26 15:49:30 +01002026 FREE_STRING(ctx->xmlctx->ctx, temp_val);
David Sedlákdfbbb442019-08-06 16:33:21 +02002027
Radek Krejcif13b87b2020-12-01 22:02:17 +01002028 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_YIN_ELEMENT, NULL, exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02002029}
2030
2031/**
2032 * @brief Parse argument element.
2033 *
Michal Vaskob36053d2020-03-26 15:49:30 +01002034 * @param[in,out] xmlctx Xml context.
David Sedlákbf8a2b72019-08-14 16:48:10 +02002035 * @param[in,out] arg_meta Meta information about destionation of parsed data.
2036 * @param[in,out] exts Extension instances to add to.
David Sedlákdfbbb442019-08-06 16:33:21 +02002037 *
2038 * @return LY_ERR values.
2039 */
2040static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002041yin_parse_argument(struct lys_yin_parser_ctx *ctx, struct yin_argument_meta *arg_meta, struct lysp_ext_instance **exts)
David Sedlákdfbbb442019-08-06 16:33:21 +02002042{
Radek Krejcif13b87b2020-12-01 22:02:17 +01002043 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002044 {LY_STMT_YIN_ELEMENT, arg_meta->flags, YIN_SUBELEM_UNIQUE},
2045 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
2046 };
David Sedlákdfbbb442019-08-06 16:33:21 +02002047
Michal Vaskob36053d2020-03-26 15:49:30 +01002048 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2049 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 +02002050
Radek Krejcif13b87b2020-12-01 22:02:17 +01002051 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_ARGUMENT, NULL, exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02002052}
2053
2054/**
2055 * @brief Parse the extension statement.
2056 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02002057 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02002058 * @param[in,out] extensions Extensions to add to.
2059 *
2060 * @return LY_ERR values.
2061 */
2062static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002063yin_parse_extension(struct lys_yin_parser_ctx *ctx, struct lysp_ext **extensions)
David Sedlákdfbbb442019-08-06 16:33:21 +02002064{
2065 struct lysp_ext *ex;
Michal Vasko69730152020-10-09 16:30:07 +02002066
Michal Vaskob36053d2020-03-26 15:49:30 +01002067 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *extensions, ex, LY_EMEM);
2068 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2069 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 +02002070
2071 struct yin_argument_meta arg_info = {&ex->flags, &ex->argument};
Radek Krejcif13b87b2020-12-01 22:02:17 +01002072 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002073 {LY_STMT_ARGUMENT, &arg_info, YIN_SUBELEM_UNIQUE},
2074 {LY_STMT_DESCRIPTION, &ex->dsc, YIN_SUBELEM_UNIQUE},
2075 {LY_STMT_REFERENCE, &ex->ref, YIN_SUBELEM_UNIQUE},
2076 {LY_STMT_STATUS, &ex->flags, YIN_SUBELEM_UNIQUE},
2077 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
2078 };
David Sedlákdfbbb442019-08-06 16:33:21 +02002079
Radek Krejcif13b87b2020-12-01 22:02:17 +01002080 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_EXTENSION, NULL, &ex->exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02002081}
2082
2083/**
David Sedlák5e13dea2019-07-22 16:06:45 +02002084 * @brief Parse feature element.
2085 *
2086 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák5e13dea2019-07-22 16:06:45 +02002087 * @param[in,out] features Features to add to.
2088 *
2089 * @return LY_ERR values.
2090 */
2091static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002092yin_parse_feature(struct lys_yin_parser_ctx *ctx, struct lysp_feature **features)
David Sedlák5e13dea2019-07-22 16:06:45 +02002093{
2094 struct lysp_feature *feat;
2095
2096 /* allocate new feature */
Michal Vaskob36053d2020-03-26 15:49:30 +01002097 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *features, feat, LY_EMEM);
David Sedlák5e13dea2019-07-22 16:06:45 +02002098
2099 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002100 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2101 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 +02002102
2103 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002104 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002105 {LY_STMT_DESCRIPTION, &feat->dsc, YIN_SUBELEM_UNIQUE},
2106 {LY_STMT_IF_FEATURE, &feat->iffeatures, 0},
2107 {LY_STMT_REFERENCE, &feat->ref, YIN_SUBELEM_UNIQUE},
2108 {LY_STMT_STATUS, &feat->flags, YIN_SUBELEM_UNIQUE},
2109 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
2110 };
Michal Vasko69730152020-10-09 16:30:07 +02002111
Radek Krejcif13b87b2020-12-01 22:02:17 +01002112 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_FEATURE, NULL, &feat->exts);
David Sedlák5e13dea2019-07-22 16:06:45 +02002113}
2114
2115/**
David Sedlák28794f22019-07-22 16:45:00 +02002116 * @brief Parse identity element.
2117 *
2118 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák28794f22019-07-22 16:45:00 +02002119 * @param[in,out] identities Identities to add to.
2120 *
2121 * @return LY_ERR values.
2122 */
2123static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002124yin_parse_identity(struct lys_yin_parser_ctx *ctx, struct lysp_ident **identities)
David Sedlák28794f22019-07-22 16:45:00 +02002125{
2126 struct lysp_ident *ident;
2127
2128 /* allocate new identity */
Michal Vaskob36053d2020-03-26 15:49:30 +01002129 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *identities, ident, LY_EMEM);
David Sedlák28794f22019-07-22 16:45:00 +02002130
2131 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002132 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2133 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 +02002134
2135 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002136 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002137 {LY_STMT_BASE, &ident->bases, 0},
2138 {LY_STMT_DESCRIPTION, &ident->dsc, YIN_SUBELEM_UNIQUE},
2139 {LY_STMT_IF_FEATURE, &ident->iffeatures, YIN_SUBELEM_VER2},
2140 {LY_STMT_REFERENCE, &ident->ref, YIN_SUBELEM_UNIQUE},
2141 {LY_STMT_STATUS, &ident->flags, YIN_SUBELEM_UNIQUE},
2142 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
2143 };
Michal Vasko69730152020-10-09 16:30:07 +02002144
Radek Krejcif13b87b2020-12-01 22:02:17 +01002145 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_IDENTITY, NULL, &ident->exts);
David Sedlák28794f22019-07-22 16:45:00 +02002146}
2147
2148/**
David Sedlákaf536aa2019-07-23 13:42:23 +02002149 * @brief Parse list element.
2150 *
2151 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákad83cf92019-08-13 12:53:53 +02002152 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlákaf536aa2019-07-23 13:42:23 +02002153 *
2154 * @return LY_ERR values.
2155 */
2156static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002157yin_parse_list(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta)
David Sedlákaf536aa2019-07-23 13:42:23 +02002158{
David Sedlákaf536aa2019-07-23 13:42:23 +02002159 struct lysp_node_list *list;
David Sedlák81497a32019-08-13 16:56:26 +02002160 LY_ERR ret = LY_SUCCESS;
2161 struct yin_subelement *subelems = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002162 size_t subelems_size;
David Sedlákaf536aa2019-07-23 13:42:23 +02002163
Michal Vaskob36053d2020-03-26 15:49:30 +01002164 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, list, next, LY_EMEM);
David Sedlákaf536aa2019-07-23 13:42:23 +02002165 list->nodetype = LYS_LIST;
2166 list->parent = node_meta->parent;
2167
David Sedlákaf536aa2019-07-23 13:42:23 +02002168 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002169 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2170 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 +02002171
2172 /* parse list content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002173 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 25, (struct lysp_node *)list, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02002174 LY_STMT_ACTION, &list->actions, 0,
2175 LY_STMT_ANYDATA, &list->child, 0,
2176 LY_STMT_ANYXML, &list->child, 0,
2177 LY_STMT_CHOICE, &list->child, 0,
2178 LY_STMT_CONFIG, &list->flags, YIN_SUBELEM_UNIQUE,
2179 LY_STMT_CONTAINER, &list->child, 0,
2180 LY_STMT_DESCRIPTION, &list->dsc, YIN_SUBELEM_UNIQUE,
2181 LY_STMT_GROUPING, &list->groupings, 0,
2182 LY_STMT_IF_FEATURE, &list->iffeatures, 0,
2183 LY_STMT_KEY, &list->key, YIN_SUBELEM_UNIQUE,
2184 LY_STMT_LEAF, &list->child, 0,
2185 LY_STMT_LEAF_LIST, &list->child, 0,
2186 LY_STMT_LIST, &list->child, 0,
2187 LY_STMT_MAX_ELEMENTS, list, YIN_SUBELEM_UNIQUE,
2188 LY_STMT_MIN_ELEMENTS, list, YIN_SUBELEM_UNIQUE,
2189 LY_STMT_MUST, &list->musts, 0,
2190 LY_STMT_NOTIFICATION, &list->notifs, 0,
2191 LY_STMT_ORDERED_BY, &list->flags, YIN_SUBELEM_UNIQUE,
2192 LY_STMT_REFERENCE, &list->ref, YIN_SUBELEM_UNIQUE,
2193 LY_STMT_STATUS, &list->flags, YIN_SUBELEM_UNIQUE,
2194 LY_STMT_TYPEDEF, &list->typedefs, 0,
2195 LY_STMT_UNIQUE, &list->uniques, 0,
2196 LY_STMT_USES, &list->child, 0,
2197 LY_STMT_WHEN, &list->when, YIN_SUBELEM_UNIQUE,
2198 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
Radek Krejcif13b87b2020-12-01 22:02:17 +01002199 ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_LIST, NULL, &list->exts);
2200 subelems_deallocator(subelems_size, subelems);
David Sedlák81497a32019-08-13 16:56:26 +02002201 LY_CHECK_RET(ret);
David Sedlákaf536aa2019-07-23 13:42:23 +02002202
2203 /* finalize parent pointers to the reallocated items */
2204 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, list->groupings, NULL, list->actions, list->notifs));
2205
Michal Vasko69730152020-10-09 16:30:07 +02002206 if (list->max && (list->min > list->max)) {
David Sedlák1538a842019-08-08 15:38:51 +02002207 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_MINMAX, list->min, list->max);
David Sedlákaf536aa2019-07-23 13:42:23 +02002208 return LY_EVALID;
2209 }
2210
2211 return LY_SUCCESS;
2212}
2213
2214/**
David Sedlák031b9e72019-07-23 15:19:37 +02002215 * @brief Parse notification element.
2216 *
2217 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák05404f62019-07-24 14:11:53 +02002218 * @param[in,out] notif_meta Meta information about parent node and notifications to add to.
David Sedlák031b9e72019-07-23 15:19:37 +02002219 *
2220 * @return LY_ERR values.
2221 */
2222static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002223yin_parse_notification(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *notif_meta)
David Sedlák031b9e72019-07-23 15:19:37 +02002224{
2225 struct lysp_notif *notif;
David Sedlákbf8a2b72019-08-14 16:48:10 +02002226 struct lysp_notif **notifs = (struct lysp_notif **)notif_meta->nodes;
David Sedlák81497a32019-08-13 16:56:26 +02002227 LY_ERR ret = LY_SUCCESS;
2228 struct yin_subelement *subelems = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002229 size_t subelems_size;
David Sedlák031b9e72019-07-23 15:19:37 +02002230
2231 /* allocate new notification */
Michal Vaskob36053d2020-03-26 15:49:30 +01002232 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *notifs, notif, LY_EMEM);
David Sedlák031b9e72019-07-23 15:19:37 +02002233 notif->nodetype = LYS_NOTIF;
2234 notif->parent = notif_meta->parent;
2235
2236 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002237 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2238 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 +02002239
2240 /* parse notification content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002241 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 16, (struct lysp_node *)notif, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02002242 LY_STMT_ANYDATA, &notif->data, 0,
2243 LY_STMT_ANYXML, &notif->data, 0,
2244 LY_STMT_CHOICE, &notif->data, 0,
2245 LY_STMT_CONTAINER, &notif->data, 0,
2246 LY_STMT_DESCRIPTION, &notif->dsc, YIN_SUBELEM_UNIQUE,
2247 LY_STMT_GROUPING, &notif->groupings, 0,
2248 LY_STMT_IF_FEATURE, &notif->iffeatures, 0,
2249 LY_STMT_LEAF, &notif->data, 0,
2250 LY_STMT_LEAF_LIST, &notif->data, 0,
2251 LY_STMT_LIST, &notif->data, 0,
2252 LY_STMT_MUST, &notif->musts, YIN_SUBELEM_VER2,
2253 LY_STMT_REFERENCE, &notif->ref, YIN_SUBELEM_UNIQUE,
2254 LY_STMT_STATUS, &notif->flags, YIN_SUBELEM_UNIQUE,
2255 LY_STMT_TYPEDEF, &notif->typedefs, 0,
2256 LY_STMT_USES, &notif->data, 0,
2257 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
David Sedlák81497a32019-08-13 16:56:26 +02002258
Radek Krejcif13b87b2020-12-01 22:02:17 +01002259 ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_NOTIFICATION, NULL, &notif->exts);
2260 subelems_deallocator(subelems_size, subelems);
David Sedlák81497a32019-08-13 16:56:26 +02002261 LY_CHECK_RET(ret);
David Sedlák031b9e72019-07-23 15:19:37 +02002262
2263 /* finalize parent pointers to the reallocated items */
2264 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, notif->groupings, NULL, NULL, NULL));
2265
2266 return LY_SUCCESS;
2267}
2268
2269/**
David Sedlákbf8a2b72019-08-14 16:48:10 +02002270 * @brief Parse grouping element.
David Sedláke3ce9ef2019-07-23 16:34:30 +02002271 *
2272 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbf8a2b72019-08-14 16:48:10 +02002273 * @param[in,out] gr_meta Meta information about parent node and groupings to add to.
David Sedláke3ce9ef2019-07-23 16:34:30 +02002274 *
2275 * @return LY_ERR values.
2276 */
2277static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002278yin_parse_grouping(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *gr_meta)
David Sedláke3ce9ef2019-07-23 16:34:30 +02002279{
2280 struct lysp_grp *grp;
David Sedlákbf8a2b72019-08-14 16:48:10 +02002281 struct lysp_grp **grps = (struct lysp_grp **)gr_meta->nodes;
David Sedlák81497a32019-08-13 16:56:26 +02002282 LY_ERR ret = LY_SUCCESS;
2283 struct yin_subelement *subelems = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002284 size_t subelems_size;
David Sedláke3ce9ef2019-07-23 16:34:30 +02002285
2286 /* create new grouping */
Michal Vaskob36053d2020-03-26 15:49:30 +01002287 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *grps, grp, LY_EMEM);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002288 grp->nodetype = LYS_GROUPING;
2289 grp->parent = gr_meta->parent;
2290
2291 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002292 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2293 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 +02002294
2295 /* parse grouping content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002296 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 16, (struct lysp_node *)grp, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02002297 LY_STMT_ACTION, &grp->actions, 0,
2298 LY_STMT_ANYDATA, &grp->data, 0,
2299 LY_STMT_ANYXML, &grp->data, 0,
2300 LY_STMT_CHOICE, &grp->data, 0,
2301 LY_STMT_CONTAINER, &grp->data, 0,
2302 LY_STMT_DESCRIPTION, &grp->dsc, YIN_SUBELEM_UNIQUE,
2303 LY_STMT_GROUPING, &grp->groupings, 0,
2304 LY_STMT_LEAF, &grp->data, 0,
2305 LY_STMT_LEAF_LIST, &grp->data, 0,
2306 LY_STMT_LIST, &grp->data, 0,
2307 LY_STMT_NOTIFICATION, &grp->notifs, 0,
2308 LY_STMT_REFERENCE, &grp->ref, YIN_SUBELEM_UNIQUE,
2309 LY_STMT_STATUS, &grp->flags, YIN_SUBELEM_UNIQUE,
2310 LY_STMT_TYPEDEF, &grp->typedefs, 0,
2311 LY_STMT_USES, &grp->data, 0,
2312 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
Radek Krejcif13b87b2020-12-01 22:02:17 +01002313 ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_GROUPING, NULL, &grp->exts);
2314 subelems_deallocator(subelems_size, subelems);
David Sedlák81497a32019-08-13 16:56:26 +02002315 LY_CHECK_RET(ret);
2316
David Sedláke3ce9ef2019-07-23 16:34:30 +02002317 /* finalize parent pointers to the reallocated items */
2318 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, grp->groupings, NULL, grp->actions, grp->notifs));
2319
2320 return LY_SUCCESS;
2321}
2322
2323/**
David Sedlákbf8a2b72019-08-14 16:48:10 +02002324 * @brief Parse container element.
David Sedlákf111bcb2019-07-23 17:15:51 +02002325 *
2326 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákad83cf92019-08-13 12:53:53 +02002327 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlákf111bcb2019-07-23 17:15:51 +02002328 *
2329 * @return LY_ERR values.
2330 */
2331static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002332yin_parse_container(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta)
David Sedlákf111bcb2019-07-23 17:15:51 +02002333{
David Sedlákf111bcb2019-07-23 17:15:51 +02002334 struct lysp_node_container *cont;
David Sedlák81497a32019-08-13 16:56:26 +02002335 LY_ERR ret = LY_SUCCESS;
2336 struct yin_subelement *subelems = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002337 size_t subelems_size;
David Sedlákf111bcb2019-07-23 17:15:51 +02002338
2339 /* create new container */
Michal Vaskob36053d2020-03-26 15:49:30 +01002340 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, cont, next, LY_EMEM);
David Sedlákf111bcb2019-07-23 17:15:51 +02002341 cont->nodetype = LYS_CONTAINER;
2342 cont->parent = node_meta->parent;
2343
David Sedlákf111bcb2019-07-23 17:15:51 +02002344 /* parse aegument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002345 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2346 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 +02002347
2348 /* parse container content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002349 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 21, (struct lysp_node *)cont, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02002350 LY_STMT_ACTION, &cont->actions, YIN_SUBELEM_VER2,
2351 LY_STMT_ANYDATA, &cont->child, YIN_SUBELEM_VER2,
2352 LY_STMT_ANYXML, &cont->child, 0,
2353 LY_STMT_CHOICE, &cont->child, 0,
2354 LY_STMT_CONFIG, &cont->flags, YIN_SUBELEM_UNIQUE,
2355 LY_STMT_CONTAINER, &cont->child, 0,
2356 LY_STMT_DESCRIPTION, &cont->dsc, YIN_SUBELEM_UNIQUE,
2357 LY_STMT_GROUPING, &cont->groupings, 0,
2358 LY_STMT_IF_FEATURE, &cont->iffeatures, 0,
2359 LY_STMT_LEAF, &cont->child, 0,
2360 LY_STMT_LEAF_LIST, &cont->child, 0,
2361 LY_STMT_LIST, &cont->child, 0,
2362 LY_STMT_MUST, &cont->musts, 0,
2363 LY_STMT_NOTIFICATION, &cont->notifs, YIN_SUBELEM_VER2,
2364 LY_STMT_PRESENCE, &cont->presence, YIN_SUBELEM_UNIQUE,
2365 LY_STMT_REFERENCE, &cont->ref, YIN_SUBELEM_UNIQUE,
2366 LY_STMT_STATUS, &cont->flags, YIN_SUBELEM_UNIQUE,
2367 LY_STMT_TYPEDEF, &cont->typedefs, 0,
2368 LY_STMT_USES, &cont->child, 0,
2369 LY_STMT_WHEN, &cont->when, YIN_SUBELEM_UNIQUE,
2370 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
Radek Krejcif13b87b2020-12-01 22:02:17 +01002371 ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_CONTAINER, NULL, &cont->exts);
2372 subelems_deallocator(subelems_size, subelems);
David Sedlák81497a32019-08-13 16:56:26 +02002373 LY_CHECK_RET(ret);
2374
David Sedlákf111bcb2019-07-23 17:15:51 +02002375 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, cont->groupings, NULL, cont->actions, cont->notifs));
2376
2377 return LY_SUCCESS;
2378}
2379
2380/**
David Sedlák5379d392019-07-24 10:42:03 +02002381 * @brief Parse case element.
2382 *
2383 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákad83cf92019-08-13 12:53:53 +02002384 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlák5379d392019-07-24 10:42:03 +02002385 *
2386 * @return LY_ERR values.
2387 */
2388static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002389yin_parse_case(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta)
David Sedlák5379d392019-07-24 10:42:03 +02002390{
David Sedlák5379d392019-07-24 10:42:03 +02002391 struct lysp_node_case *cas;
David Sedlák81497a32019-08-13 16:56:26 +02002392 LY_ERR ret = LY_SUCCESS;
Michal Vasko69730152020-10-09 16:30:07 +02002393 struct yin_subelement *subelems = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002394 size_t subelems_size;
David Sedlák5379d392019-07-24 10:42:03 +02002395
2396 /* create new case */
Michal Vaskob36053d2020-03-26 15:49:30 +01002397 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, cas, next, LY_EMEM);
David Sedlák5379d392019-07-24 10:42:03 +02002398 cas->nodetype = LYS_CASE;
2399 cas->parent = node_meta->parent;
2400
David Sedlák5379d392019-07-24 10:42:03 +02002401 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002402 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2403 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 +02002404
2405 /* parse case content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002406 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 14, (struct lysp_node *)cas, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02002407 LY_STMT_ANYDATA, &cas->child, YIN_SUBELEM_VER2,
2408 LY_STMT_ANYXML, &cas->child, 0,
2409 LY_STMT_CHOICE, &cas->child, 0,
2410 LY_STMT_CONTAINER, &cas->child, 0,
2411 LY_STMT_DESCRIPTION, &cas->dsc, YIN_SUBELEM_UNIQUE,
2412 LY_STMT_IF_FEATURE, &cas->iffeatures, 0,
2413 LY_STMT_LEAF, &cas->child, 0,
2414 LY_STMT_LEAF_LIST, &cas->child, 0,
2415 LY_STMT_LIST, &cas->child, 0,
2416 LY_STMT_REFERENCE, &cas->ref, YIN_SUBELEM_UNIQUE,
2417 LY_STMT_STATUS, &cas->flags, YIN_SUBELEM_UNIQUE,
2418 LY_STMT_USES, &cas->child, 0,
2419 LY_STMT_WHEN, &cas->when, YIN_SUBELEM_UNIQUE,
2420 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
Radek Krejcif13b87b2020-12-01 22:02:17 +01002421 ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_CASE, NULL, &cas->exts);
2422 subelems_deallocator(subelems_size, subelems);
David Sedlák81497a32019-08-13 16:56:26 +02002423
2424 return ret;
David Sedlák5379d392019-07-24 10:42:03 +02002425}
2426
2427/**
David Sedlák05404f62019-07-24 14:11:53 +02002428 * @brief Parse choice element.
David Sedlákb7abcfa2019-07-24 12:33:35 +02002429 *
2430 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákad83cf92019-08-13 12:53:53 +02002431 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlákb7abcfa2019-07-24 12:33:35 +02002432 *
2433 * @return LY_ERR values.
2434 */
2435LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002436yin_parse_choice(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta)
David Sedlákb7abcfa2019-07-24 12:33:35 +02002437{
David Sedlák81497a32019-08-13 16:56:26 +02002438 LY_ERR ret = LY_SUCCESS;
2439 struct yin_subelement *subelems = NULL;
David Sedlákb7abcfa2019-07-24 12:33:35 +02002440 struct lysp_node_choice *choice;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002441 size_t subelems_size;
David Sedlákb7abcfa2019-07-24 12:33:35 +02002442
2443 /* create new choice */
Michal Vaskob36053d2020-03-26 15:49:30 +01002444 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, choice, next, LY_EMEM);
David Sedlák8d552d62019-08-06 15:29:05 +02002445
David Sedlákb7abcfa2019-07-24 12:33:35 +02002446 choice->nodetype = LYS_CHOICE;
2447 choice->parent = node_meta->parent;
2448
David Sedlákb7abcfa2019-07-24 12:33:35 +02002449 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002450 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2451 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 +02002452
2453 /* parse choice content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002454 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 17, (struct lysp_node *)choice, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02002455 LY_STMT_ANYDATA, &choice->child, YIN_SUBELEM_VER2,
2456 LY_STMT_ANYXML, &choice->child, 0,
2457 LY_STMT_CASE, &choice->child, 0,
2458 LY_STMT_CHOICE, &choice->child, YIN_SUBELEM_VER2,
2459 LY_STMT_CONFIG, &choice->flags, YIN_SUBELEM_UNIQUE,
2460 LY_STMT_CONTAINER, &choice->child, 0,
2461 LY_STMT_DEFAULT, &choice->dflt, YIN_SUBELEM_UNIQUE,
2462 LY_STMT_DESCRIPTION, &choice->dsc, YIN_SUBELEM_UNIQUE,
2463 LY_STMT_IF_FEATURE, &choice->iffeatures, 0,
2464 LY_STMT_LEAF, &choice->child, 0,
2465 LY_STMT_LEAF_LIST, &choice->child, 0,
2466 LY_STMT_LIST, &choice->child, 0,
2467 LY_STMT_MANDATORY, &choice->flags, YIN_SUBELEM_UNIQUE,
2468 LY_STMT_REFERENCE, &choice->ref, YIN_SUBELEM_UNIQUE,
2469 LY_STMT_STATUS, &choice->flags, YIN_SUBELEM_UNIQUE,
2470 LY_STMT_WHEN, &choice->when, YIN_SUBELEM_UNIQUE,
2471 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
Radek Krejcif13b87b2020-12-01 22:02:17 +01002472 ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_CHOICE, NULL, &choice->exts);
2473 subelems_deallocator(subelems_size, subelems);
David Sedlák81497a32019-08-13 16:56:26 +02002474 return ret;
David Sedlákb7abcfa2019-07-24 12:33:35 +02002475}
2476
2477/**
David Sedlák05404f62019-07-24 14:11:53 +02002478 * @brief Parse input or output element.
2479 *
2480 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbf8a2b72019-08-14 16:48:10 +02002481 * @param[in] inout_kw Identification of input/output element.
David Sedlák05404f62019-07-24 14:11:53 +02002482 * @param[in] inout_meta Meta information about parent node and siblings and input/output pointer to write to.
2483 *
2484 * @return LY_ERR values.
2485 */
2486static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002487yin_parse_inout(struct lys_yin_parser_ctx *ctx, enum ly_stmt inout_kw, struct inout_meta *inout_meta)
David Sedlák05404f62019-07-24 14:11:53 +02002488{
David Sedlák81497a32019-08-13 16:56:26 +02002489 LY_ERR ret = LY_SUCCESS;
2490 struct yin_subelement *subelems = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002491 size_t subelems_size;
David Sedlák81497a32019-08-13 16:56:26 +02002492
David Sedlák05404f62019-07-24 14:11:53 +02002493 /* initiate structure */
Radek Krejcid6b76452019-09-03 17:03:03 +02002494 inout_meta->inout_p->nodetype = (inout_kw == LY_STMT_INPUT) ? LYS_INPUT : LYS_OUTPUT;
David Sedlák05404f62019-07-24 14:11:53 +02002495 inout_meta->inout_p->parent = inout_meta->parent;
2496
2497 /* check attributes */
Michal Vaskob36053d2020-03-26 15:49:30 +01002498 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2499 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 +02002500
2501 /* parser input/output content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002502 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 12, (struct lysp_node *)inout_meta->inout_p, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02002503 LY_STMT_ANYDATA, &inout_meta->inout_p->data, YIN_SUBELEM_VER2,
2504 LY_STMT_ANYXML, &inout_meta->inout_p->data, 0,
2505 LY_STMT_CHOICE, &inout_meta->inout_p->data, 0,
2506 LY_STMT_CONTAINER, &inout_meta->inout_p->data, 0,
2507 LY_STMT_GROUPING, &inout_meta->inout_p->groupings, 0,
2508 LY_STMT_LEAF, &inout_meta->inout_p->data, 0,
2509 LY_STMT_LEAF_LIST, &inout_meta->inout_p->data, 0,
2510 LY_STMT_LIST, &inout_meta->inout_p->data, 0,
2511 LY_STMT_MUST, &inout_meta->inout_p->musts, YIN_SUBELEM_VER2,
2512 LY_STMT_TYPEDEF, &inout_meta->inout_p->typedefs, 0,
2513 LY_STMT_USES, &inout_meta->inout_p->data, 0,
2514 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
Radek Krejcif13b87b2020-12-01 22:02:17 +01002515 ret = yin_parse_content(ctx, subelems, subelems_size, inout_kw, NULL, &inout_meta->inout_p->exts);
2516 subelems_deallocator(subelems_size, subelems);
David Sedlák81497a32019-08-13 16:56:26 +02002517 LY_CHECK_RET(ret);
David Sedlák05404f62019-07-24 14:11:53 +02002518
Michal Vaskob83af8a2020-01-06 09:49:22 +01002519 if (!inout_meta->inout_p->data) {
2520 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_MISSTMT, "data-def-stmt", ly_stmt2str(inout_kw));
2521 return LY_EVALID;
2522 }
2523
David Sedlák05404f62019-07-24 14:11:53 +02002524 /* finalize parent pointers to the reallocated items */
David Sedlák81497a32019-08-13 16:56:26 +02002525 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, inout_meta->inout_p->groupings, NULL, NULL, NULL));
David Sedlák05404f62019-07-24 14:11:53 +02002526
2527 return LY_SUCCESS;
2528}
2529
David Sedlák992fb7c2019-07-24 16:51:01 +02002530/**
2531 * @brief Parse action element.
2532 *
2533 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák992fb7c2019-07-24 16:51:01 +02002534 * @param[in] act_meta Meta information about parent node and actions to add to.
2535 *
2536 * @return LY_ERR values.
2537 */
David Sedlák85d0eca2019-07-24 15:15:21 +02002538static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002539yin_parse_action(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *act_meta)
David Sedlák85d0eca2019-07-24 15:15:21 +02002540{
David Sedlákbf8a2b72019-08-14 16:48:10 +02002541 struct lysp_action *act, **acts = (struct lysp_action **)act_meta->nodes;
David Sedlák81497a32019-08-13 16:56:26 +02002542 LY_ERR ret = LY_SUCCESS;
2543 struct yin_subelement *subelems = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002544 size_t subelems_size;
David Sedlák85d0eca2019-07-24 15:15:21 +02002545
2546 /* create new action */
Michal Vaskob36053d2020-03-26 15:49:30 +01002547 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *acts, act, LY_EMEM);
Michal Vasko1bf09392020-03-27 12:38:10 +01002548 act->nodetype = act_meta->parent ? LYS_ACTION : LYS_RPC;
David Sedlák85d0eca2019-07-24 15:15:21 +02002549 act->parent = act_meta->parent;
2550
2551 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002552 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2553 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &act->name, Y_IDENTIF_ARG, LY_STMT_ACTION));
David Sedlák85d0eca2019-07-24 15:15:21 +02002554
2555 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002556 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 9, (struct lysp_node *)act, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02002557 LY_STMT_DESCRIPTION, &act->dsc, YIN_SUBELEM_UNIQUE,
2558 LY_STMT_GROUPING, &act->groupings, 0,
2559 LY_STMT_IF_FEATURE, &act->iffeatures, 0,
2560 LY_STMT_INPUT, &act->input, YIN_SUBELEM_UNIQUE,
2561 LY_STMT_OUTPUT, &act->output, YIN_SUBELEM_UNIQUE,
2562 LY_STMT_REFERENCE, &act->ref, YIN_SUBELEM_UNIQUE,
2563 LY_STMT_STATUS, &act->flags, YIN_SUBELEM_UNIQUE,
2564 LY_STMT_TYPEDEF, &act->typedefs, 0,
2565 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
Radek Krejcif13b87b2020-12-01 22:02:17 +01002566 ret = (yin_parse_content(ctx, subelems, subelems_size, LY_STMT_ACTION, NULL, &act->exts));
2567 subelems_deallocator(subelems_size, subelems);
David Sedlák81497a32019-08-13 16:56:26 +02002568 LY_CHECK_RET(ret);
2569
Michal Vasko7f45cf22020-10-01 12:49:44 +02002570 /* always initialize inout, they are technically present (needed for later deviations/refines) */
2571 if (!act->input.nodetype) {
2572 act->input.nodetype = LYS_INPUT;
2573 act->input.parent = (struct lysp_node *)act;
2574 }
2575 if (!act->output.nodetype) {
2576 act->output.nodetype = LYS_OUTPUT;
2577 act->output.parent = (struct lysp_node *)act;
2578 }
2579
David Sedlák85d0eca2019-07-24 15:15:21 +02002580 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, act->groupings, NULL, NULL, NULL));
2581
2582 return LY_SUCCESS;
2583}
2584
David Sedlák05404f62019-07-24 14:11:53 +02002585/**
David Sedlák992fb7c2019-07-24 16:51:01 +02002586 * @brief Parse augment element.
2587 *
2588 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák992fb7c2019-07-24 16:51:01 +02002589 * @param[in] aug_meta Meta information about parent node and augments to add to.
2590 *
2591 * @return LY_ERR values.
2592 */
2593static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002594yin_parse_augment(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *aug_meta)
David Sedlák992fb7c2019-07-24 16:51:01 +02002595{
2596 struct lysp_augment *aug;
David Sedlákbf8a2b72019-08-14 16:48:10 +02002597 struct lysp_augment **augs = (struct lysp_augment **)aug_meta->nodes;
David Sedlák81497a32019-08-13 16:56:26 +02002598 LY_ERR ret = LY_SUCCESS;
2599 struct yin_subelement *subelems = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002600 size_t subelems_size;
David Sedlák992fb7c2019-07-24 16:51:01 +02002601
2602 /* create new augment */
Michal Vaskob36053d2020-03-26 15:49:30 +01002603 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *augs, aug, LY_EMEM);
David Sedlák992fb7c2019-07-24 16:51:01 +02002604 aug->nodetype = LYS_AUGMENT;
2605 aug->parent = aug_meta->parent;
2606
2607 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002608 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2609 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_TARGET_NODE, &aug->nodeid, Y_STR_ARG, LY_STMT_AUGMENT));
2610 CHECK_NONEMPTY((struct lys_parser_ctx *)ctx, strlen(aug->nodeid), "augment");
David Sedlák992fb7c2019-07-24 16:51:01 +02002611
2612 /* parser augment content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002613 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 17, (struct lysp_node *)aug, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02002614 LY_STMT_ACTION, &aug->actions, YIN_SUBELEM_VER2,
2615 LY_STMT_ANYDATA, &aug->child, YIN_SUBELEM_VER2,
2616 LY_STMT_ANYXML, &aug->child, 0,
2617 LY_STMT_CASE, &aug->child, 0,
2618 LY_STMT_CHOICE, &aug->child, 0,
2619 LY_STMT_CONTAINER, &aug->child, 0,
2620 LY_STMT_DESCRIPTION, &aug->dsc, YIN_SUBELEM_UNIQUE,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002621 LY_STMT_IF_FEATURE, &aug->iffeatures, 0,
Michal Vasko69730152020-10-09 16:30:07 +02002622 LY_STMT_LEAF, &aug->child, 0,
2623 LY_STMT_LEAF_LIST, &aug->child, 0,
2624 LY_STMT_LIST, &aug->child, 0,
2625 LY_STMT_NOTIFICATION, &aug->notifs, YIN_SUBELEM_VER2,
2626 LY_STMT_REFERENCE, &aug->ref, YIN_SUBELEM_UNIQUE,
2627 LY_STMT_STATUS, &aug->flags, YIN_SUBELEM_UNIQUE,
2628 LY_STMT_USES, &aug->child, 0,
2629 LY_STMT_WHEN, &aug->when, YIN_SUBELEM_UNIQUE,
2630 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
Radek Krejcif13b87b2020-12-01 22:02:17 +01002631 ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_AUGMENT, NULL, &aug->exts);
2632 subelems_deallocator(subelems_size, subelems);
David Sedlák81497a32019-08-13 16:56:26 +02002633 LY_CHECK_RET(ret);
David Sedlák992fb7c2019-07-24 16:51:01 +02002634
2635 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, NULL, NULL, aug->actions, aug->notifs));
2636
2637 return LY_SUCCESS;
2638}
2639
David Sedlák8b754462019-07-25 16:22:13 +02002640/**
2641 * @brief Parse deviate element.
2642 *
2643 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák8b754462019-07-25 16:22:13 +02002644 * @param[in] deviates Deviates to add to.
2645 *
2646 * @return LY_ERR values.
2647 */
David Sedlák4ffcec82019-07-25 15:10:21 +02002648static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002649yin_parse_deviate(struct lys_yin_parser_ctx *ctx, struct lysp_deviate **deviates)
David Sedlák4ffcec82019-07-25 15:10:21 +02002650{
2651 LY_ERR ret = LY_SUCCESS;
2652 uint8_t dev_mod;
2653 const char *temp_val;
David Sedlák8d552d62019-08-06 15:29:05 +02002654 struct lysp_deviate *d;
David Sedlák4ffcec82019-07-25 15:10:21 +02002655 struct lysp_deviate_add *d_add = NULL;
2656 struct lysp_deviate_rpl *d_rpl = NULL;
2657 struct lysp_deviate_del *d_del = NULL;
2658
2659 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002660 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2661 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 +02002662
2663 if (strcmp(temp_val, "not-supported") == 0) {
2664 dev_mod = LYS_DEV_NOT_SUPPORTED;
2665 } else if (strcmp(temp_val, "add") == 0) {
2666 dev_mod = LYS_DEV_ADD;
2667 } else if (strcmp(temp_val, "replace") == 0) {
2668 dev_mod = LYS_DEV_REPLACE;
2669 } else if (strcmp(temp_val, "delete") == 0) {
2670 dev_mod = LYS_DEV_DELETE;
2671 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02002672 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS4, temp_val, "value", "deviate",
Michal Vasko69730152020-10-09 16:30:07 +02002673 "not-supported", "add", "replace", "delete");
Michal Vaskob36053d2020-03-26 15:49:30 +01002674 FREE_STRING(ctx->xmlctx->ctx, temp_val);
David Sedlák4ffcec82019-07-25 15:10:21 +02002675 return LY_EVALID;
2676 }
Michal Vaskob36053d2020-03-26 15:49:30 +01002677 FREE_STRING(ctx->xmlctx->ctx, temp_val);
David Sedlák4ffcec82019-07-25 15:10:21 +02002678
2679 if (dev_mod == LYS_DEV_NOT_SUPPORTED) {
2680 d = calloc(1, sizeof *d);
Michal Vaskob36053d2020-03-26 15:49:30 +01002681 LY_CHECK_ERR_RET(!d, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
Radek Krejcif13b87b2020-12-01 22:02:17 +01002682 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002683 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
2684 };
Radek Krejcif13b87b2020-12-01 22:02:17 +01002685 ret = yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_DEVIATE, NULL, &d->exts);
David Sedlák4ffcec82019-07-25 15:10:21 +02002686
2687 } else if (dev_mod == LYS_DEV_ADD) {
2688 d_add = calloc(1, sizeof *d_add);
Michal Vaskob36053d2020-03-26 15:49:30 +01002689 LY_CHECK_ERR_RET(!d_add, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
David Sedlák4ffcec82019-07-25 15:10:21 +02002690 d = (struct lysp_deviate *)d_add;
2691 struct minmax_dev_meta min = {&d_add->min, &d_add->flags, &d_add->exts};
2692 struct minmax_dev_meta max = {&d_add->max, &d_add->flags, &d_add->exts};
Radek Krejcif13b87b2020-12-01 22:02:17 +01002693 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002694 {LY_STMT_CONFIG, &d_add->flags, YIN_SUBELEM_UNIQUE},
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002695 {LY_STMT_DEFAULT, &d_add->dflts, 0},
Radek Krejci0f969882020-08-21 16:56:47 +02002696 {LY_STMT_MANDATORY, &d_add->flags, YIN_SUBELEM_UNIQUE},
2697 {LY_STMT_MAX_ELEMENTS, &max, YIN_SUBELEM_UNIQUE},
2698 {LY_STMT_MIN_ELEMENTS, &min, YIN_SUBELEM_UNIQUE},
2699 {LY_STMT_MUST, &d_add->musts, 0},
2700 {LY_STMT_UNIQUE, &d_add->uniques, 0},
2701 {LY_STMT_UNITS, &d_add->units, YIN_SUBELEM_UNIQUE},
2702 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
2703 };
Radek Krejcif13b87b2020-12-01 22:02:17 +01002704 ret = yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_DEVIATE, NULL, &d_add->exts);
David Sedlák4ffcec82019-07-25 15:10:21 +02002705
2706 } else if (dev_mod == LYS_DEV_REPLACE) {
2707 d_rpl = calloc(1, sizeof *d_rpl);
Michal Vaskob36053d2020-03-26 15:49:30 +01002708 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
David Sedlák4ffcec82019-07-25 15:10:21 +02002709 d = (struct lysp_deviate *)d_rpl;
2710 struct minmax_dev_meta min = {&d_rpl->min, &d_rpl->flags, &d_rpl->exts};
2711 struct minmax_dev_meta max = {&d_rpl->max, &d_rpl->flags, &d_rpl->exts};
Radek Krejcif13b87b2020-12-01 22:02:17 +01002712 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002713 {LY_STMT_CONFIG, &d_rpl->flags, YIN_SUBELEM_UNIQUE},
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002714 {LY_STMT_DEFAULT, &d_rpl->dflt, YIN_SUBELEM_UNIQUE},
Radek Krejci0f969882020-08-21 16:56:47 +02002715 {LY_STMT_MANDATORY, &d_rpl->flags, YIN_SUBELEM_UNIQUE},
2716 {LY_STMT_MAX_ELEMENTS, &max, YIN_SUBELEM_UNIQUE},
2717 {LY_STMT_MIN_ELEMENTS, &min, YIN_SUBELEM_UNIQUE},
2718 {LY_STMT_TYPE, &d_rpl->type, YIN_SUBELEM_UNIQUE},
2719 {LY_STMT_UNITS, &d_rpl->units, YIN_SUBELEM_UNIQUE},
2720 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
2721 };
Radek Krejcif13b87b2020-12-01 22:02:17 +01002722 ret = yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_DEVIATE, NULL, &d_rpl->exts);
David Sedlák4ffcec82019-07-25 15:10:21 +02002723
2724 } else {
2725 d_del = calloc(1, sizeof *d_del);
Michal Vaskob36053d2020-03-26 15:49:30 +01002726 LY_CHECK_ERR_RET(!d_del, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
David Sedlák4ffcec82019-07-25 15:10:21 +02002727 d = (struct lysp_deviate *)d_del;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002728 struct yin_subelement subelems[] = {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002729 {LY_STMT_DEFAULT, &d_del->dflts, 0},
Radek Krejci0f969882020-08-21 16:56:47 +02002730 {LY_STMT_MUST, &d_del->musts, 0},
2731 {LY_STMT_UNIQUE, &d_del->uniques, 0},
2732 {LY_STMT_UNITS, &d_del->units, YIN_SUBELEM_UNIQUE},
2733 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
2734 };
Radek Krejcif13b87b2020-12-01 22:02:17 +01002735 ret = yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_DEVIATE, NULL, &d_del->exts);
David Sedlák4ffcec82019-07-25 15:10:21 +02002736 }
2737 LY_CHECK_GOTO(ret, cleanup);
2738
2739 d->mod = dev_mod;
2740 /* insert into siblings */
David Sedlák8d552d62019-08-06 15:29:05 +02002741 LY_LIST_INSERT(deviates, d, next);
David Sedlák4ffcec82019-07-25 15:10:21 +02002742
2743 return ret;
2744
2745cleanup:
2746 free(d);
David Sedlák4ffcec82019-07-25 15:10:21 +02002747 return ret;
2748}
2749
David Sedlák992fb7c2019-07-24 16:51:01 +02002750/**
David Sedlák8b754462019-07-25 16:22:13 +02002751 * @brief Parse deviation element.
2752 *
2753 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák8b754462019-07-25 16:22:13 +02002754 * @param[in] deviations Deviations to add to.
2755 *
2756 * @return LY_ERR values.
2757 */
2758static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002759yin_parse_deviation(struct lys_yin_parser_ctx *ctx, struct lysp_deviation **deviations)
David Sedlák8b754462019-07-25 16:22:13 +02002760{
2761 struct lysp_deviation *dev;
2762
2763 /* create new deviation */
Michal Vaskob36053d2020-03-26 15:49:30 +01002764 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *deviations, dev, LY_EMEM);
David Sedlák8b754462019-07-25 16:22:13 +02002765
2766 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002767 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2768 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_TARGET_NODE, &dev->nodeid, Y_STR_ARG, LY_STMT_DEVIATION));
2769 CHECK_NONEMPTY((struct lys_parser_ctx *)ctx, strlen(dev->nodeid), "deviation");
Radek Krejcif13b87b2020-12-01 22:02:17 +01002770 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002771 {LY_STMT_DESCRIPTION, &dev->dsc, YIN_SUBELEM_UNIQUE},
2772 {LY_STMT_DEVIATE, &dev->deviates, YIN_SUBELEM_MANDATORY},
2773 {LY_STMT_REFERENCE, &dev->ref, YIN_SUBELEM_UNIQUE},
2774 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
2775 };
Michal Vasko69730152020-10-09 16:30:07 +02002776
Radek Krejcif13b87b2020-12-01 22:02:17 +01002777 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_DEVIATION, NULL, &dev->exts);
David Sedlák8b754462019-07-25 16:22:13 +02002778}
2779
2780/**
David Sedlákb4e44562019-07-04 15:42:12 +02002781 * @brief Map keyword type to substatement info.
2782 *
2783 * @param[in] kw Keyword type.
2784 *
2785 * @return correct LYEXT_SUBSTMT information.
2786 */
2787static LYEXT_SUBSTMT
Radek Krejcid6b76452019-09-03 17:03:03 +02002788kw2lyext_substmt(enum ly_stmt kw)
David Sedlákb4e44562019-07-04 15:42:12 +02002789{
2790 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002791 case LY_STMT_ARGUMENT:
David Sedlákb4e44562019-07-04 15:42:12 +02002792 return LYEXT_SUBSTMT_ARGUMENT;
Radek Krejcid6b76452019-09-03 17:03:03 +02002793 case LY_STMT_BASE:
David Sedlákb4e44562019-07-04 15:42:12 +02002794 return LYEXT_SUBSTMT_BASE;
Radek Krejcid6b76452019-09-03 17:03:03 +02002795 case LY_STMT_BELONGS_TO:
David Sedlákb4e44562019-07-04 15:42:12 +02002796 return LYEXT_SUBSTMT_BELONGSTO;
Radek Krejcid6b76452019-09-03 17:03:03 +02002797 case LY_STMT_CONTACT:
David Sedlákb4e44562019-07-04 15:42:12 +02002798 return LYEXT_SUBSTMT_CONTACT;
Radek Krejcid6b76452019-09-03 17:03:03 +02002799 case LY_STMT_DEFAULT:
David Sedlákb4e44562019-07-04 15:42:12 +02002800 return LYEXT_SUBSTMT_DEFAULT;
Radek Krejcid6b76452019-09-03 17:03:03 +02002801 case LY_STMT_DESCRIPTION:
David Sedlákb4e44562019-07-04 15:42:12 +02002802 return LYEXT_SUBSTMT_DESCRIPTION;
Radek Krejcid6b76452019-09-03 17:03:03 +02002803 case LY_STMT_ERROR_APP_TAG:
David Sedlákb4e44562019-07-04 15:42:12 +02002804 return LYEXT_SUBSTMT_ERRTAG;
Radek Krejcid6b76452019-09-03 17:03:03 +02002805 case LY_STMT_ERROR_MESSAGE:
David Sedlákb4e44562019-07-04 15:42:12 +02002806 return LYEXT_SUBSTMT_ERRMSG;
Radek Krejcid6b76452019-09-03 17:03:03 +02002807 case LY_STMT_KEY:
David Sedlákb4e44562019-07-04 15:42:12 +02002808 return LYEXT_SUBSTMT_KEY;
Radek Krejcid6b76452019-09-03 17:03:03 +02002809 case LY_STMT_NAMESPACE:
David Sedlákb4e44562019-07-04 15:42:12 +02002810 return LYEXT_SUBSTMT_NAMESPACE;
Radek Krejcid6b76452019-09-03 17:03:03 +02002811 case LY_STMT_ORGANIZATION:
David Sedlákb4e44562019-07-04 15:42:12 +02002812 return LYEXT_SUBSTMT_ORGANIZATION;
Radek Krejcid6b76452019-09-03 17:03:03 +02002813 case LY_STMT_PATH:
David Sedlákb4e44562019-07-04 15:42:12 +02002814 return LYEXT_SUBSTMT_PATH;
Radek Krejcid6b76452019-09-03 17:03:03 +02002815 case LY_STMT_PREFIX:
David Sedlákb4e44562019-07-04 15:42:12 +02002816 return LYEXT_SUBSTMT_PREFIX;
Radek Krejcid6b76452019-09-03 17:03:03 +02002817 case LY_STMT_PRESENCE:
David Sedlákb4e44562019-07-04 15:42:12 +02002818 return LYEXT_SUBSTMT_PRESENCE;
Radek Krejcid6b76452019-09-03 17:03:03 +02002819 case LY_STMT_REFERENCE:
David Sedlákb4e44562019-07-04 15:42:12 +02002820 return LYEXT_SUBSTMT_REFERENCE;
Radek Krejcid6b76452019-09-03 17:03:03 +02002821 case LY_STMT_REVISION_DATE:
David Sedlákb4e44562019-07-04 15:42:12 +02002822 return LYEXT_SUBSTMT_REVISIONDATE;
Radek Krejcid6b76452019-09-03 17:03:03 +02002823 case LY_STMT_UNITS:
David Sedlákb4e44562019-07-04 15:42:12 +02002824 return LYEXT_SUBSTMT_UNITS;
Radek Krejcid6b76452019-09-03 17:03:03 +02002825 case LY_STMT_VALUE:
David Sedlákb4e44562019-07-04 15:42:12 +02002826 return LYEXT_SUBSTMT_VALUE;
Radek Krejcid6b76452019-09-03 17:03:03 +02002827 case LY_STMT_YANG_VERSION:
David Sedlákb4e44562019-07-04 15:42:12 +02002828 return LYEXT_SUBSTMT_VERSION;
Radek Krejcid6b76452019-09-03 17:03:03 +02002829 case LY_STMT_MODIFIER:
David Sedlákb4e44562019-07-04 15:42:12 +02002830 return LYEXT_SUBSTMT_MODIFIER;
Radek Krejcid6b76452019-09-03 17:03:03 +02002831 case LY_STMT_REQUIRE_INSTANCE:
David Sedlákb4e44562019-07-04 15:42:12 +02002832 return LYEXT_SUBSTMT_REQINSTANCE;
Radek Krejcid6b76452019-09-03 17:03:03 +02002833 case LY_STMT_YIN_ELEMENT:
David Sedlákb4e44562019-07-04 15:42:12 +02002834 return LYEXT_SUBSTMT_YINELEM;
Radek Krejcid6b76452019-09-03 17:03:03 +02002835 case LY_STMT_CONFIG:
David Sedlákb4e44562019-07-04 15:42:12 +02002836 return LYEXT_SUBSTMT_CONFIG;
Radek Krejcid6b76452019-09-03 17:03:03 +02002837 case LY_STMT_MANDATORY:
David Sedlákb4e44562019-07-04 15:42:12 +02002838 return LYEXT_SUBSTMT_MANDATORY;
Radek Krejcid6b76452019-09-03 17:03:03 +02002839 case LY_STMT_ORDERED_BY:
David Sedlákb4e44562019-07-04 15:42:12 +02002840 return LYEXT_SUBSTMT_ORDEREDBY;
Radek Krejcid6b76452019-09-03 17:03:03 +02002841 case LY_STMT_STATUS:
David Sedlákb4e44562019-07-04 15:42:12 +02002842 return LYEXT_SUBSTMT_STATUS;
Radek Krejcid6b76452019-09-03 17:03:03 +02002843 case LY_STMT_FRACTION_DIGITS:
David Sedlákb4e44562019-07-04 15:42:12 +02002844 return LYEXT_SUBSTMT_FRACDIGITS;
Radek Krejcid6b76452019-09-03 17:03:03 +02002845 case LY_STMT_MAX_ELEMENTS:
David Sedlákb4e44562019-07-04 15:42:12 +02002846 return LYEXT_SUBSTMT_MAX;
Radek Krejcid6b76452019-09-03 17:03:03 +02002847 case LY_STMT_MIN_ELEMENTS:
David Sedlákb4e44562019-07-04 15:42:12 +02002848 return LYEXT_SUBSTMT_MIN;
Radek Krejcid6b76452019-09-03 17:03:03 +02002849 case LY_STMT_POSITION:
David Sedlákb4e44562019-07-04 15:42:12 +02002850 return LYEXT_SUBSTMT_POSITION;
Radek Krejcid6b76452019-09-03 17:03:03 +02002851 case LY_STMT_UNIQUE:
David Sedlákb4e44562019-07-04 15:42:12 +02002852 return LYEXT_SUBSTMT_UNIQUE;
Radek Krejcid6b76452019-09-03 17:03:03 +02002853 case LY_STMT_IF_FEATURE:
David Sedlákb4e44562019-07-04 15:42:12 +02002854 return LYEXT_SUBSTMT_IFFEATURE;
2855 default:
2856 return LYEXT_SUBSTMT_SELF;
2857 }
2858}
2859
David Sedlákc5b20842019-08-13 10:18:31 +02002860/**
2861 * @brief map keyword to keyword-group.
2862 *
2863 * @param[in] ctx YIN parser context used for logging.
2864 * @param[in] kw Keyword that is child of module or submodule.
2865 * @param[out] group Group of keyword.
2866 *
2867 * @return LY_SUCCESS on success LY_EINT if kw can't be mapped to kw_group, should not happen if called correctly.
2868 */
David Sedláke6cd89e2019-08-07 12:46:02 +02002869static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002870kw2kw_group(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, enum yang_module_stmt *group)
David Sedláke6cd89e2019-08-07 12:46:02 +02002871{
2872 switch (kw) {
Radek Krejci0f969882020-08-21 16:56:47 +02002873 /* module header */
2874 case LY_STMT_NONE:
2875 case LY_STMT_NAMESPACE:
2876 case LY_STMT_PREFIX:
2877 case LY_STMT_BELONGS_TO:
2878 case LY_STMT_YANG_VERSION:
2879 *group = Y_MOD_MODULE_HEADER;
2880 break;
2881 /* linkage */
2882 case LY_STMT_INCLUDE:
2883 case LY_STMT_IMPORT:
2884 *group = Y_MOD_LINKAGE;
2885 break;
2886 /* meta */
2887 case LY_STMT_ORGANIZATION:
2888 case LY_STMT_CONTACT:
2889 case LY_STMT_DESCRIPTION:
2890 case LY_STMT_REFERENCE:
2891 *group = Y_MOD_META;
2892 break;
2893 /* revision */
2894 case LY_STMT_REVISION:
2895 *group = Y_MOD_REVISION;
2896 break;
2897 /* body */
2898 case LY_STMT_ANYDATA:
2899 case LY_STMT_ANYXML:
2900 case LY_STMT_AUGMENT:
2901 case LY_STMT_CHOICE:
2902 case LY_STMT_CONTAINER:
2903 case LY_STMT_DEVIATION:
2904 case LY_STMT_EXTENSION:
2905 case LY_STMT_FEATURE:
2906 case LY_STMT_GROUPING:
2907 case LY_STMT_IDENTITY:
2908 case LY_STMT_LEAF:
2909 case LY_STMT_LEAF_LIST:
2910 case LY_STMT_LIST:
2911 case LY_STMT_NOTIFICATION:
2912 case LY_STMT_RPC:
2913 case LY_STMT_TYPEDEF:
2914 case LY_STMT_USES:
2915 case LY_STMT_EXTENSION_INSTANCE:
2916 *group = Y_MOD_BODY;
2917 break;
2918 default:
2919 LOGINT(ctx->xmlctx->ctx);
2920 return LY_EINT;
David Sedláke6cd89e2019-08-07 12:46:02 +02002921 }
2922
2923 return LY_SUCCESS;
2924}
2925
David Sedlákc5b20842019-08-13 10:18:31 +02002926/**
2927 * @brief Check if relative order of two keywords is valid.
2928 *
2929 * @param[in] ctx YIN parser context used for logging.
2930 * @param[in] kw Current keyword.
2931 * @param[in] next_kw Next keyword.
Radek Krejcid6b76452019-09-03 17:03:03 +02002932 * @param[in] parrent Identification of parrent element, can be se to to LY_STMT_MODULE of LY_STMT_SUBMODULE,
David Sedlákc5b20842019-08-13 10:18:31 +02002933 * because relative order is required only in module and submodule sub-elements, used for logging.
2934 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02002935 * @return LY_SUCCESS on success and LY_EVALID if relative order is invalid.
David Sedlákc5b20842019-08-13 10:18:31 +02002936 */
David Sedláke6cd89e2019-08-07 12:46:02 +02002937static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002938yin_check_relative_order(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, enum ly_stmt next_kw, enum ly_stmt parrent)
David Sedláke6cd89e2019-08-07 12:46:02 +02002939{
Radek Krejcid6b76452019-09-03 17:03:03 +02002940 assert(parrent == LY_STMT_MODULE || parrent == LY_STMT_SUBMODULE);
David Sedláke6cd89e2019-08-07 12:46:02 +02002941 enum yang_module_stmt gr, next_gr;
2942
2943 LY_CHECK_RET(kw2kw_group(ctx, kw, &gr));
2944 LY_CHECK_RET(kw2kw_group(ctx, next_kw, &next_gr));
2945
2946 if (gr > next_gr) {
2947 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INORDER_YIN, ly_stmt2str(parrent), ly_stmt2str(next_kw), ly_stmt2str(kw));
2948 return LY_EVALID;
2949 }
2950
2951 return LY_SUCCESS;
2952}
2953
David Sedlákc5dd0942019-09-13 10:23:59 +02002954/**
2955 * @brief Get element name prefixed by full URI of xml namespace.
2956 *
2957 * @param[in] ctx YIN parser context used for logging and to get inormation about xml namespaces.
2958 * @param[in] name Name of element.
2959 * @param[in] name_len Length of element name.
2960 * @param[in] prefix Prefix of element.
2961 * @param[in] prefix_len Length of element prefix.
2962 *
2963 * @return Element name prefixed by URI on success, NULL on failure.
2964 */
2965static const char *
Michal Vaskob36053d2020-03-26 15:49:30 +01002966name2nsname(struct lys_yin_parser_ctx *ctx, const char *name, size_t name_len, const char *prefix, size_t prefix_len)
David Sedlák068dd352019-09-13 09:08:22 +02002967{
Radek Krejci011e4aa2020-09-04 15:22:31 +02002968 const char *ename = NULL;
Michal Vaskoc8a230d2020-08-14 12:17:10 +02002969 const struct lyxml_ns *ns = lyxml_ns_get(&ctx->xmlctx->ns, prefix, prefix_len);
Radek Krejci011e4aa2020-09-04 15:22:31 +02002970
Michal Vaskob36053d2020-03-26 15:49:30 +01002971 LY_CHECK_ERR_RET(!ns, LOGINT(ctx->xmlctx->ctx), NULL);
David Sedlák068dd352019-09-13 09:08:22 +02002972
Radek Krejci02728562019-09-18 14:13:05 +02002973 if (!strcmp(ns->uri, YIN_NS_URI)) {
2974 /* standard YANG statement in YIN namespace - keep it unprefixed as in case of YANG */
Radek Krejci011e4aa2020-09-04 15:22:31 +02002975 lydict_insert(ctx->xmlctx->ctx, name, name_len, &ename);
2976 return ename;
Radek Krejci02728562019-09-18 14:13:05 +02002977 }
2978 /* some statement in special namespace (extension instance) */
David Sedláke0ef1c62019-09-13 10:05:55 +02002979 size_t ns_len = strlen(ns->uri);
2980 size_t len = ns_len + name_len + 1; /* +1 because of ':' delimiter between ns and actual name */
2981
2982 char *result;
2983 char *temp;
Michal Vasko69730152020-10-09 16:30:07 +02002984
David Sedláke0ef1c62019-09-13 10:05:55 +02002985 temp = result = malloc(sizeof(*temp) * (len + 1)); /* +1 for '\0' terminator */
Michal Vaskob36053d2020-03-26 15:49:30 +01002986 LY_CHECK_ERR_RET(!temp, LOGMEM(ctx->xmlctx->ctx), NULL);
David Sedláke0ef1c62019-09-13 10:05:55 +02002987
2988 strcpy(result, ns->uri);
2989 result[ns_len] = ':';
2990 temp = &result[ns_len + 1];
2991 strncpy(temp, name, name_len);
2992 result[len] = '\0';
2993
Radek Krejci011e4aa2020-09-04 15:22:31 +02002994 lydict_insert_zc(ctx->xmlctx->ctx, result, &ename);
2995 return ename;
David Sedlák068dd352019-09-13 09:08:22 +02002996}
2997
Michal Vaskoafac7822020-10-20 14:22:26 +02002998/**
2999 * @brief Parse argument of extension subelement that is classic yang keyword and not another instance of extension.
3000 *
3001 * @param[in,out] ctx Yin parser context for logging and to store current state.
3002 * @param[in] elem_type Type of element that is currently being parsed.
3003 * @param[out] arg Value to write to.
3004 *
3005 * @return LY_ERR values.
3006 */
3007static LY_ERR
3008yin_parse_extension_instance_arg(struct lys_yin_parser_ctx *ctx, enum ly_stmt elem_type, const char **arg)
3009{
3010 enum ly_stmt child;
3011
3012 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3013
3014 switch (elem_type) {
3015 case LY_STMT_ACTION:
3016 case LY_STMT_ANYDATA:
3017 case LY_STMT_ANYXML:
3018 case LY_STMT_ARGUMENT:
3019 case LY_STMT_BASE:
3020 case LY_STMT_BIT:
3021 case LY_STMT_CASE:
3022 case LY_STMT_CHOICE:
3023 case LY_STMT_CONTAINER:
3024 case LY_STMT_ENUM:
3025 case LY_STMT_EXTENSION:
3026 case LY_STMT_FEATURE:
3027 case LY_STMT_GROUPING:
3028 case LY_STMT_IDENTITY:
3029 case LY_STMT_IF_FEATURE:
3030 case LY_STMT_LEAF:
3031 case LY_STMT_LEAF_LIST:
3032 case LY_STMT_LIST:
3033 case LY_STMT_MODULE:
3034 case LY_STMT_NOTIFICATION:
3035 case LY_STMT_RPC:
3036 case LY_STMT_SUBMODULE:
3037 case LY_STMT_TYPE:
3038 case LY_STMT_TYPEDEF:
3039 case LY_STMT_UNITS:
3040 case LY_STMT_USES:
3041 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, arg, Y_MAYBE_STR_ARG, elem_type));
3042 break;
3043 case LY_STMT_AUGMENT:
3044 case LY_STMT_DEVIATION:
3045 case LY_STMT_REFINE:
3046 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_TARGET_NODE, arg, Y_MAYBE_STR_ARG, elem_type));
3047 break;
3048 case LY_STMT_CONFIG:
3049 case LY_STMT_DEFAULT:
3050 case LY_STMT_DEVIATE:
3051 case LY_STMT_ERROR_APP_TAG:
3052 case LY_STMT_FRACTION_DIGITS:
3053 case LY_STMT_KEY:
3054 case LY_STMT_LENGTH:
3055 case LY_STMT_MANDATORY:
3056 case LY_STMT_MAX_ELEMENTS:
3057 case LY_STMT_MIN_ELEMENTS:
3058 case LY_STMT_MODIFIER:
3059 case LY_STMT_ORDERED_BY:
3060 case LY_STMT_PATH:
3061 case LY_STMT_PATTERN:
3062 case LY_STMT_POSITION:
3063 case LY_STMT_PREFIX:
3064 case LY_STMT_PRESENCE:
3065 case LY_STMT_RANGE:
3066 case LY_STMT_REQUIRE_INSTANCE:
3067 case LY_STMT_STATUS:
3068 case LY_STMT_VALUE:
3069 case LY_STMT_YANG_VERSION:
3070 case LY_STMT_YIN_ELEMENT:
3071 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, arg, Y_MAYBE_STR_ARG, elem_type));
3072 break;
3073 case LY_STMT_IMPORT:
3074 case LY_STMT_INCLUDE:
3075 case LY_STMT_BELONGS_TO:
3076 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_MODULE, arg, Y_MAYBE_STR_ARG, elem_type));
3077 break;
3078 case LY_STMT_INPUT:
3079 case LY_STMT_OUTPUT:
3080 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NONE, arg, Y_MAYBE_STR_ARG, elem_type));
3081 break;
3082 case LY_STMT_MUST:
3083 case LY_STMT_WHEN:
3084 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_CONDITION, arg, Y_MAYBE_STR_ARG, elem_type));
3085 break;
3086 case LY_STMT_NAMESPACE:
3087 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_URI, arg, Y_MAYBE_STR_ARG, elem_type));
3088 break;
3089 case LY_STMT_REVISION:
3090 case LY_STMT_REVISION_DATE:
3091 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_DATE, arg, Y_MAYBE_STR_ARG, elem_type));
3092 break;
3093 case LY_STMT_UNIQUE:
3094 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_TAG, arg, Y_MAYBE_STR_ARG, elem_type));
3095 break;
3096 /* argument is mapped to yin element */
3097 case LY_STMT_CONTACT:
3098 case LY_STMT_DESCRIPTION:
3099 case LY_STMT_ORGANIZATION:
3100 case LY_STMT_REFERENCE:
3101 case LY_STMT_ERROR_MESSAGE:
3102 /* there shouldn't be any attribute, argument is supposed to be first subelement */
3103 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NONE, arg, Y_MAYBE_STR_ARG, elem_type));
3104
3105 /* no content */
3106 assert(ctx->xmlctx->status == LYXML_ELEM_CONTENT);
3107 if (ctx->xmlctx->ws_only) {
3108 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3109 }
3110 if (((ctx->xmlctx->status == LYXML_ELEM_CONTENT) && !ctx->xmlctx->ws_only) || (ctx->xmlctx->status != LYXML_ELEMENT)) {
3111 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_FIRT_SUBELEM,
3112 elem_type == LY_STMT_ERROR_MESSAGE ? "value" : "text", ly_stmt2str(elem_type));
3113 return LY_EVALID;
3114 }
3115
3116 /* parse child element */
3117 child = yin_match_keyword(ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, ctx->xmlctx->prefix, ctx->xmlctx->prefix_len, elem_type);
3118 if (((elem_type == LY_STMT_ERROR_MESSAGE) && (child != LY_STMT_ARG_VALUE)) ||
3119 ((elem_type != LY_STMT_ERROR_MESSAGE) && (child != LY_STMT_ARG_TEXT))) {
3120 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_UNEXP_SUBELEM, ctx->xmlctx->name_len, ctx->xmlctx->name,
3121 ly_stmt2str(elem_type));
3122 return LY_EVALID;
3123 }
3124 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3125
3126 /* no attributes expected? TODO */
3127 while (ctx->xmlctx->status == LYXML_ATTRIBUTE) {
3128 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3129 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3130 }
3131
3132 /* load and save content */
3133 INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic, *arg);
3134 LY_CHECK_RET(!*arg, LY_EMEM);
3135
3136 /* load closing tag of subelement */
3137 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3138
3139 /* if only subelement was parsed as argument, load also closing tag TODO what? */
3140 /*if (ctx->xmlctx->status == LYXML_ELEMENT) {
3141 LY_CHECK_RET(lyxml_get_element(&ctx->xmlctx, data, &prefix, &prefix_len, &name, &name_len));
3142 }*/
3143 break;
3144 default:
3145 LOGINT(ctx->xmlctx->ctx);
3146 return LY_EINT;
3147 }
3148
3149 return LY_SUCCESS;
3150}
3151
3152/**
3153 * @brief Parse yin element into generic structure.
3154 *
3155 * @param[in,out] ctx Yin parser context for XML context, logging, and to store current state.
3156 * @param[in] parent Identification of parent element.
3157 * @param[out] element Where the element structure should be stored.
3158 *
3159 * @return LY_ERR values.
3160 */
3161LY_ERR
3162yin_parse_element_generic(struct lys_yin_parser_ctx *ctx, enum ly_stmt parent, struct lysp_stmt **element)
3163{
3164 LY_ERR ret = LY_SUCCESS;
3165 struct lysp_stmt *last = NULL, *new = NULL;
3166
3167 assert(ctx->xmlctx->status == LYXML_ELEMENT);
3168
3169 /* allocate new structure for element */
3170 *element = calloc(1, sizeof(**element));
3171 LY_CHECK_ERR_GOTO(!(*element), LOGMEM(ctx->xmlctx->ctx); ret = LY_EMEM, cleanup);
3172 (*element)->stmt = name2nsname(ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, ctx->xmlctx->prefix, ctx->xmlctx->prefix_len);
3173 LY_CHECK_ERR_GOTO(!(*element)->stmt, ret = LY_EMEM, cleanup);
3174
3175 (*element)->kw = yin_match_keyword(ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, ctx->xmlctx->prefix,
3176 ctx->xmlctx->prefix_len, parent);
3177
3178 last = (*element)->child;
3179 if ((*element)->kw == LY_STMT_NONE) {
3180 /* unrecognized element */
3181 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_UNEXP_SUBELEM, ctx->xmlctx->name_len, ctx->xmlctx->name,
3182 ly_stmt2str(parent));
3183 ret = LY_EVALID;
3184 goto cleanup;
3185 } else if ((*element)->kw != LY_STMT_EXTENSION_INSTANCE) {
3186 /* element is known yang keyword, which means argument can be parsed correctly. */
3187 ret = yin_parse_extension_instance_arg(ctx, (*element)->kw, &(*element)->arg);
3188 LY_CHECK_GOTO(ret, cleanup);
3189 } else {
3190 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3191
3192 /* load attributes in generic way, save all attributes in linked list */
3193 while (ctx->xmlctx->status == LYXML_ATTRIBUTE) {
3194 new = calloc(1, sizeof(*last));
3195 LY_CHECK_ERR_GOTO(!new, LOGMEM(ctx->xmlctx->ctx); ret = LY_EMEM, cleanup);
3196 if (!(*element)->child) {
3197 /* save first */
3198 (*element)->child = new;
3199 } else {
3200 last->next = new;
3201 }
3202 last = new;
3203
3204 last->flags |= LYS_YIN_ATTR;
3205 LY_CHECK_GOTO(ret = lydict_insert(ctx->xmlctx->ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, &last->stmt), cleanup);
3206 last->kw = LY_STMT_NONE;
3207 /* attributes with prefix are ignored */
3208 if (!ctx->xmlctx->prefix) {
3209 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3210
3211 INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic, last->arg);
3212 LY_CHECK_ERR_GOTO(!last->arg, ret = LY_EMEM, cleanup);
3213 } else {
3214 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3215 }
3216 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3217 }
3218 }
3219
3220 if ((ctx->xmlctx->status != LYXML_ELEM_CONTENT) || ctx->xmlctx->ws_only) {
3221 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3222 while (ctx->xmlctx->status == LYXML_ELEMENT) {
3223 /* parse subelements */
3224 ret = yin_parse_element_generic(ctx, (*element)->kw, &new);
3225 LY_CHECK_GOTO(ret, cleanup);
3226 if (!(*element)->child) {
3227 /* save first */
3228 (*element)->child = new;
3229 } else {
3230 last->next = new;
3231 }
3232 last = new;
3233
3234 assert(ctx->xmlctx->status == LYXML_ELEM_CLOSE);
3235 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3236 }
3237 } else {
3238 /* save element content */
3239 if (ctx->xmlctx->value_len) {
3240 INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic, (*element)->arg);
3241 LY_CHECK_ERR_GOTO(!(*element)->arg, ret = LY_EMEM, cleanup);
3242 }
3243
3244 /* read closing tag */
3245 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3246 }
3247
3248cleanup:
3249 return ret;
3250}
3251
3252/**
3253 * @brief Parse instance of extension.
3254 *
3255 * @param[in,out] ctx Yin parser context for logging and to store current state.
3256 * @param[in] subelem Type of the keyword this extension instance is a subelement of.
3257 * @param[in] subelem_index Index of the keyword instance this extension instance is a subelement of
3258 * @param[in,out] exts Extension instance to add to.
3259 *
3260 * @return LY_ERR values.
3261 */
3262LY_ERR
3263yin_parse_extension_instance(struct lys_yin_parser_ctx *ctx, LYEXT_SUBSTMT subelem, LY_ARRAY_COUNT_TYPE subelem_index,
3264 struct lysp_ext_instance **exts)
3265{
3266 struct lysp_ext_instance *e;
3267 struct lysp_stmt *last_subelem = NULL, *new_subelem = NULL;
3268
3269 assert(ctx->xmlctx->status == LYXML_ELEMENT);
Radek Krejci3aac9a72020-12-01 12:24:26 +01003270 assert(exts);
Michal Vaskoafac7822020-10-20 14:22:26 +02003271
3272 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *exts, e, LY_EMEM);
3273
3274 e->yin = 0;
3275 /* store name and insubstmt info */
3276 e->name = name2nsname(ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, ctx->xmlctx->prefix, ctx->xmlctx->prefix_len);
3277 LY_CHECK_RET(!e->name, LY_EMEM);
3278 e->insubstmt = subelem;
3279 e->insubstmt_index = subelem_index;
3280 e->yin |= LYS_YIN;
3281 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3282
3283 /* store attributes as subelements */
3284 while (ctx->xmlctx->status == LYXML_ATTRIBUTE) {
3285 if (!ctx->xmlctx->prefix) {
3286 new_subelem = calloc(1, sizeof(*new_subelem));
3287 if (!e->child) {
3288 e->child = new_subelem;
3289 } else {
3290 last_subelem->next = new_subelem;
3291 }
3292 last_subelem = new_subelem;
3293
3294 last_subelem->flags |= LYS_YIN_ATTR;
3295 LY_CHECK_RET(lydict_insert(ctx->xmlctx->ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, &last_subelem->stmt));
3296 LY_CHECK_RET(!last_subelem->stmt, LY_EMEM);
3297 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3298
3299 INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic, last_subelem->arg);
3300 LY_CHECK_RET(!last_subelem->arg, LY_EMEM);
3301 } else {
3302 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3303 }
3304
3305 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3306 }
3307
3308 /* parse subelements */
3309 assert(ctx->xmlctx->status == LYXML_ELEM_CONTENT);
3310 if (ctx->xmlctx->ws_only) {
3311 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3312 while (ctx->xmlctx->status == LYXML_ELEMENT) {
3313 LY_CHECK_RET(yin_parse_element_generic(ctx, LY_STMT_EXTENSION_INSTANCE, &new_subelem));
3314 if (!e->child) {
3315 e->child = new_subelem;
3316 } else {
3317 last_subelem->next = new_subelem;
3318 }
3319 last_subelem = new_subelem;
3320
3321 assert(ctx->xmlctx->status == LYXML_ELEM_CLOSE);
3322 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3323 }
3324 } else if (ctx->xmlctx->value_len) {
3325 /* save text content */
3326 INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic, e->argument);
3327 LY_CHECK_RET(!e->argument, LY_EMEM);
3328
3329 /* parser next */
3330 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3331 }
3332
3333 return LY_SUCCESS;
3334}
3335
3336/**
3337 * @brief Generic function for content parsing
3338 *
3339 * @param[in,out] ctx Yin parser context for logging and to store current state.
3340 * @param[in] subelem_info array of valid subelement types and meta information
3341 * @param[in] subelem_info_size Size of subelem_info array.
3342 * @param[in] current_element Type of current element.
3343 * @param[out] text_content Where the text content of element should be stored if any. Text content is ignored if set to NULL.
3344 * @param[in,out] exts Extension instance to add to. Can be set to null if element cannot have extension as subelements.
3345 *
3346 * @return LY_ERR values.
3347 */
David Sedlákd6e56892019-07-01 15:40:24 +02003348LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01003349yin_parse_content(struct lys_yin_parser_ctx *ctx, struct yin_subelement *subelem_info, size_t subelem_info_size,
Radek Krejci0f969882020-08-21 16:56:47 +02003350 enum ly_stmt current_element, const char **text_content, struct lysp_ext_instance **exts)
David Sedlákd6e56892019-07-01 15:40:24 +02003351{
3352 LY_ERR ret = LY_SUCCESS;
Michal Vaskob36053d2020-03-26 15:49:30 +01003353 enum LYXML_PARSER_STATUS next_status;
Radek Krejcid6b76452019-09-03 17:03:03 +02003354 enum ly_stmt kw = LY_STMT_NONE, last_kw = LY_STMT_NONE;
David Sedlák1af868e2019-07-17 17:03:14 +02003355 struct yin_subelement *subelem = NULL;
David Sedlák09e18c92019-07-18 11:17:11 +02003356
Michal Vaskob36053d2020-03-26 15:49:30 +01003357 assert(ctx->xmlctx->status == LYXML_ELEM_CONTENT);
David Sedlákd6e56892019-07-01 15:40:24 +02003358
Michal Vaskob36053d2020-03-26 15:49:30 +01003359 if (ctx->xmlctx->ws_only) {
3360 /* check whether there are any children */
3361 LY_CHECK_GOTO(ret = lyxml_ctx_peek(ctx->xmlctx, &next_status), cleanup);
3362 } else {
3363 /* we want to parse the value */
3364 next_status = LYXML_ELEM_CLOSE;
3365 }
3366
3367 if (next_status == LYXML_ELEMENT) {
3368 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3369
3370 /* current element has subelements as content */
3371 while (ctx->xmlctx->status == LYXML_ELEMENT) {
3372 /* match keyword */
3373 last_kw = kw;
3374 kw = yin_match_keyword(ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, ctx->xmlctx->prefix,
Michal Vasko69730152020-10-09 16:30:07 +02003375 ctx->xmlctx->prefix_len, current_element);
Michal Vaskob36053d2020-03-26 15:49:30 +01003376
3377 /* check if this element can be child of current element */
3378 subelem = get_record(kw, subelem_info_size, subelem_info);
3379 if (!subelem) {
Michal Vasko69730152020-10-09 16:30:07 +02003380 if ((current_element == LY_STMT_DEVIATE) && isdevsub(kw)) {
Michal Vaskob36053d2020-03-26 15:49:30 +01003381 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INDEV_YIN, ly_stmt2str(kw));
3382 } else {
3383 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_UNEXP_SUBELEM, ctx->xmlctx->name_len,
Michal Vasko69730152020-10-09 16:30:07 +02003384 ctx->xmlctx->name, ly_stmt2str(current_element));
Michal Vaskob36053d2020-03-26 15:49:30 +01003385 }
3386 ret = LY_EVALID;
3387 goto cleanup;
3388 }
3389
3390 /* relative order is required only in module and submodule sub-elements */
Michal Vasko69730152020-10-09 16:30:07 +02003391 if ((current_element == LY_STMT_MODULE) || (current_element == LY_STMT_SUBMODULE)) {
Michal Vaskob36053d2020-03-26 15:49:30 +01003392 ret = yin_check_relative_order(ctx, last_kw, kw, current_element);
3393 LY_CHECK_GOTO(ret, cleanup);
3394 }
3395
3396 /* flag check */
3397 if ((subelem->flags & YIN_SUBELEM_UNIQUE) && (subelem->flags & YIN_SUBELEM_PARSED)) {
3398 /* subelement uniquenes */
3399 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_SUBELEM_REDEF, ly_stmt2str(kw), ly_stmt2str(current_element));
3400 return LY_EVALID;
3401 }
3402 if (subelem->flags & YIN_SUBELEM_FIRST) {
3403 /* subelement is supposed to be defined as first subelement */
3404 ret = yin_check_subelem_first_constraint(ctx, subelem_info, subelem_info_size, current_element, subelem);
3405 LY_CHECK_GOTO(ret, cleanup);
3406 }
3407 if (subelem->flags & YIN_SUBELEM_VER2) {
3408 /* subelement is supported only in version 1.1 or higher */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003409 if (ctx->parsed_mod->version < LYS_VERSION_1_1) {
Michal Vaskob36053d2020-03-26 15:49:30 +01003410 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INSUBELEM2, ly_stmt2str(kw), ly_stmt2str(current_element));
David Sedlákd6e56892019-07-01 15:40:24 +02003411 ret = LY_EVALID;
3412 goto cleanup;
3413 }
David Sedlákd6e56892019-07-01 15:40:24 +02003414 }
Michal Vaskob36053d2020-03-26 15:49:30 +01003415 /* note that element was parsed for easy uniqueness check in next iterations */
3416 subelem->flags |= YIN_SUBELEM_PARSED;
3417
3418 switch (kw) {
3419 /* call responsible function */
3420 case LY_STMT_EXTENSION_INSTANCE:
3421 ret = yin_parse_extension_instance(ctx, kw2lyext_substmt(current_element),
Michal Vasko69730152020-10-09 16:30:07 +02003422 (subelem->dest) ? *((LY_ARRAY_COUNT_TYPE *)subelem->dest) : 0, exts);
Michal Vaskob36053d2020-03-26 15:49:30 +01003423 break;
3424 case LY_STMT_ACTION:
3425 case LY_STMT_RPC:
3426 ret = yin_parse_action(ctx, (struct tree_node_meta *)subelem->dest);
3427 break;
3428 case LY_STMT_ANYDATA:
3429 case LY_STMT_ANYXML:
3430 ret = yin_parse_any(ctx, kw, (struct tree_node_meta *)subelem->dest);
3431 break;
3432 case LY_STMT_ARGUMENT:
3433 ret = yin_parse_argument(ctx, (struct yin_argument_meta *)subelem->dest, exts);
3434 break;
3435 case LY_STMT_AUGMENT:
3436 ret = yin_parse_augment(ctx, (struct tree_node_meta *)subelem->dest);
3437 break;
3438 case LY_STMT_BASE:
3439 ret = yin_parse_base(ctx, current_element, subelem->dest, exts);
3440 break;
3441 case LY_STMT_BELONGS_TO:
3442 ret = yin_parse_belongs_to(ctx, (struct lysp_submodule *)subelem->dest, exts);
3443 break;
3444 case LY_STMT_BIT:
3445 ret = yin_parse_bit(ctx, (struct lysp_type *)subelem->dest);
3446 break;
3447 case LY_STMT_CASE:
3448 ret = yin_parse_case(ctx, (struct tree_node_meta *)subelem->dest);
3449 break;
3450 case LY_STMT_CHOICE:
3451 ret = yin_parse_choice(ctx, (struct tree_node_meta *)subelem->dest);
3452 break;
3453 case LY_STMT_CONFIG:
3454 ret = yin_parse_config(ctx, (uint16_t *)subelem->dest, exts);
3455 break;
3456 case LY_STMT_CONTACT:
3457 case LY_STMT_DESCRIPTION:
3458 case LY_STMT_ORGANIZATION:
3459 case LY_STMT_REFERENCE:
3460 ret = yin_parse_meta(ctx, kw, (const char **)subelem->dest, exts);
3461 break;
3462 case LY_STMT_CONTAINER:
3463 ret = yin_parse_container(ctx, (struct tree_node_meta *)subelem->dest);
3464 break;
3465 case LY_STMT_DEFAULT:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003466 ret = yin_parse_qname(ctx, kw, subelem, exts);
3467 break;
Michal Vaskob36053d2020-03-26 15:49:30 +01003468 case LY_STMT_ERROR_APP_TAG:
3469 case LY_STMT_KEY:
3470 case LY_STMT_PRESENCE:
3471 ret = yin_parse_simple_elem(ctx, kw, subelem, YIN_ARG_VALUE, Y_STR_ARG, exts);
3472 break;
3473 case LY_STMT_DEVIATE:
3474 ret = yin_parse_deviate(ctx, (struct lysp_deviate **)subelem->dest);
3475 break;
3476 case LY_STMT_DEVIATION:
3477 ret = yin_parse_deviation(ctx, (struct lysp_deviation **)subelem->dest);
3478 break;
3479 case LY_STMT_ENUM:
3480 ret = yin_parse_enum(ctx, (struct lysp_type *)subelem->dest);
3481 break;
3482 case LY_STMT_ERROR_MESSAGE:
3483 ret = yin_parse_err_msg(ctx, (const char **)subelem->dest, exts);
3484 break;
3485 case LY_STMT_EXTENSION:
3486 ret = yin_parse_extension(ctx, (struct lysp_ext **)subelem->dest);
3487 break;
3488 case LY_STMT_FEATURE:
3489 ret = yin_parse_feature(ctx, (struct lysp_feature **)subelem->dest);
3490 break;
3491 case LY_STMT_FRACTION_DIGITS:
3492 ret = yin_parse_fracdigits(ctx, (struct lysp_type *)subelem->dest);
3493 break;
3494 case LY_STMT_GROUPING:
3495 ret = yin_parse_grouping(ctx, (struct tree_node_meta *)subelem->dest);
3496 break;
3497 case LY_STMT_IDENTITY:
3498 ret = yin_parse_identity(ctx, (struct lysp_ident **)subelem->dest);
3499 break;
3500 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003501 ret = yin_parse_qname(ctx, kw, subelem, exts);
3502 break;
Michal Vaskob36053d2020-03-26 15:49:30 +01003503 case LY_STMT_UNITS:
3504 ret = yin_parse_simple_elem(ctx, kw, subelem, YIN_ARG_NAME, Y_STR_ARG, exts);
3505 break;
3506 case LY_STMT_IMPORT:
3507 ret = yin_parse_import(ctx, (struct import_meta *)subelem->dest);
3508 break;
3509 case LY_STMT_INCLUDE:
3510 ret = yin_parse_include(ctx, (struct include_meta *)subelem->dest);
3511 break;
3512 case LY_STMT_INPUT:
3513 case LY_STMT_OUTPUT:
3514 ret = yin_parse_inout(ctx, kw, (struct inout_meta *)subelem->dest);
3515 break;
3516 case LY_STMT_LEAF:
3517 ret = yin_parse_leaf(ctx, (struct tree_node_meta *)subelem->dest);
3518 break;
3519 case LY_STMT_LEAF_LIST:
3520 ret = yin_parse_leaflist(ctx, (struct tree_node_meta *)subelem->dest);
3521 break;
3522 case LY_STMT_LENGTH:
3523 ret = yin_parse_length(ctx, (struct lysp_type *)subelem->dest);
3524 break;
3525 case LY_STMT_LIST:
3526 ret = yin_parse_list(ctx, (struct tree_node_meta *)subelem->dest);
3527 break;
3528 case LY_STMT_MANDATORY:
3529 ret = yin_parse_mandatory(ctx, (uint16_t *)subelem->dest, exts);
3530 break;
3531 case LY_STMT_MAX_ELEMENTS:
3532 case LY_STMT_MIN_ELEMENTS:
3533 ret = yin_parse_minmax(ctx, current_element, kw, subelem->dest);
3534 break;
3535 case LY_STMT_MODIFIER:
3536 ret = yin_parse_modifier(ctx, (const char **)subelem->dest, exts);
3537 break;
3538 case LY_STMT_MUST:
3539 ret = yin_parse_must(ctx, (struct lysp_restr **)subelem->dest);
3540 break;
3541 case LY_STMT_NAMESPACE:
3542 ret = yin_parse_simple_elem(ctx, kw, subelem, YIN_ARG_URI, Y_STR_ARG, exts);
3543 break;
3544 case LY_STMT_NOTIFICATION:
3545 ret = yin_parse_notification(ctx, (struct tree_node_meta *)subelem->dest);
3546 break;
3547 case LY_STMT_ORDERED_BY:
3548 ret = yin_parse_orderedby(ctx, (uint16_t *)subelem->dest, exts);
3549 break;
3550 case LY_STMT_PATH:
3551 ret = yin_parse_path(ctx, kw, (struct lysp_type *)subelem->dest);
3552 break;
3553 case LY_STMT_PATTERN:
3554 ret = yin_parse_pattern(ctx, (struct lysp_type *)subelem->dest);
3555 break;
3556 case LY_STMT_VALUE:
3557 case LY_STMT_POSITION:
3558 ret = yin_parse_value_pos(ctx, kw, (struct lysp_type_enum *)subelem->dest);
3559 break;
3560 case LY_STMT_PREFIX:
3561 ret = yin_parse_simple_elem(ctx, kw, subelem, YIN_ARG_VALUE, Y_IDENTIF_ARG, exts);
3562 break;
3563 case LY_STMT_RANGE:
3564 ret = yin_parse_range(ctx, (struct lysp_type *)subelem->dest);
3565 break;
3566 case LY_STMT_REFINE:
3567 ret = yin_parse_refine(ctx, (struct lysp_refine **)subelem->dest);
3568 break;
3569 case LY_STMT_REQUIRE_INSTANCE:
3570 ret = yin_pasrse_reqinstance(ctx, (struct lysp_type *)subelem->dest);
3571 break;
3572 case LY_STMT_REVISION:
3573 ret = yin_parse_revision(ctx, (struct lysp_revision **)subelem->dest);
3574 break;
3575 case LY_STMT_REVISION_DATE:
3576 ret = yin_parse_revision_date(ctx, (char *)subelem->dest, exts);
3577 break;
3578 case LY_STMT_STATUS:
3579 ret = yin_parse_status(ctx, (uint16_t *)subelem->dest, exts);
3580 break;
3581 case LY_STMT_TYPE:
3582 ret = yin_parse_type(ctx, current_element, subelem);
3583 break;
3584 case LY_STMT_TYPEDEF:
3585 ret = yin_parse_typedef(ctx, (struct tree_node_meta *)subelem->dest);
3586 break;
3587 case LY_STMT_UNIQUE:
3588 ret = yin_parse_simple_elem(ctx, kw, subelem, YIN_ARG_TAG, Y_STR_ARG, exts);
3589 break;
3590 case LY_STMT_USES:
3591 ret = yin_parse_uses(ctx, (struct tree_node_meta *)subelem->dest);
3592 break;
3593 case LY_STMT_WHEN:
3594 ret = yin_parse_when(ctx, (struct lysp_when **)subelem->dest);
3595 break;
3596 case LY_STMT_YANG_VERSION:
3597 ret = yin_parse_yangversion(ctx, (uint8_t *)subelem->dest, exts);
3598 break;
3599 case LY_STMT_YIN_ELEMENT:
3600 ret = yin_parse_yin_element(ctx, (uint16_t *)subelem->dest, exts);
3601 break;
3602 case LY_STMT_ARG_TEXT:
3603 case LY_STMT_ARG_VALUE:
3604 /* TODO what to do with content/attributes? */
3605 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3606 while (ctx->xmlctx->status == LYXML_ATTRIBUTE) {
3607 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3608 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3609 }
3610 ret = yin_parse_content(ctx, NULL, 0, kw, (const char **)subelem->dest, NULL);
3611 break;
3612 default:
3613 LOGINT(ctx->xmlctx->ctx);
3614 ret = LY_EINT;
David Sedlák3ffbc522019-07-02 17:49:28 +02003615 }
Michal Vaskob36053d2020-03-26 15:49:30 +01003616 LY_CHECK_GOTO(ret, cleanup);
3617 subelem = NULL;
3618
3619 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
David Sedlákd6e56892019-07-01 15:40:24 +02003620 }
Michal Vaskob36053d2020-03-26 15:49:30 +01003621 } else {
3622 LY_CHECK_RET(ret);
3623 /* elements with text or none content */
3624 /* save text content, if text_content isn't set, it's just ignored */
3625 /* no resources are allocated in this branch, no need to use cleanup label */
3626 LY_CHECK_RET(yin_validate_value(ctx, Y_STR_ARG));
3627 if (text_content) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02003628 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 +01003629 LY_CHECK_RET(!*text_content, LY_EMEM);
3630 }
3631
3632 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
David Sedlákd6e56892019-07-01 15:40:24 +02003633 }
Michal Vaskob36053d2020-03-26 15:49:30 +01003634
David Sedlák8b754462019-07-25 16:22:13 +02003635 /* mandatory subelemnts are checked only after whole element was succesfully parsed */
3636 LY_CHECK_RET(yin_check_subelem_mandatory_constraint(ctx, subelem_info, subelem_info_size, current_element));
David Sedlákd6e56892019-07-01 15:40:24 +02003637
3638cleanup:
David Sedlákd6e56892019-07-01 15:40:24 +02003639 return ret;
3640}
3641
David Sedlákaa98bba2019-09-12 11:52:14 +02003642/**
Michal Vaskoafac7822020-10-20 14:22:26 +02003643 * @brief Parse module element.
David Sedlákaa98bba2019-09-12 11:52:14 +02003644 *
3645 * @param[in,out] ctx Yin parser context for logging and to store current state.
Michal Vaskoafac7822020-10-20 14:22:26 +02003646 * @param[out] mod Parsed module structure.
David Sedlákaa98bba2019-09-12 11:52:14 +02003647 *
3648 * @return LY_ERR values.
3649 */
David Sedlák1e2cdd02019-06-27 14:17:43 +02003650LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01003651yin_parse_mod(struct lys_yin_parser_ctx *ctx, struct lysp_module *mod)
David Sedlák3b4db242018-10-19 16:11:01 +02003652{
David Sedlák81497a32019-08-13 16:56:26 +02003653 LY_ERR ret = LY_SUCCESS;
3654 struct yin_subelement *subelems = NULL;
David Sedláked809f12019-09-13 10:15:11 +02003655 struct lysp_submodule *dup;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003656 size_t subelems_size;
David Sedlák3b4db242018-10-19 16:11:01 +02003657
Michal Vaskoc3781c32020-10-06 14:04:08 +02003658 mod->is_submod = 0;
3659
Michal Vaskob36053d2020-03-26 15:49:30 +01003660 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3661 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 +01003662 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 28, NULL, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02003663 LY_STMT_ANYDATA, &mod->data, YIN_SUBELEM_VER2,
3664 LY_STMT_ANYXML, &mod->data, 0,
3665 LY_STMT_AUGMENT, &mod->augments, 0,
3666 LY_STMT_CHOICE, &mod->data, 0,
3667 LY_STMT_CONTACT, &mod->mod->contact, YIN_SUBELEM_UNIQUE,
3668 LY_STMT_CONTAINER, &mod->data, 0,
3669 LY_STMT_DESCRIPTION, &mod->mod->dsc, YIN_SUBELEM_UNIQUE,
3670 LY_STMT_DEVIATION, &mod->deviations, 0,
3671 LY_STMT_EXTENSION, &mod->extensions, 0,
3672 LY_STMT_FEATURE, &mod->features, 0,
3673 LY_STMT_GROUPING, &mod->groupings, 0,
3674 LY_STMT_IDENTITY, &mod->identities, 0,
3675 LY_STMT_IMPORT, mod->mod->prefix, &mod->imports, 0,
3676 LY_STMT_INCLUDE, mod->mod->name, &mod->includes, 0,
3677 LY_STMT_LEAF, &mod->data, 0,
3678 LY_STMT_LEAF_LIST, &mod->data, 0,
3679 LY_STMT_LIST, &mod->data, 0,
3680 LY_STMT_NAMESPACE, &mod->mod->ns, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE,
3681 LY_STMT_NOTIFICATION, &mod->notifs, 0,
3682 LY_STMT_ORGANIZATION, &mod->mod->org, YIN_SUBELEM_UNIQUE,
3683 LY_STMT_PREFIX, &mod->mod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE,
3684 LY_STMT_REFERENCE, &mod->mod->ref, YIN_SUBELEM_UNIQUE,
3685 LY_STMT_REVISION, &mod->revs, 0,
3686 LY_STMT_RPC, &mod->rpcs, 0,
3687 LY_STMT_TYPEDEF, &mod->typedefs, 0,
3688 LY_STMT_USES, &mod->data, 0,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003689 LY_STMT_YANG_VERSION, &mod->version, YIN_SUBELEM_UNIQUE,
Michal Vasko69730152020-10-09 16:30:07 +02003690 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
David Sedlák81497a32019-08-13 16:56:26 +02003691
Radek Krejcif13b87b2020-12-01 22:02:17 +01003692 ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_MODULE, NULL, &mod->exts);
3693 subelems_deallocator(subelems_size, subelems);
David Sedláked809f12019-09-13 10:15:11 +02003694 LY_CHECK_RET(ret);
David Sedlák81497a32019-08-13 16:56:26 +02003695
David Sedláked809f12019-09-13 10:15:11 +02003696 /* finalize parent pointers to the reallocated items */
3697 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, mod->groupings, mod->augments, mod->rpcs, mod->notifs));
3698
3699 /* submodules share the namespace with the module names, so there must not be
3700 * a submodule of the same name in the context, no need for revision matching */
Michal Vaskob36053d2020-03-26 15:49:30 +01003701 dup = ly_ctx_get_submodule(ctx->xmlctx->ctx, NULL, mod->mod->name, NULL);
David Sedláked809f12019-09-13 10:15:11 +02003702 if (dup) {
Radek Krejci854e1552020-12-21 15:05:23 +01003703 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_NAME2_COL, "module", "submodule", mod->mod->name);
David Sedláked809f12019-09-13 10:15:11 +02003704 return LY_EVALID;
3705 }
3706
3707 return LY_SUCCESS;
David Sedlák3b4db242018-10-19 16:11:01 +02003708}
3709
Michal Vaskoafac7822020-10-20 14:22:26 +02003710/**
3711 * @brief Parse submodule element.
3712 *
3713 * @param[in,out] ctx Yin parser context for logging and to store current state.
3714 * @param[in] mod_attrs Attributes of submodule element.
3715 * @param[out] submod Parsed submodule structure.
3716 *
3717 * @return LY_ERR values.
3718 */
David Sedlák3b4db242018-10-19 16:11:01 +02003719LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01003720yin_parse_submod(struct lys_yin_parser_ctx *ctx, struct lysp_submodule *submod)
David Sedlák298ff6d2019-07-26 14:29:03 +02003721{
David Sedlák81497a32019-08-13 16:56:26 +02003722 LY_ERR ret = LY_SUCCESS;
3723 struct yin_subelement *subelems = NULL;
David Sedláked809f12019-09-13 10:15:11 +02003724 struct lysp_submodule *dup;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003725 size_t subelems_size;
David Sedlák298ff6d2019-07-26 14:29:03 +02003726
Michal Vaskoc3781c32020-10-06 14:04:08 +02003727 submod->is_submod = 1;
3728
Michal Vaskob36053d2020-03-26 15:49:30 +01003729 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3730 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 +01003731 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 27, NULL, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02003732 LY_STMT_ANYDATA, &submod->data, YIN_SUBELEM_VER2,
3733 LY_STMT_ANYXML, &submod->data, 0,
3734 LY_STMT_AUGMENT, &submod->augments, 0,
3735 LY_STMT_BELONGS_TO, submod, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE,
3736 LY_STMT_CHOICE, &submod->data, 0,
3737 LY_STMT_CONTACT, &submod->contact, YIN_SUBELEM_UNIQUE,
3738 LY_STMT_CONTAINER, &submod->data, 0,
3739 LY_STMT_DESCRIPTION, &submod->dsc, YIN_SUBELEM_UNIQUE,
3740 LY_STMT_DEVIATION, &submod->deviations, 0,
3741 LY_STMT_EXTENSION, &submod->extensions, 0,
3742 LY_STMT_FEATURE, &submod->features, 0,
3743 LY_STMT_GROUPING, &submod->groupings, 0,
3744 LY_STMT_IDENTITY, &submod->identities, 0,
3745 LY_STMT_IMPORT, submod->prefix, &submod->imports, 0,
3746 LY_STMT_INCLUDE, submod->name, &submod->includes, 0,
3747 LY_STMT_LEAF, &submod->data, 0,
3748 LY_STMT_LEAF_LIST, &submod->data, 0,
3749 LY_STMT_LIST, &submod->data, 0,
3750 LY_STMT_NOTIFICATION, &submod->notifs, 0,
3751 LY_STMT_ORGANIZATION, &submod->org, YIN_SUBELEM_UNIQUE,
3752 LY_STMT_REFERENCE, &submod->ref, YIN_SUBELEM_UNIQUE,
3753 LY_STMT_REVISION, &submod->revs, 0,
3754 LY_STMT_RPC, &submod->rpcs, 0,
3755 LY_STMT_TYPEDEF, &submod->typedefs, 0,
3756 LY_STMT_USES, &submod->data, 0,
3757 LY_STMT_YANG_VERSION, &submod->version, YIN_SUBELEM_UNIQUE,
3758 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
David Sedlák81497a32019-08-13 16:56:26 +02003759
Radek Krejcif13b87b2020-12-01 22:02:17 +01003760 ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_SUBMODULE, NULL, &submod->exts);
3761 subelems_deallocator(subelems_size, subelems);
David Sedláked809f12019-09-13 10:15:11 +02003762 LY_CHECK_RET(ret);
David Sedlák81497a32019-08-13 16:56:26 +02003763
David Sedláked809f12019-09-13 10:15:11 +02003764 /* finalize parent pointers to the reallocated items */
3765 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, submod->groupings, submod->augments, submod->rpcs, submod->notifs));
3766
3767 /* submodules share the namespace with the module names, so there must not be
3768 * a submodule of the same name in the context, no need for revision matching */
Michal Vaskob36053d2020-03-26 15:49:30 +01003769 dup = ly_ctx_get_submodule(ctx->xmlctx->ctx, NULL, submod->name, NULL);
Michal Vaskoc3781c32020-10-06 14:04:08 +02003770 if (dup && strcmp(dup->mod->name, submod->mod->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01003771 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_NAME_COL, "submodules", dup->name);
David Sedláked809f12019-09-13 10:15:11 +02003772 return LY_EVALID;
3773 }
3774
3775 return LY_SUCCESS;
David Sedlák298ff6d2019-07-26 14:29:03 +02003776}
3777
3778LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003779yin_parse_submodule(struct lys_yin_parser_ctx **yin_ctx, struct ly_ctx *ctx, struct lys_parser_ctx *main_ctx,
Radek Krejci0f969882020-08-21 16:56:47 +02003780 struct ly_in *in, struct lysp_submodule **submod)
David Sedlák8985a142019-07-31 16:43:06 +02003781{
Radek Krejcid6b76452019-09-03 17:03:03 +02003782 enum ly_stmt kw = LY_STMT_NONE;
David Sedlák8985a142019-07-31 16:43:06 +02003783 LY_ERR ret = LY_SUCCESS;
David Sedlák8985a142019-07-31 16:43:06 +02003784 struct lysp_submodule *mod_p = NULL;
3785
3786 /* create context */
3787 *yin_ctx = calloc(1, sizeof **yin_ctx);
David Sedlák1b623122019-08-05 15:27:49 +02003788 LY_CHECK_ERR_RET(!(*yin_ctx), LOGMEM(ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01003789 (*yin_ctx)->format = LYS_IN_YIN;
Michal Vasko405cc9e2020-12-01 12:01:27 +01003790 (*yin_ctx)->unres = main_ctx->unres;
Michal Vasko63f3d842020-07-08 10:10:14 +02003791 LY_CHECK_RET(lyxml_ctx_new(ctx, in, &(*yin_ctx)->xmlctx));
David Sedlák8985a142019-07-31 16:43:06 +02003792
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003793 mod_p = calloc(1, sizeof *mod_p);
3794 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3795 mod_p->mod = main_ctx->parsed_mod->mod;
3796 mod_p->parsing = 1;
3797 (*yin_ctx)->parsed_mod = (struct lysp_module *)mod_p;
3798
David Sedlák1b623122019-08-05 15:27:49 +02003799 /* map the typedefs and groupings list from main context to the submodule's context */
3800 memcpy(&(*yin_ctx)->tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
3801 memcpy(&(*yin_ctx)->grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes);
3802
David Sedlák8985a142019-07-31 16:43:06 +02003803 /* check submodule */
Michal Vaskob36053d2020-03-26 15:49:30 +01003804 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 +02003805 (*yin_ctx)->xmlctx->prefix_len, LY_STMT_NONE);
Radek Krejcid6b76452019-09-03 17:03:03 +02003806 if (kw == LY_STMT_MODULE) {
David Sedlák8985a142019-07-31 16:43:06 +02003807 LOGERR(ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
3808 ret = LY_EINVAL;
3809 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02003810 } else if (kw != LY_STMT_SUBMODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02003811 LOGVAL_PARSER((struct lys_parser_ctx *)*yin_ctx, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
David Sedlák8985a142019-07-31 16:43:06 +02003812 ret = LY_EVALID;
3813 goto cleanup;
3814 }
3815
Michal Vaskob36053d2020-03-26 15:49:30 +01003816 ret = yin_parse_submod(*yin_ctx, mod_p);
David Sedlák8985a142019-07-31 16:43:06 +02003817 LY_CHECK_GOTO(ret, cleanup);
3818
David Sedlákbf8a2b72019-08-14 16:48:10 +02003819 /* skip possible trailing whitespaces at end of the input */
Michal Vasko63f3d842020-07-08 10:10:14 +02003820 while (isspace(in->current[0])) {
Radek Krejcidd713ce2021-01-04 23:12:12 +01003821 if (in->current[0] == '\n') {
3822 LY_IN_NEW_LINE(in);
3823 }
Michal Vasko63f3d842020-07-08 10:10:14 +02003824 ly_in_skip(in, 1);
David Sedlák6d781b62019-08-02 15:22:52 +02003825 }
Michal Vasko63f3d842020-07-08 10:10:14 +02003826 if (in->current[0]) {
3827 LOGVAL_PARSER((struct lys_parser_ctx *)*yin_ctx, LY_VCODE_TRAILING_SUBMOD, 15, in->current,
Michal Vasko69730152020-10-09 16:30:07 +02003828 strlen(in->current) > 15 ? "..." : "");
David Sedlák6d781b62019-08-02 15:22:52 +02003829 ret = LY_EVALID;
3830 goto cleanup;
3831 }
3832
David Sedlák8985a142019-07-31 16:43:06 +02003833 mod_p->parsing = 0;
3834 *submod = mod_p;
3835
3836cleanup:
3837 if (ret) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003838 lysp_module_free((struct lysp_module *)mod_p);
David Sedlák1b623122019-08-05 15:27:49 +02003839 yin_parser_ctx_free(*yin_ctx);
3840 *yin_ctx = NULL;
David Sedlák8985a142019-07-31 16:43:06 +02003841 }
David Sedlák8985a142019-07-31 16:43:06 +02003842 return ret;
3843}
3844
3845LY_ERR
Michal Vasko405cc9e2020-12-01 12:01:27 +01003846yin_parse_module(struct lys_yin_parser_ctx **yin_ctx, struct ly_in *in, struct lys_module *mod, struct lys_glob_unres *unres)
David Sedlák3b4db242018-10-19 16:11:01 +02003847{
David Sedláke4889912018-11-02 09:52:40 +01003848 LY_ERR ret = LY_SUCCESS;
Radek Krejcid6b76452019-09-03 17:03:03 +02003849 enum ly_stmt kw = LY_STMT_NONE;
David Sedlák3017da42019-02-15 09:48:04 +01003850 struct lysp_module *mod_p = NULL;
David Sedlák3b4db242018-10-19 16:11:01 +02003851
David Sedlák8985a142019-07-31 16:43:06 +02003852 /* create context */
3853 *yin_ctx = calloc(1, sizeof **yin_ctx);
David Sedlák1b623122019-08-05 15:27:49 +02003854 LY_CHECK_ERR_RET(!(*yin_ctx), LOGMEM(mod->ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01003855 (*yin_ctx)->format = LYS_IN_YIN;
Michal Vasko405cc9e2020-12-01 12:01:27 +01003856 (*yin_ctx)->unres = unres;
Michal Vasko63f3d842020-07-08 10:10:14 +02003857 LY_CHECK_RET(lyxml_ctx_new(mod->ctx, in, &(*yin_ctx)->xmlctx));
David Sedlákda8ffa32019-07-08 14:17:10 +02003858
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003859 mod_p = calloc(1, sizeof *mod_p);
3860 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(mod->ctx), cleanup);
3861 mod_p->mod = mod;
3862 mod_p->parsing = 1;
3863 (*yin_ctx)->parsed_mod = mod_p;
3864
David Sedlák8985a142019-07-31 16:43:06 +02003865 /* check module */
Michal Vaskob36053d2020-03-26 15:49:30 +01003866 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 +02003867 (*yin_ctx)->xmlctx->prefix_len, LY_STMT_NONE);
Radek Krejcid6b76452019-09-03 17:03:03 +02003868 if (kw == LY_STMT_SUBMODULE) {
David Sedlák8985a142019-07-31 16:43:06 +02003869 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 +01003870 ret = LY_EINVAL;
3871 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02003872 } else if (kw != LY_STMT_MODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02003873 LOGVAL_PARSER((struct lys_parser_ctx *)*yin_ctx, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
David Sedlák3017da42019-02-15 09:48:04 +01003874 ret = LY_EVALID;
3875 goto cleanup;
David Sedláke4889912018-11-02 09:52:40 +01003876 }
3877
David Sedlák00250342019-06-21 14:19:39 +02003878 /* parse module substatements */
Michal Vaskob36053d2020-03-26 15:49:30 +01003879 ret = yin_parse_mod(*yin_ctx, mod_p);
David Sedlák3017da42019-02-15 09:48:04 +01003880 LY_CHECK_GOTO(ret, cleanup);
David Sedlák2e411422018-12-17 02:35:39 +01003881
David Sedlákbf8a2b72019-08-14 16:48:10 +02003882 /* skip possible trailing whitespaces at end of the input */
Michal Vasko63f3d842020-07-08 10:10:14 +02003883 while (isspace(in->current[0])) {
Radek Krejcidd713ce2021-01-04 23:12:12 +01003884 if (in->current[0] == '\n') {
3885 LY_IN_NEW_LINE(in);
3886 }
Michal Vasko63f3d842020-07-08 10:10:14 +02003887 ly_in_skip(in, 1);
David Sedlák6d781b62019-08-02 15:22:52 +02003888 }
Michal Vasko63f3d842020-07-08 10:10:14 +02003889 if (in->current[0]) {
3890 LOGVAL_PARSER((struct lys_parser_ctx *)*yin_ctx, LY_VCODE_TRAILING_MOD, 15, in->current,
Michal Vasko69730152020-10-09 16:30:07 +02003891 strlen(in->current) > 15 ? "..." : "");
David Sedlák6d781b62019-08-02 15:22:52 +02003892 ret = LY_EVALID;
3893 goto cleanup;
3894 }
3895
David Sedlák3017da42019-02-15 09:48:04 +01003896 mod_p->parsing = 0;
3897 mod->parsed = mod_p;
3898
3899cleanup:
David Sedlák8f7a1172019-06-20 14:42:18 +02003900 if (ret != LY_SUCCESS) {
David Sedlák3017da42019-02-15 09:48:04 +01003901 lysp_module_free(mod_p);
David Sedlák1b623122019-08-05 15:27:49 +02003902 yin_parser_ctx_free(*yin_ctx);
3903 *yin_ctx = NULL;
David Sedlák3017da42019-02-15 09:48:04 +01003904 }
David Sedlák2e411422018-12-17 02:35:39 +01003905 return ret;
David Sedlák3b4db242018-10-19 16:11:01 +02003906}