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

Constructor Details

#initialize(value:, location:) ⇒ Op

Returns a new instance of Op.



7995
7996
7997
7998
7999
8000
# File 'lib/syntax_tree/node.rb', line 7995

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



7993
7994
7995
# File 'lib/syntax_tree/node.rb', line 7993

def comments
  @comments
end

#nameObject (readonly)

Symbol

the symbol version of the value



7990
7991
7992
# File 'lib/syntax_tree/node.rb', line 7990

def name
  @name
end

#valueObject (readonly)

String

the operator



7987
7988
7989
# File 'lib/syntax_tree/node.rb', line 7987

def value
  @value
end

Instance Method Details

#===(other) ⇒ Object



8028
8029
8030
# File 'lib/syntax_tree/node.rb', line 8028

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

#accept(visitor) ⇒ Object



8002
8003
8004
# File 'lib/syntax_tree/node.rb', line 8002

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

#child_nodesObject Also known as: deconstruct



8006
8007
8008
# File 'lib/syntax_tree/node.rb', line 8006

def child_nodes
  []
end

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



8010
8011
8012
8013
8014
8015
8016
# File 'lib/syntax_tree/node.rb', line 8010

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



8020
8021
8022
# File 'lib/syntax_tree/node.rb', line 8020

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

#format(q) ⇒ Object



8024
8025
8026
# File 'lib/syntax_tree/node.rb', line 8024

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