Skip to content

Commit 25acfc9

Browse files
authored
Merge pull request #444 from eed3si9n/wip/poly
Polymorphic function type and lambda expression
2 parents a808feb + 45977fc commit 25acfc9

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
@@ -92,6 +92,8 @@ module.exports = grammar({
9292
[$.class_parameters],
9393
// 'for' operator_identifier ':' _annotated_type • ':' …
9494
[$._type, $.compound_type],
95+
// 'given' '(' '[' _type_parameter • ',' …
96+
[$._variant_type_parameter, $.type_lambda],
9597
// 'given' '(' operator_identifier ':' _type • ',' …
9698
[$.name_and_type, $.parameter],
9799
[$._simple_expression, $.binding, $.tuple_pattern],
@@ -984,7 +986,10 @@ module.exports = grammar({
984986

985987
function_type: $ =>
986988
prec.left(
987-
seq(field("parameter_types", $.parameter_types), $._arrow_then_type),
989+
choice(
990+
seq(field("type_parameters", $.type_parameters), $._arrow_then_type),
991+
seq(field("parameter_types", $.parameter_types), $._arrow_then_type),
992+
)
988993
),
989994

990995
_arrow_then_type: $ =>
@@ -1158,6 +1163,15 @@ module.exports = grammar({
11581163
lambda_expression: $ =>
11591164
prec.right(
11601165
seq(
1166+
optional(
1167+
seq(
1168+
field(
1169+
"type_parameters",
1170+
$.type_parameters,
1171+
),
1172+
"=>",
1173+
),
1174+
),
11611175
field(
11621176
"parameters",
11631177
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
@@ -163,6 +163,32 @@ object Main {
163163
(type_identifier)
164164
(type_identifier)))))))
165165

166+
================================================================================
167+
Polymorphic function types (Scala 3 syntax)
168+
================================================================================
169+
170+
class A:
171+
type Comparer = [X: Ord] => (X, X) => Boolean
172+
173+
--------------------------------------------------------------------------------
174+
175+
(compilation_unit
176+
(class_definition
177+
(identifier)
178+
(template_body
179+
(type_definition
180+
(type_identifier)
181+
(function_type
182+
(type_parameters
183+
(identifier)
184+
(context_bound
185+
(type_identifier)))
186+
(function_type
187+
(parameter_types
188+
(type_identifier)
189+
(type_identifier))
190+
(type_identifier)))))))
191+
166192
================================================================================
167193
Context function types (Scala 3 syntax)
168194
================================================================================

0 commit comments

Comments
 (0)