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

Constructor Details

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

Returns a new instance of Program.



7898
7899
7900
7901
7902
# File 'lib/syntax_tree/node.rb', line 7898

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



7896
7897
7898
# File 'lib/syntax_tree/node.rb', line 7896

def comments
  @comments
end

#statementsObject (readonly)

Statements

the top-level expressions of the program



7893
7894
7895
# File 'lib/syntax_tree/node.rb', line 7893

def statements
  @statements
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



7904
7905
7906
# File 'lib/syntax_tree/node.rb', line 7904

def child_nodes
  [statements]
end

#deconstruct_keys(keys) ⇒ Object



7910
7911
7912
# File 'lib/syntax_tree/node.rb', line 7910

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

#format(q) ⇒ Object



7914
7915
7916
7917
7918
7919
7920
7921
# File 'lib/syntax_tree/node.rb', line 7914

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

#pretty_print(q) ⇒ Object



7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
# File 'lib/syntax_tree/node.rb', line 7923

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("program")

    q.breakable
    q.pp(statements)

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



7934
7935
7936
7937
7938
7939
7940
7941
7942
# File 'lib/syntax_tree/node.rb', line 7934

def to_json(*opts)
  {
    type: :program,
    stmts: statements,
    comments: comments,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end