Class: Prism::CaseMatchNode
- Inherits:
-
PrismNode
- Object
- PrismNode
- Prism::CaseMatchNode
- Defined in:
- lib/prism/node.rb,
sig/generated/prism/node.rbs,
ext/prism/api_node.c
Overview
Represents the use of a case statement for pattern matching.
case true
in false
end
^^^^^^^^^
Class Method Summary collapse
-
.type ⇒ :case_match_node
See
Node.type.
Instance Method Summary collapse
-
#===(other) ⇒ boolish
: (untyped other) -> boolish.
-
#accept(visitor) ⇒ Object
See Node.accept.
-
#case_keyword ⇒ String
:call-seq: case_keyword -> String.
-
#case_keyword_loc ⇒ Location
:category: Locations :call-seq: case_keyword_loc -> Location.
-
#child_nodes ⇒ Array[node?]
(also: #deconstruct)
See Node.child_nodes.
-
#compact_child_nodes ⇒ Array[node]
See Node.compact_child_nodes.
-
#conditions ⇒ Array[InNode]
:call-seq: conditions -> Array.
-
#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
:call-seq: copy(**fields) -> CaseMatchNode.
-
#deconstruct_keys(keys) ⇒ Hash[Symbol, untyped]
: (Array? keys) -> Hash[Symbol, untyped].
-
#each_child_node {|arg0| ... } ⇒ Object
See Node.each_child_node.
-
#else_clause ⇒ ElseNode?
:call-seq: else_clause -> ElseNode | nil.
-
#end_keyword ⇒ String
:call-seq: end_keyword -> String.
-
#end_keyword_loc ⇒ Location
:category: Locations :call-seq: end_keyword_loc -> Location.
-
#initialize(source, node_id, location, flags, predicate, conditions, else_clause, case_keyword_loc, end_keyword_loc) ⇒ CaseMatchNode
constructor
Initialize a new CaseMatchNode node.
-
#inspect ⇒ String
: () -> String.
-
#predicate ⇒ Prism::node?
:call-seq: predicate -> Node | nil.
-
#save_case_keyword_loc(repository) ⇒ Relocation::Entry
:category: Repository Save the case_keyword_loc location using the given saved source so that it can be retrieved later.
-
#save_end_keyword_loc(repository) ⇒ Relocation::Entry
:category: Repository Save the end_keyword_loc location using the given saved source so that it can be retrieved later.
-
#type ⇒ :case_match_node
See
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.
: (Source source, Integer node_id, Location location, Integer flags, Prism::node? predicate, Array conditions, ElseNode? else_clause, Location case_keyword_loc, Location end_keyword_loc) -> void
6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 |
# File 'lib/prism/node.rb', line 6328 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 |
Class Method Details
.type ⇒ :case_match_node
See Node.type.
: () -> :case_match_node
6429 6430 6431 |
# File 'lib/prism/node.rb', line 6429 def self.type :case_match_node end |
Instance Method Details
#===(other) ⇒ boolish
: (untyped other) -> boolish
6574 6575 6576 6577 6578 6579 6580 6581 6582 |
# File 'lib/prism/node.rb', line 6574 def ===(other) # :nodoc: 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
See Node.accept.
: (_Visitor visitor) -> untyped
6354 6355 6356 |
# File 'lib/prism/node.rb', line 6354 def accept(visitor) visitor.visit_case_match_node(self) end |
#case_keyword ⇒ String
:call-seq:
case_keyword -> String
Slice the location of case_keyword_loc from the source.
: () -> String
6557 6558 6559 |
# File 'lib/prism/node.rb', line 6557 def case_keyword case_keyword_loc.slice end |
#case_keyword_loc ⇒ Location
:category: Locations :call-seq:
case_keyword_loc -> Location
Represents the Location of the case keyword.
case true; in false; end
^^^^
: () -> Location
6505 6506 6507 6508 6509 |
# File 'lib/prism/node.rb', line 6505 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 ⇒ Array[node?] Also known as: deconstruct
See Node.child_nodes.
: () -> Array
6361 6362 6363 |
# File 'lib/prism/node.rb', line 6361 def child_nodes [predicate, *conditions, else_clause] end |
#compact_child_nodes ⇒ Array[node]
See Node.compact_child_nodes.
: () -> Array
6380 6381 6382 6383 6384 6385 6386 |
# File 'lib/prism/node.rb', line 6380 def compact_child_nodes compact = [] #: Array[Prism::node] if (predicate = self.predicate); compact << predicate; end compact.concat(conditions) if (else_clause = self.else_clause); compact << else_clause; end compact end |
#conditions ⇒ Array[InNode]
:call-seq:
conditions -> Array[InNode]
Represents the conditions of the case match.
case true; in false; end
^^^^^^^^
: () -> Array
6470 6471 6472 |
# File 'lib/prism/node.rb', line 6470 def conditions @conditions 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) ⇒ CaseMatchNode
:call-seq:
copy(**fields) -> CaseMatchNode
Creates a copy of self with the given fields, using self as the template.
: (?node_id: Integer, ?location: Location, ?flags: Integer, ?predicate: Prism::node?, ?conditions: Array, ?else_clause: ElseNode?, ?case_keyword_loc: Location, ?end_keyword_loc: Location) -> CaseMatchNode
6394 6395 6396 |
# File 'lib/prism/node.rb', line 6394 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) ⇒ Hash[Symbol, untyped]
: (Array? keys) -> Hash[Symbol, untyped]
6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 |
# File 'lib/prism/node.rb', line 6401 def deconstruct_keys(keys) # :nodoc: (keys || %i[node_id location predicate conditions else_clause case_keyword_loc end_keyword_loc case_keyword end_keyword]).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 :predicate then deconstructed[:predicate] = self.predicate when :conditions then deconstructed[:conditions] = self.conditions when :else_clause then deconstructed[:else_clause] = self.else_clause when :case_keyword_loc then deconstructed[:case_keyword_loc] = self.case_keyword_loc when :end_keyword_loc then deconstructed[:end_keyword_loc] = self.end_keyword_loc when :case_keyword then deconstructed[:case_keyword] = self.case_keyword when :end_keyword then deconstructed[:end_keyword] = self.end_keyword end end end |
#each_child_node ⇒ Enumerator[node, void] #each_child_node ⇒ void
See Node.each_child_node.
: () -> Enumerator[node, void] : () { (node) -> void } -> void
6369 6370 6371 6372 6373 6374 6375 |
# File 'lib/prism/node.rb', line 6369 def each_child_node(&blk) return to_enum(:each_child_node) unless block_given? if (predicate = self.predicate); yield predicate; end conditions.each { |node| yield node } if (else_clause = self.else_clause); yield else_clause; end end |
#else_clause ⇒ ElseNode?
:call-seq:
else_clause -> ElseNode | nil
Represents the else clause of the case match.
case true; in false; else; end
^^^^^^^^^
: () -> ElseNode?
6487 6488 6489 |
# File 'lib/prism/node.rb', line 6487 def else_clause @else_clause end |
#end_keyword ⇒ String
:call-seq:
end_keyword -> String
Slice the location of end_keyword_loc from the source.
: () -> String
6567 6568 6569 |
# File 'lib/prism/node.rb', line 6567 def end_keyword end_keyword_loc.slice end |
#end_keyword_loc ⇒ Location
:category: Locations :call-seq:
end_keyword_loc -> Location
Represents the Location of the end keyword.
case true; in false; end
^^^
: () -> Location
6534 6535 6536 6537 6538 |
# File 'lib/prism/node.rb', line 6534 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 ⇒ String
: () -> String
6434 6435 6436 |
# File 'lib/prism/node.rb', line 6434 def inspect # :nodoc: InspectVisitor.compose(self) end |
#predicate ⇒ Prism::node?
:call-seq:
predicate -> Node | nil
Represents the predicate of the case match. This can be either nil or any non-void expressions.
case true; in false; end
^^^^
: () -> Prism::node?
6453 6454 6455 |
# File 'lib/prism/node.rb', line 6453 def predicate @predicate end |
#save_case_keyword_loc(repository) ⇒ Relocation::Entry
:category: Repository Save the case_keyword_loc location using the given saved source so that it can be retrieved later.
: (_Repository repository) -> Relocation::Entry
6516 6517 6518 |
# File 'lib/prism/node.rb', line 6516 def save_case_keyword_loc(repository) repository.enter(node_id, :case_keyword_loc) end |
#save_end_keyword_loc(repository) ⇒ Relocation::Entry
:category: Repository Save the end_keyword_loc location using the given saved source so that it can be retrieved later.
: (_Repository repository) -> Relocation::Entry
6545 6546 6547 |
# File 'lib/prism/node.rb', line 6545 def save_end_keyword_loc(repository) repository.enter(node_id, :end_keyword_loc) end |
#type ⇒ :case_match_node
See Node#type.
: () -> :case_match_node
6422 6423 6424 |
# File 'lib/prism/node.rb', line 6422 def type :case_match_node end |