1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
6 * @defgroup parse Parse
9 * @brief Create parse tree from string input and grammar tree
13 * The parse operation is to check if an input (a string or vector of
14 * strings) matches the node tree. On success, the result is stored in a
15 * tree that describes which part of the input matches which node.
23 #include <sys/queue.h>
24 #include <sys/types.h>
33 * Create an empty parse tree.
36 * The empty parse tree.
38 struct ec_parse *ec_parse(const struct ec_node *node);
45 void ec_parse_free(struct ec_parse *parse);
52 void ec_parse_free_children(struct ec_parse *parse);
59 struct ec_parse *ec_parse_dup(const struct ec_parse *parse);
67 const struct ec_strvec *ec_parse_strvec(const struct ec_parse *parse);
69 /* a NULL return value is an error, with errno set
70 ENOTSUP: no ->parse() operation
77 struct ec_parse *ec_node_parse(const struct ec_node *node, const char *str);
84 struct ec_parse *ec_node_parse_strvec(const struct ec_node *node,
85 const struct ec_strvec *strvec);
92 #define EC_PARSE_NOMATCH INT_MAX
94 /* internal: used by nodes
96 * state is the current parse tree, which is built piece by piece while
97 * parsing the node tree: ec_node_parse_child() creates a new child in
98 * this state parse tree, and calls the parse() method for the child
99 * node, with state pointing to this new child. If it does not match,
100 * the child is removed in the state, else it is kept, with its
101 * possible descendants.
104 * the number of matched strings in strvec on success
105 * EC_PARSE_NOMATCH (positive) if it does not match
106 * -1 on error, and errno is set
108 int ec_node_parse_child(const struct ec_node *node,
109 struct ec_parse *state,
110 const struct ec_strvec *strvec);
117 void ec_parse_link_child(struct ec_parse *parse,
118 struct ec_parse *child);
124 void ec_parse_unlink_child(struct ec_parse *parse,
125 struct ec_parse *child);
128 #define ec_parse_get_root(parse) ({ \
129 const struct ec_parse *p_ = parse; /* check type */ \
130 struct ec_parse *parse_ = (struct ec_parse *)parse; \
131 typeof(parse) res_; \
133 res_ = __ec_parse_get_root(parse_); \
142 struct ec_parse *__ec_parse_get_root(struct ec_parse *parse);
149 struct ec_parse *ec_parse_get_parent(const struct ec_parse *parse);
152 * Get the first child of a tree.
155 struct ec_parse *ec_parse_get_first_child(const struct ec_parse *parse);
162 struct ec_parse *ec_parse_get_last_child(const struct ec_parse *parse);
169 struct ec_parse *ec_parse_next(const struct ec_parse *parse);
176 #define EC_PARSE_FOREACH_CHILD(child, parse) \
177 for (child = ec_parse_get_first_child(parse); \
179 child = ec_parse_next(child)) \
186 bool ec_parse_has_child(const struct ec_parse *parse);
193 const struct ec_node *ec_parse_get_node(const struct ec_parse *parse);
200 void ec_parse_del_last_child(struct ec_parse *parse);
207 struct ec_dict *ec_parse_get_attrs(struct ec_parse *parse);
214 void ec_parse_dump(FILE *out, const struct ec_parse *parse);
221 struct ec_parse *ec_parse_find(struct ec_parse *parse,
229 struct ec_parse *ec_parse_find_next(struct ec_parse *root,
230 struct ec_parse *start,
231 const char *id, bool iter_children);
234 * Iterate among parse tree
237 * for (iter = state; iter != NULL; iter = EC_PARSE_ITER_NEXT(state, iter, 1))
239 struct ec_parse *__ec_parse_iter_next(const struct ec_parse *root,
240 struct ec_parse *parse, bool iter_children);
242 /* keep the const if any */
243 #define EC_PARSE_ITER_NEXT(root, parse, iter_children) ({ \
244 const struct ec_parse *p_ = parse; /* check type */ \
245 struct ec_parse *parse_ = (struct ec_parse *)parse; \
246 typeof(parse) res_; \
248 res_ = __ec_parse_iter_next(root, parse_, iter_children); \
257 size_t ec_parse_len(const struct ec_parse *parse);
264 size_t ec_parse_matches(const struct ec_parse *parse);