Class: SyntaxTree::FndPtn

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

FndPtn represents matching against a pattern where you find a pattern in an array using the Ruby 3.0+ pattern matching syntax.

case value
in [*, 7, *]
end

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #pretty_print, #to_json

Constructor Details

#initialize(constant:, left:, values:, right:, location:, comments: []) ⇒ FndPtn

Returns a new instance of FndPtn.



4510
4511
4512
4513
4514
4515
4516
4517
# File 'lib/syntax_tree/node.rb', line 4510

def initialize(constant:, left:, values:, right:, location:, comments: [])
  @constant = constant
  @left = left
  @values = values
  @right = right
  @location = location
  @comments = comments
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



4508
4509
4510
# File 'lib/syntax_tree/node.rb', line 4508

def comments
  @comments
end

#constantObject (readonly)

nil | untyped

the optional constant wrapper



4495
4496
4497
# File 'lib/syntax_tree/node.rb', line 4495

def constant
  @constant
end

#leftObject (readonly)

VarField

the splat on the left-hand side



4498
4499
4500
# File 'lib/syntax_tree/node.rb', line 4498

def left
  @left
end

#rightObject (readonly)

VarField

the splat on the right-hand side



4505
4506
4507
# File 'lib/syntax_tree/node.rb', line 4505

def right
  @right
end

#valuesObject (readonly)

Array[ untyped ]

the list of positional expressions in the pattern that

are being matched



4502
4503
4504
# File 'lib/syntax_tree/node.rb', line 4502

def values
  @values
end

Instance Method Details

#accept(visitor) ⇒ Object



4519
4520
4521
# File 'lib/syntax_tree/node.rb', line 4519

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

#child_nodesObject Also known as: deconstruct



4523
4524
4525
# File 'lib/syntax_tree/node.rb', line 4523

def child_nodes
  [constant, left, *values, right]
end

#deconstruct_keys(_keys) ⇒ Object



4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
# File 'lib/syntax_tree/node.rb', line 4529

def deconstruct_keys(_keys)
  {
    constant: constant,
    left: left,
    values: values,
    right: right,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
# File 'lib/syntax_tree/node.rb', line 4540

def format(q)
  q.format(constant) if constant
  q.group(0, "[", "]") do
    q.text("*")
    q.format(left)
    q.comma_breakable

    q.seplist(values) { |value| q.format(value) }
    q.comma_breakable

    q.text("*")
    q.format(right)
  end
end