Skip to content

Commit 73cb157

Browse files
committed
Switch magic comments over to using slices
1 parent 899af92 commit 73cb157

4 files changed

Lines changed: 19 additions & 27 deletions

File tree

ext/prism/extension.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -455,16 +455,16 @@ rb_class_new_instance_freeze(int argc, const VALUE *argv, VALUE klass, bool free
455455
* Create a new Location instance from the given parser and bounds.
456456
*/
457457
static inline VALUE
458-
parser_location(const pm_parser_t *parser, VALUE source, bool freeze, const uint8_t *start, size_t length) {
459-
VALUE argv[] = { source, LONG2FIX(start - parser->start), LONG2FIX(length) };
458+
parser_location(const pm_parser_t *parser, VALUE source, bool freeze, uint32_t start, uint32_t length) {
459+
VALUE argv[] = { source, LONG2FIX(start), LONG2FIX(length) };
460460
return rb_class_new_instance_freeze(3, argv, rb_cPrismLocation, freeze);
461461
}
462462

463463
/**
464464
* Create a new Location instance from the given parser and location.
465465
*/
466466
#define PARSER_LOCATION_LOC(parser, source, freeze, loc) \
467-
parser_location(parser, source, freeze, loc.start, (size_t) (loc.end - loc.start))
467+
parser_location(parser, source, freeze, (uint32_t) (loc.start - parser->start), (uint32_t) (loc.end - loc.start))
468468

469469
/**
470470
* Build a new Comment instance from the given parser and comment.
@@ -501,8 +501,8 @@ parser_comments(const pm_parser_t *parser, VALUE source, bool freeze) {
501501
*/
502502
static inline VALUE
503503
parser_magic_comment(const pm_parser_t *parser, VALUE source, bool freeze, const pm_magic_comment_t *magic_comment) {
504-
VALUE key_loc = parser_location(parser, source, freeze, magic_comment->key_start, magic_comment->key_length);
505-
VALUE value_loc = parser_location(parser, source, freeze, magic_comment->value_start, magic_comment->value_length);
504+
VALUE key_loc = parser_location(parser, source, freeze, magic_comment->key.start, magic_comment->key.length);
505+
VALUE value_loc = parser_location(parser, source, freeze, magic_comment->value.start, magic_comment->value.length);
506506
VALUE argv[] = { key_loc, value_loc };
507507
return rb_class_new_instance_freeze(2, argv, rb_cPrismMagicComment, freeze);
508508
}

include/prism/parser.h

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -479,17 +479,11 @@ typedef struct {
479479
/** The embedded base node. */
480480
pm_list_node_t node;
481481

482-
/** A pointer to the start of the key in the source. */
483-
const uint8_t *key_start;
482+
/** The key of the magic comment. */
483+
pm_slice_t key;
484484

485-
/** A pointer to the start of the value in the source. */
486-
const uint8_t *value_start;
487-
488-
/** The length of the key in the source. */
489-
uint32_t key_length;
490-
491-
/** The length of the value in the source. */
492-
uint32_t value_length;
485+
/** The value of the magic comment. */
486+
pm_slice_t value;
493487
} pm_magic_comment_t;
494488

495489
/**

src/prism.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7589,10 +7589,8 @@ parser_lex_magic_comment(pm_parser_t *parser, bool semantic_token_seen) {
75897589
// Allocate a new magic comment node to append to the parser's list.
75907590
pm_magic_comment_t *magic_comment;
75917591
if ((magic_comment = (pm_magic_comment_t *) xcalloc(1, sizeof(pm_magic_comment_t))) != NULL) {
7592-
magic_comment->key_start = key_start;
7593-
magic_comment->value_start = value_start;
7594-
magic_comment->key_length = (uint32_t) key_length;
7595-
magic_comment->value_length = value_length;
7592+
magic_comment->key = (pm_slice_t) { .start = (uint32_t) (key_start - parser->start), .length = (uint32_t) key_length };
7593+
magic_comment->value = (pm_slice_t) { .start = (uint32_t) (value_start - parser->start), .length = value_length };
75967594
pm_list_append(&parser->magic_comment_list, (pm_list_node_t *) magic_comment);
75977595
}
75987596
}

templates/src/serialize.c.erb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -210,23 +210,23 @@ pm_serialize_comment_list(pm_parser_t *parser, pm_list_t *list, pm_buffer_t *buf
210210
}
211211

212212
static void
213-
pm_serialize_magic_comment(pm_parser_t *parser, pm_magic_comment_t *magic_comment, pm_buffer_t *buffer) {
213+
pm_serialize_magic_comment(pm_magic_comment_t *magic_comment, pm_buffer_t *buffer) {
214214
// serialize key location
215-
pm_buffer_append_varuint(buffer, pm_ptrdifft_to_u32(magic_comment->key_start - parser->start));
216-
pm_buffer_append_varuint(buffer, pm_sizet_to_u32(magic_comment->key_length));
215+
pm_buffer_append_varuint(buffer, magic_comment->key.start);
216+
pm_buffer_append_varuint(buffer, magic_comment->key.length);
217217

218218
// serialize value location
219-
pm_buffer_append_varuint(buffer, pm_ptrdifft_to_u32(magic_comment->value_start - parser->start));
220-
pm_buffer_append_varuint(buffer, pm_sizet_to_u32(magic_comment->value_length));
219+
pm_buffer_append_varuint(buffer, magic_comment->value.start);
220+
pm_buffer_append_varuint(buffer, magic_comment->value.length);
221221
}
222222

223223
static void
224-
pm_serialize_magic_comment_list(pm_parser_t *parser, pm_list_t *list, pm_buffer_t *buffer) {
224+
pm_serialize_magic_comment_list(pm_list_t *list, pm_buffer_t *buffer) {
225225
pm_buffer_append_varuint(buffer, pm_sizet_to_u32(pm_list_size(list)));
226226

227227
pm_magic_comment_t *magic_comment;
228228
for (magic_comment = (pm_magic_comment_t *) list->head; magic_comment != NULL; magic_comment = (pm_magic_comment_t *) magic_comment->node.next) {
229-
pm_serialize_magic_comment(parser, magic_comment, buffer);
229+
pm_serialize_magic_comment(magic_comment, buffer);
230230
}
231231
}
232232

@@ -284,7 +284,7 @@ pm_serialize_metadata(pm_parser_t *parser, pm_buffer_t *buffer) {
284284
<%- unless Prism::Template::SERIALIZE_ONLY_SEMANTICS_FIELDS -%>
285285
pm_serialize_comment_list(parser, &parser->comment_list, buffer);
286286
<%- end -%>
287-
pm_serialize_magic_comment_list(parser, &parser->magic_comment_list, buffer);
287+
pm_serialize_magic_comment_list(&parser->magic_comment_list, buffer);
288288
pm_serialize_data_loc(parser, buffer);
289289
pm_serialize_diagnostic_list(parser, &parser->error_list, buffer);
290290
pm_serialize_diagnostic_list(parser, &parser->warning_list, buffer);

0 commit comments

Comments
 (0)