Skip to content

Commit

Permalink
Polymorphic function type and lambda expression
Browse files Browse the repository at this point in the history
**Problem**
Poly function types and poly function values aren't supported.

**Solution**
This implements it.
  • Loading branch information
eed3si9n committed Dec 12, 2024
1 parent 1405853 commit 2974f73
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 6 deletions.
16 changes: 15 additions & 1 deletion grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ module.exports = grammar({
[$.class_parameters],
// 'for' operator_identifier ':' _annotated_type • ':' …
[$._type, $.compound_type],
// 'given' '(' '[' _type_parameter • ',' …
[$._variant_type_parameter, $.type_lambda],
// 'if' parenthesized_expression • '{' …
[$._if_condition, $._simple_expression],
// _postfix_expression_choice ':' '(' wildcard • ':' …
Expand Down Expand Up @@ -939,7 +941,10 @@ module.exports = grammar({

function_type: $ =>
prec.left(
seq(field("parameter_types", $.parameter_types), $._arrow_then_type),
choice(
seq(field("type_parameters", $.type_parameters), $._arrow_then_type),
seq(field("parameter_types", $.parameter_types), $._arrow_then_type),
)
),

_arrow_then_type: $ =>
Expand Down Expand Up @@ -1104,6 +1109,15 @@ module.exports = grammar({
lambda_expression: $ =>
prec.right(
seq(
optional(
seq(
field(
"type_parameters",
$.type_parameters,
),
"=>",
),
),
field(
"parameters",
choice(
Expand Down
42 changes: 37 additions & 5 deletions test/corpus/expressions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1116,7 +1116,6 @@ object O {
{ b =>
if (c) d.e }
{ a => implicit b => b }
{ (a: Int) ?=> (b: Int) => b }
{ (_, a) => a }
}

Expand Down Expand Up @@ -1198,6 +1197,31 @@ object O {
(identifier)
(identifier))))
(block
(lambda_expression
(bindings
(binding
(wildcard))
(binding
(identifier)))
(identifier))))))

================================================================================
Lambda Expression (Scala 3 syntax)
================================================================================

object O:
val f = (a: Int) ?=> (b: Int) => b

val less: Comparer = [X: Ord] => (x: X, y: X) => ???

--------------------------------------------------------------------------------

(compilation_unit
(object_definition
(identifier)
(template_body
(val_definition
(identifier)
(lambda_expression
(bindings
(binding
Expand All @@ -1209,14 +1233,22 @@ object O {
(identifier)
(type_identifier)))
(identifier))))
(block
(val_definition
(identifier)
(type_identifier)
(lambda_expression
(type_parameters
(identifier)
(context_bound
(type_identifier)))
(bindings
(binding
(wildcard))
(identifier)
(type_identifier))
(binding
(identifier)))
(identifier))))))
(identifier)
(type_identifier)))
(operator_identifier))))))

================================================================================
Unit expressions
Expand Down
26 changes: 26 additions & 0 deletions test/corpus/types.txt
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,32 @@ object Main {
(type_identifier)
(type_identifier)))))))

================================================================================
Polymorphic function types (Scala 3 syntax)
================================================================================

class A:
type Comparer = [X: Ord] => (X, X) => Boolean

--------------------------------------------------------------------------------

(compilation_unit
(class_definition
(identifier)
(template_body
(type_definition
(type_identifier)
(function_type
(type_parameters
(identifier)
(context_bound
(type_identifier)))
(function_type
(parameter_types
(type_identifier)
(type_identifier))
(type_identifier)))))))

================================================================================
Context function types (Scala 3 syntax)
================================================================================
Expand Down

0 comments on commit 2974f73

Please sign in to comment.