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.



9532
9533
9534
9535
9536
9537
9538
# File 'lib/syntax_tree.rb', line 9532

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



9517
9518
9519
# File 'lib/syntax_tree.rb', line 9517

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



9530
9531
9532
# File 'lib/syntax_tree.rb', line 9530

def comments
  @comments
end

#endingObject (readonly)

String

the ending of the regular expression literal



9520
9521
9522
# File 'lib/syntax_tree.rb', line 9520

def ending
  @ending
end

#locationObject (readonly)

Location

the location of this node



9527
9528
9529
# File 'lib/syntax_tree.rb', line 9527

def location
  @location
end

#partsObject (readonly)

Array[ StringEmbExpr | StringDVar | TStringContent ]

the parts of the

regular expression literal



9524
9525
9526
# File 'lib/syntax_tree.rb', line 9524

def parts
  @parts
end

Instance Method Details

#child_nodesObject



9540
9541
9542
# File 'lib/syntax_tree.rb', line 9540

def child_nodes
  parts
end

#format(q) ⇒ Object



9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
# File 'lib/syntax_tree.rb', line 9544

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
  else
    q.group do
      q.text(braces ? "%r{" : "/")
      q.format_each(parts)
      q.text(braces ? "}" : "/")
      q.text(ending[1..-1])
    end
  end
end

#pretty_print(q) ⇒ Object



9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
# File 'lib/syntax_tree.rb', line 9563

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



9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
# File 'lib/syntax_tree.rb', line 9574

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