Skip to content

Commit 0595ded

Browse files
committed
Fix parsing errors on implementation keywords in conditional branches
1 parent 2ec51a3 commit 0595ded

File tree

5 files changed

+50
-4
lines changed

5 files changed

+50
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Fixed
11+
12+
- Parsing errors on `implementation` keywords nested in conditional branches.
13+
1014
## [1.10.0] - 2024-10-01
1115

1216
### Added

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ import au.com.integradev.delphi.antlr.ast.node.*;
122122

123123
package au.com.integradev.delphi.antlr;
124124

125+
import org.apache.commons.lang3.StringUtils;
125126
}
126127

127128
@lexer::members {
@@ -1503,9 +1504,9 @@ COMMENT : '//' ~('\n'|'\r')* {$channel
15031504

15041505
if ($text.startsWith(start + "\$")) {
15051506
$type = TkCompilerDirective;
1506-
if ($text.startsWith(start + "\$endif") || $text.startsWith(start + "\$ifend")) {
1507+
if (StringUtils.startsWithIgnoreCase($text, start + "\$endif") || StringUtils.startsWithIgnoreCase($text, start + "\$ifend")) {
15071508
--directiveNesting;
1508-
} else if ($text.startsWith(start + "\$if")) {
1509+
} else if (StringUtils.startsWithIgnoreCase($text, start + "\$if")) {
15091510
++directiveNesting;
15101511
}
15111512
}

delphi-frontend/src/test/java/au/com/integradev/delphi/file/DelphiFileTest.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
*/
1919
package au.com.integradev.delphi.file;
2020

21-
import static org.assertj.core.api.Assertions.assertThat;
22-
import static org.assertj.core.api.Assertions.assertThatThrownBy;
21+
import static org.assertj.core.api.Assertions.*;
2322

2423
import au.com.integradev.delphi.compiler.Platform;
2524
import au.com.integradev.delphi.core.Delphi;
@@ -36,6 +35,8 @@
3635
import java.nio.charset.StandardCharsets;
3736
import java.util.Collections;
3837
import org.junit.jupiter.api.Test;
38+
import org.junit.jupiter.params.ParameterizedTest;
39+
import org.junit.jupiter.params.provider.ValueSource;
3940
import org.sonar.api.batch.fs.InputFile;
4041
import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
4142

@@ -106,4 +107,21 @@ void testByteOrderMarkShouldBeStripped() {
106107
String firstLine = delphiFile.getSourceCodeFileLines().get(0);
107108
assertThat(firstLine).doesNotStartWith("\ufeff");
108109
}
110+
111+
@ParameterizedTest
112+
@ValueSource(strings = {"SkipImplementation.pas", "SkipImplementationWithDirectiveNesting.pas"})
113+
void testShouldSkipImplementation(String filename) {
114+
File file = DelphiUtils.getResource("/au/com/integradev/delphi/file/" + filename);
115+
116+
DelphiFileConfig config =
117+
DelphiFile.createConfig(
118+
StandardCharsets.UTF_8.name(),
119+
new DelphiPreprocessorFactory(Platform.WINDOWS),
120+
TypeFactoryUtils.defaultFactory(),
121+
SearchPath.create(Collections.emptyList()),
122+
Collections.emptySet(),
123+
true);
124+
125+
assertThatNoException().isThrownBy(() -> DelphiFile.from(file, config));
126+
}
109127
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
unit SkipImplementation;
2+
3+
interface
4+
5+
implementation
6+
7+
ERROR
8+
9+
end.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
unit SkipImplementationWithDirectiveNesting;
2+
3+
interface
4+
5+
{$If False}
6+
type
7+
TFoo = ERROR class
8+
emd;
9+
implementation
10+
{$eLsE}
11+
implementation
12+
{$endif}
13+
14+
end.

0 commit comments

Comments
 (0)