feat: 添加对数组和对象字面量的尾随逗号支持,更新相关测试用例

This commit is contained in:
0264408
2026-06-16 19:40:24 +08:00
parent a7bedf6fa1
commit bf705069a5
3 changed files with 206 additions and 0 deletions
+8
View File
@@ -567,6 +567,10 @@ impl Parser {
if !self.match_kind(&[TokenKind::Comma]) {
break;
}
// Allow trailing comma
if self.check(&TokenKind::RightBrace) {
break;
}
}
}
self.consume(TokenKind::RightBrace, "Expected '}' after object literal")?;
@@ -581,6 +585,10 @@ impl Parser {
if !self.match_kind(&[TokenKind::Comma]) {
break;
}
// Allow trailing comma
if self.check(&TokenKind::RightBracket) {
break;
}
}
}
self.consume(TokenKind::RightBracket, "Expected ']' after array literal")?;
+16
View File
@@ -551,6 +551,22 @@
}
}
#[test]
fn parse_array_trailing_comma() {
match parse_expr("[1, 2, 3,]") {
Expr::ArrayLiteral { elements } => assert_eq!(elements.len(), 3),
e => panic!("Expected ArrayLiteral, got {:?}", e),
}
}
#[test]
fn parse_object_trailing_comma() {
match parse_expr("{a: 1, b: 2,}") {
Expr::ObjectLiteral { properties } => assert_eq!(properties.len(), 2),
e => panic!("Expected ObjectLiteral, got {:?}", e),
}
}
// ----- statements -----
#[test]