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.



2001
2002
2003
2004
2005
# File 'lib/syntax_tree.rb', line 2001

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



1999
2000
2001
# File 'lib/syntax_tree.rb', line 1999

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



1996
1997
1998
# File 'lib/syntax_tree.rb', line 1996

def location
  @location
end

#valueObject (readonly)

String

the name of the global backreference variable



1993
1994
1995
# File 'lib/syntax_tree.rb', line 1993

def value
  @value
end

Instance Method Details

#child_nodesObject



2007
2008
2009
# File 'lib/syntax_tree.rb', line 2007

def child_nodes
  []
end

#format(q) ⇒ Object



2011
2012
2013
# File 'lib/syntax_tree.rb', line 2011

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

#pretty_print(q) ⇒ Object



2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
# File 'lib/syntax_tree.rb', line 2015

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



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

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