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.



13534
13535
13536
13537
13538
# File 'lib/syntax_tree.rb', line 13534

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



13532
13533
13534
# File 'lib/syntax_tree.rb', line 13532

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



13529
13530
13531
# File 'lib/syntax_tree.rb', line 13529

def location
  @location
end

#partsObject (readonly)

Array[ StringEmbExpr | StringDVar | TStringContent ]

the parts of the

word



13526
13527
13528
# File 'lib/syntax_tree.rb', line 13526

def parts
  @parts
end

Instance Method Details

#child_nodesObject



13544
13545
13546
# File 'lib/syntax_tree.rb', line 13544

def child_nodes
  parts
end

#format(q) ⇒ Object



13548
13549
13550
# File 'lib/syntax_tree.rb', line 13548

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

#match?(pattern) ⇒ Boolean

Returns:

  • (Boolean)


13540
13541
13542
# File 'lib/syntax_tree.rb', line 13540

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

#pretty_print(q) ⇒ Object



13552
13553
13554
13555
13556
13557
13558
13559
13560
13561
# File 'lib/syntax_tree.rb', line 13552

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



13563
13564
13565
13566
13567
# File 'lib/syntax_tree.rb', line 13563

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