Skip to content
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

SIP-58 Named tuples support #446

Merged
merged 3 commits into from
Dec 16, 2024
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
48 changes: 45 additions & 3 deletions grammar.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const PREC = {

Check notice on line 1 in grammar.js

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

ok, scala_scala/src/library/: 100.00%, expected at least 100%

Check notice on line 1 in grammar.js

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

ok, scala_scala/src/compiler/: 96.65%, expected at least 96%

Check notice on line 1 in grammar.js

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

ok, dotty/compiler/: 83.76%, expected at least 83%

Check notice on line 1 in grammar.js

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

ok, lila/modules/: 84.66%, expected at least 84%
comment: 1,
using_directive: 2,
control: 1,
Expand Down Expand Up @@ -91,6 +91,11 @@
[$.class_parameters],
// 'for' operator_identifier ':' _annotated_type • ':' …
[$._type, $.compound_type],
// 'given' '(' operator_identifier ':' _type • ',' …
[$.name_and_type, $.parameter],
[$._simple_expression, $.binding, $.tuple_pattern],
[$._simple_expression, $.tuple_pattern],
[$._simple_expression, $._type_identifier],
// 'if' parenthesized_expression • '{' …
[$._if_condition, $._simple_expression],
// _postfix_expression_choice ':' '(' wildcard • ':' …
Expand Down Expand Up @@ -785,6 +790,19 @@
),
),

/*
* NameAndType ::= id ':' Type
*/
name_and_type: $ =>
prec.left(
PREC.control,
seq(
field("name", $._identifier),
":",
field("type", $._param_type),
),
),

_block: $ =>
prec.left(
seq(
Expand Down Expand Up @@ -837,6 +855,7 @@
$.generic_type,
$.projected_type,
$.tuple_type,
$.named_tuple_type,
$.singleton_type,
$.stable_type_identifier,
$._type_identifier,
Expand Down Expand Up @@ -895,8 +914,14 @@
),
),

tuple_type: $ => seq("(", trailingCommaSep1($._type), ")"),

Check notice on line 917 in grammar.js

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

ok, complexity of the most complex definition tuple_type: 1217, lower than the allowed ceiling 1400

named_tuple_type: $ => seq(
"(",
trailingCommaSep1($.name_and_type),
")",
),

singleton_type: $ =>
prec.left(
PREC.stable_type_id,
Expand Down Expand Up @@ -991,6 +1016,7 @@
$.interpolated_string_expression,
$.capture_pattern,
$.tuple_pattern,
$.named_tuple_pattern,
$.case_class_pattern,
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we also need a modified case_class_pattern with the named arguments: https://docs.scala-lang.org/sips/named-tuples.html#pattern-matching-with-named-fields-in-general

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Addressed in 690838e

$.infix_pattern,
$.alternative_pattern,
Expand All @@ -1006,7 +1032,10 @@
seq(
field("type", choice($._type_identifier, $.stable_type_identifier)),
"(",
field("pattern", trailingCommaSep($._pattern)),
choice(
field("pattern", trailingCommaSep($._pattern)),
field("pattern", trailingCommaSep($.named_pattern)),
),
")",
),

Expand Down Expand Up @@ -1034,15 +1063,28 @@

typed_pattern: $ =>
prec.right(
-1,
seq(field("pattern", $._pattern), ":", field("type", $._type)),
),

given_pattern: $ => seq("given", field("type", $._type)),

// TODO: Flatten this.
alternative_pattern: $ => prec.left(-1, seq($._pattern, "|", $._pattern)),
alternative_pattern: $ => prec.left(-2, seq($._pattern, "|", $._pattern)),

tuple_pattern: $ => seq(
"(",
trailingCommaSep1($._pattern),
")",
),

named_pattern: $ => prec.left(-1, seq($._identifier, "=", $._pattern)),

tuple_pattern: $ => seq("(", $._pattern, repeat(seq(",", $._pattern)), ")"),
named_tuple_pattern: $ => seq(
"(",
trailingCommaSep1($.named_pattern),
")",
),

// ---------------------------------------------------------------
// Expressions
Expand Down
54 changes: 54 additions & 0 deletions test/corpus/patterns.txt
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,31 @@ val x = y match {
(identifier))
(identifier))))))

================================================================================
Name tuple patterns (Scala 3 syntax)
================================================================================

val x = y match
case (a = A, b = B) => ???

--------------------------------------------------------------------------------

(compilation_unit
(val_definition
(identifier)
(match_expression
(identifier)
(indented_cases
(case_clause
(named_tuple_pattern
(named_pattern
(identifier)
(identifier))
(named_pattern
(identifier)
(identifier)))
(operator_identifier))))))

================================================================================
Case class patterns
================================================================================
Expand Down Expand Up @@ -178,6 +203,35 @@ def showNotification(notification: Notification): String = {
(interpolation
(identifier))))))))))

================================================================================
Case class patterns (Scala 3 syntax)
================================================================================

class A:
c match
case c @ City(name = "Hoboken") => c.population

--------------------------------------------------------------------------------

(compilation_unit
(class_definition
(identifier)
(template_body
(match_expression
(identifier)
(indented_cases
(case_clause
(capture_pattern
(identifier)
(case_class_pattern
(type_identifier)
(named_pattern
(identifier)
(string))))
(field_expression
(identifier)
(identifier))))))))

================================================================================
Infix patterns
================================================================================
Expand Down
23 changes: 23 additions & 0 deletions test/corpus/types.txt
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,29 @@ object Main {
(type_identifier)
(type_identifier))))))

================================================================================
Named tuple types (Scala 3 syntax)
================================================================================

object O:
type A = (name: String, age: Int)

--------------------------------------------------------------------------------

(compilation_unit
(object_definition
(identifier)
(template_body
(type_definition
(type_identifier)
(named_tuple_type
(name_and_type
(identifier)
(type_identifier))
(name_and_type
(identifier)
(type_identifier)))))))

================================================================================
Function types
================================================================================
Expand Down
Loading