Skip to content

Commit d570b17

Browse files
committed
Allow trailing commas in MappingPattern
1 parent 6d5bbd9 commit d570b17

File tree

4 files changed

+983
-4
lines changed

4 files changed

+983
-4
lines changed

compiler/parser/python.lalrpop

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,7 @@ MappingPattern: ast::PatternKind = {
801801
rest: None,
802802
};
803803
},
804-
<location:@L> "{" <e:OneOrMore<MatchMappingEntry>> "}" <end_location:@R> => {
804+
<location:@L> "{" <e:OneOrMore<MatchMappingEntry>> ","? "}" <end_location:@R> => {
805805
let (keys, patterns) = e
806806
.into_iter()
807807
.unzip();
@@ -811,14 +811,14 @@ MappingPattern: ast::PatternKind = {
811811
rest: None,
812812
};
813813
},
814-
<location:@L> "{" "**" <rest:Identifier> "}" <end_location:@R> => {
814+
<location:@L> "{" "**" <rest:Identifier> ","? "}" <end_location:@R> => {
815815
return ast::PatternKind::MatchMapping {
816816
keys: vec![],
817817
patterns: vec![],
818818
rest: Some(rest),
819819
};
820820
},
821-
<location:@L> "{" <e:OneOrMore<MatchMappingEntry>> "," "**" <rest:Identifier> "}" <end_location:@R> => {
821+
<location:@L> "{" <e:OneOrMore<MatchMappingEntry>> "," "**" <rest:Identifier> ","? "}" <end_location:@R> => {
822822
let (keys, patterns) = e
823823
.into_iter()
824824
.unzip();

compiler/parser/src/parser.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ match match:
497497
}
498498

499499
#[test]
500-
fn test_match_complex() {
500+
fn test_patma() {
501501
let source = r#"# Cases sampled from Lib/test/test_patma.py
502502
503503
# case test_patma_098
@@ -666,4 +666,34 @@ match w := x,:
666666
let parse_ast = parse_program(source, "<test>").unwrap();
667667
insta::assert_debug_snapshot!(parse_ast);
668668
}
669+
670+
#[test]
671+
fn test_match() {
672+
let parse_ast = parse_program(
673+
r#"
674+
match {"test": 1}:
675+
case {
676+
**rest,
677+
}:
678+
print(rest)
679+
match {"label": "test"}:
680+
case {
681+
"label": str() | None as label,
682+
}:
683+
print(label)
684+
match x:
685+
case [0, 1,]:
686+
y = 0
687+
match x:
688+
case (0, 1,):
689+
y = 0
690+
match x:
691+
case (0,):
692+
y = 0
693+
"#,
694+
"<test>",
695+
)
696+
.unwrap();
697+
insta::assert_debug_snapshot!(parse_ast);
698+
}
669699
}

0 commit comments

Comments
 (0)