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, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

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

Returns a new instance of RegexpLiteral.



9229
9230
9231
9232
9233
9234
9235
# File 'lib/syntax_tree/node.rb', line 9229

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

Instance Attribute Details

#beginningObject (readonly)

String

the beginning of the regular expression literal



9217
9218
9219
# File 'lib/syntax_tree/node.rb', line 9217

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



9227
9228
9229
# File 'lib/syntax_tree/node.rb', line 9227

def comments
  @comments
end

#endingObject (readonly)

String

the ending of the regular expression literal



9220
9221
9222
# File 'lib/syntax_tree/node.rb', line 9220

def ending
  @ending
end

#partsObject (readonly)

Array[ StringEmbExpr | StringDVar | TStringContent ]

the parts of the

regular expression literal



9224
9225
9226
# File 'lib/syntax_tree/node.rb', line 9224

def parts
  @parts
end

Instance Method Details

#===(other) ⇒ Object



9311
9312
9313
9314
9315
# File 'lib/syntax_tree/node.rb', line 9311

def ===(other)
  other.is_a?(RegexpLiteral) && beginning === other.beginning &&
    ending === other.ending && options === other.options &&
    ArrayMatch.call(parts, other.parts)
end

#accept(visitor) ⇒ Object



9237
9238
9239
# File 'lib/syntax_tree/node.rb', line 9237

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

#child_nodesObject Also known as: deconstruct



9241
9242
9243
# File 'lib/syntax_tree/node.rb', line 9241

def child_nodes
  parts
end

#copy(beginning: nil, ending: nil, parts: nil, location: nil) ⇒ Object



9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
# File 'lib/syntax_tree/node.rb', line 9245

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_keys(_keys) ⇒ Object



9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
# File 'lib/syntax_tree/node.rb', line 9260

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

#format(q) ⇒ Object



9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
# File 'lib/syntax_tree/node.rb', line 9271

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



9317
9318
9319
# File 'lib/syntax_tree/node.rb', line 9317

def options
  ending[1..]
end