rework int/uint node
[protos/libecoli.git] / lib / ecoli_node.h
index e027f1d..d4f65f3 100644 (file)
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+/**
+ * Interface to manage the ecoli nodes.
+ *
+ * A node is a main structure of the ecoli library, used to define how
+ * to match and complete the input tokens. A node is a generic object
+ * that implements:
+ * - a parse(node, input) method: check if an input matches
+ * - a complete(node, input) method: return possible completions for
+ *   a given input
+ * - some other methods to initialize, free, ...
+ *
+ * One basic example is the string node (ec_node_str). A node
+ * ec_node_str("foo") will match any token list starting with "foo",
+ * for example:
+ * - ["foo"]
+ * - ["foo", "bar", ...]
+ * But will not match:
+ * - []
+ * - ["bar", ...]
+ *
+ * A node ec_node_str("foo") will complete with "foo" if the input
+ * contains one token, with the same beginning than "foo":
+ * - [""]
+ * - ["f"]
+ * - ["fo"]
+ * - ["foo"]
+ * But it will not complete:
+ * - []
+ * - ["bar"]
+ * - ["f", ""]
+ * - ["", "f"]
+ *
+ * A node can have child nodes. For instance, a sequence node
+ * ec_node_seq(ec_node_str("foo"), ec_node_str("bar")) will match
+ * ["foo", "bar"].
+ */
+
 #ifndef ECOLI_NODE_
 #define ECOLI_NODE_
 
@@ -32,6 +69,8 @@
 #include <sys/types.h>
 #include <stdio.h>
 
+#define EC_NO_ID "no-id"
+
 #define EC_NODE_ENDLIST ((void *)1)
 
 struct ec_node;
@@ -57,11 +96,12 @@ typedef int (*ec_node_build_t)(struct ec_node *node);
 typedef int (*ec_node_parse_t)(const struct ec_node *node,
                        struct ec_parsed *state,
                        const struct ec_strvec *strvec);
-typedef struct ec_completed *(*ec_node_complete_t)(const struct ec_node *node,
-                                               struct ec_parsed *state,
-                                               const struct ec_strvec *strvec);
+typedef int (*ec_node_complete_t)(const struct ec_node *node,
+                               struct ec_completed *completed_state,
+                               const struct ec_strvec *strvec);
+typedef size_t (*ec_node_get_max_parse_len_t)(const struct ec_node *node);
 typedef const char * (*ec_node_desc_t)(const struct ec_node *);
-typedef void (*ec_node_init_priv_t)(struct ec_node *);
+typedef int (*ec_node_init_priv_t)(struct ec_node *);
 typedef void (*ec_node_free_priv_t)(struct ec_node *);
 
 /**
@@ -69,10 +109,11 @@ typedef void (*ec_node_free_priv_t)(struct ec_node *);
  */
 struct ec_node_type {
        TAILQ_ENTRY(ec_node_type) next;  /**< Next in list. */
-       const char *name;              /**< Node type name. */
-       ec_node_build_t build; /* (re)build the node, called by generic parse */
+       const char *name;                /**< Node type name. */
+       ec_node_build_t build;           /**< (Re)build the node */
        ec_node_parse_t parse;
        ec_node_complete_t complete;
+       ec_node_get_max_parse_len_t get_max_parse_len;
        ec_node_desc_t desc;
        size_t size;
        ec_node_init_priv_t init_priv;
@@ -132,6 +173,13 @@ struct ec_node *ec_node(const char *typename, const char *id);
 struct ec_node *ec_node_clone(struct ec_node *node);
 void ec_node_free(struct ec_node *node);
 
+/**
+ * Get the max len of strvec that can be parsed by this node
+ *
+ * If there is no maximum, return SIZE_MAX.
+ */
+size_t ec_node_get_max_parse_len(const struct ec_node *node);
+
 /* XXX add more accessors */
 struct ec_keyval *ec_node_attrs(const struct ec_node *node);
 struct ec_node *ec_node_parent(const struct ec_node *node);
@@ -141,4 +189,8 @@ const char *ec_node_desc(const struct ec_node *node);
 void ec_node_dump(FILE *out, const struct ec_node *node);
 struct ec_node *ec_node_find(struct ec_node *node, const char *id);
 
+/* check the type of a node */
+int ec_node_check_type(const struct ec_node *node,
+               const struct ec_node_type *type);
+
 #endif