Skip to content

Commit aca1f3e

Browse files
fourlscirras
authored andcommitted
Support program and library files with no begin
1 parent e00702c commit aca1f3e

File tree

7 files changed

+47
-5
lines changed

7 files changed

+47
-5
lines changed

CHANGELOG.md

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

1010
### Fixed
1111

12+
- Parsing errors on `.dpr` files without a top-level `begin`.
1213
- The `Copy` intrinsic inferred an incorrect return type for `PChar`, `PAnsiChar`, and variants.
1314
- The `Concat` intrinsic inferred an incorrect return type for single-character string literals.
1415
- Ideographic space (U+3000) was erroneously accepted as a valid identifier character.

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,15 @@ fileWithoutImplementation : program | library | unitWithoutImplementation | p
147147
// File head
148148
//----------------------------------------------------------------------------
149149

150-
program : (programHead)? (usesFileClause)? block '.'
150+
program : programHead? usesFileClause? programBody '.'
151151
;
152-
programHead : 'program'<ProgramDeclarationNodeImpl>^ qualifiedNameDeclaration (programParmSeq)? ';'!
152+
programHead : 'program'<ProgramDeclarationNodeImpl>^ qualifiedNameDeclaration programParameters? ';'!
153153
;
154-
programParmSeq : '(' (ident (',' ident)* )? ')'
154+
programParameters : '(' (ident (',' ident)* )? ')' // Used in standard Pascal; Delphi ignores them.
155155
;
156-
library : libraryHead (usesFileClause)? block '.'
156+
programBody : localDeclSection? (compoundStatement | 'end')
157+
;
158+
library : libraryHead usesFileClause? programBody '.'
157159
;
158160
libraryHead : 'library'<LibraryDeclarationNodeImpl>^ qualifiedNameDeclaration (portabilityDirective!)* ';'!
159161
;
@@ -253,7 +255,7 @@ varValueSpec : 'absolute' constExpression
253255
;
254256
exportsSection : 'exports' ident exportItem (',' ident exportItem)* ';'
255257
;
256-
exportItem : ('(' formalParameterList ')')? (INDEX expression)? (NAME expression)? ('resident')?
258+
exportItem : ('(' formalParameterList ')')? ('index' expression)? ('name' expression)? ('resident')?
257259
;
258260

259261
//----------------------------------------------------------------------------

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,26 @@ void testParseKeyWordsAsIdentifier() {
115115
assertParsed("KeyWordsAsIdentifier.pas");
116116
}
117117

118+
@Test
119+
void testParseExportsSection() {
120+
assertParsed("ExportsSection.dpr");
121+
}
122+
123+
@Test
124+
void testParseEmptyProgram() {
125+
assertParsed("EmptyProgram.dpr");
126+
}
127+
128+
@Test
129+
void testParseProgramWithEmptyBlock() {
130+
assertParsed("ProgramWithEmptyBlock.dpr");
131+
}
132+
133+
@Test
134+
void testParseEnd() {
135+
assertParsed("End.dpr");
136+
}
137+
118138
@Test
119139
void testParseListUtils() {
120140
assertParsed("ListUtils.pas");
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
program EmptyProgram;
2+
3+
end.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
end.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
library ExportsSection;
2+
3+
uses System.SysUtils;
4+
5+
exports
6+
Foo name FooBar,
7+
Bar name BarBaz,
8+
Baz index 0,
9+
Flarp name BazFlarp resident;
10+
11+
end.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
program ProgramWithEmptyBlock;
2+
3+
begin
4+
end.

0 commit comments

Comments
 (0)