Skip to content

Commit

Permalink
Merge pull request #2265 from eregon/warnings-verbosity
Browse files Browse the repository at this point in the history
Add level to warnings and errors to categorize them
  • Loading branch information
kddnewton authored Jan 26, 2024
2 parents 3b22399 + 7a74576 commit 5c0b38f
Show file tree
Hide file tree
Showing 12 changed files with 373 additions and 247 deletions.
3 changes: 2 additions & 1 deletion bin/parse
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

$:.unshift(File.expand_path("../lib", __dir__))
require "prism"
require "pp"

if ARGV[0] == "-e"
result = Prism.parse(ARGV[1])
Expand Down Expand Up @@ -36,6 +37,6 @@ else
parts.each_with_index do |(key, value), index|
puts if index > 0
puts "#{key}:"
puts value.inspect
pp value
end
end
19 changes: 14 additions & 5 deletions docs/serialization.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,21 @@ The comment type is one of:
| location | the location of the key of the magic comment |
| location | the location of the value of the magic comment |

### diagnostic
### error

| # bytes | field |
| --- | --- |
| string | diagnostic message (ASCII-only characters) |
| location | the location in the source this diagnostic applies to |
| string | error message (ASCII-only characters) |
| location | the location in the source this error applies to |
| `1` | the level of the error, see pm_diagnostic_level_t for the values |

## warning

| # bytes | field |
| --- | --- |
| string | warning message (ASCII-only characters) |
| location | the location in the source this warning applies to |
| `1` | the level of the warning, see pm_diagnostic_level_t for the values |

## Structure

Expand All @@ -82,9 +91,9 @@ The header is structured like the following table:
| magic comment* | magic comments |
| location? | the optional location of the `__END__` keyword and its contents |
| varuint | number of errors |
| diagnostic* | errors |
| error* | errors |
| varuint | number of warnings |
| diagnostic* | warnings |
| warning* | warnings |
| `4` | content pool offset |
| varuint | content pool size |

Expand Down
23 changes: 19 additions & 4 deletions ext/prism/extension.c
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,19 @@ parser_magic_comments(pm_parser_t *parser, VALUE source) {
return magic_comments;
}

static VALUE get_diagnostic_level_symbol(uint8_t level) {
switch (level) {
case 0:
return ID2SYM(rb_intern("error_default"));
case 1:
return ID2SYM(rb_intern("warning_verbose_not_nil"));
case 2:
return ID2SYM(rb_intern("warning_verbose_true"));
default:
rb_raise(rb_eRuntimeError, "Unknown level: %" PRIu8, level);
}
}

/**
* Extract out the data location from the parser into a Location instance if one
* exists.
Expand Down Expand Up @@ -404,10 +417,11 @@ parser_errors(pm_parser_t *parser, rb_encoding *encoding, VALUE source) {

VALUE error_argv[] = {
rb_enc_str_new_cstr(error->message, encoding),
rb_class_new_instance(3, location_argv, rb_cPrismLocation)
rb_class_new_instance(3, location_argv, rb_cPrismLocation),
get_diagnostic_level_symbol(error->level)
};

rb_ary_push(errors, rb_class_new_instance(2, error_argv, rb_cPrismParseError));
rb_ary_push(errors, rb_class_new_instance(3, error_argv, rb_cPrismParseError));
}

return errors;
Expand All @@ -430,10 +444,11 @@ parser_warnings(pm_parser_t *parser, rb_encoding *encoding, VALUE source) {

VALUE warning_argv[] = {
rb_enc_str_new_cstr(warning->message, encoding),
rb_class_new_instance(3, location_argv, rb_cPrismLocation)
rb_class_new_instance(3, location_argv, rb_cPrismLocation),
get_diagnostic_level_symbol(warning->level)
};

rb_ary_push(warnings, rb_class_new_instance(2, warning_argv, rb_cPrismParseWarning));
rb_ary_push(warnings, rb_class_new_instance(3, warning_argv, rb_cPrismParseWarning));
}

return warnings;
Expand Down
15 changes: 15 additions & 0 deletions include/prism/diagnostic.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@
#include <stdlib.h>
#include <assert.h>

/**
* This enum represents the level of diagnostics generated during parsing.
*/
typedef enum {
/** The default level for errors. */
PM_ERROR_DEFAULT = 0,
/** For warnings which should be emitted if $VERBOSE != nil. */
PM_WARNING_VERBOSE_NOT_NIL = 1,
/** For warnings which should be emitted if $VERBOSE == true. */
PM_WARNING_VERBOSE_TRUE = 2
} pm_diagnostic_level_t;

/**
* This struct represents a diagnostic generated during parsing.
*
Expand All @@ -35,6 +47,9 @@ typedef struct {
* diagnostic is freed.
*/
bool owned;

/** The level of the diagnostic, see pm_diagnostic_level_t for possible values. */
uint8_t level;
} pm_diagnostic_t;

/**
Expand Down
19 changes: 17 additions & 2 deletions java/org/prism/ParseResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,38 @@ public MagicComment(Nodes.Location keyLocation, Nodes.Location valueLocation) {
}
}

public enum DiagnosticLevel {
/** The default level for errors. */
ERROR_DEFAULT,
/** For warnings which should be emitted if $VERBOSE != nil. */
WARNING_VERBOSE_NOT_NIL,
/** For warnings which should be emitted if $VERBOSE == true. */
WARNING_VERBOSE_TRUE
}

public static DiagnosticLevel[] DIAGNOSTIC_LEVELS = DiagnosticLevel.values();

public static final class Error {
public final String message;
public final Nodes.Location location;
public final DiagnosticLevel level;

public Error(String message, Nodes.Location location) {
public Error(String message, Nodes.Location location, DiagnosticLevel level) {
this.message = message;
this.location = location;
this.level = level;
}
}

public static final class Warning {
public final String message;
public final Nodes.Location location;
public final DiagnosticLevel level;

public Warning(String message, Nodes.Location location) {
public Warning(String message, Nodes.Location location, DiagnosticLevel level) {
this.message = message;
this.location = location;
this.level = level;
}
}

Expand Down
18 changes: 13 additions & 5 deletions lib/prism/parse_result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -312,10 +312,14 @@ class ParseError
# A Location object representing the location of this error in the source.
attr_reader :location

# The level of this error
attr_reader :level

# Create a new error object with the given message and location.
def initialize(message, location)
def initialize(message, location, level)
@message = message
@location = location
@level = level
end

# Implement the hash pattern matching interface for ParseError.
Expand All @@ -325,7 +329,7 @@ def deconstruct_keys(keys)

# Returns a string representation of this error.
def inspect
"#<Prism::ParseError @message=#{@message.inspect} @location=#{@location.inspect}>"
"#<Prism::ParseError @message=#{@message.inspect} @location=#{@location.inspect} @level=#{@level}>"
end
end

Expand All @@ -337,20 +341,24 @@ class ParseWarning
# A Location object representing the location of this warning in the source.
attr_reader :location

# The level of this warning
attr_reader :level

# Create a new warning object with the given message and location.
def initialize(message, location)
def initialize(message, location, level)
@message = message
@location = location
@level = level
end

# Implement the hash pattern matching interface for ParseWarning.
def deconstruct_keys(keys)
{ message: message, location: location }
{ message: message, location: location, verbose_only: verbose_only }
end

# Returns a string representation of this warning.
def inspect
"#<Prism::ParseWarning @message=#{@message.inspect} @location=#{@location.inspect}>"
"#<Prism::ParseWarning @message=#{@message.inspect} @location=#{@location.inspect} @level=#{@level}>"
end
end

Expand Down
Loading

0 comments on commit 5c0b38f

Please sign in to comment.