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.



11187
11188
11189
11190
11191
11192
# File 'lib/syntax_tree/node.rb', line 11187

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



11185
11186
11187
# File 'lib/syntax_tree/node.rb', line 11185

def comments
  @comments
end

#operatorObject (readonly)

String

the operator being used



11179
11180
11181
# File 'lib/syntax_tree/node.rb', line 11179

def operator
  @operator
end

#statementObject (readonly)

Node

the statement on which to operate



11182
11183
11184
# File 'lib/syntax_tree/node.rb', line 11182

def statement
  @statement
end

Instance Method Details

#===(other) ⇒ Object



11230
11231
11232
11233
# File 'lib/syntax_tree/node.rb', line 11230

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

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



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

def child_nodes
  [statement]
end

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



11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
# File 'lib/syntax_tree/node.rb', line 11202

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



11216
11217
11218
11219
11220
11221
11222
11223
# File 'lib/syntax_tree/node.rb', line 11216

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

#format(q) ⇒ Object



11225
11226
11227
11228
# File 'lib/syntax_tree/node.rb', line 11225

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