Skip to content
Snippets Groups Projects
Commit d7fc4a9a authored by Ondřej Štorc's avatar Ondřej Štorc
Browse files

cli: Fix global scope checking for cycle control commands

This commit also adds test for global scope checking.
parent bb568aad
No related branches found
No related tags found
1 merge request!256Parser replacement with ANTLR
......@@ -142,6 +142,10 @@ std::any AltVisitor::visitReturn(AltCliParser::ReturnContext* ctx)
 
std::any AltVisitor::visitCycleControl(AltCliParser::CycleControlContext* ctx)
{
if (isGlobalScope()) {
throw exception::CommonException("Cycle control command available in non-global scope only.");
}
if (ctx->KW_BREAK() != nullptr)
return retPtr<Command, BreakCommand>();
 
......
......@@ -8,12 +8,48 @@ void newParseString(const std::string& str)
newParseFile(std::move(iss), false);
}
 
TEST_CASE("Invalid grammar")
TEST_CASE("Invalid grammar - Scope")
{
SECTION("Global Scope - Undeclare")
SECTION("Undeclare")
{
CHECK_THROWS_AS(newParseString("begin undeclare a (); end"), exception::CommonException);
CHECK_NOTHROW(newParseString("undeclare a ()"));
}
}
 
SECTION("If")
{
CHECK_THROWS_AS(newParseString("if (1) then print 1"), exception::CommonException);
CHECK_NOTHROW(newParseString("begin if (1) then print 1; end"));
}
SECTION("While")
{
CHECK_THROWS_AS(newParseString("while (1) do print 1"), exception::CommonException);
CHECK_NOTHROW(newParseString("begin while (1) do print 1; end"));
}
SECTION("Break")
{
CHECK_THROWS_AS(newParseString("break"), exception::CommonException);
CHECK_NOTHROW(newParseString("begin break; end"));
}
SECTION("Continue")
{
CHECK_THROWS_AS(newParseString("continue"), exception::CommonException);
CHECK_NOTHROW(newParseString("begin continue; end"));
}
SECTION("Function")
{
CHECK_THROWS_AS(newParseString("begin function func () returning auto return 1; end"), exception::CommonException);
CHECK_NOTHROW(newParseString("function func () returning auto return 1"));
}
SECTION("Procedure")
{
CHECK_THROWS_AS(newParseString("begin procedure func () print 1; end"), exception::CommonException);
CHECK_NOTHROW(newParseString("procedure func () print 1"));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment