Skip to content
Draft
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 @@ -4,6 +4,7 @@
#include "rbs/defines.h"
#include "rbs/util/rbs_allocator.h"
#include "rbs/util/rbs_constant_pool.h"
#include "rbs/util/rbs_buffer.h"
#include "rbs/lexer.h"
#include "rbs/ast.h"

Expand All @@ -27,9 +28,8 @@ 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_count;
rbs_buffer_t /* of rbs_token_t */ line_tokens;

struct rbs_comment_t *next_comment;
} rbs_comment_t;
Expand Down
33 changes: 27 additions & 6 deletions include/rbs/util/rbs_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@
#include <stdlib.h>
#include <string.h>

/**
* The default capacity of a rbs_buffer_t.
* If the buffer needs to grow beyond this capacity, it will be doubled.
*/
#define RBS_BUFFER_DEFAULT_CAPACITY 128

/**
* A rbs_buffer_t is a simple memory buffer that stores data in a contiguous block of memory.
*/
Expand All @@ -37,6 +31,8 @@ typedef struct {
*/
bool rbs_buffer_init(rbs_allocator_t *, rbs_buffer_t *buffer);

bool rbs_buffer_init_with_capacity(rbs_allocator_t *allocator, rbs_buffer_t *buffer, size_t capacity);

/**
* Return the value of the buffer.
*
Expand Down Expand Up @@ -80,4 +76,29 @@ void rbs_buffer_append_string(rbs_allocator_t *, rbs_buffer_t *buffer, const cha
*/
rbs_string_t rbs_buffer_to_string(rbs_buffer_t *buffer);

/**
* Append a value to the buffer.
*
* @param allocator The allocator to use.
* @param buffer The buffer to append to.
* @param value The value to append.
* @param type The type of the value to append, which determines how many bytes to append.
*/
#define rbs_buffer_append_value(allocator, buffer, value, type) \
rbs_buffer_append_string((allocator), (buffer), (char *) (value), sizeof(type))

/**
* Returns a copy of a `type` from the `buffer` at the given `index`.
*
* This cast is unchecked, so it's up to caller to ensure the type is correct.
*
* @param buffer The buffer to get the value from.
* @param index The index of the element to retrieve.
* @param type The element type that the data will be cast to.
* @returns The value at the specified index, cast to the specified type.
*/
#define rbs_buffer_get(buffer, index, type) ( \
((type *) (buffer).value)[index] \
)

#endif
29 changes: 8 additions & 21 deletions src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -3115,8 +3115,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 = rbs_buffer_get(com->line_tokens, i, rbs_token_t);

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 @@ -3160,23 +3160,9 @@ 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) {
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);
} else {
com->tokens = rbs_allocator_calloc(allocator, com->line_size, rbs_token_t);
}
}
rbs_buffer_append_value(allocator, &com->line_tokens, &comment_token, rbs_token_t);

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

Expand All @@ -3187,13 +3173,14 @@ static rbs_comment_t *alloc_comment(rbs_allocator_t *allocator, rbs_token_t comm
.start = comment_token.range.start,
.end = comment_token.range.end,

.line_size = 0,
.line_count = 0,
.tokens = NULL,
.line_tokens_count = 0,
.line_tokens = { 0 },

.next_comment = last_comment,
};

size_t initial_line_capacity = 10;
rbs_buffer_init_with_capacity(allocator, &new_comment->line_tokens, initial_line_capacity * sizeof(rbs_token_t));
comment_insert_new_line(allocator, new_comment, comment_token);

return new_comment;
Expand Down
18 changes: 14 additions & 4 deletions src/util/rbs_buffer.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
#include "rbs/util/rbs_buffer.h"
#include "rbs/util/rbs_assert.h"

/**
* The default capacity of a rbs_buffer_t.
* If the buffer needs to grow beyond this capacity, it will be doubled.
*/
#define RBS_BUFFER_DEFAULT_CAPACITY 128

bool rbs_buffer_init(rbs_allocator_t *allocator, rbs_buffer_t *buffer) {
size_t capacity = RBS_BUFFER_DEFAULT_CAPACITY;
return rbs_buffer_init_with_capacity(allocator, buffer, RBS_BUFFER_DEFAULT_CAPACITY);
}

buffer->length = 0;
buffer->capacity = capacity;
bool rbs_buffer_init_with_capacity(rbs_allocator_t *allocator, rbs_buffer_t *buffer, size_t capacity) {
*buffer = (rbs_buffer_t) {
.length = 0,
.capacity = capacity,
.value = rbs_allocator_calloc(allocator, capacity, char),
};

buffer->value = rbs_allocator_calloc(allocator, capacity, char);
return buffer->value != NULL;
}

Expand Down
Loading