From 65ae61cb4f513a38a0c1660ae75268adf30b976d Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Wed, 17 Aug 2022 12:00:48 -0400 Subject: [PATCH] Handle regexp without ending better --- lib/syntax_tree/parser.rb | 11 +++++++++-- test/parser_test.rb | 7 +++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/syntax_tree/parser.rb b/lib/syntax_tree/parser.rb index 3824b6b3..8af0b8ed 100644 --- a/lib/syntax_tree/parser.rb +++ b/lib/syntax_tree/parser.rb @@ -2837,14 +2837,21 @@ def on_regexp_end(value) # :call-seq: # on_regexp_literal: ( # RegexpContent regexp_content, - # RegexpEnd ending + # (nil | RegexpEnd) ending # ) -> RegexpLiteral def on_regexp_literal(regexp_content, ending) + location = regexp_content.location + + if ending.nil? + message = "Cannot find expected regular expression ending" + raise ParseError.new(message, *find_token_error(location)) + end + RegexpLiteral.new( beginning: regexp_content.beginning, ending: ending.value, parts: regexp_content.parts, - location: regexp_content.location.to(ending.location) + location: location.to(ending.location) ) end diff --git a/test/parser_test.rb b/test/parser_test.rb index d0c475c1..fbff8ec2 100644 --- a/test/parser_test.rb +++ b/test/parser_test.rb @@ -41,6 +41,13 @@ def test_errors_on_missing_end_with_location assert_equal(4, error.column) end + def test_errors_on_missing_regexp_ending + error = + assert_raises(Parser::ParseError) { SyntaxTree.parse("a =~ /foo") } + + assert_equal(5, error.column) + end + def test_errors_on_missing_token_without_location assert_raises(Parser::ParseError) { SyntaxTree.parse(":\"foo") } end