Class: Prism::CaseMatchNode
- Inherits:
-
PrismNode
- Object
- PrismNode
- Prism::CaseMatchNode
- 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
-
#conditions ⇒ Object
readonly
Represents the conditions of the case match.
-
#else_clause ⇒ Object
readonly
Represents the else clause of the case match.
-
#predicate ⇒ Object
readonly
Represents the predicate of the case match.
Class Method Summary collapse
-
.type ⇒ Object
Return a symbol representation of this node type.
Instance Method Summary collapse
-
#===(other) ⇒ Object
Implements case-equality for the node.
-
#accept(visitor) ⇒ Object
def accept: (Visitor visitor) -> void.
-
#case_keyword ⇒ Object
def case_keyword: () -> String.
-
#case_keyword_loc ⇒ Object
Represents the location of the ‘case` keyword.
-
#child_nodes ⇒ Object
(also: #deconstruct)
def child_nodes: () -> Array.
-
#comment_targets ⇒ Object
def comment_targets: () -> Array[Node | Location].
-
#compact_child_nodes ⇒ Object
def compact_child_nodes: () -> Array.
-
#consequent ⇒ Object
Returns the else clause of the case match node.
-
#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.
- #deconstruct_keys(keys) ⇒ Object
-
#end_keyword ⇒ Object
def end_keyword: () -> String.
-
#end_keyword_loc ⇒ Object
Represents the location of the ‘end` keyword.
-
#initialize(source, node_id, location, flags, predicate, conditions, else_clause, case_keyword_loc, end_keyword_loc) ⇒ CaseMatchNode
constructor
Initialize a new CaseMatchNode node.
-
#inspect ⇒ Object
def inspect -> String.
-
#save_case_keyword_loc(repository) ⇒ Object
Save the case_keyword_loc location using the given saved source so that it can be retrieved later.
-
#save_end_keyword_loc(repository) ⇒ Object
Save the end_keyword_loc location using the given saved source so that it can be retrieved later.
-
#type ⇒ Object
Return a symbol representation of this node type.
Constructor Details
#initialize(source, node_id, location, flags, predicate, conditions, else_clause, case_keyword_loc, end_keyword_loc) ⇒ CaseMatchNode
Initialize a new CaseMatchNode node.
3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 |
# File 'lib/prism/node.rb', line 3570 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
#conditions ⇒ Object (readonly)
Represents the conditions of the case match.
case true; in false; end
^^^^^^^^
3629 3630 3631 |
# File 'lib/prism/node.rb', line 3629 def conditions @conditions end |
#else_clause ⇒ Object (readonly)
Represents the else clause of the case match.
case true; in false; else; end
^^^^
3635 3636 3637 |
# File 'lib/prism/node.rb', line 3635 def else_clause @else_clause end |
#predicate ⇒ Object (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
^^^^
3623 3624 3625 |
# File 'lib/prism/node.rb', line 3623 def predicate @predicate end |
Class Method Details
.type ⇒ Object
Return a symbol representation of this node type. See ‘Node::type`.
3690 3691 3692 |
# File 'lib/prism/node.rb', line 3690 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.
3696 3697 3698 3699 3700 3701 3702 3703 3704 |
# File 'lib/prism/node.rb', line 3696 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
3583 3584 3585 |
# File 'lib/prism/node.rb', line 3583 def accept(visitor) visitor.visit_case_match_node(self) end |
#case_keyword ⇒ Object
def case_keyword: () -> String
3670 3671 3672 |
# File 'lib/prism/node.rb', line 3670 def case_keyword case_keyword_loc.slice end |
#case_keyword_loc ⇒ Object
Represents the location of the ‘case` keyword.
case true; in false; end
^^^^
3641 3642 3643 3644 3645 |
# File 'lib/prism/node.rb', line 3641 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_nodes ⇒ Object Also known as: deconstruct
def child_nodes: () -> Array
3588 3589 3590 |
# File 'lib/prism/node.rb', line 3588 def child_nodes [predicate, *conditions, else_clause] end |
#comment_targets ⇒ Object
def comment_targets: () -> Array[Node | Location]
3602 3603 3604 |
# File 'lib/prism/node.rb', line 3602 def comment_targets [*predicate, *conditions, *else_clause, case_keyword_loc, end_keyword_loc] #: Array[Prism::node | Location] end |
#compact_child_nodes ⇒ Object
def compact_child_nodes: () -> Array
3593 3594 3595 3596 3597 3598 3599 |
# File 'lib/prism/node.rb', line 3593 def compact_child_nodes compact = [] #: Array[Prism::node] compact << predicate if predicate compact.concat(conditions) compact << else_clause if else_clause compact end |
#consequent ⇒ Object
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
3607 3608 3609 |
# File 'lib/prism/node.rb', line 3607 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
3615 3616 3617 |
# File 'lib/prism/node.rb', line 3615 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_keyword ⇒ Object
def end_keyword: () -> String
3675 3676 3677 |
# File 'lib/prism/node.rb', line 3675 def end_keyword end_keyword_loc.slice end |
#end_keyword_loc ⇒ Object
Represents the location of the ‘end` keyword.
case true; in false; end
^^^
3657 3658 3659 3660 3661 |
# File 'lib/prism/node.rb', line 3657 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 |
#inspect ⇒ Object
def inspect -> String
3680 3681 3682 |
# File 'lib/prism/node.rb', line 3680 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.
3649 3650 3651 |
# File 'lib/prism/node.rb', line 3649 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.
3665 3666 3667 |
# File 'lib/prism/node.rb', line 3665 def save_end_keyword_loc(repository) repository.enter(node_id, :end_keyword_loc) end |
#type ⇒ Object
Return a symbol representation of this node type. See ‘Node#type`.
3685 3686 3687 |
# File 'lib/prism/node.rb', line 3685 def type :case_match_node end |