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.



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

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



7983
7984
7985
# File 'lib/syntax_tree/node.rb', line 7983

def comments
  @comments
end

#nameObject (readonly)

Symbol

the symbol version of the value



7980
7981
7982
# File 'lib/syntax_tree/node.rb', line 7980

def name
  @name
end

#valueObject (readonly)

String

the operator



7977
7978
7979
# File 'lib/syntax_tree/node.rb', line 7977

def value
  @value
end

Instance Method Details

#===(other) ⇒ Object



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

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

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



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

def child_nodes
  []
end

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



8000
8001
8002
8003
8004
8005
8006
# File 'lib/syntax_tree/node.rb', line 8000

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



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

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

#format(q) ⇒ Object



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

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