Class: SyntaxTree::AryPtn
- Inherits:
-
Object
- Object
- SyntaxTree::AryPtn
- 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
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#constant ⇒ Object
readonly
- nil | VarRef
-
the optional constant wrapper.
-
#location ⇒ Object
readonly
- Location
-
the location of this node.
-
#posts ⇒ Object
readonly
- Array[ untyped ]
-
the list of positional arguments occurring after the optional star if there is one.
-
#requireds ⇒ Object
readonly
- Array[ untyped ]
-
the regular positional arguments that this array pattern is matching against.
-
#rest ⇒ Object
readonly
- nil | VarField
-
the optional starred identifier that grabs up a list of positional arguments.
Instance Method Summary collapse
- #child_nodes ⇒ Object
- #format(q) ⇒ Object
-
#initialize(constant:, requireds:, rest:, posts:, location:, comments: []) ⇒ AryPtn
constructor
A new instance of AryPtn.
- #pretty_print(q) ⇒ Object
- #to_json(*opts) ⇒ Object
Constructor Details
#initialize(constant:, requireds:, rest:, posts:, location:, comments: []) ⇒ AryPtn
Returns a new instance of AryPtn.
1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 |
# File 'lib/syntax_tree.rb', line 1640 def initialize( constant:, requireds:, rest:, posts:, location:, comments: [] ) @constant = constant @requireds = requireds @rest = rest @posts = posts @location = location @comments = comments end |
Instance Attribute Details
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
1638 1639 1640 |
# File 'lib/syntax_tree.rb', line 1638 def comments @comments end |
#constant ⇒ Object (readonly)
- nil | VarRef
-
the optional constant wrapper
1620 1621 1622 |
# File 'lib/syntax_tree.rb', line 1620 def constant @constant end |
#location ⇒ Object (readonly)
- Location
-
the location of this node
1635 1636 1637 |
# File 'lib/syntax_tree.rb', line 1635 def location @location end |
#posts ⇒ Object (readonly)
- Array[ untyped ]
-
the list of positional arguments occurring after the
optional star if there is one
1632 1633 1634 |
# File 'lib/syntax_tree.rb', line 1632 def posts @posts end |
#requireds ⇒ Object (readonly)
- Array[ untyped ]
-
the regular positional arguments that this array
pattern is matching against
1624 1625 1626 |
# File 'lib/syntax_tree.rb', line 1624 def requireds @requireds end |
#rest ⇒ Object (readonly)
- nil | VarField
-
the optional starred identifier that grabs up a list of
positional arguments
1628 1629 1630 |
# File 'lib/syntax_tree.rb', line 1628 def rest @rest end |
Instance Method Details
#child_nodes ⇒ Object
1656 1657 1658 |
# File 'lib/syntax_tree.rb', line 1656 def child_nodes [constant, *required, rest, *posts] end |
#format(q) ⇒ Object
1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 |
# File 'lib/syntax_tree.rb', line 1660 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
1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 |
# File 'lib/syntax_tree.rb', line 1683 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
1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 |
# File 'lib/syntax_tree.rb', line 1713 def to_json(*opts) { type: :aryptn, constant: constant, reqs: requireds, rest: rest, posts: posts, loc: location, cmts: comments }.to_json(*opts) end |