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.



963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
# File 'lib/syntax_tree/node.rb', line 963

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



961
962
963
# File 'lib/syntax_tree/node.rb', line 961

def comments
  @comments
end

#constantObject (readonly)

nil | VarRef

the optional constant wrapper



946
947
948
# File 'lib/syntax_tree/node.rb', line 946

def constant
  @constant
end

#postsObject (readonly)

Array[ untyped ]

the list of positional arguments occurring after the

optional star if there is one



958
959
960
# File 'lib/syntax_tree/node.rb', line 958

def posts
  @posts
end

#requiredsObject (readonly)

Array[ untyped ]

the regular positional arguments that this array

pattern is matching against



950
951
952
# File 'lib/syntax_tree/node.rb', line 950

def requireds
  @requireds
end

#restObject (readonly)

nil | VarField

the optional starred identifier that grabs up a list of

positional arguments



954
955
956
# File 'lib/syntax_tree/node.rb', line 954

def rest
  @rest
end

Instance Method Details

#accept(visitor) ⇒ Object



979
980
981
# File 'lib/syntax_tree/node.rb', line 979

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

#child_nodesObject Also known as: deconstruct



983
984
985
# File 'lib/syntax_tree/node.rb', line 983

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

#deconstruct_keys(keys) ⇒ Object



989
990
991
992
993
994
995
996
997
998
# File 'lib/syntax_tree/node.rb', line 989

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

#format(q) ⇒ Object



1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
# File 'lib/syntax_tree/node.rb', line 1000

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