Skip to content

Commit a9123ed

Browse files
authored
Merge pull request #442 from eed3si9n/wip/sip-64-colon
SIP-64 support
2 parents fb9aa82 + 4ef969d commit a9123ed

File tree

3 files changed

+150
-16
lines changed

3 files changed

+150
-16
lines changed

grammar.js

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const PREC = {
33
using_directive: 2,
44
control: 1,
55
stable_type_id: 2,
6+
type: 2,
67
while: 2,
78
assign: 3,
89
case: 3,
@@ -98,8 +99,12 @@ module.exports = grammar({
9899
[$._simple_expression, $._type_identifier],
99100
// 'if' parenthesized_expression • '{' …
100101
[$._if_condition, $._simple_expression],
101-
// _postfix_expression_choice ':' '(' wildcard • ':' …
102-
[$.binding, $._simple_type],
102+
[$.block, $._braced_template_body1],
103+
[$._simple_expression, $.self_type, $._type_identifier],
104+
[$._simple_expression, $._type_identifier],
105+
[$.lambda_expression, $.self_type, $._type_identifier],
106+
[$.lambda_expression, $._type_identifier],
107+
[$.binding, $._simple_expression, $._type_identifier],
103108
],
104109

105110
word: $ => $._alpha_identifier,
@@ -369,7 +374,7 @@ module.exports = grammar({
369374
field("bound", optional($.lower_bound)),
370375
field("bound", optional($.upper_bound)),
371376
field("bound", optional(repeat($.view_bound))),
372-
field("bound", optional(repeat($.context_bound))),
377+
field("bound", optional($._context_bounds)),
373378
),
374379

375380
upper_bound: $ => seq("<:", field("type", $._type)),
@@ -378,7 +383,26 @@ module.exports = grammar({
378383

379384
view_bound: $ => seq("<%", field("type", $._type)),
380385

381-
context_bound: $ => seq(":", field("type", $._type)),
386+
_context_bounds: $ => choice(
387+
repeat1(seq(
388+
":",
389+
$.context_bound
390+
)),
391+
seq(
392+
":",
393+
"{",
394+
trailingCommaSep1($.context_bound),
395+
"}",
396+
)
397+
),
398+
399+
context_bound: $ => seq(
400+
field("type", $._type),
401+
optional(seq(
402+
"as",
403+
field("name", $._identifier),
404+
)),
405+
),
382406

383407
/*
384408
* TemplateBody ::= :<<< [SelfType] TemplateStat {semi TemplateStat} >>>
@@ -525,6 +549,7 @@ module.exports = grammar({
525549
field("type_parameters", optional($.type_parameters)),
526550
field("bound", optional($.lower_bound)),
527551
field("bound", optional($.upper_bound)),
552+
field("bound", optional($._context_bounds)),
528553
),
529554
),
530555

@@ -602,6 +627,7 @@ module.exports = grammar({
602627
optional($.modifiers),
603628
"given",
604629
optional($._given_constructor),
630+
repeat($._given_sig),
605631
choice(
606632
field("return_type", $._structural_instance),
607633
seq(
@@ -612,6 +638,14 @@ module.exports = grammar({
612638
),
613639
),
614640

641+
_given_sig: $ =>
642+
seq(
643+
$._given_conditional,
644+
"=>"
645+
),
646+
647+
_given_conditional: $ => alias($.parameters, $.given_conditional),
648+
615649
_given_constructor: $ =>
616650
prec.right(
617651
seq(
@@ -634,7 +668,10 @@ module.exports = grammar({
634668
PREC.compound,
635669
seq(
636670
$._constructor_application,
637-
"with",
671+
choice(
672+
":",
673+
"with"
674+
),
638675
field("body", $.with_template_body),
639676
),
640677
),
@@ -851,15 +888,18 @@ module.exports = grammar({
851888
annotated_type: $ => prec.right(seq($._simple_type, repeat1($.annotation))),
852889

853890
_simple_type: $ =>
854-
choice(
855-
$.generic_type,
856-
$.projected_type,
857-
$.tuple_type,
858-
$.named_tuple_type,
859-
$.singleton_type,
860-
$.stable_type_identifier,
861-
$._type_identifier,
862-
$.wildcard,
891+
prec.left(
892+
PREC.type,
893+
choice(
894+
$.generic_type,
895+
$.projected_type,
896+
$.tuple_type,
897+
$.named_tuple_type,
898+
$.singleton_type,
899+
$.stable_type_identifier,
900+
$._type_identifier,
901+
$.wildcard,
902+
)
863903
),
864904

865905
compound_type: $ =>
@@ -1562,7 +1602,8 @@ module.exports = grammar({
15621602
$.string,
15631603
),
15641604

1565-
literal_type: $ => $._non_null_literal,
1605+
literal_type: $ =>
1606+
prec.left(PREC.type, $._non_null_literal),
15661607

15671608
literal: $ => choice($._non_null_literal, $.null_literal),
15681609

test/corpus/definitions.txt

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,25 @@ class A {
10271027
(type_parameters
10281028
(identifier))))))
10291029

1030+
================================================================================
1031+
Type definitions (Scala 3 syntax)
1032+
================================================================================
1033+
1034+
class A:
1035+
type Element: Order
1036+
1037+
--------------------------------------------------------------------------------
1038+
1039+
(compilation_unit
1040+
(class_definition
1041+
(identifier)
1042+
(template_body
1043+
(type_definition
1044+
(type_identifier)
1045+
(context_bound
1046+
(type_identifier))))))
1047+
1048+
10301049
================================================================================
10311050
Function declarations
10321051
================================================================================
@@ -1290,6 +1309,9 @@ object A:
12901309
given intFoo: CanFoo[Int] with
12911310
def foo(x: Int): Int = 0
12921311

1312+
given intFoo: CanFoo[Int]:
1313+
def foo(x: Int): Int = 0
1314+
12931315
given CanFoo[Int] with
12941316
def foo(x: Int): Int = 0
12951317

@@ -1317,6 +1339,8 @@ object A:
13171339
trait B:
13181340
given c: Context[T]
13191341

1342+
given (config: Config) => Factory = ConcreteFactory()
1343+
13201344
--------------------------------------------------------------------------------
13211345

13221346
(compilation_unit
@@ -1342,6 +1366,21 @@ object A:
13421366
(type_identifier)))
13431367
(type_identifier)
13441368
(integer_literal))))
1369+
(given_definition
1370+
(identifier)
1371+
(generic_type
1372+
(type_identifier)
1373+
(type_arguments
1374+
(type_identifier)))
1375+
(with_template_body
1376+
(function_definition
1377+
(identifier)
1378+
(parameters
1379+
(parameter
1380+
(identifier)
1381+
(type_identifier)))
1382+
(type_identifier)
1383+
(integer_literal))))
13451384
(given_definition
13461385
(generic_type
13471386
(type_identifier)
@@ -1464,7 +1503,16 @@ object A:
14641503
(generic_type
14651504
(type_identifier)
14661505
(type_arguments
1467-
(type_identifier)))))))))
1506+
(type_identifier))))))
1507+
(given_definition
1508+
(given_conditional
1509+
(parameter
1510+
(identifier)
1511+
(type_identifier)))
1512+
(type_identifier)
1513+
(call_expression
1514+
(identifier)
1515+
(arguments))))))
14681516

14691517
================================================================================
14701518
Top-level Definitions (Scala 3 syntax)

test/corpus/types.txt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,51 @@ class A[B : C : D]
426426
(context_bound
427427
(type_identifier)))))
428428

429+
================================================================================
430+
Context bound (Scala 3 syntax)
431+
================================================================================
432+
433+
def reduce[A : Monoid as m](xs: List[A]): A = ()
434+
435+
def showMax[X : {Ordering, Show}](x: X, y: X): String = ()
436+
437+
--------------------------------------------------------------------------------
438+
439+
(compilation_unit
440+
(function_definition
441+
(identifier)
442+
(type_parameters
443+
(identifier)
444+
(context_bound
445+
(type_identifier)
446+
(identifier)))
447+
(parameters
448+
(parameter
449+
(identifier)
450+
(generic_type
451+
(type_identifier)
452+
(type_arguments
453+
(type_identifier)))))
454+
(type_identifier)
455+
(unit))
456+
(function_definition
457+
(identifier)
458+
(type_parameters
459+
(identifier)
460+
(context_bound
461+
(type_identifier))
462+
(context_bound
463+
(type_identifier)))
464+
(parameters
465+
(parameter
466+
(identifier)
467+
(type_identifier))
468+
(parameter
469+
(identifier)
470+
(type_identifier)))
471+
(type_identifier)
472+
(unit)))
473+
429474
================================================================================
430475
Projections
431476
================================================================================

0 commit comments

Comments
 (0)