summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2007-02-28 01:21:58 +0000
committerluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2007-02-28 01:21:58 +0000
commit4df0738de2e93b8c0408561153543682aa1a3455 (patch)
treed657835c5496e1dca96aab0f1bbc3317bd743994 /lib
parent0f16bf3c5b925f5656a5592b527ae56d92c662f9 (diff)
downloadpuppet-4df0738de2e93b8c0408561153543682aa1a3455.tar.gz
puppet-4df0738de2e93b8c0408561153543682aa1a3455.tar.xz
puppet-4df0738de2e93b8c0408561153543682aa1a3455.zip
Applying a modified form of the patch by cstorey from #523. The modifications were mostly around the fact that Strscan does not set $1 and its ilk.
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@2239 980ebf18-57e1-0310-9a29-db15c13687c0
Diffstat (limited to 'lib')
-rw-r--r--lib/puppet/parser/ast/leaf.rb6
-rw-r--r--lib/puppet/parser/scope.rb53
2 files changed, 43 insertions, 16 deletions
diff --git a/lib/puppet/parser/ast/leaf.rb b/lib/puppet/parser/ast/leaf.rb
index 298c0168f..ed02c4eef 100644
--- a/lib/puppet/parser/ast/leaf.rb
+++ b/lib/puppet/parser/ast/leaf.rb
@@ -41,14 +41,12 @@ class Puppet::Parser::AST
# Interpolate the string looking for variables, and then return
# the result.
def evaluate(hash)
- return hash[:scope].strinterp(@value)
+ return hash[:scope].strinterp(@value, @file, @line)
end
end
- # The base string class.
+ # An uninterpreted string.
class FlatString < AST::Leaf
- # Interpolate the string looking for variables, and then return
- # the result.
def evaluate(hash)
return @value
end
diff --git a/lib/puppet/parser/scope.rb b/lib/puppet/parser/scope.rb
index bf4f7215e..6bb8872ee 100644
--- a/lib/puppet/parser/scope.rb
+++ b/lib/puppet/parser/scope.rb
@@ -4,6 +4,7 @@
require 'puppet/parser/parser'
require 'puppet/parser/templatewrapper'
require 'puppet/transportable'
+require 'strscan'
class Puppet::Parser::Scope
require 'puppet/parser/resource'
@@ -540,25 +541,53 @@ class Puppet::Parser::Scope
end
# Return an interpolated string.
- def strinterp(string)
+ def strinterp(string, file = nil, line = nil)
# Most strings won't have variables in them.
- if string =~ /\$/
- string = string.gsub(/\\\$|\$\{(\w+)\}|\$(\w+)/) do |value|
+ ss = StringScanner.new(string)
+ out = ""
+ while not ss.eos?
+ if ss.scan(/^\$\{(\w+)\}|^\$(\w+)/)
# If it matches the backslash, then just retun the dollar sign.
- if value == '\\$'
- '$'
+ if ss.matched == '\\$'
+ out << '$'
else # look the variable up
- lookupvar($1 || $2)
+ out << lookupvar(ss[1] || ss[2]) || ""
end
+ elsif ss.scan(/^\\(.)/)
+ # Puppet.debug("Got escape: pos:%d; m:%s" % [ss.pos, ss.matched])
+ case ss[1]
+ when 'n'
+ out << "\n"
+ when 't'
+ out << "\t"
+ when 's'
+ out << " "
+ when '\\'
+ out << '\\'
+ when '$'
+ out << '$'
+ else
+ Puppet.warning "Unrecognised escape sequence '#{ss.matched}'"
+ out << ss.matched
+ end
+ elsif ss.scan(/^\$/)
+ out << '$'
+ else
+ tmp = ss.scan(/[^\\$]+/)
+ # Puppet.debug("Got other: pos:%d; m:%s" % [ss.pos, tmp])
+ unless tmp
+ error = Puppet::ParseError.new("Could not parse string %s" %
+ string.inspect)
+ {:file= => file, :line= => line}.each do |m,v|
+ error.send(m, v) if v
+ end
+ raise error
+ end
+ out << tmp
end
end
- # And most won't have whitespace replacements.
- if string =~ /\\/
- return string.gsub(/\\t/, "\t").gsub(/\\n/, "\n").gsub(/\\s/, "\s")
- else
- return string
- end
+ return out
end
# Add a tag to our current list. These tags will be added to all