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.
9799 9800 9801 9802 9803 9804 |
# File 'lib/syntax_tree/node.rb', line 9799 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
9797 9798 9799 |
# File 'lib/syntax_tree/node.rb', line 9797 def comments @comments end |
#predicate ⇒ Object (readonly)
- untyped
-
the expression to be checked
9791 9792 9793 |
# File 'lib/syntax_tree/node.rb', line 9791 def predicate @predicate end |
#statements ⇒ Object (readonly)
- Statements
-
the expressions to be executed
9794 9795 9796 |
# File 'lib/syntax_tree/node.rb', line 9794 def statements @statements end |
Instance Method Details
#accept(visitor) ⇒ Object
9806 9807 9808 |
# File 'lib/syntax_tree/node.rb', line 9806 def accept(visitor) visitor.visit_while(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
9810 9811 9812 |
# File 'lib/syntax_tree/node.rb', line 9810 def child_nodes [predicate, statements] end |
#deconstruct_keys(_keys) ⇒ Object
9816 9817 9818 9819 9820 9821 9822 9823 |
# File 'lib/syntax_tree/node.rb', line 9816 def deconstruct_keys(_keys) { predicate: predicate, statements: statements, location: location, comments: comments } end |
#format(q) ⇒ Object
9825 9826 9827 9828 9829 9830 9831 9832 9833 9834 9835 9836 9837 9838 |
# File 'lib/syntax_tree/node.rb', line 9825 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 |