Skip to content

Commit abef408

Browse files
committed
Reuse rbs_buffer for comment tokens
1 parent 6855f49 commit abef408

File tree

3 files changed

+36
-25
lines changed

3 files changed

+36
-25
lines changed

include/rbs/parser.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "rbs/defines.h"
55
#include "rbs/util/rbs_allocator.h"
66
#include "rbs/util/rbs_constant_pool.h"
7+
#include "rbs/util/rbs_buffer.h"
78
#include "rbs/lexer.h"
89
#include "rbs/ast.h"
910

@@ -27,9 +28,8 @@ typedef struct rbs_comment_t {
2728
rbs_position_t start;
2829
rbs_position_t end;
2930

30-
size_t line_tokens_capacity;
3131
size_t line_tokens_count;
32-
rbs_token_t *line_tokens;
32+
rbs_buffer_t /* of rbs_token_t */ line_tokens;
3333

3434
struct rbs_comment_t *next_comment;
3535
} rbs_comment_t;

include/rbs/util/rbs_buffer.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,29 @@ void rbs_buffer_append_string(rbs_allocator_t *, rbs_buffer_t *buffer, const cha
7676
*/
7777
rbs_string_t rbs_buffer_to_string(rbs_buffer_t *buffer);
7878

79+
/**
80+
* Append a value to the buffer.
81+
*
82+
* @param allocator The allocator to use.
83+
* @param buffer The buffer to append to.
84+
* @param value The value to append.
85+
* @param type The type of the value to append, which determines how many bytes to append.
86+
*/
87+
#define rbs_buffer_append_value(allocator, buffer, value, type) \
88+
rbs_buffer_append_string((allocator), (buffer), (char *) (value), sizeof(type))
89+
90+
/**
91+
* Returns a copy of a `type` from the `buffer` at the given `index`.
92+
*
93+
* This cast is unchecked, so it's up to caller to ensure the type is correct.
94+
*
95+
* @param buffer The buffer to get the value from.
96+
* @param index The index of the element to retrieve.
97+
* @param type The element type that the data will be cast to.
98+
* @returns The value at the specified index, cast to the specified type.
99+
*/
100+
#define rbs_buffer_get(buffer, index, type) ( \
101+
((type *) (buffer).value)[index] \
102+
)
103+
79104
#endif

src/parser.c

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3208,7 +3208,7 @@ static rbs_ast_comment_t *parse_comment_lines(rbs_parser_t *parser, rbs_comment_
32083208
rbs_buffer_init(ALLOCATOR(), &rbs_buffer);
32093209

32103210
for (size_t i = 0; i < com->line_tokens_count; i++) {
3211-
rbs_token_t tok = com->line_tokens[i];
3211+
rbs_token_t tok = rbs_buffer_get(com->line_tokens, i, rbs_token_t);
32123212

32133213
const char *comment_start = parser->rbs_lexer_t->string.start + tok.range.start.byte_pos + hash_bytes;
32143214
size_t comment_bytes = RBS_RANGE_BYTES(tok.range) - hash_bytes;
@@ -3252,43 +3252,29 @@ static rbs_comment_t *comment_get_comment(rbs_comment_t *com, int line) {
32523252
}
32533253

32543254
static void comment_insert_new_line(rbs_allocator_t *allocator, rbs_comment_t *com, rbs_token_t comment_token) {
3255-
if (com->line_tokens_count == com->line_tokens_capacity) {
3256-
size_t old_size = com->line_tokens_capacity;
3257-
size_t new_size = old_size * 2;
3258-
com->line_tokens_capacity = new_size;
3259-
3260-
com->line_tokens = rbs_allocator_realloc(
3261-
allocator,
3262-
com->line_tokens,
3263-
sizeof(rbs_token_t) * old_size,
3264-
sizeof(rbs_token_t) * new_size,
3265-
rbs_token_t
3266-
);
3267-
}
3255+
rbs_buffer_append_value(allocator, &com->line_tokens, &comment_token, rbs_token_t);
32683256

3269-
com->line_tokens[com->line_tokens_count++] = comment_token;
3257+
com->line_tokens_count++;
32703258
com->end = comment_token.range.end;
32713259
}
32723260

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

3276-
size_t initial_line_capacity = 10;
3277-
3278-
rbs_token_t *tokens = rbs_allocator_calloc(allocator, initial_line_capacity, rbs_token_t);
3279-
tokens[0] = comment_token;
3280-
32813264
*new_comment = (rbs_comment_t) {
32823265
.start = comment_token.range.start,
32833266
.end = comment_token.range.end,
32843267

3285-
.line_tokens_capacity = initial_line_capacity,
3286-
.line_tokens_count = 1,
3287-
.line_tokens = tokens,
3268+
.line_tokens_count = 0,
3269+
.line_tokens = { 0 },
32883270

32893271
.next_comment = last_comment,
32903272
};
32913273

3274+
size_t initial_line_capacity = 10;
3275+
rbs_buffer_init_with_capacity(allocator, &new_comment->line_tokens, initial_line_capacity * sizeof(rbs_token_t));
3276+
comment_insert_new_line(allocator, new_comment, comment_token);
3277+
32923278
return new_comment;
32933279
}
32943280

0 commit comments

Comments
 (0)