Skip to content

Commit ea144d8

Browse files
committed
Givens instance
This implements support for givens instances.
1 parent a7b8fa6 commit ea144d8

File tree

3 files changed

+105
-2
lines changed

3 files changed

+105
-2
lines changed

corpus/definitions.txt

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,64 @@ class A:
621621
(indented_block
622622
(val_definition (identifier) (integer_literal)) (val_definition (identifier) (integer_literal)) (infix_expression (identifier) (operator_identifier) (identifier)))))))
623623

624+
=======================================
625+
Given instance definitions (Scala 3 syntax)
626+
=======================================
627+
628+
object A:
629+
given a: A = x
630+
631+
given intFoo: CanFoo[Int] with
632+
def foo(x: Int): Int = 0
633+
634+
private given listFoo[A1](using ev: CanFoo[A1]): CanFoo[List[A1]] with
635+
def foo(xs: List[A1]): Int = 0
636+
637+
---
638+
639+
(compilation_unit
640+
(object_definition
641+
(identifier)
642+
(template_body
643+
(given_definition
644+
(identifier)
645+
(type_identifier)
646+
(identifier))
647+
(given_definition
648+
(identifier)
649+
(generic_type
650+
(type_identifier)
651+
(type_arguments (type_identifier)))
652+
(with_template_body
653+
(function_definition
654+
(identifier)
655+
(parameters
656+
(parameter (identifier) (type_identifier)))
657+
(type_identifier)
658+
(integer_literal)
659+
)))
660+
(given_definition
661+
(modifiers (access_modifier))
662+
(identifier)
663+
(type_parameters (identifier))
664+
(parameters
665+
(parameter
666+
(identifier)
667+
(generic_type (type_identifier) (type_arguments (type_identifier)))))
668+
(generic_type
669+
(type_identifier)
670+
(type_arguments
671+
(generic_type (type_identifier) (type_arguments (type_identifier)))))
672+
(with_template_body
673+
(function_definition
674+
(identifier)
675+
(parameters
676+
(parameter
677+
(identifier)
678+
(generic_type (type_identifier) (type_arguments (type_identifier)))))
679+
(type_identifier)
680+
(integer_literal)))))))
681+
624682
=======================================
625683
Top-level Definitions (Scala 3 syntax)
626684
=======================================

grammar.js

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ module.exports = grammar({
8181
),
8282

8383
_definition: $ => choice(
84+
$.given_definition,
8485
$.class_definition,
8586
$.import_declaration,
8687
$.object_definition,
@@ -92,7 +93,7 @@ module.exports = grammar({
9293
$.var_declaration,
9394
$.type_definition,
9495
$.function_definition,
95-
$.function_declaration
96+
$.function_declaration,
9697
),
9798

9899
enum_definition: $ => seq(
@@ -313,6 +314,16 @@ module.exports = grammar({
313314
),
314315
),
315316

317+
/*
318+
* WithTemplateBody ::= <<< [SelfType] TemplateStat {semi TemplateStat} >>>
319+
*/
320+
with_template_body: $ => prec.left(PREC.control, seq(
321+
// TODO: self type
322+
$._indent,
323+
$._block,
324+
$._outdent,
325+
)),
326+
316327
_end_marker: $ => prec.left(PREC.end_marker, seq(
317328
'end',
318329
choice(
@@ -417,6 +428,40 @@ module.exports = grammar({
417428

418429
opaque_modifier: $ => 'opaque',
419430

431+
/**
432+
* GivenDef ::= [GivenSig] (AnnotType ['=' Expr] | StructuralInstance)
433+
* GivenSig ::= [id] [DefTypeParamClause] {UsingParamClause} ':'
434+
*/
435+
given_definition: $ => prec.right(seq(
436+
repeat($.annotation),
437+
optional($.modifiers),
438+
'given',
439+
field('name', $.identifier),
440+
field('type_parameters', optional($.type_parameters)),
441+
field('parameters', repeat($.parameters)),
442+
':',
443+
choice(
444+
field('return_type', $._structural_instance),
445+
seq(
446+
field('return_type', $._annotated_type),
447+
'=',
448+
field('body', $.expression),
449+
)
450+
),
451+
)),
452+
453+
/**
454+
* StructuralInstance ::= ConstrApp {'with' ConstrApp} ['with' WithTemplateBody]
455+
*/
456+
_structural_instance: $ => seq(
457+
choice(
458+
$._annotated_type,
459+
$.compound_type,
460+
),
461+
'with',
462+
field('body', $.with_template_body),
463+
),
464+
420465
modifiers: $ => repeat1(choice(
421466
'abstract',
422467
'final',

script/smoke_test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
SCALA_SCALA_LIBRARY_EXPECTED=87
66
SCALA_SCALA_COMPILER_EXPECTED=45
7-
DOTTY_COMPILER_EXPECTED=56
7+
DOTTY_COMPILER_EXPECTED=55
88

99
if [ ! -d "$SCALA_SCALA_DIR" ]; then
1010
echo "\$SCALA_SCALA_DIR must be set"

0 commit comments

Comments
 (0)