Skip to content

Commit cba1be6

Browse files
- Fix for return statement when followed by empty lines.
- Better formatting of multiline variable lists - Convert file only once. Relevant when processing large files. Now only writes .pas files when - Save temporary files to temp folder (only relevant if DelphiAST is used)
1 parent dfb4753 commit cba1be6

File tree

7 files changed

+82
-15
lines changed

7 files changed

+82
-15
lines changed

C2Delphi.Forms.Main.pas

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -236,15 +236,17 @@ procedure TfrmMain.BCEditor2CaretChanged(ASender: TObject; X, Y: Integer);
236236
end;
237237

238238
procedure TfrmMain.BCEditor2Change(Sender: TObject);
239-
var fn{$IFDEF USE_DELPHIAST},t{$ENDIF}:string;
239+
{$IFDEF USE_DELPHIAST}
240+
var fn,t:string;
241+
{$ENDIF}
240242
begin
241-
fn := Pas.Name+'.pas';
242-
BCEditor2.Lines.SaveToFile(fn);
243-
244243
{$IFDEF USE_DELPHIAST}
244+
fn := TPath.Combine(TPath.GetTempPath, Pas.Name+'_tmp.pas');
245+
BCEditor2.Lines.SaveToFile(fn);
245246
t := Parse(fn,ex);
246247
ListBox1.Clear;
247248
ListBox1.Items.Add(t);
249+
TFile.Delete(fn);
248250
{$ENDIF}
249251
end;
250252

@@ -427,8 +429,11 @@ procedure TfrmMain.WMDROPFILES(var msg: TWMDropFiles);
427429
begin
428430
DragQueryFile(msg.Drop, cnt, fileName, MAXFILENAME);
429431
cfn := fileName;
430-
p := c_to_pas(ReadCCodeFromFile(cfn),t,changefileext(ExtractFilename(cfn),''));
431-
TFile.WriteAllText( ChangeFileExt(fileName,'.pas'), p.toPascal);
432+
if fileCount>1 then
433+
begin
434+
p := c_to_pas(ReadCCodeFromFile(cfn),t,changefileext(ExtractFilename(cfn),''));
435+
TFile.WriteAllText( ChangeFileExt(fileName,'.pas'), p.toPascal);
436+
end;
432437
end;
433438

434439
if fileCount>0 then

C2Delphi.dproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<ProjectGuid>{3A5D71F7-635F-4E30-A692-1E2E1EC8BE4F}</ProjectGuid>
44
<MainSource>C2Delphi.dpr</MainSource>
55
<Base>True</Base>
6-
<Config Condition="'$(Config)'==''">Debug</Config>
6+
<Config Condition="'$(Config)'==''">Release</Config>
77
<TargetedPlatforms>1</TargetedPlatforms>
88
<AppType>Application</AppType>
99
<FrameworkType>VCL</FrameworkType>

Releases/C2Delphi-0.9.0.zip

1.69 MB
Binary file not shown.

Releases/C2Delphi-0.9.1.zip

2.29 MB
Binary file not shown.

Releases/C2Delphi-0.9.2.zip

2.29 MB
Binary file not shown.

WvN.Pascal.CReader.pas

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ implementation
2222
PARSED_MARKER_STR = PARSED_MARKER+PARSED_MARKER+PARSED_MARKER;
2323

2424
rxID = '(\*?)[a-zA-Z_\$][\w_]*(\*?)';
25-
rxT = '(\*?)(?:unsigned\s+)?(?:long\s+)?[a-zA-Z_\$][\w_]*(\*?)';
25+
rxT = '(\*?)(?:unsigned|signed\s+)?(?:long\s+)?[a-zA-Z_\$][\w_]*(\*?)';
2626
// rxType = '('+rxT+')|('+rxT+'<\s*'+rxT+'\s*>)';
2727
rxType = rxT;
2828
// rxNum = '\d*';
@@ -54,6 +54,55 @@ implementation
5454
type
5555
TLoc=(None,InStringQ1,InStringQ2,InLineComment,InMultiLineComment);
5656

57+
function StripComments(aCCode:string):string;
58+
var
59+
loc:TLoc;
60+
i,j: Integer;
61+
begin
62+
Loc := None;
63+
64+
setlength(result,length(aCCode));
65+
66+
I := 1; J:=1;
67+
while I<=aCCode.length do
68+
begin
69+
case loc of
70+
None:
71+
begin
72+
if I < aCCode.Length-1 then
73+
if aCCode[I] = '/' then
74+
if aCCode[I + 1] = '/' then
75+
Loc := InLineComment;
76+
77+
if I < aCCode.Length then
78+
if aCCode[I] = '/' then
79+
if aCCode[I + 1] = '*' then
80+
Loc := InMultiLineComment;
81+
82+
end;
83+
84+
InLineComment:
85+
if CharInSet(aCCode[I], [#13, #10]) then
86+
Loc := None;
87+
88+
InMultiLineComment:
89+
if I > 1 then
90+
if aCCode[I - 1] = '*' then
91+
if aCCode[I] = '/' then
92+
loc := None;
93+
94+
end;
95+
96+
if loc = None then
97+
begin
98+
Result[J] := aCCode[I];
99+
Inc(J);
100+
end;
101+
inc(I);
102+
end;
103+
Setlength(Result,J);
104+
end;
105+
57106
function ReplaceOutsideCommentsAndStrings(aCCode,aSearch,aReplace:string):string;
58107
var
59108
loc:TLoc;
@@ -525,6 +574,9 @@ function ConvertCLinesToPas(var lines:TArray<string>):string;
525574
expr: string;
526575
begin
527576
c := 0;
577+
// replace lines that contain a variable declaration.
578+
// when it also contains an assignment, leave the assignment,
579+
// otherwise remove the line
528580
setlength(linesAr,length(lines));
529581
for I := 0 to high(lines) do
530582
begin
@@ -542,15 +594,24 @@ function ConvertCLinesToPas(var lines:TArray<string>):string;
542594
end;
543595
end;
544596
end;
597+
598+
// strip emtpy lines at then end
599+
i := length(linesAr)-1;
600+
while (i>=0) and (linesAr[i].Trim='') do
601+
begin
602+
setlength(linesAr,i);
603+
dec(i);
604+
end;
605+
545606
setlength(linesAr,c);
546607
lines := linesAr;
547608

548609
if Length(Lines)>0 then
549610
begin
550-
l := Lines[high(Lines)];
551-
lines[high(Lines)] := TRegEx.Replace(l,'^(\s*)Exit\s*\((?<expr>.*)\)\s*[;]?\s*;?$','\1Result := \2;') ;
552-
l := Lines[high(Lines)];
553-
lines[high(Lines)] := TRegEx.Replace(l,'^(\s*)return\s*(?<expr>[^;]+)\s*;?$','\1Result := \2;') ;
611+
l := Lines[high(Lines)];
612+
lines[high(Lines)] := TRegEx.Replace(l,'^(\s*)Exit\s*\((?<expr>.*)\)\s*[;]?\s*;?$','\1Result := \2;') ;
613+
l := Lines[high(Lines)];
614+
lines[high(Lines)] := TRegEx.Replace(l,'^(\s*)return\s*(?<expr>[^;]+)\s*;?$','\1Result := \2;') ;
554615
end;
555616

556617
for I := 0 to high(lines) do
@@ -1752,6 +1813,7 @@ function c_to_pas(const aCCode:string; var t:string; aName:string='tmp'):TPascal
17521813
Result.usesListIntf.&Unit := Result;
17531814
Result.usesListImpl.&Unit := Result;
17541815
s := aCCode;
1816+
s := StripComments(s);
17551817
FixTypes(s);
17561818

17571819
t := s;

WvN.Pascal.Model.pas

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -397,21 +397,21 @@ function TVariableList.ToPascal(indent:Boolean): String;
397397
begin
398398
Result := Result + Esc(Items[i].name.Trim) + ', ';
399399
if align then
400-
Result := Result + sLineBreak+'';
400+
Result := Result + sLineBreak+' ';
401401
continue;
402402
end;
403403

404404

405405
if Align then
406-
Result := Result + ' '+copy(Esc(Items[i].name)+ StringOfChar(' ',longest) ,1,longest)
406+
Result := Result +copy(Esc(Items[i].name)+ StringOfChar(' ',longest) ,1,longest)
407407
else
408408
Result := Result + Esc(Items[i].name);
409409

410410
if Items[i].&Type<>'' then
411411
begin
412412
Result := Result + ' : ';
413413

414-
if Items[I].&Type='^nil' then
414+
if Items[I].&Type='^' then
415415
Result := Result + 'pointer'
416416
else
417417
begin

0 commit comments

Comments
 (0)