Skip to content

Commit

Permalink
Add a boolean to warnings to categorize as $VERBOSE == true / $VERBOS…
Browse files Browse the repository at this point in the history
…E == false warning

* Fixes ruby#2082
  • Loading branch information
eregon committed Jan 24, 2024
1 parent 5e28ff5 commit 7f84739
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 20 deletions.
18 changes: 13 additions & 5 deletions docs/serialization.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,20 @@ 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 |

## warning

| # bytes | field |
| --- | --- |
| string | warning message (ASCII-only characters) |
| location | the location in the source this warning applies to |
| `1` | Whether this warning should only be shown when `$VERBOSE == true` |

## Structure

Expand All @@ -82,9 +90,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
5 changes: 3 additions & 2 deletions ext/prism/extension.c
Original file line number Diff line number Diff line change
Expand Up @@ -430,10 +430,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),
warning->verbose_only ? Qtrue : Qfalse
};

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
5 changes: 5 additions & 0 deletions include/prism/diagnostic.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ typedef struct {
* diagnostic is freed.
*/
bool owned;

/**
* Only applies to warnings. Whether this warning should only be shown when $VERBOSE == true.
*/
bool verbose_only;
} pm_diagnostic_t;

/**
Expand Down
4 changes: 3 additions & 1 deletion java/org/prism/ParseResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ public Error(String message, Nodes.Location location) {
public static final class Warning {
public final String message;
public final Nodes.Location location;
public final boolean verboseOnly;

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

Expand Down
10 changes: 7 additions & 3 deletions lib/prism/parse_result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -337,20 +337,24 @@ class ParseWarning
# A Location object representing the location of this warning in the source.
attr_reader :location

# Whether this warning should only be shown when `$VERBOSE == true`
attr_reader :verbose_only

# Create a new warning object with the given message and location.
def initialize(message, location)
def initialize(message, location, verbose_only)
@message = message
@location = location
@verbose_only = verbose_only
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} @verbose_only=#{@verbose_only}>"
end
end

Expand Down
3 changes: 2 additions & 1 deletion templates/java/org/prism/Loader.java.erb
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,9 @@ public class Loader {
byte[] bytes = loadEmbeddedString();
String message = new String(bytes, StandardCharsets.US_ASCII);
Nodes.Location location = loadLocation();
boolean verboseOnly = buffer.get() != 0;

ParseResult.Warning warning = new ParseResult.Warning(message, location);
ParseResult.Warning warning = new ParseResult.Warning(message, location, verboseOnly);
warnings[i] = warning;
}

Expand Down
2 changes: 1 addition & 1 deletion templates/lib/prism/serialize.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ module Prism
magic_comments = load_varuint.times.map { MagicComment.new(load_location, load_location) }
data_loc = load_optional_location
errors = load_varuint.times.map { ParseError.new(load_embedded_string, load_location) }
warnings = load_varuint.times.map { ParseWarning.new(load_embedded_string, load_location) }
warnings = load_varuint.times.map { ParseWarning.new(load_embedded_string, load_location, io.getbyte != 0) }
[comments, magic_comments, data_loc, errors, warnings]
end

Expand Down
18 changes: 11 additions & 7 deletions templates/src/serialize.c.erb
Original file line number Diff line number Diff line change
Expand Up @@ -182,23 +182,27 @@ pm_serialize_data_loc(const pm_parser_t *parser, pm_buffer_t *buffer) {
}

static void
pm_serialize_diagnostic(pm_parser_t *parser, pm_diagnostic_t *diagnostic, pm_buffer_t *buffer) {
pm_serialize_diagnostic(pm_parser_t *parser, pm_diagnostic_t *diagnostic, pm_buffer_t *buffer, bool warnings) {
// serialize message
size_t message_length = strlen(diagnostic->message);
pm_buffer_append_varuint(buffer, pm_sizet_to_u32(message_length));
pm_buffer_append_string(buffer, diagnostic->message, message_length);

// serialize location
pm_serialize_location(parser, &diagnostic->location, buffer);

if (warnings) {
pm_buffer_append_byte(buffer, diagnostic->verbose_only ? 1 : 0);
}
}

static void
pm_serialize_diagnostic_list(pm_parser_t *parser, pm_list_t *list, pm_buffer_t *buffer) {
pm_serialize_diagnostic_list(pm_parser_t *parser, pm_list_t *list, pm_buffer_t *buffer, bool warnings) {
pm_buffer_append_varuint(buffer, pm_sizet_to_u32(pm_list_size(list)));

pm_diagnostic_t *diagnostic;
for (diagnostic = (pm_diagnostic_t *) list->head; diagnostic != NULL; diagnostic = (pm_diagnostic_t *) diagnostic->node.next) {
pm_serialize_diagnostic(parser, diagnostic, buffer);
pm_serialize_diagnostic(parser, diagnostic, buffer, warnings);
}
}

Expand All @@ -225,8 +229,8 @@ pm_serialize_content(pm_parser_t *parser, pm_node_t *node, pm_buffer_t *buffer)
<%- end -%>
pm_serialize_magic_comment_list(parser, &parser->magic_comment_list, buffer);
pm_serialize_data_loc(parser, buffer);
pm_serialize_diagnostic_list(parser, &parser->error_list, buffer);
pm_serialize_diagnostic_list(parser, &parser->warning_list, buffer);
pm_serialize_diagnostic_list(parser, &parser->error_list, buffer, false);
pm_serialize_diagnostic_list(parser, &parser->warning_list, buffer, true);

// Here we're going to leave space for the offset of the constant pool in
// the buffer.
Expand Down Expand Up @@ -322,8 +326,8 @@ pm_serialize_lex(pm_buffer_t *buffer, const uint8_t *source, size_t size, const
pm_serialize_comment_list(&parser, &parser.comment_list, buffer);
pm_serialize_magic_comment_list(&parser, &parser.magic_comment_list, buffer);
pm_serialize_data_loc(&parser, buffer);
pm_serialize_diagnostic_list(&parser, &parser.error_list, buffer);
pm_serialize_diagnostic_list(&parser, &parser.warning_list, buffer);
pm_serialize_diagnostic_list(&parser, &parser.error_list, buffer, false);
pm_serialize_diagnostic_list(&parser, &parser.warning_list, buffer, true);

pm_node_destroy(&parser, node);
pm_parser_free(&parser);
Expand Down

0 comments on commit 7f84739

Please sign in to comment.