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

Constructor Details

#initialize(value:, location:, comments: []) ⇒ Backref

Returns a new instance of Backref.



1514
1515
1516
1517
1518
# File 'lib/syntax_tree/node.rb', line 1514

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



1512
1513
1514
# File 'lib/syntax_tree/node.rb', line 1512

def comments
  @comments
end

#valueObject (readonly)

String

the name of the global backreference variable



1509
1510
1511
# File 'lib/syntax_tree/node.rb', line 1509

def value
  @value
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



1520
1521
1522
# File 'lib/syntax_tree/node.rb', line 1520

def child_nodes
  []
end

#deconstruct_keys(keys) ⇒ Object



1526
1527
1528
# File 'lib/syntax_tree/node.rb', line 1526

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

#format(q) ⇒ Object



1530
1531
1532
# File 'lib/syntax_tree/node.rb', line 1530

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

#pretty_print(q) ⇒ Object



1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
# File 'lib/syntax_tree/node.rb', line 1534

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("backref")

    q.breakable
    q.pp(value)

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



1545
1546
1547
1548
1549
# File 'lib/syntax_tree/node.rb', line 1545

def to_json(*opts)
  { type: :backref, value: value, loc: location, cmts: comments }.to_json(
    *opts
  )
end