Class: SyntaxTree::While
Overview
While represents a while loop.
while predicate
end
Instance Attribute Summary collapse
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#predicate ⇒ Object
readonly
- untyped
-
the expression to be checked.
-
#statements ⇒ Object
readonly
- Statements
-
the expressions to be executed.
Attributes inherited from Node
Instance Method Summary collapse
- #accept(visitor) ⇒ Object
- #child_nodes ⇒ Object (also: #deconstruct)
- #deconstruct_keys(_keys) ⇒ Object
- #format(q) ⇒ Object
-
#initialize(predicate:, statements:, location:, comments: []) ⇒ While
constructor
A new instance of While.
Methods inherited from Node
#construct_keys, #pretty_print, #to_json
Constructor Details
#initialize(predicate:, statements:, location:, comments: []) ⇒ While
Returns a new instance of While.
9698 9699 9700 9701 9702 9703 |
# File 'lib/syntax_tree/node.rb', line 9698 def initialize(predicate:, statements:, location:, comments: []) @predicate = predicate @statements = statements @location = location @comments = comments end |
Instance Attribute Details
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
9696 9697 9698 |
# File 'lib/syntax_tree/node.rb', line 9696 def comments @comments end |
#predicate ⇒ Object (readonly)
- untyped
-
the expression to be checked
9690 9691 9692 |
# File 'lib/syntax_tree/node.rb', line 9690 def predicate @predicate end |
#statements ⇒ Object (readonly)
- Statements
-
the expressions to be executed
9693 9694 9695 |
# File 'lib/syntax_tree/node.rb', line 9693 def statements @statements end |
Instance Method Details
#accept(visitor) ⇒ Object
9705 9706 9707 |
# File 'lib/syntax_tree/node.rb', line 9705 def accept(visitor) visitor.visit_while(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
9709 9710 9711 |
# File 'lib/syntax_tree/node.rb', line 9709 def child_nodes [predicate, statements] end |
#deconstruct_keys(_keys) ⇒ Object
9715 9716 9717 9718 9719 9720 9721 9722 |
# File 'lib/syntax_tree/node.rb', line 9715 def deconstruct_keys(_keys) { predicate: predicate, statements: statements, location: location, comments: comments } end |
#format(q) ⇒ Object
9724 9725 9726 9727 9728 9729 9730 9731 9732 9733 9734 9735 9736 9737 |
# File 'lib/syntax_tree/node.rb', line 9724 def format(q) if statements.empty? keyword = "while " q.group do q.text(keyword) q.nest(keyword.length) { q.format(predicate) } q.breakable(force: true) q.text("end") end else LoopFormatter.new("while", self, statements).format(q) end end |