From f3030cb2b2e8ebfd630e2b0d4303ef8173a99722 Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Mon, 5 Feb 2024 13:24:41 -0500 Subject: [PATCH] Add another error type for raising argument errors --- ext/prism/extension.c | 3 +++ include/prism/diagnostic.h | 6 +++++- java/org/prism/ParseResult.java | 6 +++++- src/diagnostic.c | 10 +++++++--- templates/javascript/src/deserialize.js.erb | 2 +- templates/lib/prism/serialize.rb.erb | 2 ++ 6 files changed, 23 insertions(+), 6 deletions(-) diff --git a/ext/prism/extension.c b/ext/prism/extension.c index e1fcfef20a5..7cad38404e5 100644 --- a/ext/prism/extension.c +++ b/ext/prism/extension.c @@ -404,6 +404,9 @@ parser_errors(pm_parser_t *parser, rb_encoding *encoding, VALUE source) { case PM_ERROR_LEVEL_FATAL: level = ID2SYM(rb_intern("fatal")); break; + case PM_ERROR_LEVEL_ARGUMENT: + level = ID2SYM(rb_intern("argument")); + break; default: rb_raise(rb_eRuntimeError, "Unknown level: %" PRIu8, error->level); } diff --git a/include/prism/diagnostic.h b/include/prism/diagnostic.h index 8f09fb90990..35a5c88793e 100644 --- a/include/prism/diagnostic.h +++ b/include/prism/diagnostic.h @@ -19,7 +19,10 @@ */ typedef enum { /** For errors that cannot be recovered from. */ - PM_ERROR_LEVEL_FATAL = 0 + PM_ERROR_LEVEL_FATAL = 0, + + /** For errors that should raise an argument error. */ + PM_ERROR_LEVEL_ARGUMENT = 1 } pm_error_level_t; /** @@ -28,6 +31,7 @@ typedef enum { typedef enum { /** For warnings which should be emitted if $VERBOSE != nil. */ PM_WARNING_LEVEL_DEFAULT = 0, + /** For warnings which should be emitted if $VERBOSE == true. */ PM_WARNING_LEVEL_VERBOSE = 1 } pm_warning_level_t; diff --git a/java/org/prism/ParseResult.java b/java/org/prism/ParseResult.java index a652394d4e7..dc9008c93d6 100644 --- a/java/org/prism/ParseResult.java +++ b/java/org/prism/ParseResult.java @@ -15,7 +15,10 @@ public MagicComment(Nodes.Location keyLocation, Nodes.Location valueLocation) { public enum ErrorLevel { /** For errors that cannot be recovered from. */ - ERROR_FATAL + ERROR_FATAL, + + /** For errors that should raise ArgumentError. */ + ERROR_ARGUMENT, } public static ErrorLevel[] ERROR_LEVELS = ErrorLevel.values(); @@ -35,6 +38,7 @@ public Error(String message, Nodes.Location location, ErrorLevel level) { public enum WarningLevel { /** For warnings which should be emitted if $VERBOSE != nil. */ WARNING_DEFAULT, + /** For warnings which should be emitted if $VERBOSE == true. */ WARNING_VERBOSE } diff --git a/src/diagnostic.c b/src/diagnostic.c index 815a2b2a6ca..df7ae381ba6 100644 --- a/src/diagnostic.c +++ b/src/diagnostic.c @@ -63,7 +63,8 @@ typedef struct { * * For errors, they are: * - * * `PM_ERROR_LEVEL_FATAL` - The level for all errors. + * * `PM_ERROR_LEVEL_FATAL` - The default level for errors. + * * `PM_ERROR_LEVEL_ARGUMENT` - Errors that should raise ArgumentError. * * For warnings, they are: * @@ -71,9 +72,13 @@ typedef struct { * * `PM_WARNING_LEVEL_VERBOSE` - Warnings that appear with `-w`, as in `ruby -w -c -e 'code'`. */ static const pm_diagnostic_data_t diagnostic_messages[PM_DIAGNOSTIC_ID_LEN] = { + // Special error that can be replaced [PM_ERR_CANNOT_PARSE_EXPRESSION] = { "cannot parse the expression", PM_ERROR_LEVEL_FATAL }, - // Errors + // Errors that should raise argument errors + [PM_ERR_INVALID_ENCODING_MAGIC_COMMENT] = { "unknown or invalid encoding in the magic comment", PM_ERROR_LEVEL_ARGUMENT }, + + // Errors that should raise syntax errors [PM_ERR_ALIAS_ARGUMENT] = { "invalid argument being passed to `alias`; expected a bare word, symbol, constant, or global variable", PM_ERROR_LEVEL_FATAL }, [PM_ERR_AMPAMPEQ_MULTI_ASSIGN] = { "unexpected `&&=` in a multiple assignment", PM_ERROR_LEVEL_FATAL }, [PM_ERR_ARGUMENT_AFTER_BLOCK] = { "unexpected argument after a block argument", PM_ERROR_LEVEL_FATAL }, @@ -187,7 +192,6 @@ static const pm_diagnostic_data_t diagnostic_messages[PM_DIAGNOSTIC_ID_LEN] = { [PM_ERR_INCOMPLETE_QUESTION_MARK] = { "incomplete expression at `?`", PM_ERROR_LEVEL_FATAL }, [PM_ERR_INCOMPLETE_VARIABLE_CLASS] = { "incomplete class variable", PM_ERROR_LEVEL_FATAL }, [PM_ERR_INCOMPLETE_VARIABLE_INSTANCE] = { "incomplete instance variable", PM_ERROR_LEVEL_FATAL }, - [PM_ERR_INVALID_ENCODING_MAGIC_COMMENT] = { "unknown or invalid encoding in the magic comment", PM_ERROR_LEVEL_FATAL }, [PM_ERR_INVALID_FLOAT_EXPONENT] = { "invalid exponent", PM_ERROR_LEVEL_FATAL }, [PM_ERR_INVALID_NUMBER_BINARY] = { "invalid binary number", PM_ERROR_LEVEL_FATAL }, [PM_ERR_INVALID_NUMBER_DECIMAL] = { "invalid decimal number", PM_ERROR_LEVEL_FATAL }, diff --git a/templates/javascript/src/deserialize.js.erb b/templates/javascript/src/deserialize.js.erb index 196f54837a7..cb0f41cd135 100644 --- a/templates/javascript/src/deserialize.js.erb +++ b/templates/javascript/src/deserialize.js.erb @@ -184,7 +184,7 @@ export class ParseResult { } } -const errorLevels = ["fatal"]; +const errorLevels = ["fatal", "argument"]; const warningLevels = ["default", "verbose"]; /** diff --git a/templates/lib/prism/serialize.rb.erb b/templates/lib/prism/serialize.rb.erb index 37fc46d8841..205cbc45ec0 100644 --- a/templates/lib/prism/serialize.rb.erb +++ b/templates/lib/prism/serialize.rb.erb @@ -237,6 +237,8 @@ module Prism case level when 0 :fatal + when 1 + :argument else raise "Unknown level: #{level}" end