parsepp.pp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. Writeln(AName,' : ',AClass.ClassName,' at ',ASourceFilename,':',ASourceLinenumber);
  24. Result := AClass.Create(AName, AParent);
  25. Result.Visibility := AVisibility;
  26. Result.SourceFilename := ASourceFilename;
  27. Result.SourceLinenumber := ASourceLinenumber;
  28. end;
  29. function TSimpleEngine.FindElement(const AName: String): TPasElement;
  30. begin
  31. { dummy implementation, see TFPDocEngine.FindElement for a real example }
  32. Result := nil;
  33. end;
  34. Procedure Usage;
  35. begin
  36. Writeln('Usage : ',ExtractFileName(Paramstr(0)),' [-h|--help] options ');
  37. Writeln('-h or --help shows this help');
  38. Writeln('All other options are passed as-is to the parser');
  39. Halt(0);
  40. end;
  41. var
  42. M: TPasModule;
  43. E: TPasTreeContainer;
  44. I: Integer;
  45. Decls: TFPList;
  46. cmdline : String;
  47. begin
  48. cmdline:='';
  49. if (ParamCount=0) or (Paramstr(1)='-h') or (Paramstr(1)='--help') then
  50. Usage;
  51. For I:=1 to ParamCount do
  52. CmdLine:=CmdLine+' '+Paramstr(i);
  53. E := TSimpleEngine.Create;
  54. M := nil;
  55. try
  56. M := ParseSource(E, cmdline, 'linux', 'i386');
  57. { Cool, we successfully parsed the module.
  58. Now output some info about it. }
  59. if M.InterfaceSection <> nil then
  60. begin
  61. Decls := M.InterfaceSection.Declarations;
  62. for I := 0 to Decls.Count - 1 do
  63. Writeln('Interface item ', I, ': ' +
  64. (TObject(Decls[I]) as TPasElement).Name);
  65. end else
  66. Writeln('No interface section --- this is not a unit, this is a ', M.ClassName);
  67. if M.ImplementationSection <> nil then // may be nil in case of a simple program
  68. begin
  69. Decls := M.ImplementationSection.Declarations;
  70. for I := 0 to Decls.Count - 1 do
  71. Writeln('Implementation item ', I, ': ' +
  72. (TObject(Decls[I]) as TPasElement).Name);
  73. end;
  74. finally
  75. FreeAndNil(M);
  76. FreeAndNil(E)
  77. end;
  78. end.