Class: SyntaxTree::RegexpLiteral

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree.rb

Overview

RegexpLiteral represents a regular expression literal.

/.+/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of RegexpLiteral.



10041
10042
10043
10044
10045
10046
10047
# File 'lib/syntax_tree.rb', line 10041

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



10026
10027
10028
# File 'lib/syntax_tree.rb', line 10026

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



10039
10040
10041
# File 'lib/syntax_tree.rb', line 10039

def comments
  @comments
end

#endingObject (readonly)

String

the ending of the regular expression literal



10029
10030
10031
# File 'lib/syntax_tree.rb', line 10029

def ending
  @ending
end

#locationObject (readonly)

Location

the location of this node



10036
10037
10038
# File 'lib/syntax_tree.rb', line 10036

def location
  @location
end

#partsObject (readonly)

Array[ StringEmbExpr | StringDVar | TStringContent ]

the parts of the

regular expression literal



10033
10034
10035
# File 'lib/syntax_tree.rb', line 10033

def parts
  @parts
end

Instance Method Details

#child_nodesObject



10049
10050
10051
# File 'lib/syntax_tree.rb', line 10049

def child_nodes
  parts
end

#format(q) ⇒ Object



10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
# File 'lib/syntax_tree.rb', line 10053

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(ending[1..-1])
    end
  else
    q.group do
      q.text("/")
      q.format_each(parts)
      q.text("/")
      q.text(ending[1..-1])
    end
  end
end

#pretty_print(q) ⇒ Object



10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
# File 'lib/syntax_tree.rb', line 10093

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("regexp_literal")

    q.breakable
    q.group(2, "(", ")") { q.seplist(parts) { |part| q.pp(part) } }

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
# File 'lib/syntax_tree.rb', line 10104

def to_json(*opts)
  {
    type: :regexp_literal,
    beging: beginning,
    ending: ending,
    parts: parts,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end