1 /* binding file AST interface 2 * 3 * This file is part of nsnsgenbind. 4 * Licensed under the MIT License, 5 * http://www.opensource.org/licenses/mit-license.php 6 * Copyright 2012 Vincent Sanders <vince@netsurf-browser.org> 7 */ 8 9 #ifndef nsgenbind_nsgenbind_ast_h 10 #define nsgenbind_nsgenbind_ast_h 11 12 enum genbind_node_type { 13 GENBIND_NODE_TYPE_ROOT = 0, 14 GENBIND_NODE_TYPE_IDENT, /**< generic identifier string */ 15 GENBIND_NODE_TYPE_NAME, /**< generic type string */ 16 GENBIND_NODE_TYPE_MODIFIER, /**< node modifier */ 17 GENBIND_NODE_TYPE_CDATA, /**< verbatim block of character data */ 18 GENBIND_NODE_TYPE_STRING, /**< text string */ 19 GENBIND_NODE_TYPE_LINE, /**< linenumber */ 20 GENBIND_NODE_TYPE_FILE, /**< file name */ 21 22 GENBIND_NODE_TYPE_BINDING, /**< Binding */ 23 GENBIND_NODE_TYPE_WEBIDL, 24 25 GENBIND_NODE_TYPE_CLASS, /**< class definition */ 26 GENBIND_NODE_TYPE_PRIVATE, 27 GENBIND_NODE_TYPE_INTERNAL, 28 GENBIND_NODE_TYPE_PROPERTY, 29 GENBIND_NODE_TYPE_FLAGS, 30 31 GENBIND_NODE_TYPE_METHOD, /**< binding method */ 32 GENBIND_NODE_TYPE_METHOD_TYPE, /**< binding method type */ 33 34 GENBIND_NODE_TYPE_PARAMETER, /**< method parameter */ 35 }; 36 37 /* modifier flags */ 38 enum genbind_type_modifier { 39 GENBIND_TYPE_NONE = 0, 40 GENBIND_TYPE_TYPE = 1, /**< identifies a type handler */ 41 GENBIND_TYPE_UNSHARED = 2, /**< unshared item */ 42 GENBIND_TYPE_TYPE_UNSHARED = 3, /**< identifies a unshared type handler */ 43 }; 44 45 /* binding method types */ 46 enum genbind_method_type { 47 GENBIND_METHOD_TYPE_INIT = 0, /**< method is initialiser */ 48 GENBIND_METHOD_TYPE_FINI, /**< method is finalizer */ 49 GENBIND_METHOD_TYPE_METHOD, /**< method is a method */ 50 GENBIND_METHOD_TYPE_GETTER, /**< method is a getter */ 51 GENBIND_METHOD_TYPE_SETTER, /**< method is a setter */ 52 GENBIND_METHOD_TYPE_PROTOTYPE, /**< method is a prototype */ 53 GENBIND_METHOD_TYPE_PREFACE, /**< method is a preface */ 54 GENBIND_METHOD_TYPE_PROLOGUE, /**< method is a prologue */ 55 GENBIND_METHOD_TYPE_EPILOGUE, /**< method is a epilogue */ 56 GENBIND_METHOD_TYPE_POSTFACE, /**< method is a postface */ 57 }; 58 59 struct genbind_node; 60 61 /** callback for search and iteration routines */ 62 typedef int (genbind_callback_t)(struct genbind_node *node, void *ctx); 63 64 int genbind_fprintf(FILE *stream, const char *format, ...); 65 66 int genbind_cmp_node_type(struct genbind_node *node, void *ctx); 67 68 FILE *genbindopen(const char *filename); 69 70 int genbind_parsefile(char *infilename, struct genbind_node **ast); 71 72 char *genbind_strapp(char *a, char *b); 73 74 /** 75 * create a new node with value from pointer 76 */ 77 struct genbind_node *genbind_new_node(enum genbind_node_type type, struct genbind_node *l, void *r); 78 79 /** 80 * create a new number node 81 * 82 * Create a node with of number type 83 */ 84 struct genbind_node *genbind_new_number_node(enum genbind_node_type type, struct genbind_node *l, int number); 85 86 struct genbind_node *genbind_node_link(struct genbind_node *tgt, struct genbind_node *src); 87 88 struct genbind_node *genbind_node_prepend(struct genbind_node *list, struct genbind_node *inst); 89 90 struct genbind_node *genbind_node_add(struct genbind_node *node, struct genbind_node *list); 91 92 /** 93 * Dump the binding AST to file 94 * 95 * If the debug flag has been set this causes the binding AST to be written to 96 * a binding-ast output file 97 * 98 * \param node Node of the tree to start dumping from (usually tree root) 99 * \return 0 on sucess or non zero on faliure and error message printed. 100 */ 101 int genbind_dump_ast(struct genbind_node *node); 102 103 /** 104 *Depth first left hand search using user provided comparison 105 * 106 * @param node The node to start the search from 107 * @param prev The node at which to stop the search, either NULL to 108 * search the full tree depth (initial search) or the result 109 * of a previous search to continue. 110 * @param cb Comparison callback 111 * @param ctx Context for callback 112 */ 113 struct genbind_node * 114 genbind_node_find(struct genbind_node *node, 115 struct genbind_node *prev, 116 genbind_callback_t *cb, 117 void *ctx); 118 119 /** 120 * Depth first left hand search returning nodes of the specified type 121 * 122 * @param node The node to start the search from 123 * @param prev The node at which to stop the search, either NULL to 124 * search the full tree depth (initial search) or the result 125 * of a previous search to continue. 126 * @param nodetype The type of node to seach for 127 * @return The found node or NULL for no nodes. 128 */ 129 struct genbind_node * 130 genbind_node_find_type(struct genbind_node *node, 131 struct genbind_node *prev, 132 enum genbind_node_type nodetype); 133 134 /** 135 * count how many nodes of a specified type. 136 * 137 * Enumerate how many nodes of the specified type there are by 138 * performing a depth first search for nodes of the given type and 139 * counting the number of results. 140 * 141 * @param node The node to start the search from 142 * @param nodetype The type of node to count 143 * @return The number of nodes found. 144 */ 145 int 146 genbind_node_enumerate_type(struct genbind_node *node, 147 enum genbind_node_type type); 148 149 150 /** 151 * Depth first left hand search returning nodes of the specified type 152 * with an ident child node with matching text 153 * 154 * @param node The node to start the search from 155 * @param prev The node at which to stop the search, either NULL to 156 * search the full tree depth (initial search) or the result 157 * of a previous search to continue. 158 * @param nodetype The type of node to seach for 159 * @param ident The text to match the ident child node to 160 */ 161 struct genbind_node * 162 genbind_node_find_type_ident(struct genbind_node *node, 163 struct genbind_node *prev, 164 enum genbind_node_type nodetype, 165 const char *ident); 166 167 168 /** 169 * Find a method node of a given method type 170 * 171 * \param node A node of type GENBIND_NODE_TYPE_CLASS to search for methods. 172 * \param prev The node at which to stop the search, either NULL to 173 * search the full tree depth (initial search) or the result 174 * of a previous search to continue. 175 * \param methodtype The type of method to find. 176 * \return A node of type GENBIND_NODE_TYPE_METHOD on success or NULL on faliure 177 */ 178 struct genbind_node * 179 genbind_node_find_method(struct genbind_node *node, 180 struct genbind_node *prev, 181 enum genbind_method_type methodtype); 182 183 184 /** 185 * Find a method node of a given method type and identifier 186 * 187 * \param node A node of type GENBIND_NODE_TYPE_CLASS to search for methods. 188 * \param prev The node at which to stop the search, either NULL to 189 * search the full tree depth (initial search) or the result 190 * of a previous search to continue. 191 * \param methodtype The type of method to find. 192 * \param ident The identifier to search for 193 * \return A node of type GENBIND_NODE_TYPE_METHOD on success or NULL on faliure 194 */ 195 struct genbind_node * 196 genbind_node_find_method_ident(struct genbind_node *node, 197 struct genbind_node *prev, 198 enum genbind_method_type methodtype, 199 const char *ident); 200 201 202 /** 203 * Iterate all nodes of a certian type from a node with a callback. 204 * 205 * Depth first search for nodes of the given type calling the callback 206 * with context. 207 * 208 * @param node The node to start the search from. 209 */ 210 int genbind_node_foreach_type(struct genbind_node *node, 211 enum genbind_node_type type, 212 genbind_callback_t *cb, 213 void *ctx); 214 215 /** get a nodes node list content 216 * 217 * @param node The nodes to get node list from 218 * @return pointer to the node list or NULL if the node does not contain a list 219 */ 220 struct genbind_node *genbind_node_getnode(struct genbind_node *node); 221 222 /** get a nodes text content 223 * 224 * @param node The nodes to get text from 225 * @return pointer to the node text or NULL if the node is not of type 226 * text or is empty. 227 */ 228 char *genbind_node_gettext(struct genbind_node *node); 229 230 /** get a nodes integer value 231 * 232 * @param node The nodes to get integer from 233 * @return pointer to the node value or NULL if the node is not of type 234 * int or is empty. 235 */ 236 int *genbind_node_getint(struct genbind_node *node); 237 238 #endif 239