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.



9496
9497
9498
9499
9500
# File 'lib/syntax_tree.rb', line 9496

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



9494
9495
9496
# File 'lib/syntax_tree.rb', line 9494

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



9491
9492
9493
# File 'lib/syntax_tree.rb', line 9491

def location
  @location
end

#statementsObject (readonly)

Statements

the top-level expressions of the program



9488
9489
9490
# File 'lib/syntax_tree.rb', line 9488

def statements
  @statements
end

Instance Method Details

#child_nodesObject



9502
9503
9504
# File 'lib/syntax_tree.rb', line 9502

def child_nodes
  [statements]
end

#format(q) ⇒ Object



9506
9507
9508
9509
9510
9511
9512
9513
# File 'lib/syntax_tree.rb', line 9506

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



9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
# File 'lib/syntax_tree.rb', line 9515

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



9526
9527
9528
9529
9530
9531
9532
9533
9534
# File 'lib/syntax_tree.rb', line 9526

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