Class: SyntaxTree::Lambda

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

Overview

Lambda represents using a lambda literal (not the lambda method call).

->(value) { value * 2 }

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #pretty_print, #to_json

Constructor Details

#initialize(params:, statements:, location:, comments: []) ⇒ Lambda

Returns a new instance of Lambda.



6158
6159
6160
6161
6162
6163
# File 'lib/syntax_tree/node.rb', line 6158

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



6156
6157
6158
# File 'lib/syntax_tree/node.rb', line 6156

def comments
  @comments
end

#paramsObject (readonly)

LambdaVar | Paren

the parameter declaration for this lambda



6150
6151
6152
# File 'lib/syntax_tree/node.rb', line 6150

def params
  @params
end

#statementsObject (readonly)

BodyStmt | Statements

the expressions to be executed in this lambda



6153
6154
6155
# File 'lib/syntax_tree/node.rb', line 6153

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



6165
6166
6167
# File 'lib/syntax_tree/node.rb', line 6165

def accept(visitor)
  visitor.visit_lambda(self)
end

#child_nodesObject Also known as: deconstruct



6169
6170
6171
# File 'lib/syntax_tree/node.rb', line 6169

def child_nodes
  [params, statements]
end

#deconstruct_keys(_keys) ⇒ Object



6175
6176
6177
6178
6179
6180
6181
6182
# File 'lib/syntax_tree/node.rb', line 6175

def deconstruct_keys(_keys)
  {
    params: params,
    statements: statements,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
# File 'lib/syntax_tree/node.rb', line 6184

def format(q)
  q.text("->")
  q.group do
    if params.is_a?(Paren)
      q.format(params) unless params.contents.empty?
    elsif params.empty? && params.comments.any?
      q.format(params)
    elsif !params.empty?
      q.group do
        q.text("(")
        q.format(params)
        q.text(")")
      end
    end

    q.text(" ")
    q
      .if_break do
        force_parens =
          q.parents.any? do |node|
            node.is_a?(Command) || node.is_a?(CommandCall)
          end

        if force_parens
          q.text("{")

          unless statements.empty?
            q.indent do
              q.breakable_space
              q.format(statements)
            end
            q.breakable_space
          end

          q.text("}")
        else
          q.text("do")

          unless statements.empty?
            q.indent do
              q.breakable_space
              q.format(statements)
            end
          end

          q.breakable_space
          q.text("end")
        end
      end
      .if_flat do
        q.text("{")

        unless statements.empty?
          q.text(" ")
          q.format(statements)
          q.text(" ")
        end

        q.text("}")
      end
  end
end