Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recent Grammar issues #6196

Merged
merged 4 commits into from
Jan 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions 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);
singleLineElseClause : ELSE whiteSpace? listOrLabel?;

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

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

// 5.2.3.1.3 Array Dimensions and Bounds
arrayDim : LPAREN whiteSpace? boundsList? whiteSpace? RPAREN;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,10 @@ public void Resolve(VBAParser.SingleLineIfStmtContext context)
if (context.ifWithEmptyThen() != null)
{
ResolveDefault(context.ifWithEmptyThen().booleanExpression());
ResolveListOrLabel(context.ifWithEmptyThen().singleLineElseClause().listOrLabel());
if (context.ifWithEmptyThen().singleLineElseClause() != null)
{
ResolveListOrLabel(context.ifWithEmptyThen().singleLineElseClause().listOrLabel());
}
}
else
{
Expand Down
26 changes: 26 additions & 0 deletions RubberduckTests/Grammar/VBAParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4032,6 +4032,32 @@ 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/6194
[Test]
public void UDTMemberCanHaveArrayWithoutType()
{
string code = @"
Type Test
A(0 To 2)
End Type";
var parseResult = Parse(code);
AssertTree(parseResult.Item1, parseResult.Item2, "//untypedNameMemberDeclaration", matches => matches.Count == 1);
}

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