Class: Prism::AlternationPatternNode

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

Overview

Represents an alternation pattern in pattern matching.

foo => bar | baz
       ^^^^^^^^^

Explore code sample

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, node_id, location, flags, left, right, operator_loc) ⇒ AlternationPatternNode

Initialize a new AlternationPatternNode node.

: (Source source, Integer node_id, Location location, Integer flags, Prism::node left, Prism::node right, Location operator_loc) -> void

Parameters:

  • source (Source)
  • node_id (Integer)
  • location (Location)
  • flags (Integer)
  • left (Prism::node)
  • right (Prism::node)
  • operator_loc (Location)


853
854
855
856
857
858
859
860
861
# File 'lib/prism/node.rb', line 853

def initialize(source, node_id, location, flags, left, right, operator_loc)
  @source = source
  @node_id = node_id
  @location = location
  @flags = flags
  @left = left
  @right = right
  @operator_loc = operator_loc
end

Class Method Details

.type ⇒ :alternation_pattern_node

See Node.type.

: () -> :alternation_pattern_node

Returns:

  • (:alternation_pattern_node)


944
945
946
# File 'lib/prism/node.rb', line 944

def self.type
  :alternation_pattern_node
end

Instance Method Details

#===(other) ⇒ boolish

: (untyped other) -> boolish

Parameters:

  • other (Object)

Returns:

  • (boolish)


1033
1034
1035
1036
1037
1038
# File 'lib/prism/node.rb', line 1033

def ===(other) # :nodoc:
  other.is_a?(AlternationPatternNode) &&
    (left === other.left) &&
    (right === other.right) &&
    (operator_loc.nil? == other.operator_loc.nil?)
end

#accept(visitor) ⇒ Object

See Node.accept.

: (_Visitor visitor) -> untyped

Parameters:

Returns:

  • (Object)


877
878
879
# File 'lib/prism/node.rb', line 877

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

#child_nodes ⇒ Array[node?] Also known as: deconstruct

See Node.child_nodes.

: () -> Array

Returns:

  • (Array[node?])


884
885
886
# File 'lib/prism/node.rb', line 884

def child_nodes
  [left, right]
end

#compact_child_nodes ⇒ Array[node]

See Node.compact_child_nodes.

: () -> Array

Returns:

  • (Array[node])


902
903
904
# File 'lib/prism/node.rb', line 902

def compact_child_nodes
  [left, right]
end

#copy(node_id: self.node_id, location: self.location, flags: self.flags, left: self.left, right: self.right, operator_loc: self.operator_loc) ⇒ AlternationPatternNode

:call-seq:

copy(**fields) -> AlternationPatternNode

Creates a copy of self with the given fields, using self as the template.

: (?node_id: Integer, ?location: Location, ?flags: Integer, ?left: Prism::node, ?right: Prism::node, ?operator_loc: Location) -> AlternationPatternNode

Parameters:

  • node_id: (Integer) (defaults to: self.node_id)
  • location: (Location) (defaults to: self.location)
  • flags: (Integer) (defaults to: self.flags)
  • left: (Prism::node) (defaults to: self.left)
  • right: (Prism::node) (defaults to: self.right)
  • operator_loc: (Location) (defaults to: self.operator_loc)

Returns:



912
913
914
# File 'lib/prism/node.rb', line 912

def copy(node_id: self.node_id, location: self.location, flags: self.flags, left: self.left, right: self.right, operator_loc: self.operator_loc)
  AlternationPatternNode.new(source, node_id, location, flags, left, right, operator_loc)
end

#deconstruct_keys(keys) ⇒ Hash[Symbol, untyped]

: (Array? keys) -> Hash[Symbol, untyped]

Parameters:

  • keys (Array[Symbol], nil)

Returns:

  • (Hash[Symbol, untyped])


919
920
921
922
923
924
925
926
927
928
929
930
931
932
# File 'lib/prism/node.rb', line 919

def deconstruct_keys(keys) # :nodoc:
  (keys || %i[node_id location left right operator_loc operator]).each_with_object(
    {} #: Hash[Symbol, untyped]
  ) do |key, deconstructed|
    case key
    when :node_id                 then deconstructed[:node_id] = self.node_id
    when :location                then deconstructed[:location] = self.location
    when :left                    then deconstructed[:left] = self.left
    when :right                   then deconstructed[:right] = self.right
    when :operator_loc            then deconstructed[:operator_loc] = self.operator_loc
    when :operator                then deconstructed[:operator] = self.operator
    end
  end
end

#each_child_node ⇒ Enumerator[node, void] #each_child_node ⇒ void

See Node.each_child_node.

: () -> Enumerator[node, void] : () { (node) -> void } -> void

Overloads:

  • #each_child_node ⇒ Enumerator[node, void]

    Returns:

    • (Enumerator[node, void])
  • #each_child_node ⇒ void

    This method returns an undefined value.

Yields:

Yield Parameters:

  • arg0 (node)

Yield Returns:

  • (void)


892
893
894
895
896
897
# File 'lib/prism/node.rb', line 892

def each_child_node(&blk)
  return to_enum(:each_child_node) unless block_given?

  yield left
  yield right
end

#inspect ⇒ String

: () -> String

Returns:

  • (String)


949
950
951
# File 'lib/prism/node.rb', line 949

def inspect # :nodoc:
  InspectVisitor.compose(self)
end

#left ⇒ Prism::node

:call-seq:

left -> Node

Represents the left side of the expression.

foo => bar | baz
       ^^^

Explore code sample

: () -> Prism::node

Returns:

  • (Prism::node)


968
969
970
# File 'lib/prism/node.rb', line 968

def left
  @left
end

#operator ⇒ String

:call-seq:

operator -> String

Slice the location of operator_loc from the source.

: () -> String

Returns:

  • (String)


1026
1027
1028
# File 'lib/prism/node.rb', line 1026

def operator
  operator_loc.slice
end

#operator_loc ⇒ Location

:category: Locations :call-seq:

operator_loc -> Location

Represents the alternation operator Location.

foo => bar | baz
           ^

Explore code sample

: () -> Location

Returns:



1003
1004
1005
1006
1007
# File 'lib/prism/node.rb', line 1003

def operator_loc
  location = @operator_loc
  return location if location.is_a?(Location)
  @operator_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
end

#right ⇒ Prism::node

:call-seq:

right -> Node

Represents the right side of the expression.

foo => bar | baz
             ^^^

Explore code sample

: () -> Prism::node

Returns:

  • (Prism::node)


985
986
987
# File 'lib/prism/node.rb', line 985

def right
  @right
end

#save_operator_loc(repository) ⇒ Relocation::Entry

:category: Repository Save the operator_loc location using the given saved source so that it can be retrieved later.

: (_Repository repository) -> Relocation::Entry

Parameters:

Returns:



1014
1015
1016
# File 'lib/prism/node.rb', line 1014

def save_operator_loc(repository)
  repository.enter(node_id, :operator_loc)
end

#type ⇒ :alternation_pattern_node

See Node#type.

: () -> :alternation_pattern_node

Returns:

  • (:alternation_pattern_node)


937
938
939
# File 'lib/prism/node.rb', line 937

def type
  :alternation_pattern_node
end