2 * Copyright (c) 2016, Olivier MATZ <zer0@droids-corp.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the University of California, Berkeley nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <ecoli_malloc.h>
35 #include <ecoli_strvec.h>
36 #include <ecoli_keyval.h>
37 #include <ecoli_log.h>
38 #include <ecoli_node.h>
39 #include <ecoli_parsed.h>
41 int ec_node_parse_child(struct ec_node *node, struct ec_parsed *state,
42 const struct ec_strvec *strvec)
44 struct ec_strvec *match_strvec;
45 struct ec_parsed *child;
48 assert(state != NULL);
50 /* build the node if required */
51 if (node->type->build != NULL) {
52 if ((node->flags & EC_NODE_F_BUILT) == 0) {
53 ret = node->type->build(node);
58 node->flags |= EC_NODE_F_BUILT;
60 if (node->type->parse == NULL)
67 ec_parsed_add_child(state, child);
68 ret = node->type->parse(node, child, strvec);
69 if (ret == EC_PARSED_NOMATCH) {
70 ec_parsed_del_child(state, child);
71 assert(TAILQ_EMPTY(&child->children));
72 ec_parsed_free(child);
78 match_strvec = ec_strvec_ndup(strvec, 0, ret);
79 if (match_strvec == NULL)
83 child->strvec = match_strvec;
88 struct ec_parsed *ec_node_parse_strvec(struct ec_node *node,
89 const struct ec_strvec *strvec)
91 struct ec_parsed *parsed = ec_parsed();
92 struct ec_parsed *child_parsed;
98 ret = ec_node_parse_child(node, parsed, strvec);
99 if (ret < 0 && ret != EC_PARSED_NOMATCH) {
100 ec_parsed_free(parsed);
102 } else if (ret != EC_PARSED_NOMATCH) {
103 /* remove dummy root node */
104 child_parsed = ec_parsed_get_last_child(parsed);
105 ec_parsed_del_child(parsed, child_parsed);
106 ec_parsed_free(parsed);
107 parsed = child_parsed;
113 struct ec_parsed *ec_node_parse(struct ec_node *node, const char *str)
115 struct ec_strvec *strvec = NULL;
116 struct ec_parsed *parsed = NULL;
119 strvec = ec_strvec();
123 if (ec_strvec_add(strvec, str) < 0)
126 parsed = ec_node_parse_strvec(node, strvec);
130 ec_strvec_free(strvec);
134 ec_strvec_free(strvec);
135 ec_parsed_free(parsed);
139 struct ec_parsed *ec_parsed(void)
141 struct ec_parsed *parsed = NULL;
143 parsed = ec_calloc(1, sizeof(*parsed));
147 TAILQ_INIT(&parsed->children);
155 void ec_parsed_free_children(struct ec_parsed *parsed)
157 struct ec_parsed *child;
162 while (!TAILQ_EMPTY(&parsed->children)) {
163 child = TAILQ_FIRST(&parsed->children);
164 TAILQ_REMOVE(&parsed->children, child, next);
165 ec_parsed_free(child);
169 void ec_parsed_free(struct ec_parsed *parsed)
174 ec_parsed_free_children(parsed);
175 ec_strvec_free(parsed->strvec);
179 static void __ec_parsed_dump(FILE *out,
180 const struct ec_parsed *parsed, size_t indent)
182 struct ec_parsed *child;
183 const struct ec_strvec *vec;
185 const char *id = "none", *typename = "none";
187 if (parsed->node != NULL) {
188 if (parsed->node->id != NULL)
189 id = parsed->node->id;
190 typename = parsed->node->type->name;
194 for (i = 0; i < indent; i++) {
201 fprintf(out, "node_type=%s id=%s vec=[", typename, id);
202 vec = ec_parsed_strvec(parsed);
203 for (i = 0; i < ec_strvec_len(vec); i++)
204 fprintf(out, "%s<%s>",
206 ec_strvec_val(vec, i));
208 if (!strcmp(typename, "int") || !strcmp(typename, "str"))
209 fprintf(out, "] <<<<<\n");
213 TAILQ_FOREACH(child, &parsed->children, next)
214 __ec_parsed_dump(out, child, indent + 2);
217 void ec_parsed_dump(FILE *out, const struct ec_parsed *parsed)
219 fprintf(out, "------------------- parsed dump:\n"); //XXX
221 if (parsed == NULL) {
222 fprintf(out, "parsed is NULL, error in parse\n");
225 if (!ec_parsed_matches(parsed)) {
226 fprintf(out, "no match\n");
230 __ec_parsed_dump(out, parsed, 0);
233 void ec_parsed_add_child(struct ec_parsed *parsed,
234 struct ec_parsed *child)
236 TAILQ_INSERT_TAIL(&parsed->children, child, next);
237 child->parent = parsed;
240 void ec_parsed_del_child(struct ec_parsed *parsed,
241 struct ec_parsed *child)
243 TAILQ_REMOVE(&parsed->children, child, next);
244 child->parent = NULL;
248 ec_parsed_get_last_child(struct ec_parsed *parsed)
250 return TAILQ_LAST(&parsed->children, ec_parsed_list);
253 struct ec_parsed *ec_parsed_get_root(struct ec_parsed *parsed)
258 while (parsed->parent != NULL)
259 parsed = parsed->parent;
264 struct ec_parsed *ec_parsed_find_first(struct ec_parsed *parsed,
267 struct ec_parsed *child, *ret;
272 if (parsed->node != NULL &&
273 parsed->node->id != NULL &&
274 !strcmp(parsed->node->id, id))
277 TAILQ_FOREACH(child, &parsed->children, next) {
278 ret = ec_parsed_find_first(child, id);
286 const struct ec_strvec *ec_parsed_strvec(const struct ec_parsed *parsed)
288 if (parsed == NULL || parsed->strvec == NULL)
291 return parsed->strvec;
294 /* number of parsed strings in the vector */
295 size_t ec_parsed_len(const struct ec_parsed *parsed)
297 if (parsed == NULL || parsed->strvec == NULL)
300 return ec_strvec_len(parsed->strvec);
303 size_t ec_parsed_matches(const struct ec_parsed *parsed)
308 if (parsed->node == NULL && TAILQ_EMPTY(&parsed->children)) // XXX both needed?