Class: SyntaxTree::Program

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree.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.



9288
9289
9290
9291
9292
# File 'lib/syntax_tree.rb', line 9288

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



9286
9287
9288
# File 'lib/syntax_tree.rb', line 9286

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



9283
9284
9285
# File 'lib/syntax_tree.rb', line 9283

def location
  @location
end

#statementsObject (readonly)

Statements

the top-level expressions of the program



9280
9281
9282
# File 'lib/syntax_tree.rb', line 9280

def statements
  @statements
end

Instance Method Details

#child_nodesObject



9294
9295
9296
# File 'lib/syntax_tree.rb', line 9294

def child_nodes
  [statements]
end

#format(q) ⇒ Object



9298
9299
9300
9301
9302
9303
9304
9305
# File 'lib/syntax_tree.rb', line 9298

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



9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
# File 'lib/syntax_tree.rb', line 9307

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



9318
9319
9320
9321
9322
9323
9324
9325
9326
# File 'lib/syntax_tree.rb', line 9318

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