summaryrefslogtreecommitdiffstats
path: root/lib/puppet/parser/ast
diff options
context:
space:
mode:
authorBrice Figureau <brice-puppet@daysofwonder.com>2009-07-28 19:13:54 +0200
committerJames Turnbull <james@lovedthanlost.net>2009-08-01 11:15:29 +1000
commitef68967f2b72e609a9d69e53771a61fd9f522149 (patch)
treeb9e8baa5a45d31f03fa4fae83cb0160a71957dd9 /lib/puppet/parser/ast
parent17e62b1ec806815abea909291df1e591a825c375 (diff)
downloadpuppet-ef68967f2b72e609a9d69e53771a61fd9f522149.tar.gz
puppet-ef68967f2b72e609a9d69e53771a61fd9f522149.tar.xz
puppet-ef68967f2b72e609a9d69e53771a61fd9f522149.zip
Fix #2033 - Allow regexp in if expression
This changeset introduces regexp in if expression with the use of the =~ (match) and !~ (not match) operator. Usage: if $uname =~ /Linux|Debian/ { ... } Moreover this patch creates ephemeral variables ($0 to $9) in the current scope which contains the regex captures: if $uname =~ /(Linux|Debian)/ { notice("this is a $1 system") } Signed-off-by: Brice Figureau <brice-puppet@daysofwonder.com>
Diffstat (limited to 'lib/puppet/parser/ast')
-rw-r--r--lib/puppet/parser/ast/ifstatement.rb17
-rw-r--r--lib/puppet/parser/ast/match_operator.rb31
2 files changed, 42 insertions, 6 deletions
diff --git a/lib/puppet/parser/ast/ifstatement.rb b/lib/puppet/parser/ast/ifstatement.rb
index d216b7c65..9d52123b6 100644
--- a/lib/puppet/parser/ast/ifstatement.rb
+++ b/lib/puppet/parser/ast/ifstatement.rb
@@ -18,14 +18,19 @@ class Puppet::Parser::AST
def evaluate(scope)
value = @test.safeevaluate(scope)
- if Puppet::Parser::Scope.true?(value)
- return @statements.safeevaluate(scope)
- else
- if defined? @else
- return @else.safeevaluate(scope)
+ # let's emulate a new scope for each branches
+ begin
+ if Puppet::Parser::Scope.true?(value)
+ return @statements.safeevaluate(scope)
else
- return nil
+ if defined? @else
+ return @else.safeevaluate(scope)
+ else
+ return nil
+ end
end
+ ensure
+ scope.unset_ephemeral_var
end
end
end
diff --git a/lib/puppet/parser/ast/match_operator.rb b/lib/puppet/parser/ast/match_operator.rb
new file mode 100644
index 000000000..17e27826e
--- /dev/null
+++ b/lib/puppet/parser/ast/match_operator.rb
@@ -0,0 +1,31 @@
+require 'puppet'
+require 'puppet/parser/ast/branch'
+
+class Puppet::Parser::AST
+ class MatchOperator < AST::Branch
+
+ attr_accessor :lval, :rval, :operator
+
+ # Iterate across all of our children.
+ def each
+ [@lval,@rval].each { |child| yield child }
+ end
+
+ # Returns a boolean which is the result of the boolean operation
+ # of lval and rval operands
+ def evaluate(scope)
+ lval = @lval.safeevaluate(scope)
+
+ return @operator == "=~" if rval.evaluate_match(lval, scope)
+ return @operator == "!~"
+ end
+
+ def initialize(hash)
+ super
+
+ unless %w{!~ =~}.include?(@operator)
+ raise ArgumentError, "Invalid regexp operator %s" % @operator
+ end
+ end
+ end
+end