+1 On Jul 26, 2009, at 9:03 AM, Brice Figureau wrote:
> > The lexer recognizes regex delimited by / as in: > /^$/ > > The match operator is defined by =~ > The not match operator is defined by !~ > > Signed-off-by: Brice Figureau <[email protected]> > --- > lib/puppet/parser/lexer.rb | 7 +++++++ > spec/unit/parser/lexer.rb | 14 ++++++++++++++ > 2 files changed, 21 insertions(+), 0 deletions(-) > > diff --git a/lib/puppet/parser/lexer.rb b/lib/puppet/parser/lexer.rb > index 5bab6d6..56863a2 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 3c765d4..8b73de1 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 } > > -- > 1.6.0.2 > > > > -- If you can't be a good example, then you'll just have to be a horrible warning. -- Catherine Aird --------------------------------------------------------------------- Luke Kanies | http://reductivelabs.com | http://madstop.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Puppet Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/puppet-dev?hl=en -~----------~----~----~----~------~----~------~--~---
