Cleaner exception handling in Sass::Constant.

git-svn-id: svn://hamptoncatlin.com/haml/trunk@319 7063305b-7217-0410-af8c-cdc13e5119b9
This commit is contained in:
nex3 2007-01-28 17:24:08 +00:00
parent e0ed883ae6
commit 7aedfff581
3 changed files with 15 additions and 12 deletions

View File

@ -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

View File

@ -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

View File

@ -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