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.
9855 9856 9857 9858 9859 9860 |
# File 'lib/syntax_tree/node.rb', line 9855 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
9853 9854 9855 |
# File 'lib/syntax_tree/node.rb', line 9853 def comments @comments end |
#predicate ⇒ Object (readonly)
- untyped
-
the expression to be checked
9850 9851 9852 |
# File 'lib/syntax_tree/node.rb', line 9850 def predicate @predicate end |
#statement ⇒ Object (readonly)
- untyped
-
the expression to be executed
9847 9848 9849 |
# File 'lib/syntax_tree/node.rb', line 9847 def statement @statement end |
Instance Method Details
#accept(visitor) ⇒ Object
9862 9863 9864 |
# File 'lib/syntax_tree/node.rb', line 9862 def accept(visitor) visitor.visit_while_mod(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
9866 9867 9868 |
# File 'lib/syntax_tree/node.rb', line 9866 def child_nodes [statement, predicate] end |
#deconstruct_keys(_keys) ⇒ Object
9872 9873 9874 9875 9876 9877 9878 9879 |
# File 'lib/syntax_tree/node.rb', line 9872 def deconstruct_keys(_keys) { statement: statement, predicate: predicate, location: location, comments: comments } end |
#format(q) ⇒ Object
9881 9882 9883 9884 9885 9886 9887 9888 9889 9890 9891 9892 9893 9894 9895 9896 9897 9898 9899 9900 9901 9902 9903 |
# File 'lib/syntax_tree/node.rb', line 9881 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 |