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

Returns a new instance of AryPtn.



1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
# File 'lib/syntax_tree/node.rb', line 1088

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



1086
1087
1088
# File 'lib/syntax_tree/node.rb', line 1086

def comments
  @comments
end

#constantObject (readonly)

nil | VarRef

the optional constant wrapper



1071
1072
1073
# File 'lib/syntax_tree/node.rb', line 1071

def constant
  @constant
end

#postsObject (readonly)

Array[ untyped ]

the list of positional arguments occurring after the

optional star if there is one



1083
1084
1085
# File 'lib/syntax_tree/node.rb', line 1083

def posts
  @posts
end

#requiredsObject (readonly)

Array[ untyped ]

the regular positional arguments that this array

pattern is matching against



1075
1076
1077
# File 'lib/syntax_tree/node.rb', line 1075

def requireds
  @requireds
end

#restObject (readonly)

nil | VarField

the optional starred identifier that grabs up a list of

positional arguments



1079
1080
1081
# File 'lib/syntax_tree/node.rb', line 1079

def rest
  @rest
end

Instance Method Details

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



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

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

#deconstruct_keys(_keys) ⇒ Object



1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
# File 'lib/syntax_tree/node.rb', line 1114

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

#format(q) ⇒ Object



1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
# File 'lib/syntax_tree/node.rb', line 1125

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

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

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