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.



1965
1966
1967
1968
1969
# File 'lib/syntax_tree/node.rb', line 1965

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



1963
1964
1965
# File 'lib/syntax_tree/node.rb', line 1963

def comments
  @comments
end

#statementObject (readonly)

Node

the expression being pinned



1960
1961
1962
# File 'lib/syntax_tree/node.rb', line 1960

def statement
  @statement
end

Instance Method Details

#===(other) ⇒ Object



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

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

#accept(visitor) ⇒ Object



1971
1972
1973
# File 'lib/syntax_tree/node.rb', line 1971

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

#child_nodesObject Also known as: deconstruct



1975
1976
1977
# File 'lib/syntax_tree/node.rb', line 1975

def child_nodes
  [statement]
end

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



1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
# File 'lib/syntax_tree/node.rb', line 1979

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



1992
1993
1994
# File 'lib/syntax_tree/node.rb', line 1992

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

#format(q) ⇒ Object



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

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