Class: YARP::HashPatternNode

Inherits:
YARPNode
  • Object
show all
Defined in:
lib/yarp/node.rb,
ext/yarp/api_node.c

Overview

Represents a hash pattern in pattern matching.

foo => { a: 1, b: 2 }
       ^^^^^^^^^^^^^^

foo => { a: 1, b: 2, **c }
       ^^^^^^^^^^^^^^^^^^^

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(constant, assocs, kwrest, opening_loc, closing_loc, location) ⇒ HashPatternNode

def initialize: (constant: Node?, assocs: Array, kwrest: Node?, opening_loc: Location?, closing_loc: Location?, location: Location) -> void



5012
5013
5014
5015
5016
5017
5018
5019
# File 'lib/yarp/node.rb', line 5012

def initialize(constant, assocs, kwrest, opening_loc, closing_loc, location)
  @constant = constant
  @assocs = assocs
  @kwrest = kwrest
  @opening_loc = opening_loc
  @closing_loc = closing_loc
  @location = location
end

Instance Attribute Details

#assocsObject (readonly)

attr_reader assocs: Array



5000
5001
5002
# File 'lib/yarp/node.rb', line 5000

def assocs
  @assocs
end

#closing_locObject (readonly)

attr_reader closing_loc: Location?



5009
5010
5011
# File 'lib/yarp/node.rb', line 5009

def closing_loc
  @closing_loc
end

#constantObject (readonly)

attr_reader constant: Node?



4997
4998
4999
# File 'lib/yarp/node.rb', line 4997

def constant
  @constant
end

#kwrestObject (readonly)

attr_reader kwrest: Node?



5003
5004
5005
# File 'lib/yarp/node.rb', line 5003

def kwrest
  @kwrest
end

#opening_locObject (readonly)

attr_reader opening_loc: Location?



5006
5007
5008
# File 'lib/yarp/node.rb', line 5006

def opening_loc
  @opening_loc
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



5022
5023
5024
# File 'lib/yarp/node.rb', line 5022

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

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array[nil | Node]



5027
5028
5029
# File 'lib/yarp/node.rb', line 5027

def child_nodes
  [constant, *assocs, kwrest]
end

#closingObject

def closing: () -> String?



5062
5063
5064
# File 'lib/yarp/node.rb', line 5062

def closing
  closing_loc&.slice
end

#comment_targetsObject

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



5032
5033
5034
# File 'lib/yarp/node.rb', line 5032

def comment_targets
  [*constant, *assocs, *kwrest, *opening_loc, *closing_loc]
end

#copy(**params) ⇒ Object

def copy: (**params) -> HashPatternNode



5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
# File 'lib/yarp/node.rb', line 5037

def copy(**params)
  HashPatternNode.new(
    params.fetch(:constant) { constant },
    params.fetch(:assocs) { assocs },
    params.fetch(:kwrest) { kwrest },
    params.fetch(:opening_loc) { opening_loc },
    params.fetch(:closing_loc) { closing_loc },
    params.fetch(:location) { location },
  )
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (keys: Array) -> Hash[Symbol, nil | Node | Array | String | Token | Array | Location]



5052
5053
5054
# File 'lib/yarp/node.rb', line 5052

def deconstruct_keys(keys)
  { constant: constant, assocs: assocs, kwrest: kwrest, opening_loc: opening_loc, closing_loc: closing_loc, location: location }
end

#inspect(inspector = NodeInspector.new) ⇒ Object



5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
# File 'lib/yarp/node.rb', line 5066

def inspect(inspector = NodeInspector.new)
  inspector << inspector.header(self)
  if (constant = self.constant).nil?
    inspector << "├── constant: ∅\n"
  else
    inspector << "├── constant:\n"
    inspector << constant.inspect(inspector.child_inspector("")).delete_prefix(inspector.prefix)
  end
  inspector << "├── assocs: #{inspector.list("#{inspector.prefix}", assocs)}"
  if (kwrest = self.kwrest).nil?
    inspector << "├── kwrest: ∅\n"
  else
    inspector << "├── kwrest:\n"
    inspector << kwrest.inspect(inspector.child_inspector("")).delete_prefix(inspector.prefix)
  end
  inspector << "├── opening_loc: #{inspector.location(opening_loc)}\n"
  inspector << "└── closing_loc: #{inspector.location(closing_loc)}\n"
  inspector.to_str
end

#openingObject

def opening: () -> String?



5057
5058
5059
# File 'lib/yarp/node.rb', line 5057

def opening
  opening_loc&.slice
end