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.



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

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



10993
10994
10995
# File 'lib/syntax_tree/node.rb', line 10993

def comments
  @comments
end

#operatorObject (readonly)

String

the operator being used



10987
10988
10989
# File 'lib/syntax_tree/node.rb', line 10987

def operator
  @operator
end

#statementObject (readonly)

untyped

the statement on which to operate



10990
10991
10992
# File 'lib/syntax_tree/node.rb', line 10990

def statement
  @statement
end

Instance Method Details

#===(other) ⇒ Object



11038
11039
11040
11041
# File 'lib/syntax_tree/node.rb', line 11038

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

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



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

def child_nodes
  [statement]
end

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



11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
# File 'lib/syntax_tree/node.rb', line 11010

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



11024
11025
11026
11027
11028
11029
11030
11031
# File 'lib/syntax_tree/node.rb', line 11024

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

#format(q) ⇒ Object



11033
11034
11035
11036
# File 'lib/syntax_tree/node.rb', line 11033

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