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

#pretty_print, #to_json

Constructor Details

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

Returns a new instance of FndPtn.



4471
4472
4473
4474
4475
4476
4477
4478
# File 'lib/syntax_tree/node.rb', line 4471

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



4469
4470
4471
# File 'lib/syntax_tree/node.rb', line 4469

def comments
  @comments
end

#constantObject (readonly)

nil | untyped

the optional constant wrapper



4456
4457
4458
# File 'lib/syntax_tree/node.rb', line 4456

def constant
  @constant
end

#leftObject (readonly)

VarField

the splat on the left-hand side



4459
4460
4461
# File 'lib/syntax_tree/node.rb', line 4459

def left
  @left
end

#rightObject (readonly)

VarField

the splat on the right-hand side



4466
4467
4468
# File 'lib/syntax_tree/node.rb', line 4466

def right
  @right
end

#valuesObject (readonly)

Array[ untyped ]

the list of positional expressions in the pattern that

are being matched



4463
4464
4465
# File 'lib/syntax_tree/node.rb', line 4463

def values
  @values
end

Instance Method Details

#accept(visitor) ⇒ Object



4480
4481
4482
# File 'lib/syntax_tree/node.rb', line 4480

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

#child_nodesObject Also known as: deconstruct



4484
4485
4486
# File 'lib/syntax_tree/node.rb', line 4484

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

#deconstruct_keys(keys) ⇒ Object



4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
# File 'lib/syntax_tree/node.rb', line 4490

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

#format(q) ⇒ Object



4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
# File 'lib/syntax_tree/node.rb', line 4501

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