blob: 47810a00b9c1d3a9d7327c72ba54d2b72955bb47 [file] [log] [blame]
Radek Krejci335332a2019-09-05 13:03:35 +02001/**
Michal Vasko59892dd2022-05-13 11:02:30 +02002 * @file parser_common.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief libyang common parser functions.
Radek Krejci335332a2019-09-05 13:03:35 +02005 *
Michal Vasko59892dd2022-05-13 11:02:30 +02006 * Copyright (c) 2015 - 2022 CESNET, z.s.p.o.
Radek Krejci335332a2019-09-05 13:03:35 +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
13 */
14
Michal Vasko59892dd2022-05-13 11:02:30 +020015#define _GNU_SOURCE
16#define _POSIX_C_SOURCE 200809L /* strdup, strndup */
17
18#ifdef __APPLE__
19#define _DARWIN_C_SOURCE /* F_GETPATH */
20#endif
21
22#if defined (__NetBSD__) || defined (__OpenBSD__)
23/* realpath */
24#define _XOPEN_SOURCE 1
25#define _XOPEN_SOURCE_EXTENDED 1
26#endif
27
28#include "parser_internal.h"
29
Radek Krejci335332a2019-09-05 13:03:35 +020030#include <assert.h>
31#include <ctype.h>
32#include <errno.h>
Michal Vasko59892dd2022-05-13 11:02:30 +020033#include <fcntl.h>
34#include <limits.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020035#include <stdint.h>
Michal Vasko59892dd2022-05-13 11:02:30 +020036#include <stdio.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020037#include <stdlib.h>
38#include <string.h>
Michal Vasko59892dd2022-05-13 11:02:30 +020039#include <unistd.h>
Radek Krejci335332a2019-09-05 13:03:35 +020040
Michal Vasko59892dd2022-05-13 11:02:30 +020041#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020042#include "dict.h"
Michal Vasko59892dd2022-05-13 11:02:30 +020043#include "in_internal.h"
Radek Krejci47fab892020-11-05 17:02:41 +010044#include "log.h"
Michal Vasko8f702ee2024-02-20 15:44:24 +010045#include "ly_common.h"
Michal Vasko59892dd2022-05-13 11:02:30 +020046#include "parser_data.h"
Michal Vasko69730152020-10-09 16:30:07 +020047#include "path.h"
Michal Vaskob4750962022-10-06 15:33:35 +020048#include "plugins_exts/metadata.h"
Michal Vasko820efe82023-05-12 15:47:43 +020049#include "schema_compile_node.h"
Michal Vasko193dacd2022-10-13 08:43:05 +020050#include "schema_features.h"
Radek Krejci77114102021-03-10 15:21:57 +010051#include "set.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020052#include "tree.h"
Michal Vasko59892dd2022-05-13 11:02:30 +020053#include "tree_data.h"
54#include "tree_data_internal.h"
Radek Krejci335332a2019-09-05 13:03:35 +020055#include "tree_schema.h"
56#include "tree_schema_internal.h"
57
Michal Vasko59892dd2022-05-13 11:02:30 +020058void
59lyd_ctx_free(struct lyd_ctx *lydctx)
60{
61 ly_set_erase(&lydctx->node_types, NULL);
62 ly_set_erase(&lydctx->meta_types, NULL);
63 ly_set_erase(&lydctx->node_when, NULL);
Michal Vasko135719f2022-08-25 12:18:17 +020064 ly_set_erase(&lydctx->ext_node, free);
Michal Vasko59892dd2022-05-13 11:02:30 +020065 ly_set_erase(&lydctx->ext_val, free);
66}
67
68LY_ERR
Michal Vasko820efe82023-05-12 15:47:43 +020069lyd_parser_notif_eventtime_validate(const struct lyd_node *node)
70{
71 LY_ERR rc = LY_SUCCESS;
72 struct ly_ctx *ctx = (struct ly_ctx *)LYD_CTX(node);
73 struct lysc_ctx cctx;
74 const struct lys_module *mod;
75 LY_ARRAY_COUNT_TYPE u;
76 struct ly_err_item *err = NULL;
77 struct lysp_type *type_p = NULL;
78 struct lysc_pattern **patterns = NULL;
79 const char *value;
80
81 LYSC_CTX_INIT_CTX(cctx, ctx);
82
83 /* get date-and-time parsed type */
84 mod = ly_ctx_get_module_latest(ctx, "ietf-yang-types");
85 assert(mod);
86 LY_ARRAY_FOR(mod->parsed->typedefs, u) {
87 if (!strcmp(mod->parsed->typedefs[u].name, "date-and-time")) {
88 type_p = &mod->parsed->typedefs[u].type;
89 break;
90 }
91 }
92 assert(type_p);
93
94 /* compile patterns */
95 assert(type_p->patterns);
96 LY_CHECK_GOTO(rc = lys_compile_type_patterns(&cctx, type_p->patterns, NULL, &patterns), cleanup);
97
98 /* validate */
99 value = lyd_get_value(node);
100 rc = lyplg_type_validate_patterns(patterns, value, strlen(value), &err);
101
102cleanup:
103 FREE_ARRAY(&cctx.free_ctx, patterns, lysc_pattern_free);
104 if (rc && err) {
105 LOGVAL_ERRITEM(ctx, err);
106 ly_err_free(err);
107 LOGVAL(ctx, LYVE_DATA, "Invalid \"eventTime\" in the notification.");
108 }
109 return rc;
110}
111
112LY_ERR
Michal Vasko59892dd2022-05-13 11:02:30 +0200113lyd_parser_find_operation(const struct lyd_node *parent, uint32_t int_opts, struct lyd_node **op)
114{
115 const struct lyd_node *iter;
116
117 *op = NULL;
118
119 if (!parent) {
120 /* no parent, nothing to look for */
121 return LY_SUCCESS;
122 }
123
124 /* we need to find the operation node if it already exists */
125 for (iter = parent; iter; iter = lyd_parent(iter)) {
126 if (iter->schema && (iter->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))) {
127 break;
128 }
129 }
130
131 if (!iter) {
132 /* no operation found */
133 return LY_SUCCESS;
134 }
135
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100136 if (!(int_opts & LYD_INTOPT_ANY)) {
137 if (!(int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY))) {
138 LOGERR(LYD_CTX(parent), LY_EINVAL, "Invalid parent %s \"%s\" node when not parsing any operation.",
139 lys_nodetype2str(iter->schema->nodetype), iter->schema->name);
140 return LY_EINVAL;
141 } else if ((iter->schema->nodetype == LYS_RPC) && !(int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_REPLY))) {
142 LOGERR(LYD_CTX(parent), LY_EINVAL, "Invalid parent RPC \"%s\" node when not parsing RPC nor rpc-reply.",
143 iter->schema->name);
144 return LY_EINVAL;
145 } else if ((iter->schema->nodetype == LYS_ACTION) && !(int_opts & (LYD_INTOPT_ACTION | LYD_INTOPT_REPLY))) {
146 LOGERR(LYD_CTX(parent), LY_EINVAL, "Invalid parent action \"%s\" node when not parsing action nor rpc-reply.",
147 iter->schema->name);
148 return LY_EINVAL;
149 } else if ((iter->schema->nodetype == LYS_NOTIF) && !(int_opts & LYD_INTOPT_NOTIF)) {
150 LOGERR(LYD_CTX(parent), LY_EINVAL, "Invalid parent notification \"%s\" node when not parsing a notification.",
151 iter->schema->name);
152 return LY_EINVAL;
153 }
Michal Vasko59892dd2022-05-13 11:02:30 +0200154 }
155
156 *op = (struct lyd_node *)iter;
157 return LY_SUCCESS;
158}
159
Michal Vaskoa878a892023-08-18 12:22:07 +0200160const struct lysc_node *
161lyd_parser_node_schema(const struct lyd_node *node)
162{
163 uint32_t i;
164 const struct lyd_node *iter;
165 const struct lysc_node *schema = NULL;
166 const struct lys_module *mod;
167
168 if (!node) {
169 return NULL;
170 } else if (node->schema) {
171 /* simplest case */
172 return node->schema;
173 }
174
175 /* find the first schema node in the parsed nodes */
176 i = ly_log_location_dnode_count();
177 if (i) {
178 do {
179 --i;
180 if (ly_log_location_dnode(i)->schema) {
181 /* this node is processed */
182 schema = ly_log_location_dnode(i)->schema;
183 ++i;
184 break;
185 }
186 } while (i);
187 }
188
189 /* get schema node of an opaque node */
190 do {
191 /* get next data node */
192 if (i == ly_log_location_dnode_count()) {
193 iter = node;
194 } else {
195 iter = ly_log_location_dnode(i);
196 }
197 assert(!iter->schema);
198
199 /* get module */
Michal Vasko420cc252023-08-24 08:14:24 +0200200 mod = lyd_node_module(iter);
Michal Vaskoa878a892023-08-18 12:22:07 +0200201 if (!mod) {
202 /* unknown module, no schema node */
203 schema = NULL;
204 break;
205 }
206
207 /* get schema node */
208 schema = lys_find_child(schema, mod, LYD_NAME(iter), 0, 0, 0);
209
210 /* move to the descendant */
211 ++i;
212 } while (schema && (iter != node));
213
214 return schema;
215}
216
Michal Vasko59892dd2022-05-13 11:02:30 +0200217LY_ERR
218lyd_parser_check_schema(struct lyd_ctx *lydctx, const struct lysc_node *snode)
219{
220 LY_ERR rc = LY_SUCCESS;
221
222 LOG_LOCSET(snode, NULL, NULL, NULL);
223
224 if (lydctx->int_opts & LYD_INTOPT_ANY) {
225 /* nothing to check, everything is allowed */
226 goto cleanup;
227 }
228
229 if ((lydctx->parse_opts & LYD_PARSE_NO_STATE) && (snode->flags & LYS_CONFIG_R)) {
230 LOGVAL(lydctx->data_ctx->ctx, LY_VCODE_UNEXPNODE, "state", snode->name);
231 rc = LY_EVALID;
232 goto cleanup;
233 }
234
235 if (snode->nodetype == LYS_RPC) {
236 if (lydctx->int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_REPLY)) {
237 if (lydctx->op_node) {
238 goto error_node_dup;
239 }
240 } else {
241 goto error_node_inval;
242 }
243 } else if (snode->nodetype == LYS_ACTION) {
244 if (lydctx->int_opts & (LYD_INTOPT_ACTION | LYD_INTOPT_REPLY)) {
245 if (lydctx->op_node) {
246 goto error_node_dup;
247 }
248 } else {
249 goto error_node_inval;
250 }
251 } else if (snode->nodetype == LYS_NOTIF) {
252 if (lydctx->int_opts & LYD_INTOPT_NOTIF) {
253 if (lydctx->op_node) {
254 goto error_node_dup;
255 }
256 } else {
257 goto error_node_inval;
258 }
259 }
260
261 /* success */
262 goto cleanup;
263
264error_node_dup:
265 LOGVAL(lydctx->data_ctx->ctx, LYVE_DATA, "Unexpected %s element \"%s\", %s \"%s\" already parsed.",
266 lys_nodetype2str(snode->nodetype), snode->name, lys_nodetype2str(lydctx->op_node->schema->nodetype),
267 lydctx->op_node->schema->name);
268 rc = LY_EVALID;
269 goto cleanup;
270
271error_node_inval:
272 LOGVAL(lydctx->data_ctx->ctx, LYVE_DATA, "Unexpected %s element \"%s\".", lys_nodetype2str(snode->nodetype),
273 snode->name);
274 rc = LY_EVALID;
275
276cleanup:
277 LOG_LOCBACK(1, 0, 0, 0);
278 return rc;
279}
280
281LY_ERR
282lyd_parser_create_term(struct lyd_ctx *lydctx, const struct lysc_node *schema, const void *value, size_t value_len,
283 ly_bool *dynamic, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, struct lyd_node **node)
284{
Michal Vasko9dbb91d2023-01-30 13:59:22 +0100285 LY_ERR r;
Michal Vasko59892dd2022-05-13 11:02:30 +0200286 ly_bool incomplete;
287
Michal Vasko989cdb42023-10-06 15:32:37 +0200288 if ((r = lyd_create_term(schema, value, value_len, 1, dynamic, format, prefix_data, hints, &incomplete, node))) {
Michal Vasko9dbb91d2023-01-30 13:59:22 +0100289 if (lydctx->data_ctx->ctx != schema->module->ctx) {
290 /* move errors to the main context */
291 ly_err_move(schema->module->ctx, (struct ly_ctx *)lydctx->data_ctx->ctx);
292 }
293 return r;
294 }
Michal Vasko59892dd2022-05-13 11:02:30 +0200295
296 if (incomplete && !(lydctx->parse_opts & LYD_PARSE_ONLY)) {
297 LY_CHECK_RET(ly_set_add(&lydctx->node_types, *node, 1, NULL));
298 }
299 return LY_SUCCESS;
300}
301
302LY_ERR
303lyd_parser_create_meta(struct lyd_ctx *lydctx, struct lyd_node *parent, struct lyd_meta **meta, const struct lys_module *mod,
304 const char *name, size_t name_len, const void *value, size_t value_len, ly_bool *dynamic, LY_VALUE_FORMAT format,
305 void *prefix_data, uint32_t hints, const struct lysc_node *ctx_node)
306{
Michal Vaskodd03ff12023-09-11 10:30:48 +0200307 LY_ERR rc = LY_SUCCESS;
308 char *dpath = NULL, *path = NULL;
Michal Vasko59892dd2022-05-13 11:02:30 +0200309 ly_bool incomplete;
310 struct lyd_meta *first = NULL;
311
312 if (meta && *meta) {
313 /* remember the first metadata */
314 first = *meta;
315 }
316
Michal Vaskodd03ff12023-09-11 10:30:48 +0200317 /* generate path to the metadata */
318 LY_CHECK_RET(ly_vlog_build_data_path(lydctx->data_ctx->ctx, &dpath));
319 if (asprintf(&path, "%s/@%s:%.*s", dpath, mod->name, (int)name_len, name) == -1) {
320 LOGMEM(lydctx->data_ctx->ctx);
321 rc = LY_EMEM;
322 goto cleanup;
323 }
324 LOG_LOCSET(NULL, NULL, path, NULL);
325
Michal Vasko989cdb42023-10-06 15:32:37 +0200326 LY_CHECK_GOTO(rc = lyd_create_meta(parent, meta, mod, name, name_len, value, value_len, 1, dynamic, format,
327 prefix_data, hints, ctx_node, 0, &incomplete), cleanup);
Michal Vasko59892dd2022-05-13 11:02:30 +0200328
329 if (incomplete && !(lydctx->parse_opts & LYD_PARSE_ONLY)) {
Michal Vaskodd03ff12023-09-11 10:30:48 +0200330 LY_CHECK_GOTO(rc = ly_set_add(&lydctx->meta_types, *meta, 1, NULL), cleanup);
Michal Vasko59892dd2022-05-13 11:02:30 +0200331 }
332
333 if (first) {
334 /* always return the first metadata */
335 *meta = first;
336 }
337
Michal Vaskodd03ff12023-09-11 10:30:48 +0200338cleanup:
339 LOG_LOCBACK(0, 0, 1, 0);
340 free(dpath);
341 free(path);
342 return rc;
Michal Vasko59892dd2022-05-13 11:02:30 +0200343}
344
Michal Vasko95c6d5c2022-05-20 10:33:39 +0200345LY_ERR
346lyd_parse_check_keys(struct lyd_node *node)
347{
348 const struct lysc_node *skey = NULL;
349 const struct lyd_node *key;
350
351 assert(node->schema->nodetype == LYS_LIST);
352
353 key = lyd_child(node);
354 while ((skey = lys_getnext(skey, node->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
355 if (!key || (key->schema != skey)) {
356 LOGVAL(LYD_CTX(node), LY_VCODE_NOKEY, skey->name);
357 return LY_EVALID;
358 }
359
360 key = key->next;
361 }
362
363 return LY_SUCCESS;
364}
365
366LY_ERR
367lyd_parse_set_data_flags(struct lyd_node *node, struct lyd_meta **meta, struct lyd_ctx *lydctx,
368 struct lysc_ext_instance *ext)
369{
370 struct lyd_meta *meta2, *prev_meta = NULL;
371 struct lyd_ctx_ext_val *ext_val;
372
Michal Vaskobc9f76f2022-12-01 10:31:38 +0100373 if (lydctx->parse_opts & LYD_PARSE_NO_NEW) {
374 node->flags &= ~LYD_NEW;
375 }
376
Michal Vasko95c6d5c2022-05-20 10:33:39 +0200377 if (lysc_has_when(node->schema)) {
Michal Vaskofbb48c22022-11-30 13:28:03 +0100378 if (lydctx->parse_opts & LYD_PARSE_WHEN_TRUE) {
379 /* the condition was true before */
380 node->flags |= LYD_WHEN_TRUE;
381 }
Michal Vasko95c6d5c2022-05-20 10:33:39 +0200382 if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) {
383 /* remember we need to evaluate this node's when */
384 LY_CHECK_RET(ly_set_add(&lydctx->node_when, node, 1, NULL));
385 }
386 }
387
388 LY_LIST_FOR(*meta, meta2) {
389 if (!strcmp(meta2->name, "default") && !strcmp(meta2->annotation->module->name, "ietf-netconf-with-defaults") &&
390 meta2->value.boolean) {
391 /* node is default according to the metadata */
392 node->flags |= LYD_DEFAULT;
393
394 /* delete the metadata */
395 if (prev_meta) {
396 prev_meta->next = meta2->next;
Michal Vasko4754d4a2022-12-01 10:11:21 +0100397 } else if (meta != &node->meta) {
Michal Vasko95c6d5c2022-05-20 10:33:39 +0200398 *meta = (*meta)->next;
399 }
400 lyd_free_meta_single(meta2);
Michal Vasko4754d4a2022-12-01 10:11:21 +0100401
402 /* update dflt flag for all parent NP containers */
403 lyd_cont_set_dflt(lyd_parent(node));
Michal Vasko95c6d5c2022-05-20 10:33:39 +0200404 break;
405 }
406
407 prev_meta = meta2;
408 }
409
410 if (ext) {
411 /* parsed for an extension */
412 node->flags |= LYD_EXT;
413
414 if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) {
415 /* rememeber for validation */
416 ext_val = malloc(sizeof *ext_val);
417 LY_CHECK_ERR_RET(!ext_val, LOGMEM(LYD_CTX(node)), LY_EMEM);
418 ext_val->ext = ext;
419 ext_val->sibling = node;
420 LY_CHECK_RET(ly_set_add(&lydctx->ext_val, ext_val, 1, NULL));
421 }
422 }
423
424 return LY_SUCCESS;
425}
426
Michal Vasko193dacd2022-10-13 08:43:05 +0200427void
428lys_parser_fill_filepath(struct ly_ctx *ctx, struct ly_in *in, const char **filepath)
429{
430 char path[PATH_MAX];
431
432#ifndef __APPLE__
433 char proc_path[32];
434 int len;
435#endif
436
437 LY_CHECK_ARG_RET(NULL, ctx, in, filepath, );
438 if (*filepath) {
439 /* filepath already set */
440 return;
441 }
442
443 switch (in->type) {
444 case LY_IN_FILEPATH:
445 if (realpath(in->method.fpath.filepath, path) != NULL) {
446 lydict_insert(ctx, path, 0, filepath);
447 } else {
448 lydict_insert(ctx, in->method.fpath.filepath, 0, filepath);
449 }
450
451 break;
452 case LY_IN_FD:
453#ifdef __APPLE__
454 if (fcntl(in->method.fd, F_GETPATH, path) != -1) {
455 lydict_insert(ctx, path, 0, filepath);
456 }
457#elif defined _WIN32
458 HANDLE h = _get_osfhandle(in->method.fd);
459 FILE_NAME_INFO info;
Michal Vasko2bf4af42023-01-04 12:08:38 +0100460
Michal Vasko193dacd2022-10-13 08:43:05 +0200461 if (GetFileInformationByHandleEx(h, FileNameInfo, &info, sizeof info)) {
462 char *buf = calloc(info.FileNameLength + 1 /* trailing NULL */, MB_CUR_MAX);
Michal Vasko2bf4af42023-01-04 12:08:38 +0100463
Michal Vasko193dacd2022-10-13 08:43:05 +0200464 len = wcstombs(buf, info.FileName, info.FileNameLength * MB_CUR_MAX);
465 lydict_insert(ctx, buf, len, filepath);
466 }
467#else
468 /* get URI if there is /proc */
469 sprintf(proc_path, "/proc/self/fd/%d", in->method.fd);
470 if ((len = readlink(proc_path, path, PATH_MAX - 1)) > 0) {
471 lydict_insert(ctx, path, len, filepath);
472 }
473#endif
474 break;
475 case LY_IN_MEMORY:
476 case LY_IN_FILE:
477 /* nothing to do */
478 break;
479 default:
480 LOGINT(ctx);
481 break;
482 }
483}
484
Michal Vaskod0625d72022-10-06 15:02:50 +0200485static LY_ERR lysp_stmt_container(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent,
Radek Krejci76c8c4e2021-02-17 10:16:48 +0100486 struct lysp_node **siblings);
Michal Vaskod0625d72022-10-06 15:02:50 +0200487static LY_ERR lysp_stmt_choice(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent,
Radek Krejci76c8c4e2021-02-17 10:16:48 +0100488 struct lysp_node **siblings);
Michal Vaskod0625d72022-10-06 15:02:50 +0200489static LY_ERR lysp_stmt_case(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent,
Radek Krejci76c8c4e2021-02-17 10:16:48 +0100490 struct lysp_node **siblings);
Michal Vaskod0625d72022-10-06 15:02:50 +0200491static LY_ERR lysp_stmt_uses(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent,
Radek Krejci76c8c4e2021-02-17 10:16:48 +0100492 struct lysp_node **siblings);
Michal Vaskod0625d72022-10-06 15:02:50 +0200493static LY_ERR lysp_stmt_grouping(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent,
Radek Krejci76c8c4e2021-02-17 10:16:48 +0100494 struct lysp_node_grp **groupings);
Michal Vaskod0625d72022-10-06 15:02:50 +0200495static LY_ERR lysp_stmt_list(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent,
Radek Krejci76c8c4e2021-02-17 10:16:48 +0100496 struct lysp_node **siblings);
497
Michal Vasko5d10b9b2022-12-14 12:14:20 +0100498/**
499 * @brief Validate stmt string value.
500 *
501 * @param[in] ctx Parser context.
502 * @param[in] val_type String value type.
503 * @param[in] val Value to validate.
504 * @return LY_ERR value.
505 */
Radek Krejci335332a2019-09-05 13:03:35 +0200506static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200507lysp_stmt_validate_value(struct lysp_ctx *ctx, enum yang_arg val_type, const char *val)
Radek Krejci335332a2019-09-05 13:03:35 +0200508{
Radek Krejci857189e2020-09-01 13:26:36 +0200509 uint8_t prefix = 0;
510 ly_bool first = 1;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200511 uint32_t c;
Radek Krejci335332a2019-09-05 13:03:35 +0200512 size_t utf8_char_len;
513
Michal Vaskocfa1a962023-02-27 09:28:45 +0100514 if (!val) {
515 if (val_type == Y_MAYBE_STR_ARG) {
516 /* fine */
517 return LY_SUCCESS;
518 }
519
520 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Missing an expected string.");
521 return LY_EVALID;
522 }
523
Radek Krejci335332a2019-09-05 13:03:35 +0200524 while (*val) {
525 LY_CHECK_ERR_RET(ly_getutf8(&val, &c, &utf8_char_len),
Michal Vasko69730152020-10-09 16:30:07 +0200526 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, (val)[-utf8_char_len]), LY_EVALID);
Radek Krejci335332a2019-09-05 13:03:35 +0200527
528 switch (val_type) {
529 case Y_IDENTIF_ARG:
530 LY_CHECK_RET(lysp_check_identifierchar(ctx, c, first, NULL));
531 break;
532 case Y_PREF_IDENTIF_ARG:
533 LY_CHECK_RET(lysp_check_identifierchar(ctx, c, first, &prefix));
534 break;
535 case Y_STR_ARG:
536 case Y_MAYBE_STR_ARG:
537 LY_CHECK_RET(lysp_check_stringchar(ctx, c));
538 break;
539 }
540 first = 0;
541 }
542
543 return LY_SUCCESS;
544}
545
546/**
Michal Vasko5d10b9b2022-12-14 12:14:20 +0100547 * @brief Duplicate statement siblings, recursively.
548 *
549 * @param[in] ctx Parser context.
550 * @param[in] stmt Statement to duplicate.
551 * @param[out] first First duplicated statement, the rest follow.
552 * @return LY_ERR value.
553 */
554static LY_ERR
555lysp_stmt_dup(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_stmt **first)
556{
557 struct lysp_stmt *child, *last = NULL;
558
559 LY_LIST_FOR(stmt, stmt) {
560 child = calloc(1, sizeof *child);
561 LY_CHECK_ERR_RET(!child, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
562
563 if (last) {
564 last->next = child;
565 } else {
566 assert(!*first);
567 *first = child;
568 }
569 last = child;
570
571 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->stmt, 0, &child->stmt));
572 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &child->arg));
573 child->format = stmt->format;
574 LY_CHECK_RET(ly_dup_prefix_data(PARSER_CTX(ctx), stmt->format, stmt->prefix_data, &child->prefix_data));
575 child->flags = stmt->flags;
576 child->kw = stmt->kw;
577
578 /* recursively */
579 LY_CHECK_RET(lysp_stmt_dup(ctx, stmt->child, &child->child));
580 }
581
582 return LY_SUCCESS;
583}
584
585/**
Radek Krejci335332a2019-09-05 13:03:35 +0200586 * @brief Parse extension instance.
587 *
Radek Krejci76c8c4e2021-02-17 10:16:48 +0100588 * @param[in] ctx parser context.
589 * @param[in] stmt Source statement data from the parsed extension instance.
Radek Krejcifc596f92021-02-26 22:40:26 +0100590 * @param[in] insubstmt The statement this extension instance is a substatement of.
Radek Krejci335332a2019-09-05 13:03:35 +0200591 * @param[in] insubstmt_index Index of the keyword instance this extension instance is a substatement of.
592 * @param[in,out] exts Extension instances to add to.
Radek Krejci335332a2019-09-05 13:03:35 +0200593 * @return LY_ERR values.
594 */
595static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200596lysp_stmt_ext(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, enum ly_stmt insubstmt,
Radek Krejci0f969882020-08-21 16:56:47 +0200597 LY_ARRAY_COUNT_TYPE insubstmt_index, struct lysp_ext_instance **exts)
Radek Krejci335332a2019-09-05 13:03:35 +0200598{
599 struct lysp_ext_instance *e;
600
Michal Vaskob36053d2020-03-26 15:49:30 +0100601 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *exts, e, LY_EMEM);
Radek Krejci335332a2019-09-05 13:03:35 +0200602
603 /* store name and insubstmt info */
Radek Krejci011e4aa2020-09-04 15:22:31 +0200604 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->stmt, 0, &e->name));
Radek Krejciab430862021-03-02 20:13:40 +0100605 e->parent_stmt = insubstmt;
606 e->parent_stmt_index = insubstmt_index;
aPiecek60d9d672021-04-27 15:49:57 +0200607 e->parsed = NULL;
Michal Vasko5d10b9b2022-12-14 12:14:20 +0100608 LY_CHECK_RET(lysp_stmt_dup(ctx, stmt->child, &e->child));
Radek Krejci335332a2019-09-05 13:03:35 +0200609
610 /* get optional argument */
611 if (stmt->arg) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200612 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &e->argument));
Radek Krejci335332a2019-09-05 13:03:35 +0200613 }
614
615 return LY_SUCCESS;
616}
617
618/**
619 * @brief Parse a generic text field without specific constraints. Those are contact, organization,
620 * description, etc...
621 *
Radek Krejci76c8c4e2021-02-17 10:16:48 +0100622 * @param[in] ctx parser context.
623 * @param[in] stmt Source statement data from the parsed extension instance.
Radek Krejci335332a2019-09-05 13:03:35 +0200624 * @param[in] substmt_index Index of this substatement.
625 * @param[in,out] value Place to store the parsed value.
626 * @param[in] arg Type of the YANG keyword argument (of the value).
627 * @param[in,out] exts Extension instances to add to.
628 *
629 * @return LY_ERR values.
630 */
631static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200632lysp_stmt_text_field(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, uint32_t substmt_index, const char **value,
633 enum yang_arg arg, struct lysp_ext_instance **exts)
Radek Krejci335332a2019-09-05 13:03:35 +0200634{
Radek Krejci335332a2019-09-05 13:03:35 +0200635 if (*value) {
Michal Vasko193dacd2022-10-13 08:43:05 +0200636 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(stmt->kw));
Radek Krejci335332a2019-09-05 13:03:35 +0200637 return LY_EVALID;
638 }
639
640 LY_CHECK_RET(lysp_stmt_validate_value(ctx, arg, stmt->arg));
Radek Krejci011e4aa2020-09-04 15:22:31 +0200641 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, value));
Radek Krejci335332a2019-09-05 13:03:35 +0200642
Radek Krejci76c8c4e2021-02-17 10:16:48 +0100643 for (const struct lysp_stmt *child = stmt->child; child; child = child->next) {
644 switch (child->kw) {
Radek Krejci335332a2019-09-05 13:03:35 +0200645 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci76c8c4e2021-02-17 10:16:48 +0100646 LY_CHECK_RET(lysp_stmt_ext(ctx, child, stmt->kw, substmt_index, exts));
Radek Krejci335332a2019-09-05 13:03:35 +0200647 break;
648 default:
Michal Vasko193dacd2022-10-13 08:43:05 +0200649 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), lyplg_ext_stmt2str(stmt->kw));
Radek Krejci335332a2019-09-05 13:03:35 +0200650 return LY_EVALID;
651 }
652 }
653 return LY_SUCCESS;
654}
655
656/**
Michal Vasko7f45cf22020-10-01 12:49:44 +0200657 * @brief Parse a qname that can have more instances such as if-feature.
658 *
Radek Krejci76c8c4e2021-02-17 10:16:48 +0100659 * @param[in] ctx parser context.
660 * @param[in] stmt Source statement data from the parsed extension instance.
Michal Vasko7f45cf22020-10-01 12:49:44 +0200661 * @param[in,out] qnames Parsed qnames to add to.
662 * @param[in] arg Type of the expected argument.
663 * @param[in,out] exts Extension instances to add to.
664 *
665 * @return LY_ERR values.
666 */
667static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200668lysp_stmt_qnames(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_qname **qnames, enum yang_arg arg,
669 struct lysp_ext_instance **exts)
Michal Vasko7f45cf22020-10-01 12:49:44 +0200670{
671 struct lysp_qname *item;
Michal Vasko7f45cf22020-10-01 12:49:44 +0200672
673 LY_CHECK_RET(lysp_stmt_validate_value(ctx, arg, stmt->arg));
674
675 /* allocate new pointer */
676 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *qnames, item, LY_EMEM);
677 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &item->str));
Michal Vasko8a67eff2021-12-07 14:04:47 +0100678 item->mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200679
Radek Krejci76c8c4e2021-02-17 10:16:48 +0100680 for (const struct lysp_stmt *child = stmt->child; child; child = child->next) {
681 switch (child->kw) {
Michal Vasko7f45cf22020-10-01 12:49:44 +0200682 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci76c8c4e2021-02-17 10:16:48 +0100683 LY_CHECK_RET(lysp_stmt_ext(ctx, child, stmt->kw, LY_ARRAY_COUNT(*qnames) - 1, exts));
Michal Vasko7f45cf22020-10-01 12:49:44 +0200684 break;
685 default:
Michal Vasko193dacd2022-10-13 08:43:05 +0200686 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), lyplg_ext_stmt2str(stmt->kw));
Michal Vasko7f45cf22020-10-01 12:49:44 +0200687 return LY_EVALID;
688 }
689 }
690 return LY_SUCCESS;
691}
692
693/**
Radek Krejci335332a2019-09-05 13:03:35 +0200694 * @brief Parse a generic text field that can have more instances such as base.
695 *
Radek Krejci76c8c4e2021-02-17 10:16:48 +0100696 * @param[in] ctx parser context.
697 * @param[in] stmt Source statement data from the parsed extension instance.
Radek Krejci335332a2019-09-05 13:03:35 +0200698 * @param[in,out] texts Parsed values to add to.
699 * @param[in] arg Type of the expected argument.
700 * @param[in,out] exts Extension instances to add to.
701 *
702 * @return LY_ERR values.
703 */
704static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200705lysp_stmt_text_fields(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, const char ***texts, enum yang_arg arg,
706 struct lysp_ext_instance **exts)
Radek Krejci335332a2019-09-05 13:03:35 +0200707{
708 const char **item;
Radek Krejci335332a2019-09-05 13:03:35 +0200709
710 LY_CHECK_RET(lysp_stmt_validate_value(ctx, arg, stmt->arg));
711
712 /* allocate new pointer */
Michal Vaskob36053d2020-03-26 15:49:30 +0100713 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *texts, item, LY_EMEM);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200714 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, item));
Radek Krejci335332a2019-09-05 13:03:35 +0200715
Radek Krejci76c8c4e2021-02-17 10:16:48 +0100716 for (const struct lysp_stmt *child = stmt->child; child; child = child->next) {
717 switch (child->kw) {
Radek Krejci335332a2019-09-05 13:03:35 +0200718 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci76c8c4e2021-02-17 10:16:48 +0100719 LY_CHECK_RET(lysp_stmt_ext(ctx, child, stmt->kw, LY_ARRAY_COUNT(*texts) - 1, exts));
Radek Krejci335332a2019-09-05 13:03:35 +0200720 break;
721 default:
Michal Vasko193dacd2022-10-13 08:43:05 +0200722 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), lyplg_ext_stmt2str(stmt->kw));
Radek Krejci335332a2019-09-05 13:03:35 +0200723 return LY_EVALID;
724 }
725 }
726 return LY_SUCCESS;
727}
728
729/**
730 * @brief Parse the status statement.
731 *
Radek Krejci76c8c4e2021-02-17 10:16:48 +0100732 * @param[in] ctx parser context.
733 * @param[in] stmt Source statement data from the parsed extension instance.
Radek Krejci335332a2019-09-05 13:03:35 +0200734 * @param[in,out] flags Flags to add to.
735 * @param[in,out] exts Extension instances to add to.
736 *
737 * @return LY_ERR values.
738 */
739static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200740lysp_stmt_status(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, uint16_t *flags, struct lysp_ext_instance **exts)
Radek Krejci335332a2019-09-05 13:03:35 +0200741{
Michal Vasko7b3a00e2023-08-09 11:58:03 +0200742 int arg_len;
Radek Krejci335332a2019-09-05 13:03:35 +0200743
744 if (*flags & LYS_STATUS_MASK) {
745 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status");
746 return LY_EVALID;
747 }
748
749 LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_STR_ARG, stmt->arg));
750 arg_len = strlen(stmt->arg);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100751 if ((arg_len == ly_strlen_const("current")) && !strncmp(stmt->arg, "current", arg_len)) {
Radek Krejci335332a2019-09-05 13:03:35 +0200752 *flags |= LYS_STATUS_CURR;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100753 } else if ((arg_len == ly_strlen_const("deprecated")) && !strncmp(stmt->arg, "deprecated", arg_len)) {
Radek Krejci335332a2019-09-05 13:03:35 +0200754 *flags |= LYS_STATUS_DEPRC;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100755 } else if ((arg_len == ly_strlen_const("obsolete")) && !strncmp(stmt->arg, "obsolete", arg_len)) {
Radek Krejci335332a2019-09-05 13:03:35 +0200756 *flags |= LYS_STATUS_OBSLT;
757 } else {
758 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, "status");
759 return LY_EVALID;
760 }
761
Radek Krejci76c8c4e2021-02-17 10:16:48 +0100762 for (const struct lysp_stmt *child = stmt->child; child; child = child->next) {
763 switch (child->kw) {
Radek Krejci335332a2019-09-05 13:03:35 +0200764 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +0100765 LY_CHECK_RET(lysp_stmt_ext(ctx, child, LY_STMT_STATUS, 0, exts));
Radek Krejci335332a2019-09-05 13:03:35 +0200766 break;
767 default:
Michal Vasko193dacd2022-10-13 08:43:05 +0200768 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), "status");
Radek Krejci335332a2019-09-05 13:03:35 +0200769 return LY_EVALID;
770 }
771 }
772 return LY_SUCCESS;
773}
774
775/**
Radek Krejci76c8c4e2021-02-17 10:16:48 +0100776 * @brief Parse the when statement.
Radek Krejci335332a2019-09-05 13:03:35 +0200777 *
Radek Krejci76c8c4e2021-02-17 10:16:48 +0100778 * @param[in] ctx parser context.
779 * @param[in] stmt Source statement data from the parsed extension instance.
780 * @param[in,out] when_p When pointer to parse to.
781 *
782 * @return LY_ERR values.
783 */
784static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200785lysp_stmt_when(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_when **when_p)
Radek Krejci76c8c4e2021-02-17 10:16:48 +0100786{
787 LY_ERR ret = LY_SUCCESS;
788 struct lysp_when *when;
789
790 if (*when_p) {
791 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "when");
792 return LY_EVALID;
793 }
794
795 LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_STR_ARG, stmt->arg));
796
797 when = calloc(1, sizeof *when);
798 LY_CHECK_ERR_RET(!when, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
799 *when_p = when;
800
801 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &when->cond));
802
803 for (const struct lysp_stmt *child = stmt->child; child; child = child->next) {
804 switch (child->kw) {
805 case LY_STMT_DESCRIPTION:
806 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &when->dsc, Y_STR_ARG, &when->exts));
807 break;
808 case LY_STMT_REFERENCE:
809 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &when->ref, Y_STR_ARG, &when->exts));
810 break;
811 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +0100812 LY_CHECK_RET(lysp_stmt_ext(ctx, child, LY_STMT_WHEN, 0, &when->exts));
Radek Krejci76c8c4e2021-02-17 10:16:48 +0100813 break;
814 default:
Michal Vasko193dacd2022-10-13 08:43:05 +0200815 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), "when");
Radek Krejci76c8c4e2021-02-17 10:16:48 +0100816 return LY_EVALID;
817 }
818 }
819 return ret;
820}
821
822/**
823 * @brief Parse the config statement.
824 *
825 * @param[in] ctx parser context.
826 * @param[in] stmt Source statement data from the parsed extension instance.
827 * @param[in,out] flags Flags to add to.
Radek Krejci335332a2019-09-05 13:03:35 +0200828 * @param[in,out] exts Extension instances to add to.
829 *
830 * @return LY_ERR values.
831 */
832static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200833lysp_stmt_config(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, uint16_t *flags, struct lysp_ext_instance **exts)
Radek Krejci335332a2019-09-05 13:03:35 +0200834{
Michal Vasko7b3a00e2023-08-09 11:58:03 +0200835 int arg_len;
Radek Krejci335332a2019-09-05 13:03:35 +0200836
Radek Krejci76c8c4e2021-02-17 10:16:48 +0100837 if (*flags & LYS_CONFIG_MASK) {
838 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "config");
839 return LY_EVALID;
840 }
841
842 LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_STR_ARG, stmt->arg));
843 arg_len = strlen(stmt->arg);
844 if ((arg_len == ly_strlen_const("true")) && !strncmp(stmt->arg, "true", arg_len)) {
845 *flags |= LYS_CONFIG_W;
846 } else if ((arg_len == ly_strlen_const("false")) && !strncmp(stmt->arg, "false", arg_len)) {
847 *flags |= LYS_CONFIG_R;
848 } else {
849 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, "config");
850 return LY_EVALID;
851 }
852
853 for (const struct lysp_stmt *child = stmt->child; child; child = child->next) {
854 switch (child->kw) {
855 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +0100856 LY_CHECK_RET(lysp_stmt_ext(ctx, child, LY_STMT_CONFIG, 0, exts));
Radek Krejci76c8c4e2021-02-17 10:16:48 +0100857 break;
858 default:
Michal Vasko193dacd2022-10-13 08:43:05 +0200859 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), "config");
Radek Krejci76c8c4e2021-02-17 10:16:48 +0100860 return LY_EVALID;
861 }
862 }
863
864 return LY_SUCCESS;
865}
866
867/**
868 * @brief Parse the mandatory statement.
869 *
870 * @param[in] ctx parser context.
871 * @param[in] stmt Source statement data from the parsed extension instance.
872 * @param[in,out] flags Flags to add to.
873 * @param[in,out] exts Extension instances to add to.
874 *
875 * @return LY_ERR values.
876 */
877static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200878lysp_stmt_mandatory(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, uint16_t *flags,
Michal Vasko59892dd2022-05-13 11:02:30 +0200879 struct lysp_ext_instance **exts)
Radek Krejci76c8c4e2021-02-17 10:16:48 +0100880{
Michal Vasko7b3a00e2023-08-09 11:58:03 +0200881 int arg_len;
Radek Krejci76c8c4e2021-02-17 10:16:48 +0100882
883 if (*flags & LYS_MAND_MASK) {
884 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "mandatory");
885 return LY_EVALID;
886 }
887
888 LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_STR_ARG, stmt->arg));
889 arg_len = strlen(stmt->arg);
890 if ((arg_len == ly_strlen_const("true")) && !strncmp(stmt->arg, "true", arg_len)) {
891 *flags |= LYS_MAND_TRUE;
892 } else if ((arg_len == ly_strlen_const("false")) && !strncmp(stmt->arg, "false", arg_len)) {
893 *flags |= LYS_MAND_FALSE;
894 } else {
895 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, "mandatory");
896 return LY_EVALID;
897 }
898
899 for (const struct lysp_stmt *child = stmt->child; child; child = child->next) {
900 switch (child->kw) {
901 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +0100902 LY_CHECK_RET(lysp_stmt_ext(ctx, child, LY_STMT_MANDATORY, 0, exts));
Radek Krejci76c8c4e2021-02-17 10:16:48 +0100903 break;
904 default:
Michal Vasko193dacd2022-10-13 08:43:05 +0200905 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), "mandatory");
Radek Krejci76c8c4e2021-02-17 10:16:48 +0100906 return LY_EVALID;
907 }
908 }
909
910 return LY_SUCCESS;
911}
912
913/**
914 * @brief Parse a restriction such as range or length.
915 *
916 * @param[in] ctx parser context.
917 * @param[in] stmt Source statement data from the parsed extension instance.
918 * @param[in,out] exts Extension instances to add to.
919 *
920 * @return LY_ERR values.
921 */
922static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200923lysp_stmt_restr(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_restr *restr)
Radek Krejci76c8c4e2021-02-17 10:16:48 +0100924{
Radek Krejci335332a2019-09-05 13:03:35 +0200925 LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_STR_ARG, stmt->arg));
Michal Vasko7f45cf22020-10-01 12:49:44 +0200926 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &restr->arg.str));
Michal Vasko8a67eff2021-12-07 14:04:47 +0100927 restr->arg.mod = PARSER_CUR_PMOD(ctx);
Radek Krejci335332a2019-09-05 13:03:35 +0200928
Radek Krejci76c8c4e2021-02-17 10:16:48 +0100929 for (const struct lysp_stmt *child = stmt->child; child; child = child->next) {
930 switch (child->kw) {
Radek Krejci335332a2019-09-05 13:03:35 +0200931 case LY_STMT_DESCRIPTION:
Radek Krejci76c8c4e2021-02-17 10:16:48 +0100932 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &restr->dsc, Y_STR_ARG, &restr->exts));
Radek Krejci335332a2019-09-05 13:03:35 +0200933 break;
934 case LY_STMT_REFERENCE:
Radek Krejci76c8c4e2021-02-17 10:16:48 +0100935 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &restr->ref, Y_STR_ARG, &restr->exts));
Radek Krejci335332a2019-09-05 13:03:35 +0200936 break;
937 case LY_STMT_ERROR_APP_TAG:
Radek Krejci76c8c4e2021-02-17 10:16:48 +0100938 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &restr->eapptag, Y_STR_ARG, &restr->exts));
Radek Krejci335332a2019-09-05 13:03:35 +0200939 break;
940 case LY_STMT_ERROR_MESSAGE:
Radek Krejci76c8c4e2021-02-17 10:16:48 +0100941 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &restr->emsg, Y_STR_ARG, &restr->exts));
Radek Krejci335332a2019-09-05 13:03:35 +0200942 break;
943 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +0100944 LY_CHECK_RET(lysp_stmt_ext(ctx, child, stmt->kw, 0, &restr->exts));
Radek Krejci335332a2019-09-05 13:03:35 +0200945 break;
946 default:
Michal Vasko193dacd2022-10-13 08:43:05 +0200947 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), lyplg_ext_stmt2str(stmt->kw));
Radek Krejci335332a2019-09-05 13:03:35 +0200948 return LY_EVALID;
949 }
950 }
951 return LY_SUCCESS;
952}
953
954/**
955 * @brief Parse a restriction that can have more instances such as must.
956 *
Radek Krejci76c8c4e2021-02-17 10:16:48 +0100957 * @param[in] ctx parser context.
958 * @param[in] stmt Source statement data from the parsed extension instance.
Radek Krejci335332a2019-09-05 13:03:35 +0200959 * @param[in,out] restrs Restrictions to add to.
960 *
961 * @return LY_ERR values.
962 */
963static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200964lysp_stmt_restrs(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_restr **restrs)
Radek Krejci335332a2019-09-05 13:03:35 +0200965{
966 struct lysp_restr *restr;
967
Michal Vaskob36053d2020-03-26 15:49:30 +0100968 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *restrs, restr, LY_EMEM);
Radek Krejci76c8c4e2021-02-17 10:16:48 +0100969 return lysp_stmt_restr(ctx, stmt, restr);
970}
971
972/**
973 * @brief Parse the anydata or anyxml statement.
974 *
975 * @param[in] ctx parser context.
976 * @param[in] stmt Source statement data from the parsed extension instance.
977 * @param[in,out] siblings Siblings to add to.
978 *
979 * @return LY_ERR values.
980 */
981static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200982lysp_stmt_any(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, struct lysp_node **siblings)
Radek Krejci76c8c4e2021-02-17 10:16:48 +0100983{
984 struct lysp_node_anydata *any;
985
986 LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_IDENTIF_ARG, stmt->arg));
987
988 /* create new structure and insert into siblings */
989 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, any, next, LY_EMEM);
990
991 any->nodetype = stmt->kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
992 any->parent = parent;
993
994 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &any->name));
995
996 /* parse substatements */
997 for (const struct lysp_stmt *child = stmt->child; child; child = child->next) {
998 switch (child->kw) {
999 case LY_STMT_CONFIG:
1000 LY_CHECK_RET(lysp_stmt_config(ctx, child, &any->flags, &any->exts));
1001 break;
1002 case LY_STMT_DESCRIPTION:
1003 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &any->dsc, Y_STR_ARG, &any->exts));
1004 break;
1005 case LY_STMT_IF_FEATURE:
1006 LY_CHECK_RET(lysp_stmt_qnames(ctx, child, &any->iffeatures, Y_STR_ARG, &any->exts));
1007 break;
1008 case LY_STMT_MANDATORY:
1009 LY_CHECK_RET(lysp_stmt_mandatory(ctx, child, &any->flags, &any->exts));
1010 break;
1011 case LY_STMT_MUST:
1012 LY_CHECK_RET(lysp_stmt_restrs(ctx, child, &any->musts));
1013 break;
1014 case LY_STMT_REFERENCE:
1015 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &any->ref, Y_STR_ARG, &any->exts));
1016 break;
1017 case LY_STMT_STATUS:
1018 LY_CHECK_RET(lysp_stmt_status(ctx, child, &any->flags, &any->exts));
1019 break;
1020 case LY_STMT_WHEN:
1021 LY_CHECK_RET(lysp_stmt_when(ctx, child, &any->when));
1022 break;
1023 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001024 LY_CHECK_RET(lysp_stmt_ext(ctx, child, stmt->kw, 0, &any->exts));
Radek Krejci76c8c4e2021-02-17 10:16:48 +01001025 break;
1026 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001027 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw),
1028 (any->nodetype & LYS_ANYDATA) == LYS_ANYDATA ? lyplg_ext_stmt2str(LY_STMT_ANYDATA) : lyplg_ext_stmt2str(LY_STMT_ANYXML));
Radek Krejci76c8c4e2021-02-17 10:16:48 +01001029 return LY_EVALID;
1030 }
1031 }
1032
1033 return LY_SUCCESS;
Radek Krejci335332a2019-09-05 13:03:35 +02001034}
1035
1036/**
1037 * @brief Parse the value or position statement. Substatement of type enum statement.
1038 *
Radek Krejci76c8c4e2021-02-17 10:16:48 +01001039 * @param[in] ctx parser context.
1040 * @param[in] stmt Source statement data from the parsed extension instance.
Radek Krejci335332a2019-09-05 13:03:35 +02001041 * @param[in,out] value Value to write to.
1042 * @param[in,out] flags Flags to write to.
1043 * @param[in,out] exts Extension instances to add to.
1044 *
1045 * @return LY_ERR values.
1046 */
1047static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001048lysp_stmt_type_enum_value_pos(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, int64_t *value, uint16_t *flags,
Radek Krejci0f969882020-08-21 16:56:47 +02001049 struct lysp_ext_instance **exts)
Radek Krejci335332a2019-09-05 13:03:35 +02001050{
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001051 int arg_len;
Radek Krejci335332a2019-09-05 13:03:35 +02001052 char *ptr = NULL;
Michal Vasko2bf4af42023-01-04 12:08:38 +01001053 long long num = 0;
1054 unsigned long long unum = 0;
Radek Krejci335332a2019-09-05 13:03:35 +02001055
1056 if (*flags & LYS_SET_VALUE) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001057 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(stmt->kw));
Radek Krejci335332a2019-09-05 13:03:35 +02001058 return LY_EVALID;
1059 }
1060 *flags |= LYS_SET_VALUE;
1061
1062 LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_STR_ARG, stmt->arg));
1063
1064 arg_len = strlen(stmt->arg);
Radek Krejci76c8c4e2021-02-17 10:16:48 +01001065 if (!arg_len || (stmt->arg[0] == '+') || ((stmt->arg[0] == '0') && (arg_len > 1)) ||
1066 ((stmt->kw == LY_STMT_POSITION) && !strncmp(stmt->arg, "-0", 2))) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001067 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, lyplg_ext_stmt2str(stmt->kw));
Radek Krejci335332a2019-09-05 13:03:35 +02001068 goto error;
1069 }
1070
1071 errno = 0;
Radek Krejci76c8c4e2021-02-17 10:16:48 +01001072 if (stmt->kw == LY_STMT_VALUE) {
Michal Vasko73d77ab2021-07-23 12:45:55 +02001073 num = strtoll(stmt->arg, &ptr, LY_BASE_DEC);
Michal Vasko69730152020-10-09 16:30:07 +02001074 if ((num < INT64_C(-2147483648)) || (num > INT64_C(2147483647))) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001075 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, lyplg_ext_stmt2str(stmt->kw));
Radek Krejci335332a2019-09-05 13:03:35 +02001076 goto error;
1077 }
1078 } else {
Michal Vasko73d77ab2021-07-23 12:45:55 +02001079 unum = strtoull(stmt->arg, &ptr, LY_BASE_DEC);
Radek Krejci335332a2019-09-05 13:03:35 +02001080 if (unum > UINT64_C(4294967295)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001081 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, lyplg_ext_stmt2str(stmt->kw));
Radek Krejci335332a2019-09-05 13:03:35 +02001082 goto error;
1083 }
1084 }
1085 /* we have not parsed the whole argument */
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001086 if (ptr - stmt->arg != arg_len) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001087 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, lyplg_ext_stmt2str(stmt->kw));
Radek Krejci335332a2019-09-05 13:03:35 +02001088 goto error;
1089 }
1090 if (errno == ERANGE) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001091 LOGVAL_PARSER(ctx, LY_VCODE_OOB, arg_len, stmt->arg, lyplg_ext_stmt2str(stmt->kw));
Radek Krejci335332a2019-09-05 13:03:35 +02001092 goto error;
1093 }
Radek Krejci76c8c4e2021-02-17 10:16:48 +01001094 if (stmt->kw == LY_STMT_VALUE) {
Radek Krejci335332a2019-09-05 13:03:35 +02001095 *value = num;
1096 } else {
1097 *value = unum;
1098 }
1099
Radek Krejci76c8c4e2021-02-17 10:16:48 +01001100 for (const struct lysp_stmt *child = stmt->child; child; child = child->next) {
1101 switch (child->kw) {
Radek Krejci335332a2019-09-05 13:03:35 +02001102 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001103 LY_CHECK_RET(lysp_stmt_ext(ctx, child, stmt->kw == LY_STMT_VALUE ? LY_STMT_VALUE : LY_STMT_POSITION, 0, exts));
Radek Krejci335332a2019-09-05 13:03:35 +02001104 break;
1105 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001106 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), lyplg_ext_stmt2str(stmt->kw));
Radek Krejci335332a2019-09-05 13:03:35 +02001107 return LY_EVALID;
1108 }
1109 }
1110 return LY_SUCCESS;
1111
1112error:
1113 return LY_EVALID;
1114}
1115
1116/**
1117 * @brief Parse the enum or bit statement. Substatement of type statement.
1118 *
Radek Krejci76c8c4e2021-02-17 10:16:48 +01001119 * @param[in] ctx parser context.
1120 * @param[in] stmt Source statement data from the parsed extension instance.
Radek Krejci335332a2019-09-05 13:03:35 +02001121 * @param[in,out] enums Enums or bits to add to.
1122 *
1123 * @return LY_ERR values.
1124 */
1125static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001126lysp_stmt_type_enum(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_type_enum **enums)
Radek Krejci335332a2019-09-05 13:03:35 +02001127{
1128 struct lysp_type_enum *enm;
Radek Krejci335332a2019-09-05 13:03:35 +02001129
Radek Krejci76c8c4e2021-02-17 10:16:48 +01001130 LY_CHECK_RET(lysp_stmt_validate_value(ctx, stmt->kw == LY_STMT_ENUM ? Y_STR_ARG : Y_IDENTIF_ARG, stmt->arg));
Radek Krejci335332a2019-09-05 13:03:35 +02001131
Michal Vaskob36053d2020-03-26 15:49:30 +01001132 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *enums, enm, LY_EMEM);
Radek Krejci335332a2019-09-05 13:03:35 +02001133
Radek Krejci76c8c4e2021-02-17 10:16:48 +01001134 if (stmt->kw == LY_STMT_ENUM) {
Radek Krejci335332a2019-09-05 13:03:35 +02001135 LY_CHECK_RET(lysp_check_enum_name(ctx, stmt->arg, strlen(stmt->arg)));
1136 } /* else nothing specific for YANG_BIT */
1137
Radek Krejci011e4aa2020-09-04 15:22:31 +02001138 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &enm->name));
Michal Vasko193dacd2022-10-13 08:43:05 +02001139 CHECK_UNIQUENESS(ctx, *enums, name, lyplg_ext_stmt2str(stmt->kw), enm->name);
Radek Krejci335332a2019-09-05 13:03:35 +02001140
Radek Krejci76c8c4e2021-02-17 10:16:48 +01001141 for (const struct lysp_stmt *child = stmt->child; child; child = child->next) {
1142 switch (child->kw) {
Radek Krejci335332a2019-09-05 13:03:35 +02001143 case LY_STMT_DESCRIPTION:
Radek Krejci76c8c4e2021-02-17 10:16:48 +01001144 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &enm->dsc, Y_STR_ARG, &enm->exts));
Radek Krejci335332a2019-09-05 13:03:35 +02001145 break;
1146 case LY_STMT_IF_FEATURE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001147 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", lyplg_ext_stmt2str(stmt->kw));
Radek Krejci76c8c4e2021-02-17 10:16:48 +01001148 LY_CHECK_RET(lysp_stmt_qnames(ctx, child, &enm->iffeatures, Y_STR_ARG, &enm->exts));
Radek Krejci335332a2019-09-05 13:03:35 +02001149 break;
1150 case LY_STMT_REFERENCE:
Radek Krejci76c8c4e2021-02-17 10:16:48 +01001151 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &enm->ref, Y_STR_ARG, &enm->exts));
Radek Krejci335332a2019-09-05 13:03:35 +02001152 break;
1153 case LY_STMT_STATUS:
1154 LY_CHECK_RET(lysp_stmt_status(ctx, child, &enm->flags, &enm->exts));
1155 break;
1156 case LY_STMT_VALUE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001157 LY_CHECK_ERR_RET(stmt->kw == LY_STMT_BIT, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw),
1158 lyplg_ext_stmt2str(stmt->kw)), LY_EVALID);
Radek Krejci76c8c4e2021-02-17 10:16:48 +01001159 LY_CHECK_RET(lysp_stmt_type_enum_value_pos(ctx, child, &enm->value, &enm->flags, &enm->exts));
Radek Krejci335332a2019-09-05 13:03:35 +02001160 break;
1161 case LY_STMT_POSITION:
Michal Vasko193dacd2022-10-13 08:43:05 +02001162 LY_CHECK_ERR_RET(stmt->kw == LY_STMT_ENUM, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw),
1163 lyplg_ext_stmt2str(stmt->kw)), LY_EVALID);
Radek Krejci76c8c4e2021-02-17 10:16:48 +01001164 LY_CHECK_RET(lysp_stmt_type_enum_value_pos(ctx, child, &enm->value, &enm->flags, &enm->exts));
Radek Krejci335332a2019-09-05 13:03:35 +02001165 break;
1166 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001167 LY_CHECK_RET(lysp_stmt_ext(ctx, child, stmt->kw, 0, &enm->exts));
Radek Krejci335332a2019-09-05 13:03:35 +02001168 break;
1169 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001170 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), lyplg_ext_stmt2str(stmt->kw));
Radek Krejci335332a2019-09-05 13:03:35 +02001171 return LY_EVALID;
1172 }
1173 }
Michal Vasko193dacd2022-10-13 08:43:05 +02001174
Radek Krejci335332a2019-09-05 13:03:35 +02001175 return LY_SUCCESS;
1176}
1177
1178/**
1179 * @brief Parse the fraction-digits statement. Substatement of type statement.
1180 *
Radek Krejci76c8c4e2021-02-17 10:16:48 +01001181 * @param[in] ctx parser context.
1182 * @param[in] stmt Source statement data from the parsed extension instance.
Radek Krejci335332a2019-09-05 13:03:35 +02001183 * @param[in,out] fracdig Value to write to.
1184 * @param[in,out] exts Extension instances to add to.
1185 *
1186 * @return LY_ERR values.
1187 */
1188static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001189lysp_stmt_type_fracdigits(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, uint8_t *fracdig,
Michal Vasko59892dd2022-05-13 11:02:30 +02001190 struct lysp_ext_instance **exts)
Radek Krejci335332a2019-09-05 13:03:35 +02001191{
1192 char *ptr;
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001193 int arg_len;
Michal Vasko2bf4af42023-01-04 12:08:38 +01001194 unsigned long long num;
Radek Krejci335332a2019-09-05 13:03:35 +02001195
1196 if (*fracdig) {
1197 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
1198 return LY_EVALID;
1199 }
1200
1201 LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_STR_ARG, stmt->arg));
1202 arg_len = strlen(stmt->arg);
1203 if (!arg_len || (stmt->arg[0] == '0') || !isdigit(stmt->arg[0])) {
1204 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, "fraction-digits");
1205 return LY_EVALID;
1206 }
1207
1208 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02001209 num = strtoull(stmt->arg, &ptr, LY_BASE_DEC);
Radek Krejci335332a2019-09-05 13:03:35 +02001210 /* we have not parsed the whole argument */
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001211 if (ptr - stmt->arg != arg_len) {
Radek Krejci335332a2019-09-05 13:03:35 +02001212 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, "fraction-digits");
1213 return LY_EVALID;
1214 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001215 if ((errno == ERANGE) || (num > LY_TYPE_DEC64_FD_MAX)) {
Radek Krejci335332a2019-09-05 13:03:35 +02001216 LOGVAL_PARSER(ctx, LY_VCODE_OOB, arg_len, stmt->arg, "fraction-digits");
1217 return LY_EVALID;
1218 }
1219 *fracdig = num;
1220
Radek Krejci76c8c4e2021-02-17 10:16:48 +01001221 for (const struct lysp_stmt *child = stmt->child; child; child = child->next) {
1222 switch (child->kw) {
Radek Krejci335332a2019-09-05 13:03:35 +02001223 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001224 LY_CHECK_RET(lysp_stmt_ext(ctx, child, LY_STMT_FRACTION_DIGITS, 0, exts));
Radek Krejci335332a2019-09-05 13:03:35 +02001225 break;
1226 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001227 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), "fraction-digits");
Radek Krejci335332a2019-09-05 13:03:35 +02001228 return LY_EVALID;
1229 }
1230 }
1231 return LY_SUCCESS;
1232}
1233
1234/**
1235 * @brief Parse the require-instance statement. Substatement of type statement.
1236 *
Radek Krejci76c8c4e2021-02-17 10:16:48 +01001237 * @param[in] ctx parser context.
1238 * @param[in] stmt Source statement data from the parsed extension instance.
Radek Krejci335332a2019-09-05 13:03:35 +02001239 * @param[in,out] reqinst Value to write to.
1240 * @param[in,out] flags Flags to write to.
1241 * @param[in,out] exts Extension instances to add to.
1242 *
1243 * @return LY_ERR values.
1244 */
1245static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001246lysp_stmt_type_reqinstance(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, uint8_t *reqinst, uint16_t *flags,
Radek Krejci0f969882020-08-21 16:56:47 +02001247 struct lysp_ext_instance **exts)
Radek Krejci335332a2019-09-05 13:03:35 +02001248{
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001249 int arg_len;
Radek Krejci335332a2019-09-05 13:03:35 +02001250
1251 if (*flags & LYS_SET_REQINST) {
1252 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance");
1253 return LY_EVALID;
1254 }
1255 *flags |= LYS_SET_REQINST;
1256
1257 LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_STR_ARG, stmt->arg));
1258 arg_len = strlen(stmt->arg);
Radek Krejcif13b87b2020-12-01 22:02:17 +01001259 if ((arg_len == ly_strlen_const("true")) && !strncmp(stmt->arg, "true", arg_len)) {
Radek Krejci335332a2019-09-05 13:03:35 +02001260 *reqinst = 1;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001261 } else if ((arg_len != ly_strlen_const("false")) || strncmp(stmt->arg, "false", arg_len)) {
Radek Krejci335332a2019-09-05 13:03:35 +02001262 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, "require-instance");
1263 return LY_EVALID;
1264 }
1265
Radek Krejci76c8c4e2021-02-17 10:16:48 +01001266 for (const struct lysp_stmt *child = stmt->child; child; child = child->next) {
1267 switch (child->kw) {
Radek Krejci335332a2019-09-05 13:03:35 +02001268 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001269 LY_CHECK_RET(lysp_stmt_ext(ctx, child, LY_STMT_REQUIRE_INSTANCE, 0, exts));
Radek Krejci335332a2019-09-05 13:03:35 +02001270 break;
1271 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001272 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), "require-instance");
Radek Krejci335332a2019-09-05 13:03:35 +02001273 return LY_EVALID;
1274 }
1275 }
1276 return LY_SUCCESS;
1277}
1278
1279/**
1280 * @brief Parse the modifier statement. Substatement of type pattern statement.
1281 *
Radek Krejci76c8c4e2021-02-17 10:16:48 +01001282 * @param[in] ctx parser context.
1283 * @param[in] stmt Source statement data from the parsed extension instance.
Radek Krejci335332a2019-09-05 13:03:35 +02001284 * @param[in,out] pat Value to write to.
1285 * @param[in,out] exts Extension instances to add to.
Radek Krejci335332a2019-09-05 13:03:35 +02001286 * @return LY_ERR values.
1287 */
1288static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001289lysp_stmt_type_pattern_modifier(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, const char **pat,
Michal Vasko59892dd2022-05-13 11:02:30 +02001290 struct lysp_ext_instance **exts)
Radek Krejci335332a2019-09-05 13:03:35 +02001291{
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001292 int arg_len;
Radek Krejci335332a2019-09-05 13:03:35 +02001293 char *buf;
Radek Krejci335332a2019-09-05 13:03:35 +02001294
Radek Krejcif13b87b2020-12-01 22:02:17 +01001295 if ((*pat)[0] == LYSP_RESTR_PATTERN_NACK) {
Radek Krejci335332a2019-09-05 13:03:35 +02001296 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier");
1297 return LY_EVALID;
1298 }
1299
1300 LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_STR_ARG, stmt->arg));
1301 arg_len = strlen(stmt->arg);
Radek Krejcif13b87b2020-12-01 22:02:17 +01001302 if ((arg_len != ly_strlen_const("invert-match")) || strncmp(stmt->arg, "invert-match", arg_len)) {
Radek Krejci335332a2019-09-05 13:03:35 +02001303 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, "modifier");
1304 return LY_EVALID;
1305 }
1306
1307 /* replace the value in the dictionary */
1308 buf = malloc(strlen(*pat) + 1);
Michal Vaskob36053d2020-03-26 15:49:30 +01001309 LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci335332a2019-09-05 13:03:35 +02001310 strcpy(buf, *pat);
Michal Vaskob36053d2020-03-26 15:49:30 +01001311 lydict_remove(PARSER_CTX(ctx), *pat);
Radek Krejci335332a2019-09-05 13:03:35 +02001312
Radek Krejcif13b87b2020-12-01 22:02:17 +01001313 assert(buf[0] == LYSP_RESTR_PATTERN_ACK);
1314 buf[0] = LYSP_RESTR_PATTERN_NACK;
Radek Krejci011e4aa2020-09-04 15:22:31 +02001315 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, pat));
Radek Krejci335332a2019-09-05 13:03:35 +02001316
Radek Krejci76c8c4e2021-02-17 10:16:48 +01001317 for (const struct lysp_stmt *child = stmt->child; child; child = child->next) {
1318 switch (child->kw) {
Radek Krejci335332a2019-09-05 13:03:35 +02001319 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001320 LY_CHECK_RET(lysp_stmt_ext(ctx, child, LY_STMT_MODIFIER, 0, exts));
Radek Krejci335332a2019-09-05 13:03:35 +02001321 break;
1322 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001323 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), "modifier");
Radek Krejci335332a2019-09-05 13:03:35 +02001324 return LY_EVALID;
1325 }
1326 }
1327 return LY_SUCCESS;
1328}
1329
1330/**
1331 * @brief Parse the pattern statement. Substatement of type statement.
1332 *
Radek Krejci76c8c4e2021-02-17 10:16:48 +01001333 * @param[in] ctx parser context.
1334 * @param[in] stmt Source statement data from the parsed extension instance.
Radek Krejci335332a2019-09-05 13:03:35 +02001335 * @param[in,out] patterns Restrictions to add to.
1336 *
1337 * @return LY_ERR values.
1338 */
1339static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001340lysp_stmt_type_pattern(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_restr **patterns)
Radek Krejci335332a2019-09-05 13:03:35 +02001341{
1342 char *buf;
1343 size_t arg_len;
Radek Krejci335332a2019-09-05 13:03:35 +02001344 struct lysp_restr *restr;
1345
1346 LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_STR_ARG, stmt->arg));
Michal Vaskob36053d2020-03-26 15:49:30 +01001347 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *patterns, restr, LY_EMEM);
Radek Krejci335332a2019-09-05 13:03:35 +02001348 arg_len = strlen(stmt->arg);
1349
1350 /* add special meaning first byte */
1351 buf = malloc(arg_len + 2);
Michal Vaskob36053d2020-03-26 15:49:30 +01001352 LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci335332a2019-09-05 13:03:35 +02001353 memmove(buf + 1, stmt->arg, arg_len);
Radek Krejcif13b87b2020-12-01 22:02:17 +01001354 buf[0] = LYSP_RESTR_PATTERN_ACK; /* pattern's default regular-match flag */
Radek Krejci335332a2019-09-05 13:03:35 +02001355 buf[arg_len + 1] = '\0'; /* terminating NULL byte */
Michal Vasko7f45cf22020-10-01 12:49:44 +02001356 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, &restr->arg.str));
Michal Vasko8a67eff2021-12-07 14:04:47 +01001357 restr->arg.mod = PARSER_CUR_PMOD(ctx);
Radek Krejci335332a2019-09-05 13:03:35 +02001358
Radek Krejci76c8c4e2021-02-17 10:16:48 +01001359 for (const struct lysp_stmt *child = stmt->child; child; child = child->next) {
1360 switch (child->kw) {
Radek Krejci335332a2019-09-05 13:03:35 +02001361 case LY_STMT_DESCRIPTION:
Radek Krejci76c8c4e2021-02-17 10:16:48 +01001362 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &restr->dsc, Y_STR_ARG, &restr->exts));
Radek Krejci335332a2019-09-05 13:03:35 +02001363 break;
1364 case LY_STMT_REFERENCE:
Radek Krejci76c8c4e2021-02-17 10:16:48 +01001365 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &restr->ref, Y_STR_ARG, &restr->exts));
Radek Krejci335332a2019-09-05 13:03:35 +02001366 break;
1367 case LY_STMT_ERROR_APP_TAG:
Radek Krejci76c8c4e2021-02-17 10:16:48 +01001368 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &restr->eapptag, Y_STR_ARG, &restr->exts));
Radek Krejci335332a2019-09-05 13:03:35 +02001369 break;
1370 case LY_STMT_ERROR_MESSAGE:
Radek Krejci76c8c4e2021-02-17 10:16:48 +01001371 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &restr->emsg, Y_STR_ARG, &restr->exts));
Radek Krejci335332a2019-09-05 13:03:35 +02001372 break;
1373 case LY_STMT_MODIFIER:
1374 PARSER_CHECK_STMTVER2_RET(ctx, "modifier", "pattern");
Michal Vasko7f45cf22020-10-01 12:49:44 +02001375 LY_CHECK_RET(lysp_stmt_type_pattern_modifier(ctx, child, &restr->arg.str, &restr->exts));
Radek Krejci335332a2019-09-05 13:03:35 +02001376 break;
1377 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001378 LY_CHECK_RET(lysp_stmt_ext(ctx, child, LY_STMT_PATTERN, 0, &restr->exts));
Radek Krejci335332a2019-09-05 13:03:35 +02001379 break;
1380 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001381 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), "pattern");
Radek Krejci335332a2019-09-05 13:03:35 +02001382 return LY_EVALID;
1383 }
1384 }
1385 return LY_SUCCESS;
1386}
1387
1388/**
Michal Vasko193dacd2022-10-13 08:43:05 +02001389 * @brief Parse the deviate statement. Substatement of deviation statement.
1390 *
1391 * @param[in] ctx parser context.
1392 * @param[in] stmt Source statement data from the parsed extension instance.
1393 * @param[in,out] devs Array of deviates to add to.
1394 * @param[in,out] exts Extension instances to add to.
1395 * @return LY_ERR values.
1396 */
1397static LY_ERR
1398lysp_stmt_deviate(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_deviate **devs, struct lysp_ext_instance **exts)
1399{
1400 (void)stmt;
1401 (void)devs;
1402 (void)exts;
1403
1404 /* TODO */
1405 LOGERR(PARSER_CTX(ctx), LY_EINVAL, "Extension instance \"deviate\" substatement is not supported.");
1406 return LY_EINVAL;
1407}
1408
1409/**
1410 * @brief Parse the deviation statement.
1411 *
1412 * @param[in] ctx parser context.
1413 * @param[in] stmt Source statement data from the parsed extension instance.
1414 * @param[in,out] deviations Array of deviations to add to.
1415 * @return LY_ERR values.
1416 */
1417static LY_ERR
1418lysp_stmt_deviation(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_deviation **deviations)
1419{
1420 struct lysp_deviation *dev;
1421
1422 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *deviations, dev, LY_EMEM);
1423
1424 /* store nodeid */
1425 LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_STR_ARG, stmt->arg));
1426 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &dev->nodeid));
1427
1428 /* parse substatements */
1429 for (const struct lysp_stmt *child = stmt->child; child; child = child->next) {
1430 switch (child->kw) {
1431 case LY_STMT_DESCRIPTION:
1432 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &dev->dsc, Y_STR_ARG, &dev->exts));
1433 break;
1434 case LY_STMT_DEVIATE:
1435 LY_CHECK_RET(lysp_stmt_deviate(ctx, child, &dev->deviates, &dev->exts));
1436 break;
1437 case LY_STMT_REFERENCE:
1438 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &dev->ref, Y_STR_ARG, &dev->exts));
1439 break;
1440 case LY_STMT_EXTENSION_INSTANCE:
1441 LY_CHECK_RET(lysp_stmt_ext(ctx, child, stmt->kw, 0, &dev->exts));
1442 break;
1443 default:
1444 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), lyplg_ext_stmt2str(LY_STMT_DEVIATION));
1445 return LY_EVALID;
1446 }
1447 }
1448
1449 return LY_SUCCESS;
1450}
1451
1452/**
1453 * @brief Parse the yang-version statement.
1454 *
1455 * @param[in] ctx parser context.
1456 * @param[in] stmt Source statement data from the parsed extension instance.
1457 * @param[out] version Version to write to.
1458 * @param[in,out] exts Extension instances to add to.
1459 * @return LY_ERR values.
1460 */
1461static LY_ERR
1462lysp_stmt_yangver(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, uint8_t *version, struct lysp_ext_instance **exts)
1463{
1464 if (*version) {
1465 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element");
1466 return LY_EVALID;
1467 }
1468
1469 /* store flag */
1470 if (!strcmp(stmt->arg, "1")) {
1471 *version = LYS_VERSION_1_0;
1472 } else if (!strcmp(stmt->arg, "1.1")) {
1473 *version = LYS_VERSION_1_1;
1474 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001475 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)strlen(stmt->arg), stmt->arg, "yang-version");
Michal Vasko193dacd2022-10-13 08:43:05 +02001476 return LY_EVALID;
1477 }
1478
1479 /* parse substatements */
1480 for (const struct lysp_stmt *child = stmt->child; child; child = child->next) {
1481 switch (child->kw) {
1482 case LY_STMT_EXTENSION_INSTANCE:
1483 LY_CHECK_RET(lysp_stmt_ext(ctx, child, stmt->kw, 0, exts));
1484 break;
1485 default:
1486 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), lyplg_ext_stmt2str(LY_STMT_YANG_VERSION));
1487 return LY_EVALID;
1488 }
1489 }
1490
1491 return LY_SUCCESS;
1492}
1493
1494/**
1495 * @brief Parse the module statement.
1496 *
1497 * @param[in] ctx parser context.
1498 * @param[in] stmt Source statement data from the parsed extension instance.
1499 * @param[in,out] mod Module to fill.
1500 * @return LY_ERR values.
1501 */
1502static LY_ERR
1503lysp_stmt_module(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_module *mod)
1504{
1505 (void)stmt;
1506 (void)mod;
1507
1508 /* TODO */
1509 LOGERR(PARSER_CTX(ctx), LY_EINVAL, "Extension instance \"module\" substatement is not supported.");
1510 return LY_EINVAL;
1511}
1512
1513/**
1514 * @brief Parse the submodule statement.
1515 *
1516 * @param[in] ctx parser context.
1517 * @param[in] stmt Source statement data from the parsed extension instance.
1518 * @param[in,out] submod Module to fill.
1519 * @return LY_ERR values.
1520 */
1521static LY_ERR
1522lysp_stmt_submodule(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_submodule *submod)
1523{
1524 (void)stmt;
1525 (void)submod;
1526
1527 /* TODO */
1528 LOGERR(PARSER_CTX(ctx), LY_EINVAL, "Extension instance \"submodule\" substatement is not supported.");
1529 return LY_EINVAL;
1530}
1531
1532/**
1533 * @brief Parse the yin-element statement. Substatement of argument statement.
1534 *
1535 * @param[in] ctx parser context.
1536 * @param[in] stmt Source statement data from the parsed extension instance.
1537 * @param[in,out] flags Flags to write to.
1538 * @param[in,out] exts Extension instances to add to.
1539 * @return LY_ERR values.
1540 */
1541static LY_ERR
1542lysp_stmt_yinelem(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, uint16_t *flags, struct lysp_ext_instance **exts)
1543{
1544 if (*flags & LYS_YINELEM_MASK) {
1545 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element");
1546 return LY_EVALID;
1547 }
1548
1549 /* store flag */
1550 if (!strcmp(stmt->arg, "true")) {
1551 *flags |= LYS_YINELEM_TRUE;
1552 } else if (!strcmp(stmt->arg, "false")) {
1553 *flags |= LYS_YINELEM_FALSE;
1554 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001555 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)strlen(stmt->arg), stmt->arg, "yin-element");
Michal Vasko193dacd2022-10-13 08:43:05 +02001556 return LY_EVALID;
1557 }
1558
1559 /* parse substatements */
1560 for (const struct lysp_stmt *child = stmt->child; child; child = child->next) {
1561 switch (child->kw) {
1562 case LY_STMT_EXTENSION_INSTANCE:
1563 LY_CHECK_RET(lysp_stmt_ext(ctx, child, stmt->kw, 0, exts));
1564 break;
1565 default:
1566 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), lyplg_ext_stmt2str(LY_STMT_YIN_ELEMENT));
1567 return LY_EVALID;
1568 }
1569 }
1570
1571 return LY_SUCCESS;
1572}
1573
1574/**
1575 * @brief Parse the argument statement. Substatement of extension statement.
1576 *
1577 * @param[in] ctx parser context.
1578 * @param[in] stmt Source statement data from the parsed extension instance.
1579 * @param[in,out] ex Extension to fill.
1580 * @return LY_ERR values.
1581 */
1582static LY_ERR
1583lysp_stmt_argument(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_ext *ex)
1584{
1585 if (ex->argname) {
1586 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "argument");
1587 return LY_EVALID;
1588 }
1589
1590 /* store argument name */
1591 LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_PREF_IDENTIF_ARG, stmt->arg));
1592 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &ex->argname));
1593
1594 /* parse substatements */
1595 for (const struct lysp_stmt *child = stmt->child; child; child = child->next) {
1596 switch (child->kw) {
1597 case LY_STMT_YIN_ELEMENT:
1598 LY_CHECK_RET(lysp_stmt_yinelem(ctx, child, &ex->flags, &ex->exts));
1599 break;
1600 case LY_STMT_EXTENSION_INSTANCE:
1601 LY_CHECK_RET(lysp_stmt_ext(ctx, child, stmt->kw, 0, &ex->exts));
1602 break;
1603 default:
1604 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), lyplg_ext_stmt2str(LY_STMT_ARGUMENT));
1605 return LY_EVALID;
1606 }
1607 }
1608
1609 return LY_SUCCESS;
1610}
1611
1612/**
1613 * @brief Parse the extension statement.
1614 *
1615 * @param[in] ctx parser context.
1616 * @param[in] stmt Source statement data from the parsed extension instance.
1617 * @param[in,out] extensions Array of extensions to add to.
1618 * @return LY_ERR values.
1619 */
1620static LY_ERR
1621lysp_stmt_extension(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_ext **extensions)
1622{
1623 struct lysp_ext *ex;
1624
1625 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *extensions, ex, LY_EMEM);
1626
1627 /* store name */
1628 LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_IDENTIF_ARG, stmt->arg));
1629 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &ex->name));
1630
1631 /* parse substatements */
1632 for (const struct lysp_stmt *child = stmt->child; child; child = child->next) {
1633 switch (child->kw) {
1634 case LY_STMT_DESCRIPTION:
1635 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &ex->dsc, Y_STR_ARG, &ex->exts));
1636 break;
1637 case LY_STMT_REFERENCE:
1638 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &ex->ref, Y_STR_ARG, &ex->exts));
1639 break;
1640 case LY_STMT_STATUS:
1641 LY_CHECK_RET(lysp_stmt_status(ctx, child, &ex->flags, &ex->exts));
1642 break;
1643 case LY_STMT_ARGUMENT:
1644 LY_CHECK_RET(lysp_stmt_argument(ctx, child, ex));
1645 break;
1646 case LY_STMT_EXTENSION_INSTANCE:
1647 LY_CHECK_RET(lysp_stmt_ext(ctx, child, stmt->kw, 0, &ex->exts));
1648 break;
1649 default:
1650 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), lyplg_ext_stmt2str(LY_STMT_EXTENSION));
1651 return LY_EVALID;
1652 }
1653 }
1654
1655 return LY_SUCCESS;
1656}
1657
1658/**
1659 * @brief Parse the feature statement.
1660 *
1661 * @param[in] ctx parser context.
1662 * @param[in] stmt Source statement data from the parsed extension instance.
1663 * @param[in,out] features Array of features to add to.
1664 * @return LY_ERR values.
1665 */
1666static LY_ERR
1667lysp_stmt_feature(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_feature **features)
1668{
1669 struct lysp_feature *feat;
1670
1671 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *features, feat, LY_EMEM);
1672
1673 /* store name */
1674 LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_IDENTIF_ARG, stmt->arg));
1675 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &feat->name));
1676
1677 /* parse substatements */
1678 for (const struct lysp_stmt *child = stmt->child; child; child = child->next) {
1679 switch (child->kw) {
1680 case LY_STMT_DESCRIPTION:
1681 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &feat->dsc, Y_STR_ARG, &feat->exts));
1682 break;
1683 case LY_STMT_IF_FEATURE:
1684 LY_CHECK_RET(lysp_stmt_qnames(ctx, child, &feat->iffeatures, Y_STR_ARG, &feat->exts));
1685 break;
1686 case LY_STMT_REFERENCE:
1687 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &feat->ref, Y_STR_ARG, &feat->exts));
1688 break;
1689 case LY_STMT_STATUS:
1690 LY_CHECK_RET(lysp_stmt_status(ctx, child, &feat->flags, &feat->exts));
1691 break;
1692 case LY_STMT_EXTENSION_INSTANCE:
1693 LY_CHECK_RET(lysp_stmt_ext(ctx, child, stmt->kw, 0, &feat->exts));
1694 break;
1695 default:
1696 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), lyplg_ext_stmt2str(LY_STMT_FEATURE));
1697 return LY_EVALID;
1698 }
1699 }
1700
1701 return LY_SUCCESS;
1702}
1703
1704/**
1705 * @brief Parse the identity statement.
1706 *
1707 * @param[in] ctx parser context.
1708 * @param[in] stmt Source statement data from the parsed extension instance.
1709 * @param[in,out] identities Array of identities to add to.
1710 * @return LY_ERR values.
1711 */
1712static LY_ERR
1713lysp_stmt_identity(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_ident **identities)
1714{
1715 struct lysp_ident *ident;
1716
1717 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *identities, ident, LY_EMEM);
1718
1719 /* store name */
1720 LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_IDENTIF_ARG, stmt->arg));
1721 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &ident->name));
1722
1723 /* parse substatements */
1724 for (const struct lysp_stmt *child = stmt->child; child; child = child->next) {
1725 switch (child->kw) {
1726 case LY_STMT_DESCRIPTION:
1727 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &ident->dsc, Y_STR_ARG, &ident->exts));
1728 break;
1729 case LY_STMT_IF_FEATURE:
1730 LY_CHECK_RET(lysp_stmt_qnames(ctx, child, &ident->iffeatures, Y_STR_ARG, &ident->exts));
1731 break;
1732 case LY_STMT_REFERENCE:
1733 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &ident->ref, Y_STR_ARG, &ident->exts));
1734 break;
1735 case LY_STMT_STATUS:
1736 LY_CHECK_RET(lysp_stmt_status(ctx, child, &ident->flags, &ident->exts));
1737 break;
1738 case LY_STMT_BASE:
1739 LY_CHECK_RET(lysp_stmt_text_fields(ctx, child, &ident->bases, Y_PREF_IDENTIF_ARG, &ident->exts));
1740 break;
1741 case LY_STMT_EXTENSION_INSTANCE:
1742 LY_CHECK_RET(lysp_stmt_ext(ctx, child, stmt->kw, 0, &ident->exts));
1743 break;
1744 default:
1745 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), lyplg_ext_stmt2str(LY_STMT_IDENTITY));
1746 return LY_EVALID;
1747 }
1748 }
1749
1750 return LY_SUCCESS;
1751}
1752
1753/**
1754 * @brief Parse the import statement.
1755 *
1756 * @param[in] ctx parser context.
1757 * @param[in] stmt Source statement data from the parsed extension instance.
1758 * @param[in,out] imports Array of imports to add to.
1759 * @return LY_ERR values.
1760 */
1761static LY_ERR
1762lysp_stmt_import(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_import **imports)
1763{
1764 struct lysp_import *imp;
1765 const char *str = NULL;
1766
1767 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *imports, imp, LY_EMEM);
1768
1769 /* store name */
1770 LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_IDENTIF_ARG, stmt->arg));
1771 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &imp->name));
1772
1773 /* parse substatements */
1774 for (const struct lysp_stmt *child = stmt->child; child; child = child->next) {
1775 switch (child->kw) {
1776 case LY_STMT_PREFIX:
1777 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &imp->prefix, Y_IDENTIF_ARG, &imp->exts));
1778 LY_CHECK_RET(lysp_check_prefix(ctx, *imports, NULL, &imp->prefix), LY_EVALID);
1779 break;
1780 case LY_STMT_DESCRIPTION:
1781 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &imp->dsc, Y_STR_ARG, &imp->exts));
1782 break;
1783 case LY_STMT_REFERENCE:
1784 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &imp->ref, Y_STR_ARG, &imp->exts));
1785 break;
1786 case LY_STMT_REVISION_DATE:
1787 LY_CHECK_RET(lysp_stmt_text_field(ctx, stmt, 0, &str, Y_STR_ARG, &imp->exts));
1788 strcpy(imp->rev, str);
1789 lydict_remove(PARSER_CTX(ctx), str);
1790 LY_CHECK_RET(lysp_check_date(ctx, imp->rev, LY_REV_SIZE - 1, "revision-date"));
1791 break;
1792 case LY_STMT_EXTENSION_INSTANCE:
1793 LY_CHECK_RET(lysp_stmt_ext(ctx, child, stmt->kw, 0, &imp->exts));
1794 break;
1795 default:
1796 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), lyplg_ext_stmt2str(LY_STMT_IMPORT));
1797 return LY_EVALID;
1798 }
1799 }
1800
1801 return LY_SUCCESS;
1802}
1803
1804/**
1805 * @brief Parse the include statement.
1806 *
1807 * @param[in] ctx parser context.
1808 * @param[in] stmt Source statement data from the parsed extension instance.
1809 * @param[in,out] includes Array of identities to add to.
1810 * @return LY_ERR values.
1811 */
1812static LY_ERR
1813lysp_stmt_include(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_include **includes)
1814{
1815 struct lysp_include *inc;
1816 const char *str = NULL;
1817
1818 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *includes, inc, LY_EMEM);
1819
1820 /* store name */
1821 LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_IDENTIF_ARG, stmt->arg));
1822 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &inc->name));
1823
1824 /* parse substatements */
1825 for (const struct lysp_stmt *child = stmt->child; child; child = child->next) {
1826 switch (child->kw) {
1827 case LY_STMT_DESCRIPTION:
1828 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &inc->dsc, Y_STR_ARG, &inc->exts));
1829 break;
1830 case LY_STMT_REFERENCE:
1831 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &inc->ref, Y_STR_ARG, &inc->exts));
1832 break;
1833 case LY_STMT_REVISION_DATE:
1834 LY_CHECK_RET(lysp_stmt_text_field(ctx, stmt, 0, &str, Y_STR_ARG, &inc->exts));
1835 strcpy(inc->rev, str);
1836 lydict_remove(PARSER_CTX(ctx), str);
1837 LY_CHECK_RET(lysp_check_date(ctx, inc->rev, LY_REV_SIZE - 1, "revision-date"));
1838 break;
1839 case LY_STMT_EXTENSION_INSTANCE:
1840 LY_CHECK_RET(lysp_stmt_ext(ctx, child, stmt->kw, 0, &inc->exts));
1841 break;
1842 default:
1843 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), lyplg_ext_stmt2str(LY_STMT_INCLUDE));
1844 return LY_EVALID;
1845 }
1846 }
1847
1848 return LY_SUCCESS;
1849}
1850
1851/**
1852 * @brief Parse the revision statement.
1853 *
1854 * @param[in] ctx parser context.
1855 * @param[in] stmt Source statement data from the parsed extension instance.
1856 * @param[in,out] includes Array of identities to add to.
1857 * @return LY_ERR values.
1858 */
1859static LY_ERR
1860lysp_stmt_revision(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_revision **revs)
1861{
1862 struct lysp_revision *rev;
1863
1864 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *revs, rev, LY_EMEM);
1865
1866 /* store date */
Michal Vaskob425bb32022-11-08 10:49:36 +01001867 LY_CHECK_RET(lysp_check_date(ctx, stmt->arg, strlen(stmt->arg), "revision"));
Michal Vaskod5562c72022-11-08 11:30:50 +01001868 strncpy(rev->date, stmt->arg, LY_REV_SIZE - 1);
Michal Vasko193dacd2022-10-13 08:43:05 +02001869
1870 /* parse substatements */
1871 for (const struct lysp_stmt *child = stmt->child; child; child = child->next) {
1872 switch (child->kw) {
1873 case LY_STMT_DESCRIPTION:
1874 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &rev->dsc, Y_STR_ARG, &rev->exts));
1875 break;
1876 case LY_STMT_REFERENCE:
1877 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &rev->ref, Y_STR_ARG, &rev->exts));
1878 break;
1879 case LY_STMT_EXTENSION_INSTANCE:
1880 LY_CHECK_RET(lysp_stmt_ext(ctx, child, stmt->kw, 0, &rev->exts));
1881 break;
1882 default:
1883 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), lyplg_ext_stmt2str(LY_STMT_REVISION));
1884 return LY_EVALID;
1885 }
1886 }
1887
1888 return LY_SUCCESS;
1889}
1890
1891/**
Radek Krejci335332a2019-09-05 13:03:35 +02001892 * @brief Parse the type statement.
1893 *
Radek Krejci76c8c4e2021-02-17 10:16:48 +01001894 * @param[in] ctx parser context.
1895 * @param[in] stmt Source statement data from the parsed extension instance.
Radek Krejci335332a2019-09-05 13:03:35 +02001896 * @param[in,out] type Type to wrote to.
1897 *
1898 * @return LY_ERR values.
1899 */
1900static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001901lysp_stmt_type(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_type *type)
Radek Krejci335332a2019-09-05 13:03:35 +02001902{
1903 struct lysp_type *nest_type;
Radek Krejcib1247842020-07-02 16:22:38 +02001904 const char *str_path = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02001905 LY_ERR ret;
Radek Krejci335332a2019-09-05 13:03:35 +02001906
1907 if (type->name) {
1908 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type");
1909 return LY_EVALID;
1910 }
Radek Krejci76c8c4e2021-02-17 10:16:48 +01001911
1912 LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_PREF_IDENTIF_ARG, stmt->arg));
Radek Krejci011e4aa2020-09-04 15:22:31 +02001913 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &type->name));
Michal Vasko8a67eff2021-12-07 14:04:47 +01001914 type->pmod = PARSER_CUR_PMOD(ctx);
Radek Krejci335332a2019-09-05 13:03:35 +02001915
Radek Krejci76c8c4e2021-02-17 10:16:48 +01001916 for (const struct lysp_stmt *child = stmt->child; child; child = child->next) {
1917 switch (child->kw) {
Radek Krejci335332a2019-09-05 13:03:35 +02001918 case LY_STMT_BASE:
Radek Krejci76c8c4e2021-02-17 10:16:48 +01001919 LY_CHECK_RET(lysp_stmt_text_fields(ctx, child, &type->bases, Y_PREF_IDENTIF_ARG, &type->exts));
Radek Krejci335332a2019-09-05 13:03:35 +02001920 type->flags |= LYS_SET_BASE;
1921 break;
1922 case LY_STMT_BIT:
Radek Krejci76c8c4e2021-02-17 10:16:48 +01001923 LY_CHECK_RET(lysp_stmt_type_enum(ctx, child, &type->bits));
Radek Krejci335332a2019-09-05 13:03:35 +02001924 type->flags |= LYS_SET_BIT;
1925 break;
1926 case LY_STMT_ENUM:
Radek Krejci76c8c4e2021-02-17 10:16:48 +01001927 LY_CHECK_RET(lysp_stmt_type_enum(ctx, child, &type->enums));
Radek Krejci335332a2019-09-05 13:03:35 +02001928 type->flags |= LYS_SET_ENUM;
1929 break;
1930 case LY_STMT_FRACTION_DIGITS:
1931 LY_CHECK_RET(lysp_stmt_type_fracdigits(ctx, child, &type->fraction_digits, &type->exts));
1932 type->flags |= LYS_SET_FRDIGITS;
1933 break;
1934 case LY_STMT_LENGTH:
1935 if (type->length) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001936 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(child->kw));
Radek Krejci335332a2019-09-05 13:03:35 +02001937 return LY_EVALID;
1938 }
1939 type->length = calloc(1, sizeof *type->length);
Michal Vaskob36053d2020-03-26 15:49:30 +01001940 LY_CHECK_ERR_RET(!type->length, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci335332a2019-09-05 13:03:35 +02001941
Radek Krejci76c8c4e2021-02-17 10:16:48 +01001942 LY_CHECK_RET(lysp_stmt_restr(ctx, child, type->length));
Radek Krejci335332a2019-09-05 13:03:35 +02001943 type->flags |= LYS_SET_LENGTH;
1944 break;
1945 case LY_STMT_PATH:
Radek Krejci76c8c4e2021-02-17 10:16:48 +01001946 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &str_path, Y_STR_ARG, &type->exts));
Michal Vaskoed725d72021-06-23 12:03:45 +02001947 ret = ly_path_parse(PARSER_CTX(ctx), NULL, str_path, 0, 1, LY_PATH_BEGIN_EITHER,
Michal Vasko69730152020-10-09 16:30:07 +02001948 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path);
Michal Vasko004d3152020-06-11 19:59:22 +02001949 lydict_remove(PARSER_CTX(ctx), str_path);
1950 LY_CHECK_RET(ret);
Radek Krejci335332a2019-09-05 13:03:35 +02001951 type->flags |= LYS_SET_PATH;
1952 break;
1953 case LY_STMT_PATTERN:
1954 LY_CHECK_RET(lysp_stmt_type_pattern(ctx, child, &type->patterns));
1955 type->flags |= LYS_SET_PATTERN;
1956 break;
1957 case LY_STMT_RANGE:
1958 if (type->range) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001959 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(child->kw));
Radek Krejci335332a2019-09-05 13:03:35 +02001960 return LY_EVALID;
1961 }
1962 type->range = calloc(1, sizeof *type->range);
Michal Vaskob36053d2020-03-26 15:49:30 +01001963 LY_CHECK_ERR_RET(!type->range, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci335332a2019-09-05 13:03:35 +02001964
Radek Krejci76c8c4e2021-02-17 10:16:48 +01001965 LY_CHECK_RET(lysp_stmt_restr(ctx, child, type->range));
Radek Krejci335332a2019-09-05 13:03:35 +02001966 type->flags |= LYS_SET_RANGE;
1967 break;
1968 case LY_STMT_REQUIRE_INSTANCE:
1969 LY_CHECK_RET(lysp_stmt_type_reqinstance(ctx, child, &type->require_instance, &type->flags, &type->exts));
Radek Krejci76c8c4e2021-02-17 10:16:48 +01001970 /* LYS_SET_REQINST checked and set inside lysp_stmt_type_reqinstance() */
Radek Krejci335332a2019-09-05 13:03:35 +02001971 break;
1972 case LY_STMT_TYPE:
Michal Vaskob36053d2020-03-26 15:49:30 +01001973 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), type->types, nest_type, LY_EMEM);
Radek Krejci335332a2019-09-05 13:03:35 +02001974 LY_CHECK_RET(lysp_stmt_type(ctx, child, nest_type));
1975 type->flags |= LYS_SET_TYPE;
1976 break;
1977 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01001978 LY_CHECK_RET(lysp_stmt_ext(ctx, child, LY_STMT_TYPE, 0, &type->exts));
Radek Krejci335332a2019-09-05 13:03:35 +02001979 break;
1980 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001981 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), "type");
Radek Krejci335332a2019-09-05 13:03:35 +02001982 return LY_EVALID;
1983 }
1984 }
1985 return LY_SUCCESS;
1986}
1987
Radek Krejci76c8c4e2021-02-17 10:16:48 +01001988/**
1989 * @brief Parse the leaf statement.
1990 *
1991 * @param[in] ctx parser context.
1992 * @param[in] stmt Source statement data from the parsed extension instance.
1993 * @param[in] parent Parent node to connect to (not into).
1994 * @param[in,out] siblings Siblings to add to.
1995 *
1996 * @return LY_ERR values.
1997 */
1998static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001999lysp_stmt_leaf(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, struct lysp_node **siblings)
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002000{
2001 struct lysp_node_leaf *leaf;
2002
2003 LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_IDENTIF_ARG, stmt->arg));
2004
2005 /* create new leaf structure */
2006 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, leaf, next, LY_EMEM);
2007 leaf->nodetype = LYS_LEAF;
2008 leaf->parent = parent;
2009
2010 /* get name */
2011 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &leaf->name));
2012
2013 /* parse substatements */
2014 for (const struct lysp_stmt *child = stmt->child; child; child = child->next) {
2015 switch (child->kw) {
2016 case LY_STMT_CONFIG:
2017 LY_CHECK_RET(lysp_stmt_config(ctx, child, &leaf->flags, &leaf->exts));
2018 break;
2019 case LY_STMT_DEFAULT:
2020 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &leaf->dflt.str, Y_STR_ARG, &leaf->exts));
Michal Vasko8a67eff2021-12-07 14:04:47 +01002021 leaf->dflt.mod = PARSER_CUR_PMOD(ctx);
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002022 break;
2023 case LY_STMT_DESCRIPTION:
2024 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &leaf->dsc, Y_STR_ARG, &leaf->exts));
2025 break;
2026 case LY_STMT_IF_FEATURE:
2027 LY_CHECK_RET(lysp_stmt_qnames(ctx, child, &leaf->iffeatures, Y_STR_ARG, &leaf->exts));
2028 break;
2029 case LY_STMT_MANDATORY:
2030 LY_CHECK_RET(lysp_stmt_mandatory(ctx, child, &leaf->flags, &leaf->exts));
2031 break;
2032 case LY_STMT_MUST:
2033 LY_CHECK_RET(lysp_stmt_restrs(ctx, child, &leaf->musts));
2034 break;
2035 case LY_STMT_REFERENCE:
2036 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &leaf->ref, Y_STR_ARG, &leaf->exts));
2037 break;
2038 case LY_STMT_STATUS:
2039 LY_CHECK_RET(lysp_stmt_status(ctx, child, &leaf->flags, &leaf->exts));
2040 break;
2041 case LY_STMT_TYPE:
2042 LY_CHECK_RET(lysp_stmt_type(ctx, child, &leaf->type));
2043 break;
2044 case LY_STMT_UNITS:
2045 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &leaf->units, Y_STR_ARG, &leaf->exts));
2046 break;
2047 case LY_STMT_WHEN:
2048 LY_CHECK_RET(lysp_stmt_when(ctx, child, &leaf->when));
2049 break;
2050 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002051 LY_CHECK_RET(lysp_stmt_ext(ctx, child, LY_STMT_LEAF, 0, &leaf->exts));
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002052 break;
2053 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002054 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), "leaf");
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002055 return LY_EVALID;
2056 }
2057 }
2058
2059 /* mandatory substatements */
2060 if (!leaf->type.name) {
2061 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf");
2062 return LY_EVALID;
2063 }
2064
2065 return LY_SUCCESS;
2066}
2067
2068/**
2069 * @brief Parse the max-elements statement.
2070 *
2071 * @param[in] ctx parser context.
2072 * @param[in] stmt Source statement data from the parsed extension instance.
2073 * @param[in,out] max Value to write to.
2074 * @param[in,out] flags Flags to write to.
2075 * @param[in,out] exts Extension instances to add to.
2076 *
2077 * @return LY_ERR values.
2078 */
2079static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02002080lysp_stmt_maxelements(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, uint32_t *max, uint16_t *flags,
2081 struct lysp_ext_instance **exts)
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002082{
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002083 int arg_len;
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002084 char *ptr;
Michal Vasko2bf4af42023-01-04 12:08:38 +01002085 unsigned long long num;
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002086
2087 if (*flags & LYS_SET_MAX) {
2088 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements");
2089 return LY_EVALID;
2090 }
2091 *flags |= LYS_SET_MAX;
2092
2093 /* get value */
2094 LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_STR_ARG, stmt->arg));
2095 arg_len = strlen(stmt->arg);
2096
2097 if (!arg_len || (stmt->arg[0] == '0') || ((stmt->arg[0] != 'u') && !isdigit(stmt->arg[0]))) {
2098 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, "max-elements");
2099 return LY_EVALID;
2100 }
2101
2102 if ((arg_len != ly_strlen_const("unbounded")) || strncmp(stmt->arg, "unbounded", arg_len)) {
2103 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02002104 num = strtoull(stmt->arg, &ptr, LY_BASE_DEC);
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002105 /* we have not parsed the whole argument */
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002106 if (ptr - stmt->arg != arg_len) {
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002107 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, "max-elements");
2108 return LY_EVALID;
2109 }
2110 if ((errno == ERANGE) || (num > UINT32_MAX)) {
2111 LOGVAL_PARSER(ctx, LY_VCODE_OOB, arg_len, stmt->arg, "max-elements");
2112 return LY_EVALID;
2113 }
2114
2115 *max = num;
2116 } else {
2117 /* unbounded */
2118 *max = 0;
2119 }
2120
2121 for (const struct lysp_stmt *child = stmt->child; child; child = child->next) {
2122 switch (child->kw) {
2123 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002124 LY_CHECK_RET(lysp_stmt_ext(ctx, child, LY_STMT_MAX_ELEMENTS, 0, exts));
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002125 break;
2126 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002127 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), "max-elements");
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002128 return LY_EVALID;
2129 }
2130 }
2131
2132 return LY_SUCCESS;
2133}
2134
2135/**
2136 * @brief Parse the min-elements statement.
2137 *
2138 * @param[in] ctx parser context.
2139 * @param[in] stmt Source statement data from the parsed extension instance.
2140 * @param[in,out] min Value to write to.
2141 * @param[in,out] flags Flags to write to.
2142 * @param[in,out] exts Extension instances to add to.
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002143 * @return LY_ERR values.
2144 */
2145static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02002146lysp_stmt_minelements(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, uint32_t *min, uint16_t *flags,
2147 struct lysp_ext_instance **exts)
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002148{
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002149 int arg_len;
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002150 char *ptr;
Michal Vasko2bf4af42023-01-04 12:08:38 +01002151 unsigned long long num;
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002152
2153 if (*flags & LYS_SET_MIN) {
2154 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements");
2155 return LY_EVALID;
2156 }
2157 *flags |= LYS_SET_MIN;
2158
2159 /* get value */
2160 LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_STR_ARG, stmt->arg));
2161 arg_len = strlen(stmt->arg);
2162
2163 if (!arg_len || !isdigit(stmt->arg[0]) || ((stmt->arg[0] == '0') && (arg_len > 1))) {
2164 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, "min-elements");
2165 return LY_EVALID;
2166 }
2167
2168 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02002169 num = strtoull(stmt->arg, &ptr, LY_BASE_DEC);
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002170 /* we have not parsed the whole argument */
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002171 if (ptr - stmt->arg != arg_len) {
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002172 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, "min-elements");
2173 return LY_EVALID;
2174 }
2175 if ((errno == ERANGE) || (num > UINT32_MAX)) {
2176 LOGVAL_PARSER(ctx, LY_VCODE_OOB, arg_len, stmt->arg, "min-elements");
2177 return LY_EVALID;
2178 }
2179 *min = num;
2180
2181 for (const struct lysp_stmt *child = stmt->child; child; child = child->next) {
2182 switch (child->kw) {
2183 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002184 LY_CHECK_RET(lysp_stmt_ext(ctx, child, LY_STMT_MIN_ELEMENTS, 0, exts));
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002185 break;
2186 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002187 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), "min-elements");
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002188 return LY_EVALID;
2189 }
2190 }
2191
2192 return LY_SUCCESS;
2193}
2194
2195/**
2196 * @brief Parse the ordered-by statement.
2197 *
2198 * @param[in] ctx parser context.
2199 * @param[in] stmt Source statement data from the parsed extension instance.
2200 * @param[in,out] flags Flags to write to.
2201 * @param[in,out] exts Extension instances to add to.
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002202 * @return LY_ERR values.
2203 */
2204static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002205lysp_stmt_orderedby(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, uint16_t *flags, struct lysp_ext_instance **exts)
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002206{
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002207 int arg_len;
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002208
2209 if (*flags & LYS_ORDBY_MASK) {
2210 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by");
2211 return LY_EVALID;
2212 }
2213
2214 /* get value */
2215 LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_STR_ARG, stmt->arg));
2216 arg_len = strlen(stmt->arg);
2217 if ((arg_len == ly_strlen_const("system")) && !strncmp(stmt->arg, "system", arg_len)) {
2218 *flags |= LYS_MAND_TRUE;
2219 } else if ((arg_len == ly_strlen_const("user")) && !strncmp(stmt->arg, "user", arg_len)) {
2220 *flags |= LYS_MAND_FALSE;
2221 } else {
2222 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, "ordered-by");
2223 return LY_EVALID;
2224 }
2225
2226 for (const struct lysp_stmt *child = stmt->child; child; child = child->next) {
2227 switch (child->kw) {
2228 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002229 LY_CHECK_RET(lysp_stmt_ext(ctx, child, LY_STMT_ORDERED_BY, 0, exts));
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002230 break;
2231 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002232 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), "ordered-by");
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002233 return LY_EVALID;
2234 }
2235 }
2236
2237 return LY_SUCCESS;
2238}
2239
2240/**
2241 * @brief Parse the leaf-list statement.
2242 *
2243 * @param[in] ctx parser context.
2244 * @param[in] stmt Source statement data from the parsed extension instance.
2245 * @param[in] parent Parent node to connect to (not into).
2246 * @param[in,out] siblings Siblings to add to.
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002247 * @return LY_ERR values.
2248 */
2249static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002250lysp_stmt_leaflist(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent,
Michal Vasko59892dd2022-05-13 11:02:30 +02002251 struct lysp_node **siblings)
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002252{
2253 struct lysp_node_leaflist *llist;
2254
2255 LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_IDENTIF_ARG, stmt->arg));
2256
2257 /* create new leaf-list structure */
2258 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, llist, next, LY_EMEM);
2259 llist->nodetype = LYS_LEAFLIST;
2260 llist->parent = parent;
2261
2262 /* get name */
2263 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &llist->name));
2264
2265 /* parse substatements */
2266 for (const struct lysp_stmt *child = stmt->child; child; child = child->next) {
2267 switch (child->kw) {
2268 case LY_STMT_CONFIG:
2269 LY_CHECK_RET(lysp_stmt_config(ctx, child, &llist->flags, &llist->exts));
2270 break;
2271 case LY_STMT_DEFAULT:
2272 PARSER_CHECK_STMTVER2_RET(ctx, "default", "leaf-list");
2273 LY_CHECK_RET(lysp_stmt_qnames(ctx, child, &llist->dflts, Y_STR_ARG, &llist->exts));
2274 break;
2275 case LY_STMT_DESCRIPTION:
2276 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &llist->dsc, Y_STR_ARG, &llist->exts));
2277 break;
2278 case LY_STMT_IF_FEATURE:
2279 LY_CHECK_RET(lysp_stmt_qnames(ctx, child, &llist->iffeatures, Y_STR_ARG, &llist->exts));
2280 break;
2281 case LY_STMT_MAX_ELEMENTS:
2282 LY_CHECK_RET(lysp_stmt_maxelements(ctx, child, &llist->max, &llist->flags, &llist->exts));
2283 break;
2284 case LY_STMT_MIN_ELEMENTS:
2285 LY_CHECK_RET(lysp_stmt_minelements(ctx, child, &llist->min, &llist->flags, &llist->exts));
2286 break;
2287 case LY_STMT_MUST:
2288 LY_CHECK_RET(lysp_stmt_restrs(ctx, child, &llist->musts));
2289 break;
2290 case LY_STMT_ORDERED_BY:
2291 LY_CHECK_RET(lysp_stmt_orderedby(ctx, child, &llist->flags, &llist->exts));
2292 break;
2293 case LY_STMT_REFERENCE:
2294 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &llist->ref, Y_STR_ARG, &llist->exts));
2295 break;
2296 case LY_STMT_STATUS:
2297 LY_CHECK_RET(lysp_stmt_status(ctx, child, &llist->flags, &llist->exts));
2298 break;
2299 case LY_STMT_TYPE:
2300 LY_CHECK_RET(lysp_stmt_type(ctx, child, &llist->type));
2301 break;
2302 case LY_STMT_UNITS:
2303 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &llist->units, Y_STR_ARG, &llist->exts));
2304 break;
2305 case LY_STMT_WHEN:
2306 LY_CHECK_RET(lysp_stmt_when(ctx, child, &llist->when));
2307 break;
2308 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002309 LY_CHECK_RET(lysp_stmt_ext(ctx, child, LY_STMT_LEAF_LIST, 0, &llist->exts));
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002310 break;
2311 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002312 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), "llist");
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002313 return LY_EVALID;
2314 }
2315 }
2316
2317 /* mandatory substatements */
2318 if (!llist->type.name) {
2319 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
2320 return LY_EVALID;
2321 }
2322
2323 return LY_SUCCESS;
2324}
2325
2326/**
2327 * @brief Parse the refine statement.
2328 *
2329 * @param[in] ctx parser context.
2330 * @param[in] stmt Source statement data from the parsed extension instance.
2331 * @param[in,out] refines Refines to add to.
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002332 * @return LY_ERR values.
2333 */
2334static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002335lysp_stmt_refine(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_refine **refines)
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002336{
2337 struct lysp_refine *rf;
2338
2339 LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_STR_ARG, stmt->arg));
2340
2341 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *refines, rf, LY_EMEM);
2342
2343 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &rf->nodeid));
2344
2345 for (const struct lysp_stmt *child = stmt->child; child; child = child->next) {
2346 switch (child->kw) {
2347 case LY_STMT_CONFIG:
2348 LY_CHECK_RET(lysp_stmt_config(ctx, child, &rf->flags, &rf->exts));
2349 break;
2350 case LY_STMT_DEFAULT:
2351 LY_CHECK_RET(lysp_stmt_qnames(ctx, child, &rf->dflts, Y_STR_ARG, &rf->exts));
2352 break;
2353 case LY_STMT_DESCRIPTION:
2354 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &rf->dsc, Y_STR_ARG, &rf->exts));
2355 break;
2356 case LY_STMT_IF_FEATURE:
2357 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "refine");
2358 LY_CHECK_RET(lysp_stmt_qnames(ctx, child, &rf->iffeatures, Y_STR_ARG, &rf->exts));
2359 break;
2360 case LY_STMT_MAX_ELEMENTS:
2361 LY_CHECK_RET(lysp_stmt_maxelements(ctx, child, &rf->max, &rf->flags, &rf->exts));
2362 break;
2363 case LY_STMT_MIN_ELEMENTS:
2364 LY_CHECK_RET(lysp_stmt_minelements(ctx, child, &rf->min, &rf->flags, &rf->exts));
2365 break;
2366 case LY_STMT_MUST:
2367 LY_CHECK_RET(lysp_stmt_restrs(ctx, child, &rf->musts));
2368 break;
2369 case LY_STMT_MANDATORY:
2370 LY_CHECK_RET(lysp_stmt_mandatory(ctx, child, &rf->flags, &rf->exts));
2371 break;
2372 case LY_STMT_REFERENCE:
2373 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &rf->ref, Y_STR_ARG, &rf->exts));
2374 break;
2375 case LY_STMT_PRESENCE:
2376 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &rf->presence, Y_STR_ARG, &rf->exts));
2377 break;
2378 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002379 LY_CHECK_RET(lysp_stmt_ext(ctx, child, LY_STMT_REFINE, 0, &rf->exts));
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002380 break;
2381 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002382 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), "refine");
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002383 return LY_EVALID;
2384 }
2385 }
2386
2387 return LY_SUCCESS;
2388}
2389
2390/**
2391 * @brief Parse the typedef statement.
2392 *
2393 * @param[in] ctx parser context.
2394 * @param[in] stmt Source statement data from the parsed extension instance.
2395 * @param[in] parent Parent node to connect to (not into).
2396 * @param[in,out] typedefs Typedefs to add to.
2397 *
2398 * @return LY_ERR values.
2399 */
2400static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002401lysp_stmt_typedef(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent,
Michal Vasko59892dd2022-05-13 11:02:30 +02002402 struct lysp_tpdf **typedefs)
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002403{
2404 struct lysp_tpdf *tpdf;
2405
2406 LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_IDENTIF_ARG, stmt->arg));
2407
2408 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *typedefs, tpdf, LY_EMEM);
2409
2410 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &tpdf->name));
2411
2412 /* parse substatements */
2413 for (const struct lysp_stmt *child = stmt->child; child; child = child->next) {
2414 switch (child->kw) {
2415 case LY_STMT_DEFAULT:
2416 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &tpdf->dflt.str, Y_STR_ARG, &tpdf->exts));
Michal Vasko8a67eff2021-12-07 14:04:47 +01002417 tpdf->dflt.mod = PARSER_CUR_PMOD(ctx);
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002418 break;
2419 case LY_STMT_DESCRIPTION:
2420 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &tpdf->dsc, Y_STR_ARG, &tpdf->exts));
2421 break;
2422 case LY_STMT_REFERENCE:
2423 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &tpdf->ref, Y_STR_ARG, &tpdf->exts));
2424 break;
2425 case LY_STMT_STATUS:
2426 LY_CHECK_RET(lysp_stmt_status(ctx, child, &tpdf->flags, &tpdf->exts));
2427 break;
2428 case LY_STMT_TYPE:
2429 LY_CHECK_RET(lysp_stmt_type(ctx, child, &tpdf->type));
2430 break;
2431 case LY_STMT_UNITS:
2432 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &tpdf->units, Y_STR_ARG, &tpdf->exts));
2433 break;
2434 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002435 LY_CHECK_RET(lysp_stmt_ext(ctx, child, LY_STMT_TYPEDEF, 0, &tpdf->exts));
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002436 break;
2437 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002438 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), "typedef");
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002439 return LY_EVALID;
2440 }
2441 }
2442
2443 /* mandatory substatements */
2444 if (!tpdf->type.name) {
2445 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef");
2446 return LY_EVALID;
2447 }
2448
2449 /* store data for collision check */
2450 if (parent && !(parent->nodetype & (LYS_GROUPING | LYS_RPC | LYS_ACTION | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF))) {
2451 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, parent, 0, NULL));
2452 }
2453
2454 return LY_SUCCESS;
2455}
2456
2457/**
2458 * @brief Parse the input or output statement.
2459 *
2460 * @param[in] ctx parser context.
2461 * @param[in] stmt Source statement data from the parsed extension instance.
2462 * @param[in] parent Parent node to connect to (not into).
2463 * @param[in,out] inout_p Input/output pointer to write to.
2464 *
2465 * @return LY_ERR values.
2466 */
2467static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002468lysp_stmt_inout(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent,
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002469 struct lysp_node_action_inout *inout_p)
2470{
2471 if (inout_p->nodetype) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002472 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(stmt->kw));
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002473 return LY_EVALID;
2474 }
2475
2476 /* initiate structure */
2477 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->kw == LY_STMT_INPUT ? "input" : "output", 0, &inout_p->name));
2478 inout_p->nodetype = stmt->kw == LY_STMT_INPUT ? LYS_INPUT : LYS_OUTPUT;
2479 inout_p->parent = parent;
2480
2481 /* parse substatements */
2482 for (const struct lysp_stmt *child = stmt->child; child; child = child->next) {
2483 switch (child->kw) {
2484 case LY_STMT_ANYDATA:
Michal Vasko193dacd2022-10-13 08:43:05 +02002485 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", lyplg_ext_stmt2str(stmt->kw));
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002486 /* fall through */
2487 case LY_STMT_ANYXML:
2488 LY_CHECK_RET(lysp_stmt_any(ctx, child, &inout_p->node, &inout_p->child));
2489 break;
2490 case LY_STMT_CHOICE:
2491 LY_CHECK_RET(lysp_stmt_choice(ctx, child, &inout_p->node, &inout_p->child));
2492 break;
2493 case LY_STMT_CONTAINER:
2494 LY_CHECK_RET(lysp_stmt_container(ctx, child, &inout_p->node, &inout_p->child));
2495 break;
2496 case LY_STMT_LEAF:
2497 LY_CHECK_RET(lysp_stmt_leaf(ctx, child, &inout_p->node, &inout_p->child));
2498 break;
2499 case LY_STMT_LEAF_LIST:
2500 LY_CHECK_RET(lysp_stmt_leaflist(ctx, child, &inout_p->node, &inout_p->child));
2501 break;
2502 case LY_STMT_LIST:
2503 LY_CHECK_RET(lysp_stmt_list(ctx, child, &inout_p->node, &inout_p->child));
2504 break;
2505 case LY_STMT_USES:
2506 LY_CHECK_RET(lysp_stmt_uses(ctx, child, &inout_p->node, &inout_p->child));
2507 break;
2508 case LY_STMT_TYPEDEF:
2509 LY_CHECK_RET(lysp_stmt_typedef(ctx, child, &inout_p->node, &inout_p->typedefs));
2510 break;
2511 case LY_STMT_MUST:
Michal Vasko193dacd2022-10-13 08:43:05 +02002512 PARSER_CHECK_STMTVER2_RET(ctx, "must", lyplg_ext_stmt2str(stmt->kw));
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002513 LY_CHECK_RET(lysp_stmt_restrs(ctx, child, &inout_p->musts));
2514 break;
2515 case LY_STMT_GROUPING:
2516 LY_CHECK_RET(lysp_stmt_grouping(ctx, child, &inout_p->node, &inout_p->groupings));
2517 break;
2518 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002519 LY_CHECK_RET(lysp_stmt_ext(ctx, child, stmt->kw, 0, &inout_p->exts));
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002520 break;
2521 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002522 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), lyplg_ext_stmt2str(stmt->kw));
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002523 return LY_EVALID;
2524 }
2525 }
2526
2527 if (!inout_p->child) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002528 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "data-def-stmt", lyplg_ext_stmt2str(stmt->kw));
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002529 return LY_EVALID;
2530 }
2531
2532 return LY_SUCCESS;
2533}
2534
2535/**
2536 * @brief Parse the action statement.
2537 *
2538 * @param[in] ctx parser context.
2539 * @param[in] stmt Source statement data from the parsed extension instance.
2540 * @param[in] parent Parent node to connect to (not into).
2541 * @param[in,out] actions Actions to add to.
2542 *
2543 * @return LY_ERR values.
2544 */
2545static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002546lysp_stmt_action(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent,
Michal Vasko59892dd2022-05-13 11:02:30 +02002547 struct lysp_node_action **actions)
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002548{
2549 struct lysp_node_action *act;
2550
2551 LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_IDENTIF_ARG, stmt->arg));
2552
2553 LY_LIST_NEW_RET(PARSER_CTX(ctx), actions, act, next, LY_EMEM);
2554
2555 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &act->name));
2556 act->nodetype = parent ? LYS_ACTION : LYS_RPC;
2557 act->parent = parent;
2558
2559 for (const struct lysp_stmt *child = stmt->child; child; child = child->next) {
2560 switch (child->kw) {
2561 case LY_STMT_DESCRIPTION:
2562 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &act->dsc, Y_STR_ARG, &act->exts));
2563 break;
2564 case LY_STMT_IF_FEATURE:
2565 LY_CHECK_RET(lysp_stmt_qnames(ctx, child, &act->iffeatures, Y_STR_ARG, &act->exts));
2566 break;
2567 case LY_STMT_REFERENCE:
2568 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &act->ref, Y_STR_ARG, &act->exts));
2569 break;
2570 case LY_STMT_STATUS:
2571 LY_CHECK_RET(lysp_stmt_status(ctx, child, &act->flags, &act->exts));
2572 break;
2573
2574 case LY_STMT_INPUT:
2575 LY_CHECK_RET(lysp_stmt_inout(ctx, child, &act->node, &act->input));
2576 break;
2577 case LY_STMT_OUTPUT:
2578 LY_CHECK_RET(lysp_stmt_inout(ctx, child, &act->node, &act->output));
2579 break;
2580
2581 case LY_STMT_TYPEDEF:
2582 LY_CHECK_RET(lysp_stmt_typedef(ctx, child, &act->node, &act->typedefs));
2583 break;
2584 case LY_STMT_GROUPING:
2585 LY_CHECK_RET(lysp_stmt_grouping(ctx, child, &act->node, &act->groupings));
2586 break;
2587 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002588 LY_CHECK_RET(lysp_stmt_ext(ctx, child, parent ? LY_STMT_ACTION : LY_STMT_RPC, 0, &act->exts));
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002589 break;
2590 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002591 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), parent ? "action" : "rpc");
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002592 return LY_EVALID;
2593 }
2594 }
2595
2596 /* always initialize inout, they are technically present (needed for later deviations/refines) */
2597 if (!act->input.nodetype) {
2598 act->input.nodetype = LYS_INPUT;
2599 act->input.parent = &act->node;
2600 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "input", 0, &act->input.name));
2601 }
2602 if (!act->output.nodetype) {
2603 act->output.nodetype = LYS_OUTPUT;
2604 act->output.parent = &act->node;
2605 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "output", 0, &act->output.name));
2606 }
2607
2608 return LY_SUCCESS;
2609}
2610
2611/**
2612 * @brief Parse the notification statement.
2613 *
2614 * @param[in] ctx parser context.
2615 * @param[in] stmt Source statement data from the parsed extension instance.
2616 * @param[in] parent Parent node to connect to (not into).
2617 * @param[in,out] notifs Notifications to add to.
2618 *
2619 * @return LY_ERR values.
2620 */
2621static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002622lysp_stmt_notif(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent,
Michal Vasko59892dd2022-05-13 11:02:30 +02002623 struct lysp_node_notif **notifs)
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002624{
2625 struct lysp_node_notif *notif;
2626
2627 LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_IDENTIF_ARG, stmt->arg));
2628
2629 LY_LIST_NEW_RET(PARSER_CTX(ctx), notifs, notif, next, LY_EMEM);
2630
2631 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &notif->name));
2632 notif->nodetype = LYS_NOTIF;
2633 notif->parent = parent;
2634
2635 for (const struct lysp_stmt *child = stmt->child; child; child = child->next) {
2636 switch (child->kw) {
2637 case LY_STMT_DESCRIPTION:
2638 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &notif->dsc, Y_STR_ARG, &notif->exts));
2639 break;
2640 case LY_STMT_IF_FEATURE:
2641 LY_CHECK_RET(lysp_stmt_qnames(ctx, child, &notif->iffeatures, Y_STR_ARG, &notif->exts));
2642 break;
2643 case LY_STMT_REFERENCE:
2644 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &notif->ref, Y_STR_ARG, &notif->exts));
2645 break;
2646 case LY_STMT_STATUS:
2647 LY_CHECK_RET(lysp_stmt_status(ctx, child, &notif->flags, &notif->exts));
2648 break;
2649
2650 case LY_STMT_ANYDATA:
2651 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "notification");
2652 /* fall through */
2653 case LY_STMT_ANYXML:
2654 LY_CHECK_RET(lysp_stmt_any(ctx, child, &notif->node, &notif->child));
2655 break;
2656 case LY_STMT_CHOICE:
2657 LY_CHECK_RET(lysp_stmt_case(ctx, child, &notif->node, &notif->child));
2658 break;
2659 case LY_STMT_CONTAINER:
2660 LY_CHECK_RET(lysp_stmt_container(ctx, child, &notif->node, &notif->child));
2661 break;
2662 case LY_STMT_LEAF:
2663 LY_CHECK_RET(lysp_stmt_leaf(ctx, child, &notif->node, &notif->child));
2664 break;
2665 case LY_STMT_LEAF_LIST:
2666 LY_CHECK_RET(lysp_stmt_leaflist(ctx, child, &notif->node, &notif->child));
2667 break;
2668 case LY_STMT_LIST:
2669 LY_CHECK_RET(lysp_stmt_list(ctx, child, &notif->node, &notif->child));
2670 break;
2671 case LY_STMT_USES:
2672 LY_CHECK_RET(lysp_stmt_uses(ctx, child, &notif->node, &notif->child));
2673 break;
2674
2675 case LY_STMT_MUST:
2676 PARSER_CHECK_STMTVER2_RET(ctx, "must", "notification");
2677 LY_CHECK_RET(lysp_stmt_restrs(ctx, child, &notif->musts));
2678 break;
2679 case LY_STMT_TYPEDEF:
2680 LY_CHECK_RET(lysp_stmt_typedef(ctx, child, &notif->node, &notif->typedefs));
2681 break;
2682 case LY_STMT_GROUPING:
2683 LY_CHECK_RET(lysp_stmt_grouping(ctx, child, &notif->node, &notif->groupings));
2684 break;
2685 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002686 LY_CHECK_RET(lysp_stmt_ext(ctx, child, LY_STMT_NOTIFICATION, 0, &notif->exts));
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002687 break;
2688 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002689 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), "notification");
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002690 return LY_EVALID;
2691 }
2692 }
2693
2694 return LY_SUCCESS;
2695}
2696
2697/**
2698 * @brief Parse the grouping statement.
2699 *
2700 * @param[in] ctx parser context.
2701 * @param[in] stmt Source statement data from the parsed extension instance.
2702 * @param[in] parent Parent node to connect to (not into).
2703 * @param[in,out] groupings Groupings to add to.
2704 *
2705 * @return LY_ERR values.
2706 */
2707static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002708lysp_stmt_grouping(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent,
Michal Vasko59892dd2022-05-13 11:02:30 +02002709 struct lysp_node_grp **groupings)
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002710{
2711 struct lysp_node_grp *grp;
2712
2713 LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_IDENTIF_ARG, stmt->arg));
2714
2715 LY_LIST_NEW_RET(PARSER_CTX(ctx), groupings, grp, next, LY_EMEM);
2716
2717 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &grp->name));
2718 grp->nodetype = LYS_GROUPING;
2719 grp->parent = parent;
2720
2721 for (const struct lysp_stmt *child = stmt->child; child; child = child->next) {
2722 switch (child->kw) {
2723 case LY_STMT_DESCRIPTION:
2724 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &grp->dsc, Y_STR_ARG, &grp->exts));
2725 break;
2726 case LY_STMT_REFERENCE:
2727 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &grp->ref, Y_STR_ARG, &grp->exts));
2728 break;
2729 case LY_STMT_STATUS:
2730 LY_CHECK_RET(lysp_stmt_status(ctx, child, &grp->flags, &grp->exts));
2731 break;
2732
2733 case LY_STMT_ANYDATA:
2734 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "grouping");
2735 /* fall through */
2736 case LY_STMT_ANYXML:
2737 LY_CHECK_RET(lysp_stmt_any(ctx, child, &grp->node, &grp->child));
2738 break;
2739 case LY_STMT_CHOICE:
2740 LY_CHECK_RET(lysp_stmt_choice(ctx, child, &grp->node, &grp->child));
2741 break;
2742 case LY_STMT_CONTAINER:
2743 LY_CHECK_RET(lysp_stmt_container(ctx, child, &grp->node, &grp->child));
2744 break;
2745 case LY_STMT_LEAF:
2746 LY_CHECK_RET(lysp_stmt_leaf(ctx, child, &grp->node, &grp->child));
2747 break;
2748 case LY_STMT_LEAF_LIST:
2749 LY_CHECK_RET(lysp_stmt_leaflist(ctx, child, &grp->node, &grp->child));
2750 break;
2751 case LY_STMT_LIST:
2752 LY_CHECK_RET(lysp_stmt_list(ctx, child, &grp->node, &grp->child));
2753 break;
2754 case LY_STMT_USES:
2755 LY_CHECK_RET(lysp_stmt_uses(ctx, child, &grp->node, &grp->child));
2756 break;
2757
2758 case LY_STMT_TYPEDEF:
2759 LY_CHECK_RET(lysp_stmt_typedef(ctx, child, &grp->node, &grp->typedefs));
2760 break;
2761 case LY_STMT_ACTION:
2762 PARSER_CHECK_STMTVER2_RET(ctx, "action", "grouping");
2763 LY_CHECK_RET(lysp_stmt_action(ctx, child, &grp->node, &grp->actions));
2764 break;
2765 case LY_STMT_GROUPING:
2766 LY_CHECK_RET(lysp_stmt_grouping(ctx, child, &grp->node, &grp->groupings));
2767 break;
2768 case LY_STMT_NOTIFICATION:
2769 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "grouping");
2770 LY_CHECK_RET(lysp_stmt_notif(ctx, child, &grp->node, &grp->notifs));
2771 break;
2772 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002773 LY_CHECK_RET(lysp_stmt_ext(ctx, child, LY_STMT_GROUPING, 0, &grp->exts));
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002774 break;
2775 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002776 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), "grouping");
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002777 return LY_EVALID;
2778 }
2779 }
2780
2781 return LY_SUCCESS;
2782}
2783
2784/**
2785 * @brief Parse the augment statement.
2786 *
2787 * @param[in] ctx parser context.
2788 * @param[in] stmt Source statement data from the parsed extension instance.
2789 * @param[in] parent Parent node to connect to (not into).
2790 * @param[in,out] augments Augments to add to.
2791 *
2792 * @return LY_ERR values.
2793 */
2794static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002795lysp_stmt_augment(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent,
Michal Vasko59892dd2022-05-13 11:02:30 +02002796 struct lysp_node_augment **augments)
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002797{
2798 struct lysp_node_augment *aug;
2799
2800 LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_STR_ARG, stmt->arg));
2801
2802 LY_LIST_NEW_RET(PARSER_CTX(ctx), augments, aug, next, LY_EMEM);
2803
2804 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &aug->nodeid));
2805 aug->nodetype = LYS_AUGMENT;
2806 aug->parent = parent;
2807
2808 for (const struct lysp_stmt *child = stmt->child; child; child = child->next) {
2809 switch (child->kw) {
2810 case LY_STMT_DESCRIPTION:
2811 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &aug->dsc, Y_STR_ARG, &aug->exts));
2812 break;
2813 case LY_STMT_IF_FEATURE:
2814 LY_CHECK_RET(lysp_stmt_qnames(ctx, child, &aug->iffeatures, Y_STR_ARG, &aug->exts));
2815 break;
2816 case LY_STMT_REFERENCE:
2817 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &aug->ref, Y_STR_ARG, &aug->exts));
2818 break;
2819 case LY_STMT_STATUS:
2820 LY_CHECK_RET(lysp_stmt_status(ctx, child, &aug->flags, &aug->exts));
2821 break;
2822 case LY_STMT_WHEN:
2823 LY_CHECK_RET(lysp_stmt_when(ctx, child, &aug->when));
2824 break;
2825
2826 case LY_STMT_ANYDATA:
2827 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "augment");
2828 /* fall through */
2829 case LY_STMT_ANYXML:
2830 LY_CHECK_RET(lysp_stmt_any(ctx, child, &aug->node, &aug->child));
2831 break;
2832 case LY_STMT_CASE:
2833 LY_CHECK_RET(lysp_stmt_case(ctx, child, &aug->node, &aug->child));
2834 break;
2835 case LY_STMT_CHOICE:
2836 LY_CHECK_RET(lysp_stmt_choice(ctx, child, &aug->node, &aug->child));
2837 break;
2838 case LY_STMT_CONTAINER:
2839 LY_CHECK_RET(lysp_stmt_container(ctx, child, &aug->node, &aug->child));
2840 break;
2841 case LY_STMT_LEAF:
2842 LY_CHECK_RET(lysp_stmt_leaf(ctx, child, &aug->node, &aug->child));
2843 break;
2844 case LY_STMT_LEAF_LIST:
2845 LY_CHECK_RET(lysp_stmt_leaflist(ctx, child, &aug->node, &aug->child));
2846 break;
2847 case LY_STMT_LIST:
2848 LY_CHECK_RET(lysp_stmt_list(ctx, child, &aug->node, &aug->child));
2849 break;
2850 case LY_STMT_USES:
2851 LY_CHECK_RET(lysp_stmt_uses(ctx, child, &aug->node, &aug->child));
2852 break;
2853
2854 case LY_STMT_ACTION:
2855 PARSER_CHECK_STMTVER2_RET(ctx, "action", "augment");
2856 LY_CHECK_RET(lysp_stmt_action(ctx, child, &aug->node, &aug->actions));
2857 break;
2858 case LY_STMT_NOTIFICATION:
2859 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "augment");
2860 LY_CHECK_RET(lysp_stmt_notif(ctx, child, &aug->node, &aug->notifs));
2861 break;
2862 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002863 LY_CHECK_RET(lysp_stmt_ext(ctx, child, LY_STMT_AUGMENT, 0, &aug->exts));
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002864 break;
2865 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002866 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), "augment");
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002867 return LY_EVALID;
2868 }
2869 }
2870
2871 return LY_SUCCESS;
2872}
2873
2874/**
2875 * @brief Parse the uses statement.
2876 *
2877 * @param[in] ctx parser context.
2878 * @param[in] stmt Source statement data from the parsed extension instance.
2879 * @param[in] parent Parent node to connect to (not into).
2880 * @param[in,out] siblings Siblings to add to.
2881 *
2882 * @return LY_ERR values.
2883 */
2884static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002885lysp_stmt_uses(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent,
Michal Vasko59892dd2022-05-13 11:02:30 +02002886 struct lysp_node **siblings)
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002887{
2888 struct lysp_node_uses *uses;
2889
2890 LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_PREF_IDENTIF_ARG, stmt->arg));
2891
2892 /* create uses structure */
2893 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, uses, next, LY_EMEM);
2894
2895 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &uses->name));
2896 uses->nodetype = LYS_USES;
2897 uses->parent = parent;
2898
2899 /* parse substatements */
2900 for (const struct lysp_stmt *child = stmt->child; child; child = child->next) {
2901 switch (child->kw) {
2902 case LY_STMT_DESCRIPTION:
2903 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &uses->dsc, Y_STR_ARG, &uses->exts));
2904 break;
2905 case LY_STMT_IF_FEATURE:
2906 LY_CHECK_RET(lysp_stmt_qnames(ctx, child, &uses->iffeatures, Y_STR_ARG, &uses->exts));
2907 break;
2908 case LY_STMT_REFERENCE:
2909 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &uses->ref, Y_STR_ARG, &uses->exts));
2910 break;
2911 case LY_STMT_STATUS:
2912 LY_CHECK_RET(lysp_stmt_status(ctx, child, &uses->flags, &uses->exts));
2913 break;
2914 case LY_STMT_WHEN:
2915 LY_CHECK_RET(lysp_stmt_when(ctx, child, &uses->when));
2916 break;
2917
2918 case LY_STMT_REFINE:
2919 LY_CHECK_RET(lysp_stmt_refine(ctx, child, &uses->refines));
2920 break;
2921 case LY_STMT_AUGMENT:
2922 LY_CHECK_RET(lysp_stmt_augment(ctx, child, &uses->node, &uses->augments));
2923 break;
2924 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01002925 LY_CHECK_RET(lysp_stmt_ext(ctx, child, LY_STMT_USES, 0, &uses->exts));
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002926 break;
2927 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002928 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), "uses");
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002929 return LY_EVALID;
2930 }
2931 }
2932
2933 return LY_SUCCESS;
2934}
2935
2936/**
2937 * @brief Parse the case statement.
2938 *
2939 * @param[in] ctx parser context.
2940 * @param[in] stmt Source statement data from the parsed extension instance.
2941 * @param[in] parent Parent node to connect to (not into).
2942 * @param[in,out] siblings Siblings to add to.
2943 *
2944 * @return LY_ERR values.
2945 */
2946static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002947lysp_stmt_case(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent,
Michal Vasko59892dd2022-05-13 11:02:30 +02002948 struct lysp_node **siblings)
Radek Krejci76c8c4e2021-02-17 10:16:48 +01002949{
2950 struct lysp_node_case *cas;
2951
2952 LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_IDENTIF_ARG, stmt->arg));
2953
2954 /* create new case structure */
2955 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cas, next, LY_EMEM);
2956
2957 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &cas->name));
2958 cas->nodetype = LYS_CASE;
2959 cas->parent = parent;
2960
2961 /* parse substatements */
2962 for (const struct lysp_stmt *child = stmt->child; child; child = child->next) {
2963 switch (child->kw) {
2964 case LY_STMT_DESCRIPTION:
2965 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &cas->dsc, Y_STR_ARG, &cas->exts));
2966 break;
2967 case LY_STMT_IF_FEATURE:
2968 LY_CHECK_RET(lysp_stmt_qnames(ctx, child, &cas->iffeatures, Y_STR_ARG, &cas->exts));
2969 break;
2970 case LY_STMT_REFERENCE:
2971 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &cas->ref, Y_STR_ARG, &cas->exts));
2972 break;
2973 case LY_STMT_STATUS:
2974 LY_CHECK_RET(lysp_stmt_status(ctx, child, &cas->flags, &cas->exts));
2975 break;
2976 case LY_STMT_WHEN:
2977 LY_CHECK_RET(lysp_stmt_when(ctx, child, &cas->when));
2978 break;
2979
2980 case LY_STMT_ANYDATA:
2981 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "case");
2982 /* fall through */
2983 case LY_STMT_ANYXML:
2984 LY_CHECK_RET(lysp_stmt_any(ctx, child, &cas->node, &cas->child));
2985 break;
2986 case LY_STMT_CHOICE:
2987 LY_CHECK_RET(lysp_stmt_choice(ctx, child, &cas->node, &cas->child));
2988 break;
2989 case LY_STMT_CONTAINER:
2990 LY_CHECK_RET(lysp_stmt_container(ctx, child, &cas->node, &cas->child));
2991 break;
2992 case LY_STMT_LEAF:
2993 LY_CHECK_RET(lysp_stmt_leaf(ctx, child, &cas->node, &cas->child));
2994 break;
2995 case LY_STMT_LEAF_LIST:
2996 LY_CHECK_RET(lysp_stmt_leaflist(ctx, child, &cas->node, &cas->child));
2997 break;
2998 case LY_STMT_LIST:
2999 LY_CHECK_RET(lysp_stmt_list(ctx, child, &cas->node, &cas->child));
3000 break;
3001 case LY_STMT_USES:
3002 LY_CHECK_RET(lysp_stmt_uses(ctx, child, &cas->node, &cas->child));
3003 break;
3004 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003005 LY_CHECK_RET(lysp_stmt_ext(ctx, child, LY_STMT_CASE, 0, &cas->exts));
Radek Krejci76c8c4e2021-02-17 10:16:48 +01003006 break;
3007 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003008 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), "case");
Radek Krejci76c8c4e2021-02-17 10:16:48 +01003009 return LY_EVALID;
3010 }
3011 }
3012 return LY_SUCCESS;
3013}
3014
3015/**
3016 * @brief Parse the choice statement.
3017 *
3018 * @param[in] ctx parser context.
3019 * @param[in] stmt Source statement data from the parsed extension instance.
3020 * @param[in] parent Parent node to connect to (not into).
3021 * @param[in,out] siblings Siblings to add to.
3022 *
3023 * @return LY_ERR values.
3024 */
3025static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003026lysp_stmt_choice(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent,
Michal Vasko59892dd2022-05-13 11:02:30 +02003027 struct lysp_node **siblings)
Radek Krejci76c8c4e2021-02-17 10:16:48 +01003028{
3029 struct lysp_node_choice *choice;
3030
3031 LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_IDENTIF_ARG, stmt->arg));
3032
3033 /* create new choice structure */
3034 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, choice, next, LY_EMEM);
3035
3036 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &choice->name));
3037 choice->nodetype = LYS_CHOICE;
3038 choice->parent = parent;
3039
3040 /* parse substatements */
3041 for (const struct lysp_stmt *child = stmt->child; child; child = child->next) {
3042 switch (child->kw) {
3043 case LY_STMT_CONFIG:
3044 LY_CHECK_RET(lysp_stmt_config(ctx, child, &choice->flags, &choice->exts));
3045 break;
3046 case LY_STMT_DESCRIPTION:
3047 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &choice->dsc, Y_STR_ARG, &choice->exts));
3048 break;
3049 case LY_STMT_IF_FEATURE:
3050 LY_CHECK_RET(lysp_stmt_qnames(ctx, child, &choice->iffeatures, Y_STR_ARG, &choice->exts));
3051 break;
3052 case LY_STMT_MANDATORY:
3053 LY_CHECK_RET(lysp_stmt_mandatory(ctx, child, &choice->flags, &choice->exts));
3054 break;
3055 case LY_STMT_REFERENCE:
3056 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &choice->ref, Y_STR_ARG, &choice->exts));
3057 break;
3058 case LY_STMT_STATUS:
3059 LY_CHECK_RET(lysp_stmt_status(ctx, child, &choice->flags, &choice->exts));
3060 break;
3061 case LY_STMT_WHEN:
3062 LY_CHECK_RET(lysp_stmt_when(ctx, child, &choice->when));
3063 break;
3064 case LY_STMT_DEFAULT:
3065 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &choice->dflt.str, Y_PREF_IDENTIF_ARG, &choice->exts));
Michal Vasko8a67eff2021-12-07 14:04:47 +01003066 choice->dflt.mod = PARSER_CUR_PMOD(ctx);
Radek Krejci76c8c4e2021-02-17 10:16:48 +01003067 break;
3068 case LY_STMT_ANYDATA:
3069 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "choice");
3070 /* fall through */
3071 case LY_STMT_ANYXML:
3072 LY_CHECK_RET(lysp_stmt_any(ctx, child, &choice->node, &choice->child));
3073 break;
3074 case LY_STMT_CASE:
3075 LY_CHECK_RET(lysp_stmt_case(ctx, child, &choice->node, &choice->child));
3076 break;
3077 case LY_STMT_CHOICE:
3078 PARSER_CHECK_STMTVER2_RET(ctx, "choice", "choice");
3079 LY_CHECK_RET(lysp_stmt_choice(ctx, child, &choice->node, &choice->child));
3080 break;
3081 case LY_STMT_CONTAINER:
3082 LY_CHECK_RET(lysp_stmt_container(ctx, child, &choice->node, &choice->child));
3083 break;
3084 case LY_STMT_LEAF:
3085 LY_CHECK_RET(lysp_stmt_leaf(ctx, child, &choice->node, &choice->child));
3086 break;
3087 case LY_STMT_LEAF_LIST:
3088 LY_CHECK_RET(lysp_stmt_leaflist(ctx, child, &choice->node, &choice->child));
3089 break;
3090 case LY_STMT_LIST:
3091 LY_CHECK_RET(lysp_stmt_list(ctx, child, &choice->node, &choice->child));
3092 break;
3093 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003094 LY_CHECK_RET(lysp_stmt_ext(ctx, child, LY_STMT_CHOICE, 0, &choice->exts));
Radek Krejci76c8c4e2021-02-17 10:16:48 +01003095 break;
3096 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003097 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), "choice");
Radek Krejci76c8c4e2021-02-17 10:16:48 +01003098 return LY_EVALID;
3099 }
3100 }
3101 return LY_SUCCESS;
3102}
3103
3104/**
3105 * @brief Parse the container statement.
3106 *
3107 * @param[in] ctx parser context.
3108 * @param[in] stmt Source statement data from the parsed extension instance.
3109 * @param[in] parent Parent node to connect to (not into).
3110 * @param[in,out] siblings Siblings to add to.
3111 *
3112 * @return LY_ERR values.
3113 */
3114static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003115lysp_stmt_container(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent,
Michal Vasko59892dd2022-05-13 11:02:30 +02003116 struct lysp_node **siblings)
Radek Krejci76c8c4e2021-02-17 10:16:48 +01003117{
3118 struct lysp_node_container *cont;
3119
3120 LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_IDENTIF_ARG, stmt->arg));
3121
3122 /* create new container structure */
3123 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cont, next, LY_EMEM);
3124
3125 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &cont->name));
3126 cont->nodetype = LYS_CONTAINER;
3127 cont->parent = parent;
3128
3129 /* parse substatements */
3130 for (const struct lysp_stmt *child = stmt->child; child; child = child->next) {
3131 switch (child->kw) {
3132 case LY_STMT_CONFIG:
3133 LY_CHECK_RET(lysp_stmt_config(ctx, child, &cont->flags, &cont->exts));
3134 break;
3135 case LY_STMT_DESCRIPTION:
3136 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &cont->dsc, Y_STR_ARG, &cont->exts));
3137 break;
3138 case LY_STMT_IF_FEATURE:
3139 LY_CHECK_RET(lysp_stmt_qnames(ctx, child, &cont->iffeatures, Y_STR_ARG, &cont->exts));
3140 break;
3141 case LY_STMT_REFERENCE:
3142 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &cont->ref, Y_STR_ARG, &cont->exts));
3143 break;
3144 case LY_STMT_STATUS:
3145 LY_CHECK_RET(lysp_stmt_status(ctx, child, &cont->flags, &cont->exts));
3146 break;
3147 case LY_STMT_WHEN:
3148 LY_CHECK_RET(lysp_stmt_when(ctx, child, &cont->when));
3149 break;
3150 case LY_STMT_PRESENCE:
3151 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &cont->presence, Y_STR_ARG, &cont->exts));
3152 break;
3153 case LY_STMT_ANYDATA:
3154 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "container");
3155 /* fall through */
3156 case LY_STMT_ANYXML:
3157 LY_CHECK_RET(lysp_stmt_any(ctx, child, &cont->node, &cont->child));
3158 break;
3159 case LY_STMT_CHOICE:
3160 LY_CHECK_RET(lysp_stmt_choice(ctx, child, &cont->node, &cont->child));
3161 break;
3162 case LY_STMT_CONTAINER:
3163 LY_CHECK_RET(lysp_stmt_container(ctx, child, &cont->node, &cont->child));
3164 break;
3165 case LY_STMT_LEAF:
3166 LY_CHECK_RET(lysp_stmt_leaf(ctx, child, &cont->node, &cont->child));
3167 break;
3168 case LY_STMT_LEAF_LIST:
3169 LY_CHECK_RET(lysp_stmt_leaflist(ctx, child, &cont->node, &cont->child));
3170 break;
3171 case LY_STMT_LIST:
3172 LY_CHECK_RET(lysp_stmt_list(ctx, child, &cont->node, &cont->child));
3173 break;
3174 case LY_STMT_USES:
3175 LY_CHECK_RET(lysp_stmt_uses(ctx, child, &cont->node, &cont->child));
3176 break;
3177
3178 case LY_STMT_TYPEDEF:
3179 LY_CHECK_RET(lysp_stmt_typedef(ctx, child, &cont->node, &cont->typedefs));
3180 break;
3181 case LY_STMT_MUST:
3182 LY_CHECK_RET(lysp_stmt_restrs(ctx, child, &cont->musts));
3183 break;
3184 case LY_STMT_ACTION:
3185 PARSER_CHECK_STMTVER2_RET(ctx, "action", "container");
3186 LY_CHECK_RET(lysp_stmt_action(ctx, child, &cont->node, &cont->actions));
3187 break;
3188 case LY_STMT_GROUPING:
3189 LY_CHECK_RET(lysp_stmt_grouping(ctx, child, &cont->node, &cont->groupings));
3190 break;
3191 case LY_STMT_NOTIFICATION:
3192 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "container");
3193 LY_CHECK_RET(lysp_stmt_notif(ctx, child, &cont->node, &cont->notifs));
3194 break;
3195 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003196 LY_CHECK_RET(lysp_stmt_ext(ctx, child, LY_STMT_CONTAINER, 0, &cont->exts));
Radek Krejci76c8c4e2021-02-17 10:16:48 +01003197 break;
3198 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003199 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), "container");
Radek Krejci76c8c4e2021-02-17 10:16:48 +01003200 return LY_EVALID;
3201 }
3202 }
3203
3204 return LY_SUCCESS;
3205}
3206
3207/**
3208 * @brief Parse the list statement.
3209 *
3210 * @param[in] ctx parser context.
3211 * @param[in] stmt Source statement data from the parsed extension instance.
3212 * @param[in] parent Parent node to connect to (not into).
3213 * @param[in,out] siblings Siblings to add to.
3214 *
3215 * @return LY_ERR values.
3216 */
3217static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003218lysp_stmt_list(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent,
Michal Vasko59892dd2022-05-13 11:02:30 +02003219 struct lysp_node **siblings)
Radek Krejci76c8c4e2021-02-17 10:16:48 +01003220{
3221 struct lysp_node_list *list;
3222
3223 LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_IDENTIF_ARG, stmt->arg));
3224
3225 /* create new list structure */
3226 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, list, next, LY_EMEM);
3227
3228 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &list->name));
3229 list->nodetype = LYS_LIST;
3230 list->parent = parent;
3231
3232 /* parse substatements */
3233 for (const struct lysp_stmt *child = stmt->child; child; child = child->next) {
3234 switch (child->kw) {
3235 case LY_STMT_CONFIG:
3236 LY_CHECK_RET(lysp_stmt_config(ctx, child, &list->flags, &list->exts));
3237 break;
3238 case LY_STMT_DESCRIPTION:
3239 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &list->dsc, Y_STR_ARG, &list->exts));
3240 break;
3241 case LY_STMT_IF_FEATURE:
3242 LY_CHECK_RET(lysp_stmt_qnames(ctx, child, &list->iffeatures, Y_STR_ARG, &list->exts));
3243 break;
3244 case LY_STMT_REFERENCE:
3245 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &list->ref, Y_STR_ARG, &list->exts));
3246 break;
3247 case LY_STMT_STATUS:
3248 LY_CHECK_RET(lysp_stmt_status(ctx, child, &list->flags, &list->exts));
3249 break;
3250 case LY_STMT_WHEN:
3251 LY_CHECK_RET(lysp_stmt_when(ctx, child, &list->when));
3252 break;
3253 case LY_STMT_KEY:
3254 LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &list->key, Y_STR_ARG, &list->exts));
3255 break;
3256 case LY_STMT_MAX_ELEMENTS:
3257 LY_CHECK_RET(lysp_stmt_maxelements(ctx, child, &list->max, &list->flags, &list->exts));
3258 break;
3259 case LY_STMT_MIN_ELEMENTS:
3260 LY_CHECK_RET(lysp_stmt_minelements(ctx, child, &list->min, &list->flags, &list->exts));
3261 break;
3262 case LY_STMT_ORDERED_BY:
3263 LY_CHECK_RET(lysp_stmt_orderedby(ctx, child, &list->flags, &list->exts));
3264 break;
3265 case LY_STMT_UNIQUE:
3266 LY_CHECK_RET(lysp_stmt_qnames(ctx, child, &list->uniques, Y_STR_ARG, &list->exts));
3267 break;
3268
3269 case LY_STMT_ANYDATA:
3270 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "list");
3271 /* fall through */
3272 case LY_STMT_ANYXML:
3273 LY_CHECK_RET(lysp_stmt_any(ctx, child, &list->node, &list->child));
3274 break;
3275 case LY_STMT_CHOICE:
3276 LY_CHECK_RET(lysp_stmt_choice(ctx, child, &list->node, &list->child));
3277 break;
3278 case LY_STMT_CONTAINER:
3279 LY_CHECK_RET(lysp_stmt_container(ctx, child, &list->node, &list->child));
3280 break;
3281 case LY_STMT_LEAF:
3282 LY_CHECK_RET(lysp_stmt_leaf(ctx, child, &list->node, &list->child));
3283 break;
3284 case LY_STMT_LEAF_LIST:
3285 LY_CHECK_RET(lysp_stmt_leaflist(ctx, child, &list->node, &list->child));
3286 break;
3287 case LY_STMT_LIST:
3288 LY_CHECK_RET(lysp_stmt_list(ctx, child, &list->node, &list->child));
3289 break;
3290 case LY_STMT_USES:
3291 LY_CHECK_RET(lysp_stmt_uses(ctx, child, &list->node, &list->child));
3292 break;
3293
3294 case LY_STMT_TYPEDEF:
3295 LY_CHECK_RET(lysp_stmt_typedef(ctx, child, &list->node, &list->typedefs));
3296 break;
3297 case LY_STMT_MUST:
3298 LY_CHECK_RET(lysp_stmt_restrs(ctx, child, &list->musts));
3299 break;
3300 case LY_STMT_ACTION:
3301 PARSER_CHECK_STMTVER2_RET(ctx, "action", "list");
3302 LY_CHECK_RET(lysp_stmt_action(ctx, child, &list->node, &list->actions));
3303 break;
3304 case LY_STMT_GROUPING:
3305 LY_CHECK_RET(lysp_stmt_grouping(ctx, child, &list->node, &list->groupings));
3306 break;
3307 case LY_STMT_NOTIFICATION:
3308 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "list");
3309 LY_CHECK_RET(lysp_stmt_notif(ctx, child, &list->node, &list->notifs));
3310 break;
3311 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003312 LY_CHECK_RET(lysp_stmt_ext(ctx, child, LY_STMT_LIST, 0, &list->exts));
Radek Krejci76c8c4e2021-02-17 10:16:48 +01003313 break;
3314 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003315 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), "list");
Radek Krejci76c8c4e2021-02-17 10:16:48 +01003316 return LY_EVALID;
3317 }
3318 }
3319
3320 return LY_SUCCESS;
3321}
3322
Michal Vasko193dacd2022-10-13 08:43:05 +02003323/**
3324 * @brief Parse generic statement structure into a specific parsed-schema structure.
3325 *
3326 * @param[in] pctx Parse context of the @p stmt being processed.
3327 * @param[in] stmt Generic statement structure to process.
3328 * @param[out] result Specific parsed-schema structure for the given statement. For the specific type for the particular statement, check the function code.
3329 * @param[in,out] exts [sized array](@ref sizedarrays) For extension instances in case of statements that do not store extension instances in their own list.
3330 * @return LY_ERR value.
3331 */
3332static LY_ERR
3333lysp_stmt_parse(struct lysp_ctx *pctx, const struct lysp_stmt *stmt, void **result, struct lysp_ext_instance **exts)
Radek Krejci335332a2019-09-05 13:03:35 +02003334{
Radek Krejciad5963b2019-09-06 16:03:05 +02003335 LY_ERR ret = LY_SUCCESS;
Radek Krejci76c8c4e2021-02-17 10:16:48 +01003336 uint16_t flags;
Radek Krejci335332a2019-09-05 13:03:35 +02003337
Radek Krejci76c8c4e2021-02-17 10:16:48 +01003338 switch (stmt->kw) {
Michal Vasko193dacd2022-10-13 08:43:05 +02003339 case LY_STMT_NOTIFICATION:
3340 ret = lysp_stmt_notif(pctx, stmt, NULL, (struct lysp_node_notif **)result);
3341 break;
3342 case LY_STMT_INPUT:
3343 case LY_STMT_OUTPUT: {
3344 struct lysp_node_action_inout *inout;
3345
3346 *result = inout = calloc(1, sizeof *inout);
3347 LY_CHECK_ERR_RET(!inout, LOGMEM(PARSER_CTX(pctx)), LY_EMEM);
3348 ret = lysp_stmt_inout(pctx, stmt, NULL, inout);
3349 break;
3350 }
Radek Krejci76c8c4e2021-02-17 10:16:48 +01003351 case LY_STMT_ACTION:
3352 case LY_STMT_RPC:
Michal Vasko193dacd2022-10-13 08:43:05 +02003353 ret = lysp_stmt_action(pctx, stmt, NULL, (struct lysp_node_action **)result);
Radek Krejci76c8c4e2021-02-17 10:16:48 +01003354 break;
3355 case LY_STMT_ANYDATA:
3356 case LY_STMT_ANYXML:
Michal Vasko193dacd2022-10-13 08:43:05 +02003357 ret = lysp_stmt_any(pctx, stmt, NULL, (struct lysp_node **)result);
Radek Krejci76c8c4e2021-02-17 10:16:48 +01003358 break;
3359 case LY_STMT_AUGMENT:
Michal Vasko193dacd2022-10-13 08:43:05 +02003360 ret = lysp_stmt_augment(pctx, stmt, NULL, (struct lysp_node_augment **)result);
Radek Krejci76c8c4e2021-02-17 10:16:48 +01003361 break;
3362 case LY_STMT_CASE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003363 ret = lysp_stmt_case(pctx, stmt, NULL, (struct lysp_node **)result);
Radek Krejci76c8c4e2021-02-17 10:16:48 +01003364 break;
3365 case LY_STMT_CHOICE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003366 ret = lysp_stmt_choice(pctx, stmt, NULL, (struct lysp_node **)result);
Radek Krejci76c8c4e2021-02-17 10:16:48 +01003367 break;
Michal Vasko193dacd2022-10-13 08:43:05 +02003368 case LY_STMT_CONTAINER:
3369 ret = lysp_stmt_container(pctx, stmt, NULL, (struct lysp_node **)result);
Radek Krejci76c8c4e2021-02-17 10:16:48 +01003370 break;
Michal Vasko193dacd2022-10-13 08:43:05 +02003371 case LY_STMT_GROUPING:
3372 ret = lysp_stmt_grouping(pctx, stmt, NULL, (struct lysp_node_grp **)result);
3373 break;
3374 case LY_STMT_LEAF:
3375 ret = lysp_stmt_leaf(pctx, stmt, NULL, (struct lysp_node **)result);
3376 break;
3377 case LY_STMT_LEAF_LIST:
3378 ret = lysp_stmt_leaflist(pctx, stmt, NULL, (struct lysp_node **)result);
3379 break;
3380 case LY_STMT_LIST:
3381 ret = lysp_stmt_list(pctx, stmt, NULL, (struct lysp_node **)result);
3382 break;
3383 case LY_STMT_USES:
3384 ret = lysp_stmt_uses(pctx, stmt, NULL, (struct lysp_node **)result);
3385 break;
3386 case LY_STMT_BASE:
3387 ret = lysp_stmt_text_fields(pctx, stmt, (const char ***)result, Y_PREF_IDENTIF_ARG, exts);
3388 break;
3389 case LY_STMT_ARGUMENT:
3390 case LY_STMT_BELONGS_TO:
Radek Krejci76c8c4e2021-02-17 10:16:48 +01003391 case LY_STMT_CONTACT:
3392 case LY_STMT_DESCRIPTION:
3393 case LY_STMT_ERROR_APP_TAG:
3394 case LY_STMT_ERROR_MESSAGE:
3395 case LY_STMT_KEY:
3396 case LY_STMT_NAMESPACE:
3397 case LY_STMT_ORGANIZATION:
3398 case LY_STMT_PRESENCE:
3399 case LY_STMT_REFERENCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003400 case LY_STMT_REVISION_DATE:
Radek Krejci76c8c4e2021-02-17 10:16:48 +01003401 case LY_STMT_UNITS:
Michal Vasko193dacd2022-10-13 08:43:05 +02003402 ret = lysp_stmt_text_field(pctx, stmt, 0, (const char **)result, Y_STR_ARG, exts);
Radek Krejci76c8c4e2021-02-17 10:16:48 +01003403 break;
Michal Vasko193dacd2022-10-13 08:43:05 +02003404 case LY_STMT_BIT:
3405 case LY_STMT_ENUM:
3406 ret = lysp_stmt_type_enum(pctx, stmt, (struct lysp_type_enum **)result);
3407 break;
3408 case LY_STMT_CONFIG:
3409 assert(*result);
3410 ret = lysp_stmt_config(pctx, stmt, *(uint16_t **)result, exts);
Radek Krejci76c8c4e2021-02-17 10:16:48 +01003411 break;
3412 case LY_STMT_DEFAULT:
3413 case LY_STMT_IF_FEATURE:
3414 case LY_STMT_UNIQUE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003415 ret = lysp_stmt_qnames(pctx, stmt, (struct lysp_qname **)result, Y_STR_ARG, exts);
3416 break;
3417 case LY_STMT_DEVIATE:
3418 ret = lysp_stmt_deviate(pctx, stmt, (struct lysp_deviate **)result, exts);
3419 break;
3420 case LY_STMT_DEVIATION:
3421 ret = lysp_stmt_deviation(pctx, stmt, (struct lysp_deviation **)result);
3422 break;
3423 case LY_STMT_EXTENSION:
3424 ret = lysp_stmt_extension(pctx, stmt, (struct lysp_ext **)result);
Radek Krejci76c8c4e2021-02-17 10:16:48 +01003425 break;
3426 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003427 ret = lysp_stmt_ext(pctx, stmt, LY_STMT_EXTENSION_INSTANCE, 0, (struct lysp_ext_instance **)result);
3428 break;
3429 case LY_STMT_FEATURE:
3430 ret = lysp_stmt_feature(pctx, stmt, (struct lysp_feature **)result);
Radek Krejci76c8c4e2021-02-17 10:16:48 +01003431 break;
3432 case LY_STMT_FRACTION_DIGITS:
Michal Vasko193dacd2022-10-13 08:43:05 +02003433 ret = lysp_stmt_type_fracdigits(pctx, stmt, *(uint8_t **)result, exts);
Radek Krejci76c8c4e2021-02-17 10:16:48 +01003434 break;
3435 case LY_STMT_LENGTH:
Michal Vasko193dacd2022-10-13 08:43:05 +02003436 case LY_STMT_RANGE: {
3437 struct lysp_restr *restr;
3438
3439 *result = restr = calloc(1, sizeof *restr);
3440 LY_CHECK_ERR_RET(!restr, LOGMEM(PARSER_CTX(pctx)), LY_EMEM);
3441
3442 ret = lysp_stmt_restr(pctx, stmt, restr);
Radek Krejci76c8c4e2021-02-17 10:16:48 +01003443 break;
Michal Vasko193dacd2022-10-13 08:43:05 +02003444 }
3445 case LY_STMT_MUST:
3446 ret = lysp_stmt_restrs(pctx, stmt, (struct lysp_restr **)result);
3447 break;
3448 case LY_STMT_IDENTITY:
3449 ret = lysp_stmt_identity(pctx, stmt, (struct lysp_ident **)result);
3450 break;
3451 case LY_STMT_IMPORT:
3452 ret = lysp_stmt_import(pctx, stmt, (struct lysp_import **)result);
3453 break;
3454 case LY_STMT_INCLUDE:
3455 ret = lysp_stmt_include(pctx, stmt, (struct lysp_include **)result);
Radek Krejci76c8c4e2021-02-17 10:16:48 +01003456 break;
3457 case LY_STMT_MANDATORY:
Michal Vasko193dacd2022-10-13 08:43:05 +02003458 ret = lysp_stmt_mandatory(pctx, stmt, *(uint16_t **)result, exts);
Radek Krejci76c8c4e2021-02-17 10:16:48 +01003459 break;
3460 case LY_STMT_MAX_ELEMENTS:
3461 flags = 0;
Michal Vasko193dacd2022-10-13 08:43:05 +02003462 ret = lysp_stmt_maxelements(pctx, stmt, *(uint32_t **)result, &flags, exts);
Radek Krejci76c8c4e2021-02-17 10:16:48 +01003463 break;
3464 case LY_STMT_MIN_ELEMENTS:
3465 flags = 0;
Michal Vasko193dacd2022-10-13 08:43:05 +02003466 ret = lysp_stmt_minelements(pctx, stmt, *(uint32_t **)result, &flags, exts);
Radek Krejci76c8c4e2021-02-17 10:16:48 +01003467 break;
3468 case LY_STMT_MODIFIER:
Michal Vasko193dacd2022-10-13 08:43:05 +02003469 ret = lysp_stmt_type_pattern_modifier(pctx, stmt, (const char **)result, exts);
Radek Krejci76c8c4e2021-02-17 10:16:48 +01003470 break;
Michal Vasko193dacd2022-10-13 08:43:05 +02003471 case LY_STMT_MODULE: {
3472 struct lysp_module *mod;
3473
3474 *result = mod = calloc(1, sizeof *mod);
3475 LY_CHECK_ERR_RET(!mod, LOGMEM(PARSER_CTX(pctx)), LY_EMEM);
3476 ret = lysp_stmt_module(pctx, stmt, mod);
Radek Krejci76c8c4e2021-02-17 10:16:48 +01003477 break;
Michal Vasko193dacd2022-10-13 08:43:05 +02003478 }
Radek Krejci76c8c4e2021-02-17 10:16:48 +01003479 case LY_STMT_ORDERED_BY:
Michal Vasko193dacd2022-10-13 08:43:05 +02003480 ret = lysp_stmt_orderedby(pctx, stmt, *(uint16_t **)result, exts);
Radek Krejci76c8c4e2021-02-17 10:16:48 +01003481 break;
3482 case LY_STMT_PATH: {
3483 const char *str_path = NULL;
3484
Michal Vasko193dacd2022-10-13 08:43:05 +02003485 LY_CHECK_RET(lysp_stmt_text_field(pctx, stmt, 0, &str_path, Y_STR_ARG, exts));
3486 ret = ly_path_parse(PARSER_CTX(pctx), NULL, str_path, 0, 1, LY_PATH_BEGIN_EITHER,
Radek Krejci76c8c4e2021-02-17 10:16:48 +01003487 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, (struct lyxp_expr **)result);
Michal Vasko193dacd2022-10-13 08:43:05 +02003488 lydict_remove(PARSER_CTX(pctx), str_path);
Radek Krejci76c8c4e2021-02-17 10:16:48 +01003489 break;
3490 }
3491 case LY_STMT_PATTERN:
Michal Vasko193dacd2022-10-13 08:43:05 +02003492 ret = lysp_stmt_type_pattern(pctx, stmt, (struct lysp_restr **)result);
Radek Krejci76c8c4e2021-02-17 10:16:48 +01003493 break;
3494 case LY_STMT_POSITION:
3495 case LY_STMT_VALUE:
3496 flags = 0;
Michal Vasko193dacd2022-10-13 08:43:05 +02003497 ret = lysp_stmt_type_enum_value_pos(pctx, stmt, *(int64_t **)result, &flags, exts);
Radek Krejci76c8c4e2021-02-17 10:16:48 +01003498 break;
3499 case LY_STMT_PREFIX:
Michal Vasko193dacd2022-10-13 08:43:05 +02003500 ret = lysp_stmt_text_field(pctx, stmt, 0, (const char **)result, Y_IDENTIF_ARG, exts);
Radek Krejci76c8c4e2021-02-17 10:16:48 +01003501 break;
3502 case LY_STMT_REFINE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003503 ret = lysp_stmt_refine(pctx, stmt, (struct lysp_refine **)result);
Radek Krejci76c8c4e2021-02-17 10:16:48 +01003504 break;
3505 case LY_STMT_REQUIRE_INSTANCE:
3506 flags = 0;
Michal Vasko193dacd2022-10-13 08:43:05 +02003507 ret = lysp_stmt_type_reqinstance(pctx, stmt, *(uint8_t **)result, &flags, exts);
3508 break;
3509 case LY_STMT_REVISION:
3510 ret = lysp_stmt_revision(pctx, stmt, (struct lysp_revision **)result);
Radek Krejci76c8c4e2021-02-17 10:16:48 +01003511 break;
Michal Vasko69730152020-10-09 16:30:07 +02003512 case LY_STMT_STATUS:
Michal Vasko193dacd2022-10-13 08:43:05 +02003513 ret = lysp_stmt_status(pctx, stmt, (uint16_t *)result, exts);
Radek Krejciad5963b2019-09-06 16:03:05 +02003514 break;
Michal Vasko193dacd2022-10-13 08:43:05 +02003515 case LY_STMT_SUBMODULE: {
3516 struct lysp_submodule *submod;
3517
3518 *result = submod = calloc(1, sizeof *submod);
3519 LY_CHECK_ERR_RET(!submod, LOGMEM(PARSER_CTX(pctx)), LY_EMEM);
3520 ret = lysp_stmt_submodule(pctx, stmt, submod);
3521 break;
3522 }
Radek Krejci335332a2019-09-05 13:03:35 +02003523 case LY_STMT_TYPE: {
3524 struct lysp_type *type;
Radek Krejci335332a2019-09-05 13:03:35 +02003525
Radek Krejci76c8c4e2021-02-17 10:16:48 +01003526 *result = type = calloc(1, sizeof *type);
Michal Vasko193dacd2022-10-13 08:43:05 +02003527 LY_CHECK_ERR_RET(!type, LOGMEM(PARSER_CTX(pctx)), LY_EMEM);
3528 ret = lysp_stmt_type(pctx, stmt, type);
Radek Krejci335332a2019-09-05 13:03:35 +02003529 break;
Radek Krejci0f969882020-08-21 16:56:47 +02003530 }
Radek Krejci76c8c4e2021-02-17 10:16:48 +01003531 case LY_STMT_TYPEDEF:
Michal Vasko193dacd2022-10-13 08:43:05 +02003532 ret = lysp_stmt_typedef(pctx, stmt, NULL, (struct lysp_tpdf **)result);
Radek Krejci76c8c4e2021-02-17 10:16:48 +01003533 break;
3534 case LY_STMT_WHEN:
Michal Vasko193dacd2022-10-13 08:43:05 +02003535 ret = lysp_stmt_when(pctx, stmt, (struct lysp_when **)result);
3536 break;
3537 case LY_STMT_YANG_VERSION:
3538 ret = lysp_stmt_yangver(pctx, stmt, *(uint8_t **)result, exts);
3539 break;
3540 case LY_STMT_YIN_ELEMENT:
3541 ret = lysp_stmt_yinelem(pctx, stmt, *(uint16_t **)result, exts);
Radek Krejci76c8c4e2021-02-17 10:16:48 +01003542 break;
Radek Krejci335332a2019-09-05 13:03:35 +02003543 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003544 LOGINT(PARSER_CTX(pctx));
Radek Krejci335332a2019-09-05 13:03:35 +02003545 return LY_EINT;
3546 }
3547
Radek Krejciad5963b2019-09-06 16:03:05 +02003548 return ret;
Radek Krejci335332a2019-09-05 13:03:35 +02003549}
Michal Vasko59892dd2022-05-13 11:02:30 +02003550
Michal Vasko193dacd2022-10-13 08:43:05 +02003551LY_ERR
3552lys_parse_ext_instance_stmt(struct lysp_ctx *pctx, struct lysp_ext_substmt *substmt, struct lysp_stmt *stmt)
Michal Vasko59892dd2022-05-13 11:02:30 +02003553{
Michal Vasko193dacd2022-10-13 08:43:05 +02003554 LY_ERR rc = LY_SUCCESS;
Michal Vasko59892dd2022-05-13 11:02:30 +02003555
Michal Vasko193dacd2022-10-13 08:43:05 +02003556 if (!substmt->storage) {
3557 /* nothing to parse, ignored */
3558 goto cleanup;
Michal Vasko59892dd2022-05-13 11:02:30 +02003559 }
3560
Michal Vasko193dacd2022-10-13 08:43:05 +02003561 switch (stmt->kw) {
3562 case LY_STMT_NOTIFICATION:
3563 case LY_STMT_INPUT:
3564 case LY_STMT_OUTPUT:
3565 case LY_STMT_ACTION:
3566 case LY_STMT_RPC:
3567 case LY_STMT_ANYDATA:
3568 case LY_STMT_ANYXML:
3569 case LY_STMT_AUGMENT:
3570 case LY_STMT_CASE:
3571 case LY_STMT_CHOICE:
3572 case LY_STMT_CONTAINER:
3573 case LY_STMT_GROUPING:
3574 case LY_STMT_LEAF:
3575 case LY_STMT_LEAF_LIST:
3576 case LY_STMT_LIST:
3577 case LY_STMT_USES: {
3578 struct lysp_node **pnodes_p, *pnode = NULL;
3579
3580 /* parse the node */
3581 LY_CHECK_GOTO(rc = lysp_stmt_parse(pctx, stmt, (void **)&pnode, NULL), cleanup);
3582
3583 /* usually is a linked-list of all the parsed schema nodes */
3584 pnodes_p = substmt->storage;
3585 while (*pnodes_p) {
3586 pnodes_p = &(*pnodes_p)->next;
Michal Vasko59892dd2022-05-13 11:02:30 +02003587 }
Michal Vasko193dacd2022-10-13 08:43:05 +02003588 *pnodes_p = pnode;
Michal Vasko59892dd2022-05-13 11:02:30 +02003589
3590 break;
Michal Vasko193dacd2022-10-13 08:43:05 +02003591 }
3592 case LY_STMT_BASE:
3593 case LY_STMT_BIT:
3594 case LY_STMT_DEFAULT:
3595 case LY_STMT_DEVIATE:
3596 case LY_STMT_DEVIATION:
3597 case LY_STMT_ENUM:
3598 case LY_STMT_EXTENSION:
3599 case LY_STMT_EXTENSION_INSTANCE:
3600 case LY_STMT_FEATURE:
3601 case LY_STMT_IDENTITY:
3602 case LY_STMT_IF_FEATURE:
3603 case LY_STMT_IMPORT:
3604 case LY_STMT_INCLUDE:
3605 case LY_STMT_MUST:
3606 case LY_STMT_PATTERN:
3607 case LY_STMT_REFINE:
3608 case LY_STMT_REVISION:
3609 case LY_STMT_TYPEDEF:
3610 case LY_STMT_UNIQUE:
3611 /* parse, sized array */
3612 LY_CHECK_GOTO(rc = lysp_stmt_parse(pctx, stmt, substmt->storage, NULL), cleanup);
Michal Vasko59892dd2022-05-13 11:02:30 +02003613 break;
Michal Vasko193dacd2022-10-13 08:43:05 +02003614
3615 case LY_STMT_ARGUMENT:
3616 case LY_STMT_BELONGS_TO:
3617 case LY_STMT_CONTACT:
3618 case LY_STMT_DESCRIPTION:
3619 case LY_STMT_ERROR_APP_TAG:
3620 case LY_STMT_ERROR_MESSAGE:
3621 case LY_STMT_FRACTION_DIGITS:
3622 case LY_STMT_KEY:
3623 case LY_STMT_LENGTH:
3624 case LY_STMT_MANDATORY:
3625 case LY_STMT_MAX_ELEMENTS:
3626 case LY_STMT_MIN_ELEMENTS:
3627 case LY_STMT_MODIFIER:
3628 case LY_STMT_MODULE:
3629 case LY_STMT_NAMESPACE:
3630 case LY_STMT_ORGANIZATION:
3631 case LY_STMT_PATH:
3632 case LY_STMT_POSITION:
3633 case LY_STMT_PREFIX:
3634 case LY_STMT_PRESENCE:
3635 case LY_STMT_RANGE:
3636 case LY_STMT_REFERENCE:
3637 case LY_STMT_REQUIRE_INSTANCE:
3638 case LY_STMT_REVISION_DATE:
3639 case LY_STMT_SUBMODULE:
3640 case LY_STMT_TYPE:
3641 case LY_STMT_UNITS:
3642 case LY_STMT_VALUE:
3643 case LY_STMT_WHEN:
3644 case LY_STMT_YANG_VERSION:
3645 case LY_STMT_YIN_ELEMENT:
3646 /* single item */
3647 if (*(void **)substmt->storage) {
3648 LOGVAL(PARSER_CTX(pctx), LY_VCODE_DUPSTMT, stmt->stmt);
3649 rc = LY_EVALID;
3650 goto cleanup;
3651 }
3652
3653 /* parse */
3654 LY_CHECK_GOTO(rc = lysp_stmt_parse(pctx, stmt, substmt->storage, NULL), cleanup);
Michal Vasko59892dd2022-05-13 11:02:30 +02003655 break;
Michal Vasko193dacd2022-10-13 08:43:05 +02003656
3657 case LY_STMT_CONFIG:
3658 /* single item */
3659 if ((*(uint16_t *)substmt->storage) & LYS_CONFIG_MASK) {
3660 LOGVAL(PARSER_CTX(pctx), LY_VCODE_DUPSTMT, stmt->stmt);
3661 rc = LY_EVALID;
3662 goto cleanup;
3663 }
3664
3665 /* parse */
3666 LY_CHECK_GOTO(rc = lysp_stmt_parse(pctx, stmt, substmt->storage, NULL), cleanup);
3667 break;
3668
3669 case LY_STMT_ORDERED_BY:
3670 /* single item */
3671 if ((*(uint16_t *)substmt->storage) & LYS_ORDBY_MASK) {
3672 LOGVAL(PARSER_CTX(pctx), LY_VCODE_DUPSTMT, stmt->stmt);
3673 rc = LY_EVALID;
3674 goto cleanup;
3675 }
3676
3677 /* parse */
3678 LY_CHECK_GOTO(rc = lysp_stmt_parse(pctx, stmt, substmt->storage, NULL), cleanup);
3679 break;
3680
3681 case LY_STMT_STATUS:
3682 /* single item */
3683 if ((*(uint16_t *)substmt->storage) & LYS_STATUS_MASK) {
3684 LOGVAL(PARSER_CTX(pctx), LY_VCODE_DUPSTMT, stmt->stmt);
3685 rc = LY_EVALID;
3686 goto cleanup;
3687 }
3688
3689 /* parse */
3690 LY_CHECK_GOTO(rc = lysp_stmt_parse(pctx, stmt, substmt->storage, NULL), cleanup);
3691 break;
3692
Michal Vasko59892dd2022-05-13 11:02:30 +02003693 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003694 LOGINT(PARSER_CTX(pctx));
3695 rc = LY_EINT;
3696 goto cleanup;
Michal Vasko59892dd2022-05-13 11:02:30 +02003697 }
Michal Vasko193dacd2022-10-13 08:43:05 +02003698
3699cleanup:
3700 return rc;
Michal Vasko59892dd2022-05-13 11:02:30 +02003701}