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.



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

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



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

def comments
  @comments
end

#valueObject (readonly)

String

the name of the global backreference variable



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

def value
  @value
end

Instance Method Details

#===(other) ⇒ Object



1670
1671
1672
# File 'lib/syntax_tree/node.rb', line 1670

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

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



1645
1646
1647
# File 'lib/syntax_tree/node.rb', line 1645

def child_nodes
  []
end

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



1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
# File 'lib/syntax_tree/node.rb', line 1649

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



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

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

#format(q) ⇒ Object



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

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