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.



9214
9215
9216
9217
9218
9219
9220
# File 'lib/syntax_tree/node.rb', line 9214

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



9202
9203
9204
# File 'lib/syntax_tree/node.rb', line 9202

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



9212
9213
9214
# File 'lib/syntax_tree/node.rb', line 9212

def comments
  @comments
end

#endingObject (readonly)

String

the ending of the regular expression literal



9205
9206
9207
# File 'lib/syntax_tree/node.rb', line 9205

def ending
  @ending
end

#partsObject (readonly)

Array[ StringEmbExpr | StringDVar | TStringContent ]

the parts of the

regular expression literal



9209
9210
9211
# File 'lib/syntax_tree/node.rb', line 9209

def parts
  @parts
end

Instance Method Details

#===(other) ⇒ Object



9296
9297
9298
9299
9300
# 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) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



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

def child_nodes
  parts
end

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



9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
# 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_keys(_keys) ⇒ Object



9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
# 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) ⇒ Object



9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
# 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

#optionsObject



9302
9303
9304
# File 'lib/syntax_tree/node.rb', line 9302

def options
  ending[1..]
end