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.



4691
4692
4693
4694
4695
4696
4697
4698
# File 'lib/syntax_tree/node.rb', line 4691

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



4689
4690
4691
# File 'lib/syntax_tree/node.rb', line 4689

def comments
  @comments
end

#constantObject (readonly)

nil | untyped

the optional constant wrapper



4676
4677
4678
# File 'lib/syntax_tree/node.rb', line 4676

def constant
  @constant
end

#leftObject (readonly)

VarField

the splat on the left-hand side



4679
4680
4681
# File 'lib/syntax_tree/node.rb', line 4679

def left
  @left
end

#rightObject (readonly)

VarField

the splat on the right-hand side



4686
4687
4688
# File 'lib/syntax_tree/node.rb', line 4686

def right
  @right
end

#valuesObject (readonly)

Array[ untyped ]

the list of positional expressions in the pattern that

are being matched



4683
4684
4685
# File 'lib/syntax_tree/node.rb', line 4683

def values
  @values
end

Instance Method Details

#accept(visitor) ⇒ Object



4700
4701
4702
# File 'lib/syntax_tree/node.rb', line 4700

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

#child_nodesObject Also known as: deconstruct



4704
4705
4706
# File 'lib/syntax_tree/node.rb', line 4704

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

#deconstruct_keys(_keys) ⇒ Object



4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
# File 'lib/syntax_tree/node.rb', line 4710

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

#format(q) ⇒ Object



4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
# File 'lib/syntax_tree/node.rb', line 4721

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

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

    q.indent do
      q.breakable_empty

      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_empty
    q.text("]")
  end
end