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.



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

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



6721
6722
6723
# File 'lib/syntax_tree/node.rb', line 6721

def comments
  @comments
end

#consequentObject (readonly)

nil | In | Else

the next clause in the chain



6718
6719
6720
# File 'lib/syntax_tree/node.rb', line 6718

def consequent
  @consequent
end

#patternObject (readonly)

Node

the pattern to check against



6712
6713
6714
# File 'lib/syntax_tree/node.rb', line 6712

def pattern
  @pattern
end

#statementsObject (readonly)

Statements

the expressions to execute if the pattern matched



6715
6716
6717
# File 'lib/syntax_tree/node.rb', line 6715

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



6785
6786
6787
6788
# File 'lib/syntax_tree/node.rb', line 6785

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

#accept(visitor) ⇒ Object



6731
6732
6733
# File 'lib/syntax_tree/node.rb', line 6731

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

#child_nodesObject Also known as: deconstruct



6735
6736
6737
# File 'lib/syntax_tree/node.rb', line 6735

def child_nodes
  [pattern, statements, consequent]
end

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



6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
# File 'lib/syntax_tree/node.rb', line 6739

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



6754
6755
6756
6757
6758
6759
6760
6761
6762
# File 'lib/syntax_tree/node.rb', line 6754

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

#format(q) ⇒ Object



6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
# File 'lib/syntax_tree/node.rb', line 6764

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