Class: SyntaxTree::RegexpLiteral

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

RegexpLiteral represents a regular expression literal.

/.+/

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#pretty_print, #to_json

Constructor Details

#initialize(beginning:, ending:, parts:, location:, comments: []) ⇒ RegexpLiteral

Returns a new instance of RegexpLiteral.



6727
6728
6729
6730
6731
6732
6733
# File 'lib/syntax_tree/node.rb', line 6727

def initialize(beginning:, ending:, parts:, location:, comments: [])
  @beginning = beginning
  @ending = ending
  @parts = parts
  @location = location
  @comments = comments
end

Instance Attribute Details

#beginningObject (readonly)

String

the beginning of the regular expression literal



6715
6716
6717
# File 'lib/syntax_tree/node.rb', line 6715

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



6725
6726
6727
# File 'lib/syntax_tree/node.rb', line 6725

def comments
  @comments
end

#endingObject (readonly)

String

the ending of the regular expression literal



6718
6719
6720
# File 'lib/syntax_tree/node.rb', line 6718

def ending
  @ending
end

#partsObject (readonly)

Array[ StringEmbExpr | StringDVar | TStringContent ]

the parts of the

regular expression literal



6722
6723
6724
# File 'lib/syntax_tree/node.rb', line 6722

def parts
  @parts
end

Instance Method Details

#accept(visitor) ⇒ Object



6735
6736
6737
# File 'lib/syntax_tree/node.rb', line 6735

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

#child_nodesObject Also known as: deconstruct



6739
6740
6741
# File 'lib/syntax_tree/node.rb', line 6739

def child_nodes
  parts
end

#deconstruct_keys(keys) ⇒ Object



6745
6746
6747
6748
6749
6750
6751
6752
6753
# File 'lib/syntax_tree/node.rb', line 6745

def deconstruct_keys(keys)
  {
    beginning: beginning,
    ending: ending,
    parts: parts,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
# File 'lib/syntax_tree/node.rb', line 6755

def format(q)
  braces = ambiguous?(q) || include?(%r{\/})

  if braces && include?(/[{}]/)
    q.group do
      q.text(beginning)
      q.format_each(parts)
      q.text(ending)
    end
  elsif braces
    q.group do
      q.text("%r{")

      if beginning == "/"
        # If we're changing from a forward slash to a %r{, then we can
        # replace any escaped forward slashes with regular forward slashes.
        parts.each do |part|
          if part.is_a?(TStringContent)
            q.text(part.value.gsub("\\/", "/"))
          else
            q.format(part)
          end
        end
      else
        q.format_each(parts)
      end

      q.text("}")
      q.text(ending[1..-1])
    end
  else
    q.group do
      q.text("/")
      q.format_each(parts)
      q.text("/")
      q.text(ending[1..-1])
    end
  end
end