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>
26 * Create an empty parse tree.
29 * The empty parse tree.
31 struct ec_parse *ec_parse(const struct ec_node *node);
38 void ec_parse_free(struct ec_parse *parse);
45 void ec_parse_free_children(struct ec_parse *parse);
52 struct ec_parse *ec_parse_dup(const struct ec_parse *parse);
59 const struct ec_strvec *ec_parse_strvec(const struct ec_parse *parse);
61 /* a NULL return value is an error, with errno set
62 ENOTSUP: no ->parse() operation
69 struct ec_parse *ec_node_parse(const struct ec_node *node, const char *str);
76 struct ec_parse *ec_node_parse_strvec(const struct ec_node *node,
77 const struct ec_strvec *strvec);
84 #define EC_PARSE_NOMATCH INT_MAX
86 /* internal: used by nodes
88 * state is the current parse tree, which is built piece by piece while
89 * parsing the node tree: ec_node_parse_child() creates a new child in
90 * this state parse tree, and calls the parse() method for the child
91 * node, with state pointing to this new child. If it does not match,
92 * the child is removed in the state, else it is kept, with its
93 * possible descendants.
96 * the number of matched strings in strvec on success
97 * EC_PARSE_NOMATCH (positive) if it does not match
98 * -1 on error, and errno is set
100 int ec_node_parse_child(const struct ec_node *node,
101 struct ec_parse *state,
102 const struct ec_strvec *strvec);
109 void ec_parse_link_child(struct ec_parse *parse,
110 struct ec_parse *child);
116 void ec_parse_unlink_child(struct ec_parse *parse,
117 struct ec_parse *child);
120 #define ec_parse_get_root(parse) ({ \
121 const struct ec_parse *p_ = parse; /* check type */ \
122 struct ec_parse *parse_ = (struct ec_parse *)parse; \
123 typeof(parse) res_; \
125 res_ = __ec_parse_get_root(parse_); \
134 struct ec_parse *__ec_parse_get_root(struct ec_parse *parse);
141 struct ec_parse *ec_parse_get_parent(const struct ec_parse *parse);
144 * Get the first child of a tree.
147 struct ec_parse *ec_parse_get_first_child(const struct ec_parse *parse);
154 struct ec_parse *ec_parse_get_last_child(const struct ec_parse *parse);
161 struct ec_parse *ec_parse_get_next(const struct ec_parse *parse);
168 #define EC_PARSE_FOREACH_CHILD(child, parse) \
169 for (child = ec_parse_get_first_child(parse); \
171 child = ec_parse_get_next(child)) \
178 bool ec_parse_has_child(const struct ec_parse *parse);
185 const struct ec_node *ec_parse_get_node(const struct ec_parse *parse);
192 void ec_parse_del_last_child(struct ec_parse *parse);
199 struct ec_keyval *ec_parse_get_attrs(struct ec_parse *parse);
206 void ec_parse_dump(FILE *out, const struct ec_parse *parse);
213 struct ec_parse *ec_parse_find_first(struct ec_parse *parse,
217 * Iterate among parse tree
220 * for (iter = state; iter != NULL; iter = ec_parse_iter_next(iter))
222 struct ec_parse *ec_parse_iter_next(struct ec_parse *parse);
229 size_t ec_parse_len(const struct ec_parse *parse);
236 size_t ec_parse_matches(const struct ec_parse *parse);