Skip to content

Commit cd1f206

Browse files
authored
Merge pull request #6196 from tommy9/GrammarIssues
Recent Grammar issues
2 parents 22b5a72 + c7b7d38 commit cd1f206

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

Rubberduck.Parsing/Grammar/VBAParser.g4

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ elseBlock :
421421
// 5.4.2.9 Single-line If Statement
422422
singleLineIfStmt : ifWithNonEmptyThen | ifWithEmptyThen;
423423
ifWithNonEmptyThen : IF whiteSpace? booleanExpression whiteSpace? THEN whiteSpace? listOrLabel (whiteSpace singleLineElseClause)?;
424-
ifWithEmptyThen : IF whiteSpace? booleanExpression whiteSpace? THEN whiteSpace? emptyThenStatement? singleLineElseClause?;
424+
ifWithEmptyThen : IF whiteSpace? booleanExpression whiteSpace? THEN whiteSpace? (emptyThenStatement singleLineElseClause? | singleLineElseClause);
425425
singleLineElseClause : ELSE whiteSpace? listOrLabel?;
426426

427427
// lineNumberLabel should actually be "statement-label" according to MS VBAL but they only allow lineNumberLabels:
@@ -543,12 +543,13 @@ subroutineName : identifier;
543543

544544
// 5.2.3.3 User Defined Type Declarations
545545
// member list includes trailing endOfStatement
546+
// To support actual VBA behaviour, had to change optionalArrayClause to allow a standalone arrayDim without the asTypeClause - see issue #6194
546547
udtDeclaration : (visibility whiteSpace)? TYPE whiteSpace untypedIdentifier endOfStatement udtMemberList END_TYPE;
547548
udtMemberList : (udtMember endOfStatement)+;
548549
udtMember : reservedNameMemberDeclaration | untypedNameMemberDeclaration;
549550
untypedNameMemberDeclaration : untypedIdentifier whiteSpace? optionalArrayClause;
550551
reservedNameMemberDeclaration : unrestrictedIdentifier whiteSpace asTypeClause;
551-
optionalArrayClause : (arrayDim whiteSpace)? asTypeClause;
552+
optionalArrayClause : ((arrayDim whiteSpace)? asTypeClause | arrayDim);
552553

553554
// 5.2.3.1.3 Array Dimensions and Bounds
554555
arrayDim : LPAREN whiteSpace? boundsList? whiteSpace? RPAREN;

Rubberduck.Parsing/VBA/ReferenceManagement/IdentifierReferenceResolver.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,10 @@ public void Resolve(VBAParser.SingleLineIfStmtContext context)
321321
if (context.ifWithEmptyThen() != null)
322322
{
323323
ResolveDefault(context.ifWithEmptyThen().booleanExpression());
324-
ResolveListOrLabel(context.ifWithEmptyThen().singleLineElseClause().listOrLabel());
324+
if (context.ifWithEmptyThen().singleLineElseClause() != null)
325+
{
326+
ResolveListOrLabel(context.ifWithEmptyThen().singleLineElseClause().listOrLabel());
327+
}
325328
}
326329
else
327330
{

RubberduckTests/Grammar/VBAParserTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4032,6 +4032,32 @@ public void ParserCanDealWithStatementSeparateorsInOneLineIfStatements(string on
40324032
AssertTree(parseResult.Item1, parseResult.Item2, "//singleLineIfStmt", matches => matches.Count == 1);
40334033
}
40344034

4035+
// Adapted from opened issue https://github.com/rubberduck-vba/Rubberduck/issues/6187
4036+
[Test]
4037+
public void OneLineIfStatementNotMistakenForIfStatement()
4038+
{
4039+
string code = @"
4040+
Sub Test()
4041+
If True Then: A = 1
4042+
If False Then
4043+
A = 5
4044+
End If
4045+
End Sub";
4046+
var parseResult = Parse(code);
4047+
AssertTree(parseResult.Item1, parseResult.Item2, "//singleLineIfStmt", matches => matches.Count == 1);
4048+
}
4049+
4050+
// Adapted from opened issue https://github.com/rubberduck-vba/Rubberduck/issues/6194
4051+
[Test]
4052+
public void UDTMemberCanHaveArrayWithoutType()
4053+
{
4054+
string code = @"
4055+
Type Test
4056+
A(0 To 2)
4057+
End Type";
4058+
var parseResult = Parse(code);
4059+
AssertTree(parseResult.Item1, parseResult.Item2, "//untypedNameMemberDeclaration", matches => matches.Count == 1);
4060+
}
40354061

40364062
// Adapted from opened issue https://github.com/rubberduck-vba/Rubberduck/issues/4875
40374063
[Test]

0 commit comments

Comments
 (0)