tcbaseparser.pas 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  1. unit tcbaseparser;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, fpcunit, pastree, pscanner, pparser, testregistry;
  6. const
  7. DefaultMainFilename = 'afile.pp';
  8. Type
  9. { TTestEngine }
  10. TTestEngine = Class(TPasTreeContainer)
  11. Private
  12. FList : TFPList;
  13. public
  14. Destructor Destroy; override;
  15. function CreateElement(AClass: TPTreeElement; const AName: String;
  16. AParent: TPasElement; AVisibility: TPasMemberVisibility;
  17. const ASourceFilename: String; ASourceLinenumber: Integer): TPasElement;
  18. override;
  19. function FindElement(const AName: String): TPasElement; override;
  20. end;
  21. TTestPasParser = Class(TPasParser);
  22. { TTestParser }
  23. TTestParser = class(TTestCase)
  24. Private
  25. FDeclarations: TPasDeclarations;
  26. FDefinition: TPasElement;
  27. FEngine : TPasTreeContainer;
  28. FMainFilename: string;
  29. FModule: TPasModule;
  30. FParseResult: TPasElement;
  31. FScanner : TPascalScanner;
  32. FResolver : TStreamResolver;
  33. FParser : TTestPasParser;
  34. FSource: TStrings;
  35. FFileName : string;
  36. FIsUnit : Boolean;
  37. FImplementation : Boolean;
  38. FEndSource: Boolean;
  39. FUseImplementation: Boolean;
  40. function GetPL: TPasLibrary;
  41. function GetPP: TPasProgram;
  42. procedure CleanupParser;
  43. procedure SetupParser;
  44. protected
  45. procedure SetUp; override;
  46. procedure TearDown; override;
  47. procedure CreateEngine(var TheEngine: TPasTreeContainer); virtual;
  48. Procedure StartUnit(AUnitName : String);
  49. Procedure StartProgram(AFileName : String; AIn : String = ''; AOut : String = '');
  50. Procedure StartLibrary(AFileName : String);
  51. Procedure UsesClause(Units : Array of string);
  52. Procedure StartImplementation;
  53. Procedure EndSource;
  54. Procedure Add(Const ALine : String);
  55. Procedure Add(Const Lines : array of String);
  56. Procedure StartParsing;
  57. Procedure ParseDeclarations;
  58. Procedure ParseModule;
  59. procedure ResetParser;
  60. Procedure CheckHint(AHint : TPasMemberHint);
  61. Function AssertExpression(Const Msg: String; AExpr : TPasExpr; aKind : TPasExprKind; AClass : TClass) : TPasExpr;
  62. Function AssertExpression(Const Msg: String; AExpr : TPasExpr; aKind : TPasExprKind; AValue : String) : TPrimitiveExpr;
  63. Procedure AssertExportSymbol(Const Msg: String; AIndex : Integer; AName,AExportName : String; AExportIndex : Integer = -1);
  64. Procedure AssertEquals(Const Msg : String; AExpected, AActual: TPasExprKind); overload;
  65. Procedure AssertEquals(Const Msg : String; AExpected, AActual: TLoopType); overload;
  66. Procedure AssertEquals(Const Msg : String; AExpected, AActual: TPasObjKind); overload;
  67. Procedure AssertEquals(Const Msg : String; AExpected, AActual: TexprOpcode); overload;
  68. Procedure AssertEquals(Const Msg : String; AExpected, AActual: TPasMemberHint); overload;
  69. Procedure AssertEquals(Const Msg : String; AExpected, AActual: TCallingConvention); overload;
  70. Procedure AssertEquals(Const Msg : String; AExpected, AActual: TArgumentAccess); overload;
  71. Procedure AssertEquals(Const Msg : String; AExpected, AActual: TVariableModifier); overload;
  72. Procedure AssertEquals(Const Msg : String; AExpected, AActual: TVariableModifiers); overload;
  73. Procedure AssertEquals(Const Msg : String; AExpected, AActual: TPasMemberVisibility); overload;
  74. Procedure AssertEquals(Const Msg : String; AExpected, AActual: TProcedureModifier); overload;
  75. Procedure AssertEquals(Const Msg : String; AExpected, AActual: TProcedureModifiers); overload;
  76. Procedure AssertEquals(Const Msg : String; AExpected, AActual: TProcTypeModifiers); overload;
  77. Procedure AssertEquals(Const Msg : String; AExpected, AActual: TAssignKind); overload;
  78. Procedure AssertEquals(Const Msg : String; AExpected, AActual: TProcedureMessageType); overload;
  79. Procedure AssertEquals(Const Msg : String; AExpected, AActual: TOperatorType); overload;
  80. Procedure AssertSame(Const Msg : String; AExpected, AActual: TPasElement); overload;
  81. Procedure HaveHint(AHint : TPasMemberHint; AHints : TPasMemberHints);
  82. Property Resolver : TStreamResolver Read FResolver;
  83. Property Scanner : TPascalScanner Read FScanner;
  84. Property Engine : TPasTreeContainer read FEngine;
  85. Property Parser : TTestPasParser read FParser ;
  86. Property Source : TStrings Read FSource;
  87. Property Module : TPasModule Read FModule;
  88. Property PasProgram : TPasProgram Read GetPP;
  89. Property PasLibrary : TPasLibrary Read GetPL;
  90. Property Declarations : TPasDeclarations read FDeclarations Write FDeclarations;
  91. Property Definition : TPasElement Read FDefinition Write FDefinition;
  92. // If set, Will be freed in teardown
  93. Property ParseResult : TPasElement Read FParseResult Write FParseResult;
  94. Property UseImplementation : Boolean Read FUseImplementation Write FUseImplementation;
  95. Property MainFilename: string read FMainFilename write FMainFilename;
  96. end;
  97. function ExtractFileUnitName(aFilename: string): string;
  98. function GetPasElementDesc(El: TPasElement): string;
  99. procedure ReadNextPascalToken(var Position: PChar; out TokenStart: PChar;
  100. NestedComments: boolean; SkipDirectives: boolean);
  101. implementation
  102. uses typinfo;
  103. function ExtractFileUnitName(aFilename: string): string;
  104. var
  105. p: Integer;
  106. begin
  107. Result:=ExtractFileName(aFilename);
  108. if Result='' then exit;
  109. for p:=length(Result) downto 1 do
  110. case Result[p] of
  111. '/','\': exit;
  112. '.':
  113. begin
  114. Delete(Result,p,length(Result));
  115. exit;
  116. end;
  117. end;
  118. end;
  119. function GetPasElementDesc(El: TPasElement): string;
  120. begin
  121. if El=nil then exit('nil');
  122. Result:=El.Name+':'+El.ClassName+'['+El.SourceFilename+','+IntToStr(El.SourceLinenumber)+']';
  123. end;
  124. procedure ReadNextPascalToken(var Position: PChar; out TokenStart: PChar;
  125. NestedComments: boolean; SkipDirectives: boolean);
  126. const
  127. IdentChars = ['a'..'z','A'..'Z','_','0'..'9'];
  128. HexNumberChars = ['0'..'9','a'..'f','A'..'F'];
  129. var
  130. c1:char;
  131. CommentLvl: Integer;
  132. Src: PChar;
  133. begin
  134. Src:=Position;
  135. // read till next atom
  136. while true do
  137. begin
  138. case Src^ of
  139. #0: break;
  140. #1..#32: // spaces and special characters
  141. inc(Src);
  142. #$EF:
  143. if (Src[1]=#$BB)
  144. and (Src[2]=#$BF) then
  145. begin
  146. // skip UTF BOM
  147. inc(Src,3);
  148. end
  149. else
  150. break;
  151. '{': // comment start or compiler directive
  152. if (Src[1]='$') and (not SkipDirectives) then
  153. // compiler directive
  154. break
  155. else begin
  156. // Pascal comment => skip
  157. CommentLvl:=1;
  158. while true do
  159. begin
  160. inc(Src);
  161. case Src^ of
  162. #0: break;
  163. '{':
  164. if NestedComments then
  165. inc(CommentLvl);
  166. '}':
  167. begin
  168. dec(CommentLvl);
  169. if CommentLvl=0 then
  170. begin
  171. inc(Src);
  172. break;
  173. end;
  174. end;
  175. end;
  176. end;
  177. end;
  178. '/': // comment or real division
  179. if (Src[1]='/') then
  180. begin
  181. // comment start -> read til line end
  182. inc(Src);
  183. while not (Src^ in [#0,#10,#13]) do
  184. inc(Src);
  185. end
  186. else
  187. break;
  188. '(': // comment, bracket or compiler directive
  189. if (Src[1]='*') then
  190. begin
  191. if (Src[2]='$') and (not SkipDirectives) then
  192. // compiler directive
  193. break
  194. else
  195. begin
  196. // comment start -> read til comment end
  197. inc(Src,2);
  198. CommentLvl:=1;
  199. while true do
  200. begin
  201. case Src^ of
  202. #0: break;
  203. '(':
  204. if NestedComments and (Src[1]='*') then
  205. inc(CommentLvl);
  206. '*':
  207. if (Src[1]=')') then
  208. begin
  209. dec(CommentLvl);
  210. if CommentLvl=0 then
  211. begin
  212. inc(Src,2);
  213. break;
  214. end;
  215. inc(Position);
  216. end;
  217. end;
  218. inc(Src);
  219. end;
  220. end;
  221. end else
  222. // round bracket open
  223. break;
  224. else
  225. break;
  226. end;
  227. end;
  228. // read token
  229. TokenStart:=Src;
  230. c1:=Src^;
  231. case c1 of
  232. #0:
  233. ;
  234. 'A'..'Z','a'..'z','_':
  235. begin
  236. // identifier
  237. inc(Src);
  238. while Src^ in IdentChars do
  239. inc(Src);
  240. end;
  241. '0'..'9': // number
  242. begin
  243. inc(Src);
  244. // read numbers
  245. while (Src^ in ['0'..'9']) do
  246. inc(Src);
  247. if (Src^='.') and (Src[1]<>'.') then
  248. begin
  249. // real type number
  250. inc(Src);
  251. while (Src^ in ['0'..'9']) do
  252. inc(Src);
  253. end;
  254. if (Src^ in ['e','E']) then
  255. begin
  256. // read exponent
  257. inc(Src);
  258. if (Src^='-') then inc(Src);
  259. while (Src^ in ['0'..'9']) do
  260. inc(Src);
  261. end;
  262. end;
  263. '''','#': // string constant
  264. while true do
  265. case Src^ of
  266. #0: break;
  267. '#':
  268. begin
  269. inc(Src);
  270. while Src^ in ['0'..'9'] do
  271. inc(Src);
  272. end;
  273. '''':
  274. begin
  275. inc(Src);
  276. while not (Src^ in ['''',#0]) do
  277. inc(Src);
  278. if Src^='''' then
  279. inc(Src);
  280. end;
  281. else
  282. break;
  283. end;
  284. '$': // hex constant
  285. begin
  286. inc(Src);
  287. while Src^ in HexNumberChars do
  288. inc(Src);
  289. end;
  290. '&': // octal constant or keyword as identifier (e.g. &label)
  291. begin
  292. inc(Src);
  293. if Src^ in ['0'..'7'] then
  294. while Src^ in ['0'..'7'] do
  295. inc(Src)
  296. else
  297. while Src^ in IdentChars do
  298. inc(Src);
  299. end;
  300. '{': // compiler directive (it can't be a comment, because see above)
  301. begin
  302. CommentLvl:=1;
  303. while true do
  304. begin
  305. inc(Src);
  306. case Src^ of
  307. #0: break;
  308. '{':
  309. if NestedComments then
  310. inc(CommentLvl);
  311. '}':
  312. begin
  313. dec(CommentLvl);
  314. if CommentLvl=0 then
  315. begin
  316. inc(Src);
  317. break;
  318. end;
  319. end;
  320. end;
  321. end;
  322. end;
  323. '(': // bracket or compiler directive
  324. if (Src[1]='*') then
  325. begin
  326. // compiler directive -> read til comment end
  327. inc(Src,2);
  328. while (Src^<>#0) and ((Src^<>'*') or (Src[1]<>')')) do
  329. inc(Src);
  330. inc(Src,2);
  331. end
  332. else
  333. // round bracket open
  334. inc(Src);
  335. #192..#255:
  336. begin
  337. // read UTF8 character
  338. inc(Src);
  339. if ((ord(c1) and %11100000) = %11000000) then
  340. begin
  341. // could be 2 byte character
  342. if (ord(Src[0]) and %11000000) = %10000000 then
  343. inc(Src);
  344. end
  345. else if ((ord(c1) and %11110000) = %11100000) then
  346. begin
  347. // could be 3 byte character
  348. if ((ord(Src[0]) and %11000000) = %10000000)
  349. and ((ord(Src[1]) and %11000000) = %10000000) then
  350. inc(Src,2);
  351. end
  352. else if ((ord(c1) and %11111000) = %11110000) then
  353. begin
  354. // could be 4 byte character
  355. if ((ord(Src[0]) and %11000000) = %10000000)
  356. and ((ord(Src[1]) and %11000000) = %10000000)
  357. and ((ord(Src[2]) and %11000000) = %10000000) then
  358. inc(Src,3);
  359. end;
  360. end;
  361. else
  362. inc(Src);
  363. case c1 of
  364. '<': if Src^ in ['>','='] then inc(Src);
  365. '.': if Src^='.' then inc(Src);
  366. '@':
  367. if Src^='@' then
  368. begin
  369. // @@ label
  370. repeat
  371. inc(Src);
  372. until not (Src^ in IdentChars);
  373. end
  374. else
  375. if (Src^='=') and (c1 in [':','+','-','/','*','<','>']) then
  376. inc(Src);
  377. end;
  378. end;
  379. Position:=Src;
  380. end;
  381. { TTestEngine }
  382. destructor TTestEngine.Destroy;
  383. begin
  384. FreeAndNil(FList);
  385. inherited Destroy;
  386. end;
  387. function TTestEngine.CreateElement(AClass: TPTreeElement; const AName: String;
  388. AParent: TPasElement; AVisibility: TPasMemberVisibility;
  389. const ASourceFilename: String; ASourceLinenumber: Integer): TPasElement;
  390. begin
  391. Result := AClass.Create(AName, AParent);
  392. Result.Visibility := AVisibility;
  393. Result.SourceFilename := ASourceFilename;
  394. Result.SourceLinenumber := ASourceLinenumber;
  395. if NeedComments and Assigned(CurrentParser) then
  396. begin
  397. // Writeln('Saving comment : ',CurrentParser.SavedComments);
  398. Result.DocComment:=CurrentParser.SavedComments;
  399. end;
  400. If not Assigned(FList) then
  401. FList:=TFPList.Create;
  402. FList.Add(Result);
  403. end;
  404. function TTestEngine.FindElement(const AName: String): TPasElement;
  405. Var
  406. I : Integer;
  407. begin
  408. Result:=Nil;
  409. if Assigned(FList) then
  410. begin
  411. I:=FList.Count-1;
  412. While (Result=Nil) and (I>=0) do
  413. begin
  414. if CompareText(TPasElement(FList[I]).Name,AName)=0 then
  415. Result:=TPasElement(Flist[i]);
  416. Dec(i);
  417. end;
  418. end;
  419. end;
  420. function TTestParser.GetPP: TPasProgram;
  421. begin
  422. Result:=Module as TPasProgram;
  423. end;
  424. function TTestParser.GetPL: TPasLibrary;
  425. begin
  426. Result:=Module as TPasLibrary;
  427. end;
  428. procedure TTestParser.SetupParser;
  429. begin
  430. FResolver:=TStreamResolver.Create;
  431. FResolver.OwnsStreams:=True;
  432. FScanner:=TPascalScanner.Create(FResolver);
  433. CreateEngine(FEngine);
  434. FParser:=TTestPasParser.Create(FScanner,FResolver,FEngine);
  435. FSource:=TStringList.Create;
  436. FModule:=Nil;
  437. FDeclarations:=Nil;
  438. FEndSource:=False;
  439. FImplementation:=False;
  440. FIsUnit:=False;
  441. end;
  442. procedure TTestParser.CleanupParser;
  443. begin
  444. {$IFDEF VerbosePasResolverMem}
  445. writeln('TTestParser.CleanupParser START');
  446. {$ENDIF}
  447. if Not Assigned(FModule) then
  448. FreeAndNil(FDeclarations)
  449. else
  450. FDeclarations:=Nil;
  451. FImplementation:=False;
  452. FEndSource:=False;
  453. FIsUnit:=False;
  454. {$IFDEF VerbosePasResolverMem}
  455. writeln('TTestParser.CleanupParser FModule');
  456. {$ENDIF}
  457. if Assigned(FModule) then
  458. ReleaseAndNil(TPasElement(FModule));
  459. {$IFDEF VerbosePasResolverMem}
  460. writeln('TTestParser.CleanupParser FSource');
  461. {$ENDIF}
  462. FreeAndNil(FSource);
  463. {$IFDEF VerbosePasResolverMem}
  464. writeln('TTestParser.CleanupParser FParseResult');
  465. {$ENDIF}
  466. FreeAndNil(FParseResult);
  467. {$IFDEF VerbosePasResolverMem}
  468. writeln('TTestParser.CleanupParser FParser');
  469. {$ENDIF}
  470. FreeAndNil(FParser);
  471. {$IFDEF VerbosePasResolverMem}
  472. writeln('TTestParser.CleanupParser FEngine');
  473. {$ENDIF}
  474. FreeAndNil(FEngine);
  475. {$IFDEF VerbosePasResolverMem}
  476. writeln('TTestParser.CleanupParser FScanner');
  477. {$ENDIF}
  478. FreeAndNil(FScanner);
  479. {$IFDEF VerbosePasResolverMem}
  480. writeln('TTestParser.CleanupParser FResolver');
  481. {$ENDIF}
  482. FreeAndNil(FResolver);
  483. {$IFDEF VerbosePasResolverMem}
  484. writeln('TTestParser.CleanupParser END');
  485. {$ENDIF}
  486. end;
  487. procedure TTestParser.ResetParser;
  488. begin
  489. CleanupParser;
  490. SetupParser;
  491. end;
  492. procedure TTestParser.SetUp;
  493. begin
  494. FMainFilename:=DefaultMainFilename;
  495. Inherited;
  496. SetupParser;
  497. end;
  498. procedure TTestParser.TearDown;
  499. begin
  500. {$IFDEF VerbosePasResolverMem}
  501. writeln('TTestParser.TearDown START CleanupParser');
  502. {$ENDIF}
  503. CleanupParser;
  504. {$IFDEF VerbosePasResolverMem}
  505. writeln('TTestParser.TearDown inherited');
  506. {$ENDIF}
  507. Inherited;
  508. {$IFDEF VerbosePasResolverMem}
  509. writeln('TTestParser.TearDown END');
  510. {$ENDIF}
  511. end;
  512. procedure TTestParser.CreateEngine(var TheEngine: TPasTreeContainer);
  513. begin
  514. TheEngine:=TTestEngine.Create;
  515. end;
  516. procedure TTestParser.StartUnit(AUnitName: String);
  517. begin
  518. FIsUnit:=True;
  519. If (AUnitName='') then
  520. AUnitName:=ExtractFileUnitName(MainFilename);
  521. Add('unit '+aUnitName+';');
  522. Add('');
  523. Add('interface');
  524. Add('');
  525. FFileName:=AUnitName+'.pp';
  526. end;
  527. procedure TTestParser.StartProgram(AFileName : String; AIn : String = ''; AOut : String = '');
  528. begin
  529. FIsUnit:=False;
  530. If (AFileName='') then
  531. AFileName:='proga';
  532. FFileName:=AFileName+'.pp';
  533. If (AIn<>'') then
  534. begin
  535. AFileName:=AFileName+'('+AIn;
  536. if (AOut<>'') then
  537. AFileName:=AFileName+','+AOut;
  538. AFileName:=AFileName+')';
  539. end;
  540. Add('program '+AFileName+';');
  541. FImplementation:=True;
  542. end;
  543. procedure TTestParser.StartLibrary(AFileName: String);
  544. begin
  545. FIsUnit:=False;
  546. If (AFileName='') then
  547. AFileName:='liba';
  548. FFileName:=AFileName+'.pp';
  549. Add('library '+AFileName+';');
  550. FImplementation:=True;
  551. end;
  552. procedure TTestParser.UsesClause(Units: array of string);
  553. Var
  554. S : String;
  555. I : integer;
  556. begin
  557. S:='';
  558. For I:=Low(units) to High(units) do
  559. begin
  560. If (S<>'') then
  561. S:=S+', ';
  562. S:=S+Units[i];
  563. end;
  564. Add('uses '+S+';');
  565. Add('');
  566. end;
  567. procedure TTestParser.StartImplementation;
  568. begin
  569. if Not FImplementation then
  570. begin
  571. if UseImplementation then
  572. begin
  573. FSource.Insert(0,'');
  574. FSource.Insert(0,'Implementation');
  575. FSource.Insert(0,'');
  576. end
  577. else
  578. begin
  579. Add('');
  580. Add('Implementation');
  581. Add('');
  582. end;
  583. FImplementation:=True;
  584. end;
  585. end;
  586. procedure TTestParser.EndSource;
  587. begin
  588. if Not FEndSource then
  589. begin
  590. Add('end.');
  591. FEndSource:=True;
  592. end;
  593. end;
  594. procedure TTestParser.Add(const ALine: String);
  595. begin
  596. FSource.Add(ALine);
  597. end;
  598. procedure TTestParser.Add(const Lines: array of String);
  599. var
  600. i: Integer;
  601. begin
  602. for i:=Low(Lines) to High(Lines) do
  603. Add(Lines[i]);
  604. end;
  605. procedure TTestParser.StartParsing;
  606. var
  607. i: Integer;
  608. begin
  609. If FIsUnit then
  610. StartImplementation;
  611. EndSource;
  612. If (FFileName='') then
  613. FFileName:=MainFilename;
  614. FResolver.AddStream(FFileName,TStringStream.Create(FSource.Text));
  615. FScanner.OpenFile(FFileName);
  616. Writeln('// Test : ',Self.TestName);
  617. for i:=0 to FSource.Count-1 do
  618. Writeln(Format('%:4d: ',[i+1]),FSource[i]);
  619. end;
  620. procedure TTestParser.ParseDeclarations;
  621. begin
  622. if UseImplementation then
  623. StartImplementation;
  624. FSource.Insert(0,'');
  625. FSource.Insert(0,'interface');
  626. FSource.Insert(0,'');
  627. FSource.Insert(0,'unit afile;');
  628. if Not UseImplementation then
  629. StartImplementation;
  630. EndSource;
  631. ParseModule;
  632. if UseImplementation then
  633. FDeclarations:=Module.ImplementationSection
  634. else
  635. FDeclarations:=Module.InterfaceSection;
  636. end;
  637. procedure TTestParser.ParseModule;
  638. begin
  639. StartParsing;
  640. FParser.ParseMain(FModule);
  641. AssertNotNull('Module resulted in Module',FModule);
  642. AssertEquals('modulename',ChangeFileExt(FFileName,''),Module.Name);
  643. end;
  644. procedure TTestParser.CheckHint(AHint: TPasMemberHint);
  645. begin
  646. HaveHint(AHint,Definition.Hints);
  647. end;
  648. function TTestParser.AssertExpression(const Msg: String; AExpr: TPasExpr;
  649. aKind: TPasExprKind; AClass: TClass): TPasExpr;
  650. begin
  651. AssertNotNull(AExpr);
  652. AssertEquals(Msg+': Correct expression kind',aKind,AExpr.Kind);
  653. AssertEquals(Msg+': Correct expression class',AClass,AExpr.ClassType);
  654. Result:=AExpr;
  655. end;
  656. function TTestParser.AssertExpression(const Msg: String; AExpr: TPasExpr;
  657. aKind: TPasExprKind; AValue: String): TPrimitiveExpr;
  658. begin
  659. Result:=AssertExpression(Msg,AExpr,aKind,TPrimitiveExpr) as TPrimitiveExpr;
  660. AssertEquals(Msg+': Primitive expression value',AValue,TPrimitiveExpr(AExpr).Value);
  661. end;
  662. procedure TTestParser.AssertExportSymbol(const Msg: String; AIndex: Integer;
  663. AName, AExportName: String; AExportIndex: Integer);
  664. Var
  665. E: TPasExportSymbol;
  666. begin
  667. AssertNotNull(Msg+'Have export symbols list',PasLibrary.LibrarySection.ExportSymbols);
  668. if AIndex>=PasLibrary.LibrarySection.ExportSymbols.Count then
  669. Fail(Format(Msg+'%d not a valid export list symbol',[AIndex]));
  670. AssertNotNull(Msg+'Have export symbol',PasLibrary.LibrarySection.ExportSymbols[Aindex]);
  671. AssertEquals(Msg+'Correct export symbol class',TPasExportSymbol,TObject(PasLibrary.LibrarySection.ExportSymbols[Aindex]).ClassType);
  672. E:=TPasExportSymbol(PasLibrary.LibrarySection.ExportSymbols[Aindex]);
  673. AssertEquals(Msg+'Correct export symbol name',AName,E.Name);
  674. if (AExportName='') then
  675. AssertNull(Msg+'No export name',E.ExportName)
  676. else
  677. begin
  678. AssertNotNull(Msg+'Export name symbol',E.ExportName);
  679. AssertEquals(Msg+'TPrimitiveExpr',TPrimitiveExpr,E.ExportName.CLassType);
  680. AssertEquals(Msg+'Correct export symbol export name ',''''+AExportName+'''',TPrimitiveExpr(E.ExportName).Value);
  681. end;
  682. If AExportIndex=-1 then
  683. AssertNull(Msg+'No export name',E.ExportIndex)
  684. else
  685. begin
  686. AssertNotNull(Msg+'Export name symbol',E.ExportIndex);
  687. AssertEquals(Msg+'TPrimitiveExpr',TPrimitiveExpr,E.ExportIndex.CLassType);
  688. AssertEquals(Msg+'Correct export symbol export index',IntToStr(AExportindex),TPrimitiveExpr(E.ExportIndex).Value);
  689. end;
  690. end;
  691. procedure TTestParser.AssertEquals(const Msg: String; AExpected,
  692. AActual: TPasExprKind);
  693. begin
  694. AssertEquals(Msg,GetEnumName(TypeInfo(TPasExprKind),Ord(AExpected)),
  695. GetEnumName(TypeInfo(TPasExprKind),Ord(AActual)));
  696. end;
  697. procedure TTestParser.AssertEquals(const Msg: String; AExpected,
  698. AActual: TLoopType);
  699. begin
  700. AssertEquals(Msg,GetEnumName(TypeInfo(TLoopType),Ord(AExpected)),
  701. GetEnumName(TypeInfo(TLoopType),Ord(AActual)));
  702. end;
  703. procedure TTestParser.AssertEquals(const Msg: String; AExpected,
  704. AActual: TPasObjKind);
  705. begin
  706. AssertEquals(Msg,GetEnumName(TypeInfo(TPasObjKind),Ord(AExpected)),
  707. GetEnumName(TypeInfo(TPasObjKind),Ord(AActual)));
  708. end;
  709. procedure TTestParser.AssertEquals(const Msg: String; AExpected,
  710. AActual: TexprOpcode);
  711. begin
  712. AssertEquals(Msg,GetEnumName(TypeInfo(TexprOpcode),Ord(AExpected)),
  713. GetEnumName(TypeInfo(TexprOpcode),Ord(AActual)));
  714. end;
  715. procedure TTestParser.AssertEquals(const Msg: String; AExpected,
  716. AActual: TPasMemberHint);
  717. begin
  718. AssertEquals(Msg,GetEnumName(TypeInfo(TPasMemberHint),Ord(AExpected)),
  719. GetEnumName(TypeInfo(TPasMemberHint),Ord(AActual)));
  720. end;
  721. procedure TTestParser.AssertEquals(const Msg: String; AExpected,
  722. AActual: TCallingConvention);
  723. begin
  724. AssertEquals(Msg,GetEnumName(TypeInfo(TCallingConvention),Ord(AExpected)),
  725. GetEnumName(TypeInfo(TCallingConvention),Ord(AActual)));
  726. end;
  727. procedure TTestParser.AssertEquals(const Msg: String; AExpected,
  728. AActual: TArgumentAccess);
  729. begin
  730. AssertEquals(Msg,GetEnumName(TypeInfo(TArgumentAccess),Ord(AExpected)),
  731. GetEnumName(TypeInfo(TArgumentAccess),Ord(AActual)));
  732. end;
  733. procedure TTestParser.AssertEquals(const Msg: String; AExpected,
  734. AActual: TVariableModifier);
  735. begin
  736. AssertEquals(Msg,GetEnumName(TypeInfo(TVariableModifier),Ord(AExpected)),
  737. GetEnumName(TypeInfo(TVariableModifier),Ord(AActual)));
  738. end;
  739. procedure TTestParser.AssertEquals(const Msg: String; AExpected,
  740. AActual: TVariableModifiers);
  741. Function sn (S : TVariableModifiers) : string;
  742. Var
  743. M : TVariableModifier;
  744. begin
  745. Result:='';
  746. For M:=Low(TVariableModifier) to High(TVariableModifier) do
  747. if M in S then
  748. begin
  749. if (Result<>'') then
  750. Result:=Result+',';
  751. end;
  752. Result:='['+Result+']';
  753. end;
  754. begin
  755. AssertEquals(Msg,Sn(AExpected),Sn(AActual));
  756. end;
  757. procedure TTestParser.AssertEquals(const Msg: String; AExpected,
  758. AActual: TPasMemberVisibility);
  759. begin
  760. AssertEquals(Msg,GetEnumName(TypeInfo(TPasMemberVisibility),Ord(AExpected)),
  761. GetEnumName(TypeInfo(TPasMemberVisibility),Ord(AActual)));
  762. end;
  763. procedure TTestParser.AssertEquals(const Msg: String; AExpected,
  764. AActual: TProcedureModifier);
  765. begin
  766. AssertEquals(Msg,GetEnumName(TypeInfo(TProcedureModifier),Ord(AExpected)),
  767. GetEnumName(TypeInfo(TProcedureModifier),Ord(AActual)));
  768. end;
  769. procedure TTestParser.AssertEquals(const Msg: String; AExpected,
  770. AActual: TProcedureModifiers);
  771. Function Sn (S : TProcedureModifiers) : String;
  772. Var
  773. m : TProcedureModifier;
  774. begin
  775. Result:='';
  776. For M:=Low(TProcedureModifier) to High(TProcedureModifier) do
  777. If (m in S) then
  778. begin
  779. If (Result<>'') then
  780. Result:=Result+',';
  781. Result:=Result+GetEnumName(TypeInfo(TProcedureModifier),Ord(m))
  782. end;
  783. end;
  784. begin
  785. AssertEquals(Msg,Sn(AExpected),SN(AActual));
  786. end;
  787. procedure TTestParser.AssertEquals(const Msg: String; AExpected,
  788. AActual: TProcTypeModifiers);
  789. Function Sn (S : TProcTypeModifiers) : String;
  790. Var
  791. m : TProcTypeModifier;
  792. begin
  793. Result:='';
  794. For M:=Low(TProcTypeModifier) to High(TProcTypeModifier) do
  795. If (m in S) then
  796. begin
  797. If (Result<>'') then
  798. Result:=Result+',';
  799. Result:=Result+GetEnumName(TypeInfo(TProcTypeModifier),Ord(m))
  800. end;
  801. end;
  802. begin
  803. AssertEquals(Msg,Sn(AExpected),SN(AActual));
  804. end;
  805. procedure TTestParser.AssertEquals(const Msg: String; AExpected,
  806. AActual: TAssignKind);
  807. begin
  808. AssertEquals(Msg,GetEnumName(TypeInfo(TAssignKind),Ord(AExpected)),
  809. GetEnumName(TypeInfo(TAssignKind),Ord(AActual)));
  810. end;
  811. procedure TTestParser.AssertEquals(const Msg: String; AExpected,
  812. AActual: TProcedureMessageType);
  813. begin
  814. AssertEquals(Msg,GetEnumName(TypeInfo(TProcedureMessageType),Ord(AExpected)),
  815. GetEnumName(TypeInfo(TProcedureMessageType),Ord(AActual)));
  816. end;
  817. procedure TTestParser.AssertEquals(const Msg: String; AExpected,
  818. AActual: TOperatorType);
  819. begin
  820. AssertEquals(Msg,GetEnumName(TypeInfo(TOperatorType),Ord(AExpected)),
  821. GetEnumName(TypeInfo(TOperatorType),Ord(AActual)));
  822. end;
  823. procedure TTestParser.AssertSame(const Msg: String; AExpected,
  824. AActual: TPasElement);
  825. begin
  826. if AExpected=AActual then exit;
  827. AssertEquals(Msg,GetPasElementDesc(AExpected),GetPasElementDesc(AActual));
  828. end;
  829. procedure TTestParser.HaveHint(AHint: TPasMemberHint; AHints: TPasMemberHints);
  830. begin
  831. If not (AHint in AHints) then
  832. Fail(GetEnumName(TypeInfo(TPasMemberHint),Ord(AHint))+'hint expected.');
  833. end;
  834. end.