Class: YARP::ArrayPatternNode

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

Overview

Represents an array pattern in pattern matching.

foo in 1, 2
^^^^^^^^^^^

foo in [1, 2]
^^^^^^^^^^^^^

foo in *1
^^^^^^^^^

foo in Bar[]
^^^^^^^^^^^^

foo in Bar[1, 2, 3]
^^^^^^^^^^^^^^^^^^^

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(constant, requireds, rest, posts, opening_loc, closing_loc, location) ⇒ ArrayPatternNode

def initialize: (constant: Node?, requireds: Array, rest: Node?, posts: Array, opening_loc: Location?, closing_loc: Location?, location: Location) -> void



385
386
387
388
389
390
391
392
393
# File 'lib/yarp/node.rb', line 385

def initialize(constant, requireds, rest, posts, opening_loc, closing_loc, location)
  @constant = constant
  @requireds = requireds
  @rest = rest
  @posts = posts
  @opening_loc = opening_loc
  @closing_loc = closing_loc
  @location = location
end

Instance Attribute Details

#closing_locObject (readonly)

attr_reader closing_loc: Location?



382
383
384
# File 'lib/yarp/node.rb', line 382

def closing_loc
  @closing_loc
end

#constantObject (readonly)

attr_reader constant: Node?



367
368
369
# File 'lib/yarp/node.rb', line 367

def constant
  @constant
end

#opening_locObject (readonly)

attr_reader opening_loc: Location?



379
380
381
# File 'lib/yarp/node.rb', line 379

def opening_loc
  @opening_loc
end

#postsObject (readonly)

attr_reader posts: Array



376
377
378
# File 'lib/yarp/node.rb', line 376

def posts
  @posts
end

#requiredsObject (readonly)

attr_reader requireds: Array



370
371
372
# File 'lib/yarp/node.rb', line 370

def requireds
  @requireds
end

#restObject (readonly)

attr_reader rest: Node?



373
374
375
# File 'lib/yarp/node.rb', line 373

def rest
  @rest
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



396
397
398
# File 'lib/yarp/node.rb', line 396

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

#child_nodesObject Also known as: deconstruct

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



401
402
403
# File 'lib/yarp/node.rb', line 401

def child_nodes
  [constant, *requireds, rest, *posts]
end

#closingObject

def closing: () -> String?



437
438
439
# File 'lib/yarp/node.rb', line 437

def closing
  closing_loc&.slice
end

#comment_targetsObject

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



406
407
408
# File 'lib/yarp/node.rb', line 406

def comment_targets
  [*constant, *requireds, *rest, *posts, *opening_loc, *closing_loc]
end

#copy(**params) ⇒ Object

def copy: (**params) -> ArrayPatternNode



411
412
413
414
415
416
417
418
419
420
421
# File 'lib/yarp/node.rb', line 411

def copy(**params)
  ArrayPatternNode.new(
    params.fetch(:constant) { constant },
    params.fetch(:requireds) { requireds },
    params.fetch(:rest) { rest },
    params.fetch(:posts) { posts },
    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]



427
428
429
# File 'lib/yarp/node.rb', line 427

def deconstruct_keys(keys)
  { constant: constant, requireds: requireds, rest: rest, posts: posts, opening_loc: opening_loc, closing_loc: closing_loc, location: location }
end

#inspect(inspector = NodeInspector.new) ⇒ Object



441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
# File 'lib/yarp/node.rb', line 441

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 << "├── requireds: #{inspector.list("#{inspector.prefix}│   ", requireds)}"
  if (rest = self.rest).nil?
    inspector << "├── rest: ∅\n"
  else
    inspector << "├── rest:\n"
    inspector << rest.inspect(inspector.child_inspector("│   ")).delete_prefix(inspector.prefix)
  end
  inspector << "├── posts: #{inspector.list("#{inspector.prefix}│   ", posts)}"
  inspector << "├── opening_loc: #{inspector.location(opening_loc)}\n"
  inspector << "└── closing_loc: #{inspector.location(closing_loc)}\n"
  inspector.to_str
end

#openingObject

def opening: () -> String?



432
433
434
# File 'lib/yarp/node.rb', line 432

def opening
  opening_loc&.slice
end