diff options
author | Brice Figureau <brice-puppet@daysofwonder.com> | 2009-07-28 19:11:03 +0200 |
---|---|---|
committer | James Turnbull <james@lovedthanlost.net> | 2009-08-01 11:15:28 +1000 |
commit | 17e62b1ec806815abea909291df1e591a825c375 (patch) | |
tree | 477ef33e88077c8e46d7184f31a106a0b850425a /lib/puppet | |
parent | 4f9545f2f6cb377eff126c9d52b421ab405aa677 (diff) | |
download | puppet-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')
-rw-r--r-- | lib/puppet/parser/ast/leaf.rb | 42 | ||||
-rw-r--r-- | lib/puppet/parser/grammar.ra | 4 | ||||
-rw-r--r-- | lib/puppet/parser/parser.rb | 85 |
3 files changed, 93 insertions, 38 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 diff --git a/lib/puppet/parser/grammar.ra b/lib/puppet/parser/grammar.ra index bb75ecf79..68acf35aa 100644 --- a/lib/puppet/parser/grammar.ra +++ b/lib/puppet/parser/grammar.ra @@ -765,6 +765,10 @@ comma: FARROW endcomma: # nothing | COMMA { result = nil } +regex: REGEX { + result = ast AST::Regex, :value => val[0][:value] +} + end ---- header ---- require 'puppet' diff --git a/lib/puppet/parser/parser.rb b/lib/puppet/parser/parser.rb index 6f4895abf..ef08d6e09 100644 --- a/lib/puppet/parser/parser.rb +++ b/lib/puppet/parser/parser.rb @@ -29,7 +29,7 @@ module Puppet class Parser < Racc::Parser -module_eval <<'..end grammar.ra modeval..id6e3ca7aacc', 'grammar.ra', 786 +module_eval <<'..end grammar.ra modeval..id5d1f9eb10f', 'grammar.ra', 790 # It got too annoying having code in a file that needs to be compiled. require 'puppet/parser/parser_support' @@ -41,7 +41,7 @@ require 'puppet/parser/parser_support' # $Id$ -..end grammar.ra modeval..id6e3ca7aacc +..end grammar.ra modeval..id5d1f9eb10f ##### racc 1.4.5 generates ### @@ -239,9 +239,10 @@ racc_reduce_table = [ 1, 115, :_reduce_none, 1, 115, :_reduce_none, 0, 94, :_reduce_none, - 1, 94, :_reduce_193 ] + 1, 94, :_reduce_193, + 1, 134, :_reduce_194 ] -racc_reduce_n = 194 +racc_reduce_n = 195 racc_shift_n = 321 @@ -555,38 +556,38 @@ racc_action_pointer = [ nil ] racc_action_default = [ - -169, -194, -15, -3, -162, -194, -16, -5, -160, -194, - -17, -6, -194, -7, -8, -194, -161, -9, -194, -10, - -194, -194, -11, -194, -12, -194, -194, -39, -194, -13, - -1, -14, -2, -194, -194, -169, -161, -72, -167, -166, - -165, -169, -168, -163, -79, -186, -31, -194, -33, -32, - -194, -63, -102, -21, -104, -103, -22, -62, -23, -61, - -26, -194, -27, -150, -149, -28, -29, -194, -30, -194, - -94, -112, -90, -194, -194, -97, -91, -95, -99, -92, - -194, -96, -93, -194, -98, -194, -194, -72, -169, -169, - -42, -151, -153, -194, -169, -40, -38, -194, -4, -70, - -71, -194, -194, -182, -73, -67, -64, -63, -59, -62, - -192, -65, -57, -68, -194, -66, -148, -69, -147, -194, - -194, -180, -194, -77, -78, -80, -194, -192, -20, -194, - -82, -194, -194, -194, -194, -194, -194, -189, -126, -119, - -194, -194, -194, -194, -194, -194, -194, -194, -194, -194, - -194, -194, -194, -194, -194, -194, -194, -194, -192, -169, - -194, -46, -48, -194, -62, -55, -54, -45, -194, -79, - -194, 321, -170, -194, -171, -183, -184, -185, -194, -194, - -60, -194, -194, -193, -72, -181, -194, -164, -194, -194, - -193, -18, -194, -105, -194, -190, -191, -24, -25, -101, - -194, -137, -145, -138, -62, -143, -194, -146, -144, -194, + -169, -195, -15, -3, -162, -195, -16, -5, -160, -195, + -17, -6, -195, -7, -8, -195, -161, -9, -195, -10, + -195, -195, -11, -195, -12, -195, -195, -39, -195, -13, + -1, -14, -2, -195, -195, -169, -161, -72, -167, -166, + -165, -169, -168, -163, -79, -186, -31, -195, -33, -32, + -195, -63, -102, -21, -104, -103, -22, -62, -23, -61, + -26, -195, -27, -150, -149, -28, -29, -195, -30, -195, + -94, -112, -90, -195, -195, -97, -91, -95, -99, -92, + -195, -96, -93, -195, -98, -195, -195, -72, -169, -169, + -42, -151, -153, -195, -169, -40, -38, -195, -4, -70, + -71, -195, -195, -182, -73, -67, -64, -63, -59, -62, + -192, -65, -57, -68, -195, -66, -148, -69, -147, -195, + -195, -180, -195, -77, -78, -80, -195, -192, -20, -195, + -82, -195, -195, -195, -195, -195, -195, -189, -126, -119, + -195, -195, -195, -195, -195, -195, -195, -195, -195, -195, + -195, -195, -195, -195, -195, -195, -195, -195, -192, -169, + -195, -46, -48, -195, -62, -55, -54, -45, -195, -79, + -195, 321, -170, -195, -171, -183, -184, -185, -195, -195, + -60, -195, -195, -193, -72, -181, -195, -164, -195, -195, + -193, -18, -195, -105, -195, -190, -191, -24, -25, -101, + -195, -137, -145, -138, -62, -143, -195, -146, -144, -195, -129, -187, -191, -115, -117, -121, -118, -123, -128, -125, - -122, -127, -124, -120, -109, -194, -113, -114, -116, -194, - -131, -194, -135, -106, -194, -194, -194, -51, -50, -44, - -194, -194, -43, -192, -152, -179, -174, -177, -194, -194, - -194, -157, -194, -34, -58, -75, -35, -74, -192, -159, - -194, -76, -37, -81, -19, -83, -100, -140, -192, -194, - -188, -108, -194, -109, -130, -132, -194, -194, -36, -49, - -47, -88, -84, -52, -85, -89, -86, -87, -53, -194, - -194, -194, -172, -173, -194, -155, -194, -156, -56, -158, - -194, -194, -191, -142, -194, -107, -194, -136, -41, -178, - -176, -175, -154, -141, -139, -111, -194, -134, -194, -110, + -122, -127, -124, -120, -109, -195, -113, -114, -116, -195, + -131, -195, -135, -106, -195, -195, -195, -51, -50, -44, + -195, -195, -43, -192, -152, -179, -174, -177, -195, -195, + -195, -157, -195, -34, -58, -75, -35, -74, -192, -159, + -195, -76, -37, -81, -19, -83, -100, -140, -192, -195, + -188, -108, -195, -109, -130, -132, -195, -195, -36, -49, + -47, -88, -84, -52, -85, -89, -86, -87, -53, -195, + -195, -195, -172, -173, -195, -155, -195, -156, -56, -158, + -195, -195, -191, -142, -195, -107, -195, -136, -41, -178, + -176, -175, -154, -141, -139, -111, -195, -134, -195, -110, -133 ] racc_goto_table = [ @@ -723,7 +724,7 @@ racc_goto_pointer = [ nil, nil, -208, -26, nil, 180, -22, 412, -37, 34, nil, -156, -26, -239, -201, -117, -142, nil, -120, -93, -128, -162, 22, -48, 14, 38, 10, 6, -115, -114, - -163, -38 ] + -163, -38, nil ] racc_goto_default = [ nil, nil, nil, 167, 3, 7, 11, 13, 14, 17, @@ -733,7 +734,7 @@ racc_goto_default = [ 162, 163, nil, nil, 114, 78, 81, nil, 104, 71, 124, 125, nil, 194, nil, nil, nil, 231, 67, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, - nil, nil ] + nil, nil, nil ] racc_token_table = { false => 0, @@ -953,7 +954,8 @@ Racc_token_to_s_table = [ 'nothing', 'arguments', 'argument', -'classnameordefault'] +'classnameordefault', +'regex'] Racc_debug_parser = false @@ -2173,6 +2175,13 @@ module_eval <<'.,.,', 'grammar.ra', 765 end .,., +module_eval <<'.,.,', 'grammar.ra', 770 + def _reduce_194( val, _values, result ) + result = ast AST::Regex, :value => val[0][:value] + result + end +.,., + def _reduce_none( val, _values, result ) result end |