blob: 316f535a346202db872059ea2c50aaec47378282 [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
18#include <stdint.h>
19
Radek Krejci535ea9f2020-05-29 16:01:05 +020020#include "config.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020021#include "log.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020022#include "tree_schema.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020023
24struct ly_ctx;
Radek Krejcie7b95092019-05-15 11:03:07 +020025
Radek Krejcib1646a92018-11-02 16:08:26 +010026/*
27 * XPath evaluator fully compliant with http://www.w3.org/TR/1999/REC-xpath-19991116/
28 * except the following restrictions in the grammar.
29 *
30 * PARSED GRAMMAR
31 *
32 * Full axes are not supported, abbreviated forms must be used,
33 * variables are not supported, "id()" function is not supported,
34 * and processing instruction and comment nodes are not supported,
35 * which is also reflected in the grammar. Undefined rules and
36 * constants are tokens.
37 *
38 * Modified full grammar:
39 *
40 * [1] Expr ::= OrExpr // just an alias
41 *
42 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
43 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
44 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
45 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
46 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vaskod3678892020-05-21 10:06:58 +020047 * [7] NameTest ::= '*' | NCName ':' '*' | QName
48 * [8] NodeType ::= 'text' | 'node'
49 * [9] Predicate ::= '[' Expr ']'
50 * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
51 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
52 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Radek Krejcib1646a92018-11-02 16:08:26 +010053 * | PrimaryExpr Predicate* '/' RelativeLocationPath
54 * | PrimaryExpr Predicate* '//' RelativeLocationPath
Michal Vaskod3678892020-05-21 10:06:58 +020055 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
56 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
57 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Radek Krejcib1646a92018-11-02 16:08:26 +010058 * | EqualityExpr '!=' RelationalExpr
Michal Vaskod3678892020-05-21 10:06:58 +020059 * [16] RelationalExpr ::= AdditiveExpr
Radek Krejcib1646a92018-11-02 16:08:26 +010060 * | RelationalExpr '<' AdditiveExpr
61 * | RelationalExpr '>' AdditiveExpr
62 * | RelationalExpr '<=' AdditiveExpr
63 * | RelationalExpr '>=' AdditiveExpr
Michal Vaskod3678892020-05-21 10:06:58 +020064 * [17] AdditiveExpr ::= MultiplicativeExpr
Radek Krejcib1646a92018-11-02 16:08:26 +010065 * | AdditiveExpr '+' MultiplicativeExpr
66 * | AdditiveExpr '-' MultiplicativeExpr
Michal Vaskod3678892020-05-21 10:06:58 +020067 * [18] MultiplicativeExpr ::= UnaryExpr
Radek Krejcib1646a92018-11-02 16:08:26 +010068 * | MultiplicativeExpr '*' UnaryExpr
69 * | MultiplicativeExpr 'div' UnaryExpr
70 * | MultiplicativeExpr 'mod' UnaryExpr
Michal Vaskod3678892020-05-21 10:06:58 +020071 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
72 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Radek Krejcib1646a92018-11-02 16:08:26 +010073 */
74
75/* expression tokens allocation */
76#define LYXP_EXPR_SIZE_START 10
77#define LYXP_EXPR_SIZE_STEP 5
78
79/* XPath matches allocation */
80#define LYXP_SET_SIZE_START 2
81#define LYXP_SET_SIZE_STEP 2
82
83/* building string when casting */
84#define LYXP_STRING_CAST_SIZE_START 64
85#define LYXP_STRING_CAST_SIZE_STEP 16
86
87/**
88 * @brief Tokens that can be in an XPath expression.
89 */
90enum lyxp_token {
91 LYXP_TOKEN_NONE = 0,
92 LYXP_TOKEN_PAR1, /* '(' */
93 LYXP_TOKEN_PAR2, /* ')' */
94 LYXP_TOKEN_BRACK1, /* '[' */
95 LYXP_TOKEN_BRACK2, /* ']' */
96 LYXP_TOKEN_DOT, /* '.' */
97 LYXP_TOKEN_DDOT, /* '..' */
98 LYXP_TOKEN_AT, /* '@' */
99 LYXP_TOKEN_COMMA, /* ',' */
100 /* LYXP_TOKEN_DCOLON, * '::' * axes not supported */
101 LYXP_TOKEN_NAMETEST, /* NameTest */
102 LYXP_TOKEN_NODETYPE, /* NodeType */
103 LYXP_TOKEN_FUNCNAME, /* FunctionName */
Michal Vasko3e48bf32020-06-01 08:39:07 +0200104 LYXP_TOKEN_OPER_LOG, /* Operator 'and', 'or' */
105 LYXP_TOKEN_OPER_EQUAL, /* Operator '=' */
106 LYXP_TOKEN_OPER_NEQUAL, /* Operator '!=' */
107 LYXP_TOKEN_OPER_COMP, /* Operator '<', '<=', '>', '>=' */
108 LYXP_TOKEN_OPER_MATH, /* Operator '+', '-', '*', 'div', 'mod', '-' (unary) */
109 LYXP_TOKEN_OPER_UNI, /* Operator '|' */
110 LYXP_TOKEN_OPER_PATH, /* Operator '/' */
111 LYXP_TOKEN_OPER_RPATH, /* Operator '//' (recursive path) */
Radek Krejcib1646a92018-11-02 16:08:26 +0100112 /* LYXP_TOKEN_AXISNAME, * AxisName * axes not supported */
113 LYXP_TOKEN_LITERAL, /* Literal - with either single or double quote */
114 LYXP_TOKEN_NUMBER /* Number */
115};
116
117/**
118 * @brief XPath (sub)expressions that can be repeated.
119 */
120enum lyxp_expr_type {
121 LYXP_EXPR_NONE = 0,
122 LYXP_EXPR_OR,
123 LYXP_EXPR_AND,
124 LYXP_EXPR_EQUALITY,
125 LYXP_EXPR_RELATIONAL,
126 LYXP_EXPR_ADDITIVE,
127 LYXP_EXPR_MULTIPLICATIVE,
128 LYXP_EXPR_UNARY,
129 LYXP_EXPR_UNION,
130};
131
132/**
Michal Vasko03ff5a72019-09-11 13:49:33 +0200133 * @brief Types of context nodes, #LYXP_NODE_ROOT_CONFIG used only in when or must conditions.
134 */
135enum lyxp_node_type {
Michal Vasko2caefc12019-11-14 16:07:56 +0100136 LYXP_NODE_NONE, /* invalid node type */
137
Michal Vasko03ff5a72019-09-11 13:49:33 +0200138 /* XML document roots */
139 LYXP_NODE_ROOT, /* access to all the data (node value first top-level node) */
140 LYXP_NODE_ROOT_CONFIG, /* <running> data context, no state data (node value first top-level node) */
141
142 /* XML elements */
Michal Vasko9f96a052020-03-10 09:41:45 +0100143 LYXP_NODE_ELEM, /* YANG data element (most common) */
144 LYXP_NODE_TEXT, /* YANG data text element (extremely specific use, unlikely to be ever needed) */
145 LYXP_NODE_META /* YANG metadata (do not use for the context node) */
Michal Vasko03ff5a72019-09-11 13:49:33 +0200146};
147
148/**
Radek Krejcib1646a92018-11-02 16:08:26 +0100149 * @brief Structure holding a parsed XPath expression.
150 */
151struct lyxp_expr {
152 enum lyxp_token *tokens; /* array of tokens */
153 uint16_t *tok_pos; /* array of the token offsets in expr */
154 uint16_t *tok_len; /* array of token lengths in expr */
155 enum lyxp_expr_type **repeat; /* array of expression types that this token begins and is repeated ended with 0,
156 more in the comment after this declaration */
157 uint16_t used; /* used array items */
158 uint16_t size; /* allocated array items */
159
160 const char *expr; /* the original XPath expression */
161};
162
163/*
164 * lyxp_expr repeat
165 *
166 * This value is NULL for all the tokens that do not begin an
167 * expression which can be repeated. Otherwise it is an array
168 * of expression types that this token begins. These values
169 * are used during evaluation to know whether we need to
170 * duplicate the current context or not and to decide what
171 * the current expression is (for example, if we are only
172 * starting the parsing and the first token has no repeat,
173 * we do not parse it as an OrExpr but directly as PathExpr).
174 * Examples:
175 *
176 * Expression: "/ *[key1 and key2 or key1 < key2]"
177 * Tokens: '/', '*', '[', NameTest, 'and', NameTest, 'or', NameTest, '<', NameTest, ']'
178 * Repeat: NULL, NULL, NULL, [AndExpr, NULL, NULL, NULL, [RelationalExpr, NULL, NULL, NULL
179 * OrExpr, 0],
180 * 0],
181 *
182 * Expression: "//node[key and node2]/key | /cont"
183 * Tokens: '//', 'NameTest', '[', 'NameTest', 'and', 'NameTest', ']', '/', 'NameTest', '|', '/', 'NameTest'
184 * Repeat: [UnionExpr, NULL, NULL, [AndExpr, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
185 * 0], 0],
186 *
187 * Operators between expressions which this concerns:
188 * 'or', 'and', '=', '!=', '<', '>', '<=', '>=', '+', '-', '*', 'div', 'mod', '|'
189 */
190
191/**
192 * @brief Supported types of (partial) XPath results.
193 */
194enum lyxp_set_type {
Michal Vaskod3678892020-05-21 10:06:58 +0200195 LYXP_SET_NODE_SET = 0,
Michal Vasko03ff5a72019-09-11 13:49:33 +0200196 LYXP_SET_SCNODE_SET,
Radek Krejcib1646a92018-11-02 16:08:26 +0100197 LYXP_SET_BOOLEAN,
198 LYXP_SET_NUMBER,
199 LYXP_SET_STRING
200};
Radek Krejcib1646a92018-11-02 16:08:26 +0100201
202/**
203 * @brief Item stored in an XPath set hash table.
204 */
205struct lyxp_set_hash_node {
206 struct lyd_node *node;
207 enum lyxp_node_type type;
208} _PACKED;
209
Radek Krejcib1646a92018-11-02 16:08:26 +0100210/**
211 * @brief XPath set - (partial) result.
212 */
213struct lyxp_set {
214 enum lyxp_set_type type;
215 union {
216 struct lyxp_set_node {
217 struct lyd_node *node;
218 enum lyxp_node_type type;
219 uint32_t pos;
220 } *nodes;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200221 struct lyxp_set_scnode {
222 struct lysc_node *scnode;
Radek Krejcib1646a92018-11-02 16:08:26 +0100223 enum lyxp_node_type type;
Michal Vasko5c4e5892019-11-14 12:31:38 +0100224 /* -2 - scnode not traversed, currently (the only node) in context;
225 * -1 - scnode not traversed except for the eval start, not currently in the context;
226 * 0 - scnode was traversed, but not currently in the context;
227 * 1 - scnode currently in context;
228 * 2 - scnode in context and just added, so skip it for the current operation;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200229 * >=3 - scnode is not in context because we are in a predicate and this scnode was used/will be used later */
Michal Vasko5c4e5892019-11-14 12:31:38 +0100230 int32_t in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200231 } *scnodes;
Michal Vasko9f96a052020-03-10 09:41:45 +0100232 struct lyxp_set_meta {
233 struct lyd_meta *meta;
Radek Krejcib1646a92018-11-02 16:08:26 +0100234 enum lyxp_node_type type;
Michal Vasko9f96a052020-03-10 09:41:45 +0100235 uint32_t pos; /* if node_type is LYXP_SET_NODE_META, it is the parent node position */
236 } *meta;
Radek Krejcib1646a92018-11-02 16:08:26 +0100237 char *str;
238 long double num;
Michal Vasko004d3152020-06-11 19:59:22 +0200239 int bln;
Radek Krejcib1646a92018-11-02 16:08:26 +0100240 } val;
241
Michal Vasko03ff5a72019-09-11 13:49:33 +0200242 /* this is valid only for type LYXP_SET_NODE_SET and LYXP_SET_SCNODE_SET */
Radek Krejcib1646a92018-11-02 16:08:26 +0100243 uint32_t used;
244 uint32_t size;
Radek Krejcib1646a92018-11-02 16:08:26 +0100245 struct hash_table *ht;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200246
247 /* XPath context information, this is valid only for type LYXP_SET_NODE_SET */
Radek Krejcib1646a92018-11-02 16:08:26 +0100248 uint32_t ctx_pos;
249 uint32_t ctx_size;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200250
251 /* general context */
252 struct ly_ctx *ctx;
253 union {
254 const struct lyd_node *ctx_node;
255 const struct lysc_node *ctx_scnode;
256 };
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100257 enum lyxp_node_type root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200258 const struct lys_module *local_mod;
Michal Vaskof03ed032020-03-04 13:31:44 +0100259 const struct lyd_node *tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200260 LYD_FORMAT format;
Radek Krejcib1646a92018-11-02 16:08:26 +0100261};
262
263/**
Michal Vasko24cddf82020-06-01 08:17:01 +0200264 * @brief Print an XPath token \p tok type.
265 *
266 * @param[in] tok Token to print.
267 * @return Token type string.
268 */
269const char *lyxp_print_token(enum lyxp_token tok);
270
271/**
Michal Vasko03ff5a72019-09-11 13:49:33 +0200272 * @brief Evaluate an XPath expression on data. Be careful when using this function, the result can often
273 * be confusing without thorough understanding of XPath evaluation rules defined in RFC 7950.
Radek Krejcib1646a92018-11-02 16:08:26 +0100274 *
Michal Vaskoecd62de2019-11-13 12:35:11 +0100275 * @param[in] exp Parsed XPath expression to be evaluated.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200276 * @param[in] format Format of the XPath expression (more specifcally, of any used prefixes).
277 * @param[in] local_mod Local module relative to the @p expr.
Michal Vasko004d3152020-06-11 19:59:22 +0200278 * @param[in] ctx_node Current (context) data node. In case of a root node, set @p ctx_node_type correctly,
279 * but @p ctx_node must also be set to any node from the root node module - it will be used for resolving
280 * unqualified names.
281 * @param[in] ctx_node_type Current (context) data node type.
Michal Vaskof03ed032020-03-04 13:31:44 +0100282 * @param[in] tree Data tree on which to perform the evaluation, it must include all the available data (including
Michal Vaskod0aa1a82019-11-07 08:58:42 +0100283 * the tree of @p ctx_node).
Michal Vasko004d3152020-06-11 19:59:22 +0200284 * @param[out] set Result set.
Radek Krejcib1646a92018-11-02 16:08:26 +0100285 * @param[in] options Whether to apply some evaluation restrictions.
Michal Vasko004d3152020-06-11 19:59:22 +0200286 * @return LY_EVALID for invalid argument types/count,
287 * @return LY_EINCOMPLETE for unresolved when,
288 * @return LY_EINVAL, LY_EMEM, LY_EINT for other errors.
Radek Krejcib1646a92018-11-02 16:08:26 +0100289 */
Michal Vaskoecd62de2019-11-13 12:35:11 +0100290LY_ERR lyxp_eval(struct lyxp_expr *exp, LYD_FORMAT format, const struct lys_module *local_mod, const struct lyd_node *ctx_node,
Michal Vaskof03ed032020-03-04 13:31:44 +0100291 enum lyxp_node_type ctx_node_type, const struct lyd_node *tree, struct lyxp_set *set, int options);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200292
Michal Vasko004d3152020-06-11 19:59:22 +0200293#define LYXP_SCHEMA 0x01 /**< Apply data node access restrictions defined for 'when' and 'must' evaluation. */
Radek Krejcib1646a92018-11-02 16:08:26 +0100294
295/**
Michal Vasko03ff5a72019-09-11 13:49:33 +0200296 * @brief Get all the partial XPath nodes (atoms) that are required for @p exp to be evaluated.
Radek Krejcib1646a92018-11-02 16:08:26 +0100297 *
Michal Vasko03ff5a72019-09-11 13:49:33 +0200298 * @param[in] exp Parsed XPath expression to be evaluated.
299 * @param[in] format Format of the XPath expression (more specifcally, of any used prefixes).
300 * @param[in] local_mod Local module relative to the @p exp.
Michal Vasko004d3152020-06-11 19:59:22 +0200301 * @param[in] ctx_scnode Current (context) schema node. In case of a root node, set @p ctx_scnode_type correctly,
302 * but @p ctx_scnode must also be set to any node from the root node module - it will be used for resolving
303 * unqualified names.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200304 * @param[in] ctx_scnode_type Current (context) schema node type.
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.
Michal Vaskoecd62de2019-11-13 12:35:11 +0100307 * @return LY_ERR (same as lyxp_eval()).
Radek Krejcib1646a92018-11-02 16:08:26 +0100308 */
Michal Vasko03ff5a72019-09-11 13:49:33 +0200309LY_ERR lyxp_atomize(struct lyxp_expr *exp, LYD_FORMAT format, const struct lys_module *local_mod, const struct lysc_node *ctx_scnode,
310 enum lyxp_node_type ctx_scnode_type, struct lyxp_set *set, int options);
Radek Krejcib1646a92018-11-02 16:08:26 +0100311
Michal Vasko519fd602020-05-26 12:17:39 +0200312/* used only internally */
Michal Vasko03ff5a72019-09-11 13:49:33 +0200313#define LYXP_SCNODE_ALL 0x0E
Radek Krejcib1646a92018-11-02 16:08:26 +0100314
315/**
Radek Krejcib1646a92018-11-02 16:08:26 +0100316 * @brief Cast XPath set to another type.
317 * Indirectly context position aware.
318 *
319 * @param[in] set Set to cast.
320 * @param[in] target Target type to cast \p set into.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200321 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +0100322 */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100323LY_ERR lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200324
Radek Krejcib1646a92018-11-02 16:08:26 +0100325/**
Michal Vaskod3678892020-05-21 10:06:58 +0200326 * @brief Free dynamic content of a set.
327 *
328 * @param[in] set Set to modify.
329 */
330void lyxp_set_free_content(struct lyxp_set *set);
331
332/**
Michal Vaskoecd62de2019-11-13 12:35:11 +0100333 * @brief Insert schema node into set.
334 *
335 * @param[in] set Set to insert into.
336 * @param[in] node Node to insert.
337 * @param[in] node_type Node type of @p node.
338 * @return Index of the inserted node in set.
339 */
340int lyxp_set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type);
341
342/**
343 * @brief Check for duplicates in a schema node set.
344 *
345 * @param[in] set Set to check.
346 * @param[in] node Node to look for in @p set.
347 * @param[in] node_type Type of @p node.
348 * @param[in] skip_idx Index from @p set to skip.
349 * @return Index of the found node, -1 if not found.
350 */
351int lyxp_set_scnode_dup_node_check(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type,
352 int skip_idx);
353
354/**
355 * @brief Merge 2 schema node sets.
356 *
357 * @param[in] set1 Set to merge into.
358 * @param[in] set2 Set to merge. Its content is freed.
359 */
360void lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2);
361
362/**
Radek Krejcib1646a92018-11-02 16:08:26 +0100363 * @brief Parse an XPath expression into a structure of tokens.
364 * Logs directly.
365 *
Michal Vasko03ff5a72019-09-11 13:49:33 +0200366 * https://www.w3.org/TR/1999/REC-xpath-19991116/#exprlex
Radek Krejcib1646a92018-11-02 16:08:26 +0100367 *
368 * @param[in] ctx Context for errors.
369 * @param[in] expr XPath expression to parse. It is duplicated.
Michal Vasko004d3152020-06-11 19:59:22 +0200370 * @param[in] expr_len Length of @p expr, can be 0 if @p expr is 0-terminated.
371 * @param[in] reparse Whether to re-parse the expression to finalize full XPath parsing and fill
372 * information about expressions and their operators (fill repeat).
Radek Krejcib1646a92018-11-02 16:08:26 +0100373 * @return Filled expression structure or NULL on error.
374 */
Michal Vasko004d3152020-06-11 19:59:22 +0200375struct lyxp_expr *lyxp_expr_parse(const struct ly_ctx *ctx, const char *expr, size_t expr_len, int reparse);
376
377/**
378 * @brief Duplicate parsed XPath expression.
379 *
380 * @param[in] ctx Context with a dictionary.
381 * @param[in] exp Parsed expression.
382 * @return Duplicated structure, NULL on error.
383 */
384struct lyxp_expr *lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp);
Michal Vasko14676352020-05-29 11:35:55 +0200385
386/**
387 * @brief Look at the next token and check its kind.
388 *
389 * @param[in] ctx Context for logging, not logged if NULL.
390 * @param[in] exp Expression to use.
Michal Vasko004d3152020-06-11 19:59:22 +0200391 * @param[in] tok_idx Token index in the expression \p exp.
Michal Vasko14676352020-05-29 11:35:55 +0200392 * @param[in] want_tok Expected token.
393 * @return LY_EINCOMPLETE on EOF,
394 * @return LY_ENOT on non-matching token,
395 * @return LY_SUCCESS on success.
396 */
Michal Vasko004d3152020-06-11 19:59:22 +0200397LY_ERR lyxp_check_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t tok_idx, enum lyxp_token want_tok);
398
399/**
400 * @brief Look at the next token and skip it if it matches the expected one.
401 *
402 * @param[in] ctx Context for logging, not logged if NULL.
403 * @param[in] exp Expression to use.
404 * @param[in,out] tok_idx Token index in the expression \p exp, is updated.
405 * @param[in] want_tok Expected token.
406 * @return LY_EINCOMPLETE on EOF,
407 * @return LY_ENOT on non-matching token,
408 * @return LY_SUCCESS on success.
409 */
410LY_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 +0100411
412/**
Michal Vasko03ff5a72019-09-11 13:49:33 +0200413 * @brief Frees a parsed XPath expression. @p expr should not be used afterwards.
Radek Krejcib1646a92018-11-02 16:08:26 +0100414 *
415 * @param[in] ctx libyang context of the expression.
416 * @param[in] expr Expression to free.
417 */
Michal Vasko14676352020-05-29 11:35:55 +0200418void lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr);
Radek Krejcib1646a92018-11-02 16:08:26 +0100419
Michal Vasko14676352020-05-29 11:35:55 +0200420#endif /* LY_XPATH_H */