Skip to content

Commit

Permalink
[ruby/prism] Refactor pm_diagnostic_t and pm_comment_t to use pm_loca…
Browse files Browse the repository at this point in the history
  • Loading branch information
Speak2Erase authored and matzbot committed Dec 4, 2023
1 parent 2a65d83 commit 1227b6d
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 28 deletions.
6 changes: 2 additions & 4 deletions prism/diagnostic.c
Expand Up @@ -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
};
Expand Down Expand Up @@ -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
};
Expand Down
8 changes: 3 additions & 5 deletions prism/diagnostic.h
Expand Up @@ -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"

Expand All @@ -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;
Expand Down
12 changes: 6 additions & 6 deletions prism/extension.c
Expand Up @@ -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;
Expand Down Expand Up @@ -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[] = {
Expand All @@ -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[] = {
Expand Down
7 changes: 2 additions & 5 deletions prism/parser.h
Expand Up @@ -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;
Expand Down
7 changes: 3 additions & 4 deletions prism/prism.c
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
6 changes: 2 additions & 4 deletions prism/templates/src/serialize.c.erb
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1227b6d

Please sign in to comment.