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

Returns a new instance of Program.



8624
8625
8626
8627
8628
# File 'lib/syntax_tree/node.rb', line 8624

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



8622
8623
8624
# File 'lib/syntax_tree/node.rb', line 8622

def comments
  @comments
end

#statementsObject (readonly)

Statements

the top-level expressions of the program



8619
8620
8621
# File 'lib/syntax_tree/node.rb', line 8619

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



8664
8665
8666
# File 'lib/syntax_tree/node.rb', line 8664

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

#accept(visitor) ⇒ Object



8630
8631
8632
# File 'lib/syntax_tree/node.rb', line 8630

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

#child_nodesObject Also known as: deconstruct



8634
8635
8636
# File 'lib/syntax_tree/node.rb', line 8634

def child_nodes
  [statements]
end

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



8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
# File 'lib/syntax_tree/node.rb', line 8638

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



8651
8652
8653
# File 'lib/syntax_tree/node.rb', line 8651

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

#format(q) ⇒ Object



8655
8656
8657
8658
8659
8660
8661
8662
# File 'lib/syntax_tree/node.rb', line 8655

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