Class: SyntaxTree::Program

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

Overview

Program represents the overall syntax tree.

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(statements:, location:) ⇒ Program



8560
8561
8562
8563
8564
# File 'lib/syntax_tree/node.rb', line 8560

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



8558
8559
8560
# File 'lib/syntax_tree/node.rb', line 8558

def comments
  @comments
end

#statementsObject (readonly)

Statements

the top-level expressions of the program



8555
8556
8557
# File 'lib/syntax_tree/node.rb', line 8555

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



8600
8601
8602
# File 'lib/syntax_tree/node.rb', line 8600

def ===(other)
  other.is_a?(Program) && statements === other.statements
end

#accept(visitor) ⇒ Object



8566
8567
8568
# File 'lib/syntax_tree/node.rb', line 8566

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

#child_nodesObject Also known as: deconstruct



8570
8571
8572
# File 'lib/syntax_tree/node.rb', line 8570

def child_nodes
  [statements]
end

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



8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
# File 'lib/syntax_tree/node.rb', line 8574

def copy(statements: nil, location: nil)
  node =
    Program.new(
      statements: statements || self.statements,
      location: location || self.location
    )

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

#deconstruct_keys(_keys) ⇒ Object



8587
8588
8589
# File 'lib/syntax_tree/node.rb', line 8587

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

#format(q) ⇒ Object



8591
8592
8593
8594
8595
8596
8597
8598
# File 'lib/syntax_tree/node.rb', line 8591

def format(q)
  q.format(statements)

  # We're going to put a newline on the end so that it always has one unless
  # it ends with the special __END__ syntax. In that case we want to
  # replicate the text exactly so we will just let it be.
  q.breakable_force unless statements.body.last.is_a?(EndContent)
end