class SyntaxTree::RegexpLiteral

RegexpLiteral represents a regular expression literal.

/.+/

Attributes

beginning[R]
String

the beginning of the regular expression literal

comments[R]
Array[ Comment | EmbDoc ]

the comments attached to this node

ending[R]
String

the ending of the regular expression literal

parts[R]
Array[ StringEmbExpr | StringDVar | TStringContent ]

the parts of the

regular expression literal

Public Class Methods

new(beginning:, ending:, parts:, location:) click to toggle source
# File lib/syntax_tree/node.rb, line 9214
def initialize(beginning:, ending:, parts:, location:)
  @beginning = beginning
  @ending = ending
  @parts = parts
  @location = location
  @comments = []
end

Public Instance Methods

===(other) click to toggle source
# File lib/syntax_tree/node.rb, line 9296
def ===(other)
  other.is_a?(RegexpLiteral) && beginning === other.beginning &&
    ending === other.ending && options === other.options &&
    ArrayMatch.call(parts, other.parts)
end
accept(visitor) click to toggle source
# File lib/syntax_tree/node.rb, line 9222
def accept(visitor)
  visitor.visit_regexp_literal(self)
end
child_nodes() click to toggle source
# File lib/syntax_tree/node.rb, line 9226
def child_nodes
  parts
end
Also aliased as: deconstruct
copy(beginning: nil, ending: nil, parts: nil, location: nil) click to toggle source
# File lib/syntax_tree/node.rb, line 9230
def copy(beginning: nil, ending: nil, parts: nil, location: nil)
  node =
    RegexpLiteral.new(
      beginning: beginning || self.beginning,
      ending: ending || self.ending,
      parts: parts || self.parts,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end
deconstruct()
Alias for: child_nodes
deconstruct_keys(_keys) click to toggle source
# File lib/syntax_tree/node.rb, line 9245
def deconstruct_keys(_keys)
  {
    beginning: beginning,
    ending: ending,
    options: options,
    parts: parts,
    location: location,
    comments: comments
  }
end
format(q) click to toggle source
# File lib/syntax_tree/node.rb, line 9256
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
options() click to toggle source
# File lib/syntax_tree/node.rb, line 9302
def options
  ending[1..]
end