diff options
author | Kevin Newton <[email protected]> | 2024-02-05 14:41:28 -0500 |
---|---|---|
committer | Kevin Newton <[email protected]> | 2024-02-05 15:18:15 -0500 |
commit | c5694c647afbacaf2d9e570b106d23f533601294 (patch) | |
tree | b6320bb95e572c1c234f79a238ee41c57901d649 /prism_compile.c | |
parent | 10a182f597d450d75ac3ef10f8dbdd28ed5fc9fa (diff) |
[PRISM] Raise ArgumentError for invalid encoding
Diffstat (limited to 'prism_compile.c')
-rw-r--r-- | prism_compile.c | 29 |
1 files changed, 25 insertions, 4 deletions
diff --git a/prism_compile.c b/prism_compile.c index a05b716bfd..4ba1f9a336 100644 --- a/prism_compile.c +++ b/prism_compile.c @@ -7656,17 +7656,38 @@ pm_parse_input(pm_parse_result_t *result, VALUE filepath) // If there are errors, raise an appropriate error and free the result. if (result->parser.error_list.size > 0) { + // Determine the error to raise. Any errors with the level + // PM_ERROR_LEVEL_ARGUMENT effectively take over as the only argument + // that gets raised. + const pm_diagnostic_t *error_argument = NULL; + for (const pm_diagnostic_t *error = (pm_diagnostic_t *) result->parser.error_list.head; error != NULL; error = (pm_diagnostic_t *) error->node.next) { + if (error->level == PM_ERROR_LEVEL_ARGUMENT) { + error_argument = error; + break; + } + } + + VALUE error_class; pm_buffer_t buffer = { 0 }; - pm_parser_errors_format(&result->parser, &buffer, rb_stderr_tty_p()); - pm_buffer_prepend_string(&buffer, "syntax errors found\n", 20); - VALUE error = rb_exc_new(rb_eSyntaxError, pm_buffer_value(&buffer), pm_buffer_length(&buffer)); + // If we found an error argument, then we need to use that as the error + // message. Otherwise, we will format all of the parser error together. + if (error_argument == NULL) { + error_class = rb_eSyntaxError; + pm_buffer_append_string(&buffer, "syntax errors found\n", 20); + pm_parser_errors_format(&result->parser, &buffer, rb_stderr_tty_p()); + } + else { + error_class = rb_eArgError; + pm_buffer_append_string(&buffer, error_argument->message, strlen(error_argument->message)); + } + VALUE error_object = rb_exc_new(error_class, pm_buffer_value(&buffer), pm_buffer_length(&buffer)); pm_buffer_free(&buffer); // TODO: We need to set the backtrace. // rb_funcallv(error, rb_intern("set_backtrace"), 1, &path); - return error; + return error_object; } // Emit all of the various warnings from the parse. |