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, #pretty_print, #to_json

Constructor Details

#initialize(statements:, location:) ⇒ Program

Returns a new instance of Program.



8495
8496
8497
8498
8499
# File 'lib/syntax_tree/node.rb', line 8495

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



8493
8494
8495
# File 'lib/syntax_tree/node.rb', line 8493

def comments
  @comments
end

#statementsObject (readonly)

Statements

the top-level expressions of the program



8490
8491
8492
# File 'lib/syntax_tree/node.rb', line 8490

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



8535
8536
8537
# File 'lib/syntax_tree/node.rb', line 8535

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

#accept(visitor) ⇒ Object



8501
8502
8503
# File 'lib/syntax_tree/node.rb', line 8501

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

#child_nodesObject Also known as: deconstruct



8505
8506
8507
# File 'lib/syntax_tree/node.rb', line 8505

def child_nodes
  [statements]
end

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



8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
# File 'lib/syntax_tree/node.rb', line 8509

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



8522
8523
8524
# File 'lib/syntax_tree/node.rb', line 8522

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

#format(q) ⇒ Object



8526
8527
8528
8529
8530
8531
8532
8533
# File 'lib/syntax_tree/node.rb', line 8526

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