Skip to content

Commit 7ea8323

Browse files
Vogel612MDoerner
andcommitted
Adjust Grammar update, Add repro-based testcase
This additionally reverts the incorrect modifications to the ParserTests for comments. The added test directly uses the code from Issue 3789, which was suboptimally fixed in bbc0b37 Co-authored-by: Max Doerner <maxdoerner@gmx.net>
1 parent bbc0b37 commit 7ea8323

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

Rubberduck.Parsing/Grammar/VBAParser.g4

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -502,8 +502,9 @@ subroutineName : identifier;
502502
// 5.2.3.3 User Defined Type Declarations
503503
publicTypeDeclaration : ((GLOBAL | PUBLIC) whiteSpace)? udtDeclaration;
504504
privateTypeDeclaration : PRIVATE whiteSpace udtDeclaration;
505-
udtDeclaration : TYPE whiteSpace untypedIdentifier endOfStatement udtMemberList endOfStatement END_TYPE;
506-
udtMemberList : udtMember (endOfStatement udtMember)*;
505+
// member list includes trailing endOfStatement
506+
udtDeclaration : TYPE whiteSpace untypedIdentifier endOfStatement udtMemberList END_TYPE;
507+
udtMemberList : (udtMember endOfStatement)+;
507508
udtMember : reservedNameMemberDeclaration | untypedNameMemberDeclaration;
508509
untypedNameMemberDeclaration : untypedIdentifier whiteSpace? optionalArrayClause;
509510
reservedNameMemberDeclaration : unrestrictedIdentifier whiteSpace asTypeClause;
@@ -872,15 +873,16 @@ endOfStatement :
872873

873874
// Annotations must come before comments because of precedence. ANTLR4 matches as much as possible then chooses the one that comes first.
874875
commentOrAnnotation :
875-
annotationList
876+
(annotationList
876877
| remComment
877-
| comment
878+
| comment)
879+
// all comments must end with a logical line. See VBA Language Spec 3.3.1
880+
(NEWLINE | EOF)
878881
;
879882
remComment : REM whiteSpace? commentBody;
880883
comment : SINGLEQUOTE commentBody;
881-
// commentBody must terminate with a NEWLINE, see VBA Language spec, section 3.3.1 and 3789
882-
commentBody : (LINE_CONTINUATION | ~NEWLINE)* (NEWLINE | EOF);
883-
annotationList : SINGLEQUOTE (AT annotation whiteSpace?)+ (whiteSpace? COLON commentBody)?;
884+
commentBody : (~NEWLINE)*;
885+
annotationList : SINGLEQUOTE (AT annotation whiteSpace?)+ (COLON commentBody)?;
884886
annotation : annotationName annotationArgList?;
885887
annotationName : unrestrictedIdentifier;
886888
annotationArgList :

RubberduckTests/Grammar/VBAParserTests.cs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -440,8 +440,7 @@ public void TestModuleConfig()
440440
[Test]
441441
public void TestEmptyComment()
442442
{
443-
string code = @"'
444-
";
443+
string code = @"'";
445444
var parseResult = Parse(code);
446445
AssertTree(parseResult.Item1, parseResult.Item2, "//comment");
447446
}
@@ -450,8 +449,7 @@ public void TestEmptyComment()
450449
[Test]
451450
public void TestEmptyRemComment()
452451
{
453-
string code = @"Rem
454-
";
452+
string code = @"Rem";
455453
var parseResult = Parse(code);
456454
AssertTree(parseResult.Item1, parseResult.Item2, "//remComment");
457455
}
@@ -460,8 +458,7 @@ public void TestEmptyRemComment()
460458
[Test]
461459
public void TestOneCharRemComment()
462460
{
463-
string code = @"Rem a
464-
";
461+
string code = @"Rem a";
465462
var parseResult = Parse(code);
466463
AssertTree(parseResult.Item1, parseResult.Item2, "//remComment");
467464
}
@@ -470,8 +467,7 @@ public void TestOneCharRemComment()
470467
[Test]
471468
public void TestCommentThatLooksLikeAnnotation()
472469
{
473-
string code = @"'@param foo; the value of something
474-
";
470+
string code = @"'@param foo; the value of something";
475471
var parseResult = Parse(code);
476472
AssertTree(parseResult.Item1, parseResult.Item2, "//comment");
477473
}
@@ -500,8 +496,7 @@ Sub FooFoo()
500496
[Test]
501497
public void TestOneCharComment()
502498
{
503-
string code = @"'a
504-
";
499+
string code = @"'a";
505500
var parseResult = Parse(code);
506501
AssertTree(parseResult.Item1, parseResult.Item2, "//comment");
507502
}
@@ -2185,6 +2180,23 @@ End Sub
21852180
AssertTree(parseResult.Item1, parseResult.Item2, "//variableSubStmt", matches => matches.Count == 1);
21862181
}
21872182

2183+
[Category("Parser")]
2184+
[Test]
2185+
public void UserDefinedType_TreatsFinalCommentAsComment()
2186+
{
2187+
// See Issue #3789
2188+
const string code = @"
2189+
Private Type tX
2190+
foo As String
2191+
bar As Long
2192+
'foobar as shouldNotBeVisible
2193+
End Type
2194+
";
2195+
var parseResult = Parse(code);
2196+
AssertTree(parseResult.Item1, parseResult.Item2, "//udtMember", matches => matches.Count == 2);
2197+
AssertTree(parseResult.Item1, parseResult.Item2, "//commentOrAnnotation", matches => matches.Count == 1);
2198+
}
2199+
21882200
private Tuple<VBAParser, ParserRuleContext> Parse(string code, PredictionMode predictionMode = null)
21892201
{
21902202
var stream = new AntlrInputStream(code);

0 commit comments

Comments
 (0)