node_any: add a C api to match strvec attributes
authorOlivier Matz <zer0@droids-corp.org>
Thu, 7 Mar 2019 18:30:47 +0000 (19:30 +0100)
committerOlivier Matz <zer0@droids-corp.org>
Thu, 7 Mar 2019 18:30:47 +0000 (19:30 +0100)
include/ecoli_node_any.h
src/ecoli_node_any.c

index ee638aa..cb671e3 100644 (file)
@@ -3,12 +3,25 @@
  */
 
 /**
- * This node always matches 1 string in the vector
+ * This node always matches 1 string in the vector.
+ * An optional strvec attribute can be checked too. These
+ * attributes are usually set by a lexer node.
  */
 
 #ifndef ECOLI_NODE_ANY_
 #define ECOLI_NODE_ANY_
 
-/* no specific API for this node */
+/**
+ * Create a "any" node.
+ *
+ * @param id
+ *   The node identifier.
+ * @param attr
+ *   The strvec attribute to match, or NULL.
+ * @return
+ *   The ecoli node.
+ */
+struct ec_node *
+ec_node_any(const char *id, const char *attr);
 
 #endif
index 9166dbb..fbcc460 100644 (file)
@@ -99,6 +99,38 @@ static struct ec_node_type ec_node_any_type = {
 
 EC_NODE_TYPE_REGISTER(ec_node_any_type);
 
+struct ec_node *
+ec_node_any(const char *id, const char *attr)
+{
+       struct ec_config *config = NULL;
+       struct ec_node *gen_node = NULL;
+       int ret;
+
+       gen_node = ec_node_from_type(&ec_node_any_type, id);
+       if (gen_node == NULL)
+               return NULL;
+
+       config = ec_config_dict();
+       if (config == NULL)
+               goto fail;
+
+       ret = ec_config_dict_set(config, "attr", ec_config_string(attr));
+       if (ret < 0)
+               goto fail;
+
+       ret = ec_node_set_config(gen_node, config);
+       config = NULL;
+       if (ret < 0)
+               goto fail;
+
+       return gen_node;
+
+fail:
+       ec_config_free(config);
+       ec_node_free(gen_node);
+       return NULL;
+}
+
 /* LCOV_EXCL_START */
 static int ec_node_any_testcase(void)
 {