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.



8460
8461
8462
8463
8464
# File 'lib/syntax_tree/node.rb', line 8460

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



8458
8459
8460
# File 'lib/syntax_tree/node.rb', line 8458

def comments
  @comments
end

#statementsObject (readonly)

Statements

the top-level expressions of the program



8455
8456
8457
# File 'lib/syntax_tree/node.rb', line 8455

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



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

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

#accept(visitor) ⇒ Object



8466
8467
8468
# File 'lib/syntax_tree/node.rb', line 8466

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

#child_nodesObject Also known as: deconstruct



8470
8471
8472
# File 'lib/syntax_tree/node.rb', line 8470

def child_nodes
  [statements]
end

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



8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
# File 'lib/syntax_tree/node.rb', line 8474

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



8487
8488
8489
# File 'lib/syntax_tree/node.rb', line 8487

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

#format(q) ⇒ Object



8491
8492
8493
8494
8495
8496
8497
8498
# File 'lib/syntax_tree/node.rb', line 8491

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