testunit1.pp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. //This is only for testing the parser, it is not intended to be runable in a real
  2. //program but for checking the contructs to be parsed well.
  3. //All statements are written like testparser would print them out to diff the
  4. //result with this file again to show differences.
  5. //Based on /utils/fpdoc/testunit.pp
  6. {$mode objfpc}
  7. {$h+}
  8. unit testunit1;
  9. interface
  10. uses
  11. SysUtils,Classes;
  12. const
  13. AnIntegerConst=1;
  14. AStringConst='Hello, World!';
  15. AFLoatconst=1.23;
  16. ABooleanConst=True;
  17. ATypedConst: Integer=3;
  18. AnArrayConst: Array[1..3] of Integer=(1,2,3);
  19. ARecordConst: TMethod=(Code:nil;Data:nil);
  20. ASetConst=[true,false];
  21. ADeprecatedConst=1 deprecated;
  22. Type
  23. TAnEnumType=(one,two,three);
  24. TASetType=set of TAnEnumType;
  25. TAnArrayType=Array[1..10] of Integer;
  26. TASubRangeType=one..two;
  27. TABooleanArrayType=Array[Boolean] of Integer;
  28. TARecordType=record
  29. X,Y: Integer;
  30. Z: String;
  31. end;
  32. TAVariantRecordType=record
  33. A: String;
  34. Case Integer of
  35. 1: (X,Y : Integer);
  36. 2: (phi,Omega : Real);
  37. end;
  38. TAVariantRecordType2=record
  39. A: String;
  40. Case Atype : Integer of
  41. 1 : (X,Y : Integer);
  42. 2 : (phi,Omega : Real);
  43. end;
  44. MyRec = Record
  45. X : Longint;
  46. Case byte of
  47. 2 : (Y : Longint;
  48. case byte of
  49. 3 : (Z : Longint);
  50. );
  51. end;
  52. // TADeprecatedType = Integer deprecated;
  53. { TMyParentClass }
  54. TMyParentClass=Class(TComponent)
  55. Private
  56. FI: Integer;
  57. Function GetA(AIndex: Integer): String;
  58. Function GetIP(AIndex: integer): String;
  59. procedure SetA(AIndex: Integer; const AValue: String);
  60. procedure SetIP(AIndex: integer; const AValue: String);
  61. Procedure WriteI(AI: Integer);
  62. Function ReadI: Integer;
  63. Protected
  64. Procedure AProtectedMethod;
  65. Property AProtectedProp: Integer Read FI Write FI;
  66. Public
  67. Constructor Create(AOwner: TComponent); override;
  68. Destructor Destroy; override;
  69. Procedure AVirtualProc; virtual;
  70. Procedure AnAbstractProc; virtual; abstract;
  71. Procedure AMessageProc(var Msg);message 123;
  72. Procedure AStringMessageProc(var Msg);message '123';
  73. Procedure ADeprecatedProc; deprecated;
  74. Procedure APlatformProc; Platform;
  75. Property IntProp: Integer Read FI Write Fi;
  76. Property IntROProp: Integer Read FI;
  77. Property GetIntProp: Integer Read ReadI Write WriteI;
  78. Property AnArrayProp[AIndex: Integer]: String Read GetA Write SetA;
  79. Property AnIndexProp: String Index 1 Read GetIP Write SetIP;
  80. Property AnIndexProp2: String Index 2 Read GetIP Write SetIP;
  81. Published
  82. Procedure SomePublishedMethod;
  83. end;
  84. { TMyChildClass }
  85. TMyChildClass=Class(TMyParentClass)
  86. Public
  87. Procedure AVirtualProc; Override;
  88. Procedure AnAbstractProc; Override;
  89. Published
  90. Property AProtectedProp;
  91. end;
  92. TPasFunctionType=Class(TPasProcedureType)
  93. public
  94. destructor Destroy; override;
  95. Class Function TypeName: string; override;
  96. Function ElementTypeName: string; override;
  97. Function GetDeclaration(Full: boolean): string; override;
  98. public
  99. ResultEl: TPasResultElement;
  100. end;
  101. var
  102. ASimpleVar: Integer;
  103. ATypedVar: TMethod;
  104. ARecordVar: Record
  105. A,B: Integer;
  106. end;
  107. AnArrayVar: Array[1..10] of Integer;
  108. ATypedArray: Array[TanEnumType] of Integer;
  109. AInitVar: Integer=1;
  110. ADeprecatedVar: Integer deprecated;
  111. ACVarVar: Integer ; cvar;
  112. AnExternalVar: Integer ;external name 'avar';
  113. AnExternalLibVar: Integer ;external 'library' name 'avar';
  114. Procedure SimpleProc;
  115. Procedure OverloadedProc(A: Integer);
  116. Procedure OverloadedProc(B: String);
  117. Function SimpleFunc: Integer;
  118. Function OverloadedFunc(A: Integer): Integer;
  119. Function OverloadedFunc(B: String): Integer;
  120. Procedure ConstArgProc(const A: Integer);
  121. Procedure VarArgProc(var A: Integer);
  122. Procedure OutArgProc(out A: Integer);
  123. Procedure UntypedVarArgProc(var A);
  124. Procedure UntypedConstArgProc(const A);
  125. Procedure UntypedOutArgProc(out A);
  126. Procedure ArrayArgProc(A: TAnArrayType);
  127. Procedure OpenArrayArgProc(A: Array of string);
  128. Procedure ConstArrayArgProc(A: Array of const);
  129. Procedure externalproc; external;
  130. Procedure externalnameProc; external name 'aname';
  131. Procedure externallibnameProc; external 'alibrary' name 'aname';
  132. Implementation
  133. Procedure SimpleProc;
  134. procedure SubProc;
  135. begin
  136. s:= s+'a';
  137. end;
  138. begin
  139. a:= 1;
  140. c:= a+b;
  141. for i:= 1 to 10 do
  142. write(a);
  143. end;
  144. Procedure OverloadedProc(A: Integer);
  145. begin
  146. if i=1 then ;
  147. end;
  148. Procedure OverloadedProc(B: String);
  149. begin
  150. end;
  151. Function SimpleFunc: Integer;
  152. begin
  153. end;
  154. Function OverloadedFunc(A: Integer): Integer;
  155. begin
  156. end;
  157. Function OverloadedFunc(B: String): Integer;
  158. begin
  159. end;
  160. Procedure ArrayArgProc(A: TAnArrayType);
  161. begin
  162. end;
  163. Procedure OpenArrayArgProc(A: Array of String);
  164. begin
  165. end;
  166. Procedure ConstArrayArgProc(A: Array of const);
  167. begin
  168. end;
  169. Procedure ConstArgProc(const A: Integer);
  170. begin
  171. end;
  172. Procedure VarArgProc(var A: Integer);
  173. begin
  174. end;
  175. Procedure OutArgProc(out A: Integer);
  176. begin
  177. end;
  178. Procedure UntypedVarArgProc(var A);
  179. begin
  180. end;
  181. Procedure UntypedConstArgProc(const A);
  182. begin
  183. end;
  184. Procedure UntypedOutArgProc(out A);
  185. begin
  186. end;
  187. { TMyChildClass }
  188. procedure TMyChildClass.AVirtualProc;
  189. begin
  190. inherited AVirtualProc;
  191. end;
  192. procedure TMyChildClass.AnAbstractProc;
  193. procedure SubCProc;
  194. begin
  195. sc:= sc+'ac';
  196. end;
  197. begin
  198. // Cannot call ancestor
  199. end;
  200. { TMyParentClass }
  201. procedure TMyParentClass.WriteI(AI: Integer);
  202. begin
  203. end;
  204. Function TMyParentClass.GetA(AIndex: Integer): String;
  205. begin
  206. end;
  207. Function TMyParentClass.GetIP(AIndex: integer): String;
  208. begin
  209. end;
  210. procedure TMyParentClass.SetA(AIndex: Integer; const AValue: String);
  211. begin
  212. end;
  213. procedure TMyParentClass.SetIP(AIndex: integer; const AValue: String);
  214. begin
  215. end;
  216. Function TMyParentClass.ReadI: Integer;
  217. begin
  218. end;
  219. procedure TMyParentClass.AProtectedMethod;
  220. begin
  221. end;
  222. constructor TMyParentClass.Create(AOwner: TComponent);
  223. begin
  224. inherited Create(AOwner);
  225. end;
  226. destructor TMyParentClass.Destroy;
  227. begin
  228. inherited Destroy;
  229. end;
  230. procedure TMyParentClass.AVirtualProc;
  231. begin
  232. end;
  233. procedure TMyParentClass.AMessageProc(var Msg);
  234. begin
  235. end;
  236. procedure TMyParentClass.AStringMessageProc(var Msg);
  237. begin
  238. end;
  239. procedure TMyParentClass.ADeprecatedProc;
  240. begin
  241. end;
  242. procedure TMyParentClass.APlatformProc;
  243. begin
  244. end;
  245. procedure TMyParentClass.SomePublishedMethod;
  246. begin
  247. end;
  248. Class Function TPasFunctionType.TypeName: String;
  249. begin
  250. Result:= 'Function';
  251. end;
  252. procedure Statements;
  253. const
  254. cint=1;
  255. cint1=-1;
  256. creal=3.1415;
  257. Addi=1+2;
  258. Subs=2-3;
  259. Muti=3*3;
  260. Divi=3/5;
  261. //Powe=2^3;
  262. Modu=5 mod 3;
  263. IDiv=5 div 3;
  264. fals= not TRUE;
  265. cand=true and false;
  266. cor=true or false;
  267. cxor=true xor false;
  268. lt=2<3;
  269. gt=3>2;
  270. let=2<=3;
  271. get=3>=2;
  272. LeftShift=2 shl 3;
  273. RightShift=2 shr 3;
  274. ConstString='01'+'ab';
  275. Type
  276. Passenger=Record
  277. Name: String[30];
  278. Flight: String[10];
  279. end;
  280. Type
  281. AR=record
  282. X,Y: LongInt;
  283. end;
  284. //PAR = Record;
  285. var
  286. TheCustomer: Passenger;
  287. L: ^LongInt;
  288. P: PPChar;
  289. S,T: Ar;
  290. begin
  291. X:= X+Y;
  292. //EparserError on C++ style
  293. //X+=Y; { Same as X := X+Y, needs -Sc command line switch}
  294. //x-=y;
  295. //X/=2; { Same as X := X/2, needs -Sc command line switch}
  296. //x*=y;
  297. Done:= False;
  298. Weather:= Good;
  299. //MyPi := 4* Tan(1); warum * ?
  300. L^:= 3;
  301. P^^:= 'A';
  302. Usage;
  303. WriteLn('Pascal is an easy language !');
  304. Doit();
  305. //label jumpto;
  306. //Jumpto :
  307. // Statement;
  308. //Goto jumpto;
  309. Case i of
  310. 3: DoSomething;
  311. 1..5: DoSomethingElse;
  312. end;
  313. Case C of
  314. 'a': WriteLn('A pressed');
  315. 'b': WriteLn('B pressed');
  316. 'c': WriteLn('C pressed');
  317. else
  318. WriteLn('unknown letter pressed : ',C);
  319. end;
  320. Case C of
  321. 'a','e','i','o','u': WriteLn('vowel pressed');
  322. 'y': WriteLn('This one depends on the language');
  323. else
  324. WriteLn('Consonant pressed');
  325. end;
  326. Case Number of
  327. 1..10: WriteLn('Small number');
  328. 11..100: WriteLn('Normal, medium number');
  329. else
  330. WriteLn('HUGE number');
  331. end;
  332. case block of
  333. 1: begin
  334. writeln('1');
  335. end;
  336. 2: writeln('2');
  337. else
  338. writeln('3');
  339. writeln('4');
  340. end;
  341. If exp1 Then
  342. If exp2 then
  343. Stat1
  344. else
  345. stat2;
  346. If exp3 Then
  347. begin
  348. If exp4 then
  349. Stat5
  350. else
  351. stat6
  352. end;
  353. If exp7 Then
  354. begin
  355. If exp8 then
  356. Stat9
  357. end
  358. else
  359. stat2;
  360. if i is integer then
  361. begin
  362. write('integer');
  363. end
  364. else
  365. if i is real then
  366. begin
  367. write('real');
  368. end
  369. else
  370. write('0');
  371. if Today in[Monday..Friday] then
  372. WriteLn('Must work harder')
  373. else
  374. WriteLn('Take a day off.');
  375. for Day:= Monday to Friday do
  376. Work;
  377. for I:= 100 downto 1 do
  378. WriteLn('Counting down : ',i);
  379. for I:= 1 to 7*dwarfs do
  380. KissDwarf(i);
  381. for i:= 0 to 10 do
  382. begin
  383. j:= 2+1;
  384. write(i,j);
  385. end;
  386. repeat
  387. WriteLn('I =',i);
  388. I:= I+2;
  389. until I>100;
  390. repeat
  391. X:= X/2;
  392. until x<10e-3;
  393. I:= I+2;
  394. while i<=100 do
  395. begin
  396. WriteLn('I =',i);
  397. I:= I+2;
  398. end;
  399. X:= X/2;
  400. while x>=10e-3 do
  401. dec(x);
  402. while x>0 do
  403. while y>0 do
  404. begin
  405. dec(x);
  406. dec(y);
  407. end;
  408. while x>0 do
  409. if x>2 then
  410. dec(x)
  411. else
  412. dec(x,2);
  413. X:= 2+3;
  414. TheCustomer.Name:= 'Michael';
  415. TheCustomer.Flight:= 'PS901';
  416. With TheCustomer do
  417. begin
  418. Name:= 'Michael';
  419. Flight:= 'PS901';
  420. end;
  421. With A,B,C,D do
  422. Statement;
  423. With A do
  424. With B do
  425. With C do
  426. With D do
  427. Statement;
  428. S.X:= 1;S.Y:= 1;
  429. T.X:= 2;T.Y:= 2;
  430. With S,T do
  431. WriteLn(X,' ',Y);
  432. {asm
  433. Movl $1,%ebx
  434. Movl $0,%eax
  435. addl %eax,%ebx
  436. end; ['EAX','EBX'];}
  437. try
  438. try
  439. M:= ParseSource(E,cmdl,'linux','i386');
  440. except
  441. on excep: EParserError do
  442. begin
  443. writeln(excep.message,' line:',excep.row,' column:',excep.column,' file:',excep.filename);
  444. raise ;
  445. end;
  446. end;
  447. Decls:= M.InterfaceSection.Declarations;
  448. for I:= 0 to Decls.Count-1 do
  449. Writeln('Interface item ',I,': ');
  450. FreeAndNil(M);
  451. finally
  452. FreeAndNil(E)
  453. end;
  454. raise EParserError.Create(Format(SParserErrorAtToken, [Msg, CurTokenName]) {$ifdef addlocation}+' ('+inttostr(scanner.currow)+' '+inttostr(scanner.curcolumn)+')'{$endif},Scanner.CurFilename, Scanner.CurRow, Scanner.CurColumn);
  455. // try else
  456. end;
  457. procedure Expression;
  458. begin
  459. A:= a+b *c /(-e+f)*3 div 2 + 4 mod 5 - 2 shl 3 + 3 shr 1 ;
  460. b:= (a and not b) or c xor d;
  461. u:= i<=2 or a<>b or j>=3;
  462. u:= i=1 or a>b or b<a or i<>2;
  463. u:= i in [1..2];
  464. If F=@AddOne Then
  465. WriteLn('Functions are equal');
  466. If F()=Addone then
  467. WriteLn('Functions return same values ');
  468. z:= [today,tomorrow];
  469. z:= [Monday..Friday,Sunday];
  470. z:= [2,3*2,6*2,9*2];
  471. z:= ['A'..'Z','a'..'z','0'..'9'];
  472. x:= Byte('A');
  473. x:= Char(48);
  474. x:= boolean(1);
  475. x:= longint(@Buffer);
  476. x:= Integer('A');
  477. x:= Char(4875);
  478. x:= Word(@Buffer);
  479. B:= Byte(C);
  480. Char(B):= C;
  481. TWordRec(W).L:= $FF;
  482. TWordRec(W).H:= 0;
  483. S:= TObject(P).ClassName;
  484. P:= @MyProc; //warum @ ? fix pparser 769 ?
  485. Dirname:= Dirname+'\';
  486. W:= [mon,tue]+[wed,thu,fri]; // equals [mon,tue,wed,thu,fri]
  487. W:= [mon,tue,wed]-[wed]; // equals [mon,tue]
  488. W:= [mon,tue,wed]*[wed,thu,fri]; // equals [wed] warum * ?
  489. (C as TEdit).Text:= 'Some text';
  490. C:= O as TComponent;
  491. if A is TComponent then ;
  492. If A is B then ;
  493. Inherited ;
  494. Inherited Test;
  495. if true then
  496. Inherited
  497. else
  498. DoNothing;
  499. if true then
  500. Inherited Test
  501. else
  502. DoNothing;
  503. Inherited P:= 3;
  504. Inherited SetP1(3);
  505. Result:= Char(P and $FF);
  506. Result:= Char((Inherited P) and $FF);
  507. Inherited P:= Ord(AValue);
  508. Result:= Inherited InterPretOption(Cmd,Arg);
  509. raise Exception.Create(SErrMultipleSourceFiles);
  510. if Filename<>'' then
  511. raise Exception.Create(SErrMultipleSourceFiles);
  512. if Filename<>'' then
  513. raise Exception.Create(SErrMultipleSourceFiles)
  514. else
  515. Filename:= s;
  516. Self.Write(EscapeText(AText));
  517. TObject.Create(Self);
  518. end;
  519. constructor TPasPackage.Create(const AName: String; AParent: TPasElement);
  520. begin
  521. if (Length(AName)>0)and(AName[1]<>'#') then
  522. Inherited Create('#'+AName,AParent)
  523. else
  524. Inherited Create(AName,AParent);
  525. Modules:= TList.Create;
  526. end;
  527. Function TPascalScanner.FetchToken: TToken;
  528. var
  529. IncludeStackItem: TIncludeStackItem;
  530. begin
  531. while true do
  532. begin
  533. Result:= DoFetchToken;
  534. if FCurToken=tkEOF then
  535. if FIncludeStack.Count>0 then
  536. begin
  537. CurSourceFile.Free;
  538. IncludeStackItem:= TIncludeStackItem(FIncludeStack[FIncludeStack.Count-1]);
  539. FIncludeStack.Delete(FIncludeStack.Count-1);
  540. FCurSourceFile:= IncludeStackItem.SourceFile;
  541. FCurFilename:= IncludeStackItem.Filename;
  542. FCurToken:= IncludeStackItem.Token;
  543. FCurTokenString:= IncludeStackItem.TokenString;
  544. FCurLine:= IncludeStackItem.Line;
  545. FCurRow:= IncludeStackItem.Row;
  546. TokenStr:= IncludeStackItem.TokenStr;
  547. IncludeStackItem.Free;
  548. Result:= FCurToken;
  549. end
  550. else
  551. break
  552. else
  553. if not PPIsSkipping then
  554. break;
  555. end;
  556. end;
  557. Procedure IFS;
  558. begin
  559. if true then
  560. repeat
  561. until false
  562. else
  563. Noting;
  564. end;
  565. Procedure IFS(x: integer); overload;
  566. begin
  567. if true then
  568. case x of
  569. 1: writeln;
  570. 2: write;
  571. else
  572. writeln('#');
  573. end
  574. else
  575. Noting;
  576. end;
  577. Procedure IFS1;
  578. begin
  579. if true then
  580. while true do
  581. Something
  582. else
  583. Noting;
  584. end;
  585. Procedure IFS3;
  586. begin
  587. if true then
  588. if true then
  589. write
  590. else
  591. writeln;
  592. end;
  593. Initialization
  594. hallo:= valid;
  595. end.