Ruby  3.4.0dev (2024-12-06 revision 892c46283a5ea4179500d951c9d4866c0051f27b)
Data Structures | Functions
pm_newline_list.h File Reference

(892c46283a5ea4179500d951c9d4866c0051f27b)

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_newline_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_newline_list_t
 A list of offsets of newlines in a string. More...
 
struct  pm_line_column_t
 A line and column in a string. More...
 

Functions

bool pm_newline_list_init (pm_newline_list_t *list, const uint8_t *start, size_t capacity)
 Initialize a new newline list with the given capacity. More...
 
void pm_newline_list_clear (pm_newline_list_t *list)
 Clear out the newlines that have been appended to the list. More...
 
bool pm_newline_list_append (pm_newline_list_t *list, const uint8_t *cursor)
 Append a new offset to the newline list. More...
 
int32_t pm_newline_list_line (const pm_newline_list_t *list, const uint8_t *cursor, int32_t start_line)
 Returns the line of the given offset. More...
 
pm_line_column_t pm_newline_list_line_column (const pm_newline_list_t *list, const uint8_t *cursor, int32_t start_line)
 Returns the line and column of the given offset. More...
 
void pm_newline_list_free (pm_newline_list_t *list)
 Free the internal memory allocated for the newline list. More...
 

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_newline_list.h.

Function Documentation

◆ pm_newline_list_append()

bool pm_newline_list_append ( pm_newline_list_t list,
const uint8_t *  cursor 
)

Append a new offset to the newline list.

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

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

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

Definition at line 35 of file pm_newline_list.c.

Referenced by pm_strspn_whitespace_newlines().

◆ pm_newline_list_clear()

void pm_newline_list_clear ( pm_newline_list_t list)

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

Parameters
listThe list to clear.

Definition at line 26 of file pm_newline_list.c.

◆ pm_newline_list_free()

void pm_newline_list_free ( pm_newline_list_t list)

Free the internal memory allocated for the newline list.

Parameters
listThe list to free.

Definition at line 123 of file pm_newline_list.c.

Referenced by pm_parser_free().

◆ pm_newline_list_init()

bool pm_newline_list_init ( pm_newline_list_t list,
const uint8_t *  start,
size_t  capacity 
)

Initialize a new newline list with the given capacity.

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

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

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

Definition at line 8 of file pm_newline_list.c.

◆ pm_newline_list_line()

int32_t pm_newline_list_line ( const pm_newline_list_t list,
const uint8_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.
cursorA pointer to the 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 62 of file pm_newline_list.c.

◆ pm_newline_list_line_column()

pm_line_column_t pm_newline_list_line_column ( const pm_newline_list_t list,
const uint8_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.
cursorA pointer to the 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 92 of file pm_newline_list.c.