PageRenderTime 22ms CodeModel.GetById 7ms app.highlight 12ms RepoModel.GetById 2ms app.codeStats 0ms

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

http://github.com/scalate/scalate
Ruby | 95 lines | 64 code | 12 blank | 19 comment | 11 complexity | 70c0ece5db9526ca44a08275cd8e1957 MD5 | raw file
 1require 'set'
 2require 'sass/script/string'
 3require 'sass/script/number'
 4require 'sass/script/color'
 5require 'sass/script/functions'
 6require 'sass/script/unary_operation'
 7require 'sass/script/interpolation'
 8require 'sass/script/string_interpolation'
 9
10module Sass::Script
11  # A SassScript parse node representing a binary operation,
12  # such as `$a + $b` or `"foo" + 1`.
13  class Operation < Node
14    attr_reader :operand1
15    attr_reader :operand2
16    attr_reader :operator
17
18    # @param operand1 [Script::Node] The parse-tree node
19    #   for the right-hand side of the operator
20    # @param operand2 [Script::Node] The parse-tree node
21    #   for the left-hand side of the operator
22    # @param operator [Symbol] The operator to perform.
23    #   This should be one of the binary operator names in {Lexer::OPERATORS}
24    def initialize(operand1, operand2, operator)
25      @operand1 = operand1
26      @operand2 = operand2
27      @operator = operator
28      super()
29    end
30
31    # @return [String] A human-readable s-expression representation of the operation
32    def inspect
33      "(#{@operator.inspect} #{@operand1.inspect} #{@operand2.inspect})"
34    end
35
36    # @see Node#to_sass
37    def to_sass(opts = {})
38      pred = Sass::Script::Parser.precedence_of(@operator)
39      o1 = operand_to_sass @operand1, :left, opts
40      o2 = operand_to_sass @operand2, :right, opts
41      sep =
42        case @operator
43        when :comma; ", "
44        when :concat; " "
45        else; " #{Lexer::OPERATORS_REVERSE[@operator]} "
46        end
47      "#{o1}#{sep}#{o2}"
48    end
49
50    # Returns the operands for this operation.
51    #
52    # @return [Array<Node>]
53    # @see Node#children
54    def children
55      [@operand1, @operand2]
56    end
57
58    protected
59
60    # Evaluates the operation.
61    #
62    # @param environment [Sass::Environment] The environment in which to evaluate the SassScript
63    # @return [Literal] The SassScript object that is the value of the operation
64    # @raise [Sass::SyntaxError] if the operation is undefined for the operands
65    def _perform(environment)
66      literal1 = @operand1.perform(environment)
67      literal2 = @operand2.perform(environment)
68
69      if @operator == :concat && context == :equals
70        literal1 = Sass::Script::String.new(literal1.value) if literal1.is_a?(Sass::Script::String)
71        literal2 = Sass::Script::String.new(literal2.value) if literal2.is_a?(Sass::Script::String)
72      end
73
74      begin
75        opts(literal1.send(@operator, literal2))
76      rescue NoMethodError => e
77        raise e unless e.name.to_s == @operator.to_s
78        raise Sass::SyntaxError.new("Undefined operation: \"#{literal1} #{@operator} #{literal2}\".")
79      end
80    end
81
82    private
83
84    def operand_to_sass(op, side, opts)
85      return op.to_sass(opts) unless op.is_a?(Operation)
86
87      pred = Sass::Script::Parser.precedence_of(@operator)
88      sub_pred = Sass::Script::Parser.precedence_of(op.operator)
89      assoc = Sass::Script::Parser.associative?(@operator)
90      return "(#{op.to_sass(opts)})" if sub_pred < pred ||
91        (side == :right && sub_pred == pred && !assoc)
92      op.to_sass(opts)
93    end
94  end
95end