summaryrefslogtreecommitdiff
path: root/utils/fprcp/comments.pp
blob: b878ab2d21665fc7aadf55e21042db0010fa5766 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
unit Comments;
interface
procedure ClearComments(nesting:longbool;__buf:pointer;size:longint);
implementation
procedure ClearComments(nesting:longbool;__buf:pointer;size:longint);
 type
  tat=array[1..2]of char;
  pat=^tat;
  pblock=^tblock;
  tblock=record
   next:pblock;
   _begin,_end:longint;
  end;
 type
  str255=string[255];
 var
  CommLevel:longint;
  buf:pat absolute __buf;
  i,j:longint;
  comm:pblock;
 function TwoChars(const s):str255;
  var
   d:tat absolute s;
   ii:longint;
  begin
   TwoChars:='  ';
   if succ(i)>=size then
    TwoChars:=''
   else
    begin
     ii:=2;
     TwoChars[1]:=d[1];
     TwoChars[ii]:=d[ii];
    end;
  end;
 function FindFrom(position:longint;const Origin:str255):longint;
  var
   j,k:longint;
  begin
   FindFrom:=size;
   for j:=position to Size-length(Origin)do
    begin
     for k:=1 to length(Origin)do
      begin
       if buf^[j+k-1]<>Origin[k]then
        break
       else if k=length(Origin)then
        begin
         FindFrom:=j;
         exit;
        end;
      end;
    end;
  end;
 procedure BeginComment(i:longint);
  var
   c:pBlock;
  begin
   new(c);
   c^.next:=comm;
   c^._begin:=i;
   c^._end:=size;
   comm:=c;
   CommLevel:=1;
  end;
 procedure EndComment(i:longint);
  begin
   if comm<>nil then
    comm^._end:=i;
   dec(CommLevel);
  end;
 procedure DeleteComments;
  var
   i:longint;
   c,cc:pblock;
  begin
   c:=comm;
   while c<>nil do
    begin
     for i:=c^._begin to c^._end do
      buf^[i]:=#32;
     cc:=c;
     c:=c^.next;
     dispose(cc);
    end;
  end;
 begin
  commLevel:=0;
  comm:=nil;
  i:=1;
  while i<size do
   begin
    if commlevel=0 then
     begin
      if buf^[i]=''''then
       i:=FindFrom(succ(i),'''');
      if TwoChars(buf^[i])='//'then
       begin
        BeginComment(i);
        j:=FindFrom(succ(i),#13);
        if j=size then
         j:=FindFrom(succ(i),'#10');
        i:=j;
        EndComment(i);
       end;
      if(buf^[i]='{')or(TwoChars(buf^[i])='(*')then
       BeginComment(i);
     end
    else
     begin
      if(buf^[i]='{')or(TwoChars(buf^[i])='(*')then
       begin
        if nesting then
         inc(CommLevel);
       end;
      if(buf^[i]='}')or(TwoChars(buf^[i])='*)')then
       EndComment(succ(i));
     end;
    inc(i);
   end;
  DeleteComments;
 end;
end.