parsepp.pp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. { ---------------------------------------------------------------------
  2. This is a simple program to check whether fcl-passrc
  3. ---------------------------------------------------------------------}
  4. program parsepp;
  5. {$mode objfpc}{$H+}
  6. uses SysUtils, Classes, PParser, PasTree;
  7. type
  8. { We have to override abstract TPasTreeContainer methods.
  9. See utils/fpdoc/dglobals.pp for an implementation of TFPDocEngine,
  10. a "real" engine. }
  11. TSimpleEngine = class(TPasTreeContainer)
  12. public
  13. function CreateElement(AClass: TPTreeElement; const AName: String;
  14. AParent: TPasElement; AVisibility: TPasMemberVisibility;
  15. const ASourceFilename: String; ASourceLinenumber: Integer): TPasElement;
  16. override;
  17. function FindElement(const AName: String): TPasElement; override;
  18. end;
  19. function TSimpleEngine.CreateElement(AClass: TPTreeElement; const AName: String;
  20. AParent: TPasElement; AVisibility: TPasMemberVisibility;
  21. const ASourceFilename: String; ASourceLinenumber: Integer): TPasElement;
  22. begin
  23. Result := AClass.Create(AName, AParent);
  24. Result.Visibility := AVisibility;
  25. Result.SourceFilename := ASourceFilename;
  26. Result.SourceLinenumber := ASourceLinenumber;
  27. end;
  28. function TSimpleEngine.FindElement(const AName: String): TPasElement;
  29. begin
  30. { dummy implementation, see TFPDocEngine.FindElement for a real example }
  31. Result := nil;
  32. end;
  33. Procedure Usage;
  34. begin
  35. Writeln('Usage : ',ExtractFileName(Paramstr(0)),' [-h|--help] options ');
  36. Writeln('-h or --help shows this help');
  37. Writeln('All other options are passed as-is to the parser');
  38. Halt(0);
  39. end;
  40. var
  41. M: TPasModule;
  42. E: TPasTreeContainer;
  43. I: Integer;
  44. Decls: TFPList;
  45. cmdline : String;
  46. begin
  47. cmdline:='';
  48. if (ParamCount=0) or (Paramstr(1)='-h') or (Paramstr(1)='--help') then
  49. Usage;
  50. For I:=1 to ParamCount do
  51. CmdLine:=CmdLine+' '+Paramstr(i);
  52. E := TSimpleEngine.Create;
  53. try
  54. M := ParseSource(E, cmdline, 'linux', 'i386');
  55. { Cool, we successfully parsed the module.
  56. Now output some info about it. }
  57. if M.InterfaceSection <> nil then
  58. begin
  59. Decls := M.InterfaceSection.Declarations;
  60. for I := 0 to Decls.Count - 1 do
  61. Writeln('Interface item ', I, ': ' +
  62. (TObject(Decls[I]) as TPasElement).Name);
  63. end else
  64. Writeln('No interface section --- this is not a unit, this is a ', M.ClassName);
  65. if M.ImplementationSection <> nil then // may be nil in case of a simple program
  66. begin
  67. Decls := M.ImplementationSection.Declarations;
  68. for I := 0 to Decls.Count - 1 do
  69. Writeln('Implementation item ', I, ': ' +
  70. (TObject(Decls[I]) as TPasElement).Name);
  71. end;
  72. FreeAndNil(M);
  73. finally FreeAndNil(E) end;
  74. end.