blob: cfecde024065e6a91e5d77c099db12f9e75e134b [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 */
Radek Krejci535ea9f2020-05-29 16:01:05 +020014#include "parser_yin.h"
David Sedlákecf5eb82019-06-03 14:12:44 +020015
David Sedlák3ffbc522019-07-02 17:49:28 +020016#include <assert.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020017#include <ctype.h>
18#include <errno.h>
19#include <stdarg.h>
20#include <stdbool.h>
David Sedlák3b4db242018-10-19 16:11:01 +020021#include <stdio.h>
22#include <stdlib.h>
David Sedlák872c7b42018-10-26 13:15:20 +020023#include <string.h>
David Sedlákf824ad52018-10-14 23:58:15 +020024
Radek Krejci535ea9f2020-05-29 16:01:05 +020025#include "common.h"
David Sedlákf824ad52018-10-14 23:58:15 +020026#include "context.h"
David Sedlákecf5eb82019-06-03 14:12:44 +020027#include "dict.h"
Michal Vasko004d3152020-06-11 19:59:22 +020028#include "path.h"
Michal Vasko63f3d842020-07-08 10:10:14 +020029#include "parser_internal.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020030#include "parser_schema.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020031#include "set.h"
David Sedlákecf5eb82019-06-03 14:12:44 +020032#include "tree.h"
33#include "tree_schema.h"
David Sedlák3b4db242018-10-19 16:11:01 +020034#include "tree_schema_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020035#include "xml.h"
David Sedlák00250342019-06-21 14:19:39 +020036
David Sedlák2b214ac2019-06-06 16:11:03 +020037/**
38 * @brief check if given string is URI of yin namespace.
David Sedlák8985a142019-07-31 16:43:06 +020039 *
David Sedlák2b214ac2019-06-06 16:11:03 +020040 * @param ns Namespace URI to check.
41 *
42 * @return true if ns equals YIN_NS_URI false otherwise.
43 */
44#define IS_YIN_NS(ns) (strcmp(ns, YIN_NS_URI) == 0)
45
Michal Vasko22df3f02020-08-24 13:29:22 +020046const char * const yin_attr_list[] = {
David Sedlákf6251182019-06-06 10:22:13 +020047 [YIN_ARG_NAME] = "name",
48 [YIN_ARG_TARGET_NODE] = "target-node",
49 [YIN_ARG_MODULE] = "module",
50 [YIN_ARG_VALUE] = "value",
51 [YIN_ARG_TEXT] = "text",
52 [YIN_ARG_CONDITION] = "condition",
53 [YIN_ARG_URI] = "uri",
54 [YIN_ARG_DATE] = "date",
55 [YIN_ARG_TAG] = "tag",
David Sedlák071f7662019-09-12 02:02:51 +020056 [YIN_ARG_NONE] = "none",
David Sedlákf6251182019-06-06 10:22:13 +020057};
58
Radek Krejcid6b76452019-09-03 17:03:03 +020059enum ly_stmt
Michal Vaskob36053d2020-03-26 15:49:30 +010060yin_match_keyword(struct lys_yin_parser_ctx *ctx, const char *name, size_t name_len,
Radek Krejci0f969882020-08-21 16:56:47 +020061 const char *prefix, size_t prefix_len, enum ly_stmt parent)
David Sedlák1bccdfa2019-06-17 15:55:27 +020062{
David Sedlák8f7a1172019-06-20 14:42:18 +020063 const char *start = NULL;
Radek Krejcid6b76452019-09-03 17:03:03 +020064 enum ly_stmt kw = LY_STMT_NONE;
David Sedlák8f7a1172019-06-20 14:42:18 +020065 const struct lyxml_ns *ns = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +020066 struct ly_in *in;
David Sedlák8f7a1172019-06-20 14:42:18 +020067
68 if (!name || name_len == 0) {
Radek Krejcid6b76452019-09-03 17:03:03 +020069 return LY_STMT_NONE;
David Sedlák1bccdfa2019-06-17 15:55:27 +020070 }
71
Michal Vaskoc8a230d2020-08-14 12:17:10 +020072 ns = lyxml_ns_get(&ctx->xmlctx->ns, prefix, prefix_len);
David Sedlák8f7a1172019-06-20 14:42:18 +020073 if (ns) {
74 if (!IS_YIN_NS(ns->uri)) {
Radek Krejcid6b76452019-09-03 17:03:03 +020075 return LY_STMT_EXTENSION_INSTANCE;
David Sedlák8f7a1172019-06-20 14:42:18 +020076 }
77 } else {
78 /* elements without namespace are automatically unknown */
Radek Krejcid6b76452019-09-03 17:03:03 +020079 return LY_STMT_NONE;
David Sedlák8f7a1172019-06-20 14:42:18 +020080 }
David Sedlák1bccdfa2019-06-17 15:55:27 +020081
Michal Vasko63f3d842020-07-08 10:10:14 +020082 LY_CHECK_RET(ly_in_new_memory(name, &in), LY_STMT_NONE);
83 start = in->current;
84 kw = lysp_match_kw(NULL, in);
85 name = in->current;
86 ly_in_free(in, 0);
David Sedlák8f7a1172019-06-20 14:42:18 +020087
88 if (name - start == (long int)name_len) {
David Sedlákc1771b12019-07-10 15:55:46 +020089 /* this is done because of collision in yang statement value and yang argument mapped to yin element value */
Michal Vasko63f3d842020-07-08 10:10:14 +020090 if (kw == LY_STMT_VALUE && parent == LY_STMT_ERROR_MESSAGE) {
Radek Krejcid6b76452019-09-03 17:03:03 +020091 return LY_STMT_ARG_VALUE;
David Sedlákc1771b12019-07-10 15:55:46 +020092 }
David Sedlák1bccdfa2019-06-17 15:55:27 +020093 return kw;
94 } else {
David Sedlák3ffbc522019-07-02 17:49:28 +020095 if (strncmp(start, "text", name_len) == 0) {
Radek Krejcid6b76452019-09-03 17:03:03 +020096 return LY_STMT_ARG_TEXT;
David Sedlák3ffbc522019-07-02 17:49:28 +020097 } else {
Radek Krejcid6b76452019-09-03 17:03:03 +020098 return LY_STMT_NONE;
David Sedlák3ffbc522019-07-02 17:49:28 +020099 }
David Sedlák1bccdfa2019-06-17 15:55:27 +0200100 }
101}
102
David Sedlákc5b20842019-08-13 10:18:31 +0200103enum yin_argument
David Sedlák060b00e2019-06-19 11:12:06 +0200104yin_match_argument_name(const char *name, size_t len)
David Sedlák3b4db242018-10-19 16:11:01 +0200105{
David Sedlákc5b20842019-08-13 10:18:31 +0200106 enum yin_argument arg = YIN_ARG_UNKNOWN;
David Sedlák872c7b42018-10-26 13:15:20 +0200107 size_t already_read = 0;
David Sedlák7ff55a92019-06-17 11:11:41 +0200108 LY_CHECK_RET(len == 0, YIN_ARG_NONE);
David Sedlák3b4db242018-10-19 16:11:01 +0200109
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200110#define READ_INC(LEN) already_read += LEN
111#define ARG_SET(STMT) arg=STMT
112#define ARG_CHECK(STR, LEN) (!strncmp((name) + already_read, STR, LEN) && (READ_INC(LEN)))
David Sedlákc10e7902018-12-17 02:17:59 +0100113
David Sedlák1c8b2702019-02-22 11:03:02 +0100114 switch (*name) {
David Sedlák94de2aa2019-02-15 12:42:11 +0100115 case 'c':
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200116 READ_INC(1);
117 if (ARG_CHECK("ondition", 8)) {
118 ARG_SET(YIN_ARG_CONDITION);
119 }
David Sedlák3b4db242018-10-19 16:11:01 +0200120 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200121
David Sedlák94de2aa2019-02-15 12:42:11 +0100122 case 'd':
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200123 READ_INC(1);
124 if (ARG_CHECK("ate", 3)) {
125 ARG_SET(YIN_ARG_DATE);
126 }
David Sedlák3b4db242018-10-19 16:11:01 +0200127 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200128
David Sedlák94de2aa2019-02-15 12:42:11 +0100129 case 'm':
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200130 READ_INC(1);
131 if (ARG_CHECK("odule", 5)) {
132 ARG_SET(YIN_ARG_MODULE);
133 }
David Sedlák872c7b42018-10-26 13:15:20 +0200134 break;
135
David Sedlák94de2aa2019-02-15 12:42:11 +0100136 case 'n':
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200137 READ_INC(1);
138 if (ARG_CHECK("ame", 3)) {
139 ARG_SET(YIN_ARG_NAME);
140 }
David Sedlák872c7b42018-10-26 13:15:20 +0200141 break;
142
David Sedlák94de2aa2019-02-15 12:42:11 +0100143 case 't':
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200144 READ_INC(1);
145 if (ARG_CHECK("a", 1)) {
146 if (ARG_CHECK("g", 1)) {
147 ARG_SET(YIN_ARG_TAG);
148 } else if (ARG_CHECK("rget-node", 9)) {
149 ARG_SET(YIN_ARG_TARGET_NODE);
150 }
151 } else if (ARG_CHECK("ext", 3)) {
152 ARG_SET(YIN_ARG_TEXT);
153 }
David Sedlák3b4db242018-10-19 16:11:01 +0200154 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200155
David Sedlák94de2aa2019-02-15 12:42:11 +0100156 case 'u':
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200157 READ_INC(1);
158 if (ARG_CHECK("ri", 2)) {
159 ARG_SET(YIN_ARG_URI);
160 }
David Sedlák3b4db242018-10-19 16:11:01 +0200161 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200162
David Sedlák94de2aa2019-02-15 12:42:11 +0100163 case 'v':
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200164 READ_INC(1);
165 if (ARG_CHECK("alue", 4)) {
166 ARG_SET(YIN_ARG_VALUE);
167 }
David Sedlák3b4db242018-10-19 16:11:01 +0200168 break;
169 }
170
David Sedlákc10e7902018-12-17 02:17:59 +0100171 /* whole argument must be matched */
David Sedlák872c7b42018-10-26 13:15:20 +0200172 if (already_read != len) {
David Sedláka7406952019-04-05 10:33:07 +0200173 arg = YIN_ARG_UNKNOWN;
David Sedlák872c7b42018-10-26 13:15:20 +0200174 }
175
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200176#undef ARG_CHECK
177#undef ARG_SET
178#undef READ_INC
David Sedlák18730132019-03-15 15:51:34 +0100179
David Sedlák872c7b42018-10-26 13:15:20 +0200180 return arg;
David Sedlák3b4db242018-10-19 16:11:01 +0200181}
182
Radek Krejcid6b76452019-09-03 17:03:03 +0200183#define IS_NODE_ELEM(kw) (kw == LY_STMT_ANYXML || kw == LY_STMT_ANYDATA || kw == LY_STMT_LEAF || kw == LY_STMT_LEAF_LIST || \
184 kw == LY_STMT_TYPEDEF || kw == LY_STMT_USES || kw == LY_STMT_LIST || kw == LY_STMT_NOTIFICATION || \
185 kw == LY_STMT_GROUPING || kw == LY_STMT_CONTAINER || kw == LY_STMT_CASE || kw == LY_STMT_CHOICE || \
186 kw == LY_STMT_ACTION || kw == LY_STMT_RPC || kw == LY_STMT_AUGMENT)
David Sedlák81497a32019-08-13 16:56:26 +0200187
Radek Krejcid6b76452019-09-03 17:03:03 +0200188#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 +0200189
David Sedlák26ea1432019-08-14 13:42:23 +0200190/**
191 * @brief Free subelems information allocated on heap.
192 *
193 * @param[in] count Size of subelems array.
194 * @param[in] subelems Subelems array to free.
195 */
David Sedlák81497a32019-08-13 16:56:26 +0200196static void
197subelems_deallocator(size_t count, struct yin_subelement *subelems)
198{
Michal Vaskod989ba02020-08-24 10:59:24 +0200199 for (size_t i = 0; i < count; ++i) {
David Sedlák81497a32019-08-13 16:56:26 +0200200 if (HAS_META(subelems[i].type)) {
201 free(subelems[i].dest);
202 }
203 }
204
205 free(subelems);
206}
207
David Sedlák26ea1432019-08-14 13:42:23 +0200208/**
209 * @brief Allocate subelems information on heap.
210 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200211 * @param[in] ctx YIN parser context, used for logging.
David Sedlák26ea1432019-08-14 13:42:23 +0200212 * @param[in] count Number of subelements.
213 * @param[in] parent Parent node if any.
214 * @param[out] result Allocated subelems array.
215 *
216 * @return LY_SUCCESS on success LY_EMEM on memmory allocation failure.
217 */
David Sedlák81497a32019-08-13 16:56:26 +0200218static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100219subelems_allocator(struct lys_yin_parser_ctx *ctx, size_t count, struct lysp_node *parent,
Radek Krejci0f969882020-08-21 16:56:47 +0200220 struct yin_subelement **result, ...)
David Sedlák81497a32019-08-13 16:56:26 +0200221{
222 va_list ap;
223
224 *result = calloc(count, sizeof **result);
David Sedlákbf8a2b72019-08-14 16:48:10 +0200225 LY_CHECK_GOTO(!(*result), mem_err);
David Sedlák81497a32019-08-13 16:56:26 +0200226
227 va_start(ap, result);
228 for (size_t i = 0; i < count; ++i) {
229 /* TYPE */
Radek Krejcid6b76452019-09-03 17:03:03 +0200230 (*result)[i].type = va_arg(ap, enum ly_stmt);
David Sedlák81497a32019-08-13 16:56:26 +0200231 /* DEST */
232 if (IS_NODE_ELEM((*result)[i].type)) {
233 struct tree_node_meta *node_meta = NULL;
234 node_meta = calloc(1, sizeof *node_meta);
David Sedlákbf8a2b72019-08-14 16:48:10 +0200235 LY_CHECK_GOTO(!node_meta, mem_err);
David Sedlák81497a32019-08-13 16:56:26 +0200236 node_meta->parent = parent;
David Sedlákbf8a2b72019-08-14 16:48:10 +0200237 node_meta->nodes = va_arg(ap, void *);
David Sedlák81497a32019-08-13 16:56:26 +0200238 (*result)[i].dest = node_meta;
Radek Krejcid6b76452019-09-03 17:03:03 +0200239 } else if ((*result)[i].type == LY_STMT_IMPORT) {
David Sedlák81497a32019-08-13 16:56:26 +0200240 struct import_meta *imp_meta = NULL;
241 imp_meta = calloc(1, sizeof *imp_meta);
David Sedlákbf8a2b72019-08-14 16:48:10 +0200242 LY_CHECK_GOTO(!imp_meta, mem_err);
David Sedlák81497a32019-08-13 16:56:26 +0200243 imp_meta->prefix = va_arg(ap, const char *);
244 imp_meta->imports = va_arg(ap, struct lysp_import **);
245 (*result)[i].dest = imp_meta;
Radek Krejcid6b76452019-09-03 17:03:03 +0200246 } else if ((*result)[i].type == LY_STMT_INCLUDE) {
David Sedlák81497a32019-08-13 16:56:26 +0200247 struct include_meta *inc_meta = NULL;
248 inc_meta = calloc(1, sizeof *inc_meta);
David Sedlákbf8a2b72019-08-14 16:48:10 +0200249 LY_CHECK_GOTO(!inc_meta, mem_err);
David Sedlák81497a32019-08-13 16:56:26 +0200250 inc_meta->name = va_arg(ap, const char *);
251 inc_meta->includes = va_arg(ap, struct lysp_include **);
252 (*result)[i].dest = inc_meta;
Radek Krejcid6b76452019-09-03 17:03:03 +0200253 } else if ((*result)[i].type == LY_STMT_INPUT || (*result)[i].type == LY_STMT_OUTPUT) {
David Sedlák81497a32019-08-13 16:56:26 +0200254 struct inout_meta *inout_meta = NULL;
255 inout_meta = calloc(1, sizeof *inout_meta);
David Sedlákbf8a2b72019-08-14 16:48:10 +0200256 LY_CHECK_GOTO(!inout_meta, mem_err);
David Sedlák81497a32019-08-13 16:56:26 +0200257 inout_meta->parent = parent;
258 inout_meta->inout_p = va_arg(ap, struct lysp_action_inout *);
259 (*result)[i].dest = inout_meta;
260 } else {
261 (*result)[i].dest = va_arg(ap, void *);
262 }
263 /* FLAGS */
264 (*result)[i].flags = va_arg(ap, int);
265 }
266 va_end(ap);
267
268 return LY_SUCCESS;
269
David Sedlákbf8a2b72019-08-14 16:48:10 +0200270mem_err:
David Sedlák81497a32019-08-13 16:56:26 +0200271 subelems_deallocator(count, *result);
Michal Vaskob36053d2020-03-26 15:49:30 +0100272 LOGMEM(ctx->xmlctx->ctx);
David Sedlák81497a32019-08-13 16:56:26 +0200273 return LY_EMEM;
274}
275
David Sedlák8f7a1172019-06-20 14:42:18 +0200276LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100277yin_validate_value(struct lys_yin_parser_ctx *ctx, enum yang_arg val_type)
David Sedlák4a650532019-07-10 11:55:18 +0200278{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200279 uint8_t prefix = 0;
280 uint32_t c;
Michal Vaskob36053d2020-03-26 15:49:30 +0100281 size_t utf8_char_len, already_read = 0;
282 const char *val;
283
284 assert((ctx->xmlctx->status == LYXML_ELEM_CONTENT) || (ctx->xmlctx->status == LYXML_ATTR_CONTENT));
285
286 val = ctx->xmlctx->value;
287 while (already_read < ctx->xmlctx->value_len) {
David Sedlák4a650532019-07-10 11:55:18 +0200288 LY_CHECK_ERR_RET(ly_getutf8((const char **)&val, &c, &utf8_char_len),
289 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INCHAR, (val)[-utf8_char_len]), LY_EVALID);
290 already_read += utf8_char_len;
Michal Vaskob36053d2020-03-26 15:49:30 +0100291 LY_CHECK_ERR_RET(already_read > ctx->xmlctx->value_len, LOGINT(ctx->xmlctx->ctx), LY_EINT);
David Sedlák4a650532019-07-10 11:55:18 +0200292
293 switch (val_type) {
294 case Y_IDENTIF_ARG:
Radek Krejci1deb5be2020-08-26 16:43:36 +0200295 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 +0200296 break;
297 case Y_PREF_IDENTIF_ARG:
Radek Krejci1deb5be2020-08-26 16:43:36 +0200298 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 +0200299 break;
300 case Y_STR_ARG:
301 case Y_MAYBE_STR_ARG:
302 LY_CHECK_RET(lysp_check_stringchar((struct lys_parser_ctx *)ctx, c));
303 break;
304 }
305 }
306
307 return LY_SUCCESS;
308}
309
David Sedlákb4e44562019-07-04 15:42:12 +0200310/**
Michal Vaskob36053d2020-03-26 15:49:30 +0100311 * @brief Parse yin attributes.
David Sedlákb4e44562019-07-04 15:42:12 +0200312 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200313 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák4a650532019-07-10 11:55:18 +0200314 * @param[in] arg_type Type of argument that is expected in parsed element (use YIN_ARG_NONE for elements without
315 * special argument).
David Sedlákbf8a2b72019-08-14 16:48:10 +0200316 * @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 +0200317 * @param[in] val_type Type of expected value of attribute.
David Sedlákb4e44562019-07-04 15:42:12 +0200318 * @param[in] current_element Identification of current element, used for logging.
319 *
320 * @return LY_ERR values.
321 */
322static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100323yin_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 +0200324 enum ly_stmt current_element)
David Sedlák8f7a1172019-06-20 14:42:18 +0200325{
David Sedlákc5b20842019-08-13 10:18:31 +0200326 enum yin_argument arg = YIN_ARG_UNKNOWN;
David Sedlák619db942019-07-03 14:47:30 +0200327 bool found = false;
David Sedlák8f7a1172019-06-20 14:42:18 +0200328
David Sedlák1bccdfa2019-06-17 15:55:27 +0200329 /* validation of attributes */
Michal Vaskob36053d2020-03-26 15:49:30 +0100330 while (ctx->xmlctx->status == LYXML_ATTRIBUTE) {
David Sedlák00250342019-06-21 14:19:39 +0200331 /* yin arguments represented as attributes have no namespace, which in this case means no prefix */
Michal Vaskob36053d2020-03-26 15:49:30 +0100332 if (!ctx->xmlctx->prefix) {
333 arg = yin_match_argument_name(ctx->xmlctx->name, ctx->xmlctx->name_len);
David Sedlák7ff55a92019-06-17 11:11:41 +0200334 if (arg == YIN_ARG_NONE) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100335 /* skip it */
336 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
David Sedlák7ff55a92019-06-17 11:11:41 +0200337 } else if (arg == arg_type) {
David Sedlák1538a842019-08-08 15:38:51 +0200338 LY_CHECK_ERR_RET(found, LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_DUP_ATTR,
David Sedlák292763b2019-07-09 11:10:53 +0200339 yin_attr2str(arg), ly_stmt2str(current_element)), LY_EVALID);
David Sedlák619db942019-07-03 14:47:30 +0200340 found = true;
Michal Vaskob36053d2020-03-26 15:49:30 +0100341
342 /* go to value */
343 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
344 LY_CHECK_RET(yin_validate_value(ctx, val_type));
345 *arg_val = INSERT_STRING(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic);
David Sedlák169cc522019-08-15 13:23:45 +0200346 LY_CHECK_RET(!(*arg_val), LY_EMEM);
David Sedlák2b214ac2019-06-06 16:11:03 +0200347 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100348 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_UNEXP_ATTR, ctx->xmlctx->name_len,
349 ctx->xmlctx->name, ly_stmt2str(current_element));
David Sedlák619db942019-07-03 14:47:30 +0200350 return LY_EVALID;
David Sedláka7406952019-04-05 10:33:07 +0200351 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100352 } else {
353 /* skip it */
354 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
David Sedláka7406952019-04-05 10:33:07 +0200355 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100356
357 /* next attribute */
358 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
David Sedláka7406952019-04-05 10:33:07 +0200359 }
360
David Sedlák292763b2019-07-09 11:10:53 +0200361 /* anything else than Y_MAYBE_STR_ARG is mandatory */
362 if (val_type != Y_MAYBE_STR_ARG && !found) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100363 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LYVE_SYNTAX_YIN, "Missing mandatory attribute %s of %s element.",
364 yin_attr2str(arg_type), ly_stmt2str(current_element));
David Sedlák619db942019-07-03 14:47:30 +0200365 return LY_EVALID;
366 }
367
368 return LY_SUCCESS;
David Sedláka7406952019-04-05 10:33:07 +0200369}
370
David Sedlákd6e56892019-07-01 15:40:24 +0200371/**
Radek Krejci12b1c152019-09-05 16:20:48 +0200372 * @brief Get record with given type.
David Sedlákd6e56892019-07-01 15:40:24 +0200373 *
374 * @param[in] type Type of wanted record.
375 * @param[in] array_size Size of array.
376 * @param[in] array Searched array.
377 *
378 * @return Pointer to desired record on success, NULL if element is not in the array.
379 */
David Sedlákb4e44562019-07-04 15:42:12 +0200380static struct yin_subelement *
Radek Krejci1deb5be2020-08-26 16:43:36 +0200381get_record(enum ly_stmt type, LY_ARRAY_COUNT_TYPE array_size, struct yin_subelement *array)
David Sedlákd6e56892019-07-01 15:40:24 +0200382{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200383 for (LY_ARRAY_COUNT_TYPE u = 0; u < array_size; ++u) {
Radek Krejci12b1c152019-09-05 16:20:48 +0200384 if (array[u].type == type) {
385 return &array[u];
David Sedlákd6e56892019-07-01 15:40:24 +0200386 }
387 }
David Sedlákd6e56892019-07-01 15:40:24 +0200388 return NULL;
389}
390
David Sedlákbba38e52019-07-09 15:20:01 +0200391/**
392 * @brief Helper function to check mandatory constraint of subelement.
393 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200394 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200395 * @param[in] subelem_info Array of information about subelements.
396 * @param[in] subelem_info_size Size of subelem_info array.
397 * @param[in] current_element Identification of element that is currently being parsed, used for logging.
398 *
399 * @return LY_ERR values.
400 */
401static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100402yin_check_subelem_mandatory_constraint(struct lys_yin_parser_ctx *ctx, struct yin_subelement *subelem_info,
Radek Krejci0f969882020-08-21 16:56:47 +0200403 signed char subelem_info_size, enum ly_stmt current_element)
David Sedlák21f87cd2019-07-03 16:53:23 +0200404{
David Sedlákb0faad82019-07-04 14:28:59 +0200405 for (signed char i = 0; i < subelem_info_size; ++i) {
David Sedlák5545f5d2019-07-11 11:55:16 +0200406 /* if there is element that is mandatory and isn't parsed log error and return LY_EVALID */
Radek Krejci12b1c152019-09-05 16:20:48 +0200407 if ((subelem_info[i].flags & YIN_SUBELEM_MANDATORY) && !(subelem_info[i].flags & YIN_SUBELEM_PARSED)) {
David Sedlák1538a842019-08-08 15:38:51 +0200408 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_MAND_SUBELEM,
David Sedlák555c7202019-07-04 12:14:12 +0200409 ly_stmt2str(subelem_info[i].type), ly_stmt2str(current_element));
David Sedlák21f87cd2019-07-03 16:53:23 +0200410 return LY_EVALID;
411 }
412 }
413
414 return LY_SUCCESS;
415}
416
David Sedlákbba38e52019-07-09 15:20:01 +0200417/**
418 * @brief Helper function to check "first" constraint of subelement.
419 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200420 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200421 * @param[in] subelem_info Array of information about subelements.
422 * @param[in] subelem_info_size Size of subelem_info array.
423 * @param[in] current_element Identification of element that is currently being parsed, used for logging.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200424 * @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 +0200425 *
426 * @return LY_ERR values.
427 */
428static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100429yin_check_subelem_first_constraint(struct lys_yin_parser_ctx *ctx, struct yin_subelement *subelem_info,
Radek Krejci0f969882020-08-21 16:56:47 +0200430 signed char subelem_info_size, enum ly_stmt current_element,
431 struct yin_subelement *exp_first)
David Sedlák21f87cd2019-07-03 16:53:23 +0200432{
David Sedlákb0faad82019-07-04 14:28:59 +0200433 for (signed char i = 0; i < subelem_info_size; ++i) {
David Sedlák21f87cd2019-07-03 16:53:23 +0200434 if (subelem_info[i].flags & YIN_SUBELEM_PARSED) {
David Sedlák1538a842019-08-08 15:38:51 +0200435 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_FIRT_SUBELEM,
436 ly_stmt2str(exp_first->type), ly_stmt2str(current_element));
David Sedlák21f87cd2019-07-03 16:53:23 +0200437 return LY_EVALID;
438 }
439 }
440
441 return LY_SUCCESS;
442}
443
David Sedlákbba38e52019-07-09 15:20:01 +0200444/**
David Sedlákb4e44562019-07-04 15:42:12 +0200445 * @brief Parse simple element without any special constraints and argument mapped to yin attribute,
446 * for example prefix or namespace element.
David Sedlákd6e56892019-07-01 15:40:24 +0200447 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200448 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákb4e44562019-07-04 15:42:12 +0200449 * @param[in] kw Type of current element.
450 * @param[out] value Where value of attribute should be stored.
451 * @param[in] arg_type Expected type of attribute.
David Sedlák292763b2019-07-09 11:10:53 +0200452 * @param[in] arg_val_type Type of expected value of attribute.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200453 * @param[in,out] exts Extension instances to add to.
David Sedlákd6e56892019-07-01 15:40:24 +0200454 * @return LY_ERR values.
455 */
David Sedlákb4e44562019-07-04 15:42:12 +0200456static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100457yin_parse_simple_element(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, const char **value,
Radek Krejci0f969882020-08-21 16:56:47 +0200458 enum yin_argument arg_type, enum yang_arg arg_val_type, struct lysp_ext_instance **exts)
David Sedlákb4e44562019-07-04 15:42:12 +0200459{
Michal Vaskob36053d2020-03-26 15:49:30 +0100460 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
461 LY_CHECK_RET(yin_parse_attribute(ctx, arg_type, value, arg_val_type, kw));
David Sedlák968ac342019-07-11 15:17:59 +0200462 struct yin_subelement subelems[1] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200463 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
464 };
David Sedlákb4e44562019-07-04 15:42:12 +0200465
Michal Vaskob36053d2020-03-26 15:49:30 +0100466 return yin_parse_content(ctx, subelems, 1, kw, NULL, exts);
David Sedlákb4e44562019-07-04 15:42:12 +0200467}
468
469/**
David Sedlák6542aed2019-08-14 10:47:43 +0200470 * @brief Parse path element.
471 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200472 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák6542aed2019-08-14 10:47:43 +0200473 * @param[in] kw Type of current element.
474 * @param[out] type Type structure to store parsed value, flags and extension instances.
David Sedlák6542aed2019-08-14 10:47:43 +0200475 * @return LY_ERR values.
476 */
477static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100478yin_parse_path(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, struct lysp_type *type)
David Sedlák6542aed2019-08-14 10:47:43 +0200479{
Michal Vasko004d3152020-06-11 19:59:22 +0200480 LY_ERR ret;
481 const char *str_path;
482
483 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 +0200484 ret = ly_path_parse(ctx->xmlctx->ctx, NULL, str_path, 0, LY_PATH_BEGIN_EITHER, LY_PATH_LREF_TRUE,
Michal Vasko004d3152020-06-11 19:59:22 +0200485 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path);
486 lydict_remove(ctx->xmlctx->ctx, str_path);
487 LY_CHECK_RET(ret);
David Sedlák6542aed2019-08-14 10:47:43 +0200488 type->flags |= LYS_SET_PATH;
489
490 return LY_SUCCESS;
491}
492
493/**
David Sedlákd3983112019-07-12 11:20:56 +0200494 * @brief Parse pattern element.
495 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200496 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200497 * @param[in,out] type Type structure to store parsed value, flags and extension instances.
David Sedlákd3983112019-07-12 11:20:56 +0200498 * @return LY_ERR values.
499 */
500static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100501yin_parse_pattern(struct lys_yin_parser_ctx *ctx, struct lysp_type *type)
David Sedlákd3983112019-07-12 11:20:56 +0200502{
503 const char *real_value = NULL;
504 char *saved_value = NULL;
505 struct lysp_restr *restr;
506
Michal Vaskob36053d2020-03-26 15:49:30 +0100507 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, type->patterns, restr, LY_EMEM);
508 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
509 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 +0200510 size_t len = strlen(real_value);
511
512 saved_value = malloc(len + 2);
Michal Vaskob36053d2020-03-26 15:49:30 +0100513 LY_CHECK_ERR_RET(!saved_value, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
David Sedlákd3983112019-07-12 11:20:56 +0200514 memmove(saved_value + 1, real_value, len);
Michal Vaskob36053d2020-03-26 15:49:30 +0100515 FREE_STRING(ctx->xmlctx->ctx, real_value);
David Sedlákd3983112019-07-12 11:20:56 +0200516 saved_value[0] = 0x06;
517 saved_value[len + 1] = '\0';
Michal Vaskob36053d2020-03-26 15:49:30 +0100518 restr->arg = lydict_insert_zc(ctx->xmlctx->ctx, saved_value);
519 LY_CHECK_ERR_RET(!restr->arg, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
David Sedlákd3983112019-07-12 11:20:56 +0200520 type->flags |= LYS_SET_PATTERN;
521
522 struct yin_subelement subelems[6] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200523 {LY_STMT_DESCRIPTION, &restr->dsc, YIN_SUBELEM_UNIQUE},
524 {LY_STMT_ERROR_APP_TAG, &restr->eapptag, YIN_SUBELEM_UNIQUE},
525 {LY_STMT_ERROR_MESSAGE, &restr->emsg, YIN_SUBELEM_UNIQUE},
526 {LY_STMT_MODIFIER, &restr->arg, YIN_SUBELEM_UNIQUE},
527 {LY_STMT_REFERENCE, &restr->ref, YIN_SUBELEM_UNIQUE},
528 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
529 };
Michal Vaskob36053d2020-03-26 15:49:30 +0100530 return yin_parse_content(ctx, subelems, 6, LY_STMT_PATTERN, NULL, &restr->exts);
David Sedlákd3983112019-07-12 11:20:56 +0200531}
532
David Sedlákc5b20842019-08-13 10:18:31 +0200533/**
534 * @brief Parse fraction-digits element.
535 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200536 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákc5b20842019-08-13 10:18:31 +0200537 * @param[in,out] type Type structure to store value, flags and extension instances.
David Sedlákc5b20842019-08-13 10:18:31 +0200538 * @return LY_ERR values.
539 */
David Sedlákf75d55e2019-07-12 16:52:50 +0200540static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100541yin_parse_fracdigits(struct lys_yin_parser_ctx *ctx, struct lysp_type *type)
David Sedlákf75d55e2019-07-12 16:52:50 +0200542{
543 const char *temp_val = NULL;
544 char *ptr;
545 unsigned long int num;
546
Michal Vaskob36053d2020-03-26 15:49:30 +0100547 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
548 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 +0200549
550 if (temp_val[0] == '\0' || (temp_val[0] == '0') || !isdigit(temp_val[0])) {
David Sedlák1538a842019-08-08 15:38:51 +0200551 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "fraction-digits");
Michal Vaskob36053d2020-03-26 15:49:30 +0100552 FREE_STRING(ctx->xmlctx->ctx, temp_val);
David Sedlákf75d55e2019-07-12 16:52:50 +0200553 return LY_EVALID;
554 }
555
556 errno = 0;
557 num = strtoul(temp_val, &ptr, 10);
558 if (*ptr != '\0') {
David Sedlák1538a842019-08-08 15:38:51 +0200559 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "fraction-digits");
Michal Vaskob36053d2020-03-26 15:49:30 +0100560 FREE_STRING(ctx->xmlctx->ctx, temp_val);
David Sedlákf75d55e2019-07-12 16:52:50 +0200561 return LY_EVALID;
562 }
563 if ((errno == ERANGE) || (num > 18)) {
David Sedlák1538a842019-08-08 15:38:51 +0200564 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "fraction-digits");
Michal Vaskob36053d2020-03-26 15:49:30 +0100565 FREE_STRING(ctx->xmlctx->ctx, temp_val);
David Sedlákf75d55e2019-07-12 16:52:50 +0200566 return LY_EVALID;
567 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100568 FREE_STRING(ctx->xmlctx->ctx, temp_val);
David Sedlákf75d55e2019-07-12 16:52:50 +0200569 type->fraction_digits = num;
570 type->flags |= LYS_SET_FRDIGITS;
571 struct yin_subelement subelems[1] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200572 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
573 };
Michal Vaskob36053d2020-03-26 15:49:30 +0100574 return yin_parse_content(ctx, subelems, 1, LY_STMT_FRACTION_DIGITS, NULL, &type->exts);
David Sedlákf75d55e2019-07-12 16:52:50 +0200575}
576
David Sedlák07869a52019-07-12 14:28:19 +0200577/**
David Sedlák43801c92019-08-05 15:58:54 +0200578 * @brief Parse enum element.
David Sedlák07869a52019-07-12 14:28:19 +0200579 *
580 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200581 * @param[in,out] type Type structure to store parsed value, flags and extension instances.
David Sedlák07869a52019-07-12 14:28:19 +0200582 * @return LY_ERR values.
583 */
David Sedlákca36c422019-07-12 12:47:55 +0200584static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100585yin_parse_enum(struct lys_yin_parser_ctx *ctx, struct lysp_type *type)
David Sedlákca36c422019-07-12 12:47:55 +0200586{
David Sedlák07869a52019-07-12 14:28:19 +0200587 struct lysp_type_enum *en;
David Sedlák1e696782019-07-17 15:06:07 +0200588
Michal Vaskob36053d2020-03-26 15:49:30 +0100589 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, type->enums, en, LY_EMEM);
David Sedlák43801c92019-08-05 15:58:54 +0200590 type->flags |= LYS_SET_ENUM;
Michal Vaskob36053d2020-03-26 15:49:30 +0100591 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
592 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 +0200593 LY_CHECK_RET(lysp_check_enum_name((struct lys_parser_ctx *)ctx, en->name, strlen(en->name)));
Michal Vaskob36053d2020-03-26 15:49:30 +0100594 CHECK_NONEMPTY((struct lys_parser_ctx *)ctx, strlen(en->name), "enum");
David Sedlák43801c92019-08-05 15:58:54 +0200595 CHECK_UNIQUENESS((struct lys_parser_ctx *)ctx, type->enums, name, "enum", en->name);
David Sedlákca36c422019-07-12 12:47:55 +0200596
597 struct yin_subelement subelems[6] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200598 {LY_STMT_DESCRIPTION, &en->dsc, YIN_SUBELEM_UNIQUE},
599 {LY_STMT_IF_FEATURE, &en->iffeatures, 0},
600 {LY_STMT_REFERENCE, &en->ref, YIN_SUBELEM_UNIQUE},
601 {LY_STMT_STATUS, &en->flags, YIN_SUBELEM_UNIQUE},
602 {LY_STMT_VALUE, en, YIN_SUBELEM_UNIQUE},
603 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
604 };
Michal Vaskob36053d2020-03-26 15:49:30 +0100605 return yin_parse_content(ctx, subelems, 6, LY_STMT_ENUM, NULL, &en->exts);
David Sedlák43801c92019-08-05 15:58:54 +0200606}
607
608/**
609 * @brief Parse bit element.
610 *
611 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200612 * @param[in,out] type Type structure to store parsed value, flags and extension instances.
David Sedlák43801c92019-08-05 15:58:54 +0200613 * @return LY_ERR values.
614 */
615static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100616yin_parse_bit(struct lys_yin_parser_ctx *ctx, struct lysp_type *type)
David Sedlák43801c92019-08-05 15:58:54 +0200617{
618 struct lysp_type_enum *en;
619
Michal Vaskob36053d2020-03-26 15:49:30 +0100620 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, type->bits, en, LY_EMEM);
David Sedlák43801c92019-08-05 15:58:54 +0200621 type->flags |= LYS_SET_BIT;
Michal Vaskob36053d2020-03-26 15:49:30 +0100622 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
623 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 +0200624 CHECK_UNIQUENESS((struct lys_parser_ctx *)ctx, type->enums, name, "bit", en->name);
625
626 struct yin_subelement subelems[6] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200627 {LY_STMT_DESCRIPTION, &en->dsc, YIN_SUBELEM_UNIQUE},
628 {LY_STMT_IF_FEATURE, &en->iffeatures, 0},
629 {LY_STMT_POSITION, en, YIN_SUBELEM_UNIQUE},
630 {LY_STMT_REFERENCE, &en->ref, YIN_SUBELEM_UNIQUE},
631 {LY_STMT_STATUS, &en->flags, YIN_SUBELEM_UNIQUE},
632 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
633 };
Michal Vaskob36053d2020-03-26 15:49:30 +0100634 return yin_parse_content(ctx, subelems, 6, LY_STMT_BIT, NULL, &en->exts);
David Sedlákca36c422019-07-12 12:47:55 +0200635}
636
David Sedlákd3983112019-07-12 11:20:56 +0200637/**
David Sedlák5f8191e2019-07-08 16:35:52 +0200638 * @brief Parse simple element without any special constraints and argument mapped to yin attribute, that can have
639 * more instances, such as base or if-feature.
640 *
641 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák5f8191e2019-07-08 16:35:52 +0200642 * @param[in] kw Type of current element.
643 * @param[out] values Parsed values to add to.
644 * @param[in] arg_type Expected type of attribute.
David Sedlák292763b2019-07-09 11:10:53 +0200645 * @param[in] arg_val_type Type of expected value of attribute.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200646 * @param[in,out] exts Extension instances to add to.
David Sedlák5f8191e2019-07-08 16:35:52 +0200647 * @return LY_ERR values.
648 */
649static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100650yin_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 +0200651 enum yang_arg arg_val_type, struct lysp_ext_instance **exts)
David Sedlák5f8191e2019-07-08 16:35:52 +0200652{
653 const char **value;
Michal Vaskob36053d2020-03-26 15:49:30 +0100654 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *values, value, LY_EMEM);
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200655 LY_ARRAY_COUNT_TYPE index = LY_ARRAY_COUNT(*values) - 1;
David Sedlák968ac342019-07-11 15:17:59 +0200656 struct yin_subelement subelems[1] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200657 {LY_STMT_EXTENSION_INSTANCE, &index, 0}
658 };
David Sedlák968ac342019-07-11 15:17:59 +0200659
Michal Vaskob36053d2020-03-26 15:49:30 +0100660 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
661 LY_CHECK_RET(yin_parse_attribute(ctx, arg_type, value, arg_val_type, kw));
David Sedlák5f8191e2019-07-08 16:35:52 +0200662
Michal Vaskob36053d2020-03-26 15:49:30 +0100663 return yin_parse_content(ctx, subelems, 1, kw, NULL, exts);
David Sedlák5f8191e2019-07-08 16:35:52 +0200664}
665
666/**
David Sedlák6542aed2019-08-14 10:47:43 +0200667 * @brief Parse simple element without any special constraints and argument mapped to yin attribute.
668 *
669 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák6542aed2019-08-14 10:47:43 +0200670 * @param[in] kw Type of current element.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200671 * @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 +0200672 * @param[in] arg_type Expected type of attribute.
673 * @param[in] arg_val_type Type of expected value of attribute.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200674 * @param[in,out] exts Extension instances to add to.
David Sedlák6542aed2019-08-14 10:47:43 +0200675 * @return LY_ERR values.
676 */
677static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100678yin_parse_simple_elem(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, struct yin_subelement *subinfo,
Radek Krejci0f969882020-08-21 16:56:47 +0200679 enum yin_argument arg_type, enum yang_arg arg_val_type, struct lysp_ext_instance **exts)
David Sedlák6542aed2019-08-14 10:47:43 +0200680{
681 if (subinfo->flags & YIN_SUBELEM_UNIQUE) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100682 LY_CHECK_RET(yin_parse_simple_element(ctx, kw, (const char **)subinfo->dest,
David Sedlák6542aed2019-08-14 10:47:43 +0200683 arg_type, arg_val_type, exts));
684 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100685 LY_CHECK_RET(yin_parse_simple_elements(ctx, kw, (const char ***)subinfo->dest,
David Sedlák6542aed2019-08-14 10:47:43 +0200686 arg_type, arg_val_type, exts));
687 }
688
689 return LY_SUCCESS;
690}
691
692/**
693 * @brief Parse base element.
694 *
695 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák6542aed2019-08-14 10:47:43 +0200696 * @param[in] parent Identification of parent element.
697 * @param[out] dest Where parsed values should be stored.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200698 * @param[in,out] exts Extension instances to add to.
David Sedlák6542aed2019-08-14 10:47:43 +0200699 * @return LY_ERR values.
700 */
701static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100702yin_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 +0200703{
704 struct lysp_type *type = NULL;
705
Radek Krejcid6b76452019-09-03 17:03:03 +0200706 if (parent == LY_STMT_TYPE) {
David Sedlák6542aed2019-08-14 10:47:43 +0200707 type = (struct lysp_type *)dest;
Michal Vaskob36053d2020-03-26 15:49:30 +0100708 LY_CHECK_RET(yin_parse_simple_elements(ctx, LY_STMT_BASE, &type->bases, YIN_ARG_NAME,
David Sedlák6542aed2019-08-14 10:47:43 +0200709 Y_PREF_IDENTIF_ARG, exts));
710 type->flags |= LYS_SET_BASE;
Radek Krejcid6b76452019-09-03 17:03:03 +0200711 } else if (parent == LY_STMT_IDENTITY) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100712 LY_CHECK_RET(yin_parse_simple_elements(ctx, LY_STMT_BASE, (const char ***)dest,
David Sedlák6542aed2019-08-14 10:47:43 +0200713 YIN_ARG_NAME, Y_PREF_IDENTIF_ARG, exts));
714 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100715 LOGINT(ctx->xmlctx->ctx);
David Sedlák6542aed2019-08-14 10:47:43 +0200716 return LY_EINT;
717 }
718
719 return LY_SUCCESS;
720}
721
722/**
David Sedlákbf8a2b72019-08-14 16:48:10 +0200723 * @brief Parse require-instance element.
David Sedlákcf5569a2019-07-11 13:31:34 +0200724 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200725 * @param[in,out] ctx YIN parser context for logging and to store current state.
Michal Vaskob36053d2020-03-26 15:49:30 +0100726 * @param[out] type Type structure to store value, flag and extensions.
David Sedlákcf5569a2019-07-11 13:31:34 +0200727 * @return LY_ERR values.
728 */
729static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100730yin_pasrse_reqinstance(struct lys_yin_parser_ctx *ctx, struct lysp_type *type)
David Sedlákcf5569a2019-07-11 13:31:34 +0200731{
732 const char *temp_val = NULL;
David Sedlák968ac342019-07-11 15:17:59 +0200733 struct yin_subelement subelems[1] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200734 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
735 };
David Sedlákcf5569a2019-07-11 13:31:34 +0200736
737 type->flags |= LYS_SET_REQINST;
Michal Vaskob36053d2020-03-26 15:49:30 +0100738 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
739 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 +0200740 if (strcmp(temp_val, "true") == 0) {
741 type->require_instance = 1;
742 } else if (strcmp(temp_val, "false") != 0) {
David Sedlák26ea1432019-08-14 13:42:23 +0200743 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value",
744 "require-instance", "true", "false");
Michal Vaskob36053d2020-03-26 15:49:30 +0100745 FREE_STRING(ctx->xmlctx->ctx, temp_val);
David Sedlákcf5569a2019-07-11 13:31:34 +0200746 return LY_EVALID;
747 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100748 FREE_STRING(ctx->xmlctx->ctx, temp_val);
David Sedlákcf5569a2019-07-11 13:31:34 +0200749
Michal Vaskob36053d2020-03-26 15:49:30 +0100750 return yin_parse_content(ctx, subelems, 1, LY_STMT_REQUIRE_INSTANCE, NULL, &type->exts);
David Sedlákcf5569a2019-07-11 13:31:34 +0200751}
752
753/**
David Sedlákce77bf52019-07-11 16:59:31 +0200754 * @brief Parse modifier element.
755 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200756 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákce77bf52019-07-11 16:59:31 +0200757 * @param[in,out] pat Value to write to.
758 * @param[in,out] exts Extension instances to add to.
759 *
760 * @return LY_ERR values.
761 */
762static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100763yin_parse_modifier(struct lys_yin_parser_ctx *ctx, const char **pat, struct lysp_ext_instance **exts)
David Sedlákce77bf52019-07-11 16:59:31 +0200764{
David Sedlákd3983112019-07-12 11:20:56 +0200765 assert(**pat == 0x06);
David Sedlákce77bf52019-07-11 16:59:31 +0200766 const char *temp_val;
767 char *modified_val;
768 struct yin_subelement subelems[1] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200769 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
770 };
David Sedlákce77bf52019-07-11 16:59:31 +0200771
Michal Vaskob36053d2020-03-26 15:49:30 +0100772 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
773 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 +0200774 if (strcmp(temp_val, "invert-match") != 0) {
David Sedlák26ea1432019-08-14 13:42:23 +0200775 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS1, temp_val, "value",
776 "modifier", "invert-match");
Michal Vaskob36053d2020-03-26 15:49:30 +0100777 FREE_STRING(ctx->xmlctx->ctx, temp_val);
David Sedlákce77bf52019-07-11 16:59:31 +0200778 return LY_EVALID;
779 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100780 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákce77bf52019-07-11 16:59:31 +0200781
782 /* allocate new value */
David Sedlákd3983112019-07-12 11:20:56 +0200783 modified_val = malloc(strlen(*pat) + 1);
Michal Vaskob36053d2020-03-26 15:49:30 +0100784 LY_CHECK_ERR_RET(!modified_val, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
David Sedlákd3983112019-07-12 11:20:56 +0200785 strcpy(modified_val, *pat);
Michal Vaskob36053d2020-03-26 15:49:30 +0100786 lydict_remove(ctx->xmlctx->ctx, *pat);
David Sedlákce77bf52019-07-11 16:59:31 +0200787
788 /* modify the new value */
789 modified_val[0] = 0x15;
Michal Vaskob36053d2020-03-26 15:49:30 +0100790 *pat = lydict_insert_zc(ctx->xmlctx->ctx, modified_val);
David Sedlákce77bf52019-07-11 16:59:31 +0200791
Michal Vaskob36053d2020-03-26 15:49:30 +0100792 return yin_parse_content(ctx, subelems, 1, LY_STMT_MODIFIER, NULL, exts);
David Sedlákce77bf52019-07-11 16:59:31 +0200793}
794
795/**
David Sedlákb7296dd2019-07-11 14:58:38 +0200796 * @brief Parse a restriction element (length, range or one instance of must).
797 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200798 * @param[in,out] ctx YIN parser context for logging and to store current state.
Radek Krejcid6b76452019-09-03 17:03:03 +0200799 * @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 +0200800 * @param[in] restr Value to write to.
David Sedlákb7296dd2019-07-11 14:58:38 +0200801 */
802static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100803yin_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 +0200804{
Radek Krejcid6b76452019-09-03 17:03:03 +0200805 assert(restr_kw == LY_STMT_MUST || restr_kw == LY_STMT_LENGTH || restr_kw == LY_STMT_RANGE);
David Sedlákb7296dd2019-07-11 14:58:38 +0200806 struct yin_subelement subelems[5] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200807 {LY_STMT_DESCRIPTION, &restr->dsc, YIN_SUBELEM_UNIQUE},
808 {LY_STMT_ERROR_APP_TAG, &restr->eapptag, YIN_SUBELEM_UNIQUE},
809 {LY_STMT_ERROR_MESSAGE, &restr->emsg, YIN_SUBELEM_UNIQUE},
810 {LY_STMT_REFERENCE, &restr->ref, YIN_SUBELEM_UNIQUE},
811 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
812 };
David Sedlákb7296dd2019-07-11 14:58:38 +0200813 /* argument of must is called condition, but argument of length and range is called value */
Radek Krejcid6b76452019-09-03 17:03:03 +0200814 enum yin_argument arg_type = (restr_kw == LY_STMT_MUST) ? YIN_ARG_CONDITION : YIN_ARG_VALUE;
David Sedlákb7296dd2019-07-11 14:58:38 +0200815
Michal Vaskob36053d2020-03-26 15:49:30 +0100816 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
817 LY_CHECK_RET(yin_parse_attribute(ctx, arg_type, &restr->arg, Y_STR_ARG, restr_kw));
818
819 return yin_parse_content(ctx, subelems, 5, restr_kw, NULL, &restr->exts);
David Sedlákb7296dd2019-07-11 14:58:38 +0200820}
821
822/**
David Sedlák6542aed2019-08-14 10:47:43 +0200823 * @brief Parse range element.
824 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200825 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák6542aed2019-08-14 10:47:43 +0200826 * @param[out] type Type structure to store parsed value and flags.
827 *
828 * @return LY_ERR values.
829 */
830static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100831yin_parse_range(struct lys_yin_parser_ctx *ctx, struct lysp_type *type)
David Sedlák6542aed2019-08-14 10:47:43 +0200832{
833 type->range = calloc(1, sizeof *type->range);
Michal Vaskob36053d2020-03-26 15:49:30 +0100834 LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
835 LY_CHECK_RET(yin_parse_restriction(ctx, LY_STMT_RANGE, type->range));
Michal Vasko44f3d2c2020-08-24 09:49:38 +0200836 type->flags |= LYS_SET_RANGE;
David Sedlák6542aed2019-08-14 10:47:43 +0200837
838 return LY_SUCCESS;
839}
840
841/**
842 * @brief Parse length element.
843 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200844 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák6542aed2019-08-14 10:47:43 +0200845 * @param[out] type Type structure to store parsed value and flags.
846 *
847 * @return LY_ERR values.
848 */
849static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100850yin_parse_length(struct lys_yin_parser_ctx *ctx, struct lysp_type *type)
David Sedlák6542aed2019-08-14 10:47:43 +0200851{
852 type->length = calloc(1, sizeof *type->length);
Michal Vaskob36053d2020-03-26 15:49:30 +0100853 LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
854 LY_CHECK_RET(yin_parse_restriction(ctx, LY_STMT_LENGTH, type->length));
David Sedlák6542aed2019-08-14 10:47:43 +0200855 type->flags |= LYS_SET_LENGTH;
856
857 return LY_SUCCESS;
858}
859
860/**
David Sedlákbc9ec9c2019-07-11 15:53:55 +0200861 * @brief Parse must element.
862 *
863 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbc9ec9c2019-07-11 15:53:55 +0200864 * @param[in,out] restrs Restrictions to add to.
865 *
866 * @return LY_ERR values.
867 */
868static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100869yin_parse_must(struct lys_yin_parser_ctx *ctx, struct lysp_restr **restrs)
David Sedlákbc9ec9c2019-07-11 15:53:55 +0200870{
871 struct lysp_restr *restr;
872
Michal Vaskob36053d2020-03-26 15:49:30 +0100873 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *restrs, restr, LY_EMEM);
874 return yin_parse_restriction(ctx, LY_STMT_MUST, restr);
David Sedlákbc9ec9c2019-07-11 15:53:55 +0200875}
876
877/**
David Sedlák5545f5d2019-07-11 11:55:16 +0200878 * @brief Parse position or value element.
879 *
880 * @param[in,out] ctx YIN parser context for logging and to store current state.
Radek Krejcid6b76452019-09-03 17:03:03 +0200881 * @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 +0200882 * @param[out] enm Enum structure to save value, flags and extension instances.
David Sedlák5545f5d2019-07-11 11:55:16 +0200883 *
884 * @return LY_ERR values.
885 */
886static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100887yin_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 +0200888{
Radek Krejcid6b76452019-09-03 17:03:03 +0200889 assert(kw == LY_STMT_POSITION || kw == LY_STMT_VALUE);
David Sedlák5545f5d2019-07-11 11:55:16 +0200890 const char *temp_val = NULL;
891 char *ptr;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +0200892 long int num = 0;
893 unsigned long int unum = 0;
David Sedlák5545f5d2019-07-11 11:55:16 +0200894
895 /* set value flag */
896 enm->flags |= LYS_SET_VALUE;
897
898 /* get attribute value */
Michal Vaskob36053d2020-03-26 15:49:30 +0100899 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
900 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, kw));
David Sedlákae5378f2019-07-17 11:37:59 +0200901 if (!temp_val || temp_val[0] == '\0' || (temp_val[0] == '+') ||
Radek Krejci0f969882020-08-21 16:56:47 +0200902 ((temp_val[0] == '0') && (temp_val[1] != '\0')) || ((kw == LY_STMT_POSITION) && !strcmp(temp_val, "-0"))) {
David Sedlák1538a842019-08-08 15:38:51 +0200903 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 +0200904 goto error;
905 }
906
907 /* convert value */
908 errno = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +0200909 if (kw == LY_STMT_VALUE) {
David Sedlák5545f5d2019-07-11 11:55:16 +0200910 num = strtol(temp_val, &ptr, 10);
911 if (num < INT64_C(-2147483648) || num > INT64_C(2147483647)) {
David Sedlák1538a842019-08-08 15:38:51 +0200912 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 +0200913 goto error;
914 }
915 } else {
916 unum = strtoul(temp_val, &ptr, 10);
917 if (unum > UINT64_C(4294967295)) {
David Sedlák1538a842019-08-08 15:38:51 +0200918 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 +0200919 goto error;
920 }
921 }
922 /* check if whole argument value was converted */
923 if (*ptr != '\0') {
David Sedlák1538a842019-08-08 15:38:51 +0200924 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 +0200925 goto error;
David Sedlák5545f5d2019-07-11 11:55:16 +0200926 }
927 if (errno == ERANGE) {
David Sedlák1538a842019-08-08 15:38:51 +0200928 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 +0200929 goto error;
930 }
931 /* save correctly ternary operator can't be used because num and unum have different signes */
Radek Krejcid6b76452019-09-03 17:03:03 +0200932 if (kw == LY_STMT_VALUE) {
David Sedlák5545f5d2019-07-11 11:55:16 +0200933 enm->value = num;
934 } else {
935 enm->value = unum;
936 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100937 FREE_STRING(ctx->xmlctx->ctx, temp_val);
David Sedlák5545f5d2019-07-11 11:55:16 +0200938
939 /* parse subelements */
David Sedlák968ac342019-07-11 15:17:59 +0200940 struct yin_subelement subelems[1] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200941 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
942 };
Michal Vaskob36053d2020-03-26 15:49:30 +0100943 return yin_parse_content(ctx, subelems, 1, kw, NULL, &enm->exts);
David Sedlák5545f5d2019-07-11 11:55:16 +0200944
David Sedlákbf8a2b72019-08-14 16:48:10 +0200945error:
Michal Vaskob36053d2020-03-26 15:49:30 +0100946 FREE_STRING(ctx->xmlctx->ctx, temp_val);
947 return LY_EVALID;
David Sedlák5545f5d2019-07-11 11:55:16 +0200948}
949
David Sedlák05404f62019-07-24 14:11:53 +0200950/**
951 * @brief Parse belongs-to element.
952 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200953 * @param[in] ctx YIN parser context for logging and to store current state.
David Sedlák05404f62019-07-24 14:11:53 +0200954 * @param[out] submod Structure of submodule that is being parsed.
955 * @param[in,out] exts Extension instances to add to.
956 *
957 * @return LY_ERR values
958 */
959static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100960yin_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 +0200961{
962 struct yin_subelement subelems[2] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200963 {LY_STMT_PREFIX, &submod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
964 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
965 };
David Sedlák05404f62019-07-24 14:11:53 +0200966
Michal Vaskob36053d2020-03-26 15:49:30 +0100967 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
968 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_MODULE, &submod->belongsto, Y_IDENTIF_ARG, LY_STMT_BELONGS_TO));
969
970 return yin_parse_content(ctx, subelems, 2, LY_STMT_BELONGS_TO, NULL, exts);
David Sedlák05404f62019-07-24 14:11:53 +0200971}
972
David Sedlák5545f5d2019-07-11 11:55:16 +0200973/**
David Sedlákc1771b12019-07-10 15:55:46 +0200974 * @brief Function to parse meta tags (description, contact, ...) eg. elements with
David Sedlákbf8a2b72019-08-14 16:48:10 +0200975 * text element as child.
David Sedlákb4e44562019-07-04 15:42:12 +0200976 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200977 * @param[in,out] ctx YIN parser context for logging and to store current state.
Radek Krejcid6b76452019-09-03 17:03:03 +0200978 * @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 +0200979 * @param[out] value Where the content of meta element should be stored.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200980 * @param[in,out] exts Extension instances to add to.
David Sedlákb4e44562019-07-04 15:42:12 +0200981 *
982 * @return LY_ERR values.
983 */
984static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100985yin_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 +0200986{
Radek Krejcid6b76452019-09-03 17:03:03 +0200987 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 +0200988
David Sedlák968ac342019-07-11 15:17:59 +0200989 struct yin_subelement subelems[2] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200990 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
991 {LY_STMT_ARG_TEXT, value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE | YIN_SUBELEM_FIRST}
992 };
David Sedlákdf2a9732019-08-07 13:23:16 +0200993 /* check attributes */
Michal Vaskob36053d2020-03-26 15:49:30 +0100994 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
995 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 +0200996
David Sedlákdf2a9732019-08-07 13:23:16 +0200997 /* parse content */
Michal Vaskob36053d2020-03-26 15:49:30 +0100998 return yin_parse_content(ctx, subelems, 2, elem_type, NULL, exts);
David Sedlákb4e44562019-07-04 15:42:12 +0200999}
1000
1001/**
David Sedlákc1771b12019-07-10 15:55:46 +02001002 * @brief Parse error-message element.
1003 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001004 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákc1771b12019-07-10 15:55:46 +02001005 * @param[out] value Where the content of error-message element should be stored.
David Sedlákbf8a2b72019-08-14 16:48:10 +02001006 * @param[in,out] exts Extension instances to add to.
David Sedlákc1771b12019-07-10 15:55:46 +02001007 *
1008 * @return LY_ERR values.
1009 */
1010static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001011yin_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 +02001012{
David Sedlák968ac342019-07-11 15:17:59 +02001013 struct yin_subelement subelems[2] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001014 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1015 {LY_STMT_ARG_VALUE, value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE | YIN_SUBELEM_FIRST}
1016 };
David Sedlákc1771b12019-07-10 15:55:46 +02001017
David Sedlákdf2a9732019-08-07 13:23:16 +02001018 /* check attributes */
Michal Vaskob36053d2020-03-26 15:49:30 +01001019 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1020 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 +02001021
Michal Vaskob36053d2020-03-26 15:49:30 +01001022 return yin_parse_content(ctx, subelems, 2, LY_STMT_ERROR_MESSAGE, NULL, exts);
David Sedlákc1771b12019-07-10 15:55:46 +02001023}
1024
1025/**
David Sedlák6542aed2019-08-14 10:47:43 +02001026 * @brief Parse type element.
David Sedlák374d2b32019-07-17 15:06:55 +02001027 *
1028 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák6542aed2019-08-14 10:47:43 +02001029 * @param[in] parent Identification of parent element.
David Sedlákbf8a2b72019-08-14 16:48:10 +02001030 * @param[in,out] type Type to write to.
David Sedlák374d2b32019-07-17 15:06:55 +02001031 *
1032 * @return LY_ERR values.
1033 */
1034static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001035yin_parse_type(struct lys_yin_parser_ctx *ctx, enum ly_stmt parent, struct yin_subelement *subinfo)
David Sedlák374d2b32019-07-17 15:06:55 +02001036{
David Sedlák6542aed2019-08-14 10:47:43 +02001037 struct lysp_type *type = NULL;
Radek Krejcid6b76452019-09-03 17:03:03 +02001038 if (parent == LY_STMT_DEVIATE) {
David Sedlák6542aed2019-08-14 10:47:43 +02001039 *(struct lysp_type **)subinfo->dest = calloc(1, sizeof **(struct lysp_type **)subinfo->dest);
Michal Vaskob36053d2020-03-26 15:49:30 +01001040 LY_CHECK_ERR_RET(!(*(struct lysp_type **)subinfo->dest), LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
David Sedlák6542aed2019-08-14 10:47:43 +02001041 type = *((struct lysp_type **)subinfo->dest);
Michal Vaskod989ba02020-08-24 10:59:24 +02001042 } else {
David Sedlák6542aed2019-08-14 10:47:43 +02001043 type = (struct lysp_type *)subinfo->dest;
1044 }
1045 /* type as child of another type */
Radek Krejcid6b76452019-09-03 17:03:03 +02001046 if (parent == LY_STMT_TYPE) {
David Sedlák6542aed2019-08-14 10:47:43 +02001047 struct lysp_type *nested_type = NULL;
Michal Vaskob36053d2020-03-26 15:49:30 +01001048 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, type->types, nested_type, LY_EMEM);
David Sedlák6542aed2019-08-14 10:47:43 +02001049 type->flags |= LYS_SET_TYPE;
1050 type = nested_type;
1051 }
David Sedlák374d2b32019-07-17 15:06:55 +02001052 struct yin_subelement subelems[11] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001053 {LY_STMT_BASE, type, 0},
1054 {LY_STMT_BIT, type, 0},
1055 {LY_STMT_ENUM, type, 0},
1056 {LY_STMT_FRACTION_DIGITS, type, YIN_SUBELEM_UNIQUE},
1057 {LY_STMT_LENGTH, type, YIN_SUBELEM_UNIQUE},
1058 {LY_STMT_PATH, type, YIN_SUBELEM_UNIQUE},
1059 {LY_STMT_PATTERN, type, 0},
1060 {LY_STMT_RANGE, type, YIN_SUBELEM_UNIQUE},
1061 {LY_STMT_REQUIRE_INSTANCE, type, YIN_SUBELEM_UNIQUE},
1062 {LY_STMT_TYPE, type},
1063 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1064 };
Michal Vaskob36053d2020-03-26 15:49:30 +01001065
1066 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1067 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &type->name, Y_PREF_IDENTIF_ARG, LY_STMT_TYPE));
1068 return yin_parse_content(ctx, subelems, 11, LY_STMT_TYPE, NULL, &type->exts);
David Sedlák374d2b32019-07-17 15:06:55 +02001069}
1070
David Sedlák1af868e2019-07-17 17:03:14 +02001071/**
1072 * @brief Parse max-elements element.
1073 *
1074 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák1af868e2019-07-17 17:03:14 +02001075 * @param[in,out] max Value to write to.
David Sedláka2dad212019-07-18 12:45:19 +02001076 * @param[in] flags Flags to write to.
David Sedlák1af868e2019-07-17 17:03:14 +02001077 * @param[in,out] exts Extension instances to add to.
1078 *
1079 * @return LY_ERR values.
1080 */
1081static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001082yin_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 +02001083{
1084 const char *temp_val = NULL;
1085 char *ptr;
1086 unsigned long int num;
1087 struct yin_subelement subelems[1] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001088 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1089 };
David Sedlák374d2b32019-07-17 15:06:55 +02001090
David Sedlák1af868e2019-07-17 17:03:14 +02001091 *flags |= LYS_SET_MAX;
Michal Vaskob36053d2020-03-26 15:49:30 +01001092 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1093 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_MAX_ELEMENTS));
David Sedlák1af868e2019-07-17 17:03:14 +02001094 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 +02001095 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "max-elements");
Michal Vaskob36053d2020-03-26 15:49:30 +01001096 FREE_STRING(ctx->xmlctx->ctx, temp_val);
David Sedlák1af868e2019-07-17 17:03:14 +02001097 return LY_EVALID;
1098 }
1099
1100 if (strcmp(temp_val, "unbounded")) {
1101 errno = 0;
1102 num = strtoul(temp_val, &ptr, 10);
1103 if (*ptr != '\0') {
David Sedlák1538a842019-08-08 15:38:51 +02001104 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "max-elements");
Michal Vaskob36053d2020-03-26 15:49:30 +01001105 FREE_STRING(ctx->xmlctx->ctx, temp_val);
David Sedlák1af868e2019-07-17 17:03:14 +02001106 return LY_EVALID;
1107 }
1108 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlák1538a842019-08-08 15:38:51 +02001109 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_OOB_YIN, temp_val, "value", "max-elements");
Michal Vaskob36053d2020-03-26 15:49:30 +01001110 FREE_STRING(ctx->xmlctx->ctx, temp_val);
David Sedlák1af868e2019-07-17 17:03:14 +02001111 return LY_EVALID;
1112 }
1113 *max = num;
1114 }
Michal Vaskob36053d2020-03-26 15:49:30 +01001115 FREE_STRING(ctx->xmlctx->ctx, temp_val);
1116 return yin_parse_content(ctx, subelems, 1, LY_STMT_MAX_ELEMENTS, NULL, exts);
David Sedlák1af868e2019-07-17 17:03:14 +02001117}
David Sedlák374d2b32019-07-17 15:06:55 +02001118
1119/**
David Sedlákbf8a2b72019-08-14 16:48:10 +02001120 * @brief Parse min-elements element.
David Sedlák09e18c92019-07-18 11:17:11 +02001121 *
1122 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák09e18c92019-07-18 11:17:11 +02001123 * @param[in,out] min Value to write to.
David Sedláka2dad212019-07-18 12:45:19 +02001124 * @param[in] flags Flags to write to.
David Sedlák09e18c92019-07-18 11:17:11 +02001125 * @param[in,out] exts Extension instances to add to.
1126 *
1127 * @return LY_ERR values.
1128 */
1129static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001130yin_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 +02001131{
1132 const char *temp_val = NULL;
1133 char *ptr;
1134 unsigned long int num;
1135 struct yin_subelement subelems[1] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001136 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1137 };
David Sedlák09e18c92019-07-18 11:17:11 +02001138
1139 *flags |= LYS_SET_MIN;
Michal Vaskob36053d2020-03-26 15:49:30 +01001140 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1141 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 +02001142
1143 if (!temp_val || temp_val[0] == '\0' || (temp_val[0] == '0' && temp_val[1] != '\0')) {
David Sedlák1538a842019-08-08 15:38:51 +02001144 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "min-elements");
Michal Vaskob36053d2020-03-26 15:49:30 +01001145 FREE_STRING(ctx->xmlctx->ctx, temp_val);
David Sedlák09e18c92019-07-18 11:17:11 +02001146 return LY_EVALID;
1147 }
1148
1149 errno = 0;
1150 num = strtoul(temp_val, &ptr, 10);
1151 if (ptr[0] != 0) {
David Sedlák1538a842019-08-08 15:38:51 +02001152 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "min-elements");
Michal Vaskob36053d2020-03-26 15:49:30 +01001153 FREE_STRING(ctx->xmlctx->ctx, temp_val);
David Sedlák09e18c92019-07-18 11:17:11 +02001154 return LY_EVALID;
1155 }
1156 if (errno == ERANGE || num > UINT32_MAX) {
David Sedlák1538a842019-08-08 15:38:51 +02001157 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_OOB_YIN, temp_val, "value", "min-elements");
Michal Vaskob36053d2020-03-26 15:49:30 +01001158 FREE_STRING(ctx->xmlctx->ctx, temp_val);
David Sedlák09e18c92019-07-18 11:17:11 +02001159 return LY_EVALID;
1160 }
1161 *min = num;
Michal Vaskob36053d2020-03-26 15:49:30 +01001162 FREE_STRING(ctx->xmlctx->ctx, temp_val);
1163 return yin_parse_content(ctx, subelems, 1, LY_STMT_MIN_ELEMENTS, NULL, exts);
David Sedlák09e18c92019-07-18 11:17:11 +02001164}
1165
David Sedláka2dad212019-07-18 12:45:19 +02001166/**
1167 * @brief Parse min-elements or max-elements element.
1168 *
1169 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedláka2dad212019-07-18 12:45:19 +02001170 * @param[in] parent Identification of parent element.
1171 * @param[in] current Identification of current element.
1172 * @param[in] dest Where the parsed value and flags should be stored.
1173 *
1174 * @return LY_ERR values.
1175 */
David Sedlák09e18c92019-07-18 11:17:11 +02001176static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001177yin_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 +02001178{
Radek Krejcid6b76452019-09-03 17:03:03 +02001179 assert(current == LY_STMT_MAX_ELEMENTS || current == LY_STMT_MIN_ELEMENTS);
1180 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 +02001181 uint32_t *lim;
1182 uint16_t *flags;
1183 struct lysp_ext_instance **exts;
1184
Radek Krejcid6b76452019-09-03 17:03:03 +02001185 if (parent == LY_STMT_LEAF_LIST) {
1186 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 +02001187 flags = &((struct lysp_node_leaflist *)dest)->flags;
1188 exts = &((struct lysp_node_leaflist *)dest)->exts;
Radek Krejcid6b76452019-09-03 17:03:03 +02001189 } else if (parent == LY_STMT_REFINE) {
1190 lim = (current == LY_STMT_MAX_ELEMENTS) ? &((struct lysp_refine *)dest)->max : &((struct lysp_refine *)dest)->min;
David Sedlák09e18c92019-07-18 11:17:11 +02001191 flags = &((struct lysp_refine *)dest)->flags;
1192 exts = &((struct lysp_refine *)dest)->exts;
Radek Krejcid6b76452019-09-03 17:03:03 +02001193 } else if (parent == LY_STMT_LIST) {
1194 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 +02001195 flags = &((struct lysp_node_list *)dest)->flags;
1196 exts = &((struct lysp_node_list *)dest)->exts;
David Sedlák4ffcec82019-07-25 15:10:21 +02001197 } else {
1198 lim = ((struct minmax_dev_meta *)dest)->lim;
1199 flags = ((struct minmax_dev_meta *)dest)->flags;
1200 exts = ((struct minmax_dev_meta *)dest)->exts;
David Sedlák09e18c92019-07-18 11:17:11 +02001201 }
1202
Radek Krejcid6b76452019-09-03 17:03:03 +02001203 if (current == LY_STMT_MAX_ELEMENTS) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001204 LY_CHECK_RET(yin_parse_maxelements(ctx, lim, flags, exts));
David Sedlák09e18c92019-07-18 11:17:11 +02001205 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +01001206 LY_CHECK_RET(yin_parse_minelements(ctx, lim, flags, exts));
David Sedlák09e18c92019-07-18 11:17:11 +02001207 }
1208
1209 return LY_SUCCESS;
1210}
1211
1212/**
David Sedlákbf8a2b72019-08-14 16:48:10 +02001213 * @brief Parse ordered-by element.
David Sedláka2dad212019-07-18 12:45:19 +02001214 *
1215 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedláka2dad212019-07-18 12:45:19 +02001216 * @param[out] flags Flags to write to.
David Sedlákbf8a2b72019-08-14 16:48:10 +02001217 * @param[in,out] exts Extension instances to add to.
David Sedláka2dad212019-07-18 12:45:19 +02001218 *
1219 * @return LY_ERR values.
1220 */
1221static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001222yin_parse_orderedby(struct lys_yin_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
David Sedláka2dad212019-07-18 12:45:19 +02001223{
1224 const char *temp_val;
1225 struct yin_subelement subelems[1] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001226 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1227 };
David Sedláka2dad212019-07-18 12:45:19 +02001228
Michal Vaskob36053d2020-03-26 15:49:30 +01001229 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1230 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_ORDERED_BY));
David Sedláka2dad212019-07-18 12:45:19 +02001231 if (strcmp(temp_val, "system") == 0) {
1232 *flags |= LYS_ORDBY_SYSTEM;
1233 } else if (strcmp(temp_val, "user") == 0) {
1234 *flags |= LYS_ORDBY_USER;
1235 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02001236 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value",
1237 "ordered-by", "system", "user");
Michal Vaskob36053d2020-03-26 15:49:30 +01001238 FREE_STRING(ctx->xmlctx->ctx, temp_val);
David Sedláka2dad212019-07-18 12:45:19 +02001239 return LY_EVALID;
1240 }
Michal Vaskob36053d2020-03-26 15:49:30 +01001241 FREE_STRING(ctx->xmlctx->ctx, temp_val);
David Sedláka2dad212019-07-18 12:45:19 +02001242
Michal Vaskob36053d2020-03-26 15:49:30 +01001243 return yin_parse_content(ctx, subelems, 1, LY_STMT_ORDERED_BY, NULL, exts);
David Sedláka2dad212019-07-18 12:45:19 +02001244}
1245
1246/**
David Sedlákbf8a2b72019-08-14 16:48:10 +02001247 * @brief Parse any-data or any-xml element.
David Sedlák8a83bbb2019-07-18 14:46:00 +02001248 *
1249 * @param[in,out] ctx YIN parser context for logging and to store current state.
Radek Krejcid6b76452019-09-03 17:03:03 +02001250 * @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 +02001251 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlák8a83bbb2019-07-18 14:46:00 +02001252 *
1253 * @return LY_ERR values.
1254 */
1255static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001256yin_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 +02001257{
David Sedlák8a83bbb2019-07-18 14:46:00 +02001258 struct lysp_node_anydata *any;
1259
David Sedlák8d552d62019-08-06 15:29:05 +02001260 /* create new sibling */
Michal Vaskob36053d2020-03-26 15:49:30 +01001261 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, any, next, LY_EMEM);
Radek Krejcid6b76452019-09-03 17:03:03 +02001262 any->nodetype = (any_kw == LY_STMT_ANYDATA) ? LYS_ANYDATA : LYS_ANYXML;
David Sedlák8a83bbb2019-07-18 14:46:00 +02001263 any->parent = node_meta->parent;
1264
David Sedlákbf8a2b72019-08-14 16:48:10 +02001265 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001266 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1267 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 +02001268
1269 struct yin_subelement subelems[9] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001270 {LY_STMT_CONFIG, &any->flags, YIN_SUBELEM_UNIQUE},
1271 {LY_STMT_DESCRIPTION, &any->dsc, YIN_SUBELEM_UNIQUE},
1272 {LY_STMT_IF_FEATURE, &any->iffeatures, 0},
1273 {LY_STMT_MANDATORY, &any->flags, YIN_SUBELEM_UNIQUE},
1274 {LY_STMT_MUST, &any->musts, 0},
1275 {LY_STMT_REFERENCE, &any->ref, YIN_SUBELEM_UNIQUE},
1276 {LY_STMT_STATUS, &any->flags, YIN_SUBELEM_UNIQUE},
1277 {LY_STMT_WHEN, &any->when, YIN_SUBELEM_UNIQUE},
1278 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1279 };
Michal Vaskob36053d2020-03-26 15:49:30 +01001280 return yin_parse_content(ctx, subelems, 9, any_kw, NULL, &any->exts);
David Sedlák8a83bbb2019-07-18 14:46:00 +02001281}
1282
1283/**
David Sedlák203ca3a2019-07-18 15:26:25 +02001284 * @brief parse leaf element.
1285 *
1286 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákad83cf92019-08-13 12:53:53 +02001287 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlák203ca3a2019-07-18 15:26:25 +02001288 *
1289 * @return LY_ERR values.
1290 */
1291static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001292yin_parse_leaf(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta)
David Sedlák203ca3a2019-07-18 15:26:25 +02001293{
David Sedlák203ca3a2019-07-18 15:26:25 +02001294 struct lysp_node_leaf *leaf;
1295
David Sedlák8d552d62019-08-06 15:29:05 +02001296 /* create structure new leaf */
Michal Vaskob36053d2020-03-26 15:49:30 +01001297 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, leaf, next, LY_EMEM);
David Sedlák203ca3a2019-07-18 15:26:25 +02001298 leaf->nodetype = LYS_LEAF;
1299 leaf->parent = node_meta->parent;
1300
David Sedlák203ca3a2019-07-18 15:26:25 +02001301 /* parser argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001302 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1303 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 +02001304
1305 /* parse content */
1306 struct yin_subelement subelems[12] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001307 {LY_STMT_CONFIG, &leaf->flags, YIN_SUBELEM_UNIQUE},
1308 {LY_STMT_DEFAULT, &leaf->dflt, YIN_SUBELEM_UNIQUE},
1309 {LY_STMT_DESCRIPTION, &leaf->dsc, YIN_SUBELEM_UNIQUE},
1310 {LY_STMT_IF_FEATURE, &leaf->iffeatures, 0},
1311 {LY_STMT_MANDATORY, &leaf->flags, YIN_SUBELEM_UNIQUE},
1312 {LY_STMT_MUST, &leaf->musts, 0},
1313 {LY_STMT_REFERENCE, &leaf->ref, YIN_SUBELEM_UNIQUE},
1314 {LY_STMT_STATUS, &leaf->flags, YIN_SUBELEM_UNIQUE},
1315 {LY_STMT_TYPE, &leaf->type, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_MANDATORY},
1316 {LY_STMT_UNITS, &leaf->units, YIN_SUBELEM_UNIQUE},
1317 {LY_STMT_WHEN, &leaf->when, YIN_SUBELEM_UNIQUE},
1318 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1319 };
Michal Vaskob36053d2020-03-26 15:49:30 +01001320 return yin_parse_content(ctx, subelems, 12, LY_STMT_LEAF, NULL, &leaf->exts);
David Sedlák203ca3a2019-07-18 15:26:25 +02001321}
1322
1323/**
David Sedlákc3da3ef2019-07-19 12:56:08 +02001324 * @brief Parse leaf-list element.
1325 *
1326 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákad83cf92019-08-13 12:53:53 +02001327 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlákc3da3ef2019-07-19 12:56:08 +02001328 *
1329 * @return LY_ERR values.
1330 */
1331static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001332yin_parse_leaflist(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta)
David Sedlákc3da3ef2019-07-19 12:56:08 +02001333{
David Sedlákc3da3ef2019-07-19 12:56:08 +02001334 struct lysp_node_leaflist *llist;
David Sedlákc3da3ef2019-07-19 12:56:08 +02001335
Michal Vaskob36053d2020-03-26 15:49:30 +01001336 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, llist, next, LY_EMEM);
David Sedlák8d552d62019-08-06 15:29:05 +02001337
David Sedlákc3da3ef2019-07-19 12:56:08 +02001338 llist->nodetype = LYS_LEAFLIST;
1339 llist->parent = node_meta->parent;
1340
David Sedlákc3da3ef2019-07-19 12:56:08 +02001341 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001342 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1343 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 +02001344
1345 /* parse content */
1346 struct yin_subelement subelems[14] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001347 {LY_STMT_CONFIG, &llist->flags, YIN_SUBELEM_UNIQUE},
1348 {LY_STMT_DEFAULT, &llist->dflts, 0},
1349 {LY_STMT_DESCRIPTION, &llist->dsc, YIN_SUBELEM_UNIQUE},
1350 {LY_STMT_IF_FEATURE, &llist->iffeatures, 0},
1351 {LY_STMT_MAX_ELEMENTS, llist, YIN_SUBELEM_UNIQUE},
1352 {LY_STMT_MIN_ELEMENTS, llist, YIN_SUBELEM_UNIQUE},
1353 {LY_STMT_MUST, &llist->musts, 0},
1354 {LY_STMT_ORDERED_BY, &llist->flags, YIN_SUBELEM_UNIQUE},
1355 {LY_STMT_REFERENCE, &llist->ref, YIN_SUBELEM_UNIQUE},
1356 {LY_STMT_STATUS, &llist->flags, YIN_SUBELEM_UNIQUE},
1357 {LY_STMT_TYPE, &llist->type, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_MANDATORY},
1358 {LY_STMT_UNITS, &llist->units, YIN_SUBELEM_UNIQUE},
1359 {LY_STMT_WHEN, &llist->when, YIN_SUBELEM_UNIQUE},
1360 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1361 };
Michal Vaskob36053d2020-03-26 15:49:30 +01001362 LY_CHECK_RET(yin_parse_content(ctx, subelems, 14, LY_STMT_LEAF_LIST, NULL, &llist->exts));
David Sedlákc3da3ef2019-07-19 12:56:08 +02001363
David Sedlákbf8a2b72019-08-14 16:48:10 +02001364 /* check invalid combination of subelements */
David Sedlákc3da3ef2019-07-19 12:56:08 +02001365 if ((llist->min) && (llist->dflts)) {
David Sedlák1538a842019-08-08 15:38:51 +02001366 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INCHILDSTMSCOMB_YIN, "min-elements", "default", "leaf-list");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001367 return LY_EVALID;
1368 }
1369 if (llist->max && llist->min > llist->max) {
David Sedlák1538a842019-08-08 15:38:51 +02001370 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_MINMAX, llist->min, llist->max);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001371 return LY_EVALID;
1372 }
1373
1374 return LY_SUCCESS;
1375}
1376
1377/**
David Sedlák04e17b22019-07-19 15:29:48 +02001378 * @brief Parse typedef element.
1379 *
1380 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák05404f62019-07-24 14:11:53 +02001381 * @param[in] typedef_meta Meta information about parent node and typedefs to add to.
David Sedlák04e17b22019-07-19 15:29:48 +02001382 *
1383 * @return LY_ERR values.
1384 */
1385static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001386yin_parse_typedef(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *typedef_meta)
David Sedlák04e17b22019-07-19 15:29:48 +02001387{
1388 struct lysp_tpdf *tpdf;
David Sedlákbf8a2b72019-08-14 16:48:10 +02001389 struct lysp_tpdf **tpdfs = (struct lysp_tpdf **)typedef_meta->nodes;
Michal Vaskob36053d2020-03-26 15:49:30 +01001390 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *tpdfs, tpdf, LY_EMEM);
David Sedlák04e17b22019-07-19 15:29:48 +02001391
1392 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001393 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1394 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 +02001395
1396 /* parse content */
1397 struct yin_subelement subelems[7] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001398 {LY_STMT_DEFAULT, &tpdf->dflt, YIN_SUBELEM_UNIQUE},
1399 {LY_STMT_DESCRIPTION, &tpdf->dsc, YIN_SUBELEM_UNIQUE},
1400 {LY_STMT_REFERENCE, &tpdf->ref, YIN_SUBELEM_UNIQUE},
1401 {LY_STMT_STATUS, &tpdf->flags, YIN_SUBELEM_UNIQUE},
1402 {LY_STMT_TYPE, &tpdf->type, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_MANDATORY},
1403 {LY_STMT_UNITS, &tpdf->units, YIN_SUBELEM_UNIQUE},
1404 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1405 };
Michal Vaskob36053d2020-03-26 15:49:30 +01001406 LY_CHECK_RET(yin_parse_content(ctx, subelems, 7, LY_STMT_TYPEDEF, NULL, &tpdf->exts));
David Sedlák04e17b22019-07-19 15:29:48 +02001407
1408 /* store data for collision check */
Michal Vasko1bf09392020-03-27 12:38:10 +01001409 if (typedef_meta->parent && !(typedef_meta->parent->nodetype & (LYS_GROUPING | LYS_RPC | LYS_ACTION | LYS_INOUT | LYS_NOTIF))) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02001410 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, typedef_meta->parent, 0, NULL));
David Sedlák04e17b22019-07-19 15:29:48 +02001411 }
1412
1413 return LY_SUCCESS;
1414}
1415
1416/**
David Sedlákd2d676a2019-07-22 11:28:19 +02001417 * @brief Parse refine element.
1418 *
1419 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákd2d676a2019-07-22 11:28:19 +02001420 * @param[in,out] refines Refines to add to.
1421 *
1422 * @return LY_ERR values.
1423 */
1424static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001425yin_parse_refine(struct lys_yin_parser_ctx *ctx, struct lysp_refine **refines)
David Sedlákd2d676a2019-07-22 11:28:19 +02001426{
1427 struct lysp_refine *rf;
1428
1429 /* allocate new refine */
Michal Vaskob36053d2020-03-26 15:49:30 +01001430 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *refines, rf, LY_EMEM);
David Sedlákd2d676a2019-07-22 11:28:19 +02001431
1432 /* parse attribute */
Michal Vaskob36053d2020-03-26 15:49:30 +01001433 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1434 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_TARGET_NODE, &rf->nodeid, Y_STR_ARG, LY_STMT_REFINE));
1435 CHECK_NONEMPTY(ctx, strlen(rf->nodeid), "refine");
David Sedlákd2d676a2019-07-22 11:28:19 +02001436
1437 /* parse content */
1438 struct yin_subelement subelems[11] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001439 {LY_STMT_CONFIG, &rf->flags, YIN_SUBELEM_UNIQUE},
1440 {LY_STMT_DEFAULT, &rf->dflts, 0},
1441 {LY_STMT_DESCRIPTION, &rf->dsc, YIN_SUBELEM_UNIQUE},
1442 {LY_STMT_IF_FEATURE, &rf->iffeatures, 0},
1443 {LY_STMT_MANDATORY, &rf->flags, YIN_SUBELEM_UNIQUE},
1444 {LY_STMT_MAX_ELEMENTS, rf, YIN_SUBELEM_UNIQUE},
1445 {LY_STMT_MIN_ELEMENTS, rf, YIN_SUBELEM_UNIQUE},
1446 {LY_STMT_MUST, &rf->musts, 0},
1447 {LY_STMT_PRESENCE, &rf->presence, YIN_SUBELEM_UNIQUE},
1448 {LY_STMT_REFERENCE, &rf->ref, YIN_SUBELEM_UNIQUE},
1449 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1450 };
Michal Vaskob36053d2020-03-26 15:49:30 +01001451 return yin_parse_content(ctx, subelems, 11, LY_STMT_REFINE, NULL, &rf->exts);
David Sedlákd2d676a2019-07-22 11:28:19 +02001452}
1453
1454/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02001455 * @brief Parse uses element.
1456 *
1457 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákad83cf92019-08-13 12:53:53 +02001458 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlák0d6de5a2019-07-22 13:25:44 +02001459 *
1460 * @return LY_ERR values.
1461 */
1462static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001463yin_parse_uses(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta)
David Sedlák0d6de5a2019-07-22 13:25:44 +02001464{
David Sedlák0d6de5a2019-07-22 13:25:44 +02001465 struct lysp_node_uses *uses;
1466
David Sedlák8d552d62019-08-06 15:29:05 +02001467 /* create new uses */
Michal Vaskob36053d2020-03-26 15:49:30 +01001468 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, uses, next, LY_EMEM);
David Sedlák0d6de5a2019-07-22 13:25:44 +02001469 uses->nodetype = LYS_USES;
1470 uses->parent = node_meta->parent;
1471
David Sedlák0d6de5a2019-07-22 13:25:44 +02001472 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001473 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1474 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 +02001475
1476 /* parse content */
David Sedlák6881b512019-08-13 12:52:00 +02001477 struct tree_node_meta augments = {(struct lysp_node *)uses, (struct lysp_node **)&uses->augments};
David Sedlák0d6de5a2019-07-22 13:25:44 +02001478 struct yin_subelement subelems[8] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001479 {LY_STMT_AUGMENT, &augments, 0},
1480 {LY_STMT_DESCRIPTION, &uses->dsc, YIN_SUBELEM_UNIQUE},
1481 {LY_STMT_IF_FEATURE, &uses->iffeatures, 0},
1482 {LY_STMT_REFERENCE, &uses->ref, YIN_SUBELEM_UNIQUE},
1483 {LY_STMT_REFINE, &uses->refines, 0},
1484 {LY_STMT_STATUS, &uses->flags, YIN_SUBELEM_UNIQUE},
1485 {LY_STMT_WHEN, &uses->when, YIN_SUBELEM_UNIQUE},
1486 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1487 };
Michal Vaskob36053d2020-03-26 15:49:30 +01001488 LY_CHECK_RET(yin_parse_content(ctx, subelems, 8, LY_STMT_USES, NULL, &uses->exts));
David Sedlák0d6de5a2019-07-22 13:25:44 +02001489 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, NULL, uses->augments, NULL, NULL));
1490
1491 return LY_SUCCESS;
1492}
1493
1494/**
David Sedlákaa854b02019-07-22 14:17:10 +02001495 * @brief Parse revision element.
1496 *
1497 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákaa854b02019-07-22 14:17:10 +02001498 * @param[in,out] revs Parsed revisions to add to.
1499 *
1500 * @return LY_ERR values.
1501 */
1502static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001503yin_parse_revision(struct lys_yin_parser_ctx *ctx, struct lysp_revision **revs)
David Sedlákaa854b02019-07-22 14:17:10 +02001504{
1505 struct lysp_revision *rev;
1506 const char *temp_date = NULL;
1507
1508 /* allocate new reivison */
Michal Vaskob36053d2020-03-26 15:49:30 +01001509 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *revs, rev, LY_EMEM);
David Sedlákaa854b02019-07-22 14:17:10 +02001510
1511 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001512 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1513 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 +02001514 /* check value */
1515 if (lysp_check_date((struct lys_parser_ctx *)ctx, temp_date, strlen(temp_date), "revision")) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001516 FREE_STRING(ctx->xmlctx->ctx, temp_date);
David Sedlákaa854b02019-07-22 14:17:10 +02001517 return LY_EVALID;
1518 }
1519 strcpy(rev->date, temp_date);
Michal Vaskob36053d2020-03-26 15:49:30 +01001520 FREE_STRING(ctx->xmlctx->ctx, temp_date);
David Sedlákaa854b02019-07-22 14:17:10 +02001521
1522 /* parse content */
1523 struct yin_subelement subelems[3] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001524 {LY_STMT_DESCRIPTION, &rev->dsc, YIN_SUBELEM_UNIQUE},
1525 {LY_STMT_REFERENCE, &rev->ref, YIN_SUBELEM_UNIQUE},
1526 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1527 };
Michal Vaskob36053d2020-03-26 15:49:30 +01001528 return yin_parse_content(ctx, subelems, 3, LY_STMT_REVISION, NULL, &rev->exts);
David Sedlákaa854b02019-07-22 14:17:10 +02001529}
1530
David Sedlák5e13dea2019-07-22 16:06:45 +02001531/**
1532 * @brief Parse include element.
1533 *
1534 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák5e13dea2019-07-22 16:06:45 +02001535 * @param[in,out] inc_meta Meta informatinou about module/submodule name and includes to add to.
1536 *
1537 * @return LY_ERR values.
1538 */
David Sedlák0c2bab92019-07-22 15:33:19 +02001539static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001540yin_parse_include(struct lys_yin_parser_ctx *ctx, struct include_meta *inc_meta)
David Sedlák0c2bab92019-07-22 15:33:19 +02001541{
1542 struct lysp_include *inc;
1543
1544 /* allocate new include */
Michal Vaskob36053d2020-03-26 15:49:30 +01001545 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *inc_meta->includes, inc, LY_EMEM);
David Sedlák0c2bab92019-07-22 15:33:19 +02001546
1547 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001548 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1549 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 +02001550
1551 /* submodules share the namespace with the module names, so there must not be
1552 * a module of the same name in the context, no need for revision matching */
Michal Vaskob36053d2020-03-26 15:49:30 +01001553 if (!strcmp(inc_meta->name, inc->name) || ly_ctx_get_module_latest(ctx->xmlctx->ctx, inc->name)) {
David Sedlák1538a842019-08-08 15:38:51 +02001554 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_NAME_COL, inc->name);
David Sedlák0c2bab92019-07-22 15:33:19 +02001555 return LY_EVALID;
1556 }
1557
1558 /* parse content */
1559 struct yin_subelement subelems[4] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001560 {LY_STMT_DESCRIPTION, &inc->dsc, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_VER2},
1561 {LY_STMT_REFERENCE, &inc->ref, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_VER2},
1562 {LY_STMT_REVISION_DATE, &inc->rev, YIN_SUBELEM_UNIQUE},
1563 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1564 };
Michal Vaskob36053d2020-03-26 15:49:30 +01001565 return yin_parse_content(ctx, subelems, 4, LY_STMT_INCLUDE, NULL, &inc->exts);
David Sedlák0c2bab92019-07-22 15:33:19 +02001566}
1567
David Sedlákaa854b02019-07-22 14:17:10 +02001568/**
David Sedlákbf8a2b72019-08-14 16:48:10 +02001569 * @brief Parse revision-date element.
David Sedlákdfbbb442019-08-06 16:33:21 +02001570 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001571 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001572 * @param[in,out] rev Array to store the parsed value in.
1573 * @param[in,out] exts Extension instances to add to.
1574 *
1575 * @return LY_ERR values.
1576 */
1577static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001578yin_parse_revision_date(struct lys_yin_parser_ctx *ctx, char *rev, struct lysp_ext_instance **exts)
David Sedlákdfbbb442019-08-06 16:33:21 +02001579{
1580 const char *temp_rev;
1581 struct yin_subelement subelems[1] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001582 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1583 };
David Sedlákdfbbb442019-08-06 16:33:21 +02001584
Michal Vaskob36053d2020-03-26 15:49:30 +01001585 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1586 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 +02001587 LY_CHECK_ERR_RET(lysp_check_date((struct lys_parser_ctx *)ctx, temp_rev, strlen(temp_rev), "revision-date") != LY_SUCCESS,
Michal Vaskob36053d2020-03-26 15:49:30 +01001588 FREE_STRING(ctx->xmlctx->ctx, temp_rev), LY_EVALID);
David Sedlákdfbbb442019-08-06 16:33:21 +02001589
1590 strcpy(rev, temp_rev);
Michal Vaskob36053d2020-03-26 15:49:30 +01001591 FREE_STRING(ctx->xmlctx->ctx, temp_rev);
David Sedlákdfbbb442019-08-06 16:33:21 +02001592
Michal Vaskob36053d2020-03-26 15:49:30 +01001593 return yin_parse_content(ctx, subelems, 1, LY_STMT_REVISION_DATE, NULL, exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02001594}
1595
1596/**
1597 * @brief Parse config element.
1598 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001599 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001600 * @param[in,out] flags Flags to add to.
1601 * @param[in,out] exts Extension instances to add to.
1602 *
1603 * @return LY_ERR values.
1604 */
1605static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001606yin_parse_config(struct lys_yin_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
David Sedlákdfbbb442019-08-06 16:33:21 +02001607{
1608 const char *temp_val = NULL;
1609 struct yin_subelement subelems[1] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001610 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1611 };
David Sedlákdfbbb442019-08-06 16:33:21 +02001612
Michal Vaskob36053d2020-03-26 15:49:30 +01001613 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1614 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 +02001615 if (strcmp(temp_val, "true") == 0) {
1616 *flags |= LYS_CONFIG_W;
1617 } else if (strcmp(temp_val, "false") == 0) {
1618 *flags |= LYS_CONFIG_R;
1619 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02001620 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value", "config",
1621 "true", "false");
Michal Vaskob36053d2020-03-26 15:49:30 +01001622 FREE_STRING(ctx->xmlctx->ctx, temp_val);
David Sedlákdfbbb442019-08-06 16:33:21 +02001623 return LY_EVALID;
1624 }
Michal Vaskob36053d2020-03-26 15:49:30 +01001625 FREE_STRING(ctx->xmlctx->ctx, temp_val);
David Sedlákdfbbb442019-08-06 16:33:21 +02001626
Michal Vaskob36053d2020-03-26 15:49:30 +01001627 return yin_parse_content(ctx, subelems, 1, LY_STMT_CONFIG, NULL, exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02001628}
1629
1630/**
1631 * @brief Parse yang-version element.
1632 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001633 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001634 * @param[out] version Storage for the parsed information.
David Sedlákbf8a2b72019-08-14 16:48:10 +02001635 * @param[in,out] exts Extension instances to add to.
David Sedlákdfbbb442019-08-06 16:33:21 +02001636 *
1637 * @return LY_ERR values.
1638 */
1639static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001640yin_parse_yangversion(struct lys_yin_parser_ctx *ctx, uint8_t *version, struct lysp_ext_instance **exts)
David Sedlákdfbbb442019-08-06 16:33:21 +02001641{
1642 const char *temp_version = NULL;
1643 struct yin_subelement subelems[1] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001644 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1645 };
David Sedlákdfbbb442019-08-06 16:33:21 +02001646
Michal Vaskob36053d2020-03-26 15:49:30 +01001647 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1648 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_version, Y_STR_ARG, LY_STMT_YANG_VERSION));
David Sedlákdfbbb442019-08-06 16:33:21 +02001649 if (strcmp(temp_version, "1.0") == 0) {
1650 *version = LYS_VERSION_1_0;
1651 } else if (strcmp(temp_version, "1.1") == 0) {
1652 *version = LYS_VERSION_1_1;
1653 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02001654 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_version, "value",
1655 "yang-version", "1.0", "1.1");
Michal Vaskob36053d2020-03-26 15:49:30 +01001656 FREE_STRING(ctx->xmlctx->ctx, temp_version);
David Sedlákdfbbb442019-08-06 16:33:21 +02001657 return LY_EVALID;
1658 }
Michal Vaskob36053d2020-03-26 15:49:30 +01001659 FREE_STRING(ctx->xmlctx->ctx, temp_version);
David Sedlákdfbbb442019-08-06 16:33:21 +02001660 ctx->mod_version = *version;
1661
Michal Vaskob36053d2020-03-26 15:49:30 +01001662 return yin_parse_content(ctx, subelems, 1, LY_STMT_YANG_VERSION, NULL, exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02001663}
1664
1665/**
1666 * @brief Parse import element.
1667 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001668 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001669 * @param[in,out] imp_meta Meta information about prefix and imports to add to.
1670 *
1671 * @return LY_ERR values.
1672 */
1673static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001674yin_parse_import(struct lys_yin_parser_ctx *ctx, struct import_meta *imp_meta)
David Sedlákdfbbb442019-08-06 16:33:21 +02001675{
1676 struct lysp_import *imp;
1677 /* allocate new element in sized array for import */
Michal Vaskob36053d2020-03-26 15:49:30 +01001678 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *imp_meta->imports, imp, LY_EMEM);
David Sedlákdfbbb442019-08-06 16:33:21 +02001679
1680 struct yin_subelement subelems[5] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001681 {LY_STMT_DESCRIPTION, &imp->dsc, YIN_SUBELEM_UNIQUE},
1682 {LY_STMT_PREFIX, &imp->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
1683 {LY_STMT_REFERENCE, &imp->ref, YIN_SUBELEM_UNIQUE},
1684 {LY_STMT_REVISION_DATE, imp->rev, YIN_SUBELEM_UNIQUE},
1685 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1686 };
David Sedlákdfbbb442019-08-06 16:33:21 +02001687
1688 /* parse import attributes */
Michal Vaskob36053d2020-03-26 15:49:30 +01001689 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1690 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_MODULE, &imp->name, Y_IDENTIF_ARG, LY_STMT_IMPORT));
1691 LY_CHECK_RET(yin_parse_content(ctx, subelems, 5, LY_STMT_IMPORT, NULL, &imp->exts));
David Sedlákdfbbb442019-08-06 16:33:21 +02001692 /* check prefix validity */
1693 LY_CHECK_RET(lysp_check_prefix((struct lys_parser_ctx *)ctx, *imp_meta->imports, imp_meta->prefix, &imp->prefix), LY_EVALID);
1694
1695 return LY_SUCCESS;
1696}
1697
1698/**
1699 * @brief Parse mandatory element.
1700 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001701 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001702 * @param[in,out] flags Flags to add to.
1703 * @param[in,out] exts Extension instances to add to.
1704 *
1705 * @return LY_ERR values.
1706 */
1707static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001708yin_parse_mandatory(struct lys_yin_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
David Sedlákdfbbb442019-08-06 16:33:21 +02001709{
1710 const char *temp_val = NULL;
1711 struct yin_subelement subelems[1] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001712 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1713 };
David Sedlákdfbbb442019-08-06 16:33:21 +02001714
Michal Vaskob36053d2020-03-26 15:49:30 +01001715 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1716 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 +02001717 if (strcmp(temp_val, "true") == 0) {
1718 *flags |= LYS_MAND_TRUE;
1719 } else if (strcmp(temp_val, "false") == 0) {
1720 *flags |= LYS_MAND_FALSE;
1721 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02001722 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value",
1723 "mandatory", "true", "false");
Michal Vaskob36053d2020-03-26 15:49:30 +01001724 FREE_STRING(ctx->xmlctx->ctx, temp_val);
David Sedlákdfbbb442019-08-06 16:33:21 +02001725 return LY_EVALID;
1726 }
Michal Vaskob36053d2020-03-26 15:49:30 +01001727 FREE_STRING(ctx->xmlctx->ctx, temp_val);
David Sedlákdfbbb442019-08-06 16:33:21 +02001728
Michal Vaskob36053d2020-03-26 15:49:30 +01001729 return yin_parse_content(ctx, subelems, 1, LY_STMT_MANDATORY, NULL, exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02001730}
1731
1732/**
1733 * @brief Parse status element.
1734 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001735 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001736 * @param[in,out] flags Flags to add to.
1737 * @param[in,out] exts Extension instances to add to.
1738 *
1739 * @return LY_ERR values.
1740 */
1741static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001742yin_parse_status(struct lys_yin_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
David Sedlákdfbbb442019-08-06 16:33:21 +02001743{
1744 const char *value = NULL;
1745 struct yin_subelement subelems[1] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001746 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1747 };
David Sedlákdfbbb442019-08-06 16:33:21 +02001748
Michal Vaskob36053d2020-03-26 15:49:30 +01001749 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1750 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 +02001751 if (strcmp(value, "current") == 0) {
1752 *flags |= LYS_STATUS_CURR;
1753 } else if (strcmp(value, "deprecated") == 0) {
1754 *flags |= LYS_STATUS_DEPRC;
1755 } else if (strcmp(value, "obsolete") == 0) {
1756 *flags |= LYS_STATUS_OBSLT;
1757 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02001758 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS3, value, "value",
1759 "status", "current", "deprecated", "obsolete");
Michal Vaskob36053d2020-03-26 15:49:30 +01001760 FREE_STRING(ctx->xmlctx->ctx, value);
David Sedlákdfbbb442019-08-06 16:33:21 +02001761 return LY_EVALID;
1762 }
Michal Vaskob36053d2020-03-26 15:49:30 +01001763 FREE_STRING(ctx->xmlctx->ctx, value);
David Sedlákdfbbb442019-08-06 16:33:21 +02001764
Michal Vaskob36053d2020-03-26 15:49:30 +01001765 return yin_parse_content(ctx, subelems, 1, LY_STMT_STATUS, NULL, exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02001766}
1767
1768/**
1769 * @brief Parse when element.
1770 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001771 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001772 * @param[out] when_p When pointer to parse to.
1773 */
1774static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001775yin_parse_when(struct lys_yin_parser_ctx *ctx, struct lysp_when **when_p)
David Sedlákdfbbb442019-08-06 16:33:21 +02001776{
1777 struct lysp_when *when;
David Sedláka56e0012019-08-15 13:21:25 +02001778 LY_ERR ret = LY_SUCCESS;
1779
David Sedlákdfbbb442019-08-06 16:33:21 +02001780 when = calloc(1, sizeof *when);
Michal Vaskob36053d2020-03-26 15:49:30 +01001781 LY_CHECK_ERR_RET(!when, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
Radek Krejcif6923e82020-07-02 16:36:53 +02001782
1783 ret = lyxml_ctx_next(ctx->xmlctx);
1784 LY_CHECK_ERR_RET(ret, free(when), ret);
1785
Michal Vaskob36053d2020-03-26 15:49:30 +01001786 ret = yin_parse_attribute(ctx, YIN_ARG_CONDITION, &when->cond, Y_STR_ARG, LY_STMT_WHEN);
David Sedláka56e0012019-08-15 13:21:25 +02001787 LY_CHECK_ERR_RET(ret, free(when), ret);
1788
David Sedlákdfbbb442019-08-06 16:33:21 +02001789 *when_p = when;
1790 struct yin_subelement subelems[3] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001791 {LY_STMT_DESCRIPTION, &when->dsc, YIN_SUBELEM_UNIQUE},
1792 {LY_STMT_REFERENCE, &when->ref, YIN_SUBELEM_UNIQUE},
1793 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1794 };
David Sedlákdfbbb442019-08-06 16:33:21 +02001795
Michal Vaskob36053d2020-03-26 15:49:30 +01001796 return yin_parse_content(ctx, subelems, 3, LY_STMT_WHEN, NULL, &when->exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02001797}
1798
1799/**
1800 * @brief Parse yin-elemenet element.
1801 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001802 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001803 * @param[in,out] data Data to read from, always moved to currently handled position.
1804 * @param[in,out] flags Flags to add to.
David Sedlákbf8a2b72019-08-14 16:48:10 +02001805 * @prama[in,out] exts Extension instances to add to.
David Sedlákdfbbb442019-08-06 16:33:21 +02001806 *
1807 * @return LY_ERR values.
1808 */
1809static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001810yin_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 +02001811{
1812 const char *temp_val = NULL;
1813 struct yin_subelement subelems[1] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001814 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1815 };
David Sedlákdfbbb442019-08-06 16:33:21 +02001816
Michal Vaskob36053d2020-03-26 15:49:30 +01001817 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1818 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 +02001819 if (strcmp(temp_val, "true") == 0) {
1820 *flags |= LYS_YINELEM_TRUE;
1821 } else if (strcmp(temp_val, "false") == 0) {
1822 *flags |= LYS_YINELEM_FALSE;
1823 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02001824 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value",
1825 "yin-element", "true", "false");
Michal Vaskob36053d2020-03-26 15:49:30 +01001826 FREE_STRING(ctx->xmlctx->ctx, temp_val);
David Sedlákdfbbb442019-08-06 16:33:21 +02001827 return LY_EVALID;
1828 }
Michal Vaskob36053d2020-03-26 15:49:30 +01001829 FREE_STRING(ctx->xmlctx->ctx, temp_val);
David Sedlákdfbbb442019-08-06 16:33:21 +02001830
Michal Vaskob36053d2020-03-26 15:49:30 +01001831 return yin_parse_content(ctx, subelems, 1, LY_STMT_YIN_ELEMENT, NULL, exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02001832}
1833
1834/**
1835 * @brief Parse argument element.
1836 *
Michal Vaskob36053d2020-03-26 15:49:30 +01001837 * @param[in,out] xmlctx Xml context.
David Sedlákbf8a2b72019-08-14 16:48:10 +02001838 * @param[in,out] arg_meta Meta information about destionation of parsed data.
1839 * @param[in,out] exts Extension instances to add to.
David Sedlákdfbbb442019-08-06 16:33:21 +02001840 *
1841 * @return LY_ERR values.
1842 */
1843static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001844yin_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 +02001845{
1846 struct yin_subelement subelems[2] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001847 {LY_STMT_YIN_ELEMENT, arg_meta->flags, YIN_SUBELEM_UNIQUE},
1848 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1849 };
David Sedlákdfbbb442019-08-06 16:33:21 +02001850
Michal Vaskob36053d2020-03-26 15:49:30 +01001851 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1852 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 +02001853
Michal Vaskob36053d2020-03-26 15:49:30 +01001854 return yin_parse_content(ctx, subelems, 2, LY_STMT_ARGUMENT, NULL, exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02001855}
1856
1857/**
1858 * @brief Parse the extension statement.
1859 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001860 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001861 * @param[in,out] extensions Extensions to add to.
1862 *
1863 * @return LY_ERR values.
1864 */
1865static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001866yin_parse_extension(struct lys_yin_parser_ctx *ctx, struct lysp_ext **extensions)
David Sedlákdfbbb442019-08-06 16:33:21 +02001867{
1868 struct lysp_ext *ex;
Michal Vaskob36053d2020-03-26 15:49:30 +01001869 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *extensions, ex, LY_EMEM);
1870 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1871 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 +02001872
1873 struct yin_argument_meta arg_info = {&ex->flags, &ex->argument};
1874 struct yin_subelement subelems[5] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001875 {LY_STMT_ARGUMENT, &arg_info, YIN_SUBELEM_UNIQUE},
1876 {LY_STMT_DESCRIPTION, &ex->dsc, YIN_SUBELEM_UNIQUE},
1877 {LY_STMT_REFERENCE, &ex->ref, YIN_SUBELEM_UNIQUE},
1878 {LY_STMT_STATUS, &ex->flags, YIN_SUBELEM_UNIQUE},
1879 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1880 };
David Sedlákdfbbb442019-08-06 16:33:21 +02001881
Michal Vaskob36053d2020-03-26 15:49:30 +01001882 return yin_parse_content(ctx, subelems, 5, LY_STMT_EXTENSION, NULL, &ex->exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02001883}
1884
1885/**
David Sedlák5e13dea2019-07-22 16:06:45 +02001886 * @brief Parse feature element.
1887 *
1888 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák5e13dea2019-07-22 16:06:45 +02001889 * @param[in,out] features Features to add to.
1890 *
1891 * @return LY_ERR values.
1892 */
1893static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001894yin_parse_feature(struct lys_yin_parser_ctx *ctx, struct lysp_feature **features)
David Sedlák5e13dea2019-07-22 16:06:45 +02001895{
1896 struct lysp_feature *feat;
1897
1898 /* allocate new feature */
Michal Vaskob36053d2020-03-26 15:49:30 +01001899 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *features, feat, LY_EMEM);
David Sedlák5e13dea2019-07-22 16:06:45 +02001900
1901 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001902 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1903 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 +02001904
1905 /* parse content */
1906 struct yin_subelement subelems[5] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001907 {LY_STMT_DESCRIPTION, &feat->dsc, YIN_SUBELEM_UNIQUE},
1908 {LY_STMT_IF_FEATURE, &feat->iffeatures, 0},
1909 {LY_STMT_REFERENCE, &feat->ref, YIN_SUBELEM_UNIQUE},
1910 {LY_STMT_STATUS, &feat->flags, YIN_SUBELEM_UNIQUE},
1911 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1912 };
Michal Vaskob36053d2020-03-26 15:49:30 +01001913 return yin_parse_content(ctx, subelems, 5, LY_STMT_FEATURE, NULL, &feat->exts);
David Sedlák5e13dea2019-07-22 16:06:45 +02001914}
1915
1916/**
David Sedlák28794f22019-07-22 16:45:00 +02001917 * @brief Parse identity element.
1918 *
1919 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák28794f22019-07-22 16:45:00 +02001920 * @param[in,out] identities Identities to add to.
1921 *
1922 * @return LY_ERR values.
1923 */
1924static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001925yin_parse_identity(struct lys_yin_parser_ctx *ctx, struct lysp_ident **identities)
David Sedlák28794f22019-07-22 16:45:00 +02001926{
1927 struct lysp_ident *ident;
1928
1929 /* allocate new identity */
Michal Vaskob36053d2020-03-26 15:49:30 +01001930 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *identities, ident, LY_EMEM);
David Sedlák28794f22019-07-22 16:45:00 +02001931
1932 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001933 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1934 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 +02001935
1936 /* parse content */
1937 struct yin_subelement subelems[6] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001938 {LY_STMT_BASE, &ident->bases, 0},
1939 {LY_STMT_DESCRIPTION, &ident->dsc, YIN_SUBELEM_UNIQUE},
1940 {LY_STMT_IF_FEATURE, &ident->iffeatures, YIN_SUBELEM_VER2},
1941 {LY_STMT_REFERENCE, &ident->ref, YIN_SUBELEM_UNIQUE},
1942 {LY_STMT_STATUS, &ident->flags, YIN_SUBELEM_UNIQUE},
1943 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1944 };
Michal Vaskob36053d2020-03-26 15:49:30 +01001945 return yin_parse_content(ctx, subelems, 6, LY_STMT_IDENTITY, NULL, &ident->exts);
David Sedlák28794f22019-07-22 16:45:00 +02001946}
1947
1948/**
David Sedlákaf536aa2019-07-23 13:42:23 +02001949 * @brief Parse list element.
1950 *
1951 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákad83cf92019-08-13 12:53:53 +02001952 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlákaf536aa2019-07-23 13:42:23 +02001953 *
1954 * @return LY_ERR values.
1955 */
1956static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001957yin_parse_list(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta)
David Sedlákaf536aa2019-07-23 13:42:23 +02001958{
David Sedlákaf536aa2019-07-23 13:42:23 +02001959 struct lysp_node_list *list;
David Sedlák81497a32019-08-13 16:56:26 +02001960 LY_ERR ret = LY_SUCCESS;
1961 struct yin_subelement *subelems = NULL;
David Sedlákaf536aa2019-07-23 13:42:23 +02001962
Michal Vaskob36053d2020-03-26 15:49:30 +01001963 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, list, next, LY_EMEM);
David Sedlákaf536aa2019-07-23 13:42:23 +02001964 list->nodetype = LYS_LIST;
1965 list->parent = node_meta->parent;
1966
David Sedlákaf536aa2019-07-23 13:42:23 +02001967 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001968 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1969 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 +02001970
1971 /* parse list content */
David Sedlák81497a32019-08-13 16:56:26 +02001972 LY_CHECK_RET(subelems_allocator(ctx, 25, (struct lysp_node *)list, &subelems,
Radek Krejcid6b76452019-09-03 17:03:03 +02001973 LY_STMT_ACTION, &list->actions, 0,
1974 LY_STMT_ANYDATA, &list->child, 0,
1975 LY_STMT_ANYXML, &list->child, 0,
1976 LY_STMT_CHOICE, &list->child, 0,
1977 LY_STMT_CONFIG, &list->flags, YIN_SUBELEM_UNIQUE,
1978 LY_STMT_CONTAINER, &list->child, 0,
1979 LY_STMT_DESCRIPTION, &list->dsc, YIN_SUBELEM_UNIQUE,
1980 LY_STMT_GROUPING, &list->groupings, 0,
1981 LY_STMT_IF_FEATURE, &list->iffeatures, 0,
1982 LY_STMT_KEY, &list->key, YIN_SUBELEM_UNIQUE,
1983 LY_STMT_LEAF, &list->child, 0,
1984 LY_STMT_LEAF_LIST, &list->child, 0,
1985 LY_STMT_LIST, &list->child, 0,
1986 LY_STMT_MAX_ELEMENTS, list, YIN_SUBELEM_UNIQUE,
1987 LY_STMT_MIN_ELEMENTS, list, YIN_SUBELEM_UNIQUE,
1988 LY_STMT_MUST, &list->musts, 0,
1989 LY_STMT_NOTIFICATION, &list->notifs, 0,
1990 LY_STMT_ORDERED_BY, &list->flags, YIN_SUBELEM_UNIQUE,
1991 LY_STMT_REFERENCE, &list->ref, YIN_SUBELEM_UNIQUE,
1992 LY_STMT_STATUS, &list->flags, YIN_SUBELEM_UNIQUE,
1993 LY_STMT_TYPEDEF, &list->typedefs, 0,
1994 LY_STMT_UNIQUE, &list->uniques, 0,
1995 LY_STMT_USES, &list->child, 0,
1996 LY_STMT_WHEN, &list->when, YIN_SUBELEM_UNIQUE,
1997 LY_STMT_EXTENSION_INSTANCE, NULL, 0
David Sedlák81497a32019-08-13 16:56:26 +02001998 ));
Michal Vaskob36053d2020-03-26 15:49:30 +01001999 ret = yin_parse_content(ctx, subelems, 25, LY_STMT_LIST, NULL, &list->exts);
David Sedlák81497a32019-08-13 16:56:26 +02002000 subelems_deallocator(25, subelems);
2001 LY_CHECK_RET(ret);
David Sedlákaf536aa2019-07-23 13:42:23 +02002002
2003 /* finalize parent pointers to the reallocated items */
2004 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, list->groupings, NULL, list->actions, list->notifs));
2005
2006 if (list->max && list->min > list->max) {
David Sedlák1538a842019-08-08 15:38:51 +02002007 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_MINMAX, list->min, list->max);
David Sedlákaf536aa2019-07-23 13:42:23 +02002008 return LY_EVALID;
2009 }
2010
2011 return LY_SUCCESS;
2012}
2013
2014/**
David Sedlák031b9e72019-07-23 15:19:37 +02002015 * @brief Parse notification element.
2016 *
2017 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák05404f62019-07-24 14:11:53 +02002018 * @param[in,out] notif_meta Meta information about parent node and notifications to add to.
David Sedlák031b9e72019-07-23 15:19:37 +02002019 *
2020 * @return LY_ERR values.
2021 */
2022static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002023yin_parse_notification(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *notif_meta)
David Sedlák031b9e72019-07-23 15:19:37 +02002024{
2025 struct lysp_notif *notif;
David Sedlákbf8a2b72019-08-14 16:48:10 +02002026 struct lysp_notif **notifs = (struct lysp_notif **)notif_meta->nodes;
David Sedlák81497a32019-08-13 16:56:26 +02002027 LY_ERR ret = LY_SUCCESS;
2028 struct yin_subelement *subelems = NULL;
David Sedlák031b9e72019-07-23 15:19:37 +02002029
2030 /* allocate new notification */
Michal Vaskob36053d2020-03-26 15:49:30 +01002031 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *notifs, notif, LY_EMEM);
David Sedlák031b9e72019-07-23 15:19:37 +02002032 notif->nodetype = LYS_NOTIF;
2033 notif->parent = notif_meta->parent;
2034
2035 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002036 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2037 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 +02002038
2039 /* parse notification content */
David Sedlák81497a32019-08-13 16:56:26 +02002040 LY_CHECK_RET(subelems_allocator(ctx, 16, (struct lysp_node *)notif, &subelems,
Radek Krejcid6b76452019-09-03 17:03:03 +02002041 LY_STMT_ANYDATA, &notif->data, 0,
2042 LY_STMT_ANYXML, &notif->data, 0,
2043 LY_STMT_CHOICE, &notif->data, 0,
2044 LY_STMT_CONTAINER, &notif->data, 0,
2045 LY_STMT_DESCRIPTION, &notif->dsc, YIN_SUBELEM_UNIQUE,
2046 LY_STMT_GROUPING, &notif->groupings, 0,
2047 LY_STMT_IF_FEATURE, &notif->iffeatures, 0,
2048 LY_STMT_LEAF, &notif->data, 0,
2049 LY_STMT_LEAF_LIST, &notif->data, 0,
2050 LY_STMT_LIST, &notif->data, 0,
2051 LY_STMT_MUST, &notif->musts, YIN_SUBELEM_VER2,
2052 LY_STMT_REFERENCE, &notif->ref, YIN_SUBELEM_UNIQUE,
2053 LY_STMT_STATUS, &notif->flags, YIN_SUBELEM_UNIQUE,
2054 LY_STMT_TYPEDEF, &notif->typedefs, 0,
2055 LY_STMT_USES, &notif->data, 0,
2056 LY_STMT_EXTENSION_INSTANCE, NULL, 0
David Sedlák81497a32019-08-13 16:56:26 +02002057 ));
2058
Michal Vaskob36053d2020-03-26 15:49:30 +01002059 ret = yin_parse_content(ctx, subelems, 16, LY_STMT_NOTIFICATION, NULL, &notif->exts);
David Sedlák81497a32019-08-13 16:56:26 +02002060 subelems_deallocator(16, subelems);
2061 LY_CHECK_RET(ret);
David Sedlák031b9e72019-07-23 15:19:37 +02002062
2063 /* finalize parent pointers to the reallocated items */
2064 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, notif->groupings, NULL, NULL, NULL));
2065
2066 return LY_SUCCESS;
2067}
2068
2069/**
David Sedlákbf8a2b72019-08-14 16:48:10 +02002070 * @brief Parse grouping element.
David Sedláke3ce9ef2019-07-23 16:34:30 +02002071 *
2072 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbf8a2b72019-08-14 16:48:10 +02002073 * @param[in,out] gr_meta Meta information about parent node and groupings to add to.
David Sedláke3ce9ef2019-07-23 16:34:30 +02002074 *
2075 * @return LY_ERR values.
2076 */
2077static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002078yin_parse_grouping(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *gr_meta)
David Sedláke3ce9ef2019-07-23 16:34:30 +02002079{
2080 struct lysp_grp *grp;
David Sedlákbf8a2b72019-08-14 16:48:10 +02002081 struct lysp_grp **grps = (struct lysp_grp **)gr_meta->nodes;
David Sedlák81497a32019-08-13 16:56:26 +02002082 LY_ERR ret = LY_SUCCESS;
2083 struct yin_subelement *subelems = NULL;
David Sedláke3ce9ef2019-07-23 16:34:30 +02002084
2085 /* create new grouping */
Michal Vaskob36053d2020-03-26 15:49:30 +01002086 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *grps, grp, LY_EMEM);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002087 grp->nodetype = LYS_GROUPING;
2088 grp->parent = gr_meta->parent;
2089
2090 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002091 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2092 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 +02002093
2094 /* parse grouping content */
David Sedlák81497a32019-08-13 16:56:26 +02002095 LY_CHECK_RET(subelems_allocator(ctx, 16, (struct lysp_node *)grp, &subelems,
Radek Krejcid6b76452019-09-03 17:03:03 +02002096 LY_STMT_ACTION, &grp->actions, 0,
2097 LY_STMT_ANYDATA, &grp->data, 0,
2098 LY_STMT_ANYXML, &grp->data, 0,
2099 LY_STMT_CHOICE, &grp->data, 0,
2100 LY_STMT_CONTAINER, &grp->data, 0,
2101 LY_STMT_DESCRIPTION, &grp->dsc, YIN_SUBELEM_UNIQUE,
2102 LY_STMT_GROUPING, &grp->groupings, 0,
2103 LY_STMT_LEAF, &grp->data, 0,
2104 LY_STMT_LEAF_LIST, &grp->data, 0,
2105 LY_STMT_LIST, &grp->data, 0,
2106 LY_STMT_NOTIFICATION, &grp->notifs, 0,
2107 LY_STMT_REFERENCE, &grp->ref, YIN_SUBELEM_UNIQUE,
2108 LY_STMT_STATUS, &grp->flags, YIN_SUBELEM_UNIQUE,
2109 LY_STMT_TYPEDEF, &grp->typedefs, 0,
2110 LY_STMT_USES, &grp->data, 0,
2111 LY_STMT_EXTENSION_INSTANCE, NULL, 0
David Sedlák81497a32019-08-13 16:56:26 +02002112 ));
Michal Vaskob36053d2020-03-26 15:49:30 +01002113 ret = yin_parse_content(ctx, subelems, 16, LY_STMT_GROUPING, NULL, &grp->exts);
David Sedlák81497a32019-08-13 16:56:26 +02002114 subelems_deallocator(16, subelems);
2115 LY_CHECK_RET(ret);
2116
David Sedláke3ce9ef2019-07-23 16:34:30 +02002117 /* finalize parent pointers to the reallocated items */
2118 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, grp->groupings, NULL, grp->actions, grp->notifs));
2119
2120 return LY_SUCCESS;
2121}
2122
2123/**
David Sedlákbf8a2b72019-08-14 16:48:10 +02002124 * @brief Parse container element.
David Sedlákf111bcb2019-07-23 17:15:51 +02002125 *
2126 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákad83cf92019-08-13 12:53:53 +02002127 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlákf111bcb2019-07-23 17:15:51 +02002128 *
2129 * @return LY_ERR values.
2130 */
2131static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002132yin_parse_container(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta)
David Sedlákf111bcb2019-07-23 17:15:51 +02002133{
David Sedlákf111bcb2019-07-23 17:15:51 +02002134 struct lysp_node_container *cont;
David Sedlák81497a32019-08-13 16:56:26 +02002135 LY_ERR ret = LY_SUCCESS;
2136 struct yin_subelement *subelems = NULL;
David Sedlákf111bcb2019-07-23 17:15:51 +02002137
2138 /* create new container */
Michal Vaskob36053d2020-03-26 15:49:30 +01002139 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, cont, next, LY_EMEM);
David Sedlákf111bcb2019-07-23 17:15:51 +02002140 cont->nodetype = LYS_CONTAINER;
2141 cont->parent = node_meta->parent;
2142
David Sedlákf111bcb2019-07-23 17:15:51 +02002143 /* parse aegument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002144 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2145 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 +02002146
2147 /* parse container content */
David Sedlák81497a32019-08-13 16:56:26 +02002148 LY_CHECK_RET(subelems_allocator(ctx, 21, (struct lysp_node *)cont, &subelems,
Radek Krejcid6b76452019-09-03 17:03:03 +02002149 LY_STMT_ACTION, &cont->actions, YIN_SUBELEM_VER2,
2150 LY_STMT_ANYDATA, &cont->child, YIN_SUBELEM_VER2,
2151 LY_STMT_ANYXML, &cont->child, 0,
2152 LY_STMT_CHOICE, &cont->child, 0,
2153 LY_STMT_CONFIG, &cont->flags, YIN_SUBELEM_UNIQUE,
2154 LY_STMT_CONTAINER, &cont->child, 0,
2155 LY_STMT_DESCRIPTION, &cont->dsc, YIN_SUBELEM_UNIQUE,
2156 LY_STMT_GROUPING, &cont->groupings, 0,
2157 LY_STMT_IF_FEATURE, &cont->iffeatures, 0,
2158 LY_STMT_LEAF, &cont->child, 0,
2159 LY_STMT_LEAF_LIST, &cont->child, 0,
2160 LY_STMT_LIST, &cont->child, 0,
2161 LY_STMT_MUST, &cont->musts, 0,
2162 LY_STMT_NOTIFICATION, &cont->notifs, YIN_SUBELEM_VER2,
2163 LY_STMT_PRESENCE, &cont->presence, YIN_SUBELEM_UNIQUE,
2164 LY_STMT_REFERENCE, &cont->ref, YIN_SUBELEM_UNIQUE,
2165 LY_STMT_STATUS, &cont->flags, YIN_SUBELEM_UNIQUE,
2166 LY_STMT_TYPEDEF, &cont->typedefs, 0,
2167 LY_STMT_USES, &cont->child, 0,
2168 LY_STMT_WHEN, &cont->when, YIN_SUBELEM_UNIQUE,
2169 LY_STMT_EXTENSION_INSTANCE, NULL, 0
David Sedlák81497a32019-08-13 16:56:26 +02002170 ));
Michal Vaskob36053d2020-03-26 15:49:30 +01002171 ret = yin_parse_content(ctx, subelems, 21, LY_STMT_CONTAINER, NULL, &cont->exts);
David Sedlák81497a32019-08-13 16:56:26 +02002172 subelems_deallocator(21, subelems);
2173 LY_CHECK_RET(ret);
2174
David Sedlákf111bcb2019-07-23 17:15:51 +02002175 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, cont->groupings, NULL, cont->actions, cont->notifs));
2176
2177 return LY_SUCCESS;
2178}
2179
2180/**
David Sedlák5379d392019-07-24 10:42:03 +02002181 * @brief Parse case element.
2182 *
2183 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákad83cf92019-08-13 12:53:53 +02002184 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlák5379d392019-07-24 10:42:03 +02002185 *
2186 * @return LY_ERR values.
2187 */
2188static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002189yin_parse_case(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta)
David Sedlák5379d392019-07-24 10:42:03 +02002190{
David Sedlák5379d392019-07-24 10:42:03 +02002191 struct lysp_node_case *cas;
David Sedlák81497a32019-08-13 16:56:26 +02002192 LY_ERR ret = LY_SUCCESS;
2193 struct yin_subelement *subelems = NULL;;
David Sedlák5379d392019-07-24 10:42:03 +02002194
2195 /* create new case */
Michal Vaskob36053d2020-03-26 15:49:30 +01002196 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, cas, next, LY_EMEM);
David Sedlák5379d392019-07-24 10:42:03 +02002197 cas->nodetype = LYS_CASE;
2198 cas->parent = node_meta->parent;
2199
David Sedlák5379d392019-07-24 10:42:03 +02002200 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002201 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2202 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 +02002203
2204 /* parse case content */
David Sedlák81497a32019-08-13 16:56:26 +02002205 LY_CHECK_RET(subelems_allocator(ctx, 14, (struct lysp_node *)cas, &subelems,
Radek Krejcid6b76452019-09-03 17:03:03 +02002206 LY_STMT_ANYDATA, &cas->child, YIN_SUBELEM_VER2,
2207 LY_STMT_ANYXML, &cas->child, 0,
2208 LY_STMT_CHOICE, &cas->child, 0,
2209 LY_STMT_CONTAINER, &cas->child, 0,
2210 LY_STMT_DESCRIPTION, &cas->dsc, YIN_SUBELEM_UNIQUE,
2211 LY_STMT_IF_FEATURE, &cas->iffeatures, 0,
2212 LY_STMT_LEAF, &cas->child, 0,
2213 LY_STMT_LEAF_LIST, &cas->child, 0,
2214 LY_STMT_LIST, &cas->child, 0,
2215 LY_STMT_REFERENCE, &cas->ref, YIN_SUBELEM_UNIQUE,
2216 LY_STMT_STATUS, &cas->flags, YIN_SUBELEM_UNIQUE,
2217 LY_STMT_USES, &cas->child, 0,
2218 LY_STMT_WHEN, &cas->when, YIN_SUBELEM_UNIQUE,
2219 LY_STMT_EXTENSION_INSTANCE, NULL, 0
David Sedlák81497a32019-08-13 16:56:26 +02002220 ));
Michal Vaskob36053d2020-03-26 15:49:30 +01002221 ret = yin_parse_content(ctx, subelems, 14, LY_STMT_CASE, NULL, &cas->exts);
David Sedlák81497a32019-08-13 16:56:26 +02002222 subelems_deallocator(14, subelems);
2223
2224 return ret;
David Sedlák5379d392019-07-24 10:42:03 +02002225}
2226
2227/**
David Sedlák05404f62019-07-24 14:11:53 +02002228 * @brief Parse choice element.
David Sedlákb7abcfa2019-07-24 12:33:35 +02002229 *
2230 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákad83cf92019-08-13 12:53:53 +02002231 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlákb7abcfa2019-07-24 12:33:35 +02002232 *
2233 * @return LY_ERR values.
2234 */
2235LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002236yin_parse_choice(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta)
David Sedlákb7abcfa2019-07-24 12:33:35 +02002237{
David Sedlák81497a32019-08-13 16:56:26 +02002238 LY_ERR ret = LY_SUCCESS;
2239 struct yin_subelement *subelems = NULL;
David Sedlákb7abcfa2019-07-24 12:33:35 +02002240 struct lysp_node_choice *choice;
2241
2242 /* create new choice */
Michal Vaskob36053d2020-03-26 15:49:30 +01002243 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, choice, next, LY_EMEM);
David Sedlák8d552d62019-08-06 15:29:05 +02002244
David Sedlákb7abcfa2019-07-24 12:33:35 +02002245 choice->nodetype = LYS_CHOICE;
2246 choice->parent = node_meta->parent;
2247
David Sedlákb7abcfa2019-07-24 12:33:35 +02002248 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002249 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2250 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 +02002251
2252 /* parse choice content */
David Sedlák81497a32019-08-13 16:56:26 +02002253 LY_CHECK_RET(subelems_allocator(ctx, 17, (struct lysp_node *)choice, &subelems,
Radek Krejcid6b76452019-09-03 17:03:03 +02002254 LY_STMT_ANYDATA, &choice->child, YIN_SUBELEM_VER2,
2255 LY_STMT_ANYXML, &choice->child, 0,
2256 LY_STMT_CASE, &choice->child, 0,
2257 LY_STMT_CHOICE, &choice->child, YIN_SUBELEM_VER2,
2258 LY_STMT_CONFIG, &choice->flags, YIN_SUBELEM_UNIQUE,
2259 LY_STMT_CONTAINER, &choice->child, 0,
2260 LY_STMT_DEFAULT, &choice->dflt, YIN_SUBELEM_UNIQUE,
2261 LY_STMT_DESCRIPTION, &choice->dsc, YIN_SUBELEM_UNIQUE,
2262 LY_STMT_IF_FEATURE, &choice->iffeatures, 0,
2263 LY_STMT_LEAF, &choice->child, 0,
2264 LY_STMT_LEAF_LIST, &choice->child, 0,
2265 LY_STMT_LIST, &choice->child, 0,
2266 LY_STMT_MANDATORY, &choice->flags, YIN_SUBELEM_UNIQUE,
2267 LY_STMT_REFERENCE, &choice->ref, YIN_SUBELEM_UNIQUE,
2268 LY_STMT_STATUS, &choice->flags, YIN_SUBELEM_UNIQUE,
2269 LY_STMT_WHEN, &choice->when, YIN_SUBELEM_UNIQUE,
2270 LY_STMT_EXTENSION_INSTANCE, NULL, 0
David Sedlák81497a32019-08-13 16:56:26 +02002271 ));
Michal Vaskob36053d2020-03-26 15:49:30 +01002272 ret = yin_parse_content(ctx, subelems, 17, LY_STMT_CHOICE, NULL, &choice->exts);
David Sedlák81497a32019-08-13 16:56:26 +02002273 subelems_deallocator(17, subelems);
2274 return ret;
David Sedlákb7abcfa2019-07-24 12:33:35 +02002275}
2276
2277/**
David Sedlák05404f62019-07-24 14:11:53 +02002278 * @brief Parse input or output element.
2279 *
2280 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbf8a2b72019-08-14 16:48:10 +02002281 * @param[in] inout_kw Identification of input/output element.
David Sedlák05404f62019-07-24 14:11:53 +02002282 * @param[in] inout_meta Meta information about parent node and siblings and input/output pointer to write to.
2283 *
2284 * @return LY_ERR values.
2285 */
2286static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002287yin_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 +02002288{
David Sedlák81497a32019-08-13 16:56:26 +02002289 LY_ERR ret = LY_SUCCESS;
2290 struct yin_subelement *subelems = NULL;
2291
David Sedlák05404f62019-07-24 14:11:53 +02002292 /* initiate structure */
Radek Krejcid6b76452019-09-03 17:03:03 +02002293 inout_meta->inout_p->nodetype = (inout_kw == LY_STMT_INPUT) ? LYS_INPUT : LYS_OUTPUT;
David Sedlák05404f62019-07-24 14:11:53 +02002294 inout_meta->inout_p->parent = inout_meta->parent;
2295
2296 /* check attributes */
Michal Vaskob36053d2020-03-26 15:49:30 +01002297 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2298 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 +02002299
2300 /* parser input/output content */
David Sedlák81497a32019-08-13 16:56:26 +02002301 LY_CHECK_RET(subelems_allocator(ctx, 12, (struct lysp_node *)inout_meta->inout_p, &subelems,
Radek Krejcid6b76452019-09-03 17:03:03 +02002302 LY_STMT_ANYDATA, &inout_meta->inout_p->data, YIN_SUBELEM_VER2,
2303 LY_STMT_ANYXML, &inout_meta->inout_p->data, 0,
2304 LY_STMT_CHOICE, &inout_meta->inout_p->data, 0,
2305 LY_STMT_CONTAINER, &inout_meta->inout_p->data, 0,
2306 LY_STMT_GROUPING, &inout_meta->inout_p->groupings, 0,
2307 LY_STMT_LEAF, &inout_meta->inout_p->data, 0,
2308 LY_STMT_LEAF_LIST, &inout_meta->inout_p->data, 0,
2309 LY_STMT_LIST, &inout_meta->inout_p->data, 0,
2310 LY_STMT_MUST, &inout_meta->inout_p->musts, YIN_SUBELEM_VER2,
2311 LY_STMT_TYPEDEF, &inout_meta->inout_p->typedefs, 0,
2312 LY_STMT_USES, &inout_meta->inout_p->data, 0,
2313 LY_STMT_EXTENSION_INSTANCE, NULL, 0
David Sedlák81497a32019-08-13 16:56:26 +02002314 ));
Michal Vaskob36053d2020-03-26 15:49:30 +01002315 ret = yin_parse_content(ctx, subelems, 12, inout_kw, NULL, &inout_meta->inout_p->exts);
David Sedlák81497a32019-08-13 16:56:26 +02002316 subelems_deallocator(12, subelems);
2317 LY_CHECK_RET(ret);
David Sedlák05404f62019-07-24 14:11:53 +02002318
Michal Vaskob83af8a2020-01-06 09:49:22 +01002319 if (!inout_meta->inout_p->data) {
2320 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_MISSTMT, "data-def-stmt", ly_stmt2str(inout_kw));
2321 return LY_EVALID;
2322 }
2323
David Sedlák05404f62019-07-24 14:11:53 +02002324 /* finalize parent pointers to the reallocated items */
David Sedlák81497a32019-08-13 16:56:26 +02002325 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 +02002326
2327 return LY_SUCCESS;
2328}
2329
David Sedlák992fb7c2019-07-24 16:51:01 +02002330/**
2331 * @brief Parse action element.
2332 *
2333 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák992fb7c2019-07-24 16:51:01 +02002334 * @param[in] act_meta Meta information about parent node and actions to add to.
2335 *
2336 * @return LY_ERR values.
2337 */
David Sedlák85d0eca2019-07-24 15:15:21 +02002338static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002339yin_parse_action(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *act_meta)
David Sedlák85d0eca2019-07-24 15:15:21 +02002340{
David Sedlákbf8a2b72019-08-14 16:48:10 +02002341 struct lysp_action *act, **acts = (struct lysp_action **)act_meta->nodes;
David Sedlák81497a32019-08-13 16:56:26 +02002342 LY_ERR ret = LY_SUCCESS;
2343 struct yin_subelement *subelems = NULL;
David Sedlák85d0eca2019-07-24 15:15:21 +02002344
2345 /* create new action */
Michal Vaskob36053d2020-03-26 15:49:30 +01002346 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *acts, act, LY_EMEM);
Michal Vasko1bf09392020-03-27 12:38:10 +01002347 act->nodetype = act_meta->parent ? LYS_ACTION : LYS_RPC;
David Sedlák85d0eca2019-07-24 15:15:21 +02002348 act->parent = act_meta->parent;
2349
2350 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002351 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2352 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 +02002353
2354 /* parse content */
David Sedlák81497a32019-08-13 16:56:26 +02002355 LY_CHECK_RET(subelems_allocator(ctx, 9, (struct lysp_node *)act, &subelems,
Radek Krejcid6b76452019-09-03 17:03:03 +02002356 LY_STMT_DESCRIPTION, &act->dsc, YIN_SUBELEM_UNIQUE,
2357 LY_STMT_GROUPING, &act->groupings, 0,
2358 LY_STMT_IF_FEATURE, &act->iffeatures, 0,
2359 LY_STMT_INPUT, &act->input, YIN_SUBELEM_UNIQUE,
2360 LY_STMT_OUTPUT, &act->output, YIN_SUBELEM_UNIQUE,
2361 LY_STMT_REFERENCE, &act->ref, YIN_SUBELEM_UNIQUE,
2362 LY_STMT_STATUS, &act->flags, YIN_SUBELEM_UNIQUE,
2363 LY_STMT_TYPEDEF, &act->typedefs, 0,
2364 LY_STMT_EXTENSION_INSTANCE, NULL, 0
David Sedlák81497a32019-08-13 16:56:26 +02002365 ));
Michal Vaskob36053d2020-03-26 15:49:30 +01002366 ret = (yin_parse_content(ctx, subelems, 9, LY_STMT_ACTION, NULL, &act->exts));
David Sedlák81497a32019-08-13 16:56:26 +02002367 subelems_deallocator(9, subelems);
2368 LY_CHECK_RET(ret);
2369
David Sedlák85d0eca2019-07-24 15:15:21 +02002370 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, act->groupings, NULL, NULL, NULL));
2371
2372 return LY_SUCCESS;
2373}
2374
David Sedlák05404f62019-07-24 14:11:53 +02002375/**
David Sedlák992fb7c2019-07-24 16:51:01 +02002376 * @brief Parse augment element.
2377 *
2378 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák992fb7c2019-07-24 16:51:01 +02002379 * @param[in] aug_meta Meta information about parent node and augments to add to.
2380 *
2381 * @return LY_ERR values.
2382 */
2383static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002384yin_parse_augment(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *aug_meta)
David Sedlák992fb7c2019-07-24 16:51:01 +02002385{
2386 struct lysp_augment *aug;
David Sedlákbf8a2b72019-08-14 16:48:10 +02002387 struct lysp_augment **augs = (struct lysp_augment **)aug_meta->nodes;
David Sedlák81497a32019-08-13 16:56:26 +02002388 LY_ERR ret = LY_SUCCESS;
2389 struct yin_subelement *subelems = NULL;
David Sedlák992fb7c2019-07-24 16:51:01 +02002390
2391 /* create new augment */
Michal Vaskob36053d2020-03-26 15:49:30 +01002392 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *augs, aug, LY_EMEM);
David Sedlák992fb7c2019-07-24 16:51:01 +02002393 aug->nodetype = LYS_AUGMENT;
2394 aug->parent = aug_meta->parent;
2395
2396 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002397 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2398 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_TARGET_NODE, &aug->nodeid, Y_STR_ARG, LY_STMT_AUGMENT));
2399 CHECK_NONEMPTY((struct lys_parser_ctx *)ctx, strlen(aug->nodeid), "augment");
David Sedlák992fb7c2019-07-24 16:51:01 +02002400
2401 /* parser augment content */
David Sedlák81497a32019-08-13 16:56:26 +02002402 LY_CHECK_RET(subelems_allocator(ctx, 17, (struct lysp_node *)aug, &subelems,
Radek Krejcid6b76452019-09-03 17:03:03 +02002403 LY_STMT_ACTION, &aug->actions, YIN_SUBELEM_VER2,
2404 LY_STMT_ANYDATA, &aug->child, YIN_SUBELEM_VER2,
2405 LY_STMT_ANYXML, &aug->child, 0,
2406 LY_STMT_CASE, &aug->child, 0,
2407 LY_STMT_CHOICE, &aug->child, 0,
2408 LY_STMT_CONTAINER, &aug->child, 0,
2409 LY_STMT_DESCRIPTION, &aug->dsc, YIN_SUBELEM_UNIQUE,
2410 LY_STMT_IF_FEATURE, &aug->iffeatures, 0,
2411 LY_STMT_LEAF, &aug->child, 0,
2412 LY_STMT_LEAF_LIST, &aug->child, 0,
2413 LY_STMT_LIST, &aug->child, 0,
2414 LY_STMT_NOTIFICATION, &aug->notifs, YIN_SUBELEM_VER2,
2415 LY_STMT_REFERENCE, &aug->ref, YIN_SUBELEM_UNIQUE,
2416 LY_STMT_STATUS, &aug->flags, YIN_SUBELEM_UNIQUE,
2417 LY_STMT_USES, &aug->child, 0,
2418 LY_STMT_WHEN, &aug->when, YIN_SUBELEM_UNIQUE,
2419 LY_STMT_EXTENSION_INSTANCE, NULL, 0
David Sedlák81497a32019-08-13 16:56:26 +02002420 ));
Michal Vaskob36053d2020-03-26 15:49:30 +01002421 ret = yin_parse_content(ctx, subelems, 17, LY_STMT_AUGMENT, NULL, &aug->exts);
David Sedlák81497a32019-08-13 16:56:26 +02002422 subelems_deallocator(17, subelems);
2423 LY_CHECK_RET(ret);
David Sedlák992fb7c2019-07-24 16:51:01 +02002424
2425 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, NULL, NULL, aug->actions, aug->notifs));
2426
2427 return LY_SUCCESS;
2428}
2429
David Sedlák8b754462019-07-25 16:22:13 +02002430/**
2431 * @brief Parse deviate element.
2432 *
2433 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák8b754462019-07-25 16:22:13 +02002434 * @param[in] deviates Deviates to add to.
2435 *
2436 * @return LY_ERR values.
2437 */
David Sedlák4ffcec82019-07-25 15:10:21 +02002438static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002439yin_parse_deviate(struct lys_yin_parser_ctx *ctx, struct lysp_deviate **deviates)
David Sedlák4ffcec82019-07-25 15:10:21 +02002440{
2441 LY_ERR ret = LY_SUCCESS;
2442 uint8_t dev_mod;
2443 const char *temp_val;
David Sedlák8d552d62019-08-06 15:29:05 +02002444 struct lysp_deviate *d;
David Sedlák4ffcec82019-07-25 15:10:21 +02002445 struct lysp_deviate_add *d_add = NULL;
2446 struct lysp_deviate_rpl *d_rpl = NULL;
2447 struct lysp_deviate_del *d_del = NULL;
2448
2449 /* 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_VALUE, &temp_val, Y_STR_ARG, LY_STMT_DEVIATE));
David Sedlák4ffcec82019-07-25 15:10:21 +02002452
2453 if (strcmp(temp_val, "not-supported") == 0) {
2454 dev_mod = LYS_DEV_NOT_SUPPORTED;
2455 } else if (strcmp(temp_val, "add") == 0) {
2456 dev_mod = LYS_DEV_ADD;
2457 } else if (strcmp(temp_val, "replace") == 0) {
2458 dev_mod = LYS_DEV_REPLACE;
2459 } else if (strcmp(temp_val, "delete") == 0) {
2460 dev_mod = LYS_DEV_DELETE;
2461 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02002462 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS4, temp_val, "value", "deviate",
2463 "not-supported", "add", "replace", "delete");
Michal Vaskob36053d2020-03-26 15:49:30 +01002464 FREE_STRING(ctx->xmlctx->ctx, temp_val);
David Sedlák4ffcec82019-07-25 15:10:21 +02002465 return LY_EVALID;
2466 }
Michal Vaskob36053d2020-03-26 15:49:30 +01002467 FREE_STRING(ctx->xmlctx->ctx, temp_val);
David Sedlák4ffcec82019-07-25 15:10:21 +02002468
2469 if (dev_mod == LYS_DEV_NOT_SUPPORTED) {
2470 d = calloc(1, sizeof *d);
Michal Vaskob36053d2020-03-26 15:49:30 +01002471 LY_CHECK_ERR_RET(!d, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
David Sedlák4ffcec82019-07-25 15:10:21 +02002472 struct yin_subelement subelems[1] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002473 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
2474 };
Michal Vaskob36053d2020-03-26 15:49:30 +01002475 ret = yin_parse_content(ctx, subelems, 1, LY_STMT_DEVIATE, NULL, &d->exts);
David Sedlák4ffcec82019-07-25 15:10:21 +02002476
2477 } else if (dev_mod == LYS_DEV_ADD) {
2478 d_add = calloc(1, sizeof *d_add);
Michal Vaskob36053d2020-03-26 15:49:30 +01002479 LY_CHECK_ERR_RET(!d_add, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
David Sedlák4ffcec82019-07-25 15:10:21 +02002480 d = (struct lysp_deviate *)d_add;
2481 struct minmax_dev_meta min = {&d_add->min, &d_add->flags, &d_add->exts};
2482 struct minmax_dev_meta max = {&d_add->max, &d_add->flags, &d_add->exts};
2483 struct yin_subelement subelems[9] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002484 {LY_STMT_CONFIG, &d_add->flags, YIN_SUBELEM_UNIQUE},
2485 {LY_STMT_DEFAULT, &d_add->dflts, 0},
2486 {LY_STMT_MANDATORY, &d_add->flags, YIN_SUBELEM_UNIQUE},
2487 {LY_STMT_MAX_ELEMENTS, &max, YIN_SUBELEM_UNIQUE},
2488 {LY_STMT_MIN_ELEMENTS, &min, YIN_SUBELEM_UNIQUE},
2489 {LY_STMT_MUST, &d_add->musts, 0},
2490 {LY_STMT_UNIQUE, &d_add->uniques, 0},
2491 {LY_STMT_UNITS, &d_add->units, YIN_SUBELEM_UNIQUE},
2492 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
2493 };
Michal Vaskob36053d2020-03-26 15:49:30 +01002494 ret = yin_parse_content(ctx, subelems, 9, LY_STMT_DEVIATE, NULL, &d_add->exts);
David Sedlák4ffcec82019-07-25 15:10:21 +02002495
2496 } else if (dev_mod == LYS_DEV_REPLACE) {
2497 d_rpl = calloc(1, sizeof *d_rpl);
Michal Vaskob36053d2020-03-26 15:49:30 +01002498 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
David Sedlák4ffcec82019-07-25 15:10:21 +02002499 d = (struct lysp_deviate *)d_rpl;
2500 struct minmax_dev_meta min = {&d_rpl->min, &d_rpl->flags, &d_rpl->exts};
2501 struct minmax_dev_meta max = {&d_rpl->max, &d_rpl->flags, &d_rpl->exts};
2502 struct yin_subelement subelems[8] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002503 {LY_STMT_CONFIG, &d_rpl->flags, YIN_SUBELEM_UNIQUE},
2504 {LY_STMT_DEFAULT, &d_rpl->dflt, YIN_SUBELEM_UNIQUE},
2505 {LY_STMT_MANDATORY, &d_rpl->flags, YIN_SUBELEM_UNIQUE},
2506 {LY_STMT_MAX_ELEMENTS, &max, YIN_SUBELEM_UNIQUE},
2507 {LY_STMT_MIN_ELEMENTS, &min, YIN_SUBELEM_UNIQUE},
2508 {LY_STMT_TYPE, &d_rpl->type, YIN_SUBELEM_UNIQUE},
2509 {LY_STMT_UNITS, &d_rpl->units, YIN_SUBELEM_UNIQUE},
2510 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
2511 };
Michal Vaskob36053d2020-03-26 15:49:30 +01002512 ret = yin_parse_content(ctx, subelems, 8, LY_STMT_DEVIATE, NULL, &d_rpl->exts);
David Sedlák4ffcec82019-07-25 15:10:21 +02002513
2514 } else {
2515 d_del = calloc(1, sizeof *d_del);
Michal Vaskob36053d2020-03-26 15:49:30 +01002516 LY_CHECK_ERR_RET(!d_del, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
David Sedlák4ffcec82019-07-25 15:10:21 +02002517 d = (struct lysp_deviate *)d_del;
2518 struct yin_subelement subelems[5] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002519 {LY_STMT_DEFAULT, &d_del->dflts, 0},
2520 {LY_STMT_MUST, &d_del->musts, 0},
2521 {LY_STMT_UNIQUE, &d_del->uniques, 0},
2522 {LY_STMT_UNITS, &d_del->units, YIN_SUBELEM_UNIQUE},
2523 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
2524 };
Michal Vaskob36053d2020-03-26 15:49:30 +01002525 ret = yin_parse_content(ctx, subelems, 5, LY_STMT_DEVIATE, NULL, &d_del->exts);
David Sedlák4ffcec82019-07-25 15:10:21 +02002526 }
2527 LY_CHECK_GOTO(ret, cleanup);
2528
2529 d->mod = dev_mod;
2530 /* insert into siblings */
David Sedlák8d552d62019-08-06 15:29:05 +02002531 LY_LIST_INSERT(deviates, d, next);
David Sedlák4ffcec82019-07-25 15:10:21 +02002532
2533 return ret;
2534
2535cleanup:
2536 free(d);
David Sedlák4ffcec82019-07-25 15:10:21 +02002537 return ret;
2538}
2539
David Sedlák992fb7c2019-07-24 16:51:01 +02002540/**
David Sedlák8b754462019-07-25 16:22:13 +02002541 * @brief Parse deviation element.
2542 *
2543 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák8b754462019-07-25 16:22:13 +02002544 * @param[in] deviations Deviations to add to.
2545 *
2546 * @return LY_ERR values.
2547 */
2548static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002549yin_parse_deviation(struct lys_yin_parser_ctx *ctx, struct lysp_deviation **deviations)
David Sedlák8b754462019-07-25 16:22:13 +02002550{
2551 struct lysp_deviation *dev;
2552
2553 /* create new deviation */
Michal Vaskob36053d2020-03-26 15:49:30 +01002554 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *deviations, dev, LY_EMEM);
David Sedlák8b754462019-07-25 16:22:13 +02002555
2556 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002557 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2558 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_TARGET_NODE, &dev->nodeid, Y_STR_ARG, LY_STMT_DEVIATION));
2559 CHECK_NONEMPTY((struct lys_parser_ctx *)ctx, strlen(dev->nodeid), "deviation");
David Sedlák8b754462019-07-25 16:22:13 +02002560 struct yin_subelement subelems[4] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002561 {LY_STMT_DESCRIPTION, &dev->dsc, YIN_SUBELEM_UNIQUE},
2562 {LY_STMT_DEVIATE, &dev->deviates, YIN_SUBELEM_MANDATORY},
2563 {LY_STMT_REFERENCE, &dev->ref, YIN_SUBELEM_UNIQUE},
2564 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
2565 };
Michal Vaskob36053d2020-03-26 15:49:30 +01002566 return yin_parse_content(ctx, subelems, 4, LY_STMT_DEVIATION, NULL, &dev->exts);
David Sedlák8b754462019-07-25 16:22:13 +02002567}
2568
2569/**
David Sedlákb4e44562019-07-04 15:42:12 +02002570 * @brief Map keyword type to substatement info.
2571 *
2572 * @param[in] kw Keyword type.
2573 *
2574 * @return correct LYEXT_SUBSTMT information.
2575 */
2576static LYEXT_SUBSTMT
Radek Krejcid6b76452019-09-03 17:03:03 +02002577kw2lyext_substmt(enum ly_stmt kw)
David Sedlákb4e44562019-07-04 15:42:12 +02002578{
2579 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002580 case LY_STMT_ARGUMENT:
David Sedlákb4e44562019-07-04 15:42:12 +02002581 return LYEXT_SUBSTMT_ARGUMENT;
Radek Krejcid6b76452019-09-03 17:03:03 +02002582 case LY_STMT_BASE:
David Sedlákb4e44562019-07-04 15:42:12 +02002583 return LYEXT_SUBSTMT_BASE;
Radek Krejcid6b76452019-09-03 17:03:03 +02002584 case LY_STMT_BELONGS_TO:
David Sedlákb4e44562019-07-04 15:42:12 +02002585 return LYEXT_SUBSTMT_BELONGSTO;
Radek Krejcid6b76452019-09-03 17:03:03 +02002586 case LY_STMT_CONTACT:
David Sedlákb4e44562019-07-04 15:42:12 +02002587 return LYEXT_SUBSTMT_CONTACT;
Radek Krejcid6b76452019-09-03 17:03:03 +02002588 case LY_STMT_DEFAULT:
David Sedlákb4e44562019-07-04 15:42:12 +02002589 return LYEXT_SUBSTMT_DEFAULT;
Radek Krejcid6b76452019-09-03 17:03:03 +02002590 case LY_STMT_DESCRIPTION:
David Sedlákb4e44562019-07-04 15:42:12 +02002591 return LYEXT_SUBSTMT_DESCRIPTION;
Radek Krejcid6b76452019-09-03 17:03:03 +02002592 case LY_STMT_ERROR_APP_TAG:
David Sedlákb4e44562019-07-04 15:42:12 +02002593 return LYEXT_SUBSTMT_ERRTAG;
Radek Krejcid6b76452019-09-03 17:03:03 +02002594 case LY_STMT_ERROR_MESSAGE:
David Sedlákb4e44562019-07-04 15:42:12 +02002595 return LYEXT_SUBSTMT_ERRMSG;
Radek Krejcid6b76452019-09-03 17:03:03 +02002596 case LY_STMT_KEY:
David Sedlákb4e44562019-07-04 15:42:12 +02002597 return LYEXT_SUBSTMT_KEY;
Radek Krejcid6b76452019-09-03 17:03:03 +02002598 case LY_STMT_NAMESPACE:
David Sedlákb4e44562019-07-04 15:42:12 +02002599 return LYEXT_SUBSTMT_NAMESPACE;
Radek Krejcid6b76452019-09-03 17:03:03 +02002600 case LY_STMT_ORGANIZATION:
David Sedlákb4e44562019-07-04 15:42:12 +02002601 return LYEXT_SUBSTMT_ORGANIZATION;
Radek Krejcid6b76452019-09-03 17:03:03 +02002602 case LY_STMT_PATH:
David Sedlákb4e44562019-07-04 15:42:12 +02002603 return LYEXT_SUBSTMT_PATH;
Radek Krejcid6b76452019-09-03 17:03:03 +02002604 case LY_STMT_PREFIX:
David Sedlákb4e44562019-07-04 15:42:12 +02002605 return LYEXT_SUBSTMT_PREFIX;
Radek Krejcid6b76452019-09-03 17:03:03 +02002606 case LY_STMT_PRESENCE:
David Sedlákb4e44562019-07-04 15:42:12 +02002607 return LYEXT_SUBSTMT_PRESENCE;
Radek Krejcid6b76452019-09-03 17:03:03 +02002608 case LY_STMT_REFERENCE:
David Sedlákb4e44562019-07-04 15:42:12 +02002609 return LYEXT_SUBSTMT_REFERENCE;
Radek Krejcid6b76452019-09-03 17:03:03 +02002610 case LY_STMT_REVISION_DATE:
David Sedlákb4e44562019-07-04 15:42:12 +02002611 return LYEXT_SUBSTMT_REVISIONDATE;
Radek Krejcid6b76452019-09-03 17:03:03 +02002612 case LY_STMT_UNITS:
David Sedlákb4e44562019-07-04 15:42:12 +02002613 return LYEXT_SUBSTMT_UNITS;
Radek Krejcid6b76452019-09-03 17:03:03 +02002614 case LY_STMT_VALUE:
David Sedlákb4e44562019-07-04 15:42:12 +02002615 return LYEXT_SUBSTMT_VALUE;
Radek Krejcid6b76452019-09-03 17:03:03 +02002616 case LY_STMT_YANG_VERSION:
David Sedlákb4e44562019-07-04 15:42:12 +02002617 return LYEXT_SUBSTMT_VERSION;
Radek Krejcid6b76452019-09-03 17:03:03 +02002618 case LY_STMT_MODIFIER:
David Sedlákb4e44562019-07-04 15:42:12 +02002619 return LYEXT_SUBSTMT_MODIFIER;
Radek Krejcid6b76452019-09-03 17:03:03 +02002620 case LY_STMT_REQUIRE_INSTANCE:
David Sedlákb4e44562019-07-04 15:42:12 +02002621 return LYEXT_SUBSTMT_REQINSTANCE;
Radek Krejcid6b76452019-09-03 17:03:03 +02002622 case LY_STMT_YIN_ELEMENT:
David Sedlákb4e44562019-07-04 15:42:12 +02002623 return LYEXT_SUBSTMT_YINELEM;
Radek Krejcid6b76452019-09-03 17:03:03 +02002624 case LY_STMT_CONFIG:
David Sedlákb4e44562019-07-04 15:42:12 +02002625 return LYEXT_SUBSTMT_CONFIG;
Radek Krejcid6b76452019-09-03 17:03:03 +02002626 case LY_STMT_MANDATORY:
David Sedlákb4e44562019-07-04 15:42:12 +02002627 return LYEXT_SUBSTMT_MANDATORY;
Radek Krejcid6b76452019-09-03 17:03:03 +02002628 case LY_STMT_ORDERED_BY:
David Sedlákb4e44562019-07-04 15:42:12 +02002629 return LYEXT_SUBSTMT_ORDEREDBY;
Radek Krejcid6b76452019-09-03 17:03:03 +02002630 case LY_STMT_STATUS:
David Sedlákb4e44562019-07-04 15:42:12 +02002631 return LYEXT_SUBSTMT_STATUS;
Radek Krejcid6b76452019-09-03 17:03:03 +02002632 case LY_STMT_FRACTION_DIGITS:
David Sedlákb4e44562019-07-04 15:42:12 +02002633 return LYEXT_SUBSTMT_FRACDIGITS;
Radek Krejcid6b76452019-09-03 17:03:03 +02002634 case LY_STMT_MAX_ELEMENTS:
David Sedlákb4e44562019-07-04 15:42:12 +02002635 return LYEXT_SUBSTMT_MAX;
Radek Krejcid6b76452019-09-03 17:03:03 +02002636 case LY_STMT_MIN_ELEMENTS:
David Sedlákb4e44562019-07-04 15:42:12 +02002637 return LYEXT_SUBSTMT_MIN;
Radek Krejcid6b76452019-09-03 17:03:03 +02002638 case LY_STMT_POSITION:
David Sedlákb4e44562019-07-04 15:42:12 +02002639 return LYEXT_SUBSTMT_POSITION;
Radek Krejcid6b76452019-09-03 17:03:03 +02002640 case LY_STMT_UNIQUE:
David Sedlákb4e44562019-07-04 15:42:12 +02002641 return LYEXT_SUBSTMT_UNIQUE;
Radek Krejcid6b76452019-09-03 17:03:03 +02002642 case LY_STMT_IF_FEATURE:
David Sedlákb4e44562019-07-04 15:42:12 +02002643 return LYEXT_SUBSTMT_IFFEATURE;
2644 default:
2645 return LYEXT_SUBSTMT_SELF;
2646 }
2647}
2648
David Sedlákc5b20842019-08-13 10:18:31 +02002649/**
2650 * @brief map keyword to keyword-group.
2651 *
2652 * @param[in] ctx YIN parser context used for logging.
2653 * @param[in] kw Keyword that is child of module or submodule.
2654 * @param[out] group Group of keyword.
2655 *
2656 * @return LY_SUCCESS on success LY_EINT if kw can't be mapped to kw_group, should not happen if called correctly.
2657 */
David Sedláke6cd89e2019-08-07 12:46:02 +02002658static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002659kw2kw_group(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, enum yang_module_stmt *group)
David Sedláke6cd89e2019-08-07 12:46:02 +02002660{
2661 switch (kw) {
Radek Krejci0f969882020-08-21 16:56:47 +02002662 /* module header */
2663 case LY_STMT_NONE:
2664 case LY_STMT_NAMESPACE:
2665 case LY_STMT_PREFIX:
2666 case LY_STMT_BELONGS_TO:
2667 case LY_STMT_YANG_VERSION:
2668 *group = Y_MOD_MODULE_HEADER;
2669 break;
2670 /* linkage */
2671 case LY_STMT_INCLUDE:
2672 case LY_STMT_IMPORT:
2673 *group = Y_MOD_LINKAGE;
2674 break;
2675 /* meta */
2676 case LY_STMT_ORGANIZATION:
2677 case LY_STMT_CONTACT:
2678 case LY_STMT_DESCRIPTION:
2679 case LY_STMT_REFERENCE:
2680 *group = Y_MOD_META;
2681 break;
2682 /* revision */
2683 case LY_STMT_REVISION:
2684 *group = Y_MOD_REVISION;
2685 break;
2686 /* body */
2687 case LY_STMT_ANYDATA:
2688 case LY_STMT_ANYXML:
2689 case LY_STMT_AUGMENT:
2690 case LY_STMT_CHOICE:
2691 case LY_STMT_CONTAINER:
2692 case LY_STMT_DEVIATION:
2693 case LY_STMT_EXTENSION:
2694 case LY_STMT_FEATURE:
2695 case LY_STMT_GROUPING:
2696 case LY_STMT_IDENTITY:
2697 case LY_STMT_LEAF:
2698 case LY_STMT_LEAF_LIST:
2699 case LY_STMT_LIST:
2700 case LY_STMT_NOTIFICATION:
2701 case LY_STMT_RPC:
2702 case LY_STMT_TYPEDEF:
2703 case LY_STMT_USES:
2704 case LY_STMT_EXTENSION_INSTANCE:
2705 *group = Y_MOD_BODY;
2706 break;
2707 default:
2708 LOGINT(ctx->xmlctx->ctx);
2709 return LY_EINT;
David Sedláke6cd89e2019-08-07 12:46:02 +02002710 }
2711
2712 return LY_SUCCESS;
2713}
2714
David Sedlákc5b20842019-08-13 10:18:31 +02002715/**
2716 * @brief Check if relative order of two keywords is valid.
2717 *
2718 * @param[in] ctx YIN parser context used for logging.
2719 * @param[in] kw Current keyword.
2720 * @param[in] next_kw Next keyword.
Radek Krejcid6b76452019-09-03 17:03:03 +02002721 * @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 +02002722 * because relative order is required only in module and submodule sub-elements, used for logging.
2723 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02002724 * @return LY_SUCCESS on success and LY_EVALID if relative order is invalid.
David Sedlákc5b20842019-08-13 10:18:31 +02002725 */
David Sedláke6cd89e2019-08-07 12:46:02 +02002726static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002727yin_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 +02002728{
Radek Krejcid6b76452019-09-03 17:03:03 +02002729 assert(parrent == LY_STMT_MODULE || parrent == LY_STMT_SUBMODULE);
David Sedláke6cd89e2019-08-07 12:46:02 +02002730 enum yang_module_stmt gr, next_gr;
2731
2732 LY_CHECK_RET(kw2kw_group(ctx, kw, &gr));
2733 LY_CHECK_RET(kw2kw_group(ctx, next_kw, &next_gr));
2734
2735 if (gr > next_gr) {
2736 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INORDER_YIN, ly_stmt2str(parrent), ly_stmt2str(next_kw), ly_stmt2str(kw));
2737 return LY_EVALID;
2738 }
2739
2740 return LY_SUCCESS;
2741}
2742
David Sedlákc5dd0942019-09-13 10:23:59 +02002743/**
2744 * @brief Get element name prefixed by full URI of xml namespace.
2745 *
2746 * @param[in] ctx YIN parser context used for logging and to get inormation about xml namespaces.
2747 * @param[in] name Name of element.
2748 * @param[in] name_len Length of element name.
2749 * @param[in] prefix Prefix of element.
2750 * @param[in] prefix_len Length of element prefix.
2751 *
2752 * @return Element name prefixed by URI on success, NULL on failure.
2753 */
2754static const char *
Michal Vaskob36053d2020-03-26 15:49:30 +01002755name2nsname(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 +02002756{
Michal Vaskoc8a230d2020-08-14 12:17:10 +02002757 const struct lyxml_ns *ns = lyxml_ns_get(&ctx->xmlctx->ns, prefix, prefix_len);
Michal Vaskob36053d2020-03-26 15:49:30 +01002758 LY_CHECK_ERR_RET(!ns, LOGINT(ctx->xmlctx->ctx), NULL);
David Sedlák068dd352019-09-13 09:08:22 +02002759
Radek Krejci02728562019-09-18 14:13:05 +02002760 if (!strcmp(ns->uri, YIN_NS_URI)) {
2761 /* standard YANG statement in YIN namespace - keep it unprefixed as in case of YANG */
Michal Vaskob36053d2020-03-26 15:49:30 +01002762 return lydict_insert(ctx->xmlctx->ctx, name, name_len);
Radek Krejci02728562019-09-18 14:13:05 +02002763 }
2764 /* some statement in special namespace (extension instance) */
David Sedláke0ef1c62019-09-13 10:05:55 +02002765 size_t ns_len = strlen(ns->uri);
2766 size_t len = ns_len + name_len + 1; /* +1 because of ':' delimiter between ns and actual name */
2767
2768 char *result;
2769 char *temp;
2770 temp = result = malloc(sizeof(*temp) * (len + 1)); /* +1 for '\0' terminator */
Michal Vaskob36053d2020-03-26 15:49:30 +01002771 LY_CHECK_ERR_RET(!temp, LOGMEM(ctx->xmlctx->ctx), NULL);
David Sedláke0ef1c62019-09-13 10:05:55 +02002772
2773 strcpy(result, ns->uri);
2774 result[ns_len] = ':';
2775 temp = &result[ns_len + 1];
2776 strncpy(temp, name, name_len);
2777 result[len] = '\0';
2778
Michal Vaskob36053d2020-03-26 15:49:30 +01002779 return lydict_insert_zc(ctx->xmlctx->ctx, result);
David Sedlák068dd352019-09-13 09:08:22 +02002780}
2781
David Sedlákd6e56892019-07-01 15:40:24 +02002782LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002783yin_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 +02002784 enum ly_stmt current_element, const char **text_content, struct lysp_ext_instance **exts)
David Sedlákd6e56892019-07-01 15:40:24 +02002785{
2786 LY_ERR ret = LY_SUCCESS;
Michal Vaskob36053d2020-03-26 15:49:30 +01002787 enum LYXML_PARSER_STATUS next_status;
Radek Krejcid6b76452019-09-03 17:03:03 +02002788 enum ly_stmt kw = LY_STMT_NONE, last_kw = LY_STMT_NONE;
David Sedlák1af868e2019-07-17 17:03:14 +02002789 struct yin_subelement *subelem = NULL;
David Sedlák09e18c92019-07-18 11:17:11 +02002790
Michal Vaskob36053d2020-03-26 15:49:30 +01002791 assert(ctx->xmlctx->status == LYXML_ELEM_CONTENT);
David Sedlákd6e56892019-07-01 15:40:24 +02002792
Michal Vaskob36053d2020-03-26 15:49:30 +01002793 if (ctx->xmlctx->ws_only) {
2794 /* check whether there are any children */
2795 LY_CHECK_GOTO(ret = lyxml_ctx_peek(ctx->xmlctx, &next_status), cleanup);
2796 } else {
2797 /* we want to parse the value */
2798 next_status = LYXML_ELEM_CLOSE;
2799 }
2800
2801 if (next_status == LYXML_ELEMENT) {
2802 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
2803
2804 /* current element has subelements as content */
2805 while (ctx->xmlctx->status == LYXML_ELEMENT) {
2806 /* match keyword */
2807 last_kw = kw;
2808 kw = yin_match_keyword(ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, ctx->xmlctx->prefix,
2809 ctx->xmlctx->prefix_len, current_element);
2810
2811 /* check if this element can be child of current element */
2812 subelem = get_record(kw, subelem_info_size, subelem_info);
2813 if (!subelem) {
2814 if (current_element == LY_STMT_DEVIATE && isdevsub(kw)) {
2815 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INDEV_YIN, ly_stmt2str(kw));
2816 } else {
2817 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_UNEXP_SUBELEM, ctx->xmlctx->name_len,
2818 ctx->xmlctx->name, ly_stmt2str(current_element));
2819 }
2820 ret = LY_EVALID;
2821 goto cleanup;
2822 }
2823
2824 /* relative order is required only in module and submodule sub-elements */
2825 if (current_element == LY_STMT_MODULE || current_element == LY_STMT_SUBMODULE) {
2826 ret = yin_check_relative_order(ctx, last_kw, kw, current_element);
2827 LY_CHECK_GOTO(ret, cleanup);
2828 }
2829
2830 /* flag check */
2831 if ((subelem->flags & YIN_SUBELEM_UNIQUE) && (subelem->flags & YIN_SUBELEM_PARSED)) {
2832 /* subelement uniquenes */
2833 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_SUBELEM_REDEF, ly_stmt2str(kw), ly_stmt2str(current_element));
2834 return LY_EVALID;
2835 }
2836 if (subelem->flags & YIN_SUBELEM_FIRST) {
2837 /* subelement is supposed to be defined as first subelement */
2838 ret = yin_check_subelem_first_constraint(ctx, subelem_info, subelem_info_size, current_element, subelem);
2839 LY_CHECK_GOTO(ret, cleanup);
2840 }
2841 if (subelem->flags & YIN_SUBELEM_VER2) {
2842 /* subelement is supported only in version 1.1 or higher */
2843 if (ctx->mod_version < 2) {
2844 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 +02002845 ret = LY_EVALID;
2846 goto cleanup;
2847 }
David Sedlákd6e56892019-07-01 15:40:24 +02002848 }
Michal Vaskob36053d2020-03-26 15:49:30 +01002849 /* note that element was parsed for easy uniqueness check in next iterations */
2850 subelem->flags |= YIN_SUBELEM_PARSED;
2851
2852 switch (kw) {
2853 /* call responsible function */
2854 case LY_STMT_EXTENSION_INSTANCE:
2855 ret = yin_parse_extension_instance(ctx, kw2lyext_substmt(current_element),
Michal Vasko22df3f02020-08-24 13:29:22 +02002856 (subelem->dest) ? *((LY_ARRAY_COUNT_TYPE *)subelem->dest) : 0, exts);
Michal Vaskob36053d2020-03-26 15:49:30 +01002857 break;
2858 case LY_STMT_ACTION:
2859 case LY_STMT_RPC:
2860 ret = yin_parse_action(ctx, (struct tree_node_meta *)subelem->dest);
2861 break;
2862 case LY_STMT_ANYDATA:
2863 case LY_STMT_ANYXML:
2864 ret = yin_parse_any(ctx, kw, (struct tree_node_meta *)subelem->dest);
2865 break;
2866 case LY_STMT_ARGUMENT:
2867 ret = yin_parse_argument(ctx, (struct yin_argument_meta *)subelem->dest, exts);
2868 break;
2869 case LY_STMT_AUGMENT:
2870 ret = yin_parse_augment(ctx, (struct tree_node_meta *)subelem->dest);
2871 break;
2872 case LY_STMT_BASE:
2873 ret = yin_parse_base(ctx, current_element, subelem->dest, exts);
2874 break;
2875 case LY_STMT_BELONGS_TO:
2876 ret = yin_parse_belongs_to(ctx, (struct lysp_submodule *)subelem->dest, exts);
2877 break;
2878 case LY_STMT_BIT:
2879 ret = yin_parse_bit(ctx, (struct lysp_type *)subelem->dest);
2880 break;
2881 case LY_STMT_CASE:
2882 ret = yin_parse_case(ctx, (struct tree_node_meta *)subelem->dest);
2883 break;
2884 case LY_STMT_CHOICE:
2885 ret = yin_parse_choice(ctx, (struct tree_node_meta *)subelem->dest);
2886 break;
2887 case LY_STMT_CONFIG:
2888 ret = yin_parse_config(ctx, (uint16_t *)subelem->dest, exts);
2889 break;
2890 case LY_STMT_CONTACT:
2891 case LY_STMT_DESCRIPTION:
2892 case LY_STMT_ORGANIZATION:
2893 case LY_STMT_REFERENCE:
2894 ret = yin_parse_meta(ctx, kw, (const char **)subelem->dest, exts);
2895 break;
2896 case LY_STMT_CONTAINER:
2897 ret = yin_parse_container(ctx, (struct tree_node_meta *)subelem->dest);
2898 break;
2899 case LY_STMT_DEFAULT:
2900 case LY_STMT_ERROR_APP_TAG:
2901 case LY_STMT_KEY:
2902 case LY_STMT_PRESENCE:
2903 ret = yin_parse_simple_elem(ctx, kw, subelem, YIN_ARG_VALUE, Y_STR_ARG, exts);
2904 break;
2905 case LY_STMT_DEVIATE:
2906 ret = yin_parse_deviate(ctx, (struct lysp_deviate **)subelem->dest);
2907 break;
2908 case LY_STMT_DEVIATION:
2909 ret = yin_parse_deviation(ctx, (struct lysp_deviation **)subelem->dest);
2910 break;
2911 case LY_STMT_ENUM:
2912 ret = yin_parse_enum(ctx, (struct lysp_type *)subelem->dest);
2913 break;
2914 case LY_STMT_ERROR_MESSAGE:
2915 ret = yin_parse_err_msg(ctx, (const char **)subelem->dest, exts);
2916 break;
2917 case LY_STMT_EXTENSION:
2918 ret = yin_parse_extension(ctx, (struct lysp_ext **)subelem->dest);
2919 break;
2920 case LY_STMT_FEATURE:
2921 ret = yin_parse_feature(ctx, (struct lysp_feature **)subelem->dest);
2922 break;
2923 case LY_STMT_FRACTION_DIGITS:
2924 ret = yin_parse_fracdigits(ctx, (struct lysp_type *)subelem->dest);
2925 break;
2926 case LY_STMT_GROUPING:
2927 ret = yin_parse_grouping(ctx, (struct tree_node_meta *)subelem->dest);
2928 break;
2929 case LY_STMT_IDENTITY:
2930 ret = yin_parse_identity(ctx, (struct lysp_ident **)subelem->dest);
2931 break;
2932 case LY_STMT_IF_FEATURE:
2933 case LY_STMT_UNITS:
2934 ret = yin_parse_simple_elem(ctx, kw, subelem, YIN_ARG_NAME, Y_STR_ARG, exts);
2935 break;
2936 case LY_STMT_IMPORT:
2937 ret = yin_parse_import(ctx, (struct import_meta *)subelem->dest);
2938 break;
2939 case LY_STMT_INCLUDE:
2940 ret = yin_parse_include(ctx, (struct include_meta *)subelem->dest);
2941 break;
2942 case LY_STMT_INPUT:
2943 case LY_STMT_OUTPUT:
2944 ret = yin_parse_inout(ctx, kw, (struct inout_meta *)subelem->dest);
2945 break;
2946 case LY_STMT_LEAF:
2947 ret = yin_parse_leaf(ctx, (struct tree_node_meta *)subelem->dest);
2948 break;
2949 case LY_STMT_LEAF_LIST:
2950 ret = yin_parse_leaflist(ctx, (struct tree_node_meta *)subelem->dest);
2951 break;
2952 case LY_STMT_LENGTH:
2953 ret = yin_parse_length(ctx, (struct lysp_type *)subelem->dest);
2954 break;
2955 case LY_STMT_LIST:
2956 ret = yin_parse_list(ctx, (struct tree_node_meta *)subelem->dest);
2957 break;
2958 case LY_STMT_MANDATORY:
2959 ret = yin_parse_mandatory(ctx, (uint16_t *)subelem->dest, exts);
2960 break;
2961 case LY_STMT_MAX_ELEMENTS:
2962 case LY_STMT_MIN_ELEMENTS:
2963 ret = yin_parse_minmax(ctx, current_element, kw, subelem->dest);
2964 break;
2965 case LY_STMT_MODIFIER:
2966 ret = yin_parse_modifier(ctx, (const char **)subelem->dest, exts);
2967 break;
2968 case LY_STMT_MUST:
2969 ret = yin_parse_must(ctx, (struct lysp_restr **)subelem->dest);
2970 break;
2971 case LY_STMT_NAMESPACE:
2972 ret = yin_parse_simple_elem(ctx, kw, subelem, YIN_ARG_URI, Y_STR_ARG, exts);
2973 break;
2974 case LY_STMT_NOTIFICATION:
2975 ret = yin_parse_notification(ctx, (struct tree_node_meta *)subelem->dest);
2976 break;
2977 case LY_STMT_ORDERED_BY:
2978 ret = yin_parse_orderedby(ctx, (uint16_t *)subelem->dest, exts);
2979 break;
2980 case LY_STMT_PATH:
2981 ret = yin_parse_path(ctx, kw, (struct lysp_type *)subelem->dest);
2982 break;
2983 case LY_STMT_PATTERN:
2984 ret = yin_parse_pattern(ctx, (struct lysp_type *)subelem->dest);
2985 break;
2986 case LY_STMT_VALUE:
2987 case LY_STMT_POSITION:
2988 ret = yin_parse_value_pos(ctx, kw, (struct lysp_type_enum *)subelem->dest);
2989 break;
2990 case LY_STMT_PREFIX:
2991 ret = yin_parse_simple_elem(ctx, kw, subelem, YIN_ARG_VALUE, Y_IDENTIF_ARG, exts);
2992 break;
2993 case LY_STMT_RANGE:
2994 ret = yin_parse_range(ctx, (struct lysp_type *)subelem->dest);
2995 break;
2996 case LY_STMT_REFINE:
2997 ret = yin_parse_refine(ctx, (struct lysp_refine **)subelem->dest);
2998 break;
2999 case LY_STMT_REQUIRE_INSTANCE:
3000 ret = yin_pasrse_reqinstance(ctx, (struct lysp_type *)subelem->dest);
3001 break;
3002 case LY_STMT_REVISION:
3003 ret = yin_parse_revision(ctx, (struct lysp_revision **)subelem->dest);
3004 break;
3005 case LY_STMT_REVISION_DATE:
3006 ret = yin_parse_revision_date(ctx, (char *)subelem->dest, exts);
3007 break;
3008 case LY_STMT_STATUS:
3009 ret = yin_parse_status(ctx, (uint16_t *)subelem->dest, exts);
3010 break;
3011 case LY_STMT_TYPE:
3012 ret = yin_parse_type(ctx, current_element, subelem);
3013 break;
3014 case LY_STMT_TYPEDEF:
3015 ret = yin_parse_typedef(ctx, (struct tree_node_meta *)subelem->dest);
3016 break;
3017 case LY_STMT_UNIQUE:
3018 ret = yin_parse_simple_elem(ctx, kw, subelem, YIN_ARG_TAG, Y_STR_ARG, exts);
3019 break;
3020 case LY_STMT_USES:
3021 ret = yin_parse_uses(ctx, (struct tree_node_meta *)subelem->dest);
3022 break;
3023 case LY_STMT_WHEN:
3024 ret = yin_parse_when(ctx, (struct lysp_when **)subelem->dest);
3025 break;
3026 case LY_STMT_YANG_VERSION:
3027 ret = yin_parse_yangversion(ctx, (uint8_t *)subelem->dest, exts);
3028 break;
3029 case LY_STMT_YIN_ELEMENT:
3030 ret = yin_parse_yin_element(ctx, (uint16_t *)subelem->dest, exts);
3031 break;
3032 case LY_STMT_ARG_TEXT:
3033 case LY_STMT_ARG_VALUE:
3034 /* TODO what to do with content/attributes? */
3035 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3036 while (ctx->xmlctx->status == LYXML_ATTRIBUTE) {
3037 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3038 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3039 }
3040 ret = yin_parse_content(ctx, NULL, 0, kw, (const char **)subelem->dest, NULL);
3041 break;
3042 default:
3043 LOGINT(ctx->xmlctx->ctx);
3044 ret = LY_EINT;
David Sedlák3ffbc522019-07-02 17:49:28 +02003045 }
Michal Vaskob36053d2020-03-26 15:49:30 +01003046 LY_CHECK_GOTO(ret, cleanup);
3047 subelem = NULL;
3048
3049 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
David Sedlákd6e56892019-07-01 15:40:24 +02003050 }
Michal Vaskob36053d2020-03-26 15:49:30 +01003051 } else {
3052 LY_CHECK_RET(ret);
3053 /* elements with text or none content */
3054 /* save text content, if text_content isn't set, it's just ignored */
3055 /* no resources are allocated in this branch, no need to use cleanup label */
3056 LY_CHECK_RET(yin_validate_value(ctx, Y_STR_ARG));
3057 if (text_content) {
3058 *text_content = INSERT_STRING(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic);
3059 LY_CHECK_RET(!*text_content, LY_EMEM);
3060 }
3061
3062 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
David Sedlákd6e56892019-07-01 15:40:24 +02003063 }
Michal Vaskob36053d2020-03-26 15:49:30 +01003064
David Sedlák8b754462019-07-25 16:22:13 +02003065 /* mandatory subelemnts are checked only after whole element was succesfully parsed */
3066 LY_CHECK_RET(yin_check_subelem_mandatory_constraint(ctx, subelem_info, subelem_info_size, current_element));
David Sedlákd6e56892019-07-01 15:40:24 +02003067
3068cleanup:
David Sedlákd6e56892019-07-01 15:40:24 +02003069 return ret;
3070}
3071
David Sedlák619db942019-07-03 14:47:30 +02003072LY_ERR
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003073yin_parse_extension_instance(struct lys_yin_parser_ctx *ctx, LYEXT_SUBSTMT subelem, LY_ARRAY_COUNT_TYPE subelem_index,
Radek Krejci0f969882020-08-21 16:56:47 +02003074 struct lysp_ext_instance **exts)
David Sedlák1e2cdd02019-06-27 14:17:43 +02003075{
David Sedlák1e2cdd02019-06-27 14:17:43 +02003076 struct lysp_ext_instance *e;
David Sedlákb1a78352019-06-28 16:16:29 +02003077 struct lysp_stmt *last_subelem = NULL, *new_subelem = NULL;
David Sedlák1e2cdd02019-06-27 14:17:43 +02003078
Michal Vaskob36053d2020-03-26 15:49:30 +01003079 assert(ctx->xmlctx->status == LYXML_ELEMENT);
3080
3081 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *exts, e, LY_EMEM);
David Sedlák1e2cdd02019-06-27 14:17:43 +02003082
3083 e->yin = 0;
3084 /* store name and insubstmt info */
Michal Vaskob36053d2020-03-26 15:49:30 +01003085 e->name = name2nsname(ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, ctx->xmlctx->prefix, ctx->xmlctx->prefix_len);
David Sedláke0ef1c62019-09-13 10:05:55 +02003086 LY_CHECK_RET(!e->name, LY_EMEM);
David Sedlák619db942019-07-03 14:47:30 +02003087 e->insubstmt = subelem;
3088 e->insubstmt_index = subelem_index;
David Sedlák1e2cdd02019-06-27 14:17:43 +02003089 e->yin |= LYS_YIN;
Michal Vaskob36053d2020-03-26 15:49:30 +01003090 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
David Sedlák1e2cdd02019-06-27 14:17:43 +02003091
David Sedlákb1a78352019-06-28 16:16:29 +02003092 /* store attributes as subelements */
Michal Vaskob36053d2020-03-26 15:49:30 +01003093 while (ctx->xmlctx->status == LYXML_ATTRIBUTE) {
3094 if (!ctx->xmlctx->prefix) {
David Sedlákb1a78352019-06-28 16:16:29 +02003095 new_subelem = calloc(1, sizeof(*new_subelem));
3096 if (!e->child) {
3097 e->child = new_subelem;
David Sedlák1e2cdd02019-06-27 14:17:43 +02003098 } else {
David Sedlákb1a78352019-06-28 16:16:29 +02003099 last_subelem->next = new_subelem;
3100 }
3101 last_subelem = new_subelem;
3102
3103 last_subelem->flags |= LYS_YIN_ATTR;
Michal Vaskob36053d2020-03-26 15:49:30 +01003104 last_subelem->stmt = lydict_insert(ctx->xmlctx->ctx, ctx->xmlctx->name, ctx->xmlctx->name_len);
David Sedlák169cc522019-08-15 13:23:45 +02003105 LY_CHECK_RET(!last_subelem->stmt, LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01003106 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
David Sedlák169cc522019-08-15 13:23:45 +02003107
Michal Vaskob36053d2020-03-26 15:49:30 +01003108 last_subelem->arg = INSERT_STRING(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic);
David Sedlák169cc522019-08-15 13:23:45 +02003109 LY_CHECK_RET(!last_subelem->arg, LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01003110 } else {
3111 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
David Sedlák1e2cdd02019-06-27 14:17:43 +02003112 }
Michal Vaskob36053d2020-03-26 15:49:30 +01003113
3114 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
David Sedlák1e2cdd02019-06-27 14:17:43 +02003115 }
David Sedlák1e2cdd02019-06-27 14:17:43 +02003116
David Sedlákf250ecf2019-07-01 11:02:05 +02003117 /* parse subelements */
Michal Vaskob36053d2020-03-26 15:49:30 +01003118 assert(ctx->xmlctx->status == LYXML_ELEM_CONTENT);
3119 if (ctx->xmlctx->ws_only) {
3120 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3121 while (ctx->xmlctx->status == LYXML_ELEMENT) {
3122 LY_CHECK_RET(yin_parse_element_generic(ctx, LY_STMT_EXTENSION_INSTANCE, &new_subelem));
3123 if (!e->child) {
3124 e->child = new_subelem;
3125 } else {
3126 last_subelem->next = new_subelem;
David Sedlákb1a78352019-06-28 16:16:29 +02003127 }
Michal Vaskob36053d2020-03-26 15:49:30 +01003128 last_subelem = new_subelem;
David Sedlák169cc522019-08-15 13:23:45 +02003129
Michal Vaskob36053d2020-03-26 15:49:30 +01003130 assert(ctx->xmlctx->status == LYXML_ELEM_CLOSE);
3131 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
David Sedlákb1a78352019-06-28 16:16:29 +02003132 }
Michal Vaskob36053d2020-03-26 15:49:30 +01003133 } else if (ctx->xmlctx->value_len) {
3134 /* save text content */
3135 e->argument = INSERT_STRING(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic);
3136 LY_CHECK_RET(!e->argument, LY_EMEM);
3137
3138 /* parser next */
3139 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
David Sedlákb1a78352019-06-28 16:16:29 +02003140 }
3141
3142 return LY_SUCCESS;
3143}
3144
David Sedlákaa98bba2019-09-12 11:52:14 +02003145/**
3146 * @brief Parse argument of extension subelement that is classic yang keyword and not another instance of extension.
3147 *
3148 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákaa98bba2019-09-12 11:52:14 +02003149 * @param[in] elem_type Type of element that is currently being parsed.
3150 * @param[out] arg Value to write to.
3151 *
3152 * @return LY_ERR values.
3153 */
3154static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01003155yin_parse_extension_instance_arg(struct lys_yin_parser_ctx *ctx, enum ly_stmt elem_type, const char **arg)
David Sedláke6284fc2019-09-10 11:57:02 +02003156{
David Sedlák071f7662019-09-12 02:02:51 +02003157 enum ly_stmt child;
David Sedláke6284fc2019-09-10 11:57:02 +02003158
Michal Vaskob36053d2020-03-26 15:49:30 +01003159 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3160
David Sedláke6284fc2019-09-10 11:57:02 +02003161 switch (elem_type) {
3162 case LY_STMT_ACTION:
3163 case LY_STMT_ANYDATA:
3164 case LY_STMT_ANYXML:
3165 case LY_STMT_ARGUMENT:
3166 case LY_STMT_BASE:
3167 case LY_STMT_BIT:
3168 case LY_STMT_CASE:
3169 case LY_STMT_CHOICE:
3170 case LY_STMT_CONTAINER:
3171 case LY_STMT_ENUM:
3172 case LY_STMT_EXTENSION:
3173 case LY_STMT_FEATURE:
3174 case LY_STMT_GROUPING:
3175 case LY_STMT_IDENTITY:
3176 case LY_STMT_IF_FEATURE:
3177 case LY_STMT_LEAF:
3178 case LY_STMT_LEAF_LIST:
3179 case LY_STMT_LIST:
3180 case LY_STMT_MODULE:
3181 case LY_STMT_NOTIFICATION:
3182 case LY_STMT_RPC:
3183 case LY_STMT_SUBMODULE:
3184 case LY_STMT_TYPE:
3185 case LY_STMT_TYPEDEF:
3186 case LY_STMT_UNITS:
3187 case LY_STMT_USES:
Michal Vaskob36053d2020-03-26 15:49:30 +01003188 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, arg, Y_MAYBE_STR_ARG, elem_type));
David Sedláke6284fc2019-09-10 11:57:02 +02003189 break;
3190 case LY_STMT_AUGMENT:
3191 case LY_STMT_DEVIATION:
3192 case LY_STMT_REFINE:
Michal Vaskob36053d2020-03-26 15:49:30 +01003193 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_TARGET_NODE, arg, Y_MAYBE_STR_ARG, elem_type));
David Sedláke6284fc2019-09-10 11:57:02 +02003194 break;
3195 case LY_STMT_CONFIG:
3196 case LY_STMT_DEFAULT:
3197 case LY_STMT_DEVIATE:
3198 case LY_STMT_ERROR_APP_TAG:
David Sedláke6284fc2019-09-10 11:57:02 +02003199 case LY_STMT_FRACTION_DIGITS:
3200 case LY_STMT_KEY:
3201 case LY_STMT_LENGTH:
3202 case LY_STMT_MANDATORY:
3203 case LY_STMT_MAX_ELEMENTS:
3204 case LY_STMT_MIN_ELEMENTS:
3205 case LY_STMT_MODIFIER:
3206 case LY_STMT_ORDERED_BY:
3207 case LY_STMT_PATH:
3208 case LY_STMT_PATTERN:
3209 case LY_STMT_POSITION:
3210 case LY_STMT_PREFIX:
3211 case LY_STMT_PRESENCE:
3212 case LY_STMT_RANGE:
3213 case LY_STMT_REQUIRE_INSTANCE:
3214 case LY_STMT_STATUS:
3215 case LY_STMT_VALUE:
3216 case LY_STMT_YANG_VERSION:
3217 case LY_STMT_YIN_ELEMENT:
Michal Vaskob36053d2020-03-26 15:49:30 +01003218 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, arg, Y_MAYBE_STR_ARG, elem_type));
David Sedláke6284fc2019-09-10 11:57:02 +02003219 break;
3220 case LY_STMT_IMPORT:
3221 case LY_STMT_INCLUDE:
3222 case LY_STMT_BELONGS_TO:
Michal Vaskob36053d2020-03-26 15:49:30 +01003223 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_MODULE, arg, Y_MAYBE_STR_ARG, elem_type));
David Sedláke6284fc2019-09-10 11:57:02 +02003224 break;
3225 case LY_STMT_INPUT:
3226 case LY_STMT_OUTPUT:
Michal Vaskob36053d2020-03-26 15:49:30 +01003227 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NONE, arg, Y_MAYBE_STR_ARG, elem_type));
David Sedláke6284fc2019-09-10 11:57:02 +02003228 break;
3229 case LY_STMT_MUST:
3230 case LY_STMT_WHEN:
Michal Vaskob36053d2020-03-26 15:49:30 +01003231 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_CONDITION, arg, Y_MAYBE_STR_ARG, elem_type));
David Sedláke6284fc2019-09-10 11:57:02 +02003232 break;
3233 case LY_STMT_NAMESPACE:
Michal Vaskob36053d2020-03-26 15:49:30 +01003234 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_URI, arg, Y_MAYBE_STR_ARG, elem_type));
David Sedláke6284fc2019-09-10 11:57:02 +02003235 break;
3236 case LY_STMT_REVISION:
3237 case LY_STMT_REVISION_DATE:
Michal Vaskob36053d2020-03-26 15:49:30 +01003238 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_DATE, arg, Y_MAYBE_STR_ARG, elem_type));
David Sedláke6284fc2019-09-10 11:57:02 +02003239 break;
3240 case LY_STMT_UNIQUE:
Michal Vaskob36053d2020-03-26 15:49:30 +01003241 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_TAG, arg, Y_MAYBE_STR_ARG, elem_type));
David Sedláke6284fc2019-09-10 11:57:02 +02003242 break;
David Sedlák071f7662019-09-12 02:02:51 +02003243 /* argument is mapped to yin element */
3244 case LY_STMT_CONTACT:
3245 case LY_STMT_DESCRIPTION:
3246 case LY_STMT_ORGANIZATION:
3247 case LY_STMT_REFERENCE:
3248 case LY_STMT_ERROR_MESSAGE:
David Sedlákaa98bba2019-09-12 11:52:14 +02003249 /* there shouldn't be any attribute, argument is supposed to be first subelement */
Michal Vaskob36053d2020-03-26 15:49:30 +01003250 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NONE, arg, Y_MAYBE_STR_ARG, elem_type));
3251
3252 /* no content */
3253 assert(ctx->xmlctx->status == LYXML_ELEM_CONTENT);
3254 if (ctx->xmlctx->ws_only) {
3255 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3256 }
3257 if (((ctx->xmlctx->status == LYXML_ELEM_CONTENT) && !ctx->xmlctx->ws_only) || (ctx->xmlctx->status != LYXML_ELEMENT)) {
3258 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_FIRT_SUBELEM,
3259 elem_type == LY_STMT_ERROR_MESSAGE ? "value" : "text", ly_stmt2str(elem_type));
David Sedlák071f7662019-09-12 02:02:51 +02003260 return LY_EVALID;
3261 }
Michal Vaskob36053d2020-03-26 15:49:30 +01003262
3263 /* parse child element */
3264 child = yin_match_keyword(ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, ctx->xmlctx->prefix, ctx->xmlctx->prefix_len, elem_type);
3265 if ((elem_type == LY_STMT_ERROR_MESSAGE && child != LY_STMT_ARG_VALUE) ||
Radek Krejci0f969882020-08-21 16:56:47 +02003266 (elem_type != LY_STMT_ERROR_MESSAGE && child != LY_STMT_ARG_TEXT)) {
Michal Vaskob36053d2020-03-26 15:49:30 +01003267 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_UNEXP_SUBELEM, ctx->xmlctx->name_len, ctx->xmlctx->name,
3268 ly_stmt2str(elem_type));
3269 return LY_EVALID;
David Sedlákaa98bba2019-09-12 11:52:14 +02003270 }
Michal Vaskob36053d2020-03-26 15:49:30 +01003271 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3272
3273 /* no attributes expected? TODO */
3274 while (ctx->xmlctx->status == LYXML_ATTRIBUTE) {
3275 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3276 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3277 }
3278
3279 /* load and save content */
3280 *arg = INSERT_STRING(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic);
3281 LY_CHECK_RET(!*arg, LY_EMEM);
3282
3283 /* load closing tag of subelement */
3284 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3285
3286 /* if only subelement was parsed as argument, load also closing tag TODO what? */
3287 /*if (ctx->xmlctx->status == LYXML_ELEMENT) {
3288 LY_CHECK_RET(lyxml_get_element(&ctx->xmlctx, data, &prefix, &prefix_len, &name, &name_len));
3289 }*/
David Sedlákb0ca07d2019-09-11 11:54:05 +02003290 break;
David Sedláke6284fc2019-09-10 11:57:02 +02003291 default:
Michal Vaskob36053d2020-03-26 15:49:30 +01003292 LOGINT(ctx->xmlctx->ctx);
David Sedlák071f7662019-09-12 02:02:51 +02003293 return LY_EINT;
David Sedláke6284fc2019-09-10 11:57:02 +02003294 }
3295
David Sedlák071f7662019-09-12 02:02:51 +02003296 return LY_SUCCESS;
David Sedláke6284fc2019-09-10 11:57:02 +02003297}
3298
3299LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01003300yin_parse_element_generic(struct lys_yin_parser_ctx *ctx, enum ly_stmt parent, struct lysp_stmt **element)
David Sedlákb1a78352019-06-28 16:16:29 +02003301{
3302 LY_ERR ret = LY_SUCCESS;
David Sedlákb1a78352019-06-28 16:16:29 +02003303 struct lysp_stmt *last = NULL, *new = NULL;
3304
Michal Vaskob36053d2020-03-26 15:49:30 +01003305 assert(ctx->xmlctx->status == LYXML_ELEMENT);
David Sedláke0ef1c62019-09-13 10:05:55 +02003306
3307 /* allocate new structure for element */
3308 *element = calloc(1, sizeof(**element));
Michal Vaskob36053d2020-03-26 15:49:30 +01003309 LY_CHECK_ERR_GOTO(!(*element), LOGMEM(ctx->xmlctx->ctx); ret = LY_EMEM, cleanup);
3310 (*element)->stmt = name2nsname(ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, ctx->xmlctx->prefix, ctx->xmlctx->prefix_len);
David Sedláke0ef1c62019-09-13 10:05:55 +02003311 LY_CHECK_ERR_GOTO(!(*element)->stmt, ret = LY_EMEM, cleanup);
David Sedláke0ef1c62019-09-13 10:05:55 +02003312
Michal Vaskob36053d2020-03-26 15:49:30 +01003313 (*element)->kw = yin_match_keyword(ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, ctx->xmlctx->prefix,
3314 ctx->xmlctx->prefix_len, parent);
David Sedlákb1a78352019-06-28 16:16:29 +02003315
3316 last = (*element)->child;
David Sedláke6284fc2019-09-10 11:57:02 +02003317 if ((*element)->kw == LY_STMT_NONE) {
3318 /* unrecognized element */
Michal Vaskob36053d2020-03-26 15:49:30 +01003319 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_UNEXP_SUBELEM, ctx->xmlctx->name_len, ctx->xmlctx->name,
3320 ly_stmt2str(parent));
David Sedlákaa98bba2019-09-12 11:52:14 +02003321 ret = LY_EVALID;
3322 goto cleanup;
David Sedláke6284fc2019-09-10 11:57:02 +02003323 } else if ((*element)->kw != LY_STMT_EXTENSION_INSTANCE) {
3324 /* element is known yang keyword, which means argument can be parsed correctly. */
Michal Vaskob36053d2020-03-26 15:49:30 +01003325 ret = yin_parse_extension_instance_arg(ctx, (*element)->kw, &(*element)->arg);
David Sedlákaa98bba2019-09-12 11:52:14 +02003326 LY_CHECK_GOTO(ret, cleanup);
David Sedláke6284fc2019-09-10 11:57:02 +02003327 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +01003328 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3329
David Sedlákaa98bba2019-09-12 11:52:14 +02003330 /* load attributes in generic way, save all attributes in linked list */
Michal Vaskob36053d2020-03-26 15:49:30 +01003331 while (ctx->xmlctx->status == LYXML_ATTRIBUTE) {
David Sedláke6284fc2019-09-10 11:57:02 +02003332 new = calloc(1, sizeof(*last));
Michal Vaskob36053d2020-03-26 15:49:30 +01003333 LY_CHECK_ERR_GOTO(!new, LOGMEM(ctx->xmlctx->ctx); ret = LY_EMEM, cleanup);
David Sedláke6284fc2019-09-10 11:57:02 +02003334 if (!(*element)->child) {
3335 /* save first */
3336 (*element)->child = new;
3337 } else {
3338 last->next = new;
3339 }
3340 last = new;
David Sedlákb1a78352019-06-28 16:16:29 +02003341
David Sedláke6284fc2019-09-10 11:57:02 +02003342 last->flags |= LYS_YIN_ATTR;
Michal Vaskob36053d2020-03-26 15:49:30 +01003343 last->stmt = lydict_insert(ctx->xmlctx->ctx, ctx->xmlctx->name, ctx->xmlctx->name_len);
David Sedlákb0ca07d2019-09-11 11:54:05 +02003344 last->kw = LY_STMT_NONE;
David Sedláke6284fc2019-09-10 11:57:02 +02003345 /* attributes with prefix are ignored */
Michal Vaskob36053d2020-03-26 15:49:30 +01003346 if (!ctx->xmlctx->prefix) {
3347 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3348
3349 last->arg = INSERT_STRING(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic);
David Sedlákaa98bba2019-09-12 11:52:14 +02003350 LY_CHECK_ERR_GOTO(!last->arg, ret = LY_EMEM, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +01003351 } else {
3352 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
David Sedláka82ecff2019-08-16 10:51:48 +02003353 }
Michal Vaskob36053d2020-03-26 15:49:30 +01003354 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
David Sedlákb1a78352019-06-28 16:16:29 +02003355 }
3356 }
3357
Michal Vaskob36053d2020-03-26 15:49:30 +01003358 if ((ctx->xmlctx->status != LYXML_ELEM_CONTENT) || ctx->xmlctx->ws_only) {
3359 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3360 while (ctx->xmlctx->status == LYXML_ELEMENT) {
3361 /* parse subelements */
3362 ret = yin_parse_element_generic(ctx, (*element)->kw, &new);
David Sedlákaa98bba2019-09-12 11:52:14 +02003363 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +01003364 if (!(*element)->child) {
3365 /* save first */
3366 (*element)->child = new;
3367 } else {
3368 last->next = new;
David Sedláka82ecff2019-08-16 10:51:48 +02003369 }
Michal Vaskob36053d2020-03-26 15:49:30 +01003370 last = new;
David Sedlák169cc522019-08-15 13:23:45 +02003371
Michal Vaskob36053d2020-03-26 15:49:30 +01003372 assert(ctx->xmlctx->status == LYXML_ELEM_CLOSE);
3373 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
David Sedláka82ecff2019-08-16 10:51:48 +02003374 }
Michal Vaskob36053d2020-03-26 15:49:30 +01003375 } else {
3376 /* save element content */
3377 if (ctx->xmlctx->value_len) {
3378 (*element)->arg = INSERT_STRING(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic);
3379 LY_CHECK_ERR_GOTO(!(*element)->arg, ret = LY_EMEM, cleanup);
3380 }
3381
3382 /* read closing tag */
3383 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
David Sedlákb1a78352019-06-28 16:16:29 +02003384 }
3385
David Sedlákb0ca07d2019-09-11 11:54:05 +02003386cleanup:
David Sedlákb0ca07d2019-09-11 11:54:05 +02003387 return ret;
David Sedlák1e2cdd02019-06-27 14:17:43 +02003388}
3389
3390LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01003391yin_parse_mod(struct lys_yin_parser_ctx *ctx, struct lysp_module *mod)
David Sedlák3b4db242018-10-19 16:11:01 +02003392{
David Sedlák81497a32019-08-13 16:56:26 +02003393 LY_ERR ret = LY_SUCCESS;
3394 struct yin_subelement *subelems = NULL;
David Sedláked809f12019-09-13 10:15:11 +02003395 struct lysp_submodule *dup;
David Sedlák3b4db242018-10-19 16:11:01 +02003396
Michal Vaskob36053d2020-03-26 15:49:30 +01003397 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3398 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &mod->mod->name, Y_IDENTIF_ARG, LY_STMT_MODULE));
David Sedlák81497a32019-08-13 16:56:26 +02003399 LY_CHECK_RET(subelems_allocator(ctx, 28, NULL, &subelems,
Radek Krejcid6b76452019-09-03 17:03:03 +02003400 LY_STMT_ANYDATA, &mod->data, YIN_SUBELEM_VER2,
3401 LY_STMT_ANYXML, &mod->data, 0,
3402 LY_STMT_AUGMENT, &mod->augments, 0,
3403 LY_STMT_CHOICE, &mod->data, 0,
3404 LY_STMT_CONTACT, &mod->mod->contact, YIN_SUBELEM_UNIQUE,
3405 LY_STMT_CONTAINER, &mod->data, 0,
3406 LY_STMT_DESCRIPTION, &mod->mod->dsc, YIN_SUBELEM_UNIQUE,
3407 LY_STMT_DEVIATION, &mod->deviations, 0,
3408 LY_STMT_EXTENSION, &mod->extensions, 0,
3409 LY_STMT_FEATURE, &mod->features, 0,
3410 LY_STMT_GROUPING, &mod->groupings, 0,
3411 LY_STMT_IDENTITY, &mod->identities, 0,
3412 LY_STMT_IMPORT, mod->mod->prefix, &mod->imports, 0,
3413 LY_STMT_INCLUDE, mod->mod->name, &mod->includes, 0,
3414 LY_STMT_LEAF, &mod->data, 0,
3415 LY_STMT_LEAF_LIST, &mod->data, 0,
3416 LY_STMT_LIST, &mod->data, 0,
3417 LY_STMT_NAMESPACE, &mod->mod->ns, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE,
3418 LY_STMT_NOTIFICATION, &mod->notifs, 0,
3419 LY_STMT_ORGANIZATION, &mod->mod->org, YIN_SUBELEM_UNIQUE,
3420 LY_STMT_PREFIX, &mod->mod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE,
3421 LY_STMT_REFERENCE, &mod->mod->ref, YIN_SUBELEM_UNIQUE,
3422 LY_STMT_REVISION, &mod->revs, 0,
3423 LY_STMT_RPC, &mod->rpcs, 0,
3424 LY_STMT_TYPEDEF, &mod->typedefs, 0,
3425 LY_STMT_USES, &mod->data, 0,
Radek Krejcicd1c5e32019-09-06 16:28:42 +02003426 LY_STMT_YANG_VERSION, &mod->mod->version, YIN_SUBELEM_UNIQUE,
Radek Krejcid6b76452019-09-03 17:03:03 +02003427 LY_STMT_EXTENSION_INSTANCE, NULL, 0
David Sedlák81497a32019-08-13 16:56:26 +02003428 ));
3429
Michal Vaskob36053d2020-03-26 15:49:30 +01003430 ret = yin_parse_content(ctx, subelems, 28, LY_STMT_MODULE, NULL, &mod->exts);
David Sedlák81497a32019-08-13 16:56:26 +02003431 subelems_deallocator(28, subelems);
David Sedláked809f12019-09-13 10:15:11 +02003432 LY_CHECK_RET(ret);
David Sedlák81497a32019-08-13 16:56:26 +02003433
David Sedláked809f12019-09-13 10:15:11 +02003434 /* finalize parent pointers to the reallocated items */
3435 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, mod->groupings, mod->augments, mod->rpcs, mod->notifs));
3436
3437 /* submodules share the namespace with the module names, so there must not be
3438 * a submodule of the same name in the context, no need for revision matching */
Michal Vaskob36053d2020-03-26 15:49:30 +01003439 dup = ly_ctx_get_submodule(ctx->xmlctx->ctx, NULL, mod->mod->name, NULL);
David Sedláked809f12019-09-13 10:15:11 +02003440 if (dup) {
3441 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LYVE_SYNTAX_YANG, "Name collision between module and submodule of name \"%s\".", mod->mod->name);
3442 return LY_EVALID;
3443 }
3444
3445 return LY_SUCCESS;
David Sedlák3b4db242018-10-19 16:11:01 +02003446}
3447
3448LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01003449yin_parse_submod(struct lys_yin_parser_ctx *ctx, struct lysp_submodule *submod)
David Sedlák298ff6d2019-07-26 14:29:03 +02003450{
David Sedlák81497a32019-08-13 16:56:26 +02003451 LY_ERR ret = LY_SUCCESS;
3452 struct yin_subelement *subelems = NULL;
David Sedláked809f12019-09-13 10:15:11 +02003453 struct lysp_submodule *dup;
David Sedlák298ff6d2019-07-26 14:29:03 +02003454
Michal Vaskob36053d2020-03-26 15:49:30 +01003455 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3456 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &submod->name, Y_IDENTIF_ARG, LY_STMT_SUBMODULE));
David Sedlák81497a32019-08-13 16:56:26 +02003457 LY_CHECK_RET(subelems_allocator(ctx, 27, NULL, &subelems,
Radek Krejcid6b76452019-09-03 17:03:03 +02003458 LY_STMT_ANYDATA, &submod->data, YIN_SUBELEM_VER2,
3459 LY_STMT_ANYXML, &submod->data, 0,
3460 LY_STMT_AUGMENT, &submod->augments, 0,
3461 LY_STMT_BELONGS_TO, submod, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE,
3462 LY_STMT_CHOICE, &submod->data, 0,
3463 LY_STMT_CONTACT, &submod->contact, YIN_SUBELEM_UNIQUE,
3464 LY_STMT_CONTAINER, &submod->data, 0,
3465 LY_STMT_DESCRIPTION, &submod->dsc, YIN_SUBELEM_UNIQUE,
3466 LY_STMT_DEVIATION, &submod->deviations, 0,
3467 LY_STMT_EXTENSION, &submod->extensions, 0,
3468 LY_STMT_FEATURE, &submod->features, 0,
3469 LY_STMT_GROUPING, &submod->groupings, 0,
3470 LY_STMT_IDENTITY, &submod->identities, 0,
3471 LY_STMT_IMPORT, submod->prefix, &submod->imports, 0,
3472 LY_STMT_INCLUDE, submod->name, &submod->includes, 0,
3473 LY_STMT_LEAF, &submod->data, 0,
3474 LY_STMT_LEAF_LIST, &submod->data, 0,
3475 LY_STMT_LIST, &submod->data, 0,
3476 LY_STMT_NOTIFICATION, &submod->notifs, 0,
3477 LY_STMT_ORGANIZATION, &submod->org, YIN_SUBELEM_UNIQUE,
3478 LY_STMT_REFERENCE, &submod->ref, YIN_SUBELEM_UNIQUE,
3479 LY_STMT_REVISION, &submod->revs, 0,
3480 LY_STMT_RPC, &submod->rpcs, 0,
3481 LY_STMT_TYPEDEF, &submod->typedefs, 0,
3482 LY_STMT_USES, &submod->data, 0,
Radek Krejcicd1c5e32019-09-06 16:28:42 +02003483 LY_STMT_YANG_VERSION, &submod->version, YIN_SUBELEM_UNIQUE,
Radek Krejcid6b76452019-09-03 17:03:03 +02003484 LY_STMT_EXTENSION_INSTANCE, NULL, 0
David Sedlák81497a32019-08-13 16:56:26 +02003485 ));
3486
Michal Vaskob36053d2020-03-26 15:49:30 +01003487 ret = yin_parse_content(ctx, subelems, 27, LY_STMT_SUBMODULE, NULL, &submod->exts);
David Sedlák81497a32019-08-13 16:56:26 +02003488 subelems_deallocator(27, subelems);
David Sedláked809f12019-09-13 10:15:11 +02003489 LY_CHECK_RET(ret);
David Sedlák81497a32019-08-13 16:56:26 +02003490
David Sedláked809f12019-09-13 10:15:11 +02003491 /* finalize parent pointers to the reallocated items */
3492 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, submod->groupings, submod->augments, submod->rpcs, submod->notifs));
3493
3494 /* submodules share the namespace with the module names, so there must not be
3495 * a submodule of the same name in the context, no need for revision matching */
Michal Vaskob36053d2020-03-26 15:49:30 +01003496 dup = ly_ctx_get_submodule(ctx->xmlctx->ctx, NULL, submod->name, NULL);
David Sedláked809f12019-09-13 10:15:11 +02003497 if (dup && strcmp(dup->belongsto, submod->belongsto)) {
3498 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LYVE_SYNTAX_YANG, "Name collision between submodules of name \"%s\".", dup->name);
3499 return LY_EVALID;
3500 }
3501
3502 return LY_SUCCESS;
David Sedlák298ff6d2019-07-26 14:29:03 +02003503}
3504
3505LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003506yin_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 +02003507 struct ly_in *in, struct lysp_submodule **submod)
David Sedlák8985a142019-07-31 16:43:06 +02003508{
Radek Krejcid6b76452019-09-03 17:03:03 +02003509 enum ly_stmt kw = LY_STMT_NONE;
David Sedlák8985a142019-07-31 16:43:06 +02003510 LY_ERR ret = LY_SUCCESS;
David Sedlák8985a142019-07-31 16:43:06 +02003511 struct lysp_submodule *mod_p = NULL;
3512
3513 /* create context */
3514 *yin_ctx = calloc(1, sizeof **yin_ctx);
David Sedlák1b623122019-08-05 15:27:49 +02003515 LY_CHECK_ERR_RET(!(*yin_ctx), LOGMEM(ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01003516 (*yin_ctx)->format = LYS_IN_YIN;
Michal Vasko7c8439f2020-08-05 13:25:19 +02003517 (*yin_ctx)->main_mod = main_ctx->main_mod;
Michal Vasko63f3d842020-07-08 10:10:14 +02003518 LY_CHECK_RET(lyxml_ctx_new(ctx, in, &(*yin_ctx)->xmlctx));
David Sedlák8985a142019-07-31 16:43:06 +02003519
David Sedlák1b623122019-08-05 15:27:49 +02003520 /* map the typedefs and groupings list from main context to the submodule's context */
3521 memcpy(&(*yin_ctx)->tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
3522 memcpy(&(*yin_ctx)->grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes);
3523
David Sedlák8985a142019-07-31 16:43:06 +02003524 /* check submodule */
Michal Vaskob36053d2020-03-26 15:49:30 +01003525 kw = yin_match_keyword(*yin_ctx, (*yin_ctx)->xmlctx->name, (*yin_ctx)->xmlctx->name_len, (*yin_ctx)->xmlctx->prefix,
Radek Krejci0f969882020-08-21 16:56:47 +02003526 (*yin_ctx)->xmlctx->prefix_len, LY_STMT_NONE);
Radek Krejcid6b76452019-09-03 17:03:03 +02003527 if (kw == LY_STMT_MODULE) {
David Sedlák8985a142019-07-31 16:43:06 +02003528 LOGERR(ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
3529 ret = LY_EINVAL;
3530 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02003531 } else if (kw != LY_STMT_SUBMODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02003532 LOGVAL_PARSER((struct lys_parser_ctx *)*yin_ctx, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
David Sedlák8985a142019-07-31 16:43:06 +02003533 ret = LY_EVALID;
3534 goto cleanup;
3535 }
3536
3537 mod_p = calloc(1, sizeof *mod_p);
3538 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx), cleanup);
3539 mod_p->parsing = 1;
3540
Michal Vaskob36053d2020-03-26 15:49:30 +01003541 ret = yin_parse_submod(*yin_ctx, mod_p);
David Sedlák8985a142019-07-31 16:43:06 +02003542 LY_CHECK_GOTO(ret, cleanup);
3543
David Sedlákbf8a2b72019-08-14 16:48:10 +02003544 /* skip possible trailing whitespaces at end of the input */
Michal Vasko63f3d842020-07-08 10:10:14 +02003545 while (isspace(in->current[0])) {
3546 ly_in_skip(in, 1);
David Sedlák6d781b62019-08-02 15:22:52 +02003547 }
Michal Vasko63f3d842020-07-08 10:10:14 +02003548 if (in->current[0]) {
3549 LOGVAL_PARSER((struct lys_parser_ctx *)*yin_ctx, LY_VCODE_TRAILING_SUBMOD, 15, in->current,
3550 strlen(in->current) > 15 ? "..." : "");
David Sedlák6d781b62019-08-02 15:22:52 +02003551 ret = LY_EVALID;
3552 goto cleanup;
3553 }
3554
David Sedlák8985a142019-07-31 16:43:06 +02003555 mod_p->parsing = 0;
3556 *submod = mod_p;
3557
3558cleanup:
3559 if (ret) {
3560 lysp_submodule_free(ctx, mod_p);
David Sedlák1b623122019-08-05 15:27:49 +02003561 yin_parser_ctx_free(*yin_ctx);
3562 *yin_ctx = NULL;
David Sedlák8985a142019-07-31 16:43:06 +02003563 }
David Sedlák8985a142019-07-31 16:43:06 +02003564 return ret;
3565}
3566
3567LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003568yin_parse_module(struct lys_yin_parser_ctx **yin_ctx, struct ly_in *in, struct lys_module *mod)
David Sedlák3b4db242018-10-19 16:11:01 +02003569{
David Sedláke4889912018-11-02 09:52:40 +01003570 LY_ERR ret = LY_SUCCESS;
Radek Krejcid6b76452019-09-03 17:03:03 +02003571 enum ly_stmt kw = LY_STMT_NONE;
David Sedlák3017da42019-02-15 09:48:04 +01003572 struct lysp_module *mod_p = NULL;
David Sedlák3b4db242018-10-19 16:11:01 +02003573
David Sedlák8985a142019-07-31 16:43:06 +02003574 /* create context */
3575 *yin_ctx = calloc(1, sizeof **yin_ctx);
David Sedlák1b623122019-08-05 15:27:49 +02003576 LY_CHECK_ERR_RET(!(*yin_ctx), LOGMEM(mod->ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01003577 (*yin_ctx)->format = LYS_IN_YIN;
Michal Vasko7c8439f2020-08-05 13:25:19 +02003578 (*yin_ctx)->main_mod = mod;
Michal Vasko63f3d842020-07-08 10:10:14 +02003579 LY_CHECK_RET(lyxml_ctx_new(mod->ctx, in, &(*yin_ctx)->xmlctx));
David Sedlákda8ffa32019-07-08 14:17:10 +02003580
David Sedlák8985a142019-07-31 16:43:06 +02003581 /* check module */
Michal Vaskob36053d2020-03-26 15:49:30 +01003582 kw = yin_match_keyword(*yin_ctx, (*yin_ctx)->xmlctx->name, (*yin_ctx)->xmlctx->name_len, (*yin_ctx)->xmlctx->prefix,
Radek Krejci0f969882020-08-21 16:56:47 +02003583 (*yin_ctx)->xmlctx->prefix_len, LY_STMT_NONE);
Radek Krejcid6b76452019-09-03 17:03:03 +02003584 if (kw == LY_STMT_SUBMODULE) {
David Sedlák8985a142019-07-31 16:43:06 +02003585 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 +01003586 ret = LY_EINVAL;
3587 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02003588 } else if (kw != LY_STMT_MODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02003589 LOGVAL_PARSER((struct lys_parser_ctx *)*yin_ctx, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
David Sedlák3017da42019-02-15 09:48:04 +01003590 ret = LY_EVALID;
3591 goto cleanup;
David Sedláke4889912018-11-02 09:52:40 +01003592 }
3593
David Sedlák3017da42019-02-15 09:48:04 +01003594 /* allocate module */
3595 mod_p = calloc(1, sizeof *mod_p);
David Sedlák8985a142019-07-31 16:43:06 +02003596 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(mod->ctx), cleanup);
David Sedlák3017da42019-02-15 09:48:04 +01003597 mod_p->mod = mod;
3598 mod_p->parsing = 1;
David Sedláke4889912018-11-02 09:52:40 +01003599
David Sedlák00250342019-06-21 14:19:39 +02003600 /* parse module substatements */
Michal Vaskob36053d2020-03-26 15:49:30 +01003601 ret = yin_parse_mod(*yin_ctx, mod_p);
David Sedlák3017da42019-02-15 09:48:04 +01003602 LY_CHECK_GOTO(ret, cleanup);
David Sedlák2e411422018-12-17 02:35:39 +01003603
David Sedlákbf8a2b72019-08-14 16:48:10 +02003604 /* skip possible trailing whitespaces at end of the input */
Michal Vasko63f3d842020-07-08 10:10:14 +02003605 while (isspace(in->current[0])) {
3606 ly_in_skip(in, 1);
David Sedlák6d781b62019-08-02 15:22:52 +02003607 }
Michal Vasko63f3d842020-07-08 10:10:14 +02003608 if (in->current[0]) {
3609 LOGVAL_PARSER((struct lys_parser_ctx *)*yin_ctx, LY_VCODE_TRAILING_MOD, 15, in->current,
3610 strlen(in->current) > 15 ? "..." : "");
David Sedlák6d781b62019-08-02 15:22:52 +02003611 ret = LY_EVALID;
3612 goto cleanup;
3613 }
3614
David Sedlák3017da42019-02-15 09:48:04 +01003615 mod_p->parsing = 0;
3616 mod->parsed = mod_p;
3617
3618cleanup:
David Sedlák8f7a1172019-06-20 14:42:18 +02003619 if (ret != LY_SUCCESS) {
David Sedlák3017da42019-02-15 09:48:04 +01003620 lysp_module_free(mod_p);
David Sedlák1b623122019-08-05 15:27:49 +02003621 yin_parser_ctx_free(*yin_ctx);
3622 *yin_ctx = NULL;
David Sedlák3017da42019-02-15 09:48:04 +01003623 }
David Sedlák2e411422018-12-17 02:35:39 +01003624 return ret;
David Sedlák3b4db242018-10-19 16:11:01 +02003625}