Skip to content

Commit f6f6708

Browse files
committed
SIP-58 Named Tuples support
**Problem** Named tuples are now available as an experimental feature. **Solution** This implements a support for them.
1 parent 68773df commit f6f6708

File tree

3 files changed

+85
-1
lines changed

3 files changed

+85
-1
lines changed

grammar.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ module.exports = grammar({
9191
[$.class_parameters],
9292
// 'for' operator_identifier ':' _annotated_type • ':' …
9393
[$._type, $.compound_type],
94+
// 'given' '(' operator_identifier ':' _type • ',' …
95+
[$.name_and_type, $.parameter],
96+
[$._simple_expression, $.binding, $.tuple_pattern],
97+
[$._simple_expression, $.tuple_pattern],
98+
[$._simple_expression, $._type_identifier],
9499
// 'if' parenthesized_expression • '{' …
95100
[$._if_condition, $._simple_expression],
96101
// _postfix_expression_choice ':' '(' wildcard • ':' …
@@ -785,6 +790,19 @@ module.exports = grammar({
785790
),
786791
),
787792

793+
/*
794+
* NameAndType ::= id ':' Type
795+
*/
796+
name_and_type: $ =>
797+
prec.left(
798+
PREC.control,
799+
seq(
800+
field("name", $._identifier),
801+
":",
802+
field("type", $._param_type),
803+
),
804+
),
805+
788806
_block: $ =>
789807
prec.left(
790808
seq(
@@ -837,6 +855,7 @@ module.exports = grammar({
837855
$.generic_type,
838856
$.projected_type,
839857
$.tuple_type,
858+
$.named_tuple_type,
840859
$.singleton_type,
841860
$.stable_type_identifier,
842861
$._type_identifier,
@@ -897,6 +916,12 @@ module.exports = grammar({
897916

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

919+
named_tuple_type: $ => seq(
920+
"(",
921+
trailingCommaSep1($.name_and_type),
922+
")",
923+
),
924+
900925
singleton_type: $ =>
901926
prec.left(
902927
PREC.stable_type_id,
@@ -991,6 +1016,7 @@ module.exports = grammar({
9911016
$.interpolated_string_expression,
9921017
$.capture_pattern,
9931018
$.tuple_pattern,
1019+
$.named_tuple_pattern,
9941020
$.case_class_pattern,
9951021
$.infix_pattern,
9961022
$.alternative_pattern,
@@ -1034,16 +1060,26 @@ module.exports = grammar({
10341060

10351061
typed_pattern: $ =>
10361062
prec.right(
1063+
-1,
10371064
seq(field("pattern", $._pattern), ":", field("type", $._type)),
10381065
),
10391066

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

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

10451072
tuple_pattern: $ => seq("(", $._pattern, repeat(seq(",", $._pattern)), ")"),
10461073

1074+
named_pattern: $ => prec.left(-1, seq($._identifier, "=", $._pattern)),
1075+
1076+
named_tuple_pattern: $ => seq(
1077+
"(",
1078+
$.named_pattern,
1079+
repeat(seq(",", $.named_pattern)),
1080+
")",
1081+
),
1082+
10471083
// ---------------------------------------------------------------
10481084
// Expressions
10491085

test/corpus/patterns.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,31 @@ val x = y match {
111111
(identifier))
112112
(identifier))))))
113113

114+
================================================================================
115+
Name tuple patterns (Scala 3 syntax)
116+
================================================================================
117+
118+
val x = y match
119+
case (a = A, b = B) => ???
120+
121+
--------------------------------------------------------------------------------
122+
123+
(compilation_unit
124+
(val_definition
125+
(identifier)
126+
(match_expression
127+
(identifier)
128+
(indented_cases
129+
(case_clause
130+
(named_tuple_pattern
131+
(named_pattern
132+
(identifier)
133+
(identifier))
134+
(named_pattern
135+
(identifier)
136+
(identifier)))
137+
(operator_identifier))))))
138+
114139
================================================================================
115140
Case class patterns
116141
================================================================================

test/corpus/types.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,29 @@ object Main {
9797
(type_identifier)
9898
(type_identifier))))))
9999

100+
================================================================================
101+
Named tuple types (Scala 3 syntax)
102+
================================================================================
103+
104+
object O:
105+
type A = (name: String, age: Int)
106+
107+
--------------------------------------------------------------------------------
108+
109+
(compilation_unit
110+
(object_definition
111+
(identifier)
112+
(template_body
113+
(type_definition
114+
(type_identifier)
115+
(named_tuple_type
116+
(name_and_type
117+
(identifier)
118+
(type_identifier))
119+
(name_and_type
120+
(identifier)
121+
(type_identifier)))))))
122+
100123
================================================================================
101124
Function types
102125
================================================================================

0 commit comments

Comments
 (0)