Skip to content

Commit 45977fc

Browse files
committed
Merge branch 'master' into wip/poly
2 parents 2974f73 + a9123ed commit 45977fc

File tree

8 files changed

+404758
-404556
lines changed

8 files changed

+404758
-404556
lines changed

grammar.js

Lines changed: 106 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const PREC = {
33
using_directive: 2,
44
control: 1,
55
stable_type_id: 2,
6+
type: 2,
67
while: 2,
78
assign: 3,
89
case: 3,
@@ -93,10 +94,19 @@ module.exports = grammar({
9394
[$._type, $.compound_type],
9495
// 'given' '(' '[' _type_parameter • ',' …
9596
[$._variant_type_parameter, $.type_lambda],
97+
// 'given' '(' operator_identifier ':' _type • ',' …
98+
[$.name_and_type, $.parameter],
99+
[$._simple_expression, $.binding, $.tuple_pattern],
100+
[$._simple_expression, $.tuple_pattern],
101+
[$._simple_expression, $._type_identifier],
96102
// 'if' parenthesized_expression • '{' …
97103
[$._if_condition, $._simple_expression],
98-
// _postfix_expression_choice ':' '(' wildcard • ':' …
99-
[$.binding, $._simple_type],
104+
[$.block, $._braced_template_body1],
105+
[$._simple_expression, $.self_type, $._type_identifier],
106+
[$._simple_expression, $._type_identifier],
107+
[$.lambda_expression, $.self_type, $._type_identifier],
108+
[$.lambda_expression, $._type_identifier],
109+
[$.binding, $._simple_expression, $._type_identifier],
100110
],
101111

102112
word: $ => $._alpha_identifier,
@@ -366,7 +376,7 @@ module.exports = grammar({
366376
field("bound", optional($.lower_bound)),
367377
field("bound", optional($.upper_bound)),
368378
field("bound", optional(repeat($.view_bound))),
369-
field("bound", optional(repeat($.context_bound))),
379+
field("bound", optional($._context_bounds)),
370380
),
371381

372382
upper_bound: $ => seq("<:", field("type", $._type)),
@@ -375,7 +385,26 @@ module.exports = grammar({
375385

376386
view_bound: $ => seq("<%", field("type", $._type)),
377387

378-
context_bound: $ => seq(":", field("type", $._type)),
388+
_context_bounds: $ => choice(
389+
repeat1(seq(
390+
":",
391+
$.context_bound
392+
)),
393+
seq(
394+
":",
395+
"{",
396+
trailingCommaSep1($.context_bound),
397+
"}",
398+
)
399+
),
400+
401+
context_bound: $ => seq(
402+
field("type", $._type),
403+
optional(seq(
404+
"as",
405+
field("name", $._identifier),
406+
)),
407+
),
379408

380409
/*
381410
* TemplateBody ::= :<<< [SelfType] TemplateStat {semi TemplateStat} >>>
@@ -522,6 +551,7 @@ module.exports = grammar({
522551
field("type_parameters", optional($.type_parameters)),
523552
field("bound", optional($.lower_bound)),
524553
field("bound", optional($.upper_bound)),
554+
field("bound", optional($._context_bounds)),
525555
),
526556
),
527557

@@ -552,10 +582,14 @@ module.exports = grammar({
552582
prec.right(
553583
seq(
554584
field("name", $._identifier),
555-
field("type_parameters", optional($.type_parameters)),
556585
field(
557586
"parameters",
558-
repeat(seq(optional($._automatic_semicolon), $.parameters)),
587+
repeat(
588+
seq(
589+
optional($._automatic_semicolon),
590+
choice($.parameters, $.type_parameters),
591+
),
592+
),
559593
),
560594
optional($._automatic_semicolon),
561595
),
@@ -595,6 +629,7 @@ module.exports = grammar({
595629
optional($.modifiers),
596630
"given",
597631
optional($._given_constructor),
632+
repeat($._given_sig),
598633
choice(
599634
field("return_type", $._structural_instance),
600635
seq(
@@ -605,6 +640,14 @@ module.exports = grammar({
605640
),
606641
),
607642

643+
_given_sig: $ =>
644+
seq(
645+
$._given_conditional,
646+
"=>"
647+
),
648+
649+
_given_conditional: $ => alias($.parameters, $.given_conditional),
650+
608651
_given_constructor: $ =>
609652
prec.right(
610653
seq(
@@ -627,7 +670,10 @@ module.exports = grammar({
627670
PREC.compound,
628671
seq(
629672
$._constructor_application,
630-
"with",
673+
choice(
674+
":",
675+
"with"
676+
),
631677
field("body", $.with_template_body),
632678
),
633679
),
@@ -783,6 +829,19 @@ module.exports = grammar({
783829
),
784830
),
785831

832+
/*
833+
* NameAndType ::= id ':' Type
834+
*/
835+
name_and_type: $ =>
836+
prec.left(
837+
PREC.control,
838+
seq(
839+
field("name", $._identifier),
840+
":",
841+
field("type", $._param_type),
842+
),
843+
),
844+
786845
_block: $ =>
787846
prec.left(
788847
seq(
@@ -831,14 +890,18 @@ module.exports = grammar({
831890
annotated_type: $ => prec.right(seq($._simple_type, repeat1($.annotation))),
832891

833892
_simple_type: $ =>
834-
choice(
835-
$.generic_type,
836-
$.projected_type,
837-
$.tuple_type,
838-
$.singleton_type,
839-
$.stable_type_identifier,
840-
$._type_identifier,
841-
$.wildcard,
893+
prec.left(
894+
PREC.type,
895+
choice(
896+
$.generic_type,
897+
$.projected_type,
898+
$.tuple_type,
899+
$.named_tuple_type,
900+
$.singleton_type,
901+
$.stable_type_identifier,
902+
$._type_identifier,
903+
$.wildcard,
904+
)
842905
),
843906

844907
compound_type: $ =>
@@ -895,6 +958,12 @@ module.exports = grammar({
895958

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

961+
named_tuple_type: $ => seq(
962+
"(",
963+
trailingCommaSep1($.name_and_type),
964+
")",
965+
),
966+
898967
singleton_type: $ =>
899968
prec.left(
900969
PREC.stable_type_id,
@@ -992,6 +1061,7 @@ module.exports = grammar({
9921061
$.interpolated_string_expression,
9931062
$.capture_pattern,
9941063
$.tuple_pattern,
1064+
$.named_tuple_pattern,
9951065
$.case_class_pattern,
9961066
$.infix_pattern,
9971067
$.alternative_pattern,
@@ -1007,7 +1077,10 @@ module.exports = grammar({
10071077
seq(
10081078
field("type", choice($._type_identifier, $.stable_type_identifier)),
10091079
"(",
1010-
field("pattern", trailingCommaSep($._pattern)),
1080+
choice(
1081+
field("pattern", trailingCommaSep($._pattern)),
1082+
field("pattern", trailingCommaSep($.named_pattern)),
1083+
),
10111084
")",
10121085
),
10131086

@@ -1035,15 +1108,28 @@ module.exports = grammar({
10351108

10361109
typed_pattern: $ =>
10371110
prec.right(
1111+
-1,
10381112
seq(field("pattern", $._pattern), ":", field("type", $._type)),
10391113
),
10401114

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

10431117
// TODO: Flatten this.
1044-
alternative_pattern: $ => prec.left(-1, seq($._pattern, "|", $._pattern)),
1118+
alternative_pattern: $ => prec.left(-2, seq($._pattern, "|", $._pattern)),
1119+
1120+
tuple_pattern: $ => seq(
1121+
"(",
1122+
trailingCommaSep1($._pattern),
1123+
")",
1124+
),
1125+
1126+
named_pattern: $ => prec.left(-1, seq($._identifier, "=", $._pattern)),
10451127

1046-
tuple_pattern: $ => seq("(", $._pattern, repeat(seq(",", $._pattern)), ")"),
1128+
named_tuple_pattern: $ => seq(
1129+
"(",
1130+
trailingCommaSep1($.named_pattern),
1131+
")",
1132+
),
10471133

10481134
// ---------------------------------------------------------------
10491135
// Expressions
@@ -1530,7 +1616,8 @@ module.exports = grammar({
15301616
$.string,
15311617
),
15321618

1533-
literal_type: $ => $._non_null_literal,
1619+
literal_type: $ =>
1620+
prec.left(PREC.type, $._non_null_literal),
15341621

15351622
literal: $ => choice($._non_null_literal, $.null_literal),
15361623

0 commit comments

Comments
 (0)