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.
1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 |
# File 'lib/syntax_tree.rb', line 1540 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
1538 1539 1540 |
# File 'lib/syntax_tree.rb', line 1538 def comments @comments end |
#constant ⇒ Object (readonly)
- nil | VarRef
-
the optional constant wrapper
1520 1521 1522 |
# File 'lib/syntax_tree.rb', line 1520 def constant @constant end |
#location ⇒ Object (readonly)
- Location
-
the location of this node
1535 1536 1537 |
# File 'lib/syntax_tree.rb', line 1535 def location @location end |
#posts ⇒ Object (readonly)
- Array[ untyped ]
-
the list of positional arguments occurring after the
optional star if there is one
1532 1533 1534 |
# File 'lib/syntax_tree.rb', line 1532 def posts @posts end |
#requireds ⇒ Object (readonly)
- Array[ untyped ]
-
the regular positional arguments that this array
pattern is matching against
1524 1525 1526 |
# File 'lib/syntax_tree.rb', line 1524 def requireds @requireds end |
#rest ⇒ Object (readonly)
- nil | VarField
-
the optional starred identifier that grabs up a list of
positional arguments
1528 1529 1530 |
# File 'lib/syntax_tree.rb', line 1528 def rest @rest end |
Instance Method Details
#child_nodes ⇒ Object
1556 1557 1558 |
# File 'lib/syntax_tree.rb', line 1556 def child_nodes [constant, *required, rest, *posts] end |
#format(q) ⇒ Object
1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 |
# File 'lib/syntax_tree.rb', line 1560 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.any? { |pattern| parent.is_a?(pattern) } 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
1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 |
# File 'lib/syntax_tree.rb', line 1583 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
1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 |
# File 'lib/syntax_tree.rb', line 1613 def to_json(*opts) { type: :aryptn, constant: constant, reqs: requireds, rest: rest, posts: posts, loc: location, cmts: comments }.to_json(*opts) end |