Class: SyntaxTree::Word

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.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

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

#initialize(parts:, location:) ⇒ Word

Returns a new instance of Word.



12012
12013
12014
12015
12016
# File 'lib/syntax_tree/node.rb', line 12012

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



12010
12011
12012
# File 'lib/syntax_tree/node.rb', line 12010

def comments
  @comments
end

#partsObject (readonly)

Array[ StringEmbExpr | StringDVar | TStringContent ]

the parts of the

word



12007
12008
12009
# File 'lib/syntax_tree/node.rb', line 12007

def parts
  @parts
end

Instance Method Details

#===(other) ⇒ Object



12051
12052
12053
# File 'lib/syntax_tree/node.rb', line 12051

def ===(other)
  other.is_a?(Word) && ArrayMatch.call(parts, other.parts)
end

#accept(visitor) ⇒ Object



12022
12023
12024
# File 'lib/syntax_tree/node.rb', line 12022

def accept(visitor)
  visitor.visit_word(self)
end

#child_nodesObject Also known as: deconstruct



12026
12027
12028
# File 'lib/syntax_tree/node.rb', line 12026

def child_nodes
  parts
end

#copy(parts: nil, location: nil) ⇒ Object



12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
# File 'lib/syntax_tree/node.rb', line 12030

def copy(parts: nil, location: nil)
  node =
    Word.new(
      parts: parts || self.parts,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



12043
12044
12045
# File 'lib/syntax_tree/node.rb', line 12043

def deconstruct_keys(_keys)
  { parts: parts, location: location, comments: comments }
end

#format(q) ⇒ Object



12047
12048
12049
# File 'lib/syntax_tree/node.rb', line 12047

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

#match?(pattern) ⇒ Boolean

Returns:

  • (Boolean)


12018
12019
12020
# File 'lib/syntax_tree/node.rb', line 12018

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