Class: SyntaxTree::Words
- Inherits:
-
Object
- Object
- SyntaxTree::Words
- Defined in:
- lib/syntax_tree.rb
Overview
Words represents a string literal array with interpolation.
%W[one two three]
Instance Attribute Summary collapse
-
#beginning ⇒ Object
readonly
- WordsBeg
-
the token that opens this array literal.
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#elements ⇒ Object
readonly
- Array[ Word ]
-
the elements of this array.
-
#location ⇒ Object
readonly
- Location
-
the location of this node.
Instance Method Summary collapse
- #child_nodes ⇒ Object
- #format(q) ⇒ Object
-
#initialize(beginning:, elements:, location:, comments: []) ⇒ Words
constructor
A new instance of Words.
- #pretty_print(q) ⇒ Object
- #to_json(*opts) ⇒ Object
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
#beginning ⇒ Object (readonly)
- WordsBeg
-
the token that opens this array literal
13299 13300 13301 |
# File 'lib/syntax_tree.rb', line 13299 def beginning @beginning end |
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
13308 13309 13310 |
# File 'lib/syntax_tree.rb', line 13308 def comments @comments end |
#elements ⇒ Object (readonly)
- Array[ Word ]
-
the elements of this array
13302 13303 13304 |
# File 'lib/syntax_tree.rb', line 13302 def elements @elements end |
#location ⇒ Object (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_nodes ⇒ Object
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 |