Class: SyntaxTree::Program
Overview
Program represents the overall syntax tree.
Instance Attribute Summary collapse
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#location ⇒ Object
readonly
- Location
-
the location of this node.
-
#statements ⇒ Object
readonly
- Statements
-
the top-level expressions of the program.
Instance Method Summary collapse
- #child_nodes ⇒ Object (also: #deconstruct)
- #deconstruct_keys(keys) ⇒ Object
- #format(q) ⇒ Object
-
#initialize(statements:, location:, comments: []) ⇒ Program
constructor
A new instance of Program.
- #pretty_print(q) ⇒ Object
- #to_json(*opts) ⇒ Object
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
#comments ⇒ Object (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 |
#location ⇒ Object (readonly)
- Location
-
the location of this node
8178 8179 8180 |
# File 'lib/syntax_tree/node.rb', line 8178 def location @location end |
#statements ⇒ Object (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_nodes ⇒ Object 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 |