Class: SyntaxTree::LParen

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

Overview

LParen represents the use of a left parenthesis, i.e., (.

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Node

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

Constructor Details

#initialize(value:, location:) ⇒ LParen

Returns a new instance of LParen.



7427
7428
7429
7430
7431
# File 'lib/syntax_tree/node.rb', line 7427

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



7425
7426
7427
# File 'lib/syntax_tree/node.rb', line 7425

def comments
  @comments
end

#valueObject (readonly)

String

the left parenthesis



7422
7423
7424
# File 'lib/syntax_tree/node.rb', line 7422

def value
  @value
end

Class Method Details

.defaultObject

Because some nodes keep around a ( token so that comments can be attached to it if they occur in the source, oftentimes an LParen is a child of another node. This means it’s required at initialization time. To make it easier to create LParen nodes without any specific value, this method provides a default node.



7471
7472
7473
# File 'lib/syntax_tree/node.rb', line 7471

def self.default
  new(value: "(", location: Location.default)
end

Instance Method Details

#===(other) ⇒ Object



7462
7463
7464
# File 'lib/syntax_tree/node.rb', line 7462

def ===(other)
  other.is_a?(LParen) && value === other.value
end

#accept(visitor) ⇒ Object



7433
7434
7435
# File 'lib/syntax_tree/node.rb', line 7433

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

#child_nodesObject Also known as: deconstruct



7437
7438
7439
# File 'lib/syntax_tree/node.rb', line 7437

def child_nodes
  []
end

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



7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
# File 'lib/syntax_tree/node.rb', line 7441

def copy(value: nil, location: nil)
  node =
    LParen.new(
      value: value || self.value,
      location: location || self.location
    )

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

#deconstruct_keys(_keys) ⇒ Object



7454
7455
7456
# File 'lib/syntax_tree/node.rb', line 7454

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

#format(q) ⇒ Object



7458
7459
7460
# File 'lib/syntax_tree/node.rb', line 7458

def format(q)
  q.text(value)
end