Class: SyntaxTree::Backref

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

Backref represents a global variable referencing a matched value. It comes in the form of a $ followed by a positive integer.

$1

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:) ⇒ Backref

Returns a new instance of Backref.



1631
1632
1633
1634
1635
# File 'lib/syntax_tree/node.rb', line 1631

def initialize(value:, location:)
  @value = value
  @location = location
  @comments = []
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



1629
1630
1631
# File 'lib/syntax_tree/node.rb', line 1629

def comments
  @comments
end

#valueObject (readonly)

String

the name of the global backreference variable



1626
1627
1628
# File 'lib/syntax_tree/node.rb', line 1626

def value
  @value
end

Instance Method Details

#===(other) ⇒ Object



1666
1667
1668
# File 'lib/syntax_tree/node.rb', line 1666

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

#accept(visitor) ⇒ Object



1637
1638
1639
# File 'lib/syntax_tree/node.rb', line 1637

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

#child_nodesObject Also known as: deconstruct



1641
1642
1643
# File 'lib/syntax_tree/node.rb', line 1641

def child_nodes
  []
end

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



1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
# File 'lib/syntax_tree/node.rb', line 1645

def copy(value: nil, location: nil)
  node =
    Backref.new(
      value: value || self.value,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



1658
1659
1660
# File 'lib/syntax_tree/node.rb', line 1658

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

#format(q) ⇒ Object



1662
1663
1664
# File 'lib/syntax_tree/node.rb', line 1662

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