Skip to content

Commit 9558faa

Browse files
committed
Switch comments over to using slices
1 parent 460575a commit 9558faa

5 files changed

Lines changed: 18 additions & 24 deletions

File tree

ext/prism/extension.c

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -463,21 +463,15 @@ parser_location(VALUE source, bool freeze, uint32_t start, uint32_t length) {
463463
/**
464464
* Create a new Location instance from the given parser and slice.
465465
*/
466-
#define PARSER_LOCATION_SLICE(parser, source, freeze, slice) \
466+
#define PARSER_LOCATION_SLICE(source, freeze, slice) \
467467
parser_location(source, freeze, slice.start, slice.length)
468468

469-
/**
470-
* Create a new Location instance from the given parser and location.
471-
*/
472-
#define PARSER_LOCATION_LOC(parser, source, freeze, loc) \
473-
parser_location(source, freeze, (uint32_t) (loc.start - parser->start), (uint32_t) (loc.end - loc.start))
474-
475469
/**
476470
* Build a new Comment instance from the given parser and comment.
477471
*/
478472
static inline VALUE
479-
parser_comment(const pm_parser_t *parser, VALUE source, bool freeze, const pm_comment_t *comment) {
480-
VALUE argv[] = { PARSER_LOCATION_LOC(parser, source, freeze, comment->location) };
473+
parser_comment(VALUE source, bool freeze, const pm_comment_t *comment) {
474+
VALUE argv[] = { PARSER_LOCATION_SLICE(source, freeze, comment->location) };
481475
VALUE type = (comment->type == PM_COMMENT_EMBDOC) ? rb_cPrismEmbDocComment : rb_cPrismInlineComment;
482476
return rb_class_new_instance_freeze(1, argv, type, freeze);
483477
}
@@ -494,7 +488,7 @@ parser_comments(const pm_parser_t *parser, VALUE source, bool freeze) {
494488
comment != NULL;
495489
comment = (const pm_comment_t *) comment->node.next
496490
) {
497-
VALUE value = parser_comment(parser, source, freeze, comment);
491+
VALUE value = parser_comment(source, freeze, comment);
498492
rb_ary_push(comments, value);
499493
}
500494

@@ -560,7 +554,7 @@ parser_errors(const pm_parser_t *parser, rb_encoding *encoding, VALUE source, bo
560554
) {
561555
VALUE type = ID2SYM(rb_intern(pm_diagnostic_id_human(error->diag_id)));
562556
VALUE message = rb_obj_freeze(rb_enc_str_new_cstr(error->message, encoding));
563-
VALUE location = PARSER_LOCATION_SLICE(parser, source, freeze, error->location);
557+
VALUE location = PARSER_LOCATION_SLICE(source, freeze, error->location);
564558

565559
VALUE level = Qnil;
566560
switch (error->level) {
@@ -600,7 +594,7 @@ parser_warnings(const pm_parser_t *parser, rb_encoding *encoding, VALUE source,
600594
) {
601595
VALUE type = ID2SYM(rb_intern(pm_diagnostic_id_human(warning->diag_id)));
602596
VALUE message = rb_obj_freeze(rb_enc_str_new_cstr(warning->message, encoding));
603-
VALUE location = PARSER_LOCATION_SLICE(parser, source, freeze, warning->location);
597+
VALUE location = PARSER_LOCATION_SLICE(source, freeze, warning->location);
604598

605599
VALUE level = Qnil;
606600
switch (warning->level) {

include/prism.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,10 @@ PRISM_EXPORTED_FUNCTION void pm_serialize_parse_stream(pm_buffer_t *buffer, void
143143
/**
144144
* Serialize the given list of comments to the given buffer.
145145
*
146-
* @param parser The parser to serialize.
147146
* @param list The list of comments to serialize.
148147
* @param buffer The buffer to serialize to.
149148
*/
150-
void pm_serialize_comment_list(pm_parser_t *parser, pm_list_t *list, pm_buffer_t *buffer);
149+
void pm_serialize_comment_list(pm_list_t *list, pm_buffer_t *buffer);
151150

152151
/**
153152
* Serialize the name of the encoding to the buffer.

include/prism/parser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ typedef struct pm_comment {
463463
pm_list_node_t node;
464464

465465
/** The location of the comment in the source. */
466-
pm_location_t location;
466+
pm_slice_t location;
467467

468468
/** The type of comment that we've found. */
469469
pm_comment_type_t type;

src/prism.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9224,7 +9224,7 @@ parser_comment(pm_parser_t *parser, pm_comment_type_t type) {
92249224

92259225
*comment = (pm_comment_t) {
92269226
.type = type,
9227-
.location = { parser->current.start, parser->current.end }
9227+
.location = TOKEN2SLICE(parser, &parser->current)
92289228
};
92299229

92309230
return comment;
@@ -9251,6 +9251,7 @@ lex_embdoc(pm_parser_t *parser) {
92519251
parser_lex_callback(parser);
92529252

92539253
// Now, create a comment that is going to be attached to the parser.
9254+
const uint8_t *comment_start = parser->current.start;
92549255
pm_comment_t *comment = parser_comment(parser, PM_COMMENT_EMBDOC);
92559256
if (comment == NULL) return PM_TOKEN_EOF;
92569257

@@ -9283,7 +9284,7 @@ lex_embdoc(pm_parser_t *parser) {
92839284
parser->current.type = PM_TOKEN_EMBDOC_END;
92849285
parser_lex_callback(parser);
92859286

9286-
comment->location.end = parser->current.end;
9287+
comment->location.length = (uint32_t) (parser->current.end - comment_start);
92879288
pm_list_append(&parser->comment_list, (pm_list_node_t *) comment);
92889289

92899290
return PM_TOKEN_EMBDOC_END;
@@ -9306,7 +9307,7 @@ lex_embdoc(pm_parser_t *parser) {
93069307

93079308
pm_parser_err_current(parser, PM_ERR_EMBDOC_TERM);
93089309

9309-
comment->location.end = parser->current.end;
9310+
comment->location.length = (uint32_t) (parser->current.end - comment_start);
93109311
pm_list_append(&parser->comment_list, (pm_list_node_t *) comment);
93119312

93129313
return PM_TOKEN_EOF;
@@ -22238,7 +22239,7 @@ pm_serialize_parse_comments(pm_buffer_t *buffer, const uint8_t *source, size_t s
2223822239
pm_serialize_header(buffer);
2223922240
pm_serialize_encoding(parser.encoding, buffer);
2224022241
pm_buffer_append_varsint(buffer, parser.start_line);
22241-
pm_serialize_comment_list(&parser, &parser.comment_list, buffer);
22242+
pm_serialize_comment_list(&parser.comment_list, buffer);
2224222243

2224322244
pm_node_destroy(&parser, node);
2224422245
pm_parser_free(&parser);

templates/src/serialize.c.erb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,24 +188,24 @@ pm_serialize_newline_list(pm_newline_list_t *list, pm_buffer_t *buffer) {
188188
}
189189

190190
static void
191-
pm_serialize_comment(pm_parser_t *parser, pm_comment_t *comment, pm_buffer_t *buffer) {
191+
pm_serialize_comment(pm_comment_t *comment, pm_buffer_t *buffer) {
192192
// serialize type
193193
pm_buffer_append_byte(buffer, (uint8_t) comment->type);
194194

195195
// serialize location
196-
pm_serialize_location(parser, &comment->location, buffer);
196+
pm_serialize_slice(&comment->location, buffer);
197197
}
198198

199199
/**
200200
* Serialize the given list of comments to the given buffer.
201201
*/
202202
void
203-
pm_serialize_comment_list(pm_parser_t *parser, pm_list_t *list, pm_buffer_t *buffer) {
203+
pm_serialize_comment_list(pm_list_t *list, pm_buffer_t *buffer) {
204204
pm_buffer_append_varuint(buffer, pm_sizet_to_u32(pm_list_size(list)));
205205

206206
pm_comment_t *comment;
207207
for (comment = (pm_comment_t *) list->head; comment != NULL; comment = (pm_comment_t *) comment->node.next) {
208-
pm_serialize_comment(parser, comment, buffer);
208+
pm_serialize_comment(comment, buffer);
209209
}
210210
}
211211

@@ -282,7 +282,7 @@ pm_serialize_metadata(pm_parser_t *parser, pm_buffer_t *buffer) {
282282
pm_buffer_append_varsint(buffer, parser->start_line);
283283
pm_serialize_newline_list(&parser->newline_list, buffer);
284284
<%- unless Prism::Template::SERIALIZE_ONLY_SEMANTICS_FIELDS -%>
285-
pm_serialize_comment_list(parser, &parser->comment_list, buffer);
285+
pm_serialize_comment_list(&parser->comment_list, buffer);
286286
<%- end -%>
287287
pm_serialize_magic_comment_list(&parser->magic_comment_list, buffer);
288288
pm_serialize_data_loc(parser, buffer);

0 commit comments

Comments
 (0)