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.



6762
6763
6764
6765
6766
6767
6768
# File 'lib/syntax_tree/node.rb', line 6762

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



6760
6761
6762
# File 'lib/syntax_tree/node.rb', line 6760

def comments
  @comments
end

#consequentObject (readonly)

nil | In | Else

the next clause in the chain



6757
6758
6759
# File 'lib/syntax_tree/node.rb', line 6757

def consequent
  @consequent
end

#patternObject (readonly)

Node

the pattern to check against



6751
6752
6753
# File 'lib/syntax_tree/node.rb', line 6751

def pattern
  @pattern
end

#statementsObject (readonly)

Statements

the expressions to execute if the pattern matched



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

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



6827
6828
6829
6830
# File 'lib/syntax_tree/node.rb', line 6827

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

#accept(visitor) ⇒ Object



6770
6771
6772
# File 'lib/syntax_tree/node.rb', line 6770

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

#child_nodesObject Also known as: deconstruct



6774
6775
6776
# File 'lib/syntax_tree/node.rb', line 6774

def child_nodes
  [pattern, statements, consequent]
end

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



6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
# File 'lib/syntax_tree/node.rb', line 6778

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



6793
6794
6795
6796
6797
6798
6799
6800
6801
# File 'lib/syntax_tree/node.rb', line 6793

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

#format(q) ⇒ Object



6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
# File 'lib/syntax_tree/node.rb', line 6803

def format(q)
  keyword = "in "
  pattern = self.pattern
  consequent = self.consequent

  q.group do
    q.text(keyword)
    q.nest(keyword.length) { q.format(pattern) }
    q.text(" then") if pattern.is_a?(RangeNode) && pattern.right.nil?

    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