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.



13310
13311
13312
13313
13314
13315
# File 'lib/syntax_tree.rb', line 13310

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



13299
13300
13301
# File 'lib/syntax_tree.rb', line 13299

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



13308
13309
13310
# File 'lib/syntax_tree.rb', line 13308

def comments
  @comments
end

#elementsObject (readonly)

Array[ Word ]

the elements of this array



13302
13303
13304
# File 'lib/syntax_tree.rb', line 13302

def elements
  @elements
end

#locationObject (readonly)

Location

the location of this node



13305
13306
13307
# File 'lib/syntax_tree.rb', line 13305

def location
  @location
end

Instance Method Details

#child_nodesObject



13317
13318
13319
# File 'lib/syntax_tree.rb', line 13317

def child_nodes
  []
end

#format(q) ⇒ Object



13321
13322
13323
13324
13325
13326
13327
13328
13329
13330
13331
13332
13333
13334
13335
13336
13337
13338
# File 'lib/syntax_tree.rb', line 13321

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



13340
13341
13342
13343
13344
13345
13346
13347
13348
13349
# File 'lib/syntax_tree.rb', line 13340

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



13351
13352
13353
13354
13355
# File 'lib/syntax_tree.rb', line 13351

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