blob: 4c5db9131d23c73fe7d0b6479bfaec400cd76528 [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 Krejci47fab892020-11-05 17:02:41 +010023#include "tree_data.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 Krejcie7b95092019-05-15 11:03:07 +020027
Radek Krejcib1646a92018-11-02 16:08:26 +010028/*
29 * XPath evaluator fully compliant with http://www.w3.org/TR/1999/REC-xpath-19991116/
30 * except the following restrictions in the grammar.
31 *
32 * PARSED GRAMMAR
33 *
34 * Full axes are not supported, abbreviated forms must be used,
35 * variables are not supported, "id()" function is not supported,
36 * and processing instruction and comment nodes are not supported,
37 * which is also reflected in the grammar. Undefined rules and
38 * constants are tokens.
39 *
40 * Modified full grammar:
41 *
42 * [1] Expr ::= OrExpr // just an alias
43 *
44 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
45 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
46 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
47 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
48 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vaskod3678892020-05-21 10:06:58 +020049 * [7] NameTest ::= '*' | NCName ':' '*' | QName
50 * [8] NodeType ::= 'text' | 'node'
51 * [9] Predicate ::= '[' Expr ']'
52 * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
53 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
54 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Radek Krejcib1646a92018-11-02 16:08:26 +010055 * | PrimaryExpr Predicate* '/' RelativeLocationPath
56 * | PrimaryExpr Predicate* '//' RelativeLocationPath
Michal Vaskod3678892020-05-21 10:06:58 +020057 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
58 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
59 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Radek Krejcib1646a92018-11-02 16:08:26 +010060 * | EqualityExpr '!=' RelationalExpr
Michal Vaskod3678892020-05-21 10:06:58 +020061 * [16] RelationalExpr ::= AdditiveExpr
Radek Krejcib1646a92018-11-02 16:08:26 +010062 * | RelationalExpr '<' AdditiveExpr
63 * | RelationalExpr '>' AdditiveExpr
64 * | RelationalExpr '<=' AdditiveExpr
65 * | RelationalExpr '>=' AdditiveExpr
Michal Vaskod3678892020-05-21 10:06:58 +020066 * [17] AdditiveExpr ::= MultiplicativeExpr
Radek Krejcib1646a92018-11-02 16:08:26 +010067 * | AdditiveExpr '+' MultiplicativeExpr
68 * | AdditiveExpr '-' MultiplicativeExpr
Michal Vaskod3678892020-05-21 10:06:58 +020069 * [18] MultiplicativeExpr ::= UnaryExpr
Radek Krejcib1646a92018-11-02 16:08:26 +010070 * | MultiplicativeExpr '*' UnaryExpr
71 * | MultiplicativeExpr 'div' UnaryExpr
72 * | MultiplicativeExpr 'mod' UnaryExpr
Michal Vaskod3678892020-05-21 10:06:58 +020073 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
74 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Radek Krejcib1646a92018-11-02 16:08:26 +010075 */
76
77/* expression tokens allocation */
78#define LYXP_EXPR_SIZE_START 10
79#define LYXP_EXPR_SIZE_STEP 5
80
81/* XPath matches allocation */
82#define LYXP_SET_SIZE_START 2
83#define LYXP_SET_SIZE_STEP 2
84
85/* building string when casting */
86#define LYXP_STRING_CAST_SIZE_START 64
87#define LYXP_STRING_CAST_SIZE_STEP 16
88
89/**
90 * @brief Tokens that can be in an XPath expression.
91 */
92enum lyxp_token {
93 LYXP_TOKEN_NONE = 0,
94 LYXP_TOKEN_PAR1, /* '(' */
95 LYXP_TOKEN_PAR2, /* ')' */
96 LYXP_TOKEN_BRACK1, /* '[' */
97 LYXP_TOKEN_BRACK2, /* ']' */
98 LYXP_TOKEN_DOT, /* '.' */
99 LYXP_TOKEN_DDOT, /* '..' */
100 LYXP_TOKEN_AT, /* '@' */
101 LYXP_TOKEN_COMMA, /* ',' */
102 /* LYXP_TOKEN_DCOLON, * '::' * axes not supported */
103 LYXP_TOKEN_NAMETEST, /* NameTest */
104 LYXP_TOKEN_NODETYPE, /* NodeType */
105 LYXP_TOKEN_FUNCNAME, /* FunctionName */
Michal Vasko3e48bf32020-06-01 08:39:07 +0200106 LYXP_TOKEN_OPER_LOG, /* Operator 'and', 'or' */
107 LYXP_TOKEN_OPER_EQUAL, /* Operator '=' */
108 LYXP_TOKEN_OPER_NEQUAL, /* Operator '!=' */
109 LYXP_TOKEN_OPER_COMP, /* Operator '<', '<=', '>', '>=' */
110 LYXP_TOKEN_OPER_MATH, /* Operator '+', '-', '*', 'div', 'mod', '-' (unary) */
111 LYXP_TOKEN_OPER_UNI, /* Operator '|' */
112 LYXP_TOKEN_OPER_PATH, /* Operator '/' */
113 LYXP_TOKEN_OPER_RPATH, /* Operator '//' (recursive path) */
Radek Krejcib1646a92018-11-02 16:08:26 +0100114 /* LYXP_TOKEN_AXISNAME, * AxisName * axes not supported */
115 LYXP_TOKEN_LITERAL, /* Literal - with either single or double quote */
116 LYXP_TOKEN_NUMBER /* Number */
117};
118
119/**
120 * @brief XPath (sub)expressions that can be repeated.
121 */
122enum lyxp_expr_type {
123 LYXP_EXPR_NONE = 0,
124 LYXP_EXPR_OR,
125 LYXP_EXPR_AND,
126 LYXP_EXPR_EQUALITY,
127 LYXP_EXPR_RELATIONAL,
128 LYXP_EXPR_ADDITIVE,
129 LYXP_EXPR_MULTIPLICATIVE,
130 LYXP_EXPR_UNARY,
Michal Vasko69730152020-10-09 16:30:07 +0200131 LYXP_EXPR_UNION
Radek Krejcib1646a92018-11-02 16:08:26 +0100132};
133
134/**
Michal Vasko03ff5a72019-09-11 13:49:33 +0200135 * @brief Types of context nodes, #LYXP_NODE_ROOT_CONFIG used only in when or must conditions.
136 */
137enum lyxp_node_type {
Michal Vasko2caefc12019-11-14 16:07:56 +0100138 LYXP_NODE_NONE, /* invalid node type */
139
Michal Vasko03ff5a72019-09-11 13:49:33 +0200140 /* XML document roots */
141 LYXP_NODE_ROOT, /* access to all the data (node value first top-level node) */
142 LYXP_NODE_ROOT_CONFIG, /* <running> data context, no state data (node value first top-level node) */
143
144 /* XML elements */
Michal Vasko9f96a052020-03-10 09:41:45 +0100145 LYXP_NODE_ELEM, /* YANG data element (most common) */
146 LYXP_NODE_TEXT, /* YANG data text element (extremely specific use, unlikely to be ever needed) */
147 LYXP_NODE_META /* YANG metadata (do not use for the context node) */
Michal Vasko03ff5a72019-09-11 13:49:33 +0200148};
149
150/**
Radek Krejcib1646a92018-11-02 16:08:26 +0100151 * @brief Structure holding a parsed XPath expression.
152 */
153struct lyxp_expr {
154 enum lyxp_token *tokens; /* array of tokens */
155 uint16_t *tok_pos; /* array of the token offsets in expr */
156 uint16_t *tok_len; /* array of token lengths in expr */
157 enum lyxp_expr_type **repeat; /* array of expression types that this token begins and is repeated ended with 0,
158 more in the comment after this declaration */
159 uint16_t used; /* used array items */
160 uint16_t size; /* allocated array items */
161
162 const char *expr; /* the original XPath expression */
163};
164
165/*
166 * lyxp_expr repeat
167 *
168 * This value is NULL for all the tokens that do not begin an
169 * expression which can be repeated. Otherwise it is an array
170 * of expression types that this token begins. These values
171 * are used during evaluation to know whether we need to
172 * duplicate the current context or not and to decide what
173 * the current expression is (for example, if we are only
174 * starting the parsing and the first token has no repeat,
175 * we do not parse it as an OrExpr but directly as PathExpr).
176 * Examples:
177 *
178 * Expression: "/ *[key1 and key2 or key1 < key2]"
179 * Tokens: '/', '*', '[', NameTest, 'and', NameTest, 'or', NameTest, '<', NameTest, ']'
180 * Repeat: NULL, NULL, NULL, [AndExpr, NULL, NULL, NULL, [RelationalExpr, NULL, NULL, NULL
181 * OrExpr, 0],
182 * 0],
183 *
184 * Expression: "//node[key and node2]/key | /cont"
185 * Tokens: '//', 'NameTest', '[', 'NameTest', 'and', 'NameTest', ']', '/', 'NameTest', '|', '/', 'NameTest'
186 * Repeat: [UnionExpr, NULL, NULL, [AndExpr, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
187 * 0], 0],
188 *
189 * Operators between expressions which this concerns:
190 * 'or', 'and', '=', '!=', '<', '>', '<=', '>=', '+', '-', '*', 'div', 'mod', '|'
191 */
192
193/**
194 * @brief Supported types of (partial) XPath results.
195 */
196enum lyxp_set_type {
Michal Vaskod3678892020-05-21 10:06:58 +0200197 LYXP_SET_NODE_SET = 0,
Michal Vasko03ff5a72019-09-11 13:49:33 +0200198 LYXP_SET_SCNODE_SET,
Radek Krejcib1646a92018-11-02 16:08:26 +0100199 LYXP_SET_BOOLEAN,
200 LYXP_SET_NUMBER,
201 LYXP_SET_STRING
202};
Radek Krejcib1646a92018-11-02 16:08:26 +0100203
204/**
205 * @brief Item stored in an XPath set hash table.
206 */
207struct lyxp_set_hash_node {
208 struct lyd_node *node;
209 enum lyxp_node_type type;
210} _PACKED;
211
Radek Krejcib1646a92018-11-02 16:08:26 +0100212/**
213 * @brief XPath set - (partial) result.
214 */
215struct lyxp_set {
216 enum lyxp_set_type type;
217 union {
218 struct lyxp_set_node {
219 struct lyd_node *node;
220 enum lyxp_node_type type;
221 uint32_t pos;
222 } *nodes;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200223 struct lyxp_set_scnode {
224 struct lysc_node *scnode;
Radek Krejcib1646a92018-11-02 16:08:26 +0100225 enum lyxp_node_type type;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100226
227#define LYXP_SET_SCNODE_START -2 /**< scnode not traversed, currently (the only node) in context */
228#define LYXP_SET_SCNODE_START_USED -1 /**< scnode not traversed except for the eval start, not currently in the context */
229#define LYXP_SET_SCNODE_ATOM 0 /**< scnode was traversed, but not currently in the context */
230#define LYXP_SET_SCNODE_ATOM_CTX 1 /**< scnode currently in context */
231#define LYXP_SET_SCNODE_ATOM_NEW_CTX 2 /**< scnode in context and just added, so skip it for the current operation */
232#define LYXP_SET_SCNODE_ATOM_PRED_CTX 3 /**< includes any higher value - scnode is not in context because we are in
233 a predicate and this scnode was used/will be used later */
234 int32_t in_ctx; /**< values defined as LYXP_SET_SCNODE_* */
Michal Vasko03ff5a72019-09-11 13:49:33 +0200235 } *scnodes;
Michal Vasko9f96a052020-03-10 09:41:45 +0100236 struct lyxp_set_meta {
237 struct lyd_meta *meta;
Radek Krejcib1646a92018-11-02 16:08:26 +0100238 enum lyxp_node_type type;
Michal Vasko9f96a052020-03-10 09:41:45 +0100239 uint32_t pos; /* if node_type is LYXP_SET_NODE_META, it is the parent node position */
240 } *meta;
Radek Krejcib1646a92018-11-02 16:08:26 +0100241 char *str;
242 long double num;
Radek Krejci857189e2020-09-01 13:26:36 +0200243 ly_bool bln; /* boolean */
Radek Krejcib1646a92018-11-02 16:08:26 +0100244 } val;
245
Michal Vasko03ff5a72019-09-11 13:49:33 +0200246 /* this is valid only for type LYXP_SET_NODE_SET and LYXP_SET_SCNODE_SET */
Radek Krejcib1646a92018-11-02 16:08:26 +0100247 uint32_t used;
248 uint32_t size;
Radek Krejcib1646a92018-11-02 16:08:26 +0100249 struct hash_table *ht;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200250
251 /* XPath context information, this is valid only for type LYXP_SET_NODE_SET */
Radek Krejcib1646a92018-11-02 16:08:26 +0100252 uint32_t ctx_pos;
253 uint32_t ctx_size;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200254
255 /* general context */
256 struct ly_ctx *ctx;
257 union {
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200258 const struct lyd_node *cur_node;
259 const struct lysc_node *cur_scnode;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200260 };
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100261 enum lyxp_node_type root_type;
Michal Vasko6b26e742020-07-17 15:02:10 +0200262 const struct lysc_node *context_op;
Michal Vaskof03ed032020-03-04 13:31:44 +0100263 const struct lyd_node *tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200264 const struct lys_module *cur_mod;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200265 LY_PREFIX_FORMAT format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200266 void *prefix_data;
Radek Krejcib1646a92018-11-02 16:08:26 +0100267};
268
269/**
Michal Vasko24cddf82020-06-01 08:17:01 +0200270 * @brief Print an XPath token \p tok type.
271 *
272 * @param[in] tok Token to print.
273 * @return Token type string.
274 */
275const char *lyxp_print_token(enum lyxp_token tok);
276
277/**
Michal Vasko03ff5a72019-09-11 13:49:33 +0200278 * @brief Evaluate an XPath expression on data. Be careful when using this function, the result can often
279 * be confusing without thorough understanding of XPath evaluation rules defined in RFC 7950.
Radek Krejcib1646a92018-11-02 16:08:26 +0100280 *
Michal Vaskoecd62de2019-11-13 12:35:11 +0100281 * @param[in] exp Parsed XPath expression to be evaluated.
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200282 * @param[in] cur_mod Current module for the expression (where it was "instantiated").
Michal Vasko03ff5a72019-09-11 13:49:33 +0200283 * @param[in] format Format of the XPath expression (more specifcally, of any used prefixes).
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200284 * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
285 * @param[in] ctx_node Current (context) data node, NULL in case of the root node.
Michal Vaskof03ed032020-03-04 13:31:44 +0100286 * @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 +0100287 * the tree of @p ctx_node). Can be any node of the tree, it is adjusted.
Michal Vasko004d3152020-06-11 19:59:22 +0200288 * @param[out] set Result set.
Radek Krejcib1646a92018-11-02 16:08:26 +0100289 * @param[in] options Whether to apply some evaluation restrictions.
Michal Vasko004d3152020-06-11 19:59:22 +0200290 * @return LY_EVALID for invalid argument types/count,
291 * @return LY_EINCOMPLETE for unresolved when,
292 * @return LY_EINVAL, LY_EMEM, LY_EINT for other errors.
Radek Krejcib1646a92018-11-02 16:08:26 +0100293 */
Michal Vasko40308e72020-10-20 16:38:40 +0200294LY_ERR lyxp_eval(const struct lyxp_expr *exp, const struct lys_module *cur_mod, LY_PREFIX_FORMAT format, void *prefix_data,
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200295 const struct lyd_node *ctx_node, const struct lyd_node *tree, struct lyxp_set *set, uint32_t options);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200296
Radek Krejcib1646a92018-11-02 16:08:26 +0100297/**
Michal Vasko03ff5a72019-09-11 13:49:33 +0200298 * @brief Get all the partial XPath nodes (atoms) that are required for @p exp to be evaluated.
Radek Krejcib1646a92018-11-02 16:08:26 +0100299 *
Michal Vasko03ff5a72019-09-11 13:49:33 +0200300 * @param[in] exp Parsed XPath expression to be evaluated.
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200301 * @param[in] cur_mod Current module for the expression (where it was "instantiated").
Michal Vasko03ff5a72019-09-11 13:49:33 +0200302 * @param[in] format Format of the XPath expression (more specifcally, of any used prefixes).
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200303 * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
304 * @param[in] ctx_scnode Current (context) schema node, NULL in case of the root node.
Michal Vasko004d3152020-06-11 19:59:22 +0200305 * @param[out] set Result set.
Radek Krejcib1646a92018-11-02 16:08:26 +0100306 * @param[in] options Whether to apply some evaluation restrictions, one flag must always be used.
Radek Krejci8678fa42020-08-18 16:07:28 +0200307 * @return LY_ERR (same as ::lyxp_eval()).
Radek Krejcib1646a92018-11-02 16:08:26 +0100308 */
Michal Vasko40308e72020-10-20 16:38:40 +0200309LY_ERR lyxp_atomize(const struct lyxp_expr *exp, const struct lys_module *cur_mod, LY_PREFIX_FORMAT format, void *prefix_data,
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200310 const struct lysc_node *ctx_scnode, struct lyxp_set *set, uint32_t options);
Radek Krejcib1646a92018-11-02 16:08:26 +0100311
Radek Krejcibed13942020-10-19 16:06:28 +0200312/* used only internally, maps with @ref findxpathoptions */
Michal Vaskocdad7122020-11-09 21:04:44 +0100313#define LYXP_IGNORE_WHEN 0x01 /**< Ignore unevaluated when in data nodes and do not return ::LY_EINCOMPLETE. */
314#define LYXP_SCHEMA 0x02 /**< Apply data node access restrictions defined for 'when' and 'must' evaluation. */
315#define LYXP_SCNODE 0x04 /**< No special tree access modifiers. */
316#define LYXP_SCNODE_SCHEMA LYS_FIND_XP_SCHEMA /**< Apply node access restrictions defined for 'when' and 'must' evaluation. */
317#define LYXP_SCNODE_OUTPUT LYS_FIND_XP_OUTPUT /**< Search RPC/action output nodes instead of input ones. */
318#define LYXP_SCNODE_ALL 0x1C /**< mask for all the LYXP_* values */
Radek Krejcib1646a92018-11-02 16:08:26 +0100319
320/**
Radek Krejcib1646a92018-11-02 16:08:26 +0100321 * @brief Cast XPath set to another type.
322 * Indirectly context position aware.
323 *
324 * @param[in] set Set to cast.
325 * @param[in] target Target type to cast \p set into.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200326 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +0100327 */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100328LY_ERR lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200329
Radek Krejcib1646a92018-11-02 16:08:26 +0100330/**
Michal Vaskod3678892020-05-21 10:06:58 +0200331 * @brief Free dynamic content of a set.
332 *
333 * @param[in] set Set to modify.
334 */
335void lyxp_set_free_content(struct lyxp_set *set);
336
337/**
Michal Vaskoecd62de2019-11-13 12:35:11 +0100338 * @brief Insert schema node into set.
339 *
340 * @param[in] set Set to insert into.
341 * @param[in] node Node to insert.
342 * @param[in] node_type Node type of @p node.
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200343 * @param[out] index_p Optional pointer to store index if the inserted @p node.
344 * @return LY_SUCCESS on success.
345 * @return LY_EMEM on memory allocation failure.
Michal Vaskoecd62de2019-11-13 12:35:11 +0100346 */
Michal Vaskoee38a5d2020-11-09 21:02:18 +0100347LY_ERR lyxp_set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type,
348 uint32_t *index_p);
Michal Vaskoecd62de2019-11-13 12:35:11 +0100349
350/**
351 * @brief Check for duplicates in a schema node set.
352 *
353 * @param[in] set Set to check.
354 * @param[in] node Node to look for in @p set.
355 * @param[in] node_type Type of @p node.
356 * @param[in] skip_idx Index from @p set to skip.
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200357 * @param[out] index_p Optional pointer to store index if the node is found.
Radek Krejci857189e2020-09-01 13:26:36 +0200358 * @return Boolean value whether the @p node found or not.
Michal Vaskoecd62de2019-11-13 12:35:11 +0100359 */
Radek Krejci857189e2020-09-01 13:26:36 +0200360ly_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 +0200361 int skip_idx, uint32_t *index_p);
Michal Vaskoecd62de2019-11-13 12:35:11 +0100362
363/**
364 * @brief Merge 2 schema node sets.
365 *
366 * @param[in] set1 Set to merge into.
367 * @param[in] set2 Set to merge. Its content is freed.
368 */
369void lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2);
370
371/**
Radek Krejcib1646a92018-11-02 16:08:26 +0100372 * @brief Parse an XPath expression into a structure of tokens.
373 * Logs directly.
374 *
Michal Vasko03ff5a72019-09-11 13:49:33 +0200375 * https://www.w3.org/TR/1999/REC-xpath-19991116/#exprlex
Radek Krejcib1646a92018-11-02 16:08:26 +0100376 *
377 * @param[in] ctx Context for errors.
Radek Krejcif03a9e22020-09-18 20:09:31 +0200378 * @param[in] expr_str XPath expression to parse. It is duplicated.
Michal Vasko004d3152020-06-11 19:59:22 +0200379 * @param[in] expr_len Length of @p expr, can be 0 if @p expr is 0-terminated.
380 * @param[in] reparse Whether to re-parse the expression to finalize full XPath parsing and fill
381 * information about expressions and their operators (fill repeat).
Radek Krejcif03a9e22020-09-18 20:09:31 +0200382 * @param[out] expr_p Pointer to return the filled expression structure.
383 * @return LY_SUCCESS in case of success.
384 * @return LY_EMEM in case of memory allocation failure.
385 * @return LY_EVALID in case of invalid XPath expression in @p expr_str.
Radek Krejcib1646a92018-11-02 16:08:26 +0100386 */
Michal Vaskoee38a5d2020-11-09 21:02:18 +0100387LY_ERR lyxp_expr_parse(const struct ly_ctx *ctx, const char *expr_str, size_t expr_len, ly_bool reparse,
388 struct lyxp_expr **expr_p);
Michal Vasko004d3152020-06-11 19:59:22 +0200389
390/**
391 * @brief Duplicate parsed XPath expression.
392 *
393 * @param[in] ctx Context with a dictionary.
394 * @param[in] exp Parsed expression.
Michal Vasko1734be92020-09-22 08:55:10 +0200395 * @param[out] dup Duplicated structure.
396 * @return LY_ERR value.
Michal Vasko004d3152020-06-11 19:59:22 +0200397 */
Michal Vasko1734be92020-09-22 08:55:10 +0200398LY_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 +0200399
400/**
401 * @brief Look at the next token and check its kind.
402 *
403 * @param[in] ctx Context for logging, not logged if NULL.
404 * @param[in] exp Expression to use.
Michal Vasko004d3152020-06-11 19:59:22 +0200405 * @param[in] tok_idx Token index in the expression \p exp.
Michal Vasko14676352020-05-29 11:35:55 +0200406 * @param[in] want_tok Expected token.
407 * @return LY_EINCOMPLETE on EOF,
408 * @return LY_ENOT on non-matching token,
409 * @return LY_SUCCESS on success.
410 */
Michal Vasko004d3152020-06-11 19:59:22 +0200411LY_ERR lyxp_check_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t tok_idx, enum lyxp_token want_tok);
412
413/**
414 * @brief Look at the next token and skip it if it matches the expected one.
415 *
416 * @param[in] ctx Context for logging, not logged if NULL.
417 * @param[in] exp Expression to use.
418 * @param[in,out] tok_idx Token index in the expression \p exp, is updated.
419 * @param[in] want_tok Expected token.
420 * @return LY_EINCOMPLETE on EOF,
421 * @return LY_ENOT on non-matching token,
422 * @return LY_SUCCESS on success.
423 */
424LY_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 +0100425
426/**
Michal Vasko03ff5a72019-09-11 13:49:33 +0200427 * @brief Frees a parsed XPath expression. @p expr should not be used afterwards.
Radek Krejcib1646a92018-11-02 16:08:26 +0100428 *
429 * @param[in] ctx libyang context of the expression.
430 * @param[in] expr Expression to free.
431 */
Michal Vasko14676352020-05-29 11:35:55 +0200432void lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr);
Radek Krejcib1646a92018-11-02 16:08:26 +0100433
Michal Vasko14676352020-05-29 11:35:55 +0200434#endif /* LY_XPATH_H */