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.



2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
# File 'lib/prism/node.rb', line 2968

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)

attr_reader conditions: Array



3021
3022
3023
# File 'lib/prism/node.rb', line 3021

def conditions
  @conditions
end

#else_clauseObject (readonly)

attr_reader else_clause: ElseNode?



3024
3025
3026
# File 'lib/prism/node.rb', line 3024

def else_clause
  @else_clause
end

#predicateObject (readonly)

attr_reader predicate: Prism::node?



3018
3019
3020
# File 'lib/prism/node.rb', line 3018

def predicate
  @predicate
end

Class Method Details

.typeObject

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



3061
3062
3063
# File 'lib/prism/node.rb', line 3061

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.



3067
3068
3069
3070
3071
3072
3073
3074
3075
# File 'lib/prism/node.rb', line 3067

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



2981
2982
2983
# File 'lib/prism/node.rb', line 2981

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

#case_keywordObject

def case_keyword: () -> String



3041
3042
3043
# File 'lib/prism/node.rb', line 3041

def case_keyword
  case_keyword_loc.slice
end

#case_keyword_locObject

attr_reader case_keyword_loc: Location



3027
3028
3029
3030
3031
# File 'lib/prism/node.rb', line 3027

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[nil | Node]



2986
2987
2988
# File 'lib/prism/node.rb', line 2986

def child_nodes
  [predicate, *conditions, else_clause]
end

#comment_targetsObject

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



3000
3001
3002
# File 'lib/prism/node.rb', line 3000

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



2991
2992
2993
2994
2995
2996
2997
# File 'lib/prism/node.rb', line 2991

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.



467
468
469
470
# File 'lib/prism/node_ext.rb', line 467

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



3005
3006
3007
# File 'lib/prism/node.rb', line 3005

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 }



3013
3014
3015
# File 'lib/prism/node.rb', line 3013

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

#end_keywordObject

def end_keyword: () -> String



3046
3047
3048
# File 'lib/prism/node.rb', line 3046

def end_keyword
  end_keyword_loc.slice
end

#end_keyword_locObject

attr_reader end_keyword_loc: Location



3034
3035
3036
3037
3038
# File 'lib/prism/node.rb', line 3034

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



3051
3052
3053
# File 'lib/prism/node.rb', line 3051

def inspect
  InspectVisitor.compose(self)
end

#typeObject

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



3056
3057
3058
# File 'lib/prism/node.rb', line 3056

def type
  :case_match_node
end