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
- Symbol
-
the operator used between the two expressions
Public Class Methods
Source
# File lib/syntax_tree/node.rb, line 2071 def initialize(left:, operator:, right:, location:) @left = left @operator = operator @right = right @location = location @comments = [] end
Public Instance Methods
Source
# File lib/syntax_tree/node.rb, line 2143 def ===(other) other.is_a?(Binary) && left === other.left && operator === other.operator && right === other.right end
Source
# File lib/syntax_tree/node.rb, line 2079 def accept(visitor) visitor.visit_binary(self) end
Source
# File lib/syntax_tree/node.rb, line 2083 def child_nodes [left, right] end
Also aliased as: deconstruct
Source
# File lib/syntax_tree/node.rb, line 2087 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
Source
# File lib/syntax_tree/node.rb, line 2102 def deconstruct_keys(_keys) { left: left, operator: operator, right: right, location: location, comments: comments } end
Source
# File lib/syntax_tree/node.rb, line 2112 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