1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
6 * @defgroup grammar_tree Grammar Tree
9 * @brief Libecoli grammar nodes.
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
14 * - a parse(node, input) method: check if an input matches
15 * - a complete(node, input) method: return possible completions for
17 * - some other methods to initialize, free, ...
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",
23 * - ["foo", "bar", ...]
28 * A node ec_node_str("foo") will complete with "foo" if the input
29 * contains one token, with the same beginning than "foo":
34 * But it will not complete:
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
48 #include <sys/queue.h>
49 #include <sys/types.h>
53 * Node has no identifier.
55 #define EC_NO_ID "no-id"
63 struct ec_config_schema;
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) \
70 if (ec_node_type_register(&t) < 0) \
72 "cannot register node type %s\n", \
76 TAILQ_HEAD(ec_node_type_list, ec_node_type);
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);
94 * A structure describing a 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 */
104 ec_complete_t complete;
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;
114 * Register a node type.
117 * A pointer to a ec_test structure describing the test
120 * 0 on success, negative value on error.
122 int ec_node_type_register(struct ec_node_type *type);
125 * Lookup node type by name
128 * The name of the node type to search.
130 * The node type if found, or NULL on error.
132 const struct ec_node_type *ec_node_type_lookup(const char *name);
135 * Dump registered log types
137 void ec_node_type_dump(FILE *out);
140 * Get the config schema of a node type.
142 const struct ec_config_schema *
143 ec_node_type_schema(const struct ec_node_type *type);
146 * Get the name of a node type.
149 ec_node_type_name(const struct ec_node_type *type);
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,
159 /* create a new node when the type is known, typically called from the node
161 struct ec_node *ec_node_from_type(const struct ec_node_type *type, const char *id);
163 /* create a new node */
164 struct ec_node *ec_node(const char *typename, const char *id);
166 struct ec_node *ec_node_clone(struct ec_node *node);
167 void ec_node_free(struct ec_node *node);
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);
175 /* get the current node configuration. Return NULL if no configuration. */
176 const struct ec_config *ec_node_get_config(struct ec_node *node);
178 size_t ec_node_get_children_count(const struct ec_node *node);
180 ec_node_get_child(const struct ec_node *node, size_t i,
181 struct ec_node **child, unsigned int *refs);
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);
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);
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);
196 const char *ec_node_get_type_name(const struct ec_node *node);
199 * Get the pointer to the node private area.
204 * The pointer to the node private area.
206 void *ec_node_priv(const struct ec_node *node);