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

#construct_keys, #pretty_print, #to_json

Constructor Details

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

Returns a new instance of RegexpLiteral.



7735
7736
7737
7738
7739
7740
7741
# File 'lib/syntax_tree/node.rb', line 7735

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



7723
7724
7725
# File 'lib/syntax_tree/node.rb', line 7723

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



7733
7734
7735
# File 'lib/syntax_tree/node.rb', line 7733

def comments
  @comments
end

#endingObject (readonly)

String

the ending of the regular expression literal



7726
7727
7728
# File 'lib/syntax_tree/node.rb', line 7726

def ending
  @ending
end

#partsObject (readonly)

Array[ StringEmbExpr | StringDVar | TStringContent ]

the parts of the

regular expression literal



7730
7731
7732
# File 'lib/syntax_tree/node.rb', line 7730

def parts
  @parts
end

Instance Method Details

#accept(visitor) ⇒ Object



7743
7744
7745
# File 'lib/syntax_tree/node.rb', line 7743

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

#child_nodesObject Also known as: deconstruct



7747
7748
7749
# File 'lib/syntax_tree/node.rb', line 7747

def child_nodes
  parts
end

#deconstruct_keys(_keys) ⇒ Object



7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
# File 'lib/syntax_tree/node.rb', line 7753

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

#format(q) ⇒ Object



7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
# File 'lib/syntax_tree/node.rb', line 7764

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(options)
    end
  else
    q.group do
      q.text("/")
      q.format_each(parts)
      q.text("/")
      q.text(options)
    end
  end
end

#optionsObject



7804
7805
7806
# File 'lib/syntax_tree/node.rb', line 7804

def options
  ending[1..]
end