Skip to content

Commit 9b85c6f

Browse files
authored
Merge pull request #5589 from MDoerner/LineContinuationsInPrecompilerDirectives
Allow line continuations in precompiler directives
2 parents 4797188 + 3c70bbb commit 9b85c6f

File tree

2 files changed

+85
-23
lines changed

2 files changed

+85
-23
lines changed

Rubberduck.Parsing/Preprocessing/VBAConditionalCompilationParser.g4

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ ccBlock :
1010
(ccConst | ccIfBlock | physicalLine)*?
1111
;
1212

13-
ccConst : WS* hashConst WS+ ccVarLhs WS* EQ WS* ccExpression ccEol;
13+
ccConst : whiteSpace* hashConst whiteSpace+ ccVarLhs whiteSpace* EQ whiteSpace* ccExpression ccEol;
1414
ccVarLhs : name;
1515

1616
ccIfBlock : ccIf ccBlock ccElseIfBlock* ccElseBlock? ccEndIf;
17-
ccIf : WS* hashIf WS+ ccExpression WS+ THEN ccEol;
17+
ccIf : whiteSpace* hashIf whiteSpace+ ccExpression whiteSpace+ THEN ccEol;
1818
ccElseIfBlock : ccElseIf ccBlock;
19-
ccElseIf : WS* hashElseIf WS+ ccExpression WS+ THEN ccEol;
19+
ccElseIf : whiteSpace* hashElseIf whiteSpace+ ccExpression whiteSpace+ THEN ccEol;
2020
ccElseBlock : ccElse ccBlock;
21-
ccElse : WS* hashElse ccEol;
22-
ccEndIf : WS* hashEndIf ccEol;
23-
ccEol : WS* comment? (NEWLINE | EOF);
21+
ccElse : whiteSpace* hashElse ccEol;
22+
ccEndIf : whiteSpace* hashEndIf ccEol;
23+
ccEol : whiteSpace* comment? (NEWLINE | EOF);
2424
// We use parser rules instead of tokens (such as HASHCONST) because
2525
// marked file numbers have a similar format and cause conflicts.
2626
hashConst : HASH CONST;
@@ -32,26 +32,26 @@ hashEndIf : HASH END_IF;
3232
physicalLine : ~(NEWLINE | EOF)* (NEWLINE | EOF);
3333

3434
ccExpression :
35-
LPAREN WS* ccExpression WS* RPAREN
36-
| ccExpression WS* POW WS* ccExpression
37-
| MINUS WS* ccExpression
38-
| ccExpression WS* (MULT | DIV) WS* ccExpression
39-
| ccExpression WS* INTDIV WS* ccExpression
40-
| ccExpression WS* MOD WS* ccExpression
41-
| ccExpression WS* (PLUS | MINUS) WS* ccExpression
42-
| ccExpression WS* AMPERSAND WS* ccExpression
43-
| ccExpression WS* (EQ | NEQ | LT | GT | LEQ | GEQ | LIKE | IS) WS* ccExpression
44-
| NOT WS* ccExpression
45-
| ccExpression WS* AND WS* ccExpression
46-
| ccExpression WS* OR WS* ccExpression
47-
| ccExpression WS* XOR WS* ccExpression
48-
| ccExpression WS* EQV WS* ccExpression
49-
| ccExpression WS* IMP WS* ccExpression
35+
LPAREN whiteSpace* ccExpression whiteSpace* RPAREN
36+
| ccExpression whiteSpace* POW whiteSpace* ccExpression
37+
| MINUS whiteSpace* ccExpression
38+
| ccExpression whiteSpace* (MULT | DIV) whiteSpace* ccExpression
39+
| ccExpression whiteSpace* INTDIV whiteSpace* ccExpression
40+
| ccExpression whiteSpace* MOD whiteSpace* ccExpression
41+
| ccExpression whiteSpace* (PLUS | MINUS) whiteSpace* ccExpression
42+
| ccExpression whiteSpace* AMPERSAND whiteSpace* ccExpression
43+
| ccExpression whiteSpace* (EQ | NEQ | LT | GT | LEQ | GEQ | LIKE | IS) whiteSpace* ccExpression
44+
| NOT whiteSpace* ccExpression
45+
| ccExpression whiteSpace* AND whiteSpace* ccExpression
46+
| ccExpression whiteSpace* OR whiteSpace* ccExpression
47+
| ccExpression whiteSpace* XOR whiteSpace* ccExpression
48+
| ccExpression whiteSpace* EQV whiteSpace* ccExpression
49+
| ccExpression whiteSpace* IMP whiteSpace* ccExpression
5050
| intrinsicFunction
5151
| literal
5252
| name;
5353

54-
intrinsicFunction : intrinsicFunctionName LPAREN WS* ccExpression WS* RPAREN;
54+
intrinsicFunction : intrinsicFunctionName LPAREN whiteSpace* ccExpression whiteSpace* RPAREN;
5555

5656
intrinsicFunctionName :
5757
INT
@@ -270,4 +270,6 @@ statementKeyword :
270270
| WEND
271271
| WHILE
272272
| WITH
273-
;
273+
;
274+
275+
whiteSpace : (WS | LINE_CONTINUATION)+;

RubberduckTests/Preprocessing/VBAPreprocessorTests.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,66 @@ public void TestPreprocessor()
2727
}
2828
}
2929

30+
[Test]
31+
[Category("Preprocessor")]
32+
//See issue #5294 at https://github.com/rubberduck-vba/Rubberduck/issues/5294
33+
public void CanDealWithLineContinuationsInPrecompilerDirectives()
34+
{
35+
const string code = @"
36+
Private Sub Main()
37+
Dim a as Long: a= 10
38+
Dim b as Long: b=5
39+
Dim c as Long : c=a+b
40+
41+
#Const CCG_VERSION1 _
42+
= _
43+
True
44+
45+
#Const CCG_VERSION2 = _
46+
False
47+
48+
#If CCG_VERSION1 Or _
49+
CCG_VERSION2 Then
50+
51+
c=c+c
52+
#else
53+
c=c*c
54+
#end if
55+
56+
Print c
57+
58+
End Sub
59+
";
60+
const string expectedProcessed = @"
61+
Private Sub Main()
62+
Dim a as Long: a= 10
63+
Dim b as Long: b=5
64+
Dim c as Long : c=a+b
65+
66+
_
67+
_
68+
69+
70+
_
71+
72+
73+
_
74+
75+
76+
c=c+c
77+
78+
79+
80+
81+
Print c
82+
83+
End Sub
84+
";
85+
86+
var actualProcessed = Parse(code);
87+
Assert.AreEqual(expectedProcessed, actualProcessed);
88+
}
89+
3090
private void AssertParseResult(string filename, string originalCode, string materializedParseTree)
3191
{
3292
Assert.AreEqual(originalCode, materializedParseTree, $"{filename} mismatch detected.");

0 commit comments

Comments
 (0)