@@ -22,7 +22,7 @@ implementation
22
22
PARSED_MARKER_STR = PARSED_MARKER+PARSED_MARKER+PARSED_MARKER;
23
23
24
24
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_]*(\*?)' ;
26
26
// rxType = '('+rxT+')|('+rxT+'<\s*'+rxT+'\s*>)';
27
27
rxType = rxT;
28
28
// rxNum = '\d*';
@@ -54,6 +54,55 @@ implementation
54
54
type
55
55
TLoc=(None,InStringQ1,InStringQ2,InLineComment,InMultiLineComment);
56
56
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
+
57
106
function ReplaceOutsideCommentsAndStrings (aCCode,aSearch,aReplace:string):string;
58
107
var
59
108
loc:TLoc;
@@ -525,6 +574,9 @@ function ConvertCLinesToPas(var lines:TArray<string>):string;
525
574
expr: string;
526
575
begin
527
576
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
528
580
setlength(linesAr,length(lines));
529
581
for I := 0 to high(lines) do
530
582
begin
@@ -542,15 +594,24 @@ function ConvertCLinesToPas(var lines:TArray<string>):string;
542
594
end ;
543
595
end ;
544
596
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
+
545
606
setlength(linesAr,c);
546
607
lines := linesAr;
547
608
548
609
if Length(Lines)>0 then
549
610
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;' ) ;
554
615
end ;
555
616
556
617
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
1752
1813
Result.usesListIntf.&Unit := Result;
1753
1814
Result.usesListImpl.&Unit := Result;
1754
1815
s := aCCode;
1816
+ s := StripComments(s);
1755
1817
FixTypes(s);
1756
1818
1757
1819
t := s;
0 commit comments