pascodegen.pp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. unit pascodegen;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils;
  6. Type
  7. TCodegenLogType = (cltInfo);
  8. TCodegenLogTypes = Set of TCodegenLogType;
  9. TCodeGeneratorLogEvent = Procedure (Sender : TObject; LogType : TCodegenLogType; Const Msg : String) of object;
  10. TCodesection = (csUnknown, csConst, csType, csVar, csResourcestring, csDeclaration);
  11. { TPascalCodeGenerator }
  12. TPascalCodeGenerator = Class(TComponent)
  13. Private
  14. FAddTimeStamp: Boolean;
  15. FExtraUnits: String;
  16. FKeywordPrefix: String;
  17. FKeywordSuffix: String;
  18. FLicenseText: TStrings;
  19. FOnLog: TCodeGeneratorLogEvent;
  20. FOutputUnitName: String;
  21. FSource : TStrings;
  22. Findent : String;
  23. FSections : Array of TCodeSection;
  24. FSectionCount : Integer;
  25. FSwitches: TStrings;
  26. function GetSection: TCodeSection;
  27. procedure SetLicenseText(AValue: TStrings);
  28. procedure SetSection(AValue: TCodeSection);
  29. procedure SetSwitches(AValue: TStrings);
  30. Protected
  31. // Source manipulation
  32. Procedure DoLog(Const Msg : String; AType : TCodegenLogType = cltInfo);
  33. Procedure DoLog(Const Fmt : String; Args : Array of const; AType : TCodegenLogType = cltInfo);
  34. Function BaseUnits : String; virtual;
  35. Public
  36. Public
  37. Constructor Create(AOwner : TComponent); override;
  38. Destructor Destroy; override;
  39. // Emit section type word
  40. Procedure EnsureSection(aSection : TCodeSection);
  41. Procedure PushSection(ASection : TCodeSection = csUnknown);
  42. Function PopSection : TCodeSection;
  43. Procedure CreateHeader; virtual;
  44. Procedure CreateUnitClause; virtual;
  45. Procedure Indent;
  46. Procedure Undent;
  47. Function IsKeyWord (Const S : String) : Boolean;
  48. Function EscapeKeyWord(Const S : String) : String;
  49. Function MakePascalString(S: String; AddQuotes: Boolean=False): String;
  50. Function PrettyPrint(Const S: string): String;
  51. Procedure AddLn(Const Aline: string);
  52. Procedure AddLn(Const Alines : array of string);
  53. Procedure AddLn(Const Alines : TStrings);
  54. Procedure AddLn(Const Fmt: string; Args : Array of const);
  55. Procedure Comment(Const AComment : String; Curly : Boolean = False);
  56. Procedure Comment(Const AComment : Array of String);
  57. Procedure Comment(Const AComment : TStrings);
  58. Procedure ClassHeader(Const AClassName: String); virtual;
  59. Procedure SimpleMethodBody(Lines: Array of string); virtual;
  60. procedure SaveToStream(const AStream: TStream);
  61. Procedure SaveToFile(Const AFileName : string);
  62. Property Source : TStrings Read FSource;
  63. Property CurrentSection : TCodeSection Read GetSection Write SetSection;
  64. Published
  65. Property OutputUnitName : String Read FOutputUnitName Write FOutputUnitName;
  66. Property ExtraUnits : String Read FExtraUnits Write FExtraUnits;
  67. Property LicenseText : TStrings Read FLicenseText Write SetLicenseText;
  68. Property Switches : TStrings Read FSwitches Write SetSwitches;
  69. Property OnLog : TCodeGeneratorLogEvent Read FOnLog Write FOnlog;
  70. Property AddTimeStamp : Boolean Read FAddTimeStamp Write FAddTimeStamp;
  71. Property KeywordSuffix : String Read FKeywordSuffix Write FKeywordSuffix;
  72. Property KeywordPrefix : String Read FKeywordPrefix Write FKeywordPrefix;
  73. end;
  74. implementation
  75. { TPascalCodeGenerator }
  76. procedure TPascalCodeGenerator.Indent;
  77. begin
  78. FIndent:=FIndent+StringOfChar(' ',2);
  79. end;
  80. procedure TPascalCodeGenerator.Undent;
  81. Var
  82. L : Integer;
  83. begin
  84. L:=Length(Findent);
  85. if L>0 then
  86. FIndent:=Copy(FIndent,1,L-2)
  87. end;
  88. function TPascalCodeGenerator.IsKeyWord(const S: String): Boolean;
  89. Const
  90. KW=';absolute;and;array;asm;begin;case;const;constructor;destructor;div;do;'+
  91. 'downto;else;end;file;for;function;goto;if;implementation;in;inherited;'+
  92. 'inline;interface;label;mod;nil;not;object;of;on;operator;or;packed;'+
  93. 'procedure;program;record;reintroduce;repeat;self;set;shl;shr;string;then;'+
  94. 'to;type;unit;until;uses;var;while;with;xor;dispose;exit;false;new;true;'+
  95. 'as;class;dispinterface;except;exports;finalization;finally;initialization;'+
  96. 'inline;is;library;on;out;packed;property;raise;resourcestring;threadvar;try;'+
  97. 'private;published;length;setlength;';
  98. begin
  99. Result:=Pos(';'+lowercase(S)+';',KW)<>0;
  100. end;
  101. function TPascalCodeGenerator.EscapeKeyWord(const S: String): String;
  102. begin
  103. Result:=S;
  104. if IsKeyWord(S) then
  105. Result:=KeywordPrefix+Result+KeywordSuffix
  106. end;
  107. procedure TPascalCodeGenerator.AddLn(const Aline: string);
  108. begin
  109. FSource.Add(FIndent+ALine);
  110. end;
  111. procedure TPascalCodeGenerator.AddLn(const Alines: array of string);
  112. Var
  113. S : String;
  114. begin
  115. For s in alines do
  116. Addln(S);
  117. end;
  118. procedure TPascalCodeGenerator.AddLn(const Alines: TStrings);
  119. Var
  120. S : String;
  121. begin
  122. For s in alines do
  123. Addln(S);
  124. end;
  125. procedure TPascalCodeGenerator.AddLn(const Fmt: string; Args: array of const);
  126. begin
  127. AddLn(Format(Fmt,Args));
  128. end;
  129. procedure TPascalCodeGenerator.Comment(const AComment: String; Curly: Boolean);
  130. begin
  131. if Curly then
  132. AddLn('{ '+AComment+' }')
  133. else
  134. AddLn('// '+AComment);
  135. end;
  136. procedure TPascalCodeGenerator.Comment(const AComment: array of String);
  137. begin
  138. AddLn('{');
  139. Indent;
  140. AddLn(AComment);
  141. Undent;
  142. AddLn('}');
  143. end;
  144. procedure TPascalCodeGenerator.Comment(const AComment: TStrings);
  145. begin
  146. AddLn('{');
  147. Indent;
  148. AddLn(AComment);
  149. Undent;
  150. AddLn('}');
  151. end;
  152. constructor TPascalCodeGenerator.Create(AOwner: TComponent);
  153. begin
  154. inherited Create(AOwner);
  155. FSource:=TstringList.Create;
  156. FLicenseText:=TstringList.Create;
  157. FSwitches:=TstringList.Create;
  158. FSwitches.Add('MODE ObjFPC');
  159. FSwitches.Add('H+');
  160. SetLength(FSections,0);
  161. FSectionCount:=0;
  162. PushSection(csUnknown);
  163. FKeywordPrefix:='&';
  164. end;
  165. destructor TPascalCodeGenerator.Destroy;
  166. begin
  167. FreeAndNil(FSwitches);
  168. FreeAndNil(FLicenseText);
  169. FreeAndNil(FSource);
  170. inherited Destroy;
  171. end;
  172. procedure TPascalCodeGenerator.EnsureSection(aSection: TCodeSection);
  173. Const
  174. SectionKeyWords : Array[TCodesection] of string
  175. = ('', 'Const', 'Type', 'Var', 'Resourcestring', '');
  176. begin
  177. If CurrentSection<>aSection then
  178. begin
  179. CurrentSection:=aSection;
  180. AddLn(SectionKeyWords[CurrentSection]);
  181. end;
  182. end;
  183. procedure TPascalCodeGenerator.PushSection(ASection : TCodeSection = csUnknown);
  184. begin
  185. if FSectionCount=Length(FSections) then
  186. SetLength(FSections,FSectionCount+10);
  187. FSections[FSectionCount]:=ASection;
  188. Inc(FSectionCount);
  189. end;
  190. function TPascalCodeGenerator.PopSection: TCodeSection;
  191. begin
  192. if FSectionCount=0 then
  193. Result:=csUnknown
  194. else
  195. begin
  196. Dec(FSectionCount);
  197. Result:=FSections[FSectionCount];
  198. end;
  199. end;
  200. procedure TPascalCodeGenerator.SaveToStream(const AStream : TStream);
  201. begin
  202. FSource.SaveToStream(AStream)
  203. end;
  204. procedure TPascalCodeGenerator.SaveToFile(const AFileName: string);
  205. Var
  206. F : TFileStream;
  207. B : Boolean;
  208. begin
  209. B:=False;
  210. F:=Nil;
  211. try
  212. B:=(Source.Count=0) and (OutputUnitName='');
  213. if B then
  214. OutputUnitname:=ChangeFileExt(ExtractFileName(AFileName),'');
  215. F:=TFileStream.Create(aFilename,fmCreate);
  216. SaveToStream(F);
  217. finally
  218. F.Free;
  219. if B then
  220. OutputUnitName:='';
  221. end;
  222. end;
  223. procedure TPascalCodeGenerator.SetSection(AValue: TCodeSection);
  224. begin
  225. if GetSection=AValue then
  226. Exit;
  227. FSections[FSectionCount-1]:=AValue;
  228. end;
  229. procedure TPascalCodeGenerator.SetSwitches(AValue: TStrings);
  230. begin
  231. if FSwitches=AValue then Exit;
  232. FSwitches.Assign(AValue);
  233. end;
  234. function TPascalCodeGenerator.GetSection: TCodeSection;
  235. begin
  236. Result:=FSections[FSectionCount-1];
  237. end;
  238. procedure TPascalCodeGenerator.SetLicenseText(AValue: TStrings);
  239. begin
  240. if FLicenseText=AValue then Exit;
  241. FLicenseText.Assign(AValue);
  242. end;
  243. procedure TPascalCodeGenerator.DoLog(const Msg: String; AType: TCodegenLogType);
  244. begin
  245. If Assigned(FOnLog) then
  246. FOnLog(Self,Atype,Msg);
  247. end;
  248. procedure TPascalCodeGenerator.DoLog(const Fmt: String; Args: array of const;
  249. AType: TCodegenLogType);
  250. begin
  251. DoLog(Format(Fmt,Args),AType);
  252. end;
  253. procedure TPascalCodeGenerator.CreateHeader;
  254. Var
  255. B,S : String;
  256. begin
  257. if LicenseText.Count>0 then
  258. Comment(LicenseText);
  259. if AddTimeStamp then
  260. Comment('Generated on: '+DateTimeToStr(Now));
  261. For S in Switches do
  262. addln('{$%s}',[S]);
  263. addln('');
  264. addln('interface');
  265. addln('');
  266. S:=ExtraUnits;
  267. B:=BaseUnits;
  268. if (B<>'') then
  269. if (S<>'') then
  270. begin
  271. if (B[Length(B)]<>',') then
  272. B:=B+',';
  273. S:=B+S;
  274. end
  275. else
  276. S:=B;
  277. addln('uses %s;',[S]);
  278. addln('');
  279. end;
  280. procedure TPascalCodeGenerator.CreateUnitClause;
  281. begin
  282. AddLn('Unit %s;',[OutputUnitName]);
  283. AddLn('');
  284. end;
  285. procedure TPascalCodeGenerator.SimpleMethodBody(Lines: array of string);
  286. Var
  287. S : String;
  288. begin
  289. AddLn('');
  290. AddLn('begin');
  291. Indent;
  292. For S in Lines do
  293. AddLn(S);
  294. Undent;
  295. AddLn('end;');
  296. AddLn('');
  297. end;
  298. function TPascalCodeGenerator.BaseUnits: String;
  299. begin
  300. Result:='';
  301. end;
  302. function TPascalCodeGenerator.MakePascalString(S: String; AddQuotes: Boolean
  303. ): String;
  304. begin
  305. Result:=StringReplace(S,'''','''''',[rfReplaceAll]);
  306. if AddQuotes then
  307. Result:=''''+Result+'''';
  308. end;
  309. function TPascalCodeGenerator.PrettyPrint(const S: string): String;
  310. begin
  311. If (S='') then
  312. Result:=''
  313. else
  314. Result:=Upcase(S[1])+Copy(S,2,Length(S)-1);
  315. end;
  316. procedure TPascalCodeGenerator.ClassHeader(const AClassName: String);
  317. begin
  318. AddLn('');
  319. AddLn('{ '+StringOfChar('-',68));
  320. AddLn(' '+AClassName);
  321. AddLn(' '+StringOfChar('-',68)+'}');
  322. AddLn('');
  323. end;
  324. end.
  325. end.