@@ -3182,8 +3182,8 @@ static rbs_ast_comment_t *parse_comment_lines(rbs_parser_t *parser, rbs_comment_
3182
3182
rbs_buffer_t rbs_buffer ;
3183
3183
rbs_buffer_init (ALLOCATOR (), & rbs_buffer );
3184
3184
3185
- for (size_t i = 0 ; i < com -> line_count ; i ++ ) {
3186
- rbs_token_t tok = com -> tokens [i ];
3185
+ for (size_t i = 0 ; i < com -> line_tokens_count ; i ++ ) {
3186
+ rbs_token_t tok = com -> line_tokens [i ];
3187
3187
3188
3188
const char * comment_start = parser -> rbs_lexer_t -> string .start + tok .range .start .byte_pos + hash_bytes ;
3189
3189
size_t comment_bytes = RBS_RANGE_BYTES (tok .range ) - hash_bytes ;
@@ -3227,42 +3227,43 @@ static rbs_comment_t *comment_get_comment(rbs_comment_t *com, int line) {
3227
3227
}
3228
3228
3229
3229
static void comment_insert_new_line (rbs_allocator_t * allocator , rbs_comment_t * com , rbs_token_t comment_token ) {
3230
- if (com -> line_count == 0 ) {
3231
- com -> start = comment_token .range .start ;
3232
- }
3233
-
3234
- if (com -> line_count == com -> line_size ) {
3235
- com -> line_size += 10 ;
3236
-
3237
- if (com -> tokens ) {
3238
- rbs_token_t * p = com -> tokens ;
3239
- com -> tokens = rbs_allocator_calloc (allocator , com -> line_size , rbs_token_t );
3240
- memcpy (com -> tokens , p , sizeof (rbs_token_t ) * com -> line_count );
3241
- } else {
3242
- com -> tokens = rbs_allocator_calloc (allocator , com -> line_size , rbs_token_t );
3243
- }
3230
+ if (com -> line_tokens_count == com -> line_tokens_capacity ) {
3231
+ size_t old_size = com -> line_tokens_capacity ;
3232
+ size_t new_size = old_size * 2 ;
3233
+ com -> line_tokens_capacity = new_size ;
3234
+
3235
+ com -> line_tokens = rbs_allocator_realloc (
3236
+ allocator ,
3237
+ com -> line_tokens ,
3238
+ sizeof (rbs_token_t ) * old_size ,
3239
+ sizeof (rbs_token_t ) * new_size ,
3240
+ rbs_token_t
3241
+ );
3244
3242
}
3245
3243
3246
- com -> tokens [com -> line_count ++ ] = comment_token ;
3244
+ com -> line_tokens [com -> line_tokens_count ++ ] = comment_token ;
3247
3245
com -> end = comment_token .range .end ;
3248
3246
}
3249
3247
3250
3248
static rbs_comment_t * alloc_comment (rbs_allocator_t * allocator , rbs_token_t comment_token , rbs_comment_t * last_comment ) {
3251
3249
rbs_comment_t * new_comment = rbs_allocator_alloc (allocator , rbs_comment_t );
3252
3250
3251
+ size_t initial_line_capacity = 10 ;
3252
+
3253
+ rbs_token_t * tokens = rbs_allocator_calloc (allocator , initial_line_capacity , rbs_token_t );
3254
+ tokens [0 ] = comment_token ;
3255
+
3253
3256
* new_comment = (rbs_comment_t ) {
3254
3257
.start = comment_token .range .start ,
3255
3258
.end = comment_token .range .end ,
3256
3259
3257
- .line_size = 0 ,
3258
- .line_count = 0 ,
3259
- .tokens = NULL ,
3260
+ .line_tokens_capacity = initial_line_capacity ,
3261
+ .line_tokens_count = 1 ,
3262
+ .line_tokens = tokens ,
3260
3263
3261
3264
.next_comment = last_comment ,
3262
3265
};
3263
3266
3264
- comment_insert_new_line (allocator , new_comment , comment_token );
3265
-
3266
3267
return new_comment ;
3267
3268
}
3268
3269
0 commit comments