dts2pas.pp 7.1 KB

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