Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions include/rbs/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ typedef struct rbs_comment_t {
rbs_position_t start;
rbs_position_t end;

size_t line_size;
size_t line_count;
rbs_token_t *tokens;
size_t line_tokens_capacity;
size_t line_tokens_count;
rbs_token_t *line_tokens;
Comment on lines +30 to +32
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This naming was confusing, because line_size sounds like "the size of a line".

What is actually means is "the capacity of the line_tokens buffer.


struct rbs_comment_t *next_comment;
} rbs_comment_t;
Expand Down
45 changes: 23 additions & 22 deletions src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -3145,8 +3145,8 @@ static rbs_ast_comment_t *parse_comment_lines(rbs_parser_t *parser, rbs_comment_
rbs_buffer_t rbs_buffer;
rbs_buffer_init(ALLOCATOR(), &rbs_buffer);

for (size_t i = 0; i < com->line_count; i++) {
rbs_token_t tok = com->tokens[i];
for (size_t i = 0; i < com->line_tokens_count; i++) {
rbs_token_t tok = com->line_tokens[i];

const char *comment_start = parser->rbs_lexer_t->string.start + tok.range.start.byte_pos + hash_bytes;
size_t comment_bytes = RBS_RANGE_BYTES(tok.range) - hash_bytes;
Expand Down Expand Up @@ -3190,42 +3190,43 @@ static rbs_comment_t *comment_get_comment(rbs_comment_t *com, int line) {
}

static void comment_insert_new_line(rbs_allocator_t *allocator, rbs_comment_t *com, rbs_token_t comment_token) {
if (com->line_count == 0) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By changing alloc_comment() to not call comment_insert_new_line(), we never have to deal with the 0 case here 🥳

com->start = comment_token.range.start;
}

if (com->line_count == com->line_size) {
com->line_size += 10;

if (com->tokens) {
rbs_token_t *p = com->tokens;
com->tokens = rbs_allocator_calloc(allocator, com->line_size, rbs_token_t);
memcpy(com->tokens, p, sizeof(rbs_token_t) * com->line_count);
Comment on lines -3202 to -3203
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was just re-implementing realloc

} else {
com->tokens = rbs_allocator_calloc(allocator, com->line_size, rbs_token_t);
}
if (com->line_tokens_count == com->line_tokens_capacity) {
size_t old_size = com->line_tokens_capacity;
size_t new_size = old_size * 2;
com->line_tokens_capacity = new_size;

com->line_tokens = rbs_allocator_realloc(
allocator,
com->line_tokens,
sizeof(rbs_token_t) * old_size,
sizeof(rbs_token_t) * new_size,
rbs_token_t
);
}

com->tokens[com->line_count++] = comment_token;
com->line_tokens[com->line_tokens_count++] = comment_token;
com->end = comment_token.range.end;
}

static rbs_comment_t *alloc_comment(rbs_allocator_t *allocator, rbs_token_t comment_token, rbs_comment_t *last_comment) {
rbs_comment_t *new_comment = rbs_allocator_alloc(allocator, rbs_comment_t);

size_t initial_line_capacity = 10;

rbs_token_t *tokens = rbs_allocator_calloc(allocator, initial_line_capacity, rbs_token_t);
tokens[0] = comment_token;

*new_comment = (rbs_comment_t) {
.start = comment_token.range.start,
.end = comment_token.range.end,

.line_size = 0,
.line_count = 0,
.tokens = NULL,
.line_tokens_capacity = initial_line_capacity,
.line_tokens_count = 1,
.line_tokens = tokens,

.next_comment = last_comment,
};

comment_insert_new_line(allocator, new_comment, comment_token);

return new_comment;
}

Expand Down
Loading