test_parser.pp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. cmdl : string;
  35. begin
  36. if Paramcount<1 then
  37. begin
  38. // remember to put the whole cmdline in quotes, and
  39. // to always add some path options. Even if only -Fu. -Fi.
  40. writeln('usage: test_parser <commandline>');
  41. halt;
  42. end;
  43. cmdl:=paramstr(1);
  44. if paramcount>1 then
  45. for i:=2 to paramcount do
  46. cmdl:=cmdl+' '+paramstr(i);
  47. E := TSimpleEngine.Create;
  48. try
  49. M := ParseSource(E, cmdl , 'linux', 'i386');
  50. { Cool, we successfully parsed the unit.
  51. Now output some info about it. }
  52. Decls := M.InterfaceSection.Declarations;
  53. for I := 0 to Decls.Count - 1 do
  54. Writeln('Interface item ', I, ': ', (TObject(Decls[I]) as TPasElement).Name);
  55. FreeAndNil(M);
  56. finally
  57. FreeAndNil(E)
  58. end;
  59. end.