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.



7071
7072
7073
7074
7075
# File 'lib/syntax_tree/node.rb', line 7071

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



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

def comments
  @comments
end

#statementsObject (readonly)

Statements

the top-level expressions of the program



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

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



7081
7082
7083
# File 'lib/syntax_tree/node.rb', line 7081

def child_nodes
  [statements]
end

#deconstruct_keys(_keys) ⇒ Object



7087
7088
7089
# File 'lib/syntax_tree/node.rb', line 7087

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

#format(q) ⇒ Object



7091
7092
7093
7094
7095
7096
7097
7098
# File 'lib/syntax_tree/node.rb', line 7091

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