Class: SyntaxTree::Op

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

Overview

Op represents an operator literal in the source.

1 + 2

In the example above, the Op node represents the + operator.

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(value:, location:) ⇒ Op

Returns a new instance of Op.



7887
7888
7889
7890
7891
7892
# File 'lib/syntax_tree/node.rb', line 7887

def initialize(value:, location:)
  @value = value
  @name = value.to_sym
  @location = location
  @comments = []
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



7885
7886
7887
# File 'lib/syntax_tree/node.rb', line 7885

def comments
  @comments
end

#nameObject (readonly)

Symbol

the symbol version of the value



7882
7883
7884
# File 'lib/syntax_tree/node.rb', line 7882

def name
  @name
end

#valueObject (readonly)

String

the operator



7879
7880
7881
# File 'lib/syntax_tree/node.rb', line 7879

def value
  @value
end

Instance Method Details

#===(other) ⇒ Object



7920
7921
7922
# File 'lib/syntax_tree/node.rb', line 7920

def ===(other)
  other.is_a?(Op) && value === other.value
end

#accept(visitor) ⇒ Object



7894
7895
7896
# File 'lib/syntax_tree/node.rb', line 7894

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

#child_nodesObject Also known as: deconstruct



7898
7899
7900
# File 'lib/syntax_tree/node.rb', line 7898

def child_nodes
  []
end

#copy(value: nil, location: nil) ⇒ Object



7902
7903
7904
7905
7906
7907
7908
# File 'lib/syntax_tree/node.rb', line 7902

def copy(value: nil, location: nil)
  node =
    Op.new(value: value || self.value, location: location || self.location)

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

#deconstruct_keys(_keys) ⇒ Object



7912
7913
7914
# File 'lib/syntax_tree/node.rb', line 7912

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

#format(q) ⇒ Object



7916
7917
7918
# File 'lib/syntax_tree/node.rb', line 7916

def format(q)
  q.text(value)
end