Skip to content

Handle regexp without ending better #137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 17, 2022
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
11 changes: 9 additions & 2 deletions lib/syntax_tree/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 7 additions & 0 deletions test/parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down