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.



5686
5687
5688
5689
5690
5691
5692
# File 'lib/syntax_tree/node.rb', line 5686

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



5684
5685
5686
# File 'lib/syntax_tree/node.rb', line 5684

def comments
  @comments
end

#consequentObject (readonly)

nil | In | Else

the next clause in the chain



5681
5682
5683
# File 'lib/syntax_tree/node.rb', line 5681

def consequent
  @consequent
end

#patternObject (readonly)

untyped

the pattern to check against



5675
5676
5677
# File 'lib/syntax_tree/node.rb', line 5675

def pattern
  @pattern
end

#statementsObject (readonly)

Statements

the expressions to execute if the pattern matched



5678
5679
5680
# File 'lib/syntax_tree/node.rb', line 5678

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



5694
5695
5696
# File 'lib/syntax_tree/node.rb', line 5694

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

#child_nodesObject Also known as: deconstruct



5698
5699
5700
# File 'lib/syntax_tree/node.rb', line 5698

def child_nodes
  [pattern, statements, consequent]
end

#deconstruct_keys(_keys) ⇒ Object



5704
5705
5706
5707
5708
5709
5710
5711
5712
# File 'lib/syntax_tree/node.rb', line 5704

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

#format(q) ⇒ Object



5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
# File 'lib/syntax_tree/node.rb', line 5714

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