Skip to content

Commit

Permalink
Merge pull request #6169 from MDoerner/OneLineIfStatementParserFix
Browse files Browse the repository at this point in the history
Make the parser understand completely empty single line if statements
  • Loading branch information
retailcoder authored Oct 24, 2023
2 parents 3dd7ee4 + eb75069 commit 415ae07
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Rubberduck.Parsing/Grammar/VBAParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ elseBlock :
// 5.4.2.9 Single-line If Statement
singleLineIfStmt : ifWithNonEmptyThen | ifWithEmptyThen;
ifWithNonEmptyThen : IF whiteSpace? booleanExpression whiteSpace? THEN whiteSpace? listOrLabel (whiteSpace singleLineElseClause)?;
ifWithEmptyThen : IF whiteSpace? booleanExpression whiteSpace? THEN whiteSpace? emptyThenStatement? singleLineElseClause;
ifWithEmptyThen : IF whiteSpace? booleanExpression whiteSpace? THEN whiteSpace? emptyThenStatement? singleLineElseClause?;
singleLineElseClause : ELSE whiteSpace? listOrLabel?;

// lineNumberLabel should actually be "statement-label" according to MS VBAL but they only allow lineNumberLabels:
Expand Down
21 changes: 21 additions & 0 deletions RubberduckTests/Grammar/VBAParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4012,6 +4012,27 @@ public void ParserCanDealWithMultiplyLineContinuedMemberAccess(string lineContin
AssertTree(parseResult.Item1, parseResult.Item2, "//lExpression", matches => matches.Count == 3);
}


// Adapted from opened issue https://github.com/rubberduck-vba/Rubberduck/issues/6163
[Test]
[TestCase(@"If True Then:")]
[TestCase(@"If True Then: ")]
[TestCase(@"If True Then:
")]
[TestCase(@" If True Then::::::::::::: _
:::::::::::::::: _
:::")]
[TestCase(@" If True Then::::::::::::: _
:::::::::::::::: _
:::Else")]
public void ParserCanDealWithStatementSeparateorsInOneLineIfStatements(string oneLineIfStatement)
{
string code = $"Sub Test()\r\n {oneLineIfStatement}\r\nEnd Sub";
var parseResult = Parse(code);
AssertTree(parseResult.Item1, parseResult.Item2, "//singleLineIfStmt", matches => matches.Count == 1);
}


// Adapted from opened issue https://github.com/rubberduck-vba/Rubberduck/issues/4875
[Test]
[TestCase("form.Line (0, 0)-(12, 12), RGB(255, 255, 0), B")]
Expand Down

0 comments on commit 415ae07

Please sign in to comment.