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.



7525
7526
7527
7528
7529
7530
7531
# File 'lib/syntax_tree/node.rb', line 7525

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



7513
7514
7515
# File 'lib/syntax_tree/node.rb', line 7513

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



7523
7524
7525
# File 'lib/syntax_tree/node.rb', line 7523

def comments
  @comments
end

#endingObject (readonly)

String

the ending of the regular expression literal



7516
7517
7518
# File 'lib/syntax_tree/node.rb', line 7516

def ending
  @ending
end

#partsObject (readonly)

Array[ StringEmbExpr | StringDVar | TStringContent ]

the parts of the

regular expression literal



7520
7521
7522
# File 'lib/syntax_tree/node.rb', line 7520

def parts
  @parts
end

Instance Method Details

#accept(visitor) ⇒ Object



7533
7534
7535
# File 'lib/syntax_tree/node.rb', line 7533

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

#child_nodesObject Also known as: deconstruct



7537
7538
7539
# File 'lib/syntax_tree/node.rb', line 7537

def child_nodes
  parts
end

#deconstruct_keys(_keys) ⇒ Object



7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
# File 'lib/syntax_tree/node.rb', line 7543

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

#format(q) ⇒ Object



7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
# File 'lib/syntax_tree/node.rb', line 7554

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



7594
7595
7596
# File 'lib/syntax_tree/node.rb', line 7594

def options
  ending[1..]
end