Skip to content

Given instances #99

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions corpus/definitions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,64 @@ class A:
(indented_block
(val_definition (identifier) (integer_literal)) (val_definition (identifier) (integer_literal)) (infix_expression (identifier) (operator_identifier) (identifier)))))))

=======================================
Given instance definitions (Scala 3 syntax)
=======================================

object A:
given a: A = x

given intFoo: CanFoo[Int] with
def foo(x: Int): Int = 0

private given listFoo[A1](using ev: CanFoo[A1]): CanFoo[List[A1]] with
def foo(xs: List[A1]): Int = 0

---

(compilation_unit
(object_definition
(identifier)
(template_body
(given_definition
(identifier)
(type_identifier)
(identifier))
(given_definition
(identifier)
(generic_type
(type_identifier)
(type_arguments (type_identifier)))
(with_template_body
(function_definition
(identifier)
(parameters
(parameter (identifier) (type_identifier)))
(type_identifier)
(integer_literal)
)))
(given_definition
(modifiers (access_modifier))
(identifier)
(type_parameters (identifier))
(parameters
(parameter
(identifier)
(generic_type (type_identifier) (type_arguments (type_identifier)))))
(generic_type
(type_identifier)
(type_arguments
(generic_type (type_identifier) (type_arguments (type_identifier)))))
(with_template_body
(function_definition
(identifier)
(parameters
(parameter
(identifier)
(generic_type (type_identifier) (type_arguments (type_identifier)))))
(type_identifier)
(integer_literal)))))))

=======================================
Top-level Definitions (Scala 3 syntax)
=======================================
Expand Down
47 changes: 46 additions & 1 deletion grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ module.exports = grammar({
),

_definition: $ => choice(
$.given_definition,
$.class_definition,
$.import_declaration,
$.object_definition,
Expand All @@ -92,7 +93,7 @@ module.exports = grammar({
$.var_declaration,
$.type_definition,
$.function_definition,
$.function_declaration
$.function_declaration,
),

enum_definition: $ => seq(
Expand Down Expand Up @@ -313,6 +314,16 @@ module.exports = grammar({
),
),

/*
* WithTemplateBody ::= <<< [SelfType] TemplateStat {semi TemplateStat} >>>
*/
with_template_body: $ => prec.left(PREC.control, seq(
// TODO: self type
$._indent,
$._block,
$._outdent,
)),

_end_marker: $ => prec.left(PREC.end_marker, seq(
'end',
choice(
Expand Down Expand Up @@ -417,6 +428,40 @@ module.exports = grammar({

opaque_modifier: $ => 'opaque',

/**
* GivenDef ::= [GivenSig] (AnnotType ['=' Expr] | StructuralInstance)
* GivenSig ::= [id] [DefTypeParamClause] {UsingParamClause} ':'
*/
given_definition: $ => prec.right(seq(
repeat($.annotation),
optional($.modifiers),
'given',
field('name', $.identifier),
field('type_parameters', optional($.type_parameters)),
field('parameters', repeat($.parameters)),
':',
choice(
field('return_type', $._structural_instance),
seq(
field('return_type', $._annotated_type),
'=',
field('body', $.expression),
)
),
)),

/**
* StructuralInstance ::= ConstrApp {'with' ConstrApp} ['with' WithTemplateBody]
*/
_structural_instance: $ => seq(
choice(
$._annotated_type,
$.compound_type,
),
'with',
field('body', $.with_template_body),
),

modifiers: $ => repeat1(choice(
'abstract',
'final',
Expand Down
2 changes: 1 addition & 1 deletion script/smoke_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

SCALA_SCALA_LIBRARY_EXPECTED=87
SCALA_SCALA_COMPILER_EXPECTED=45
DOTTY_COMPILER_EXPECTED=56
DOTTY_COMPILER_EXPECTED=55
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note 1% regression on Dotty compiler.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Odd, I'd expect this to bump the coverage up, not down. Any idea why?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With these small percentage difference, it's hard to tell. Potentially some files were parsed incorrectly before, and now that givens is interpreted correctly it's hitting some other unsupported feature?


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