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.



5935
5936
5937
5938
5939
5940
# File 'lib/syntax_tree/node.rb', line 5935

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



5933
5934
5935
# File 'lib/syntax_tree/node.rb', line 5933

def comments
  @comments
end

#paramsObject (readonly)

LambdaVar | Paren

the parameter declaration for this lambda



5927
5928
5929
# File 'lib/syntax_tree/node.rb', line 5927

def params
  @params
end

#statementsObject (readonly)

BodyStmt | Statements

the expressions to be executed in this lambda



5930
5931
5932
# File 'lib/syntax_tree/node.rb', line 5930

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



5942
5943
5944
# File 'lib/syntax_tree/node.rb', line 5942

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

#child_nodesObject Also known as: deconstruct



5946
5947
5948
# File 'lib/syntax_tree/node.rb', line 5946

def child_nodes
  [params, statements]
end

#deconstruct_keys(_keys) ⇒ Object



5952
5953
5954
5955
5956
5957
5958
5959
# File 'lib/syntax_tree/node.rb', line 5952

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

#format(q) ⇒ Object



5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
# File 'lib/syntax_tree/node.rb', line 5961

def format(q)
  q.group(0, "->") do
    if params.is_a?(Paren)
      q.format(params) unless params.contents.empty?
    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
              q.format(statements)
            end
            q.breakable
          end

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

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

          q.breakable
          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