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.



4109
4110
4111
4112
4113
4114
4115
4116
# File 'lib/syntax_tree/node.rb', line 4109

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



4107
4108
4109
# File 'lib/syntax_tree/node.rb', line 4107

def comments
  @comments
end

#constantObject (readonly)

nil | untyped

the optional constant wrapper



4094
4095
4096
# File 'lib/syntax_tree/node.rb', line 4094

def constant
  @constant
end

#leftObject (readonly)

VarField

the splat on the left-hand side



4097
4098
4099
# File 'lib/syntax_tree/node.rb', line 4097

def left
  @left
end

#rightObject (readonly)

VarField

the splat on the right-hand side



4104
4105
4106
# File 'lib/syntax_tree/node.rb', line 4104

def right
  @right
end

#valuesObject (readonly)

Array[ untyped ]

the list of positional expressions in the pattern that

are being matched



4101
4102
4103
# File 'lib/syntax_tree/node.rb', line 4101

def values
  @values
end

Instance Method Details

#accept(visitor) ⇒ Object



4118
4119
4120
# File 'lib/syntax_tree/node.rb', line 4118

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

#child_nodesObject Also known as: deconstruct



4122
4123
4124
# File 'lib/syntax_tree/node.rb', line 4122

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

#deconstruct_keys(keys) ⇒ Object



4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
# File 'lib/syntax_tree/node.rb', line 4128

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

#format(q) ⇒ Object



4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
# File 'lib/syntax_tree/node.rb', line 4139

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