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:) ⇒ In

Returns a new instance of In.



6620
6621
6622
6623
6624
6625
6626
# File 'lib/syntax_tree/node.rb', line 6620

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



6618
6619
6620
# File 'lib/syntax_tree/node.rb', line 6618

def comments
  @comments
end

#consequentObject (readonly)

nil | In | Else

the next clause in the chain



6615
6616
6617
# File 'lib/syntax_tree/node.rb', line 6615

def consequent
  @consequent
end

#patternObject (readonly)

untyped

the pattern to check against



6609
6610
6611
# File 'lib/syntax_tree/node.rb', line 6609

def pattern
  @pattern
end

#statementsObject (readonly)

Statements

the expressions to execute if the pattern matched



6612
6613
6614
# File 'lib/syntax_tree/node.rb', line 6612

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



6682
6683
6684
6685
# File 'lib/syntax_tree/node.rb', line 6682

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

#accept(visitor) ⇒ Object



6628
6629
6630
# File 'lib/syntax_tree/node.rb', line 6628

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

#child_nodesObject Also known as: deconstruct



6632
6633
6634
# File 'lib/syntax_tree/node.rb', line 6632

def child_nodes
  [pattern, statements, consequent]
end

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



6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
# File 'lib/syntax_tree/node.rb', line 6636

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



6651
6652
6653
6654
6655
6656
6657
6658
6659
# File 'lib/syntax_tree/node.rb', line 6651

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

#format(q) ⇒ Object



6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
# File 'lib/syntax_tree/node.rb', line 6661

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