From 617f16c63bcd524536b2073c7e29ccdb5a7b6abf Mon Sep 17 00:00:00 2001 From: Tom Helm Date: Sat, 27 Jan 2024 09:41:32 +0000 Subject: [PATCH] Fix for #6187, single line if must have something after the Then --- Rubberduck.Parsing/Grammar/VBAParser.g4 | 2 +- RubberduckTests/Grammar/VBAParserTests.cs | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/Rubberduck.Parsing/Grammar/VBAParser.g4 b/Rubberduck.Parsing/Grammar/VBAParser.g4 index c968dccee9..9311ff9176 100644 --- a/Rubberduck.Parsing/Grammar/VBAParser.g4 +++ b/Rubberduck.Parsing/Grammar/VBAParser.g4 @@ -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); singleLineElseClause : ELSE whiteSpace? listOrLabel?; // lineNumberLabel should actually be "statement-label" according to MS VBAL but they only allow lineNumberLabels: diff --git a/RubberduckTests/Grammar/VBAParserTests.cs b/RubberduckTests/Grammar/VBAParserTests.cs index 5fb1c2320b..682734b760 100644 --- a/RubberduckTests/Grammar/VBAParserTests.cs +++ b/RubberduckTests/Grammar/VBAParserTests.cs @@ -4032,6 +4032,20 @@ public void ParserCanDealWithStatementSeparateorsInOneLineIfStatements(string on AssertTree(parseResult.Item1, parseResult.Item2, "//singleLineIfStmt", matches => matches.Count == 1); } + // Adapted from opened issue https://github.com/rubberduck-vba/Rubberduck/issues/6187 + [Test] + public void OneLineIfStatementNotMistakenForIfStatement() + { + string code = @" +Sub Test() + If True Then: A = 1 + If False Then + A = 5 + End If +End 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]