From 7aedfff5819246424452997285b5f2bfb4760801 Mon Sep 17 00:00:00 2001 From: nex3 Date: Sun, 28 Jan 2007 17:24:08 +0000 Subject: [PATCH] Cleaner exception handling in Sass::Constant. git-svn-id: svn://hamptoncatlin.com/haml/trunk@319 7063305b-7217-0410-af8c-cdc13e5119b9 --- lib/sass/constant.rb | 16 ++++++++++------ lib/sass/constant/operation.rb | 7 +++---- lib/sass/error.rb | 4 ++-- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/lib/sass/constant.rb b/lib/sass/constant.rb index 1dac51ed..6c03dc1d 100644 --- a/lib/sass/constant.rb +++ b/lib/sass/constant.rb @@ -35,8 +35,12 @@ module Sass class << self def parse(value, constants, line) - @@line = line - operationalize(parenthesize(tokenize(value)), value, constants).to_s + begin + operationalize(parenthesize(tokenize(value)), value, constants).to_s + rescue SyntaxError => e + e.sass_line = line + raise e + end end private @@ -122,12 +126,12 @@ module Sass Literal.parse(insert_constant(value, constants)) end elsif length == 2 - raise SyntaxError.new("Constant arithmetic error: #{original}", @@line) + raise SyntaxError.new("Constant arithmetic error: #{original}") elsif length == 3 - Operation.new(operationalize(value[0], original, constants), operationalize(value[2], original, constants), value[1], @@line) + Operation.new(operationalize(value[0], original, constants), operationalize(value[2], original, constants), value[1]) else unless length >= 5 && length % 2 == 1 - raise SyntaxError.new("Constant arithmetic error: #{original}", @@line) + raise SyntaxError.new("Constant arithmetic error: #{original}") end if SECOND_ORDER.include?(value[1]) && FIRST_ORDER.include?(value[3]) operationalize([value[0], value[1], operationalize(value[2..4], original, constants), *value[5..-1]], original, constants) @@ -142,7 +146,7 @@ module Sass if value[0] == CONSTANT_CHAR to_return = constants[value[1..-1]] unless to_return - raise SyntaxError.new("Undefined constant: #{value}", @@line) + raise SyntaxError.new("Undefined constant: #{value}") end end to_return diff --git a/lib/sass/constant/operation.rb b/lib/sass/constant/operation.rb index 2dec5d26..0295a4f4 100644 --- a/lib/sass/constant/operation.rb +++ b/lib/sass/constant/operation.rb @@ -4,11 +4,10 @@ require 'sass/constant/color' module Sass::Constant class Operation - def initialize(operand1, operand2, operator, line) + def initialize(operand1, operand2, operator) @operand1 = operand1 @operand2 = operand2 @operator = operator - @line = line end def to_s @@ -23,8 +22,8 @@ module Sass::Constant begin literal1.send(@operator, literal2) rescue NoMethodError => e - raise e unless e.name == @operator - raise Sass::SyntaxError.new("Undefined operation: #{literal1} #{@operator} #{literal2}", @line) + raise e unless e.name == @operator + raise Sass::SyntaxError.new("Undefined operation: #{literal1} #{@operator} #{literal2}") end end end diff --git a/lib/sass/error.rb b/lib/sass/error.rb index a205cfe4..8b81ff35 100644 --- a/lib/sass/error.rb +++ b/lib/sass/error.rb @@ -6,7 +6,7 @@ module Sass # because of a faulty template. class SyntaxError < StandardError # The line of the Sass template on which the exception was thrown. - attr_reader :sass_line + attr_accessor :sass_line # The name of the file that was being parsed when the exception was raised. # This will be nil unless Sass is being used as an ActionView plugin. @@ -14,7 +14,7 @@ module Sass # Creates a new SyntaxError. # +lineno+ should be the line of the Sass template on which the error occurred. - def initialize(msg, lineno) + def initialize(msg, lineno = nil) super(msg) @sass_line = lineno end