Skip to content

Commit 3a1582a

Browse files
committed
Update parser to report syntax error at { token
1 parent 5a19847 commit 3a1582a

2 files changed

Lines changed: 18 additions & 9 deletions

File tree

src/parser.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ typedef struct {
746746
| {} self_type_binding? `->` <optional>
747747
*/
748748
NODISCARD
749-
static bool parse_function(rbs_parser_t *parser, bool accept_type_binding, parse_function_result **result, bool self_allowed, bool classish_allowed) {
749+
static bool parse_function(rbs_parser_t *parser, bool accept_type_binding, bool block_allowed, parse_function_result **result, bool self_allowed, bool classish_allowed) {
750750
rbs_node_t *function = NULL;
751751
rbs_types_block_t *block = NULL;
752752
rbs_node_t *function_self_type = NULL;
@@ -777,6 +777,11 @@ static bool parse_function(rbs_parser_t *parser, bool accept_type_binding, parse
777777
bool required = true;
778778
rbs_range_t block_range;
779779

780+
if (!block_allowed && (parser->next_token.type == pLBRACE || (parser->next_token.type == pQUESTION && parser->next_token2.type == pLBRACE))) {
781+
rbs_parser_set_error(parser, parser->next_token, true, "block is not allowed in this context");
782+
return false;
783+
}
784+
780785
if (parser->next_token.type == pQUESTION && parser->next_token2.type == pLBRACE) {
781786
// Optional block
782787
block_range.start = parser->next_token.range.start;
@@ -865,7 +870,7 @@ NODISCARD
865870
static bool parse_proc_type(rbs_parser_t *parser, rbs_types_proc_t **proc, bool self_allowed, bool classish_allowed) {
866871
rbs_position_t start = parser->current_token.range.start;
867872
parse_function_result *result = rbs_allocator_alloc(ALLOCATOR(), parse_function_result);
868-
CHECK_PARSE(parse_function(parser, true, &result, self_allowed, classish_allowed));
873+
CHECK_PARSE(parse_function(parser, true, true, &result, self_allowed, classish_allowed));
869874

870875
rbs_position_t end = parser->current_token.range.end;
871876
rbs_location_range range = { .start_char = start.char_pos, .start_byte = start.byte_pos, .end_char = end.char_pos, .end_byte = end.byte_pos };
@@ -1597,7 +1602,7 @@ bool rbs_parse_method_type(rbs_parser_t *parser, rbs_method_type_t **method_type
15971602
type_range.start = parser->next_token.range.start;
15981603

15991604
parse_function_result *result = rbs_allocator_alloc(ALLOCATOR(), parse_function_result);
1600-
CHECK_PARSE(parse_function(parser, false, &result, true, classish_allowed));
1605+
CHECK_PARSE(parse_function(parser, false, true, &result, true, classish_allowed));
16011606

16021607
CHECK_PARSE(parser_pop_typevar_table(parser));
16031608

@@ -4017,12 +4022,7 @@ static bool parse_inline_leading_annotation(rbs_parser_t *parser, rbs_ast_ruby_a
40174022
type_range.start = parser->next_token.range.start;
40184023

40194024
parse_function_result *result = rbs_allocator_alloc(ALLOCATOR(), parse_function_result);
4020-
if (!parse_function(parser, true, &result, true, true)) {
4021-
return false;
4022-
}
4023-
4024-
if (result->block != NULL) {
4025-
rbs_parser_set_error(parser, parser->current_token, true, "block type annotation cannot have a nested block");
4025+
if (!parse_function(parser, true, false, &result, true, true)) {
40264026
return false;
40274027
}
40284028

test/rbs/inline_annotation_parsing_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,4 +489,13 @@ def test_parse__block_type_annotation
489489
assert_equal false, annot.required?
490490
end
491491
end
492+
493+
def test_error__block_type_annotation
494+
error = assert_raises RBS::ParsingError do
495+
Parser.parse_inline_leading_annotation("@rbs &block: () { () -> void } -> void", 0...)
496+
end
497+
assert_match(/block is not allowed in this context/, error.message)
498+
assert_match(/pLBRACE/, error.message)
499+
assert_equal "{", error.location.source
500+
end
492501
end

0 commit comments

Comments
 (0)