Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions src/prism.c
Original file line number Diff line number Diff line change
Expand Up @@ -18599,7 +18599,8 @@ parse_class(pm_parser_t *parser, uint8_t flags, uint16_t depth) {

pm_node_t *constant_path = parse_expression(parser, PM_BINDING_POWER_INDEX, flags & PM_PARSE_ACCEPTS_DO_BLOCK, PM_ERR_CLASS_NAME, (uint16_t) (depth + 1));
pm_token_t name = parser->previous;
if (name.type != PM_TOKEN_CONSTANT) {
/* parse_expression already reported a missing name. */
if (name.type != PM_TOKEN_CONSTANT && !PM_NODE_TYPE_P(constant_path, PM_ERROR_RECOVERY_NODE)) {
pm_parser_err_token(parser, &name, PM_ERR_CLASS_NAME);
}

Expand Down Expand Up @@ -18654,7 +18655,8 @@ parse_class(pm_parser_t *parser, uint8_t flags, uint16_t depth) {
pm_parser_scope_pop(parser);

if (!PM_NODE_TYPE_P(constant_path, PM_CONSTANT_PATH_NODE) && !(PM_NODE_TYPE_P(constant_path, PM_CONSTANT_READ_NODE))) {
pm_parser_err_node(parser, constant_path, PM_ERR_CLASS_NAME);
/* The name token check above already reported a non-constant name. */
if (name.type == PM_TOKEN_CONSTANT) pm_parser_err_node(parser, constant_path, PM_ERR_CLASS_NAME);
if (!PM_NODE_TYPE_P(constant_path, PM_ERROR_RECOVERY_NODE)) {
constant_path = UP(pm_error_recovery_node_create_unexpected(parser, constant_path));
}
Expand Down Expand Up @@ -23384,6 +23386,27 @@ pm_parse_continuable(pm_parser_t *parser) {
return;
}

if (error->diag_id == PM_ERR_CLASS_NAME || error->diag_id == PM_ERR_MODULE_NAME) {
const uint8_t *cursor = parser->start + error_end;

/* A colon immediately after the name can still become ::. */
if (parser->end - cursor == 1 && *cursor == ':') cursor++;

/* A name can continue on the next line, even across line comments. */
while (cursor < parser->end) {
cursor += pm_strspn_whitespace(cursor, parser->end - cursor);
if (cursor == parser->end || *cursor != '#') break;

const uint8_t *newline = next_newline(cursor, parser->end - cursor);
cursor = newline == NULL ? parser->end : newline + 1;
}

if (cursor < parser->end) {
parser->continuable = false;
return;
}
}

// Track non-stray, non-fatal error positions in list order.
if (error->diag_id != PM_ERR_UNEXPECTED_TOKEN_IGNORE &&
error->diag_id != PM_ERR_UNEXPECTED_TOKEN_CLOSE_CONTEXT) {
Expand Down
3 changes: 3 additions & 0 deletions test/prism/errors/class_name_lowercase.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class a; end
^ unexpected constant path after `class`; class/module name must be CONSTANT

5 changes: 5 additions & 0 deletions test/prism/errors_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ def test_module_name_rejects_symbol_ending_in_constant_token
assert_equal ":A", result.errors.first.location.slice
end

def test_missing_class_name_is_reported_once
errors = Prism.parse("class # comment\n").errors.select { |error| error.type == :class_name }
assert_equal ["class"], errors.map { |error| error.location.slice }
end

def test_begin_with_rescue_and_ensure_rejects_void_values
result = Prism.parse("def f; x = begin; return :body; rescue; return :rescue; ensure; end; end")
assert_equal [:void_expression], result.errors.map(&:type)
Expand Down
26 changes: 26 additions & 0 deletions test/prism/result/continuable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,32 @@

module Prism
class ContinuableTest < TestCase
def test_invalid_class_and_module_names
# The declaration has moved past the invalid name.
refute_predicate Prism.parse("class a; end\n"), :continuable?
refute_predicate Prism.parse("module a; end\n"), :continuable?
refute_predicate Prism.parse("class Mine\n self << class\n self.class\n end\nend\n"), :continuable?

# Followed only by whitespace (or EOF), the name may still continue:
# `class a::B` or `class a\n.b::C` are valid.
assert_predicate Prism.parse("class a"), :continuable?
assert_predicate Prism.parse("class a\n"), :continuable?
assert_predicate Prism.parse("module a"), :continuable?
end

def test_class_and_module_names_with_a_trailing_colon_or_comment
%w[class module].each do |keyword|
{ "a:" => ":B; end", "a # comment\n" => ".b::C; end", "# comment\n" => "M; end" }.each do |name, suffix|
source = "#{keyword} #{name}"
assert_predicate Prism.parse(source), :continuable?, source
assert_predicate Prism.parse(source + suffix), :success?, source + suffix
end

refute_predicate Prism.parse("#{keyword} a: "), :continuable?
refute_predicate Prism.parse("#{keyword} a # comment\nbody"), :continuable?
end
end

def test_valid_input
# Valid input is not continuable (nothing to continue).
refute_predicate Prism.parse("1 + 1"), :continuable?
Expand Down
Loading