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

Constructor Details

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

Returns a new instance of RegexpLiteral.



7361
7362
7363
7364
7365
7366
7367
# File 'lib/syntax_tree/node.rb', line 7361

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



7349
7350
7351
# File 'lib/syntax_tree/node.rb', line 7349

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



7359
7360
7361
# File 'lib/syntax_tree/node.rb', line 7359

def comments
  @comments
end

#endingObject (readonly)

String

the ending of the regular expression literal



7352
7353
7354
# File 'lib/syntax_tree/node.rb', line 7352

def ending
  @ending
end

#partsObject (readonly)

Array[ StringEmbExpr | StringDVar | TStringContent ]

the parts of the

regular expression literal



7356
7357
7358
# File 'lib/syntax_tree/node.rb', line 7356

def parts
  @parts
end

Instance Method Details

#accept(visitor) ⇒ Object



7369
7370
7371
# File 'lib/syntax_tree/node.rb', line 7369

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

#child_nodesObject Also known as: deconstruct



7373
7374
7375
# File 'lib/syntax_tree/node.rb', line 7373

def child_nodes
  parts
end

#deconstruct_keys(_keys) ⇒ Object



7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
# File 'lib/syntax_tree/node.rb', line 7379

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

#format(q) ⇒ Object



7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
# File 'lib/syntax_tree/node.rb', line 7390

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



7430
7431
7432
# File 'lib/syntax_tree/node.rb', line 7430

def options
  ending[1..]
end