[ruby/prism] Refactor pm_diagnostic_t and pm_comment_t to use pm_location_t

https://github.com/ruby/prism/commit/115b6a2fc6
This commit is contained in:
Lily Lyons 2023-12-01 13:00:56 -08:00 коммит произвёл git
Родитель 2a65d83707
Коммит 1227b6d912
6 изменённых файлов: 18 добавлений и 28 удалений

Просмотреть файл

@ -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
};

Просмотреть файл

@ -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;

Просмотреть файл

@ -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[] = {

Просмотреть файл

@ -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;

Просмотреть файл

@ -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;

Просмотреть файл

@ -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