Class: SyntaxTree::HshPtn
- Inherits:
-
Object
- Object
- SyntaxTree::HshPtn
- Defined in:
- lib/syntax_tree.rb
Overview
HshPtn represents matching against a hash pattern using the Ruby 2.7+ pattern matching syntax.
case value
in { key: }
end
Defined Under Namespace
Classes: KeywordFormatter, KeywordRestFormatter
Instance Attribute Summary collapse
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#constant ⇒ Object
readonly
- nil | untyped
-
the optional constant wrapper.
-
#keyword_rest ⇒ Object
readonly
- nil | VarField
-
an optional parameter to gather up all remaining keywords.
-
#keywords ⇒ Object
readonly
- Array[ [Label, untyped
-
]] the set of tuples representing the keywords that should be matched against in the pattern.
-
#location ⇒ Object
readonly
- Location
-
the location of this node.
Instance Method Summary collapse
- #child_nodes ⇒ Object
- #format(q) ⇒ Object
-
#initialize(constant:, keywords:, keyword_rest:, location:, comments: []) ⇒ HshPtn
constructor
A new instance of HshPtn.
- #pretty_print(q) ⇒ Object
- #to_json(*opts) ⇒ Object
Constructor Details
#initialize(constant:, keywords:, keyword_rest:, location:, comments: []) ⇒ HshPtn
Returns a new instance of HshPtn.
6608 6609 6610 6611 6612 6613 6614 |
# File 'lib/syntax_tree.rb', line 6608 def initialize(constant:, keywords:, keyword_rest:, location:, comments: []) @constant = constant @keywords = keywords @keyword_rest = keyword_rest @location = location @comments = comments end |
Instance Attribute Details
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
6606 6607 6608 |
# File 'lib/syntax_tree.rb', line 6606 def comments @comments end |
#constant ⇒ Object (readonly)
- nil | untyped
-
the optional constant wrapper
6593 6594 6595 |
# File 'lib/syntax_tree.rb', line 6593 def constant @constant end |
#keyword_rest ⇒ Object (readonly)
- nil | VarField
-
an optional parameter to gather up all remaining keywords
6600 6601 6602 |
# File 'lib/syntax_tree.rb', line 6600 def keyword_rest @keyword_rest end |
#keywords ⇒ Object (readonly)
- Array[ [Label, untyped
-
]] the set of tuples representing the keywords
that should be matched against in the pattern
6597 6598 6599 |
# File 'lib/syntax_tree.rb', line 6597 def keywords @keywords end |
#location ⇒ Object (readonly)
- Location
-
the location of this node
6603 6604 6605 |
# File 'lib/syntax_tree.rb', line 6603 def location @location end |
Instance Method Details
#child_nodes ⇒ Object
6616 6617 6618 |
# File 'lib/syntax_tree.rb', line 6616 def child_nodes [constant, *keywords.flatten(1), keyword_rest] end |
#format(q) ⇒ Object
6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 |
# File 'lib/syntax_tree.rb', line 6620 def format(q) parts = keywords.map { |(key, value)| KeywordFormatter.new(key, value) } parts << KeywordRestFormatter.new(keyword_rest) if keyword_rest contents = -> { q.seplist(parts) { |part| q.format(part) } } if constant q.format(constant) q.text("[") contents.call q.text("]") return end parent = q.parent if PATTERNS.include?(parent.class) q.text("{ ") contents.call q.text(" }") else contents.call end end |
#pretty_print(q) ⇒ Object
6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 |
# File 'lib/syntax_tree.rb', line 6643 def pretty_print(q) q.group(2, "(", ")") do q.text("hshptn") if constant q.breakable q.pp(constant) end if keywords.any? q.breakable q.group(2, "(", ")") do q.seplist(keywords) do |(key, value)| q.group(2, "(", ")") do q.pp(key) if value q.breakable q.pp(value) end end end end end if keyword_rest q.breakable q.pp(keyword_rest) end q.pp(Comment::List.new(comments)) end end |
#to_json(*opts) ⇒ Object
6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 |
# File 'lib/syntax_tree.rb', line 6677 def to_json(*opts) { type: :hshptn, constant: constant, keywords: keywords, kwrest: keyword_rest, loc: location, cmts: comments }.to_json(*opts) end |