Class: SyntaxTree::Word

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

Overview

Word represents an element within a special array literal that accepts interpolation.

%W[a#{b}c xyz]

In the example above, there would be two Word nodes within a parent Words node.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parts:, location:, comments: []) ⇒ Word

Returns a new instance of Word.



13330
13331
13332
13333
13334
# File 'lib/syntax_tree.rb', line 13330

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



13328
13329
13330
# File 'lib/syntax_tree.rb', line 13328

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



13325
13326
13327
# File 'lib/syntax_tree.rb', line 13325

def location
  @location
end

#partsObject (readonly)

Array[ StringEmbExpr | StringDVar | TStringContent ]

the parts of the

word



13322
13323
13324
# File 'lib/syntax_tree.rb', line 13322

def parts
  @parts
end

Instance Method Details

#child_nodesObject



13340
13341
13342
# File 'lib/syntax_tree.rb', line 13340

def child_nodes
  parts
end

#format(q) ⇒ Object



13344
13345
13346
# File 'lib/syntax_tree.rb', line 13344

def format(q)
  q.format_each(parts)
end

#match?(pattern) ⇒ Boolean

Returns:

  • (Boolean)


13336
13337
13338
# File 'lib/syntax_tree.rb', line 13336

def match?(pattern)
  parts.any? { |part| part.is_a?(TStringContent) && part.match?(pattern) }
end

#pretty_print(q) ⇒ Object



13348
13349
13350
13351
13352
13353
13354
13355
13356
13357
# File 'lib/syntax_tree.rb', line 13348

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

    q.breakable
    q.group(2, "(", ")") { q.seplist(parts) { |part| q.pp(part) } }

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

#to_json(*opts) ⇒ Object



13359
13360
13361
13362
13363
# File 'lib/syntax_tree.rb', line 13359

def to_json(*opts)
  { type: :word, parts: parts, loc: location, cmts: comments }.to_json(
    *opts
  )
end