Skip to content

Commit

Permalink
- Fix for return statement when followed by empty lines.
Browse files Browse the repository at this point in the history
- 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)
WouterVanNifterick committed Dec 14, 2016

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
1 parent dfb4753 commit cba1be6
Showing 7 changed files with 82 additions and 15 deletions.
17 changes: 11 additions & 6 deletions C2Delphi.Forms.Main.pas
Original file line number Diff line number Diff line change
@@ -236,15 +236,17 @@ procedure TfrmMain.BCEditor2CaretChanged(ASender: TObject; X, Y: Integer);
end;

procedure TfrmMain.BCEditor2Change(Sender: TObject);
var fn{$IFDEF USE_DELPHIAST},t{$ENDIF}:string;
{$IFDEF USE_DELPHIAST}
var fn,t:string;
{$ENDIF}
begin
fn := Pas.Name+'.pas';
BCEditor2.Lines.SaveToFile(fn);

{$IFDEF USE_DELPHIAST}
fn := TPath.Combine(TPath.GetTempPath, Pas.Name+'_tmp.pas');
BCEditor2.Lines.SaveToFile(fn);
t := Parse(fn,ex);
ListBox1.Clear;
ListBox1.Items.Add(t);
TFile.Delete(fn);
{$ENDIF}
end;

@@ -427,8 +429,11 @@ procedure TfrmMain.WMDROPFILES(var msg: TWMDropFiles);
begin
DragQueryFile(msg.Drop, cnt, fileName, MAXFILENAME);
cfn := fileName;
p := c_to_pas(ReadCCodeFromFile(cfn),t,changefileext(ExtractFilename(cfn),''));
TFile.WriteAllText( ChangeFileExt(fileName,'.pas'), p.toPascal);
if fileCount>1 then
begin
p := c_to_pas(ReadCCodeFromFile(cfn),t,changefileext(ExtractFilename(cfn),''));
TFile.WriteAllText( ChangeFileExt(fileName,'.pas'), p.toPascal);
end;
end;

if fileCount>0 then
2 changes: 1 addition & 1 deletion C2Delphi.dproj
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@
<ProjectGuid>{3A5D71F7-635F-4E30-A692-1E2E1EC8BE4F}</ProjectGuid>
<MainSource>C2Delphi.dpr</MainSource>
<Base>True</Base>
<Config Condition="'$(Config)'==''">Debug</Config>
<Config Condition="'$(Config)'==''">Release</Config>
<TargetedPlatforms>1</TargetedPlatforms>
<AppType>Application</AppType>
<FrameworkType>VCL</FrameworkType>
Binary file added Releases/C2Delphi-0.9.0.zip
Binary file not shown.
Binary file added Releases/C2Delphi-0.9.1.zip
Binary file not shown.
Binary file added Releases/C2Delphi-0.9.2.zip
Binary file not shown.
72 changes: 67 additions & 5 deletions WvN.Pascal.CReader.pas
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@ implementation
PARSED_MARKER_STR = PARSED_MARKER+PARSED_MARKER+PARSED_MARKER;

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

function StripComments(aCCode:string):string;
var
loc:TLoc;
i,j: Integer;
begin
Loc := None;

setlength(result,length(aCCode));

I := 1; J:=1;
while I<=aCCode.length do
begin
case loc of
None:
begin
if I < aCCode.Length-1 then
if aCCode[I] = '/' then
if aCCode[I + 1] = '/' then
Loc := InLineComment;

if I < aCCode.Length then
if aCCode[I] = '/' then
if aCCode[I + 1] = '*' then
Loc := InMultiLineComment;

end;

InLineComment:
if CharInSet(aCCode[I], [#13, #10]) then
Loc := None;

InMultiLineComment:
if I > 1 then
if aCCode[I - 1] = '*' then
if aCCode[I] = '/' then
loc := None;

end;

if loc = None then
begin
Result[J] := aCCode[I];
Inc(J);
end;
inc(I);
end;
Setlength(Result,J);
end;

function ReplaceOutsideCommentsAndStrings(aCCode,aSearch,aReplace:string):string;
var
loc:TLoc;
@@ -525,6 +574,9 @@ function ConvertCLinesToPas(var lines:TArray<string>):string;
expr: string;
begin
c := 0;
// replace lines that contain a variable declaration.
// when it also contains an assignment, leave the assignment,
// otherwise remove the line
setlength(linesAr,length(lines));
for I := 0 to high(lines) do
begin
@@ -542,15 +594,24 @@ function ConvertCLinesToPas(var lines:TArray<string>):string;
end;
end;
end;

// strip emtpy lines at then end
i := length(linesAr)-1;
while (i>=0) and (linesAr[i].Trim='') do
begin
setlength(linesAr,i);
dec(i);
end;

setlength(linesAr,c);
lines := linesAr;

if Length(Lines)>0 then
begin
l := Lines[high(Lines)];
lines[high(Lines)] := TRegEx.Replace(l,'^(\s*)Exit\s*\((?<expr>.*)\)\s*[;]?\s*;?$','\1Result := \2;') ;
l := Lines[high(Lines)];
lines[high(Lines)] := TRegEx.Replace(l,'^(\s*)return\s*(?<expr>[^;]+)\s*;?$','\1Result := \2;') ;
l := Lines[high(Lines)];
lines[high(Lines)] := TRegEx.Replace(l,'^(\s*)Exit\s*\((?<expr>.*)\)\s*[;]?\s*;?$','\1Result := \2;') ;
l := Lines[high(Lines)];
lines[high(Lines)] := TRegEx.Replace(l,'^(\s*)return\s*(?<expr>[^;]+)\s*;?$','\1Result := \2;') ;
end;

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
Result.usesListIntf.&Unit := Result;
Result.usesListImpl.&Unit := Result;
s := aCCode;
s := StripComments(s);
FixTypes(s);

t := s;
6 changes: 3 additions & 3 deletions WvN.Pascal.Model.pas
Original file line number Diff line number Diff line change
@@ -397,21 +397,21 @@ function TVariableList.ToPascal(indent:Boolean): String;
begin
Result := Result + Esc(Items[i].name.Trim) + ', ';
if align then
Result := Result + sLineBreak+'';
Result := Result + sLineBreak+' ';
continue;
end;


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

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

if Items[I].&Type='^nil' then
if Items[I].&Type='^' then
Result := Result + 'pointer'
else
begin

0 comments on commit cba1be6

Please sign in to comment.