Class: SyntaxTree::Symbols

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

Overview

Symbols represents a symbol array literal with interpolation.

I[one two three]

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(beginning:, elements:, location:) ⇒ Symbols

Returns a new instance of Symbols.



10646
10647
10648
10649
10650
10651
# File 'lib/syntax_tree/node.rb', line 10646

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

Instance Attribute Details

#beginningObject (readonly)

SymbolsBeg

the token that opens this array literal



10638
10639
10640
# File 'lib/syntax_tree/node.rb', line 10638

def beginning
  @beginning
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



10644
10645
10646
# File 'lib/syntax_tree/node.rb', line 10644

def comments
  @comments
end

#elementsObject (readonly)

Array[ Word ]

the words in the symbol array literal



10641
10642
10643
# File 'lib/syntax_tree/node.rb', line 10641

def elements
  @elements
end

Instance Method Details

#===(other) ⇒ Object



10702
10703
10704
10705
# File 'lib/syntax_tree/node.rb', line 10702

def ===(other)
  other.is_a?(Symbols) && beginning === other.beginning &&
    ArrayMatch.call(elements, other.elements)
end

#accept(visitor) ⇒ Object



10653
10654
10655
# File 'lib/syntax_tree/node.rb', line 10653

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

#child_nodesObject Also known as: deconstruct



10657
10658
10659
# File 'lib/syntax_tree/node.rb', line 10657

def child_nodes
  []
end

#copy(beginning: nil, elements: nil, location: nil) ⇒ Object



10661
10662
10663
10664
10665
10666
10667
# File 'lib/syntax_tree/node.rb', line 10661

def copy(beginning: nil, elements: nil, location: nil)
  Symbols.new(
    beginning: beginning || self.beginning,
    elements: elements || self.elements,
    location: location || self.location
  )
end

#deconstruct_keys(_keys) ⇒ Object



10671
10672
10673
10674
10675
10676
10677
10678
# File 'lib/syntax_tree/node.rb', line 10671

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

#format(q) ⇒ Object



10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
# File 'lib/syntax_tree/node.rb', line 10680

def format(q)
  opening, closing = "%I[", "]"

  if elements.any? { |element| element.match?(/[\[\]]/) }
    opening = beginning.value
    closing = Quotes.matching(opening[2])
  end

  q.text(opening)
  q.group do
    q.indent do
      q.breakable_empty
      q.seplist(
        elements,
        ArrayLiteral::BREAKABLE_SPACE_SEPARATOR
      ) { |element| q.format(element) }
    end
    q.breakable_empty
  end
  q.text(closing)
end