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.



5851
5852
5853
5854
5855
5856
5857
# File 'lib/syntax_tree/node.rb', line 5851

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



5849
5850
5851
# File 'lib/syntax_tree/node.rb', line 5849

def comments
  @comments
end

#consequentObject (readonly)

nil | In | Else

the next clause in the chain



5846
5847
5848
# File 'lib/syntax_tree/node.rb', line 5846

def consequent
  @consequent
end

#patternObject (readonly)

untyped

the pattern to check against



5840
5841
5842
# File 'lib/syntax_tree/node.rb', line 5840

def pattern
  @pattern
end

#statementsObject (readonly)

Statements

the expressions to execute if the pattern matched



5843
5844
5845
# File 'lib/syntax_tree/node.rb', line 5843

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



5859
5860
5861
# File 'lib/syntax_tree/node.rb', line 5859

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

#child_nodesObject Also known as: deconstruct



5863
5864
5865
# File 'lib/syntax_tree/node.rb', line 5863

def child_nodes
  [pattern, statements, consequent]
end

#deconstruct_keys(_keys) ⇒ Object



5869
5870
5871
5872
5873
5874
5875
5876
5877
# File 'lib/syntax_tree/node.rb', line 5869

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

#format(q) ⇒ Object



5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
# File 'lib/syntax_tree/node.rb', line 5879

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