tcvarparser.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. unit tcvarparser;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, fpcunit, pastree, pscanner, pparser,
  6. tcbaseparser, testregistry;
  7. Type
  8. { TTestVarParser }
  9. TTestVarParser = Class(TTestParser)
  10. private
  11. FHint: string;
  12. FIsThreadVar: Boolean;
  13. FVar: TPasVariable;
  14. Protected
  15. Function ParseVar(ASource : String; Const AHint : String = '') : TPasVariable; virtual; overload;
  16. Procedure AssertVariableType(Const ATypeName : String);
  17. Procedure AssertVariableType(Const AClass : TClass);
  18. Procedure AssertParseVarError(ASource : String);
  19. Property IsThreadVar : Boolean Read FIsThreadVar Write FIsThreadVar;
  20. Property TheVar : TPasVariable Read FVar;
  21. Property Hint : string Read FHint Write FHint;
  22. procedure SetUp; override;
  23. Procedure TearDown; override;
  24. Published
  25. Procedure TestSimpleVar;
  26. Procedure TestSimpleThreadVar;
  27. Procedure TestSimpleVarAbsoluteName;
  28. Procedure TestSimpleVarHelperName;
  29. procedure TestSimpleVarHelperType;
  30. Procedure TestSimpleVarDeprecated;
  31. Procedure TestSimpleVarPlatform;
  32. Procedure TestSimpleVarInitialized;
  33. procedure TestSimpleVarInitializedDeprecated;
  34. procedure TestSimpleVarInitializedPlatform;
  35. Procedure TestSimpleVarAbsolute;
  36. Procedure TestSimpleVarAbsoluteAddress;
  37. Procedure TestSimpleVarAbsoluteDot;
  38. Procedure TestSimpleVarAbsolute2Dots;
  39. Procedure TestVarProcedure;
  40. procedure TestVarProcedureCdecl;
  41. procedure TestVarFunctionFar;
  42. Procedure TestVarFunctionINitialized;
  43. Procedure TestVarProcedureDeprecated;
  44. Procedure TestVarRecord;
  45. Procedure TestVarRecordDeprecated;
  46. Procedure TestVarRecordPlatform;
  47. Procedure TestVarArray;
  48. Procedure TestVarArrayDeprecated;
  49. Procedure TestVarDynArray;
  50. Procedure TestVarExternal;
  51. Procedure TestVarExternalLib;
  52. Procedure TestVarExternalLibName;
  53. procedure TestVarExternalNoSemiColon;
  54. procedure TestVarExternalLibNoName;
  55. Procedure TestVarCVar;
  56. Procedure TestVarCVarExternal;
  57. Procedure TestVarCVarExport;
  58. Procedure TestVarPublic;
  59. Procedure TestVarPublicName;
  60. Procedure TestVarDeprecatedExternalName;
  61. Procedure TestVarHintPriorToInit;
  62. Procedure TestVarAttribute;
  63. Procedure TestErrorRecovery;
  64. end;
  65. implementation
  66. uses typinfo;
  67. { TTestVarParser }
  68. function TTestVarParser.ParseVar(ASource: String; const AHint: String
  69. ): TPasVariable;
  70. Var
  71. D : String;
  72. begin
  73. Hint:=AHint;
  74. if not IsThreadVar then
  75. Add('Var')
  76. else
  77. Add('Threadvar');
  78. D:='A : '+ASource;
  79. If Hint<>'' then
  80. D:=D+' '+Hint;
  81. Add(' '+D+';');
  82. // Writeln(source.text);
  83. ParseDeclarations;
  84. AssertEquals('One variable definition',1,Declarations.Variables.Count);
  85. AssertEquals('First declaration is type definition.',TPasVariable,TObject(Declarations.Variables[0]).ClassType);
  86. Result:=TPasVariable(Declarations.Variables[0]);
  87. AssertEquals('First declaration has correct name.','A',Result.Name);
  88. FVar:=Result;
  89. Definition:=Result;
  90. if (Hint<>'') then
  91. CheckHint(TPasMemberHint(Getenumvalue(typeinfo(TPasMemberHint),'h'+Hint)));
  92. end;
  93. procedure TTestVarParser.AssertVariableType(const ATypeName: String);
  94. begin
  95. AssertVariableType(TPasUnresolvedTypeRef);
  96. AssertEquals('Correct unresolved type name',ATypeName,theVar.VarType.Name);
  97. end;
  98. procedure TTestVarParser.AssertVariableType(const AClass: TClass);
  99. begin
  100. AssertNotNull('Have variable type',theVar.VarType);
  101. AssertEquals('Correct type class',AClass,theVar.VarType.ClassType);
  102. end;
  103. procedure TTestVarParser.AssertParseVarError(ASource: String);
  104. begin
  105. try
  106. ParseVar(ASource,'');
  107. Fail('Expected parser error');
  108. except
  109. // all OK.
  110. end;
  111. end;
  112. procedure TTestVarParser.SetUp;
  113. begin
  114. inherited SetUp;
  115. FHint:='';
  116. FVar:=Nil;
  117. end;
  118. procedure TTestVarParser.TearDown;
  119. begin
  120. FVar:=Nil;
  121. inherited TearDown;
  122. end;
  123. procedure TTestVarParser.TestSimpleVar;
  124. begin
  125. ParseVar('b','');
  126. AssertVariableType('b');
  127. end;
  128. procedure TTestVarParser.TestSimpleThreadVar;
  129. begin
  130. IsThreadVar:=True;
  131. ParseVar('b','');
  132. AssertVariableType('b');
  133. end;
  134. procedure TTestVarParser.TestSimpleVarAbsoluteName;
  135. Var
  136. R : TPasVariable;
  137. begin
  138. Add('Var');
  139. Add(' Absolute : integer;');
  140. // Writeln(source.text);
  141. ParseDeclarations;
  142. AssertEquals('One variable definition',1,Declarations.Variables.Count);
  143. AssertEquals('First declaration is type definition.',TPasVariable,TObject(Declarations.Variables[0]).ClassType);
  144. R:=TPasVariable(Declarations.Variables[0]);
  145. AssertEquals('First declaration has correct name.','Absolute',R.Name);
  146. end;
  147. procedure TTestVarParser.TestSimpleVarHelperName;
  148. Var
  149. R : TPasVariable;
  150. begin
  151. Add('Var');
  152. Add(' Helper : integer;');
  153. // Writeln(source.text);
  154. ParseDeclarations;
  155. AssertEquals('One variable definition',1,Declarations.Variables.Count);
  156. AssertEquals('First declaration is type definition.',TPasVariable,TObject(Declarations.Variables[0]).ClassType);
  157. R:=TPasVariable(Declarations.Variables[0]);
  158. AssertEquals('First declaration has correct name.','Helper',R.Name);
  159. end;
  160. procedure TTestVarParser.TestSimpleVarHelperType;
  161. begin
  162. ParseVar('helper','');
  163. AssertVariableType('helper');
  164. end;
  165. procedure TTestVarParser.TestSimpleVarDeprecated;
  166. begin
  167. ParseVar('b','deprecated');
  168. AssertVariableType('b');
  169. end;
  170. procedure TTestVarParser.TestSimpleVarPlatform;
  171. begin
  172. ParseVar('b','platform');
  173. AssertVariableType('b');
  174. end;
  175. procedure TTestVarParser.TestSimpleVarInitialized;
  176. begin
  177. ParseVar('b = 123','');
  178. AssertVariableType('b');
  179. AssertNotNull(TheVar.expr);
  180. AssertExpression('Variable value',TheVar.expr,pekNumber,'123');
  181. end;
  182. procedure TTestVarParser.TestSimpleVarInitializedDeprecated;
  183. begin
  184. ParseVar('b = 123','deprecated');
  185. AssertVariableType('b');
  186. AssertNotNull(TheVar.expr);
  187. AssertExpression('Variable value',TheVar.expr,pekNumber,'123');
  188. end;
  189. procedure TTestVarParser.TestSimpleVarInitializedPlatform;
  190. begin
  191. ParseVar('b = 123','platform');
  192. AssertVariableType('b');
  193. AssertNotNull(TheVar.expr);
  194. AssertExpression('Variable value',TheVar.expr,pekNumber,'123');
  195. end;
  196. procedure TTestVarParser.TestSimpleVarAbsolute;
  197. begin
  198. ParseVar('q absolute v','');
  199. AssertVariableType('q');
  200. AssertExpression('correct absolute location',TheVar.AbsoluteExpr,pekIdent,'v');
  201. end;
  202. procedure TTestVarParser.TestSimpleVarAbsoluteAddress;
  203. begin
  204. ParseVar('q absolute $123','');
  205. AssertVariableType('q');
  206. AssertExpression('correct absolute location',TheVar.AbsoluteExpr,pekNumber,'$123');
  207. end;
  208. procedure TTestVarParser.TestSimpleVarAbsoluteDot;
  209. var
  210. B: TBinaryExpr;
  211. begin
  212. ParseVar('q absolute v.w','');
  213. AssertVariableType('q');
  214. B:=AssertExpression('binary',TheVar.AbsoluteExpr,eopSubIdent);
  215. AssertExpression('correct absolute expr v',B.Left,pekIdent,'v');
  216. AssertExpression('correct absolute expr w',B.Right,pekIdent,'w');
  217. end;
  218. procedure TTestVarParser.TestSimpleVarAbsolute2Dots;
  219. var
  220. B: TBinaryExpr;
  221. begin
  222. ParseVar('q absolute v.w.x','');
  223. AssertVariableType('q');
  224. B:=AssertExpression('binary',TheVar.AbsoluteExpr,eopSubIdent);
  225. AssertExpression('correct absolute expr x',B.Right,pekIdent,'x');
  226. B:=AssertExpression('binary',B.Left,eopSubIdent);
  227. AssertExpression('correct absolute expr w',B.Right,pekIdent,'w');
  228. AssertExpression('correct absolute expr v',B.Left,pekIdent,'v');
  229. end;
  230. procedure TTestVarParser.TestVarProcedure;
  231. begin
  232. ParseVar('procedure','');
  233. AssertVariableType(TPasProcedureType);
  234. end;
  235. procedure TTestVarParser.TestVarProcedureCdecl;
  236. begin
  237. ParseVar('procedure; cdecl;','');
  238. AssertVariableType(TPasProcedureType);
  239. end;
  240. procedure TTestVarParser.TestVarFunctionFar;
  241. begin
  242. ParseVar('function (cinfo : j_decompress_ptr) : int; far;','');
  243. AssertVariableType(TPasFunctionType);
  244. end;
  245. procedure TTestVarParser.TestVarFunctionINitialized;
  246. begin
  247. ParseVar('function (device: pointer): pointer; cdecl = nil','');
  248. AssertVariableType(TPasFunctionType);
  249. end;
  250. procedure TTestVarParser.TestVarProcedureDeprecated;
  251. begin
  252. ParseVar('procedure','deprecated');
  253. AssertVariableType(TPasProcedureType);
  254. end;
  255. procedure TTestVarParser.TestVarRecord;
  256. Var
  257. R : TPasRecordtype;
  258. begin
  259. ParseVar('record x,y : intger; end','');
  260. AssertVariableType(TPasRecordType);
  261. R:=TheVar.VarType as TPasRecordType;
  262. AssertEquals('Correct number of fields',2,R.Members.Count);
  263. end;
  264. procedure TTestVarParser.TestVarRecordDeprecated;
  265. Var
  266. R : TPasRecordtype;
  267. begin
  268. ParseVar('record x,y : integer; end','deprecated');
  269. AssertVariableType(TPasRecordType);
  270. R:=TheVar.VarType as TPasRecordType;
  271. AssertEquals('Correct number of fields',2,R.Members.Count);
  272. end;
  273. procedure TTestVarParser.TestVarRecordPlatform;
  274. Var
  275. R : TPasRecordtype;
  276. begin
  277. ParseVar('record x,y : integer; end','platform');
  278. AssertVariableType(TPasRecordType);
  279. R:=TheVar.VarType as TPasRecordType;
  280. AssertEquals('Correct number of fields',2,R.Members.Count);
  281. end;
  282. procedure TTestVarParser.TestVarArray;
  283. Var
  284. R : TPasArrayType;
  285. begin
  286. ParseVar('Array[1..20] of integer','');
  287. AssertVariableType(TPasArrayType);
  288. R:=TheVar.VarType as TPasArrayType;
  289. AssertNotNull('Correct array type name',R.ElType);
  290. AssertEquals('Correct array type name',TPasunresolvedTypeRef,R.ElType.ClassType);
  291. end;
  292. procedure TTestVarParser.TestVarArrayDeprecated;
  293. Var
  294. R : TPasArrayType;
  295. begin
  296. ParseVar('Array[1..20] of integer','Deprecated');
  297. AssertVariableType(TPasArrayType);
  298. R:=TheVar.VarType as TPasArrayType;
  299. AssertNotNull('Correct array type name',R.ElType);
  300. AssertEquals('Correct array type name',TPasunresolvedTypeRef,R.ElType.ClassType);
  301. end;
  302. procedure TTestVarParser.TestVarDynArray;
  303. Var
  304. R : TPasArrayType;
  305. begin
  306. ParseVar('Array of integer','');
  307. AssertVariableType(TPasArrayType);
  308. R:=TheVar.VarType as TPasArrayType;
  309. AssertEquals('No index','',R.IndexRange);
  310. AssertNotNull('Correct array type name',R.ElType);
  311. AssertEquals('Correct array type name',TPasunresolvedTypeRef,R.ElType.ClassType);
  312. end;
  313. procedure TTestVarParser.TestVarExternal;
  314. begin
  315. ParseVar('integer; external','');
  316. AssertEquals('Variable modifiers',[vmexternal],TheVar.VarModifiers);
  317. end;
  318. procedure TTestVarParser.TestVarExternalNoSemiColon;
  319. begin
  320. ParseVar('integer external','');
  321. AssertEquals('Variable modifiers',[vmexternal],TheVar.VarModifiers);
  322. end;
  323. procedure TTestVarParser.TestVarExternalLib;
  324. begin
  325. ParseVar('integer; external name ''mylib''','');
  326. AssertEquals('Variable modifiers',[vmexternal],TheVar.VarModifiers);
  327. AssertNull('Library name',TheVar.LibraryName);
  328. AssertNotNull('Library symbol',TheVar.ExportName);
  329. end;
  330. procedure TTestVarParser.TestVarExternalLibNoName;
  331. begin
  332. // Found in e.g.apache headers
  333. ParseVar('integer; external ''mylib''','');
  334. AssertEquals('Variable modifiers',[vmexternal],TheVar.VarModifiers);
  335. AssertNotNull('Library name',TheVar.LibraryName);
  336. end;
  337. procedure TTestVarParser.TestVarExternalLibName;
  338. begin
  339. ParseVar('integer; external ''mylib'' name ''de''','');
  340. AssertEquals('Variable modifiers',[vmexternal],TheVar.VarModifiers);
  341. AssertNotNull('Library name',TheVar.LibraryName);
  342. AssertNotNull('Library symbol',TheVar.ExportName);
  343. end;
  344. procedure TTestVarParser.TestVarCVar;
  345. begin
  346. ParseVar('integer; cvar','');
  347. AssertEquals('Variable modifiers',[vmcvar],TheVar.VarModifiers);
  348. end;
  349. procedure TTestVarParser.TestVarCVarExternal;
  350. begin
  351. ParseVar('integer; cvar;external','');
  352. AssertEquals('Variable modifiers',[vmcvar,vmexternal],TheVar.VarModifiers);
  353. end;
  354. procedure TTestVarParser.TestVarCVarExport;
  355. begin
  356. ParseVar('integer; cvar; export','');
  357. AssertEquals('Variable modifiers',[vmCVar,vmExport],TheVar.VarModifiers);
  358. end;
  359. procedure TTestVarParser.TestVarPublic;
  360. begin
  361. ParseVar('integer; public','');
  362. AssertEquals('Variable modifiers',[vmpublic],TheVar.VarModifiers);
  363. end;
  364. procedure TTestVarParser.TestVarPublicName;
  365. begin
  366. ParseVar('integer; public name ''ce''','');
  367. AssertEquals('Variable modifiers',[vmpublic],TheVar.VarModifiers);
  368. AssertNotNull('Public export name',TheVar.ExportName);
  369. end;
  370. procedure TTestVarParser.TestVarDeprecatedExternalName;
  371. begin
  372. ParseVar('integer deprecated; external name ''me''','');
  373. CheckHint(TPasMemberHint(Getenumvalue(typeinfo(TPasMemberHint),'hdeprecated')));
  374. AssertEquals('Variable modifiers',[vmexternal],TheVar.VarModifiers);
  375. AssertNull('Library name',TheVar.LibraryName);
  376. AssertNotNull('Library symbol',TheVar.ExportName);
  377. end;
  378. procedure TTestVarParser.TestVarHintPriorToInit;
  379. Var
  380. E : TBoolConstExpr;
  381. begin
  382. ParseVar('boolean platform = false','');
  383. CheckHint(TPasMemberHint(Getenumvalue(typeinfo(TPasMemberHint),'hplatform')));
  384. AssertNotNull('Correctly initialized',Thevar.Expr);
  385. AssertEquals('Correctly initialized',TBoolConstExpr,Thevar.Expr.ClassType);
  386. E:=Thevar.Expr as TBoolConstExpr;
  387. AssertEquals('Correct initialization value',False, E.Value);
  388. end;
  389. procedure TTestVarParser.TestVarAttribute;
  390. var
  391. V : TPasVariable;
  392. begin
  393. add('{$mode delphi}');
  394. Add('Var');
  395. Add(' [xyz] A : integer;');
  396. ParseDeclarations;
  397. AssertEquals('One variable definition',1,Declarations.Variables.Count);
  398. AssertEquals('First declaration is type definition.',TPasVariable,TObject(Declarations.Variables[0]).ClassType);
  399. V:=TPasVariable(Declarations.Variables[0]);
  400. AssertEquals('First declaration has correct name.','A',V.Name);
  401. end;
  402. procedure TTestVarParser.TestErrorRecovery;
  403. begin
  404. Add('Var');
  405. Add(' a : integer;');
  406. Add(' a = integer;');
  407. Add(' a : abc integer;');
  408. // Writeln(source.text);
  409. try
  410. Parser.MaxErrorCount:=3;
  411. Parser.OnLog:=@DoParserLog;
  412. ParseDeclarations;
  413. except
  414. On E : Exception do
  415. begin
  416. AssertEquals('Correct class',E.ClassType,EParserError);
  417. end;
  418. end;
  419. AssertErrorCount(2);
  420. end;
  421. initialization
  422. RegisterTests([TTestVarParser]);
  423. end.