Skip to content

Commit fb9aa82

Browse files
authored
Merge pull request #446 from eed3si9n/wip/sip-58
SIP-58 Named tuples support
2 parents 68773df + 709057b commit fb9aa82

File tree

3 files changed

+122
-3
lines changed

3 files changed

+122
-3
lines changed

grammar.js

Lines changed: 45 additions & 3 deletions
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,
@@ -1006,7 +1032,10 @@ module.exports = grammar({
10061032
seq(
10071033
field("type", choice($._type_identifier, $.stable_type_identifier)),
10081034
"(",
1009-
field("pattern", trailingCommaSep($._pattern)),
1035+
choice(
1036+
field("pattern", trailingCommaSep($._pattern)),
1037+
field("pattern", trailingCommaSep($.named_pattern)),
1038+
),
10101039
")",
10111040
),
10121041

@@ -1034,15 +1063,28 @@ module.exports = grammar({
10341063

10351064
typed_pattern: $ =>
10361065
prec.right(
1066+
-1,
10371067
seq(field("pattern", $._pattern), ":", field("type", $._type)),
10381068
),
10391069

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

10421072
// TODO: Flatten this.
1043-
alternative_pattern: $ => prec.left(-1, seq($._pattern, "|", $._pattern)),
1073+
alternative_pattern: $ => prec.left(-2, seq($._pattern, "|", $._pattern)),
1074+
1075+
tuple_pattern: $ => seq(
1076+
"(",
1077+
trailingCommaSep1($._pattern),
1078+
")",
1079+
),
1080+
1081+
named_pattern: $ => prec.left(-1, seq($._identifier, "=", $._pattern)),
10441082

1045-
tuple_pattern: $ => seq("(", $._pattern, repeat(seq(",", $._pattern)), ")"),
1083+
named_tuple_pattern: $ => seq(
1084+
"(",
1085+
trailingCommaSep1($.named_pattern),
1086+
")",
1087+
),
10461088

10471089
// ---------------------------------------------------------------
10481090
// Expressions

test/corpus/patterns.txt

Lines changed: 54 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
================================================================================
@@ -178,6 +203,35 @@ def showNotification(notification: Notification): String = {
178203
(interpolation
179204
(identifier))))))))))
180205

206+
================================================================================
207+
Case class patterns (Scala 3 syntax)
208+
================================================================================
209+
210+
class A:
211+
c match
212+
case c @ City(name = "Hoboken") => c.population
213+
214+
--------------------------------------------------------------------------------
215+
216+
(compilation_unit
217+
(class_definition
218+
(identifier)
219+
(template_body
220+
(match_expression
221+
(identifier)
222+
(indented_cases
223+
(case_clause
224+
(capture_pattern
225+
(identifier)
226+
(case_class_pattern
227+
(type_identifier)
228+
(named_pattern
229+
(identifier)
230+
(string))))
231+
(field_expression
232+
(identifier)
233+
(identifier))))))))
234+
181235
================================================================================
182236
Infix patterns
183237
================================================================================

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)