Class: SyntaxTree::Binary
Overview
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
Instance Attribute Summary collapse
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#left ⇒ Object
readonly
- Node
-
the left-hand side of the expression.
-
#operator ⇒ Object
readonly
- Symbol
-
the operator used between the two expressions.
-
#right ⇒ Object
readonly
- Node
-
the right-hand side of the expression.
Attributes inherited from Node
Instance Method Summary collapse
- #===(other) ⇒ Object
- #accept(visitor) ⇒ Object
- #child_nodes ⇒ Object (also: #deconstruct)
- #copy(left: nil, operator: nil, right: nil, location: nil) ⇒ Object
- #deconstruct_keys(_keys) ⇒ Object
- #format(q) ⇒ Object
-
#initialize(left:, operator:, right:, location:) ⇒ Binary
constructor
A new instance of Binary.
Methods inherited from Node
#construct_keys, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid
Constructor Details
#initialize(left:, operator:, right:, location:) ⇒ Binary
Returns a new instance of Binary.
2071 2072 2073 2074 2075 2076 2077 |
# File 'lib/syntax_tree/node.rb', line 2071 def initialize(left:, operator:, right:, location:) @left = left @operator = operator @right = right @location = location @comments = [] end |
Instance Attribute Details
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
2069 2070 2071 |
# File 'lib/syntax_tree/node.rb', line 2069 def comments @comments end |
#left ⇒ Object (readonly)
- Node
-
the left-hand side of the expression
2060 2061 2062 |
# File 'lib/syntax_tree/node.rb', line 2060 def left @left end |
#operator ⇒ Object (readonly)
- Symbol
-
the operator used between the two expressions
2063 2064 2065 |
# File 'lib/syntax_tree/node.rb', line 2063 def operator @operator end |
#right ⇒ Object (readonly)
- Node
-
the right-hand side of the expression
2066 2067 2068 |
# File 'lib/syntax_tree/node.rb', line 2066 def right @right end |
Instance Method Details
#===(other) ⇒ Object
2143 2144 2145 2146 |
# File 'lib/syntax_tree/node.rb', line 2143 def ===(other) other.is_a?(Binary) && left === other.left && operator === other.operator && right === other.right end |
#accept(visitor) ⇒ Object
2079 2080 2081 |
# File 'lib/syntax_tree/node.rb', line 2079 def accept(visitor) visitor.visit_binary(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
2083 2084 2085 |
# File 'lib/syntax_tree/node.rb', line 2083 def child_nodes [left, right] end |
#copy(left: nil, operator: nil, right: nil, location: nil) ⇒ Object
2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 |
# 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 |
#deconstruct_keys(_keys) ⇒ Object
2102 2103 2104 2105 2106 2107 2108 2109 2110 |
# File 'lib/syntax_tree/node.rb', line 2102 def deconstruct_keys(_keys) { left: left, operator: operator, right: right, location: location, comments: comments } end |
#format(q) ⇒ Object
2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 |
# 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 |