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:) ⇒ FndPtn

Returns a new instance of FndPtn.



5327
5328
5329
5330
5331
5332
5333
5334
# File 'lib/syntax_tree/node.rb', line 5327

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



5325
5326
5327
# File 'lib/syntax_tree/node.rb', line 5325

def comments
  @comments
end

#constantObject (readonly)

nil | untyped

the optional constant wrapper



5312
5313
5314
# File 'lib/syntax_tree/node.rb', line 5312

def constant
  @constant
end

#leftObject (readonly)

VarField

the splat on the left-hand side



5315
5316
5317
# File 'lib/syntax_tree/node.rb', line 5315

def left
  @left
end

#rightObject (readonly)

VarField

the splat on the right-hand side



5322
5323
5324
# File 'lib/syntax_tree/node.rb', line 5322

def right
  @right
end

#valuesObject (readonly)

Array[ untyped ]

the list of positional expressions in the pattern that

are being matched



5319
5320
5321
# File 'lib/syntax_tree/node.rb', line 5319

def values
  @values
end

Instance Method Details

#===(other) ⇒ Object



5396
5397
5398
5399
5400
# File 'lib/syntax_tree/node.rb', line 5396

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

#accept(visitor) ⇒ Object



5336
5337
5338
# File 'lib/syntax_tree/node.rb', line 5336

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

#child_nodesObject Also known as: deconstruct



5340
5341
5342
# File 'lib/syntax_tree/node.rb', line 5340

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

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



5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
# File 'lib/syntax_tree/node.rb', line 5344

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



5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
# File 'lib/syntax_tree/node.rb', line 5360

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

#format(q) ⇒ Object



5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
# File 'lib/syntax_tree/node.rb', line 5371

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