dts2pas.pp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. { *********************************************************************
  2. This file is part of the Free Component Library (FCL)
  3. Copyright (c) 2021 Michael Van Canneyt.
  4. Typescript declaration module conversion to pascal program.
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. program dts2pas;
  12. {$mode objfpc}{$H+}
  13. {AllowWriteln}
  14. uses
  15. Classes, SysUtils, StrUtils, CustApp, pascodegen, tstopas;
  16. type
  17. { TParseTSApplication }
  18. TParseTSApplication = class(TCustomApplication)
  19. private
  20. FVerbose,
  21. FWeb : Boolean;
  22. FLinks,
  23. FUnits,
  24. FAliases : TStringArray;
  25. procedure AddAliases(Converter: TTypescriptToPas; aAlias: String);
  26. procedure AddWebAliases(S: Tstrings);
  27. procedure AddJSAliases(S: Tstrings);
  28. procedure DoLog(Sender: TObject; LogType: TCodegenLogType; const Msg: String);
  29. function ParseFile(const aInputFileName, aOutputFileName, aUnitName: string): Boolean;
  30. protected
  31. procedure DoRun; override;
  32. public
  33. constructor Create(TheOwner: TComponent); override;
  34. destructor Destroy; override;
  35. procedure Usage(Msg : string); virtual;
  36. end;
  37. { TParseTSApplication }
  38. procedure TParseTSApplication.DoRun;
  39. var
  40. ErrorMsg: String;
  41. aUnitName,InputFile,OutputFile : String;
  42. begin
  43. Terminate;
  44. ErrorMsg:=CheckOptions('hi:o:a:wx:u:vl:', ['help','input:','output:','alias:','web','extra-units:','unitname:','verbose','link:']);
  45. if (ErrorMsg<>'') or HasOption('h','help') then
  46. begin
  47. Usage(ErrorMsg);
  48. Exit;
  49. end;
  50. InputFile:=GetOptionValue('i','input');
  51. OutputFile:=GetOptionValue('o','output');
  52. FAliases:=GetOptionValues('a','alias');
  53. FLinks:=GetOptionValues('l','link');
  54. FUnits:=GetOptionValues('x','extra-units');
  55. FWeb:=HasOption('w','web');
  56. FVerbose:=HasOption('v','verbose');
  57. If OutputFile='' then
  58. if InputFile.EndsWith('d.ts') then
  59. OutputFile:=ChangeFileExt(ChangeFileExt(InputFile,''),'.pp')
  60. else
  61. OutputFile:=ChangeFileExt(InputFile,'.pp');
  62. aUnitName:=GetOptionValue('u','unitname');
  63. if aUnitName='' then
  64. aUnitName:=ChangeFileExt(ExtractFileName(outputFile),'');
  65. if not ParseFile(InputFIle,OutputFile,aUnitName) then
  66. ExitCode:=1;
  67. end;
  68. procedure TParseTSApplication.AddAliases(Converter : TTypescriptToPas; aAlias : String);
  69. Var
  70. aList : TStringList;
  71. S : String;
  72. begin
  73. if (aAlias='') then
  74. exit;
  75. if aAlias[1]='@' then
  76. begin
  77. AList:=TStringList.Create;
  78. try
  79. aList.LoadFromFile(Copy(aAlias,2,Length(aAlias)-1));
  80. Converter.TypeAliases.AddStrings(AList);
  81. finally
  82. AList.Free;
  83. end;
  84. end
  85. else
  86. For S in SplitString(aAlias,',;') do
  87. if Pos('=',S)<>0 then
  88. Converter.TypeAliases.Add(S);
  89. end;
  90. Function TParseTSApplication.ParseFile(const aInputFileName,aOutputFileName,aUnitName : string) : Boolean;
  91. Var
  92. Converter : TTypescriptToPas;
  93. A, S,U,U1,U2 : String;
  94. L : TStringArray;
  95. begin
  96. Result:=False;
  97. try
  98. Converter:=TTypescriptToPas.Create(Self);
  99. try
  100. AddJSAliases(Converter.TypeAliases);
  101. For A in FAliases do
  102. AddAliases(Converter,A);
  103. if FWeb then
  104. begin
  105. AddWebAliases(Converter.TypeAliases);
  106. Funits:=Concat(Funits, [ 'web' ]);
  107. end;
  108. U:='';
  109. For S in FUnits do
  110. begin
  111. L:=SplitString(S,',');
  112. For U1 in L do
  113. begin
  114. U2:=Trim(U1);
  115. if U2<>'' then
  116. begin
  117. if U<>'' then
  118. U:=U+', ';
  119. U:=U+U2;
  120. end;
  121. end;
  122. end;
  123. For S in Flinks do
  124. Converter.LinkStatements.Add(S);
  125. Converter.Verbose:=FVerbose;
  126. Converter.Options:=Converter.Options+[coInterfaceAsClass];
  127. Converter.ExtraUnits:=U;
  128. Converter.InputFileName:=aInputFileName;
  129. Converter.OutputFileName:=aOutputFileName;
  130. Converter.OutputUnitName:=aUnitName;
  131. Converter.Execute;
  132. Converter.OnLog:=@DoLog;
  133. Result:=True;
  134. finally
  135. Converter.Free;
  136. end;
  137. except
  138. on E : Exception do
  139. Writeln('Conversion error ',E.ClassName,' : ',E.Message);
  140. end;
  141. end;
  142. constructor TParseTSApplication.Create(TheOwner: TComponent);
  143. begin
  144. inherited Create(TheOwner);
  145. StopOnException:=True;
  146. end;
  147. destructor TParseTSApplication.Destroy;
  148. begin
  149. inherited Destroy;
  150. end;
  151. procedure TParseTSApplication.AddWebAliases(S : Tstrings);
  152. begin
  153. With S do
  154. begin
  155. {$i web.inc}
  156. end;
  157. end;
  158. procedure TParseTSApplication.AddJSAliases(S: Tstrings);
  159. begin
  160. With S do
  161. begin
  162. Add('Object=TJSObject');
  163. Add('Function=TJSFunction');
  164. Add('RegExp=TJSRegexp');
  165. Add('Promise=TJSPromise');
  166. Add('Date=TJSDate');
  167. Add('Array=TJSArray');
  168. Add('Iterator=TJSIterator');
  169. Add('IteratorResult=TJSIteratorResult');
  170. Add('AsyncIterator=TJSAsyncIterator');
  171. Add('ArrayBuffer=TJSArrayBuffer');
  172. Add('Set=TJSSet');
  173. Add('Map=TJSMap');
  174. Add('BufferSource=TJSBufferSource');
  175. Add('DataView=TJSDataView');
  176. Add('Int8Array=TJSInt8Array');
  177. Add('Int8ClampedArray=TJSInt8ClampedArray');
  178. Add('Int16Array=TJSInt16Array');
  179. Add('Int32Array=TJSInt32Array');
  180. Add('Uint8Array=TJSUInt8Array');
  181. Add('Uint8ClampedArray=TJSUInt8ClampedArray');
  182. Add('Uint16Array=TJSUInt16Array');
  183. Add('Uint32Array=TJSUInt32Array');
  184. Add('Float32Array=TJSFloat32Array');
  185. Add('Float64Array=TJSFloat64Array');
  186. Add('JSON=TJSJSON');
  187. Add('TextDecoder=TJSTextDecoder');
  188. Add('TextEncoder=TJSTextEncoder');
  189. Add('SyntaxError=TJSSyntaxError');
  190. Add('Error=TJSError');
  191. end;
  192. end;
  193. procedure TParseTSApplication.DoLog(Sender: TObject; LogType: TCodegenLogType; const Msg: String);
  194. begin
  195. Writeln('[',LogType,'] : ',Msg);
  196. end;
  197. procedure TParseTSApplication.Usage(Msg: string);
  198. begin
  199. if Msg<>'' then
  200. Writeln('Error : ',Msg);
  201. writeln('Usage: ', ExeName, ' [options]');
  202. Writeln('Where options is one or mote of:');
  203. Writeln('-a --alias=ALIAS Define type aliases (option can be speficied multiple times)');
  204. Writeln(' where ALIAS is one of');
  205. Writeln(' a comma-separated list of Alias=TypeName values');
  206. Writeln(' a @FILE : list is read from FILENAME, one line per alias');
  207. Writeln('-h --help Display this help text');
  208. Writeln('-i --input=FILENAME Parse .d.ts file FILENAME');
  209. Writeln('-l --link=FILENAME add {$linklib FILENAME} statement. (option can be specified multiple times)');
  210. Writeln('-o --output=FILENAME Output unit in file FILENAME');
  211. Writeln('-u --unit=NAME Set output unitname');
  212. Writeln('-w --web Add web unit to uses, define type aliases for web unit');
  213. Writeln('-x --extra-units=UNITLIST Add units (comma-separated list of unit names) to uses clause.');
  214. Writeln(' This option can be specified multiple times.');
  215. end;
  216. var
  217. Application: TParseTSApplication;
  218. begin
  219. Application:=TParseTSApplication.Create(nil);
  220. Application.Title:='My Application';
  221. Application.Run;
  222. Application.Free;
  223. end.