Class: SyntaxTree::In

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

In represents using the in keyword within the Ruby 2.7+ pattern matching syntax.

case value
in pattern
end

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

#initialize(pattern:, statements:, consequent:, location:) ⇒ In

Returns a new instance of In.



6692
6693
6694
6695
6696
6697
6698
# File 'lib/syntax_tree/node.rb', line 6692

def initialize(pattern:, statements:, consequent:, location:)
  @pattern = pattern
  @statements = statements
  @consequent = consequent
  @location = location
  @comments = []
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



6690
6691
6692
# File 'lib/syntax_tree/node.rb', line 6690

def comments
  @comments
end

#consequentObject (readonly)

nil | In | Else

the next clause in the chain



6687
6688
6689
# File 'lib/syntax_tree/node.rb', line 6687

def consequent
  @consequent
end

#patternObject (readonly)

Node

the pattern to check against



6681
6682
6683
# File 'lib/syntax_tree/node.rb', line 6681

def pattern
  @pattern
end

#statementsObject (readonly)

Statements

the expressions to execute if the pattern matched



6684
6685
6686
# File 'lib/syntax_tree/node.rb', line 6684

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



6754
6755
6756
6757
# File 'lib/syntax_tree/node.rb', line 6754

def ===(other)
  other.is_a?(In) && pattern === other.pattern &&
    statements === other.statements && consequent === other.consequent
end

#accept(visitor) ⇒ Object



6700
6701
6702
# File 'lib/syntax_tree/node.rb', line 6700

def accept(visitor)
  visitor.visit_in(self)
end

#child_nodesObject Also known as: deconstruct



6704
6705
6706
# File 'lib/syntax_tree/node.rb', line 6704

def child_nodes
  [pattern, statements, consequent]
end

#copy(pattern: nil, statements: nil, consequent: nil, location: nil) ⇒ Object



6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
# File 'lib/syntax_tree/node.rb', line 6708

def copy(pattern: nil, statements: nil, consequent: nil, location: nil)
  node =
    In.new(
      pattern: pattern || self.pattern,
      statements: statements || self.statements,
      consequent: consequent || self.consequent,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



6723
6724
6725
6726
6727
6728
6729
6730
6731
# File 'lib/syntax_tree/node.rb', line 6723

def deconstruct_keys(_keys)
  {
    pattern: pattern,
    statements: statements,
    consequent: consequent,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
# File 'lib/syntax_tree/node.rb', line 6733

def format(q)
  keyword = "in "

  q.group do
    q.text(keyword)
    q.nest(keyword.length) { q.format(pattern) }

    unless statements.empty?
      q.indent do
        q.breakable_force
        q.format(statements)
      end
    end

    if consequent
      q.breakable_force
      q.format(consequent)
    end
  end
end