Class: SyntaxTree::PinnedBegin

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

PinnedBegin represents a pinning a nested statement within pattern matching.

case value
in ^(statement)
end

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

#initialize(statement:, location:) ⇒ PinnedBegin

Returns a new instance of PinnedBegin.



1984
1985
1986
1987
1988
# File 'lib/syntax_tree/node.rb', line 1984

def initialize(statement:, location:)
  @statement = statement
  @location = location
  @comments = []
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



1982
1983
1984
# File 'lib/syntax_tree/node.rb', line 1982

def comments
  @comments
end

#statementObject (readonly)

Node

the expression being pinned



1979
1980
1981
# File 'lib/syntax_tree/node.rb', line 1979

def statement
  @statement
end

Instance Method Details

#===(other) ⇒ Object



2029
2030
2031
# File 'lib/syntax_tree/node.rb', line 2029

def ===(other)
  other.is_a?(PinnedBegin) && statement === other.statement
end

#accept(visitor) ⇒ Object



1990
1991
1992
# File 'lib/syntax_tree/node.rb', line 1990

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

#child_nodesObject Also known as: deconstruct



1994
1995
1996
# File 'lib/syntax_tree/node.rb', line 1994

def child_nodes
  [statement]
end

#copy(statement: nil, location: nil) ⇒ Object



1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
# File 'lib/syntax_tree/node.rb', line 1998

def copy(statement: nil, location: nil)
  node =
    PinnedBegin.new(
      statement: statement || self.statement,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



2011
2012
2013
# File 'lib/syntax_tree/node.rb', line 2011

def deconstruct_keys(_keys)
  { statement: statement, location: location, comments: comments }
end

#format(q) ⇒ Object



2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
# File 'lib/syntax_tree/node.rb', line 2015

def format(q)
  q.group do
    q.text("^(")
    q.nest(1) do
      q.indent do
        q.breakable_empty
        q.format(statement)
      end
      q.breakable_empty
      q.text(")")
    end
  end
end