/scalate-jruby/src/main/resources/haml-3.0.25/lib/sass/script/unary_operation.rb

http://github.com/scalate/scalate · Ruby · 57 lines · 34 code · 5 blank · 18 comment · 4 complexity · b5bb7309afa4bd4f216fcafdcf2eee9b MD5 · raw file

  1. module Sass::Script
  2. # A SassScript parse node representing a unary operation,
  3. # such as `-$b` or `not true`.
  4. #
  5. # Currently only `-`, `/`, and `not` are unary operators.
  6. class UnaryOperation < Node
  7. # @param operand [Script::Node] The parse-tree node
  8. # for the object of the operator
  9. # @param operator [Symbol] The operator to perform
  10. def initialize(operand, operator)
  11. @operand = operand
  12. @operator = operator
  13. super()
  14. end
  15. # @return [String] A human-readable s-expression representation of the operation
  16. def inspect
  17. "(#{@operator.inspect} #{@operand.inspect})"
  18. end
  19. # @see Node#to_sass
  20. def to_sass(opts = {})
  21. operand = @operand.to_sass(opts)
  22. if @operand.is_a?(Operation) ||
  23. (@operator == :minus &&
  24. (operand =~ Sass::SCSS::RX::IDENT) == 0)
  25. operand = "(#{@operand.to_sass(opts)})"
  26. end
  27. op = Lexer::OPERATORS_REVERSE[@operator]
  28. op + (op =~ /[a-z]/ ? " " : "") + operand
  29. end
  30. # Returns the operand of the operation.
  31. #
  32. # @return [Array<Node>]
  33. # @see Node#children
  34. def children
  35. [@operand]
  36. end
  37. protected
  38. # Evaluates the operation.
  39. #
  40. # @param environment [Sass::Environment] The environment in which to evaluate the SassScript
  41. # @return [Literal] The SassScript object that is the value of the operation
  42. # @raise [Sass::SyntaxError] if the operation is undefined for the operand
  43. def _perform(environment)
  44. operator = "unary_#{@operator}"
  45. literal = @operand.perform(environment)
  46. literal.send(operator)
  47. rescue NoMethodError => e
  48. raise e unless e.name.to_s == operator.to_s
  49. raise Sass::SyntaxError.new("Undefined unary operation: \"#{@operator} #{literal}\".")
  50. end
  51. end
  52. end