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:) ⇒ RegexpLiteral

Returns a new instance of RegexpLiteral.



9066
9067
9068
9069
9070
9071
9072
# File 'lib/syntax_tree/node.rb', line 9066

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



9054
9055
9056
# File 'lib/syntax_tree/node.rb', line 9054

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



9064
9065
9066
# File 'lib/syntax_tree/node.rb', line 9064

def comments
  @comments
end

#endingObject (readonly)

String

the ending of the regular expression literal



9057
9058
9059
# File 'lib/syntax_tree/node.rb', line 9057

def ending
  @ending
end

#partsObject (readonly)

Array[ StringEmbExpr | StringDVar | TStringContent ]

the parts of the

regular expression literal



9061
9062
9063
# File 'lib/syntax_tree/node.rb', line 9061

def parts
  @parts
end

Instance Method Details

#===(other) ⇒ Object



9148
9149
9150
9151
9152
# File 'lib/syntax_tree/node.rb', line 9148

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

#accept(visitor) ⇒ Object



9074
9075
9076
# File 'lib/syntax_tree/node.rb', line 9074

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

#child_nodesObject Also known as: deconstruct



9078
9079
9080
# File 'lib/syntax_tree/node.rb', line 9078

def child_nodes
  parts
end

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



9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
# File 'lib/syntax_tree/node.rb', line 9082

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



9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
# File 'lib/syntax_tree/node.rb', line 9097

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

#format(q) ⇒ Object



9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
# File 'lib/syntax_tree/node.rb', line 9108

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



9154
9155
9156
# File 'lib/syntax_tree/node.rb', line 9154

def options
  ending[1..]
end