Skip to content

Commit

Permalink
SIP-58 Named Tuples support
Browse files Browse the repository at this point in the history
**Problem**
Named tuples are now available as an experimental feature.

**Solution**
This implements a support for them.
  • Loading branch information
eed3si9n committed Dec 14, 2024
1 parent 68773df commit f6f6708
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
38 changes: 37 additions & 1 deletion grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ module.exports = grammar({
[$.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 @@ module.exports = grammar({
),
),

/*
* 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 @@ module.exports = grammar({
$.generic_type,
$.projected_type,
$.tuple_type,
$.named_tuple_type,
$.singleton_type,
$.stable_type_identifier,
$._type_identifier,
Expand Down Expand Up @@ -897,6 +916,12 @@ module.exports = grammar({

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 @@ module.exports = grammar({
$.interpolated_string_expression,
$.capture_pattern,
$.tuple_pattern,
$.named_tuple_pattern,
$.case_class_pattern,
$.infix_pattern,
$.alternative_pattern,
Expand Down Expand Up @@ -1034,16 +1060,26 @@ module.exports = grammar({

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("(", $._pattern, repeat(seq(",", $._pattern)), ")"),

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

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

// ---------------------------------------------------------------
// Expressions

Expand Down
25 changes: 25 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
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

0 comments on commit f6f6708

Please sign in to comment.