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

Constructor Details

#initialize(constant:, requireds:, rest:, posts:, location:) ⇒ AryPtn

Returns a new instance of AryPtn.



1320
1321
1322
1323
1324
1325
1326
1327
# File 'lib/syntax_tree/node.rb', line 1320

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



1318
1319
1320
# File 'lib/syntax_tree/node.rb', line 1318

def comments
  @comments
end

#constantObject (readonly)

nil | VarRef

the optional constant wrapper



1303
1304
1305
# File 'lib/syntax_tree/node.rb', line 1303

def constant
  @constant
end

#postsObject (readonly)

Array[ Node ]

the list of positional arguments occurring after the

optional star if there is one



1315
1316
1317
# File 'lib/syntax_tree/node.rb', line 1315

def posts
  @posts
end

#requiredsObject (readonly)

Array[ Node ]

the regular positional arguments that this array

pattern is matching against



1307
1308
1309
# File 'lib/syntax_tree/node.rb', line 1307

def requireds
  @requireds
end

#restObject (readonly)

nil | VarField

the optional starred identifier that grabs up a list of

positional arguments



1311
1312
1313
# File 'lib/syntax_tree/node.rb', line 1311

def rest
  @rest
end

Instance Method Details

#===(other) ⇒ Object



1388
1389
1390
1391
1392
# File 'lib/syntax_tree/node.rb', line 1388

def ===(other)
  other.is_a?(AryPtn) && constant === other.constant &&
    ArrayMatch.call(requireds, other.requireds) && rest === other.rest &&
    ArrayMatch.call(posts, other.posts)
end

#accept(visitor) ⇒ Object



1329
1330
1331
# File 'lib/syntax_tree/node.rb', line 1329

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

#child_nodesObject Also known as: deconstruct



1333
1334
1335
# File 'lib/syntax_tree/node.rb', line 1333

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

#copy(constant: nil, requireds: nil, rest: nil, posts: nil, location: nil) ⇒ Object



1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
# File 'lib/syntax_tree/node.rb', line 1337

def copy(
  constant: nil,
  requireds: nil,
  rest: nil,
  posts: nil,
  location: nil
)
  node =
    AryPtn.new(
      constant: constant || self.constant,
      requireds: requireds || self.requireds,
      rest: rest || self.rest,
      posts: posts || self.posts,
      location: location || self.location
    )

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

#deconstruct_keys(_keys) ⇒ Object



1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
# File 'lib/syntax_tree/node.rb', line 1359

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

#format(q) ⇒ Object



1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
# File 'lib/syntax_tree/node.rb', line 1370

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