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_dict.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_pnode_list, ec_pnode);
29 TAILQ_ENTRY(ec_pnode) next;
30 struct ec_pnode_list children;
31 struct ec_pnode *parent;
32 const struct ec_node *node;
33 struct ec_strvec *strvec;
34 struct ec_dict *attrs;
37 static int __ec_parse_child(const struct ec_node *node,
38 struct ec_pnode *state,
39 bool is_root, const struct ec_strvec *strvec)
41 struct ec_strvec *match_strvec;
42 struct ec_pnode *child = NULL;
45 // XXX limit max number of recursions to avoid segfault
47 if (ec_node_type(node)->parse == NULL) {
53 child = ec_pnode(node);
57 ec_pnode_link_child(state, child);
61 ret = ec_node_type(node)->parse(node, child, strvec);
65 if (ret == EC_PARSE_NOMATCH) {
67 ec_pnode_unlink_child(state, child);
73 match_strvec = ec_strvec_ndup(strvec, 0, ret);
74 if (match_strvec == NULL)
77 child->strvec = match_strvec;
83 ec_pnode_unlink_child(state, child);
89 int ec_parse_child(const struct ec_node *node, struct ec_pnode *state,
90 const struct ec_strvec *strvec)
92 assert(state != NULL);
93 return __ec_parse_child(node, state, false, strvec);
96 // XXX what is returned if no match ??
97 struct ec_pnode *ec_parse_strvec(const struct ec_node *node,
98 const struct ec_strvec *strvec)
100 struct ec_pnode *parse = ec_pnode(node);
106 ret = __ec_parse_child(node, parse, true, strvec);
108 ec_pnode_free(parse);
115 struct ec_pnode *ec_parse(const struct ec_node *node, const char *str)
117 struct ec_strvec *strvec = NULL;
118 struct ec_pnode *parse = NULL;
121 strvec = ec_strvec();
125 if (ec_strvec_add(strvec, str) < 0)
128 parse = ec_parse_strvec(node, strvec);
132 ec_strvec_free(strvec);
136 ec_strvec_free(strvec);
137 ec_pnode_free(parse);
141 struct ec_pnode *ec_pnode(const struct ec_node *node)
143 struct ec_pnode *parse = NULL;
145 parse = ec_calloc(1, sizeof(*parse));
149 TAILQ_INIT(&parse->children);
152 parse->attrs = ec_dict();
153 if (parse->attrs == NULL)
160 ec_dict_free(parse->attrs);
166 static struct ec_pnode *
167 __ec_pnode_dup(const struct ec_pnode *root, const struct ec_pnode *ref,
168 struct ec_pnode **new_ref)
170 struct ec_pnode *dup = NULL;
171 struct ec_pnode *child, *dup_child;
172 struct ec_dict *attrs = NULL;
177 dup = ec_pnode(root->node);
184 attrs = ec_dict_dup(root->attrs);
187 ec_dict_free(dup->attrs);
190 if (root->strvec != NULL) {
191 dup->strvec = ec_strvec_dup(root->strvec);
192 if (dup->strvec == NULL)
196 TAILQ_FOREACH(child, &root->children, next) {
197 dup_child = __ec_pnode_dup(child, ref, new_ref);
198 if (dup_child == NULL)
200 ec_pnode_link_child(dup, dup_child);
210 struct ec_pnode *ec_pnode_dup(const struct ec_pnode *parse)
212 const struct ec_pnode *root;
213 struct ec_pnode *dup_root, *dup = NULL;
215 root = ec_pnode_get_root(parse);
216 dup_root = __ec_pnode_dup(root, parse, &dup);
217 if (dup_root == NULL)
224 void ec_pnode_free_children(struct ec_pnode *parse)
226 struct ec_pnode *child;
231 while (!TAILQ_EMPTY(&parse->children)) {
232 child = TAILQ_FIRST(&parse->children);
233 TAILQ_REMOVE(&parse->children, child, next);
234 child->parent = NULL;
235 ec_pnode_free(child);
239 void ec_pnode_free(struct ec_pnode *parse)
244 ec_assert_print(parse->parent == NULL,
245 "parent not NULL in ec_pnode_free()");
247 ec_pnode_free_children(parse);
248 ec_strvec_free(parse->strvec);
249 ec_dict_free(parse->attrs);
253 static void __ec_pnode_dump(FILE *out,
254 const struct ec_pnode *parse, size_t indent)
256 struct ec_pnode *child;
257 const struct ec_strvec *vec;
258 const char *id = "none", *typename = "none";
260 /* node can be null when parsing is incomplete */
261 if (parse->node != NULL) {
262 id = ec_node_id(parse->node);
263 typename = ec_node_type(parse->node)->name;
266 fprintf(out, "%*s" "type=%s id=%s vec=",
267 (int)indent * 4, "", typename, id);
268 vec = ec_pnode_strvec(parse);
269 ec_strvec_dump(out, vec);
271 TAILQ_FOREACH(child, &parse->children, next)
272 __ec_pnode_dump(out, child, indent + 1);
275 void ec_pnode_dump(FILE *out, const struct ec_pnode *parse)
277 fprintf(out, "------------------- parse dump:\n");
280 fprintf(out, "parse is NULL\n");
284 /* only exist if it does not match (strvec == NULL) and if it
285 * does not have children: an incomplete parse, like those
286 * generated by complete() don't match but have children that
288 if (!ec_pnode_matches(parse) && TAILQ_EMPTY(&parse->children)) {
289 fprintf(out, "no match\n");
293 __ec_pnode_dump(out, parse, 0);
296 void ec_pnode_link_child(struct ec_pnode *parse,
297 struct ec_pnode *child)
299 TAILQ_INSERT_TAIL(&parse->children, child, next);
300 child->parent = parse;
303 void ec_pnode_unlink_child(struct ec_pnode *parse,
304 struct ec_pnode *child)
306 TAILQ_REMOVE(&parse->children, child, next);
307 child->parent = NULL;
311 ec_pnode_get_first_child(const struct ec_pnode *parse)
313 return TAILQ_FIRST(&parse->children);
317 ec_pnode_get_last_child(const struct ec_pnode *parse)
319 return TAILQ_LAST(&parse->children, ec_pnode_list);
322 struct ec_pnode *ec_pnode_next(const struct ec_pnode *parse)
324 return TAILQ_NEXT(parse, next);
327 bool ec_pnode_has_child(const struct ec_pnode *parse)
329 return !TAILQ_EMPTY(&parse->children);
332 const struct ec_node *ec_pnode_get_node(const struct ec_pnode *parse)
340 void ec_pnode_del_last_child(struct ec_pnode *parse)
342 struct ec_pnode *child;
344 child = ec_pnode_get_last_child(parse);
345 ec_pnode_unlink_child(parse, child);
346 ec_pnode_free(child);
349 struct ec_pnode *__ec_pnode_get_root(struct ec_pnode *parse)
354 while (parse->parent != NULL)
355 parse = parse->parent;
360 struct ec_pnode *ec_pnode_get_parent(const struct ec_pnode *parse)
365 return parse->parent;
368 struct ec_pnode *__ec_pnode_iter_next(const struct ec_pnode *root,
369 struct ec_pnode *parse, bool iter_children)
371 struct ec_pnode *child, *parent, *next;
374 child = TAILQ_FIRST(&parse->children);
378 parent = parse->parent;
379 while (parent != NULL && parse != root) {
380 next = TAILQ_NEXT(parse, next);
384 parent = parse->parent;
390 ec_pnode_find_next(struct ec_pnode *root, struct ec_pnode *start,
391 const char *id, bool iter_children)
393 struct ec_pnode *iter;
400 start = EC_PNODE_ITER_NEXT(root, start, iter_children);
402 for (iter = start; iter != NULL;
403 iter = EC_PNODE_ITER_NEXT(root, iter, 1)) {
404 if (iter->node != NULL &&
405 !strcmp(ec_node_id(iter->node), id))
412 struct ec_pnode *ec_pnode_find(struct ec_pnode *parse,
415 return ec_pnode_find_next(parse, NULL, id, 1);
419 ec_pnode_get_attrs(struct ec_pnode *parse)
427 const struct ec_strvec *ec_pnode_strvec(const struct ec_pnode *parse)
429 if (parse == NULL || parse->strvec == NULL)
432 return parse->strvec;
435 /* number of strings in the parse vector */
436 size_t ec_pnode_len(const struct ec_pnode *parse)
438 if (parse == NULL || parse->strvec == NULL)
441 return ec_strvec_len(parse->strvec);
444 size_t ec_pnode_matches(const struct ec_pnode *parse)
449 if (parse->strvec == NULL)
455 /* LCOV_EXCL_START */
456 static int ec_pnode_testcase(void)
458 struct ec_node *node = NULL;
459 struct ec_pnode *p = NULL, *p2 = NULL;
460 const struct ec_pnode *pc;
467 node = ec_node_sh_lex(EC_NO_ID,
468 EC_NODE_SEQ(EC_NO_ID,
469 ec_node_str("id_x", "x"),
470 ec_node_str("id_y", "y")));
474 p = ec_parse(node, "xcdscds");
475 testres |= EC_TEST_CHECK(
476 p != NULL && !ec_pnode_matches(p),
477 "parse should not match\n");
479 f = open_memstream(&buf, &buflen);
486 testres |= EC_TEST_CHECK(
487 strstr(buf, "no match"), "bad dump\n");
492 p = ec_parse(node, "x y");
493 testres |= EC_TEST_CHECK(
494 p != NULL && ec_pnode_matches(p),
495 "parse should match\n");
496 testres |= EC_TEST_CHECK(
497 ec_pnode_len(p) == 1, "bad parse len\n");
499 ret = ec_dict_set(ec_pnode_get_attrs(p), "key", "val", NULL);
500 testres |= EC_TEST_CHECK(ret == 0,
501 "cannot set parse attribute\n");
503 p2 = ec_pnode_dup(p);
504 testres |= EC_TEST_CHECK(
505 p2 != NULL && ec_pnode_matches(p2),
506 "parse should match\n");
510 pc = ec_pnode_find(p, "id_x");
511 testres |= EC_TEST_CHECK(pc != NULL, "cannot find id_x");
512 testres |= EC_TEST_CHECK(pc != NULL &&
513 ec_pnode_get_parent(pc) != NULL &&
514 ec_pnode_get_parent(ec_pnode_get_parent(pc)) == p,
517 pc = ec_pnode_find(p, "id_y");
518 testres |= EC_TEST_CHECK(pc != NULL, "cannot find id_y");
519 pc = ec_pnode_find(p, "id_dezdezdez");
520 testres |= EC_TEST_CHECK(pc == NULL, "should not find bad id");
523 f = open_memstream(&buf, &buflen);
530 testres |= EC_TEST_CHECK(
531 strstr(buf, "type=sh_lex id=no-id") &&
532 strstr(buf, "type=seq id=no-id") &&
533 strstr(buf, "type=str id=id_x") &&
534 strstr(buf, "type=str id=id_x"),
555 static struct ec_test ec_pnode_test = {
557 .test = ec_pnode_testcase,
560 EC_TEST_REGISTER(ec_pnode_test);