Skip to content

Commit 030de2e

Browse files
committed
Givens instance
This implements support for givens instances.
1 parent 27894cb commit 030de2e

File tree

2 files changed

+104
-1
lines changed

2 files changed

+104
-1
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(
@@ -307,6 +308,16 @@ module.exports = grammar({
307308
),
308309
),
309310

311+
/*
312+
* WithTemplateBody ::= <<< [SelfType] TemplateStat {semi TemplateStat} >>>
313+
*/
314+
with_template_body: $ => prec.left(PREC.control, seq(
315+
// TODO: self type
316+
$._indent,
317+
$._block,
318+
$._outdent,
319+
)),
320+
310321
_end_marker: $ => prec.left(PREC.end_marker, seq(
311322
'end',
312323
choice(
@@ -411,6 +422,40 @@ module.exports = grammar({
411422

412423
opaque_modifier: $ => 'opaque',
413424

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

0 commit comments

Comments
 (0)