1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
8 * The parse operation is to check if an input (a string or vector of
9 * strings) matches the node tree. On success, the result is stored in a
10 * tree that describes which part of the input matches which node.
16 #include <sys/queue.h>
17 #include <sys/types.h>
25 * Create an empty parse tree.
28 * The empty parse tree.
30 struct ec_parsed *ec_parsed(const struct ec_node *node);
37 void ec_parsed_free(struct ec_parsed *parsed);
44 void ec_parsed_free_children(struct ec_parsed *parsed);
51 struct ec_parsed *ec_parsed_dup(const struct ec_parsed *parsed);
58 const struct ec_strvec *ec_parsed_strvec(const struct ec_parsed *parsed);
60 /* a NULL return value is an error, with errno set
61 ENOTSUP: no ->parse() operation
68 struct ec_parsed *ec_node_parse(const struct ec_node *node, const char *str);
75 struct ec_parsed *ec_node_parse_strvec(const struct ec_node *node,
76 const struct ec_strvec *strvec);
83 #define EC_PARSED_NOMATCH INT_MAX
85 /* internal: used by nodes
87 * state is the current parse tree, which is built piece by piece while
88 * parsing the node tree: ec_node_parse_child() creates a new child in
89 * this state parse tree, and calls the parse() method for the child
90 * node, with state pointing to this new child. If it does not match,
91 * the child is removed in the state, else it is kept, with its
92 * possible descendants.
95 * EC_PARSED_NOMATCH (positive) if it does not match
96 * any other negative value (-errno) for other errors
97 * the number of matched strings in strvec
99 int ec_node_parse_child(const struct ec_node *node,
100 struct ec_parsed *state,
101 const struct ec_strvec *strvec);
108 void ec_parsed_link_child(struct ec_parsed *parsed,
109 struct ec_parsed *child);
115 void ec_parsed_unlink_child(struct ec_parsed *parsed,
116 struct ec_parsed *child);
119 #define ec_parsed_get_root(parsed) ({ \
120 const struct ec_parsed *p_ = parsed; /* check type */ \
121 struct ec_parsed *parsed_ = (struct ec_parsed *)parsed; \
122 typeof(parsed) res_; \
124 res_ = __ec_parsed_get_root(parsed_); \
133 struct ec_parsed *__ec_parsed_get_root(struct ec_parsed *parsed);
140 struct ec_parsed *ec_parsed_get_parent(const struct ec_parsed *parsed);
143 * Get the first child of a tree.
146 struct ec_parsed *ec_parsed_get_first_child(const struct ec_parsed *parsed);
153 struct ec_parsed *ec_parsed_get_last_child(const struct ec_parsed *parsed);
160 struct ec_parsed *ec_parsed_get_next(const struct ec_parsed *parsed);
167 #define EC_PARSED_FOREACH_CHILD(child, parsed) \
168 for (child = ec_parsed_get_first_child(parsed); \
170 child = ec_parsed_get_next(child)) \
177 bool ec_parsed_has_child(const struct ec_parsed *parsed);
184 const struct ec_node *ec_parsed_get_node(const struct ec_parsed *parsed);
191 void ec_parsed_del_last_child(struct ec_parsed *parsed);
198 struct ec_keyval *ec_parsed_get_attrs(struct ec_parsed *parsed);
205 void ec_parsed_dump(FILE *out, const struct ec_parsed *parsed);
212 struct ec_parsed *ec_parsed_find_first(struct ec_parsed *parsed,
216 * Iterate among parsed tree
219 * for (iter = state; iter != NULL; iter = ec_parsed_iter_next(iter))
221 struct ec_parsed *ec_parsed_iter_next(struct ec_parsed *parsed);
228 size_t ec_parsed_len(const struct ec_parsed *parsed);
235 size_t ec_parsed_matches(const struct ec_parsed *parsed);