Skip to content

Commit af9c301

Browse files
authored
Merge pull request #137 from ruby-syntax-tree/handle-regexp-no-end
Handle regexp without ending better
2 parents db67ea1 + 65ae61c commit af9c301

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

lib/syntax_tree/parser.rb

+9-2
Original file line numberDiff line numberDiff line change
@@ -2837,14 +2837,21 @@ def on_regexp_end(value)
28372837
# :call-seq:
28382838
# on_regexp_literal: (
28392839
# RegexpContent regexp_content,
2840-
# RegexpEnd ending
2840+
# (nil | RegexpEnd) ending
28412841
# ) -> RegexpLiteral
28422842
def on_regexp_literal(regexp_content, ending)
2843+
location = regexp_content.location
2844+
2845+
if ending.nil?
2846+
message = "Cannot find expected regular expression ending"
2847+
raise ParseError.new(message, *find_token_error(location))
2848+
end
2849+
28432850
RegexpLiteral.new(
28442851
beginning: regexp_content.beginning,
28452852
ending: ending.value,
28462853
parts: regexp_content.parts,
2847-
location: regexp_content.location.to(ending.location)
2854+
location: location.to(ending.location)
28482855
)
28492856
end
28502857

test/parser_test.rb

+7
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ def test_errors_on_missing_end_with_location
4141
assert_equal(4, error.column)
4242
end
4343

44+
def test_errors_on_missing_regexp_ending
45+
error =
46+
assert_raises(Parser::ParseError) { SyntaxTree.parse("a =~ /foo") }
47+
48+
assert_equal(5, error.column)
49+
end
50+
4451
def test_errors_on_missing_token_without_location
4552
assert_raises(Parser::ParseError) { SyntaxTree.parse(":\"foo") }
4653
end

0 commit comments

Comments
 (0)