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, #pretty_print, #to_json

Constructor Details

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

Returns a new instance of Unary.



11004
11005
11006
11007
11008
11009
# File 'lib/syntax_tree/node.rb', line 11004

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



11002
11003
11004
# File 'lib/syntax_tree/node.rb', line 11002

def comments
  @comments
end

#operatorObject (readonly)

String

the operator being used



10996
10997
10998
# File 'lib/syntax_tree/node.rb', line 10996

def operator
  @operator
end

#statementObject (readonly)

untyped

the statement on which to operate



10999
11000
11001
# File 'lib/syntax_tree/node.rb', line 10999

def statement
  @statement
end

Instance Method Details

#===(other) ⇒ Object



11047
11048
11049
11050
# File 'lib/syntax_tree/node.rb', line 11047

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

#accept(visitor) ⇒ Object



11011
11012
11013
# File 'lib/syntax_tree/node.rb', line 11011

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

#child_nodesObject Also known as: deconstruct



11015
11016
11017
# File 'lib/syntax_tree/node.rb', line 11015

def child_nodes
  [statement]
end

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



11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
# File 'lib/syntax_tree/node.rb', line 11019

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



11033
11034
11035
11036
11037
11038
11039
11040
# File 'lib/syntax_tree/node.rb', line 11033

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

#format(q) ⇒ Object



11042
11043
11044
11045
# File 'lib/syntax_tree/node.rb', line 11042

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