Class: SyntaxTree::FndPtn

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree.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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of FndPtn.



5694
5695
5696
5697
5698
5699
5700
5701
# File 'lib/syntax_tree.rb', line 5694

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



5692
5693
5694
# File 'lib/syntax_tree.rb', line 5692

def comments
  @comments
end

#constantObject (readonly)

nil | untyped

the optional constant wrapper



5676
5677
5678
# File 'lib/syntax_tree.rb', line 5676

def constant
  @constant
end

#leftObject (readonly)

VarField

the splat on the left-hand side



5679
5680
5681
# File 'lib/syntax_tree.rb', line 5679

def left
  @left
end

#locationObject (readonly)

Location

the location of this node



5689
5690
5691
# File 'lib/syntax_tree.rb', line 5689

def location
  @location
end

#rightObject (readonly)

VarField

the splat on the right-hand side



5686
5687
5688
# File 'lib/syntax_tree.rb', line 5686

def right
  @right
end

#valuesObject (readonly)

Array[ untyped ]

the list of positional expressions in the pattern that

are being matched



5683
5684
5685
# File 'lib/syntax_tree.rb', line 5683

def values
  @values
end

Instance Method Details

#child_nodesObject



5703
5704
5705
# File 'lib/syntax_tree.rb', line 5703

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

#format(q) ⇒ Object



5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
# File 'lib/syntax_tree.rb', line 5707

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

#pretty_print(q) ⇒ Object



5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
# File 'lib/syntax_tree.rb', line 5722

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("fndptn")

    if constant
      q.breakable
      q.pp(constant)
    end

    q.breakable
    q.pp(left)

    q.breakable
    q.group(2, "(", ")") { q.seplist(values) { |value| q.pp(value) } }

    q.breakable
    q.pp(right)

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
# File 'lib/syntax_tree.rb', line 5744

def to_json(*opts)
  {
    type: :fndptn,
    constant: constant,
    left: left,
    values: values,
    right: right,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end