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.



7269
7270
7271
7272
7273
7274
7275
# File 'lib/syntax_tree/node.rb', line 7269

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



7257
7258
7259
# File 'lib/syntax_tree/node.rb', line 7257

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



7267
7268
7269
# File 'lib/syntax_tree/node.rb', line 7267

def comments
  @comments
end

#endingObject (readonly)

String

the ending of the regular expression literal



7260
7261
7262
# File 'lib/syntax_tree/node.rb', line 7260

def ending
  @ending
end

#partsObject (readonly)

Array[ StringEmbExpr | StringDVar | TStringContent ]

the parts of the

regular expression literal



7264
7265
7266
# File 'lib/syntax_tree/node.rb', line 7264

def parts
  @parts
end

Instance Method Details

#accept(visitor) ⇒ Object



7277
7278
7279
# File 'lib/syntax_tree/node.rb', line 7277

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

#child_nodesObject Also known as: deconstruct



7281
7282
7283
# File 'lib/syntax_tree/node.rb', line 7281

def child_nodes
  parts
end

#deconstruct_keys(keys) ⇒ Object



7287
7288
7289
7290
7291
7292
7293
7294
7295
# File 'lib/syntax_tree/node.rb', line 7287

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

#format(q) ⇒ Object



7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
# File 'lib/syntax_tree/node.rb', line 7297

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