Class: SyntaxTree::Unary

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

Unary represents a unary method being called on an expression, as in ! or ~.

!value

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

#initialize(operator:, statement:, location:) ⇒ Unary

Returns a new instance of Unary.



11154
11155
11156
11157
11158
11159
# File 'lib/syntax_tree/node.rb', line 11154

def initialize(operator:, statement:, location:)
  @operator = operator
  @statement = statement
  @location = location
  @comments = []
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



11152
11153
11154
# File 'lib/syntax_tree/node.rb', line 11152

def comments
  @comments
end

#operatorObject (readonly)

String

the operator being used



11146
11147
11148
# File 'lib/syntax_tree/node.rb', line 11146

def operator
  @operator
end

#statementObject (readonly)

Node

the statement on which to operate



11149
11150
11151
# File 'lib/syntax_tree/node.rb', line 11149

def statement
  @statement
end

Instance Method Details

#===(other) ⇒ Object



11197
11198
11199
11200
# File 'lib/syntax_tree/node.rb', line 11197

def ===(other)
  other.is_a?(Unary) && operator === other.operator &&
    statement === other.statement
end

#accept(visitor) ⇒ Object



11161
11162
11163
# File 'lib/syntax_tree/node.rb', line 11161

def accept(visitor)
  visitor.visit_unary(self)
end

#child_nodesObject Also known as: deconstruct



11165
11166
11167
# File 'lib/syntax_tree/node.rb', line 11165

def child_nodes
  [statement]
end

#copy(operator: nil, statement: nil, location: nil) ⇒ Object



11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
# File 'lib/syntax_tree/node.rb', line 11169

def copy(operator: nil, statement: nil, location: nil)
  node =
    Unary.new(
      operator: operator || self.operator,
      statement: statement || self.statement,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



11183
11184
11185
11186
11187
11188
11189
11190
# File 'lib/syntax_tree/node.rb', line 11183

def deconstruct_keys(_keys)
  {
    operator: operator,
    statement: statement,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



11192
11193
11194
11195
# File 'lib/syntax_tree/node.rb', line 11192

def format(q)
  q.text(operator)
  q.format(statement)
end