Class: SyntaxTree::WhileMod
Overview
WhileMod represents the modifier form of a while loop.
expression while predicate
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.
-
#statement ⇒ Object
readonly
- untyped
-
the expression 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(statement:, predicate:, location:, comments: []) ⇒ WhileMod
constructor
A new instance of WhileMod.
Methods inherited from Node
#construct_keys, #pretty_print, #to_json
Constructor Details
#initialize(statement:, predicate:, location:, comments: []) ⇒ WhileMod
Returns a new instance of WhileMod.
9841 9842 9843 9844 9845 9846 |
# File 'lib/syntax_tree/node.rb', line 9841 def initialize(statement:, predicate:, location:, comments: []) @statement = statement @predicate = predicate @location = location @comments = comments end |
Instance Attribute Details
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
9839 9840 9841 |
# File 'lib/syntax_tree/node.rb', line 9839 def comments @comments end |
#predicate ⇒ Object (readonly)
- untyped
-
the expression to be checked
9836 9837 9838 |
# File 'lib/syntax_tree/node.rb', line 9836 def predicate @predicate end |
#statement ⇒ Object (readonly)
- untyped
-
the expression to be executed
9833 9834 9835 |
# File 'lib/syntax_tree/node.rb', line 9833 def statement @statement end |
Instance Method Details
#accept(visitor) ⇒ Object
9848 9849 9850 |
# File 'lib/syntax_tree/node.rb', line 9848 def accept(visitor) visitor.visit_while_mod(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
9852 9853 9854 |
# File 'lib/syntax_tree/node.rb', line 9852 def child_nodes [statement, predicate] end |
#deconstruct_keys(_keys) ⇒ Object
9858 9859 9860 9861 9862 9863 9864 9865 |
# File 'lib/syntax_tree/node.rb', line 9858 def deconstruct_keys(_keys) { statement: statement, predicate: predicate, location: location, comments: comments } end |
#format(q) ⇒ Object
9867 9868 9869 9870 9871 9872 9873 9874 9875 9876 9877 9878 9879 9880 9881 9882 9883 9884 9885 9886 9887 9888 9889 |
# File 'lib/syntax_tree/node.rb', line 9867 def format(q) # If we're in the modifier form and we're modifying a `begin`, then this # is a special case where we need to explicitly use the modifier form # because otherwise the semantic meaning changes. This looks like: # # begin # foo # end while bar # # Also, if the statement of the modifier includes an assignment, then we # can't know for certain that it won't impact the predicate, so we need to # force it to stay as it is. This looks like: # # foo = bar while foo # if statement.is_a?(Begin) || ContainsAssignment.call(statement) q.format(statement) q.text(" while ") q.format(predicate) else LoopFormatter.new("while", self, statement).format(q) end end |