[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:
Richard Smith
2021-08-02 16:18:18 -07:00
committed by GitHub
parent a83c22288f
commit b1993a6cd0
4 changed files with 73 additions and 21 deletions
+33
View File
@@ -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);