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.



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

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



8009
8010
8011
# File 'lib/syntax_tree/node.rb', line 8009

def comments
  @comments
end

#nameObject (readonly)

Symbol

the symbol version of the value



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

def name
  @name
end

#valueObject (readonly)

String

the operator



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

def value
  @value
end

Instance Method Details

#===(other) ⇒ Object



8044
8045
8046
# File 'lib/syntax_tree/node.rb', line 8044

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

#accept(visitor) ⇒ Object



8018
8019
8020
# File 'lib/syntax_tree/node.rb', line 8018

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

#child_nodesObject Also known as: deconstruct



8022
8023
8024
# File 'lib/syntax_tree/node.rb', line 8022

def child_nodes
  []
end

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



8026
8027
8028
8029
8030
8031
8032
# File 'lib/syntax_tree/node.rb', line 8026

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



8036
8037
8038
# File 'lib/syntax_tree/node.rb', line 8036

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

#format(q) ⇒ Object



8040
8041
8042
# File 'lib/syntax_tree/node.rb', line 8040

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