@@ -82,4 +82,66 @@ PRISM_EXPORTED_FUNCTION void pm_node_memsize(pm_node_t *node, pm_memsize_t *mems
82
82
*/
83
83
PRISM_EXPORTED_FUNCTION const char * pm_node_type_to_str (pm_node_type_t node_type );
84
84
85
+ /**
86
+ * Visit each of the nodes in this subtree using the given visitor callback. The
87
+ * callback function will be called for each node in the subtree. If it returns
88
+ * false, then that node's children will not be visited. If it returns true,
89
+ * then the children will be visited. The data parameter is treated as an opaque
90
+ * pointer and is passed to the visitor callback for consumers to use as they
91
+ * see fit.
92
+ *
93
+ * As an example:
94
+ *
95
+ * ```c
96
+ * #include "prism.h"
97
+ *
98
+ * bool visit(const pm_node_t *node, void *data) {
99
+ * size_t *indent = (size_t *) data;
100
+ * for (size_t i = 0; i < *indent * 2; i++) putc(' ', stdout);
101
+ * printf("%s\n", pm_node_type_to_str(node->type));
102
+ *
103
+ * size_t next_indent = *indent + 1;
104
+ * size_t *next_data = &next_indent;
105
+ * pm_visit_child_nodes(node, visit, next_data);
106
+ *
107
+ * return false;
108
+ * }
109
+ *
110
+ * int main(void) {
111
+ * const char *source = "1 + 2; 3 + 4";
112
+ * size_t size = strlen(source);
113
+ *
114
+ * pm_parser_t parser;
115
+ * pm_options_t options = { 0 };
116
+ * pm_parser_init(&parser, (const uint8_t *) source, size, &options);
117
+ *
118
+ * size_t indent = 0;
119
+ * pm_node_t *node = pm_parse(&parser);
120
+ *
121
+ * size_t *data = &indent;
122
+ * pm_visit_node(node, visit, data);
123
+ *
124
+ * pm_node_destroy(&parser, node);
125
+ * pm_parser_free(&parser);
126
+ * return EXIT_SUCCESS;
127
+ * }
128
+ * ```
129
+ *
130
+ * @param node The root node to start visiting from.
131
+ * @param visitor The callback to call for each node in the subtree.
132
+ * @param data An opaque pointer that is passed to the visitor callback.
133
+ */
134
+ PRISM_EXPORTED_FUNCTION void pm_visit_node (const pm_node_t * node , bool (* visitor )(const pm_node_t * node , void * data ), void * data );
135
+
136
+ /**
137
+ * Visit the children of the given node with the given callback. This is the
138
+ * default behavior for walking the tree that is called from pm_visit_node if
139
+ * the callback returns true.
140
+ *
141
+ * @param node The node to visit the children of.
142
+ * @param visitor The callback to call for each child node.
143
+ * @param data An opaque pointer that is passed to the visitor callback.
144
+ */
145
+ PRISM_EXPORTED_FUNCTION void pm_visit_child_nodes (const pm_node_t * node , bool (* visitor )(const pm_node_t * node , void * data ), void * data );
146
+
85
147
#endif
0 commit comments