Skip to content
Open
6 changes: 3 additions & 3 deletions ext/prism/extension.c
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ parse_lex_input(pm_string_t *input, const pm_options_t *options, bool return_nod
pm_parser_register_encoding_changed_callback(&parser, parse_lex_encoding_changed_callback);

VALUE source_string = rb_str_new((const char *) pm_string_source(input), pm_string_length(input));
VALUE offsets = rb_ary_new_capa(parser.newline_list.size);
VALUE offsets = rb_ary_new_capa(parser.line_offsets.size);
VALUE source = rb_funcall(rb_cPrismSource, rb_id_source_for, 3, source_string, LONG2NUM(parser.start_line), offsets);

parse_lex_data_t parse_lex_data = {
Expand All @@ -765,8 +765,8 @@ parse_lex_input(pm_string_t *input, const pm_options_t *options, bool return_nod
rb_encoding *encoding = rb_enc_find(parser.encoding->name);
rb_enc_associate(source_string, encoding);

for (size_t index = 0; index < parser.newline_list.size; index++) {
rb_ary_push(offsets, ULONG2NUM(parser.newline_list.offsets[index]));
for (size_t index = 0; index < parser.line_offsets.size; index++) {
rb_ary_push(offsets, ULONG2NUM(parser.line_offsets.offsets[index]));
}

if (options->freeze) {
Expand Down
6 changes: 3 additions & 3 deletions include/prism/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "prism/static_literals.h"
#include "prism/util/pm_constant_pool.h"
#include "prism/util/pm_list.h"
#include "prism/util/pm_newline_list.h"
#include "prism/util/pm_line_offset_list.h"
#include "prism/util/pm_string.h"

#include <stdbool.h>
Expand Down Expand Up @@ -782,8 +782,8 @@ struct pm_parser {
*/
pm_constant_pool_t constant_pool;

/** This is the list of newline offsets in the source file. */
pm_newline_list_t newline_list;
/** This is the list of line offsets in the source file. */
pm_line_offset_list_t line_offsets;

/**
* We want to add a flag to integer nodes that indicates their base. We only
Expand Down
10 changes: 5 additions & 5 deletions include/prism/static_literals.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include "prism/defines.h"
#include "prism/ast.h"
#include "prism/util/pm_newline_list.h"
#include "prism/util/pm_line_offset_list.h"

#include <assert.h>
#include <stdbool.h>
Expand Down Expand Up @@ -91,15 +91,15 @@ typedef struct {
/**
* Add a node to the set of static literals.
*
* @param newline_list The list of newline offsets to use to calculate lines.
* @param line_offsets The list of newline offsets to use to calculate lines.
* @param start The start of the source being parsed.
* @param start_line The line number that the parser starts on.
* @param literals The set of static literals to add the node to.
* @param node The node to add to the set.
* @param replace Whether to replace the previous node if one already exists.
* @return A pointer to the node that is being overwritten, if there is one.
*/
pm_node_t * pm_static_literals_add(const pm_newline_list_t *newline_list, const uint8_t *start, int32_t start_line, pm_static_literals_t *literals, pm_node_t *node, bool replace);
pm_node_t * pm_static_literals_add(const pm_line_offset_list_t *line_offsets, const uint8_t *start, int32_t start_line, pm_static_literals_t *literals, pm_node_t *node, bool replace);

/**
* Free the internal memory associated with the given static literals set.
Expand All @@ -112,12 +112,12 @@ void pm_static_literals_free(pm_static_literals_t *literals);
* Create a string-based representation of the given static literal.
*
* @param buffer The buffer to write the string to.
* @param newline_list The list of newline offsets to use to calculate lines.
* @param line_offsets The list of newline offsets to use to calculate lines.
* @param start The start of the source being parsed.
* @param start_line The line number that the parser starts on.
* @param encoding_name The name of the encoding of the source being parsed.
* @param node The node to create a string representation of.
*/
void pm_static_literal_inspect(pm_buffer_t *buffer, const pm_newline_list_t *newline_list, const uint8_t *start, int32_t start_line, const char *encoding_name, const pm_node_t *node);
void pm_static_literal_inspect(pm_buffer_t *buffer, const pm_line_offset_list_t *line_offsets, const uint8_t *start, int32_t start_line, const char *encoding_name, const pm_node_t *node);

#endif
6 changes: 3 additions & 3 deletions include/prism/util/pm_char.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#define PRISM_CHAR_H

#include "prism/defines.h"
#include "prism/util/pm_newline_list.h"
#include "prism/util/pm_line_offset_list.h"

#include <stdbool.h>
#include <stddef.h>
Expand All @@ -30,13 +30,13 @@ size_t pm_strspn_whitespace(const uint8_t *string, ptrdiff_t length);
*
* @param string The string to search.
* @param length The maximum number of characters to search.
* @param newline_list The list of newlines to populate.
* @param line_offsets The list of newlines to populate.
* @param start_offset The offset at which the string occurs in the source, for
* the purpose of tracking newlines.
* @return The number of characters at the start of the string that are
* whitespace.
*/
size_t pm_strspn_whitespace_newlines(const uint8_t *string, ptrdiff_t length, pm_newline_list_t *newline_list, uint32_t start_offset);
size_t pm_strspn_whitespace_newlines(const uint8_t *string, ptrdiff_t length, pm_line_offset_list_t *line_offsets, uint32_t start_offset);

/**
* Returns the number of characters at the start of the string that are inline
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @file pm_newline_list.h
* @file pm_line_offset_list.h
*
* A list of byte offsets of newlines in a string.
*
Expand All @@ -11,8 +11,8 @@
* end column on every node in addition to the offsets that we already store,
* but that would be quite a lot of memory overhead.
*/
#ifndef PRISM_NEWLINE_LIST_H
#define PRISM_NEWLINE_LIST_H
#ifndef PRISM_LINE_OFFSET_LIST_H
#define PRISM_LINE_OFFSET_LIST_H

#include "prism/defines.h"

Expand All @@ -22,8 +22,8 @@
#include <stdlib.h>

/**
* A list of offsets of newlines in a string. The offsets are assumed to be
* sorted/inserted in ascending order.
* A list of offsets of the start of lines in a string. The offsets are assumed
* to be sorted/inserted in ascending order.
*/
typedef struct {
/** The number of offsets in the list. */
Expand All @@ -34,7 +34,7 @@ typedef struct {

/** The list of offsets. */
uint32_t *offsets;
} pm_newline_list_t;
} pm_line_offset_list_t;

/**
* A line and column in a string.
Expand All @@ -48,32 +48,32 @@ typedef struct {
} pm_line_column_t;

/**
* Initialize a new newline list with the given capacity. Returns true if the
* allocation of the offsets succeeds, otherwise returns false.
* Initialize a new line offset list with the given capacity. Returns true if
* the allocation of the offsets succeeds, otherwise returns false.
*
* @param list The list to initialize.
* @param capacity The initial capacity of the list.
* @return True if the allocation of the offsets succeeds, otherwise false.
*/
bool pm_newline_list_init(pm_newline_list_t *list, size_t capacity);
bool pm_line_offset_list_init(pm_line_offset_list_t *list, size_t capacity);

/**
* Clear out the newlines that have been appended to the list.
* Clear out the offsets that have been appended to the list.
*
* @param list The list to clear.
*/
void pm_newline_list_clear(pm_newline_list_t *list);
void pm_line_offset_list_clear(pm_line_offset_list_t *list);

/**
* Append a new offset to the newline list. Returns true if the reallocation of
* the offsets succeeds (if one was necessary), otherwise returns false.
* Append a new offset to the list. Returns true if the reallocation of the
* offsets succeeds (if one was necessary), otherwise returns false.
*
* @param list The list to append to.
* @param cursor The offset to append.
* @return True if the reallocation of the offsets succeeds (if one was
* necessary), otherwise false.
*/
bool pm_newline_list_append(pm_newline_list_t *list, uint32_t cursor);
bool pm_line_offset_list_append(pm_line_offset_list_t *list, uint32_t cursor);

/**
* Returns the line of the given offset. If the offset is not in the list, the
Expand All @@ -84,7 +84,7 @@ bool pm_newline_list_append(pm_newline_list_t *list, uint32_t cursor);
* @param start_line The line to start counting from.
* @return The line of the given offset.
*/
int32_t pm_newline_list_line(const pm_newline_list_t *list, uint32_t cursor, int32_t start_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 and column of the given offset. If the offset is not in the
Expand All @@ -96,13 +96,13 @@ int32_t pm_newline_list_line(const pm_newline_list_t *list, uint32_t cursor, int
* @param start_line The line to start counting from.
* @return The line and column of the given offset.
*/
pm_line_column_t pm_newline_list_line_column(const pm_newline_list_t *list, uint32_t cursor, int32_t start_line);
pm_line_column_t pm_line_offset_list_line_column(const pm_line_offset_list_t *list, uint32_t cursor, int32_t start_line);

/**
* Free the internal memory allocated for the newline list.
* Free the internal memory allocated for the list.
*
* @param list The list to free.
*/
void pm_newline_list_free(pm_newline_list_t *list);
void pm_line_offset_list_free(pm_line_offset_list_t *list);

#endif
4 changes: 2 additions & 2 deletions prism.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Gem::Specification.new do |spec|
"include/prism/util/pm_integer.h",
"include/prism/util/pm_list.h",
"include/prism/util/pm_memchr.h",
"include/prism/util/pm_newline_list.h",
"include/prism/util/pm_line_offset_list.h",
"include/prism/util/pm_strncasecmp.h",
"include/prism/util/pm_string.h",
"include/prism/util/pm_strpbrk.h",
Expand Down Expand Up @@ -154,7 +154,7 @@ Gem::Specification.new do |spec|
"src/util/pm_integer.c",
"src/util/pm_list.c",
"src/util/pm_memchr.c",
"src/util/pm_newline_list.c",
"src/util/pm_line_offset_list.c",
"src/util/pm_string.c",
"src/util/pm_strncasecmp.c",
"src/util/pm_strpbrk.c"
Expand Down
Loading
Loading