test_parser.pp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. try
  50. M := ParseSource(E, cmdl , 'linux', 'i386');
  51. except
  52. on excep:EParserError do
  53. begin
  54. writeln(excep.message,' line:',excep.row,' column:',excep.column,' file:',excep.filename);
  55. raise;
  56. end;
  57. end;
  58. { Cool, we successfully parsed the unit.
  59. Now output some info about it. }
  60. Decls := M.InterfaceSection.Declarations;
  61. for I := 0 to Decls.Count - 1 do
  62. Writeln('Interface item ', I, ': ', (TObject(Decls[I]) as TPasElement).Name);
  63. FreeAndNil(M);
  64. finally
  65. FreeAndNil(E)
  66. end;
  67. end.