summaryrefslogtreecommitdiffstats
path: root/lib/puppet/parser/ast
diff options
context:
space:
mode:
authorBrice Figureau <brice-puppet@daysofwonder.com>2009-07-28 19:11:03 +0200
committerJames Turnbull <james@lovedthanlost.net>2009-08-01 11:15:28 +1000
commit17e62b1ec806815abea909291df1e591a825c375 (patch)
tree477ef33e88077c8e46d7184f31a106a0b850425a /lib/puppet/parser/ast
parent4f9545f2f6cb377eff126c9d52b421ab405aa677 (diff)
downloadpuppet-17e62b1ec806815abea909291df1e591a825c375.tar.gz
puppet-17e62b1ec806815abea909291df1e591a825c375.tar.xz
puppet-17e62b1ec806815abea909291df1e591a825c375.zip
Add AST::Regex, an AST leaf node representing a regex
Add a regex rule (unused for the moment) to the parser. Signed-off-by: Brice Figureau <brice-puppet@daysofwonder.com>
Diffstat (limited to 'lib/puppet/parser/ast')
-rw-r--r--lib/puppet/parser/ast/leaf.rb42
1 files changed, 42 insertions, 0 deletions
diff --git a/lib/puppet/parser/ast/leaf.rb b/lib/puppet/parser/ast/leaf.rb
index d08938011..7743862d7 100644
--- a/lib/puppet/parser/ast/leaf.rb
+++ b/lib/puppet/parser/ast/leaf.rb
@@ -10,6 +10,15 @@ class Puppet::Parser::AST
return @value
end
+ # evaluate ourselves, and match
+ def evaluate_match(value, scope, options = {})
+ obj = self.safeevaluate(scope)
+ if ! options[:sensitive] && obj.respond_to?(:downcase)
+ obj = obj.downcase
+ end
+ obj == value
+ end
+
def to_s
return @value.to_s unless @value.nil?
end
@@ -99,4 +108,37 @@ class Puppet::Parser::AST
end
end
end
+
+ class Regex < AST::Leaf
+ def initialize(hash)
+ super
+ @value = Regexp.new(@value) unless @value.is_a?(Regexp)
+ end
+
+ # we're returning self here to wrap the regexp and to be used in places
+ # where a string would have been used, without modifying any client code.
+ # For instance, in many places we have the following code snippet:
+ # val = @val.safeevaluate(@scope)
+ # if val.match(otherval)
+ # ...
+ # end
+ # this way, we don't have to modify this test specifically for handling
+ # regexes.
+ def evaluate(scope)
+ return self
+ end
+
+ def evaluate_match(value, scope, options = {})
+ value = value.is_a?(String) ? value : value.to_s
+
+ if matched = @value.match(value)
+ scope.ephemeral_from(matched, options[:file], options[:line])
+ end
+ matched
+ end
+
+ def to_s
+ return "/#{@value.source}/"
+ end
+ end
end