From 6e347d5cca282de32c639a5fe650804fedfe27fa Mon Sep 17 00:00:00 2001 From: Nathan Weizenbaum Date: Sun, 12 Oct 2008 20:26:56 -0700 Subject: [PATCH] Delay SassScript variable resolution until #perform. --- lib/sass/script.rb | 3 ++- lib/sass/script/funcall.rb | 10 +++------- lib/sass/script/literal.rb | 2 +- lib/sass/script/operation.rb | 10 +++------- lib/sass/script/parser.rb | 9 +++------ lib/sass/script/unary_operation.rb | 8 ++------ lib/sass/script/variable.rb | 20 ++++++++++++++++++++ 7 files changed, 34 insertions(+), 28 deletions(-) create mode 100644 lib/sass/script/variable.rb diff --git a/lib/sass/script.rb b/lib/sass/script.rb index 3afbb30b..d625811f 100644 --- a/lib/sass/script.rb +++ b/lib/sass/script.rb @@ -1,4 +1,5 @@ require 'strscan' +require 'sass/script/variable' require 'sass/script/funcall' require 'sass/script/operation' require 'sass/script/literal' @@ -22,7 +23,7 @@ module Sass end def self.parse(value, environment, line) - Parser.parse(value, environment).perform + Parser.parse(value).perform(environment) rescue Sass::SyntaxError => e if e.message == "SassScript error" e.instance_eval do diff --git a/lib/sass/script/funcall.rb b/lib/sass/script/funcall.rb index 519bbc97..e67a9278 100644 --- a/lib/sass/script/funcall.rb +++ b/lib/sass/script/funcall.rb @@ -8,18 +8,14 @@ module Sass @args = args end - def to_s - perform.to_s - end - def inspect "#{name}(#{args.map {|a| a.inspect}.join(', ')})" end - def perform - args = self.args.map {|a| a.perform} + def perform(environment) + args = self.args.map {|a| a.perform(environment)} unless Functions.public_instance_methods.include?(name) && name !~ /^__/ - return Script::String.new("#{name}(#{args.join(', ')})") + return Script::String.new("#{name}(#{args.map {|a| a.perform(environment)}.join(', ')})") end return Functions.send(name, *args) diff --git a/lib/sass/script/literal.rb b/lib/sass/script/literal.rb index fbe935a4..cfb97ef8 100644 --- a/lib/sass/script/literal.rb +++ b/lib/sass/script/literal.rb @@ -10,7 +10,7 @@ class Sass::Script::Literal # :nodoc: @value = value end - def perform + def perform(environment) self end diff --git a/lib/sass/script/operation.rb b/lib/sass/script/operation.rb index 75cbe820..4781a6b0 100644 --- a/lib/sass/script/operation.rb +++ b/lib/sass/script/operation.rb @@ -12,17 +12,13 @@ module Sass::Script @operator = operator end - def to_s - self.perform.to_s - end - def inspect "(#{@operator.inspect} #{@operand1.inspect} #{@operand2.inspect})" end - def perform - literal1 = @operand1.perform - literal2 = @operand2.perform + def perform(environment) + literal1 = @operand1.perform(environment) + literal2 = @operand2.perform(environment) begin literal1.send(@operator, literal2) rescue NoMethodError => e diff --git a/lib/sass/script/parser.rb b/lib/sass/script/parser.rb index 96bd4138..7474cab2 100644 --- a/lib/sass/script/parser.rb +++ b/lib/sass/script/parser.rb @@ -2,10 +2,9 @@ require 'sass/script/lexer' module Sass module Script - class Parser # :nodoc: - def initialize(str, environment = {}) + class Parser + def initialize(str) @lexer = Lexer.new(str) - @environment = environment end def parse @@ -88,9 +87,7 @@ RUBY def variable return literal unless c = try_tok(:const) - (val = @environment[c.last]) && (return val) - - raise SyntaxError.new("Undefined variable: \"!#{c.last}\".") + Variable.new(c.last) end def literal diff --git a/lib/sass/script/unary_operation.rb b/lib/sass/script/unary_operation.rb index 7209515e..9f82502d 100644 --- a/lib/sass/script/unary_operation.rb +++ b/lib/sass/script/unary_operation.rb @@ -5,17 +5,13 @@ module Sass::Script @operator = operator end - def to_s - self.perform.to_s - end - def inspect "(#{@operator.inspect} #{@operand.inspect})" end - def perform + def perform(environment) operator = "unary_#{@operator}" - literal = @operand.perform + literal = @operand.perform(environment) literal.send(operator) rescue NoMethodError => e raise e unless e.name.to_s == operator.to_s diff --git a/lib/sass/script/variable.rb b/lib/sass/script/variable.rb new file mode 100644 index 00000000..33e157c0 --- /dev/null +++ b/lib/sass/script/variable.rb @@ -0,0 +1,20 @@ +module Sass + module Script + class Variable # :nodoc: + attr_reader :name + + def initialize(name) + @name = name + end + + def inspect + "!#{name}" + end + + def perform(environment) + (val = environment[name]) && (return val) + raise SyntaxError.new("Undefined variable: \"!#{name}\".") + end + end + end +end