Skip to content

Commit

Permalink
Improving blocks parsing in case of errors
Browse files Browse the repository at this point in the history
  • Loading branch information
pachanga committed Nov 22, 2024
1 parent 0e6e166 commit b0165db
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/compile/antlr_proc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4914,7 +4914,7 @@ public override object VisitStmParal(bhlParser.StmParalContext ctx)
LSP_AddSemanticToken(ctx.PARAL(), SemanticToken.Keyword);

var block = ProcBlock(BlockType.PARAL, ctx.block()?.statement());
if(block.children.Count == 0)
if(block == null)
AddError(ctx, "empty paral blocks are not allowed");
return null;
}
Expand All @@ -4924,7 +4924,7 @@ public override object VisitStmParalAll(bhlParser.StmParalAllContext ctx)
LSP_AddSemanticToken(ctx.PARAL_ALL(), SemanticToken.Keyword);

var block = ProcBlock(BlockType.PARAL_ALL, ctx.block()?.statement());
if(block.children.Count == 0)
if(block == null)
AddError(ctx, "empty paral blocks are not allowed");
return null;
}
Expand Down Expand Up @@ -5123,11 +5123,16 @@ public override object VisitStmWhile(bhlParser.StmWhileContext ctx)

PopBlock(ast);

return_found.Remove(PeekFuncDecl());
BlockResetsCurrentFunctionReturnInfo();

return null;
}

void BlockResetsCurrentFunctionReturnInfo()
{
return_found.Remove(PeekFuncDecl());
}

public override object VisitStmDoWhile(bhlParser.StmDoWhileContext ctx)
{
LSP_AddSemanticToken(ctx.DO(), SemanticToken.Keyword);
Expand Down Expand Up @@ -5158,7 +5163,7 @@ public override object VisitStmDoWhile(bhlParser.StmDoWhileContext ctx)

PopBlock(ast);

return_found.Remove(PeekFuncDecl());
BlockResetsCurrentFunctionReturnInfo();

return null;
}
Expand Down Expand Up @@ -5222,7 +5227,7 @@ public override object VisitStmFor(bhlParser.StmForContext ctx)
local_scope.Exit();
PopScope();

return_found.Remove(PeekFuncDecl());
BlockResetsCurrentFunctionReturnInfo();

return null;
}
Expand Down Expand Up @@ -5653,10 +5658,15 @@ AST_Block ProcBlock(BlockType type, IParseTree[] sts)
PopScope();

if(is_paral)
return_found.Remove(PeekFuncDecl());
BlockResetsCurrentFunctionReturnInfo();

PopBlock(ast);

//NOTE: if there are no children, something is definitely wrong
// probably due to parsing errors
if(ast.children.Count == 0)
return null;

PeekAST().AddChild(ast);
return ast;
}
Expand Down
25 changes: 25 additions & 0 deletions tests/test_vm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4185,6 +4185,31 @@ func int test()
AssertEqual(3, Execute(vm, "test").result.PopRelease().num);
CommonChecks(vm);
}

[Fact]
public void TestParseErrorInDoWhileBlockWithReturn()
{
string bhl = @"
func int test()
{
string i;
do {
return i
} while(true)
}
";
AssertError<Exception>(
delegate() {
Compile(bhl);
},
"incompatible types: 'int' and 'string'",
new PlaceAssert(bhl, @"
return i
---------------^"
)
);
}

[Fact]
public void TestBreakInDoWhile()
Expand Down

0 comments on commit b0165db

Please sign in to comment.