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:, comments: []) ⇒ Program



7275
7276
7277
7278
7279
# File 'lib/syntax_tree/node.rb', line 7275

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



7273
7274
7275
# File 'lib/syntax_tree/node.rb', line 7273

def comments
  @comments
end

#statementsObject (readonly)

Statements

the top-level expressions of the program



7270
7271
7272
# File 'lib/syntax_tree/node.rb', line 7270

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



7281
7282
7283
# File 'lib/syntax_tree/node.rb', line 7281

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

#child_nodesObject Also known as: deconstruct



7285
7286
7287
# File 'lib/syntax_tree/node.rb', line 7285

def child_nodes
  [statements]
end

#deconstruct_keys(_keys) ⇒ Object



7291
7292
7293
# File 'lib/syntax_tree/node.rb', line 7291

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

#format(q) ⇒ Object



7295
7296
7297
7298
7299
7300
7301
7302
# File 'lib/syntax_tree/node.rb', line 7295

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