Class: SyntaxTree::HshPtn

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(constant:, keywords:, keyword_rest:, location:, comments: []) ⇒ HshPtn

Returns a new instance of HshPtn.



6649
6650
6651
6652
6653
6654
6655
# File 'lib/syntax_tree.rb', line 6649

def initialize(constant:, keywords:, keyword_rest:, location:, comments: [])
  @constant = constant
  @keywords = keywords
  @keyword_rest = keyword_rest
  @location = location
  @comments = comments
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



6647
6648
6649
# File 'lib/syntax_tree.rb', line 6647

def comments
  @comments
end

#constantObject (readonly)

nil | untyped

the optional constant wrapper



6634
6635
6636
# File 'lib/syntax_tree.rb', line 6634

def constant
  @constant
end

#keyword_restObject (readonly)

nil | VarField

an optional parameter to gather up all remaining keywords



6641
6642
6643
# File 'lib/syntax_tree.rb', line 6641

def keyword_rest
  @keyword_rest
end

#keywordsObject (readonly)

Array[ [Label, untyped

]] the set of tuples representing the keywords

that should be matched against in the pattern



6638
6639
6640
# File 'lib/syntax_tree.rb', line 6638

def keywords
  @keywords
end

#locationObject (readonly)

Location

the location of this node



6644
6645
6646
# File 'lib/syntax_tree.rb', line 6644

def location
  @location
end

Instance Method Details

#child_nodesObject



6657
6658
6659
# File 'lib/syntax_tree.rb', line 6657

def child_nodes
  [constant, *keywords.flatten(1), keyword_rest]
end

#format(q) ⇒ Object



6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
# File 'lib/syntax_tree.rb', line 6661

def format(q)
  parts = keywords.map { |(key, value)| KeywordFormatter.new(key, value) }
  parts << KeywordRestFormatter.new(keyword_rest) if keyword_rest
  contents = -> do
    q.seplist(parts) { |part| q.format(part, stackable: false) }
  end

  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



6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
# File 'lib/syntax_tree.rb', line 6686

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



6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
# File 'lib/syntax_tree.rb', line 6720

def to_json(*opts)
  {
    type: :hshptn,
    constant: constant,
    keywords: keywords,
    kwrest: keyword_rest,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end