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.



2031
2032
2033
2034
2035
# File 'lib/syntax_tree.rb', line 2031

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



2029
2030
2031
# File 'lib/syntax_tree.rb', line 2029

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



2026
2027
2028
# File 'lib/syntax_tree.rb', line 2026

def location
  @location
end

#valueObject (readonly)

String

the name of the global backreference variable



2023
2024
2025
# File 'lib/syntax_tree.rb', line 2023

def value
  @value
end

Instance Method Details

#child_nodesObject



2037
2038
2039
# File 'lib/syntax_tree.rb', line 2037

def child_nodes
  []
end

#format(q) ⇒ Object



2041
2042
2043
# File 'lib/syntax_tree.rb', line 2041

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

#pretty_print(q) ⇒ Object



2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
# File 'lib/syntax_tree.rb', line 2045

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



2056
2057
2058
2059
2060
# File 'lib/syntax_tree.rb', line 2056

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