Class: SyntaxTree::If

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

Overview

If represents the first clause in an if chain.

if predicate
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(predicate:, statements:, consequent:, location:, comments: []) ⇒ If

Returns a new instance of If.



7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
# File 'lib/syntax_tree.rb', line 7043

def initialize(
  predicate:,
  statements:,
  consequent:,
  location:,
  comments: []
)
  @predicate = predicate
  @statements = statements
  @consequent = consequent
  @location = location
  @comments = comments
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



7041
7042
7043
# File 'lib/syntax_tree.rb', line 7041

def comments
  @comments
end

#consequentObject (readonly)

nil, Elsif, Else

the next clause in the chain



7035
7036
7037
# File 'lib/syntax_tree.rb', line 7035

def consequent
  @consequent
end

#locationObject (readonly)

Location

the location of this node



7038
7039
7040
# File 'lib/syntax_tree.rb', line 7038

def location
  @location
end

#predicateObject (readonly)

untyped

the expression to be checked



7029
7030
7031
# File 'lib/syntax_tree.rb', line 7029

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



7032
7033
7034
# File 'lib/syntax_tree.rb', line 7032

def statements
  @statements
end

Instance Method Details

#child_nodesObject



7057
7058
7059
# File 'lib/syntax_tree.rb', line 7057

def child_nodes
  [predicate, statements, consequent]
end

#format(q) ⇒ Object



7061
7062
7063
# File 'lib/syntax_tree.rb', line 7061

def format(q)
  ConditionalFormatter.new("if", self).format(q)
end

#pretty_print(q) ⇒ Object



7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
# File 'lib/syntax_tree.rb', line 7065

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("if")

    q.breakable
    q.pp(predicate)

    q.breakable
    q.pp(statements)

    if consequent
      q.breakable
      q.pp(consequent)
    end

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
# File 'lib/syntax_tree.rb', line 7084

def to_json(*opts)
  {
    type: :if,
    pred: predicate,
    stmts: statements,
    cons: consequent,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end