blob: 04589e2b7765c36c0d158c868b60ff0e6194aa0e [file] [log] [blame]
David Sedlákf824ad52018-10-14 23:58:15 +02001/**
2 * @file parser_yin.c
3 * @author David Sedlák <xsedla1d@stud.fit.vutbr.cz>
David Sedlák3b4db242018-10-19 16:11:01 +02004 * @brief YIN parser.
5 *
David Sedlákb1ce3f82019-06-05 14:37:26 +02006 * Copyright (c) 2015 - 2019 CESNET, z.s.p.o.
David Sedlák3b4db242018-10-19 16:11:01 +02007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
David Sedlákf824ad52018-10-14 23:58:15 +020013 */
David Sedlákecf5eb82019-06-03 14:12:44 +020014#include "common.h"
15
David Sedlák3ffbc522019-07-02 17:49:28 +020016#include <assert.h>
David Sedlák3b4db242018-10-19 16:11:01 +020017#include <stdio.h>
18#include <stdlib.h>
19#include <unistd.h>
David Sedlák872c7b42018-10-26 13:15:20 +020020#include <string.h>
David Sedlákd6e56892019-07-01 15:40:24 +020021#include <stdbool.h>
David Sedlák5545f5d2019-07-11 11:55:16 +020022#include <errno.h>
David Sedlákf75d55e2019-07-12 16:52:50 +020023#include <ctype.h>
David Sedlák81497a32019-08-13 16:56:26 +020024#include <stdarg.h>
David Sedlákf824ad52018-10-14 23:58:15 +020025
David Sedlákf824ad52018-10-14 23:58:15 +020026#include "context.h"
David Sedlákecf5eb82019-06-03 14:12:44 +020027#include "dict.h"
David Sedlák3b4db242018-10-19 16:11:01 +020028#include "xml.h"
David Sedlákecf5eb82019-06-03 14:12:44 +020029#include "tree.h"
30#include "tree_schema.h"
David Sedlák3b4db242018-10-19 16:11:01 +020031#include "tree_schema_internal.h"
David Sedlákecf5eb82019-06-03 14:12:44 +020032#include "parser_yin.h"
David Sedlák00250342019-06-21 14:19:39 +020033
David Sedlák2b214ac2019-06-06 16:11:03 +020034/**
35 * @brief check if given string is URI of yin namespace.
David Sedlák8985a142019-07-31 16:43:06 +020036 *
David Sedlák2b214ac2019-06-06 16:11:03 +020037 * @param ns Namespace URI to check.
38 *
39 * @return true if ns equals YIN_NS_URI false otherwise.
40 */
41#define IS_YIN_NS(ns) (strcmp(ns, YIN_NS_URI) == 0)
42
David Sedlákf6251182019-06-06 10:22:13 +020043const char *const yin_attr_list[] = {
44 [YIN_ARG_NAME] = "name",
45 [YIN_ARG_TARGET_NODE] = "target-node",
46 [YIN_ARG_MODULE] = "module",
47 [YIN_ARG_VALUE] = "value",
48 [YIN_ARG_TEXT] = "text",
49 [YIN_ARG_CONDITION] = "condition",
50 [YIN_ARG_URI] = "uri",
51 [YIN_ARG_DATE] = "date",
52 [YIN_ARG_TAG] = "tag",
David Sedlák071f7662019-09-12 02:02:51 +020053 [YIN_ARG_NONE] = "none",
David Sedlákf6251182019-06-06 10:22:13 +020054};
55
Radek Krejcid6b76452019-09-03 17:03:03 +020056enum ly_stmt
David Sedlákc1771b12019-07-10 15:55:46 +020057yin_match_keyword(struct yin_parser_ctx *ctx, const char *name, size_t name_len,
Radek Krejcid6b76452019-09-03 17:03:03 +020058 const char *prefix, size_t prefix_len, enum ly_stmt parrent)
David Sedlák1bccdfa2019-06-17 15:55:27 +020059{
David Sedlák8f7a1172019-06-20 14:42:18 +020060 const char *start = NULL;
Radek Krejcid6b76452019-09-03 17:03:03 +020061 enum ly_stmt kw = LY_STMT_NONE;
David Sedlák8f7a1172019-06-20 14:42:18 +020062 const struct lyxml_ns *ns = NULL;
63
64 if (!name || name_len == 0) {
Radek Krejcid6b76452019-09-03 17:03:03 +020065 return LY_STMT_NONE;
David Sedlák1bccdfa2019-06-17 15:55:27 +020066 }
67
David Sedlákda8ffa32019-07-08 14:17:10 +020068 ns = lyxml_ns_get(&ctx->xml_ctx, prefix, prefix_len);
David Sedlák8f7a1172019-06-20 14:42:18 +020069 if (ns) {
70 if (!IS_YIN_NS(ns->uri)) {
Radek Krejcid6b76452019-09-03 17:03:03 +020071 return LY_STMT_EXTENSION_INSTANCE;
David Sedlák8f7a1172019-06-20 14:42:18 +020072 }
73 } else {
74 /* elements without namespace are automatically unknown */
Radek Krejcid6b76452019-09-03 17:03:03 +020075 return LY_STMT_NONE;
David Sedlák8f7a1172019-06-20 14:42:18 +020076 }
David Sedlák1bccdfa2019-06-17 15:55:27 +020077
David Sedlák8f7a1172019-06-20 14:42:18 +020078 start = name;
79 kw = lysp_match_kw(NULL, &name);
80
81 if (name - start == (long int)name_len) {
David Sedlákc1771b12019-07-10 15:55:46 +020082 /* this is done because of collision in yang statement value and yang argument mapped to yin element value */
Radek Krejcid6b76452019-09-03 17:03:03 +020083 if (kw == LY_STMT_VALUE && parrent == LY_STMT_ERROR_MESSAGE) {
84 return LY_STMT_ARG_VALUE;
David Sedlákc1771b12019-07-10 15:55:46 +020085 }
David Sedlák1bccdfa2019-06-17 15:55:27 +020086 return kw;
87 } else {
David Sedlák3ffbc522019-07-02 17:49:28 +020088 if (strncmp(start, "text", name_len) == 0) {
Radek Krejcid6b76452019-09-03 17:03:03 +020089 return LY_STMT_ARG_TEXT;
David Sedlák3ffbc522019-07-02 17:49:28 +020090 } else {
Radek Krejcid6b76452019-09-03 17:03:03 +020091 return LY_STMT_NONE;
David Sedlák3ffbc522019-07-02 17:49:28 +020092 }
David Sedlák1bccdfa2019-06-17 15:55:27 +020093 }
94}
95
David Sedlákc5b20842019-08-13 10:18:31 +020096enum yin_argument
David Sedlák060b00e2019-06-19 11:12:06 +020097yin_match_argument_name(const char *name, size_t len)
David Sedlák3b4db242018-10-19 16:11:01 +020098{
David Sedlákc5b20842019-08-13 10:18:31 +020099 enum yin_argument arg = YIN_ARG_UNKNOWN;
David Sedlák872c7b42018-10-26 13:15:20 +0200100 size_t already_read = 0;
David Sedlák7ff55a92019-06-17 11:11:41 +0200101 LY_CHECK_RET(len == 0, YIN_ARG_NONE);
David Sedlák3b4db242018-10-19 16:11:01 +0200102
David Sedlák94de2aa2019-02-15 12:42:11 +0100103#define IF_ARG(STR, LEN, STMT) if (!strncmp((name) + already_read, STR, LEN)) {already_read+=LEN;arg=STMT;}
104#define IF_ARG_PREFIX(STR, LEN) if (!strncmp((name) + already_read, STR, LEN)) {already_read+=LEN;
David Sedlákc10e7902018-12-17 02:17:59 +0100105#define IF_ARG_PREFIX_END }
106
David Sedlák1c8b2702019-02-22 11:03:02 +0100107 switch (*name) {
David Sedlák94de2aa2019-02-15 12:42:11 +0100108 case 'c':
109 already_read += 1;
110 IF_ARG("ondition", 8, YIN_ARG_CONDITION);
David Sedlák3b4db242018-10-19 16:11:01 +0200111 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200112
David Sedlák94de2aa2019-02-15 12:42:11 +0100113 case 'd':
114 already_read += 1;
115 IF_ARG("ate", 3, YIN_ARG_DATE);
David Sedlák3b4db242018-10-19 16:11:01 +0200116 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200117
David Sedlák94de2aa2019-02-15 12:42:11 +0100118 case 'm':
119 already_read += 1;
120 IF_ARG("odule", 5, YIN_ARG_MODULE);
David Sedlák872c7b42018-10-26 13:15:20 +0200121 break;
122
David Sedlák94de2aa2019-02-15 12:42:11 +0100123 case 'n':
124 already_read += 1;
125 IF_ARG("ame", 3, YIN_ARG_NAME);
David Sedlák872c7b42018-10-26 13:15:20 +0200126 break;
127
David Sedlák94de2aa2019-02-15 12:42:11 +0100128 case 't':
129 already_read += 1;
130 IF_ARG_PREFIX("a", 1)
131 IF_ARG("g", 1, YIN_ARG_TAG)
132 else IF_ARG("rget-node", 9, YIN_ARG_TARGET_NODE)
133 IF_ARG_PREFIX_END
134 else IF_ARG("ext", 3, YIN_ARG_TEXT)
David Sedlák3b4db242018-10-19 16:11:01 +0200135 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200136
David Sedlák94de2aa2019-02-15 12:42:11 +0100137 case 'u':
138 already_read += 1;
139 IF_ARG("ri", 2, YIN_ARG_URI)
David Sedlák3b4db242018-10-19 16:11:01 +0200140 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200141
David Sedlák94de2aa2019-02-15 12:42:11 +0100142 case 'v':
143 already_read += 1;
144 IF_ARG("alue", 4, YIN_ARG_VALUE);
David Sedlák3b4db242018-10-19 16:11:01 +0200145 break;
146 }
147
David Sedlákc10e7902018-12-17 02:17:59 +0100148 /* whole argument must be matched */
David Sedlák872c7b42018-10-26 13:15:20 +0200149 if (already_read != len) {
David Sedláka7406952019-04-05 10:33:07 +0200150 arg = YIN_ARG_UNKNOWN;
David Sedlák872c7b42018-10-26 13:15:20 +0200151 }
152
David Sedlák18730132019-03-15 15:51:34 +0100153#undef IF_ARG
154#undef IF_ARG_PREFIX
155#undef IF_ARG_PREFIX_END
156
David Sedlák872c7b42018-10-26 13:15:20 +0200157 return arg;
David Sedlák3b4db242018-10-19 16:11:01 +0200158}
159
David Sedlák4f03b932019-07-26 13:01:47 +0200160void free_arg_rec(struct yin_parser_ctx *ctx, struct yin_arg_record *record) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200161 (void)ctx; /* unused */
David Sedlákd2d676a2019-07-22 11:28:19 +0200162 if (record && record->dynamic_content) {
David Sedlák00250342019-06-21 14:19:39 +0200163 free(record->content);
164 }
165}
166
Radek Krejcid6b76452019-09-03 17:03:03 +0200167#define IS_NODE_ELEM(kw) (kw == LY_STMT_ANYXML || kw == LY_STMT_ANYDATA || kw == LY_STMT_LEAF || kw == LY_STMT_LEAF_LIST || \
168 kw == LY_STMT_TYPEDEF || kw == LY_STMT_USES || kw == LY_STMT_LIST || kw == LY_STMT_NOTIFICATION || \
169 kw == LY_STMT_GROUPING || kw == LY_STMT_CONTAINER || kw == LY_STMT_CASE || kw == LY_STMT_CHOICE || \
170 kw == LY_STMT_ACTION || kw == LY_STMT_RPC || kw == LY_STMT_AUGMENT)
David Sedlák81497a32019-08-13 16:56:26 +0200171
Radek Krejcid6b76452019-09-03 17:03:03 +0200172#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 +0200173
David Sedlák26ea1432019-08-14 13:42:23 +0200174/**
175 * @brief Free subelems information allocated on heap.
176 *
177 * @param[in] count Size of subelems array.
178 * @param[in] subelems Subelems array to free.
179 */
David Sedlák81497a32019-08-13 16:56:26 +0200180static void
181subelems_deallocator(size_t count, struct yin_subelement *subelems)
182{
183 for(size_t i = 0; i < count; ++i) {
184 if (HAS_META(subelems[i].type)) {
185 free(subelems[i].dest);
186 }
187 }
188
189 free(subelems);
190}
191
David Sedlák26ea1432019-08-14 13:42:23 +0200192/**
193 * @brief Allocate subelems information on heap.
194 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200195 * @param[in] ctx YIN parser context, used for logging.
David Sedlák26ea1432019-08-14 13:42:23 +0200196 * @param[in] count Number of subelements.
197 * @param[in] parent Parent node if any.
198 * @param[out] result Allocated subelems array.
199 *
200 * @return LY_SUCCESS on success LY_EMEM on memmory allocation failure.
201 */
David Sedlák81497a32019-08-13 16:56:26 +0200202static LY_ERR
203subelems_allocator(struct yin_parser_ctx *ctx, size_t count, struct lysp_node *parent,
204 struct yin_subelement **result, ...)
205{
206 va_list ap;
207
208 *result = calloc(count, sizeof **result);
David Sedlákbf8a2b72019-08-14 16:48:10 +0200209 LY_CHECK_GOTO(!(*result), mem_err);
David Sedlák81497a32019-08-13 16:56:26 +0200210
211 va_start(ap, result);
212 for (size_t i = 0; i < count; ++i) {
213 /* TYPE */
Radek Krejcid6b76452019-09-03 17:03:03 +0200214 (*result)[i].type = va_arg(ap, enum ly_stmt);
David Sedlák81497a32019-08-13 16:56:26 +0200215 /* DEST */
216 if (IS_NODE_ELEM((*result)[i].type)) {
217 struct tree_node_meta *node_meta = NULL;
218 node_meta = calloc(1, sizeof *node_meta);
David Sedlákbf8a2b72019-08-14 16:48:10 +0200219 LY_CHECK_GOTO(!node_meta, mem_err);
David Sedlák81497a32019-08-13 16:56:26 +0200220 node_meta->parent = parent;
David Sedlákbf8a2b72019-08-14 16:48:10 +0200221 node_meta->nodes = va_arg(ap, void *);
David Sedlák81497a32019-08-13 16:56:26 +0200222 (*result)[i].dest = node_meta;
Radek Krejcid6b76452019-09-03 17:03:03 +0200223 } else if ((*result)[i].type == LY_STMT_IMPORT) {
David Sedlák81497a32019-08-13 16:56:26 +0200224 struct import_meta *imp_meta = NULL;
225 imp_meta = calloc(1, sizeof *imp_meta);
David Sedlákbf8a2b72019-08-14 16:48:10 +0200226 LY_CHECK_GOTO(!imp_meta, mem_err);
David Sedlák81497a32019-08-13 16:56:26 +0200227 imp_meta->prefix = va_arg(ap, const char *);
228 imp_meta->imports = va_arg(ap, struct lysp_import **);
229 (*result)[i].dest = imp_meta;
Radek Krejcid6b76452019-09-03 17:03:03 +0200230 } else if ((*result)[i].type == LY_STMT_INCLUDE) {
David Sedlák81497a32019-08-13 16:56:26 +0200231 struct include_meta *inc_meta = NULL;
232 inc_meta = calloc(1, sizeof *inc_meta);
David Sedlákbf8a2b72019-08-14 16:48:10 +0200233 LY_CHECK_GOTO(!inc_meta, mem_err);
David Sedlák81497a32019-08-13 16:56:26 +0200234 inc_meta->name = va_arg(ap, const char *);
235 inc_meta->includes = va_arg(ap, struct lysp_include **);
236 (*result)[i].dest = inc_meta;
Radek Krejcid6b76452019-09-03 17:03:03 +0200237 } else if ((*result)[i].type == LY_STMT_INPUT || (*result)[i].type == LY_STMT_OUTPUT) {
David Sedlák81497a32019-08-13 16:56:26 +0200238 struct inout_meta *inout_meta = NULL;
239 inout_meta = calloc(1, sizeof *inout_meta);
David Sedlákbf8a2b72019-08-14 16:48:10 +0200240 LY_CHECK_GOTO(!inout_meta, mem_err);
David Sedlák81497a32019-08-13 16:56:26 +0200241 inout_meta->parent = parent;
242 inout_meta->inout_p = va_arg(ap, struct lysp_action_inout *);
243 (*result)[i].dest = inout_meta;
244 } else {
245 (*result)[i].dest = va_arg(ap, void *);
246 }
247 /* FLAGS */
248 (*result)[i].flags = va_arg(ap, int);
249 }
250 va_end(ap);
251
252 return LY_SUCCESS;
253
David Sedlákbf8a2b72019-08-14 16:48:10 +0200254mem_err:
David Sedlák81497a32019-08-13 16:56:26 +0200255 subelems_deallocator(count, *result);
256 LOGMEM(ctx->xml_ctx.ctx);
257 return LY_EMEM;
258}
259
David Sedlák8f7a1172019-06-20 14:42:18 +0200260LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200261yin_load_attributes(struct yin_parser_ctx *ctx, const char **data, struct yin_arg_record **attrs)
David Sedláka7406952019-04-05 10:33:07 +0200262{
263 LY_ERR ret = LY_SUCCESS;
David Sedlák8f7a1172019-06-20 14:42:18 +0200264 struct yin_arg_record *argument_record = NULL;
David Sedlákc5b20842019-08-13 10:18:31 +0200265 const char *prefix, *name;
266 size_t prefix_len, name_len;
David Sedláka7406952019-04-05 10:33:07 +0200267
David Sedlák555c7202019-07-04 12:14:12 +0200268 /* load all attributes */
David Sedlákda8ffa32019-07-08 14:17:10 +0200269 while (ctx->xml_ctx.status == LYXML_ATTRIBUTE) {
David Sedlákc5b20842019-08-13 10:18:31 +0200270 ret = lyxml_get_attribute(&ctx->xml_ctx, data, &prefix, &prefix_len, &name, &name_len);
David Sedlák26ea1432019-08-14 13:42:23 +0200271 LY_CHECK_GOTO(ret, cleanup);
David Sedláka7406952019-04-05 10:33:07 +0200272
David Sedlákda8ffa32019-07-08 14:17:10 +0200273 if (ctx->xml_ctx.status == LYXML_ATTR_CONTENT) {
274 LY_ARRAY_NEW_GOTO(ctx->xml_ctx.ctx, *attrs, argument_record, ret, cleanup);
David Sedlákc5b20842019-08-13 10:18:31 +0200275 argument_record->name = name;
276 argument_record->name_len = name_len;
277 argument_record->prefix = prefix;
278 argument_record->prefix_len = prefix_len;
David Sedlákda8ffa32019-07-08 14:17:10 +0200279 ret = lyxml_get_string(&ctx->xml_ctx, data, &argument_record->content, &argument_record->content_len,
David Sedlák57715b12019-06-17 13:05:22 +0200280 &argument_record->content, &argument_record->content_len, &argument_record->dynamic_content);
David Sedlák26ea1432019-08-14 13:42:23 +0200281 LY_CHECK_GOTO(ret, cleanup);
David Sedlák7ff55a92019-06-17 11:11:41 +0200282 }
283 }
284
David Sedlák8f7a1172019-06-20 14:42:18 +0200285cleanup:
286 if (ret != LY_SUCCESS) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200287 FREE_ARRAY(ctx, *attrs, free_arg_rec);
David Sedlákb4e44562019-07-04 15:42:12 +0200288 *attrs = NULL;
David Sedlák8f7a1172019-06-20 14:42:18 +0200289 }
290 return ret;
291}
292
David Sedlák4a650532019-07-10 11:55:18 +0200293LY_ERR
294yin_validate_value(struct yin_parser_ctx *ctx, enum yang_arg val_type, char *val, size_t len)
295{
296 int prefix = 0;
297 unsigned int c;
298 size_t utf8_char_len;
299 size_t already_read = 0;
300 while (already_read < len) {
301 LY_CHECK_ERR_RET(ly_getutf8((const char **)&val, &c, &utf8_char_len),
302 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INCHAR, (val)[-utf8_char_len]), LY_EVALID);
303 already_read += utf8_char_len;
304 LY_CHECK_ERR_RET(already_read > len, LOGINT(ctx->xml_ctx.ctx), LY_EINT);
305
306 switch (val_type) {
307 case Y_IDENTIF_ARG:
308 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !already_read, NULL));
309 break;
310 case Y_PREF_IDENTIF_ARG:
311 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !already_read, &prefix));
312 break;
313 case Y_STR_ARG:
314 case Y_MAYBE_STR_ARG:
315 LY_CHECK_RET(lysp_check_stringchar((struct lys_parser_ctx *)ctx, c));
316 break;
317 }
318 }
319
320 return LY_SUCCESS;
321}
322
David Sedlákb4e44562019-07-04 15:42:12 +0200323/**
David Sedlákbf8a2b72019-08-14 16:48:10 +0200324 * @brief Parse yin attribute.
David Sedlákb4e44562019-07-04 15:42:12 +0200325 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200326 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200327 * @param[in] attrs ([Sized array](@ref sizedarrays)) of attributes.
David Sedlák4a650532019-07-10 11:55:18 +0200328 * @param[in] arg_type Type of argument that is expected in parsed element (use YIN_ARG_NONE for elements without
329 * special argument).
David Sedlákbf8a2b72019-08-14 16:48:10 +0200330 * @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 +0200331 * @param[in] val_type Type of expected value of attribute.
David Sedlákb4e44562019-07-04 15:42:12 +0200332 * @param[in] current_element Identification of current element, used for logging.
333 *
334 * @return LY_ERR values.
335 */
336static LY_ERR
David Sedlákc5b20842019-08-13 10:18:31 +0200337yin_parse_attribute(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, enum yin_argument arg_type,
Radek Krejcid6b76452019-09-03 17:03:03 +0200338 const char **arg_val, enum yang_arg val_type, enum ly_stmt current_element)
David Sedlák8f7a1172019-06-20 14:42:18 +0200339{
David Sedlákc5b20842019-08-13 10:18:31 +0200340 enum yin_argument arg = YIN_ARG_UNKNOWN;
David Sedlák8f7a1172019-06-20 14:42:18 +0200341 struct yin_arg_record *iter = NULL;
David Sedlák619db942019-07-03 14:47:30 +0200342 bool found = false;
David Sedlák8f7a1172019-06-20 14:42:18 +0200343
David Sedlák1bccdfa2019-06-17 15:55:27 +0200344 /* validation of attributes */
David Sedlák1f90d252019-07-10 17:09:32 +0200345 LY_ARRAY_FOR(attrs, struct yin_arg_record, iter) {
David Sedlák00250342019-06-21 14:19:39 +0200346 /* yin arguments represented as attributes have no namespace, which in this case means no prefix */
347 if (!iter->prefix) {
David Sedlák060b00e2019-06-19 11:12:06 +0200348 arg = yin_match_argument_name(iter->name, iter->name_len);
David Sedlák7ff55a92019-06-17 11:11:41 +0200349 if (arg == YIN_ARG_NONE) {
David Sedlák2b214ac2019-06-06 16:11:03 +0200350 continue;
David Sedlák7ff55a92019-06-17 11:11:41 +0200351 } else if (arg == arg_type) {
David Sedlák1538a842019-08-08 15:38:51 +0200352 LY_CHECK_ERR_RET(found, LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_DUP_ATTR,
David Sedlák292763b2019-07-09 11:10:53 +0200353 yin_attr2str(arg), ly_stmt2str(current_element)), LY_EVALID);
David Sedlák619db942019-07-03 14:47:30 +0200354 found = true;
David Sedlák4a650532019-07-10 11:55:18 +0200355 LY_CHECK_RET(yin_validate_value(ctx, val_type, iter->content, iter->content_len));
David Sedlák169cc522019-08-15 13:23:45 +0200356 INSERT_STRING(ctx->xml_ctx.ctx, *arg_val, iter->dynamic_content, iter->content, iter->content_len);
357 iter->dynamic_content = 0;
358 LY_CHECK_RET(!(*arg_val), LY_EMEM);
David Sedlák2b214ac2019-06-06 16:11:03 +0200359 } else {
David Sedlák1538a842019-08-08 15:38:51 +0200360 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_UNEXP_ATTR, iter->name_len, iter->name, ly_stmt2str(current_element));
David Sedlák619db942019-07-03 14:47:30 +0200361 return LY_EVALID;
David Sedláka7406952019-04-05 10:33:07 +0200362 }
363 }
364 }
365
David Sedlák292763b2019-07-09 11:10:53 +0200366 /* anything else than Y_MAYBE_STR_ARG is mandatory */
367 if (val_type != Y_MAYBE_STR_ARG && !found) {
David Sedlák9c40a922019-07-08 17:04:43 +0200368 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LYVE_SYNTAX_YIN, "Missing mandatory attribute %s of %s element.", yin_attr2str(arg_type), ly_stmt2str(current_element));
David Sedlák619db942019-07-03 14:47:30 +0200369 return LY_EVALID;
370 }
371
372 return LY_SUCCESS;
David Sedláka7406952019-04-05 10:33:07 +0200373}
374
David Sedlákd6e56892019-07-01 15:40:24 +0200375/**
Radek Krejci12b1c152019-09-05 16:20:48 +0200376 * @brief Get record with given type.
David Sedlákd6e56892019-07-01 15:40:24 +0200377 *
378 * @param[in] type Type of wanted record.
379 * @param[in] array_size Size of array.
380 * @param[in] array Searched array.
381 *
382 * @return Pointer to desired record on success, NULL if element is not in the array.
383 */
David Sedlákb4e44562019-07-04 15:42:12 +0200384static struct yin_subelement *
Radek Krejci12b1c152019-09-05 16:20:48 +0200385get_record(enum ly_stmt type, size_t array_size, struct yin_subelement *array)
David Sedlákd6e56892019-07-01 15:40:24 +0200386{
Radek Krejci12b1c152019-09-05 16:20:48 +0200387 for (unsigned int u = 0; u < array_size; ++u) {
388 if (array[u].type == type) {
389 return &array[u];
David Sedlákd6e56892019-07-01 15:40:24 +0200390 }
391 }
David Sedlákd6e56892019-07-01 15:40:24 +0200392 return NULL;
393}
394
David Sedlákbba38e52019-07-09 15:20:01 +0200395/**
396 * @brief Helper function to check mandatory constraint of subelement.
397 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200398 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200399 * @param[in] subelem_info Array of information about subelements.
400 * @param[in] subelem_info_size Size of subelem_info array.
401 * @param[in] current_element Identification of element that is currently being parsed, used for logging.
402 *
403 * @return LY_ERR values.
404 */
405static LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200406yin_check_subelem_mandatory_constraint(struct yin_parser_ctx *ctx, struct yin_subelement *subelem_info,
Radek Krejcid6b76452019-09-03 17:03:03 +0200407 signed char subelem_info_size, enum ly_stmt current_element)
David Sedlák21f87cd2019-07-03 16:53:23 +0200408{
David Sedlákb0faad82019-07-04 14:28:59 +0200409 for (signed char i = 0; i < subelem_info_size; ++i) {
David Sedlák5545f5d2019-07-11 11:55:16 +0200410 /* if there is element that is mandatory and isn't parsed log error and return LY_EVALID */
Radek Krejci12b1c152019-09-05 16:20:48 +0200411 if ((subelem_info[i].flags & YIN_SUBELEM_MANDATORY) && !(subelem_info[i].flags & YIN_SUBELEM_PARSED)) {
David Sedlák1538a842019-08-08 15:38:51 +0200412 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_MAND_SUBELEM,
David Sedlák555c7202019-07-04 12:14:12 +0200413 ly_stmt2str(subelem_info[i].type), ly_stmt2str(current_element));
David Sedlák21f87cd2019-07-03 16:53:23 +0200414 return LY_EVALID;
415 }
416 }
417
418 return LY_SUCCESS;
419}
420
David Sedlákbba38e52019-07-09 15:20:01 +0200421/**
422 * @brief Helper function to check "first" constraint of subelement.
423 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200424 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200425 * @param[in] subelem_info Array of information about subelements.
426 * @param[in] subelem_info_size Size of subelem_info array.
427 * @param[in] current_element Identification of element that is currently being parsed, used for logging.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200428 * @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 +0200429 *
430 * @return LY_ERR values.
431 */
432static LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200433yin_check_subelem_first_constraint(struct yin_parser_ctx *ctx, struct yin_subelement *subelem_info,
Radek Krejcid6b76452019-09-03 17:03:03 +0200434 signed char subelem_info_size, enum ly_stmt current_element,
David Sedláke1a30302019-07-10 13:49:38 +0200435 struct yin_subelement *exp_first)
David Sedlák21f87cd2019-07-03 16:53:23 +0200436{
David Sedlákb0faad82019-07-04 14:28:59 +0200437 for (signed char i = 0; i < subelem_info_size; ++i) {
David Sedlák21f87cd2019-07-03 16:53:23 +0200438 if (subelem_info[i].flags & YIN_SUBELEM_PARSED) {
David Sedlák1538a842019-08-08 15:38:51 +0200439 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_FIRT_SUBELEM,
440 ly_stmt2str(exp_first->type), ly_stmt2str(current_element));
David Sedlák21f87cd2019-07-03 16:53:23 +0200441 return LY_EVALID;
442 }
443 }
444
445 return LY_SUCCESS;
446}
447
David Sedlákbba38e52019-07-09 15:20:01 +0200448/**
David Sedlákb4e44562019-07-04 15:42:12 +0200449 * @brief Parse simple element without any special constraints and argument mapped to yin attribute,
450 * for example prefix or namespace element.
David Sedlákd6e56892019-07-01 15:40:24 +0200451 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200452 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200453 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
David Sedlákd6e56892019-07-01 15:40:24 +0200454 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlákb4e44562019-07-04 15:42:12 +0200455 * @param[in] kw Type of current element.
456 * @param[out] value Where value of attribute should be stored.
457 * @param[in] arg_type Expected type of attribute.
David Sedlák292763b2019-07-09 11:10:53 +0200458 * @param[in] arg_val_type Type of expected value of attribute.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200459 * @param[in,out] exts Extension instances to add to.
David Sedlákb4e44562019-07-04 15:42:12 +0200460 *
David Sedlákd6e56892019-07-01 15:40:24 +0200461 * @return LY_ERR values.
462 */
David Sedlákb4e44562019-07-04 15:42:12 +0200463static LY_ERR
Radek Krejcid6b76452019-09-03 17:03:03 +0200464yin_parse_simple_element(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, enum ly_stmt kw,
David Sedlákc5b20842019-08-13 10:18:31 +0200465 const char **value, enum yin_argument arg_type, enum yang_arg arg_val_type, struct lysp_ext_instance **exts)
David Sedlákb4e44562019-07-04 15:42:12 +0200466{
David Sedlák1f90d252019-07-10 17:09:32 +0200467 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, arg_type, value, arg_val_type, kw));
David Sedlák968ac342019-07-11 15:17:59 +0200468 struct yin_subelement subelems[1] = {
Radek Krejcid6b76452019-09-03 17:03:03 +0200469 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
David Sedlák968ac342019-07-11 15:17:59 +0200470 };
David Sedlákb4e44562019-07-04 15:42:12 +0200471
David Sedlákda8ffa32019-07-08 14:17:10 +0200472 return yin_parse_content(ctx, subelems, 1, data, kw, NULL, exts);
David Sedlákb4e44562019-07-04 15:42:12 +0200473}
474
475/**
David Sedlák6542aed2019-08-14 10:47:43 +0200476 * @brief Parse path element.
477 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200478 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák6542aed2019-08-14 10:47:43 +0200479 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
480 * @param[in,out] data Data to read from, always moved to currently handled character.
481 * @param[in] kw Type of current element.
482 * @param[out] type Type structure to store parsed value, flags and extension instances.
483 *
484 * @return LY_ERR values.
485 */
486static LY_ERR
Radek Krejcid6b76452019-09-03 17:03:03 +0200487yin_parse_path(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, enum ly_stmt kw,
David Sedlák6542aed2019-08-14 10:47:43 +0200488 struct lysp_type *type)
489{
490 LY_CHECK_RET(yin_parse_simple_element(ctx, attrs, data, kw, &type->path,
491 YIN_ARG_VALUE, Y_STR_ARG, &type->exts));
492 type->flags |= LYS_SET_PATH;
493
494 return LY_SUCCESS;
495}
496
497/**
David Sedlákd3983112019-07-12 11:20:56 +0200498 * @brief Parse pattern element.
499 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200500 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákd3983112019-07-12 11:20:56 +0200501 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
502 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200503 * @param[in,out] type Type structure to store parsed value, flags and extension instances.
David Sedlákd3983112019-07-12 11:20:56 +0200504 *
505 * @return LY_ERR values.
506 */
507static LY_ERR
508yin_parse_pattern(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
509 struct lysp_type *type)
510{
511 const char *real_value = NULL;
512 char *saved_value = NULL;
513 struct lysp_restr *restr;
514
515 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, type->patterns, restr, LY_EMEM);
Radek Krejcid6b76452019-09-03 17:03:03 +0200516 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &real_value, Y_STR_ARG, LY_STMT_PATTERN));
David Sedlákd3983112019-07-12 11:20:56 +0200517 size_t len = strlen(real_value);
518
519 saved_value = malloc(len + 2);
520 LY_CHECK_ERR_RET(!saved_value, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
521 memmove(saved_value + 1, real_value, len);
522 FREE_STRING(ctx->xml_ctx.ctx, real_value);
523 saved_value[0] = 0x06;
524 saved_value[len + 1] = '\0';
525 restr->arg = lydict_insert_zc(ctx->xml_ctx.ctx, saved_value);
526 LY_CHECK_ERR_RET(!restr->arg, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
527 type->flags |= LYS_SET_PATTERN;
528
529 struct yin_subelement subelems[6] = {
Radek Krejcid6b76452019-09-03 17:03:03 +0200530 {LY_STMT_DESCRIPTION, &restr->dsc, YIN_SUBELEM_UNIQUE},
531 {LY_STMT_ERROR_APP_TAG, &restr->eapptag, YIN_SUBELEM_UNIQUE},
532 {LY_STMT_ERROR_MESSAGE, &restr->emsg, YIN_SUBELEM_UNIQUE},
533 {LY_STMT_MODIFIER, &restr->arg, YIN_SUBELEM_UNIQUE},
534 {LY_STMT_REFERENCE, &restr->ref, YIN_SUBELEM_UNIQUE},
535 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
David Sedlákd3983112019-07-12 11:20:56 +0200536 };
Radek Krejcid6b76452019-09-03 17:03:03 +0200537 return yin_parse_content(ctx, subelems, 6, data, LY_STMT_PATTERN, NULL, &restr->exts);
David Sedlákd3983112019-07-12 11:20:56 +0200538}
539
David Sedlákc5b20842019-08-13 10:18:31 +0200540/**
541 * @brief Parse fraction-digits element.
542 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200543 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákc5b20842019-08-13 10:18:31 +0200544 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
545 * @param[in,out] data Data to read from, always moved to currently handled character.
546 * @param[in,out] type Type structure to store value, flags and extension instances.
547 *
548 * @return LY_ERR values.
549 */
David Sedlákf75d55e2019-07-12 16:52:50 +0200550static LY_ERR
551yin_parse_fracdigits(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
552 struct lysp_type *type)
553{
554 const char *temp_val = NULL;
555 char *ptr;
556 unsigned long int num;
557
Radek Krejcid6b76452019-09-03 17:03:03 +0200558 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_FRACTION_DIGITS));
David Sedlákf75d55e2019-07-12 16:52:50 +0200559
560 if (temp_val[0] == '\0' || (temp_val[0] == '0') || !isdigit(temp_val[0])) {
David Sedlák1538a842019-08-08 15:38:51 +0200561 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "fraction-digits");
David Sedlákf75d55e2019-07-12 16:52:50 +0200562 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
563 return LY_EVALID;
564 }
565
566 errno = 0;
567 num = strtoul(temp_val, &ptr, 10);
568 if (*ptr != '\0') {
David Sedlák1538a842019-08-08 15:38:51 +0200569 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "fraction-digits");
David Sedlákf75d55e2019-07-12 16:52:50 +0200570 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
571 return LY_EVALID;
572 }
573 if ((errno == ERANGE) || (num > 18)) {
David Sedlák1538a842019-08-08 15:38:51 +0200574 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "fraction-digits");
David Sedlákf75d55e2019-07-12 16:52:50 +0200575 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
576 return LY_EVALID;
577 }
David Sedlák2ab5d8e2019-07-16 11:19:41 +0200578 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
David Sedlákf75d55e2019-07-12 16:52:50 +0200579 type->fraction_digits = num;
580 type->flags |= LYS_SET_FRDIGITS;
581 struct yin_subelement subelems[1] = {
Radek Krejcid6b76452019-09-03 17:03:03 +0200582 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
David Sedlákf75d55e2019-07-12 16:52:50 +0200583 };
Radek Krejcid6b76452019-09-03 17:03:03 +0200584 return yin_parse_content(ctx, subelems, 1, data, LY_STMT_FRACTION_DIGITS, NULL, &type->exts);
David Sedlákf75d55e2019-07-12 16:52:50 +0200585}
586
David Sedlák07869a52019-07-12 14:28:19 +0200587/**
David Sedlák43801c92019-08-05 15:58:54 +0200588 * @brief Parse enum element.
David Sedlák07869a52019-07-12 14:28:19 +0200589 *
590 * @param[in,out] ctx YIN parser context for logging and to store current state.
591 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
592 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200593 * @param[in,out] type Type structure to store parsed value, flags and extension instances.
David Sedlák07869a52019-07-12 14:28:19 +0200594 *
595 * @return LY_ERR values.
596 */
David Sedlákca36c422019-07-12 12:47:55 +0200597static LY_ERR
David Sedlák43801c92019-08-05 15:58:54 +0200598yin_parse_enum(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, struct lysp_type *type)
David Sedlákca36c422019-07-12 12:47:55 +0200599{
David Sedlák07869a52019-07-12 14:28:19 +0200600 struct lysp_type_enum *en;
David Sedlák1e696782019-07-17 15:06:07 +0200601
David Sedlák43801c92019-08-05 15:58:54 +0200602 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, type->enums, en, LY_EMEM);
603 type->flags |= LYS_SET_ENUM;
David Sedláke6284fc2019-09-10 11:57:02 +0200604 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &en->name, Y_STR_ARG, LY_STMT_ENUM));
David Sedlák43801c92019-08-05 15:58:54 +0200605 LY_CHECK_RET(lysp_check_enum_name((struct lys_parser_ctx *)ctx, en->name, strlen(en->name)));
606 YANG_CHECK_NONEMPTY((struct lys_parser_ctx *)ctx, strlen(en->name), "enum");
607 CHECK_UNIQUENESS((struct lys_parser_ctx *)ctx, type->enums, name, "enum", en->name);
David Sedlákca36c422019-07-12 12:47:55 +0200608
609 struct yin_subelement subelems[6] = {
Radek Krejcid6b76452019-09-03 17:03:03 +0200610 {LY_STMT_DESCRIPTION, &en->dsc, YIN_SUBELEM_UNIQUE},
611 {LY_STMT_IF_FEATURE, &en->iffeatures, 0},
612 {LY_STMT_REFERENCE, &en->ref, YIN_SUBELEM_UNIQUE},
613 {LY_STMT_STATUS, &en->flags, YIN_SUBELEM_UNIQUE},
614 {LY_STMT_VALUE, en, YIN_SUBELEM_UNIQUE},
615 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
David Sedlákca36c422019-07-12 12:47:55 +0200616 };
Radek Krejcid6b76452019-09-03 17:03:03 +0200617 return yin_parse_content(ctx, subelems, 6, data, LY_STMT_ENUM, NULL, &en->exts);
David Sedlák43801c92019-08-05 15:58:54 +0200618}
619
620/**
621 * @brief Parse bit element.
622 *
623 * @param[in,out] ctx YIN parser context for logging and to store current state.
624 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
625 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200626 * @param[in,out] type Type structure to store parsed value, flags and extension instances.
David Sedlák43801c92019-08-05 15:58:54 +0200627 *
628 * @return LY_ERR values.
629 */
630static LY_ERR
631yin_parse_bit(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
632 struct lysp_type *type)
633{
634 struct lysp_type_enum *en;
635
636 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, type->bits, en, LY_EMEM);
637 type->flags |= LYS_SET_BIT;
Radek Krejcid6b76452019-09-03 17:03:03 +0200638 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &en->name, Y_IDENTIF_ARG, LY_STMT_BIT));
David Sedlák43801c92019-08-05 15:58:54 +0200639 CHECK_UNIQUENESS((struct lys_parser_ctx *)ctx, type->enums, name, "bit", en->name);
640
641 struct yin_subelement subelems[6] = {
Radek Krejcid6b76452019-09-03 17:03:03 +0200642 {LY_STMT_DESCRIPTION, &en->dsc, YIN_SUBELEM_UNIQUE},
643 {LY_STMT_IF_FEATURE, &en->iffeatures, 0},
644 {LY_STMT_POSITION, en, YIN_SUBELEM_UNIQUE},
645 {LY_STMT_REFERENCE, &en->ref, YIN_SUBELEM_UNIQUE},
646 {LY_STMT_STATUS, &en->flags, YIN_SUBELEM_UNIQUE},
647 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
David Sedlák43801c92019-08-05 15:58:54 +0200648 };
Radek Krejcid6b76452019-09-03 17:03:03 +0200649 return yin_parse_content(ctx, subelems, 6, data, LY_STMT_BIT, NULL, &en->exts);
David Sedlákca36c422019-07-12 12:47:55 +0200650}
651
David Sedlákd3983112019-07-12 11:20:56 +0200652/**
David Sedlák5f8191e2019-07-08 16:35:52 +0200653 * @brief Parse simple element without any special constraints and argument mapped to yin attribute, that can have
654 * more instances, such as base or if-feature.
655 *
656 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200657 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
David Sedlák5f8191e2019-07-08 16:35:52 +0200658 * @param[in,out] data Data to read from, always moved to currently handled character.
659 * @param[in] kw Type of current element.
660 * @param[out] values Parsed values to add to.
661 * @param[in] arg_type Expected type of attribute.
David Sedlák292763b2019-07-09 11:10:53 +0200662 * @param[in] arg_val_type Type of expected value of attribute.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200663 * @param[in,out] exts Extension instances to add to.
David Sedlák5f8191e2019-07-08 16:35:52 +0200664 *
665 * @return LY_ERR values.
666 */
667static LY_ERR
Radek Krejcid6b76452019-09-03 17:03:03 +0200668yin_parse_simple_elements(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, enum ly_stmt kw,
David Sedlákc5b20842019-08-13 10:18:31 +0200669 const char ***values, enum yin_argument arg_type, enum yang_arg arg_val_type, struct lysp_ext_instance **exts)
David Sedlák5f8191e2019-07-08 16:35:52 +0200670{
671 const char **value;
David Sedlák5f8191e2019-07-08 16:35:52 +0200672 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *values, value, LY_EMEM);
David Sedlákcb5d83f2019-07-09 09:32:53 +0200673 uint32_t index = LY_ARRAY_SIZE(*values) - 1;
David Sedlák968ac342019-07-11 15:17:59 +0200674 struct yin_subelement subelems[1] = {
Radek Krejcid6b76452019-09-03 17:03:03 +0200675 {LY_STMT_EXTENSION_INSTANCE, &index, 0}
David Sedlák968ac342019-07-11 15:17:59 +0200676 };
677
David Sedlák1f90d252019-07-10 17:09:32 +0200678 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, arg_type, value, arg_val_type, kw));
David Sedlák5f8191e2019-07-08 16:35:52 +0200679
680 return yin_parse_content(ctx, subelems, 1, data, kw, NULL, exts);
681}
682
683/**
David Sedlák6542aed2019-08-14 10:47:43 +0200684 * @brief Parse simple element without any special constraints and argument mapped to yin attribute.
685 *
686 * @param[in,out] ctx YIN parser context for logging and to store current state.
687 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
688 * @param[in,out] data Data to read from, always moved to currently handled character.
689 * @param[in] kw Type of current element.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200690 * @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 +0200691 * @param[in] arg_type Expected type of attribute.
692 * @param[in] arg_val_type Type of expected value of attribute.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200693 * @param[in,out] exts Extension instances to add to.
David Sedlák6542aed2019-08-14 10:47:43 +0200694 *
695 * @return LY_ERR values.
696 */
697static LY_ERR
Radek Krejcid6b76452019-09-03 17:03:03 +0200698yin_parse_simple_elem(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, enum ly_stmt kw,
David Sedlák6542aed2019-08-14 10:47:43 +0200699 struct yin_subelement *subinfo, enum yin_argument arg_type, enum yang_arg arg_val_type, struct lysp_ext_instance **exts)
700{
701 if (subinfo->flags & YIN_SUBELEM_UNIQUE) {
702 LY_CHECK_RET(yin_parse_simple_element(ctx, attrs, data, kw, (const char **)subinfo->dest,
703 arg_type, arg_val_type, exts));
704 } else {
705 LY_CHECK_RET(yin_parse_simple_elements(ctx, attrs, data, kw, (const char ***)subinfo->dest,
706 arg_type, arg_val_type, exts));
707 }
708
709 return LY_SUCCESS;
710}
711
712/**
713 * @brief Parse base element.
714 *
715 * @param[in,out] ctx YIN parser context for logging and to store current state.
716 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
717 * @param[in,out] data Data to read from, always moved to currently handled character.
718 * @param[in] parent Identification of parent element.
719 * @param[out] dest Where parsed values should be stored.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200720 * @param[in,out] exts Extension instances to add to.
David Sedlák6542aed2019-08-14 10:47:43 +0200721 *
722 * @return LY_ERR values.
723 */
724static LY_ERR
Radek Krejcid6b76452019-09-03 17:03:03 +0200725yin_parse_base(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, enum ly_stmt parent,
David Sedlák6542aed2019-08-14 10:47:43 +0200726 void *dest, struct lysp_ext_instance **exts)
727{
728 struct lysp_type *type = NULL;
729
Radek Krejcid6b76452019-09-03 17:03:03 +0200730 if (parent == LY_STMT_TYPE) {
David Sedlák6542aed2019-08-14 10:47:43 +0200731 type = (struct lysp_type *)dest;
Radek Krejcid6b76452019-09-03 17:03:03 +0200732 LY_CHECK_RET(yin_parse_simple_elements(ctx, attrs, data, LY_STMT_BASE, &type->bases, YIN_ARG_NAME,
David Sedlák6542aed2019-08-14 10:47:43 +0200733 Y_PREF_IDENTIF_ARG, exts));
734 type->flags |= LYS_SET_BASE;
Radek Krejcid6b76452019-09-03 17:03:03 +0200735 } else if (parent == LY_STMT_IDENTITY) {
736 LY_CHECK_RET(yin_parse_simple_elements(ctx, attrs, data, LY_STMT_BASE, (const char ***)dest,
David Sedlák6542aed2019-08-14 10:47:43 +0200737 YIN_ARG_NAME, Y_PREF_IDENTIF_ARG, exts));
738 } else {
739 LOGINT(ctx->xml_ctx.ctx);
740 return LY_EINT;
741 }
742
743 return LY_SUCCESS;
744}
745
746/**
David Sedlákbf8a2b72019-08-14 16:48:10 +0200747 * @brief Parse require-instance element.
David Sedlákcf5569a2019-07-11 13:31:34 +0200748 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200749 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákcf5569a2019-07-11 13:31:34 +0200750 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
751 * @param[in,out] data Data to read from, always moved to currently handled character.
752 * @prama[out] type Type structure to store value, flag and extensions.
753 *
754 * @return LY_ERR values.
755 */
756static LY_ERR
757yin_pasrse_reqinstance(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs,
758 const char **data, struct lysp_type *type)
759{
760 const char *temp_val = NULL;
David Sedlák968ac342019-07-11 15:17:59 +0200761 struct yin_subelement subelems[1] = {
Radek Krejcid6b76452019-09-03 17:03:03 +0200762 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
David Sedlák968ac342019-07-11 15:17:59 +0200763 };
David Sedlákcf5569a2019-07-11 13:31:34 +0200764
765 type->flags |= LYS_SET_REQINST;
Radek Krejcid6b76452019-09-03 17:03:03 +0200766 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_REQUIRE_INSTANCE));
David Sedlákcf5569a2019-07-11 13:31:34 +0200767 if (strcmp(temp_val, "true") == 0) {
768 type->require_instance = 1;
769 } else if (strcmp(temp_val, "false") != 0) {
David Sedlák26ea1432019-08-14 13:42:23 +0200770 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value",
771 "require-instance", "true", "false");
David Sedlákcf5569a2019-07-11 13:31:34 +0200772 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
773 return LY_EVALID;
774 }
775 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
776
Radek Krejcid6b76452019-09-03 17:03:03 +0200777 return yin_parse_content(ctx, subelems, 1, data, LY_STMT_REQUIRE_INSTANCE, NULL, &type->exts);
David Sedlákcf5569a2019-07-11 13:31:34 +0200778}
779
780/**
David Sedlákce77bf52019-07-11 16:59:31 +0200781 * @brief Parse modifier element.
782 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200783 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákce77bf52019-07-11 16:59:31 +0200784 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
785 * @param[in,out] data Data to read from, always moved to currently handled character.
786 * @param[in,out] pat Value to write to.
787 * @param[in,out] exts Extension instances to add to.
788 *
789 * @return LY_ERR values.
790 */
791static LY_ERR
792yin_parse_modifier(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
793 const char **pat, struct lysp_ext_instance **exts)
794{
David Sedlákd3983112019-07-12 11:20:56 +0200795 assert(**pat == 0x06);
David Sedlákce77bf52019-07-11 16:59:31 +0200796 const char *temp_val;
797 char *modified_val;
798 struct yin_subelement subelems[1] = {
Radek Krejcid6b76452019-09-03 17:03:03 +0200799 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
David Sedlákce77bf52019-07-11 16:59:31 +0200800 };
801
Radek Krejcid6b76452019-09-03 17:03:03 +0200802 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_MODIFIER));
David Sedlákce77bf52019-07-11 16:59:31 +0200803 if (strcmp(temp_val, "invert-match") != 0) {
David Sedlák26ea1432019-08-14 13:42:23 +0200804 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS1, temp_val, "value",
805 "modifier", "invert-match");
David Sedlákce77bf52019-07-11 16:59:31 +0200806 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
807 return LY_EVALID;
808 }
David Sedlákd3983112019-07-12 11:20:56 +0200809 lydict_remove(ctx->xml_ctx.ctx, temp_val);
David Sedlákce77bf52019-07-11 16:59:31 +0200810
811 /* allocate new value */
David Sedlákd3983112019-07-12 11:20:56 +0200812 modified_val = malloc(strlen(*pat) + 1);
David Sedlákce77bf52019-07-11 16:59:31 +0200813 LY_CHECK_ERR_RET(!modified_val, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlákd3983112019-07-12 11:20:56 +0200814 strcpy(modified_val, *pat);
815 lydict_remove(ctx->xml_ctx.ctx, *pat);
David Sedlákce77bf52019-07-11 16:59:31 +0200816
817 /* modify the new value */
818 modified_val[0] = 0x15;
819 *pat = lydict_insert_zc(ctx->xml_ctx.ctx, modified_val);
820
Radek Krejcid6b76452019-09-03 17:03:03 +0200821 return yin_parse_content(ctx, subelems, 1, data, LY_STMT_MODIFIER, NULL, exts);
David Sedlákce77bf52019-07-11 16:59:31 +0200822}
823
824/**
David Sedlákb7296dd2019-07-11 14:58:38 +0200825 * @brief Parse a restriction element (length, range or one instance of must).
826 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200827 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákb7296dd2019-07-11 14:58:38 +0200828 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
829 * @param[in,out] data Data to read from, always moved to currently handled character.
Radek Krejcid6b76452019-09-03 17:03:03 +0200830 * @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 +0200831 * @param[in] restr Value to write to.
David Sedlákb7296dd2019-07-11 14:58:38 +0200832 */
833static LY_ERR
834yin_parse_restriction(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
Radek Krejcid6b76452019-09-03 17:03:03 +0200835 enum ly_stmt restr_kw, struct lysp_restr *restr)
David Sedlákb7296dd2019-07-11 14:58:38 +0200836{
Radek Krejcid6b76452019-09-03 17:03:03 +0200837 assert(restr_kw == LY_STMT_MUST || restr_kw == LY_STMT_LENGTH || restr_kw == LY_STMT_RANGE);
David Sedlákb7296dd2019-07-11 14:58:38 +0200838 struct yin_subelement subelems[5] = {
Radek Krejcid6b76452019-09-03 17:03:03 +0200839 {LY_STMT_DESCRIPTION, &restr->dsc, YIN_SUBELEM_UNIQUE},
840 {LY_STMT_ERROR_APP_TAG, &restr->eapptag, YIN_SUBELEM_UNIQUE},
841 {LY_STMT_ERROR_MESSAGE, &restr->emsg, YIN_SUBELEM_UNIQUE},
842 {LY_STMT_REFERENCE, &restr->ref, YIN_SUBELEM_UNIQUE},
843 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
David Sedlák968ac342019-07-11 15:17:59 +0200844 };
David Sedlákb7296dd2019-07-11 14:58:38 +0200845 /* argument of must is called condition, but argument of length and range is called value */
Radek Krejcid6b76452019-09-03 17:03:03 +0200846 enum yin_argument arg_type = (restr_kw == LY_STMT_MUST) ? YIN_ARG_CONDITION : YIN_ARG_VALUE;
David Sedlákb7296dd2019-07-11 14:58:38 +0200847 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, arg_type, &restr->arg, Y_STR_ARG, restr_kw));
848
849 return yin_parse_content(ctx, subelems, 5, data, restr_kw, NULL, &restr->exts);
850}
851
852/**
David Sedlák6542aed2019-08-14 10:47:43 +0200853 * @brief Parse range element.
854 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200855 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák6542aed2019-08-14 10:47:43 +0200856 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
857 * @param[in,out] data Data to read from, always moved to currently handled character.
858 * @param[out] type Type structure to store parsed value and flags.
859 *
860 * @return LY_ERR values.
861 */
862static LY_ERR
863yin_parse_range(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs,
864 const char **data, struct lysp_type *type)
865{
866 type->range = calloc(1, sizeof *type->range);
867 LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
Radek Krejcid6b76452019-09-03 17:03:03 +0200868 LY_CHECK_RET(yin_parse_restriction(ctx, attrs, data, LY_STMT_RANGE, type->range));
David Sedlák6542aed2019-08-14 10:47:43 +0200869 type->flags |= LYS_SET_RANGE;
870
871 return LY_SUCCESS;
872}
873
874/**
875 * @brief Parse length element.
876 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200877 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák6542aed2019-08-14 10:47:43 +0200878 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
879 * @param[in,out] data Data to read from, always moved to currently handled character.
880 * @param[out] type Type structure to store parsed value and flags.
881 *
882 * @return LY_ERR values.
883 */
884static LY_ERR
885yin_parse_length(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs,
886 const char **data, struct lysp_type *type)
887{
888 type->length = calloc(1, sizeof *type->length);
889 LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
Radek Krejcid6b76452019-09-03 17:03:03 +0200890 LY_CHECK_RET(yin_parse_restriction(ctx, attrs, data, LY_STMT_LENGTH, type->length));
David Sedlák6542aed2019-08-14 10:47:43 +0200891 type->flags |= LYS_SET_LENGTH;
892
893 return LY_SUCCESS;
894}
895
896/**
David Sedlákbc9ec9c2019-07-11 15:53:55 +0200897 * @brief Parse must element.
898 *
899 * @param[in,out] ctx YIN parser context for logging and to store current state.
900 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
901 * @param[in,out] data Data to read from, always moved to currently handled character.
902 * @param[in,out] restrs Restrictions to add to.
903 *
904 * @return LY_ERR values.
905 */
906static LY_ERR
907yin_parse_must(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, struct lysp_restr **restrs)
908{
909 struct lysp_restr *restr;
910
911 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *restrs, restr, LY_EMEM);
Radek Krejcid6b76452019-09-03 17:03:03 +0200912 return yin_parse_restriction(ctx, attrs, data, LY_STMT_MUST, restr);
David Sedlákbc9ec9c2019-07-11 15:53:55 +0200913}
914
915/**
David Sedlák5545f5d2019-07-11 11:55:16 +0200916 * @brief Parse position or value element.
917 *
918 * @param[in,out] ctx YIN parser context for logging and to store current state.
919 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
920 * @param[in,out] data Data to read from, always moved to currently handled character.
Radek Krejcid6b76452019-09-03 17:03:03 +0200921 * @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 +0200922 * @param[out] enm Enum structure to save value, flags and extension instances.
David Sedlák5545f5d2019-07-11 11:55:16 +0200923 *
924 * @return LY_ERR values.
925 */
926static LY_ERR
David Sedlákdf2691f2019-08-15 13:32:27 +0200927yin_parse_value_pos(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
Radek Krejcid6b76452019-09-03 17:03:03 +0200928 enum ly_stmt kw, struct lysp_type_enum *enm)
David Sedlák5545f5d2019-07-11 11:55:16 +0200929{
Radek Krejcid6b76452019-09-03 17:03:03 +0200930 assert(kw == LY_STMT_POSITION || kw == LY_STMT_VALUE);
David Sedlák5545f5d2019-07-11 11:55:16 +0200931 const char *temp_val = NULL;
932 char *ptr;
933 long int num;
934 unsigned long int unum;
935
936 /* set value flag */
937 enm->flags |= LYS_SET_VALUE;
938
939 /* get attribute value */
David Sedlákcf5569a2019-07-11 13:31:34 +0200940 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, kw));
David Sedlákae5378f2019-07-17 11:37:59 +0200941 if (!temp_val || temp_val[0] == '\0' || (temp_val[0] == '+') ||
Radek Krejcid6b76452019-09-03 17:03:03 +0200942 ((temp_val[0] == '0') && (temp_val[1] != '\0')) || ((kw == LY_STMT_POSITION) && !strcmp(temp_val, "-0"))) {
David Sedlák1538a842019-08-08 15:38:51 +0200943 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 +0200944 goto error;
945 }
946
947 /* convert value */
948 errno = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +0200949 if (kw == LY_STMT_VALUE) {
David Sedlák5545f5d2019-07-11 11:55:16 +0200950 num = strtol(temp_val, &ptr, 10);
951 if (num < INT64_C(-2147483648) || num > INT64_C(2147483647)) {
David Sedlák1538a842019-08-08 15:38:51 +0200952 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 +0200953 goto error;
954 }
955 } else {
956 unum = strtoul(temp_val, &ptr, 10);
957 if (unum > UINT64_C(4294967295)) {
David Sedlák1538a842019-08-08 15:38:51 +0200958 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 +0200959 goto error;
960 }
961 }
962 /* check if whole argument value was converted */
963 if (*ptr != '\0') {
David Sedlák1538a842019-08-08 15:38:51 +0200964 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 +0200965 goto error;
David Sedlák5545f5d2019-07-11 11:55:16 +0200966 }
967 if (errno == ERANGE) {
David Sedlák1538a842019-08-08 15:38:51 +0200968 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 +0200969 goto error;
970 }
971 /* save correctly ternary operator can't be used because num and unum have different signes */
Radek Krejcid6b76452019-09-03 17:03:03 +0200972 if (kw == LY_STMT_VALUE) {
David Sedlák5545f5d2019-07-11 11:55:16 +0200973 enm->value = num;
974 } else {
975 enm->value = unum;
976 }
977 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
978
979 /* parse subelements */
David Sedlák968ac342019-07-11 15:17:59 +0200980 struct yin_subelement subelems[1] = {
Radek Krejcid6b76452019-09-03 17:03:03 +0200981 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
David Sedlák968ac342019-07-11 15:17:59 +0200982 };
David Sedlák5545f5d2019-07-11 11:55:16 +0200983 return yin_parse_content(ctx, subelems, 1, data, kw, NULL, &enm->exts);
984
David Sedlákbf8a2b72019-08-14 16:48:10 +0200985error:
David Sedlák5545f5d2019-07-11 11:55:16 +0200986 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
987 return LY_EVALID;
988}
989
David Sedlák05404f62019-07-24 14:11:53 +0200990
991/**
992 * @brief Parse belongs-to element.
993 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200994 * @param[in] ctx YIN parser context for logging and to store current state.
David Sedlák05404f62019-07-24 14:11:53 +0200995 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
996 * @param[in,out] data Data to read from, always moved to currently handled character.
997 * @param[out] submod Structure of submodule that is being parsed.
998 * @param[in,out] exts Extension instances to add to.
999 *
1000 * @return LY_ERR values
1001 */
1002static LY_ERR
1003yin_parse_belongs_to(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1004 struct lysp_submodule *submod, struct lysp_ext_instance **exts)
1005{
1006 struct yin_subelement subelems[2] = {
Radek Krejcid6b76452019-09-03 17:03:03 +02001007 {LY_STMT_PREFIX, &submod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
1008 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
David Sedlák05404f62019-07-24 14:11:53 +02001009 };
Radek Krejcid6b76452019-09-03 17:03:03 +02001010 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_MODULE, &submod->belongsto, Y_IDENTIF_ARG, LY_STMT_BELONGS_TO));
David Sedlák05404f62019-07-24 14:11:53 +02001011
Radek Krejcid6b76452019-09-03 17:03:03 +02001012 return yin_parse_content(ctx, subelems, 2, data, LY_STMT_BELONGS_TO, NULL, exts);
David Sedlák05404f62019-07-24 14:11:53 +02001013}
1014
David Sedlák5545f5d2019-07-11 11:55:16 +02001015/**
David Sedlákc1771b12019-07-10 15:55:46 +02001016 * @brief Function to parse meta tags (description, contact, ...) eg. elements with
David Sedlákbf8a2b72019-08-14 16:48:10 +02001017 * text element as child.
David Sedlákb4e44562019-07-04 15:42:12 +02001018 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001019 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdf2a9732019-08-07 13:23:16 +02001020 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
David Sedlákcf5569a2019-07-11 13:31:34 +02001021 * @param[in,out] data Data to read from, always moved to currently handled character.
Radek Krejcid6b76452019-09-03 17:03:03 +02001022 * @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 +02001023 * @param[out] value Where the content of meta element should be stored.
David Sedlákbf8a2b72019-08-14 16:48:10 +02001024 * @param[in,out] exts Extension instances to add to.
David Sedlákb4e44562019-07-04 15:42:12 +02001025 *
1026 * @return LY_ERR values.
1027 */
1028static LY_ERR
David Sedlákdf2691f2019-08-15 13:32:27 +02001029yin_parse_meta(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
Radek Krejcid6b76452019-09-03 17:03:03 +02001030 enum ly_stmt elem_type, const char **value, struct lysp_ext_instance **exts)
David Sedlákb4e44562019-07-04 15:42:12 +02001031{
Radek Krejcid6b76452019-09-03 17:03:03 +02001032 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 +02001033
David Sedlák968ac342019-07-11 15:17:59 +02001034 struct yin_subelement subelems[2] = {
Radek Krejcid6b76452019-09-03 17:03:03 +02001035 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1036 {LY_STMT_ARG_TEXT, value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE | YIN_SUBELEM_FIRST}
David Sedlák968ac342019-07-11 15:17:59 +02001037 };
David Sedlákdf2a9732019-08-07 13:23:16 +02001038 /* check attributes */
1039 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NONE, NULL, Y_MAYBE_STR_ARG, elem_type));
David Sedlákb4e44562019-07-04 15:42:12 +02001040
David Sedlákdf2a9732019-08-07 13:23:16 +02001041 /* parse content */
David Sedlákda8ffa32019-07-08 14:17:10 +02001042 return yin_parse_content(ctx, subelems, 2, data, elem_type, NULL, exts);
David Sedlákb4e44562019-07-04 15:42:12 +02001043}
1044
1045/**
David Sedlákc1771b12019-07-10 15:55:46 +02001046 * @brief Parse error-message element.
1047 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001048 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdf2a9732019-08-07 13:23:16 +02001049 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
David Sedlákc1771b12019-07-10 15:55:46 +02001050 * @param[in,out] data Data to read from.
1051 * @param[out] value Where the content of error-message element should be stored.
David Sedlákbf8a2b72019-08-14 16:48:10 +02001052 * @param[in,out] exts Extension instances to add to.
David Sedlákc1771b12019-07-10 15:55:46 +02001053 *
1054 * @return LY_ERR values.
1055 */
1056static LY_ERR
David Sedlákdf2691f2019-08-15 13:32:27 +02001057yin_parse_err_msg(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
David Sedlákdf2a9732019-08-07 13:23:16 +02001058 const char **value, struct lysp_ext_instance **exts)
David Sedlákc1771b12019-07-10 15:55:46 +02001059{
David Sedlák968ac342019-07-11 15:17:59 +02001060 struct yin_subelement subelems[2] = {
Radek Krejcid6b76452019-09-03 17:03:03 +02001061 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1062 {LY_STMT_ARG_VALUE, value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE | YIN_SUBELEM_FIRST}
David Sedlák968ac342019-07-11 15:17:59 +02001063 };
David Sedlákc1771b12019-07-10 15:55:46 +02001064
David Sedlákdf2a9732019-08-07 13:23:16 +02001065 /* check attributes */
David Sedlák071f7662019-09-12 02:02:51 +02001066 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NONE, NULL, Y_MAYBE_STR_ARG, LY_STMT_ERROR_MESSAGE));
David Sedlákdf2a9732019-08-07 13:23:16 +02001067
Radek Krejcid6b76452019-09-03 17:03:03 +02001068 return yin_parse_content(ctx, subelems, 2, data, LY_STMT_ERROR_MESSAGE, NULL, exts);
David Sedlákc1771b12019-07-10 15:55:46 +02001069}
1070
1071/**
David Sedlák6542aed2019-08-14 10:47:43 +02001072 * @brief Parse type element.
David Sedlák374d2b32019-07-17 15:06:55 +02001073 *
1074 * @param[in,out] ctx YIN parser context for logging and to store current state.
1075 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1076 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlák6542aed2019-08-14 10:47:43 +02001077 * @param[in] parent Identification of parent element.
David Sedlákbf8a2b72019-08-14 16:48:10 +02001078 * @param[in,out] type Type to write to.
David Sedlák374d2b32019-07-17 15:06:55 +02001079 *
1080 * @return LY_ERR values.
1081 */
1082static LY_ERR
David Sedlák6542aed2019-08-14 10:47:43 +02001083yin_parse_type(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
Radek Krejcid6b76452019-09-03 17:03:03 +02001084 enum ly_stmt parent, struct yin_subelement *subinfo)
David Sedlák374d2b32019-07-17 15:06:55 +02001085{
David Sedlák6542aed2019-08-14 10:47:43 +02001086 struct lysp_type *type = NULL;
Radek Krejcid6b76452019-09-03 17:03:03 +02001087 if (parent == LY_STMT_DEVIATE) {
David Sedlák6542aed2019-08-14 10:47:43 +02001088 *(struct lysp_type **)subinfo->dest = calloc(1, sizeof **(struct lysp_type **)subinfo->dest);
1089 LY_CHECK_ERR_RET(!(*(struct lysp_type **)subinfo->dest), LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
1090 type = *((struct lysp_type **)subinfo->dest);
1091 } else {
1092 type = (struct lysp_type *)subinfo->dest;
1093 }
1094 /* type as child of another type */
Radek Krejcid6b76452019-09-03 17:03:03 +02001095 if (parent == LY_STMT_TYPE) {
David Sedlák6542aed2019-08-14 10:47:43 +02001096 struct lysp_type *nested_type = NULL;
1097 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, type->types, nested_type, LY_EMEM);
1098 type->flags |= LYS_SET_TYPE;
1099 type = nested_type;
1100 }
David Sedlák374d2b32019-07-17 15:06:55 +02001101 struct yin_subelement subelems[11] = {
Radek Krejcid6b76452019-09-03 17:03:03 +02001102 {LY_STMT_BASE, type, 0},
1103 {LY_STMT_BIT, type, 0},
1104 {LY_STMT_ENUM, type, 0},
1105 {LY_STMT_FRACTION_DIGITS, type, YIN_SUBELEM_UNIQUE},
1106 {LY_STMT_LENGTH, type, YIN_SUBELEM_UNIQUE},
1107 {LY_STMT_PATH, type, YIN_SUBELEM_UNIQUE},
1108 {LY_STMT_PATTERN, type, 0},
1109 {LY_STMT_RANGE, type, YIN_SUBELEM_UNIQUE},
1110 {LY_STMT_REQUIRE_INSTANCE, type, YIN_SUBELEM_UNIQUE},
1111 {LY_STMT_TYPE, type},
1112 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
David Sedlák374d2b32019-07-17 15:06:55 +02001113 };
Radek Krejcid6b76452019-09-03 17:03:03 +02001114 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &type->name, Y_PREF_IDENTIF_ARG, LY_STMT_TYPE));
1115 return yin_parse_content(ctx, subelems, 11, data, LY_STMT_TYPE, NULL, &type->exts);
David Sedlák374d2b32019-07-17 15:06:55 +02001116}
1117
David Sedlák1af868e2019-07-17 17:03:14 +02001118/**
1119 * @brief Parse max-elements element.
1120 *
1121 * @param[in,out] ctx YIN parser context for logging and to store current state.
1122 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1123 * @param[in,out] data Data to read from, always moved to currently handled character.
1124 * @param[in,out] max Value to write to.
David Sedláka2dad212019-07-18 12:45:19 +02001125 * @param[in] flags Flags to write to.
David Sedlák1af868e2019-07-17 17:03:14 +02001126 * @param[in,out] exts Extension instances to add to.
1127 *
1128 * @return LY_ERR values.
1129 */
1130static LY_ERR
1131yin_parse_maxelements(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint32_t *max,
1132 uint16_t *flags, struct lysp_ext_instance **exts)
1133{
1134 const char *temp_val = NULL;
1135 char *ptr;
1136 unsigned long int num;
1137 struct yin_subelement subelems[1] = {
Radek Krejcid6b76452019-09-03 17:03:03 +02001138 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
David Sedlák1af868e2019-07-17 17:03:14 +02001139 };
David Sedlák374d2b32019-07-17 15:06:55 +02001140
David Sedlák1af868e2019-07-17 17:03:14 +02001141 *flags |= LYS_SET_MAX;
Radek Krejcid6b76452019-09-03 17:03:03 +02001142 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_MAX_ELEMENTS));
David Sedlák1af868e2019-07-17 17:03:14 +02001143 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 +02001144 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "max-elements");
David Sedlák1af868e2019-07-17 17:03:14 +02001145 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1146 return LY_EVALID;
1147 }
1148
1149 if (strcmp(temp_val, "unbounded")) {
1150 errno = 0;
1151 num = strtoul(temp_val, &ptr, 10);
1152 if (*ptr != '\0') {
David Sedlák1538a842019-08-08 15:38:51 +02001153 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "max-elements");
David Sedlák1af868e2019-07-17 17:03:14 +02001154 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1155 return LY_EVALID;
1156 }
1157 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlák1538a842019-08-08 15:38:51 +02001158 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_OOB_YIN, temp_val, "value", "max-elements");
David Sedlák1af868e2019-07-17 17:03:14 +02001159 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1160 return LY_EVALID;
1161 }
1162 *max = num;
1163 }
1164 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
Radek Krejcid6b76452019-09-03 17:03:03 +02001165 return yin_parse_content(ctx, subelems, 1, data, LY_STMT_MAX_ELEMENTS, NULL, exts);
David Sedlák1af868e2019-07-17 17:03:14 +02001166}
David Sedlák374d2b32019-07-17 15:06:55 +02001167
1168/**
David Sedlákbf8a2b72019-08-14 16:48:10 +02001169 * @brief Parse min-elements element.
David Sedlák09e18c92019-07-18 11:17:11 +02001170 *
1171 * @param[in,out] ctx YIN parser context for logging and to store current state.
1172 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1173 * @param[in,out] data Data to read from, always moved to currently handled character.
1174 * @param[in,out] min Value to write to.
David Sedláka2dad212019-07-18 12:45:19 +02001175 * @param[in] flags Flags to write to.
David Sedlák09e18c92019-07-18 11:17:11 +02001176 * @param[in,out] exts Extension instances to add to.
1177 *
1178 * @return LY_ERR values.
1179 */
1180static LY_ERR
1181yin_parse_minelements(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint32_t *min,
1182 uint16_t *flags, struct lysp_ext_instance **exts)
1183{
1184 const char *temp_val = NULL;
1185 char *ptr;
1186 unsigned long int num;
1187 struct yin_subelement subelems[1] = {
Radek Krejcid6b76452019-09-03 17:03:03 +02001188 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
David Sedlák09e18c92019-07-18 11:17:11 +02001189 };
1190
1191 *flags |= LYS_SET_MIN;
Radek Krejcid6b76452019-09-03 17:03:03 +02001192 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_MIN_ELEMENTS));
David Sedlák09e18c92019-07-18 11:17:11 +02001193
1194 if (!temp_val || temp_val[0] == '\0' || (temp_val[0] == '0' && temp_val[1] != '\0')) {
David Sedlák1538a842019-08-08 15:38:51 +02001195 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "min-elements");
David Sedlák09e18c92019-07-18 11:17:11 +02001196 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1197 return LY_EVALID;
1198 }
1199
1200 errno = 0;
1201 num = strtoul(temp_val, &ptr, 10);
1202 if (ptr[0] != 0) {
David Sedlák1538a842019-08-08 15:38:51 +02001203 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "min-elements");
David Sedlák09e18c92019-07-18 11:17:11 +02001204 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1205 return LY_EVALID;
1206 }
1207 if (errno == ERANGE || num > UINT32_MAX) {
David Sedlák1538a842019-08-08 15:38:51 +02001208 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_OOB_YIN, temp_val, "value", "min-elements");
David Sedlák09e18c92019-07-18 11:17:11 +02001209 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1210 return LY_EVALID;
1211 }
1212 *min = num;
1213 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
Radek Krejcid6b76452019-09-03 17:03:03 +02001214 return yin_parse_content(ctx, subelems, 1, data, LY_STMT_MIN_ELEMENTS, NULL, exts);
David Sedlák09e18c92019-07-18 11:17:11 +02001215}
1216
David Sedláka2dad212019-07-18 12:45:19 +02001217/**
1218 * @brief Parse min-elements or max-elements element.
1219 *
1220 * @param[in,out] ctx YIN parser context for logging and to store current state.
1221 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1222 * @param[in,out] data Data to read from, always moved to currently handled character.
1223 * @param[in] parent Identification of parent element.
1224 * @param[in] current Identification of current element.
1225 * @param[in] dest Where the parsed value and flags should be stored.
1226 *
1227 * @return LY_ERR values.
1228 */
David Sedlák09e18c92019-07-18 11:17:11 +02001229static LY_ERR
1230yin_parse_minmax(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
Radek Krejcid6b76452019-09-03 17:03:03 +02001231 enum ly_stmt parent, enum ly_stmt current, void *dest)
David Sedlák09e18c92019-07-18 11:17:11 +02001232{
Radek Krejcid6b76452019-09-03 17:03:03 +02001233 assert(current == LY_STMT_MAX_ELEMENTS || current == LY_STMT_MIN_ELEMENTS);
1234 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 +02001235 uint32_t *lim;
1236 uint16_t *flags;
1237 struct lysp_ext_instance **exts;
1238
Radek Krejcid6b76452019-09-03 17:03:03 +02001239 if (parent == LY_STMT_LEAF_LIST) {
1240 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 +02001241 flags = &((struct lysp_node_leaflist *)dest)->flags;
1242 exts = &((struct lysp_node_leaflist *)dest)->exts;
Radek Krejcid6b76452019-09-03 17:03:03 +02001243 } else if (parent == LY_STMT_REFINE) {
1244 lim = (current == LY_STMT_MAX_ELEMENTS) ? &((struct lysp_refine *)dest)->max : &((struct lysp_refine *)dest)->min;
David Sedlák09e18c92019-07-18 11:17:11 +02001245 flags = &((struct lysp_refine *)dest)->flags;
1246 exts = &((struct lysp_refine *)dest)->exts;
Radek Krejcid6b76452019-09-03 17:03:03 +02001247 } else if (parent == LY_STMT_LIST) {
1248 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 +02001249 flags = &((struct lysp_node_list *)dest)->flags;
1250 exts = &((struct lysp_node_list *)dest)->exts;
David Sedlák4ffcec82019-07-25 15:10:21 +02001251 } else {
1252 lim = ((struct minmax_dev_meta *)dest)->lim;
1253 flags = ((struct minmax_dev_meta *)dest)->flags;
1254 exts = ((struct minmax_dev_meta *)dest)->exts;
David Sedlák09e18c92019-07-18 11:17:11 +02001255 }
1256
Radek Krejcid6b76452019-09-03 17:03:03 +02001257 if (current == LY_STMT_MAX_ELEMENTS) {
David Sedlák09e18c92019-07-18 11:17:11 +02001258 LY_CHECK_RET(yin_parse_maxelements(ctx, attrs, data, lim, flags, exts));
1259 } else {
1260 LY_CHECK_RET(yin_parse_minelements(ctx, attrs, data, lim, flags, exts));
1261 }
1262
1263 return LY_SUCCESS;
1264}
1265
1266/**
David Sedlákbf8a2b72019-08-14 16:48:10 +02001267 * @brief Parse ordered-by element.
David Sedláka2dad212019-07-18 12:45:19 +02001268 *
1269 * @param[in,out] ctx YIN parser context for logging and to store current state.
1270 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1271 * @param[in,out] data Data to read from, always moved to currently handled character.
1272 * @param[out] flags Flags to write to.
David Sedlákbf8a2b72019-08-14 16:48:10 +02001273 * @param[in,out] exts Extension instances to add to.
David Sedláka2dad212019-07-18 12:45:19 +02001274 *
1275 * @return LY_ERR values.
1276 */
1277static LY_ERR
1278yin_parse_orderedby(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1279 uint16_t *flags, struct lysp_ext_instance **exts)
1280{
1281 const char *temp_val;
1282 struct yin_subelement subelems[1] = {
Radek Krejcid6b76452019-09-03 17:03:03 +02001283 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
David Sedláka2dad212019-07-18 12:45:19 +02001284 };
1285
Radek Krejcid6b76452019-09-03 17:03:03 +02001286 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_ORDERED_BY));
David Sedláka2dad212019-07-18 12:45:19 +02001287 if (strcmp(temp_val, "system") == 0) {
1288 *flags |= LYS_ORDBY_SYSTEM;
1289 } else if (strcmp(temp_val, "user") == 0) {
1290 *flags |= LYS_ORDBY_USER;
1291 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02001292 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value",
1293 "ordered-by", "system", "user");
David Sedláka2dad212019-07-18 12:45:19 +02001294 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1295 return LY_EVALID;
1296 }
1297 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1298
Radek Krejcid6b76452019-09-03 17:03:03 +02001299 return yin_parse_content(ctx, subelems, 1, data, LY_STMT_ORDERED_BY, NULL, exts);
David Sedláka2dad212019-07-18 12:45:19 +02001300}
1301
1302/**
David Sedlákbf8a2b72019-08-14 16:48:10 +02001303 * @brief Parse any-data or any-xml element.
David Sedlák8a83bbb2019-07-18 14:46:00 +02001304 *
1305 * @param[in,out] ctx YIN parser context for logging and to store current state.
1306 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1307 * @param[in,out] data Data to read from, always moved to currently handled character.
Radek Krejcid6b76452019-09-03 17:03:03 +02001308 * @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 +02001309 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlák8a83bbb2019-07-18 14:46:00 +02001310 *
1311 * @return LY_ERR values.
1312 */
1313static LY_ERR
1314yin_parse_any(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
Radek Krejcid6b76452019-09-03 17:03:03 +02001315 enum ly_stmt any_kw, struct tree_node_meta *node_meta)
David Sedlák8a83bbb2019-07-18 14:46:00 +02001316{
David Sedlák8a83bbb2019-07-18 14:46:00 +02001317 struct lysp_node_anydata *any;
1318
David Sedlák8d552d62019-08-06 15:29:05 +02001319 /* create new sibling */
David Sedlákbf8a2b72019-08-14 16:48:10 +02001320 LY_LIST_NEW_RET(ctx->xml_ctx.ctx, node_meta->nodes, any, next);
Radek Krejcid6b76452019-09-03 17:03:03 +02001321 any->nodetype = (any_kw == LY_STMT_ANYDATA) ? LYS_ANYDATA : LYS_ANYXML;
David Sedlák8a83bbb2019-07-18 14:46:00 +02001322 any->parent = node_meta->parent;
1323
David Sedlákbf8a2b72019-08-14 16:48:10 +02001324 /* parse argument */
David Sedlák203ca3a2019-07-18 15:26:25 +02001325 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &any->name, Y_IDENTIF_ARG, any_kw));
David Sedlák8a83bbb2019-07-18 14:46:00 +02001326
1327 struct yin_subelement subelems[9] = {
Radek Krejcid6b76452019-09-03 17:03:03 +02001328 {LY_STMT_CONFIG, &any->flags, YIN_SUBELEM_UNIQUE},
1329 {LY_STMT_DESCRIPTION, &any->dsc, YIN_SUBELEM_UNIQUE},
1330 {LY_STMT_IF_FEATURE, &any->iffeatures, 0},
1331 {LY_STMT_MANDATORY, &any->flags, YIN_SUBELEM_UNIQUE},
1332 {LY_STMT_MUST, &any->musts, 0},
1333 {LY_STMT_REFERENCE, &any->ref, YIN_SUBELEM_UNIQUE},
1334 {LY_STMT_STATUS, &any->flags, YIN_SUBELEM_UNIQUE},
1335 {LY_STMT_WHEN, &any->when, YIN_SUBELEM_UNIQUE},
1336 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
David Sedlák8a83bbb2019-07-18 14:46:00 +02001337 };
1338 return yin_parse_content(ctx, subelems, 9, data, any_kw, NULL, &any->exts);
1339}
1340
1341/**
David Sedlák203ca3a2019-07-18 15:26:25 +02001342 * @brief parse leaf element.
1343 *
1344 * @param[in,out] ctx YIN parser context for logging and to store current state.
1345 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1346 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlákad83cf92019-08-13 12:53:53 +02001347 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlák203ca3a2019-07-18 15:26:25 +02001348 *
1349 * @return LY_ERR values.
1350 */
1351static LY_ERR
1352yin_parse_leaf(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1353 struct tree_node_meta *node_meta)
1354{
David Sedlák203ca3a2019-07-18 15:26:25 +02001355 struct lysp_node_leaf *leaf;
1356
David Sedlák8d552d62019-08-06 15:29:05 +02001357 /* create structure new leaf */
David Sedlákbf8a2b72019-08-14 16:48:10 +02001358 LY_LIST_NEW_RET(ctx->xml_ctx.ctx, node_meta->nodes, leaf, next);
David Sedlák203ca3a2019-07-18 15:26:25 +02001359 leaf->nodetype = LYS_LEAF;
1360 leaf->parent = node_meta->parent;
1361
David Sedlák203ca3a2019-07-18 15:26:25 +02001362 /* parser argument */
Radek Krejcid6b76452019-09-03 17:03:03 +02001363 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &leaf->name, Y_IDENTIF_ARG, LY_STMT_LEAF));
David Sedlák203ca3a2019-07-18 15:26:25 +02001364
1365 /* parse content */
1366 struct yin_subelement subelems[12] = {
Radek Krejcid6b76452019-09-03 17:03:03 +02001367 {LY_STMT_CONFIG, &leaf->flags, YIN_SUBELEM_UNIQUE},
1368 {LY_STMT_DEFAULT, &leaf->dflt, YIN_SUBELEM_UNIQUE},
1369 {LY_STMT_DESCRIPTION, &leaf->dsc, YIN_SUBELEM_UNIQUE},
1370 {LY_STMT_IF_FEATURE, &leaf->iffeatures, 0},
1371 {LY_STMT_MANDATORY, &leaf->flags, YIN_SUBELEM_UNIQUE},
1372 {LY_STMT_MUST, &leaf->musts, 0},
1373 {LY_STMT_REFERENCE, &leaf->ref, YIN_SUBELEM_UNIQUE},
1374 {LY_STMT_STATUS, &leaf->flags, YIN_SUBELEM_UNIQUE},
1375 {LY_STMT_TYPE, &leaf->type, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_MANDATORY},
1376 {LY_STMT_UNITS, &leaf->units, YIN_SUBELEM_UNIQUE},
1377 {LY_STMT_WHEN, &leaf->when, YIN_SUBELEM_UNIQUE},
1378 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
David Sedlák05404f62019-07-24 14:11:53 +02001379 };
Radek Krejcid6b76452019-09-03 17:03:03 +02001380 return yin_parse_content(ctx, subelems, 12, data, LY_STMT_LEAF, NULL, &leaf->exts);
David Sedlák203ca3a2019-07-18 15:26:25 +02001381}
1382
1383/**
David Sedlákc3da3ef2019-07-19 12:56:08 +02001384 * @brief Parse leaf-list element.
1385 *
1386 * @param[in,out] ctx YIN parser context for logging and to store current state.
1387 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1388 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlákad83cf92019-08-13 12:53:53 +02001389 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlákc3da3ef2019-07-19 12:56:08 +02001390 *
1391 * @return LY_ERR values.
1392 */
1393static LY_ERR
1394yin_parse_leaflist(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1395 struct tree_node_meta *node_meta)
1396{
David Sedlákc3da3ef2019-07-19 12:56:08 +02001397 struct lysp_node_leaflist *llist;
David Sedlákc3da3ef2019-07-19 12:56:08 +02001398
David Sedlákbf8a2b72019-08-14 16:48:10 +02001399 LY_LIST_NEW_RET(ctx->xml_ctx.ctx, node_meta->nodes, llist, next);
David Sedlák8d552d62019-08-06 15:29:05 +02001400
David Sedlákc3da3ef2019-07-19 12:56:08 +02001401 llist->nodetype = LYS_LEAFLIST;
1402 llist->parent = node_meta->parent;
1403
David Sedlákc3da3ef2019-07-19 12:56:08 +02001404 /* parse argument */
Radek Krejcid6b76452019-09-03 17:03:03 +02001405 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &llist->name, Y_IDENTIF_ARG, LY_STMT_LEAF_LIST));
David Sedlákc3da3ef2019-07-19 12:56:08 +02001406
1407 /* parse content */
1408 struct yin_subelement subelems[14] = {
Radek Krejcid6b76452019-09-03 17:03:03 +02001409 {LY_STMT_CONFIG, &llist->flags, YIN_SUBELEM_UNIQUE},
1410 {LY_STMT_DEFAULT, &llist->dflts, 0},
1411 {LY_STMT_DESCRIPTION, &llist->dsc, YIN_SUBELEM_UNIQUE},
1412 {LY_STMT_IF_FEATURE, &llist->iffeatures, 0},
1413 {LY_STMT_MAX_ELEMENTS, llist, YIN_SUBELEM_UNIQUE},
1414 {LY_STMT_MIN_ELEMENTS, llist, YIN_SUBELEM_UNIQUE},
1415 {LY_STMT_MUST, &llist->musts, 0},
1416 {LY_STMT_ORDERED_BY, &llist->flags, YIN_SUBELEM_UNIQUE},
1417 {LY_STMT_REFERENCE, &llist->ref, YIN_SUBELEM_UNIQUE},
1418 {LY_STMT_STATUS, &llist->flags, YIN_SUBELEM_UNIQUE},
1419 {LY_STMT_TYPE, &llist->type, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_MANDATORY},
1420 {LY_STMT_UNITS, &llist->units, YIN_SUBELEM_UNIQUE},
1421 {LY_STMT_WHEN, &llist->when, YIN_SUBELEM_UNIQUE},
1422 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
David Sedlákaa854b02019-07-22 14:17:10 +02001423 };
Radek Krejcid6b76452019-09-03 17:03:03 +02001424 LY_CHECK_RET(yin_parse_content(ctx, subelems, 14, data, LY_STMT_LEAF_LIST, NULL, &llist->exts));
David Sedlákc3da3ef2019-07-19 12:56:08 +02001425
David Sedlákbf8a2b72019-08-14 16:48:10 +02001426 /* check invalid combination of subelements */
David Sedlákc3da3ef2019-07-19 12:56:08 +02001427 if ((llist->min) && (llist->dflts)) {
David Sedlák1538a842019-08-08 15:38:51 +02001428 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INCHILDSTMSCOMB_YIN, "min-elements", "default", "leaf-list");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001429 return LY_EVALID;
1430 }
1431 if (llist->max && llist->min > llist->max) {
David Sedlák1538a842019-08-08 15:38:51 +02001432 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_MINMAX, llist->min, llist->max);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001433 return LY_EVALID;
1434 }
1435
1436 return LY_SUCCESS;
1437}
1438
1439/**
David Sedlák04e17b22019-07-19 15:29:48 +02001440 * @brief Parse typedef element.
1441 *
1442 * @param[in,out] ctx YIN parser context for logging and to store current state.
1443 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1444 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlák05404f62019-07-24 14:11:53 +02001445 * @param[in] typedef_meta Meta information about parent node and typedefs to add to.
David Sedlák04e17b22019-07-19 15:29:48 +02001446 *
1447 * @return LY_ERR values.
1448 */
1449static LY_ERR
1450yin_parse_typedef(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
David Sedlák6881b512019-08-13 12:52:00 +02001451 struct tree_node_meta *typedef_meta)
David Sedlák04e17b22019-07-19 15:29:48 +02001452{
1453 struct lysp_tpdf *tpdf;
David Sedlákbf8a2b72019-08-14 16:48:10 +02001454 struct lysp_tpdf **tpdfs = (struct lysp_tpdf **)typedef_meta->nodes;
David Sedlák6881b512019-08-13 12:52:00 +02001455 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *tpdfs, tpdf, LY_EMEM);
David Sedlák04e17b22019-07-19 15:29:48 +02001456
1457 /* parse argument */
Radek Krejcid6b76452019-09-03 17:03:03 +02001458 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &tpdf->name, Y_IDENTIF_ARG, LY_STMT_TYPEDEF));
David Sedlák04e17b22019-07-19 15:29:48 +02001459
1460 /* parse content */
1461 struct yin_subelement subelems[7] = {
Radek Krejcid6b76452019-09-03 17:03:03 +02001462 {LY_STMT_DEFAULT, &tpdf->dflt, YIN_SUBELEM_UNIQUE},
1463 {LY_STMT_DESCRIPTION, &tpdf->dsc, YIN_SUBELEM_UNIQUE},
1464 {LY_STMT_REFERENCE, &tpdf->ref, YIN_SUBELEM_UNIQUE},
1465 {LY_STMT_STATUS, &tpdf->flags, YIN_SUBELEM_UNIQUE},
1466 {LY_STMT_TYPE, &tpdf->type, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_MANDATORY},
1467 {LY_STMT_UNITS, &tpdf->units, YIN_SUBELEM_UNIQUE},
1468 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
David Sedlákaa854b02019-07-22 14:17:10 +02001469 };
Radek Krejcid6b76452019-09-03 17:03:03 +02001470 LY_CHECK_RET(yin_parse_content(ctx, subelems, 7, data, LY_STMT_TYPEDEF, NULL, &tpdf->exts));
David Sedlák04e17b22019-07-19 15:29:48 +02001471
1472 /* store data for collision check */
1473 if (typedef_meta->parent && !(typedef_meta->parent->nodetype & (LYS_GROUPING | LYS_ACTION | LYS_INOUT | LYS_NOTIF))) {
David Sedláke8b74df2019-08-14 14:18:22 +02001474 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, typedef_meta->parent, 0) == -1, LY_EMEM);
David Sedlák04e17b22019-07-19 15:29:48 +02001475 }
1476
1477 return LY_SUCCESS;
1478}
1479
1480/**
David Sedlákd2d676a2019-07-22 11:28:19 +02001481 * @brief Parse refine element.
1482 *
1483 * @param[in,out] ctx YIN parser context for logging and to store current state.
1484 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1485 * @param[in,out] data Data to read from, always moved to currently handled character.
1486 * @param[in,out] refines Refines to add to.
1487 *
1488 * @return LY_ERR values.
1489 */
1490static LY_ERR
1491yin_parse_refine(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1492 struct lysp_refine **refines)
1493{
1494 struct lysp_refine *rf;
1495
1496 /* allocate new refine */
1497 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *refines, rf, LY_EMEM);
1498
1499 /* parse attribute */
Radek Krejcid6b76452019-09-03 17:03:03 +02001500 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_TARGET_NODE, &rf->nodeid, Y_STR_ARG, LY_STMT_REFINE));
David Sedlákd2d676a2019-07-22 11:28:19 +02001501 YANG_CHECK_NONEMPTY((struct lys_parser_ctx *)ctx, strlen(rf->nodeid), "refine");
1502
1503 /* parse content */
1504 struct yin_subelement subelems[11] = {
Radek Krejcid6b76452019-09-03 17:03:03 +02001505 {LY_STMT_CONFIG, &rf->flags, YIN_SUBELEM_UNIQUE},
1506 {LY_STMT_DEFAULT, &rf->dflts, 0},
1507 {LY_STMT_DESCRIPTION, &rf->dsc, YIN_SUBELEM_UNIQUE},
1508 {LY_STMT_IF_FEATURE, &rf->iffeatures, 0},
1509 {LY_STMT_MANDATORY, &rf->flags, YIN_SUBELEM_UNIQUE},
1510 {LY_STMT_MAX_ELEMENTS, rf, YIN_SUBELEM_UNIQUE},
1511 {LY_STMT_MIN_ELEMENTS, rf, YIN_SUBELEM_UNIQUE},
1512 {LY_STMT_MUST, &rf->musts, 0},
1513 {LY_STMT_PRESENCE, &rf->presence, YIN_SUBELEM_UNIQUE},
1514 {LY_STMT_REFERENCE, &rf->ref, YIN_SUBELEM_UNIQUE},
1515 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
David Sedlákaa854b02019-07-22 14:17:10 +02001516 };
Radek Krejcid6b76452019-09-03 17:03:03 +02001517 return yin_parse_content(ctx, subelems, 11, data, LY_STMT_REFINE, NULL, &rf->exts);
David Sedlákd2d676a2019-07-22 11:28:19 +02001518}
1519
1520/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02001521 * @brief Parse uses element.
1522 *
1523 * @param[in,out] ctx YIN parser context for logging and to store current state.
1524 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1525 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlákad83cf92019-08-13 12:53:53 +02001526 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlák0d6de5a2019-07-22 13:25:44 +02001527 *
1528 * @return LY_ERR values.
1529 */
1530static LY_ERR
1531yin_parse_uses(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1532 struct tree_node_meta *node_meta)
1533{
David Sedlák0d6de5a2019-07-22 13:25:44 +02001534 struct lysp_node_uses *uses;
1535
David Sedlák8d552d62019-08-06 15:29:05 +02001536 /* create new uses */
David Sedlákbf8a2b72019-08-14 16:48:10 +02001537 LY_LIST_NEW_RET(ctx->xml_ctx.ctx, node_meta->nodes, uses, next);
David Sedlák0d6de5a2019-07-22 13:25:44 +02001538 uses->nodetype = LYS_USES;
1539 uses->parent = node_meta->parent;
1540
David Sedlák0d6de5a2019-07-22 13:25:44 +02001541 /* parse argument */
Radek Krejcid6b76452019-09-03 17:03:03 +02001542 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &uses->name, Y_PREF_IDENTIF_ARG, LY_STMT_USES));
David Sedlák0d6de5a2019-07-22 13:25:44 +02001543
1544 /* parse content */
David Sedlák6881b512019-08-13 12:52:00 +02001545 struct tree_node_meta augments = {(struct lysp_node *)uses, (struct lysp_node **)&uses->augments};
David Sedlák0d6de5a2019-07-22 13:25:44 +02001546 struct yin_subelement subelems[8] = {
Radek Krejcid6b76452019-09-03 17:03:03 +02001547 {LY_STMT_AUGMENT, &augments, 0},
1548 {LY_STMT_DESCRIPTION, &uses->dsc, YIN_SUBELEM_UNIQUE},
1549 {LY_STMT_IF_FEATURE, &uses->iffeatures, 0},
1550 {LY_STMT_REFERENCE, &uses->ref, YIN_SUBELEM_UNIQUE},
1551 {LY_STMT_REFINE, &uses->refines, 0},
1552 {LY_STMT_STATUS, &uses->flags, YIN_SUBELEM_UNIQUE},
1553 {LY_STMT_WHEN, &uses->when, YIN_SUBELEM_UNIQUE},
1554 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
David Sedlákaa854b02019-07-22 14:17:10 +02001555 };
Radek Krejcid6b76452019-09-03 17:03:03 +02001556 LY_CHECK_RET(yin_parse_content(ctx, subelems, 8, data, LY_STMT_USES, NULL, &uses->exts));
David Sedlák0d6de5a2019-07-22 13:25:44 +02001557 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, NULL, uses->augments, NULL, NULL));
1558
1559 return LY_SUCCESS;
1560}
1561
1562/**
David Sedlákaa854b02019-07-22 14:17:10 +02001563 * @brief Parse revision element.
1564 *
1565 * @param[in,out] ctx YIN parser context for logging and to store current state.
1566 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1567 * @param[in,out] data Data to read from, always moved to currently handled character.
1568 * @param[in,out] revs Parsed revisions to add to.
1569 *
1570 * @return LY_ERR values.
1571 */
1572static LY_ERR
1573yin_parse_revision(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1574 struct lysp_revision **revs)
1575{
1576 struct lysp_revision *rev;
1577 const char *temp_date = NULL;
1578
1579 /* allocate new reivison */
1580 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *revs, rev, LY_EMEM);
1581
1582 /* parse argument */
Radek Krejcid6b76452019-09-03 17:03:03 +02001583 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_DATE, &temp_date, Y_STR_ARG, LY_STMT_REVISION));
David Sedlákaa854b02019-07-22 14:17:10 +02001584 /* check value */
1585 if (lysp_check_date((struct lys_parser_ctx *)ctx, temp_date, strlen(temp_date), "revision")) {
1586 FREE_STRING(ctx->xml_ctx.ctx, temp_date);
1587 return LY_EVALID;
1588 }
1589 strcpy(rev->date, temp_date);
1590 FREE_STRING(ctx->xml_ctx.ctx, temp_date);
1591
1592 /* parse content */
1593 struct yin_subelement subelems[3] = {
Radek Krejcid6b76452019-09-03 17:03:03 +02001594 {LY_STMT_DESCRIPTION, &rev->dsc, YIN_SUBELEM_UNIQUE},
1595 {LY_STMT_REFERENCE, &rev->ref, YIN_SUBELEM_UNIQUE},
1596 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
David Sedlákaa854b02019-07-22 14:17:10 +02001597 };
Radek Krejcid6b76452019-09-03 17:03:03 +02001598 return yin_parse_content(ctx, subelems, 3, data, LY_STMT_REVISION, NULL, &rev->exts);
David Sedlákaa854b02019-07-22 14:17:10 +02001599}
1600
David Sedlák5e13dea2019-07-22 16:06:45 +02001601/**
1602 * @brief Parse include element.
1603 *
1604 * @param[in,out] ctx YIN parser context for logging and to store current state.
1605 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1606 * @param[in,out] data Data to read from, always moved to currently handled character.
1607 * @param[in,out] inc_meta Meta informatinou about module/submodule name and includes to add to.
1608 *
1609 * @return LY_ERR values.
1610 */
David Sedlák0c2bab92019-07-22 15:33:19 +02001611static LY_ERR
1612yin_parse_include(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1613 struct include_meta *inc_meta)
1614{
1615 struct lysp_include *inc;
1616
1617 /* allocate new include */
1618 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *inc_meta->includes, inc, LY_EMEM);
1619
1620 /* parse argument */
Radek Krejcid6b76452019-09-03 17:03:03 +02001621 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_MODULE, &inc->name, Y_IDENTIF_ARG, LY_STMT_INCLUDE));
David Sedlák0c2bab92019-07-22 15:33:19 +02001622
1623 /* submodules share the namespace with the module names, so there must not be
1624 * a module of the same name in the context, no need for revision matching */
1625 if (!strcmp(inc_meta->name, inc->name) || ly_ctx_get_module_latest(ctx->xml_ctx.ctx, inc->name)) {
David Sedlák1538a842019-08-08 15:38:51 +02001626 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_NAME_COL, inc->name);
David Sedlák0c2bab92019-07-22 15:33:19 +02001627 return LY_EVALID;
1628 }
1629
1630 /* parse content */
1631 struct yin_subelement subelems[4] = {
Radek Krejcid6b76452019-09-03 17:03:03 +02001632 {LY_STMT_DESCRIPTION, &inc->dsc, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_VER2},
1633 {LY_STMT_REFERENCE, &inc->ref, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_VER2},
1634 {LY_STMT_REVISION_DATE, &inc->rev, YIN_SUBELEM_UNIQUE},
1635 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
David Sedlák0c2bab92019-07-22 15:33:19 +02001636 };
Radek Krejcid6b76452019-09-03 17:03:03 +02001637 return yin_parse_content(ctx, subelems, 4, data, LY_STMT_INCLUDE, NULL, &inc->exts);
David Sedlák0c2bab92019-07-22 15:33:19 +02001638}
1639
David Sedlákaa854b02019-07-22 14:17:10 +02001640/**
David Sedlákbf8a2b72019-08-14 16:48:10 +02001641 * @brief Parse revision-date element.
David Sedlákdfbbb442019-08-06 16:33:21 +02001642 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001643 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001644 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of revision-date element.
1645 * @param[in,out] data Data to read from, always moved to currently handled character.
1646 * @param[in,out] rev Array to store the parsed value in.
1647 * @param[in,out] exts Extension instances to add to.
1648 *
1649 * @return LY_ERR values.
1650 */
1651static LY_ERR
1652yin_parse_revision_date(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, char *rev,
1653 struct lysp_ext_instance **exts)
1654{
1655 const char *temp_rev;
1656 struct yin_subelement subelems[1] = {
Radek Krejcid6b76452019-09-03 17:03:03 +02001657 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
David Sedlákdfbbb442019-08-06 16:33:21 +02001658 };
1659
Radek Krejcid6b76452019-09-03 17:03:03 +02001660 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_DATE, &temp_rev, Y_STR_ARG, LY_STMT_REVISION_DATE));
David Sedlákdfbbb442019-08-06 16:33:21 +02001661 LY_CHECK_ERR_RET(lysp_check_date((struct lys_parser_ctx *)ctx, temp_rev, strlen(temp_rev), "revision-date") != LY_SUCCESS,
1662 FREE_STRING(ctx->xml_ctx.ctx, temp_rev), LY_EVALID);
1663
1664 strcpy(rev, temp_rev);
1665 FREE_STRING(ctx->xml_ctx.ctx, temp_rev);
1666
Radek Krejcid6b76452019-09-03 17:03:03 +02001667 return yin_parse_content(ctx, subelems, 1, data, LY_STMT_REVISION_DATE, NULL, exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02001668}
1669
1670/**
1671 * @brief Parse config element.
1672 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001673 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001674 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of import element.
1675 * @param[in,out] data Data to read from, always moved to currently handled character.
1676 * @param[in,out] flags Flags to add to.
1677 * @param[in,out] exts Extension instances to add to.
1678 *
1679 * @return LY_ERR values.
1680 */
1681static LY_ERR
1682yin_parse_config(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint16_t *flags,
1683 struct lysp_ext_instance **exts)
1684{
1685 const char *temp_val = NULL;
1686 struct yin_subelement subelems[1] = {
Radek Krejcid6b76452019-09-03 17:03:03 +02001687 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
David Sedlákdfbbb442019-08-06 16:33:21 +02001688 };
1689
Radek Krejcid6b76452019-09-03 17:03:03 +02001690 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_CONFIG));
David Sedlákdfbbb442019-08-06 16:33:21 +02001691 if (strcmp(temp_val, "true") == 0) {
1692 *flags |= LYS_CONFIG_W;
1693 } else if (strcmp(temp_val, "false") == 0) {
1694 *flags |= LYS_CONFIG_R;
1695 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02001696 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value", "config",
1697 "true", "false");
David Sedlákdfbbb442019-08-06 16:33:21 +02001698 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1699 return LY_EVALID;
1700 }
1701 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1702
Radek Krejcid6b76452019-09-03 17:03:03 +02001703 return yin_parse_content(ctx, subelems, 1, data, LY_STMT_CONFIG, NULL, exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02001704}
1705
1706/**
1707 * @brief Parse yang-version element.
1708 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001709 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001710 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of yang-version element.
1711 * @param[in] data Data to read from, always moved to currently handled character.
1712 * @param[out] version Storage for the parsed information.
David Sedlákbf8a2b72019-08-14 16:48:10 +02001713 * @param[in,out] exts Extension instances to add to.
David Sedlákdfbbb442019-08-06 16:33:21 +02001714 *
1715 * @return LY_ERR values.
1716 */
1717static LY_ERR
1718yin_parse_yangversion(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint8_t *version,
1719 struct lysp_ext_instance **exts)
1720{
1721 const char *temp_version = NULL;
1722 struct yin_subelement subelems[1] = {
Radek Krejcid6b76452019-09-03 17:03:03 +02001723 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
David Sedlákdfbbb442019-08-06 16:33:21 +02001724 };
1725
Radek Krejcid6b76452019-09-03 17:03:03 +02001726 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_version, Y_STR_ARG, LY_STMT_YANG_VERSION));
David Sedlákdfbbb442019-08-06 16:33:21 +02001727 if (strcmp(temp_version, "1.0") == 0) {
1728 *version = LYS_VERSION_1_0;
1729 } else if (strcmp(temp_version, "1.1") == 0) {
1730 *version = LYS_VERSION_1_1;
1731 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02001732 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_version, "value",
1733 "yang-version", "1.0", "1.1");
David Sedlákdfbbb442019-08-06 16:33:21 +02001734 FREE_STRING(ctx->xml_ctx.ctx, temp_version);
1735 return LY_EVALID;
1736 }
1737 FREE_STRING(ctx->xml_ctx.ctx, temp_version);
1738 ctx->mod_version = *version;
1739
Radek Krejcid6b76452019-09-03 17:03:03 +02001740 return yin_parse_content(ctx, subelems, 1, data, LY_STMT_YANG_VERSION, NULL, exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02001741}
1742
1743/**
1744 * @brief Parse import element.
1745 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001746 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001747 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of import element.
1748 * @param[in,out] data Data to read from, always moved to currently handled character.
1749 * @param[in,out] imp_meta Meta information about prefix and imports to add to.
1750 *
1751 * @return LY_ERR values.
1752 */
1753static LY_ERR
1754yin_parse_import(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, struct import_meta *imp_meta)
1755{
1756 struct lysp_import *imp;
1757 /* allocate new element in sized array for import */
1758 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *imp_meta->imports, imp, LY_EMEM);
1759
1760 struct yin_subelement subelems[5] = {
Radek Krejcid6b76452019-09-03 17:03:03 +02001761 {LY_STMT_DESCRIPTION, &imp->dsc, YIN_SUBELEM_UNIQUE},
1762 {LY_STMT_PREFIX, &imp->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
1763 {LY_STMT_REFERENCE, &imp->ref, YIN_SUBELEM_UNIQUE},
1764 {LY_STMT_REVISION_DATE, imp->rev, YIN_SUBELEM_UNIQUE},
1765 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
David Sedlákdfbbb442019-08-06 16:33:21 +02001766 };
1767
1768 /* parse import attributes */
Radek Krejcid6b76452019-09-03 17:03:03 +02001769 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_MODULE, &imp->name, Y_IDENTIF_ARG, LY_STMT_IMPORT));
1770 LY_CHECK_RET(yin_parse_content(ctx, subelems, 5, data, LY_STMT_IMPORT, NULL, &imp->exts));
David Sedlákdfbbb442019-08-06 16:33:21 +02001771 /* check prefix validity */
1772 LY_CHECK_RET(lysp_check_prefix((struct lys_parser_ctx *)ctx, *imp_meta->imports, imp_meta->prefix, &imp->prefix), LY_EVALID);
1773
1774 return LY_SUCCESS;
1775}
1776
1777/**
1778 * @brief Parse mandatory element.
1779 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001780 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001781 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of status element.
1782 * @param[in,out] data Data to read from, always moved to currently handled character.
1783 * @param[in,out] flags Flags to add to.
1784 * @param[in,out] exts Extension instances to add to.
1785 *
1786 * @return LY_ERR values.
1787 */
1788static LY_ERR
1789yin_parse_mandatory(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint16_t *flags,
1790 struct lysp_ext_instance **exts)
1791{
1792 const char *temp_val = NULL;
1793 struct yin_subelement subelems[1] = {
Radek Krejcid6b76452019-09-03 17:03:03 +02001794 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
David Sedlákdfbbb442019-08-06 16:33:21 +02001795 };
1796
Radek Krejcid6b76452019-09-03 17:03:03 +02001797 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_MANDATORY));
David Sedlákdfbbb442019-08-06 16:33:21 +02001798 if (strcmp(temp_val, "true") == 0) {
1799 *flags |= LYS_MAND_TRUE;
1800 } else if (strcmp(temp_val, "false") == 0) {
1801 *flags |= LYS_MAND_FALSE;
1802 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02001803 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value",
1804 "mandatory", "true", "false");
David Sedlákdfbbb442019-08-06 16:33:21 +02001805 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1806 return LY_EVALID;
1807 }
1808 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1809
Radek Krejcid6b76452019-09-03 17:03:03 +02001810 return yin_parse_content(ctx, subelems, 1, data, LY_STMT_MANDATORY, NULL, exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02001811}
1812
1813/**
1814 * @brief Parse status element.
1815 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001816 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001817 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of status element.
1818 * @param[in,out] data Data to read from, always moved to currently handled character.
1819 * @param[in,out] flags Flags to add to.
1820 * @param[in,out] exts Extension instances to add to.
1821 *
1822 * @return LY_ERR values.
1823 */
1824static LY_ERR
1825yin_parse_status(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint16_t *flags,
1826 struct lysp_ext_instance **exts)
1827{
1828 const char *value = NULL;
1829 struct yin_subelement subelems[1] = {
Radek Krejcid6b76452019-09-03 17:03:03 +02001830 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
David Sedlákdfbbb442019-08-06 16:33:21 +02001831 };
1832
Radek Krejcid6b76452019-09-03 17:03:03 +02001833 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &value, Y_STR_ARG, LY_STMT_STATUS));
David Sedlákdfbbb442019-08-06 16:33:21 +02001834 if (strcmp(value, "current") == 0) {
1835 *flags |= LYS_STATUS_CURR;
1836 } else if (strcmp(value, "deprecated") == 0) {
1837 *flags |= LYS_STATUS_DEPRC;
1838 } else if (strcmp(value, "obsolete") == 0) {
1839 *flags |= LYS_STATUS_OBSLT;
1840 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02001841 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS3, value, "value",
1842 "status", "current", "deprecated", "obsolete");
David Sedlákdfbbb442019-08-06 16:33:21 +02001843 FREE_STRING(ctx->xml_ctx.ctx, value);
1844 return LY_EVALID;
1845 }
1846 FREE_STRING(ctx->xml_ctx.ctx, value);
1847
Radek Krejcid6b76452019-09-03 17:03:03 +02001848 return yin_parse_content(ctx, subelems, 1, data, LY_STMT_STATUS, NULL, exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02001849}
1850
1851/**
1852 * @brief Parse when element.
1853 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001854 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001855 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of when element.
1856 * @param[in,out] data Data to read from, always moved to currently handled character.
1857 * @param[out] when_p When pointer to parse to.
1858 */
1859static LY_ERR
1860yin_parse_when(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, struct lysp_when **when_p)
1861{
1862 struct lysp_when *when;
David Sedláka56e0012019-08-15 13:21:25 +02001863 LY_ERR ret = LY_SUCCESS;
1864
David Sedlákdfbbb442019-08-06 16:33:21 +02001865 when = calloc(1, sizeof *when);
1866 LY_CHECK_ERR_RET(!when, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
Radek Krejcid6b76452019-09-03 17:03:03 +02001867 ret = yin_parse_attribute(ctx, attrs, YIN_ARG_CONDITION, &when->cond, Y_STR_ARG, LY_STMT_WHEN);
David Sedláka56e0012019-08-15 13:21:25 +02001868 LY_CHECK_ERR_RET(ret, free(when), ret);
1869
David Sedlákdfbbb442019-08-06 16:33:21 +02001870 *when_p = when;
1871 struct yin_subelement subelems[3] = {
Radek Krejcid6b76452019-09-03 17:03:03 +02001872 {LY_STMT_DESCRIPTION, &when->dsc, YIN_SUBELEM_UNIQUE},
1873 {LY_STMT_REFERENCE, &when->ref, YIN_SUBELEM_UNIQUE},
1874 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
David Sedlákdfbbb442019-08-06 16:33:21 +02001875 };
1876
Radek Krejcid6b76452019-09-03 17:03:03 +02001877 return yin_parse_content(ctx, subelems, 3, data, LY_STMT_WHEN, NULL, &when->exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02001878}
1879
1880/**
1881 * @brief Parse yin-elemenet element.
1882 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001883 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001884 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of yin-element element.
1885 * @param[in,out] data Data to read from, always moved to currently handled position.
1886 * @param[in,out] flags Flags to add to.
David Sedlákbf8a2b72019-08-14 16:48:10 +02001887 * @prama[in,out] exts Extension instances to add to.
David Sedlákdfbbb442019-08-06 16:33:21 +02001888 *
1889 * @return LY_ERR values.
1890 */
1891static LY_ERR
David Sedlákdf2691f2019-08-15 13:32:27 +02001892yin_parse_yin_element(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
David Sedlákdfbbb442019-08-06 16:33:21 +02001893 uint16_t *flags, struct lysp_ext_instance **exts)
1894{
1895 const char *temp_val = NULL;
1896 struct yin_subelement subelems[1] = {
Radek Krejcid6b76452019-09-03 17:03:03 +02001897 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
David Sedlákdfbbb442019-08-06 16:33:21 +02001898 };
1899
Radek Krejcid6b76452019-09-03 17:03:03 +02001900 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_YIN_ELEMENT));
David Sedlákdfbbb442019-08-06 16:33:21 +02001901 if (strcmp(temp_val, "true") == 0) {
1902 *flags |= LYS_YINELEM_TRUE;
1903 } else if (strcmp(temp_val, "false") == 0) {
1904 *flags |= LYS_YINELEM_FALSE;
1905 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02001906 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value",
1907 "yin-element", "true", "false");
David Sedlákdfbbb442019-08-06 16:33:21 +02001908 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1909 return LY_EVALID;
1910 }
1911 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1912
Radek Krejcid6b76452019-09-03 17:03:03 +02001913 return yin_parse_content(ctx, subelems, 1, data, LY_STMT_YIN_ELEMENT, NULL, exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02001914}
1915
1916/**
1917 * @brief Parse argument element.
1918 *
1919 * @param[in,out] xml_ctx Xml context.
1920 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of argument element.
1921 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlákbf8a2b72019-08-14 16:48:10 +02001922 * @param[in,out] arg_meta Meta information about destionation of parsed data.
1923 * @param[in,out] exts Extension instances to add to.
David Sedlákdfbbb442019-08-06 16:33:21 +02001924 *
1925 * @return LY_ERR values.
1926 */
1927static LY_ERR
David Sedlákdf2691f2019-08-15 13:32:27 +02001928yin_parse_argument(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
David Sedlákdfbbb442019-08-06 16:33:21 +02001929 struct yin_argument_meta *arg_meta, struct lysp_ext_instance **exts)
1930{
1931 struct yin_subelement subelems[2] = {
Radek Krejcid6b76452019-09-03 17:03:03 +02001932 {LY_STMT_YIN_ELEMENT, arg_meta->flags, YIN_SUBELEM_UNIQUE},
1933 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
David Sedlákdfbbb442019-08-06 16:33:21 +02001934 };
1935
Radek Krejcid6b76452019-09-03 17:03:03 +02001936 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, arg_meta->argument, Y_IDENTIF_ARG, LY_STMT_ARGUMENT));
David Sedlákdfbbb442019-08-06 16:33:21 +02001937
Radek Krejcid6b76452019-09-03 17:03:03 +02001938 return yin_parse_content(ctx, subelems, 2, data, LY_STMT_ARGUMENT, NULL, exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02001939}
1940
1941/**
1942 * @brief Parse the extension statement.
1943 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001944 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001945 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of extension element.
1946 * @param[in,out] data Data to read from.
1947 * @param[in,out] extensions Extensions to add to.
1948 *
1949 * @return LY_ERR values.
1950 */
1951static LY_ERR
1952yin_parse_extension(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, struct lysp_ext **extensions)
1953{
1954 struct lysp_ext *ex;
1955 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *extensions, ex, LY_EMEM);
Radek Krejcid6b76452019-09-03 17:03:03 +02001956 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &ex->name, Y_IDENTIF_ARG, LY_STMT_EXTENSION));
David Sedlákdfbbb442019-08-06 16:33:21 +02001957
1958 struct yin_argument_meta arg_info = {&ex->flags, &ex->argument};
1959 struct yin_subelement subelems[5] = {
Radek Krejcid6b76452019-09-03 17:03:03 +02001960 {LY_STMT_ARGUMENT, &arg_info, YIN_SUBELEM_UNIQUE},
1961 {LY_STMT_DESCRIPTION, &ex->dsc, YIN_SUBELEM_UNIQUE},
1962 {LY_STMT_REFERENCE, &ex->ref, YIN_SUBELEM_UNIQUE},
1963 {LY_STMT_STATUS, &ex->flags, YIN_SUBELEM_UNIQUE},
1964 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
David Sedlákdfbbb442019-08-06 16:33:21 +02001965 };
1966
Radek Krejcid6b76452019-09-03 17:03:03 +02001967 return yin_parse_content(ctx, subelems, 5, data, LY_STMT_EXTENSION, NULL, &ex->exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02001968}
1969
1970/**
David Sedlák5e13dea2019-07-22 16:06:45 +02001971 * @brief Parse feature element.
1972 *
1973 * @param[in,out] ctx YIN parser context for logging and to store current state.
1974 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1975 * @param[in,out] data Data to read from, always moved to currently handled character.
1976 * @param[in,out] features Features to add to.
1977 *
1978 * @return LY_ERR values.
1979 */
1980static LY_ERR
1981yin_parse_feature(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1982 struct lysp_feature **features)
1983{
1984 struct lysp_feature *feat;
1985
1986 /* allocate new feature */
1987 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *features, feat, LY_EMEM);
1988
1989 /* parse argument */
Radek Krejcid6b76452019-09-03 17:03:03 +02001990 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &feat->name, Y_IDENTIF_ARG, LY_STMT_FEATURE));
David Sedlák5e13dea2019-07-22 16:06:45 +02001991
1992 /* parse content */
1993 struct yin_subelement subelems[5] = {
Radek Krejcid6b76452019-09-03 17:03:03 +02001994 {LY_STMT_DESCRIPTION, &feat->dsc, YIN_SUBELEM_UNIQUE},
1995 {LY_STMT_IF_FEATURE, &feat->iffeatures, 0},
1996 {LY_STMT_REFERENCE, &feat->ref, YIN_SUBELEM_UNIQUE},
1997 {LY_STMT_STATUS, &feat->flags, YIN_SUBELEM_UNIQUE},
1998 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
David Sedlák5e13dea2019-07-22 16:06:45 +02001999 };
Radek Krejcid6b76452019-09-03 17:03:03 +02002000 return yin_parse_content(ctx, subelems, 5, data, LY_STMT_FEATURE, NULL, &feat->exts);
David Sedlák5e13dea2019-07-22 16:06:45 +02002001}
2002
2003/**
David Sedlák28794f22019-07-22 16:45:00 +02002004 * @brief Parse identity element.
2005 *
2006 * @param[in,out] ctx YIN parser context for logging and to store current state.
2007 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
2008 * @param[in,out] data Data to read from, always moved to currently handled character.
2009 * @param[in,out] identities Identities to add to.
2010 *
2011 * @return LY_ERR values.
2012 */
2013static LY_ERR
2014yin_parse_identity(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
2015 struct lysp_ident **identities)
2016{
2017 struct lysp_ident *ident;
2018
2019 /* allocate new identity */
2020 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *identities, ident, LY_EMEM);
2021
2022 /* parse argument */
Radek Krejcid6b76452019-09-03 17:03:03 +02002023 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &ident->name, Y_IDENTIF_ARG, LY_STMT_IDENTITY));
David Sedlák28794f22019-07-22 16:45:00 +02002024
2025 /* parse content */
2026 struct yin_subelement subelems[6] = {
Radek Krejcid6b76452019-09-03 17:03:03 +02002027 {LY_STMT_BASE, &ident->bases, 0},
2028 {LY_STMT_DESCRIPTION, &ident->dsc, YIN_SUBELEM_UNIQUE},
2029 {LY_STMT_IF_FEATURE, &ident->iffeatures, YIN_SUBELEM_VER2},
2030 {LY_STMT_REFERENCE, &ident->ref, YIN_SUBELEM_UNIQUE},
2031 {LY_STMT_STATUS, &ident->flags, YIN_SUBELEM_UNIQUE},
2032 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
David Sedlák28794f22019-07-22 16:45:00 +02002033 };
Radek Krejcid6b76452019-09-03 17:03:03 +02002034 return yin_parse_content(ctx, subelems, 6, data, LY_STMT_IDENTITY, NULL, &ident->exts);
David Sedlák28794f22019-07-22 16:45:00 +02002035}
2036
2037/**
David Sedlákaf536aa2019-07-23 13:42:23 +02002038 * @brief Parse list element.
2039 *
2040 * @param[in,out] ctx YIN parser context for logging and to store current state.
2041 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
2042 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlákad83cf92019-08-13 12:53:53 +02002043 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlákaf536aa2019-07-23 13:42:23 +02002044 *
2045 * @return LY_ERR values.
2046 */
2047static LY_ERR
David Sedlákf111bcb2019-07-23 17:15:51 +02002048yin_parse_list(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
2049 struct tree_node_meta *node_meta)
David Sedlákaf536aa2019-07-23 13:42:23 +02002050{
David Sedlákaf536aa2019-07-23 13:42:23 +02002051 struct lysp_node_list *list;
David Sedlák81497a32019-08-13 16:56:26 +02002052 LY_ERR ret = LY_SUCCESS;
2053 struct yin_subelement *subelems = NULL;
David Sedlákaf536aa2019-07-23 13:42:23 +02002054
David Sedlákbf8a2b72019-08-14 16:48:10 +02002055 LY_LIST_NEW_RET(ctx->xml_ctx.ctx, node_meta->nodes, list, next);
David Sedlákaf536aa2019-07-23 13:42:23 +02002056 list->nodetype = LYS_LIST;
2057 list->parent = node_meta->parent;
2058
David Sedlákaf536aa2019-07-23 13:42:23 +02002059 /* parse argument */
Radek Krejcid6b76452019-09-03 17:03:03 +02002060 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &list->name, Y_IDENTIF_ARG, LY_STMT_LIST));
David Sedlákaf536aa2019-07-23 13:42:23 +02002061
2062 /* parse list content */
David Sedlák81497a32019-08-13 16:56:26 +02002063 LY_CHECK_RET(subelems_allocator(ctx, 25, (struct lysp_node *)list, &subelems,
Radek Krejcid6b76452019-09-03 17:03:03 +02002064 LY_STMT_ACTION, &list->actions, 0,
2065 LY_STMT_ANYDATA, &list->child, 0,
2066 LY_STMT_ANYXML, &list->child, 0,
2067 LY_STMT_CHOICE, &list->child, 0,
2068 LY_STMT_CONFIG, &list->flags, YIN_SUBELEM_UNIQUE,
2069 LY_STMT_CONTAINER, &list->child, 0,
2070 LY_STMT_DESCRIPTION, &list->dsc, YIN_SUBELEM_UNIQUE,
2071 LY_STMT_GROUPING, &list->groupings, 0,
2072 LY_STMT_IF_FEATURE, &list->iffeatures, 0,
2073 LY_STMT_KEY, &list->key, YIN_SUBELEM_UNIQUE,
2074 LY_STMT_LEAF, &list->child, 0,
2075 LY_STMT_LEAF_LIST, &list->child, 0,
2076 LY_STMT_LIST, &list->child, 0,
2077 LY_STMT_MAX_ELEMENTS, list, YIN_SUBELEM_UNIQUE,
2078 LY_STMT_MIN_ELEMENTS, list, YIN_SUBELEM_UNIQUE,
2079 LY_STMT_MUST, &list->musts, 0,
2080 LY_STMT_NOTIFICATION, &list->notifs, 0,
2081 LY_STMT_ORDERED_BY, &list->flags, YIN_SUBELEM_UNIQUE,
2082 LY_STMT_REFERENCE, &list->ref, YIN_SUBELEM_UNIQUE,
2083 LY_STMT_STATUS, &list->flags, YIN_SUBELEM_UNIQUE,
2084 LY_STMT_TYPEDEF, &list->typedefs, 0,
2085 LY_STMT_UNIQUE, &list->uniques, 0,
2086 LY_STMT_USES, &list->child, 0,
2087 LY_STMT_WHEN, &list->when, YIN_SUBELEM_UNIQUE,
2088 LY_STMT_EXTENSION_INSTANCE, NULL, 0
David Sedlák81497a32019-08-13 16:56:26 +02002089 ));
Radek Krejcid6b76452019-09-03 17:03:03 +02002090 ret = yin_parse_content(ctx, subelems, 25, data, LY_STMT_LIST, NULL, &list->exts);
David Sedlák81497a32019-08-13 16:56:26 +02002091 subelems_deallocator(25, subelems);
2092 LY_CHECK_RET(ret);
David Sedlákaf536aa2019-07-23 13:42:23 +02002093
2094 /* finalize parent pointers to the reallocated items */
2095 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, list->groupings, NULL, list->actions, list->notifs));
2096
2097 if (list->max && list->min > list->max) {
David Sedlák1538a842019-08-08 15:38:51 +02002098 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_MINMAX, list->min, list->max);
David Sedlákaf536aa2019-07-23 13:42:23 +02002099 return LY_EVALID;
2100 }
2101
2102 return LY_SUCCESS;
2103}
2104
2105/**
David Sedlák031b9e72019-07-23 15:19:37 +02002106 * @brief Parse notification element.
2107 *
2108 * @param[in,out] ctx YIN parser context for logging and to store current state.
2109 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
2110 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlák05404f62019-07-24 14:11:53 +02002111 * @param[in,out] notif_meta Meta information about parent node and notifications to add to.
David Sedlák031b9e72019-07-23 15:19:37 +02002112 *
2113 * @return LY_ERR values.
2114 */
2115static LY_ERR
2116yin_parse_notification(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
David Sedlák6881b512019-08-13 12:52:00 +02002117 struct tree_node_meta *notif_meta)
David Sedlák031b9e72019-07-23 15:19:37 +02002118{
2119 struct lysp_notif *notif;
David Sedlákbf8a2b72019-08-14 16:48:10 +02002120 struct lysp_notif **notifs = (struct lysp_notif **)notif_meta->nodes;
David Sedlák81497a32019-08-13 16:56:26 +02002121 LY_ERR ret = LY_SUCCESS;
2122 struct yin_subelement *subelems = NULL;
David Sedlák031b9e72019-07-23 15:19:37 +02002123
2124 /* allocate new notification */
David Sedlák6881b512019-08-13 12:52:00 +02002125 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *notifs, notif, LY_EMEM);
David Sedlák031b9e72019-07-23 15:19:37 +02002126 notif->nodetype = LYS_NOTIF;
2127 notif->parent = notif_meta->parent;
2128
2129 /* parse argument */
Radek Krejcid6b76452019-09-03 17:03:03 +02002130 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &notif->name, Y_IDENTIF_ARG, LY_STMT_NOTIFICATION));
David Sedlák031b9e72019-07-23 15:19:37 +02002131
2132 /* parse notification content */
David Sedlák81497a32019-08-13 16:56:26 +02002133 LY_CHECK_RET(subelems_allocator(ctx, 16, (struct lysp_node *)notif, &subelems,
Radek Krejcid6b76452019-09-03 17:03:03 +02002134 LY_STMT_ANYDATA, &notif->data, 0,
2135 LY_STMT_ANYXML, &notif->data, 0,
2136 LY_STMT_CHOICE, &notif->data, 0,
2137 LY_STMT_CONTAINER, &notif->data, 0,
2138 LY_STMT_DESCRIPTION, &notif->dsc, YIN_SUBELEM_UNIQUE,
2139 LY_STMT_GROUPING, &notif->groupings, 0,
2140 LY_STMT_IF_FEATURE, &notif->iffeatures, 0,
2141 LY_STMT_LEAF, &notif->data, 0,
2142 LY_STMT_LEAF_LIST, &notif->data, 0,
2143 LY_STMT_LIST, &notif->data, 0,
2144 LY_STMT_MUST, &notif->musts, YIN_SUBELEM_VER2,
2145 LY_STMT_REFERENCE, &notif->ref, YIN_SUBELEM_UNIQUE,
2146 LY_STMT_STATUS, &notif->flags, YIN_SUBELEM_UNIQUE,
2147 LY_STMT_TYPEDEF, &notif->typedefs, 0,
2148 LY_STMT_USES, &notif->data, 0,
2149 LY_STMT_EXTENSION_INSTANCE, NULL, 0
David Sedlák81497a32019-08-13 16:56:26 +02002150 ));
2151
Radek Krejcid6b76452019-09-03 17:03:03 +02002152 ret = yin_parse_content(ctx, subelems, 16, data, LY_STMT_NOTIFICATION, NULL, &notif->exts);
David Sedlák81497a32019-08-13 16:56:26 +02002153 subelems_deallocator(16, subelems);
2154 LY_CHECK_RET(ret);
David Sedlák031b9e72019-07-23 15:19:37 +02002155
2156 /* finalize parent pointers to the reallocated items */
2157 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, notif->groupings, NULL, NULL, NULL));
2158
2159 return LY_SUCCESS;
2160}
2161
2162/**
David Sedlákbf8a2b72019-08-14 16:48:10 +02002163 * @brief Parse grouping element.
David Sedláke3ce9ef2019-07-23 16:34:30 +02002164 *
2165 * @param[in,out] ctx YIN parser context for logging and to store current state.
2166 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
2167 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlákbf8a2b72019-08-14 16:48:10 +02002168 * @param[in,out] gr_meta Meta information about parent node and groupings to add to.
David Sedláke3ce9ef2019-07-23 16:34:30 +02002169 *
2170 * @return LY_ERR values.
2171 */
2172static LY_ERR
2173yin_parse_grouping(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
David Sedlák6881b512019-08-13 12:52:00 +02002174 struct tree_node_meta *gr_meta)
David Sedláke3ce9ef2019-07-23 16:34:30 +02002175{
2176 struct lysp_grp *grp;
David Sedlákbf8a2b72019-08-14 16:48:10 +02002177 struct lysp_grp **grps = (struct lysp_grp **)gr_meta->nodes;
David Sedlák81497a32019-08-13 16:56:26 +02002178 LY_ERR ret = LY_SUCCESS;
2179 struct yin_subelement *subelems = NULL;
David Sedláke3ce9ef2019-07-23 16:34:30 +02002180
2181 /* create new grouping */
David Sedlák6881b512019-08-13 12:52:00 +02002182 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *grps, grp, LY_EMEM);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002183 grp->nodetype = LYS_GROUPING;
2184 grp->parent = gr_meta->parent;
2185
2186 /* parse argument */
Radek Krejcid6b76452019-09-03 17:03:03 +02002187 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &grp->name, Y_IDENTIF_ARG, LY_STMT_GROUPING));
David Sedláke3ce9ef2019-07-23 16:34:30 +02002188
2189 /* parse grouping content */
David Sedlák81497a32019-08-13 16:56:26 +02002190 LY_CHECK_RET(subelems_allocator(ctx, 16, (struct lysp_node *)grp, &subelems,
Radek Krejcid6b76452019-09-03 17:03:03 +02002191 LY_STMT_ACTION, &grp->actions, 0,
2192 LY_STMT_ANYDATA, &grp->data, 0,
2193 LY_STMT_ANYXML, &grp->data, 0,
2194 LY_STMT_CHOICE, &grp->data, 0,
2195 LY_STMT_CONTAINER, &grp->data, 0,
2196 LY_STMT_DESCRIPTION, &grp->dsc, YIN_SUBELEM_UNIQUE,
2197 LY_STMT_GROUPING, &grp->groupings, 0,
2198 LY_STMT_LEAF, &grp->data, 0,
2199 LY_STMT_LEAF_LIST, &grp->data, 0,
2200 LY_STMT_LIST, &grp->data, 0,
2201 LY_STMT_NOTIFICATION, &grp->notifs, 0,
2202 LY_STMT_REFERENCE, &grp->ref, YIN_SUBELEM_UNIQUE,
2203 LY_STMT_STATUS, &grp->flags, YIN_SUBELEM_UNIQUE,
2204 LY_STMT_TYPEDEF, &grp->typedefs, 0,
2205 LY_STMT_USES, &grp->data, 0,
2206 LY_STMT_EXTENSION_INSTANCE, NULL, 0
David Sedlák81497a32019-08-13 16:56:26 +02002207 ));
Radek Krejcid6b76452019-09-03 17:03:03 +02002208 ret = yin_parse_content(ctx, subelems, 16, data, LY_STMT_GROUPING, NULL, &grp->exts);
David Sedlák81497a32019-08-13 16:56:26 +02002209 subelems_deallocator(16, subelems);
2210 LY_CHECK_RET(ret);
2211
David Sedláke3ce9ef2019-07-23 16:34:30 +02002212 /* finalize parent pointers to the reallocated items */
2213 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, grp->groupings, NULL, grp->actions, grp->notifs));
2214
2215 return LY_SUCCESS;
2216}
2217
2218/**
David Sedlákbf8a2b72019-08-14 16:48:10 +02002219 * @brief Parse container element.
David Sedlákf111bcb2019-07-23 17:15:51 +02002220 *
2221 * @param[in,out] ctx YIN parser context for logging and to store current state.
2222 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
2223 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlákad83cf92019-08-13 12:53:53 +02002224 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlákf111bcb2019-07-23 17:15:51 +02002225 *
2226 * @return LY_ERR values.
2227 */
2228static LY_ERR
2229yin_parse_container(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
2230 struct tree_node_meta *node_meta)
2231{
David Sedlákf111bcb2019-07-23 17:15:51 +02002232 struct lysp_node_container *cont;
David Sedlák81497a32019-08-13 16:56:26 +02002233 LY_ERR ret = LY_SUCCESS;
2234 struct yin_subelement *subelems = NULL;
David Sedlákf111bcb2019-07-23 17:15:51 +02002235
2236 /* create new container */
David Sedlákbf8a2b72019-08-14 16:48:10 +02002237 LY_LIST_NEW_RET(ctx->xml_ctx.ctx, node_meta->nodes, cont, next);
David Sedlákf111bcb2019-07-23 17:15:51 +02002238 cont->nodetype = LYS_CONTAINER;
2239 cont->parent = node_meta->parent;
2240
David Sedlákf111bcb2019-07-23 17:15:51 +02002241 /* parse aegument */
Radek Krejcid6b76452019-09-03 17:03:03 +02002242 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &cont->name, Y_IDENTIF_ARG, LY_STMT_CONTAINER));
David Sedlákf111bcb2019-07-23 17:15:51 +02002243
2244 /* parse container content */
David Sedlák81497a32019-08-13 16:56:26 +02002245 LY_CHECK_RET(subelems_allocator(ctx, 21, (struct lysp_node *)cont, &subelems,
Radek Krejcid6b76452019-09-03 17:03:03 +02002246 LY_STMT_ACTION, &cont->actions, YIN_SUBELEM_VER2,
2247 LY_STMT_ANYDATA, &cont->child, YIN_SUBELEM_VER2,
2248 LY_STMT_ANYXML, &cont->child, 0,
2249 LY_STMT_CHOICE, &cont->child, 0,
2250 LY_STMT_CONFIG, &cont->flags, YIN_SUBELEM_UNIQUE,
2251 LY_STMT_CONTAINER, &cont->child, 0,
2252 LY_STMT_DESCRIPTION, &cont->dsc, YIN_SUBELEM_UNIQUE,
2253 LY_STMT_GROUPING, &cont->groupings, 0,
2254 LY_STMT_IF_FEATURE, &cont->iffeatures, 0,
2255 LY_STMT_LEAF, &cont->child, 0,
2256 LY_STMT_LEAF_LIST, &cont->child, 0,
2257 LY_STMT_LIST, &cont->child, 0,
2258 LY_STMT_MUST, &cont->musts, 0,
2259 LY_STMT_NOTIFICATION, &cont->notifs, YIN_SUBELEM_VER2,
2260 LY_STMT_PRESENCE, &cont->presence, YIN_SUBELEM_UNIQUE,
2261 LY_STMT_REFERENCE, &cont->ref, YIN_SUBELEM_UNIQUE,
2262 LY_STMT_STATUS, &cont->flags, YIN_SUBELEM_UNIQUE,
2263 LY_STMT_TYPEDEF, &cont->typedefs, 0,
2264 LY_STMT_USES, &cont->child, 0,
2265 LY_STMT_WHEN, &cont->when, YIN_SUBELEM_UNIQUE,
2266 LY_STMT_EXTENSION_INSTANCE, NULL, 0
David Sedlák81497a32019-08-13 16:56:26 +02002267 ));
Radek Krejcid6b76452019-09-03 17:03:03 +02002268 ret = yin_parse_content(ctx, subelems, 21, data, LY_STMT_CONTAINER, NULL, &cont->exts);
David Sedlák81497a32019-08-13 16:56:26 +02002269 subelems_deallocator(21, subelems);
2270 LY_CHECK_RET(ret);
2271
David Sedlákf111bcb2019-07-23 17:15:51 +02002272 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, cont->groupings, NULL, cont->actions, cont->notifs));
2273
2274 return LY_SUCCESS;
2275}
2276
2277/**
David Sedlák5379d392019-07-24 10:42:03 +02002278 * @brief Parse case element.
2279 *
2280 * @param[in,out] ctx YIN parser context for logging and to store current state.
2281 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
2282 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlákad83cf92019-08-13 12:53:53 +02002283 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlák5379d392019-07-24 10:42:03 +02002284 *
2285 * @return LY_ERR values.
2286 */
2287static LY_ERR
2288yin_parse_case(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
2289 struct tree_node_meta *node_meta)
2290{
David Sedlák5379d392019-07-24 10:42:03 +02002291 struct lysp_node_case *cas;
David Sedlák81497a32019-08-13 16:56:26 +02002292 LY_ERR ret = LY_SUCCESS;
2293 struct yin_subelement *subelems = NULL;;
David Sedlák5379d392019-07-24 10:42:03 +02002294
2295 /* create new case */
David Sedlákbf8a2b72019-08-14 16:48:10 +02002296 LY_LIST_NEW_RET(ctx->xml_ctx.ctx, node_meta->nodes, cas, next);
David Sedlák5379d392019-07-24 10:42:03 +02002297 cas->nodetype = LYS_CASE;
2298 cas->parent = node_meta->parent;
2299
David Sedlák5379d392019-07-24 10:42:03 +02002300 /* parse argument */
Radek Krejcid6b76452019-09-03 17:03:03 +02002301 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &cas->name, Y_IDENTIF_ARG, LY_STMT_CASE));
David Sedlák5379d392019-07-24 10:42:03 +02002302
2303 /* parse case content */
David Sedlák81497a32019-08-13 16:56:26 +02002304 LY_CHECK_RET(subelems_allocator(ctx, 14, (struct lysp_node *)cas, &subelems,
Radek Krejcid6b76452019-09-03 17:03:03 +02002305 LY_STMT_ANYDATA, &cas->child, YIN_SUBELEM_VER2,
2306 LY_STMT_ANYXML, &cas->child, 0,
2307 LY_STMT_CHOICE, &cas->child, 0,
2308 LY_STMT_CONTAINER, &cas->child, 0,
2309 LY_STMT_DESCRIPTION, &cas->dsc, YIN_SUBELEM_UNIQUE,
2310 LY_STMT_IF_FEATURE, &cas->iffeatures, 0,
2311 LY_STMT_LEAF, &cas->child, 0,
2312 LY_STMT_LEAF_LIST, &cas->child, 0,
2313 LY_STMT_LIST, &cas->child, 0,
2314 LY_STMT_REFERENCE, &cas->ref, YIN_SUBELEM_UNIQUE,
2315 LY_STMT_STATUS, &cas->flags, YIN_SUBELEM_UNIQUE,
2316 LY_STMT_USES, &cas->child, 0,
2317 LY_STMT_WHEN, &cas->when, YIN_SUBELEM_UNIQUE,
2318 LY_STMT_EXTENSION_INSTANCE, NULL, 0
David Sedlák81497a32019-08-13 16:56:26 +02002319 ));
Radek Krejcid6b76452019-09-03 17:03:03 +02002320 ret = yin_parse_content(ctx, subelems, 14, data, LY_STMT_CASE, NULL, &cas->exts);
David Sedlák81497a32019-08-13 16:56:26 +02002321 subelems_deallocator(14, subelems);
2322
2323 return ret;
David Sedlák5379d392019-07-24 10:42:03 +02002324}
2325
2326/**
David Sedlák05404f62019-07-24 14:11:53 +02002327 * @brief Parse choice element.
David Sedlákb7abcfa2019-07-24 12:33:35 +02002328 *
2329 * @param[in,out] ctx YIN parser context for logging and to store current state.
2330 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
2331 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlákad83cf92019-08-13 12:53:53 +02002332 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlákb7abcfa2019-07-24 12:33:35 +02002333 *
2334 * @return LY_ERR values.
2335 */
2336LY_ERR
2337yin_parse_choice(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
2338 struct tree_node_meta *node_meta)
2339{
David Sedlák81497a32019-08-13 16:56:26 +02002340 LY_ERR ret = LY_SUCCESS;
2341 struct yin_subelement *subelems = NULL;
David Sedlákb7abcfa2019-07-24 12:33:35 +02002342 struct lysp_node_choice *choice;
2343
2344 /* create new choice */
David Sedlákbf8a2b72019-08-14 16:48:10 +02002345 LY_LIST_NEW_RET(ctx->xml_ctx.ctx, node_meta->nodes, choice, next);
David Sedlák8d552d62019-08-06 15:29:05 +02002346
David Sedlákb7abcfa2019-07-24 12:33:35 +02002347 choice->nodetype = LYS_CHOICE;
2348 choice->parent = node_meta->parent;
2349
David Sedlákb7abcfa2019-07-24 12:33:35 +02002350 /* parse argument */
Radek Krejcid6b76452019-09-03 17:03:03 +02002351 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &choice->name, Y_IDENTIF_ARG, LY_STMT_CHOICE));
David Sedlákb7abcfa2019-07-24 12:33:35 +02002352
2353 /* parse choice content */
David Sedlák81497a32019-08-13 16:56:26 +02002354 LY_CHECK_RET(subelems_allocator(ctx, 17, (struct lysp_node *)choice, &subelems,
Radek Krejcid6b76452019-09-03 17:03:03 +02002355 LY_STMT_ANYDATA, &choice->child, YIN_SUBELEM_VER2,
2356 LY_STMT_ANYXML, &choice->child, 0,
2357 LY_STMT_CASE, &choice->child, 0,
2358 LY_STMT_CHOICE, &choice->child, YIN_SUBELEM_VER2,
2359 LY_STMT_CONFIG, &choice->flags, YIN_SUBELEM_UNIQUE,
2360 LY_STMT_CONTAINER, &choice->child, 0,
2361 LY_STMT_DEFAULT, &choice->dflt, YIN_SUBELEM_UNIQUE,
2362 LY_STMT_DESCRIPTION, &choice->dsc, YIN_SUBELEM_UNIQUE,
2363 LY_STMT_IF_FEATURE, &choice->iffeatures, 0,
2364 LY_STMT_LEAF, &choice->child, 0,
2365 LY_STMT_LEAF_LIST, &choice->child, 0,
2366 LY_STMT_LIST, &choice->child, 0,
2367 LY_STMT_MANDATORY, &choice->flags, YIN_SUBELEM_UNIQUE,
2368 LY_STMT_REFERENCE, &choice->ref, YIN_SUBELEM_UNIQUE,
2369 LY_STMT_STATUS, &choice->flags, YIN_SUBELEM_UNIQUE,
2370 LY_STMT_WHEN, &choice->when, YIN_SUBELEM_UNIQUE,
2371 LY_STMT_EXTENSION_INSTANCE, NULL, 0
David Sedlák81497a32019-08-13 16:56:26 +02002372 ));
Radek Krejcid6b76452019-09-03 17:03:03 +02002373 ret = yin_parse_content(ctx, subelems, 17, data, LY_STMT_CHOICE, NULL, &choice->exts);
David Sedlák81497a32019-08-13 16:56:26 +02002374 subelems_deallocator(17, subelems);
2375 return ret;
David Sedlákb7abcfa2019-07-24 12:33:35 +02002376}
2377
2378/**
David Sedlák05404f62019-07-24 14:11:53 +02002379 * @brief Parse input or output element.
2380 *
2381 * @param[in,out] ctx YIN parser context for logging and to store current state.
2382 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
2383 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlákbf8a2b72019-08-14 16:48:10 +02002384 * @param[in] inout_kw Identification of input/output element.
David Sedlák05404f62019-07-24 14:11:53 +02002385 * @param[in] inout_meta Meta information about parent node and siblings and input/output pointer to write to.
2386 *
2387 * @return LY_ERR values.
2388 */
2389static LY_ERR
Radek Krejcid6b76452019-09-03 17:03:03 +02002390yin_parse_inout(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, enum ly_stmt inout_kw,
David Sedlák05404f62019-07-24 14:11:53 +02002391 struct inout_meta *inout_meta)
2392{
David Sedlák81497a32019-08-13 16:56:26 +02002393 LY_ERR ret = LY_SUCCESS;
2394 struct yin_subelement *subelems = NULL;
2395
David Sedlák05404f62019-07-24 14:11:53 +02002396 /* initiate structure */
Radek Krejcid6b76452019-09-03 17:03:03 +02002397 inout_meta->inout_p->nodetype = (inout_kw == LY_STMT_INPUT) ? LYS_INPUT : LYS_OUTPUT;
David Sedlák05404f62019-07-24 14:11:53 +02002398 inout_meta->inout_p->parent = inout_meta->parent;
2399
2400 /* check attributes */
2401 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NONE, NULL, Y_MAYBE_STR_ARG, inout_kw));
2402
2403 /* parser input/output content */
David Sedlák81497a32019-08-13 16:56:26 +02002404 LY_CHECK_RET(subelems_allocator(ctx, 12, (struct lysp_node *)inout_meta->inout_p, &subelems,
Radek Krejcid6b76452019-09-03 17:03:03 +02002405 LY_STMT_ANYDATA, &inout_meta->inout_p->data, YIN_SUBELEM_VER2,
2406 LY_STMT_ANYXML, &inout_meta->inout_p->data, 0,
2407 LY_STMT_CHOICE, &inout_meta->inout_p->data, 0,
2408 LY_STMT_CONTAINER, &inout_meta->inout_p->data, 0,
2409 LY_STMT_GROUPING, &inout_meta->inout_p->groupings, 0,
2410 LY_STMT_LEAF, &inout_meta->inout_p->data, 0,
2411 LY_STMT_LEAF_LIST, &inout_meta->inout_p->data, 0,
2412 LY_STMT_LIST, &inout_meta->inout_p->data, 0,
2413 LY_STMT_MUST, &inout_meta->inout_p->musts, YIN_SUBELEM_VER2,
2414 LY_STMT_TYPEDEF, &inout_meta->inout_p->typedefs, 0,
2415 LY_STMT_USES, &inout_meta->inout_p->data, 0,
2416 LY_STMT_EXTENSION_INSTANCE, NULL, 0
David Sedlák81497a32019-08-13 16:56:26 +02002417 ));
2418 ret = yin_parse_content(ctx, subelems, 12, data, inout_kw, NULL, &inout_meta->inout_p->exts);
2419 subelems_deallocator(12, subelems);
2420 LY_CHECK_RET(ret);
David Sedlák05404f62019-07-24 14:11:53 +02002421
2422 /* finalize parent pointers to the reallocated items */
David Sedlák81497a32019-08-13 16:56:26 +02002423 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 +02002424
2425 return LY_SUCCESS;
2426}
2427
David Sedlák992fb7c2019-07-24 16:51:01 +02002428/**
2429 * @brief Parse action element.
2430 *
2431 * @param[in,out] ctx YIN parser context for logging and to store current state.
2432 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
2433 * @param[in,out] data Data to read from, always moved to currently handled character.
2434 * @param[in] act_meta Meta information about parent node and actions to add to.
2435 *
2436 * @return LY_ERR values.
2437 */
David Sedlák85d0eca2019-07-24 15:15:21 +02002438static LY_ERR
2439yin_parse_action(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
David Sedlák6881b512019-08-13 12:52:00 +02002440 struct tree_node_meta *act_meta)
David Sedlák85d0eca2019-07-24 15:15:21 +02002441{
David Sedlákbf8a2b72019-08-14 16:48:10 +02002442 struct lysp_action *act, **acts = (struct lysp_action **)act_meta->nodes;
David Sedlák81497a32019-08-13 16:56:26 +02002443 LY_ERR ret = LY_SUCCESS;
2444 struct yin_subelement *subelems = NULL;
David Sedlák85d0eca2019-07-24 15:15:21 +02002445
2446 /* create new action */
David Sedlák6881b512019-08-13 12:52:00 +02002447 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *acts, act, LY_EMEM);
David Sedlák85d0eca2019-07-24 15:15:21 +02002448 act->nodetype = LYS_ACTION;
2449 act->parent = act_meta->parent;
2450
2451 /* parse argument */
Radek Krejcid6b76452019-09-03 17:03:03 +02002452 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &act->name, Y_IDENTIF_ARG, LY_STMT_ACTION));
David Sedlák85d0eca2019-07-24 15:15:21 +02002453
2454 /* parse content */
David Sedlák81497a32019-08-13 16:56:26 +02002455 LY_CHECK_RET(subelems_allocator(ctx, 9, (struct lysp_node *)act, &subelems,
Radek Krejcid6b76452019-09-03 17:03:03 +02002456 LY_STMT_DESCRIPTION, &act->dsc, YIN_SUBELEM_UNIQUE,
2457 LY_STMT_GROUPING, &act->groupings, 0,
2458 LY_STMT_IF_FEATURE, &act->iffeatures, 0,
2459 LY_STMT_INPUT, &act->input, YIN_SUBELEM_UNIQUE,
2460 LY_STMT_OUTPUT, &act->output, YIN_SUBELEM_UNIQUE,
2461 LY_STMT_REFERENCE, &act->ref, YIN_SUBELEM_UNIQUE,
2462 LY_STMT_STATUS, &act->flags, YIN_SUBELEM_UNIQUE,
2463 LY_STMT_TYPEDEF, &act->typedefs, 0,
2464 LY_STMT_EXTENSION_INSTANCE, NULL, 0
David Sedlák81497a32019-08-13 16:56:26 +02002465 ));
Radek Krejcid6b76452019-09-03 17:03:03 +02002466 ret = (yin_parse_content(ctx, subelems, 9, data, LY_STMT_ACTION, NULL, &act->exts));
David Sedlák81497a32019-08-13 16:56:26 +02002467 subelems_deallocator(9, subelems);
2468 LY_CHECK_RET(ret);
2469
David Sedlák85d0eca2019-07-24 15:15:21 +02002470 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, act->groupings, NULL, NULL, NULL));
2471
2472 return LY_SUCCESS;
2473}
2474
David Sedlák05404f62019-07-24 14:11:53 +02002475/**
David Sedlák992fb7c2019-07-24 16:51:01 +02002476 * @brief Parse augment element.
2477 *
2478 * @param[in,out] ctx YIN parser context for logging and to store current state.
2479 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
2480 * @param[in,out] data Data to read from, always moved to currently handled character.
2481 * @param[in] aug_meta Meta information about parent node and augments to add to.
2482 *
2483 * @return LY_ERR values.
2484 */
2485static LY_ERR
2486yin_parse_augment(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
David Sedlák6881b512019-08-13 12:52:00 +02002487 struct tree_node_meta *aug_meta)
David Sedlák992fb7c2019-07-24 16:51:01 +02002488{
2489 struct lysp_augment *aug;
David Sedlákbf8a2b72019-08-14 16:48:10 +02002490 struct lysp_augment **augs = (struct lysp_augment **)aug_meta->nodes;
David Sedlák81497a32019-08-13 16:56:26 +02002491 LY_ERR ret = LY_SUCCESS;
2492 struct yin_subelement *subelems = NULL;
David Sedlák992fb7c2019-07-24 16:51:01 +02002493
2494 /* create new augment */
David Sedlák6881b512019-08-13 12:52:00 +02002495 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *augs, aug, LY_EMEM);
David Sedlák992fb7c2019-07-24 16:51:01 +02002496 aug->nodetype = LYS_AUGMENT;
2497 aug->parent = aug_meta->parent;
2498
2499 /* parse argument */
Radek Krejcid6b76452019-09-03 17:03:03 +02002500 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_TARGET_NODE, &aug->nodeid, Y_STR_ARG, LY_STMT_AUGMENT));
David Sedlák992fb7c2019-07-24 16:51:01 +02002501 YANG_CHECK_NONEMPTY((struct lys_parser_ctx *)ctx, strlen(aug->nodeid), "augment");
2502
2503 /* parser augment content */
David Sedlák81497a32019-08-13 16:56:26 +02002504 LY_CHECK_RET(subelems_allocator(ctx, 17, (struct lysp_node *)aug, &subelems,
Radek Krejcid6b76452019-09-03 17:03:03 +02002505 LY_STMT_ACTION, &aug->actions, YIN_SUBELEM_VER2,
2506 LY_STMT_ANYDATA, &aug->child, YIN_SUBELEM_VER2,
2507 LY_STMT_ANYXML, &aug->child, 0,
2508 LY_STMT_CASE, &aug->child, 0,
2509 LY_STMT_CHOICE, &aug->child, 0,
2510 LY_STMT_CONTAINER, &aug->child, 0,
2511 LY_STMT_DESCRIPTION, &aug->dsc, YIN_SUBELEM_UNIQUE,
2512 LY_STMT_IF_FEATURE, &aug->iffeatures, 0,
2513 LY_STMT_LEAF, &aug->child, 0,
2514 LY_STMT_LEAF_LIST, &aug->child, 0,
2515 LY_STMT_LIST, &aug->child, 0,
2516 LY_STMT_NOTIFICATION, &aug->notifs, YIN_SUBELEM_VER2,
2517 LY_STMT_REFERENCE, &aug->ref, YIN_SUBELEM_UNIQUE,
2518 LY_STMT_STATUS, &aug->flags, YIN_SUBELEM_UNIQUE,
2519 LY_STMT_USES, &aug->child, 0,
2520 LY_STMT_WHEN, &aug->when, YIN_SUBELEM_UNIQUE,
2521 LY_STMT_EXTENSION_INSTANCE, NULL, 0
David Sedlák81497a32019-08-13 16:56:26 +02002522 ));
Radek Krejcid6b76452019-09-03 17:03:03 +02002523 ret = yin_parse_content(ctx, subelems, 17, data, LY_STMT_AUGMENT, NULL, &aug->exts);
David Sedlák81497a32019-08-13 16:56:26 +02002524 subelems_deallocator(17, subelems);
2525 LY_CHECK_RET(ret);
David Sedlák992fb7c2019-07-24 16:51:01 +02002526
2527 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, NULL, NULL, aug->actions, aug->notifs));
2528
2529 return LY_SUCCESS;
2530}
2531
David Sedlák8b754462019-07-25 16:22:13 +02002532/**
2533 * @brief Parse deviate element.
2534 *
2535 * @param[in,out] ctx YIN parser context for logging and to store current state.
2536 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
2537 * @param[in,out] data Data to read from, always moved to currently handled character.
2538 * @param[in] deviates Deviates to add to.
2539 *
2540 * @return LY_ERR values.
2541 */
David Sedlák4ffcec82019-07-25 15:10:21 +02002542static LY_ERR
2543yin_parse_deviate(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
2544 struct lysp_deviate **deviates)
2545{
2546 LY_ERR ret = LY_SUCCESS;
2547 uint8_t dev_mod;
2548 const char *temp_val;
David Sedlák8d552d62019-08-06 15:29:05 +02002549 struct lysp_deviate *d;
David Sedlák4ffcec82019-07-25 15:10:21 +02002550 struct lysp_deviate_add *d_add = NULL;
2551 struct lysp_deviate_rpl *d_rpl = NULL;
2552 struct lysp_deviate_del *d_del = NULL;
2553
2554 /* parse argument */
Radek Krejcid6b76452019-09-03 17:03:03 +02002555 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_DEVIATE));
David Sedlák4ffcec82019-07-25 15:10:21 +02002556
2557 if (strcmp(temp_val, "not-supported") == 0) {
2558 dev_mod = LYS_DEV_NOT_SUPPORTED;
2559 } else if (strcmp(temp_val, "add") == 0) {
2560 dev_mod = LYS_DEV_ADD;
2561 } else if (strcmp(temp_val, "replace") == 0) {
2562 dev_mod = LYS_DEV_REPLACE;
2563 } else if (strcmp(temp_val, "delete") == 0) {
2564 dev_mod = LYS_DEV_DELETE;
2565 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02002566 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS4, temp_val, "value", "deviate",
2567 "not-supported", "add", "replace", "delete");
David Sedlák4ffcec82019-07-25 15:10:21 +02002568 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
2569 return LY_EVALID;
2570 }
2571 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
2572
2573 if (dev_mod == LYS_DEV_NOT_SUPPORTED) {
2574 d = calloc(1, sizeof *d);
2575 LY_CHECK_ERR_RET(!d, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
2576 struct yin_subelement subelems[1] = {
Radek Krejcid6b76452019-09-03 17:03:03 +02002577 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
David Sedlák4ffcec82019-07-25 15:10:21 +02002578 };
Radek Krejcid6b76452019-09-03 17:03:03 +02002579 ret = yin_parse_content(ctx, subelems, 1, data, LY_STMT_DEVIATE, NULL, &d->exts);
David Sedlák4ffcec82019-07-25 15:10:21 +02002580
2581 } else if (dev_mod == LYS_DEV_ADD) {
2582 d_add = calloc(1, sizeof *d_add);
2583 LY_CHECK_ERR_RET(!d_add, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
2584 d = (struct lysp_deviate *)d_add;
2585 struct minmax_dev_meta min = {&d_add->min, &d_add->flags, &d_add->exts};
2586 struct minmax_dev_meta max = {&d_add->max, &d_add->flags, &d_add->exts};
2587 struct yin_subelement subelems[9] = {
Radek Krejcid6b76452019-09-03 17:03:03 +02002588 {LY_STMT_CONFIG, &d_add->flags, YIN_SUBELEM_UNIQUE},
2589 {LY_STMT_DEFAULT, &d_add->dflts, 0},
2590 {LY_STMT_MANDATORY, &d_add->flags, YIN_SUBELEM_UNIQUE},
2591 {LY_STMT_MAX_ELEMENTS, &max, YIN_SUBELEM_UNIQUE},
2592 {LY_STMT_MIN_ELEMENTS, &min, YIN_SUBELEM_UNIQUE},
2593 {LY_STMT_MUST, &d_add->musts, 0},
2594 {LY_STMT_UNIQUE, &d_add->uniques, 0},
2595 {LY_STMT_UNITS, &d_add->units, YIN_SUBELEM_UNIQUE},
2596 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
David Sedlák4ffcec82019-07-25 15:10:21 +02002597 };
Radek Krejcid6b76452019-09-03 17:03:03 +02002598 ret = yin_parse_content(ctx, subelems, 9, data, LY_STMT_DEVIATE, NULL, &d_add->exts);
David Sedlák4ffcec82019-07-25 15:10:21 +02002599
2600 } else if (dev_mod == LYS_DEV_REPLACE) {
2601 d_rpl = calloc(1, sizeof *d_rpl);
2602 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
2603 d = (struct lysp_deviate *)d_rpl;
2604 struct minmax_dev_meta min = {&d_rpl->min, &d_rpl->flags, &d_rpl->exts};
2605 struct minmax_dev_meta max = {&d_rpl->max, &d_rpl->flags, &d_rpl->exts};
2606 struct yin_subelement subelems[8] = {
Radek Krejcid6b76452019-09-03 17:03:03 +02002607 {LY_STMT_CONFIG, &d_rpl->flags, YIN_SUBELEM_UNIQUE},
2608 {LY_STMT_DEFAULT, &d_rpl->dflt, YIN_SUBELEM_UNIQUE},
2609 {LY_STMT_MANDATORY, &d_rpl->flags, YIN_SUBELEM_UNIQUE},
2610 {LY_STMT_MAX_ELEMENTS, &max, YIN_SUBELEM_UNIQUE},
2611 {LY_STMT_MIN_ELEMENTS, &min, YIN_SUBELEM_UNIQUE},
2612 {LY_STMT_TYPE, &d_rpl->type, YIN_SUBELEM_UNIQUE},
2613 {LY_STMT_UNITS, &d_rpl->units, YIN_SUBELEM_UNIQUE},
2614 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
David Sedlák4ffcec82019-07-25 15:10:21 +02002615 };
Radek Krejcid6b76452019-09-03 17:03:03 +02002616 ret = yin_parse_content(ctx, subelems, 8, data, LY_STMT_DEVIATE, NULL, &d_rpl->exts);
David Sedlák4ffcec82019-07-25 15:10:21 +02002617
2618 } else {
2619 d_del = calloc(1, sizeof *d_del);
2620 LY_CHECK_ERR_RET(!d_del, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
2621 d = (struct lysp_deviate *)d_del;
2622 struct yin_subelement subelems[5] = {
Radek Krejcid6b76452019-09-03 17:03:03 +02002623 {LY_STMT_DEFAULT, &d_del->dflts, 0},
2624 {LY_STMT_MUST, &d_del->musts, 0},
2625 {LY_STMT_UNIQUE, &d_del->uniques, 0},
2626 {LY_STMT_UNITS, &d_del->units, YIN_SUBELEM_UNIQUE},
2627 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
David Sedlák4ffcec82019-07-25 15:10:21 +02002628 };
Radek Krejcid6b76452019-09-03 17:03:03 +02002629 ret = yin_parse_content(ctx, subelems, 5, data, LY_STMT_DEVIATE, NULL, &d_del->exts);
David Sedlák4ffcec82019-07-25 15:10:21 +02002630 }
2631 LY_CHECK_GOTO(ret, cleanup);
2632
2633 d->mod = dev_mod;
2634 /* insert into siblings */
David Sedlák8d552d62019-08-06 15:29:05 +02002635 LY_LIST_INSERT(deviates, d, next);
David Sedlák4ffcec82019-07-25 15:10:21 +02002636
2637 return ret;
2638
2639cleanup:
2640 free(d);
David Sedlák4ffcec82019-07-25 15:10:21 +02002641 return ret;
2642}
2643
David Sedlák992fb7c2019-07-24 16:51:01 +02002644/**
David Sedlák8b754462019-07-25 16:22:13 +02002645 * @brief Parse deviation element.
2646 *
2647 * @param[in,out] ctx YIN parser context for logging and to store current state.
2648 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
2649 * @param[in,out] data Data to read from, always moved to currently handled character.
2650 * @param[in] deviations Deviations to add to.
2651 *
2652 * @return LY_ERR values.
2653 */
2654static LY_ERR
2655yin_parse_deviation(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
2656 struct lysp_deviation **deviations)
2657{
2658 struct lysp_deviation *dev;
2659
2660 /* create new deviation */
2661 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *deviations, dev, LY_EMEM);
2662
2663 /* parse argument */
Radek Krejcid6b76452019-09-03 17:03:03 +02002664 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_TARGET_NODE, &dev->nodeid, Y_STR_ARG, LY_STMT_DEVIATION));
David Sedlák8b754462019-07-25 16:22:13 +02002665 YANG_CHECK_NONEMPTY((struct lys_parser_ctx *)ctx, strlen(dev->nodeid), "deviation");
2666 struct yin_subelement subelems[4] = {
Radek Krejcid6b76452019-09-03 17:03:03 +02002667 {LY_STMT_DESCRIPTION, &dev->dsc, YIN_SUBELEM_UNIQUE},
2668 {LY_STMT_DEVIATE, &dev->deviates, YIN_SUBELEM_MANDATORY},
2669 {LY_STMT_REFERENCE, &dev->ref, YIN_SUBELEM_UNIQUE},
2670 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
David Sedlák8b754462019-07-25 16:22:13 +02002671 };
Radek Krejcid6b76452019-09-03 17:03:03 +02002672 return yin_parse_content(ctx, subelems, 4, data, LY_STMT_DEVIATION, NULL, &dev->exts);
David Sedlák8b754462019-07-25 16:22:13 +02002673}
2674
2675/**
David Sedlákb4e44562019-07-04 15:42:12 +02002676 * @brief Map keyword type to substatement info.
2677 *
2678 * @param[in] kw Keyword type.
2679 *
2680 * @return correct LYEXT_SUBSTMT information.
2681 */
2682static LYEXT_SUBSTMT
Radek Krejcid6b76452019-09-03 17:03:03 +02002683kw2lyext_substmt(enum ly_stmt kw)
David Sedlákb4e44562019-07-04 15:42:12 +02002684{
2685 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002686 case LY_STMT_ARGUMENT:
David Sedlákb4e44562019-07-04 15:42:12 +02002687 return LYEXT_SUBSTMT_ARGUMENT;
Radek Krejcid6b76452019-09-03 17:03:03 +02002688 case LY_STMT_BASE:
David Sedlákb4e44562019-07-04 15:42:12 +02002689 return LYEXT_SUBSTMT_BASE;
Radek Krejcid6b76452019-09-03 17:03:03 +02002690 case LY_STMT_BELONGS_TO:
David Sedlákb4e44562019-07-04 15:42:12 +02002691 return LYEXT_SUBSTMT_BELONGSTO;
Radek Krejcid6b76452019-09-03 17:03:03 +02002692 case LY_STMT_CONTACT:
David Sedlákb4e44562019-07-04 15:42:12 +02002693 return LYEXT_SUBSTMT_CONTACT;
Radek Krejcid6b76452019-09-03 17:03:03 +02002694 case LY_STMT_DEFAULT:
David Sedlákb4e44562019-07-04 15:42:12 +02002695 return LYEXT_SUBSTMT_DEFAULT;
Radek Krejcid6b76452019-09-03 17:03:03 +02002696 case LY_STMT_DESCRIPTION:
David Sedlákb4e44562019-07-04 15:42:12 +02002697 return LYEXT_SUBSTMT_DESCRIPTION;
Radek Krejcid6b76452019-09-03 17:03:03 +02002698 case LY_STMT_ERROR_APP_TAG:
David Sedlákb4e44562019-07-04 15:42:12 +02002699 return LYEXT_SUBSTMT_ERRTAG;
Radek Krejcid6b76452019-09-03 17:03:03 +02002700 case LY_STMT_ERROR_MESSAGE:
David Sedlákb4e44562019-07-04 15:42:12 +02002701 return LYEXT_SUBSTMT_ERRMSG;
Radek Krejcid6b76452019-09-03 17:03:03 +02002702 case LY_STMT_KEY:
David Sedlákb4e44562019-07-04 15:42:12 +02002703 return LYEXT_SUBSTMT_KEY;
Radek Krejcid6b76452019-09-03 17:03:03 +02002704 case LY_STMT_NAMESPACE:
David Sedlákb4e44562019-07-04 15:42:12 +02002705 return LYEXT_SUBSTMT_NAMESPACE;
Radek Krejcid6b76452019-09-03 17:03:03 +02002706 case LY_STMT_ORGANIZATION:
David Sedlákb4e44562019-07-04 15:42:12 +02002707 return LYEXT_SUBSTMT_ORGANIZATION;
Radek Krejcid6b76452019-09-03 17:03:03 +02002708 case LY_STMT_PATH:
David Sedlákb4e44562019-07-04 15:42:12 +02002709 return LYEXT_SUBSTMT_PATH;
Radek Krejcid6b76452019-09-03 17:03:03 +02002710 case LY_STMT_PREFIX:
David Sedlákb4e44562019-07-04 15:42:12 +02002711 return LYEXT_SUBSTMT_PREFIX;
Radek Krejcid6b76452019-09-03 17:03:03 +02002712 case LY_STMT_PRESENCE:
David Sedlákb4e44562019-07-04 15:42:12 +02002713 return LYEXT_SUBSTMT_PRESENCE;
Radek Krejcid6b76452019-09-03 17:03:03 +02002714 case LY_STMT_REFERENCE:
David Sedlákb4e44562019-07-04 15:42:12 +02002715 return LYEXT_SUBSTMT_REFERENCE;
Radek Krejcid6b76452019-09-03 17:03:03 +02002716 case LY_STMT_REVISION_DATE:
David Sedlákb4e44562019-07-04 15:42:12 +02002717 return LYEXT_SUBSTMT_REVISIONDATE;
Radek Krejcid6b76452019-09-03 17:03:03 +02002718 case LY_STMT_UNITS:
David Sedlákb4e44562019-07-04 15:42:12 +02002719 return LYEXT_SUBSTMT_UNITS;
Radek Krejcid6b76452019-09-03 17:03:03 +02002720 case LY_STMT_VALUE:
David Sedlákb4e44562019-07-04 15:42:12 +02002721 return LYEXT_SUBSTMT_VALUE;
Radek Krejcid6b76452019-09-03 17:03:03 +02002722 case LY_STMT_YANG_VERSION:
David Sedlákb4e44562019-07-04 15:42:12 +02002723 return LYEXT_SUBSTMT_VERSION;
Radek Krejcid6b76452019-09-03 17:03:03 +02002724 case LY_STMT_MODIFIER:
David Sedlákb4e44562019-07-04 15:42:12 +02002725 return LYEXT_SUBSTMT_MODIFIER;
Radek Krejcid6b76452019-09-03 17:03:03 +02002726 case LY_STMT_REQUIRE_INSTANCE:
David Sedlákb4e44562019-07-04 15:42:12 +02002727 return LYEXT_SUBSTMT_REQINSTANCE;
Radek Krejcid6b76452019-09-03 17:03:03 +02002728 case LY_STMT_YIN_ELEMENT:
David Sedlákb4e44562019-07-04 15:42:12 +02002729 return LYEXT_SUBSTMT_YINELEM;
Radek Krejcid6b76452019-09-03 17:03:03 +02002730 case LY_STMT_CONFIG:
David Sedlákb4e44562019-07-04 15:42:12 +02002731 return LYEXT_SUBSTMT_CONFIG;
Radek Krejcid6b76452019-09-03 17:03:03 +02002732 case LY_STMT_MANDATORY:
David Sedlákb4e44562019-07-04 15:42:12 +02002733 return LYEXT_SUBSTMT_MANDATORY;
Radek Krejcid6b76452019-09-03 17:03:03 +02002734 case LY_STMT_ORDERED_BY:
David Sedlákb4e44562019-07-04 15:42:12 +02002735 return LYEXT_SUBSTMT_ORDEREDBY;
Radek Krejcid6b76452019-09-03 17:03:03 +02002736 case LY_STMT_STATUS:
David Sedlákb4e44562019-07-04 15:42:12 +02002737 return LYEXT_SUBSTMT_STATUS;
Radek Krejcid6b76452019-09-03 17:03:03 +02002738 case LY_STMT_FRACTION_DIGITS:
David Sedlákb4e44562019-07-04 15:42:12 +02002739 return LYEXT_SUBSTMT_FRACDIGITS;
Radek Krejcid6b76452019-09-03 17:03:03 +02002740 case LY_STMT_MAX_ELEMENTS:
David Sedlákb4e44562019-07-04 15:42:12 +02002741 return LYEXT_SUBSTMT_MAX;
Radek Krejcid6b76452019-09-03 17:03:03 +02002742 case LY_STMT_MIN_ELEMENTS:
David Sedlákb4e44562019-07-04 15:42:12 +02002743 return LYEXT_SUBSTMT_MIN;
Radek Krejcid6b76452019-09-03 17:03:03 +02002744 case LY_STMT_POSITION:
David Sedlákb4e44562019-07-04 15:42:12 +02002745 return LYEXT_SUBSTMT_POSITION;
Radek Krejcid6b76452019-09-03 17:03:03 +02002746 case LY_STMT_UNIQUE:
David Sedlákb4e44562019-07-04 15:42:12 +02002747 return LYEXT_SUBSTMT_UNIQUE;
Radek Krejcid6b76452019-09-03 17:03:03 +02002748 case LY_STMT_IF_FEATURE:
David Sedlákb4e44562019-07-04 15:42:12 +02002749 return LYEXT_SUBSTMT_IFFEATURE;
2750 default:
2751 return LYEXT_SUBSTMT_SELF;
2752 }
2753}
2754
David Sedlákc5b20842019-08-13 10:18:31 +02002755/**
2756 * @brief map keyword to keyword-group.
2757 *
2758 * @param[in] ctx YIN parser context used for logging.
2759 * @param[in] kw Keyword that is child of module or submodule.
2760 * @param[out] group Group of keyword.
2761 *
2762 * @return LY_SUCCESS on success LY_EINT if kw can't be mapped to kw_group, should not happen if called correctly.
2763 */
David Sedláke6cd89e2019-08-07 12:46:02 +02002764static LY_ERR
Radek Krejcid6b76452019-09-03 17:03:03 +02002765kw2kw_group(struct yin_parser_ctx *ctx, enum ly_stmt kw, enum yang_module_stmt *group)
David Sedláke6cd89e2019-08-07 12:46:02 +02002766{
2767 switch (kw) {
2768 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02002769 case LY_STMT_NONE:
2770 case LY_STMT_NAMESPACE:
2771 case LY_STMT_PREFIX:
2772 case LY_STMT_BELONGS_TO:
2773 case LY_STMT_YANG_VERSION:
David Sedláke6cd89e2019-08-07 12:46:02 +02002774 *group = Y_MOD_MODULE_HEADER;
2775 break;
2776 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02002777 case LY_STMT_INCLUDE:
2778 case LY_STMT_IMPORT:
David Sedláke6cd89e2019-08-07 12:46:02 +02002779 *group = Y_MOD_LINKAGE;
2780 break;
2781 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02002782 case LY_STMT_ORGANIZATION:
2783 case LY_STMT_CONTACT:
2784 case LY_STMT_DESCRIPTION:
2785 case LY_STMT_REFERENCE:
David Sedláke6cd89e2019-08-07 12:46:02 +02002786 *group = Y_MOD_META;
2787 break;
2788 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02002789 case LY_STMT_REVISION:
David Sedláke6cd89e2019-08-07 12:46:02 +02002790 *group = Y_MOD_REVISION;
2791 break;
2792 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02002793 case LY_STMT_ANYDATA:
2794 case LY_STMT_ANYXML:
2795 case LY_STMT_AUGMENT:
2796 case LY_STMT_CHOICE:
2797 case LY_STMT_CONTAINER:
2798 case LY_STMT_DEVIATION:
2799 case LY_STMT_EXTENSION:
2800 case LY_STMT_FEATURE:
2801 case LY_STMT_GROUPING:
2802 case LY_STMT_IDENTITY:
2803 case LY_STMT_LEAF:
2804 case LY_STMT_LEAF_LIST:
2805 case LY_STMT_LIST:
2806 case LY_STMT_NOTIFICATION:
2807 case LY_STMT_RPC:
2808 case LY_STMT_TYPEDEF:
2809 case LY_STMT_USES:
2810 case LY_STMT_EXTENSION_INSTANCE:
David Sedláke6cd89e2019-08-07 12:46:02 +02002811 *group = Y_MOD_BODY;
2812 break;
2813 default:
2814 LOGINT(ctx->xml_ctx.ctx);
2815 return LY_EINT;
2816 }
2817
2818 return LY_SUCCESS;
2819}
2820
David Sedlákc5b20842019-08-13 10:18:31 +02002821/**
2822 * @brief Check if relative order of two keywords is valid.
2823 *
2824 * @param[in] ctx YIN parser context used for logging.
2825 * @param[in] kw Current keyword.
2826 * @param[in] next_kw Next keyword.
Radek Krejcid6b76452019-09-03 17:03:03 +02002827 * @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 +02002828 * because relative order is required only in module and submodule sub-elements, used for logging.
2829 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02002830 * @return LY_SUCCESS on success and LY_EVALID if relative order is invalid.
David Sedlákc5b20842019-08-13 10:18:31 +02002831 */
David Sedláke6cd89e2019-08-07 12:46:02 +02002832static LY_ERR
Radek Krejcid6b76452019-09-03 17:03:03 +02002833yin_check_relative_order(struct 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 +02002834{
Radek Krejcid6b76452019-09-03 17:03:03 +02002835 assert(parrent == LY_STMT_MODULE || parrent == LY_STMT_SUBMODULE);
David Sedláke6cd89e2019-08-07 12:46:02 +02002836 enum yang_module_stmt gr, next_gr;
2837
2838 LY_CHECK_RET(kw2kw_group(ctx, kw, &gr));
2839 LY_CHECK_RET(kw2kw_group(ctx, next_kw, &next_gr));
2840
2841 if (gr > next_gr) {
2842 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INORDER_YIN, ly_stmt2str(parrent), ly_stmt2str(next_kw), ly_stmt2str(kw));
2843 return LY_EVALID;
2844 }
2845
2846 return LY_SUCCESS;
2847}
2848
David Sedlákc5dd0942019-09-13 10:23:59 +02002849/**
2850 * @brief Get element name prefixed by full URI of xml namespace.
2851 *
2852 * @param[in] ctx YIN parser context used for logging and to get inormation about xml namespaces.
2853 * @param[in] name Name of element.
2854 * @param[in] name_len Length of element name.
2855 * @param[in] prefix Prefix of element.
2856 * @param[in] prefix_len Length of element prefix.
2857 *
2858 * @return Element name prefixed by URI on success, NULL on failure.
2859 */
2860static const char *
David Sedlák068dd352019-09-13 09:08:22 +02002861name2nsname(struct yin_parser_ctx *ctx, const char *name, size_t name_len, const char *prefix, size_t prefix_len)
2862{
2863 const struct lyxml_ns *ns = lyxml_ns_get(&ctx->xml_ctx, prefix, prefix_len);
David Sedláke0ef1c62019-09-13 10:05:55 +02002864 LY_CHECK_ERR_RET(!ns, LOGINT(ctx->xml_ctx.ctx), NULL);
David Sedlák068dd352019-09-13 09:08:22 +02002865
David Sedláke0ef1c62019-09-13 10:05:55 +02002866 size_t ns_len = strlen(ns->uri);
2867 size_t len = ns_len + name_len + 1; /* +1 because of ':' delimiter between ns and actual name */
2868
2869 char *result;
2870 char *temp;
2871 temp = result = malloc(sizeof(*temp) * (len + 1)); /* +1 for '\0' terminator */
2872 LY_CHECK_ERR_RET(!temp, LOGMEM(ctx->xml_ctx.ctx), NULL);
2873
2874 strcpy(result, ns->uri);
2875 result[ns_len] = ':';
2876 temp = &result[ns_len + 1];
2877 strncpy(temp, name, name_len);
2878 result[len] = '\0';
2879
2880 return lydict_insert_zc(ctx->xml_ctx.ctx, result);
David Sedlák068dd352019-09-13 09:08:22 +02002881}
2882
David Sedlákd6e56892019-07-01 15:40:24 +02002883LY_ERR
Radek Krejci12b1c152019-09-05 16:20:48 +02002884yin_parse_content(struct yin_parser_ctx *ctx, struct yin_subelement *subelem_info, size_t subelem_info_size,
Radek Krejcid6b76452019-09-03 17:03:03 +02002885 const char **data, enum ly_stmt current_element, const char **text_content, struct lysp_ext_instance **exts)
David Sedlákd6e56892019-07-01 15:40:24 +02002886{
2887 LY_ERR ret = LY_SUCCESS;
David Sedlák8e7bda82019-07-16 17:57:50 +02002888 char *out = NULL;
David Sedlákc5b20842019-08-13 10:18:31 +02002889 const char *prefix, *name;
2890 size_t out_len = 0, prefix_len, name_len;
David Sedlák8e7bda82019-07-16 17:57:50 +02002891 int dynamic = 0;
David Sedlák1af868e2019-07-17 17:03:14 +02002892 struct yin_arg_record *attrs = NULL;
Radek Krejcid6b76452019-09-03 17:03:03 +02002893 enum ly_stmt kw = LY_STMT_NONE, last_kw = LY_STMT_NONE;
David Sedlák1af868e2019-07-17 17:03:14 +02002894 struct yin_subelement *subelem = NULL;
David Sedlák09e18c92019-07-18 11:17:11 +02002895
David Sedlákda8ffa32019-07-08 14:17:10 +02002896 if (ctx->xml_ctx.status == LYXML_ELEM_CONTENT) {
2897 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákd6e56892019-07-01 15:40:24 +02002898 /* current element has subelements as content */
2899 if (ret == LY_EINVAL) {
David Sedlákda8ffa32019-07-08 14:17:10 +02002900 while (ctx->xml_ctx.status == LYXML_ELEMENT) {
David Sedlákc5b20842019-08-13 10:18:31 +02002901 ret = lyxml_get_element(&ctx->xml_ctx, data, &prefix, &prefix_len, &name, &name_len);
David Sedlákd6e56892019-07-01 15:40:24 +02002902 LY_CHECK_GOTO(ret, cleanup);
David Sedlákc5b20842019-08-13 10:18:31 +02002903 if (!name) {
David Sedlákd6e56892019-07-01 15:40:24 +02002904 /* end of current element reached */
2905 break;
2906 }
David Sedlák1af868e2019-07-17 17:03:14 +02002907 ret = yin_load_attributes(ctx, data, &attrs);
David Sedlákd6e56892019-07-01 15:40:24 +02002908 LY_CHECK_GOTO(ret, cleanup);
David Sedláke6cd89e2019-08-07 12:46:02 +02002909 last_kw = kw;
David Sedlákc5b20842019-08-13 10:18:31 +02002910 kw = yin_match_keyword(ctx, name, name_len, prefix, prefix_len, current_element);
David Sedlákd6e56892019-07-01 15:40:24 +02002911
2912 /* check if this element can be child of current element */
David Sedlák1af868e2019-07-17 17:03:14 +02002913 subelem = get_record(kw, subelem_info_size, subelem_info);
2914 if (!subelem) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002915 if (current_element == LY_STMT_DEVIATE && isdevsub(kw)) {
David Sedlák4ffcec82019-07-25 15:10:21 +02002916 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INDEV_YIN, ly_stmt2str(kw));
2917 } else {
David Sedlákc5b20842019-08-13 10:18:31 +02002918 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_UNEXP_SUBELEM, name_len, name, ly_stmt2str(current_element));
David Sedlák4ffcec82019-07-25 15:10:21 +02002919 }
David Sedlákd6e56892019-07-01 15:40:24 +02002920 ret = LY_EVALID;
2921 goto cleanup;
2922 }
2923
David Sedlák6542aed2019-08-14 10:47:43 +02002924 /* relative order is required only in module and submodule sub-elements */
Radek Krejcid6b76452019-09-03 17:03:03 +02002925 if (current_element == LY_STMT_MODULE || current_element == LY_STMT_SUBMODULE) {
David Sedláke6cd89e2019-08-07 12:46:02 +02002926 ret = yin_check_relative_order(ctx, last_kw, kw, current_element);
2927 LY_CHECK_GOTO(ret, cleanup);
2928 }
David Sedlák5f8191e2019-07-08 16:35:52 +02002929
David Sedlák4ffcec82019-07-25 15:10:21 +02002930 /* flag check */
David Sedlák1af868e2019-07-17 17:03:14 +02002931 if ((subelem->flags & YIN_SUBELEM_UNIQUE) && (subelem->flags & YIN_SUBELEM_PARSED)) {
David Sedlák4ffcec82019-07-25 15:10:21 +02002932 /* subelement uniquenes */
David Sedlák1538a842019-08-08 15:38:51 +02002933 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_SUBELEM_REDEF, ly_stmt2str(kw), ly_stmt2str(current_element));
David Sedlák21f87cd2019-07-03 16:53:23 +02002934 return LY_EVALID;
2935 }
David Sedlák1af868e2019-07-17 17:03:14 +02002936 if (subelem->flags & YIN_SUBELEM_FIRST) {
David Sedlák4ffcec82019-07-25 15:10:21 +02002937 /* subelement is supposed to be defined as first subelement */
David Sedlák1af868e2019-07-17 17:03:14 +02002938 ret = yin_check_subelem_first_constraint(ctx, subelem_info, subelem_info_size, current_element, subelem);
David Sedlák66d7c842019-07-11 15:06:04 +02002939 LY_CHECK_GOTO(ret, cleanup);
David Sedlák21f87cd2019-07-03 16:53:23 +02002940 }
David Sedlák0c2bab92019-07-22 15:33:19 +02002941 if (subelem->flags & YIN_SUBELEM_VER2) {
David Sedlák4ffcec82019-07-25 15:10:21 +02002942 /* subelement is supported only in version 1.1 or higher */
David Sedlák0c2bab92019-07-22 15:33:19 +02002943 if (ctx->mod_version < 2) {
David Sedlák1538a842019-08-08 15:38:51 +02002944 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INSUBELEM2, ly_stmt2str(kw), ly_stmt2str(current_element));
David Sedlák9bb1c042019-07-22 16:45:37 +02002945 ret = LY_EVALID;
2946 goto cleanup;
David Sedlák0c2bab92019-07-22 15:33:19 +02002947 }
2948 }
David Sedlák4ffcec82019-07-25 15:10:21 +02002949 /* note that element was parsed for easy uniqueness check in next iterations */
David Sedlák1af868e2019-07-17 17:03:14 +02002950 subelem->flags |= YIN_SUBELEM_PARSED;
David Sedlák21f87cd2019-07-03 16:53:23 +02002951
David Sedlákd6e56892019-07-01 15:40:24 +02002952 switch (kw) {
David Sedlák4ffcec82019-07-25 15:10:21 +02002953 /* call responsible function */
Radek Krejcid6b76452019-09-03 17:03:03 +02002954 case LY_STMT_EXTENSION_INSTANCE:
David Sedláke0ef1c62019-09-13 10:05:55 +02002955 ret = yin_parse_extension_instance(ctx, attrs, data, name, name_len, prefix, prefix_len,
David Sedlák1af868e2019-07-17 17:03:14 +02002956 kw2lyext_substmt(current_element),
2957 (subelem->dest) ? *((uint32_t*)subelem->dest) : 0, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02002958 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002959 case LY_STMT_ACTION:
2960 case LY_STMT_RPC:
David Sedlák6881b512019-08-13 12:52:00 +02002961 ret = yin_parse_action(ctx, attrs, data, (struct tree_node_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002962 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002963 case LY_STMT_ANYDATA:
2964 case LY_STMT_ANYXML:
David Sedlák8a83bbb2019-07-18 14:46:00 +02002965 ret = yin_parse_any(ctx, attrs, data, kw, (struct tree_node_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002966 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002967 case LY_STMT_ARGUMENT:
David Sedlákdf2691f2019-08-15 13:32:27 +02002968 ret = yin_parse_argument(ctx, attrs, data, (struct yin_argument_meta *)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02002969 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002970 case LY_STMT_AUGMENT:
David Sedlák6881b512019-08-13 12:52:00 +02002971 ret = yin_parse_augment(ctx, attrs, data, (struct tree_node_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002972 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002973 case LY_STMT_BASE:
David Sedlák6542aed2019-08-14 10:47:43 +02002974 ret = yin_parse_base(ctx, attrs, data, current_element, subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02002975 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002976 case LY_STMT_BELONGS_TO:
David Sedlák1af868e2019-07-17 17:03:14 +02002977 ret = yin_parse_belongs_to(ctx, attrs, data, (struct lysp_submodule *)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02002978 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002979 case LY_STMT_BIT:
David Sedlák43801c92019-08-05 15:58:54 +02002980 ret = yin_parse_bit(ctx, attrs, data, (struct lysp_type *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002981 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002982 case LY_STMT_CASE:
David Sedlák5379d392019-07-24 10:42:03 +02002983 ret = yin_parse_case(ctx, attrs, data, (struct tree_node_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002984 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002985 case LY_STMT_CHOICE:
David Sedlákb7abcfa2019-07-24 12:33:35 +02002986 ret = yin_parse_choice(ctx, attrs, data, (struct tree_node_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002987 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002988 case LY_STMT_CONFIG:
David Sedlák1af868e2019-07-17 17:03:14 +02002989 ret = yin_parse_config(ctx, attrs, data, (uint16_t *)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02002990 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002991 case LY_STMT_CONTACT:
2992 case LY_STMT_DESCRIPTION:
2993 case LY_STMT_ORGANIZATION:
2994 case LY_STMT_REFERENCE:
David Sedlákdf2691f2019-08-15 13:32:27 +02002995 ret = yin_parse_meta(ctx, attrs, data, kw, (const char **)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02002996 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002997 case LY_STMT_CONTAINER:
David Sedlákf111bcb2019-07-23 17:15:51 +02002998 ret = yin_parse_container(ctx, attrs, data, (struct tree_node_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002999 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003000 case LY_STMT_DEFAULT:
3001 case LY_STMT_ERROR_APP_TAG:
3002 case LY_STMT_KEY:
3003 case LY_STMT_PRESENCE:
David Sedlák6542aed2019-08-14 10:47:43 +02003004 ret = yin_parse_simple_elem(ctx, attrs, data, kw, subelem, YIN_ARG_VALUE, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02003005 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003006 case LY_STMT_DEVIATE:
David Sedlák4ffcec82019-07-25 15:10:21 +02003007 ret = yin_parse_deviate(ctx, attrs, data, (struct lysp_deviate **)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003008 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003009 case LY_STMT_DEVIATION:
David Sedlák8b754462019-07-25 16:22:13 +02003010 ret = yin_parse_deviation(ctx, attrs, data, (struct lysp_deviation **)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003011 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003012 case LY_STMT_ENUM:
David Sedlák43801c92019-08-05 15:58:54 +02003013 ret = yin_parse_enum(ctx, attrs, data, (struct lysp_type *)subelem->dest);
3014 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003015 case LY_STMT_ERROR_MESSAGE:
David Sedlákdf2691f2019-08-15 13:32:27 +02003016 ret = yin_parse_err_msg(ctx, attrs, data, (const char **)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02003017 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003018 case LY_STMT_EXTENSION:
David Sedlák1af868e2019-07-17 17:03:14 +02003019 ret = yin_parse_extension(ctx, attrs, data, (struct lysp_ext **)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003020 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003021 case LY_STMT_FEATURE:
David Sedlák5e13dea2019-07-22 16:06:45 +02003022 ret = yin_parse_feature(ctx, attrs, data, (struct lysp_feature **)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003023 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003024 case LY_STMT_FRACTION_DIGITS:
David Sedlák1af868e2019-07-17 17:03:14 +02003025 ret = yin_parse_fracdigits(ctx, attrs, data, (struct lysp_type *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003026 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003027 case LY_STMT_GROUPING:
David Sedlák6881b512019-08-13 12:52:00 +02003028 ret = yin_parse_grouping(ctx, attrs, data, (struct tree_node_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003029 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003030 case LY_STMT_IDENTITY:
David Sedlák28794f22019-07-22 16:45:00 +02003031 ret = yin_parse_identity(ctx, attrs, data, (struct lysp_ident **)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003032 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003033 case LY_STMT_IF_FEATURE:
3034 case LY_STMT_UNITS:
David Sedlák6542aed2019-08-14 10:47:43 +02003035 ret = yin_parse_simple_elem(ctx, attrs, data, kw, subelem, YIN_ARG_NAME, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02003036 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003037 case LY_STMT_IMPORT:
David Sedlák298ff6d2019-07-26 14:29:03 +02003038 ret = yin_parse_import(ctx, attrs, data, (struct import_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003039 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003040 case LY_STMT_INCLUDE:
David Sedlák0c2bab92019-07-22 15:33:19 +02003041 ret = yin_parse_include(ctx, attrs, data, (struct include_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003042 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003043 case LY_STMT_INPUT:
3044 case LY_STMT_OUTPUT:
David Sedlák05404f62019-07-24 14:11:53 +02003045 ret = yin_parse_inout(ctx, attrs, data, kw, (struct inout_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003046 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003047 case LY_STMT_LEAF:
David Sedlák203ca3a2019-07-18 15:26:25 +02003048 ret = yin_parse_leaf(ctx, attrs, data, (struct tree_node_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003049 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003050 case LY_STMT_LEAF_LIST:
David Sedlákc3da3ef2019-07-19 12:56:08 +02003051 ret = yin_parse_leaflist(ctx, attrs, data, (struct tree_node_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003052 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003053 case LY_STMT_LENGTH:
David Sedlák6542aed2019-08-14 10:47:43 +02003054 ret = yin_parse_length(ctx, attrs, data, (struct lysp_type *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003055 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003056 case LY_STMT_LIST:
David Sedlákaf536aa2019-07-23 13:42:23 +02003057 ret = yin_parse_list(ctx, attrs, data, (struct tree_node_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003058 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003059 case LY_STMT_MANDATORY:
David Sedlák1af868e2019-07-17 17:03:14 +02003060 ret = yin_parse_mandatory(ctx, attrs, data, (uint16_t *)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02003061 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003062 case LY_STMT_MAX_ELEMENTS:
3063 case LY_STMT_MIN_ELEMENTS:
David Sedlák09e18c92019-07-18 11:17:11 +02003064 ret = yin_parse_minmax(ctx, attrs, data, current_element, kw, subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003065 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003066 case LY_STMT_MODIFIER:
David Sedlák1af868e2019-07-17 17:03:14 +02003067 ret = yin_parse_modifier(ctx, attrs, data, (const char **)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02003068 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003069 case LY_STMT_MUST:
David Sedlák1af868e2019-07-17 17:03:14 +02003070 ret = yin_parse_must(ctx, attrs, data, (struct lysp_restr **)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003071 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003072 case LY_STMT_NAMESPACE:
David Sedlák6542aed2019-08-14 10:47:43 +02003073 ret = yin_parse_simple_elem(ctx, attrs, data, kw, subelem, YIN_ARG_URI, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02003074 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003075 case LY_STMT_NOTIFICATION:
David Sedlák6881b512019-08-13 12:52:00 +02003076 ret = yin_parse_notification(ctx, attrs, data, (struct tree_node_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003077 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003078 case LY_STMT_ORDERED_BY:
David Sedláka2dad212019-07-18 12:45:19 +02003079 ret = yin_parse_orderedby(ctx, attrs, data, (uint16_t *)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02003080 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003081 case LY_STMT_PATH:
David Sedlák6542aed2019-08-14 10:47:43 +02003082 ret = yin_parse_path(ctx, attrs, data, kw, (struct lysp_type *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003083 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003084 case LY_STMT_PATTERN:
David Sedlák1af868e2019-07-17 17:03:14 +02003085 ret = yin_parse_pattern(ctx, attrs, data, (struct lysp_type *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003086 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003087 case LY_STMT_VALUE:
3088 case LY_STMT_POSITION:
David Sedlákdf2691f2019-08-15 13:32:27 +02003089 ret = yin_parse_value_pos(ctx, attrs, data, kw, (struct lysp_type_enum *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003090 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003091 case LY_STMT_PREFIX:
David Sedlák6542aed2019-08-14 10:47:43 +02003092 ret = yin_parse_simple_elem(ctx, attrs, data, kw, subelem, YIN_ARG_VALUE, Y_IDENTIF_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02003093 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003094 case LY_STMT_RANGE:
David Sedlák6542aed2019-08-14 10:47:43 +02003095 ret = yin_parse_range(ctx, attrs, data, (struct lysp_type *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003096 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003097 case LY_STMT_REFINE:
David Sedlákd2d676a2019-07-22 11:28:19 +02003098 ret = yin_parse_refine(ctx, attrs, data, (struct lysp_refine **)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003099 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003100 case LY_STMT_REQUIRE_INSTANCE:
David Sedlák1af868e2019-07-17 17:03:14 +02003101 ret = yin_pasrse_reqinstance(ctx, attrs, data, (struct lysp_type *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003102 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003103 case LY_STMT_REVISION:
David Sedlákaa854b02019-07-22 14:17:10 +02003104 ret = yin_parse_revision(ctx, attrs, data, (struct lysp_revision **)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003105 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003106 case LY_STMT_REVISION_DATE:
David Sedlák1af868e2019-07-17 17:03:14 +02003107 ret = yin_parse_revision_date(ctx, attrs, data, (char *)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02003108 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003109 case LY_STMT_STATUS:
David Sedlák1af868e2019-07-17 17:03:14 +02003110 ret = yin_parse_status(ctx, attrs, data, (uint16_t *)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02003111 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003112 case LY_STMT_TYPE:
David Sedlák6542aed2019-08-14 10:47:43 +02003113 ret = yin_parse_type(ctx, attrs, data, current_element, subelem);
David Sedlákd6e56892019-07-01 15:40:24 +02003114 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003115 case LY_STMT_TYPEDEF:
David Sedlák6881b512019-08-13 12:52:00 +02003116 ret = yin_parse_typedef(ctx, attrs, data, (struct tree_node_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003117 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003118 case LY_STMT_UNIQUE:
David Sedlák6542aed2019-08-14 10:47:43 +02003119 ret = yin_parse_simple_elem(ctx, attrs, data, kw, subelem, YIN_ARG_TAG, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02003120 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003121 case LY_STMT_USES:
David Sedlák0d6de5a2019-07-22 13:25:44 +02003122 ret = yin_parse_uses(ctx, attrs, data, (struct tree_node_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003123 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003124 case LY_STMT_WHEN:
David Sedlák1af868e2019-07-17 17:03:14 +02003125 ret = yin_parse_when(ctx, attrs, data, (struct lysp_when **)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003126 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003127 case LY_STMT_YANG_VERSION:
David Sedlák1af868e2019-07-17 17:03:14 +02003128 ret = yin_parse_yangversion(ctx, attrs, data, (uint8_t *)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02003129 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003130 case LY_STMT_YIN_ELEMENT:
David Sedlákdf2691f2019-08-15 13:32:27 +02003131 ret = yin_parse_yin_element(ctx, attrs, data, (uint16_t *)subelem->dest, exts);
David Sedlák3ffbc522019-07-02 17:49:28 +02003132 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003133 case LY_STMT_ARG_TEXT:
3134 case LY_STMT_ARG_VALUE:
David Sedlák1af868e2019-07-17 17:03:14 +02003135 ret = yin_parse_content(ctx, NULL, 0, data, kw, (const char **)subelem->dest, NULL);
David Sedlákd6e56892019-07-01 15:40:24 +02003136 break;
3137 default:
David Sedlákda8ffa32019-07-08 14:17:10 +02003138 LOGINT(ctx->xml_ctx.ctx);
David Sedlákbf8a2b72019-08-14 16:48:10 +02003139 ret = LY_EINT;
David Sedlákd6e56892019-07-01 15:40:24 +02003140 }
David Sedlák3ffbc522019-07-02 17:49:28 +02003141 LY_CHECK_GOTO(ret, cleanup);
David Sedlák1af868e2019-07-17 17:03:14 +02003142 FREE_ARRAY(ctx, attrs, free_arg_rec);
3143 attrs = NULL;
3144 subelem = NULL;
David Sedlákd6e56892019-07-01 15:40:24 +02003145 }
3146 } else {
David Sedlákbf8a2b72019-08-14 16:48:10 +02003147 LY_CHECK_RET(ret);
David Sedlákd6e56892019-07-01 15:40:24 +02003148 /* elements with text or none content */
David Sedlák3ffbc522019-07-02 17:49:28 +02003149 /* save text content, if text_content isn't set, it's just ignored */
David Sedlákc5b20842019-08-13 10:18:31 +02003150 /* no resources are allocated in this branch, no need to use cleanup label */
David Sedlák3b4df842019-07-17 11:39:46 +02003151 LY_CHECK_RET(yin_validate_value(ctx, Y_STR_ARG, out, out_len));
David Sedlák3ffbc522019-07-02 17:49:28 +02003152 if (text_content) {
David Sedlák169cc522019-08-15 13:23:45 +02003153 INSERT_STRING(ctx->xml_ctx.ctx, *text_content, dynamic, out, out_len);
3154 LY_CHECK_RET(!*text_content, LY_EMEM);
David Sedlák3ffbc522019-07-02 17:49:28 +02003155 }
David Sedlákd6e56892019-07-01 15:40:24 +02003156 /* load closing element */
David Sedlákc5b20842019-08-13 10:18:31 +02003157 LY_CHECK_RET(lyxml_get_element(&ctx->xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
David Sedlákd6e56892019-07-01 15:40:24 +02003158 }
3159 }
David Sedlák8b754462019-07-25 16:22:13 +02003160 /* mandatory subelemnts are checked only after whole element was succesfully parsed */
3161 LY_CHECK_RET(yin_check_subelem_mandatory_constraint(ctx, subelem_info, subelem_info_size, current_element));
David Sedlákd6e56892019-07-01 15:40:24 +02003162
3163cleanup:
David Sedlák1af868e2019-07-17 17:03:14 +02003164 FREE_ARRAY(ctx, attrs, free_arg_rec);
David Sedlákd6e56892019-07-01 15:40:24 +02003165 return ret;
3166}
3167
David Sedlák619db942019-07-03 14:47:30 +02003168LY_ERR
David Sedláke0ef1c62019-09-13 10:05:55 +02003169yin_parse_extension_instance(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
3170 const char *ext_name, size_t ext_name_len, const char *ext_prefix, size_t ext_prefix_len,
3171 LYEXT_SUBSTMT subelem, uint32_t subelem_index, struct lysp_ext_instance **exts)
David Sedlák1e2cdd02019-06-27 14:17:43 +02003172{
3173 LY_ERR ret = LY_SUCCESS;
David Sedlákb1a78352019-06-28 16:16:29 +02003174 char *out;
3175 const char *name, *prefix;
3176 size_t out_len, prefix_len, name_len;
3177 int dynamic;
David Sedlák1e2cdd02019-06-27 14:17:43 +02003178 struct lysp_ext_instance *e;
David Sedlákb1a78352019-06-28 16:16:29 +02003179 struct lysp_stmt *last_subelem = NULL, *new_subelem = NULL;
3180 struct yin_arg_record *iter;
David Sedlák1e2cdd02019-06-27 14:17:43 +02003181
David Sedlákda8ffa32019-07-08 14:17:10 +02003182 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *exts, e, LY_EMEM);
David Sedlák1e2cdd02019-06-27 14:17:43 +02003183
3184 e->yin = 0;
3185 /* store name and insubstmt info */
David Sedláke0ef1c62019-09-13 10:05:55 +02003186 e->name = name2nsname(ctx, ext_name, ext_name_len, ext_prefix, ext_prefix_len);
3187 LY_CHECK_RET(!e->name, LY_EMEM);
David Sedlák619db942019-07-03 14:47:30 +02003188 e->insubstmt = subelem;
3189 e->insubstmt_index = subelem_index;
David Sedlák1e2cdd02019-06-27 14:17:43 +02003190 e->yin |= LYS_YIN;
3191
David Sedlákb1a78352019-06-28 16:16:29 +02003192 /* store attributes as subelements */
David Sedlák1f90d252019-07-10 17:09:32 +02003193 LY_ARRAY_FOR_ITER(attrs, struct yin_arg_record, iter) {
David Sedlákb1a78352019-06-28 16:16:29 +02003194 if (!iter->prefix) {
3195 new_subelem = calloc(1, sizeof(*new_subelem));
3196 if (!e->child) {
3197 e->child = new_subelem;
David Sedlák1e2cdd02019-06-27 14:17:43 +02003198 } else {
David Sedlákb1a78352019-06-28 16:16:29 +02003199 last_subelem->next = new_subelem;
3200 }
3201 last_subelem = new_subelem;
3202
3203 last_subelem->flags |= LYS_YIN_ATTR;
David Sedlákda8ffa32019-07-08 14:17:10 +02003204 last_subelem->stmt = lydict_insert(ctx->xml_ctx.ctx, iter->name, iter->name_len);
David Sedlák169cc522019-08-15 13:23:45 +02003205 LY_CHECK_RET(!last_subelem->stmt, LY_EMEM);
3206
3207 INSERT_STRING(ctx->xml_ctx.ctx, last_subelem->arg, iter->dynamic_content, iter->content, iter->content_len);
3208 LY_CHECK_RET(!last_subelem->arg, LY_EMEM);
David Sedlák1e2cdd02019-06-27 14:17:43 +02003209 }
3210 }
David Sedlák1e2cdd02019-06-27 14:17:43 +02003211
David Sedlákf250ecf2019-07-01 11:02:05 +02003212 /* parse subelements */
David Sedlákda8ffa32019-07-08 14:17:10 +02003213 if (ctx->xml_ctx.status == LYXML_ELEM_CONTENT) {
3214 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákb1a78352019-06-28 16:16:29 +02003215 if (ret == LY_EINVAL) {
David Sedlákda8ffa32019-07-08 14:17:10 +02003216 while (ctx->xml_ctx.status == LYXML_ELEMENT) {
3217 LY_CHECK_RET(lyxml_get_element(&ctx->xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
David Sedlákb1a78352019-06-28 16:16:29 +02003218 if (!name) {
3219 /* end of extension instance reached */
3220 break;
3221 }
David Sedlák071f7662019-09-12 02:02:51 +02003222 LY_CHECK_RET(yin_parse_element_generic(ctx, name, name_len, prefix, prefix_len, LY_STMT_EXTENSION_INSTANCE, data, &new_subelem));
David Sedlákb1a78352019-06-28 16:16:29 +02003223 if (!e->child) {
3224 e->child = new_subelem;
3225 } else {
3226 last_subelem->next = new_subelem;
3227 }
3228 last_subelem = new_subelem;
3229 }
David Sedlák555c7202019-07-04 12:14:12 +02003230 } else {
David Sedlák169cc522019-08-15 13:23:45 +02003231 if (out_len != 0) {
3232 /* save text content */
3233 LY_CHECK_RET(ret);
3234
3235 INSERT_STRING(ctx->xml_ctx.ctx, e->argument, dynamic, out, out_len);
David Sedlák555c7202019-07-04 12:14:12 +02003236 LY_CHECK_RET(!e->argument, LY_EMEM);
David Sedlák169cc522019-08-15 13:23:45 +02003237
3238 /* load closing element */
3239 LY_CHECK_RET(lyxml_get_element(&ctx->xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
3240 LY_CHECK_RET(name, LY_EINT);
David Sedlák555c7202019-07-04 12:14:12 +02003241 }
David Sedlákb1a78352019-06-28 16:16:29 +02003242 }
David Sedlákb1a78352019-06-28 16:16:29 +02003243 }
3244
3245 return LY_SUCCESS;
3246}
3247
David Sedlákaa98bba2019-09-12 11:52:14 +02003248/**
3249 * @brief Parse argument of extension subelement that is classic yang keyword and not another instance of extension.
3250 *
3251 * @param[in,out] ctx Yin parser context for logging and to store current state.
3252 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of extension instance.
3253 * @param[in,out] data Data to read from, always moved to currently handled character.
3254 * @param[in] elem_type Type of element that is currently being parsed.
3255 * @param[out] arg Value to write to.
3256 *
3257 * @return LY_ERR values.
3258 */
3259static LY_ERR
3260yin_parse_arg_inext_known(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, enum ly_stmt elem_type,
3261 const char **arg)
David Sedláke6284fc2019-09-10 11:57:02 +02003262{
David Sedláke6284fc2019-09-10 11:57:02 +02003263 LY_ERR ret = LY_SUCCESS;
David Sedlák071f7662019-09-12 02:02:51 +02003264 char *out = NULL;
3265 const char *name, *prefix;
3266 size_t out_len, name_len, prefix_len;
3267 int dynamic;
3268 enum ly_stmt child;
David Sedláke6284fc2019-09-10 11:57:02 +02003269
David Sedláke6284fc2019-09-10 11:57:02 +02003270 switch (elem_type) {
3271 case LY_STMT_ACTION:
3272 case LY_STMT_ANYDATA:
3273 case LY_STMT_ANYXML:
3274 case LY_STMT_ARGUMENT:
3275 case LY_STMT_BASE:
3276 case LY_STMT_BIT:
3277 case LY_STMT_CASE:
3278 case LY_STMT_CHOICE:
3279 case LY_STMT_CONTAINER:
3280 case LY_STMT_ENUM:
3281 case LY_STMT_EXTENSION:
3282 case LY_STMT_FEATURE:
3283 case LY_STMT_GROUPING:
3284 case LY_STMT_IDENTITY:
3285 case LY_STMT_IF_FEATURE:
3286 case LY_STMT_LEAF:
3287 case LY_STMT_LEAF_LIST:
3288 case LY_STMT_LIST:
3289 case LY_STMT_MODULE:
3290 case LY_STMT_NOTIFICATION:
3291 case LY_STMT_RPC:
3292 case LY_STMT_SUBMODULE:
3293 case LY_STMT_TYPE:
3294 case LY_STMT_TYPEDEF:
3295 case LY_STMT_UNITS:
3296 case LY_STMT_USES:
David Sedlákaa98bba2019-09-12 11:52:14 +02003297 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, arg, Y_MAYBE_STR_ARG, elem_type));
David Sedláke6284fc2019-09-10 11:57:02 +02003298 break;
3299 case LY_STMT_AUGMENT:
3300 case LY_STMT_DEVIATION:
3301 case LY_STMT_REFINE:
David Sedlákaa98bba2019-09-12 11:52:14 +02003302 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_TARGET_NODE, arg, Y_MAYBE_STR_ARG, elem_type));
David Sedláke6284fc2019-09-10 11:57:02 +02003303 break;
3304 case LY_STMT_CONFIG:
3305 case LY_STMT_DEFAULT:
3306 case LY_STMT_DEVIATE:
3307 case LY_STMT_ERROR_APP_TAG:
David Sedláke6284fc2019-09-10 11:57:02 +02003308 case LY_STMT_FRACTION_DIGITS:
3309 case LY_STMT_KEY:
3310 case LY_STMT_LENGTH:
3311 case LY_STMT_MANDATORY:
3312 case LY_STMT_MAX_ELEMENTS:
3313 case LY_STMT_MIN_ELEMENTS:
3314 case LY_STMT_MODIFIER:
3315 case LY_STMT_ORDERED_BY:
3316 case LY_STMT_PATH:
3317 case LY_STMT_PATTERN:
3318 case LY_STMT_POSITION:
3319 case LY_STMT_PREFIX:
3320 case LY_STMT_PRESENCE:
3321 case LY_STMT_RANGE:
3322 case LY_STMT_REQUIRE_INSTANCE:
3323 case LY_STMT_STATUS:
3324 case LY_STMT_VALUE:
3325 case LY_STMT_YANG_VERSION:
3326 case LY_STMT_YIN_ELEMENT:
David Sedlákaa98bba2019-09-12 11:52:14 +02003327 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, arg, Y_MAYBE_STR_ARG, elem_type));
David Sedláke6284fc2019-09-10 11:57:02 +02003328 break;
3329 case LY_STMT_IMPORT:
3330 case LY_STMT_INCLUDE:
3331 case LY_STMT_BELONGS_TO:
David Sedlákaa98bba2019-09-12 11:52:14 +02003332 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_MODULE, arg, Y_MAYBE_STR_ARG, elem_type));
David Sedláke6284fc2019-09-10 11:57:02 +02003333 break;
3334 case LY_STMT_INPUT:
3335 case LY_STMT_OUTPUT:
David Sedlákaa98bba2019-09-12 11:52:14 +02003336 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NONE, arg, Y_MAYBE_STR_ARG, elem_type));
David Sedláke6284fc2019-09-10 11:57:02 +02003337 break;
3338 case LY_STMT_MUST:
3339 case LY_STMT_WHEN:
David Sedlákaa98bba2019-09-12 11:52:14 +02003340 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_CONDITION, arg, Y_MAYBE_STR_ARG, elem_type));
David Sedláke6284fc2019-09-10 11:57:02 +02003341 break;
3342 case LY_STMT_NAMESPACE:
David Sedlákaa98bba2019-09-12 11:52:14 +02003343 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_URI, arg, Y_MAYBE_STR_ARG, elem_type));
David Sedláke6284fc2019-09-10 11:57:02 +02003344 break;
3345 case LY_STMT_REVISION:
3346 case LY_STMT_REVISION_DATE:
David Sedlákaa98bba2019-09-12 11:52:14 +02003347 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_DATE, arg, Y_MAYBE_STR_ARG, elem_type));
David Sedláke6284fc2019-09-10 11:57:02 +02003348 break;
3349 case LY_STMT_UNIQUE:
David Sedlákaa98bba2019-09-12 11:52:14 +02003350 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_TAG, arg, Y_MAYBE_STR_ARG, elem_type));
David Sedláke6284fc2019-09-10 11:57:02 +02003351 break;
David Sedlák071f7662019-09-12 02:02:51 +02003352 /* argument is mapped to yin element */
3353 case LY_STMT_CONTACT:
3354 case LY_STMT_DESCRIPTION:
3355 case LY_STMT_ORGANIZATION:
3356 case LY_STMT_REFERENCE:
3357 case LY_STMT_ERROR_MESSAGE:
David Sedlákaa98bba2019-09-12 11:52:14 +02003358 /* there shouldn't be any attribute, argument is supposed to be first subelement */
3359 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NONE, arg, Y_MAYBE_STR_ARG, elem_type));
David Sedlák071f7662019-09-12 02:02:51 +02003360 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákaa98bba2019-09-12 11:52:14 +02003361 LY_CHECK_ERR_RET(ret != LY_EINVAL, LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_FIRT_SUBELEM,
3362 elem_type == LY_STMT_ERROR_MESSAGE ? "value" : "text", ly_stmt2str(elem_type)),
3363 LY_EVALID);
David Sedlák071f7662019-09-12 02:02:51 +02003364 LY_CHECK_RET(lyxml_get_element(&ctx->xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
3365 child = yin_match_keyword(ctx, name, name_len, prefix, prefix_len, elem_type);
David Sedlákaa98bba2019-09-12 11:52:14 +02003366 if ((elem_type == LY_STMT_ERROR_MESSAGE && child == LY_STMT_ARG_VALUE) ||
3367 (elem_type != LY_STMT_ERROR_MESSAGE && child == LY_STMT_ARG_TEXT)) {
3368 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_UNEXP_SUBELEM, name_len, name, ly_stmt2str(elem_type));
David Sedlák071f7662019-09-12 02:02:51 +02003369 return LY_EVALID;
3370 }
3371 /* load and save content */
3372 LY_CHECK_RET(lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic));
David Sedlákaa98bba2019-09-12 11:52:14 +02003373 INSERT_STRING(ctx->xml_ctx.ctx, *arg, dynamic, out, out_len);
3374 LY_CHECK_RET(!*arg, LY_EMEM);
3375 /* load closing tag of subelement */
David Sedlák071f7662019-09-12 02:02:51 +02003376 LY_CHECK_RET(lyxml_get_element(&ctx->xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
David Sedlákaa98bba2019-09-12 11:52:14 +02003377 /* if only subelement was parsed as argument, load also closing tag */
3378 if (ctx->xml_ctx.status == LYXML_ELEMENT) {
3379 LY_CHECK_RET(lyxml_get_element(&ctx->xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
3380 }
David Sedlákb0ca07d2019-09-11 11:54:05 +02003381 break;
David Sedláke6284fc2019-09-10 11:57:02 +02003382 default:
3383 LOGINT(ctx->xml_ctx.ctx);
David Sedlák071f7662019-09-12 02:02:51 +02003384 return LY_EINT;
David Sedláke6284fc2019-09-10 11:57:02 +02003385 }
3386
David Sedlák071f7662019-09-12 02:02:51 +02003387 return LY_SUCCESS;
David Sedláke6284fc2019-09-10 11:57:02 +02003388}
3389
3390LY_ERR
David Sedlákaa98bba2019-09-12 11:52:14 +02003391yin_parse_element_generic(struct yin_parser_ctx *ctx, const char *name, size_t name_len, const char *prefix, size_t prefix_len, enum ly_stmt parent,
David Sedlák071f7662019-09-12 02:02:51 +02003392 const char **data, struct lysp_stmt **element)
David Sedlákb1a78352019-06-28 16:16:29 +02003393{
3394 LY_ERR ret = LY_SUCCESS;
3395 const char *temp_prefix, *temp_name;
3396 char *out = NULL;
Radek Krejci6df08da2019-09-09 14:07:21 +02003397 size_t out_len, temp_name_len, temp_prefix_len;
David Sedlákb1a78352019-06-28 16:16:29 +02003398 int dynamic;
David Sedlákb1a78352019-06-28 16:16:29 +02003399 struct lysp_stmt *last = NULL, *new = NULL;
David Sedlákb0ca07d2019-09-11 11:54:05 +02003400 struct yin_arg_record *attrs = NULL;
3401 struct yin_arg_record *iter = NULL;
David Sedlákb1a78352019-06-28 16:16:29 +02003402
David Sedláke0ef1c62019-09-13 10:05:55 +02003403 /* load all attributes for correct namespace evaluation */
David Sedlákb0ca07d2019-09-11 11:54:05 +02003404 ret = yin_load_attributes(ctx, data, &attrs);
3405 LY_CHECK_GOTO(ret, cleanup);
David Sedláke0ef1c62019-09-13 10:05:55 +02003406
3407 /* allocate new structure for element */
3408 *element = calloc(1, sizeof(**element));
3409 LY_CHECK_ERR_GOTO(!(*element), LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, cleanup);
David Sedláke0ef1c62019-09-13 10:05:55 +02003410 (*element)->stmt = name2nsname(ctx, name, name_len, prefix, prefix_len);
3411 LY_CHECK_ERR_GOTO(!(*element)->stmt, ret = LY_EMEM, cleanup);
David Sedláke0ef1c62019-09-13 10:05:55 +02003412
David Sedlákaa98bba2019-09-12 11:52:14 +02003413 (*element)->kw = yin_match_keyword(ctx, name, name_len, prefix, prefix_len, parent);
David Sedlákb1a78352019-06-28 16:16:29 +02003414
3415 last = (*element)->child;
David Sedláke6284fc2019-09-10 11:57:02 +02003416 if ((*element)->kw == LY_STMT_NONE) {
3417 /* unrecognized element */
David Sedlákaa98bba2019-09-12 11:52:14 +02003418 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_UNEXP_SUBELEM, name_len, name, ly_stmt2str(parent));
3419 ret = LY_EVALID;
3420 goto cleanup;
David Sedláke6284fc2019-09-10 11:57:02 +02003421 } else if ((*element)->kw != LY_STMT_EXTENSION_INSTANCE) {
3422 /* element is known yang keyword, which means argument can be parsed correctly. */
David Sedlákaa98bba2019-09-12 11:52:14 +02003423 ret = yin_parse_arg_inext_known(ctx, attrs, data, (*element)->kw, &(*element)->arg);
3424 LY_CHECK_GOTO(ret, cleanup);
David Sedláke6284fc2019-09-10 11:57:02 +02003425 } else {
David Sedlákaa98bba2019-09-12 11:52:14 +02003426 /* load attributes in generic way, save all attributes in linked list */
David Sedlákb0ca07d2019-09-11 11:54:05 +02003427 LY_ARRAY_FOR(attrs, struct yin_arg_record, iter) {
David Sedláke6284fc2019-09-10 11:57:02 +02003428 new = calloc(1, sizeof(*last));
David Sedlákaa98bba2019-09-12 11:52:14 +02003429 LY_CHECK_ERR_GOTO(!new, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, cleanup);
David Sedláke6284fc2019-09-10 11:57:02 +02003430 if (!(*element)->child) {
3431 /* save first */
3432 (*element)->child = new;
3433 } else {
3434 last->next = new;
3435 }
3436 last = new;
David Sedlákb1a78352019-06-28 16:16:29 +02003437
David Sedláke6284fc2019-09-10 11:57:02 +02003438 last->flags |= LYS_YIN_ATTR;
David Sedlákb0ca07d2019-09-11 11:54:05 +02003439 last->stmt = lydict_insert(ctx->xml_ctx.ctx, iter->name, iter->name_len);
3440 last->kw = LY_STMT_NONE;
David Sedláke6284fc2019-09-10 11:57:02 +02003441 /* attributes with prefix are ignored */
David Sedlákb0ca07d2019-09-11 11:54:05 +02003442 if (!iter->prefix) {
3443 INSERT_STRING(ctx->xml_ctx.ctx, last->arg, iter->dynamic_content, iter->content, iter->content_len);
David Sedlákaa98bba2019-09-12 11:52:14 +02003444 LY_CHECK_ERR_GOTO(!last->arg, ret = LY_EMEM, cleanup);
David Sedlákb0ca07d2019-09-11 11:54:05 +02003445 /* string is no longer supposed to be freed when the array is freed */
3446 iter->dynamic_content = 0;
David Sedláka82ecff2019-08-16 10:51:48 +02003447 }
David Sedlákb1a78352019-06-28 16:16:29 +02003448 }
3449 }
3450
David Sedláka82ecff2019-08-16 10:51:48 +02003451 /* parse content of element if any */
3452 if (ctx->xml_ctx.status == LYXML_ELEM_CONTENT) {
3453 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
3454 if (ret == LY_EINVAL) {
3455 while (ctx->xml_ctx.status == LYXML_ELEMENT) {
3456 /* parse subelements */
David Sedlákaa98bba2019-09-12 11:52:14 +02003457 ret = lyxml_get_element(&ctx->xml_ctx, data, &temp_prefix, &temp_prefix_len, &temp_name, &temp_name_len);
3458 LY_CHECK_GOTO(ret , cleanup);
David Sedláka82ecff2019-08-16 10:51:48 +02003459 if (!temp_name) {
3460 /* end of element reached */
3461 break;
3462 }
David Sedlákaa98bba2019-09-12 11:52:14 +02003463 ret = yin_parse_element_generic(ctx, temp_name, temp_name_len, temp_prefix, temp_prefix_len, (*element)->kw, data, &new);
3464 LY_CHECK_GOTO(ret, cleanup);
David Sedláka82ecff2019-08-16 10:51:48 +02003465 if (!(*element)->child) {
3466 /* save first */
3467 (*element)->child = new;
3468 } else {
3469 last->next = new;
3470 }
3471 last = new;
David Sedlákb1a78352019-06-28 16:16:29 +02003472 }
David Sedlákb0ca07d2019-09-11 11:54:05 +02003473 ret = LY_SUCCESS;
David Sedláka82ecff2019-08-16 10:51:48 +02003474 } else {
David Sedlákaa98bba2019-09-12 11:52:14 +02003475 LY_CHECK_GOTO(ret, cleanup);
David Sedláka82ecff2019-08-16 10:51:48 +02003476 /* save element content */
3477 if (out_len != 0) {
3478 INSERT_STRING(ctx->xml_ctx.ctx, (*element)->arg, dynamic, out, out_len);
David Sedlákaa98bba2019-09-12 11:52:14 +02003479 LY_CHECK_ERR_GOTO(!(*element)->arg, ret = LY_EMEM, cleanup);
David Sedláka82ecff2019-08-16 10:51:48 +02003480 }
David Sedlák169cc522019-08-15 13:23:45 +02003481
David Sedláka82ecff2019-08-16 10:51:48 +02003482 /* read closing tag */
David Sedlákaa98bba2019-09-12 11:52:14 +02003483 ret = lyxml_get_element(&ctx->xml_ctx, data, &temp_prefix, &temp_prefix_len, &temp_name, &temp_name_len);
3484 LY_CHECK_GOTO(ret, cleanup);
David Sedláka82ecff2019-08-16 10:51:48 +02003485 }
David Sedlákb1a78352019-06-28 16:16:29 +02003486 }
3487
David Sedlákb0ca07d2019-09-11 11:54:05 +02003488cleanup:
3489 FREE_ARRAY(ctx, attrs, free_arg_rec);
3490 return ret;
David Sedlák1e2cdd02019-06-27 14:17:43 +02003491}
3492
3493LY_ERR
David Sedlák4f03b932019-07-26 13:01:47 +02003494yin_parse_mod(struct yin_parser_ctx *ctx, struct yin_arg_record *mod_attrs, const char **data, struct lysp_module *mod)
David Sedlák3b4db242018-10-19 16:11:01 +02003495{
David Sedlák81497a32019-08-13 16:56:26 +02003496 LY_ERR ret = LY_SUCCESS;
3497 struct yin_subelement *subelems = NULL;
David Sedláked809f12019-09-13 10:15:11 +02003498 struct lysp_submodule *dup;
David Sedlák3b4db242018-10-19 16:11:01 +02003499
Radek Krejcid6b76452019-09-03 17:03:03 +02003500 LY_CHECK_RET(yin_parse_attribute(ctx, mod_attrs, YIN_ARG_NAME, &mod->mod->name, Y_IDENTIF_ARG, LY_STMT_MODULE));
David Sedlák81497a32019-08-13 16:56:26 +02003501 LY_CHECK_RET(subelems_allocator(ctx, 28, NULL, &subelems,
Radek Krejcid6b76452019-09-03 17:03:03 +02003502 LY_STMT_ANYDATA, &mod->data, YIN_SUBELEM_VER2,
3503 LY_STMT_ANYXML, &mod->data, 0,
3504 LY_STMT_AUGMENT, &mod->augments, 0,
3505 LY_STMT_CHOICE, &mod->data, 0,
3506 LY_STMT_CONTACT, &mod->mod->contact, YIN_SUBELEM_UNIQUE,
3507 LY_STMT_CONTAINER, &mod->data, 0,
3508 LY_STMT_DESCRIPTION, &mod->mod->dsc, YIN_SUBELEM_UNIQUE,
3509 LY_STMT_DEVIATION, &mod->deviations, 0,
3510 LY_STMT_EXTENSION, &mod->extensions, 0,
3511 LY_STMT_FEATURE, &mod->features, 0,
3512 LY_STMT_GROUPING, &mod->groupings, 0,
3513 LY_STMT_IDENTITY, &mod->identities, 0,
3514 LY_STMT_IMPORT, mod->mod->prefix, &mod->imports, 0,
3515 LY_STMT_INCLUDE, mod->mod->name, &mod->includes, 0,
3516 LY_STMT_LEAF, &mod->data, 0,
3517 LY_STMT_LEAF_LIST, &mod->data, 0,
3518 LY_STMT_LIST, &mod->data, 0,
3519 LY_STMT_NAMESPACE, &mod->mod->ns, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE,
3520 LY_STMT_NOTIFICATION, &mod->notifs, 0,
3521 LY_STMT_ORGANIZATION, &mod->mod->org, YIN_SUBELEM_UNIQUE,
3522 LY_STMT_PREFIX, &mod->mod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE,
3523 LY_STMT_REFERENCE, &mod->mod->ref, YIN_SUBELEM_UNIQUE,
3524 LY_STMT_REVISION, &mod->revs, 0,
3525 LY_STMT_RPC, &mod->rpcs, 0,
3526 LY_STMT_TYPEDEF, &mod->typedefs, 0,
3527 LY_STMT_USES, &mod->data, 0,
Radek Krejcicd1c5e32019-09-06 16:28:42 +02003528 LY_STMT_YANG_VERSION, &mod->mod->version, YIN_SUBELEM_UNIQUE,
Radek Krejcid6b76452019-09-03 17:03:03 +02003529 LY_STMT_EXTENSION_INSTANCE, NULL, 0
David Sedlák81497a32019-08-13 16:56:26 +02003530 ));
3531
Radek Krejcid6b76452019-09-03 17:03:03 +02003532 ret = yin_parse_content(ctx, subelems, 28, data, LY_STMT_MODULE, NULL, &mod->exts);
David Sedlák81497a32019-08-13 16:56:26 +02003533 subelems_deallocator(28, subelems);
David Sedláked809f12019-09-13 10:15:11 +02003534 LY_CHECK_RET(ret);
David Sedlák81497a32019-08-13 16:56:26 +02003535
David Sedláked809f12019-09-13 10:15:11 +02003536 /* finalize parent pointers to the reallocated items */
3537 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, mod->groupings, mod->augments, mod->rpcs, mod->notifs));
3538
3539 /* submodules share the namespace with the module names, so there must not be
3540 * a submodule of the same name in the context, no need for revision matching */
3541 dup = ly_ctx_get_submodule(ctx->xml_ctx.ctx, NULL, mod->mod->name, NULL);
3542 if (dup) {
3543 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LYVE_SYNTAX_YANG, "Name collision between module and submodule of name \"%s\".", mod->mod->name);
3544 return LY_EVALID;
3545 }
3546
3547 return LY_SUCCESS;
David Sedlák3b4db242018-10-19 16:11:01 +02003548}
3549
3550LY_ERR
David Sedlák298ff6d2019-07-26 14:29:03 +02003551yin_parse_submod(struct yin_parser_ctx *ctx, struct yin_arg_record *mod_attrs, const char **data, struct lysp_submodule *submod)
3552{
David Sedlák81497a32019-08-13 16:56:26 +02003553 LY_ERR ret = LY_SUCCESS;
3554 struct yin_subelement *subelems = NULL;
David Sedláked809f12019-09-13 10:15:11 +02003555 struct lysp_submodule *dup;
David Sedlák298ff6d2019-07-26 14:29:03 +02003556
Radek Krejcid6b76452019-09-03 17:03:03 +02003557 LY_CHECK_RET(yin_parse_attribute(ctx, mod_attrs, YIN_ARG_NAME, &submod->name, Y_IDENTIF_ARG, LY_STMT_SUBMODULE));
David Sedlák81497a32019-08-13 16:56:26 +02003558 LY_CHECK_RET(subelems_allocator(ctx, 27, NULL, &subelems,
Radek Krejcid6b76452019-09-03 17:03:03 +02003559 LY_STMT_ANYDATA, &submod->data, YIN_SUBELEM_VER2,
3560 LY_STMT_ANYXML, &submod->data, 0,
3561 LY_STMT_AUGMENT, &submod->augments, 0,
3562 LY_STMT_BELONGS_TO, submod, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE,
3563 LY_STMT_CHOICE, &submod->data, 0,
3564 LY_STMT_CONTACT, &submod->contact, YIN_SUBELEM_UNIQUE,
3565 LY_STMT_CONTAINER, &submod->data, 0,
3566 LY_STMT_DESCRIPTION, &submod->dsc, YIN_SUBELEM_UNIQUE,
3567 LY_STMT_DEVIATION, &submod->deviations, 0,
3568 LY_STMT_EXTENSION, &submod->extensions, 0,
3569 LY_STMT_FEATURE, &submod->features, 0,
3570 LY_STMT_GROUPING, &submod->groupings, 0,
3571 LY_STMT_IDENTITY, &submod->identities, 0,
3572 LY_STMT_IMPORT, submod->prefix, &submod->imports, 0,
3573 LY_STMT_INCLUDE, submod->name, &submod->includes, 0,
3574 LY_STMT_LEAF, &submod->data, 0,
3575 LY_STMT_LEAF_LIST, &submod->data, 0,
3576 LY_STMT_LIST, &submod->data, 0,
3577 LY_STMT_NOTIFICATION, &submod->notifs, 0,
3578 LY_STMT_ORGANIZATION, &submod->org, YIN_SUBELEM_UNIQUE,
3579 LY_STMT_REFERENCE, &submod->ref, YIN_SUBELEM_UNIQUE,
3580 LY_STMT_REVISION, &submod->revs, 0,
3581 LY_STMT_RPC, &submod->rpcs, 0,
3582 LY_STMT_TYPEDEF, &submod->typedefs, 0,
3583 LY_STMT_USES, &submod->data, 0,
Radek Krejcicd1c5e32019-09-06 16:28:42 +02003584 LY_STMT_YANG_VERSION, &submod->version, YIN_SUBELEM_UNIQUE,
Radek Krejcid6b76452019-09-03 17:03:03 +02003585 LY_STMT_EXTENSION_INSTANCE, NULL, 0
David Sedlák81497a32019-08-13 16:56:26 +02003586 ));
3587
Radek Krejcid6b76452019-09-03 17:03:03 +02003588 ret = yin_parse_content(ctx, subelems, 27, data, LY_STMT_SUBMODULE, NULL, &submod->exts);
David Sedlák81497a32019-08-13 16:56:26 +02003589 subelems_deallocator(27, subelems);
David Sedláked809f12019-09-13 10:15:11 +02003590 LY_CHECK_RET(ret);
David Sedlák81497a32019-08-13 16:56:26 +02003591
David Sedláked809f12019-09-13 10:15:11 +02003592 /* finalize parent pointers to the reallocated items */
3593 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, submod->groupings, submod->augments, submod->rpcs, submod->notifs));
3594
3595 /* submodules share the namespace with the module names, so there must not be
3596 * a submodule of the same name in the context, no need for revision matching */
3597 dup = ly_ctx_get_submodule(ctx->xml_ctx.ctx, NULL, submod->name, NULL);
3598 if (dup && strcmp(dup->belongsto, submod->belongsto)) {
3599 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LYVE_SYNTAX_YANG, "Name collision between submodules of name \"%s\".", dup->name);
3600 return LY_EVALID;
3601 }
3602
3603 return LY_SUCCESS;
David Sedlák298ff6d2019-07-26 14:29:03 +02003604}
3605
3606LY_ERR
David Sedlák1b623122019-08-05 15:27:49 +02003607yin_parse_submodule(struct yin_parser_ctx **yin_ctx, struct ly_ctx *ctx, struct lys_parser_ctx *main_ctx, const char *data, struct lysp_submodule **submod)
David Sedlák8985a142019-07-31 16:43:06 +02003608{
Radek Krejcid6b76452019-09-03 17:03:03 +02003609 enum ly_stmt kw = LY_STMT_NONE;
David Sedlák8985a142019-07-31 16:43:06 +02003610 LY_ERR ret = LY_SUCCESS;
3611 const char *prefix, *name;
3612 size_t prefix_len, name_len;
3613 struct yin_arg_record *attrs = NULL;
3614 struct lysp_submodule *mod_p = NULL;
3615
3616 /* create context */
3617 *yin_ctx = calloc(1, sizeof **yin_ctx);
David Sedlák1b623122019-08-05 15:27:49 +02003618 LY_CHECK_ERR_RET(!(*yin_ctx), LOGMEM(ctx), LY_EMEM);
David Sedlák8985a142019-07-31 16:43:06 +02003619 (*yin_ctx)->xml_ctx.ctx = ctx;
Radek Krejci12b1c152019-09-05 16:20:48 +02003620 (*yin_ctx)->pos_type = LY_VLOG_LINE;
David Sedlák8985a142019-07-31 16:43:06 +02003621 (*yin_ctx)->xml_ctx.line = 1;
3622
David Sedlák1b623122019-08-05 15:27:49 +02003623 /* map the typedefs and groupings list from main context to the submodule's context */
3624 memcpy(&(*yin_ctx)->tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
3625 memcpy(&(*yin_ctx)->grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes);
3626
David Sedlák8985a142019-07-31 16:43:06 +02003627 /* check submodule */
3628 ret = lyxml_get_element(&(*yin_ctx)->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
3629 LY_CHECK_GOTO(ret, cleanup);
3630 ret = yin_load_attributes(*yin_ctx, &data, &attrs);
3631 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcid6b76452019-09-03 17:03:03 +02003632 kw = yin_match_keyword(*yin_ctx, name, name_len, prefix, prefix_len, LY_STMT_NONE);
David Sedlák8985a142019-07-31 16:43:06 +02003633
Radek Krejcid6b76452019-09-03 17:03:03 +02003634 if (kw == LY_STMT_MODULE) {
David Sedlák8985a142019-07-31 16:43:06 +02003635 LOGERR(ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
3636 ret = LY_EINVAL;
3637 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02003638 } else if (kw != LY_STMT_SUBMODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02003639 LOGVAL_PARSER((struct lys_parser_ctx *)*yin_ctx, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
David Sedlák8985a142019-07-31 16:43:06 +02003640 ret = LY_EVALID;
3641 goto cleanup;
3642 }
3643
3644 mod_p = calloc(1, sizeof *mod_p);
3645 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx), cleanup);
3646 mod_p->parsing = 1;
3647
3648 ret = yin_parse_submod(*yin_ctx, attrs, &data, mod_p);
3649 LY_CHECK_GOTO(ret, cleanup);
3650
David Sedlák6d781b62019-08-02 15:22:52 +02003651 name = NULL;
David Sedlákbf8a2b72019-08-14 16:48:10 +02003652 /* skip possible trailing whitespaces at end of the input */
3653 while(*data && isspace(*data)) {
3654 data++;
David Sedlák6d781b62019-08-02 15:22:52 +02003655 }
David Sedlákbf8a2b72019-08-14 16:48:10 +02003656 if (*data) {
David Sedlák1538a842019-08-08 15:38:51 +02003657 LOGVAL_PARSER((struct lys_parser_ctx *)*yin_ctx, LY_VCODE_TRAILING_SUBMOD, 15, data, strlen(data) > 15 ? "..." : "");
David Sedlák6d781b62019-08-02 15:22:52 +02003658 ret = LY_EVALID;
3659 goto cleanup;
3660 }
3661
David Sedlák8985a142019-07-31 16:43:06 +02003662 mod_p->parsing = 0;
3663 *submod = mod_p;
3664
3665cleanup:
3666 if (ret) {
3667 lysp_submodule_free(ctx, mod_p);
David Sedlák1b623122019-08-05 15:27:49 +02003668 yin_parser_ctx_free(*yin_ctx);
3669 *yin_ctx = NULL;
David Sedlák8985a142019-07-31 16:43:06 +02003670 }
3671
3672 FREE_ARRAY(*yin_ctx, attrs, free_arg_rec);
3673 return ret;
3674}
3675
3676LY_ERR
3677yin_parse_module(struct yin_parser_ctx **yin_ctx, const char *data, struct lys_module *mod)
David Sedlák3b4db242018-10-19 16:11:01 +02003678{
David Sedláke4889912018-11-02 09:52:40 +01003679 LY_ERR ret = LY_SUCCESS;
Radek Krejcid6b76452019-09-03 17:03:03 +02003680 enum ly_stmt kw = LY_STMT_NONE;
David Sedlák3017da42019-02-15 09:48:04 +01003681 struct lysp_module *mod_p = NULL;
3682 const char *prefix, *name;
3683 size_t prefix_len, name_len;
David Sedlák1f90d252019-07-10 17:09:32 +02003684 struct yin_arg_record *attrs = NULL;
David Sedlák3b4db242018-10-19 16:11:01 +02003685
David Sedlák8985a142019-07-31 16:43:06 +02003686 /* create context */
3687 *yin_ctx = calloc(1, sizeof **yin_ctx);
David Sedlák1b623122019-08-05 15:27:49 +02003688 LY_CHECK_ERR_RET(!(*yin_ctx), LOGMEM(mod->ctx), LY_EMEM);
David Sedlák8985a142019-07-31 16:43:06 +02003689 (*yin_ctx)->xml_ctx.ctx = mod->ctx;
Radek Krejci335332a2019-09-05 13:03:35 +02003690 (*yin_ctx)->pos_type = LY_VLOG_LINE;
David Sedlák8985a142019-07-31 16:43:06 +02003691 (*yin_ctx)->xml_ctx.line = 1;
David Sedlákda8ffa32019-07-08 14:17:10 +02003692
David Sedlák8985a142019-07-31 16:43:06 +02003693 /* check module */
3694 ret = lyxml_get_element(&(*yin_ctx)->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlák00250342019-06-21 14:19:39 +02003695 LY_CHECK_GOTO(ret, cleanup);
David Sedlák8985a142019-07-31 16:43:06 +02003696 ret = yin_load_attributes(*yin_ctx, &data, &attrs);
David Sedlák00250342019-06-21 14:19:39 +02003697 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcid6b76452019-09-03 17:03:03 +02003698 kw = yin_match_keyword(*yin_ctx, name, name_len, prefix, prefix_len, LY_STMT_NONE);
3699 if (kw == LY_STMT_SUBMODULE) {
David Sedlák8985a142019-07-31 16:43:06 +02003700 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 +01003701 ret = LY_EINVAL;
3702 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02003703 } else if (kw != LY_STMT_MODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02003704 LOGVAL_PARSER((struct lys_parser_ctx *)*yin_ctx, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
David Sedlák3017da42019-02-15 09:48:04 +01003705 ret = LY_EVALID;
3706 goto cleanup;
David Sedláke4889912018-11-02 09:52:40 +01003707 }
3708
David Sedlák3017da42019-02-15 09:48:04 +01003709 /* allocate module */
3710 mod_p = calloc(1, sizeof *mod_p);
David Sedlák8985a142019-07-31 16:43:06 +02003711 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(mod->ctx), cleanup);
David Sedlák3017da42019-02-15 09:48:04 +01003712 mod_p->mod = mod;
3713 mod_p->parsing = 1;
David Sedláke4889912018-11-02 09:52:40 +01003714
David Sedlák00250342019-06-21 14:19:39 +02003715 /* parse module substatements */
David Sedlák8985a142019-07-31 16:43:06 +02003716 ret = yin_parse_mod(*yin_ctx, attrs, &data, mod_p);
David Sedlák3017da42019-02-15 09:48:04 +01003717 LY_CHECK_GOTO(ret, cleanup);
David Sedlák2e411422018-12-17 02:35:39 +01003718
David Sedlákbf8a2b72019-08-14 16:48:10 +02003719 name = NULL;
3720 /* skip possible trailing whitespaces at end of the input */
3721 while(*data && isspace(*data)) {
3722 data++;
David Sedlák6d781b62019-08-02 15:22:52 +02003723 }
David Sedlákbf8a2b72019-08-14 16:48:10 +02003724 if (*data) {
David Sedlák1538a842019-08-08 15:38:51 +02003725 LOGVAL_PARSER((struct lys_parser_ctx *)*yin_ctx, LY_VCODE_TRAILING_MOD, 15, data, strlen(data) > 15 ? "..." : "");
David Sedlák6d781b62019-08-02 15:22:52 +02003726 ret = LY_EVALID;
3727 goto cleanup;
3728 }
3729
David Sedlák3017da42019-02-15 09:48:04 +01003730 mod_p->parsing = 0;
3731 mod->parsed = mod_p;
3732
3733cleanup:
David Sedlák8f7a1172019-06-20 14:42:18 +02003734 if (ret != LY_SUCCESS) {
David Sedlák3017da42019-02-15 09:48:04 +01003735 lysp_module_free(mod_p);
David Sedlák1b623122019-08-05 15:27:49 +02003736 yin_parser_ctx_free(*yin_ctx);
3737 *yin_ctx = NULL;
David Sedlák3017da42019-02-15 09:48:04 +01003738 }
David Sedlák8985a142019-07-31 16:43:06 +02003739 FREE_ARRAY(*yin_ctx, attrs, free_arg_rec);
David Sedlák2e411422018-12-17 02:35:39 +01003740 return ret;
David Sedlák3b4db242018-10-19 16:11:01 +02003741}