| 1 | /** |
| 2 | * @file node.h |
| 3 | * |
| 4 | * Functions related to nodes in the AST. |
| 5 | */ |
| 6 | #ifndef PRISM_NODE_H |
| 7 | #define PRISM_NODE_Hcxx . . $ `include/prism/node.h:7:9`! |
| 8 | |
| 9 | #include "prism/defines.h"cxx . . $ `<file>/include/prism/defines.h`/ |
| 10 | #include "prism/parser.h"cxx . . $ `<file>/include/prism/parser.h`/ |
| 11 | #include "prism/util/pm_buffer.h"cxx . . $ `<file>/include/prism/util/pm_buffer.h`/ |
| 12 | |
| 13 | /** |
| 14 | * Loop through each node in the node list, writing each node to the given |
| 15 | * pm_node_t pointer. |
| 16 | */ |
| 17 | #define PM_NODE_LIST_FOREACHcxx . . $ `include/prism/node.h:17:9`!(list, index, node) \ |
| 18 | for (size_t index = 0; index < (list)->size && ((node) = (list)->nodes[index]); index++) |
| 19 | |
| 20 | /** |
| 21 | * Append a new node onto the end of the node list. |
| 22 | * |
| 23 | * @param list The list to append to. |
| 24 | * @param node The node to append. |
| 25 | */ |
| 26 | void pm_node_list_appendcxx . . $ pm_node_list_append(63cefce6a0733239).(pm_node_list_tcxx . . $ pm_node_list_t# *list, pm_node_tcxx . . $ pm_node_t# *node); |
| 27 | |
| 28 | /** |
| 29 | * Prepend a new node onto the beginning of the node list. |
| 30 | * |
| 31 | * @param list The list to prepend to. |
| 32 | * @param node The node to prepend. |
| 33 | */ |
| 34 | void pm_node_list_prependcxx . . $ pm_node_list_prepend(63cefce6a0733239).(pm_node_list_tcxx . . $ pm_node_list_t# *list, pm_node_tcxx . . $ pm_node_t# *node); |
| 35 | |
| 36 | /** |
| 37 | * Concatenate the given node list onto the end of the other node list. |
| 38 | * |
| 39 | * @param list The list to concatenate onto. |
| 40 | * @param other The list to concatenate. |
| 41 | */ |
| 42 | void pm_node_list_concatcxx . . $ pm_node_list_concat(cd1b142af689bb10).(pm_node_list_tcxx . . $ pm_node_list_t# *list, pm_node_list_tcxx . . $ pm_node_list_t# *other); |
| 43 | |
| 44 | /** |
| 45 | * Free the internal memory associated with the given node list. |
| 46 | * |
| 47 | * @param list The list to free. |
| 48 | */ |
| 49 | void pm_node_list_freecxx . . $ pm_node_list_free(47f682aabf6c68aa).(pm_node_list_tcxx . . $ pm_node_list_t# *list); |
| 50 | |
| 51 | /** |
| 52 | * Deallocate a node and all of its children. |
| 53 | * |
| 54 | * @param parser The parser that owns the node. |
| 55 | * @param node The node to deallocate. |
| 56 | */ |
| 57 | PRISM_EXPORTED_FUNCTIONcxx . . $ `include/prism/defines.h:50:19`! void pm_node_destroycxx . . $ pm_node_destroy(51572ef0eb29741b).(pm_parser_tcxx . . $ pm_parser_t# *parser, struct pm_nodecxx . . $ pm_node# *node); |
| 58 | |
| 59 | /** |
| 60 | * Returns a string representation of the given node type. |
| 61 | * |
| 62 | * @param node_type The node type to convert to a string. |
| 63 | * @return A string representation of the given node type. |
| 64 | */ |
| 65 | PRISM_EXPORTED_FUNCTIONcxx . . $ `include/prism/defines.h:50:19`! const char * pm_node_type_to_strcxx . . $ pm_node_type_to_str(4310ac79bf069db).(pm_node_type_tcxx . . $ pm_node_type_t# node_type); |
| 66 | |
| 67 | /** |
| 68 | * Visit each of the nodes in this subtree using the given visitor callback. The |
| 69 | * callback function will be called for each node in the subtree. If it returns |
| 70 | * false, then that node's children will not be visited. If it returns true, |
| 71 | * then the children will be visited. The data parameter is treated as an opaque |
| 72 | * pointer and is passed to the visitor callback for consumers to use as they |
| 73 | * see fit. |
| 74 | * |
| 75 | * As an example: |
| 76 | * |
| 77 | * ```c |
| 78 | * #include "prism.h" |
| 79 | * |
| 80 | * bool visit(const pm_node_t *node, void *data) { |
| 81 | * size_t *indent = (size_t *) data; |
| 82 | * for (size_t i = 0; i < *indent * 2; i++) putc(' ', stdout); |
| 83 | * printf("%s\n", pm_node_type_to_str(node->type)); |
| 84 | * |
| 85 | * size_t next_indent = *indent + 1; |
| 86 | * size_t *next_data = &next_indent; |
| 87 | * pm_visit_child_nodes(node, visit, next_data); |
| 88 | * |
| 89 | * return false; |
| 90 | * } |
| 91 | * |
| 92 | * int main(void) { |
| 93 | * const char *source = "1 + 2; 3 + 4"; |
| 94 | * size_t size = strlen(source); |
| 95 | * |
| 96 | * pm_parser_t parser; |
| 97 | * pm_options_t options = { 0 }; |
| 98 | * pm_parser_init(&parser, (const uint8_t *) source, size, &options); |
| 99 | * |
| 100 | * size_t indent = 0; |
| 101 | * pm_node_t *node = pm_parse(&parser); |
| 102 | * |
| 103 | * size_t *data = &indent; |
| 104 | * pm_visit_node(node, visit, data); |
| 105 | * |
| 106 | * pm_node_destroy(&parser, node); |
| 107 | * pm_parser_free(&parser); |
| 108 | * return EXIT_SUCCESS; |
| 109 | * } |
| 110 | * ``` |
| 111 | * |
| 112 | * @param node The root node to start visiting from. |
| 113 | * @param visitor The callback to call for each node in the subtree. |
| 114 | * @param data An opaque pointer that is passed to the visitor callback. |
| 115 | */ |
| 116 | PRISM_EXPORTED_FUNCTIONcxx . . $ `include/prism/defines.h:50:19`! void pm_visit_nodecxx . . $ pm_visit_node(e835be1ae5272e61).(const pm_node_tcxx . . $ pm_node_t# *node, boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! (*visitor)(const pm_node_tcxx . . $ pm_node_t# *node, void *data), void *data); |
| 117 | |
| 118 | /** |
| 119 | * Visit the children of the given node with the given callback. This is the |
| 120 | * default behavior for walking the tree that is called from pm_visit_node if |
| 121 | * the callback returns true. |
| 122 | * |
| 123 | * @param node The node to visit the children of. |
| 124 | * @param visitor The callback to call for each child node. |
| 125 | * @param data An opaque pointer that is passed to the visitor callback. |
| 126 | */ |
| 127 | PRISM_EXPORTED_FUNCTIONcxx . . $ `include/prism/defines.h:50:19`! void pm_visit_child_nodescxx . . $ pm_visit_child_nodes(e835be1ae5272e61).(const pm_node_tcxx . . $ pm_node_t# *node, boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! (*visitor)(const pm_node_tcxx . . $ pm_node_t# *node, void *data), void *data); |
| 128 | |
| 129 | #endif |