Skip to content

Commit 5734e9c

Browse files
committed
Adapted grammar to cope with combined next statements for For and For Each loops.
1 parent 3155223 commit 5734e9c

File tree

4 files changed

+28
-12
lines changed

4 files changed

+28
-12
lines changed

Rubberduck.CodeAnalysis/Inspections/Concrete/EmptyBlockInspectionListenerBase.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Rubberduck.VBEditor;
66
using System.Collections.Generic;
77
using System.Diagnostics;
8+
using Antlr4.Runtime.Tree;
89

910
namespace Rubberduck.Inspections.Concrete
1011
{
@@ -35,12 +36,12 @@ public void AddResult(QualifiedContext<ParserRuleContext> qualifiedContext)
3536

3637
private bool BlockContainsExecutableStatements(VBAParser.BlockContext block)
3738
{
38-
return block?.children != null && ContainsExecutableStatements(block);
39+
return block?.children != null && ContainsExecutableStatements(block.children);
3940
}
4041

41-
private bool ContainsExecutableStatements(VBAParser.BlockContext block)
42+
private bool ContainsExecutableStatements(IList<IParseTree> blockChildren)
4243
{
43-
foreach (var child in block.children)
44+
foreach (var child in blockChildren)
4445
{
4546
if (child is VBAParser.BlockStmtContext blockStmt)
4647
{
@@ -76,5 +77,18 @@ child is VBAParser.CommentOrAnnotationContext ||
7677

7778
return false;
7879
}
80+
81+
public void InspectBlockForExecutableStatements<T>(VBAParser.UnterminatedBlockContext block, T context) where T : ParserRuleContext
82+
{
83+
if (!BlockContainsExecutableStatements(block))
84+
{
85+
AddResult(new QualifiedContext<ParserRuleContext>(CurrentModuleName, context));
86+
}
87+
}
88+
89+
private bool BlockContainsExecutableStatements(VBAParser.UnterminatedBlockContext block)
90+
{
91+
return block?.children != null && ContainsExecutableStatements(block.children);
92+
}
7993
}
8094
}

Rubberduck.CodeAnalysis/Inspections/Concrete/EmptyForEachBlockInspection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class EmptyForEachBlockListener : EmptyBlockInspectionListenerBase
3333
{
3434
public override void EnterForEachStmt([NotNull] VBAParser.ForEachStmtContext context)
3535
{
36-
InspectBlockForExecutableStatements(context.block(), context);
36+
InspectBlockForExecutableStatements(context.unterminatedBlock(), context);
3737
}
3838
}
3939
}

Rubberduck.CodeAnalysis/Inspections/Concrete/EmptyForLoopBlockInspection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class EmptyForloopBlockListener : EmptyBlockInspectionListenerBase
3333
{
3434
public override void EnterForNextStmt([NotNull] VBAParser.ForNextStmtContext context)
3535
{
36-
InspectBlockForExecutableStatements(context.block(), context);
36+
InspectBlockForExecutableStatements(context.unterminatedBlock(), context);
3737
}
3838
}
3939
}

Rubberduck.Parsing/Grammar/VBAParser.g4

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ moduleBodyElement :
108108

109109
block : (blockStmt endOfStatement)*;
110110

111-
unterminatedBlock : blockStmt? (endOfStatement blockStmt)*;
111+
unterminatedBlock : blockStmt (endOfStatement blockStmt)*;
112112

113113
blockStmt :
114114
statementLabelDefinition whiteSpace? mainBlockStmt?
@@ -353,16 +353,18 @@ eventStmt : (visibility whiteSpace)? EVENT whiteSpace identifier whiteSpace? arg
353353
exitStmt : EXIT_DO | EXIT_FOR | EXIT_FUNCTION | EXIT_PROPERTY | EXIT_SUB;
354354

355355
forEachStmt :
356-
FOR whiteSpace EACH whiteSpace expression whiteSpace IN whiteSpace expression endOfStatement
357-
block
358-
statementLabelDefinition? whiteSpace? NEXT (whiteSpace expression)?
356+
FOR whiteSpace EACH whiteSpace expression whiteSpace IN whiteSpace expression
357+
(endOfStatement unterminatedBlock)?
358+
(endOfStatement statementLabelDefinition? whiteSpace? NEXT (whiteSpace expression)?
359+
| whiteSpace? COMMA whiteSpace? expression)
359360
;
360361

361362
// expression EQ expression refactored to expression to allow SLL
362363
forNextStmt :
363-
FOR whiteSpace expression whiteSpace TO whiteSpace expression stepStmt? whiteSpace* endOfStatement
364-
block
365-
statementLabelDefinition? whiteSpace? NEXT (whiteSpace expression)?
364+
FOR whiteSpace expression whiteSpace TO whiteSpace expression stepStmt? whiteSpace*
365+
(endOfStatement unterminatedBlock)?
366+
(endOfStatement statementLabelDefinition? whiteSpace? NEXT (whiteSpace expression)?
367+
| whiteSpace? COMMA whiteSpace? expression)
366368
;
367369

368370
stepStmt : whiteSpace STEP whiteSpace expression;

0 commit comments

Comments
 (0)