Parsing for while, break, and continue statements, following #340.

This commit is contained in:
Richard Smith
2021-04-19 17:09:54 -07:00
parent 146d995995
commit 153d92b390
4 changed files with 91 additions and 0 deletions
+36
View File
@@ -643,6 +643,42 @@ TEST_F(ParseTreeTest, IfError) {
MatchFileEnd()}));
}
TEST_F(ParseTreeTest, WhileBreakContinue) {
TokenizedBuffer tokens = GetTokenizedBuffer(
"fn F() {\n"
" while (a) {\n"
" if (b)\n"
" break;\n"
" if (c)\n"
" continue;\n"
"}");
ParseTree tree = ParseTree::Parse(tokens, consumer);
EXPECT_FALSE(tree.HasErrors());
EXPECT_THAT(
tree,
MatchParseTreeNodes(
{MatchFunctionDeclaration(
MatchDeclaredName("F"),
MatchParameterList(MatchParameterListEnd()),
MatchCodeBlock(
MatchWhileStatement(
MatchCondition(MatchNameReference("a"),
MatchConditionEnd()),
MatchCodeBlock(
MatchIfStatement(
MatchCondition(MatchNameReference("b"),
MatchConditionEnd()),
MatchBreakStatement(MatchStatementEnd())),
MatchIfStatement(
MatchCondition(MatchNameReference("c"),
MatchConditionEnd()),
MatchContinueStatement(MatchStatementEnd())),
MatchCodeBlockEnd())),
MatchCodeBlockEnd())),
MatchFileEnd()}));
}
auto GetAndDropLine(llvm::StringRef& s) -> std::string {
auto newline_offset = s.find_first_of('\n');
llvm::StringRef line = s.slice(0, newline_offset);