diff options
-rw-r--r-- | lib/puppet/parser/lexer.rb | 17 | ||||
-rwxr-xr-x | spec/unit/parser/lexer.rb | 14 |
2 files changed, 28 insertions, 3 deletions
diff --git a/lib/puppet/parser/lexer.rb b/lib/puppet/parser/lexer.rb index 56863a2fc..e027a69d1 100644 --- a/lib/puppet/parser/lexer.rb +++ b/lib/puppet/parser/lexer.rb @@ -171,9 +171,14 @@ class Puppet::Parser::Lexer [self,value] end - TOKENS.add_token :REGEX, %r{/(.*?)/} do |lexer, value| - value.sub!(/^\/(.*?)\/$/,"\\1") - [self, Regexp.new(value)] + TOKENS.add_token :REGEX, %r{/[^/]*/} do |lexer, value| + # Make sure we haven't matched an escaped / + while value[-2..-2] == '\\' + other = lexer.scan_until(%r{/}) + value += other + end + regex = value.sub(%r{\A/}, "").sub(%r{/\Z}, '').gsub("\\/", "/") + [self, Regexp.new(regex)] end TOKENS.add_token :RETURN, "\n", :skip => true, :incr_line => true, :skip_text => true @@ -472,6 +477,12 @@ class Puppet::Parser::Lexer @scanner.skip(@skip) end + # Provide some limited access to the scanner, for those + # tokens that need it. + def scan_until(regex) + @scanner.scan_until(regex) + end + # we've encountered an opening quote... # slurp in the rest of the string and return it def slurpstring(quote) diff --git a/spec/unit/parser/lexer.rb b/spec/unit/parser/lexer.rb index 8b73de195..1c3e91bd6 100755 --- a/spec/unit/parser/lexer.rb +++ b/spec/unit/parser/lexer.rb @@ -460,6 +460,20 @@ describe Puppet::Parser::Lexer::TOKENS[:REGEX] do @token.regex.should =~ '/this is a regex/' end + describe "when including escaped slashes" do + before { @lexer = Puppet::Parser::Lexer.new } + + it "should not consider escaped slashes to be the end of a regex" do + @lexer.string = "/this \\/ foo/" + tokens = [] + @lexer.scan do |name, value| + tokens << value + end + tokens[0][:value].should == Regexp.new("this / foo") + end + end + + it "should return the REGEX token and a Regexp" do @token.convert(stub("lexer"), "/myregex/").should == [Puppet::Parser::Lexer::TOKENS[:REGEX], Regexp.new(/myregex/)] end |