blob: 8215f8f7be9a1b31275e83feb4acad00618eff38 [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 Vasko306e2832022-07-25 09:15:17 +02006 * Copyright (c) 2015 - 2022 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,
aPiecekfba75362021-10-07 12:39:48 +020040 * "id()" function is not supported, and processing instruction and comment nodes are not supported,
41 * which is also reflected in the grammar. Undefined rules and constants are tokens.
Radek Krejcib1646a92018-11-02 16:08:26 +010042 *
43 * Modified full grammar:
Radek Krejci84d7fd72021-07-14 18:32:21 +020044 * @code
Radek Krejcib1646a92018-11-02 16:08:26 +010045 * [1] Expr ::= OrExpr // just an alias
46 *
47 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
48 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
49 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
Michal Vasko49fec8e2022-05-24 10:28:33 +020050 * [5] Step ::= (AxisName '::' | '@')? NodeTest Predicate* | '.' | '..'
Radek Krejcib1646a92018-11-02 16:08:26 +010051 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vaskod3678892020-05-21 10:06:58 +020052 * [7] NameTest ::= '*' | NCName ':' '*' | QName
53 * [8] NodeType ::= 'text' | 'node'
54 * [9] Predicate ::= '[' Expr ']'
aPiecekfba75362021-10-07 12:39:48 +020055 * [10] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall
Michal Vaskod3678892020-05-21 10:06:58 +020056 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
57 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Radek Krejcib1646a92018-11-02 16:08:26 +010058 * | PrimaryExpr Predicate* '/' RelativeLocationPath
59 * | PrimaryExpr Predicate* '//' RelativeLocationPath
Michal Vaskod3678892020-05-21 10:06:58 +020060 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
61 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
62 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Radek Krejcib1646a92018-11-02 16:08:26 +010063 * | EqualityExpr '!=' RelationalExpr
Michal Vaskod3678892020-05-21 10:06:58 +020064 * [16] RelationalExpr ::= AdditiveExpr
Radek Krejcib1646a92018-11-02 16:08:26 +010065 * | RelationalExpr '<' AdditiveExpr
66 * | RelationalExpr '>' AdditiveExpr
67 * | RelationalExpr '<=' AdditiveExpr
68 * | RelationalExpr '>=' AdditiveExpr
Michal Vaskod3678892020-05-21 10:06:58 +020069 * [17] AdditiveExpr ::= MultiplicativeExpr
Radek Krejcib1646a92018-11-02 16:08:26 +010070 * | AdditiveExpr '+' MultiplicativeExpr
71 * | AdditiveExpr '-' MultiplicativeExpr
Michal Vaskod3678892020-05-21 10:06:58 +020072 * [18] MultiplicativeExpr ::= UnaryExpr
Radek Krejcib1646a92018-11-02 16:08:26 +010073 * | MultiplicativeExpr '*' UnaryExpr
74 * | MultiplicativeExpr 'div' UnaryExpr
75 * | MultiplicativeExpr 'mod' UnaryExpr
Michal Vaskod3678892020-05-21 10:06:58 +020076 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
77 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Radek Krejci84d7fd72021-07-14 18:32:21 +020078 * @endcode
Radek Krejcib1646a92018-11-02 16:08:26 +010079 */
80
81/* expression tokens allocation */
82#define LYXP_EXPR_SIZE_START 10
83#define LYXP_EXPR_SIZE_STEP 5
84
85/* XPath matches allocation */
Michal Vasko871df522022-04-06 12:14:41 +020086#define LYXP_SET_SIZE_START 4
87#define LYXP_SET_SIZE_MUL_STEP 2
Radek Krejcib1646a92018-11-02 16:08:26 +010088
89/* building string when casting */
90#define LYXP_STRING_CAST_SIZE_START 64
91#define LYXP_STRING_CAST_SIZE_STEP 16
92
aPiecekbf968d92021-05-27 14:35:05 +020093/* Maximum number of nested expressions. */
94#define LYXP_MAX_BLOCK_DEPTH 100
95
Radek Krejcib1646a92018-11-02 16:08:26 +010096/**
97 * @brief Tokens that can be in an XPath expression.
98 */
99enum lyxp_token {
100 LYXP_TOKEN_NONE = 0,
101 LYXP_TOKEN_PAR1, /* '(' */
102 LYXP_TOKEN_PAR2, /* ')' */
103 LYXP_TOKEN_BRACK1, /* '[' */
104 LYXP_TOKEN_BRACK2, /* ']' */
105 LYXP_TOKEN_DOT, /* '.' */
106 LYXP_TOKEN_DDOT, /* '..' */
107 LYXP_TOKEN_AT, /* '@' */
108 LYXP_TOKEN_COMMA, /* ',' */
Michal Vasko49fec8e2022-05-24 10:28:33 +0200109 LYXP_TOKEN_DCOLON, /* '::' */
Radek Krejcib1646a92018-11-02 16:08:26 +0100110 LYXP_TOKEN_NAMETEST, /* NameTest */
111 LYXP_TOKEN_NODETYPE, /* NodeType */
aPiecekfba75362021-10-07 12:39:48 +0200112 LYXP_TOKEN_VARREF, /* VariableReference */
Radek Krejcib1646a92018-11-02 16:08:26 +0100113 LYXP_TOKEN_FUNCNAME, /* FunctionName */
Michal Vasko3e48bf32020-06-01 08:39:07 +0200114 LYXP_TOKEN_OPER_LOG, /* Operator 'and', 'or' */
115 LYXP_TOKEN_OPER_EQUAL, /* Operator '=' */
116 LYXP_TOKEN_OPER_NEQUAL, /* Operator '!=' */
117 LYXP_TOKEN_OPER_COMP, /* Operator '<', '<=', '>', '>=' */
118 LYXP_TOKEN_OPER_MATH, /* Operator '+', '-', '*', 'div', 'mod', '-' (unary) */
119 LYXP_TOKEN_OPER_UNI, /* Operator '|' */
120 LYXP_TOKEN_OPER_PATH, /* Operator '/' */
121 LYXP_TOKEN_OPER_RPATH, /* Operator '//' (recursive path) */
Michal Vasko49fec8e2022-05-24 10:28:33 +0200122 LYXP_TOKEN_AXISNAME, /* AxisName */
Radek Krejcib1646a92018-11-02 16:08:26 +0100123 LYXP_TOKEN_LITERAL, /* Literal - with either single or double quote */
124 LYXP_TOKEN_NUMBER /* Number */
125};
126
127/**
Michal Vasko49fec8e2022-05-24 10:28:33 +0200128 * @brief XPath Axes types.
129 */
130enum lyxp_axis {
131 LYXP_AXIS_ANCESTOR,
132 LYXP_AXIS_ANCESTOR_OR_SELF,
133 LYXP_AXIS_ATTRIBUTE,
134 LYXP_AXIS_CHILD,
135 LYXP_AXIS_DESCENDANT,
136 LYXP_AXIS_DESCENDANT_OR_SELF,
137 LYXP_AXIS_FOLLOWING,
138 LYXP_AXIS_FOLLOWING_SIBLING,
139 // LYXP_AXIS_NAMESPACE, /* not supported */
140 LYXP_AXIS_PARENT,
141 LYXP_AXIS_PRECEDING,
142 LYXP_AXIS_PRECEDING_SIBLING,
143 LYXP_AXIS_SELF
144};
145
146/**
Radek Krejcib1646a92018-11-02 16:08:26 +0100147 * @brief XPath (sub)expressions that can be repeated.
148 */
149enum lyxp_expr_type {
150 LYXP_EXPR_NONE = 0,
151 LYXP_EXPR_OR,
152 LYXP_EXPR_AND,
153 LYXP_EXPR_EQUALITY,
154 LYXP_EXPR_RELATIONAL,
155 LYXP_EXPR_ADDITIVE,
156 LYXP_EXPR_MULTIPLICATIVE,
157 LYXP_EXPR_UNARY,
Michal Vasko69730152020-10-09 16:30:07 +0200158 LYXP_EXPR_UNION
Radek Krejcib1646a92018-11-02 16:08:26 +0100159};
160
161/**
Michal Vasko03ff5a72019-09-11 13:49:33 +0200162 * @brief Types of context nodes, #LYXP_NODE_ROOT_CONFIG used only in when or must conditions.
163 */
164enum lyxp_node_type {
Michal Vasko2caefc12019-11-14 16:07:56 +0100165 LYXP_NODE_NONE, /* invalid node type */
166
Michal Vasko03ff5a72019-09-11 13:49:33 +0200167 /* XML document roots */
168 LYXP_NODE_ROOT, /* access to all the data (node value first top-level node) */
169 LYXP_NODE_ROOT_CONFIG, /* <running> data context, no state data (node value first top-level node) */
170
171 /* XML elements */
Michal Vasko9f96a052020-03-10 09:41:45 +0100172 LYXP_NODE_ELEM, /* YANG data element (most common) */
173 LYXP_NODE_TEXT, /* YANG data text element (extremely specific use, unlikely to be ever needed) */
174 LYXP_NODE_META /* YANG metadata (do not use for the context node) */
Michal Vasko03ff5a72019-09-11 13:49:33 +0200175};
176
177/**
Radek Krejcib1646a92018-11-02 16:08:26 +0100178 * @brief Structure holding a parsed XPath expression.
179 */
180struct lyxp_expr {
aPiecek6da713d2021-10-11 12:50:28 +0200181 enum lyxp_token *tokens; /**< Array of tokens. */
182 uint16_t *tok_pos; /**< Array of the token offsets in expr. */
183 uint16_t *tok_len; /**< Array of token lengths in expr. */
184 enum lyxp_expr_type **repeat; /**< Array of expression types that this token begins and is repeated ended with 0,
185 more in the comment after this declaration. */
186 uint16_t used; /**< Used array items. */
187 uint16_t size; /**< Allocated array items. */
Radek Krejcib1646a92018-11-02 16:08:26 +0100188
aPiecek6da713d2021-10-11 12:50:28 +0200189 const char *expr; /**< The original XPath expression. */
Radek Krejcib1646a92018-11-02 16:08:26 +0100190};
191
192/*
193 * lyxp_expr repeat
194 *
195 * This value is NULL for all the tokens that do not begin an
196 * expression which can be repeated. Otherwise it is an array
197 * of expression types that this token begins. These values
198 * are used during evaluation to know whether we need to
199 * duplicate the current context or not and to decide what
200 * the current expression is (for example, if we are only
201 * starting the parsing and the first token has no repeat,
202 * we do not parse it as an OrExpr but directly as PathExpr).
203 * Examples:
204 *
aPiecekff49d762021-10-11 10:25:27 +0200205 * Expr: "/ *[key1 and key2 or key1 < key2]"
206 * Tokens: '/' '*' '[' NameTest 'and' NameTest 'or' NameTest '<' NameTest ']'
207 * Repeat: NULL NULL NULL _ NULL NULL NULL _ NULL NULL NULL
208 * | v
209 * v RelationalExpr 0
210 * AndExpr OrExpr 0
Radek Krejcib1646a92018-11-02 16:08:26 +0100211 *
aPiecekff49d762021-10-11 10:25:27 +0200212 * Expr: "//node[key and node2]/key | /cont"
213 * Tokens: '//' NameTest '[' NameTest 'and' NameTest ']' '/' NameTest '|' '/' NameTest
214 * Repeat: _ NULL NULL _ NULL NULL NULL NULL NULL NULL NULL NULL
215 * | v
216 * v AndExpr 0
217 * UnionExpr 0
Radek Krejcib1646a92018-11-02 16:08:26 +0100218 *
219 * Operators between expressions which this concerns:
220 * 'or', 'and', '=', '!=', '<', '>', '<=', '>=', '+', '-', '*', 'div', 'mod', '|'
221 */
222
223/**
224 * @brief Supported types of (partial) XPath results.
225 */
226enum lyxp_set_type {
Michal Vaskod3678892020-05-21 10:06:58 +0200227 LYXP_SET_NODE_SET = 0,
Michal Vasko03ff5a72019-09-11 13:49:33 +0200228 LYXP_SET_SCNODE_SET,
Radek Krejcib1646a92018-11-02 16:08:26 +0100229 LYXP_SET_BOOLEAN,
230 LYXP_SET_NUMBER,
231 LYXP_SET_STRING
232};
Radek Krejcib1646a92018-11-02 16:08:26 +0100233
234/**
235 * @brief Item stored in an XPath set hash table.
236 */
237struct lyxp_set_hash_node {
238 struct lyd_node *node;
239 enum lyxp_node_type type;
240} _PACKED;
241
Radek Krejcib1646a92018-11-02 16:08:26 +0100242/**
aPiecekdf23eee2021-10-07 12:21:50 +0200243 * @brief XPath variable bindings.
244 */
245struct lyxp_var {
246 char *name; /**< Variable name. In the XPath expression, the name is preceded by a '$' character. */
247 char *value; /**< The value of a variable is an object, which can be of any of the type that are possible
248 for the value of an expression. */
249};
250
251/**
Radek Krejcib1646a92018-11-02 16:08:26 +0100252 * @brief XPath set - (partial) result.
253 */
254struct lyxp_set {
aPiecek6da713d2021-10-11 12:50:28 +0200255 enum lyxp_set_type type; /**< Type of the object (value). */
Radek Krejcib1646a92018-11-02 16:08:26 +0100256 union {
257 struct lyxp_set_node {
aPiecek6da713d2021-10-11 12:50:28 +0200258 struct lyd_node *node; /**< Data node. */
259 enum lyxp_node_type type; /**< Type of the node. */
260 uint32_t pos; /**< Unique node position in the data. */
261 } *nodes; /**< Set of data nodes. */
Michal Vasko03ff5a72019-09-11 13:49:33 +0200262 struct lyxp_set_scnode {
aPiecek6da713d2021-10-11 12:50:28 +0200263 struct lysc_node *scnode; /**< Compiled YANG node. */
264 enum lyxp_node_type type; /**< Type of the node. */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100265
Michal Vaskod97959c2020-12-10 12:18:28 +0100266/* _START and _ATOM values should have grouped values */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100267#define LYXP_SET_SCNODE_START -2 /**< scnode not traversed, currently (the only node) in context */
268#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 +0200269#define LYXP_SET_SCNODE_ATOM_NODE 0 /**< scnode was traversed, but not currently in the context */
270#define LYXP_SET_SCNODE_ATOM_VAL 1 /**< scnode was traversed and its value used, but not currently in the context */
271#define LYXP_SET_SCNODE_ATOM_CTX 2 /**< scnode currently in context */
272#define LYXP_SET_SCNODE_ATOM_NEW_CTX 3 /**< scnode in context and just added, so skip it for the current operation */
273#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 +0100274 a predicate and this scnode was used/will be used later */
aPiecek6da713d2021-10-11 12:50:28 +0200275 int32_t in_ctx; /**< Flag specifies the state of the node in context. Values are defined
276 as LYXP_SET_SCNODE_* */
277 } *scnodes; /**< Set of compiled YANG data nodes. */
Michal Vasko9f96a052020-03-10 09:41:45 +0100278 struct lyxp_set_meta {
aPiecek6da713d2021-10-11 12:50:28 +0200279 struct lyd_meta *meta; /**< Node that provides information about metadata of a data element. */
280 enum lyxp_node_type type; /**< Type of the node. */
281 uint32_t pos; /**< Unique node position in the data. if node_type is LYXP_SET_NODE_META,
282 it is the parent node position */
283 } *meta; /**< Set of YANG metadata objects. */
284 char *str; /**< String object. */
285 long double num; /**< Object of the floating-point number. */
286 ly_bool bln; /**< Boolean object. */
287 } val; /**< Evaluated object (value). */
Radek Krejcib1646a92018-11-02 16:08:26 +0100288
Michal Vasko03ff5a72019-09-11 13:49:33 +0200289 /* this is valid only for type LYXP_SET_NODE_SET and LYXP_SET_SCNODE_SET */
Michal Vasko306e2832022-07-25 09:15:17 +0200290 uint32_t used; /**< Number of nodes in the set. */
291 uint32_t size; /**< Allocated size for the set. */
292 struct hash_table *ht; /**< Hash table for quick determination of whether a node is in the set. */
Michal Vasko03ff5a72019-09-11 13:49:33 +0200293
294 /* XPath context information, this is valid only for type LYXP_SET_NODE_SET */
Michal Vasko306e2832022-07-25 09:15:17 +0200295 uint32_t ctx_pos; /**< Position of the current examined node in the set. */
296 uint32_t ctx_size; /**< Position of the last node at the time the node was examined. */
297 ly_bool non_child_axis; /**< Whether any node change was performed on a non-child axis. */
Michal Vasko03ff5a72019-09-11 13:49:33 +0200298
299 /* general context */
aPiecek6da713d2021-10-11 12:50:28 +0200300 struct ly_ctx *ctx; /**< General context for logging. */
Michal Vasko03ff5a72019-09-11 13:49:33 +0200301 union {
aPiecek6da713d2021-10-11 12:50:28 +0200302 const struct lyd_node *cur_node; /**< Current (original context) node. */
303 const struct lysc_node *cur_scnode; /**< Current (original context) compiled node. */
Michal Vasko03ff5a72019-09-11 13:49:33 +0200304 };
aPiecek6da713d2021-10-11 12:50:28 +0200305 enum lyxp_node_type root_type; /**< Type of root node. */
306 const struct lysc_node *context_op; /**< Schema of the current node. */
307 const struct lyd_node *tree; /**< Data tree on which to perform the evaluation. */
308 const struct lys_module *cur_mod; /**< Current module for the expression (where it was "instantiated"). */
309 LY_VALUE_FORMAT format; /**< Format of the XPath expression. */
310 void *prefix_data; /**< Format-specific prefix data (see ::ly_resolve_prefix). */
aPiecekfba75362021-10-07 12:39:48 +0200311 const struct lyxp_var *vars; /**< XPath variables. [Sized array](@ref sizedarrays).
312 Set of variable bindings. */
Radek Krejcib1646a92018-11-02 16:08:26 +0100313};
314
315/**
Michal Vasko49fec8e2022-05-24 10:28:33 +0200316 * @brief Get string format of an XPath token.
Michal Vasko24cddf82020-06-01 08:17:01 +0200317 *
Michal Vasko49fec8e2022-05-24 10:28:33 +0200318 * @param[in] tok Token to transform.
Michal Vasko24cddf82020-06-01 08:17:01 +0200319 * @return Token type string.
320 */
Michal Vasko49fec8e2022-05-24 10:28:33 +0200321const char *lyxp_token2str(enum lyxp_token tok);
Michal Vasko24cddf82020-06-01 08:17:01 +0200322
323/**
Michal Vasko03ff5a72019-09-11 13:49:33 +0200324 * @brief Evaluate an XPath expression on data. Be careful when using this function, the result can often
325 * be confusing without thorough understanding of XPath evaluation rules defined in RFC 7950.
Radek Krejcib1646a92018-11-02 16:08:26 +0100326 *
Michal Vasko400e9672021-01-11 13:39:17 +0100327 * @param[in] ctx libyang context to use.
Michal Vaskoecd62de2019-11-13 12:35:11 +0100328 * @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).
Michal Vaskoa3e92bc2022-07-29 14:56:23 +0200332 * @param[in] cur_node Current data node, NULL in case of the root node. Equal to @p ctx_node unless a
333 * subexpression is being evaluated.
334 * @param[in] ctx_node Starting context data node, NULL in case of the root node. Equal to @p cur_node unless a
335 * subexpression is being evaluated.
Michal Vaskof03ed032020-03-04 13:31:44 +0100336 * @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 +0100337 * the tree of @p ctx_node). Can be any node of the tree, it is adjusted.
aPiecekfba75362021-10-07 12:39:48 +0200338 * @param[in] vars [Sized array](@ref sizedarrays) of XPath variables.
Michal Vasko004d3152020-06-11 19:59:22 +0200339 * @param[out] set Result set.
Radek Krejcib1646a92018-11-02 16:08:26 +0100340 * @param[in] options Whether to apply some evaluation restrictions.
Michal Vasko004d3152020-06-11 19:59:22 +0200341 * @return LY_EVALID for invalid argument types/count,
342 * @return LY_EINCOMPLETE for unresolved when,
343 * @return LY_EINVAL, LY_EMEM, LY_EINT for other errors.
Radek Krejcib1646a92018-11-02 16:08:26 +0100344 */
Michal Vasko400e9672021-01-11 13:39:17 +0100345LY_ERR lyxp_eval(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Michal Vaskoa3e92bc2022-07-29 14:56:23 +0200346 LY_VALUE_FORMAT format, void *prefix_data, const struct lyd_node *cur_node, const struct lyd_node *ctx_node,
347 const struct lyd_node *tree, const struct lyxp_var *vars, struct lyxp_set *set, uint32_t options);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200348
Radek Krejcib1646a92018-11-02 16:08:26 +0100349/**
Michal Vasko03ff5a72019-09-11 13:49:33 +0200350 * @brief Get all the partial XPath nodes (atoms) that are required for @p exp to be evaluated.
Radek Krejcib1646a92018-11-02 16:08:26 +0100351 *
Michal Vasko400e9672021-01-11 13:39:17 +0100352 * @param[in] ctx libyang context to use.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200353 * @param[in] exp Parsed XPath expression to be evaluated.
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200354 * @param[in] cur_mod Current module for the expression (where it was "instantiated").
aPiecekb0445f22021-06-24 11:34:07 +0200355 * @param[in] format Format of the XPath expression (more specifically, of any used prefixes).
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200356 * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
Michal Vaskoa3e92bc2022-07-29 14:56:23 +0200357 * @param[in] cur_scnode Current schema node, NULL in case of the root node. Equal to @p ctx_scnode unless a
358 * subexpression is being atomized.
359 * @param[in] ctx_scnode Starting context schema node, NULL in case of the root node. Equal to @p cur_scnode unless a
360 * subexpression is being atomized.
Michal Vasko004d3152020-06-11 19:59:22 +0200361 * @param[out] set Result set.
Radek Krejcib1646a92018-11-02 16:08:26 +0100362 * @param[in] options Whether to apply some evaluation restrictions, one flag must always be used.
Radek Krejci8678fa42020-08-18 16:07:28 +0200363 * @return LY_ERR (same as ::lyxp_eval()).
Radek Krejcib1646a92018-11-02 16:08:26 +0100364 */
Michal Vasko400e9672021-01-11 13:39:17 +0100365LY_ERR lyxp_atomize(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Michal Vaskoa3e92bc2022-07-29 14:56:23 +0200366 LY_VALUE_FORMAT format, void *prefix_data, const struct lysc_node *cur_scnode,
367 const struct lysc_node *ctx_scnode, struct lyxp_set *set, uint32_t options);
Radek Krejcib1646a92018-11-02 16:08:26 +0100368
Michal Vasko4ad69e72021-10-26 16:25:55 +0200369/** used only internally, maps with @ref findxpathoptions */
Michal Vaskoa27245c2022-05-02 09:01:35 +0200370#define LYXP_IGNORE_WHEN 0x01 /**< Ignore unevaluated when in data nodes and do not return ::LY_EINCOMPLETE. */
371#define LYXP_SCHEMA 0x02 /**< Apply data node access restrictions defined for 'when' and 'must' evaluation. */
372#define LYXP_SCNODE 0x04 /**< No special tree access modifiers. */
373#define LYXP_SCNODE_SCHEMA LYS_FIND_XP_SCHEMA /**< Apply node access restrictions defined for 'when' and 'must' evaluation. */
374#define LYXP_SCNODE_OUTPUT LYS_FIND_XP_OUTPUT /**< Search RPC/action output nodes instead of input ones. */
375#define LYXP_SCNODE_ALL 0x1C /**< mask for all the LYXP_* values */
376#define LYXP_SKIP_EXPR 0x20 /**< The rest of the expression will not be evaluated (lazy evaluation) */
377#define LYXP_SCNODE_ERROR LYS_FIND_NO_MATCH_ERROR /**< Return error if a path segment matches no nodes, otherwise only
378 warning is printed. */
379#define LYXP_ACCESS_TREE_ALL 0x80 /**< Explicit accessible tree of all the nodes. */
380#define LYXP_ACCESS_TREE_CONFIG 0x0100 /**< Explicit accessible tree of only configuration data. */
Radek Krejcib1646a92018-11-02 16:08:26 +0100381
382/**
Radek Krejcib1646a92018-11-02 16:08:26 +0100383 * @brief Cast XPath set to another type.
384 * Indirectly context position aware.
385 *
386 * @param[in] set Set to cast.
387 * @param[in] target Target type to cast \p set into.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200388 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +0100389 */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100390LY_ERR lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200391
Radek Krejcib1646a92018-11-02 16:08:26 +0100392/**
Michal Vaskod3678892020-05-21 10:06:58 +0200393 * @brief Free dynamic content of a set.
394 *
395 * @param[in] set Set to modify.
396 */
397void lyxp_set_free_content(struct lyxp_set *set);
398
399/**
Michal Vaskoecd62de2019-11-13 12:35:11 +0100400 * @brief Insert schema node into set.
401 *
402 * @param[in] set Set to insert into.
403 * @param[in] node Node to insert.
404 * @param[in] node_type Node type of @p node.
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200405 * @param[out] index_p Optional pointer to store index if the inserted @p node.
406 * @return LY_SUCCESS on success.
407 * @return LY_EMEM on memory allocation failure.
Michal Vaskoecd62de2019-11-13 12:35:11 +0100408 */
Michal Vaskoee38a5d2020-11-09 21:02:18 +0100409LY_ERR lyxp_set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type,
410 uint32_t *index_p);
Michal Vaskoecd62de2019-11-13 12:35:11 +0100411
412/**
413 * @brief Check for duplicates in a schema node set.
414 *
415 * @param[in] set Set to check.
416 * @param[in] node Node to look for in @p set.
417 * @param[in] node_type Type of @p node.
418 * @param[in] skip_idx Index from @p set to skip.
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200419 * @param[out] index_p Optional pointer to store index if the node is found.
Radek Krejci857189e2020-09-01 13:26:36 +0200420 * @return Boolean value whether the @p node found or not.
Michal Vaskoecd62de2019-11-13 12:35:11 +0100421 */
Radek Krejci857189e2020-09-01 13:26:36 +0200422ly_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 +0200423 int skip_idx, uint32_t *index_p);
Michal Vaskoecd62de2019-11-13 12:35:11 +0100424
425/**
426 * @brief Merge 2 schema node sets.
427 *
428 * @param[in] set1 Set to merge into.
429 * @param[in] set2 Set to merge. Its content is freed.
430 */
431void lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2);
432
433/**
Radek Krejcib1646a92018-11-02 16:08:26 +0100434 * @brief Parse an XPath expression into a structure of tokens.
435 * Logs directly.
436 *
Michal Vasko03ff5a72019-09-11 13:49:33 +0200437 * https://www.w3.org/TR/1999/REC-xpath-19991116/#exprlex
Radek Krejcib1646a92018-11-02 16:08:26 +0100438 *
439 * @param[in] ctx Context for errors.
Radek Krejcif03a9e22020-09-18 20:09:31 +0200440 * @param[in] expr_str XPath expression to parse. It is duplicated.
Michal Vasko004d3152020-06-11 19:59:22 +0200441 * @param[in] expr_len Length of @p expr, can be 0 if @p expr is 0-terminated.
442 * @param[in] reparse Whether to re-parse the expression to finalize full XPath parsing and fill
443 * information about expressions and their operators (fill repeat).
Radek Krejcif03a9e22020-09-18 20:09:31 +0200444 * @param[out] expr_p Pointer to return the filled expression structure.
445 * @return LY_SUCCESS in case of success.
446 * @return LY_EMEM in case of memory allocation failure.
447 * @return LY_EVALID in case of invalid XPath expression in @p expr_str.
Radek Krejcib1646a92018-11-02 16:08:26 +0100448 */
Michal Vaskoee38a5d2020-11-09 21:02:18 +0100449LY_ERR lyxp_expr_parse(const struct ly_ctx *ctx, const char *expr_str, size_t expr_len, ly_bool reparse,
450 struct lyxp_expr **expr_p);
Michal Vasko004d3152020-06-11 19:59:22 +0200451
452/**
453 * @brief Duplicate parsed XPath expression.
454 *
Michal Vaskoe33134a2022-07-29 14:54:40 +0200455 * If @p start_idx and @p end_idx are both 0, the whole expression is duplicated.
456 *
Michal Vasko004d3152020-06-11 19:59:22 +0200457 * @param[in] ctx Context with a dictionary.
458 * @param[in] exp Parsed expression.
Michal Vaskoe33134a2022-07-29 14:54:40 +0200459 * @param[in] start_idx Starting @p exp index to duplicate.
460 * @param[in] end_idx Last @p exp index to duplicate.
Michal Vasko1734be92020-09-22 08:55:10 +0200461 * @param[out] dup Duplicated structure.
462 * @return LY_ERR value.
Michal Vasko004d3152020-06-11 19:59:22 +0200463 */
Michal Vaskoe33134a2022-07-29 14:54:40 +0200464LY_ERR lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t start_idx, uint16_t end_idx,
465 struct lyxp_expr **dup);
Michal Vasko14676352020-05-29 11:35:55 +0200466
467/**
468 * @brief Look at the next token and check its kind.
469 *
470 * @param[in] ctx Context for logging, not logged if NULL.
471 * @param[in] exp Expression to use.
Michal Vasko004d3152020-06-11 19:59:22 +0200472 * @param[in] tok_idx Token index in the expression \p exp.
Michal Vasko14676352020-05-29 11:35:55 +0200473 * @param[in] want_tok Expected token.
474 * @return LY_EINCOMPLETE on EOF,
475 * @return LY_ENOT on non-matching token,
476 * @return LY_SUCCESS on success.
477 */
Michal Vasko004d3152020-06-11 19:59:22 +0200478LY_ERR lyxp_check_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t tok_idx, enum lyxp_token want_tok);
479
480/**
481 * @brief Look at the next token and skip it if it matches the expected one.
482 *
483 * @param[in] ctx Context for logging, not logged if NULL.
484 * @param[in] exp Expression to use.
485 * @param[in,out] tok_idx Token index in the expression \p exp, is updated.
486 * @param[in] want_tok Expected token.
487 * @return LY_EINCOMPLETE on EOF,
488 * @return LY_ENOT on non-matching token,
489 * @return LY_SUCCESS on success.
490 */
491LY_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 +0100492
493/**
Michal Vasko4911eeb2021-06-28 11:23:05 +0200494 * @brief Look at the next token and skip it if it matches either of the 2 expected ones.
495 *
496 * @param[in] ctx Context for logging, not logged if NULL.
497 * @param[in] exp Expression to use.
498 * @param[in,out] tok_idx Token index in the expression \p exp, is updated.
499 * @param[in] want_tok1 Expected token 1.
500 * @param[in] want_tok2 Expected token 2.
501 * @return LY_EINCOMPLETE on EOF,
502 * @return LY_ENOT on non-matching token,
503 * @return LY_SUCCESS on success.
504 */
505LY_ERR lyxp_next_token2(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx,
506 enum lyxp_token want_tok1, enum lyxp_token want_tok2);
507
508/**
aPiecekdf23eee2021-10-07 12:21:50 +0200509 * @brief Find variable named @name in @p vars.
510 *
511 * @param[in] vars [Sized array](@ref sizedarrays) of XPath variables.
512 * @param[in] name Name of the variable being searched.
513 * @param[in] name_len Name length can be set to 0 if @p name is terminated by null byte.
514 * @param[out] var Variable that was found. The parameter is optional.
515 * @return LY_SUCCESS if the variable was found, otherwise LY_ENOTFOUND.
516 */
517LY_ERR lyxp_vars_find(struct lyxp_var *vars, const char *name, size_t name_len, struct lyxp_var **var);
518
519/**
Michal Vasko03ff5a72019-09-11 13:49:33 +0200520 * @brief Frees a parsed XPath expression. @p expr should not be used afterwards.
Radek Krejcib1646a92018-11-02 16:08:26 +0100521 *
522 * @param[in] ctx libyang context of the expression.
523 * @param[in] expr Expression to free.
524 */
Michal Vasko14676352020-05-29 11:35:55 +0200525void lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr);
Radek Krejcib1646a92018-11-02 16:08:26 +0100526
Michal Vasko14676352020-05-29 11:35:55 +0200527#endif /* LY_XPATH_H */