Ruby 4.1.0dev (2026-10-27 revision db563a357875d4efbadec58b0fd8695ac9a82f42)
Macros | Typedefs | Enumerations | Functions
node.h File Reference

(db563a357875d4efbadec58b0fd8695ac9a82f42)

Functions related to nodes in the AST. More...

#include "prism/compiler/exported.h"
#include "prism/compiler/nonnull.h"
#include "prism/ast.h"
Include dependency graph for node.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define PM_NODE_LIST_FOREACH(list, index, node)    for (size_t index = 0; index < (list)->size && ((node) = (list)->nodes[index]); index++)
 Loop through each node in the node list, writing each node to the given pm_node_t pointer.
 

Typedefs

typedef pm_visit_result_t(* pm_visit_callback_t) (const pm_node_t *node, void *data)
 A callback that is called for each node visited by pm_visit_node and pm_visit_child_nodes.
 

Enumerations

enum  pm_visit_result_t { PM_VISIT_SKIP , PM_VISIT_DESCEND , PM_VISIT_STOP }
 The value returned by a visitor callback, which determines where the walk goes next. More...
 

Functions

PRISM_EXPORTED_FUNCTION void pm_node_list_append (pm_arena_t *arena, pm_node_list_t *list, pm_node_t *node) PRISM_NONNULL(1
 Append a node to the end of a node list.
 
PRISM_EXPORTED_FUNCTION void PRISM_EXPORTED_FUNCTION const char * pm_node_type (pm_node_type_t node_type)
 Returns a string representation of the given node type.
 
PRISM_EXPORTED_FUNCTION bool pm_visit_node (const pm_node_t *node, pm_visit_callback_t visitor, void *data) PRISM_NONNULL(1)
 Visit each of the nodes in this subtree using the given visitor callback.
 
PRISM_EXPORTED_FUNCTION bool pm_visit_child_nodes (const pm_node_t *node, pm_visit_callback_t visitor, void *data) PRISM_NONNULL(1)
 Visit the children of the given node with the given callback.
 

Detailed Description

Functions related to nodes in the AST.

Definition in file node.h.

Macro Definition Documentation

◆ PM_NODE_LIST_FOREACH

#define PM_NODE_LIST_FOREACH (   list,
  index,
  node 
)     for (size_t index = 0; index < (list)->size && ((node) = (list)->nodes[index]); index++)

Loop through each node in the node list, writing each node to the given pm_node_t pointer.

Definition at line 22 of file node.h.

Typedef Documentation

◆ pm_visit_callback_t

typedef pm_visit_result_t(* pm_visit_callback_t) (const pm_node_t *node, void *data)

A callback that is called for each node visited by pm_visit_node and pm_visit_child_nodes.

The data parameter is the opaque pointer that was passed to the function that started the walk.

Definition at line 65 of file node.h.

Enumeration Type Documentation

◆ pm_visit_result_t

The value returned by a visitor callback, which determines where the walk goes next.

Enumerator
PM_VISIT_SKIP 

Continue the walk without visiting this node's children.

PM_VISIT_DESCEND 

Continue the walk by visiting this node's children.

PM_VISIT_STOP 

End the walk without visiting any more nodes.

Definition at line 49 of file node.h.

Function Documentation

◆ pm_node_list_append()

PRISM_EXPORTED_FUNCTION void pm_node_list_append ( pm_arena_t *  arena,
pm_node_list_t *  list,
pm_node_t *  node 
)

Append a node to the end of a node list.

If the list is full, its storage is grown by allocating from the given arena, so the arena must live as long as the list does. For a list in a parsed tree, that is the arena that was passed to pm_parser_new.

Parameters
arenaThe arena to allocate the list's storage from.
listThe list to append to.
nodeThe node to append.

◆ pm_node_type()

PRISM_EXPORTED_FUNCTION void PRISM_EXPORTED_FUNCTION const char * pm_node_type ( pm_node_type_t  node_type)

Returns a string representation of the given node type.

Parameters
node_typeThe node type to convert to a string.
Returns
A string representation of the given node type.

Definition at line 98 of file node.c.

◆ pm_visit_child_nodes()

PRISM_EXPORTED_FUNCTION bool pm_visit_child_nodes ( const pm_node_t *  node,
pm_visit_callback_t  visitor,
void *  data 
)

Visit the children of the given node with the given callback.

This is the default behavior for walking the tree that is called from pm_visit_node if the callback returns PM_VISIT_DESCEND.

Parameters
nodeThe node to visit the children of.
visitorThe callback to call for each child node.
dataAn opaque pointer that is passed to the visitor callback.
Returns
false if the visitor stopped the walk by returning PM_VISIT_STOP, true otherwise.

This is the default behavior for walking the tree that is called from pm_visit_node if the callback returns PM_VISIT_DESCEND. A stop from any child returns false immediately, so that it propagates up through every enclosing call.

Definition at line 430 of file node.c.

◆ pm_visit_node()

PRISM_EXPORTED_FUNCTION bool pm_visit_node ( const pm_node_t *  node,
pm_visit_callback_t  visitor,
void *  data 
)

Visit each of the nodes in this subtree using the given visitor callback.

The callback function will be called for each node in the subtree, and its return value determines whether the walk visits that node's children, skips them, or stops entirely. The data parameter is treated as an opaque pointer and is passed to the visitor callback for consumers to use as they see fit.

As an example:

#include "prism.h"
pm_visit_result_t visit(const pm_node_t *node, void *data) {
size_t *indent = (size_t *) data;
for (size_t i = 0; i < *indent * 2; i++) putc(' ', stdout);
printf("%s\n", pm_node_type(node->type));
size_t next_indent = *indent + 1;
size_t *next_data = &next_indent;
pm_visit_child_nodes(node, visit, next_data);
return PM_VISIT_SKIP;
}
int main(void) {
const char *source = "1 + 2; 3 + 4";
size_t size = strlen(source);
pm_arena_t *arena = pm_arena_new();
pm_options_t *options = pm_options_new();
pm_parser_t *parser = pm_parser_new(arena, (const uint8_t *) source, size, options);
size_t indent = 0;
pm_node_t *node = pm_parse(parser);
size_t *data = &indent;
pm_visit_node(node, visit, data);
pm_parser_free(parser);
pm_options_free(options);
pm_arena_free(arena);
return EXIT_SUCCESS;
}
PRISM_NODISCARD PRISM_EXPORTED_FUNCTION pm_parser_t * pm_parser_new(pm_arena_t *arena, const uint8_t *source, size_t size, const pm_options_t *options) PRISM_NONNULL(1)
Allocate and initialize a parser with the given start and end pointers.
Definition prism.c:23541
PRISM_EXPORTED_FUNCTION void pm_parser_free(pm_parser_t *parser) PRISM_NONNULL(1)
Free both the memory held by the given parser and the parser itself.
Definition prism.c:23562
PRISM_EXPORTED_FUNCTION pm_node_t * pm_parse(pm_parser_t *parser) PRISM_NONNULL(1)
Initiate the parser with the given parser.
Definition prism.c:23733
pm_visit_result_t
The value returned by a visitor callback, which determines where the walk goes next.
Definition node.h:49
@ PM_VISIT_SKIP
Continue the walk without visiting this node's children.
Definition node.h:51
The main header file for the prism parser.
This is the base structure that represents a node in the syntax tree.
Definition ast.h:1090
pm_node_type_t type
This represents the type of the node.
Definition ast.h:1095
Parameters
nodeThe root node to start visiting from.
visitorThe callback to call for each node in the subtree.
dataAn opaque pointer that is passed to the visitor callback.
Returns
false if the visitor stopped the walk by returning PM_VISIT_STOP, true otherwise.

The callback function will be called for each node in the subtree, and its return value determines whether the walk visits that node's children, skips them, or stops entirely. The data parameter is treated as an opaque pointer and is passed to the visitor callback for consumers to use as they see fit.

Definition at line 417 of file node.c.