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

#pretty_print, #to_json

Constructor Details

#initialize(statements:, location:, comments: []) ⇒ Program

Returns a new instance of Program.



6264
6265
6266
6267
6268
# File 'lib/syntax_tree/node.rb', line 6264

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



6262
6263
6264
# File 'lib/syntax_tree/node.rb', line 6262

def comments
  @comments
end

#statementsObject (readonly)

Statements

the top-level expressions of the program



6259
6260
6261
# File 'lib/syntax_tree/node.rb', line 6259

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



6270
6271
6272
# File 'lib/syntax_tree/node.rb', line 6270

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

#child_nodesObject Also known as: deconstruct



6274
6275
6276
# File 'lib/syntax_tree/node.rb', line 6274

def child_nodes
  [statements]
end

#deconstruct_keys(keys) ⇒ Object



6280
6281
6282
# File 'lib/syntax_tree/node.rb', line 6280

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

#format(q) ⇒ Object



6284
6285
6286
6287
6288
6289
6290
6291
# File 'lib/syntax_tree/node.rb', line 6284

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: true) unless statements.body.last.is_a?(EndContent)
end