pasprep.pp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. unit PasPrep;
  2. interface
  3. uses
  4. Comments;
  5. const
  6. PasNesting:longbool=true;
  7. procedure do_pascal(__buf:pointer;size:longint;proc:pointer);
  8. implementation
  9. type
  10. at=array[1..1]of char;
  11. pat=^at;
  12. str255=string[255];
  13. procedure do_pascal(__buf:pointer;size:longint;proc:pointer);
  14. var
  15. old,i:longint;
  16. buf:pat absolute __buf;
  17. const
  18. GetWord_Pos:longint=0;
  19. LastWord:str255='';
  20. StringBody:longbool=false;
  21. procedure GetWord;
  22. begin
  23. LastWord:='';
  24. if GetWord_Pos>size then
  25. exit;
  26. while buf^[GetWord_Pos]<=#32 do
  27. begin
  28. if GetWord_Pos>size then
  29. exit;
  30. inc(GetWord_Pos);
  31. end;
  32. repeat
  33. if buf^[GetWord_Pos]=''''then
  34. StringBody:=not StringBody;
  35. LastWord:=LastWord+upcase(buf^[GetWord_Pos]);
  36. inc(GetWord_Pos);
  37. if GetWord_Pos>size then
  38. break;
  39. if(buf^[GetWord_Pos]in[#0..#32,';'])and not StringBody then
  40. break;
  41. until false;
  42. while(length(LastWord)>1)and(lastWord[1]=';')do
  43. begin
  44. inc(GetWord_Pos);
  45. delete(LastWord,1,1);
  46. end;
  47. end;
  48. function IsTypeDef(pos:longint):longbool;
  49. var
  50. i:longint;
  51. begin
  52. IsTypeDef:=false;
  53. for i:=pos downto 1 do
  54. if buf^[i]>=#32 then
  55. begin
  56. IsTypeDef:=buf^[i]in['=',':'];
  57. exit;
  58. end;
  59. end;
  60. procedure JumpToNext;
  61. var iLastword: Longint;
  62. begin
  63. repeat
  64. iLastword:=GetWord_Pos;
  65. if GetWord_Pos>size then
  66. exit;
  67. GetWord;
  68. i:=GetWord_Pos;
  69. if(LastWord='EXTERNAL')or(LastWord='FORWARD')or(LastWord='INLINE')then
  70. break
  71. else if (LastWord='CONST')then begin
  72. GetWord_Pos:=iLastword;
  73. break;
  74. end;
  75. until false;
  76. end;
  77. procedure JumpToEnd;
  78. var
  79. mainBegin:str255;
  80. procedure do_body;
  81. var
  82. level:longint;
  83. begin
  84. level:=1;
  85. while level>0 do
  86. begin
  87. if GetWord_Pos>size then
  88. exit;
  89. GetWord;
  90. if (LastWord='BEGIN')or(LastWord='ASM')or(LastWord='CASE')then
  91. inc(level)
  92. else if (LastWord='END')then
  93. dec(level);
  94. end;
  95. end;
  96. begin
  97. mainBegin:='BEGIN';
  98. repeat
  99. if GetWord_Pos>size then
  100. exit;
  101. GetWord;
  102. i:=GetWord_Pos;
  103. if((LastWord='PROCEDURE')or(lastword='FUNCTION')or(lastword='OPERATOR'))and not isTypedef(old)then
  104. JumpToEnd
  105. else if(LastWord='EXTERNAL')or(LastWord='FORWARD')or(LastWord='INLINE')then
  106. exit
  107. else if (LastWord='ASSEMBLER')then
  108. mainBegin:='ASM';
  109. until LastWord=mainBegin;
  110. do_body;
  111. end;
  112. procedure do_consts(savefunc:pointer);
  113. type
  114. Tpushfunc=procedure(const key,value:str255;CaseSent:longbool);
  115. var
  116. old,k,kk:longint;
  117. s:str255;
  118. ss:array[1..2]of str255;
  119. pushfunc:Tpushfunc absolute SaveFunc;
  120. begin
  121. repeat
  122. if GetWord_Pos>size then
  123. exit;
  124. old:=GetWord_Pos;
  125. GetWord;
  126. if(((LastWord='PROCEDURE')or(lastword='FUNCTION')or(lastword='OPERATOR'))and not isTypedef(old))
  127. or(lastword='TYPE')
  128. or(lastword='CONST')
  129. or(lastword='VAR')then
  130. begin
  131. GetWord_Pos:=old;
  132. exit;
  133. end
  134. else
  135. begin
  136. s:=LastWord;
  137. while LastWord<>';'do
  138. begin
  139. GetWord;
  140. if GetWord_Pos>size then
  141. exit;
  142. s:=s+LastWord;
  143. end;
  144. if s[length(s)]=';'then
  145. dec(s[0]);
  146. if s<>''then
  147. if pos(':',s)=0 then
  148. if pos('=',s)>0 then
  149. begin
  150. ss[1]:='';
  151. ss[2]:='';
  152. kk:=1;
  153. for k:=1 to length(s)do
  154. begin
  155. if s[k]>#32 then
  156. begin
  157. if(s[k]='=')and(kk=1)then
  158. inc(kk)
  159. else
  160. ss[kk]:=ss[kk]+s[k];
  161. end;
  162. end;
  163. TpushFunc(PushFunc)(ss[1],ss[2],false);
  164. end;
  165. end;
  166. until false;
  167. end;
  168. begin
  169. ClearComments(PasNesting,buf,size);
  170. i:=1;
  171. GetWord_Pos:=0;
  172. while i<=size do
  173. begin
  174. old:=GetWord_Pos;
  175. GetWord;
  176. i:=GetWord_Pos;
  177. if (lastword='OPERATOR')and not isTypedef(old)then
  178. JumpToEnd
  179. else if ((LastWord='PROCEDURE')or(lastword='FUNCTION')) and not isTypedef(old) then
  180. JumpToNext
  181. else if LastWord='CONST'then
  182. Do_Consts(proc)
  183. else if LastWord='IMPLEMENTATION'then
  184. exit;
  185. end;
  186. end;
  187. end.