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.



7954
7955
7956
7957
7958
7959
# File 'lib/syntax_tree/node.rb', line 7954

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



7952
7953
7954
# File 'lib/syntax_tree/node.rb', line 7952

def comments
  @comments
end

#nameObject (readonly)

Symbol

the symbol version of the value



7949
7950
7951
# File 'lib/syntax_tree/node.rb', line 7949

def name
  @name
end

#valueObject (readonly)

String

the operator



7946
7947
7948
# File 'lib/syntax_tree/node.rb', line 7946

def value
  @value
end

Instance Method Details

#===(other) ⇒ Object



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

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

#accept(visitor) ⇒ Object



7961
7962
7963
# File 'lib/syntax_tree/node.rb', line 7961

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

#child_nodesObject Also known as: deconstruct



7965
7966
7967
# File 'lib/syntax_tree/node.rb', line 7965

def child_nodes
  []
end

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



7969
7970
7971
7972
7973
7974
7975
# File 'lib/syntax_tree/node.rb', line 7969

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



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

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

#format(q) ⇒ Object



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

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