Ruby 4.1.0dev (2026-03-03 revision 726205b354d1068147719fb42e1de743f1838ef1)
Data Structures | Functions
pm_line_offset_list.h File Reference

(726205b354d1068147719fb42e1de743f1838ef1)

A list of byte offsets of newlines in a string. More...

#include "prism/defines.h"
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
Include dependency graph for pm_line_offset_list.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  pm_line_offset_list_t
 A list of offsets of the start of lines in a string. More...
 
struct  pm_line_column_t
 A line and column in a string. More...
 

Functions

bool pm_line_offset_list_init (pm_line_offset_list_t *list, size_t capacity)
 Initialize a new line offset list with the given capacity.
 
void pm_line_offset_list_clear (pm_line_offset_list_t *list)
 Clear out the offsets that have been appended to the list.
 
bool pm_line_offset_list_append (pm_line_offset_list_t *list, uint32_t cursor)
 Append a new offset to the list.
 
int32_t pm_line_offset_list_line (const pm_line_offset_list_t *list, uint32_t cursor, int32_t start_line)
 Returns the line of the given offset.
 
pm_line_column_t pm_line_offset_list_line_column (const pm_line_offset_list_t *list, uint32_t cursor, int32_t start_line)
 Returns the line and column of the given offset.
 
void pm_line_offset_list_free (pm_line_offset_list_t *list)
 Free the internal memory allocated for the list.
 

Detailed Description

A list of byte offsets of newlines in a string.

When compiling the syntax tree, it's necessary to know the line and column of many nodes. This is necessary to support things like error messages, tracepoints, etc.

It's possible that we could store the start line, start column, end line, and end column on every node in addition to the offsets that we already store, but that would be quite a lot of memory overhead.

Definition in file pm_line_offset_list.h.

Function Documentation

◆ pm_line_offset_list_append()

bool pm_line_offset_list_append ( pm_line_offset_list_t list,
uint32_t  cursor 
)

Append a new offset to the list.

Returns true if the reallocation of the offsets succeeds (if one was necessary), otherwise returns false.

Parameters
listThe list to append to.
cursorThe offset to append.
Returns
True if the reallocation of the offsets succeeds (if one was necessary), otherwise false.

Append a new offset to the list.

Returns true if the reallocation of the offsets succeeds (if one was necessary), otherwise returns false.

Definition at line 33 of file pm_line_offset_list.c.

◆ pm_line_offset_list_clear()

void pm_line_offset_list_clear ( pm_line_offset_list_t list)

Clear out the offsets that have been appended to the list.

Parameters
listThe list to clear.

Clear out the offsets that have been appended to the list.

Definition at line 24 of file pm_line_offset_list.c.

◆ pm_line_offset_list_free()

void pm_line_offset_list_free ( pm_line_offset_list_t list)

Free the internal memory allocated for the list.

Parameters
listThe list to free.

Free the internal memory allocated for the list.

Definition at line 111 of file pm_line_offset_list.c.

◆ pm_line_offset_list_init()

bool pm_line_offset_list_init ( pm_line_offset_list_t list,
size_t  capacity 
)

Initialize a new line offset list with the given capacity.

Returns true if the allocation of the offsets succeeds, otherwise returns false.

Parameters
listThe list to initialize.
capacityThe initial capacity of the list.
Returns
True if the allocation of the offsets succeeds, otherwise false.

Initialize a new line offset list with the given capacity.

Returns true if the allocation of the offsets succeeds, otherwise returns false.

Definition at line 8 of file pm_line_offset_list.c.

◆ pm_line_offset_list_line()

int32_t pm_line_offset_list_line ( const pm_line_offset_list_t list,
uint32_t  cursor,
int32_t  start_line 
)

Returns the line of the given offset.

If the offset is not in the list, the line of the closest offset less than the given offset is returned.

Parameters
listThe list to search.
cursorThe offset to search for.
start_lineThe line to start counting from.
Returns
The line of the given offset.

If the offset is not in the list, the line of the closest offset less than the given offset is returned.

Definition at line 56 of file pm_line_offset_list.c.

◆ pm_line_offset_list_line_column()

pm_line_column_t pm_line_offset_list_line_column ( const pm_line_offset_list_t list,
uint32_t  cursor,
int32_t  start_line 
)

Returns the line and column of the given offset.

If the offset is not in the list, the line and column of the closest offset less than the given offset are returned.

Parameters
listThe list to search.
cursorThe offset to search for.
start_lineThe line to start counting from.
Returns
The line and column of the given offset.

If the offset is not in the list, the line and column of the closest offset less than the given offset are returned.

Definition at line 83 of file pm_line_offset_list.c.