Class: Prism::CaseMatchNode

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

Overview

Represents the use of a case statement for pattern matching.

case true
in false
end
^^^^^^^^^

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, node_id, location, flags, predicate, conditions, else_clause, case_keyword_loc, end_keyword_loc) ⇒ CaseMatchNode

Initialize a new CaseMatchNode node.



3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
# File 'lib/prism/node.rb', line 3762

def initialize(source, node_id, location, flags, predicate, conditions, else_clause, case_keyword_loc, end_keyword_loc)
  @source = source
  @node_id = node_id
  @location = location
  @flags = flags
  @predicate = predicate
  @conditions = conditions
  @else_clause = else_clause
  @case_keyword_loc = case_keyword_loc
  @end_keyword_loc = end_keyword_loc
end

Instance Attribute Details

#conditionsObject (readonly)

Represents the conditions of the case match.

case true; in false; end
           ^^^^^^^^


3830
3831
3832
# File 'lib/prism/node.rb', line 3830

def conditions
  @conditions
end

#else_clauseObject (readonly)

Represents the else clause of the case match.

case true; in false; else; end
                     ^^^^


3836
3837
3838
# File 'lib/prism/node.rb', line 3836

def else_clause
  @else_clause
end

#predicateObject (readonly)

Represents the predicate of the case match. This can be either nil or any [non-void expressions](github.com/ruby/prism/blob/main/docs/parsing_rules.md#non-void-expression).

case true; in false; end
^^^^


3824
3825
3826
# File 'lib/prism/node.rb', line 3824

def predicate
  @predicate
end

Class Method Details

.typeObject

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



3891
3892
3893
# File 'lib/prism/node.rb', line 3891

def self.type
  :case_match_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.



3897
3898
3899
3900
3901
3902
3903
3904
3905
# File 'lib/prism/node.rb', line 3897

def ===(other)
  other.is_a?(CaseMatchNode) &&
    (predicate === other.predicate) &&
    (conditions.length == other.conditions.length) &&
    conditions.zip(other.conditions).all? { |left, right| left === right } &&
    (else_clause === other.else_clause) &&
    (case_keyword_loc.nil? == other.case_keyword_loc.nil?) &&
    (end_keyword_loc.nil? == other.end_keyword_loc.nil?)
end

#accept(visitor) ⇒ Object

def accept: (Visitor visitor) -> void



3775
3776
3777
# File 'lib/prism/node.rb', line 3775

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

#case_keywordObject

def case_keyword: () -> String



3871
3872
3873
# File 'lib/prism/node.rb', line 3871

def case_keyword
  case_keyword_loc.slice
end

#case_keyword_locObject

Represents the location of the case keyword.

case true; in false; end
^^^^


3842
3843
3844
3845
3846
# File 'lib/prism/node.rb', line 3842

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

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array



3780
3781
3782
# File 'lib/prism/node.rb', line 3780

def child_nodes
  [predicate, *conditions, else_clause]
end

#comment_targetsObject

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



3803
3804
3805
# File 'lib/prism/node.rb', line 3803

def comment_targets
  [*predicate, *conditions, *else_clause, case_keyword_loc, end_keyword_loc] #: Array[Prism::node | Location]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



3794
3795
3796
3797
3798
3799
3800
# File 'lib/prism/node.rb', line 3794

def compact_child_nodes
  compact = [] #: Array[Prism::node]
  compact << predicate if predicate
  compact.concat(conditions)
  compact << else_clause if else_clause
  compact
end

#consequentObject

Returns the else clause of the case match node. This method is deprecated in favor of #else_clause.



470
471
472
473
# File 'lib/prism/node_ext.rb', line 470

def consequent
  deprecated("else_clause")
  else_clause
end

#copy(node_id: self.node_id, location: self.location, flags: self.flags, predicate: self.predicate, conditions: self.conditions, else_clause: self.else_clause, case_keyword_loc: self.case_keyword_loc, end_keyword_loc: self.end_keyword_loc) ⇒ Object

def copy: (?node_id: Integer, ?location: Location, ?flags: Integer, ?predicate: Prism::node?, ?conditions: Array, ?else_clause: ElseNode?, ?case_keyword_loc: Location, ?end_keyword_loc: Location) -> CaseMatchNode



3808
3809
3810
# File 'lib/prism/node.rb', line 3808

def copy(node_id: self.node_id, location: self.location, flags: self.flags, predicate: self.predicate, conditions: self.conditions, else_clause: self.else_clause, case_keyword_loc: self.case_keyword_loc, end_keyword_loc: self.end_keyword_loc)
  CaseMatchNode.new(source, node_id, location, flags, predicate, conditions, else_clause, case_keyword_loc, end_keyword_loc)
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (Array keys) -> { node_id: Integer, location: Location, predicate: Prism::node?, conditions: Array, else_clause: ElseNode?, case_keyword_loc: Location, end_keyword_loc: Location }



3816
3817
3818
# File 'lib/prism/node.rb', line 3816

def deconstruct_keys(keys)
  { node_id: node_id, location: location, predicate: predicate, conditions: conditions, else_clause: else_clause, case_keyword_loc: case_keyword_loc, end_keyword_loc: end_keyword_loc }
end

#each_child_node {|predicate| ... } ⇒ Object

def each_child_node: () { (Prism::node) -> void } -> void | () -> Enumerator

Yields:



3785
3786
3787
3788
3789
3790
3791
# File 'lib/prism/node.rb', line 3785

def each_child_node
  return to_enum(:each_child_node) unless block_given?

  yield predicate if predicate
  conditions.each { |node| yield node }
  yield else_clause if else_clause
end

#end_keywordObject

def end_keyword: () -> String



3876
3877
3878
# File 'lib/prism/node.rb', line 3876

def end_keyword
  end_keyword_loc.slice
end

#end_keyword_locObject

Represents the location of the end keyword.

case true; in false; end
                     ^^^


3858
3859
3860
3861
3862
# File 'lib/prism/node.rb', line 3858

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

#inspectObject

def inspect -> String



3881
3882
3883
# File 'lib/prism/node.rb', line 3881

def inspect
  InspectVisitor.compose(self)
end

#save_case_keyword_loc(repository) ⇒ Object

Save the case_keyword_loc location using the given saved source so that it can be retrieved later.



3850
3851
3852
# File 'lib/prism/node.rb', line 3850

def save_case_keyword_loc(repository)
  repository.enter(node_id, :case_keyword_loc)
end

#save_end_keyword_loc(repository) ⇒ Object

Save the end_keyword_loc location using the given saved source so that it can be retrieved later.



3866
3867
3868
# File 'lib/prism/node.rb', line 3866

def save_end_keyword_loc(repository)
  repository.enter(node_id, :end_keyword_loc)
end

#typeObject

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



3886
3887
3888
# File 'lib/prism/node.rb', line 3886

def type
  :case_match_node
end