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.



9364
9365
9366
9367
9368
# File 'lib/syntax_tree.rb', line 9364

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



9362
9363
9364
# File 'lib/syntax_tree.rb', line 9362

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



9359
9360
9361
# File 'lib/syntax_tree.rb', line 9359

def location
  @location
end

#statementsObject (readonly)

Statements

the top-level expressions of the program



9356
9357
9358
# File 'lib/syntax_tree.rb', line 9356

def statements
  @statements
end

Instance Method Details

#child_nodesObject



9370
9371
9372
# File 'lib/syntax_tree.rb', line 9370

def child_nodes
  [statements]
end

#format(q) ⇒ Object



9374
9375
9376
9377
9378
9379
9380
9381
# File 'lib/syntax_tree.rb', line 9374

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



9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
# File 'lib/syntax_tree.rb', line 9383

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



9394
9395
9396
9397
9398
9399
9400
9401
9402
# File 'lib/syntax_tree.rb', line 9394

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