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.
-
#location ⇒ Object
readonly
- Location
-
the location of this node.
-
#predicate ⇒ Object
readonly
- untyped
-
the expression to be checked.
-
#statement ⇒ Object
readonly
- untyped
-
the expression to be executed.
Instance Method Summary collapse
- #child_nodes ⇒ Object (also: #deconstruct)
- #deconstruct_keys(keys) ⇒ Object
- #format(q) ⇒ Object
-
#initialize(statement:, predicate:, location:, comments: []) ⇒ WhileMod
constructor
A new instance of WhileMod.
- #pretty_print(q) ⇒ Object
- #to_json(*opts) ⇒ Object
Constructor Details
#initialize(statement:, predicate:, location:, comments: []) ⇒ WhileMod
Returns a new instance of WhileMod.
11506 11507 11508 11509 11510 11511 |
# File 'lib/syntax_tree/node.rb', line 11506 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
11504 11505 11506 |
# File 'lib/syntax_tree/node.rb', line 11504 def comments @comments end |
#location ⇒ Object (readonly)
- Location
-
the location of this node
11501 11502 11503 |
# File 'lib/syntax_tree/node.rb', line 11501 def location @location end |
#predicate ⇒ Object (readonly)
- untyped
-
the expression to be checked
11498 11499 11500 |
# File 'lib/syntax_tree/node.rb', line 11498 def predicate @predicate end |
#statement ⇒ Object (readonly)
- untyped
-
the expression to be executed
11495 11496 11497 |
# File 'lib/syntax_tree/node.rb', line 11495 def statement @statement end |
Instance Method Details
#child_nodes ⇒ Object Also known as: deconstruct
11513 11514 11515 |
# File 'lib/syntax_tree/node.rb', line 11513 def child_nodes [statement, predicate] end |
#deconstruct_keys(keys) ⇒ Object
11519 11520 11521 11522 11523 11524 11525 11526 |
# File 'lib/syntax_tree/node.rb', line 11519 def deconstruct_keys(keys) { statement: statement, predicate: predicate, location: location, comments: comments } end |
#format(q) ⇒ Object
11528 11529 11530 11531 11532 11533 11534 11535 11536 11537 11538 11539 11540 11541 11542 11543 11544 11545 11546 11547 11548 11549 11550 |
# File 'lib/syntax_tree/node.rb', line 11528 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 |
#pretty_print(q) ⇒ Object
11552 11553 11554 11555 11556 11557 11558 11559 11560 11561 11562 11563 11564 |
# File 'lib/syntax_tree/node.rb', line 11552 def pretty_print(q) q.group(2, "(", ")") do q.text("while_mod") q.breakable q.pp(statement) q.breakable q.pp(predicate) q.pp(Comment::List.new(comments)) end end |
#to_json(*opts) ⇒ Object
11566 11567 11568 11569 11570 11571 11572 11573 11574 |
# File 'lib/syntax_tree/node.rb', line 11566 def to_json(*opts) { type: :while_mod, stmt: statement, pred: predicate, loc: location, cmts: comments }.to_json(*opts) end |