Class: SyntaxTree::AryPtn

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

AryPtn represents matching against an array pattern using the Ruby 2.7+ pattern matching syntax. It’s one of the more complicated nodes, because the four parameters that it accepts can almost all be nil.

case [1, 2, 3]
in [Integer, Integer]
  "matched"
in Container[Integer, Integer]
  "matched"
in [Integer, *, Integer]
  "matched"
end

An AryPtn node is created with four parameters: an optional constant wrapper, an array of positional matches, an optional splat with identifier, and an optional array of positional matches that occur after the splat. All of the in clauses above would create an AryPtn node.

Defined Under Namespace

Classes: RestFormatter

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:, requireds:, rest:, posts:, location:, comments: []) ⇒ AryPtn



1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
# File 'lib/syntax_tree/node.rb', line 1122

def initialize(
  constant:,
  requireds:,
  rest:,
  posts:,
  location:,
  comments: []
)
  @constant = constant
  @requireds = requireds
  @rest = rest
  @posts = posts
  @location = location
  @comments = comments
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



1120
1121
1122
# File 'lib/syntax_tree/node.rb', line 1120

def comments
  @comments
end

#constantObject (readonly)

nil | VarRef

the optional constant wrapper



1105
1106
1107
# File 'lib/syntax_tree/node.rb', line 1105

def constant
  @constant
end

#postsObject (readonly)

Array[ untyped ]

the list of positional arguments occurring after the

optional star if there is one



1117
1118
1119
# File 'lib/syntax_tree/node.rb', line 1117

def posts
  @posts
end

#requiredsObject (readonly)

Array[ untyped ]

the regular positional arguments that this array

pattern is matching against



1109
1110
1111
# File 'lib/syntax_tree/node.rb', line 1109

def requireds
  @requireds
end

#restObject (readonly)

nil | VarField

the optional starred identifier that grabs up a list of

positional arguments



1113
1114
1115
# File 'lib/syntax_tree/node.rb', line 1113

def rest
  @rest
end

Instance Method Details

#accept(visitor) ⇒ Object



1138
1139
1140
# File 'lib/syntax_tree/node.rb', line 1138

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

#child_nodesObject Also known as: deconstruct



1142
1143
1144
# File 'lib/syntax_tree/node.rb', line 1142

def child_nodes
  [constant, *requireds, rest, *posts]
end

#deconstruct_keys(_keys) ⇒ Object



1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
# File 'lib/syntax_tree/node.rb', line 1148

def deconstruct_keys(_keys)
  {
    constant: constant,
    requireds: requireds,
    rest: rest,
    posts: posts,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
# File 'lib/syntax_tree/node.rb', line 1159

def format(q)
  q.group do
    q.format(constant) if constant
    q.text("[")
    q.indent do
      q.breakable_empty

      parts = [*requireds]
      parts << RestFormatter.new(rest) if rest
      parts += posts

      q.seplist(parts) { |part| q.format(part) }
    end
    q.breakable_empty
    q.text("]")
  end
end