Skip to content

Commit

Permalink
Merge pull request #2362 from ruby/raise-arg-errors
Browse files Browse the repository at this point in the history
Add another error type for raising argument errors
  • Loading branch information
kddnewton authored Feb 5, 2024
2 parents ff4f83c + f3030cb commit ed337ad
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 6 deletions.
3 changes: 3 additions & 0 deletions ext/prism/extension.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
6 changes: 5 additions & 1 deletion include/prism/diagnostic.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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;
Expand Down
6 changes: 5 additions & 1 deletion java/org/prism/ParseResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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
}
Expand Down
10 changes: 7 additions & 3 deletions src/diagnostic.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,22 @@ 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:
*
* * `PM_WARNING_LEVEL_DEFAULT` - Warnings that appear for `ruby -c -e 'code'`.
* * `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 },
Expand Down Expand Up @@ -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 },
Expand Down
2 changes: 1 addition & 1 deletion templates/javascript/src/deserialize.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export class ParseResult {
}
}

const errorLevels = ["fatal"];
const errorLevels = ["fatal", "argument"];
const warningLevels = ["default", "verbose"];

/**
Expand Down
2 changes: 2 additions & 0 deletions templates/lib/prism/serialize.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ module Prism
case level
when 0
:fatal
when 1
:argument
else
raise "Unknown level: #{level}"
end
Expand Down

0 comments on commit ed337ad

Please sign in to comment.