From 2974f734d8d6b1c2fb3025958a119f1b978bff61 Mon Sep 17 00:00:00 2001 From: Eugene Yokota Date: Thu, 12 Dec 2024 02:08:08 -0500 Subject: [PATCH] Polymorphic function type and lambda expression **Problem** Poly function types and poly function values aren't supported. **Solution** This implements it. --- grammar.js | 16 +++++++++++++- test/corpus/expressions.txt | 42 ++++++++++++++++++++++++++++++++----- test/corpus/types.txt | 26 +++++++++++++++++++++++ 3 files changed, 78 insertions(+), 6 deletions(-) diff --git a/grammar.js b/grammar.js index f30d2b9d..af4e9ffb 100644 --- a/grammar.js +++ b/grammar.js @@ -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 • ':' … @@ -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: $ => @@ -1104,6 +1109,15 @@ module.exports = grammar({ lambda_expression: $ => prec.right( seq( + optional( + seq( + field( + "type_parameters", + $.type_parameters, + ), + "=>", + ), + ), field( "parameters", choice( diff --git a/test/corpus/expressions.txt b/test/corpus/expressions.txt index 6c5613f9..5392b129 100644 --- a/test/corpus/expressions.txt +++ b/test/corpus/expressions.txt @@ -1116,7 +1116,6 @@ object O { { b => if (c) d.e } { a => implicit b => b } - { (a: Int) ?=> (b: Int) => b } { (_, a) => a } } @@ -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 @@ -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 diff --git a/test/corpus/types.txt b/test/corpus/types.txt index a239d0bf..cf7ef2e9 100644 --- a/test/corpus/types.txt +++ b/test/corpus/types.txt @@ -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) ================================================================================