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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Program.



8183
8184
8185
8186
8187
# File 'lib/syntax_tree/node.rb', line 8183

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



8181
8182
8183
# File 'lib/syntax_tree/node.rb', line 8181

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



8178
8179
8180
# File 'lib/syntax_tree/node.rb', line 8178

def location
  @location
end

#statementsObject (readonly)

Statements

the top-level expressions of the program



8175
8176
8177
# File 'lib/syntax_tree/node.rb', line 8175

def statements
  @statements
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



8189
8190
8191
# File 'lib/syntax_tree/node.rb', line 8189

def child_nodes
  [statements]
end

#deconstruct_keys(keys) ⇒ Object



8195
8196
8197
# File 'lib/syntax_tree/node.rb', line 8195

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

#format(q) ⇒ Object



8199
8200
8201
8202
8203
8204
8205
8206
# File 'lib/syntax_tree/node.rb', line 8199

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



8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
# File 'lib/syntax_tree/node.rb', line 8208

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



8219
8220
8221
8222
8223
8224
8225
8226
8227
# File 'lib/syntax_tree/node.rb', line 8219

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