Class: Prism::ArrayNode

Inherits:
PrismNode
  • Object
show all
Defined in:
lib/prism/node.rb,
ext/prism/api_node.c

Overview

Represents an array literal. This can be a regular array using brackets or a special array using % like %w or %i.

[1, 2, 3]
^^^^^^^^^

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, node_id, location, flags, elements, opening_loc, closing_loc) ⇒ ArrayNode

Initialize a new ArrayNode node.



748
749
750
751
752
753
754
755
756
# File 'lib/prism/node.rb', line 748

def initialize(source, node_id, location, flags, elements, opening_loc, closing_loc)
  @source = source
  @node_id = node_id
  @location = location
  @flags = flags
  @elements = elements
  @opening_loc = opening_loc
  @closing_loc = closing_loc
end

Instance Attribute Details

#elementsObject (readonly)

Represent the list of zero or more [non-void expressions](github.com/ruby/prism/blob/main/docs/parsing_rules.md#non-void-expression) within the array.



797
798
799
# File 'lib/prism/node.rb', line 797

def elements
  @elements
end

Class Method Details

.typeObject

Return a symbol representation of this node type. See ‘Node::type`.



856
857
858
# File 'lib/prism/node.rb', line 856

def self.type
  :array_node
end

Instance Method Details

#===(other) ⇒ Object

Implements case-equality for the node. This is effectively == but without comparing the value of locations. Locations are checked only for presence.



862
863
864
865
866
867
868
869
# File 'lib/prism/node.rb', line 862

def ===(other)
  other.is_a?(ArrayNode) &&
    (flags === other.flags) &&
    (elements.length == other.elements.length) &&
    elements.zip(other.elements).all? { |left, right| left === right } &&
    (opening_loc.nil? == other.opening_loc.nil?) &&
    (closing_loc.nil? == other.closing_loc.nil?)
end

#accept(visitor) ⇒ Object

def accept: (Visitor visitor) -> void



759
760
761
# File 'lib/prism/node.rb', line 759

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

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array[nil | Node]



764
765
766
# File 'lib/prism/node.rb', line 764

def child_nodes
  [*elements]
end

#closingObject

def closing: () -> String?



841
842
843
# File 'lib/prism/node.rb', line 841

def closing
  closing_loc&.slice
end

#closing_locObject

Represents the optional source location for the closing token.

[1,2,3]                 # "]"
%w[foo bar baz]         # "]"
%I(apple orange banana) # ")"
foo = 1, 2, 3           # nil


823
824
825
826
827
828
829
830
831
832
833
# File 'lib/prism/node.rb', line 823

def closing_loc
  location = @closing_loc
  case location
  when nil
    nil
  when Location
    location
  else
    @closing_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
  end
end

#comment_targetsObject

def comment_targets: () -> Array[Node | Location]



774
775
776
# File 'lib/prism/node.rb', line 774

def comment_targets
  [*elements, *opening_loc, *closing_loc] #: Array[Prism::node | Location]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



769
770
771
# File 'lib/prism/node.rb', line 769

def compact_child_nodes
  [*elements]
end

#contains_splat?Boolean

def contains_splat?: () -> bool

Returns:

  • (Boolean)


792
793
794
# File 'lib/prism/node.rb', line 792

def contains_splat?
  flags.anybits?(ArrayNodeFlags::CONTAINS_SPLAT)
end

#copy(node_id: self.node_id, location: self.location, flags: self.flags, elements: self.elements, opening_loc: self.opening_loc, closing_loc: self.closing_loc) ⇒ Object

def copy: (?node_id: Integer, ?location: Location, ?flags: Integer, ?elements: Array, ?opening_loc: Location?, ?closing_loc: Location?) -> ArrayNode



779
780
781
# File 'lib/prism/node.rb', line 779

def copy(node_id: self.node_id, location: self.location, flags: self.flags, elements: self.elements, opening_loc: self.opening_loc, closing_loc: self.closing_loc)
  ArrayNode.new(source, node_id, location, flags, elements, opening_loc, closing_loc)
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (Array keys) -> { node_id: Integer, location: Location, elements: Array, opening_loc: Location?, closing_loc: Location? }



787
788
789
# File 'lib/prism/node.rb', line 787

def deconstruct_keys(keys)
  { node_id: node_id, location: location, elements: elements, opening_loc: opening_loc, closing_loc: closing_loc }
end

#inspectObject

def inspect -> String



846
847
848
# File 'lib/prism/node.rb', line 846

def inspect
  InspectVisitor.compose(self)
end

#openingObject

def opening: () -> String?



836
837
838
# File 'lib/prism/node.rb', line 836

def opening
  opening_loc&.slice
end

#opening_locObject

Represents the optional source location for the opening token.

[1,2,3]                 # "["
%w[foo bar baz]         # "%w["
%I(apple orange banana) # "%I("
foo = 1, 2, 3           # nil


805
806
807
808
809
810
811
812
813
814
815
# File 'lib/prism/node.rb', line 805

def opening_loc
  location = @opening_loc
  case location
  when nil
    nil
  when Location
    location
  else
    @opening_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
  end
end

#typeObject

Return a symbol representation of this node type. See ‘Node#type`.



851
852
853
# File 'lib/prism/node.rb', line 851

def type
  :array_node
end