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
- #child_nodes ⇒ Object (also: #deconstruct)
- #deconstruct_keys(keys) ⇒ Object
- #format(q) ⇒ Object
-
#initialize(predicate:, statements:, location:, comments: []) ⇒ While
constructor
A new instance of While.
- #pretty_print(q) ⇒ Object
- #to_json(*opts) ⇒ Object
Constructor Details
#initialize(predicate:, statements:, location:, comments: []) ⇒ While
Returns a new instance of While.
10980 10981 10982 10983 10984 10985 |
# File 'lib/syntax_tree/node.rb', line 10980 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
10978 10979 10980 |
# File 'lib/syntax_tree/node.rb', line 10978 def comments @comments end |
#predicate ⇒ Object (readonly)
- untyped
-
the expression to be checked
10972 10973 10974 |
# File 'lib/syntax_tree/node.rb', line 10972 def predicate @predicate end |
#statements ⇒ Object (readonly)
- Statements
-
the expressions to be executed
10975 10976 10977 |
# File 'lib/syntax_tree/node.rb', line 10975 def statements @statements end |
Instance Method Details
#child_nodes ⇒ Object Also known as: deconstruct
10987 10988 10989 |
# File 'lib/syntax_tree/node.rb', line 10987 def child_nodes [predicate, statements] end |
#deconstruct_keys(keys) ⇒ Object
10993 10994 10995 10996 10997 10998 10999 11000 |
# File 'lib/syntax_tree/node.rb', line 10993 def deconstruct_keys(keys) { predicate: predicate, statements: statements, location: location, comments: comments } end |
#format(q) ⇒ Object
11002 11003 11004 11005 11006 11007 11008 11009 11010 11011 11012 11013 11014 11015 |
# File 'lib/syntax_tree/node.rb', line 11002 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 |
#pretty_print(q) ⇒ Object
11017 11018 11019 11020 11021 11022 11023 11024 11025 11026 11027 11028 11029 |
# File 'lib/syntax_tree/node.rb', line 11017 def pretty_print(q) q.group(2, "(", ")") do q.text("while") q.breakable q.pp(predicate) q.breakable q.pp(statements) q.pp(Comment::List.new(comments)) end end |
#to_json(*opts) ⇒ Object
11031 11032 11033 11034 11035 11036 11037 11038 11039 |
# File 'lib/syntax_tree/node.rb', line 11031 def to_json(*opts) { type: :while, pred: predicate, stmts: statements, loc: location, cmts: comments }.to_json(*opts) end |