Class: SyntaxTree::Backref

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree.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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Backref.



1891
1892
1893
1894
1895
# File 'lib/syntax_tree.rb', line 1891

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



1889
1890
1891
# File 'lib/syntax_tree.rb', line 1889

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



1886
1887
1888
# File 'lib/syntax_tree.rb', line 1886

def location
  @location
end

#valueObject (readonly)

String

the name of the global backreference variable



1883
1884
1885
# File 'lib/syntax_tree.rb', line 1883

def value
  @value
end

Instance Method Details

#child_nodesObject



1897
1898
1899
# File 'lib/syntax_tree.rb', line 1897

def child_nodes
  []
end

#format(q) ⇒ Object



1901
1902
1903
# File 'lib/syntax_tree.rb', line 1901

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

#pretty_print(q) ⇒ Object



1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
# File 'lib/syntax_tree.rb', line 1905

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



1916
1917
1918
1919
1920
# File 'lib/syntax_tree.rb', line 1916

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