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

#pretty_print, #to_json

Constructor Details

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

Returns a new instance of In.



4979
4980
4981
4982
4983
4984
4985
# File 'lib/syntax_tree/node.rb', line 4979

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



4977
4978
4979
# File 'lib/syntax_tree/node.rb', line 4977

def comments
  @comments
end

#consequentObject (readonly)

nil | In | Else

the next clause in the chain



4974
4975
4976
# File 'lib/syntax_tree/node.rb', line 4974

def consequent
  @consequent
end

#patternObject (readonly)

untyped

the pattern to check against



4968
4969
4970
# File 'lib/syntax_tree/node.rb', line 4968

def pattern
  @pattern
end

#statementsObject (readonly)

Statements

the expressions to execute if the pattern matched



4971
4972
4973
# File 'lib/syntax_tree/node.rb', line 4971

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



4987
4988
4989
# File 'lib/syntax_tree/node.rb', line 4987

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

#child_nodesObject Also known as: deconstruct



4991
4992
4993
# File 'lib/syntax_tree/node.rb', line 4991

def child_nodes
  [pattern, statements, consequent]
end

#deconstruct_keys(keys) ⇒ Object



4997
4998
4999
5000
5001
5002
5003
5004
5005
# File 'lib/syntax_tree/node.rb', line 4997

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

#format(q) ⇒ Object



5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
# File 'lib/syntax_tree/node.rb', line 5007

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