summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/puppet/parser/lexer.rb7
-rwxr-xr-xspec/unit/parser/lexer.rb14
2 files changed, 21 insertions, 0 deletions
diff --git a/lib/puppet/parser/lexer.rb b/lib/puppet/parser/lexer.rb
index 5bab6d65d..56863a2fc 100644
--- a/lib/puppet/parser/lexer.rb
+++ b/lib/puppet/parser/lexer.rb
@@ -134,6 +134,8 @@ class Puppet::Parser::Lexer
'*' => :TIMES,
'<<' => :LSHIFT,
'>>' => :RSHIFT,
+ '=~' => :MATCH,
+ '!~' => :NOMATCH,
%r{([a-z][-\w]*)?(::[a-z][-\w]*)+} => :CLASSNAME, # Require '::' in the class name, else we'd compete with NAME
%r{((::){0,1}[A-Z][-\w]*)+} => :CLASSREF
)
@@ -169,6 +171,11 @@ class Puppet::Parser::Lexer
[self,value]
end
+ TOKENS.add_token :REGEX, %r{/(.*?)/} do |lexer, value|
+ value.sub!(/^\/(.*?)\/$/,"\\1")
+ [self, Regexp.new(value)]
+ end
+
TOKENS.add_token :RETURN, "\n", :skip => true, :incr_line => true, :skip_text => true
TOKENS.add_token :SQUOTE, "'" do |lexer, value|
diff --git a/spec/unit/parser/lexer.rb b/spec/unit/parser/lexer.rb
index 3c765d431..8b73de195 100755
--- a/spec/unit/parser/lexer.rb
+++ b/spec/unit/parser/lexer.rb
@@ -178,6 +178,8 @@ describe Puppet::Parser::Lexer::TOKENS do
:TIMES => '*',
:LSHIFT => '<<',
:RSHIFT => '>>',
+ :MATCH => '=~',
+ :NOMATCH => '!~',
}.each do |name, string|
it "should have a token named #{name.to_s}" do
Puppet::Parser::Lexer::TOKENS[name].should_not be_nil
@@ -451,6 +453,18 @@ describe Puppet::Parser::Lexer::TOKENS[:VARIABLE] do
end
end
+describe Puppet::Parser::Lexer::TOKENS[:REGEX] do
+ before { @token = Puppet::Parser::Lexer::TOKENS[:REGEX] }
+
+ it "should match against any expression enclosed in //" do
+ @token.regex.should =~ '/this is a regex/'
+ 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
+end
+
describe Puppet::Parser::Lexer, "when lexing comments" do
before { @lexer = Puppet::Parser::Lexer.new }