@@ -3098,8 +3098,8 @@ static rbs_ast_comment_t *parse_comment_lines(rbs_parser_t *parser, rbs_comment_
30983098 rbs_buffer_t rbs_buffer ;
30993099 rbs_buffer_init (ALLOCATOR (), & rbs_buffer );
31003100
3101- for (size_t i = 0 ; i < com -> line_count ; i ++ ) {
3102- rbs_token_t tok = com -> tokens [i ];
3101+ for (size_t i = 0 ; i < com -> line_tokens_count ; i ++ ) {
3102+ rbs_token_t tok = com -> line_tokens [i ];
31033103
31043104 const char * comment_start = parser -> rbs_lexer_t -> string .start + tok .range .start .byte_pos + hash_bytes ;
31053105 size_t comment_bytes = RBS_RANGE_BYTES (tok .range ) - hash_bytes ;
@@ -3143,42 +3143,43 @@ static rbs_comment_t *comment_get_comment(rbs_comment_t *com, int line) {
31433143}
31443144
31453145static void comment_insert_new_line (rbs_allocator_t * allocator , rbs_comment_t * com , rbs_token_t comment_token ) {
3146- if (com -> line_count == 0 ) {
3147- com -> start = comment_token .range .start ;
3148- }
3149-
3150- if (com -> line_count == com -> line_size ) {
3151- com -> line_size += 10 ;
3152-
3153- if (com -> tokens ) {
3154- rbs_token_t * p = com -> tokens ;
3155- com -> tokens = rbs_allocator_calloc (allocator , com -> line_size , rbs_token_t );
3156- memcpy (com -> tokens , p , sizeof (rbs_token_t ) * com -> line_count );
3157- } else {
3158- com -> tokens = rbs_allocator_calloc (allocator , com -> line_size , rbs_token_t );
3159- }
3146+ if (com -> line_tokens_count == com -> line_tokens_capacity ) {
3147+ size_t old_size = com -> line_tokens_capacity ;
3148+ size_t new_size = old_size * 2 ;
3149+ com -> line_tokens_capacity = new_size ;
3150+
3151+ com -> line_tokens = rbs_allocator_realloc (
3152+ allocator ,
3153+ com -> line_tokens ,
3154+ sizeof (rbs_token_t ) * old_size ,
3155+ sizeof (rbs_token_t ) * new_size ,
3156+ rbs_token_t
3157+ );
31603158 }
31613159
3162- com -> tokens [com -> line_count ++ ] = comment_token ;
3160+ com -> line_tokens [com -> line_tokens_count ++ ] = comment_token ;
31633161 com -> end = comment_token .range .end ;
31643162}
31653163
31663164static rbs_comment_t * alloc_comment (rbs_allocator_t * allocator , rbs_token_t comment_token , rbs_comment_t * last_comment ) {
31673165 rbs_comment_t * new_comment = rbs_allocator_alloc (allocator , rbs_comment_t );
31683166
3167+ size_t initial_line_capacity = 10 ;
3168+
3169+ rbs_token_t * tokens = rbs_allocator_calloc (allocator , initial_line_capacity , rbs_token_t );
3170+ tokens [0 ] = comment_token ;
3171+
31693172 * new_comment = (rbs_comment_t ) {
31703173 .start = comment_token .range .start ,
31713174 .end = comment_token .range .end ,
31723175
3173- .line_size = 0 ,
3174- .line_count = 0 ,
3175- .tokens = NULL ,
3176+ .line_tokens_capacity = initial_line_capacity ,
3177+ .line_tokens_count = 1 ,
3178+ .line_tokens = tokens ,
31763179
31773180 .next_comment = last_comment ,
31783181 };
31793182
3180- comment_insert_new_line (allocator , new_comment , comment_token );
3181-
31823183 return new_comment ;
31833184}
31843185
0 commit comments