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.



9197
9198
9199
9200
9201
9202
9203
# File 'lib/syntax_tree/node.rb', line 9197

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



9185
9186
9187
# File 'lib/syntax_tree/node.rb', line 9185

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



9195
9196
9197
# File 'lib/syntax_tree/node.rb', line 9195

def comments
  @comments
end

#endingObject (readonly)

String

the ending of the regular expression literal



9188
9189
9190
# File 'lib/syntax_tree/node.rb', line 9188

def ending
  @ending
end

#partsObject (readonly)

Array[ StringEmbExpr | StringDVar | TStringContent ]

the parts of the

regular expression literal



9192
9193
9194
# File 'lib/syntax_tree/node.rb', line 9192

def parts
  @parts
end

Instance Method Details

#===(other) ⇒ Object



9279
9280
9281
9282
9283
# File 'lib/syntax_tree/node.rb', line 9279

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

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



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

def child_nodes
  parts
end

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



9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
# File 'lib/syntax_tree/node.rb', line 9213

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



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

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

#format(q) ⇒ Object



9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
# File 'lib/syntax_tree/node.rb', line 9239

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



9285
9286
9287
# File 'lib/syntax_tree/node.rb', line 9285

def options
  ending[1..]
end