uHSFParser.pas 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. {
  2. TIGCC IDE
  3. Copyright (C) 2004 Fréderic Bour
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software Foundation,
  14. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. }
  16. unit uHSFParser;
  17. interface
  18. uses Classes, uEditor;
  19. type
  20. TOnGetLine = function(const Symbol: string): Integer of object;
  21. procedure ImportFile(const FileName: string; F: TCEditorForm; GetLine: TOnGetLine);
  22. procedure ImportDir(Dir: string; F: TCEditorForm; GetLine: TOnGetLine);
  23. function SimplifyType(T: string): string;
  24. function SimplifyLeft(T: string; out Right: string): string;
  25. function FilterComment(const Comment: string): string;
  26. function FilterDesc(const Def: string): string;
  27. var
  28. DoEnum: Boolean;
  29. implementation
  30. uses SysUtils, StrUtils, UtilsDos;
  31. function SimplifyType(T: string): string;
  32. begin
  33. T := Trim(T);
  34. if T = 'Type' then
  35. Result := '<c:Olive>type'
  36. else if T = 'Function' then
  37. Result := '<c:Teal>func'
  38. else if T = 'Constant' then
  39. Result := '<c:Green>const'
  40. else if T = 'Variable' then
  41. Result := '<c:Maroon>var'
  42. else if T = 'Enum' then
  43. Result := '<c:Olive>enum'
  44. else
  45. Result := T;
  46. end;
  47. function SimplifyLeft(T: string; out Right: string): string;
  48. begin
  49. T := Trim(T);
  50. if AnsiStartsText('typedef', T) then
  51. begin
  52. Right := Copy(T, 9, MaxInt);
  53. Result := '';
  54. end
  55. else if AnsiStartsText('CALLBACK', T) then
  56. begin
  57. Result := '<i>callback';
  58. Right := Trim(Copy(T, 10, MaxInt)) + ' | ';
  59. end
  60. else if T = 'unknown_retval' then
  61. Result := '?'
  62. else if T = '#define' then
  63. Result := ''
  64. else
  65. Result := T;
  66. end;
  67. function FilterComment(const Comment: string): string;
  68. var
  69. i, j: Integer;
  70. begin
  71. Result := '';
  72. i := Pos('/*', Comment);
  73. j := -1;
  74. while i <> 0 do
  75. begin
  76. Result := Result + Copy(Comment, j + 2, i - j - 1);
  77. j := PosEx('*/', Comment, i);
  78. if j = 0 then
  79. begin
  80. j := MaxInt;
  81. Break;
  82. end;
  83. i := PosEx('/*', Comment, j);
  84. end;
  85. Result := Result + Copy(Comment, j + 2, MaxInt);
  86. end;
  87. function FilterDesc(const Def: string): string;
  88. var
  89. i, j: Integer;
  90. begin
  91. Result := '';
  92. i := Pos('<', Def);
  93. j := 0;
  94. while i <> 0 do
  95. begin
  96. Result := Result + Copy(Def, j + 1, i - j - 1);
  97. j := PosEx('>', Def, i);
  98. if j = 0 then
  99. begin
  100. j := MaxInt;
  101. Break;
  102. end;
  103. i := PosEx('<', Def, j);
  104. end;
  105. Result := Result + Copy(Def, j + 1, MaxInt);
  106. end;
  107. procedure FilterEnum(const Definition, Description: string; F: TCEditorForm; Line: Integer);
  108. var
  109. Items: TStringList;
  110. i, p: Integer;
  111. L, R, S: string;
  112. begin
  113. Items := TStringList.Create;
  114. p := Pos('{', Definition);
  115. if p <> 0 then
  116. begin
  117. Inc(p);
  118. Items.CommaText := Copy(Definition, p, PosEx('}', Definition, p) - p);
  119. L := '<c:Olive>' + Trim(Copy(Definition, 1, p - 2));
  120. for i := 0 to Items.Count - 1 do
  121. begin
  122. S := Trim(Items[i]);
  123. p := Pos('=', S);
  124. if P <> 0 then
  125. begin
  126. R := Trim(Copy(S, p + 1, MaxInt));
  127. S := Trim(Copy(S, 1, p - 1));
  128. end
  129. else
  130. R := '';
  131. F.SetItem(S, L, R, Description, Line);
  132. end;
  133. end;
  134. Items.Free;
  135. end;
  136. procedure ImportFile(const FileName: string; F: TCEditorForm; GetLine: TOnGetLine);
  137. var
  138. Lst: TStringList;
  139. Name, Left, Right, Description, Definition, Tmp: string;
  140. i, Line: Integer;
  141. begin
  142. Lst := TStringList.Create;
  143. Lst.LoadFromFile(FileName);
  144. Name := Lst.Values['Name'];
  145. Definition := Lst.Values['Definition'];
  146. Left := FilterComment(SimplifyType(Lst.Values['Type']) + ' <c:Blue>' + SimplifyLeft(Copy(Definition, 1, Pos(Name, Definition) - 1), Tmp));
  147. Right := Tmp + Trim(Copy(Definition, Pos(Name, Definition) + Length(Name), MaxInt));
  148. i := Lst.IndexOf('[Description]');
  149. if i <> -1 then
  150. Description := FilterDesc(Lst[i + 1])
  151. else
  152. Description := '';
  153. if Assigned(GetLine) then
  154. Line := GetLine(Name)
  155. else
  156. Line := 0;
  157. F.SetItem(Name, Left, Right, Description, Line);
  158. if DoEnum and (Lst.Values['SubType'] = 'Enumeration') then
  159. FilterEnum(Definition, Description, F, Line);
  160. Lst.Free;
  161. end;
  162. procedure ImportDir(Dir: string; F: TCEditorForm; GetLine: TOnGetLine);
  163. var
  164. SR: TSearchRec;
  165. begin
  166. Dir := IncludeTrailingPathDelimiter(Dir);
  167. if FindFirst(Dir + '*.*', faAnyFile, SR) = 0 then
  168. begin
  169. repeat
  170. if SR.Name[1] <> '.' then
  171. begin
  172. if SR.Attr and faDirectory = faDirectory then
  173. ImportDir(Dir + SR.Name, F, GetLine)
  174. else if UpperCase(ExtractFileExt(SR.Name)) = '.HSF' then
  175. ImportFile(Dir + SR.Name, F, GetLine);
  176. end;
  177. until FindNext(SR) <> 0;
  178. FindClose(SR);
  179. end;
  180. end;
  181. end.