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
26 changes: 13 additions & 13 deletions lib/parser/lexer.rl
Original file line number Diff line number Diff line change
Expand Up @@ -1357,21 +1357,21 @@ class Parser::Lexer
w_space* '?'
=> { fhold; fgoto expr_beg; };

# a %{1}, a %[1] (but not "a %=1=" or "a % foo")
# a /foo/ (but not "a / foo" or "a /=foo")
w_space+ ( [%/] ( c_any - c_space_nl - '=' ) # /
# a <<HEREDOC
| '<<'
)
=> { fhold; fhold; fgoto expr_beg; };

# x /1
# Ambiguous regexp literal.
w_space+ '/'
# a %{1}, a %[1] (but not "a %=1=" or "a % foo")
# a /foo/ (but not "a / foo" or "a /=foo")
# a <<HEREDOC
w_space+ %{ tm = p }
( [%/] ( c_any - c_space_nl - '=' ) # /
| '<<'
)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@whitequark Do you mean this form?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty much

=> {
diagnostic :warning, :ambiguous_literal, nil, range(@te - 1, @te)
if tok(tm, tm + 1) == '/'
# Ambiguous regexp literal.
diagnostic :warning, :ambiguous_literal, nil, range(tm, tm + 1)
end

fhold; fgoto expr_beg;
p = tm - 1
fgoto expr_beg;
};

# x *1
Expand Down
17 changes: 17 additions & 0 deletions test/parse_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,23 @@ def assert_diagnoses(diagnostic, code, source_maps='', versions=ALL_VERSIONS)
end
end

def refute_diagnoses(code, versions=ALL_VERSIONS)
with_versions(versions) do |version, parser|
source_file = Parser::Source::Buffer.new('(refute_diagnoses)')
source_file.source = code

begin
parser = parser.parse(source_file)
rescue Parser::SyntaxError
# do nothing; the diagnostic was reported
end

assert_empty @diagnostics,
"(#{version}) emits no diagnostics, not\n" \
"#{@diagnostics.map(&:render).join("\n")}"
end
end

SOURCE_MAP_DESCRIPTION_RE =
/(?x)
^(?# $1 skip) ^(\s*)
Expand Down
10 changes: 10 additions & 0 deletions test/test_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2578,6 +2578,16 @@ def test_send_plain_cmd
|~~~~~~~~~~~~ expression})
end

def test_send_plain_cmd_ambiguous_literal
assert_diagnoses(
[:warning, :ambiguous_literal],
%q{m /foo/},
%q{ ^ location})

refute_diagnoses(
%q{m %[1]})
end

def test_send_plain_cmd_ambiguous_prefix
assert_diagnoses(
[:warning, :ambiguous_prefix, { :prefix => '+' }],
Expand Down