[ruby/prism] Ensure deserialization works with errors+warnings>256

https://github.com/ruby/prism/commit/f540e830b5
This commit is contained in:
Kevin Newton 2024-03-28 10:47:04 -04:00
Родитель 35ff302893
Коммит d583616f32
4 изменённых файлов: 28 добавлений и 4 удалений

Просмотреть файл

@ -451,6 +451,9 @@ parser_errors(pm_parser_t *parser, rb_encoding *encoding, VALUE source) {
case PM_ERROR_LEVEL_ARGUMENT: case PM_ERROR_LEVEL_ARGUMENT:
level = ID2SYM(rb_intern("argument")); level = ID2SYM(rb_intern("argument"));
break; break;
case PM_ERROR_LEVEL_LOAD:
level = ID2SYM(rb_intern("load"));
break;
default: default:
rb_raise(rb_eRuntimeError, "Unknown level: %" PRIu8, error->level); rb_raise(rb_eRuntimeError, "Unknown level: %" PRIu8, error->level);
} }

Просмотреть файл

@ -131,8 +131,8 @@ module Prism
comments = load_comments comments = load_comments
magic_comments = Array.new(load_varuint) { MagicComment.new(load_location_object, load_location_object) } magic_comments = Array.new(load_varuint) { MagicComment.new(load_location_object, load_location_object) }
data_loc = load_optional_location_object data_loc = load_optional_location_object
errors = Array.new(load_varuint) { ParseError.new(DIAGNOSTIC_TYPES[io.getbyte], load_embedded_string, load_location_object, load_error_level) } errors = Array.new(load_varuint) { ParseError.new(DIAGNOSTIC_TYPES[load_varuint], load_embedded_string, load_location_object, load_error_level) }
warnings = Array.new(load_varuint) { ParseWarning.new(DIAGNOSTIC_TYPES[io.getbyte], load_embedded_string, load_location_object, load_warning_level) } warnings = Array.new(load_varuint) { ParseWarning.new(DIAGNOSTIC_TYPES[load_varuint], load_embedded_string, load_location_object, load_warning_level) }
[comments, magic_comments, data_loc, errors, warnings] [comments, magic_comments, data_loc, errors, warnings]
end end
@ -296,9 +296,11 @@ module Prism
case level case level
when 0 when 0
:fatal :syntax
when 1 when 1
:argument :argument
when 2
:load
else else
raise "Unknown level: #{level}" raise "Unknown level: #{level}"
end end

Просмотреть файл

@ -220,7 +220,7 @@ pm_serialize_data_loc(const pm_parser_t *parser, pm_buffer_t *buffer) {
static void 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) {
// serialize the type // serialize the type
pm_buffer_append_byte(buffer, (uint8_t) diagnostic->diag_id); pm_buffer_append_varuint(buffer, (uint32_t) diagnostic->diag_id);
// serialize message // serialize message
size_t message_length = strlen(diagnostic->message); size_t message_length = strlen(diagnostic->message);

Просмотреть файл

@ -88,5 +88,24 @@ module Prism
assert_kind_of IntegerNode, result.value.statements.body.first assert_kind_of IntegerNode, result.value.statements.body.first
end end
def test_command_line_x_implicit_fail
result = Prism.parse(<<~RUBY)
#!/bin/bash
exit 1
RUBY
assert_equal 1, result.errors.length
assert_equal :load, result.errors.first.level
end
def test_command_line_x_explicit_fail
result = Prism.parse(<<~RUBY, command_line: "x")
exit 1
RUBY
assert_equal 1, result.errors.length
assert_equal :load, result.errors.first.level
end
end end
end end