ISPP.Preprocessor.pas 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856
  1. {
  2. Inno Setup Preprocessor
  3. Copyright (C) 2001-2002 Alex Yackimoff
  4. Inno Setup
  5. Copyright (C) 1997-2025 Jordan Russell
  6. Portions by Martijn Laan
  7. For conditions of distribution and use, see LICENSE.TXT.
  8. }
  9. unit ISPP.Preprocessor;
  10. interface
  11. uses
  12. Windows, SysUtils, Classes, Shared.PreprocInt, IniFiles, Registry, ISPP.Intf,
  13. ISPP.Base, ISPP.Stack, ISPP.IdentMan, ISPP.Parser;
  14. type
  15. TPreprocessor = class;
  16. EPreprocError = class(Exception)
  17. FileName: string;
  18. LineNumber: Integer;
  19. ColumnNumber: Integer;
  20. constructor Create(Preproc: TPreprocessor; const Msg: string);
  21. end;
  22. TConditionalBlockInfo = record
  23. BlockState, Fired, HadElse, Reserved: Boolean;
  24. end;
  25. TConditionalVerboseMsg = (cvmIf, cvmElif, cvmElse, cvmEndif);
  26. TConditionalTranslationStack = class(TStack<TConditionalBlockInfo>)
  27. private
  28. FPreproc: TPreprocessor;
  29. FCache: Boolean;
  30. FCacheValid: Boolean;
  31. procedure VerboseMsg(Msg: TConditionalVerboseMsg; Eval: Boolean);
  32. protected
  33. function Last: TConditionalBlockInfo;
  34. procedure UpdateLast(const Value: TConditionalBlockInfo);
  35. public
  36. constructor Create(Preproc: TPreprocessor);
  37. procedure IfInstruction(Eval: Boolean);
  38. procedure ElseIfInstruction(Eval: Boolean);
  39. procedure ElseInstruction;
  40. procedure EndIfInstruction;
  41. function Include: Boolean;
  42. procedure Resolved;
  43. end;
  44. TPreprocessorCommand = (pcError, pcIf, pcIfDef, pcIfNDef, pcIfExist,
  45. pcIfNExist, pcElseIf, pcElse, pcEndIf, pcDefine, pcUndef, pcInclude,
  46. pcErrorDir, pcPragma, pcLine, pcImport, pcEmit, pcEnv, pcFile,
  47. pcExpr, pcInsert, pcAppend, pcDim, pcSub, pcEndSub, pcEndLoop,
  48. pcFor, pcReDim);
  49. TDropGarbageProc = procedure(Item: Pointer);
  50. TIsppMessageType = (imtStatus, imtWarning);
  51. TPreprocessor = class(TObject, IIdentManager)
  52. private
  53. FCompilerParams: TPreprocessScriptParams;
  54. FCompilerPath: string;
  55. FCounter: Integer;
  56. FCurrentFile: Integer;
  57. FCurrentLine: Integer;
  58. FDefaultScope: TDefineScope;
  59. FFileStack: TStringList; { strs: files being included }
  60. FIncludes: TStringList; { strs: files been included, for error msgs }
  61. FIncludePath: string;
  62. FInsertionPoint: Integer;
  63. FLinePointer: Integer;
  64. FMainCounter: Integer;
  65. FOutput: TStringList; { strs: translation }
  66. FQueuedLine: string;
  67. FQueuedLineCount: Integer;
  68. FSourcePath: string;
  69. FStack: TConditionalTranslationStack;
  70. FIdentManager: TIdentManager;
  71. FInProcBody: Boolean;
  72. FInForBody: Boolean;
  73. FProcs: TStringList;
  74. FGarbageCollection: TList;
  75. procedure DropGarbage;
  76. function ProcessInlineDirectives(P: PChar): string;
  77. function ProcessPreprocCommand(Command: TPreprocessorCommand;
  78. var Params: string; ParamsOffset: NativeInt): Boolean;
  79. procedure PushFile(const FileName: string);
  80. procedure PopFile;
  81. function CheckFile(const FileName: string): Boolean;
  82. function EmitDestination: TStringList;
  83. procedure SendMsg(Msg: string; Typ: TIsppMessageType);
  84. function GetFileName(Code: Integer): string;
  85. function GetLineNumber(Code: Integer): Integer;
  86. procedure RaiseErrorEx(const Message: string; Column: Integer);
  87. procedure ExecProc(Body: TStrings);
  88. protected
  89. function GetDefaultScope: TDefineScope;
  90. procedure SetDefaultScope(Scope: TDefineScope);
  91. procedure InternalAddLine(const LineRead: string; FileIndex, LineNo: Integer;
  92. NonISS: Boolean);
  93. function InternalQueueLine(const LineRead: string; FileIndex, LineNo: Integer;
  94. NonISS: Boolean): Integer;
  95. function ParseFormalParams(Parser: TParser; var ParamList: PParamList): Integer;
  96. { IUnknown }
  97. function QueryInterface(const IID: TGUID; out Obj): HRESULT; stdcall;
  98. function _AddRef: Integer; stdcall;
  99. function _Release: Integer; stdcall;
  100. { IIdentManager }
  101. function LookupPredefined(Name: string; Value: PIsppVariant): Boolean;
  102. function Defined(const Name: String): Boolean;
  103. function GetIdent(const Name: String;
  104. out CallContext: ICallContext): TIdentType;
  105. function TypeOf(const Name: String): Byte;
  106. function DimOf(const Name: String): Integer;
  107. public
  108. FOptions: TISPPOptions;
  109. constructor Create(const CompilerParams: TPreprocessScriptParams;
  110. VarManager: TIdentManager; const Options: TIsppOptions;
  111. const SourcePath: string; const CompilerPath: string; const FileName: string = '');
  112. destructor Destroy; override;
  113. procedure CallIdleProc;
  114. procedure VerboseMsg(Level: Integer; const Msg: string); overload;
  115. procedure VerboseMsg(Level: Integer; const Msg: string; const Args: array of const); overload;
  116. procedure StatusMsg(const Msg: string); overload;
  117. procedure StatusMsg(const Msg: string; const Args: array of const); overload;
  118. procedure WarningMsg(const Msg: string); overload;
  119. procedure WarningMsg(const Msg: string; const Args: array of const); overload;
  120. function GetNextOutputLine(var LineFilename: string; var LineNumber: Integer;
  121. var LineText: string): Boolean;
  122. procedure GetNextOutputLineReset;
  123. procedure IncludeFile(FileName: string; Builtins, UseIncludePathOnly, ResetCurrentFile: Boolean);
  124. procedure QueueLine(const LineRead: string);
  125. function PrependDirName(const FileName, Dir: string): string;
  126. procedure RegisterFunction(const Name: string; Handler: TIsppFunction; Ext: NativeInt);
  127. procedure RaiseError(const Message: string);
  128. procedure SaveToFile(const FileName: string);
  129. procedure CollectGarbage(Item: Pointer; Proc: TDropGarbageProc);
  130. procedure UncollectGarbage(Item: Pointer);
  131. property IncludedFiles: TStringList read FIncludes;
  132. property IncludePath: string read FIncludePath write FIncludePath;
  133. property SourcePath: string read FSourcePath;
  134. property StringList: TStringList read FOutput;
  135. property Stack: TConditionalTranslationStack read FStack;
  136. property VarMan: TIdentManager read FIdentManager;
  137. end;
  138. implementation
  139. uses
  140. PathFunc, UnsignedFunc,
  141. Shared.CommonFunc, Shared.FileClass, Shared.Struct,
  142. ISPP.Consts, ISPP.Funcs, ISPP.VarUtils, ISPP.Sessions, ISPP.CTokenizer;
  143. const
  144. PreprocCommands: array[TPreprocessorCommand] of String =
  145. ('', 'if', 'ifdef', 'ifndef', 'ifexist', 'ifnexist', 'elif', 'else',
  146. 'endif', 'define', 'undef', 'include', 'error', 'pragma', 'line', 'import',
  147. 'emit', 'env', 'file', 'expr', 'insert', 'append', 'dim', 'sub', 'endsub',
  148. 'endloop', 'for', 'redim');
  149. PpCmdSynonyms: array[TPreprocessorCommand] of Char =
  150. (#0, '?', #0, #0, #0, #0, #0, '^', '.', ':', #0, '+', #0, #0, #0, #0,
  151. '=', '%', #0, '!', #0, #0, #0, #0, #0, #0, #0, #0);
  152. function GetEnv(const EnvVar: String): String;
  153. function AdjustLength(var S: String; const Res: Cardinal): Boolean;
  154. begin
  155. Result := Integer(Res) < Length(S);
  156. SetLength (S, Res);
  157. end;
  158. var
  159. Res: DWORD;
  160. begin
  161. SetLength(Result, 255);
  162. repeat
  163. Res := GetEnvironmentVariable(PChar(EnvVar), PChar(Result), ULength(Result));
  164. if Res = 0 then begin
  165. Result := '';
  166. Break;
  167. end;
  168. until AdjustLength(Result, Res);
  169. end;
  170. function ParsePreprocCommand(var P: PChar; ExtraTerminator: Char): TPreprocessorCommand;
  171. begin
  172. for Result := TPreprocessorCommand(1) to High(TPreprocessorCommand) do
  173. begin
  174. if (P^ = PpCmdSynonyms[Result]) then
  175. Inc(P)
  176. else if (StrLIComp(P, @PreprocCommands[Result][1], ULength(PreprocCommands[Result])) = 0) and
  177. CharInSet(P[Length(PreprocCommands[Result])], [#0..#32, ExtraTerminator]) then
  178. Inc(P, Length(PreprocCommands[Result]))
  179. else
  180. Continue;
  181. Exit;
  182. end;
  183. if StrLIComp('echo', P, 4) = 0 then
  184. begin
  185. Result := pcEmit;
  186. Inc(P, 4)
  187. end
  188. else if StrLIComp('call', P, 4) = 0 then
  189. begin
  190. Result := pcExpr;
  191. Inc(P, 4);
  192. end
  193. else
  194. Result := pcError;
  195. end;
  196. { EPreprocError }
  197. constructor EPreprocError.Create(Preproc: TPreprocessor; const Msg: string);
  198. begin
  199. inherited Create(AddPeriod(Msg));
  200. FileName := Preproc.GetFileName(-1);
  201. LineNumber := Preproc.GetLineNumber(-1);
  202. end;
  203. { TPreprocessor }
  204. function CheckReservedIdent(const Ident: string): string;
  205. begin
  206. Result := UpperCase(Ident);
  207. if (Result = SLocal) or
  208. (Result = SGlobal) or
  209. (Result = SInt) or
  210. (Result = SStr) or
  211. (Result = SAny) then
  212. raise EParsingError.CreateFmt(SExpectedButFound, [SIdent, '''' + Result + '''']);
  213. Result := Ident;
  214. end;
  215. constructor TPreprocessor.Create(const CompilerParams: TPreprocessScriptParams;
  216. VarManager: TIdentManager; const Options: TIsppOptions;
  217. const SourcePath, CompilerPath, FileName: string);
  218. begin
  219. PushPreproc(Self);
  220. if VarManager = nil then
  221. FIdentManager := TIdentManager.Create(Self, NativeInt(Self))
  222. else
  223. FIdentManager := VarManager;
  224. FOptions := Options;
  225. FIdentManager._AddRef;
  226. FIdentManager.BeginLocal;
  227. FCompilerParams := CompilerParams;
  228. FCompilerPath := CompilerPath;
  229. FSourcePath := SourcePath;
  230. FFileStack := TStringList.Create;
  231. FIncludes := TStringList.Create;
  232. FIncludes.Add(FileName); //main file - no name
  233. FInsertionPoint := -1;
  234. FOutput := TStringList.Create;
  235. FProcs := TStringList.Create;
  236. FStack := TConditionalTranslationStack.Create(Self);
  237. if VarManager = nil then ISPP.Funcs.RegisterFunctions(Self);
  238. end;
  239. destructor TPreprocessor.Destroy;
  240. begin
  241. DropGarbage;
  242. if PopPreproc <> Self then
  243. RaiseError('Internal error: FSP');
  244. FStack.Free;
  245. FProcs.Free;
  246. FOutput.Free;
  247. FIncludes.Free;
  248. if FFileStack.Count <> 0 then
  249. RaiseError('Internal error: FNE');
  250. FFileStack.Free;
  251. FIdentManager.EndLocal;
  252. FIdentManager._Release;
  253. end;
  254. function TPreprocessor.GetFileName(Code: Integer): string;
  255. begin
  256. if Code = -1 then
  257. Result := FIncludes[FCurrentFile]
  258. else
  259. Result := FIncludes[Integer(FOutput.Objects[Code]) shr 16];
  260. end;
  261. function TPreprocessor.GetLineNumber(Code: Integer): Integer;
  262. begin
  263. if Code = -1 then
  264. Result := FCurrentLine
  265. else
  266. Result := Integer(FOutput.Objects[Code]) and $FFFF
  267. end;
  268. function TPreprocessor.GetNextOutputLine(var LineFilename: string; var LineNumber: Integer;
  269. var LineText: string): Boolean;
  270. begin
  271. Result := False;
  272. if FLinePointer < FOutput.Count then
  273. begin
  274. LineFilename := GetFileName(FLinePointer);
  275. LineNumber := GetLineNumber(FLinePointer);
  276. LineText := FOutput[FLinePointer];
  277. Inc(FLinePointer);
  278. Result := True;
  279. end;
  280. end;
  281. procedure TPreprocessor.GetNextOutputLineReset;
  282. begin
  283. FLinePointer := 0;
  284. end;
  285. procedure TPreprocessor.InternalAddLine(const LineRead: string; FileIndex, LineNo: Integer;
  286. NonISS: Boolean);
  287. var
  288. IncludeLine: Boolean;
  289. P, P1: PChar;
  290. Command: TPreprocessorCommand;
  291. State: Boolean;
  292. S, S1: string;
  293. begin
  294. try
  295. Inc(LineNo);
  296. FCurrentFile := FileIndex;
  297. FCurrentLine := LineNo;
  298. P := PChar(LineRead);
  299. IncludeLine := True;
  300. if P^ <> #0 then
  301. begin
  302. P1 := P;
  303. while CharInSet(P^, [#1..#32]) do Inc(P);
  304. if P^ = '#' then
  305. begin
  306. Inc(P);
  307. while CharInSet(P^, [#1..#32]) do Inc(P);
  308. IncludeLine := FInProcBody;
  309. Command := ParsePreprocCommand(P, #0);
  310. if FInProcBody then
  311. begin
  312. case Command of
  313. pcError: RaiseError(SUnknownPreprocessorDirective);
  314. pcSub: RaiseError('Nested procedure declaration not allowed');
  315. pcEndSub:
  316. begin
  317. S := P;
  318. ProcessPreprocCommand(Command, S, P - P1);
  319. IncludeLine := False;
  320. end
  321. else
  322. S := LineRead;
  323. end;
  324. end
  325. else
  326. begin
  327. State := FStack.Include;
  328. const DirectiveOffset = P - P1;
  329. //S := Copy(LineRead, DirectiveOffset + 1, MaxInt);
  330. S := P;
  331. case Command of
  332. pcIf..pcIfNExist:
  333. FStack.IfInstruction(FStack.Include and
  334. ProcessPreprocCommand(Command, S, DirectiveOffset));
  335. pcElseIf:
  336. FStack.ElseIfInstruction(FStack.Last.Fired or
  337. (FStack.Include or not FStack.Last.BlockState) and
  338. ProcessPreprocCommand(Command, S, DirectiveOffset));
  339. pcElse: FStack.ElseInstruction;
  340. pcEndIf: FStack.EndIfInstruction
  341. else
  342. if State then
  343. case Command of
  344. pcEmit, pcEnv:
  345. begin
  346. ProcessPreprocCommand(Command, S, DirectiveOffset);
  347. VerboseMsg(8, SLineEmitted, [S]);
  348. IncludeLine := True
  349. end;
  350. pcFile: RaiseError(SFileDirectiveCanBeOnlyInline);
  351. else
  352. ProcessPreprocCommand(Command, S, DirectiveOffset);
  353. end;
  354. end
  355. end;
  356. end
  357. else
  358. if not FInProcBody and not FStack.Include then
  359. IncludeLine := False
  360. else
  361. if ((P^ = '/') and (P[1] = '/')) or
  362. ((P^ = #0) and not (optEmitEmptyLines in FOptions.Options)) then //P^ is #0 if the line was all whitespace
  363. IncludeLine := False
  364. else
  365. if (P^ <> #0) and (P^ <> ';') and not FInProcBody then
  366. S := PChar(ProcessInlineDirectives(P1))
  367. else
  368. S := P1;
  369. end
  370. else
  371. begin
  372. S := '';
  373. IncludeLine := optEmitEmptyLines in FOptions.Options
  374. end;
  375. if IncludeLine then
  376. begin
  377. P := PChar(S);
  378. repeat
  379. P1 := P;
  380. while not CharInSet(P^, [#0, #10, #13]) do Inc(P);
  381. SetString(S1, P1, P - P1);
  382. if FInsertionPoint >= 0 then
  383. begin
  384. EmitDestination.InsertObject(FInsertionPoint, S1,
  385. TObject(FileIndex shl 16 or LineNo));
  386. Inc(FInsertionPoint);
  387. end
  388. else
  389. EmitDestination.AddObject(S1, TObject(FileIndex shl 16 or LineNo));
  390. while CharInSet(P^, [#10, #13]) do Inc(P);
  391. until P^ = #0;
  392. end;
  393. except
  394. on E: EParsingError do
  395. RaiseErrorEx(E.Message, E.Position);
  396. on E: EPreprocError do
  397. raise;
  398. on E: Exception do
  399. RaiseError(E.Message);
  400. end;
  401. end;
  402. function TPreprocessor.ProcessInlineDirectives(P: PChar): string;
  403. var
  404. S: string;
  405. Command: TPreprocessorCommand;
  406. LineStack: TConditionalTranslationStack;
  407. LineStart, P1, DStart, DEnd: PChar;
  408. function ScanForInlineStart(var P, D: PChar): Boolean;
  409. var
  410. I: Integer;
  411. begin
  412. Result := False;
  413. while P^ <> #0 do
  414. begin
  415. if P^ = FOptions.InlineStart[1] then
  416. begin
  417. D := P;
  418. Result := True;
  419. for I := 2 to Length(FOptions.InlineStart) do
  420. begin
  421. Inc(D);
  422. if D^ <> FOptions.InlineStart[I] then
  423. begin
  424. Result := False;
  425. Break;
  426. end;
  427. end;
  428. Inc(D);
  429. end;
  430. if Result then Break;
  431. Inc(P);
  432. end;
  433. end;
  434. function ScanForInlineEnd(var P: PChar): PChar;
  435. var
  436. I: Integer;
  437. begin
  438. Result := nil;
  439. while P^ <> #0 do
  440. begin
  441. if P^ = FOptions.InlineEnd[1] then
  442. begin
  443. Result := P;
  444. for I := 2 to Length(FOptions.InlineEnd) do
  445. begin
  446. Inc(P);
  447. if P^ <> FOptions.InlineEnd[I] then
  448. begin
  449. Result := nil;
  450. Break;
  451. end;
  452. end;
  453. Inc(P);
  454. end;
  455. if Result <> nil then Exit;
  456. Inc(P);
  457. end;
  458. RaiseError(SUnterminatedPreprocessorDirectiv);
  459. end;
  460. begin
  461. LineStack := TConditionalTranslationStack.Create(Self);
  462. try
  463. Result := '';
  464. LineStart := P;
  465. P1 := P;
  466. while ScanForInlineStart(P, DStart) do
  467. begin
  468. SetString(S, P1, P - P1);
  469. if LineStack.Include then Result := Result + S;
  470. Command := ParsePreprocCommand(DStart, Char(FOptions.InlineEnd[1]));
  471. if Command = pcError then
  472. Command := pcEmit;
  473. DEnd := DStart;
  474. SetString(S, DStart, ScanForInlineEnd(DEnd) - DStart);
  475. case Command of
  476. pcError: RaiseError(SUnknownPreprocessorDirective);
  477. pcIf..pcIfNExist:
  478. LineStack.IfInstruction(LineStack.Include and
  479. ProcessPreprocCommand(Command, S, DStart - LineStart));
  480. pcElseIf:
  481. LineStack.ElseIfInstruction(LineStack.Last.Fired or
  482. (LineStack.Include or not LineStack.Last.BlockState) and
  483. ProcessPreprocCommand(Command, S, DStart - LineStart));
  484. pcElse: LineStack.ElseInstruction;
  485. pcEndIf: LineStack.EndIfInstruction;
  486. else
  487. if LineStack.Include then
  488. case Command of
  489. pcInclude, pcInsert..pcEndSub:
  490. RaiseError(Format(SDirectiveCannotBeInline,
  491. [PreprocCommands[Command]]));
  492. pcEmit, pcEnv, pcFile:
  493. begin
  494. ProcessPreprocCommand(Command, S, DStart - LineStart);
  495. Result := Result + S;
  496. end;
  497. else
  498. ProcessPreprocCommand(Command, S, DStart - LineStart)
  499. end;
  500. end;
  501. P1 := DEnd;
  502. P := DEnd;
  503. //Inc(P);
  504. end;
  505. Result := Result + P1;
  506. LineStack.Resolved;
  507. finally
  508. LineStack.Free
  509. end;
  510. end;
  511. function TPreprocessor.GetDefaultScope: TDefineScope;
  512. begin
  513. if FFileStack.Count > 0 then
  514. Result := TDefineScope(FFileStack.Objects[FFileStack.Count - 1])
  515. else
  516. Result := FDefaultScope;
  517. end;
  518. procedure TPreprocessor.SetDefaultScope(Scope: TDefineScope);
  519. begin
  520. if Scope = dsAny then Scope := dsPublic;
  521. if FFileStack.Count > 0 then
  522. FFileStack.Objects[FFileStack.Count - 1] := TObject(Scope)
  523. else
  524. FDefaultScope := Scope;
  525. end;
  526. type
  527. TParserAccess = class(TParser);
  528. function TPreprocessor.ProcessPreprocCommand(Command: TPreprocessorCommand;
  529. var Params: string; ParamsOffset: NativeInt): Boolean;
  530. function ParseScope(Parser: TParser; ExpectedTokens: TTokenKinds = [tkIdent]): TDefineScope;
  531. const
  532. ScopeClauses: array[dsPublic..dsPrivate] of string =
  533. ('public', 'protected', 'private');
  534. begin
  535. Parser.NextTokenExpect([tkIdent]);
  536. for Result := Low(ScopeClauses) to High(ScopeClauses) do
  537. if CompareText(Parser.TokenString, ScopeClauses[Result]) = 0 then
  538. begin
  539. Parser.NextTokenExpect(ExpectedTokens);
  540. Exit;
  541. end;
  542. Result := dsAny;
  543. end;
  544. function GetScope(Parser: TParser): TDefineScope;
  545. begin
  546. Result := ParseScope(Parser);
  547. if Result = dsAny then Result := GetDefaultScope;
  548. end;
  549. procedure ParseDim(Parser: TParserAccess; ReDim: Boolean);
  550. var
  551. Name: string;
  552. N, NValues, I: Integer;
  553. Scope: TDefineScope;
  554. Values: array of TIsppVariant;
  555. begin
  556. with Parser do
  557. try
  558. Scope := GetScope(Parser);
  559. Name := CheckReservedIdent(TokenString);
  560. NextTokenExpect([tkOpenBracket]);
  561. N := IntegerExpr(True);
  562. NValues := 0;
  563. NextTokenExpect([tkCloseBracket]);
  564. if PeekAtNextToken = tkOpenBrace then
  565. begin
  566. NextToken;
  567. SetLength(Values, N);
  568. NValues := 0;
  569. while True do begin
  570. if NValues >= N then
  571. raise EIdentError.CreateFmt(SIndexIsOutOfArraySize, [NValues, Name]);
  572. Values[NValues] := Expr(True);
  573. MakeRValue(Values[NValues]);
  574. Inc(NValues);
  575. if PeekAtNextToken <> tkComma then
  576. Break;
  577. NextToken;
  578. end;
  579. NextTokenExpect([tkCloseBrace]);
  580. end;
  581. FIdentManager.DimVariable(Name, N, Scope, ReDim);
  582. if ReDim and (NValues <> 0) then
  583. Error('Initializers not allowed on #redim of existing array');
  584. for I := 0 to NValues-1 do
  585. FIdentManager.DefineVariable(Name, I, Values[I], Scope);
  586. finally
  587. //Free
  588. end;
  589. end;
  590. procedure ParseDefine(Parser: TParserAccess);
  591. var
  592. Name: string;
  593. Start, P: PChar;
  594. IsMacroDefine: Boolean;
  595. ParamList: PParamList;
  596. AExpr: string;
  597. VarIndex: Integer;
  598. Scope: TDefineScope;
  599. MacroExprPos: TExprPosition;
  600. begin
  601. with Parser do
  602. begin
  603. Start := FExpr;
  604. Scope := ParseScope(Parser, [tkEOF, tkIdent, tkSemicolon]);
  605. if Scope = dsAny then
  606. Scope := GetDefaultScope
  607. else
  608. if Token <> tkIdent then
  609. begin
  610. SetDefaultScope(Scope);
  611. Exit;
  612. end;
  613. Name := CheckReservedIdent(TokenString);
  614. IsMacroDefine := FExpr^ = '(';
  615. if IsMacroDefine then
  616. begin
  617. NextToken;
  618. const AParamCount = ParseFormalParams(Parser, ParamList);
  619. try
  620. Inc(FExpr);
  621. P := FExpr;
  622. MacroExprPos.FileIndex := FCurrentFile;
  623. MacroExprPos.Line := FCurrentLine;
  624. MacroExprPos.Column := Integer((FExpr - Start) + ParamsOffset);
  625. while P^ <> #0 do Inc(P);
  626. SetString(AExpr, FExpr, P - FExpr);
  627. AExpr := Trim(AExpr);
  628. if AExpr = '' then RaiseError(SMacroExpressionExpected);
  629. FIdentManager.DefineMacro(Name, AExpr, MacroExprPos, FOptions.ParserOptions,
  630. Slice(ParamList^, AParamCount), Scope);
  631. finally
  632. Finalize(ParamList^[0], AParamCount);
  633. FreeMem(ParamList)
  634. end;
  635. end
  636. else
  637. begin
  638. VarIndex := -1;
  639. if PeekAtNextToken = tkOpenBracket then
  640. begin
  641. NextToken;
  642. VarIndex := IntegerExpr(True);
  643. NextTokenExpect([tkCloseBracket]);
  644. end;
  645. case PeekAtNextToken of
  646. opAssign: NextToken;
  647. tkEOF:
  648. begin
  649. FIdentManager.DefineVariable(Name, VarIndex, NULL, Scope);
  650. Exit;
  651. end
  652. end;
  653. FIdentManager.DefineVariable(Name, VarIndex, Evaluate, Scope);
  654. end;
  655. end;
  656. end;
  657. procedure ParseUndef(Parser: TParserAccess);
  658. var
  659. Scope: TDefineScope;
  660. begin
  661. with Parser do
  662. begin
  663. Scope := GetScope(Parser);
  664. FIdentManager.Delete(CheckReservedIdent(TokenString), Scope);
  665. EndOfExpr;
  666. end
  667. end;
  668. procedure IncludeFile(const Params: string);
  669. var
  670. FileName: string;
  671. function TryPascal: Boolean;
  672. begin
  673. Result := not (optPascalStrings in FOptions.ParserOptions.Options);
  674. if Result then
  675. begin
  676. Include(FOptions.ParserOptions.Options, optPascalStrings);
  677. try
  678. try
  679. FileName := ParseStr(Self, Params, ParamsOffset,
  680. @FOptions.ParserOptions);
  681. except
  682. Result := False
  683. end;
  684. finally
  685. Exclude(FOptions.ParserOptions.Options, optPascalStrings);
  686. end;
  687. end
  688. end;
  689. var
  690. IncludePathOnly: Boolean;
  691. begin
  692. FileName := Params;
  693. if Pos(';', FileName) > 0 then
  694. Delete(FileName, Pos(';', FileName), MaxInt);
  695. FileName := Trim(FileName);
  696. if (FileName <> '') and (FileName[1] = '<') and
  697. (FileName[Length(FileName)] = '>') then
  698. begin
  699. FileName := Copy(FileName, 2, Length(FileName) - 2);
  700. IncludePathOnly := True;
  701. end
  702. else
  703. begin
  704. try
  705. FileName := ParseStr(Self, Params, ParamsOffset, @FOptions.ParserOptions);
  706. except
  707. if not TryPascal then
  708. raise
  709. end;
  710. IncludePathOnly := False;
  711. end;
  712. Self.IncludeFile(FileName, False, IncludePathOnly, False);
  713. end;
  714. procedure Pragma(Parser: TParserAccess);
  715. var
  716. P: string;
  717. function StrPragma(AllowEmpty: Boolean): string;
  718. begin
  719. Result := Parser.StrExpr(True);
  720. if (Result = '') and not AllowEmpty then
  721. RaiseError(SNonEmptyStringExpected);
  722. Parser.EndOfExpr;
  723. end;
  724. procedure OptionPragma(var Options: TOptions);
  725. var
  726. C: Char;
  727. V: Boolean;
  728. begin
  729. with Parser do
  730. begin
  731. NextTokenExpect([opSubtract]);
  732. repeat
  733. NextTokenExpect([tkIdent]);
  734. if Length(TokenString) > 1 then
  735. RaiseError(SInvalidOptionName);
  736. C := TokenString[1];
  737. V := NextTokenExpect([opAdd, opSubtract]) = opAdd;
  738. SetOption(Options, C, V);
  739. until NextTokenExpect([tkEOF, opSubtract, tkSemicolon]) <> opSubtract;
  740. end;
  741. end;
  742. var
  743. CatchException: Boolean;
  744. ErrorMsg: string;
  745. begin
  746. CatchException := True;
  747. try
  748. with Parser do
  749. begin
  750. NextTokenExpect([tkIdent]);
  751. P := LowerCase(TokenString);
  752. if P = 'include' then
  753. FIncludePath := StrPragma(True)
  754. else if P = 'inlinestart' then
  755. FOptions.InlineStart := StrPragma(False)
  756. else if P = 'inlineend' then
  757. FOptions.InlineEnd := StrPragma(False)
  758. else if P = 'spansymbol' then
  759. FOptions.SpanSymbol := StrPragma(False)[1]
  760. else if P = 'parseroption' then
  761. OptionPragma(FOptions.ParserOptions.Options)
  762. else if P = 'option' then
  763. OptionPragma(FOptions.Options)
  764. else if P = 'verboselevel' then
  765. begin
  766. Include(FOptions.Options, optVerbose);
  767. FOptions.VerboseLevel := IntegerExpr(True);
  768. VerboseMsg(0, SChangedVerboseLevel, [FOptions.VerboseLevel]);
  769. EndOfExpr;
  770. end
  771. else if P = 'warning' then begin
  772. { Also see WarningFunc in IsppFuncs }
  773. WarningMsg(StrPragma(True))
  774. end else if P = 'message' then begin
  775. { Also see MessageFunc in IsppFuncs }
  776. StatusMsg(StrPragma(True))
  777. end else if P = 'error' then begin
  778. { Also see ErrorFunc in IsppFuncs }
  779. ErrorMsg := StrPragma(True);
  780. if ErrorMsg = '' then ErrorMsg := 'Error';
  781. CatchException := False;
  782. RaiseError(ErrorMsg)
  783. end
  784. else
  785. WarningMsg(SFailedToParsePragmaDirective);
  786. end;
  787. except
  788. if CatchException then
  789. WarningMsg(SFailedToParsePragmaDirective)
  790. else
  791. raise
  792. end;
  793. end;
  794. function DoFile(FileName: string): string;
  795. function GetTempFileName(const Original: string): string;
  796. var
  797. Path: string;
  798. begin
  799. SetLength(Path, MAX_PATH);
  800. SetLength(Path, GetTempPath(MAX_PATH, PChar(Path)));
  801. SetLength(Result, MAX_PATH);
  802. if Windows.GetTempFileName(PChar(Path), PChar(UpperCase(Original)), 0, PChar(Result)) <> 0 then
  803. SetLength(Result, StrLen(PChar(Result)))
  804. else
  805. RaiseLastOSError;
  806. end;
  807. var
  808. F: TTextFileReader;
  809. ALine: string;
  810. Preprocessor: TPreprocessor;
  811. NewOptions: TIsppOptions;
  812. begin
  813. FileName := PrependDirName(FileName, FSourcePath);
  814. if FileExists(FileName) then
  815. begin
  816. Result := GetTempFileName(PathExtractName(FileName));
  817. StatusMsg(SProcessingExternalFile, [FileName]);
  818. NewOptions := FOptions;
  819. Preprocessor := TPreprocessor.Create(FCompilerParams, FIdentManager,
  820. NewOptions, FSourcePath, FCompilerPath, FileName);
  821. try
  822. F := TTextFileReader.Create(Filename, fdOpenExisting, faRead, fsRead);
  823. try
  824. while not F.Eof do begin
  825. ALine := F.ReadLine;
  826. Preprocessor.QueueLine(ALine);
  827. end;
  828. finally
  829. F.Free;
  830. end;
  831. Preprocessor.SaveToFile(Result);
  832. QueueFileForDeletion(Result);
  833. VerboseMsg(1, STemporaryFileCreated, [Result]);
  834. finally
  835. Preprocessor.Free;
  836. end;
  837. end
  838. else
  839. RaiseError(Format(SFileNotFound, [FileName]));
  840. end;
  841. procedure ParseFor(Parser: TParserAccess);
  842. var
  843. Condition, Action, Body: PChar;
  844. begin
  845. Parser.NextTokenExpect([tkOpenBrace]);
  846. Parser.Expr(False);
  847. Parser.NextTokenExpect([tkSemicolon]);
  848. { Skip condition and remember it }
  849. Condition := Parser.FExpr;
  850. Parser.Sequentional(False);
  851. Parser.NextTokenExpect([tkSemicolon]);
  852. Action := Parser.FExpr;
  853. Parser.Sequentional(False);
  854. Parser.NextTokenExpect([tkCloseBrace]);
  855. Body := Parser.FExpr;
  856. Parser.Sequentional(False);
  857. Parser.EndOfExpr;
  858. Parser.SetPos(Condition);
  859. while Parser.IntExpr(False) <> 0 do
  860. begin
  861. Parser.SetPos(Body);
  862. Parser.Sequentional(True);
  863. Parser.SetPos(Action);
  864. Parser.Sequentional(True);
  865. Parser.SetPos(Condition);
  866. end;
  867. end;
  868. procedure BeginInsert(LineNo: Integer);
  869. begin
  870. if LineNo > FOutput.Count then
  871. RaiseError(Format(SInsertLineNoTooBig, [LineNo]));
  872. FInsertionPoint := LineNo;
  873. VerboseMsg(2, SChangingInsertionPointToLine, [FInsertionPoint]);
  874. end;
  875. procedure EndInsert;
  876. begin
  877. VerboseMsg(2, SResettingInsertionPoint);
  878. FInsertionPoint := -1;
  879. end;
  880. procedure BeginProcDecl(Parser: TParserAccess);
  881. var
  882. ProcName: string;
  883. begin
  884. if FInForBody or FInProcBody then
  885. RaiseError('Nested procedure declaration and compound loops not allowed');
  886. FInProcBody := True;
  887. Parser.NextTokenExpect([tkIdent]);
  888. ProcName := Parser.TokenString;
  889. Parser.EndOfExpr;
  890. FProcs.AddObject(ProcName, TStringList.Create);
  891. EmitDestination.Add('#define private');
  892. end;
  893. procedure EndProcDecl;
  894. begin
  895. if not FInProcBody then
  896. RaiseError('''endproc'' without ''procedure''');
  897. FInProcBody := False;
  898. end;
  899. var
  900. IfCondition: TIsppVariant;
  901. DummyContext: ICallContext;
  902. Parser: TParserAccess;
  903. begin
  904. Result := False;
  905. Parser := TParserAccess.Create(Self, Params, ParamsOffset, @FOptions.ParserOptions);
  906. with Parser do
  907. try
  908. case Command of
  909. pcError: RaiseError(SUnknownPreprocessorDirective);
  910. pcIf, pcElseIf:
  911. begin
  912. IfCondition := Evaluate;
  913. Result := IfCondition.AsBoolean;
  914. if IfCondition.Typ = evNull then
  915. WarningMsg(SSpecifiedConditionEvalatedToVoid);
  916. end;
  917. pcIfdef, pcIfndef:
  918. begin
  919. NextTokenExpect([tkIdent]);
  920. case GetIdent(TokenString, DummyContext) of
  921. itUnknown: Result := Command = pcIfNDef;
  922. itVariable, itMacro: Result := Command = pcIfDef;
  923. itFunc:
  924. begin
  925. Result := Command = pcIfDef;
  926. WarningMsg(SFuncIdentForIfdef);
  927. end;
  928. else
  929. begin
  930. Result := Command = pcIfNDef;
  931. WarningMsg(SSpecFuncIdentForIfdef);
  932. end;
  933. end;
  934. EndOfExpr;
  935. end;
  936. pcIfExist, pcIfNExist:
  937. Result := FileExists(PrependDirName(StrExpr(False), FSourcePath)) xor (Command = pcIfNExist);
  938. pcDefine: ParseDefine(Parser);
  939. pcDim: ParseDim(Parser, False);
  940. pcReDim: ParseDim(Parser, True);
  941. pcUndef: ParseUndef(Parser);
  942. pcInclude: IncludeFile(Params);
  943. pcErrorDir:
  944. begin
  945. { Also see ErrorFunc in IsppFuncs }
  946. if Params = '' then Params := 'Error';
  947. RaiseError(Params.Trim);
  948. end;
  949. pcPragma: Pragma(Parser);
  950. pcEmit: Params := ToStr(Evaluate).AsStr;
  951. pcEnv:
  952. begin
  953. NextTokenExpect([tkIdent]);
  954. Params := GetEnv(TokenString);
  955. EndOfExpr;
  956. end;
  957. pcFile: Params := DoFile(StrExpr(False));
  958. pcExpr: Evaluate;
  959. pcInsert: BeginInsert(IntegerExpr(False));
  960. pcAppend: EndInsert;
  961. pcFor: ParseFor(Parser);
  962. pcSub: BeginProcDecl(Parser);
  963. pcEndSub: EndProcDecl;
  964. else
  965. WarningMsg(SDirectiveNotYetSupported, [PreprocCommands[Command]])
  966. end;
  967. finally
  968. Free
  969. end;
  970. end;
  971. function TPreprocessor.InternalQueueLine(const LineRead: string;
  972. FileIndex, LineNo: Integer; NonISS: Boolean): Integer; //how many just been added
  973. var
  974. L: Integer;
  975. begin
  976. L := Length(LineRead);
  977. if (L > 2) and (LineRead[L] = FOptions.SpanSymbol) and (LineRead[L - 1] <= #32) then
  978. begin
  979. FQueuedLine := FQueuedLine + TrimLeft(Copy(LineRead, 1, L - 1));
  980. Inc(FQueuedLineCount);
  981. Result := 0;
  982. end
  983. else
  984. if FQueuedLineCount > 0 then
  985. begin
  986. InternalAddLine(FQueuedLine + TrimLeft(LineRead), FileIndex, LineNo, NonISS);
  987. FQueuedLine := '';
  988. Result := FQueuedLineCount + 1;
  989. FQueuedLineCount := 0;
  990. end
  991. else
  992. begin
  993. InternalAddLine(LineRead, FileIndex, LineNo, NonISS);
  994. Result := 1;
  995. end;
  996. end;
  997. procedure TPreprocessor.QueueLine(const LineRead: string);
  998. begin
  999. Inc(FMainCounter, InternalQueueLine(LineRead, 0, FMainCounter, False));
  1000. end;
  1001. procedure TPreprocessor.RegisterFunction(const Name: string; Handler: TIsppFunction; Ext: NativeInt);
  1002. begin
  1003. FIdentManager.DefineFunction(Name, Handler, Ext);
  1004. end;
  1005. procedure TPreprocessor.SaveToFile(const FileName: string);
  1006. begin
  1007. var OldWriteBOM := FOutput.WriteBOM;
  1008. try
  1009. FOutput.WriteBOM := False;
  1010. FOutput.SaveToFile(FileName, TEncoding.UTF8);
  1011. finally
  1012. FOutput.WriteBOM := OldWriteBOM;
  1013. end;
  1014. end;
  1015. function TPreprocessor.CheckFile(const FileName: string): Boolean;
  1016. begin
  1017. Result := FFileStack.IndexOf(PathExpand(FileName)) < 0;
  1018. end;
  1019. procedure TPreprocessor.PopFile;
  1020. begin
  1021. FFileStack.Delete(FFileStack.Count - 1);
  1022. end;
  1023. procedure TPreprocessor.PushFile(const FileName: string);
  1024. begin
  1025. FFileStack.AddObject(PathExpand(FileName), TObject(dsPublic));
  1026. end;
  1027. procedure TPreprocessor.CallIdleProc;
  1028. begin
  1029. FCompilerParams.IdleProc(FCompilerParams.CompilerData);
  1030. end;
  1031. procedure TPreprocessor.VerboseMsg(Level: Integer; const Msg: string);
  1032. begin
  1033. if (optVerbose in FOptions.Options) and (FOptions.VerboseLevel >= Level) then
  1034. StatusMsg(Msg);
  1035. end;
  1036. procedure TPreprocessor.VerboseMsg(Level: Integer; const Msg: string;
  1037. const Args: array of const);
  1038. begin
  1039. VerboseMsg(Level, Format(Msg, Args));
  1040. end;
  1041. procedure TPreprocessor.StatusMsg(const Msg: string);
  1042. begin
  1043. SendMsg(Msg, imtStatus);
  1044. end;
  1045. procedure TPreprocessor.StatusMsg(const Msg: string; const Args: array of const);
  1046. begin
  1047. StatusMsg(Format(Msg, Args));
  1048. end;
  1049. procedure TPreprocessor.WarningMsg(const Msg: string);
  1050. begin
  1051. SendMsg(Msg, imtWarning);
  1052. end;
  1053. procedure TPreprocessor.WarningMsg(const Msg: string; const Args: array of const);
  1054. begin
  1055. WarningMsg(Format(Msg, Args));
  1056. end;
  1057. procedure TPreprocessor.SendMsg(Msg: string; Typ: TIsppMessageType);
  1058. const
  1059. MsgPrefixes: array[TIsppMessageType] of string = ('', 'Warning: ');
  1060. var
  1061. FileName: String;
  1062. begin
  1063. Msg := MsgPrefixes[Typ] + Msg;
  1064. const LineNumber = GetLineNumber(-1);
  1065. if LineNumber <> 0 then begin
  1066. FileName := GetFileName(-1);
  1067. if FileName <> '' then
  1068. Msg := Format('Line %d of %s: %s', [LineNumber, PathExtractName(FileName), Msg])
  1069. else
  1070. Msg := Format('Line %d: %s', [LineNumber, Msg]);
  1071. end;
  1072. FCompilerParams.StatusProc(FCompilerParams.CompilerData, PChar(Msg), Typ = imtWarning);
  1073. end;
  1074. function TPreprocessor.DimOf(const Name: String): Integer;
  1075. begin
  1076. Result := FIdentManager.DimOf(Name)
  1077. end;
  1078. function TPreprocessor.EmitDestination: TStringList;
  1079. begin
  1080. if FInProcBody then
  1081. Result := TStringList(FProcs.Objects[FProcs.Count - 1])
  1082. else
  1083. Result := FOutput;
  1084. end;
  1085. procedure TPreprocessor.ExecProc(Body: TStrings);
  1086. var
  1087. I: Integer;
  1088. begin
  1089. for I := 0 to Body.Count - 1 do
  1090. InternalAddLine(Body[I], Integer(Body.Objects[I]) shr 16,
  1091. Integer(Body.Objects[I]) and $FFFF - 1, False);
  1092. end;
  1093. { TConditionalTranslationStack }
  1094. constructor TConditionalTranslationStack.Create(Preproc: TPreprocessor);
  1095. begin
  1096. inherited Create;
  1097. FPreproc := Preproc;
  1098. FCache := True;
  1099. end;
  1100. procedure TConditionalTranslationStack.IfInstruction(Eval: Boolean);
  1101. var
  1102. A: TConditionalBlockInfo;
  1103. begin
  1104. A.BlockState := Eval;
  1105. A.Fired := Eval;
  1106. A.HadElse := False;
  1107. PushItem(A);
  1108. FCacheValid := False;
  1109. VerboseMsg(cvmIf, Eval);
  1110. end;
  1111. procedure TConditionalTranslationStack.ElseIfInstruction(Eval: Boolean);
  1112. var
  1113. A: TConditionalBlockInfo;
  1114. begin
  1115. if AtLeast(1) then begin
  1116. A := Last;
  1117. with A do begin
  1118. if HadElse then FPreproc.RaiseError(SElifAfterElse);
  1119. BlockState := not Fired and Eval;
  1120. Fired := Fired or Eval;
  1121. FCacheValid := False;
  1122. end;
  1123. UpdateLast(A);
  1124. VerboseMsg(cvmElif, Eval);
  1125. end else
  1126. FPreproc.RaiseError(SElseWithoutIf);
  1127. end;
  1128. procedure TConditionalTranslationStack.ElseInstruction;
  1129. var
  1130. A: TConditionalBlockInfo;
  1131. begin
  1132. if AtLeast(1) then begin
  1133. A := Last;
  1134. with A do begin
  1135. if HadElse then FPreproc.RaiseError(SDoubleElse);
  1136. BlockState := not Fired;
  1137. Fired := True;
  1138. HadElse := True;
  1139. FCacheValid := False;
  1140. end;
  1141. UpdateLast(A);
  1142. VerboseMsg(cvmElse, False);
  1143. end else
  1144. FPreproc.RaiseError(SElseWithoutIf);
  1145. end;
  1146. procedure TConditionalTranslationStack.EndIfInstruction;
  1147. begin
  1148. if AtLeast(1) then begin
  1149. PopItem;
  1150. FCacheValid := False;
  1151. VerboseMsg(cvmEndif, False);
  1152. end else
  1153. FPreproc.RaiseError(SEndifWithoutIf);
  1154. end;
  1155. function TConditionalTranslationStack.Include: Boolean;
  1156. begin
  1157. if FCacheValid then
  1158. Result := FCache
  1159. else begin
  1160. FCacheValid := True;
  1161. if Count > 0 then begin
  1162. Result := False;
  1163. FCache := False;
  1164. for var I := Count - 1 downto 0 do
  1165. if not TConditionalBlockInfo(List[I]).BlockState then
  1166. Exit;
  1167. end;
  1168. Result := True;
  1169. FCache := True;
  1170. end;
  1171. end;
  1172. procedure TConditionalTranslationStack.Resolved;
  1173. begin
  1174. if Count > 0 then
  1175. FPreproc.RaiseError(SEndifExpected);
  1176. end;
  1177. function TConditionalTranslationStack.Last: TConditionalBlockInfo;
  1178. begin
  1179. Result := List.Last;
  1180. end;
  1181. procedure TConditionalTranslationStack.UpdateLast(
  1182. const Value: TConditionalBlockInfo);
  1183. begin
  1184. List[List.Count - 1] := Value;
  1185. end;
  1186. procedure TConditionalTranslationStack.VerboseMsg(
  1187. Msg: TConditionalVerboseMsg; Eval: Boolean);
  1188. const
  1189. B: array[Boolean] of string = ('false', 'true');
  1190. var
  1191. M: string;
  1192. begin
  1193. case Msg of
  1194. cvmIf: M := SStartingConditionalInclusionIf;
  1195. cvmElif: M := SUpdatingConditionalInclusionElif;
  1196. cvmElse: M := SUpdatingConditionalInclusionElse;
  1197. else
  1198. begin
  1199. FPreproc.VerboseMsg(6, SFinishedConditionalInclusion);
  1200. Exit;
  1201. end;
  1202. end;
  1203. FPreproc.VerboseMsg(6, M);
  1204. end;
  1205. { TPreprocessor }
  1206. function TPreprocessor._AddRef: Integer;
  1207. begin
  1208. Result := -1
  1209. end;
  1210. function TPreprocessor._Release: Integer;
  1211. begin
  1212. Result := -1;
  1213. end;
  1214. function TPreprocessor.QueryInterface(const IID: TGUID; out Obj): HRESULT;
  1215. begin
  1216. if GetInterface(IID, Obj) then Result := 0 else Result := E_NOINTERFACE
  1217. end;
  1218. procedure TPreprocessor.RaiseError(const Message: string);
  1219. begin
  1220. RaiseErrorEx(Message, 0);
  1221. end;
  1222. procedure TPreprocessor.RaiseErrorEx(const Message: string; Column: Integer);
  1223. var
  1224. E: EPreprocError;
  1225. begin
  1226. E := EPreprocError.Create(Self, Message);
  1227. E.ColumnNumber := Column;
  1228. raise E;
  1229. end;
  1230. { TPredefinedVarCallContext }
  1231. type
  1232. TPredefinedVarCallContext = class(TInterfacedObject, ICallContext)
  1233. private
  1234. FValue: TIsppVariant;
  1235. public
  1236. constructor Create(const Value: TIsppVariant);
  1237. procedure Add(const Name: String; const Value: TIsppVariant);
  1238. function Call: TIsppVariant; dynamic;
  1239. function GroupingStyle: TArgGroupingStyle;
  1240. procedure Clone(out NewCallContext: ICallContext);
  1241. end;
  1242. TCounterCallContext = class(TPredefinedVarCallContext)
  1243. private
  1244. FCounter: PInteger;
  1245. public
  1246. constructor Create(Counter: PInteger);
  1247. function Call: TIsppVariant; override;
  1248. end;
  1249. TProcCallContext = class(TInterfacedObject, ICallContext)
  1250. private
  1251. FPreproc: TPreprocessor;
  1252. FBody: TStrings;
  1253. FScopeUpdated: Boolean;
  1254. FIndex: Integer;
  1255. procedure UpdateScope;
  1256. public
  1257. constructor Create(Proprocessor: TPreprocessor; ProcBody: TStrings);
  1258. procedure Add(const Name: String; const Value: TIsppVariant);
  1259. function Call: TIsppVariant;
  1260. procedure Clone(out NewContext: ICallContext);
  1261. function GroupingStyle: TArgGroupingStyle;
  1262. end;
  1263. constructor TCounterCallContext.Create(Counter: PInteger);
  1264. begin
  1265. FCounter := Counter;
  1266. end;
  1267. function TCounterCallContext.Call: TIsppVariant;
  1268. begin
  1269. MakeInt(Result, FCounter^);
  1270. Inc(FCounter^);
  1271. end;
  1272. constructor TPredefinedVarCallContext.Create(const Value: TIsppVariant);
  1273. begin
  1274. FValue := Value;
  1275. end;
  1276. procedure TPredefinedVarCallContext.Add(const Name: String;
  1277. const Value: TIsppVariant);
  1278. begin
  1279. raise EIdentError.Create(SParameterlessVariable);
  1280. end;
  1281. function TPredefinedVarCallContext.Call: TIsppVariant;
  1282. begin
  1283. Result := FValue;
  1284. end;
  1285. function TPredefinedVarCallContext.GroupingStyle: TArgGroupingStyle;
  1286. begin
  1287. Result := agsNone;
  1288. end;
  1289. { IIdentManager }
  1290. function LookupAlwaysDefined(const Name: string): Boolean;
  1291. const
  1292. AlwaysDefined: array[0..3] of string =
  1293. ('ISPP_INVOKED', 'WINDOWS', '__WIN32__', 'UNICODE');
  1294. var
  1295. I: Integer;
  1296. begin
  1297. Result := True;
  1298. for I := Low(AlwaysDefined) to High(AlwaysDefined) do
  1299. if CompareText(AlwaysDefined[I], Name) = 0 then Exit;
  1300. Result := False;
  1301. end;
  1302. const
  1303. SCounter = '__COUNTER__';
  1304. function TPreprocessor.Defined(const Name: String): Boolean;
  1305. begin
  1306. Result := LookupAlwaysDefined(Name) or LookupPredefined(Name, nil) or
  1307. (CompareText(Name, SCounter) = 0) or FIdentManager.Defined(Name);
  1308. end;
  1309. function TPreprocessor.GetIdent(const Name: String;
  1310. out CallContext: ICallContext): TIdentType;
  1311. var
  1312. V: TIsppVariant;
  1313. I: Integer;
  1314. begin
  1315. Result := itVariable;
  1316. I := FProcs.IndexOf(Name);
  1317. if I >= 0 then
  1318. begin
  1319. Result := itFunc;
  1320. CallContext := TProcCallContext.Create(Self, TStrings(FProcs.Objects[I]));
  1321. end
  1322. else
  1323. if LookupAlwaysDefined(Name) then
  1324. CallContext := TPredefinedVarCallContext.Create(NULL)
  1325. else
  1326. if LookupPredefined(Name, @V) then
  1327. CallContext := TPredefinedVarCallContext.Create(V)
  1328. else
  1329. if CompareText(Name, SCounter) = 0 then
  1330. CallContext := TCounterCallContext.Create(@FCounter)
  1331. else
  1332. Result := FIdentManager.GetIdent(Name, CallContext)
  1333. end;
  1334. function TPreprocessor.TypeOf(const Name: String): Byte;
  1335. var
  1336. V: TIsppVariant;
  1337. begin
  1338. if LookupAlwaysDefined(Name) then
  1339. Result := TYPE_NULL
  1340. else
  1341. if LookupPredefined(Name, @V) then
  1342. case V.Typ of
  1343. evInt: Result := TYPE_INTEGER;
  1344. evStr: Result := TYPE_STRING
  1345. else
  1346. Result := TYPE_NULL
  1347. end
  1348. else
  1349. if CompareText(Name, SCounter) = 0 then
  1350. Result := TYPE_INTEGER
  1351. else
  1352. Result := FIdentManager.TypeOf(Name)
  1353. end;
  1354. function TPreprocessor.LookupPredefined(Name: string;
  1355. Value: PIsppVariant): Boolean;
  1356. begin
  1357. Result := True;
  1358. Name := UpperCase(Name);
  1359. if (Name = '__FILENAME__') or (Name = '__FILE__') then
  1360. begin
  1361. if Value <> nil then MakeStr(Value^, PathExtractName(FIncludes[FCurrentFile]))
  1362. end
  1363. else if Name = '__PATHFILENAME__' then
  1364. begin
  1365. if Value <> nil then MakeStr(Value^, FIncludes[FCurrentFile])
  1366. end
  1367. else if Name = '__DIR__' then
  1368. begin
  1369. if Value <> nil then MakeStr(Value^, PathExtractDir(FIncludes[FCurrentFile]))
  1370. end
  1371. else if Name = '__LINE__' then
  1372. begin
  1373. if Value <> nil then MakeInt(Value^, FCurrentLine)
  1374. end
  1375. else if Name = 'PREPROCVER' then
  1376. begin
  1377. if Value <> nil then MakeInt(Value^, SetupBinVersion)
  1378. end
  1379. else if Name = '__INCLUDE__' then
  1380. begin
  1381. if Value <> nil then MakeStr(Value^, FIncludePath);
  1382. end
  1383. else if (Length(Name) = 9) and (Copy(Name, 1, 6) = '__OPT_') and
  1384. (Copy(Name, 8, 2) = '__') then
  1385. begin
  1386. if Value <> nil then Value^ := NULL;
  1387. Result := GetOption(FOptions.Options, Name[7]);
  1388. end
  1389. else if (Length(Name) = 10) and (Copy(Name, 1, 7) = '__POPT_') and
  1390. (Copy(Name, 9, 2) = '__') then
  1391. begin
  1392. if Value <> nil then Value^ := NULL;
  1393. Result := GetOption(FOptions.ParserOptions.Options, Name[8]);
  1394. end
  1395. else
  1396. Result := False;
  1397. end;
  1398. procedure TPredefinedVarCallContext.Clone(
  1399. out NewCallContext: ICallContext);
  1400. begin
  1401. NewCallContext := Self
  1402. end;
  1403. procedure TPreprocessor.CollectGarbage(Item: Pointer;
  1404. Proc: TDropGarbageProc);
  1405. begin
  1406. if (Item = nil) or (@Proc = nil) then
  1407. Exit;
  1408. if FGarbageCollection = nil then
  1409. FGarbageCollection := TList.Create;
  1410. FGarbageCollection.Add(Item);
  1411. FGarbageCollection.Add(@Proc);
  1412. end;
  1413. procedure TPreprocessor.UncollectGarbage(Item: Pointer);
  1414. begin
  1415. if FGarbageCollection = nil then
  1416. Exit;
  1417. for var I := 0 to FGarbageCollection.Count div 2 - 1 do
  1418. if FGarbageCollection.Items[I * 2] = Item then begin
  1419. FGarbageCollection.Items[I * 2] := nil;
  1420. FGarbageCollection.Items[I * 2 + 1] := nil;
  1421. end;
  1422. FGarbageCollection.Pack;
  1423. if FGarbageCollection.Count = 0 then
  1424. FreeAndNil(FGarbageCollection);
  1425. end;
  1426. procedure TPreprocessor.DropGarbage;
  1427. var
  1428. Proc: TDropGarbageProc;
  1429. Item: Pointer;
  1430. begin
  1431. if FGarbageCollection <> nil then
  1432. try
  1433. for var I := 0 to FGarbageCollection.Count div 2 - 1 do begin
  1434. Item := FGarbageCollection.Items[I * 2];
  1435. Proc := FGarbageCollection.Items[I * 2 + 1];
  1436. try
  1437. if @Proc <> nil then
  1438. try
  1439. Proc(Item);
  1440. except
  1441. end
  1442. else
  1443. if Item <> nil then
  1444. begin
  1445. try
  1446. TObject(Item).Free
  1447. except
  1448. try Dispose(Item) except end;
  1449. end;
  1450. end;
  1451. finally
  1452. FGarbageCollection.Items[I * 2] := nil;
  1453. FGarbageCollection.Items[I * 2 + 1] := nil;
  1454. end;
  1455. end;
  1456. finally
  1457. FreeAndNil(FGarbageCollection);
  1458. end;
  1459. end;
  1460. function TPreprocessor.PrependDirName(const FileName, Dir: string): string;
  1461. var
  1462. P: PChar;
  1463. begin
  1464. P := FCompilerParams.PrependDirNameProc(FCompilerParams.CompilerData,
  1465. PChar(FileName), PChar(Dir), PChar(GetFileName(-1)), GetLineNumber(-1), 0);
  1466. if P = nil then
  1467. RaiseError('PrependDirNameProc failed');
  1468. Result := P;
  1469. end;
  1470. procedure TPreprocessor.IncludeFile(FileName: string;
  1471. Builtins, UseIncludePathOnly, ResetCurrentFile: Boolean);
  1472. function IsDotRelativePath(const Filename: String): Boolean;
  1473. begin
  1474. { Check for '.\' and '..\' }
  1475. if (Length(Filename) >= 2) and (Filename[1] = '.') and PathCharIsSlash(Filename[2]) then
  1476. Result := True
  1477. else if (Length(Filename) >= 3) and (Filename[1] = '.') and (Filename[2] = '.') and
  1478. PathCharIsSlash(Filename[3]) then
  1479. Result := True
  1480. else
  1481. Result := False;
  1482. end;
  1483. procedure AddToPath(var Path: string; const Dir: string);
  1484. begin
  1485. if (Dir <> '') and (Pos(';' + Dir + ';', ';' + Path + ';') = 0) then
  1486. begin
  1487. if Path <> '' then Path := Path + ';';
  1488. Path := Path + Dir;
  1489. end;
  1490. end;
  1491. function DoSearch(const SearchDirs: String): String;
  1492. var
  1493. FilePart: PChar;
  1494. begin
  1495. SetLength(Result, MAX_PATH);
  1496. SetLength(Result, SearchPath(PChar(SearchDirs), PChar(FileName), nil, MAX_PATH,
  1497. PChar(Result), FilePart));
  1498. end;
  1499. var
  1500. CurPath, SearchDirs, FullFileName: String;
  1501. FileHandle: TPreprocFileHandle;
  1502. FileIndex: Integer;
  1503. LineText: PChar;
  1504. LineTextStr: string;
  1505. begin
  1506. if ResetCurrentFile then begin
  1507. FCurrentFile := 0;
  1508. FCurrentLine := 0;
  1509. end;
  1510. { Expand any prefix on the filename (e.g. 'compiler:') }
  1511. FileName := PrependDirName(FileName, '');
  1512. if IsDotRelativePath(FileName) then
  1513. begin
  1514. { Make filenames beginning with '.\' and '..\' relative to the directory
  1515. containing the current file }
  1516. CurPath := PathExtractPath(FIncludes[FCurrentFile]);
  1517. if CurPath = '' then
  1518. CurPath := FSourcePath;
  1519. FileName := PathCombine(CurPath, FileName);
  1520. end
  1521. else if not PathIsRooted(FileName) then
  1522. begin
  1523. if not UseIncludePathOnly then
  1524. begin
  1525. for var I := FFileStack.Count - 1 downto 0 do
  1526. AddToPath(SearchDirs, PathExtractDir(FFileStack[I]));
  1527. if FIncludes[0] <> '' then
  1528. AddToPath(SearchDirs, PathExtractDir(FIncludes[0]));
  1529. AddToPath(SearchDirs, RemoveBackslashUnlessRoot(FSourcePath));
  1530. end;
  1531. AddToPath(SearchDirs, FIncludePath);
  1532. AddToPath(SearchDirs, GetEnv('INCLUDE'));
  1533. if not UseIncludePathOnly then
  1534. AddToPath(SearchDirs, RemoveBackslashUnlessRoot(FCompilerPath));
  1535. end;
  1536. FullFileName := DoSearch(SearchDirs);
  1537. if FullFileName <> '' then
  1538. begin
  1539. if not CheckFile(FullFileName) then
  1540. RaiseError(Format(SFileIsAlreadyBeingIncluded, [FullFileName]));
  1541. if not Builtins then
  1542. StatusMsg(SIncludingFile, [FullFileName]);
  1543. PushFile(FullFileName);
  1544. try
  1545. FileHandle := FCompilerParams.LoadFileProc(FCompilerParams.CompilerData,
  1546. PChar(FullFileName), PChar(GetFileName(-1)), GetLineNumber(-1), 0);
  1547. if FileHandle < 0 then
  1548. RaiseError('LoadFileProc failed');
  1549. FileIndex := FIncludes.Add(FullFileName);
  1550. FIdentManager.BeginLocal;
  1551. try
  1552. var I := 0;
  1553. var J := 0;
  1554. while True do
  1555. begin
  1556. LineText := FCompilerParams.LineInProc(FCompilerParams.CompilerData,
  1557. FileHandle, I);
  1558. if LineText = nil then
  1559. Break;
  1560. LineTextStr := LineText;
  1561. Inc(J, InternalQueueLine(LineTextStr, FileIndex, J, False));
  1562. Inc(I);
  1563. end;
  1564. finally
  1565. FIdentManager.EndLocal
  1566. end;
  1567. finally
  1568. PopFile;
  1569. end;
  1570. end
  1571. else
  1572. RaiseError(Format(SFileNotFound, [FileName]));
  1573. end;
  1574. // ParseFormalParams
  1575. // Parser must be behind the opening parenthesis
  1576. function TPreprocessor.ParseFormalParams(Parser: TParser;
  1577. var ParamList: PParamList): Integer;
  1578. var
  1579. Param: TIsppMacroParam;
  1580. Ident: string;
  1581. procedure Grow;
  1582. var
  1583. OldCapacity, NewCapacity: Integer;
  1584. begin
  1585. OldCapacity := ((Result div 4) * 4) * SizeOf(TIsppMacroParam);
  1586. NewCapacity := ((Result div 4 + 1) * 4);
  1587. if NewCapacity > High(Byte) then RaiseError(STooManyFormalParams);
  1588. NewCapacity := NewCapacity * SizeOf(TIsppMacroParam);
  1589. ReallocMem(ParamList, NewCapacity);
  1590. { Initializing to zeroes is required to prevent compiler's attempts to
  1591. finilize not existing strings }
  1592. FillChar(ParamList^[Result], NewCapacity - OldCapacity, 0)
  1593. end;
  1594. begin
  1595. with Parser do
  1596. begin
  1597. Result := 0;
  1598. ParamList := AllocMem(SizeOf(TIsppMacroParam) * 4);
  1599. while not (PeekAtNextToken in [tkEOF, tkCloseParen]) do
  1600. begin
  1601. Param.Name := '';
  1602. Param.DefValue.AsStr := '';
  1603. FillChar(Param, SizeOf(Param), 0);
  1604. Param.ParamFlags := [];
  1605. if NextTokenExpect([tkIdent, opMul]) = tkIdent then
  1606. begin
  1607. Ident := TokenString;
  1608. if not (PeekAtNextToken in [tkEOF, tkComma, tkCloseParen, opAssign]) then
  1609. begin
  1610. Ident := UpperCase(Ident);
  1611. if Ident = sAny then {do nothing }
  1612. else if Ident = sInt then Param.DefValue.Typ := evInt
  1613. else if Ident = sStr then Param.DefValue.Typ := evStr
  1614. else if Ident = 'FUNC' then
  1615. begin
  1616. Param.DefValue.Typ := evCallContext;
  1617. Include(Param.ParamFlags, pfFunc)
  1618. end
  1619. else if Ident = 'ARRAY' then Param.DefValue.Typ := evCallContext
  1620. else RaiseError(Format(SInvalidTypeId, [Ident]));
  1621. if Param.DefValue.Typ <> evSpecial then
  1622. Include(Param.ParamFlags, pfTypeDefined);
  1623. if NextTokenExpect([tkIdent, opMul]) = opMul then
  1624. begin
  1625. Include(Param.ParamFlags, pfByRef);
  1626. NextTokenExpect([tkIdent]);
  1627. end;
  1628. end;
  1629. end
  1630. else
  1631. begin
  1632. Include(Param.ParamFlags, pfByRef);
  1633. NextTokenExpect([tkIdent]);
  1634. end;
  1635. Ident := TokenString;
  1636. Param.Name := CheckReservedIdent(Ident);
  1637. if PeekAtNextToken = opAssign then
  1638. begin
  1639. if pfByRef in Param.ParamFlags then
  1640. RaiseError(SByRefNoDefault);
  1641. NextToken;
  1642. case Param.DefValue.Typ of
  1643. evSpecial: Param.DefValue := GetRValue(Expr(True));
  1644. evInt: Param.DefValue.AsInt64 := IntExpr(True);
  1645. evStr: Param.DefValue.AsStr := StrExpr(True);
  1646. end;
  1647. Include(Param.ParamFlags, pfHasDefault);
  1648. end;
  1649. ParamList^[Result] := Param;
  1650. Inc(Result);
  1651. if Result mod 4 = 0 then
  1652. Grow;
  1653. if NextTokenExpect([tkComma, tkCloseParen]) = tkCloseParen then Break;
  1654. end;
  1655. end;
  1656. end;
  1657. { TProcCallContext }
  1658. procedure TProcCallContext.Add(const Name: String;
  1659. const Value: TIsppVariant);
  1660. begin
  1661. UpdateScope;
  1662. if Name <> '' then
  1663. FPreproc.FIdentManager.DefineVariable(Name, -1, Value, dsPrivate);
  1664. FPreproc.FIdentManager.DefineVariable(SLocal, FIndex, Value, dsPrivate);
  1665. Inc(FIndex);
  1666. end;
  1667. function TProcCallContext.Call: TIsppVariant;
  1668. begin
  1669. UpdateScope;
  1670. try
  1671. FPreproc.ExecProc(FBody);
  1672. finally
  1673. FPreproc.FIdentManager.EndLocal
  1674. end;
  1675. end;
  1676. procedure TProcCallContext.Clone(out NewContext: ICallContext);
  1677. begin
  1678. NewContext := TProcCallContext.Create(FPreproc, FBody);
  1679. end;
  1680. constructor TProcCallContext.Create(Proprocessor: TPreprocessor;
  1681. ProcBody: TStrings);
  1682. begin
  1683. FPreproc := Proprocessor;
  1684. FBody := ProcBody
  1685. end;
  1686. function TProcCallContext.GroupingStyle: TArgGroupingStyle;
  1687. begin
  1688. Result := agsParenteses;
  1689. end;
  1690. procedure TProcCallContext.UpdateScope;
  1691. var
  1692. ReDim: Boolean;
  1693. begin
  1694. if not FScopeUpdated then
  1695. begin
  1696. FPreproc.FIdentManager.BeginLocal;
  1697. ReDim := False;
  1698. FPreproc.FIdentManager.DimVariable(SLocal, 16, dsPrivate, ReDim);
  1699. FScopeUpdated := True;
  1700. end;
  1701. end;
  1702. initialization
  1703. { The code above stuffs TConditionalBlockInfo records in a TList without additional allocations.
  1704. In other words, TConditionalBlockInfo must fit into a Pointer. }
  1705. if SizeOf(TConditionalBlockInfo) > SizeOf(Pointer) then
  1706. raise Exception.Create('SizeOf(TConditionalBlockInfo) > SizeOf(Pointer)');
  1707. end.