class SyntaxTree::Binary

Binary represents any expression that involves two sub-expressions with an operator in between. This can be something that looks like a mathematical operation:

1 + 1

but can also be something like pushing a value onto an array:

array << value

Attributes

comments[R]
Array[ Comment | EmbDoc ]

the comments attached to this node

left[R]
Node

the left-hand side of the expression

operator[R]
Symbol

the operator used between the two expressions

right[R]
Node

the right-hand side of the expression

Public Class Methods

new(left:, operator:, right:, location:) click to toggle source
# File lib/syntax_tree/node.rb, line 2056
def initialize(left:, operator:, right:, location:)
  @left = left
  @operator = operator
  @right = right
  @location = location
  @comments = []
end

Public Instance Methods

===(other) click to toggle source
# File lib/syntax_tree/node.rb, line 2128
def ===(other)
  other.is_a?(Binary) && left === other.left &&
    operator === other.operator && right === other.right
end
accept(visitor) click to toggle source
# File lib/syntax_tree/node.rb, line 2064
def accept(visitor)
  visitor.visit_binary(self)
end
child_nodes() click to toggle source
# File lib/syntax_tree/node.rb, line 2068
def child_nodes
  [left, right]
end
Also aliased as: deconstruct
copy(left: nil, operator: nil, right: nil, location: nil) click to toggle source
# File lib/syntax_tree/node.rb, line 2072
def copy(left: nil, operator: nil, right: nil, location: nil)
  node =
    Binary.new(
      left: left || self.left,
      operator: operator || self.operator,
      right: right || self.right,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end
deconstruct()
Alias for: child_nodes
deconstruct_keys(_keys) click to toggle source
# File lib/syntax_tree/node.rb, line 2087
def deconstruct_keys(_keys)
  {
    left: left,
    operator: operator,
    right: right,
    location: location,
    comments: comments
  }
end
format(q) click to toggle source
# File lib/syntax_tree/node.rb, line 2097
def format(q)
  left = self.left
  power = operator == :**

  q.group do
    q.group { q.format(left) }
    q.text(" ") unless power

    if operator != :<<
      q.group do
        q.text(operator.name)
        q.indent do
          power ? q.breakable_empty : q.breakable_space
          q.format(right)
        end
      end
    elsif left.is_a?(Binary) && left.operator == :<<
      q.group do
        q.text(operator.name)
        q.indent do
          power ? q.breakable_empty : q.breakable_space
          q.format(right)
        end
      end
    else
      q.text("<< ")
      q.format(right)
    end
  end
end
name() click to toggle source
# File lib/syntax_tree/node.rb, line 2037
def name
  to_s.freeze
end