Class: SyntaxTree::Words

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

Overview

Words represents a string literal array with interpolation.

%W[one two three]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(beginning:, elements:, location:, comments: []) ⇒ Words

Returns a new instance of Words.



13605
13606
13607
13608
13609
13610
# File 'lib/syntax_tree.rb', line 13605

def initialize(beginning:, elements:, location:, comments: [])
  @beginning = beginning
  @elements = elements
  @location = location
  @comments = comments
end

Instance Attribute Details

#beginningObject (readonly)

WordsBeg

the token that opens this array literal



13594
13595
13596
# File 'lib/syntax_tree.rb', line 13594

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



13603
13604
13605
# File 'lib/syntax_tree.rb', line 13603

def comments
  @comments
end

#elementsObject (readonly)

Array[ Word ]

the elements of this array



13597
13598
13599
# File 'lib/syntax_tree.rb', line 13597

def elements
  @elements
end

#locationObject (readonly)

Location

the location of this node



13600
13601
13602
# File 'lib/syntax_tree.rb', line 13600

def location
  @location
end

Instance Method Details

#child_nodesObject



13612
13613
13614
# File 'lib/syntax_tree.rb', line 13612

def child_nodes
  []
end

#format(q) ⇒ Object



13616
13617
13618
13619
13620
13621
13622
13623
13624
13625
13626
13627
13628
13629
13630
13631
13632
13633
# File 'lib/syntax_tree.rb', line 13616

def format(q)
  opening, closing = "%W[", "]"

  if elements.any? { |element| element.match?(/[\[\]]/) }
    opening = beginning.value
    closing = Quotes.matching(opening[2])
  end

  q.group(0, opening, closing) do
    q.indent do
      q.breakable("")
      q.seplist(elements, -> { q.breakable }) do |element|
        q.format(element)
      end
    end
    q.breakable("")
  end
end

#pretty_print(q) ⇒ Object



13635
13636
13637
13638
13639
13640
13641
13642
13643
13644
# File 'lib/syntax_tree.rb', line 13635

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

    q.breakable
    q.group(2, "(", ")") { q.seplist(elements) { |element| q.pp(element) } }

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

#to_json(*opts) ⇒ Object



13646
13647
13648
13649
13650
# File 'lib/syntax_tree.rb', line 13646

def to_json(*opts)
  { type: :words, elems: elements, loc: location, cmts: comments }.to_json(
    *opts
  )
end