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

Returns a new instance of Program.



7068
7069
7070
7071
7072
# File 'lib/syntax_tree/node.rb', line 7068

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



7066
7067
7068
# File 'lib/syntax_tree/node.rb', line 7066

def comments
  @comments
end

#statementsObject (readonly)

Statements

the top-level expressions of the program



7063
7064
7065
# File 'lib/syntax_tree/node.rb', line 7063

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



7074
7075
7076
# File 'lib/syntax_tree/node.rb', line 7074

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

#child_nodesObject Also known as: deconstruct



7078
7079
7080
# File 'lib/syntax_tree/node.rb', line 7078

def child_nodes
  [statements]
end

#deconstruct_keys(_keys) ⇒ Object



7084
7085
7086
# File 'lib/syntax_tree/node.rb', line 7084

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

#format(q) ⇒ Object



7088
7089
7090
7091
7092
7093
7094
7095
# File 'lib/syntax_tree/node.rb', line 7088

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