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.



6907
6908
6909
6910
6911
# File 'lib/syntax_tree/node.rb', line 6907

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



6905
6906
6907
# File 'lib/syntax_tree/node.rb', line 6905

def comments
  @comments
end

#statementsObject (readonly)

Statements

the top-level expressions of the program



6902
6903
6904
# File 'lib/syntax_tree/node.rb', line 6902

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



6913
6914
6915
# File 'lib/syntax_tree/node.rb', line 6913

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

#child_nodesObject Also known as: deconstruct



6917
6918
6919
# File 'lib/syntax_tree/node.rb', line 6917

def child_nodes
  [statements]
end

#deconstruct_keys(_keys) ⇒ Object



6923
6924
6925
# File 'lib/syntax_tree/node.rb', line 6923

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

#format(q) ⇒ Object



6927
6928
6929
6930
6931
6932
6933
6934
# File 'lib/syntax_tree/node.rb', line 6927

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