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, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

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

Returns a new instance of FndPtn.



5382
5383
5384
5385
5386
5387
5388
5389
# File 'lib/syntax_tree/node.rb', line 5382

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



5380
5381
5382
# File 'lib/syntax_tree/node.rb', line 5380

def comments
  @comments
end

#constantObject (readonly)

nil | Node

the optional constant wrapper



5367
5368
5369
# File 'lib/syntax_tree/node.rb', line 5367

def constant
  @constant
end

#leftObject (readonly)

VarField

the splat on the left-hand side



5370
5371
5372
# File 'lib/syntax_tree/node.rb', line 5370

def left
  @left
end

#rightObject (readonly)

VarField

the splat on the right-hand side



5377
5378
5379
# File 'lib/syntax_tree/node.rb', line 5377

def right
  @right
end

#valuesObject (readonly)

Array[ Node ]

the list of positional expressions in the pattern that

are being matched



5374
5375
5376
# File 'lib/syntax_tree/node.rb', line 5374

def values
  @values
end

Instance Method Details

#===(other) ⇒ Object



5451
5452
5453
5454
5455
# File 'lib/syntax_tree/node.rb', line 5451

def ===(other)
  other.is_a?(FndPtn) && constant === other.constant &&
    left === other.left && ArrayMatch.call(values, other.values) &&
    right === other.right
end

#accept(visitor) ⇒ Object



5391
5392
5393
# File 'lib/syntax_tree/node.rb', line 5391

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

#child_nodesObject Also known as: deconstruct



5395
5396
5397
# File 'lib/syntax_tree/node.rb', line 5395

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

#copy(constant: nil, left: nil, values: nil, right: nil, location: nil) ⇒ Object



5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
# File 'lib/syntax_tree/node.rb', line 5399

def copy(constant: nil, left: nil, values: nil, right: nil, location: nil)
  node =
    FndPtn.new(
      constant: constant || self.constant,
      left: left || self.left,
      values: values || self.values,
      right: right || self.right,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
# File 'lib/syntax_tree/node.rb', line 5415

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

#format(q) ⇒ Object



5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
# File 'lib/syntax_tree/node.rb', line 5426

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