mirror of
https://github.com/ruby/ruby.git
synced 2026-10-28 16:36:20 +08:00
[PRISM] Use the new iteration APIs for errors and warnings
This commit is contained in:
1 parent
f25e997399
commit
16130709da
2 files changed
+29
-40
Notes:
git
2026-10-20 16:29:00 +00:00
No files matched your search
+17
-28
@@ -10815,27 +10815,6 @@ pm_intern_constants_callback(const pm_constant_t *constant, void *data)
|
||||
ctx->constants[ctx->index++] = rb_intern3((const char *) pm_constant_start(constant), pm_constant_length(constant), ctx->encoding);
|
||||
}
|
||||
|
||||
/** Context for emitting warnings via callback. */
|
||||
typedef struct {
|
||||
const pm_parser_t *parser;
|
||||
rb_encoding *encoding;
|
||||
const char *filepath;
|
||||
} pm_warning_emit_ctx_t;
|
||||
|
||||
static void
|
||||
pm_warning_emit_callback(const pm_diagnostic_t *diagnostic, void *data) {
|
||||
pm_warning_emit_ctx_t *ctx = (pm_warning_emit_ctx_t *) data;
|
||||
pm_location_t loc = pm_diagnostic_location(diagnostic);
|
||||
int line = pm_location_line_number(ctx->parser, &loc);
|
||||
|
||||
if (pm_diagnostic_warning_level(diagnostic) == PM_WARNING_LEVEL_VERBOSE) {
|
||||
rb_enc_compile_warning(ctx->encoding, ctx->filepath, line, "%s", pm_diagnostic_message(diagnostic));
|
||||
}
|
||||
else {
|
||||
rb_enc_compile_warn(ctx->encoding, ctx->filepath, line, "%s", pm_diagnostic_message(diagnostic));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the source hash of the source that was parsed, as stored on
|
||||
* instruction sequences. A source hash of 0 means that there is no source hash,
|
||||
@@ -10890,13 +10869,23 @@ pm_parse_process(pm_parse_result_t *result, pm_node_t *node, VALUE *script_lines
|
||||
scope_node->script_lines = script_lines;
|
||||
}
|
||||
|
||||
// Emit all of the various warnings from the parse.
|
||||
pm_warning_emit_ctx_t warning_ctx = {
|
||||
.parser = parser,
|
||||
.encoding = scope_node->encoding,
|
||||
.filepath = (const char *) pm_string_source(pm_parser_filepath(parser))
|
||||
};
|
||||
pm_parser_warnings_each(parser, pm_warning_emit_callback, &warning_ctx);
|
||||
const char *filepath = (const char *) pm_string_source(pm_parser_filepath(parser));
|
||||
|
||||
for (
|
||||
const pm_diagnostic_t *diagnostic = pm_parser_warnings_first(parser);
|
||||
diagnostic != NULL;
|
||||
diagnostic = pm_diagnostic_next(diagnostic)
|
||||
) {
|
||||
pm_location_t loc = pm_diagnostic_location(diagnostic);
|
||||
int line = pm_location_line_number(parser, &loc);
|
||||
|
||||
if (pm_diagnostic_warning_level(diagnostic) == PM_WARNING_LEVEL_VERBOSE) {
|
||||
rb_enc_compile_warning(scope_node->encoding, filepath, line, "%s", pm_diagnostic_message(diagnostic));
|
||||
}
|
||||
else {
|
||||
rb_enc_compile_warn(scope_node->encoding, filepath, line, "%s", pm_diagnostic_message(diagnostic));
|
||||
}
|
||||
}
|
||||
|
||||
// If there are errors, raise an appropriate error and free the result.
|
||||
if (pm_parser_errors_size(parser) > 0) {
|
||||
|
||||
+12
-12
@@ -18,15 +18,6 @@ void *ruby_xrealloc_sized(void *ptr, size_t newsiz, size_t _oldsiz) { return rea
|
||||
|
||||
#include "prism.h"
|
||||
|
||||
static void
|
||||
print_error(const pm_diagnostic_t *diagnostic, void *data)
|
||||
{
|
||||
const pm_parser_t *parser = (const pm_parser_t *) data;
|
||||
pm_location_t loc = pm_diagnostic_location(diagnostic);
|
||||
const pm_line_column_t line_column = pm_line_offset_list_line_column(pm_parser_line_offsets(parser), loc.start, pm_parser_start_line(parser));
|
||||
fprintf(stderr, "%" PRIi32 ":%" PRIu32 ":%s\n", line_column.line, line_column.column, pm_diagnostic_message(diagnostic));
|
||||
}
|
||||
|
||||
static void
|
||||
usage(const char *prog)
|
||||
{
|
||||
@@ -105,10 +96,19 @@ main(int argc, const char *argv[])
|
||||
pm_node_t *node = pm_parse(parser);
|
||||
int exit_status;
|
||||
|
||||
if (pm_parser_errors_size(parser) > 0)
|
||||
{
|
||||
if (pm_parser_errors_size(parser) > 0) {
|
||||
fprintf(stderr, "error parsing %s\n", filepath);
|
||||
pm_parser_errors_each(parser, print_error, parser);
|
||||
|
||||
for (
|
||||
const pm_diagnostic_t *diagnostic = pm_parser_errors_first(parser);
|
||||
diagnostic != NULL;
|
||||
diagnostic = pm_diagnostic_next(diagnostic)
|
||||
) {
|
||||
pm_location_t loc = pm_diagnostic_location(diagnostic);
|
||||
const pm_line_column_t line_column = pm_line_offset_list_line_column(pm_parser_line_offsets(parser), loc.start, pm_parser_start_line(parser));
|
||||
fprintf(stderr, "%" PRIi32 ":%" PRIu32 ":%s\n", line_column.line, line_column.column, pm_diagnostic_message(diagnostic));
|
||||
}
|
||||
|
||||
exit_status = EXIT_FAILURE;
|
||||
}
|
||||
else {
|
||||
|
||||
Reference in new issue
Block a user