Skip to content

Commit 3399616

Browse files
committed
Fix scan failures on immediately-terminated multiline comments
There's a bug in the lookahead for multiline comments where the first character inside the comment is discarded. The effect is that an immediately-terminated comment will erroneously consume input until it finds another terminator.
1 parent 58a870f commit 3399616

File tree

5 files changed

+28
-8
lines changed

5 files changed

+28
-8
lines changed

delphi-frontend/src/main/antlr3/au/com/integradev/delphi/antlr/Delphi.g

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ package au.com.integradev.delphi.antlr;
184184
"if".equalsIgnoreCase(directiveName) || "elseif".equalsIgnoreCase(directiveName);
185185

186186
while (true) {
187-
int character = input.LA(++i);
187+
int character = input.LA(i);
188188

189189
if (character == endStart) {
190190
int j;
@@ -234,6 +234,8 @@ package au.com.integradev.delphi.antlr;
234234
default:
235235
// do nothing
236236
}
237+
238+
++i;
237239
}
238240
}
239241

delphi-frontend/src/main/java/au/com/integradev/delphi/preprocessor/directive/expression/ExpressionLexer.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ private int lookaheadMultilineComment(String end, int i) {
364364
"if".equalsIgnoreCase(directiveName) || "elseif".equalsIgnoreCase(directiveName);
365365

366366
while (true) {
367-
int character = peekChar(++i);
367+
int character = peekChar(i);
368368

369369
if (character == endStart) {
370370
int j;
@@ -409,6 +409,8 @@ private int lookaheadMultilineComment(String end, int i) {
409409
default:
410410
// do nothing
411411
}
412+
413+
++i;
412414
}
413415
}
414416

delphi-frontend/src/test/java/au/com/integradev/delphi/antlr/GrammarTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,4 +338,9 @@ void testUnterminatedCommentShouldThrow() {
338338
.hasCauseInstanceOf(DelphiLexer.LexerException.class)
339339
.hasMessageContaining("line 7:0 unterminated multi-line comment");
340340
}
341+
342+
@Test
343+
void testImmediatelyTerminatedComments() {
344+
assertParsed("ImmediatelyTerminatedComments.pas");
345+
}
341346
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
unit ImmediatelyTerminatedComments;
2+
3+
interface
4+
5+
implementation
6+
7+
//
8+
{}
9+
(**)
10+
11+
end.

delphi-frontend/src/test/resources/au/com/integradev/delphi/preprocessor/Expressions.pas

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ implementation
1414
{$endif}
1515

1616
{$if
17-
123 // }
18-
= { }
19-
321 (* } *)
17+
123 //}
18+
= {}
19+
321 (*}*)
2020
}
2121
{$endif}
2222

2323
{$if
2424
{$if
25-
123 // }
26-
= { }
27-
321 (* } *)
25+
123 //}
26+
= {}
27+
321 (*}*)
2828
}
2929
{$endif}
3030
}

0 commit comments

Comments
 (0)