tree-sitter-scala icon indicating copy to clipboard operation
tree-sitter-scala copied to clipboard

New line in lambda removes the body from function definition

Open dharm-kr16 opened this issue 3 years ago • 1 comments

object Lambda
{
	def main(args:Array[String])
	{
		randomInt() match {
			case (0, 1) => (zzz:Int, _) =>
                         println(zzz)
		}
	}
}

An extra body is added in case_clause instead of lambda_expression:

        (match_expression
          value: (call_expression
            function: (identifier 'randomInt')
            arguments: (arguments)
          )
          body: (case_block
            (case_clause
              pattern: (tuple_pattern
                (integer_literal)
                (integer_literal)
              )
              body: (lambda_expression
                (bindings
                  (binding
                    name: (identifier 'zzz')
                    type: (type_identifier 'Int')
                  )
                  (binding
                    name: (identifier '_')
                  )
                )
              )
              body: (call_expression
                function: (identifier 'println')
                arguments: (arguments
                  (identifier 'zzz')
                )
              )
            )
          )
        )

dharm-kr16 avatar Sep 20 '22 14:09 dharm-kr16

When we remove the new line from body of lambda expression it works fine as below:

object Lambda
{
	def main(args:Array[String])
	{
		randomInt() match {
			case (0, 1) => (zzz:Int, _) => println(zzz)
		}
	}
}

AST for above is as follows:

(compilation_unit 'tests/scala/dharm.scala'
  (object_definition
    name: (identifier 'Lambda')
    body: (template_body
      (function_declaration
        name: (identifier 'main')
        parameters: (parameters
          (parameter
            name: (identifier 'args')
            type: (generic_type
              type: (type_identifier 'Array')
              type_arguments: (type_arguments
                (type_identifier 'String')
              )
            )
          )
        )
      )
      (block
        (match_expression
          value: (call_expression
            function: (identifier 'randomInt')
            arguments: (arguments)
          )
          body: (case_block
            (case_clause
              pattern: (tuple_pattern
                (integer_literal)
                (integer_literal)
              )
              body: (lambda_expression
                (bindings
                  (binding
                    name: (identifier 'zzz')
                    type: (type_identifier 'Int')
                  )
                  (binding
                    name: (identifier '_')
                  )
                )
                (call_expression
                  function: (identifier 'println')
                  arguments: (arguments
                    (identifier 'zzz')
                  )
                )
              )
            )
          )
        )
      )
    )
  )
)

dharm-kr16 avatar Sep 20 '22 14:09 dharm-kr16

$ cat examples/B.scala
object Lambda
{
        def main(args:Array[String])
        {
                randomInt() match {
                        case (0, 1) => (zzz:Int, _) =>
                         println(zzz)
                }
        }
}

$ tree-sitter parse examples/B.scala
(compilation_unit [0, 0] - [11, 0]
  (object_definition [0, 0] - [9, 1]
    name: (identifier [0, 7] - [0, 13])
    body: (template_body [1, 0] - [9, 1]
      (function_declaration [2, 1] - [2, 29]
        name: (identifier [2, 5] - [2, 9])
        parameters: (parameters [2, 9] - [2, 29]
          (parameter [2, 10] - [2, 28]
            name: (identifier [2, 10] - [2, 14])
            type: (generic_type [2, 15] - [2, 28]
              type: (type_identifier [2, 15] - [2, 20])
              type_arguments: (type_arguments [2, 20] - [2, 28]
                (type_identifier [2, 21] - [2, 27]))))))
      (block [3, 1] - [8, 2]
        (match_expression [4, 2] - [7, 3]
          value: (call_expression [4, 2] - [4, 13]
            function: (identifier [4, 2] - [4, 11])
            arguments: (arguments [4, 11] - [4, 13]))
          body: (case_block [4, 20] - [7, 3]
            (case_clause [5, 3] - [7, 2]
              pattern: (tuple_pattern [5, 8] - [5, 14]
                (integer_literal [5, 9] - [5, 10])
                (integer_literal [5, 12] - [5, 13]))
              body: (lambda_expression [5, 18] - [6, 37]
                (bindings [5, 18] - [5, 30]
                  (binding [5, 19] - [5, 26]
                    name: (identifier [5, 19] - [5, 22])
                    type: (type_identifier [5, 23] - [5, 26]))
                  (binding [5, 28] - [5, 29]
                    name: (identifier [5, 28] - [5, 29])))
                (call_expression [6, 25] - [6, 37]
                  function: (identifier [6, 25] - [6, 32])
                  arguments: (arguments [6, 32] - [6, 37]
                    (identifier [6, 33] - [6, 36])))))))))))

This now works on the latest.

eed3si9n avatar Jan 14 '23 20:01 eed3si9n