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.



3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
# File 'lib/prism/node.rb', line 3538

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
           ^^^^^^^^


3597
3598
3599
# File 'lib/prism/node.rb', line 3597

def conditions
  @conditions
end

#else_clauseObject (readonly)

Represents the else clause of the case match.

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


3603
3604
3605
# File 'lib/prism/node.rb', line 3603

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
^^^^


3591
3592
3593
# File 'lib/prism/node.rb', line 3591

def predicate
  @predicate
end

Class Method Details

.typeObject

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



3658
3659
3660
# File 'lib/prism/node.rb', line 3658

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.



3664
3665
3666
3667
3668
3669
3670
3671
3672
# File 'lib/prism/node.rb', line 3664

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



3551
3552
3553
# File 'lib/prism/node.rb', line 3551

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

#case_keywordObject

def case_keyword: () -> String



3638
3639
3640
# File 'lib/prism/node.rb', line 3638

def case_keyword
  case_keyword_loc.slice
end

#case_keyword_locObject

Represents the location of the ‘case` keyword.

case true; in false; end
^^^^


3609
3610
3611
3612
3613
# File 'lib/prism/node.rb', line 3609

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



3556
3557
3558
# File 'lib/prism/node.rb', line 3556

def child_nodes
  [predicate, *conditions, else_clause]
end

#comment_targetsObject

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



3570
3571
3572
# File 'lib/prism/node.rb', line 3570

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



3561
3562
3563
3564
3565
3566
3567
# File 'lib/prism/node.rb', line 3561

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



3575
3576
3577
# File 'lib/prism/node.rb', line 3575

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 }



3583
3584
3585
# File 'lib/prism/node.rb', line 3583

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



3643
3644
3645
# File 'lib/prism/node.rb', line 3643

def end_keyword
  end_keyword_loc.slice
end

#end_keyword_locObject

Represents the location of the ‘end` keyword.

case true; in false; end
                     ^^^


3625
3626
3627
3628
3629
# File 'lib/prism/node.rb', line 3625

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



3648
3649
3650
# File 'lib/prism/node.rb', line 3648

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.



3617
3618
3619
# File 'lib/prism/node.rb', line 3617

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.



3633
3634
3635
# File 'lib/prism/node.rb', line 3633

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`.



3653
3654
3655
# File 'lib/prism/node.rb', line 3653

def type
  :case_match_node
end