diff --git a/prism/diagnostic.c b/prism/diagnostic.c index 443ad35c6a466a..f9fd95cb06934f 100644 --- a/prism/diagnostic.c +++ b/prism/diagnostic.c @@ -283,8 +283,7 @@ pm_diagnostic_list_append(pm_list_t *list, const uint8_t *start, const uint8_t * if (diagnostic == NULL) return false; *diagnostic = (pm_diagnostic_t) { - .start = start, - .end = end, + .location = { start, end }, .message = pm_diagnostic_message(diag_id), .owned = false }; @@ -327,8 +326,7 @@ pm_diagnostic_list_append_format(pm_list_t *list, const uint8_t *start, const ui va_end(arguments); *diagnostic = (pm_diagnostic_t) { - .start = start, - .end = end, + .location = { start, end }, .message = message, .owned = true }; diff --git a/prism/diagnostic.h b/prism/diagnostic.h index 3614a38ae79ca0..fc408ccbd69031 100644 --- a/prism/diagnostic.h +++ b/prism/diagnostic.h @@ -6,6 +6,7 @@ #ifndef PRISM_DIAGNOSTIC_H #define PRISM_DIAGNOSTIC_H +#include "prism/ast.h" #include "prism/defines.h" #include "prism/util/pm_list.h" @@ -22,11 +23,8 @@ typedef struct { /** The embedded base node. */ pm_list_node_t node; - /** A pointer to the start of the source that generated the diagnostic. */ - const uint8_t *start; - - /** A pointer to the end of the source that generated the diagnostic. */ - const uint8_t *end; + /** The location of the diagnostic in the source. */ + pm_location_t location; /** The message associated with the diagnostic. */ const char *message; diff --git a/prism/extension.c b/prism/extension.c index b1b0d52dea2804..f6f2b6b195145c 100644 --- a/prism/extension.c +++ b/prism/extension.c @@ -316,8 +316,8 @@ parser_comments(pm_parser_t *parser, VALUE source) { for (pm_comment_t *comment = (pm_comment_t *) parser->comment_list.head; comment != NULL; comment = (pm_comment_t *) comment->node.next) { VALUE location_argv[] = { source, - LONG2FIX(comment->start - parser->start), - LONG2FIX(comment->end - comment->start) + LONG2FIX(comment->location.start - parser->start), + LONG2FIX(comment->location.end - comment->location.start) }; VALUE type = (comment->type == PM_COMMENT_EMBDOC) ? rb_cPrismEmbDocComment : rb_cPrismInlineComment; @@ -389,8 +389,8 @@ parser_errors(pm_parser_t *parser, rb_encoding *encoding, VALUE source) { for (error = (pm_diagnostic_t *) parser->error_list.head; error != NULL; error = (pm_diagnostic_t *) error->node.next) { VALUE location_argv[] = { source, - LONG2FIX(error->start - parser->start), - LONG2FIX(error->end - error->start) + LONG2FIX(error->location.start - parser->start), + LONG2FIX(error->location.end - error->location.start) }; VALUE error_argv[] = { @@ -415,8 +415,8 @@ parser_warnings(pm_parser_t *parser, rb_encoding *encoding, VALUE source) { for (warning = (pm_diagnostic_t *) parser->warning_list.head; warning != NULL; warning = (pm_diagnostic_t *) warning->node.next) { VALUE location_argv[] = { source, - LONG2FIX(warning->start - parser->start), - LONG2FIX(warning->end - warning->start) + LONG2FIX(warning->location.start - parser->start), + LONG2FIX(warning->location.end - warning->location.start) }; VALUE warning_argv[] = { diff --git a/prism/parser.h b/prism/parser.h index 237f9fead9c7fb..98d8c0159bd956 100644 --- a/prism/parser.h +++ b/prism/parser.h @@ -382,11 +382,8 @@ typedef struct pm_comment { /** The embedded base node. */ pm_list_node_t node; - /** A pointer to the start of the comment in the source. */ - const uint8_t *start; - - /** A pointer to the end of the comment in the source. */ - const uint8_t *end; + /** The location of the comment in the source. */ + pm_location_t location; /** The type of comment that we've found. */ pm_comment_type_t type; diff --git a/prism/prism.c b/prism/prism.c index 651a632079ed96..15f99445fbec9d 100644 --- a/prism/prism.c +++ b/prism/prism.c @@ -7704,8 +7704,7 @@ parser_comment(pm_parser_t *parser, pm_comment_type_t type) { *comment = (pm_comment_t) { .type = type, - .start = parser->current.start, - .end = parser->current.end + .location = { parser->current.start, parser->current.end } }; return comment; @@ -7756,7 +7755,7 @@ lex_embdoc(pm_parser_t *parser) { parser->current.type = PM_TOKEN_EMBDOC_END; parser_lex_callback(parser); - comment->end = parser->current.end; + comment->location.end = parser->current.end; pm_list_append(&parser->comment_list, (pm_list_node_t *) comment); return PM_TOKEN_EMBDOC_END; @@ -7779,7 +7778,7 @@ lex_embdoc(pm_parser_t *parser) { pm_parser_err_current(parser, PM_ERR_EMBDOC_TERM); - comment->end = parser->current.end; + comment->location.end = parser->current.end; pm_list_append(&parser->comment_list, (pm_list_node_t *) comment); return PM_TOKEN_EOF; diff --git a/prism/templates/src/serialize.c.erb b/prism/templates/src/serialize.c.erb index 8fa70ffb5565d1..92fe9188f940c0 100644 --- a/prism/templates/src/serialize.c.erb +++ b/prism/templates/src/serialize.c.erb @@ -134,8 +134,7 @@ pm_serialize_comment(pm_parser_t *parser, pm_comment_t *comment, pm_buffer_t *bu pm_buffer_append_byte(buffer, (uint8_t) comment->type); // serialize location - pm_buffer_append_varuint(buffer, pm_ptrdifft_to_u32(comment->start - parser->start)); - pm_buffer_append_varuint(buffer, pm_ptrdifft_to_u32(comment->end - comment->start)); + pm_serialize_location(parser, &comment->location, buffer); } /** @@ -190,8 +189,7 @@ pm_serialize_diagnostic(pm_parser_t *parser, pm_diagnostic_t *diagnostic, pm_buf pm_buffer_append_string(buffer, diagnostic->message, message_length); // serialize location - pm_buffer_append_varuint(buffer, pm_ptrdifft_to_u32(diagnostic->start - parser->start)); - pm_buffer_append_varuint(buffer, pm_ptrdifft_to_u32(diagnostic->end - diagnostic->start)); + pm_serialize_location(parser, &diagnostic->location, buffer); } static void