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.



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

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



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

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



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

def comments
  @comments
end

#endingObject (readonly)

String

the ending of the regular expression literal



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

def ending
  @ending
end

#partsObject (readonly)

Array[ StringEmbExpr | StringDVar | TStringContent ]

the parts of the

regular expression literal



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

def parts
  @parts
end

Instance Method Details

#===(other) ⇒ Object



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

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

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



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

def child_nodes
  parts
end

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



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

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



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

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

#format(q) ⇒ Object



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
9310
# File 'lib/syntax_tree/node.rb', line 9272

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



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

def options
  ending[1..]
end