re_lex: attach attribute to strings in vec
authorOlivier Matz <zer0@droids-corp.org>
Fri, 14 Dec 2018 21:46:37 +0000 (22:46 +0100)
committerOlivier Matz <zer0@droids-corp.org>
Fri, 14 Dec 2018 21:46:37 +0000 (22:46 +0100)
libecoli/ecoli_node_cmd.c
libecoli/ecoli_node_expr_test.c
libecoli/ecoli_node_re_lex.c
libecoli/ecoli_node_re_lex.h

index c61b759..3b56084 100644 (file)
@@ -329,19 +329,19 @@ ec_node_cmd_build_parser(struct ec_node *expr)
        if (lex == NULL)
                goto fail;
 
        if (lex == NULL)
                goto fail;
 
-       ret = ec_node_re_lex_add(lex, "[a-zA-Z0-9]+", 1);
+       ret = ec_node_re_lex_add(lex, "[a-zA-Z0-9]+", 1, NULL);
        if (ret < 0)
                goto fail;
        if (ret < 0)
                goto fail;
-       ret = ec_node_re_lex_add(lex, "[*|,()]", 1);
+       ret = ec_node_re_lex_add(lex, "[*|,()]", 1, NULL);
        if (ret < 0)
                goto fail;
        if (ret < 0)
                goto fail;
-       ret = ec_node_re_lex_add(lex, "\\[", 1);
+       ret = ec_node_re_lex_add(lex, "\\[", 1, NULL);
        if (ret < 0)
                goto fail;
        if (ret < 0)
                goto fail;
-       ret = ec_node_re_lex_add(lex, "\\]", 1);
+       ret = ec_node_re_lex_add(lex, "\\]", 1, NULL);
        if (ret < 0)
                goto fail;
        if (ret < 0)
                goto fail;
-       ret = ec_node_re_lex_add(lex, "[         ]+", 0);
+       ret = ec_node_re_lex_add(lex, "[         ]+", 0, NULL);
        if (ret < 0)
                goto fail;
 
        if (ret < 0)
                goto fail;
 
index 93e33a4..e3a0c79 100644 (file)
@@ -251,9 +251,9 @@ static int ec_node_expr_testcase(void)
        if (lex_node == NULL)
                goto fail;
 
        if (lex_node == NULL)
                goto fail;
 
-       testres |= ec_node_re_lex_add(lex_node, "[0-9]+", 1); /* vars */
-       testres |= ec_node_re_lex_add(lex_node, "[+*!^()]", 1); /* operators */
-       testres |= ec_node_re_lex_add(lex_node, "[      ]+", 0); /* spaces */
+       testres |= ec_node_re_lex_add(lex_node, "[0-9]+", 1, NULL); /* vars */
+       testres |= ec_node_re_lex_add(lex_node, "[+*!^()]", 1, NULL); /* operators */
+       testres |= ec_node_re_lex_add(lex_node, "[      ]+", 0, NULL); /* spaces */
 
        /* valid expressions */
        testres |= EC_TEST_CHECK_PARSE(lex_node, 1, "!1");
 
        /* valid expressions */
        testres |= EC_TEST_CHECK_PARSE(lex_node, 1, "!1");
index e5754ba..4ebb9fd 100644 (file)
@@ -13,6 +13,7 @@
 #include <ecoli_log.h>
 #include <ecoli_test.h>
 #include <ecoli_strvec.h>
 #include <ecoli_log.h>
 #include <ecoli_test.h>
 #include <ecoli_strvec.h>
+#include <ecoli_keyval.h>
 #include <ecoli_node.h>
 #include <ecoli_complete.h>
 #include <ecoli_parse.h>
 #include <ecoli_node.h>
 #include <ecoli_complete.h>
 #include <ecoli_parse.h>
@@ -27,6 +28,7 @@ EC_LOG_TYPE_REGISTER(node_re_lex);
 
 struct regexp_pattern {
        char *pattern;
 
 struct regexp_pattern {
        char *pattern;
+       char *attr_name;
        regex_t r;
        bool keep;
 };
        regex_t r;
        bool keep;
 };
@@ -42,6 +44,7 @@ static struct ec_strvec *
 tokenize(struct regexp_pattern *table, size_t table_len, const char *str)
 {
        struct ec_strvec *strvec = NULL;
 tokenize(struct regexp_pattern *table, size_t table_len, const char *str)
 {
        struct ec_strvec *strvec = NULL;
+       struct ec_keyval *attrs = NULL;
        char *dup = NULL;
        char c;
        size_t len, off = 0;
        char *dup = NULL;
        char c;
        size_t len, off = 0;
@@ -77,6 +80,22 @@ tokenize(struct regexp_pattern *table, size_t table_len, const char *str)
                        if (ec_strvec_add(strvec, &dup[off]) < 0)
                                goto fail;
 
                        if (ec_strvec_add(strvec, &dup[off]) < 0)
                                goto fail;
 
+                       if (table[i].attr_name != NULL) {
+                               attrs = ec_keyval();
+                               if (attrs == NULL)
+                                       goto fail;
+                               if (ec_keyval_set(attrs, table[i].attr_name,
+                                               NULL, NULL) < 0)
+                                       goto fail;
+                               if (ec_strvec_set_attrs(strvec,
+                                               ec_strvec_len(strvec) - 1,
+                                               attrs) < 0) {
+                                       attrs = NULL;
+                                       goto fail;
+                               }
+                               attrs = NULL;
+                       }
+
                        dup[pos.rm_eo + off] = c;
                        break;
                }
                        dup[pos.rm_eo + off] = c;
                        break;
                }
@@ -152,6 +171,7 @@ static void ec_node_re_lex_free_priv(struct ec_node *gen_node)
        ec_node_free(node->child);
        for (i = 0; i < node->len; i++) {
                ec_free(node->table[i].pattern);
        ec_node_free(node->child);
        for (i = 0; i < node->len; i++) {
                ec_free(node->table[i].pattern);
+               ec_free(node->table[i].attr_name);
                regfree(&node->table[i].r);
        }
 
                regfree(&node->table[i].r);
        }
 
@@ -194,6 +214,11 @@ static const struct ec_config_schema ec_node_re_lex_dict[] = {
                "the regular expression.",
                .type = EC_CONFIG_TYPE_BOOL,
        },
                "the regular expression.",
                .type = EC_CONFIG_TYPE_BOOL,
        },
+       {
+               .key = "attr",
+               .desc = "The optional attribute name to attach.",
+               .type = EC_CONFIG_TYPE_STRING,
+       },
        {
                .type = EC_CONFIG_TYPE_NONE,
        },
        {
                .type = EC_CONFIG_TYPE_NONE,
        },
@@ -232,8 +257,8 @@ static int ec_node_re_lex_set_config(struct ec_node *gen_node,
 {
        struct ec_node_re_lex *node = (struct ec_node_re_lex *)gen_node;
        struct regexp_pattern *table = NULL;
 {
        struct ec_node_re_lex *node = (struct ec_node_re_lex *)gen_node;
        struct regexp_pattern *table = NULL;
-       const struct ec_config *patterns, *child, *elt, *pattern, *keep;
-       char *pattern_str = NULL;
+       const struct ec_config *patterns, *child, *elt, *pattern, *keep, *attr;
+       char *pattern_str = NULL, *attr_name = NULL;
        ssize_t i, n = 0;
        int ret;
 
        ssize_t i, n = 0;
        int ret;
 
@@ -279,9 +304,20 @@ static int ec_node_re_lex_set_config(struct ec_node *gen_node,
                                errno = EINVAL;
                                goto fail;
                        }
                                errno = EINVAL;
                                goto fail;
                        }
+                       attr = ec_config_dict_get(elt, "attr");
+                       if (attr != NULL && ec_config_get_type(attr) !=
+                                       EC_CONFIG_TYPE_STRING) {
+                               errno = EINVAL;
+                               goto fail;
+                       }
                        pattern_str = ec_strdup(pattern->string);
                        if (pattern_str == NULL)
                                goto fail;
                        pattern_str = ec_strdup(pattern->string);
                        if (pattern_str == NULL)
                                goto fail;
+                       if (attr != NULL && attr->string != NULL) {
+                               attr_name = ec_strdup(attr->string);
+                               if (attr_name == NULL)
+                                       goto fail;
+                       }
 
                        ret = regcomp(&table[n].r, pattern_str, REG_EXTENDED);
                        if (ret != 0) {
 
                        ret = regcomp(&table[n].r, pattern_str, REG_EXTENDED);
                        if (ret != 0) {
@@ -296,7 +332,9 @@ static int ec_node_re_lex_set_config(struct ec_node *gen_node,
                        }
                        table[n].pattern = pattern_str;
                        table[n].keep = keep->boolean;
                        }
                        table[n].pattern = pattern_str;
                        table[n].keep = keep->boolean;
+                       table[n].attr_name = attr_name;
                        pattern_str = NULL;
                        pattern_str = NULL;
+                       attr_name = NULL;
 
                        n++;
                }
 
                        n++;
                }
@@ -343,7 +381,8 @@ static struct ec_node_type ec_node_re_lex_type = {
 
 EC_NODE_TYPE_REGISTER(ec_node_re_lex_type);
 
 
 EC_NODE_TYPE_REGISTER(ec_node_re_lex_type);
 
-int ec_node_re_lex_add(struct ec_node *gen_node, const char *pattern, int keep)
+int ec_node_re_lex_add(struct ec_node *gen_node, const char *pattern, int keep,
+       const char *attr_name)
 {
        const struct ec_config *cur_config = NULL;
        struct ec_config *config = NULL, *patterns = NULL, *elt = NULL;
 {
        const struct ec_config *cur_config = NULL;
        struct ec_config *config = NULL, *patterns = NULL, *elt = NULL;
@@ -359,6 +398,11 @@ int ec_node_re_lex_add(struct ec_node *gen_node, const char *pattern, int keep)
                goto fail;
        if (ec_config_dict_set(elt, "keep", ec_config_bool(keep)) < 0)
                goto fail;
                goto fail;
        if (ec_config_dict_set(elt, "keep", ec_config_bool(keep)) < 0)
                goto fail;
+       if (attr_name != NULL) {
+               if (ec_config_dict_set(elt, "attr",
+                                       ec_config_string(attr_name)) < 0)
+                       goto fail;
+       }
 
        cur_config = ec_node_get_config(gen_node);
        if (cur_config == NULL)
 
        cur_config = ec_node_get_config(gen_node);
        if (cur_config == NULL)
@@ -478,20 +522,17 @@ static int ec_node_re_lex_testcase(void)
                return -1;
        }
 
                return -1;
        }
 
-       ret = ec_node_re_lex_add(node, "[a-zA-Z]+", 1);
+       ret = ec_node_re_lex_add(node, "[a-zA-Z]+", 1, NULL);
        testres |= EC_TEST_CHECK(ret == 0, "cannot add regexp");
        testres |= EC_TEST_CHECK(ret == 0, "cannot add regexp");
-       ec_node_free(node);
-       return 0;
-
-       ret = ec_node_re_lex_add(node, "[0-9]+", 1);
+       ret = ec_node_re_lex_add(node, "[0-9]+", 1, NULL);
        testres |= EC_TEST_CHECK(ret == 0, "cannot add regexp");
        testres |= EC_TEST_CHECK(ret == 0, "cannot add regexp");
-       ret = ec_node_re_lex_add(node, "=", 1);
+       ret = ec_node_re_lex_add(node, "=", 1, NULL);
        testres |= EC_TEST_CHECK(ret == 0, "cannot add regexp");
        testres |= EC_TEST_CHECK(ret == 0, "cannot add regexp");
-       ret = ec_node_re_lex_add(node, "-", 1);
+       ret = ec_node_re_lex_add(node, "-", 1, NULL);
        testres |= EC_TEST_CHECK(ret == 0, "cannot add regexp");
        testres |= EC_TEST_CHECK(ret == 0, "cannot add regexp");
-       ret = ec_node_re_lex_add(node, "\\+", 1);
+       ret = ec_node_re_lex_add(node, "\\+", 1, NULL);
        testres |= EC_TEST_CHECK(ret == 0, "cannot add regexp");
        testres |= EC_TEST_CHECK(ret == 0, "cannot add regexp");
-       ret = ec_node_re_lex_add(node, "[       ]+", 0);
+       ret = ec_node_re_lex_add(node, "[       ]+", 0, NULL);
        testres |= EC_TEST_CHECK(ret == 0, "cannot add regexp");
        if (ret != 0) {
                EC_LOG(EC_LOG_ERR, "cannot add regexp to node\n");
        testres |= EC_TEST_CHECK(ret == 0, "cannot add regexp");
        if (ret != 0) {
                EC_LOG(EC_LOG_ERR, "cannot add regexp to node\n");
index 632627c..94d426e 100644 (file)
@@ -9,6 +9,7 @@
 
 struct ec_node *ec_node_re_lex(const char *id, struct ec_node *child);
 
 
 struct ec_node *ec_node_re_lex(const char *id, struct ec_node *child);
 
-int ec_node_re_lex_add(struct ec_node *gen_node, const char *pattern, int keep);
+int ec_node_re_lex_add(struct ec_node *gen_node, const char *pattern, int keep,
+       const char *attr_name);
 
 #endif
 
 #endif