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.



8608
8609
8610
8611
8612
# File 'lib/syntax_tree/node.rb', line 8608

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



8606
8607
8608
# File 'lib/syntax_tree/node.rb', line 8606

def comments
  @comments
end

#statementsObject (readonly)

Statements

the top-level expressions of the program



8603
8604
8605
# File 'lib/syntax_tree/node.rb', line 8603

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



8648
8649
8650
# File 'lib/syntax_tree/node.rb', line 8648

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

#accept(visitor) ⇒ Object



8614
8615
8616
# File 'lib/syntax_tree/node.rb', line 8614

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

#child_nodesObject Also known as: deconstruct



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

def child_nodes
  [statements]
end

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



8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
# File 'lib/syntax_tree/node.rb', line 8622

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



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

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

#format(q) ⇒ Object



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

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