1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
12 #include <ecoli_assert.h>
13 #include <ecoli_malloc.h>
14 #include <ecoli_strvec.h>
15 #include <ecoli_keyval.h>
16 #include <ecoli_log.h>
17 #include <ecoli_test.h>
18 #include <ecoli_node.h>
19 #include <ecoli_node_sh_lex.h>
20 #include <ecoli_node_str.h>
21 #include <ecoli_node_seq.h>
22 #include <ecoli_parse.h>
24 EC_LOG_TYPE_REGISTER(parse);
26 TAILQ_HEAD(ec_parse_list, ec_parse);
29 TAILQ_ENTRY(ec_parse) next;
30 struct ec_parse_list children;
31 struct ec_parse *parent;
32 const struct ec_node *node;
33 struct ec_strvec *strvec;
34 struct ec_keyval *attrs;
37 static int __ec_node_parse_child(const struct ec_node *node,
38 struct ec_parse *state,
39 bool is_root, const struct ec_strvec *strvec)
41 struct ec_strvec *match_strvec;
42 struct ec_parse *child = NULL;
45 if (ec_node_type(node)->parse == NULL) {
51 child = ec_parse(node);
55 ec_parse_link_child(state, child);
59 ret = ec_node_type(node)->parse(node, child, strvec);
63 if (ret == EC_PARSE_NOMATCH) {
65 ec_parse_unlink_child(state, child);
71 match_strvec = ec_strvec_ndup(strvec, 0, ret);
72 if (match_strvec == NULL)
75 child->strvec = match_strvec;
81 ec_parse_unlink_child(state, child);
87 int ec_node_parse_child(const struct ec_node *node, struct ec_parse *state,
88 const struct ec_strvec *strvec)
90 assert(state != NULL);
91 return __ec_node_parse_child(node, state, false, strvec);
94 // XXX what is returned if no match ??
95 struct ec_parse *ec_node_parse_strvec(const struct ec_node *node,
96 const struct ec_strvec *strvec)
98 struct ec_parse *parse = ec_parse(node);
104 ret = __ec_node_parse_child(node, parse, true, strvec);
106 ec_parse_free(parse);
113 struct ec_parse *ec_node_parse(const struct ec_node *node, const char *str)
115 struct ec_strvec *strvec = NULL;
116 struct ec_parse *parse = NULL;
119 strvec = ec_strvec();
123 if (ec_strvec_add(strvec, str) < 0)
126 parse = ec_node_parse_strvec(node, strvec);
130 ec_strvec_free(strvec);
134 ec_strvec_free(strvec);
135 ec_parse_free(parse);
139 struct ec_parse *ec_parse(const struct ec_node *node)
141 struct ec_parse *parse = NULL;
143 parse = ec_calloc(1, sizeof(*parse));
147 TAILQ_INIT(&parse->children);
150 parse->attrs = ec_keyval();
151 if (parse->attrs == NULL)
158 ec_keyval_free(parse->attrs);
164 static struct ec_parse *
165 __ec_parse_dup(const struct ec_parse *root, const struct ec_parse *ref,
166 struct ec_parse **new_ref)
168 struct ec_parse *dup = NULL;
169 struct ec_parse *child, *dup_child;
170 struct ec_keyval *attrs = NULL;
175 dup = ec_parse(root->node);
182 attrs = ec_keyval_dup(root->attrs);
185 ec_keyval_free(dup->attrs);
188 if (root->strvec != NULL) {
189 dup->strvec = ec_strvec_dup(root->strvec);
190 if (dup->strvec == NULL)
194 TAILQ_FOREACH(child, &root->children, next) {
195 dup_child = __ec_parse_dup(child, ref, new_ref);
196 if (dup_child == NULL)
198 ec_parse_link_child(dup, dup_child);
208 struct ec_parse *ec_parse_dup(const struct ec_parse *parse)
210 const struct ec_parse *root;
211 struct ec_parse *dup_root, *dup = NULL;
213 root = ec_parse_get_root(parse);
214 dup_root = __ec_parse_dup(root, parse, &dup);
215 if (dup_root == NULL)
222 void ec_parse_free_children(struct ec_parse *parse)
224 struct ec_parse *child;
229 while (!TAILQ_EMPTY(&parse->children)) {
230 child = TAILQ_FIRST(&parse->children);
231 TAILQ_REMOVE(&parse->children, child, next);
232 child->parent = NULL;
233 ec_parse_free(child);
237 void ec_parse_free(struct ec_parse *parse)
242 ec_assert_print(parse->parent == NULL,
243 "parent not NULL in ec_parse_free()");
245 ec_parse_free_children(parse);
246 ec_strvec_free(parse->strvec);
247 ec_keyval_free(parse->attrs);
251 static void __ec_parse_dump(FILE *out,
252 const struct ec_parse *parse, size_t indent)
254 struct ec_parse *child;
255 const struct ec_strvec *vec;
256 const char *id = "none", *typename = "none";
258 /* node can be null when parsing is incomplete */
259 if (parse->node != NULL) {
260 id = parse->node->id;
261 typename = ec_node_type(parse->node)->name;
264 fprintf(out, "%*s" "type=%s id=%s vec=",
265 (int)indent * 4, "", typename, id);
266 vec = ec_parse_strvec(parse);
267 ec_strvec_dump(out, vec);
269 TAILQ_FOREACH(child, &parse->children, next)
270 __ec_parse_dump(out, child, indent + 1);
273 void ec_parse_dump(FILE *out, const struct ec_parse *parse)
275 fprintf(out, "------------------- parse dump:\n");
278 fprintf(out, "parse is NULL\n");
282 /* only exist if it does not match (strvec == NULL) and if it
283 * does not have children: an incomplete parse, like those
284 * generated by complete() don't match but have children that
286 if (!ec_parse_matches(parse) && TAILQ_EMPTY(&parse->children)) {
287 fprintf(out, "no match\n");
291 __ec_parse_dump(out, parse, 0);
294 void ec_parse_link_child(struct ec_parse *parse,
295 struct ec_parse *child)
297 TAILQ_INSERT_TAIL(&parse->children, child, next);
298 child->parent = parse;
301 void ec_parse_unlink_child(struct ec_parse *parse,
302 struct ec_parse *child)
304 TAILQ_REMOVE(&parse->children, child, next);
305 child->parent = NULL;
309 ec_parse_get_first_child(const struct ec_parse *parse)
311 return TAILQ_FIRST(&parse->children);
315 ec_parse_get_last_child(const struct ec_parse *parse)
317 return TAILQ_LAST(&parse->children, ec_parse_list);
320 struct ec_parse *ec_parse_get_next(const struct ec_parse *parse)
322 return TAILQ_NEXT(parse, next);
325 bool ec_parse_has_child(const struct ec_parse *parse)
327 return !TAILQ_EMPTY(&parse->children);
330 const struct ec_node *ec_parse_get_node(const struct ec_parse *parse)
338 void ec_parse_del_last_child(struct ec_parse *parse)
340 struct ec_parse *child;
342 child = ec_parse_get_last_child(parse);
343 ec_parse_unlink_child(parse, child);
344 ec_parse_free(child);
347 struct ec_parse *__ec_parse_get_root(struct ec_parse *parse)
352 while (parse->parent != NULL)
353 parse = parse->parent;
358 struct ec_parse *ec_parse_get_parent(const struct ec_parse *parse)
363 return parse->parent;
366 struct ec_parse *ec_parse_iter_next(struct ec_parse *parse)
368 struct ec_parse *child, *parent, *next;
370 child = TAILQ_FIRST(&parse->children);
373 parent = parse->parent;
374 while (parent != NULL) {
375 next = TAILQ_NEXT(parse, next);
379 parent = parse->parent;
384 struct ec_parse *ec_parse_find_first(struct ec_parse *parse,
387 struct ec_parse *iter;
392 for (iter = parse; iter != NULL; iter = ec_parse_iter_next(iter)) {
393 if (iter->node != NULL &&
394 iter->node->id != NULL &&
395 !strcmp(iter->node->id, id))
403 ec_parse_get_attrs(struct ec_parse *parse)
411 const struct ec_strvec *ec_parse_strvec(const struct ec_parse *parse)
413 if (parse == NULL || parse->strvec == NULL)
416 return parse->strvec;
419 /* number of strings in the parse vector */
420 size_t ec_parse_len(const struct ec_parse *parse)
422 if (parse == NULL || parse->strvec == NULL)
425 return ec_strvec_len(parse->strvec);
428 size_t ec_parse_matches(const struct ec_parse *parse)
433 if (parse->strvec == NULL)
439 /* LCOV_EXCL_START */
440 static int ec_parse_testcase(void)
442 struct ec_node *node = NULL;
443 struct ec_parse *p = NULL, *p2 = NULL;
444 const struct ec_parse *pc;
451 node = ec_node_sh_lex(EC_NO_ID,
452 EC_NODE_SEQ(EC_NO_ID,
453 ec_node_str("id_x", "x"),
454 ec_node_str("id_y", "y")));
458 p = ec_node_parse(node, "xcdscds");
459 testres |= EC_TEST_CHECK(
460 p != NULL && !ec_parse_matches(p),
461 "parse should not match\n");
463 f = open_memstream(&buf, &buflen);
470 testres |= EC_TEST_CHECK(
471 strstr(buf, "no match"), "bad dump\n");
476 p = ec_node_parse(node, "x y");
477 testres |= EC_TEST_CHECK(
478 p != NULL && ec_parse_matches(p),
479 "parse should match\n");
480 testres |= EC_TEST_CHECK(
481 ec_parse_len(p) == 1, "bad parse len\n");
483 ret = ec_keyval_set(ec_parse_get_attrs(p), "key", "val", NULL);
484 testres |= EC_TEST_CHECK(ret == 0,
485 "cannot set parse attribute\n");
487 p2 = ec_parse_dup(p);
488 testres |= EC_TEST_CHECK(
489 p2 != NULL && ec_parse_matches(p2),
490 "parse should match\n");
494 pc = ec_parse_find_first(p, "id_x");
495 testres |= EC_TEST_CHECK(pc != NULL, "cannot find id_x");
496 testres |= EC_TEST_CHECK(pc != NULL &&
497 ec_parse_get_parent(pc) != NULL &&
498 ec_parse_get_parent(ec_parse_get_parent(pc)) == p,
501 pc = ec_parse_find_first(p, "id_y");
502 testres |= EC_TEST_CHECK(pc != NULL, "cannot find id_y");
503 pc = ec_parse_find_first(p, "id_dezdezdez");
504 testres |= EC_TEST_CHECK(pc == NULL, "should not find bad id");
507 f = open_memstream(&buf, &buflen);
514 testres |= EC_TEST_CHECK(
515 strstr(buf, "type=sh_lex id=no-id") &&
516 strstr(buf, "type=seq id=no-id") &&
517 strstr(buf, "type=str id=id_x") &&
518 strstr(buf, "type=str id=id_x"),
539 static struct ec_test ec_parse_test = {
541 .test = ec_parse_testcase,
544 EC_TEST_REGISTER(ec_parse_test);