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



9075
9076
9077
9078
9079
9080
9081
# File 'lib/syntax_tree/node.rb', line 9075

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



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

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



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

def comments
  @comments
end

#endingObject (readonly)

String

the ending of the regular expression literal



9066
9067
9068
# File 'lib/syntax_tree/node.rb', line 9066

def ending
  @ending
end

#partsObject (readonly)

Array[ StringEmbExpr | StringDVar | TStringContent ]

the parts of the

regular expression literal



9070
9071
9072
# File 'lib/syntax_tree/node.rb', line 9070

def parts
  @parts
end

Instance Method Details

#===(other) ⇒ Object



9157
9158
9159
9160
9161
# File 'lib/syntax_tree/node.rb', line 9157

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

#accept(visitor) ⇒ Object



9083
9084
9085
# File 'lib/syntax_tree/node.rb', line 9083

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

#child_nodesObject Also known as: deconstruct



9087
9088
9089
# File 'lib/syntax_tree/node.rb', line 9087

def child_nodes
  parts
end

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



9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
# File 'lib/syntax_tree/node.rb', line 9091

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



9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
# File 'lib/syntax_tree/node.rb', line 9106

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

#format(q) ⇒ Object



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
9147
9148
9149
9150
9151
9152
9153
9154
9155
# File 'lib/syntax_tree/node.rb', line 9117

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



9163
9164
9165
# File 'lib/syntax_tree/node.rb', line 9163

def options
  ending[1..]
end