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.



6744
6745
6746
6747
6748
6749
6750
# File 'lib/syntax_tree/node.rb', line 6744

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



6742
6743
6744
# File 'lib/syntax_tree/node.rb', line 6742

def comments
  @comments
end

#consequentObject (readonly)

nil | In | Else

the next clause in the chain



6739
6740
6741
# File 'lib/syntax_tree/node.rb', line 6739

def consequent
  @consequent
end

#patternObject (readonly)

Node

the pattern to check against



6733
6734
6735
# File 'lib/syntax_tree/node.rb', line 6733

def pattern
  @pattern
end

#statementsObject (readonly)

Statements

the expressions to execute if the pattern matched



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

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



6809
6810
6811
6812
# File 'lib/syntax_tree/node.rb', line 6809

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

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



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

def child_nodes
  [pattern, statements, consequent]
end

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



6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
# File 'lib/syntax_tree/node.rb', line 6760

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



6775
6776
6777
6778
6779
6780
6781
6782
6783
# File 'lib/syntax_tree/node.rb', line 6775

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

#format(q) ⇒ Object



6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
# File 'lib/syntax_tree/node.rb', line 6785

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