Skip to content

Commit bd3eeb3

Browse files
kddnewtoneregon
andcommitted
Error follow-up
Split up the diagnostic levels so that error and warning levels aren't mixed. Also fix up deconstruct_keys implementation. Co-authored-by: Benoit Daloze <eregontp@gmail.com>
1 parent 9b6907b commit bd3eeb3

File tree

11 files changed

+320
-287
lines changed

11 files changed

+320
-287
lines changed

bin/parse

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
$:.unshift(File.expand_path("../lib", __dir__))
1010
require "prism"
11-
require "pp"
1211

1312
if ARGV[0] == "-e"
1413
result = Prism.parse(ARGV[1])

docs/serialization.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,15 @@ The comment type is one of:
5757
| --- | --- |
5858
| string | error message (ASCII-only characters) |
5959
| location | the location in the source this error applies to |
60-
| `1` | the level of the error, see pm_diagnostic_level_t for the values |
60+
| `1` | the level of the error: `0` for `fatal` |
6161

6262
## warning
6363

6464
| # bytes | field |
6565
| --- | --- |
6666
| string | warning message (ASCII-only characters) |
6767
| location | the location in the source this warning applies to |
68-
| `1` | the level of the warning, see pm_diagnostic_level_t for the values |
68+
| `1` | the level of the warning: `0` for `default` and `1` for `verbose` |
6969

7070
## Structure
7171

ext/prism/extension.c

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -368,19 +368,6 @@ parser_magic_comments(pm_parser_t *parser, VALUE source) {
368368
return magic_comments;
369369
}
370370

371-
static VALUE get_diagnostic_level_symbol(uint8_t level) {
372-
switch (level) {
373-
case 0:
374-
return ID2SYM(rb_intern("error_default"));
375-
case 1:
376-
return ID2SYM(rb_intern("warning_verbose_not_nil"));
377-
case 2:
378-
return ID2SYM(rb_intern("warning_verbose_true"));
379-
default:
380-
rb_raise(rb_eRuntimeError, "Unknown level: %" PRIu8, level);
381-
}
382-
}
383-
384371
/**
385372
* Extract out the data location from the parser into a Location instance if one
386373
* exists.
@@ -415,10 +402,19 @@ parser_errors(pm_parser_t *parser, rb_encoding *encoding, VALUE source) {
415402
LONG2FIX(error->location.end - error->location.start)
416403
};
417404

405+
VALUE level = Qnil;
406+
switch (error->level) {
407+
case PM_ERROR_LEVEL_FATAL:
408+
level = ID2SYM(rb_intern("fatal"));
409+
break;
410+
default:
411+
rb_raise(rb_eRuntimeError, "Unknown level: %" PRIu8, error->level);
412+
}
413+
418414
VALUE error_argv[] = {
419415
rb_enc_str_new_cstr(error->message, encoding),
420416
rb_class_new_instance(3, location_argv, rb_cPrismLocation),
421-
get_diagnostic_level_symbol(error->level)
417+
level
422418
};
423419

424420
rb_ary_push(errors, rb_class_new_instance(3, error_argv, rb_cPrismParseError));
@@ -442,10 +438,22 @@ parser_warnings(pm_parser_t *parser, rb_encoding *encoding, VALUE source) {
442438
LONG2FIX(warning->location.end - warning->location.start)
443439
};
444440

441+
VALUE level = Qnil;
442+
switch (warning->level) {
443+
case PM_WARNING_LEVEL_DEFAULT:
444+
level = ID2SYM(rb_intern("default"));
445+
break;
446+
case PM_WARNING_LEVEL_VERBOSE:
447+
level = ID2SYM(rb_intern("verbose"));
448+
break;
449+
default:
450+
rb_raise(rb_eRuntimeError, "Unknown level: %" PRIu8, warning->level);
451+
}
452+
445453
VALUE warning_argv[] = {
446454
rb_enc_str_new_cstr(warning->message, encoding),
447455
rb_class_new_instance(3, location_argv, rb_cPrismLocation),
448-
get_diagnostic_level_symbol(warning->level)
456+
level
449457
};
450458

451459
rb_ary_push(warnings, rb_class_new_instance(3, warning_argv, rb_cPrismParseWarning));

include/prism/diagnostic.h

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,22 @@
1515
#include <assert.h>
1616

1717
/**
18-
* This enum represents the level of diagnostics generated during parsing.
18+
* The levels of errors generated during parsing.
19+
*/
20+
typedef enum {
21+
/** For errors that cannot be recovered from. */
22+
PM_ERROR_LEVEL_FATAL = 0
23+
} pm_error_level_t;
24+
25+
/**
26+
* The levels of warnings generated during parsing.
1927
*/
2028
typedef enum {
21-
/** The default level for errors. */
22-
PM_ERROR_DEFAULT = 0,
2329
/** For warnings which should be emitted if $VERBOSE != nil. */
24-
PM_WARNING_VERBOSE_NOT_NIL = 1,
30+
PM_WARNING_LEVEL_DEFAULT = 0,
2531
/** For warnings which should be emitted if $VERBOSE == true. */
26-
PM_WARNING_VERBOSE_TRUE = 2
27-
} pm_diagnostic_level_t;
32+
PM_WARNING_LEVEL_VERBOSE = 1
33+
} pm_warning_level_t;
2834

2935
/**
3036
* This struct represents a diagnostic generated during parsing.
@@ -48,7 +54,10 @@ typedef struct {
4854
*/
4955
bool owned;
5056

51-
/** The level of the diagnostic, see pm_diagnostic_level_t for possible values. */
57+
/**
58+
* The level of the diagnostic, see `pm_error_level_t` and
59+
* `pm_warning_level_t` for possible values.
60+
*/
5261
uint8_t level;
5362
} pm_diagnostic_t;
5463

java/org/prism/ParseResult.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,40 @@ public MagicComment(Nodes.Location keyLocation, Nodes.Location valueLocation) {
1313
}
1414
}
1515

16-
public enum DiagnosticLevel {
17-
/** The default level for errors. */
18-
ERROR_DEFAULT,
19-
/** For warnings which should be emitted if $VERBOSE != nil. */
20-
WARNING_VERBOSE_NOT_NIL,
21-
/** For warnings which should be emitted if $VERBOSE == true. */
22-
WARNING_VERBOSE_TRUE
16+
public enum ErrorLevel {
17+
/** For errors that cannot be recovered from. */
18+
ERROR_FATAL
2319
}
2420

25-
public static DiagnosticLevel[] DIAGNOSTIC_LEVELS = DiagnosticLevel.values();
21+
public static ErrorLevel[] ERROR_LEVELS = ErrorLevel.values();
2622

2723
public static final class Error {
2824
public final String message;
2925
public final Nodes.Location location;
30-
public final DiagnosticLevel level;
26+
public final ErrorLevel level;
3127

32-
public Error(String message, Nodes.Location location, DiagnosticLevel level) {
28+
public Error(String message, Nodes.Location location, ErrorLevel level) {
3329
this.message = message;
3430
this.location = location;
3531
this.level = level;
3632
}
3733
}
3834

35+
public enum WarningLevel {
36+
/** For warnings which should be emitted if $VERBOSE != nil. */
37+
WARNING_DEFAULT,
38+
/** For warnings which should be emitted if $VERBOSE == true. */
39+
WARNING_VERBOSE
40+
}
41+
42+
public static WarningLevel[] WARNING_LEVELS = WarningLevel.values();
43+
3944
public static final class Warning {
4045
public final String message;
4146
public final Nodes.Location location;
42-
public final DiagnosticLevel level;
47+
public final WarningLevel level;
4348

44-
public Warning(String message, Nodes.Location location, DiagnosticLevel level) {
49+
public Warning(String message, Nodes.Location location, WarningLevel level) {
4550
this.message = message;
4651
this.location = location;
4752
this.level = level;

lib/prism/parse_result.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ class ParseError
312312
# A Location object representing the location of this error in the source.
313313
attr_reader :location
314314

315-
# The level of this error
315+
# The level of this error.
316316
attr_reader :level
317317

318318
# Create a new error object with the given message and location.
@@ -324,7 +324,7 @@ def initialize(message, location, level)
324324

325325
# Implement the hash pattern matching interface for ParseError.
326326
def deconstruct_keys(keys)
327-
{ message: message, location: location }
327+
{ message: message, location: location, level: level }
328328
end
329329

330330
# Returns a string representation of this error.
@@ -341,7 +341,7 @@ class ParseWarning
341341
# A Location object representing the location of this warning in the source.
342342
attr_reader :location
343343

344-
# The level of this warning
344+
# The level of this warning.
345345
attr_reader :level
346346

347347
# Create a new warning object with the given message and location.
@@ -353,7 +353,7 @@ def initialize(message, location, level)
353353

354354
# Implement the hash pattern matching interface for ParseWarning.
355355
def deconstruct_keys(keys)
356-
{ message: message, location: location, verbose_only: verbose_only }
356+
{ message: message, location: location, level: level }
357357
end
358358

359359
# Returns a string representation of this warning.

0 commit comments

Comments
 (0)