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, #pretty_print, #to_json

Constructor Details

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

Returns a new instance of In.



5621
5622
5623
5624
5625
5626
5627
# File 'lib/syntax_tree/node.rb', line 5621

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

Instance Attribute Details

#comments ⇒ Object (readonly)

[Array[ Comment | EmbDoc ]] the comments attached to this node



5619
5620
5621
# File 'lib/syntax_tree/node.rb', line 5619

def comments
  @comments
end

#consequent ⇒ Object (readonly)

[nil | In | Else] the next clause in the chain



5616
5617
5618
# File 'lib/syntax_tree/node.rb', line 5616

def consequent
  @consequent
end

#pattern ⇒ Object (readonly)

[untyped] the pattern to check against



5610
5611
5612
# File 'lib/syntax_tree/node.rb', line 5610

def pattern
  @pattern
end

#statements ⇒ Object (readonly)

[Statements] the expressions to execute if the pattern matched



5613
5614
5615
# File 'lib/syntax_tree/node.rb', line 5613

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



5629
5630
5631
# File 'lib/syntax_tree/node.rb', line 5629

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

#child_nodes ⇒ Object Also known as: deconstruct



5633
5634
5635
# File 'lib/syntax_tree/node.rb', line 5633

def child_nodes
  [pattern, statements, consequent]
end

#deconstruct_keys(_keys) ⇒ Object



5639
5640
5641
5642
5643
5644
5645
5646
5647
# File 'lib/syntax_tree/node.rb', line 5639

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

#format(q) ⇒ Object



5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
# File 'lib/syntax_tree/node.rb', line 5649

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: true)
        q.format(statements)
      end
    end

    if consequent
      q.breakable(force: true)
      q.format(consequent)
    end
  end
end