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.



4543
4544
4545
4546
4547
4548
4549
4550
# File 'lib/syntax_tree/node.rb', line 4543

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



4541
4542
4543
# File 'lib/syntax_tree/node.rb', line 4541

def comments
  @comments
end

#constantObject (readonly)

nil | untyped

the optional constant wrapper



4528
4529
4530
# File 'lib/syntax_tree/node.rb', line 4528

def constant
  @constant
end

#leftObject (readonly)

VarField

the splat on the left-hand side



4531
4532
4533
# File 'lib/syntax_tree/node.rb', line 4531

def left
  @left
end

#rightObject (readonly)

VarField

the splat on the right-hand side



4538
4539
4540
# File 'lib/syntax_tree/node.rb', line 4538

def right
  @right
end

#valuesObject (readonly)

Array[ untyped ]

the list of positional expressions in the pattern that

are being matched



4535
4536
4537
# File 'lib/syntax_tree/node.rb', line 4535

def values
  @values
end

Instance Method Details

#accept(visitor) ⇒ Object



4552
4553
4554
# File 'lib/syntax_tree/node.rb', line 4552

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

#child_nodesObject Also known as: deconstruct



4556
4557
4558
# File 'lib/syntax_tree/node.rb', line 4556

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

#deconstruct_keys(_keys) ⇒ Object



4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
# File 'lib/syntax_tree/node.rb', line 4562

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

#format(q) ⇒ Object



4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
# File 'lib/syntax_tree/node.rb', line 4573

def format(q)
  q.format(constant) if constant

  q.group do
    q.text("[")

    q.indent do
      q.breakable("")

      q.text("*")
      q.format(left)
      q.comma_breakable

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

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

    q.breakable("")
    q.text("]")
  end
end