Class: SyntaxTree::While

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

While represents a while loop.

while predicate
end

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

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

#commentsObject (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

#predicateObject (readonly)

untyped

the expression to be checked



10972
10973
10974
# File 'lib/syntax_tree/node.rb', line 10972

def predicate
  @predicate
end

#statementsObject (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_nodesObject 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