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

#pretty_print, #to_json

Constructor Details

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

Returns a new instance of AryPtn.



1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
# File 'lib/syntax_tree/node.rb', line 1012

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



1010
1011
1012
# File 'lib/syntax_tree/node.rb', line 1010

def comments
  @comments
end

#constantObject (readonly)

nil | VarRef

the optional constant wrapper



995
996
997
# File 'lib/syntax_tree/node.rb', line 995

def constant
  @constant
end

#postsObject (readonly)

Array[ untyped ]

the list of positional arguments occurring after the

optional star if there is one



1007
1008
1009
# File 'lib/syntax_tree/node.rb', line 1007

def posts
  @posts
end

#requiredsObject (readonly)

Array[ untyped ]

the regular positional arguments that this array

pattern is matching against



999
1000
1001
# File 'lib/syntax_tree/node.rb', line 999

def requireds
  @requireds
end

#restObject (readonly)

nil | VarField

the optional starred identifier that grabs up a list of

positional arguments



1003
1004
1005
# File 'lib/syntax_tree/node.rb', line 1003

def rest
  @rest
end

Instance Method Details

#accept(visitor) ⇒ Object



1028
1029
1030
# File 'lib/syntax_tree/node.rb', line 1028

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

#child_nodesObject Also known as: deconstruct



1032
1033
1034
# File 'lib/syntax_tree/node.rb', line 1032

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

#deconstruct_keys(keys) ⇒ Object



1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
# File 'lib/syntax_tree/node.rb', line 1038

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

#format(q) ⇒ Object



1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
# File 'lib/syntax_tree/node.rb', line 1049

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

  if constant
    q.group do
      q.format(constant)
      q.text("[")
      q.seplist(parts) { |part| q.format(part) }
      q.text("]")
    end

    return
  end

  parent = q.parent
  if parts.length == 1 || PATTERNS.include?(parent.class)
    q.text("[")
    q.seplist(parts) { |part| q.format(part) }
    q.text("]")
  else
    q.group { q.seplist(parts) { |part| q.format(part) } }
  end
end