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.



8887
8888
8889
8890
8891
# File 'lib/syntax_tree.rb', line 8887

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



8885
8886
8887
# File 'lib/syntax_tree.rb', line 8885

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



8882
8883
8884
# File 'lib/syntax_tree.rb', line 8882

def location
  @location
end

#statementsObject (readonly)

Statements

the top-level expressions of the program



8879
8880
8881
# File 'lib/syntax_tree.rb', line 8879

def statements
  @statements
end

Instance Method Details

#child_nodesObject



8893
8894
8895
# File 'lib/syntax_tree.rb', line 8893

def child_nodes
  [statements]
end

#format(q) ⇒ Object



8897
8898
8899
8900
# File 'lib/syntax_tree.rb', line 8897

def format(q)
  q.format(statements)
  q.breakable(force: true)
end

#pretty_print(q) ⇒ Object



8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
# File 'lib/syntax_tree.rb', line 8902

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



8913
8914
8915
8916
8917
8918
8919
8920
8921
# File 'lib/syntax_tree.rb', line 8913

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