Skip to content

Commit 68988bf

Browse files
committed
Reuse rbs_buffer for comment tokens
1 parent db6bdf2 commit 68988bf

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
@@ -3116,7 +3116,7 @@ static rbs_ast_comment_t *parse_comment_lines(rbs_parser_t *parser, rbs_comment_
31163116
rbs_buffer_init(ALLOCATOR(), &rbs_buffer);
31173117

31183118
for (size_t i = 0; i < com->line_tokens_count; i++) {
3119-
rbs_token_t tok = com->line_tokens[i];
3119+
rbs_token_t tok = rbs_buffer_get(com->line_tokens, i, rbs_token_t);
31203120

31213121
const char *comment_start = parser->rbs_lexer_t->string.start + tok.range.start.byte_pos + hash_bytes;
31223122
size_t comment_bytes = RBS_RANGE_BYTES(tok.range) - hash_bytes;
@@ -3160,43 +3160,29 @@ static rbs_comment_t *comment_get_comment(rbs_comment_t *com, int line) {
31603160
}
31613161

31623162
static void comment_insert_new_line(rbs_allocator_t *allocator, rbs_comment_t *com, rbs_token_t comment_token) {
3163-
if (com->line_tokens_count == com->line_tokens_capacity) {
3164-
size_t old_size = com->line_tokens_capacity;
3165-
size_t new_size = old_size * 2;
3166-
com->line_tokens_capacity = new_size;
3167-
3168-
com->line_tokens = rbs_allocator_realloc(
3169-
allocator,
3170-
com->line_tokens,
3171-
sizeof(rbs_token_t) * old_size,
3172-
sizeof(rbs_token_t) * new_size,
3173-
rbs_token_t
3174-
);
3175-
}
3163+
rbs_buffer_append_value(allocator, &com->line_tokens, &comment_token, rbs_token_t);
31763164

3177-
com->line_tokens[com->line_tokens_count++] = comment_token;
3165+
com->line_tokens_count++;
31783166
com->end = comment_token.range.end;
31793167
}
31803168

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

3184-
size_t initial_line_capacity = 10;
3185-
3186-
rbs_token_t *tokens = rbs_allocator_calloc(allocator, initial_line_capacity, rbs_token_t);
3187-
tokens[0] = comment_token;
3188-
31893172
*new_comment = (rbs_comment_t) {
31903173
.start = comment_token.range.start,
31913174
.end = comment_token.range.end,
31923175

3193-
.line_tokens_capacity = initial_line_capacity,
3194-
.line_tokens_count = 1,
3195-
.line_tokens = tokens,
3176+
.line_tokens_count = 0,
3177+
.line_tokens = { 0 },
31963178

31973179
.next_comment = last_comment,
31983180
};
31993181

3182+
size_t initial_line_capacity = 10;
3183+
rbs_buffer_init_with_capacity(allocator, &new_comment->line_tokens, initial_line_capacity * sizeof(rbs_token_t));
3184+
comment_insert_new_line(allocator, new_comment, comment_token);
3185+
32003186
return new_comment;
32013187
}
32023188

0 commit comments

Comments
 (0)