blob: eb832eb598811ec31d11f1c2f271e80b3f2ef48a [file] [log] [blame]
Radek Krejcib1646a92018-11-02 16:08:26 +01001/**
2 * @file xpath.h
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief YANG XPath evaluation functions header
5 *
Michal Vasko519fd602020-05-26 12:17:39 +02006 * Copyright (c) 2015 - 2020 CESNET, z.s.p.o.
Radek Krejcib1646a92018-11-02 16:08:26 +01007 *
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 Vasko14676352020-05-29 11:35:55 +020015#ifndef LY_XPATH_H
16#define LY_XPATH_H
Radek Krejcib1646a92018-11-02 16:08:26 +010017
Radek Krejciad97c5f2020-06-30 09:19:28 +020018#include <stddef.h>
Michal Vasko69730152020-10-09 16:30:07 +020019#include <stdint.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010020
Michal Vaskoc5a22832020-08-20 13:21:33 +020021#include "compat.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include "log.h"
Radek Krejci77114102021-03-10 15:21:57 +010023#include "tree.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020024#include "tree_schema.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020025
26struct ly_ctx;
Radek Krejci77114102021-03-10 15:21:57 +010027struct lyd_node;
Radek Krejcie7b95092019-05-15 11:03:07 +020028
Radek Krejci84d7fd72021-07-14 18:32:21 +020029/**
30 * @internal
31 * @page internals
32 * @section internalsXpath XPath Implementation
33 *
Radek Krejcib1646a92018-11-02 16:08:26 +010034 * XPath evaluator fully compliant with http://www.w3.org/TR/1999/REC-xpath-19991116/
35 * except the following restrictions in the grammar.
36 *
Radek Krejci84d7fd72021-07-14 18:32:21 +020037 * @subsection internalsXpathGrammar Parsed Grammar
Radek Krejcib1646a92018-11-02 16:08:26 +010038 *
39 * Full axes are not supported, abbreviated forms must be used,
40 * variables are not supported, "id()" function is not supported,
41 * and processing instruction and comment nodes are not supported,
42 * which is also reflected in the grammar. Undefined rules and
43 * constants are tokens.
44 *
45 * Modified full grammar:
Radek Krejci84d7fd72021-07-14 18:32:21 +020046 * @code
Radek Krejcib1646a92018-11-02 16:08:26 +010047 * [1] Expr ::= OrExpr // just an alias
48 *
49 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
50 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
51 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
52 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
53 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vaskod3678892020-05-21 10:06:58 +020054 * [7] NameTest ::= '*' | NCName ':' '*' | QName
55 * [8] NodeType ::= 'text' | 'node'
56 * [9] Predicate ::= '[' Expr ']'
57 * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
58 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
59 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Radek Krejcib1646a92018-11-02 16:08:26 +010060 * | PrimaryExpr Predicate* '/' RelativeLocationPath
61 * | PrimaryExpr Predicate* '//' RelativeLocationPath
Michal Vaskod3678892020-05-21 10:06:58 +020062 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
63 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
64 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Radek Krejcib1646a92018-11-02 16:08:26 +010065 * | EqualityExpr '!=' RelationalExpr
Michal Vaskod3678892020-05-21 10:06:58 +020066 * [16] RelationalExpr ::= AdditiveExpr
Radek Krejcib1646a92018-11-02 16:08:26 +010067 * | RelationalExpr '<' AdditiveExpr
68 * | RelationalExpr '>' AdditiveExpr
69 * | RelationalExpr '<=' AdditiveExpr
70 * | RelationalExpr '>=' AdditiveExpr
Michal Vaskod3678892020-05-21 10:06:58 +020071 * [17] AdditiveExpr ::= MultiplicativeExpr
Radek Krejcib1646a92018-11-02 16:08:26 +010072 * | AdditiveExpr '+' MultiplicativeExpr
73 * | AdditiveExpr '-' MultiplicativeExpr
Michal Vaskod3678892020-05-21 10:06:58 +020074 * [18] MultiplicativeExpr ::= UnaryExpr
Radek Krejcib1646a92018-11-02 16:08:26 +010075 * | MultiplicativeExpr '*' UnaryExpr
76 * | MultiplicativeExpr 'div' UnaryExpr
77 * | MultiplicativeExpr 'mod' UnaryExpr
Michal Vaskod3678892020-05-21 10:06:58 +020078 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
79 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Radek Krejci84d7fd72021-07-14 18:32:21 +020080 * @endcode
Radek Krejcib1646a92018-11-02 16:08:26 +010081 */
82
83/* expression tokens allocation */
84#define LYXP_EXPR_SIZE_START 10
85#define LYXP_EXPR_SIZE_STEP 5
86
87/* XPath matches allocation */
88#define LYXP_SET_SIZE_START 2
89#define LYXP_SET_SIZE_STEP 2
90
91/* building string when casting */
92#define LYXP_STRING_CAST_SIZE_START 64
93#define LYXP_STRING_CAST_SIZE_STEP 16
94
aPiecekbf968d92021-05-27 14:35:05 +020095/* Maximum number of nested expressions. */
96#define LYXP_MAX_BLOCK_DEPTH 100
97
Radek Krejcib1646a92018-11-02 16:08:26 +010098/**
99 * @brief Tokens that can be in an XPath expression.
100 */
101enum lyxp_token {
102 LYXP_TOKEN_NONE = 0,
103 LYXP_TOKEN_PAR1, /* '(' */
104 LYXP_TOKEN_PAR2, /* ')' */
105 LYXP_TOKEN_BRACK1, /* '[' */
106 LYXP_TOKEN_BRACK2, /* ']' */
107 LYXP_TOKEN_DOT, /* '.' */
108 LYXP_TOKEN_DDOT, /* '..' */
109 LYXP_TOKEN_AT, /* '@' */
110 LYXP_TOKEN_COMMA, /* ',' */
111 /* LYXP_TOKEN_DCOLON, * '::' * axes not supported */
112 LYXP_TOKEN_NAMETEST, /* NameTest */
113 LYXP_TOKEN_NODETYPE, /* NodeType */
114 LYXP_TOKEN_FUNCNAME, /* FunctionName */
Michal Vasko3e48bf32020-06-01 08:39:07 +0200115 LYXP_TOKEN_OPER_LOG, /* Operator 'and', 'or' */
116 LYXP_TOKEN_OPER_EQUAL, /* Operator '=' */
117 LYXP_TOKEN_OPER_NEQUAL, /* Operator '!=' */
118 LYXP_TOKEN_OPER_COMP, /* Operator '<', '<=', '>', '>=' */
119 LYXP_TOKEN_OPER_MATH, /* Operator '+', '-', '*', 'div', 'mod', '-' (unary) */
120 LYXP_TOKEN_OPER_UNI, /* Operator '|' */
121 LYXP_TOKEN_OPER_PATH, /* Operator '/' */
122 LYXP_TOKEN_OPER_RPATH, /* Operator '//' (recursive path) */
Radek Krejcib1646a92018-11-02 16:08:26 +0100123 /* LYXP_TOKEN_AXISNAME, * AxisName * axes not supported */
124 LYXP_TOKEN_LITERAL, /* Literal - with either single or double quote */
125 LYXP_TOKEN_NUMBER /* Number */
126};
127
128/**
129 * @brief XPath (sub)expressions that can be repeated.
130 */
131enum lyxp_expr_type {
132 LYXP_EXPR_NONE = 0,
133 LYXP_EXPR_OR,
134 LYXP_EXPR_AND,
135 LYXP_EXPR_EQUALITY,
136 LYXP_EXPR_RELATIONAL,
137 LYXP_EXPR_ADDITIVE,
138 LYXP_EXPR_MULTIPLICATIVE,
139 LYXP_EXPR_UNARY,
Michal Vasko69730152020-10-09 16:30:07 +0200140 LYXP_EXPR_UNION
Radek Krejcib1646a92018-11-02 16:08:26 +0100141};
142
143/**
Michal Vasko03ff5a72019-09-11 13:49:33 +0200144 * @brief Types of context nodes, #LYXP_NODE_ROOT_CONFIG used only in when or must conditions.
145 */
146enum lyxp_node_type {
Michal Vasko2caefc12019-11-14 16:07:56 +0100147 LYXP_NODE_NONE, /* invalid node type */
148
Michal Vasko03ff5a72019-09-11 13:49:33 +0200149 /* XML document roots */
150 LYXP_NODE_ROOT, /* access to all the data (node value first top-level node) */
151 LYXP_NODE_ROOT_CONFIG, /* <running> data context, no state data (node value first top-level node) */
152
153 /* XML elements */
Michal Vasko9f96a052020-03-10 09:41:45 +0100154 LYXP_NODE_ELEM, /* YANG data element (most common) */
155 LYXP_NODE_TEXT, /* YANG data text element (extremely specific use, unlikely to be ever needed) */
156 LYXP_NODE_META /* YANG metadata (do not use for the context node) */
Michal Vasko03ff5a72019-09-11 13:49:33 +0200157};
158
159/**
Radek Krejcib1646a92018-11-02 16:08:26 +0100160 * @brief Structure holding a parsed XPath expression.
161 */
162struct lyxp_expr {
aPiecek6da713d2021-10-11 12:50:28 +0200163 enum lyxp_token *tokens; /**< Array of tokens. */
164 uint16_t *tok_pos; /**< Array of the token offsets in expr. */
165 uint16_t *tok_len; /**< Array of token lengths in expr. */
166 enum lyxp_expr_type **repeat; /**< Array of expression types that this token begins and is repeated ended with 0,
167 more in the comment after this declaration. */
168 uint16_t used; /**< Used array items. */
169 uint16_t size; /**< Allocated array items. */
Radek Krejcib1646a92018-11-02 16:08:26 +0100170
aPiecek6da713d2021-10-11 12:50:28 +0200171 const char *expr; /**< The original XPath expression. */
Radek Krejcib1646a92018-11-02 16:08:26 +0100172};
173
174/*
175 * lyxp_expr repeat
176 *
177 * This value is NULL for all the tokens that do not begin an
178 * expression which can be repeated. Otherwise it is an array
179 * of expression types that this token begins. These values
180 * are used during evaluation to know whether we need to
181 * duplicate the current context or not and to decide what
182 * the current expression is (for example, if we are only
183 * starting the parsing and the first token has no repeat,
184 * we do not parse it as an OrExpr but directly as PathExpr).
185 * Examples:
186 *
aPiecekff49d762021-10-11 10:25:27 +0200187 * Expr: "/ *[key1 and key2 or key1 < key2]"
188 * Tokens: '/' '*' '[' NameTest 'and' NameTest 'or' NameTest '<' NameTest ']'
189 * Repeat: NULL NULL NULL _ NULL NULL NULL _ NULL NULL NULL
190 * | v
191 * v RelationalExpr 0
192 * AndExpr OrExpr 0
Radek Krejcib1646a92018-11-02 16:08:26 +0100193 *
aPiecekff49d762021-10-11 10:25:27 +0200194 * Expr: "//node[key and node2]/key | /cont"
195 * Tokens: '//' NameTest '[' NameTest 'and' NameTest ']' '/' NameTest '|' '/' NameTest
196 * Repeat: _ NULL NULL _ NULL NULL NULL NULL NULL NULL NULL NULL
197 * | v
198 * v AndExpr 0
199 * UnionExpr 0
Radek Krejcib1646a92018-11-02 16:08:26 +0100200 *
201 * Operators between expressions which this concerns:
202 * 'or', 'and', '=', '!=', '<', '>', '<=', '>=', '+', '-', '*', 'div', 'mod', '|'
203 */
204
205/**
206 * @brief Supported types of (partial) XPath results.
207 */
208enum lyxp_set_type {
Michal Vaskod3678892020-05-21 10:06:58 +0200209 LYXP_SET_NODE_SET = 0,
Michal Vasko03ff5a72019-09-11 13:49:33 +0200210 LYXP_SET_SCNODE_SET,
Radek Krejcib1646a92018-11-02 16:08:26 +0100211 LYXP_SET_BOOLEAN,
212 LYXP_SET_NUMBER,
213 LYXP_SET_STRING
214};
Radek Krejcib1646a92018-11-02 16:08:26 +0100215
216/**
217 * @brief Item stored in an XPath set hash table.
218 */
219struct lyxp_set_hash_node {
220 struct lyd_node *node;
221 enum lyxp_node_type type;
222} _PACKED;
223
Radek Krejcib1646a92018-11-02 16:08:26 +0100224/**
aPiecekdf23eee2021-10-07 12:21:50 +0200225 * @brief XPath variable bindings.
226 */
227struct lyxp_var {
228 char *name; /**< Variable name. In the XPath expression, the name is preceded by a '$' character. */
229 char *value; /**< The value of a variable is an object, which can be of any of the type that are possible
230 for the value of an expression. */
231};
232
233/**
Radek Krejcib1646a92018-11-02 16:08:26 +0100234 * @brief XPath set - (partial) result.
235 */
236struct lyxp_set {
aPiecek6da713d2021-10-11 12:50:28 +0200237 enum lyxp_set_type type; /**< Type of the object (value). */
Radek Krejcib1646a92018-11-02 16:08:26 +0100238 union {
239 struct lyxp_set_node {
aPiecek6da713d2021-10-11 12:50:28 +0200240 struct lyd_node *node; /**< Data node. */
241 enum lyxp_node_type type; /**< Type of the node. */
242 uint32_t pos; /**< Unique node position in the data. */
243 } *nodes; /**< Set of data nodes. */
Michal Vasko03ff5a72019-09-11 13:49:33 +0200244 struct lyxp_set_scnode {
aPiecek6da713d2021-10-11 12:50:28 +0200245 struct lysc_node *scnode; /**< Compiled YANG node. */
246 enum lyxp_node_type type; /**< Type of the node. */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100247
Michal Vaskod97959c2020-12-10 12:18:28 +0100248/* _START and _ATOM values should have grouped values */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100249#define LYXP_SET_SCNODE_START -2 /**< scnode not traversed, currently (the only node) in context */
250#define LYXP_SET_SCNODE_START_USED -1 /**< scnode not traversed except for the eval start, not currently in the context */
Michal Vasko1a09b212021-05-06 13:00:10 +0200251#define LYXP_SET_SCNODE_ATOM_NODE 0 /**< scnode was traversed, but not currently in the context */
252#define LYXP_SET_SCNODE_ATOM_VAL 1 /**< scnode was traversed and its value used, but not currently in the context */
253#define LYXP_SET_SCNODE_ATOM_CTX 2 /**< scnode currently in context */
254#define LYXP_SET_SCNODE_ATOM_NEW_CTX 3 /**< scnode in context and just added, so skip it for the current operation */
255#define LYXP_SET_SCNODE_ATOM_PRED_CTX 4 /**< includes any higher value - scnode is not in context because we are in
Radek Krejcif13b87b2020-12-01 22:02:17 +0100256 a predicate and this scnode was used/will be used later */
aPiecek6da713d2021-10-11 12:50:28 +0200257 int32_t in_ctx; /**< Flag specifies the state of the node in context. Values are defined
258 as LYXP_SET_SCNODE_* */
259 } *scnodes; /**< Set of compiled YANG data nodes. */
Michal Vasko9f96a052020-03-10 09:41:45 +0100260 struct lyxp_set_meta {
aPiecek6da713d2021-10-11 12:50:28 +0200261 struct lyd_meta *meta; /**< Node that provides information about metadata of a data element. */
262 enum lyxp_node_type type; /**< Type of the node. */
263 uint32_t pos; /**< Unique node position in the data. if node_type is LYXP_SET_NODE_META,
264 it is the parent node position */
265 } *meta; /**< Set of YANG metadata objects. */
266 char *str; /**< String object. */
267 long double num; /**< Object of the floating-point number. */
268 ly_bool bln; /**< Boolean object. */
269 } val; /**< Evaluated object (value). */
Radek Krejcib1646a92018-11-02 16:08:26 +0100270
Michal Vasko03ff5a72019-09-11 13:49:33 +0200271 /* this is valid only for type LYXP_SET_NODE_SET and LYXP_SET_SCNODE_SET */
aPiecek6da713d2021-10-11 12:50:28 +0200272 uint32_t used; /**< Number of nodes in the set. */
273 uint32_t size; /**< Allocated size for the set. */
274 struct hash_table *ht; /**< Hash table for quick determination of whether a node is in the set. */
Michal Vasko03ff5a72019-09-11 13:49:33 +0200275
276 /* XPath context information, this is valid only for type LYXP_SET_NODE_SET */
aPiecek6da713d2021-10-11 12:50:28 +0200277 uint32_t ctx_pos; /**< Position of the current examined node in the set. */
278 uint32_t ctx_size; /**< Position of the last node at the time the node was examined. */
Michal Vasko03ff5a72019-09-11 13:49:33 +0200279
280 /* general context */
aPiecek6da713d2021-10-11 12:50:28 +0200281 struct ly_ctx *ctx; /**< General context for logging. */
Michal Vasko03ff5a72019-09-11 13:49:33 +0200282 union {
aPiecek6da713d2021-10-11 12:50:28 +0200283 const struct lyd_node *cur_node; /**< Current (original context) node. */
284 const struct lysc_node *cur_scnode; /**< Current (original context) compiled node. */
Michal Vasko03ff5a72019-09-11 13:49:33 +0200285 };
aPiecek6da713d2021-10-11 12:50:28 +0200286 enum lyxp_node_type root_type; /**< Type of root node. */
287 const struct lysc_node *context_op; /**< Schema of the current node. */
288 const struct lyd_node *tree; /**< Data tree on which to perform the evaluation. */
289 const struct lys_module *cur_mod; /**< Current module for the expression (where it was "instantiated"). */
290 LY_VALUE_FORMAT format; /**< Format of the XPath expression. */
291 void *prefix_data; /**< Format-specific prefix data (see ::ly_resolve_prefix). */
Radek Krejcib1646a92018-11-02 16:08:26 +0100292};
293
294/**
Michal Vasko24cddf82020-06-01 08:17:01 +0200295 * @brief Print an XPath token \p tok type.
296 *
297 * @param[in] tok Token to print.
298 * @return Token type string.
299 */
300const char *lyxp_print_token(enum lyxp_token tok);
301
302/**
Michal Vasko03ff5a72019-09-11 13:49:33 +0200303 * @brief Evaluate an XPath expression on data. Be careful when using this function, the result can often
304 * be confusing without thorough understanding of XPath evaluation rules defined in RFC 7950.
Radek Krejcib1646a92018-11-02 16:08:26 +0100305 *
Michal Vasko400e9672021-01-11 13:39:17 +0100306 * @param[in] ctx libyang context to use.
Michal Vaskoecd62de2019-11-13 12:35:11 +0100307 * @param[in] exp Parsed XPath expression to be evaluated.
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200308 * @param[in] cur_mod Current module for the expression (where it was "instantiated").
aPiecekb0445f22021-06-24 11:34:07 +0200309 * @param[in] format Format of the XPath expression (more specifically, of any used prefixes).
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200310 * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
311 * @param[in] ctx_node Current (context) data node, NULL in case of the root node.
Michal Vaskof03ed032020-03-04 13:31:44 +0100312 * @param[in] tree Data tree on which to perform the evaluation, it must include all the available data (including
Michal Vaskod3bb12f2020-12-04 14:33:09 +0100313 * the tree of @p ctx_node). Can be any node of the tree, it is adjusted.
Michal Vasko004d3152020-06-11 19:59:22 +0200314 * @param[out] set Result set.
Radek Krejcib1646a92018-11-02 16:08:26 +0100315 * @param[in] options Whether to apply some evaluation restrictions.
Michal Vasko004d3152020-06-11 19:59:22 +0200316 * @return LY_EVALID for invalid argument types/count,
317 * @return LY_EINCOMPLETE for unresolved when,
318 * @return LY_EINVAL, LY_EMEM, LY_EINT for other errors.
Radek Krejcib1646a92018-11-02 16:08:26 +0100319 */
Michal Vasko400e9672021-01-11 13:39:17 +0100320LY_ERR lyxp_eval(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Radek Krejci8df109d2021-04-23 12:19:08 +0200321 LY_VALUE_FORMAT format, void *prefix_data, const struct lyd_node *ctx_node, const struct lyd_node *tree,
Michal Vasko400e9672021-01-11 13:39:17 +0100322 struct lyxp_set *set, uint32_t options);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200323
Radek Krejcib1646a92018-11-02 16:08:26 +0100324/**
Michal Vasko03ff5a72019-09-11 13:49:33 +0200325 * @brief Get all the partial XPath nodes (atoms) that are required for @p exp to be evaluated.
Radek Krejcib1646a92018-11-02 16:08:26 +0100326 *
Michal Vasko400e9672021-01-11 13:39:17 +0100327 * @param[in] ctx libyang context to use.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200328 * @param[in] exp Parsed XPath expression to be evaluated.
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200329 * @param[in] cur_mod Current module for the expression (where it was "instantiated").
aPiecekb0445f22021-06-24 11:34:07 +0200330 * @param[in] format Format of the XPath expression (more specifically, of any used prefixes).
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200331 * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
332 * @param[in] ctx_scnode Current (context) schema node, NULL in case of the root node.
Michal Vasko004d3152020-06-11 19:59:22 +0200333 * @param[out] set Result set.
Radek Krejcib1646a92018-11-02 16:08:26 +0100334 * @param[in] options Whether to apply some evaluation restrictions, one flag must always be used.
Radek Krejci8678fa42020-08-18 16:07:28 +0200335 * @return LY_ERR (same as ::lyxp_eval()).
Radek Krejcib1646a92018-11-02 16:08:26 +0100336 */
Michal Vasko400e9672021-01-11 13:39:17 +0100337LY_ERR lyxp_atomize(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Radek Krejci8df109d2021-04-23 12:19:08 +0200338 LY_VALUE_FORMAT format, void *prefix_data, const struct lysc_node *ctx_scnode, struct lyxp_set *set,
Michal Vasko400e9672021-01-11 13:39:17 +0100339 uint32_t options);
Radek Krejcib1646a92018-11-02 16:08:26 +0100340
Radek Krejcibed13942020-10-19 16:06:28 +0200341/* used only internally, maps with @ref findxpathoptions */
Michal Vaskocdad7122020-11-09 21:04:44 +0100342#define LYXP_IGNORE_WHEN 0x01 /**< Ignore unevaluated when in data nodes and do not return ::LY_EINCOMPLETE. */
343#define LYXP_SCHEMA 0x02 /**< Apply data node access restrictions defined for 'when' and 'must' evaluation. */
344#define LYXP_SCNODE 0x04 /**< No special tree access modifiers. */
345#define LYXP_SCNODE_SCHEMA LYS_FIND_XP_SCHEMA /**< Apply node access restrictions defined for 'when' and 'must' evaluation. */
346#define LYXP_SCNODE_OUTPUT LYS_FIND_XP_OUTPUT /**< Search RPC/action output nodes instead of input ones. */
347#define LYXP_SCNODE_ALL 0x1C /**< mask for all the LYXP_* values */
aPiecek8b0cc152021-05-31 16:40:31 +0200348#define LYXP_SKIP_EXPR 0x20 /**< The rest of the expression will not be evaluated (lazy evaluation) */
Radek Krejcib1646a92018-11-02 16:08:26 +0100349
350/**
Radek Krejcib1646a92018-11-02 16:08:26 +0100351 * @brief Cast XPath set to another type.
352 * Indirectly context position aware.
353 *
354 * @param[in] set Set to cast.
355 * @param[in] target Target type to cast \p set into.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200356 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +0100357 */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100358LY_ERR lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200359
Radek Krejcib1646a92018-11-02 16:08:26 +0100360/**
Michal Vaskod3678892020-05-21 10:06:58 +0200361 * @brief Free dynamic content of a set.
362 *
363 * @param[in] set Set to modify.
364 */
365void lyxp_set_free_content(struct lyxp_set *set);
366
367/**
Michal Vaskoecd62de2019-11-13 12:35:11 +0100368 * @brief Insert schema node into set.
369 *
370 * @param[in] set Set to insert into.
371 * @param[in] node Node to insert.
372 * @param[in] node_type Node type of @p node.
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200373 * @param[out] index_p Optional pointer to store index if the inserted @p node.
374 * @return LY_SUCCESS on success.
375 * @return LY_EMEM on memory allocation failure.
Michal Vaskoecd62de2019-11-13 12:35:11 +0100376 */
Michal Vaskoee38a5d2020-11-09 21:02:18 +0100377LY_ERR lyxp_set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type,
378 uint32_t *index_p);
Michal Vaskoecd62de2019-11-13 12:35:11 +0100379
380/**
381 * @brief Check for duplicates in a schema node set.
382 *
383 * @param[in] set Set to check.
384 * @param[in] node Node to look for in @p set.
385 * @param[in] node_type Type of @p node.
386 * @param[in] skip_idx Index from @p set to skip.
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200387 * @param[out] index_p Optional pointer to store index if the node is found.
Radek Krejci857189e2020-09-01 13:26:36 +0200388 * @return Boolean value whether the @p node found or not.
Michal Vaskoecd62de2019-11-13 12:35:11 +0100389 */
Radek Krejci857189e2020-09-01 13:26:36 +0200390ly_bool lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type,
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200391 int skip_idx, uint32_t *index_p);
Michal Vaskoecd62de2019-11-13 12:35:11 +0100392
393/**
394 * @brief Merge 2 schema node sets.
395 *
396 * @param[in] set1 Set to merge into.
397 * @param[in] set2 Set to merge. Its content is freed.
398 */
399void lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2);
400
401/**
Radek Krejcib1646a92018-11-02 16:08:26 +0100402 * @brief Parse an XPath expression into a structure of tokens.
403 * Logs directly.
404 *
Michal Vasko03ff5a72019-09-11 13:49:33 +0200405 * https://www.w3.org/TR/1999/REC-xpath-19991116/#exprlex
Radek Krejcib1646a92018-11-02 16:08:26 +0100406 *
407 * @param[in] ctx Context for errors.
Radek Krejcif03a9e22020-09-18 20:09:31 +0200408 * @param[in] expr_str XPath expression to parse. It is duplicated.
Michal Vasko004d3152020-06-11 19:59:22 +0200409 * @param[in] expr_len Length of @p expr, can be 0 if @p expr is 0-terminated.
410 * @param[in] reparse Whether to re-parse the expression to finalize full XPath parsing and fill
411 * information about expressions and their operators (fill repeat).
Radek Krejcif03a9e22020-09-18 20:09:31 +0200412 * @param[out] expr_p Pointer to return the filled expression structure.
413 * @return LY_SUCCESS in case of success.
414 * @return LY_EMEM in case of memory allocation failure.
415 * @return LY_EVALID in case of invalid XPath expression in @p expr_str.
Radek Krejcib1646a92018-11-02 16:08:26 +0100416 */
Michal Vaskoee38a5d2020-11-09 21:02:18 +0100417LY_ERR lyxp_expr_parse(const struct ly_ctx *ctx, const char *expr_str, size_t expr_len, ly_bool reparse,
418 struct lyxp_expr **expr_p);
Michal Vasko004d3152020-06-11 19:59:22 +0200419
420/**
421 * @brief Duplicate parsed XPath expression.
422 *
423 * @param[in] ctx Context with a dictionary.
424 * @param[in] exp Parsed expression.
Michal Vasko1734be92020-09-22 08:55:10 +0200425 * @param[out] dup Duplicated structure.
426 * @return LY_ERR value.
Michal Vasko004d3152020-06-11 19:59:22 +0200427 */
Michal Vasko1734be92020-09-22 08:55:10 +0200428LY_ERR lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, struct lyxp_expr **dup);
Michal Vasko14676352020-05-29 11:35:55 +0200429
430/**
431 * @brief Look at the next token and check its kind.
432 *
433 * @param[in] ctx Context for logging, not logged if NULL.
434 * @param[in] exp Expression to use.
Michal Vasko004d3152020-06-11 19:59:22 +0200435 * @param[in] tok_idx Token index in the expression \p exp.
Michal Vasko14676352020-05-29 11:35:55 +0200436 * @param[in] want_tok Expected token.
437 * @return LY_EINCOMPLETE on EOF,
438 * @return LY_ENOT on non-matching token,
439 * @return LY_SUCCESS on success.
440 */
Michal Vasko004d3152020-06-11 19:59:22 +0200441LY_ERR lyxp_check_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t tok_idx, enum lyxp_token want_tok);
442
443/**
444 * @brief Look at the next token and skip it if it matches the expected one.
445 *
446 * @param[in] ctx Context for logging, not logged if NULL.
447 * @param[in] exp Expression to use.
448 * @param[in,out] tok_idx Token index in the expression \p exp, is updated.
449 * @param[in] want_tok Expected token.
450 * @return LY_EINCOMPLETE on EOF,
451 * @return LY_ENOT on non-matching token,
452 * @return LY_SUCCESS on success.
453 */
454LY_ERR lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok);
Radek Krejcib1646a92018-11-02 16:08:26 +0100455
456/**
Michal Vasko4911eeb2021-06-28 11:23:05 +0200457 * @brief Look at the next token and skip it if it matches either of the 2 expected ones.
458 *
459 * @param[in] ctx Context for logging, not logged if NULL.
460 * @param[in] exp Expression to use.
461 * @param[in,out] tok_idx Token index in the expression \p exp, is updated.
462 * @param[in] want_tok1 Expected token 1.
463 * @param[in] want_tok2 Expected token 2.
464 * @return LY_EINCOMPLETE on EOF,
465 * @return LY_ENOT on non-matching token,
466 * @return LY_SUCCESS on success.
467 */
468LY_ERR lyxp_next_token2(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx,
469 enum lyxp_token want_tok1, enum lyxp_token want_tok2);
470
471/**
aPiecekdf23eee2021-10-07 12:21:50 +0200472 * @brief Find variable named @name in @p vars.
473 *
474 * @param[in] vars [Sized array](@ref sizedarrays) of XPath variables.
475 * @param[in] name Name of the variable being searched.
476 * @param[in] name_len Name length can be set to 0 if @p name is terminated by null byte.
477 * @param[out] var Variable that was found. The parameter is optional.
478 * @return LY_SUCCESS if the variable was found, otherwise LY_ENOTFOUND.
479 */
480LY_ERR lyxp_vars_find(struct lyxp_var *vars, const char *name, size_t name_len, struct lyxp_var **var);
481
482/**
Michal Vasko03ff5a72019-09-11 13:49:33 +0200483 * @brief Frees a parsed XPath expression. @p expr should not be used afterwards.
Radek Krejcib1646a92018-11-02 16:08:26 +0100484 *
485 * @param[in] ctx libyang context of the expression.
486 * @param[in] expr Expression to free.
487 */
Michal Vasko14676352020-05-29 11:35:55 +0200488void lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr);
Radek Krejcib1646a92018-11-02 16:08:26 +0100489
Michal Vasko14676352020-05-29 11:35:55 +0200490#endif /* LY_XPATH_H */