Skip to content

Commit

Permalink
Handle interpolation simplification at the start or in the middle of …
Browse files Browse the repository at this point in the history
…regexps.
  • Loading branch information
mvz committed Apr 4, 2012
1 parent 95878e0 commit 6464b9d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
15 changes: 11 additions & 4 deletions lib/ripper_ruby_parser/sexp_handlers/literals.rb
Expand Up @@ -20,7 +20,12 @@ def process_string_content exp

def process_string_embexpr exp
_, list = exp.shift 2
s(:evstr, process(list.first))
val = process(list.first)
if val.sexp_type == :str
val
else
s(:evstr, val)
end
end

def process_xstring_literal exp
Expand All @@ -46,11 +51,13 @@ def process_regexp_literal exp
flags =~ /n/ and numflags |= Regexp::NOENCODING
flags =~ /[ues]/ and numflags |= Regexp::FIXEDENCODING

while not(rest.empty?) and rest.first.sexp_type == :str
str = rest.shift
string += str[1]
end

if rest.empty?
s(:lit, Regexp.new(string, numflags))
elsif rest.size == 1 and rest[0].sexp_type == :evstr and rest[0][1].sexp_type == :str
string += rest[0][1][1]
s(:lit, Regexp.new(string, numflags))
else
rest << numflags if numflags > 0
sexp_type = if flags =~ /o/
Expand Down
8 changes: 8 additions & 0 deletions test/unit/parser_literals_test.rb
Expand Up @@ -48,6 +48,14 @@
it "performs the interpolation when it is at the end" do
'/foo#{"bar"}/'.must_be_parsed_as s(:lit, /foobar/)
end

it "performs the interpolation when it is in the middle" do
'/foo#{"bar"}baz/'.must_be_parsed_as s(:lit, /foobarbaz/)
end

it "performs the interpolation when it is at the start" do
'/#{"foo"}bar/'.must_be_parsed_as s(:lit, /foobar/)
end
end
end
end
Expand Down

0 comments on commit 6464b9d

Please sign in to comment.