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.



9965
9966
9967
9968
9969
9970
9971
# File 'lib/syntax_tree.rb', line 9965

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



9950
9951
9952
# File 'lib/syntax_tree.rb', line 9950

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



9963
9964
9965
# File 'lib/syntax_tree.rb', line 9963

def comments
  @comments
end

#endingObject (readonly)

String

the ending of the regular expression literal



9953
9954
9955
# File 'lib/syntax_tree.rb', line 9953

def ending
  @ending
end

#locationObject (readonly)

Location

the location of this node



9960
9961
9962
# File 'lib/syntax_tree.rb', line 9960

def location
  @location
end

#partsObject (readonly)

Array[ StringEmbExpr | StringDVar | TStringContent ]

the parts of the

regular expression literal



9957
9958
9959
# File 'lib/syntax_tree.rb', line 9957

def parts
  @parts
end

Instance Method Details

#child_nodesObject



9973
9974
9975
# File 'lib/syntax_tree.rb', line 9973

def child_nodes
  parts
end

#format(q) ⇒ Object



9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
# File 'lib/syntax_tree.rb', line 9977

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



10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
# File 'lib/syntax_tree.rb', line 10017

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



10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
# File 'lib/syntax_tree.rb', line 10028

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