Class: Prism::CaseMatchNode

Inherits:
PrismNode
  • Object
show all
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
^^^^^^^^^

Explore code sample

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.

: (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

Parameters:



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

Returns:

  • (: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

Parameters:

  • other (Object)

Returns:

  • (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

Parameters:

Returns:

  • (Object)


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

Returns:

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

Explore code sample

: () -> Location

Returns:



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

Returns:

  • (Array[node?])


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

Returns:

  • (Array[node])


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

Explore code sample

: () -> Array

Returns:



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

Parameters:

  • node_id: (Integer) (defaults to: self.node_id)
  • location: (Location) (defaults to: self.location)
  • flags: (Integer) (defaults to: self.flags)
  • predicate: (Prism::node, nil) (defaults to: self.predicate)
  • conditions: (Array[InNode]) (defaults to: self.conditions)
  • else_clause: (ElseNode, nil) (defaults to: self.else_clause)
  • case_keyword_loc: (Location) (defaults to: self.case_keyword_loc)
  • end_keyword_loc: (Location) (defaults to: self.end_keyword_loc)

Returns:



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]

Parameters:

  • keys (Array[Symbol], nil)

Returns:

  • (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

Overloads:

  • #each_child_node ⇒ Enumerator[node, void]

    Returns:

    • (Enumerator[node, void])
  • #each_child_node ⇒ void

    This method returns an undefined value.

Yields:

Yield Parameters:

  • arg0 (node)

Yield Returns:

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

Explore code sample

: () -> ElseNode?

Returns:



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

Returns:

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

Explore code sample

: () -> Location

Returns:



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

Returns:

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

Explore code sample

: () -> Prism::node?

Returns:

  • (Prism::node, nil)


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

Parameters:

Returns:



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

Parameters:

Returns:



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

Returns:

  • (:case_match_node)


6422
6423
6424
# File 'lib/prism/node.rb', line 6422

def type
  :case_match_node
end