Class: SyntaxTree::AryPtn

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree.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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of AryPtn.



1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
# File 'lib/syntax_tree.rb', line 1653

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



1651
1652
1653
# File 'lib/syntax_tree.rb', line 1651

def comments
  @comments
end

#constantObject (readonly)

nil | VarRef

the optional constant wrapper



1633
1634
1635
# File 'lib/syntax_tree.rb', line 1633

def constant
  @constant
end

#locationObject (readonly)

Location

the location of this node



1648
1649
1650
# File 'lib/syntax_tree.rb', line 1648

def location
  @location
end

#postsObject (readonly)

Array[ untyped ]

the list of positional arguments occurring after the

optional star if there is one



1645
1646
1647
# File 'lib/syntax_tree.rb', line 1645

def posts
  @posts
end

#requiredsObject (readonly)

Array[ untyped ]

the regular positional arguments that this array

pattern is matching against



1637
1638
1639
# File 'lib/syntax_tree.rb', line 1637

def requireds
  @requireds
end

#restObject (readonly)

nil | VarField

the optional starred identifier that grabs up a list of

positional arguments



1641
1642
1643
# File 'lib/syntax_tree.rb', line 1641

def rest
  @rest
end

Instance Method Details

#child_nodesObject



1669
1670
1671
# File 'lib/syntax_tree.rb', line 1669

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

#format(q) ⇒ Object



1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
# File 'lib/syntax_tree.rb', line 1673

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

  if constant
    q.format(constant)
    q.text("[")
    q.seplist(parts) { |part| q.format(part) }
    q.text("]")
    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.seplist(parts) { |part| q.format(part) }
  end
end

#pretty_print(q) ⇒ Object



1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
# File 'lib/syntax_tree.rb', line 1696

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("aryptn")

    if constant
      q.breakable
      q.pp(constant)
    end

    if requireds.any?
      q.breakable
      q.group(2, "(", ")") do
        q.seplist(requireds) { |required| q.pp(required) }
      end
    end

    if rest
      q.breakable
      q.pp(rest)
    end

    if posts.any?
      q.breakable
      q.group(2, "(", ")") { q.seplist(posts) { |post| q.pp(post) } }
    end

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
# File 'lib/syntax_tree.rb', line 1726

def to_json(*opts)
  {
    type: :aryptn,
    constant: constant,
    reqs: requireds,
    rest: rest,
    posts: posts,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end