test_parser.pp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. {$mode objfpc}{$H+}
  2. uses SysUtils, Classes, PParser, PasTree;
  3. type
  4. { We have to override abstract TPasTreeContainer methods.
  5. See utils/fpdoc/dglobals.pp for an implementation of TFPDocEngine,
  6. a "real" engine. }
  7. TSimpleEngine = class(TPasTreeContainer)
  8. public
  9. function CreateElement(AClass: TPTreeElement; const AName: String;
  10. AParent: TPasElement; AVisibility: TPasMemberVisibility;
  11. const ASourceFilename: String; ASourceLinenumber: Integer): TPasElement;
  12. override;
  13. function FindElement(const AName: String): TPasElement; override;
  14. end;
  15. function TSimpleEngine.CreateElement(AClass: TPTreeElement; const AName: String;
  16. AParent: TPasElement; AVisibility: TPasMemberVisibility;
  17. const ASourceFilename: String; ASourceLinenumber: Integer): TPasElement;
  18. begin
  19. Result := AClass.Create(AName, AParent);
  20. Result.Visibility := AVisibility;
  21. Result.SourceFilename := ASourceFilename;
  22. Result.SourceLinenumber := ASourceLinenumber;
  23. end;
  24. function TSimpleEngine.FindElement(const AName: String): TPasElement;
  25. begin
  26. { dummy implementation, see TFPDocEngine.FindElement for a real example }
  27. Result := nil;
  28. end;
  29. var
  30. M: TPasModule;
  31. E: TPasTreeContainer;
  32. I: Integer;
  33. Decls: TList;
  34. begin
  35. if Paramcount<1 then
  36. begin
  37. // remember to put the whole cmdline in quotes, and
  38. // to always add some path options. Even if only -Fu. -Fi.
  39. writeln('usage: test_parser <commandline>');
  40. halt;
  41. end;
  42. E := TSimpleEngine.Create;
  43. try
  44. M := ParseSource(E, ParamStr(1), 'linux', 'i386');
  45. { Cool, we successfully parsed the unit.
  46. Now output some info about it. }
  47. Decls := M.InterfaceSection.Declarations;
  48. for I := 0 to Decls.Count - 1 do
  49. Writeln('Interface item ', I, ': ', (TObject(Decls[I]) as TPasElement).Name);
  50. FreeAndNil(M);
  51. finally
  52. FreeAndNil(E)
  53. end;
  54. end.