mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-04 14:01:06 +01:00
[toolchain] Parsing support for tuple literal syntax. (#694)
For now, we permit non-empty tuples to have trailing commas, and require a trailing comma if there's exactly one list element. The exact rule here has not yet been decided.
This commit is contained in:
@@ -937,6 +937,39 @@ TEST_F(ParseTreeTest, Return) {
|
||||
MatchFileEnd()}));
|
||||
}
|
||||
|
||||
TEST_F(ParseTreeTest, Tuples) {
|
||||
TokenizedBuffer tokens = GetTokenizedBuffer(R"(
|
||||
var x: (i32, i32) = (1, 2);
|
||||
var y: ((), (), ());
|
||||
)");
|
||||
ParseTree tree = ParseTree::Parse(tokens, consumer);
|
||||
EXPECT_FALSE(tree.HasErrors());
|
||||
|
||||
auto empty_tuple = MatchTupleLiteral(MatchTupleLiteralEnd());
|
||||
|
||||
EXPECT_THAT(
|
||||
tree,
|
||||
MatchParseTreeNodes(
|
||||
{MatchVariableDeclaration(
|
||||
MatchPatternBinding(MatchDeclaredName("x"), ":",
|
||||
MatchTupleLiteral(MatchLiteral("i32"),
|
||||
MatchTupleLiteralComma(),
|
||||
MatchLiteral("i32"),
|
||||
MatchTupleLiteralEnd())),
|
||||
MatchVariableInitializer(MatchTupleLiteral(
|
||||
MatchLiteral("1"), MatchTupleLiteralComma(),
|
||||
MatchLiteral("2"), MatchTupleLiteralEnd())),
|
||||
MatchDeclarationEnd()),
|
||||
MatchVariableDeclaration(
|
||||
MatchPatternBinding(
|
||||
MatchDeclaredName("y"), ":",
|
||||
MatchTupleLiteral(empty_tuple, MatchTupleLiteralComma(),
|
||||
empty_tuple, MatchTupleLiteralComma(),
|
||||
empty_tuple, MatchTupleLiteralEnd())),
|
||||
MatchDeclarationEnd()),
|
||||
MatchFileEnd()}));
|
||||
}
|
||||
|
||||
auto GetAndDropLine(llvm::StringRef& s) -> std::string {
|
||||
auto newline_offset = s.find_first_of('\n');
|
||||
llvm::StringRef line = s.slice(0, newline_offset);
|
||||
|
||||
Reference in New Issue
Block a user