Class: SyntaxTree::LBracket

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

Overview

LBracket represents the use of a left bracket, 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:) ⇒ LBracket

Returns a new instance of LBracket.



7396
7397
7398
7399
7400
# File 'lib/syntax_tree/node.rb', line 7396

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



7394
7395
7396
# File 'lib/syntax_tree/node.rb', line 7394

def comments
  @comments
end

#valueObject (readonly)

String

the left bracket



7391
7392
7393
# File 'lib/syntax_tree/node.rb', line 7391

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 LBracket is a child of another node. This means it’s required at initialization time. To make it easier to create LBracket nodes without any specific value, this method provides a default node.



7440
7441
7442
# File 'lib/syntax_tree/node.rb', line 7440

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

Instance Method Details

#===(other) ⇒ Object



7431
7432
7433
# File 'lib/syntax_tree/node.rb', line 7431

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

#accept(visitor) ⇒ Object



7402
7403
7404
# File 'lib/syntax_tree/node.rb', line 7402

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

#child_nodesObject Also known as: deconstruct



7406
7407
7408
# File 'lib/syntax_tree/node.rb', line 7406

def child_nodes
  []
end

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



7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
# File 'lib/syntax_tree/node.rb', line 7410

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

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

#deconstruct_keys(_keys) ⇒ Object



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

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

#format(q) ⇒ Object



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

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