diff --git a/ext/prism/extension.c b/ext/prism/extension.c index 7a5dd8a984..9b3a66caf5 100644 --- a/ext/prism/extension.c +++ b/ext/prism/extension.c @@ -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 = { @@ -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) { diff --git a/include/prism/parser.h b/include/prism/parser.h index a8d840d3bf..b4e3038439 100644 --- a/include/prism/parser.h +++ b/include/prism/parser.h @@ -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 @@ -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 diff --git a/include/prism/static_literals.h b/include/prism/static_literals.h index 0f8eb43bfa..6d73e5d04f 100644 --- a/include/prism/static_literals.h +++ b/include/prism/static_literals.h @@ -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 #include @@ -91,7 +91,7 @@ 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. @@ -99,7 +99,7 @@ typedef struct { * @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. @@ -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 diff --git a/include/prism/util/pm_char.h b/include/prism/util/pm_char.h index b213e8edee..ab1f513a66 100644 --- a/include/prism/util/pm_char.h +++ b/include/prism/util/pm_char.h @@ -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 #include @@ -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 diff --git a/include/prism/util/pm_newline_list.h b/include/prism/util/pm_line_offset_list.h similarity index 65% rename from include/prism/util/pm_newline_list.h rename to include/prism/util/pm_line_offset_list.h index dd3e625089..fe16e0b034 100644 --- a/include/prism/util/pm_newline_list.h +++ b/include/prism/util/pm_line_offset_list.h @@ -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. * @@ -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" @@ -22,8 +22,8 @@ #include /** - * 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. */ @@ -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. @@ -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 @@ -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 @@ -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 diff --git a/prism.gemspec b/prism.gemspec index c5c315fc8d..d7f31716ce 100644 --- a/prism.gemspec +++ b/prism.gemspec @@ -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", @@ -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" diff --git a/src/prism.c b/src/prism.c index ca0f2a55aa..c9d002b52a 100644 --- a/src/prism.c +++ b/src/prism.c @@ -1000,7 +1000,7 @@ pm_locals_order(PRISM_ATTRIBUTE_UNUSED pm_parser_t *parser, pm_locals_t *locals, if (local->name != PM_CONSTANT_ID_UNSET) { pm_constant_id_list_insert(list, (size_t) local->index, local->name); - if (warn_unused && local->reads == 0 && ((parser->start_line >= 0) || (pm_newline_list_line(&parser->newline_list, local->location.start, parser->start_line) >= 0))) { + if (warn_unused && local->reads == 0 && ((parser->start_line >= 0) || (pm_line_offset_list_line(&parser->line_offsets, local->location.start, parser->start_line) >= 0))) { pm_constant_t *constant = pm_constant_pool_id_to_constant(&parser->constant_pool, local->name); if (constant->length >= 1 && *constant->start != '_') { @@ -9367,7 +9367,7 @@ lex_embdoc(pm_parser_t *parser) { if (newline == NULL) { parser->current.end = parser->end; } else { - pm_newline_list_append(&parser->newline_list, U32(newline - parser->start + 1)); + pm_line_offset_list_append(&parser->line_offsets, U32(newline - parser->start + 1)); parser->current.end = newline + 1; } @@ -9401,7 +9401,7 @@ lex_embdoc(pm_parser_t *parser) { if (newline == NULL) { parser->current.end = parser->end; } else { - pm_newline_list_append(&parser->newline_list, U32(newline - parser->start + 1)); + pm_line_offset_list_append(&parser->line_offsets, U32(newline - parser->start + 1)); parser->current.end = newline + 1; } @@ -9421,7 +9421,7 @@ lex_embdoc(pm_parser_t *parser) { if (newline == NULL) { parser->current.end = parser->end; } else { - pm_newline_list_append(&parser->newline_list, U32(newline - parser->start + 1)); + pm_line_offset_list_append(&parser->line_offsets, U32(newline - parser->start + 1)); parser->current.end = newline + 1; } @@ -9735,7 +9735,7 @@ pm_lex_percent_delimiter(pm_parser_t *parser) { parser_flush_heredoc_end(parser); } else { // Otherwise, we'll add the newline to the list of newlines. - pm_newline_list_append(&parser->newline_list, PM_TOKEN_END(parser, &parser->current) + U32(eol_length)); + pm_line_offset_list_append(&parser->line_offsets, PM_TOKEN_END(parser, &parser->current) + U32(eol_length)); } uint8_t delimiter = *parser->current.end; @@ -9839,7 +9839,7 @@ parser_lex(pm_parser_t *parser) { parser->heredoc_end = NULL; } else { parser->current.end += eol_length + 1; - pm_newline_list_append(&parser->newline_list, PM_TOKEN_END(parser, &parser->current)); + pm_line_offset_list_append(&parser->line_offsets, PM_TOKEN_END(parser, &parser->current)); space_seen = true; } } else if (pm_char_is_inline_whitespace(*parser->current.end)) { @@ -9941,7 +9941,7 @@ parser_lex(pm_parser_t *parser) { } if (parser->heredoc_end == NULL) { - pm_newline_list_append(&parser->newline_list, PM_TOKEN_END(parser, &parser->current)); + pm_line_offset_list_append(&parser->line_offsets, PM_TOKEN_END(parser, &parser->current)); } } @@ -10450,7 +10450,7 @@ parser_lex(pm_parser_t *parser) { } else { // Otherwise, we want to indicate that the body of the // heredoc starts on the character after the next newline. - pm_newline_list_append(&parser->newline_list, U32(body_start - parser->start + 1)); + pm_line_offset_list_append(&parser->line_offsets, U32(body_start - parser->start + 1)); body_start++; } @@ -11091,7 +11091,7 @@ parser_lex(pm_parser_t *parser) { // correct column information for it. const uint8_t *cursor = parser->current.end; while ((cursor = next_newline(cursor, parser->end - cursor)) != NULL) { - pm_newline_list_append(&parser->newline_list, U32(++cursor - parser->start)); + pm_line_offset_list_append(&parser->line_offsets, U32(++cursor - parser->start)); } parser->current.end = parser->end; @@ -11152,7 +11152,7 @@ parser_lex(pm_parser_t *parser) { whitespace += 1; } } else { - whitespace = pm_strspn_whitespace_newlines(parser->current.end, parser->end - parser->current.end, &parser->newline_list, PM_TOKEN_END(parser, &parser->current)); + whitespace = pm_strspn_whitespace_newlines(parser->current.end, parser->end - parser->current.end, &parser->line_offsets, PM_TOKEN_END(parser, &parser->current)); } if (whitespace > 0) { @@ -11267,7 +11267,7 @@ parser_lex(pm_parser_t *parser) { LEX(PM_TOKEN_STRING_CONTENT); } else { // ... else track the newline. - pm_newline_list_append(&parser->newline_list, PM_TOKEN_END(parser, &parser->current) + 1); + pm_line_offset_list_append(&parser->line_offsets, PM_TOKEN_END(parser, &parser->current) + 1); } parser->current.end++; @@ -11405,7 +11405,7 @@ parser_lex(pm_parser_t *parser) { // would have already have added the newline to the // list. if (parser->heredoc_end == NULL) { - pm_newline_list_append(&parser->newline_list, PM_TOKEN_END(parser, &parser->current)); + pm_line_offset_list_append(&parser->line_offsets, PM_TOKEN_END(parser, &parser->current)); } } else { parser->current.end = breakpoint + 1; @@ -11452,7 +11452,7 @@ parser_lex(pm_parser_t *parser) { // If we've hit a newline, then we need to track that in // the list of newlines. if (parser->heredoc_end == NULL) { - pm_newline_list_append(&parser->newline_list, U32(breakpoint - parser->start + 1)); + pm_line_offset_list_append(&parser->line_offsets, U32(breakpoint - parser->start + 1)); parser->current.end = breakpoint + 1; breakpoint = pm_strpbrk(parser, parser->current.end, breakpoints, parser->end - parser->current.end, false); break; @@ -11500,7 +11500,7 @@ parser_lex(pm_parser_t *parser) { LEX(PM_TOKEN_STRING_CONTENT); } else { // ... else track the newline. - pm_newline_list_append(&parser->newline_list, PM_TOKEN_END(parser, &parser->current) + 1); + pm_line_offset_list_append(&parser->line_offsets, PM_TOKEN_END(parser, &parser->current) + 1); } parser->current.end++; @@ -11665,7 +11665,7 @@ parser_lex(pm_parser_t *parser) { // would have already have added the newline to the // list. if (parser->heredoc_end == NULL) { - pm_newline_list_append(&parser->newline_list, PM_TOKEN_END(parser, &parser->current)); + pm_line_offset_list_append(&parser->line_offsets, PM_TOKEN_END(parser, &parser->current)); } } else { parser->current.end = breakpoint + 1; @@ -11717,7 +11717,7 @@ parser_lex(pm_parser_t *parser) { // for the terminator in case the terminator is a // newline character. if (parser->heredoc_end == NULL) { - pm_newline_list_append(&parser->newline_list, U32(breakpoint - parser->start + 1)); + pm_line_offset_list_append(&parser->line_offsets, U32(breakpoint - parser->start + 1)); parser->current.end = breakpoint + 1; breakpoint = pm_strpbrk(parser, parser->current.end, breakpoints, parser->end - parser->current.end, true); break; @@ -11771,7 +11771,7 @@ parser_lex(pm_parser_t *parser) { LEX(PM_TOKEN_STRING_CONTENT); } else { // ... else track the newline. - pm_newline_list_append(&parser->newline_list, PM_TOKEN_END(parser, &parser->current) + 1); + pm_line_offset_list_append(&parser->line_offsets, PM_TOKEN_END(parser, &parser->current) + 1); } parser->current.end++; @@ -11900,7 +11900,7 @@ parser_lex(pm_parser_t *parser) { (memcmp(terminator_start, ident_start, ident_length) == 0) ) { if (newline != NULL) { - pm_newline_list_append(&parser->newline_list, U32(newline - parser->start + 1)); + pm_line_offset_list_append(&parser->line_offsets, U32(newline - parser->start + 1)); } parser->current.end = terminator_end; @@ -11972,7 +11972,7 @@ parser_lex(pm_parser_t *parser) { LEX(PM_TOKEN_STRING_CONTENT); } - pm_newline_list_append(&parser->newline_list, U32(breakpoint - parser->start + 1)); + pm_line_offset_list_append(&parser->line_offsets, U32(breakpoint - parser->start + 1)); // If we have a - or ~ heredoc, then we can match after // some leading whitespace. @@ -12092,7 +12092,7 @@ parser_lex(pm_parser_t *parser) { const uint8_t *end = parser->current.end; if (parser->heredoc_end == NULL) { - pm_newline_list_append(&parser->newline_list, U32(end - parser->start + 1)); + pm_line_offset_list_append(&parser->line_offsets, U32(end - parser->start + 1)); } // Here we want the buffer to only @@ -13316,11 +13316,11 @@ parse_statements(pm_parser_t *parser, pm_context_t context, uint16_t depth) { */ static void pm_hash_key_static_literals_add(pm_parser_t *parser, pm_static_literals_t *literals, pm_node_t *node) { - const pm_node_t *duplicated = pm_static_literals_add(&parser->newline_list, parser->start, parser->start_line, literals, node, true); + const pm_node_t *duplicated = pm_static_literals_add(&parser->line_offsets, parser->start, parser->start_line, literals, node, true); if (duplicated != NULL) { pm_buffer_t buffer = { 0 }; - pm_static_literal_inspect(&buffer, &parser->newline_list, parser->start, parser->start_line, parser->encoding->name, duplicated); + pm_static_literal_inspect(&buffer, &parser->line_offsets, parser->start, parser->start_line, parser->encoding->name, duplicated); pm_diagnostic_list_append_format( &parser->warning_list, @@ -13329,7 +13329,7 @@ pm_hash_key_static_literals_add(pm_parser_t *parser, pm_static_literals_t *liter PM_WARN_DUPLICATED_HASH_KEY, (int) pm_buffer_length(&buffer), pm_buffer_value(&buffer), - pm_newline_list_line_column(&parser->newline_list, PM_NODE_START(node), parser->start_line).line + pm_line_offset_list_line_column(&parser->line_offsets, PM_NODE_START(node), parser->start_line).line ); pm_buffer_free(&buffer); @@ -13344,14 +13344,14 @@ static void pm_when_clause_static_literals_add(pm_parser_t *parser, pm_static_literals_t *literals, pm_node_t *node) { pm_node_t *previous; - if ((previous = pm_static_literals_add(&parser->newline_list, parser->start, parser->start_line, literals, node, false)) != NULL) { + if ((previous = pm_static_literals_add(&parser->line_offsets, parser->start, parser->start_line, literals, node, false)) != NULL) { pm_diagnostic_list_append_format( &parser->warning_list, PM_NODE_START(node), PM_NODE_LENGTH(node), PM_WARN_DUPLICATED_WHEN_CLAUSE, - pm_newline_list_line_column(&parser->newline_list, PM_NODE_START(node), parser->start_line).line, - pm_newline_list_line_column(&parser->newline_list, PM_NODE_START(previous), parser->start_line).line + pm_line_offset_list_line_column(&parser->line_offsets, PM_NODE_START(node), parser->start_line).line, + pm_line_offset_list_line_column(&parser->line_offsets, PM_NODE_START(previous), parser->start_line).line ); } } @@ -14293,13 +14293,13 @@ token_newline_index(const pm_parser_t *parser) { // This is the common case. In this case we can look at the previously // recorded newline in the newline list and subtract from the current // offset. - return parser->newline_list.size - 1; + return parser->line_offsets.size - 1; } else { // This is unlikely. This is the case that we have already parsed the // start of a heredoc, so we cannot rely on looking at the previous // offset of the newline list, and instead must go through the whole // process of a binary search for the line number. - return (size_t) pm_newline_list_line(&parser->newline_list, PM_TOKEN_START(parser, &parser->current), 0); + return (size_t) pm_line_offset_list_line(&parser->line_offsets, PM_TOKEN_START(parser, &parser->current), 0); } } @@ -14309,7 +14309,7 @@ token_newline_index(const pm_parser_t *parser) { */ static int64_t token_column(const pm_parser_t *parser, size_t newline_index, const pm_token_t *token, bool break_on_non_space) { - const uint8_t *cursor = parser->start + parser->newline_list.offsets[newline_index]; + const uint8_t *cursor = parser->start + parser->line_offsets.offsets[newline_index]; const uint8_t *end = token->start; // Skip over the BOM if it is present. @@ -16458,7 +16458,7 @@ parse_pattern_hash_implicit_value(pm_parser_t *parser, pm_constant_id_list_t *ca */ static void parse_pattern_hash_key(pm_parser_t *parser, pm_static_literals_t *keys, pm_node_t *node) { - if (pm_static_literals_add(&parser->newline_list, parser->start, parser->start_line, keys, node, true) != NULL) { + if (pm_static_literals_add(&parser->line_offsets, parser->start, parser->start_line, keys, node, true) != NULL) { pm_parser_err_node(parser, node, PM_ERR_PATTERN_HASH_KEY_DUPLICATE); } } @@ -21948,7 +21948,7 @@ pm_parser_init(pm_parser_t *parser, const uint8_t *source, size_t size, const pm .lex_callback = NULL, .filepath = { 0 }, .constant_pool = { 0 }, - .newline_list = { 0 }, + .line_offsets = { 0 }, .integer_base = 0, .current_string = PM_STRING_EMPTY, .start_line = 1, @@ -21990,7 +21990,7 @@ pm_parser_init(pm_parser_t *parser, const uint8_t *source, size_t size, const pm // guess at the number of newlines that we'll need based on the size of the // input. size_t newline_size = size / 22; - pm_newline_list_init(&parser->newline_list, newline_size < 4 ? 4 : newline_size); + pm_line_offset_list_init(&parser->line_offsets, newline_size < 4 ? 4 : newline_size); // If options were provided to this parse, establish them here. if (options != NULL) { @@ -22129,7 +22129,7 @@ pm_parser_init(pm_parser_t *parser, const uint8_t *source, size_t size, const pm const uint8_t *newline = next_newline(cursor, parser->end - cursor); while (newline != NULL) { - pm_newline_list_append(&parser->newline_list, U32(newline - parser->start + 1)); + pm_line_offset_list_append(&parser->line_offsets, U32(newline - parser->start + 1)); cursor = newline + 1; newline = next_newline(cursor, parser->end - cursor); @@ -22159,7 +22159,7 @@ pm_parser_init(pm_parser_t *parser, const uint8_t *source, size_t size, const pm parser->current = (pm_token_t) { .type = PM_TOKEN_EOF, .start = cursor, .end = cursor }; } else { pm_parser_err(parser, 0, 0, PM_ERR_SCRIPT_NOT_FOUND); - pm_newline_list_clear(&parser->newline_list); + pm_line_offset_list_clear(&parser->line_offsets); } } @@ -22219,7 +22219,7 @@ pm_parser_free(pm_parser_t *parser) { pm_comment_list_free(&parser->comment_list); pm_magic_comment_list_free(&parser->magic_comment_list); pm_constant_pool_free(&parser->constant_pool); - pm_newline_list_free(&parser->newline_list); + pm_line_offset_list_free(&parser->line_offsets); while (parser->current_scope != NULL) { // Normally, popping the scope doesn't free the locals since it is diff --git a/src/static_literals.c b/src/static_literals.c index 13a52378dd..58a08a8c03 100644 --- a/src/static_literals.c +++ b/src/static_literals.c @@ -7,7 +7,7 @@ */ typedef struct { /** The list of newline offsets to use to calculate line numbers. */ - const pm_newline_list_t *newline_list; + const pm_line_offset_list_t *line_offsets; /** The start of the source being parsed. */ const uint8_t *start; @@ -95,7 +95,7 @@ node_hash(const pm_static_literals_metadata_t *metadata, const pm_node_t *node) } case PM_SOURCE_LINE_NODE: { // Source lines hash their line number. - const pm_line_column_t line_column = pm_newline_list_line_column(metadata->newline_list, node->location.start, metadata->start_line); + const pm_line_column_t line_column = pm_line_offset_list_line_column(metadata->line_offsets, node->location.start, metadata->start_line); const int32_t *value = &line_column.line; return murmur_hash((const uint8_t *) value, sizeof(int32_t)); } @@ -243,7 +243,7 @@ pm_int64_value(const pm_static_literals_metadata_t *metadata, const pm_node_t *n return integer->negative ? -value : value; } case PM_SOURCE_LINE_NODE: - return (int64_t) pm_newline_list_line_column(metadata->newline_list, node->location.start, metadata->start_line).line; + return (int64_t) pm_line_offset_list_line_column(metadata->line_offsets, node->location.start, metadata->start_line).line; default: assert(false && "unreachable"); return 0; @@ -356,14 +356,14 @@ pm_compare_regular_expression_nodes(PRISM_ATTRIBUTE_UNUSED const pm_static_liter * Add a node to the set of static literals. */ 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_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) { switch (PM_NODE_TYPE(node)) { case PM_INTEGER_NODE: case PM_SOURCE_LINE_NODE: return pm_node_hash_insert( &literals->integer_nodes, &(pm_static_literals_metadata_t) { - .newline_list = newline_list, + .line_offsets = line_offsets, .start = start, .start_line = start_line, .encoding_name = NULL @@ -376,7 +376,7 @@ pm_static_literals_add(const pm_newline_list_t *newline_list, const uint8_t *sta return pm_node_hash_insert( &literals->float_nodes, &(pm_static_literals_metadata_t) { - .newline_list = newline_list, + .line_offsets = line_offsets, .start = start, .start_line = start_line, .encoding_name = NULL @@ -390,7 +390,7 @@ pm_static_literals_add(const pm_newline_list_t *newline_list, const uint8_t *sta return pm_node_hash_insert( &literals->number_nodes, &(pm_static_literals_metadata_t) { - .newline_list = newline_list, + .line_offsets = line_offsets, .start = start, .start_line = start_line, .encoding_name = NULL @@ -404,7 +404,7 @@ pm_static_literals_add(const pm_newline_list_t *newline_list, const uint8_t *sta return pm_node_hash_insert( &literals->string_nodes, &(pm_static_literals_metadata_t) { - .newline_list = newline_list, + .line_offsets = line_offsets, .start = start, .start_line = start_line, .encoding_name = NULL @@ -417,7 +417,7 @@ pm_static_literals_add(const pm_newline_list_t *newline_list, const uint8_t *sta return pm_node_hash_insert( &literals->regexp_nodes, &(pm_static_literals_metadata_t) { - .newline_list = newline_list, + .line_offsets = line_offsets, .start = start, .start_line = start_line, .encoding_name = NULL @@ -430,7 +430,7 @@ pm_static_literals_add(const pm_newline_list_t *newline_list, const uint8_t *sta return pm_node_hash_insert( &literals->symbol_nodes, &(pm_static_literals_metadata_t) { - .newline_list = newline_list, + .line_offsets = line_offsets, .start = start, .start_line = start_line, .encoding_name = NULL @@ -585,7 +585,7 @@ pm_static_literal_inspect_node(pm_buffer_t *buffer, const pm_static_literals_met break; } case PM_SOURCE_LINE_NODE: - pm_buffer_append_format(buffer, "%d", pm_newline_list_line_column(metadata->newline_list, node->location.start, metadata->start_line).line); + pm_buffer_append_format(buffer, "%d", pm_line_offset_list_line_column(metadata->line_offsets, node->location.start, metadata->start_line).line); break; case PM_STRING_NODE: { const pm_string_t *unescaped = &((const pm_string_node_t *) node)->unescaped; @@ -613,11 +613,11 @@ pm_static_literal_inspect_node(pm_buffer_t *buffer, const pm_static_literals_met * Create a string-based representation of the given static literal. */ 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) { +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) { pm_static_literal_inspect_node( buffer, &(pm_static_literals_metadata_t) { - .newline_list = newline_list, + .line_offsets = line_offsets, .start = start, .start_line = start_line, .encoding_name = encoding_name diff --git a/src/util/pm_char.c b/src/util/pm_char.c index 748582b7fe..f0baf47784 100644 --- a/src/util/pm_char.c +++ b/src/util/pm_char.c @@ -83,7 +83,7 @@ pm_strspn_whitespace(const uint8_t *string, ptrdiff_t length) { * searching past the given maximum number of characters. */ size_t -pm_strspn_whitespace_newlines(const uint8_t *string, ptrdiff_t length, pm_newline_list_t *newline_list, uint32_t start_offset) { +pm_strspn_whitespace_newlines(const uint8_t *string, ptrdiff_t length, pm_line_offset_list_t *line_offsets, uint32_t start_offset) { if (length <= 0) return 0; uint32_t size = 0; @@ -91,7 +91,7 @@ pm_strspn_whitespace_newlines(const uint8_t *string, ptrdiff_t length, pm_newlin while (size < maximum && (pm_byte_table[string[size]] & PRISM_CHAR_BIT_WHITESPACE)) { if (string[size] == '\n') { - pm_newline_list_append(newline_list, start_offset + size + 1); + pm_line_offset_list_append(line_offsets, start_offset + size + 1); } size++; diff --git a/src/util/pm_newline_list.c b/src/util/pm_line_offset_list.c similarity index 84% rename from src/util/pm_newline_list.c rename to src/util/pm_line_offset_list.c index 89c294a6d7..710fa4b788 100644 --- a/src/util/pm_newline_list.c +++ b/src/util/pm_line_offset_list.c @@ -1,11 +1,11 @@ -#include "prism/util/pm_newline_list.h" +#include "prism/util/pm_line_offset_list.h" /** * Initialize a new newline list with the given capacity. Returns true if the * allocation of the offsets succeeds, otherwise returns false. */ bool -pm_newline_list_init(pm_newline_list_t *list, size_t capacity) { +pm_line_offset_list_init(pm_line_offset_list_t *list, size_t capacity) { list->offsets = (uint32_t *) xcalloc(capacity, sizeof(uint32_t)); if (list->offsets == NULL) return false; @@ -21,7 +21,7 @@ pm_newline_list_init(pm_newline_list_t *list, size_t capacity) { * Clear out the newlines that have been appended to the list. */ void -pm_newline_list_clear(pm_newline_list_t *list) { +pm_line_offset_list_clear(pm_line_offset_list_t *list) { list->size = 1; } @@ -30,7 +30,7 @@ pm_newline_list_clear(pm_newline_list_t *list) { * the offsets succeeds (if one was necessary), otherwise returns false. */ bool -pm_newline_list_append(pm_newline_list_t *list, uint32_t cursor) { +pm_line_offset_list_append(pm_line_offset_list_t *list, uint32_t cursor) { if (list->size == list->capacity) { uint32_t *original_offsets = list->offsets; @@ -53,7 +53,7 @@ pm_newline_list_append(pm_newline_list_t *list, uint32_t cursor) { * line of the closest offset less than the given offset is returned. */ int32_t -pm_newline_list_line(const pm_newline_list_t *list, uint32_t cursor, int32_t start_line) { +pm_line_offset_list_line(const pm_line_offset_list_t *list, uint32_t cursor, int32_t start_line) { size_t left = 0; size_t right = list->size - 1; @@ -80,7 +80,7 @@ pm_newline_list_line(const pm_newline_list_t *list, uint32_t cursor, int32_t sta * are returned. */ pm_line_column_t -pm_newline_list_line_column(const pm_newline_list_t *list, uint32_t cursor, int32_t start_line) { +pm_line_offset_list_line_column(const pm_line_offset_list_t *list, uint32_t cursor, int32_t start_line) { size_t left = 0; size_t right = list->size - 1; @@ -108,6 +108,6 @@ pm_newline_list_line_column(const pm_newline_list_t *list, uint32_t cursor, int3 * Free the internal memory allocated for the newline list. */ void -pm_newline_list_free(pm_newline_list_t *list) { +pm_line_offset_list_free(pm_line_offset_list_t *list) { xfree(list->offsets); } diff --git a/templates/ext/prism/api_node.c.erb b/templates/ext/prism/api_node.c.erb index e9c3742085..69f70240f8 100644 --- a/templates/ext/prism/api_node.c.erb +++ b/templates/ext/prism/api_node.c.erb @@ -76,9 +76,9 @@ VALUE pm_source_new(const pm_parser_t *parser, rb_encoding *encoding, bool freeze) { VALUE source_string = rb_enc_str_new((const char *) parser->start, parser->end - parser->start, encoding); - VALUE offsets = rb_ary_new_capa(parser->newline_list.size); - for (size_t index = 0; index < parser->newline_list.size; index++) { - rb_ary_push(offsets, ULONG2NUM(parser->newline_list.offsets[index])); + VALUE offsets = rb_ary_new_capa(parser->line_offsets.size); + for (size_t index = 0; index < parser->line_offsets.size; index++) { + rb_ary_push(offsets, ULONG2NUM(parser->line_offsets.offsets[index])); } if (freeze) { diff --git a/templates/src/prettyprint.c.erb b/templates/src/prettyprint.c.erb index 74c0f6dbdf..44423ca42b 100644 --- a/templates/src/prettyprint.c.erb +++ b/templates/src/prettyprint.c.erb @@ -12,8 +12,8 @@ void pm_prettyprint(void) {} static inline void prettyprint_location(pm_buffer_t *output_buffer, const pm_parser_t *parser, const pm_location_t *location) { - pm_line_column_t start = pm_newline_list_line_column(&parser->newline_list, location->start, parser->start_line); - pm_line_column_t end = pm_newline_list_line_column(&parser->newline_list, location->start + location->length, parser->start_line); + pm_line_column_t start = pm_line_offset_list_line_column(&parser->line_offsets, location->start, parser->start_line); + pm_line_column_t end = pm_line_offset_list_line_column(&parser->line_offsets, location->start + location->length, parser->start_line); pm_buffer_append_format(output_buffer, "(%" PRIi32 ",%" PRIu32 ")-(%" PRIi32 ",%" PRIu32 ")", start.line, start.column, end.line, end.column); } diff --git a/templates/src/serialize.c.erb b/templates/src/serialize.c.erb index 958b0fd7cf..54a499d8bb 100644 --- a/templates/src/serialize.c.erb +++ b/templates/src/serialize.c.erb @@ -154,7 +154,7 @@ pm_serialize_node(pm_parser_t *parser, pm_node_t *node, pm_buffer_t *buffer) { } static void -pm_serialize_newline_list(pm_newline_list_t *list, pm_buffer_t *buffer) { +pm_serialize_line_offset_list(pm_line_offset_list_t *list, pm_buffer_t *buffer) { uint32_t size = pm_sizet_to_u32(list->size); pm_buffer_append_varuint(buffer, size); @@ -257,7 +257,7 @@ static void pm_serialize_metadata(pm_parser_t *parser, pm_buffer_t *buffer) { pm_serialize_encoding(parser->encoding, buffer); pm_buffer_append_varsint(buffer, parser->start_line); - pm_serialize_newline_list(&parser->newline_list, buffer); + pm_serialize_line_offset_list(&parser->line_offsets, buffer); <%- unless Prism::Template::SERIALIZE_ONLY_SEMANTICS_FIELDS -%> pm_serialize_comment_list(&parser->comment_list, buffer); <%- end -%>