Ruby: prism/node.h File Reference

Ruby 3.4.3p32 (2025-04-14 revision d0b7e5b6a04bde21ca483d20a1546b28b401c2d4)

Macros | Functions
node.h File Reference

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

`#include "prism/defines.h"`
`#include "prism/parser.h"`
`#include "prism/util/pm_buffer.h"`

Go to the source code of this file.

## Macros
#define | PM_NODE_LIST_FOREACH(list, index, node)
| Loop through each node in the node list, writing each node to the given pm_node_t pointer.

## Functions
void | pm_node_list_append (pm_node_list_t *list, pm_node_t *node)
| Append a new node onto the end of the node list.
void | pm_node_list_prepend (pm_node_list_t *list, pm_node_t *node)
| Prepend a new node onto the beginning of the node list.
void | pm_node_list_concat (pm_node_list_t *list, pm_node_list_t *other)
| Concatenate the given node list onto the end of the other node list.
void | pm_node_list_free (pm_node_list_t *list)
| Free the internal memory associated with the given node list.
PRISM_EXPORTED_FUNCTION void | pm_node_destroy (pm_parser_t *parser, struct pm_node *node)
| Deallocate a node and all of its children.
PRISM_EXPORTED_FUNCTION const char * | pm_node_type_to_str (pm_node_type_t node_type)
| Returns a string representation of the given node type.
PRISM_EXPORTED_FUNCTION void | pm_visit_node (const pm_node_t *node, bool(*visitor)(const pm_node_t *node, void *data), void *data)
| Visit each of the nodes in this subtree using the given visitor callback.
PRISM_EXPORTED_FUNCTION void | pm_visit_child_nodes (const pm_node_t *node, bool(*visitor)(const pm_node_t *node, void *data), void *data)
| 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 )

Value:
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 17 of file node.h.

## Function Documentation

## ◆ pm_node_destroy()

PRISM_EXPORTED_FUNCTION void pm_node_destroy | ( | pm_parser_t * | parser,
|  | pm_node_t * | node )

Deallocate a node and all of its children.

Parameters

  parser | The parser that owns the node.
  node | The node to deallocate.

Deallocate a node and all of its children.

Similarly to pm_node_alloc, we're not using the parser argument, but it's there to allow for the future possibility of pre-allocating larger memory pools.

Definition at line 114 of file node.c.

## ◆ pm_node_list_append()

void pm_node_list_append | ( | pm_node_list_t * | list,
|  | pm_node_t * | node )

Append a new node onto the end of the node list.

Parameters

  list | The list to append to.
  node | The node to append.

Definition at line 55 of file node.c.

## ◆ pm_node_list_concat()

void pm_node_list_concat | ( | pm_node_list_t * | list,
|  | pm_node_list_t * | other )

Concatenate the given node list onto the end of the other node list.

Parameters

  list | The list to concatenate onto.
  other | The list to concatenate.

Definition at line 77 of file node.c.

## ◆ pm_node_list_free()

void pm_node_list_free | ( | pm_node_list_t * | list | ) |

Free the internal memory associated with the given node list.

Parameters

  list | The list to free.

Definition at line 88 of file node.c.

## ◆ pm_node_list_prepend()

void pm_node_list_prepend | ( | pm_node_list_t * | list,
|  | pm_node_t * | node )

Prepend a new node onto the beginning of the node list.

Parameters

  list | The list to prepend to.
  node | The node to prepend.

Definition at line 65 of file node.c.

## ◆ pm_node_type_to_str()

PRISM_EXPORTED_FUNCTION const char * pm_node_type_to_str | ( | pm_node_type_t | node_type | ) |

Returns a string representation of the given node type.

Parameters

  node_type | The node type to convert to a string.

Returns
  A string representation of the given node type.

Definition at line 1216 of file node.c.

## ◆ pm_visit_child_nodes()

PRISM_EXPORTED_FUNCTION void pm_visit_child_nodes | ( | const pm_node_t * | node,
|  | bool(* | visitor )(const pm_node_t *node, void *data),
|  | 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 true.

Parameters

  node | The node to visit the children of.
  visitor | The callback to call for each child node.
  data | An opaque pointer that is passed to the visitor callback.

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

Definition at line 1544 of file node.c.

## ◆ pm_visit_node()

PRISM_EXPORTED_FUNCTION void pm_visit_node | ( | const pm_node_t * | node,
|  | bool(* | visitor )(const pm_node_t *node, void *data),
|  | 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. If it returns false, then that node's children will not be visited. If it returns true, then the children will be visited. 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"
bool 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_to_str(node->type));
size_t next_indent = *indent + 1;
size_t *next_data = &next_indent;
pm_visit_child_nodes(node, visit, next_data);
return false;
}
int main(void) {
const char *source = "1 + 2; 3 + 4";
size_t size = strlen(source);
pm_parser_t parser;
pm_options_t options = { 0 };
pm_parser_init(&parser, (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_node_destroy(&parser, node);
pm_parser_free(&parser);
return EXIT_SUCCESS;
}
pm_node_t
struct pm_node pm_node_t
This is the base structure that represents a node in the syntax tree.
pm_options_t
struct pm_options pm_options_t
The options that can be passed to the parser.
pm_parser_t
struct pm_parser pm_parser_t
The parser used to parse Ruby source.
Definition parser.h:267
pm_visit_child_nodes
PRISM_EXPORTED_FUNCTION void pm_visit_child_nodes(const pm_node_t *node, bool(*visitor)(const pm_node_t *node, void *data), void *data)
Visit the children of the given node with the given callback.
Definition node.c:1544
pm_node_type_to_str
PRISM_EXPORTED_FUNCTION const char * pm_node_type_to_str(pm_node_type_t node_type)
Returns a string representation of the given node type.
Definition node.c:1216
prism.h
The main header file for the prism parser.
pm_node::type
pm_node_type_t type
This represents the type of the node.
Definition ast.h:1074

Parameters

  node | The root node to start visiting from.
  visitor | The callback to call for each node in the subtree.
  data | An opaque pointer that is passed to the visitor callback.

The callback function will be called for each node in the subtree. If it returns false, then that node's children will not be visited. If it returns true, then the children will be visited. 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 1534 of file node.c.