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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of RegexpLiteral.



8647
8648
8649
8650
8651
8652
8653
# File 'lib/syntax_tree/node.rb', line 8647

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



8632
8633
8634
# File 'lib/syntax_tree/node.rb', line 8632

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



8645
8646
8647
# File 'lib/syntax_tree/node.rb', line 8645

def comments
  @comments
end

#endingObject (readonly)

String

the ending of the regular expression literal



8635
8636
8637
# File 'lib/syntax_tree/node.rb', line 8635

def ending
  @ending
end

#locationObject (readonly)

Location

the location of this node



8642
8643
8644
# File 'lib/syntax_tree/node.rb', line 8642

def location
  @location
end

#partsObject (readonly)

Array[ StringEmbExpr | StringDVar | TStringContent ]

the parts of the

regular expression literal



8639
8640
8641
# File 'lib/syntax_tree/node.rb', line 8639

def parts
  @parts
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



8655
8656
8657
# File 'lib/syntax_tree/node.rb', line 8655

def child_nodes
  parts
end

#deconstruct_keys(keys) ⇒ Object



8661
8662
8663
8664
8665
8666
8667
8668
8669
# File 'lib/syntax_tree/node.rb', line 8661

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

#format(q) ⇒ Object



8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
# File 'lib/syntax_tree/node.rb', line 8671

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

#pretty_print(q) ⇒ Object



8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
# File 'lib/syntax_tree/node.rb', line 8711

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

    q.breakable
    q.group(2, "(", ")") { q.seplist(parts) { |part| q.pp(part) } }

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

#to_json(*opts) ⇒ Object



8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
# File 'lib/syntax_tree/node.rb', line 8722

def to_json(*opts)
  {
    type: :regexp_literal,
    beging: beginning,
    ending: ending,
    parts: parts,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end