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



7890
7891
7892
7893
7894
7895
# File 'lib/syntax_tree/node.rb', line 7890

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



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

def comments
  @comments
end

#nameObject (readonly)

Symbol

the symbol version of the value



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

def name
  @name
end

#valueObject (readonly)

String

the operator



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

def value
  @value
end

Instance Method Details

#===(other) ⇒ Object



7923
7924
7925
# File 'lib/syntax_tree/node.rb', line 7923

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

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



7901
7902
7903
# File 'lib/syntax_tree/node.rb', line 7901

def child_nodes
  []
end

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



7905
7906
7907
7908
7909
7910
7911
# File 'lib/syntax_tree/node.rb', line 7905

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



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

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

#format(q) ⇒ Object



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

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