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.



13401
13402
13403
13404
13405
13406
# File 'lib/syntax_tree.rb', line 13401

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



13390
13391
13392
# File 'lib/syntax_tree.rb', line 13390

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



13399
13400
13401
# File 'lib/syntax_tree.rb', line 13399

def comments
  @comments
end

#elementsObject (readonly)

Array[ Word ]

the elements of this array



13393
13394
13395
# File 'lib/syntax_tree.rb', line 13393

def elements
  @elements
end

#locationObject (readonly)

Location

the location of this node



13396
13397
13398
# File 'lib/syntax_tree.rb', line 13396

def location
  @location
end

Instance Method Details

#child_nodesObject



13408
13409
13410
# File 'lib/syntax_tree.rb', line 13408

def child_nodes
  []
end

#format(q) ⇒ Object



13412
13413
13414
13415
13416
13417
13418
13419
13420
13421
13422
13423
13424
13425
13426
13427
13428
13429
# File 'lib/syntax_tree.rb', line 13412

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



13431
13432
13433
13434
13435
13436
13437
13438
13439
13440
# File 'lib/syntax_tree.rb', line 13431

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



13442
13443
13444
13445
13446
# File 'lib/syntax_tree.rb', line 13442

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