Skip to content

Commit 2974f73

Browse files
committed
Polymorphic function type and lambda expression
**Problem** Poly function types and poly function values aren't supported. **Solution** This implements it.
1 parent 1405853 commit 2974f73

File tree

3 files changed

+78
-6
lines changed

3 files changed

+78
-6
lines changed

grammar.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ module.exports = grammar({
9191
[$.class_parameters],
9292
// 'for' operator_identifier ':' _annotated_type • ':' …
9393
[$._type, $.compound_type],
94+
// 'given' '(' '[' _type_parameter • ',' …
95+
[$._variant_type_parameter, $.type_lambda],
9496
// 'if' parenthesized_expression • '{' …
9597
[$._if_condition, $._simple_expression],
9698
// _postfix_expression_choice ':' '(' wildcard • ':' …
@@ -939,7 +941,10 @@ module.exports = grammar({
939941

940942
function_type: $ =>
941943
prec.left(
942-
seq(field("parameter_types", $.parameter_types), $._arrow_then_type),
944+
choice(
945+
seq(field("type_parameters", $.type_parameters), $._arrow_then_type),
946+
seq(field("parameter_types", $.parameter_types), $._arrow_then_type),
947+
)
943948
),
944949

945950
_arrow_then_type: $ =>
@@ -1104,6 +1109,15 @@ module.exports = grammar({
11041109
lambda_expression: $ =>
11051110
prec.right(
11061111
seq(
1112+
optional(
1113+
seq(
1114+
field(
1115+
"type_parameters",
1116+
$.type_parameters,
1117+
),
1118+
"=>",
1119+
),
1120+
),
11071121
field(
11081122
"parameters",
11091123
choice(

test/corpus/expressions.txt

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,7 +1116,6 @@ object O {
11161116
{ b =>
11171117
if (c) d.e }
11181118
{ a => implicit b => b }
1119-
{ (a: Int) ?=> (b: Int) => b }
11201119
{ (_, a) => a }
11211120
}
11221121

@@ -1198,6 +1197,31 @@ object O {
11981197
(identifier)
11991198
(identifier))))
12001199
(block
1200+
(lambda_expression
1201+
(bindings
1202+
(binding
1203+
(wildcard))
1204+
(binding
1205+
(identifier)))
1206+
(identifier))))))
1207+
1208+
================================================================================
1209+
Lambda Expression (Scala 3 syntax)
1210+
================================================================================
1211+
1212+
object O:
1213+
val f = (a: Int) ?=> (b: Int) => b
1214+
1215+
val less: Comparer = [X: Ord] => (x: X, y: X) => ???
1216+
1217+
--------------------------------------------------------------------------------
1218+
1219+
(compilation_unit
1220+
(object_definition
1221+
(identifier)
1222+
(template_body
1223+
(val_definition
1224+
(identifier)
12011225
(lambda_expression
12021226
(bindings
12031227
(binding
@@ -1209,14 +1233,22 @@ object O {
12091233
(identifier)
12101234
(type_identifier)))
12111235
(identifier))))
1212-
(block
1236+
(val_definition
1237+
(identifier)
1238+
(type_identifier)
12131239
(lambda_expression
1240+
(type_parameters
1241+
(identifier)
1242+
(context_bound
1243+
(type_identifier)))
12141244
(bindings
12151245
(binding
1216-
(wildcard))
1246+
(identifier)
1247+
(type_identifier))
12171248
(binding
1218-
(identifier)))
1219-
(identifier))))))
1249+
(identifier)
1250+
(type_identifier)))
1251+
(operator_identifier))))))
12201252

12211253
================================================================================
12221254
Unit expressions

test/corpus/types.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,32 @@ object Main {
140140
(type_identifier)
141141
(type_identifier)))))))
142142

143+
================================================================================
144+
Polymorphic function types (Scala 3 syntax)
145+
================================================================================
146+
147+
class A:
148+
type Comparer = [X: Ord] => (X, X) => Boolean
149+
150+
--------------------------------------------------------------------------------
151+
152+
(compilation_unit
153+
(class_definition
154+
(identifier)
155+
(template_body
156+
(type_definition
157+
(type_identifier)
158+
(function_type
159+
(type_parameters
160+
(identifier)
161+
(context_bound
162+
(type_identifier)))
163+
(function_type
164+
(parameter_types
165+
(type_identifier)
166+
(type_identifier))
167+
(type_identifier)))))))
168+
143169
================================================================================
144170
Context function types (Scala 3 syntax)
145171
================================================================================

0 commit comments

Comments
 (0)