rename structures and functions
[protos/libecoli.git] / include / ecoli_node.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
3  */
4
5 /**
6  * @defgroup grammar_tree Grammar Tree
7  * @{
8  *
9  * @brief Libecoli grammar nodes.
10  *
11  * The grammar node is a main structure of the ecoli library, used to define
12  * how to match and complete the input tokens. A node is a generic object
13  * that implements:
14  * - a parse(node, input) method: check if an input matches
15  * - a complete(node, input) method: return possible completions for
16  *   a given input
17  * - some other methods to initialize, free, ...
18  *
19  * One basic example is the string node (ec_node_str). A node
20  * ec_node_str("foo") will match any token list starting with "foo",
21  * for example:
22  * - ["foo"]
23  * - ["foo", "bar", ...]
24  * But will not match:
25  * - []
26  * - ["bar", ...]
27  *
28  * A node ec_node_str("foo") will complete with "foo" if the input
29  * contains one token, with the same beginning than "foo":
30  * - [""]
31  * - ["f"]
32  * - ["fo"]
33  * - ["foo"]
34  * But it will not complete:
35  * - []
36  * - ["bar"]
37  * - ["f", ""]
38  * - ["", "f"]
39  *
40  * A node can have child nodes. For instance, a sequence node
41  * ec_node_seq(ec_node_str("foo"), ec_node_str("bar")) will match
42  * ["foo", "bar"].
43  */
44
45 #ifndef ECOLI_NODE_
46 #define ECOLI_NODE_
47
48 #include <sys/queue.h>
49 #include <sys/types.h>
50 #include <stdio.h>
51
52 /**
53  * Node has no identifier.
54  */
55 #define EC_NO_ID "no-id"
56
57 struct ec_node;
58 struct ec_pnode;
59 struct ec_comp;
60 struct ec_strvec;
61 struct ec_dict;
62 struct ec_config;
63 struct ec_config_schema;
64
65 #define EC_NODE_TYPE_REGISTER(t)                                        \
66         static void ec_node_init_##t(void);                             \
67         static void __attribute__((constructor, used))                  \
68         ec_node_init_##t(void)                                          \
69         {                                                               \
70                 if (ec_node_type_register(&t) < 0)                      \
71                         fprintf(stderr,                                 \
72                                 "cannot register node type %s\n",       \
73                                 t.name);                                \
74         }
75
76 TAILQ_HEAD(ec_node_type_list, ec_node_type);
77
78 typedef int (*ec_node_set_config_t)(struct ec_node *node,
79                                 const struct ec_config *config);
80 typedef int (*ec_parse_t)(const struct ec_node *node,
81                         struct ec_pnode *state,
82                         const struct ec_strvec *strvec);
83 typedef int (*ec_complete_t)(const struct ec_node *node,
84                                 struct ec_comp *comp_state,
85                                 const struct ec_strvec *strvec);
86 typedef const char * (*ec_node_desc_t)(const struct ec_node *);
87 typedef int (*ec_node_init_priv_t)(struct ec_node *);
88 typedef void (*ec_node_free_priv_t)(struct ec_node *);
89 typedef size_t (*ec_node_get_children_count_t)(const struct ec_node *);
90 typedef int (*ec_node_get_child_t)(const struct ec_node *,
91         size_t i, struct ec_node **child, unsigned int *refs);
92
93 /**
94  * A structure describing a node type.
95  */
96 struct ec_node_type {
97         TAILQ_ENTRY(ec_node_type) next;  /**< Next in list. */
98         const char *name;                /**< Node type name. */
99         /** Configuration schema array, must be terminated by a sentinel
100          *  (.type = EC_CONFIG_TYPE_NONE). */
101         const struct ec_config_schema *schema;
102         ec_node_set_config_t set_config; /* validate/ack a config change */
103         ec_parse_t parse;
104         ec_complete_t complete;
105         ec_node_desc_t desc;
106         size_t size;
107         ec_node_init_priv_t init_priv;
108         ec_node_free_priv_t free_priv;
109         ec_node_get_children_count_t get_children_count;
110         ec_node_get_child_t get_child;
111 };
112
113 /**
114  * Register a node type.
115  *
116  * @param type
117  *   A pointer to a ec_test structure describing the test
118  *   to be registered.
119  * @return
120  *   0 on success, negative value on error.
121  */
122 int ec_node_type_register(struct ec_node_type *type);
123
124 /**
125  * Lookup node type by name
126  *
127  * @param name
128  *   The name of the node type to search.
129  * @return
130  *   The node type if found, or NULL on error.
131  */
132 const struct ec_node_type *ec_node_type_lookup(const char *name);
133
134 /**
135  * Dump registered log types
136  */
137 void ec_node_type_dump(FILE *out);
138
139 /**
140  * Get the config schema of a node type.
141  */
142 const struct ec_config_schema *
143 ec_node_type_schema(const struct ec_node_type *type);
144
145 /**
146  * Get the name of a node type.
147  */
148 const char *
149 ec_node_type_name(const struct ec_node_type *type);
150
151 enum ec_node_free_state {
152         EC_NODE_FREE_STATE_NONE,
153         EC_NODE_FREE_STATE_TRAVERSED,
154         EC_NODE_FREE_STATE_FREEABLE,
155         EC_NODE_FREE_STATE_NOT_FREEABLE,
156         EC_NODE_FREE_STATE_FREEING,
157 };
158
159 /* create a new node when the type is known, typically called from the node
160  * code */
161 struct ec_node *ec_node_from_type(const struct ec_node_type *type, const char *id);
162
163 /* create a new node */
164 struct ec_node *ec_node(const char *typename, const char *id);
165
166 struct ec_node *ec_node_clone(struct ec_node *node);
167 void ec_node_free(struct ec_node *node);
168
169 /* set configuration of a node
170  * after a call to this function, the config is
171  * owned by the node and must not be used by the caller
172  * on error, the config is freed. */
173 int ec_node_set_config(struct ec_node *node, struct ec_config *config);
174
175 /* get the current node configuration. Return NULL if no configuration. */
176 const struct ec_config *ec_node_get_config(struct ec_node *node);
177
178 size_t ec_node_get_children_count(const struct ec_node *node);
179 int
180 ec_node_get_child(const struct ec_node *node, size_t i,
181         struct ec_node **child, unsigned int *refs);
182
183 /* XXX add more accessors */
184 const struct ec_node_type *ec_node_type(const struct ec_node *node);
185 struct ec_dict *ec_node_attrs(const struct ec_node *node);
186 const char *ec_node_id(const struct ec_node *node);
187 const char *ec_node_desc(const struct ec_node *node);
188
189 void ec_node_dump(FILE *out, const struct ec_node *node);
190 struct ec_node *ec_node_find(struct ec_node *node, const char *id);
191
192 /* check the type of a node */
193 int ec_node_check_type(const struct ec_node *node,
194                 const struct ec_node_type *type);
195
196 const char *ec_node_get_type_name(const struct ec_node *node);
197
198 /**
199  * Get the pointer to the node private area.
200  *
201  * @param node
202  *   The grammar node.
203  * @return
204  *   The pointer to the node private area.
205  */
206 void *ec_node_priv(const struct ec_node *node);
207
208 #endif
209
210  /** @} */