pasprep.pp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 JumpToEnd;
  61. var
  62. mainBegin:str255;
  63. procedure do_body;
  64. var
  65. level:longint;
  66. begin
  67. level:=1;
  68. while level>0 do
  69. begin
  70. if GetWord_Pos>size then
  71. exit;
  72. GetWord;
  73. if (LastWord='BEGIN')or(LastWord='ASM')or(LastWord='CASE')then
  74. inc(level)
  75. else if (LastWord='END')then
  76. dec(level);
  77. end;
  78. end;
  79. begin
  80. mainBegin:='BEGIN';
  81. repeat
  82. if GetWord_Pos>size then
  83. exit;
  84. GetWord;
  85. i:=GetWord_Pos;
  86. if((LastWord='PROCEDURE')or(lastword='FUNCTION')or(lastword='OPERATOR'))and not isTypedef(old)then
  87. JumpToEnd
  88. else if(LastWord='EXTERNAL')or(LastWord='FORWARD')or(LastWord='INLINE')then
  89. exit
  90. else if (LastWord='ASSEMBLER')then
  91. mainBegin:='ASM';
  92. until LastWord=mainBegin;
  93. do_body;
  94. end;
  95. procedure do_consts(savefunc:pointer);
  96. type
  97. Tpushfunc=procedure(const key,value:str255;CaseSent:longbool);
  98. var
  99. old,k,kk:longint;
  100. s:str255;
  101. ss:array[1..2]of str255;
  102. pushfunc:Tpushfunc absolute SaveFunc;
  103. begin
  104. repeat
  105. if GetWord_Pos>size then
  106. exit;
  107. old:=GetWord_Pos;
  108. GetWord;
  109. if(((LastWord='PROCEDURE')or(lastword='FUNCTION')or(lastword='OPERATOR'))and not isTypedef(old))
  110. or(lastword='TYPE')
  111. or(lastword='CONST')
  112. or(lastword='VAR')then
  113. begin
  114. GetWord_Pos:=old;
  115. exit;
  116. end
  117. else
  118. begin
  119. s:=LastWord;
  120. while LastWord<>';'do
  121. begin
  122. GetWord;
  123. if GetWord_Pos>size then
  124. exit;
  125. s:=s+LastWord;
  126. end;
  127. if s[length(s)]=';'then
  128. dec(s[0]);
  129. if s<>''then
  130. if pos(':',s)=0 then
  131. if pos('=',s)>0 then
  132. begin
  133. ss[1]:='';
  134. ss[2]:='';
  135. kk:=1;
  136. for k:=1 to length(s)do
  137. begin
  138. if s[k]>#32 then
  139. begin
  140. if(s[k]='=')and(kk=1)then
  141. inc(kk)
  142. else
  143. ss[kk]:=ss[kk]+s[k];
  144. end;
  145. end;
  146. TpushFunc(PushFunc)(ss[1],ss[2],false);
  147. end;
  148. end;
  149. until false;
  150. end;
  151. begin
  152. ClearComments(PasNesting,buf,size);
  153. i:=1;
  154. while i<=size do
  155. begin
  156. old:=GetWord_Pos;
  157. GetWord;
  158. i:=GetWord_Pos;
  159. if((LastWord='PROCEDURE')or(lastword='FUNCTION')or(lastword='OPERATOR'))and not isTypedef(old)then
  160. JumpToEnd
  161. else if LastWord='CONST'then
  162. Do_Consts(proc)
  163. else if LastWord='IMPLEMENTATION'then
  164. exit;
  165. end;
  166. end;
  167. end.